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.
- memory_cli/__init__.py +1 -0
- memory_cli/__main__.py +49 -0
- memory_cli/capture/__init__.py +7 -0
- memory_cli/capture/codex_rollout.py +164 -0
- memory_cli/capture/event_lines.py +66 -0
- memory_cli/capture/extract.py +458 -0
- memory_cli/capture/state.py +183 -0
- memory_cli/commands/__init__.py +21 -0
- memory_cli/commands/admin.py +363 -0
- memory_cli/commands/common.py +609 -0
- memory_cli/commands/curate.py +1189 -0
- memory_cli/commands/diagnostics.py +126 -0
- memory_cli/commands/edit.py +1282 -0
- memory_cli/commands/hooks.py +730 -0
- memory_cli/commands/import_recovery.py +45 -0
- memory_cli/commands/install.py +152 -0
- memory_cli/commands/integrate.py +480 -0
- memory_cli/commands/memory.py +2152 -0
- memory_cli/commands/read.py +1290 -0
- memory_cli/commands/sources.py +766 -0
- memory_cli/commands/split.py +97 -0
- memory_cli/commands/split_plan.py +196 -0
- memory_cli/commands/split_transaction.py +230 -0
- memory_cli/commands/synthesis.py +1122 -0
- memory_cli/core/__init__.py +16 -0
- memory_cli/core/auto_state.py +30 -0
- memory_cli/core/codex_pass.py +109 -0
- memory_cli/core/config.py +87 -0
- memory_cli/core/file_journal.py +163 -0
- memory_cli/core/headless.py +130 -0
- memory_cli/core/jobs.py +130 -0
- memory_cli/core/resources.py +57 -0
- memory_cli/core/runner.py +398 -0
- memory_cli/core/safe_io.py +155 -0
- memory_cli/core/store_git.py +407 -0
- memory_cli/core/yaml_load.py +14 -0
- memory_cli/corroborate/__init__.py +11 -0
- memory_cli/corroborate/cli.py +267 -0
- memory_cli/corroborate/decide.py +893 -0
- memory_cli/corroborate/digest.py +425 -0
- memory_cli/corroborate/fill.py +744 -0
- memory_cli/corroborate/job.py +15 -0
- memory_cli/corroborate/reword.py +446 -0
- memory_cli/corroborate/run.py +1142 -0
- memory_cli/corroborate/state.py +300 -0
- memory_cli/digest/__init__.py +4 -0
- memory_cli/digest/lessons.py +483 -0
- memory_cli/entry/__init__.py +12 -0
- memory_cli/entry/auto.py +660 -0
- memory_cli/entry/capture.py +1403 -0
- memory_cli/entry/corroborate.py +214 -0
- memory_cli/entry/integrate.py +684 -0
- memory_cli/entry/integrate_self_check.py +203 -0
- memory_cli/entry/research.py +9 -0
- memory_cli/entry/settle.py +614 -0
- memory_cli/entry/split_auto.py +460 -0
- memory_cli/entry/synthesize.py +22 -0
- memory_cli/integrate/__init__.py +9 -0
- memory_cli/integrate/answers.py +42 -0
- memory_cli/integrate/briefs/check-brief.md +16 -0
- memory_cli/integrate/briefs/place-brief.md +26 -0
- memory_cli/integrate/log.py +43 -0
- memory_cli/integrate/plan.py +125 -0
- memory_cli/integrate/rows.py +99 -0
- memory_cli/integrate/state.py +158 -0
- memory_cli/integrate/undo.py +143 -0
- memory_cli/mem.py +1180 -0
- memory_cli/memories/__init__.py +16 -0
- memory_cli/memories/fingerprints.py +51 -0
- memory_cli/memories/journal.py +93 -0
- memory_cli/memories/locations.py +282 -0
- memory_cli/memories/matching.py +451 -0
- memory_cli/memories/model.py +205 -0
- memory_cli/memories/parse.py +237 -0
- memory_cli/memories/records.py +266 -0
- memory_cli/memories/rules.py +291 -0
- memory_cli/memories/sightings.py +314 -0
- memory_cli/memories/terms.py +148 -0
- memory_cli/memories/write.py +358 -0
- memory_cli/notes/__init__.py +13 -0
- memory_cli/notes/check.py +806 -0
- memory_cli/notes/due.py +260 -0
- memory_cli/notes/fields.py +217 -0
- memory_cli/notes/index.py +230 -0
- memory_cli/notes/links.py +170 -0
- memory_cli/notes/note.py +222 -0
- memory_cli/notes/search.py +391 -0
- memory_cli/notes/split_candidates.py +72 -0
- memory_cli/research/__init__.py +5 -0
- memory_cli/research/checks.py +1065 -0
- memory_cli/research/pipeline.py +952 -0
- memory_cli/session/__init__.py +5 -0
- memory_cli/session/handoff.py +139 -0
- memory_cli/session/notices.py +111 -0
- memory_cli/settle/__init__.py +5 -0
- memory_cli/settle/state.py +111 -0
- memory_cli/settle/verdicts.py +693 -0
- memory_cli/split/briefs/checker-brief.md +5 -0
- memory_cli/split/briefs/editor-brief.md +20 -0
- memory_cli/syntheses/__init__.py +8 -0
- memory_cli/syntheses/document.py +400 -0
- memory_cli/syntheses/edit.py +581 -0
- memory_cli/syntheses/project.py +384 -0
- memory_cli/syntheses/split_conservation.py +50 -0
- memory_cli/syntheses/units.py +427 -0
- memory_cli/synthesize/__init__.py +9 -0
- memory_cli/synthesize/backfill.py +431 -0
- memory_cli/synthesize/briefs/backfill-brief.md +26 -0
- memory_cli/synthesize/briefs/checker-brief.md +13 -0
- memory_cli/synthesize/briefs/fixer-brief.md +19 -0
- memory_cli/synthesize/briefs/memory-brief.md +30 -0
- memory_cli/synthesize/briefs/settle-brief.md +32 -0
- memory_cli/synthesize/briefs/verdict-brief.md +50 -0
- memory_cli/synthesize/briefs/web-brief.md +33 -0
- memory_cli/synthesize/cli.py +311 -0
- memory_cli/synthesize/pipeline.py +764 -0
- memory_cli/synthesize/references.py +164 -0
- memory_cli/synthesize/run.py +150 -0
- memory_cli/synthesize/topics.py +528 -0
- memory_cli/templates/knowledge.md +23 -0
- memory_cli/templates/literature.md +25 -0
- memory_cli/templates/research.md +23 -0
- memory_cli/templates/synthesis.md +6 -0
- memory_cli-0.1.0.dist-info/METADATA +102 -0
- memory_cli-0.1.0.dist-info/RECORD +128 -0
- memory_cli-0.1.0.dist-info/WHEEL +4 -0
- memory_cli-0.1.0.dist-info/entry_points.txt +9 -0
- memory_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
"""mem-corroborate: `digest`, a heading per topic and a line per lesson for the session start."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import datetime
|
|
6
|
+
import hashlib
|
|
7
|
+
import json
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
import shutil
|
|
11
|
+
import tempfile
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import NamedTuple
|
|
14
|
+
|
|
15
|
+
from memory_cli.core import jobs
|
|
16
|
+
from memory_cli.core import runner as runners
|
|
17
|
+
from memory_cli.corroborate import decide as corroborate_decide
|
|
18
|
+
from memory_cli.corroborate import job as corroborate_job
|
|
19
|
+
from memory_cli.corroborate import run as corroborate_run
|
|
20
|
+
from memory_cli.corroborate import state as corroborate_state
|
|
21
|
+
from memory_cli.digest import lessons as dg
|
|
22
|
+
from memory_cli.memories import records
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
DIGEST_ATTEMPTS_NAME = "digest-attempts.json"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
DIGEST_WRITER_OUT = "digest-write.json"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
DIGEST_CHECK_OUT = "digest-check.json"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _digest_state(cdir: Path) -> dict:
|
|
36
|
+
"""The step's memory, read defensively: a missing or broken file is an empty state."""
|
|
37
|
+
data = jobs.read_json(cdir / DIGEST_ATTEMPTS_NAME)
|
|
38
|
+
tries = data.get("tries")
|
|
39
|
+
failures = data.get("failures")
|
|
40
|
+
tries = (
|
|
41
|
+
{k: v for k, v in tries.items() if isinstance(k, str) and isinstance(v, int) and v > 0}
|
|
42
|
+
if isinstance(tries, dict)
|
|
43
|
+
else {}
|
|
44
|
+
)
|
|
45
|
+
failures = failures if isinstance(failures, dict) else {}
|
|
46
|
+
count = failures.get("count")
|
|
47
|
+
return {
|
|
48
|
+
"tries": tries,
|
|
49
|
+
"failures": {
|
|
50
|
+
"count": count if isinstance(count, int) and count > 0 else 0,
|
|
51
|
+
"fingerprint": failures.get("fingerprint"),
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _one_line(text: object) -> str:
|
|
57
|
+
return " ".join(str(text).split())
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _digest_pair_text(pairs: list) -> str:
|
|
61
|
+
"""What the checker reads: per lesson `id`, `claim`, `context` when there is one, `heading`, `line`; a blank line between."""
|
|
62
|
+
blocks = []
|
|
63
|
+
for ident, m, head, text in pairs:
|
|
64
|
+
rows = [f"id: {ident}", f"claim: {_one_line(m.claim)}"]
|
|
65
|
+
if m.context:
|
|
66
|
+
rows.append(f"context: {_one_line(m.context)}")
|
|
67
|
+
rows += [f"heading: {_one_line(head)}", f"line: {_one_line(text)}"]
|
|
68
|
+
blocks.append("\n".join(rows))
|
|
69
|
+
return "\n\n".join(blocks)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _digest_verdicts(payload: object) -> tuple[dict, dict] | None:
|
|
73
|
+
"""({id: {"fits", "faithful"}}, {id: reason}) from the checker's answer, or None when it is not one. Only the value
|
|
74
|
+
`true` counts; an id the checker left out is neither."""
|
|
75
|
+
if not isinstance(payload, dict) or not isinstance(payload.get("verdicts"), list):
|
|
76
|
+
return None
|
|
77
|
+
verdicts, reasons = {}, {}
|
|
78
|
+
for v in payload["verdicts"]:
|
|
79
|
+
if isinstance(v, dict) and isinstance(v.get("id"), str) and v["id"] not in verdicts:
|
|
80
|
+
verdicts[v["id"]] = {
|
|
81
|
+
"fits": v.get("fits") is True,
|
|
82
|
+
"faithful": v.get("faithful") is True,
|
|
83
|
+
}
|
|
84
|
+
if isinstance(v.get("reason"), str):
|
|
85
|
+
reasons[v["id"]] = v["reason"]
|
|
86
|
+
return verdicts, reasons
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _digest_sheet(path: Path, rows: list[tuple]) -> None:
|
|
90
|
+
lines = [
|
|
91
|
+
"| topic | heading | id | claim | short form | line | outcome | checker's reason |",
|
|
92
|
+
"|---|---|---|---|---|---|---|---|",
|
|
93
|
+
]
|
|
94
|
+
lines += ["| " + " | ".join(corroborate_job.cell(x) for x in row) + " |" for row in rows]
|
|
95
|
+
corroborate_state.write_private(path, "\n".join(lines) + "\n")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class _Params(NamedTuple):
|
|
99
|
+
claude: str | None
|
|
100
|
+
budget: float
|
|
101
|
+
apply: bool
|
|
102
|
+
sheet: Path | None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class _Inputs(NamedTuple):
|
|
106
|
+
"""What the step reads before it asks anything: the data folder, the stored digest and state, the topics and the work."""
|
|
107
|
+
|
|
108
|
+
data: Path
|
|
109
|
+
stored: dict
|
|
110
|
+
state: dict
|
|
111
|
+
topics: dict
|
|
112
|
+
works: list[dg.Work]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class _Sessions(NamedTuple):
|
|
116
|
+
"""What the writer and checker sessions gave, or `failure` (why a session gave no usable answer)."""
|
|
117
|
+
|
|
118
|
+
proposed: dict
|
|
119
|
+
verdicts: dict
|
|
120
|
+
reasons: dict[str, str]
|
|
121
|
+
failure: str | None = None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class _Sheet(NamedTuple):
|
|
125
|
+
"""What the sheet's rows are made of."""
|
|
126
|
+
|
|
127
|
+
proposed: dict
|
|
128
|
+
verdicts: dict
|
|
129
|
+
reasons: dict[str, str]
|
|
130
|
+
new_digest: dict
|
|
131
|
+
apply: bool
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class _TopicView(NamedTuple):
|
|
135
|
+
work: dg.Work
|
|
136
|
+
proposed: dict
|
|
137
|
+
paired: dict
|
|
138
|
+
stored_topic: dict
|
|
139
|
+
head_text: str | None
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
_finish = corroborate_job.finish
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def digest_auto(
|
|
146
|
+
claude: str | None, budget: float, *, apply: bool = True, sheet: Path | None = None
|
|
147
|
+
) -> dict:
|
|
148
|
+
"""Writes the digest of the loaded lessons, with nobody reading it first: a writer session (a heading per topic, a
|
|
149
|
+
line per lesson), the code checks (`digest.decide`), a second session that checks every line against its claim, then
|
|
150
|
+
the digest file and the step's state. It writes no record and runs no `mem` command: nothing to journal or undo. With
|
|
151
|
+
`apply` false it writes neither file (the sessions still run, and cost). Never raises: a failure is a counted result
|
|
152
|
+
with `stopped` set."""
|
|
153
|
+
result = {
|
|
154
|
+
"topics": 0,
|
|
155
|
+
"headings_written": 0,
|
|
156
|
+
"lines_written": 0,
|
|
157
|
+
"refused_code": 0,
|
|
158
|
+
"refused_check": 0,
|
|
159
|
+
"missing": 0,
|
|
160
|
+
"unjudged": 0,
|
|
161
|
+
"cost": 0.0,
|
|
162
|
+
"stopped": None,
|
|
163
|
+
}
|
|
164
|
+
try:
|
|
165
|
+
return _digest_guarded(_Params(claude, budget, apply, sheet), result)
|
|
166
|
+
except Exception:
|
|
167
|
+
logger.debug("digest step failed", exc_info=True)
|
|
168
|
+
return _finish(result, stopped="the step failed")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _digest_guarded(params: _Params, result: dict) -> dict:
|
|
172
|
+
cdir = corroborate_state.corroborate_dir(corroborate_state.data_dir())
|
|
173
|
+
lock = jobs.take_run_lock(cdir)
|
|
174
|
+
if lock is None:
|
|
175
|
+
return {**result, "stopped": "a run is going"}
|
|
176
|
+
try:
|
|
177
|
+
return _digest_locked(params, cdir, result)
|
|
178
|
+
finally:
|
|
179
|
+
os.close(lock)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _digest_locked(params: _Params, cdir: Path, result: dict) -> dict:
|
|
183
|
+
data = corroborate_state.data_dir()
|
|
184
|
+
lessons = [
|
|
185
|
+
m
|
|
186
|
+
for m in records.load_memories(records.global_memories_root(os.environ), "global")
|
|
187
|
+
if m.error is None
|
|
188
|
+
]
|
|
189
|
+
stored = dg.read(data)
|
|
190
|
+
state = _digest_state(cdir)
|
|
191
|
+
topics = dg.topics_of(lessons, datetime.date.today())
|
|
192
|
+
works = dg.work(topics, stored, state)
|
|
193
|
+
result["topics"] = len(works)
|
|
194
|
+
return _DigestPass(params, cdir, result, _Inputs(data, stored, state, topics, works)).run()
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class _DigestPass:
|
|
198
|
+
"""One run of the step past the lock: the sessions, the checks, the files and the sheet."""
|
|
199
|
+
|
|
200
|
+
writer_why = "" # why the writer session failed (`_ask`), for the stop text
|
|
201
|
+
|
|
202
|
+
def __init__(self, params: _Params, cdir: Path, result: dict, inputs: _Inputs) -> None:
|
|
203
|
+
self.params, self.cdir, self.result = params, cdir, result
|
|
204
|
+
self.data, self.stored, self.state, self.topics, self.works = inputs
|
|
205
|
+
self.failures = self.state["failures"]
|
|
206
|
+
self.fingerprint = ""
|
|
207
|
+
self.spend = corroborate_run.Spend(runners.resolve_runner(None)) # one cap for all sessions
|
|
208
|
+
|
|
209
|
+
def run(self) -> dict:
|
|
210
|
+
if not self.works:
|
|
211
|
+
return self._idle()
|
|
212
|
+
keys = sorted([
|
|
213
|
+
*(
|
|
214
|
+
dg._heading_key(w.topic, w.lessons)
|
|
215
|
+
for w in self.works
|
|
216
|
+
if w.heading_state == "write"
|
|
217
|
+
),
|
|
218
|
+
*(dg._line_key(m, w.lessons) for w in self.works for m in w.write),
|
|
219
|
+
])
|
|
220
|
+
self.fingerprint = hashlib.sha256(json.dumps(keys).encode()).hexdigest()
|
|
221
|
+
if self.failures["fingerprint"] != self.fingerprint:
|
|
222
|
+
self.failures["count"] = 0
|
|
223
|
+
if self.failures["count"] >= corroborate_decide.FILL_FAILURES:
|
|
224
|
+
return _finish(self.result, stopped="waiting for the lessons to change", topics=0)
|
|
225
|
+
claude = self.params.claude
|
|
226
|
+
if not claude:
|
|
227
|
+
return _finish(self.result, stopped="no claude binary")
|
|
228
|
+
return self._attempt(claude)
|
|
229
|
+
|
|
230
|
+
def _idle(self) -> dict:
|
|
231
|
+
if self.params.apply: # nothing to ask, but what no longer applies still goes
|
|
232
|
+
pruned_digest, pruned_state = dg.pruned(self.stored, self.state, self.topics)
|
|
233
|
+
if pruned_digest != self.stored:
|
|
234
|
+
dg.write(self.data, pruned_digest)
|
|
235
|
+
if pruned_state != self.state:
|
|
236
|
+
jobs.write_json(self.cdir / DIGEST_ATTEMPTS_NAME, pruned_state)
|
|
237
|
+
return _finish(self.result)
|
|
238
|
+
|
|
239
|
+
def _attempt(self, claude: str) -> dict:
|
|
240
|
+
sessions = self._sessions(claude)
|
|
241
|
+
if sessions.failure is not None:
|
|
242
|
+
return self._failed_session(sessions.failure)
|
|
243
|
+
new_digest, new_state, counts = dg.decide(
|
|
244
|
+
self.works, sessions.proposed, sessions.verdicts, self.stored, self.state
|
|
245
|
+
)
|
|
246
|
+
new_digest, new_state = dg.pruned(new_digest, new_state, self.topics)
|
|
247
|
+
new_state["failures"] = {"count": 0, "fingerprint": None}
|
|
248
|
+
if self.params.apply and not self._write_files(new_digest, new_state):
|
|
249
|
+
return self._failed_session("the digest or its state could not be written")
|
|
250
|
+
self.result.update(counts)
|
|
251
|
+
if not self.params.apply: # as the rewording: a dry run reports nothing as written
|
|
252
|
+
self.result["headings_written"] = self.result["lines_written"] = 0
|
|
253
|
+
return self._sheet_result(sessions, new_digest)
|
|
254
|
+
|
|
255
|
+
def _save_state(self, new: dict) -> None:
|
|
256
|
+
if self.params.apply:
|
|
257
|
+
jobs.write_json(self.cdir / DIGEST_ATTEMPTS_NAME, new)
|
|
258
|
+
|
|
259
|
+
def _failed_session(self, why: str) -> dict:
|
|
260
|
+
self.failures["count"] += 1
|
|
261
|
+
self.failures["fingerprint"] = self.fingerprint
|
|
262
|
+
try:
|
|
263
|
+
self._save_state(self.state)
|
|
264
|
+
except Exception: # the state file may be what cannot be written: the result still says so
|
|
265
|
+
logger.debug("digest state not saved", exc_info=True)
|
|
266
|
+
return _finish(self.result, stopped=why)
|
|
267
|
+
|
|
268
|
+
def _write_files(self, new_digest: dict, new_state: dict) -> bool:
|
|
269
|
+
try:
|
|
270
|
+
dg.write(self.data, new_digest)
|
|
271
|
+
self._save_state(new_state)
|
|
272
|
+
except Exception:
|
|
273
|
+
logger.debug("digest or state not written", exc_info=True)
|
|
274
|
+
return False
|
|
275
|
+
return True
|
|
276
|
+
|
|
277
|
+
def _sessions(self, claude: str) -> _Sessions:
|
|
278
|
+
scratch = Path(tempfile.mkdtemp(prefix="mem-corroborate-digest-"))
|
|
279
|
+
try:
|
|
280
|
+
return self._run_sessions(claude, scratch)
|
|
281
|
+
finally:
|
|
282
|
+
shutil.rmtree(scratch, ignore_errors=True)
|
|
283
|
+
|
|
284
|
+
def _run_sessions(self, claude: str, scratch: Path) -> _Sessions:
|
|
285
|
+
corroborate_state.write_private(
|
|
286
|
+
scratch / "settings.json", json.dumps({"disableAllHooks": True})
|
|
287
|
+
)
|
|
288
|
+
proposed = self._ask_writer(claude, scratch)
|
|
289
|
+
if proposed is None:
|
|
290
|
+
return _Sessions(
|
|
291
|
+
{}, {}, {}, f"the writer session gave no usable answer{self.writer_why}"
|
|
292
|
+
)
|
|
293
|
+
pairs = dg.pairs(self.works, proposed)
|
|
294
|
+
if not pairs:
|
|
295
|
+
return _Sessions(proposed, {}, {})
|
|
296
|
+
return self._ask_checker(claude, scratch, proposed, pairs)
|
|
297
|
+
|
|
298
|
+
def _ask_writer(self, claude: str, scratch: Path) -> dict | None:
|
|
299
|
+
limits = corroborate_run.SessionLimits(corroborate_run.PROPOSE_MODEL, self.params.budget)
|
|
300
|
+
job = corroborate_run.AskJob(
|
|
301
|
+
corroborate_run.DIGEST_BRIEF,
|
|
302
|
+
dg.blocks(self.works),
|
|
303
|
+
"topics",
|
|
304
|
+
DIGEST_WRITER_OUT,
|
|
305
|
+
self.spend,
|
|
306
|
+
)
|
|
307
|
+
payload, cost, ok, why = corroborate_run._ask(claude, scratch, "write", limits, job)
|
|
308
|
+
self.writer_why = why
|
|
309
|
+
self.result["cost"] += cost
|
|
310
|
+
try:
|
|
311
|
+
return dg.validate(payload, self.works) if ok else None
|
|
312
|
+
except ValueError:
|
|
313
|
+
return None
|
|
314
|
+
|
|
315
|
+
def _ask_checker(self, claude: str, scratch: Path, proposed: dict, pairs: list) -> _Sessions:
|
|
316
|
+
limits = corroborate_run.SessionLimits(corroborate_run.CHECK_MODEL, self.params.budget)
|
|
317
|
+
job = corroborate_run.AskJob(
|
|
318
|
+
corroborate_run.DIGEST_CHECK_BRIEF,
|
|
319
|
+
_digest_pair_text(pairs),
|
|
320
|
+
"pairs",
|
|
321
|
+
DIGEST_CHECK_OUT,
|
|
322
|
+
self.spend,
|
|
323
|
+
)
|
|
324
|
+
payload, cost, ok, why = corroborate_run._ask(claude, scratch, "check", limits, job)
|
|
325
|
+
self.result["cost"] += cost
|
|
326
|
+
checked = _digest_verdicts(payload) if ok else None
|
|
327
|
+
if checked is None:
|
|
328
|
+
return _Sessions({}, {}, {}, f"the checker session gave no usable answer{why}")
|
|
329
|
+
return _Sessions(proposed, *checked)
|
|
330
|
+
|
|
331
|
+
def _sheet_result(self, sessions: _Sessions, new_digest: dict) -> dict:
|
|
332
|
+
sheet = self.params.sheet
|
|
333
|
+
if sheet is not None:
|
|
334
|
+
ctx = _Sheet(
|
|
335
|
+
sessions.proposed,
|
|
336
|
+
sessions.verdicts,
|
|
337
|
+
sessions.reasons,
|
|
338
|
+
new_digest,
|
|
339
|
+
self.params.apply,
|
|
340
|
+
)
|
|
341
|
+
if not self._write_sheet(sheet, ctx):
|
|
342
|
+
return _finish(
|
|
343
|
+
self.result, stopped=self.result["stopped"] or "the sheet could not be written"
|
|
344
|
+
)
|
|
345
|
+
return _finish(self.result)
|
|
346
|
+
|
|
347
|
+
def _write_sheet(self, sheet: Path, ctx: _Sheet) -> bool:
|
|
348
|
+
"""False when the sheet could not be written: the run is done, so that must not turn it into a failed step."""
|
|
349
|
+
try:
|
|
350
|
+
_digest_sheet(sheet, _sheet_rows(ctx, self.works))
|
|
351
|
+
except Exception:
|
|
352
|
+
logger.debug("digest sheet not written", exc_info=True)
|
|
353
|
+
return False
|
|
354
|
+
return True
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _sheet_rows(ctx: _Sheet, works: list[dg.Work]) -> list[tuple]:
|
|
358
|
+
rows: list[tuple] = []
|
|
359
|
+
for w in works:
|
|
360
|
+
rows += _topic_rows(ctx, w)
|
|
361
|
+
return rows
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _topic_rows(ctx: _Sheet, w: dg.Work) -> list[tuple]:
|
|
365
|
+
p = ctx.proposed.get(w.topic, {})
|
|
366
|
+
paired = {m.handle: (ident, head, text) for ident, m, head, text in dg.pairs([w], ctx.proposed)}
|
|
367
|
+
head_text = p.get("heading") if w.heading_state == "write" else w.heading
|
|
368
|
+
view = _TopicView(w, p, paired, ctx.new_digest["topics"].get(w.topic, {}), head_text)
|
|
369
|
+
rows = []
|
|
370
|
+
if w.heading_state == "write" and isinstance(head_text, str):
|
|
371
|
+
outcome, why = _heading_outcome(ctx, view, head_text)
|
|
372
|
+
rows.append((w.topic, head_text, "", "", "", "", outcome, why))
|
|
373
|
+
for m in w.lessons:
|
|
374
|
+
row = _lesson_row(ctx, view, m)
|
|
375
|
+
if row is not None:
|
|
376
|
+
rows.append(row)
|
|
377
|
+
return rows
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _heading_outcome(ctx: _Sheet, view: _TopicView, head_text: str) -> tuple[str, str | None]:
|
|
381
|
+
problem = dg.heading_problem(head_text)
|
|
382
|
+
if problem is not None:
|
|
383
|
+
return "refused_code", problem
|
|
384
|
+
if view.stored_topic.get("heading") == head_text:
|
|
385
|
+
return ("would be written" if not ctx.apply else "written"), None
|
|
386
|
+
if not any(isinstance(ctx.verdicts.get(ident), dict) for ident, _h, _t in view.paired.values()):
|
|
387
|
+
return "unjudged", None
|
|
388
|
+
return "refused_check", None
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def _lesson_row(ctx: _Sheet, view: _TopicView, m) -> tuple | None:
|
|
392
|
+
topic = view.work.topic
|
|
393
|
+
if m.handle in view.paired:
|
|
394
|
+
ident, head, text = view.paired[m.handle]
|
|
395
|
+
return (
|
|
396
|
+
topic,
|
|
397
|
+
head,
|
|
398
|
+
ident,
|
|
399
|
+
m.claim,
|
|
400
|
+
m.short,
|
|
401
|
+
text,
|
|
402
|
+
_paired_outcome(ctx, view, m, ident, text),
|
|
403
|
+
ctx.reasons.get(ident),
|
|
404
|
+
)
|
|
405
|
+
text = (view.proposed.get("lines") or {}).get(m.handle)
|
|
406
|
+
if m in view.work.write and isinstance(text, str):
|
|
407
|
+
head_text = view.head_text
|
|
408
|
+
problem = (
|
|
409
|
+
dg.line_problem(text, m)
|
|
410
|
+
if (head_text is not None and dg.heading_problem(head_text) is None)
|
|
411
|
+
else None
|
|
412
|
+
)
|
|
413
|
+
outcome, why = (
|
|
414
|
+
("refused_code", problem) if problem else ("unjudged", "its heading was refused")
|
|
415
|
+
)
|
|
416
|
+
return (topic, head_text, f"g:{m.handle}", m.claim, m.short, text, outcome, why)
|
|
417
|
+
return None
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def _paired_outcome(ctx: _Sheet, view: _TopicView, m, ident: str, text: object) -> str:
|
|
421
|
+
line = view.stored_topic.get("lines", {}).get(m.handle)
|
|
422
|
+
if line is not None and line["text"] == text:
|
|
423
|
+
outcome = "written" if m in view.work.write else "kept"
|
|
424
|
+
return f"would be {outcome}" if not ctx.apply and outcome == "written" else outcome
|
|
425
|
+
return "unjudged" if not isinstance(ctx.verdicts.get(ident), dict) else "refused_check"
|