memory-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. memory_cli/__init__.py +1 -0
  2. memory_cli/__main__.py +49 -0
  3. memory_cli/capture/__init__.py +7 -0
  4. memory_cli/capture/codex_rollout.py +164 -0
  5. memory_cli/capture/event_lines.py +66 -0
  6. memory_cli/capture/extract.py +458 -0
  7. memory_cli/capture/state.py +183 -0
  8. memory_cli/commands/__init__.py +21 -0
  9. memory_cli/commands/admin.py +363 -0
  10. memory_cli/commands/common.py +609 -0
  11. memory_cli/commands/curate.py +1189 -0
  12. memory_cli/commands/diagnostics.py +126 -0
  13. memory_cli/commands/edit.py +1282 -0
  14. memory_cli/commands/hooks.py +730 -0
  15. memory_cli/commands/import_recovery.py +45 -0
  16. memory_cli/commands/install.py +152 -0
  17. memory_cli/commands/integrate.py +480 -0
  18. memory_cli/commands/memory.py +2152 -0
  19. memory_cli/commands/read.py +1290 -0
  20. memory_cli/commands/sources.py +766 -0
  21. memory_cli/commands/split.py +97 -0
  22. memory_cli/commands/split_plan.py +196 -0
  23. memory_cli/commands/split_transaction.py +230 -0
  24. memory_cli/commands/synthesis.py +1122 -0
  25. memory_cli/core/__init__.py +16 -0
  26. memory_cli/core/auto_state.py +30 -0
  27. memory_cli/core/codex_pass.py +109 -0
  28. memory_cli/core/config.py +87 -0
  29. memory_cli/core/file_journal.py +163 -0
  30. memory_cli/core/headless.py +130 -0
  31. memory_cli/core/jobs.py +130 -0
  32. memory_cli/core/resources.py +57 -0
  33. memory_cli/core/runner.py +398 -0
  34. memory_cli/core/safe_io.py +155 -0
  35. memory_cli/core/store_git.py +407 -0
  36. memory_cli/core/yaml_load.py +14 -0
  37. memory_cli/corroborate/__init__.py +11 -0
  38. memory_cli/corroborate/cli.py +267 -0
  39. memory_cli/corroborate/decide.py +893 -0
  40. memory_cli/corroborate/digest.py +425 -0
  41. memory_cli/corroborate/fill.py +744 -0
  42. memory_cli/corroborate/job.py +15 -0
  43. memory_cli/corroborate/reword.py +446 -0
  44. memory_cli/corroborate/run.py +1142 -0
  45. memory_cli/corroborate/state.py +300 -0
  46. memory_cli/digest/__init__.py +4 -0
  47. memory_cli/digest/lessons.py +483 -0
  48. memory_cli/entry/__init__.py +12 -0
  49. memory_cli/entry/auto.py +660 -0
  50. memory_cli/entry/capture.py +1403 -0
  51. memory_cli/entry/corroborate.py +214 -0
  52. memory_cli/entry/integrate.py +684 -0
  53. memory_cli/entry/integrate_self_check.py +203 -0
  54. memory_cli/entry/research.py +9 -0
  55. memory_cli/entry/settle.py +614 -0
  56. memory_cli/entry/split_auto.py +460 -0
  57. memory_cli/entry/synthesize.py +22 -0
  58. memory_cli/integrate/__init__.py +9 -0
  59. memory_cli/integrate/answers.py +42 -0
  60. memory_cli/integrate/briefs/check-brief.md +16 -0
  61. memory_cli/integrate/briefs/place-brief.md +26 -0
  62. memory_cli/integrate/log.py +43 -0
  63. memory_cli/integrate/plan.py +125 -0
  64. memory_cli/integrate/rows.py +99 -0
  65. memory_cli/integrate/state.py +158 -0
  66. memory_cli/integrate/undo.py +143 -0
  67. memory_cli/mem.py +1180 -0
  68. memory_cli/memories/__init__.py +16 -0
  69. memory_cli/memories/fingerprints.py +51 -0
  70. memory_cli/memories/journal.py +93 -0
  71. memory_cli/memories/locations.py +282 -0
  72. memory_cli/memories/matching.py +451 -0
  73. memory_cli/memories/model.py +205 -0
  74. memory_cli/memories/parse.py +237 -0
  75. memory_cli/memories/records.py +266 -0
  76. memory_cli/memories/rules.py +291 -0
  77. memory_cli/memories/sightings.py +314 -0
  78. memory_cli/memories/terms.py +148 -0
  79. memory_cli/memories/write.py +358 -0
  80. memory_cli/notes/__init__.py +13 -0
  81. memory_cli/notes/check.py +806 -0
  82. memory_cli/notes/due.py +260 -0
  83. memory_cli/notes/fields.py +217 -0
  84. memory_cli/notes/index.py +230 -0
  85. memory_cli/notes/links.py +170 -0
  86. memory_cli/notes/note.py +222 -0
  87. memory_cli/notes/search.py +391 -0
  88. memory_cli/notes/split_candidates.py +72 -0
  89. memory_cli/research/__init__.py +5 -0
  90. memory_cli/research/checks.py +1065 -0
  91. memory_cli/research/pipeline.py +952 -0
  92. memory_cli/session/__init__.py +5 -0
  93. memory_cli/session/handoff.py +139 -0
  94. memory_cli/session/notices.py +111 -0
  95. memory_cli/settle/__init__.py +5 -0
  96. memory_cli/settle/state.py +111 -0
  97. memory_cli/settle/verdicts.py +693 -0
  98. memory_cli/split/briefs/checker-brief.md +5 -0
  99. memory_cli/split/briefs/editor-brief.md +20 -0
  100. memory_cli/syntheses/__init__.py +8 -0
  101. memory_cli/syntheses/document.py +400 -0
  102. memory_cli/syntheses/edit.py +581 -0
  103. memory_cli/syntheses/project.py +384 -0
  104. memory_cli/syntheses/split_conservation.py +50 -0
  105. memory_cli/syntheses/units.py +427 -0
  106. memory_cli/synthesize/__init__.py +9 -0
  107. memory_cli/synthesize/backfill.py +431 -0
  108. memory_cli/synthesize/briefs/backfill-brief.md +26 -0
  109. memory_cli/synthesize/briefs/checker-brief.md +13 -0
  110. memory_cli/synthesize/briefs/fixer-brief.md +19 -0
  111. memory_cli/synthesize/briefs/memory-brief.md +30 -0
  112. memory_cli/synthesize/briefs/settle-brief.md +32 -0
  113. memory_cli/synthesize/briefs/verdict-brief.md +50 -0
  114. memory_cli/synthesize/briefs/web-brief.md +33 -0
  115. memory_cli/synthesize/cli.py +311 -0
  116. memory_cli/synthesize/pipeline.py +764 -0
  117. memory_cli/synthesize/references.py +164 -0
  118. memory_cli/synthesize/run.py +150 -0
  119. memory_cli/synthesize/topics.py +528 -0
  120. memory_cli/templates/knowledge.md +23 -0
  121. memory_cli/templates/literature.md +25 -0
  122. memory_cli/templates/research.md +23 -0
  123. memory_cli/templates/synthesis.md +6 -0
  124. memory_cli-0.1.0.dist-info/METADATA +102 -0
  125. memory_cli-0.1.0.dist-info/RECORD +128 -0
  126. memory_cli-0.1.0.dist-info/WHEEL +4 -0
  127. memory_cli-0.1.0.dist-info/entry_points.txt +9 -0
  128. memory_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,311 @@
