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,267 @@
1
+ """mem-corroborate: the commands that need the whole engine, and the self-check."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import datetime
7
+ import json
8
+ import logging
9
+ import os
10
+ import sys
11
+ import tempfile
12
+ import time
13
+ from pathlib import Path
14
+
15
+ from memory_cli.capture import state as cs
16
+ from memory_cli.core import jobs
17
+ from memory_cli.corroborate import digest as corroborate_digest
18
+ from memory_cli.corroborate import fill as corroborate_fill
19
+ from memory_cli.corroborate import reword as corroborate_reword
20
+ from memory_cli.corroborate import run as corroborate_run
21
+ from memory_cli.corroborate import state as corroborate_state
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+ _CHECK_BUDGET = 0.6
26
+ _PRIVATE_MODE = 0o700
27
+
28
+
29
+ def cmd_run(args) -> int:
30
+ if args.dry_run and not args.out:
31
+ print("mem-corroborate: --dry-run needs --out FILE", file=sys.stderr)
32
+ return 2
33
+ if args.out and not args.dry_run:
34
+ print("mem-corroborate: --out goes with --dry-run", file=sys.stderr)
35
+ return 2
36
+ if args.dry_run and args.groups:
37
+ print(
38
+ "mem-corroborate: --dry-run runs the sessions: it does not take --groups",
39
+ file=sys.stderr,
40
+ )
41
+ return 2
42
+ if args.dry_run:
43
+ repo = corroborate_run.repo_above(Path(args.out).expanduser().resolve().parent)
44
+ if repo is not None:
45
+ print(
46
+ f"mem-corroborate: --out is inside a repository ({repo}); the result holds memory text",
47
+ file=sys.stderr,
48
+ )
49
+ return 2
50
+ line = corroborate_run.execute(args.claude, args.budget, args.groups, args.dry_run, args.out)
51
+ print(
52
+ summary(line)
53
+ if not line.get("failed_reason")
54
+ else f"mem-corroborate: failed: {line['failed_reason']}"
55
+ )
56
+ return 1 if line["failed"] else 0
57
+
58
+
59
+ def cmd_fill(args) -> int:
60
+ usage = "usage: mem-corroborate fill --dry-run --out FILE [--claude PATH] [--budget USD] | fill --items FILE"
61
+ if args.items:
62
+ if args.dry_run or args.out:
63
+ print(
64
+ f"mem-corroborate: --items applies a file and goes with neither --dry-run nor --out\n{usage}",
65
+ file=sys.stderr,
66
+ )
67
+ return 2
68
+ return corroborate_fill.fill_items(args)
69
+ if not (args.dry_run and args.out):
70
+ print(
71
+ f"mem-corroborate: fill writes nothing unread: it needs --dry-run --out FILE or --items FILE\n{usage}",
72
+ file=sys.stderr,
73
+ )
74
+ return 2
75
+ return corroborate_fill.fill_dry_run(args)
76
+
77
+
78
+ def cmd_reword(args) -> int:
79
+ if (
80
+ args.every and not args.dry_run
81
+ ): # --every is for the gate on a copy: on a real store it would mark every lesson reworded
82
+ print(
83
+ "mem-corroborate: --every goes with --dry-run: it takes lessons that did not get worse, for a trial on a copy",
84
+ file=sys.stderr,
85
+ )
86
+ return 2
87
+ sheet = Path(args.sheet).expanduser() if args.sheet else None
88
+ if sheet is not None:
89
+ repo = corroborate_run.repo_above(sheet.resolve().parent)
90
+ if repo is not None:
91
+ print(
92
+ f"mem-corroborate: --sheet is inside a repository ({repo}); the sheet holds memory text",
93
+ file=sys.stderr,
94
+ )
95
+ return 2
96
+ result = corroborate_reword.reword_auto(
97
+ args.claude,
98
+ args.budget,
99
+ every=args.every,
100
+ apply=not args.dry_run,
101
+ sheet=sheet,
102
+ limit=args.limit,
103
+ )
104
+ print(json.dumps(result))
105
+ return 1 if result["stopped"] else 0
106
+
107
+
108
+ def cmd_digest(args) -> int:
109
+ sheet = Path(args.sheet).expanduser() if args.sheet else None
110
+ if sheet is not None:
111
+ repo = corroborate_run.repo_above(sheet.resolve().parent)
112
+ if repo is not None:
113
+ print(
114
+ f"mem-corroborate: --sheet is inside a repository ({repo}); the sheet holds memory text",
115
+ file=sys.stderr,
116
+ )
117
+ return 2
118
+ result = corroborate_digest.digest_auto(
119
+ args.claude, args.budget, apply=not args.dry_run, sheet=sheet
120
+ )
121
+ print(json.dumps(result))
122
+ return 1 if result["stopped"] else 0
123
+
124
+
125
+ def _auto_budget() -> float | None:
126
+ """The budget of `auto`, or None when the option is off or cannot be read."""
127
+ try:
128
+ if os.environ.get(cs.OPTION) != "true":
129
+ return None
130
+ return corroborate_state.budget_of(os.environ.get(cs.BUDGET_OPTION))
131
+ except BaseException:
132
+ logger.debug("auto: the option could not be read", exc_info=True)
133
+ return None
134
+
135
+
136
+ def _quietly(step) -> None:
137
+ """Run one step of `auto`; whatever it raises is logged at debug level and dropped."""
138
+ try:
139
+ step()
140
+ except BaseException:
141
+ logger.debug("auto: a step failed", exc_info=True)
142
+
143
+
144
+ def _logged_pass(key: str, run_pass) -> None:
145
+ """Run a pass and append its result and duration to the corroborate log."""
146
+ began = time.time()
147
+ stamp = datetime.datetime.now(datetime.UTC).isoformat(timespec="seconds")
148
+ result = run_pass()
149
+ corroborate_state.append_log(
150
+ corroborate_state.corroborate_dir(corroborate_state.data_dir()),
151
+ {"t": stamp, key: result, "ms": int((time.time() - began) * 1000)},
152
+ )
153
+
154
+
155
+ def _loaded_rules_passes(claude, budget: float) -> None:
156
+ """The passes over the lessons that load as rules; the optional ones fail on their own."""
157
+ if (
158
+ os.environ.get("CLAUDE_PLUGIN_OPTION_LOADED_RULES") == "false"
159
+ or os.environ.get("MEM_LOADED_RULES") == "off"
160
+ ):
161
+ return
162
+ _logged_pass("fill", lambda: corroborate_fill.fill_auto(claude, budget))
163
+ if (
164
+ os.environ.get("CLAUDE_PLUGIN_OPTION_AUTO_REWORD") == "true"
165
+ ): # option auto_reword, off by default
166
+ _quietly(
167
+ lambda: _logged_pass("reword", lambda: corroborate_reword.reword_auto(claude, budget))
168
+ )
169
+ if (
170
+ os.environ.get("CLAUDE_PLUGIN_OPTION_RULES_LAYOUT") == "digest"
171
+ ): # option rules_layout, "flat" by default
172
+ _quietly(
173
+ lambda: _logged_pass("digest", lambda: corroborate_digest.digest_auto(claude, budget))
174
+ )
175
+
176
+
177
+ def cmd_auto(args) -> int:
178
+ """The whole of `auto`; never raises and never prints."""
179
+ budget = _auto_budget()
180
+ if budget is None:
181
+ return 0
182
+ _quietly(lambda: corroborate_run.execute(args.claude, budget))
183
+ _quietly(lambda: _loaded_rules_passes(args.claude, budget))
184
+ return 0
185
+
186
+
187
+ def summary(line: dict) -> str:
188
+ if line["failed"]:
189
+ return f"mem-corroborate: failed: {line['failed']}"
190
+ if line["skipped"]:
191
+ return f"mem-corroborate: skipped: {line['skipped']}"
192
+ more = (
193
+ f", {line['failed_actions']} failed (tried again next run)"
194
+ if line["failed_actions"]
195
+ else ""
196
+ )
197
+ return (
198
+ f"mem-corroborate: {line['lessons']} lessons, {line['verified']} of {line['proposed']} groups applied: "
199
+ f"{line['written']} written, {line['sightings']} sightings, {line['links']} links, {line['merges']} merges, "
200
+ f"{line['refused']} refused{more}"
201
+ )
202
+
203
+
204
+ def self_check() -> int:
205
+ """No model: the briefs, the tool list, the gate of `auto`, the lock and the folder mode, the journal and undo."""
206
+ assert (
207
+ corroborate_state.budget_of("x") == corroborate_state.DEFAULT_BUDGET
208
+ and corroborate_state.budget_of("0.6") == _CHECK_BUDGET
209
+ and corroborate_state.budget_of("0") == corroborate_state.DEFAULT_BUDGET
210
+ )
211
+ corroborate_run.PROPOSE_BRIEF.format(lessons="a", out="b")
212
+ corroborate_run.VERIFY_BRIEF.format(groups="a", out="b")
213
+ corroborate_run.FILL_BRIEF.format(lessons="a", out="b")
214
+ corroborate_run.REWORD_BRIEF.format(lessons="a", out="b")
215
+ corroborate_run.DIGEST_BRIEF.format(topics="a", out="b")
216
+ corroborate_run.DIGEST_CHECK_BRIEF.format(pairs="a", out="b")
217
+ with tempfile.TemporaryDirectory(prefix="mem-corroborate-check-") as tmp_name:
218
+ tmp = Path(tmp_name)
219
+ os.environ.pop(cs.OPTION, None)
220
+ os.environ["MEM_DATA"] = str(tmp / "data")
221
+ cmd_auto(argparse.Namespace(claude="/nonexistent"))
222
+ assert not corroborate_state.corroborate_dir(tmp / "data").exists(), (
223
+ "auto ran without the option"
224
+ )
225
+ cdir = corroborate_state.corroborate_dir(tmp / "data")
226
+ held = jobs.take_run_lock(cdir)
227
+ assert held is not None and jobs.take_run_lock(cdir) is None
228
+ assert (cdir.stat().st_mode & 0o777) == _PRIVATE_MODE, oct(cdir.stat().st_mode)
229
+ os.close(held)
230
+ cmd = corroborate_run.session_cmd(
231
+ "claude",
232
+ tmp / "w",
233
+ "brief",
234
+ tmp / "s.json",
235
+ corroborate_run.SessionLimits("haiku", 0.25),
236
+ )
237
+ allowed = cmd[cmd.index("--allowedTools") + 1 : cmd.index("--disallowedTools")]
238
+ assert (
239
+ not any(t.startswith("Bash") for t in allowed)
240
+ and "Bash" in cmd[cmd.index("--disallowedTools") + 1 :]
241
+ )
242
+ assert all(
243
+ t.startswith(("Read(", "Write(", "Edit(")) and f"{tmp}/w" in t for t in allowed
244
+ ), allowed
245
+ run = cdir / "runs" / "x"
246
+ jobs.private_dir(run)
247
+ a, b = tmp / "a.md", tmp / "b.md"
248
+ a.write_text("one")
249
+ journal = corroborate_state.Journal(run)
250
+ watched = journal.watch([a, b])
251
+ a.write_text("two")
252
+ b.write_text("new")
253
+ journal.commit("check", watched)
254
+ assert corroborate_state.undo_run(run)[0] and a.read_text() == "one" and not b.exists()
255
+ a.write_text("two")
256
+ run2 = cdir / "runs" / "y"
257
+ jobs.private_dir(run2)
258
+ journal = corroborate_state.Journal(run2)
259
+ watched = journal.watch([a])
260
+ a.write_text("three")
261
+ journal.commit("check", watched)
262
+ a.write_text("edited")
263
+ assert not corroborate_state.undo_run(run2)[0] and a.read_text() == "edited", (
264
+ "undo ran over an edited file"
265
+ )
266
+ print("self-check passed")
267
+ return 0