-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_access_tracker.py
More file actions
67 lines (50 loc) · 1.85 KB
/
memory_access_tracker.py
File metadata and controls
67 lines (50 loc) · 1.85 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
# Copyright (c) 2026 Nardo (nardovibecoding). AGPL-3.0 -- see LICENSE
"""PostToolUse hook: bump importance + access_count when a memory file is read.
Stats stored in separate JSON to avoid race conditions with Edit tool."""
import json
import sys
from datetime import date
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from hook_base import run_hook
MEMORY_DIR = Path.home() / ".claude" / "projects" / f"-Users-{Path.home().name}" / "memory"
STATS_FILE = MEMORY_DIR / "memory_stats.json"
SKIP_FILES = {"MEMORY.md", "memory_stats.json"}
def _load_stats():
if STATS_FILE.exists():
try:
return json.loads(STATS_FILE.read_text())
except (json.JSONDecodeError, OSError):
pass
return {}
def _save_stats(stats):
tmp = STATS_FILE.with_suffix(".tmp")
tmp.write_text(json.dumps(stats, indent=2))
tmp.rename(STATS_FILE)
def check(tool_name, tool_input, input_data):
if tool_name != "Read":
return False
file_path = tool_input.get("file_path", "")
return (
"memory/" in file_path
and file_path.endswith(".md")
and Path(file_path).name not in SKIP_FILES
and "/archive/" not in file_path
)
def action(tool_name, tool_input, input_data):
file_path = Path(tool_input.get("file_path", ""))
if not file_path.exists():
return None
key = file_path.name
today = date.today().isoformat()
stats = _load_stats()
entry = stats.get(key, {"access_count": 0, "importance": 50, "last_accessed": today})
entry["access_count"] = entry.get("access_count", 0) + 1
entry["importance"] = min(100, entry.get("importance", 50) + 3)
entry["last_accessed"] = today
stats[key] = entry
_save_stats(stats)
return None
if __name__ == "__main__":
run_hook(check, action, "memory_access_tracker")