1
+ """The command line of `mem-synthesize`: argument parser, `main` and the self-check."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import os
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ import tempfile
11
+ from pathlib import Path
12
+
13
+ from memory_cli.core import codex_pass, resources
14
+ from memory_cli.core import runner as runners
15
+ from memory_cli.synthesize import backfill, run, topics
16
+ from memory_cli.synthesize import pipeline as sp
17
+
18
+
19
+ def _check_parsers() -> None:
20
+ curate = (
21
+ "2 memories seen several times\n foo\n\ntopics with 8 notes or more and no synthesis (mem synthesis new)\n"
22
+ " alpha-beta 26 notes\n gamma 9 notes\nnotes to unify\n delta 4 notes\n"
23
+ )
24
+ assert run.topics_without_synthesis(curate) == ["alpha-beta", "gamma"], (
25
+ run.topics_without_synthesis(curate)
26
+ )
27
+ answer = "Slug: x\nATTACH\nknowledge/a/b.md\ts:x#part-one\nknowledge/a/c.md s:x#bad\nliterature/z.md\ts:x#p\n"
28
+ assert run.attach_lines(answer) == [("knowledge/a/b.md", "s:x#part-one")], run.attach_lines(
29
+ answer
30
+ )
31
+ assert (
32
+ "/plugins/cache/" not in run.session_env(Path("/x"))["PATH"]
33
+ and run.session_env(Path("/x"))["MEM_STORE"] == "/x"
34
+ )
35
+ rows = [
36
+ {
37
+ **topics.new_row("t"),
38
+ "parts": 5,
39
+ "attached": 7,
40
+ "fixed": 2,
41
+ "deleted": 1,
42
+ "refs": 4,
43
+ "mem_check": 0,
44
+ "cost": 0.31,
45
+ }
46
+ ]
47
+ assert topics.table(rows).splitlines()[1].split()[:3] == ["t", "5", "7"], topics.table(rows)
48
+
49
+
50
+ def _init_store(store: Path, env: dict) -> None:
51
+ assert (
52
+ subprocess.run(
53
+ [*sp.MEM, "init", str(store)],
54
+ env=resources.child_env(env),
55
+ capture_output=True,
56
+ check=False,
57
+ ).returncode
58
+ == 0
59
+ )
60
+ assert (
61
+ subprocess.run(
62
+ [*sp.MEM, "history", "init"],
63
+ env=resources.child_env({**env, "MEM_STORE": str(store)}),
64
+ cwd=store,
65
+ capture_output=True,
66
+ check=False,
67
+ ).returncode
68
+ == 0
69
+ )
70
+
71
+
72
+ def _check_dry_runs(store: Path, tmp: Path) -> None:
73
+ lines: list[str] = []
74
+ assert topics.run(None, store, ["one", "two"], 3.0, True, out=lines.append) == 0
75
+ assert "2 topics" in lines[0] and "1.10 USD" in lines[0] and "reference step" in lines[0], lines
76
+ lines.clear()
77
+ assert (
78
+ topics.run(None, store, ["one", "two"], 3.0, True, out=lines.append, references=False) == 0
79
+ )
80
+ assert "1.00 USD" in lines[0] and "no reference step" in lines[0], lines
81
+ lines.clear()
82
+ assert topics.run(None, store, ["one", "two"], 3.0, True, out=lines.append) == 0
83
+ assert not (tmp / "data").exists(), "a dry run wrote something"
84
+
85
+
86
+ def _commit_all(store: Path, message: str) -> None:
87
+ subprocess.run(["git", "add", "-A"], cwd=store, check=True)
88
+ subprocess.run(
89
+ ["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", message],
90
+ cwd=store,
91
+ check=True,
92
+ )
93
+
94
+
95
+ # What the fake session writes: it must pass `mem check`, or the store's commit gate holds it back.
96
+ FAKE_NOTE = """---
97
+ claim: A fake session wrote this
98
+ topics: [notes]
99
+ confidence: low
100
+ level: basic
101
+ origin: experience
102
+ updated: 2026-11-26
103
+ ---
104
+
105
+ ## Claim
106
+
107
+ x
108
+
109
+ ## Evidence
110
+
111
+ x
112
+
113
+ ## Applies when
114
+
115
+ x
116
+
117
+ ## Does not apply when
118
+
119
+ x
120
+ """
121
+
122
+
123
+ def _fake_claude(tmp: Path, store: Path) -> Path:
124
+ """A `claude` that writes a valid note into the store and stops; the store gets a committed fixture."""
125
+ fake = tmp / "fake-claude"
126
+ fake.write_text(
127
+ '#!/bin/sh\nmkdir -p "$MEM_STORE/knowledge/notes"\ncat > "$MEM_STORE/knowledge/notes/touched.md" <<\'EOF\'\n'
128
+ + FAKE_NOTE
129
+ + 'EOF\necho \'{"type":"result","is_error":false,"result":"STOP: fake","total_cost_usd":0.0}\'\n',
130
+ encoding="utf-8",
131
+ )
132
+ fake.chmod(0o755)
133
+ (store / "knowledge").mkdir(exist_ok=True)
134
+ (store / "knowledge" / "keep.md").write_text("kept\n", encoding="utf-8")
135
+ _commit_all(store, "fixture")
136
+ return fake
137
+
138
+
139
+ def _check_guard(store: Path, env: dict) -> None:
140
+ """The out-of-scope guard: a fake fixer edits two parts, the report lists one; the other is put back."""
141
+ os.environ["MEM_STORE"] = str(store)
142
+ assert (
143
+ sp.mem(
144
+ store,
145
+ env,
146
+ "synthesis",
147
+ "new",
148
+ "--title",
149
+ "Demo",
150
+ "--topics",
151
+ "notes",
152
+ "--slug",
153
+ "demo",
154
+ "--summary-file",
155
+ "-",
156
+ stdin="Summary.\n",
157
+ ).returncode
158
+ == 0
159
+ )
160
+ for heading in ("Alpha", "Beta"):
161
+ added = sp.mem(
162
+ store,
163
+ env,
164
+ "synthesis",
165
+ "add",
166
+ "s:demo",
167
+ "--section",
168
+ heading,
169
+ "--body-file",
170
+ "-",
171
+ stdin=f"{heading} text one. Second sentence here.\n\nSources: experience 2026-11-17\n",
172
+ )
173
+ assert added.returncode == 0, added.stderr
174
+ demo = store / "syntheses" / "demo.md"
175
+ before = demo.read_text(encoding="utf-8")
176
+ demo.write_text(
177
+ before.replace("Alpha text one.", "Alpha fixed.").replace("Second sentence here.", "Cut."),
178
+ encoding="utf-8",
179
+ )
180
+ after = demo.read_text(encoding="utf-8")
181
+ bad, back = sp.guard_parts(store, env, "demo", before, after, allowed=["alpha"])
182
+ assert (bad, back) == (["beta"], ["beta"]), (bad, back)
183
+ now = sp.split_parts(demo.read_text(encoding="utf-8"))
184
+ assert (
185
+ now["beta"].strip() == sp.split_parts(before)["beta"].strip()
186
+ and "Alpha fixed." in now["alpha"]
187
+ ), now
188
+ checked = sp.mem(store, env, "check")
189
+ assert "demo.md" not in checked.stdout, checked.stdout # keep.md above is not a note
190
+ assert (
191
+ sp.mem(store, env, "restore", "s:demo").returncode == 0
192
+ ) # the revert is one undoable step
193
+ assert sp.guard_parts(store, env, "demo", before, before, allowed=[])[0] == []
194
+ os.environ.pop("MEM_STORE", None)
195
+
196
+
197
+ def _check_fake_run(store: Path, fake: Path, env: dict) -> None:
198
+ lines: list[str] = []
199
+ assert topics.run(str(fake), store, ["one", "two"], 3.0, False, out=lines.append) == 1
200
+ assert (
201
+ "failed: STOP: fake" in lines[1]
202
+ and lines[1].count("failed") == run.FAILED_WORDS_PER_FAKE_ROW
203
+ ), lines
204
+ assert (store / "knowledge" / "keep.md").read_text(encoding="utf-8") == "kept\n"
205
+ log = subprocess.run(
206
+ ["git", "log", "--format=%s"], cwd=store, capture_output=True, text=True, check=False
207
+ ).stdout
208
+ assert "mem-synthesize: after one, two" in log and "mem-synthesize: before" not in log, (
209
+ log
210
+ ) # only a change commits
211
+ shown = subprocess.run(
212
+ ["git", "show", "--name-only", "--format=", "HEAD"],
213
+ cwd=store,
214
+ capture_output=True,
215
+ text=True,
216
+ check=False,
217
+ ).stdout
218
+ assert "knowledge/notes/touched.md" in shown.splitlines(), shown # not held back
219
+ checked = sp.mem(store, env, "check")
220
+ report = checked.stdout + checked.stderr
221
+ # keep.md is a fixture finding: seeing it proves `mem check` ran; only the fake's note must be clean
222
+ assert "knowledge/keep.md" in report and "notes/touched.md" not in report, report
223
+
224
+
225
+ def self_check() -> int:
226
+ """No model: parsers, the dry run, and a whole run with a fake `claude` that writes a valid note and stops
227
+ (every topic fails, the run goes on, the closing commit holds the note, `mem check` reports nothing on it, the store keeps its content)."""
228
+ _check_parsers()
229
+ with tempfile.TemporaryDirectory(prefix="mem-synthesize-check-") as tmp_name:
230
+ tmp = Path(tmp_name)
231
+ store = tmp / "store"
232
+ env = {**os.environ, "MEM_DATA": str(tmp / "data")}
233
+ _init_store(store, env)
234
+ os.environ["MEM_DATA"] = str(tmp / "data")
235
+ _check_dry_runs(store, tmp)
236
+ fake = _fake_claude(tmp, store)
237
+ _check_guard(store, env)
238
+ _commit_all(store, "demo")
239
+ _check_fake_run(store, fake, env)
240
+ print("self-check passed")
241
+ return 0
242
+
243
+
244
+ def _parser(description: str) -> argparse.ArgumentParser:
245
+ parser = argparse.ArgumentParser(prog="mem-synthesize", description=description.splitlines()[0])
246
+ parser.add_argument(
247
+ "--backfill",
248
+ nargs="*",
249
+ metavar="s:HANDLE",
250
+ help="add inline references to existing "
251
+ "syntheses (the named ones, or all) with one read-only session each",
252
+ )
253
+ parser.add_argument(
254
+ "--topic",
255
+ action="append",
256
+ default=[],
257
+ help="topic id (repeatable; default: the topics "
258
+ "`mem curate --notes` lists without a synthesis)",
259
+ )
260
+ parser.add_argument(
261
+ "--budget", type=float, default=3.0, help="USD; no new topic starts past it"
262
+ )
263
+ parser.add_argument(
264
+ "--no-references",
265
+ action="store_true",
266
+ help="skip the last step of a topic (adding inline references with the --backfill session)",
267
+ )
268
+ parser.add_argument(
269
+ "--dry-run", action="store_true", help="list the topics and the estimated cost; run nothing"
270
+ )
271
+ parser.add_argument(
272
+ "--store", type=Path, help="default: the store `mem` resolves (MEM_STORE, else the default)"
273
+ )
274
+ parser.add_argument(
275
+ "--claude", default=os.environ.get("CLAUDE_CODE_EXECPATH") or shutil.which("claude")
276
+ )
277
+ runners.add_flag(parser)
278
+ parser.add_argument(
279
+ "--self-check",
280
+ action="store_true",
281
+ help="no model: prove the parsers and a run with a fake claude",
282
+ )
283
+ return parser
284
+
285
+
286
+ def _check_args(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None:
287
+ if args.backfill is not None and args.topic:
288
+ parser.error("--backfill takes synthesis handles (s:<handle>), not --topic")
289
+ if args.backfill is not None and args.no_references:
290
+ parser.error("--no-references applies to writing syntheses, not to --backfill")
291
+ if args.backfill is not None and args.self_check:
292
+ parser.error("--backfill takes synthesis handles (s:<handle>), not --self-check")
293
+
294
+
295
+ def main(description: str, argv: list[str] | None = None) -> int:
296
+ parser = _parser(description)
297
+ args = parser.parse_args(argv)
298
+ _check_args(parser, args)
299
+ if args.self_check:
300
+ return self_check()
301
+ if (refused := codex_pass.refuse_codex("mem-synthesize", args.runner)) is not None:
302
+ return refused
303
+ store = args.store.expanduser().resolve() if args.store else run.default_store()
304
+ if store is None or not (store / "mem-store.yaml").is_file():
305
+ print(f"mem-synthesize: no writable store at {store}", file=sys.stderr)
306
+ return 2
307
+ if args.backfill is not None:
308
+ return backfill.run_backfill(args.claude, store, args.backfill, args.budget, args.dry_run)
309
+ return topics.run(
310
+ args.claude, store, args.topic, args.budget, args.dry_run, references=not args.no_references
311
+ )