-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory_auto_commit.py
More file actions
60 lines (45 loc) · 1.73 KB
/
memory_auto_commit.py
File metadata and controls
60 lines (45 loc) · 1.73 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
#!/usr/bin/env python3
# Copyright (c) 2026 Nardo (nardovibecoding). AGPL-3.0 — see LICENSE
"""Stop hook: auto-sync and commit changed memory files when session ends."""
import json
import os
import subprocess
import sys
from pathlib import Path
# Skip during convos (auto-clear flow)
_tty = os.environ.get("CLAUDE_TTY_ID", "").strip()
if Path(f"/tmp/claude_ctx_exit_pending_{_tty}").exists() if _tty else Path("/tmp/claude_ctx_exit_pending").exists():
print("{}")
sys.exit(0)
MEMORY_SRC = Path.home() / ".claude" / "projects" / f"-Users-{Path.home().name}" / "memory"
BOT_REPO = Path.home() / "telegram-claude-bot"
MEMORY_DST = BOT_REPO / "memory"
def run(cmd, **kwargs):
return subprocess.run(cmd, capture_output=True, text=True, timeout=15, **kwargs)
def main():
try:
json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
pass
if not MEMORY_SRC.exists() or not BOT_REPO.exists():
print("{}")
return
# Rsync memory from ~/.claude/ to telegram-claude-bot/memory/
run(["rsync", "-a", "--delete",
str(MEMORY_SRC) + "/",
str(MEMORY_DST) + "/"])
# Check for changes in bot repo memory/
diff = run(["git", "status", "--porcelain", "memory/"], cwd=BOT_REPO)
changed_lines = [l for l in diff.stdout.strip().splitlines() if l.strip()]
if not changed_lines:
print("{}")
return
# Count files
n = len(changed_lines)
# Auto-commit
run(["git", "add", "memory/"], cwd=BOT_REPO)
run(["git", "commit", "-m", f"memory: auto-sync {n} file(s) at session end"],
cwd=BOT_REPO)
print(json.dumps({"systemMessage": f"Memory auto-committed: {n} file(s) synced to telegram-claude-bot."}))
if __name__ == "__main__":
main()