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
memory_cli/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """The memory engine: the `mem` command and the background entries (`mem-auto`, `mem-settle`, ...)."""
memory_cli/__main__.py ADDED
@@ -0,0 +1,49 @@
1
+ """`python -m memory_cli [--bin NAME] ARGS`: runs the entry of one console script, or `mem` without `--bin`.
2
+
3
+ `engine/run.py` (the PEP 723 launcher behind `plugin/bin/`) and the children the engine starts for itself
4
+ (`core.resources.mem_command`) both come here. `mem` is imported only for the default path, so Python caches mem.pyc.
5
+ """
6
+
7
+ import sys
8
+
9
+ if __name__ == "__main__":
10
+ # One static import per name, so the import graph sees each edge; the other entries stay unloaded.
11
+ name = sys.argv[2] if sys.argv[1:2] == ["--bin"] and sys.argv[2:3] else ""
12
+ if name == "mem-auto":
13
+ from memory_cli.entry import auto
14
+
15
+ main = auto.main
16
+ elif name == "mem-capture":
17
+ from memory_cli.entry import capture
18
+
19
+ main = capture.main
20
+ elif name == "mem-corroborate":
21
+ from memory_cli.entry import corroborate
22
+
23
+ main = corroborate.main
24
+ elif name == "mem-integrate":
25
+ from memory_cli.entry import integrate
26
+
27
+ main = integrate.main
28
+ elif name == "mem-research":
29
+ from memory_cli.entry import research
30
+
31
+ main = research.main
32
+ elif name == "mem-settle":
33
+ from memory_cli.entry import settle
34
+
35
+ main = settle.main
36
+ elif name == "mem-synthesize":
37
+ from memory_cli.entry import synthesize
38
+
39
+ main = synthesize.main
40
+ elif sys.argv[1:2] == ["--bin"]:
41
+ print(f"memory_cli: unknown --bin name: {name!r}", file=sys.stderr)
42
+ sys.exit(2)
43
+ else:
44
+ from memory_cli import mem
45
+
46
+ main = mem.main
47
+ if name:
48
+ sys.argv = [name, *sys.argv[3:]]
49
+ sys.exit(main())
@@ -0,0 +1,7 @@
1
+ """Transcript capture: reducing a session transcript to candidate memories and the state of the background run.
2
+
3
+ `extract`: transcript to compact extract, candidate validation
4
+ `codex_rollout`: a Codex rollout to the same extract lines
5
+ `event_lines`: the line pieces both decoders share
6
+ `state`: state of the background capture run
7
+ """
@@ -0,0 +1,164 @@
1
+ """Decode a Codex rollout (`~/.codex/sessions/.../rollout-*.jsonl`) into extract event lines.
2
+
3
+ Same line format as the Claude decoder in `extract`. The owner's words come only from
4
+ `response_item` messages of role `user` (the `event_msg` copies are duplicates); injected
5
+ context and developer messages are not the owner's. Shares the line limits and the tool
6
+ bookkeeping with the Claude decoder through `event_lines`.
7
+ """
8
+
9
+ import json
10
+ import re
11
+
12
+ from memory_cli.capture import event_lines as ev
13
+
14
+ # Only known injected envelopes are filtered: arbitrary snake_case XML can be owner text.
15
+ _INJECTED = re.compile(
16
+ r"# AGENTS\.md instructions(?: for /[^\n]+)?[ \t]*\n\s*<INSTRUCTIONS>.*</INSTRUCTIONS>|"
17
+ r"<(environment_context|codex_internal_context|external_codex_apps_[a-z0-9_]+|"
18
+ r"send_user_message_question_reply)(?:\s[^>]*)?>.*</\1>",
19
+ re.DOTALL,
20
+ )
21
+ _SUBAGENT_ARGS = ("message", "cmd", "target") # JSON keys of the Codex subagent tools
22
+ _SUBAGENT = "\0subagent" # state key: this rollout is a Codex subagent thread, which yields nothing
23
+ _FAILED_PREFIX = "Script failed"
24
+ _PROCESS_EXIT = re.compile(r"^Process exited with code (-?\d+)(?:\r?\n|$)")
25
+ _OUTPUT_MARK = "Output:\n"
26
+ _EXEC_MIN_PARTS = 2 # header and structured result
27
+ # Outer types that carry no content for the extract; any other outer type is counted as skipped.
28
+ _KNOWN_OUTER = (
29
+ "session_meta",
30
+ "turn_context",
31
+ "event_msg",
32
+ "response_item",
33
+ "compacted",
34
+ "token_usage_record",
35
+ "world_state",
36
+ "inter_agent_communication_metadata",
37
+ )
38
+ _CALLS = ("custom_tool_call", "function_call")
39
+ _OUTPUTS = ("custom_tool_call_output", "function_call_output")
40
+
41
+
42
+ def is_rollout(first_line: str) -> bool:
43
+ """True when the first line of a file is a Codex `session_meta` item."""
44
+ try:
45
+ entry = json.loads(first_line)
46
+ except (ValueError, RecursionError): # a first line nested deeper than the parser allows
47
+ return False
48
+ return isinstance(entry, dict) and entry.get("type") == "session_meta"
49
+
50
+
51
+ # not `event_lines.text_of`: Codex parts are typed `input_text`/`output_text`, which it skips
52
+ def _texts(content: object) -> list[str]:
53
+ if not isinstance(content, list):
54
+ return []
55
+ return [b["text"] for b in content if isinstance(b, dict) and isinstance(b.get("text"), str)]
56
+
57
+
58
+ def _message_events(payload: dict) -> list[str]:
59
+ role = payload.get("role")
60
+ if role == "user":
61
+ texts = [t for t in _texts(payload.get("content")) if not _INJECTED.fullmatch(t.strip())]
62
+ body = ev.flat(" ".join(texts), ev.HUMAN_LIMIT)
63
+ return [f"OWNER: {body}"] if body else []
64
+ if role == "assistant":
65
+ body = ev.flat(" ".join(_texts(payload.get("content"))), ev.AGENT_LIMIT)
66
+ return [f"AGENT: {body}"] if body else []
67
+ return []
68
+
69
+
70
+ def _argument(payload: dict) -> str:
71
+ raw = payload.get("input") if payload.get("type") == "custom_tool_call" else None
72
+ if isinstance(raw, str):
73
+ return raw
74
+ try:
75
+ args = json.loads(payload.get("arguments") or "")
76
+ except (TypeError, ValueError):
77
+ return ""
78
+ return ev.tool_arg(args) or _subagent_arg(args)
79
+
80
+
81
+ def _subagent_arg(args: object) -> str:
82
+ if not isinstance(args, dict):
83
+ return ""
84
+ values = (args.get(key) for key in _SUBAGENT_ARGS)
85
+ return next((v for v in values if isinstance(v, str) and v), "")
86
+
87
+
88
+ def _call_events(payload: dict, names: dict) -> list[str]:
89
+ block = {
90
+ "name": payload.get("name"),
91
+ "id": payload.get("call_id"),
92
+ "input": {"command": _argument(payload)},
93
+ }
94
+ return [ev.tool_use_line(block, names)]
95
+
96
+
97
+ def _output_text(output: object) -> str:
98
+ if isinstance(output, dict):
99
+ return _output_text(output.get("output", output.get("content")))
100
+ return output if isinstance(output, str) else " ".join(_texts(output))
101
+
102
+
103
+ def _exec_result(output: object) -> dict | None:
104
+ """Decode the shell runner's metadata part, separate from the command's own output."""
105
+ parts = _texts(output)
106
+ if len(parts) < _EXEC_MIN_PARTS or not parts[0].startswith("Script completed\nWall time "):
107
+ return None
108
+ try:
109
+ meta = json.loads(parts[1])
110
+ except (ValueError, RecursionError):
111
+ return None
112
+ if (
113
+ isinstance(meta, dict)
114
+ and {"chunk_id", "wall_time_seconds", "exit_code", "output"} <= meta.keys()
115
+ ):
116
+ return meta
117
+ return None
118
+
119
+
120
+ def _output_events(payload: dict, names: dict, state: dict) -> list[str]:
121
+ output = payload.get("output")
122
+ name = names.get(payload.get("call_id"), "?")
123
+ if name == "exec":
124
+ output = _exec_result(output) or output
125
+ text = _output_text(output)
126
+ exit_code = output.get("exit_code") if isinstance(output, dict) else payload.get("exit_code")
127
+ process_exit = _PROCESS_EXIT.match(text)
128
+ failed = (
129
+ (isinstance(exit_code, int) and not isinstance(exit_code, bool) and exit_code != 0)
130
+ or payload.get("is_error") is True
131
+ or (isinstance(output, dict) and output.get("is_error") is True)
132
+ or text.startswith(_FAILED_PREFIX)
133
+ or (process_exit is not None and process_exit.group(1).lstrip("-").strip("0"))
134
+ )
135
+ if failed:
136
+ text = text.partition(_OUTPUT_MARK)[2] or text
137
+ event = ev.result_event({"is_error": failed, "content": text}, name, state)
138
+ return [event] if event is not None else []
139
+
140
+
141
+ def _item_events(payload: dict, names: dict, state: dict) -> list[str]:
142
+ kind = payload.get("type")
143
+ if kind == "message":
144
+ return _message_events(payload)
145
+ if kind in _CALLS:
146
+ return _call_events(payload, names)
147
+ if kind in _OUTPUTS:
148
+ return _output_events(payload, names, state)
149
+ return []
150
+
151
+
152
+ def events(entry: dict, names: dict, state: dict) -> list[str]:
153
+ """Extract lines (without the line number prefix) of one rollout item."""
154
+ kind = entry.get("type")
155
+ payload = entry.get("payload")
156
+ if kind not in _KNOWN_OUTER:
157
+ raise ValueError(f"unknown rollout item type: {kind!r}")
158
+ if not isinstance(payload, dict):
159
+ return []
160
+ if kind == "session_meta":
161
+ state[_SUBAGENT] = payload.get("thread_source", "user") != "user"
162
+ if kind != "response_item" or state.get(_SUBAGENT):
163
+ return []
164
+ return _item_events(payload, names, state)
@@ -0,0 +1,66 @@
1
+ """Pieces of an extract line that every transcript decoder shares: the length limits and how a text,
2
+ a tool call and a tool result become a line. Pure; standard library only."""
3
+
4
+ HUMAN_LIMIT = 2000 # an owner's words are kept longer than a subagent report
5
+ REPORT_LIMIT = 800
6
+ AGENT_LIMIT = 1500
7
+ ARG_LIMIT = 200
8
+ ERROR_LIMIT = 400
9
+ _TOOL_ARGS = ("command", "file_path", "description")
10
+
11
+
12
+ def flat(text: str, limit: int) -> str:
13
+ # cut before normalizing so memory does not follow the size of the line; a text that is mostly
14
+ # whitespace can come out shorter than `limit` with its tail lost, which is accepted
15
+ return " ".join(text[: limit * 8].split())[:limit].strip()
16
+
17
+
18
+ def text_of(content: object) -> str:
19
+ """Text of a string or of the text blocks of a list; "" otherwise."""
20
+ if isinstance(content, str):
21
+ return content
22
+ if isinstance(content, list):
23
+ parts = []
24
+ for b in content:
25
+ if isinstance(b, str):
26
+ parts.append(b)
27
+ elif (
28
+ isinstance(b, dict)
29
+ and b.get("type", "text") == "text"
30
+ and isinstance(b.get("text"), str)
31
+ ):
32
+ parts.append(b["text"])
33
+ return " ".join(parts)
34
+ return ""
35
+
36
+
37
+ def name_of(value: object) -> str:
38
+ return value if isinstance(value, str) and value else "?"
39
+
40
+
41
+ def tool_arg(inp: object) -> str:
42
+ if not isinstance(inp, dict):
43
+ return ""
44
+ for key in _TOOL_ARGS:
45
+ value = inp.get(key)
46
+ if isinstance(value, str):
47
+ return value
48
+ return ""
49
+
50
+
51
+ def tool_use_line(b: dict, names: dict) -> str:
52
+ name = name_of(b.get("name"))
53
+ if isinstance(b.get("id"), str):
54
+ names[b["id"]] = name
55
+ return f"TOOL {name}: {flat(tool_arg(b.get('input')), ARG_LIMIT)}"
56
+
57
+
58
+ def result_event(b: dict, name: str, state: dict) -> str | None:
59
+ if b.get("is_error"):
60
+ state[name] = True
61
+ return f"TOOL ERROR {name}: {flat(text_of(b.get('content')), ERROR_LIMIT)}"
62
+ was_failing = state.get(name)
63
+ state[name] = False
64
+ if name == "AskUserQuestion":
65
+ return f"OWNER ANSWER: {flat(text_of(b.get('content')), REPORT_LIMIT)}"
66
+ return f"TOOL OK {name}" if was_failing else None