-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork_checkpoint.py
More file actions
67 lines (48 loc) · 2 KB
/
work_checkpoint.py
File metadata and controls
67 lines (48 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""PostToolUse hook that logs Write/Edit operations to .claude/checkpoint.md.
Reads a JSON payload from stdin describing the completed tool call,
and appends a one-line summary for Write and Edit operations so that
the record survives context compression.
"""
import json
import os
import sys
from datetime import datetime, timezone
TRACKED_TOOLS = {"Write", "Edit"}
def _summary(tool_name: str, tool_input: dict) -> str:
"""Return a one-line human-readable summary of the tool call."""
file_path = tool_input.get("file_path", "<unknown>")
basename = os.path.basename(file_path)
if tool_name == "Write":
return f"Write: created/overwrote {basename} ({file_path})"
if tool_name == "Edit":
old = tool_input.get("old_string", "")
new = tool_input.get("new_string", "")
replace_all = tool_input.get("replace_all", False)
verb = "replaced all" if replace_all else "replaced"
old_preview = (old[:40] + "...") if len(old) > 40 else old
old_preview = old_preview.replace("\n", "\\n")
return f"Edit: {basename} – {verb} `{old_preview}` ({file_path})"
return f"{tool_name}: {file_path}"
def main() -> None:
try:
payload = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
return
tool_name = payload.get("tool_name", "")
if tool_name not in TRACKED_TOOLS:
return
tool_input = payload.get("tool_input", {})
line = _summary(tool_name, tool_input)
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
entry = f"- [{timestamp}] {line}\n"
checkpoint_dir = os.path.join(os.getcwd(), ".claude")
checkpoint_file = os.path.join(checkpoint_dir, "checkpoint.md")
os.makedirs(checkpoint_dir, exist_ok=True)
needs_header = not os.path.exists(checkpoint_file)
with open(checkpoint_file, "a") as f:
if needs_header:
f.write("# Work Checkpoint Log\n\n")
f.write(entry)
if __name__ == "__main__":
main()