-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_start.py
More file actions
91 lines (80 loc) · 3.39 KB
/
Copy pathsession_start.py
File metadata and controls
91 lines (80 loc) · 3.39 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""
Memory Engineering V1 — Session Start Hook
在每次新会话开始时调用,加载 Core Context。
输出 JSON,由 Agent 注入上下文。
同时写入 Atomic Event(Event Layer 集成)。
"""
import json, os, glob, sys, uuid
from datetime import datetime
BASE = os.path.expanduser("~/Desktop/Memory_System/memory")
AGENT = "hermes"
SYSTEM_DIR = os.path.dirname(os.path.abspath(__file__))
# 确保 event_writer 可导入
sys.path.insert(0, SYSTEM_DIR)
def load_core_context():
"""加载 Core Context: Identity + Active Projects + Active Decisions"""
core = {"identity": [], "project_state": [], "active_decisions": []}
for mem_type, key, status_filter in [
("identity", "identity", None),
("project_state", "project_state", "active"),
("decision", "active_decisions", "active"),
]:
pattern = os.path.join(BASE, AGENT, mem_type, "*.json")
for f in sorted(glob.glob(pattern)):
with open(f) as fh:
mem = json.load(fh)
if status_filter and mem.get("status") != status_filter:
continue
core[key].append(mem)
# Also load shared lessons
core["shared_lessons"] = []
for f in sorted(glob.glob(os.path.join(BASE, "shared", "lesson", "*.json"))):
with open(f) as fh:
mem = json.load(fh)
if mem.get("status") == "active":
core["shared_lessons"].append(mem)
return {
"hook": "session_start",
"agent": AGENT,
"timestamp": datetime.now().isoformat(),
"core_context": core,
"stats": {
"identity": len(core["identity"]),
"active_projects": len(core["project_state"]),
"active_decisions": len(core["active_decisions"]),
"shared_lessons": len(core["shared_lessons"])
}
}
if __name__ == "__main__":
context = load_core_context()
# ── Event Layer: 写入 Atomic Event ──
sid = os.environ.get("MEMORY_SESSION_ID") or uuid.uuid4().hex[:12]
os.environ["MEMORY_SESSION_ID"] = sid
# 持久化 session_id 到临时文件(write_hook 等独立 shell 调用需要)
sid_file = os.path.join(SYSTEM_DIR, ".current_session_id")
with open(sid_file, "w") as f:
f.write(sid)
events_dir = os.path.join(SYSTEM_DIR, "events")
try:
from event_writer import write_atomic_event, index_core_context
state = index_core_context(context["core_context"])
evt = write_atomic_event(
action="session_start", trigger="session_start.py",
state_before={}, state_after=state,
actor="system", session_id=sid, events_dir=events_dir
)
if evt:
print(f"[session_start] Atomic Event written: {evt['id']} (seq={evt['seq']})", file=sys.stderr)
except Exception as e:
print(f"[session_start] Event write failed (non-fatal): {e}", file=sys.stderr)
# ── IFIL: 从历史 event log 注入 feedback ──
try:
from ifil import generate_feedback
fb = generate_feedback(events_dir=events_dir, current_session_id=sid)
if fb:
context["feedback"] = fb
print(f"[session_start] IFIL injected {len(fb)} feedback signals", file=sys.stderr)
except Exception as e:
print(f"[session_start] IFIL failed (non-fatal): {e}", file=sys.stderr)
print(json.dumps(context, ensure_ascii=False, indent=2))