-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnemo_session.py
More file actions
381 lines (298 loc) · 12.7 KB
/
mnemo_session.py
File metadata and controls
381 lines (298 loc) · 12.7 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
mnemo_session.py — Session store lifecycle
A session store is ephemeral working memory for a single agent session.
It holds reasoning chains that haven't been committed to the project store
yet — preliminary work that may be promoted, stashed, or discarded.
== Why this exists ==
In v1, every memory_claim immediately became a permanent project node.
This created pressure to only store "finished" thoughts, losing the
intermediate reasoning that's often the most valuable part. The session
store lets an agent think out loud without polluting the project tree.
== Store layout ==
.mnemo/
├── sessions/
│ ├── s_a7f3c2d1/ ← current session
│ │ ├── nodes/
│ │ ├── active.json
│ │ └── chains.json ← preliminary chains only
│ └── ...
└── session_archive/
├── s_b91e4d3f/ ← archived sessions (recoverable)
└── ...
== Lifecycle ==
session start
└── new_session_id() → get_session_store() → session Store object
work happens
└── claims with preliminary=True go to session store
chains build with status="preliminary"
promote
└── promote_chain() copies nodes + chain to project store
chain status: preliminary → active
session compress
└── unpromoted chains: archive_session() moves dir to session_archive/
promoted chains: already in project store, session copy is redundant
discard (noise)
└── session dir is archived (not deleted) — content-addressed files
are cheap; recovery is valuable
== Node format ==
Identical to project store nodes. Promotion is a file copy + active.json
update. Content-addressing means the same node address in both stores
points to identical content — no reconciliation needed.
Preliminary chains carry status="preliminary" in chains.json. After
promotion, the project store chain gets status="active".
"""
import json
import secrets
import shutil
import time
from pathlib import Path
from typing import Optional
from mnemo import Store, Node
# ===================================================================
# Session ID
# ===================================================================
def new_session_id() -> str:
"""Generate a unique session ID: s_ + 8 hex chars."""
return "s_" + secrets.token_hex(4)
# ===================================================================
# Session store access
# ===================================================================
def session_store_path(base_store: Store, session_id: str,
agent_id: Optional[str] = None) -> Path:
"""
Return the path for a session store.
Single-agent: .mnemo/sessions/<session_id>/
Multi-agent: .mnemo/sessions/<session_id>/<agent_id>/
"""
sessions_dir = base_store.root / "sessions"
if agent_id:
return sessions_dir / session_id / agent_id
return sessions_dir / session_id
def get_session_store(base_store: Store, session_id: str,
agent_id: Optional[str] = None) -> Store:
"""
Return a Store for the given session. Creates the directory if needed.
The session store is a full Store object — same format as the project
store, but scoped to this session. No TF-IDF index is built (linear
scan is fast enough for <50 nodes per session).
"""
path = session_store_path(base_store, session_id, agent_id)
path.mkdir(parents=True, exist_ok=True)
# Initialize chains.json if not present
chains_path = path / "chains.json"
if not chains_path.exists():
chains_path.write_text("{}\n", encoding="utf-8")
return Store(path)
def load_or_create_session(base_store: Store,
session_state_path: Optional[Path] = None
) -> tuple[str, Store]:
"""
Load the current session ID from session_state.json, or create a new one.
Returns (session_id, session_store).
"""
state_path = session_state_path or (base_store.root / "session_state.json")
session_id = None
if state_path.exists():
try:
state = json.loads(state_path.read_text(encoding="utf-8"))
saved_at = state.get("saved_at", 0)
# Reuse session if less than 2 hours old
if time.time() - saved_at < 7200:
session_id = state.get("session_id")
except (json.JSONDecodeError, OSError):
pass
if not session_id:
session_id = new_session_id()
return session_id, get_session_store(base_store, session_id)
# ===================================================================
# Preliminary chain management
# ===================================================================
def create_preliminary_chain(session_store: Store, head_addr: str,
domain: str = "", summary: str = "",
agent_id: Optional[str] = None) -> str:
"""
Create a chain with status="preliminary" in the session store.
Wraps mnemo_chains.create_chain with the preliminary status.
"""
from mnemo_chains import create_chain
return create_chain(
session_store, head_addr,
domain=domain, summary=summary,
agent_id=agent_id, status="preliminary",
)
def list_preliminary_chains(session_store: Store) -> list[dict]:
"""Return all preliminary chains in the session store."""
from mnemo_chains import list_chains
return list_chains(session_store, status_filter={"preliminary"})
# ===================================================================
# Promotion: session store → project store
# ===================================================================
def promote_nodes(session_store: Store, project_store: Store,
addrs: list[str]) -> list[str]:
"""
Copy nodes from session store to project store and add them to active.
Since nodes are content-addressed, copying a node that already exists
in the project store is a no-op (same address = same file). Returns the
list of addresses that were promoted (some may have already existed).
Does NOT modify the session store — session copy remains until archived.
"""
promoted = []
project_active = project_store.get_active()
for addr in addrs:
node = session_store.get(addr)
if not node:
continue
# Copy node file to project store
src = session_store.nodes_dir / f"{addr}.json"
dst = project_store.nodes_dir / f"{addr}.json"
if not dst.exists():
dst.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
project_active.add(addr)
promoted.append(addr)
project_store.set_active(project_active)
# Re-register any content_hash anchors in the project store's file index
if promoted:
try:
from mnemo_anchor import update_file_index
for addr in promoted:
node = project_store.get(addr)
if node and node.meta.get("anchors"):
update_file_index(project_store, node)
except Exception:
pass
return promoted
def promote_chain(session_store: Store, project_store: Store,
chain_id: str) -> Optional[str]:
"""
Promote a preliminary chain from the session store to the project store.
Steps:
1. Copy all member nodes to the project store
2. Add them to project active set
3. Write the chain to project store's chains.json with status="active"
4. Mark the session chain as status="archived" (it stays in session store)
Returns the chain_id on success, None if chain not found.
"""
from mnemo_chains import get_chain, get_chains, _set_chains
# Get chain from session store
chain = get_chain(session_store, chain_id)
if not chain:
return None
# Promote member nodes
members = chain.get("members", [])
promote_nodes(session_store, project_store, members)
# Write chain to project store with status=active
project_chains_path = project_store.root / "chains.json"
if project_chains_path.exists():
try:
project_chains = json.loads(
project_chains_path.read_text(encoding="utf-8")
)
except (json.JSONDecodeError, OSError):
project_chains = {}
else:
project_chains = {}
promoted_chain = dict(chain)
promoted_chain["status"] = "active"
promoted_chain["promoted_at"] = time.time()
promoted_chain["promoted_from_session"] = str(session_store.root)
project_chains[chain_id] = promoted_chain
project_chains_path.write_text(
json.dumps(project_chains, indent=2), encoding="utf-8"
)
# Mark session chain as archived
session_chains = get_chains(session_store)
if chain_id in session_chains:
session_chains[chain_id]["status"] = "archived"
_set_chains(session_store, session_chains)
# Update node meta in project store to reflect active chain membership
for addr in members:
node = project_store.get(addr)
if node:
existing_chains = node.meta.get("chains", [])
if chain_id not in existing_chains:
node.meta["chains"] = existing_chains + [chain_id]
project_store.put(node)
return chain_id
def promote_all_preliminary(session_store: Store,
project_store: Store) -> list[str]:
"""
Promote all preliminary chains in the session store to the project store.
Returns list of promoted chain IDs.
"""
preliminary = list_preliminary_chains(session_store)
promoted = []
for chain in preliminary:
chain_id = chain["chain_id"]
result = promote_chain(session_store, project_store, chain_id)
if result:
promoted.append(chain_id)
return promoted
# ===================================================================
# Session archiving and cleanup
# ===================================================================
def archive_session(base_store: Store, session_id: str,
agent_id: Optional[str] = None) -> Optional[Path]:
"""
Move the session directory to session_archive/ for recovery.
Returns the archive path, or None if the session dir doesn't exist.
Does NOT delete the source — the move IS the cleanup.
"""
src = session_store_path(base_store, session_id, agent_id)
if not src.exists():
return None
archive_dir = base_store.root / "session_archive"
archive_dir.mkdir(exist_ok=True)
# Add timestamp to avoid collisions if same session_id reused
ts = int(time.time())
dest_name = f"{session_id}_{ts}" if agent_id is None else f"{session_id}_{agent_id}_{ts}"
dest = archive_dir / dest_name
shutil.move(str(src), str(dest))
return dest
def session_summary(session_store: Store) -> dict:
"""
Return a summary of the current session store state.
Useful for the session compress prompt — shows what's preliminary,
what's already been promoted, and what's available to promote.
"""
from mnemo_chains import list_chains
active = session_store.get_active()
all_chains = list_chains(session_store)
preliminary = [c for c in all_chains if c.get("status") == "preliminary"]
archived = [c for c in all_chains if c.get("status") == "archived"]
return {
"session_path": str(session_store.root),
"active_nodes": len(active),
"total_chains": len(all_chains),
"preliminary_chains": len(preliminary),
"promoted_chains": len(archived), # archived in session = promoted to project
"preliminary_chain_list": [
{
"chain_id": c["chain_id"],
"domain": c.get("domain", ""),
"summary": c.get("summary", ""),
"members": len(c.get("members", [])),
}
for c in preliminary
],
}
# ===================================================================
# Session GC
# ===================================================================
def gc_sessions(base_store: Store, max_age_days: int = 30) -> int:
"""
Purge session archives older than max_age_days.
Only removes archived sessions (in session_archive/), not active ones.
Returns the number of session archives purged.
"""
archive_dir = base_store.root / "session_archive"
if not archive_dir.exists():
return 0
cutoff = time.time() - (max_age_days * 86400)
purged = 0
for entry in archive_dir.iterdir():
if entry.is_dir():
mtime = entry.stat().st_mtime
if mtime < cutoff:
shutil.rmtree(str(entry))
purged += 1
return purged