dev-box / hooks /session_auto_reset.py
ameobius's picture
Upload hooks/session_auto_reset.py with huggingface_hub
c2b0d2a verified
Raw History Blame Contribute Delete
1.18 kB
#!/usr/bin/env python3
"""Auto-reset channel sessions with >50 messages. Run via picoclaw cron."""
import os, json, shutil
sess = os.path.expanduser("~/.picoclaw/workspace/sessions")
backup = sess + "_backup"
os.makedirs(backup, exist_ok=True)
reset = []
if os.path.exists(sess):
for f in os.listdir(sess):
if not f.endswith('.meta.json'):
continue
full = os.path.join(sess, f)
try:
with open(full) as fh:
meta = json.load(fh)
count = meta.get("count", 0)
chat = meta.get("scope", {}).get("values", {}).get("chat", "")
if count > 50 and "channel:" in chat:
base = f.replace('.meta.json', '')
for suffix in ['.meta.json', '.jsonl']:
target = os.path.join(sess, base + suffix)
if os.path.exists(target):
shutil.copy2(target, os.path.join(backup, base + suffix))
os.remove(target)
reset.append(f"{base} (count={count})")
except:
pass
print(f"Auto-reset: {len(reset)} sessions reset")
for r in reset:
print(f" {r}")