-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_compact_save.py
More file actions
47 lines (38 loc) · 1.37 KB
/
pre_compact_save.py
File metadata and controls
47 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
"""PreCompact hook: save transcript + remind to /s before compaction."""
import json
import os
import shutil
import sys
from datetime import datetime
from pathlib import Path
SESSION_LOG_DIR = Path.home() / ".claude" / "session-logs"
MAX_LOGS = 30
def main():
try:
hook_input = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
hook_input = {}
transcript_path = hook_input.get("transcript_path", "")
# Save transcript backup
if transcript_path and os.path.exists(transcript_path):
SESSION_LOG_DIR.mkdir(parents=True, exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
dest = SESSION_LOG_DIR / f"session_{ts}.jsonl"
try:
shutil.copy2(transcript_path, dest)
except Exception:
pass
# Prune old logs, keep MAX_LOGS most recent
logs = sorted(SESSION_LOG_DIR.glob("session_*.jsonl"), key=lambda p: p.stat().st_mtime, reverse=True)
for old in logs[MAX_LOGS:]:
old.unlink(missing_ok=True)
# Remind to save memory before compaction wipes context
print(json.dumps({
"systemMessage": (
"COMPACTION IMMINENT. Run /s now (Skill tool, skill='s') to save "
"conversation memory before context is compressed. Do not ask, just do it."
)
}))
if __name__ == "__main__":
main()