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,237 @@
1
+ """Reading a memory: a Markdown file with YAML frontmatter becomes a Memory, tolerating what a hand edit may leave."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import datetime
6
+ import logging
7
+ import re
8
+ from dataclasses import replace
9
+ from typing import TYPE_CHECKING
10
+
11
+ from memory_cli.core.yaml_load import safe_load
12
+ from memory_cli.memories.model import (
13
+ KINDS,
14
+ ORIGINS,
15
+ REACHES,
16
+ STATUSES,
17
+ TRIAGES,
18
+ WEIGHTS,
19
+ FieldTypeError,
20
+ Memory,
21
+ valid_short,
22
+ valid_topic,
23
+ valid_triage_reason,
24
+ )
25
+ from memory_cli.memories.sightings import (
26
+ _version,
27
+ case,
28
+ )
29
+
30
+ if TYPE_CHECKING:
31
+ from collections.abc import Sequence
32
+ from pathlib import Path
33
+
34
+
35
+ logger = logging.getLogger(__name__)
36
+
37
+
38
+ def _date(value, name: str) -> datetime.date | None:
39
+ if value is None:
40
+ return None
41
+ if isinstance(value, datetime.datetime):
42
+ raise FieldTypeError(f"{name} must be a date")
43
+ if isinstance(value, datetime.date):
44
+ return value
45
+ if isinstance(value, str):
46
+ return datetime.date.fromisoformat(value)
47
+ raise FieldTypeError(f"{name} must be an ISO date")
48
+
49
+
50
+ def _split(text: str) -> tuple[dict, str]:
51
+ m = re.match(r"---\r?\n(.*?)\r?\n---[ \t]*(?:\r?\n|$)", text, re.DOTALL)
52
+ if not m:
53
+ raise ValueError("no YAML frontmatter")
54
+ meta = safe_load(m.group(1))
55
+ if not isinstance(meta, dict):
56
+ raise FieldTypeError("frontmatter is not a mapping")
57
+ return meta, text[m.end() :]
58
+
59
+
60
+ def _checked_kind(meta: dict) -> str:
61
+ kind = meta.get("kind")
62
+ if kind not in KINDS:
63
+ raise ValueError(f"unknown kind {kind!r}")
64
+ return kind
65
+
66
+
67
+ def _checked_claim(meta: dict) -> str:
68
+ claim = meta.get("claim")
69
+ if not isinstance(claim, str) or not claim.strip():
70
+ raise ValueError("claim is missing")
71
+ return claim
72
+
73
+
74
+ def _checked_observed(meta: dict) -> int:
75
+ observed = meta.get("observed", 1)
76
+ if isinstance(observed, bool) or not isinstance(observed, int) or observed < 1:
77
+ raise ValueError("observed must be an integer of at least 1")
78
+ return observed
79
+
80
+
81
+ def _checked_status(meta: dict) -> str:
82
+ status = meta.get("status", "active")
83
+ if status not in STATUSES:
84
+ raise ValueError(f"unknown status {status!r}")
85
+ return status
86
+
87
+
88
+ def _checked_context(meta: dict) -> str | None:
89
+ context = meta.get("context")
90
+ if context is not None and not isinstance(context, str):
91
+ raise ValueError("context must be a string")
92
+ return context
93
+
94
+
95
+ def _checked_sources(meta: dict) -> list[str]:
96
+ sources = meta.get("sources") or []
97
+ if not isinstance(sources, list) or not all(isinstance(s, str) for s in sources):
98
+ raise ValueError("sources must be a list of strings")
99
+ return list(sources)
100
+
101
+
102
+ def _required_fields(meta: dict) -> dict:
103
+ """The fields a memory needs, each checked in this order; the first bad one raises ValueError."""
104
+ return {
105
+ "kind": _checked_kind(meta),
106
+ "claim": _checked_claim(meta).strip(),
107
+ "observed": _checked_observed(meta),
108
+ "status": _checked_status(meta),
109
+ "context": _checked_context(meta),
110
+ "sources": _checked_sources(meta),
111
+ }
112
+
113
+
114
+ # a bad value reads as absent: only curation reads these dates, and one must not hide the memory
115
+ _LENIENT_DATES = (
116
+ "last_observed",
117
+ "refuted",
118
+ "reviewed",
119
+ "reworded",
120
+ "reword_failed",
121
+ "owner_confirmed",
122
+ "released",
123
+ )
124
+
125
+
126
+ def _lenient_date(meta: dict, key: str) -> datetime.date | None:
127
+ try:
128
+ return _date(meta.get(key), key)
129
+ except ValueError:
130
+ return None
131
+
132
+
133
+ _PROMOTE_NO = ("no", False) # a tuple, not a set: a YAML list or mapping is unhashable
134
+
135
+
136
+ def _promote_of(meta: dict) -> str | None:
137
+ value = meta.get("promote") # YAML reads a bare no as False
138
+ return "no" if value in _PROMOTE_NO else "done" if value == "done" else None
139
+
140
+
141
+ def _load_of(meta: dict) -> str | None:
142
+ value = meta.get("load") # YAML reads a bare no as False; 0 is not no
143
+ return "index" if value == "index" else "no" if value == "no" or value is False else None
144
+
145
+
146
+ def _choice(meta: dict, key: str, allowed: Sequence[str]) -> str | None:
147
+ value = meta.get(key)
148
+ return value if value in allowed else None
149
+
150
+
151
+ def _text_fields(meta: dict) -> dict:
152
+ """The optional text fields; a bad value reads as absent."""
153
+ superseded_by = meta.get("superseded_by")
154
+ general = meta.get("general")
155
+ triage = _choice(meta, "triage", TRIAGES)
156
+ reason = meta.get("triage_reason")
157
+ return {
158
+ "superseded_by": str(superseded_by) if superseded_by else None,
159
+ "general": general if isinstance(general, str) and general.strip() else None,
160
+ "short": meta.get("short") if valid_short(meta.get("short")) else None,
161
+ "short_by": "model" if meta.get("short_by") == "model" else None,
162
+ "triage": triage,
163
+ "triage_reason": reason if triage == "pending" and valid_triage_reason(reason) else None,
164
+ "topic": meta.get("topic") if valid_topic(meta.get("topic")) else None,
165
+ }
166
+
167
+
168
+ def _list_fields(meta: dict) -> dict:
169
+ """The optional list fields; a bad value reads as absent."""
170
+ distinct = meta.get("distinct_from")
171
+ raw_log = meta.get("log")
172
+ raw_versions = meta.get("versions")
173
+ return {
174
+ "distinct_from": [str(h) for h in distinct] if isinstance(distinct, list) else [],
175
+ "log": [e for e in raw_log if isinstance(e, str) and case(e)]
176
+ if isinstance(raw_log, list)
177
+ else [],
178
+ "versions": sorted((e for e in raw_versions if _version(e)), key=lambda e: e[:10])
179
+ if isinstance(raw_versions, list)
180
+ else [],
181
+ }
182
+
183
+
184
+ def _optional_fields(meta: dict) -> dict:
185
+ return {
186
+ **{key: _lenient_date(meta, key) for key in _LENIENT_DATES},
187
+ **_text_fields(meta),
188
+ **_list_fields(meta),
189
+ "promote": _promote_of(meta),
190
+ "load": _load_of(meta),
191
+ "origin": _choice(meta, "origin", ORIGINS),
192
+ "reach": _choice(meta, "reach", REACHES),
193
+ "weight": _choice(meta, "weight", WEIGHTS),
194
+ }
195
+
196
+
197
+ def _parse_text(base: Memory, text: str) -> Memory:
198
+ meta, body = _split(text)
199
+ required = _required_fields(meta)
200
+ optional = _optional_fields(meta)
201
+ return replace(
202
+ base,
203
+ **required,
204
+ **optional,
205
+ created=_date(meta.get("created"), "created"),
206
+ valid_until=_date(meta.get("valid_until"), "valid_until"),
207
+ body=body,
208
+ )
209
+
210
+
211
+ def _parse(path: Path, scope: str) -> Memory:
212
+ base = Memory(scope, path, path.stem, "lesson", "", None, 1, "active", None, None, [], "", None)
213
+ try:
214
+ return _parse_text(base, path.read_text(encoding="utf-8"))
215
+ except Exception as e: # one broken file must not break the rest
216
+ logger.debug("cannot parse %s", path, exc_info=True)
217
+ name = (
218
+ "ValueError" if isinstance(e, FieldTypeError) else type(e).__name__
219
+ ) # the error text predates the subclass
220
+ return replace(base, error=f"{name}: {e}".splitlines()[0])
221
+
222
+
223
+ def load_memories(folder: Path | None, scope: str) -> list[Memory]:
224
+ try:
225
+ if folder is None or not folder.is_dir():
226
+ return []
227
+ if (
228
+ scope == "project" and folder.is_symlink()
229
+ ): # a cloned repository's planted link; global folders may be synced links
230
+ return []
231
+ return [
232
+ _parse(p, scope)
233
+ for p in sorted(folder.glob("*.md"), key=lambda p: p.stem)
234
+ if p.is_file() and not (scope == "project" and p.is_symlink())
235
+ ]
236
+ except OSError:
237
+ return []
@@ -0,0 +1,266 @@
1
+ """The records facade: re-exports the names importers reach as `records.<name>`; the code lives in the sibling modules.
2
+
3
+ Holds imports and `__all__` only. Store modules import the defining module, never this one (test_import_graph).
4
+ Names that are not the store's own (`safe_load`, `datetime`, `replace`, `Path`) are here because importers reach them through it.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import datetime
10
+ from dataclasses import replace
11
+ from pathlib import Path
12
+
13
+ from memory_cli.core.yaml_load import safe_load
14
+ from memory_cli.memories.journal import (
15
+ CURATION_LOG,
16
+ Logged,
17
+ last_log_entry,
18
+ log_before,
19
+ restore,
20
+ )
21
+ from memory_cli.memories.locations import (
22
+ REGISTRY_FILE,
23
+ _main_worktree,
24
+ default_store,
25
+ ensure_excluded,
26
+ forget_project,
27
+ global_memories_root,
28
+ mark_repository,
29
+ memories_root,
30
+ project_dir,
31
+ project_key,
32
+ project_root,
33
+ read_registry,
34
+ register_project,
35
+ tracked_memories,
36
+ under_temp,
37
+ )
38
+ from memory_cli.memories.matching import (
39
+ MIN_TERMS,
40
+ OPPOSITES,
41
+ POSSIBLE,
42
+ STOPWORDS,
43
+ STRONG,
44
+ Budget,
45
+ MatchOptions,
46
+ PairSearch,
47
+ Related,
48
+ containment,
49
+ differing_words,
50
+ flagged_words,
51
+ match_memories,
52
+ pairs,
53
+ pairs_by,
54
+ refuted_pairs,
55
+ related_records,
56
+ similarity,
57
+ tokenize,
58
+ )
59
+ from memory_cli.memories.model import (
60
+ GLOBAL_DIR,
61
+ GLOBAL_MIN_PROJECTS,
62
+ KINDS,
63
+ ORIGINS,
64
+ OUTCOMES,
65
+ PROJECT_DIR,
66
+ PROJECT_KEY_LEN,
67
+ REACHES,
68
+ SESSION_PREFIX,
69
+ SHORT_DATE_LEN,
70
+ SHORT_LIMIT,
71
+ SIGHTINGS_NEEDED,
72
+ STATUSES,
73
+ STORE_MARKER,
74
+ TOPIC_LIMIT,
75
+ TRIAGE_REASON_LIMIT,
76
+ TRIAGES,
77
+ UNCONFIRMED_DAYS,
78
+ WEIGHTS,
79
+ FieldTypeError,
80
+ Memory,
81
+ is_live,
82
+ slug_of,
83
+ valid_short,
84
+ valid_topic,
85
+ valid_triage_reason,
86
+ )
87
+ from memory_cli.memories.parse import (
88
+ _parse,
89
+ load_memories,
90
+ logger,
91
+ )
92
+ from memory_cli.memories.sightings import (
93
+ FAILED_TO_LIST,
94
+ MAX_SESSION_ID,
95
+ Case,
96
+ Tally,
97
+ case,
98
+ case_entry,
99
+ cases,
100
+ counted,
101
+ got_worse,
102
+ has_case,
103
+ mark_words,
104
+ merged_log,
105
+ merged_versions,
106
+ normalized_sources,
107
+ projects,
108
+ quarantined,
109
+ reword_note,
110
+ session_key,
111
+ sessions,
112
+ sighting,
113
+ sighting_entry,
114
+ standing,
115
+ standing_changes,
116
+ tallies,
117
+ version_entry,
118
+ version_list,
119
+ )
120
+ from memory_cli.memories.terms import (
121
+ SECRET_PATTERNS,
122
+ TERM_KINDS,
123
+ TERMS_HEADER,
124
+ ForbiddenListError,
125
+ Term,
126
+ ensure_terms_file,
127
+ forbidden_file,
128
+ forbidden_line,
129
+ inside,
130
+ parse_term,
131
+ read_terms,
132
+ secret_in,
133
+ store_is_writable,
134
+ term_hits,
135
+ )
136
+ from memory_cli.memories.write import (
137
+ SightingOptions,
138
+ WriteOptions,
139
+ add_case,
140
+ add_sighting,
141
+ observe,
142
+ update,
143
+ write_memory,
144
+ )
145
+
146
+ __all__ = [
147
+ "CURATION_LOG",
148
+ "FAILED_TO_LIST",
149
+ "GLOBAL_DIR",
150
+ "GLOBAL_MIN_PROJECTS",
151
+ "KINDS",
152
+ "MAX_SESSION_ID",
153
+ "MIN_TERMS",
154
+ "OPPOSITES",
155
+ "ORIGINS",
156
+ "OUTCOMES",
157
+ "POSSIBLE",
158
+ "PROJECT_DIR",
159
+ "PROJECT_KEY_LEN",
160
+ "REACHES",
161
+ "REGISTRY_FILE",
162
+ "SECRET_PATTERNS",
163
+ "SESSION_PREFIX",
164
+ "SHORT_DATE_LEN",
165
+ "SHORT_LIMIT",
166
+ "SIGHTINGS_NEEDED",
167
+ "STATUSES",
168
+ "STOPWORDS",
169
+ "STORE_MARKER",
170
+ "STRONG",
171
+ "TERMS_HEADER",
172
+ "TERM_KINDS",
173
+ "TOPIC_LIMIT",
174
+ "TRIAGES",
175
+ "TRIAGE_REASON_LIMIT",
176
+ "UNCONFIRMED_DAYS",
177
+ "WEIGHTS",
178
+ "Budget",
179
+ "Case",
180
+ "FieldTypeError",
181
+ "ForbiddenListError",
182
+ "Logged",
183
+ "MatchOptions",
184
+ "Memory",
185
+ "PairSearch",
186
+ "Path",
187
+ "Related",
188
+ "SightingOptions",
189
+ "Tally",
190
+ "Term",
191
+ "WriteOptions",
192
+ "_main_worktree",
193
+ "_parse",
194
+ "add_case",
195
+ "add_sighting",
196
+ "case",
197
+ "case_entry",
198
+ "cases",
199
+ "containment",
200
+ "counted",
201
+ "datetime",
202
+ "default_store",
203
+ "differing_words",
204
+ "ensure_excluded",
205
+ "ensure_terms_file",
206
+ "flagged_words",
207
+ "forbidden_file",
208
+ "forbidden_line",
209
+ "forget_project",
210
+ "global_memories_root",
211
+ "got_worse",
212
+ "has_case",
213
+ "inside",
214
+ "is_live",
215
+ "last_log_entry",
216
+ "load_memories",
217
+ "log_before",
218
+ "logger",
219
+ "mark_repository",
220
+ "mark_words",
221
+ "match_memories",
222
+ "memories_root",
223
+ "merged_log",
224
+ "merged_versions",
225
+ "normalized_sources",
226
+ "observe",
227
+ "pairs",
228
+ "pairs_by",
229
+ "parse_term",
230
+ "project_dir",
231
+ "project_key",
232
+ "project_root",
233
+ "projects",
234
+ "quarantined",
235
+ "read_registry",
236
+ "read_terms",
237
+ "refuted_pairs",
238
+ "register_project",
239
+ "related_records",
240
+ "replace",
241
+ "restore",
242
+ "reword_note",
243
+ "safe_load",
244
+ "secret_in",
245
+ "session_key",
246
+ "sessions",
247
+ "sighting",
248
+ "sighting_entry",
249
+ "similarity",
250
+ "slug_of",
251
+ "standing",
252
+ "standing_changes",
253
+ "store_is_writable",
254
+ "tallies",
255
+ "term_hits",
256
+ "tokenize",
257
+ "tracked_memories",
258
+ "under_temp",
259
+ "update",
260
+ "valid_short",
261
+ "valid_topic",
262
+ "valid_triage_reason",
263
+ "version_entry",
264
+ "version_list",
265
+ "write_memory",
266
+ ]