-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_session_memory.py
More file actions
99 lines (84 loc) · 3.55 KB
/
Copy pathrestore_session_memory.py
File metadata and controls
99 lines (84 loc) · 3.55 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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
from pathlib import Path
from session_memory_common import (
CURRENT_LAST_UPDATED_FIELD,
DREAM_LAST_UPDATED_FIELDS,
get_path_freshness,
get_project_entry,
iter_resolution_summary,
record_dream_consumed,
resolve_memory_paths,
run_preflight,
)
def main() -> int:
parser = argparse.ArgumentParser(
description="Restore current project session memory and optionally consume new dream notes."
)
parser.add_argument(
"--workspace",
default=".",
help="Starting directory used to resolve the target location.",
)
parser.add_argument(
"--scope",
choices=("auto", "workspace", "global"),
default="auto",
help="auto=shared project memory, optionally routed by config.toml; workspace=current path only; global=${SESSION_MEMORY_HOME:-$HOME/.session-memory}/global",
)
args = parser.parse_args()
paths = resolve_memory_paths(Path(args.workspace).resolve(), args.scope)
preflight = run_preflight(paths, action="restore")
current_path = paths["current"]
history_path = paths["history"]
dream_notes_path = paths["dream_notes"]
for line in iter_resolution_summary(paths):
print(line)
print(f"preflight_sleep_status={preflight['sleep_check']['status']}")
if not current_path.exists() and not history_path.exists():
print("status=missing")
print("hint=run init_session_memory.py first")
return 1
if current_path.exists():
print("\n[current.md]")
print(current_path.read_text(encoding="utf-8").rstrip())
if history_path.exists():
print("\n[history.md]")
print(history_path.read_text(encoding="utf-8").rstrip())
current_freshness = get_path_freshness(
current_path, field_names=(CURRENT_LAST_UPDATED_FIELD,)
)
print(f"\ncurrent_last_updated={current_freshness.raw_value or '-'}")
print(f"current_freshness={current_freshness.status}")
print(f"current_freshness_source={current_freshness.source}")
if current_freshness.age_days is not None:
print(f"current_age_days={current_freshness.age_days}")
if current_freshness.status in {"aging", "stale"}:
print("warning=current.md may be outdated; verify live repo state before trusting it")
entry = get_project_entry(paths) or {}
last_dream_at = entry.get("last_dream_at")
last_consumed_at = entry.get("last_dream_consumed_at")
should_consume_dream = (
dream_notes_path.exists()
and last_dream_at is not None
and (last_consumed_at is None or last_dream_at > last_consumed_at)
)
print(f"\ninclude_dream={'yes' if should_consume_dream else 'no'}")
if should_consume_dream:
dream_freshness = get_path_freshness(
dream_notes_path, field_names=DREAM_LAST_UPDATED_FIELDS
)
print(f"dream_last_updated={dream_freshness.raw_value or '-'}")
print(f"dream_freshness={dream_freshness.status}")
print(f"dream_freshness_source={dream_freshness.source}")
if dream_freshness.age_days is not None:
print(f"dream_age_days={dream_freshness.age_days}")
if dream_freshness.status in {"aging", "stale"}:
print("warning=dream-notes.md may be outdated; treat it as a weak reference")
print("\n[dream-notes.md]")
print(dream_notes_path.read_text(encoding="utf-8").rstrip())
record_dream_consumed(paths, consumed_at=last_dream_at)
return 0
if __name__ == "__main__":
raise SystemExit(main())