agent-lyceum 0.50.0 → 0.51.0
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.
- package/dist/cli.js +5 -1
- package/dist/decision-log.d.ts +23 -0
- package/dist/decision-log.js +148 -11
- package/dist/memory-tidy.d.ts +8 -0
- package/dist/memory-tidy.js +19 -3
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -710,7 +710,11 @@ program
|
|
|
710
710
|
console.log(` restored proposals/${f}`);
|
|
711
711
|
for (const k of r.proposals?.skipped ?? [])
|
|
712
712
|
console.log(` skipped proposals/${k.rel}: ${k.reason}`);
|
|
713
|
-
|
|
713
|
+
for (const f of r.promoted?.removed ?? [])
|
|
714
|
+
console.log(` removed records/${f} (promoted by that tidy)`);
|
|
715
|
+
for (const k of r.promoted?.kept ?? [])
|
|
716
|
+
console.log(` kept records/${k.rel}: ${k.reason}`);
|
|
717
|
+
if (!r.restored.length && !r.skipped.length && !r.records?.restored.length && !r.records?.skipped.length && !r.proposals?.restored.length && !r.proposals?.skipped.length && !r.promoted)
|
|
714
718
|
console.log(`Archive ${opts.undo} holds no files to restore.`);
|
|
715
719
|
}
|
|
716
720
|
finally {
|
package/dist/decision-log.d.ts
CHANGED
|
@@ -7,9 +7,32 @@ export type DecisionLogResult = {
|
|
|
7
7
|
};
|
|
8
8
|
/** Where the entry for a run goes: one stable file per run, so saving twice is harmless. */
|
|
9
9
|
export declare function decisionLogId(runId: string): string;
|
|
10
|
+
type DecisionEntry = {
|
|
11
|
+
id: string;
|
|
12
|
+
text: string;
|
|
13
|
+
outcome?: string;
|
|
14
|
+
};
|
|
10
15
|
/**
|
|
11
16
|
* Keep what a run decided before `clear` deletes the run folder: its `## Decisions` (and `## Not done` as consequences) with the
|
|
12
17
|
* task as context. Never overwrites an existing entry. Returns `none` when the run reported no decision.
|
|
13
18
|
* Throws when the entry cannot be written, so the caller can stop before deleting anything.
|
|
14
19
|
*/
|
|
15
20
|
export declare function saveRunDecisions(project: ResolvedProject, runId: string): DecisionLogResult;
|
|
21
|
+
/**
|
|
22
|
+
* The decisions a tidy should turn into records before it tidies: the `## Decisions` of every completed run, plus what `clear`
|
|
23
|
+
* kept in the decision log, minus what the records already hold. Only completed runs count; a partial or failed run's choices
|
|
24
|
+
* are often provisional. Writes nothing.
|
|
25
|
+
*/
|
|
26
|
+
export declare function listPromotable(project: ResolvedProject): DecisionEntry[];
|
|
27
|
+
export declare const promotedManifest: (project: ResolvedProject, stamp: string) => string;
|
|
28
|
+
/** Write `listPromotable` into `records/decisions/` (never over an existing file) and note them under `stamp` so `tidy --undo` can take them back. */
|
|
29
|
+
export declare function promoteDecisions(project: ResolvedProject, stamp: string): string[];
|
|
30
|
+
/** Remove what the tidy `stamp` promoted, except files someone has changed since. */
|
|
31
|
+
export declare function undoPromoted(project: ResolvedProject, stamp: string): {
|
|
32
|
+
removed: string[];
|
|
33
|
+
kept: {
|
|
34
|
+
rel: string;
|
|
35
|
+
reason: string;
|
|
36
|
+
}[];
|
|
37
|
+
};
|
|
38
|
+
export {};
|
package/dist/decision-log.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
1
2
|
import fs from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
3
|
-
import { decisionLogDir } from "./policy.js";
|
|
4
|
+
import { decisionLogDir, recordsDir, recordsKindDir } from "./policy.js";
|
|
4
5
|
/** Body of the `## <name>` section of a done report, up to the next `## ` heading. */
|
|
5
6
|
function section(text, name) {
|
|
6
7
|
const m = new RegExp(`^##[ \\t]+${name}[ \\t]*\\r?\\n([\\s\\S]*?)(?=^##[ \\t]|(?![\\s\\S]))`, "m").exec(text);
|
|
@@ -15,29 +16,27 @@ const oneLine = (s) => s.replace(/\s+/g, " ").trim();
|
|
|
15
16
|
export function decisionLogId(runId) {
|
|
16
17
|
return `run-${runId.toLowerCase().replace(/[^a-z0-9-]/g, "").slice(0, 28)}`;
|
|
17
18
|
}
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
* task as context. Never overwrites an existing entry. Returns `none` when the run reported no decision.
|
|
21
|
-
* Throws when the entry cannot be written, so the caller can stop before deleting anything.
|
|
22
|
-
*/
|
|
23
|
-
export function saveRunDecisions(project, runId) {
|
|
19
|
+
/** The decision-log entry of one run, or why there is none. Reads the run's report and state; writes nothing. */
|
|
20
|
+
function buildEntry(project, runId) {
|
|
24
21
|
const runDir = path.join(project.paths.runs, runId);
|
|
25
22
|
let result;
|
|
26
23
|
try {
|
|
27
24
|
result = fs.readFileSync(path.join(runDir, "result.md"), "utf8");
|
|
28
25
|
}
|
|
29
26
|
catch {
|
|
30
|
-
return {
|
|
27
|
+
return { reason: "no result.md" };
|
|
31
28
|
}
|
|
32
29
|
const decisions = section(result, "Decisions");
|
|
33
30
|
if (isEmpty(decisions))
|
|
34
|
-
return {
|
|
31
|
+
return { reason: "the report lists no decisions" };
|
|
35
32
|
let summary = "";
|
|
36
33
|
let started = "";
|
|
34
|
+
let stateOutcome;
|
|
37
35
|
try {
|
|
38
36
|
const state = JSON.parse(fs.readFileSync(path.join(runDir, "state.json"), "utf8"));
|
|
39
37
|
summary = state.task_summary ?? "";
|
|
40
38
|
started = state.started_at ?? "";
|
|
39
|
+
stateOutcome = state.outcome;
|
|
41
40
|
}
|
|
42
41
|
catch {
|
|
43
42
|
// The report is enough to keep.
|
|
@@ -45,10 +44,9 @@ export function saveRunDecisions(project, runId) {
|
|
|
45
44
|
const heading = /^#\s+(.+)$/m.exec(result)?.[1]?.trim();
|
|
46
45
|
const title = oneLine(heading || summary || runId).slice(0, 200);
|
|
47
46
|
const date = /^\d{4}-\d{2}-\d{2}/.exec(started)?.[0] ?? new Date().toISOString().slice(0, 10);
|
|
48
|
-
const outcome = /^\*\*Outcome:\*\*\s*(\S+)/m.exec(result)?.[1];
|
|
47
|
+
const outcome = /^\*\*Outcome:\*\*\s*(\S+)/m.exec(result)?.[1] ?? stateOutcome;
|
|
49
48
|
const notDone = section(result, "Not done");
|
|
50
49
|
const id = decisionLogId(runId);
|
|
51
|
-
const file = path.join(decisionLogDir(project), `${id}.md`);
|
|
52
50
|
const text = [
|
|
53
51
|
"---",
|
|
54
52
|
"status: active",
|
|
@@ -65,6 +63,19 @@ export function saveRunDecisions(project, runId) {
|
|
|
65
63
|
...(isEmpty(notDone) ? [] : ["", "## Consequences", notDone]),
|
|
66
64
|
"",
|
|
67
65
|
].join("\n");
|
|
66
|
+
return { entry: { id, text, ...(outcome ? { outcome } : {}) } };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Keep what a run decided before `clear` deletes the run folder: its `## Decisions` (and `## Not done` as consequences) with the
|
|
70
|
+
* task as context. Never overwrites an existing entry. Returns `none` when the run reported no decision.
|
|
71
|
+
* Throws when the entry cannot be written, so the caller can stop before deleting anything.
|
|
72
|
+
*/
|
|
73
|
+
export function saveRunDecisions(project, runId) {
|
|
74
|
+
const built = buildEntry(project, runId);
|
|
75
|
+
if ("reason" in built)
|
|
76
|
+
return { status: "none", reason: built.reason };
|
|
77
|
+
const { id, text } = built.entry;
|
|
78
|
+
const file = path.join(decisionLogDir(project), `${id}.md`);
|
|
68
79
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
69
80
|
try {
|
|
70
81
|
fs.writeFileSync(file, text, { flag: "wx" });
|
|
@@ -76,3 +87,129 @@ export function saveRunDecisions(project, runId) {
|
|
|
76
87
|
}
|
|
77
88
|
return { status: "saved", id, file };
|
|
78
89
|
}
|
|
90
|
+
const ID_FILE_RE = /^[a-z0-9-]+\.md$/;
|
|
91
|
+
const sha = (text) => crypto.createHash("sha256").update(text).digest("hex");
|
|
92
|
+
/** Ids the records already hold for decisions, live or archived by a tidy: a decision a tidy merged away must not come back. */
|
|
93
|
+
function knownDecisionIds(project) {
|
|
94
|
+
const known = new Set();
|
|
95
|
+
const add = (dir) => {
|
|
96
|
+
try {
|
|
97
|
+
for (const n of fs.readdirSync(dir))
|
|
98
|
+
if (ID_FILE_RE.test(n))
|
|
99
|
+
known.add(n.slice(0, -3));
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// A missing folder holds nothing.
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
add(recordsKindDir(project, "decisions"));
|
|
106
|
+
const archive = path.join(recordsDir(project), ".archive");
|
|
107
|
+
try {
|
|
108
|
+
for (const stamp of fs.readdirSync(archive))
|
|
109
|
+
add(path.join(archive, stamp, "decisions"));
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
// No archive yet.
|
|
113
|
+
}
|
|
114
|
+
return known;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The decisions a tidy should turn into records before it tidies: the `## Decisions` of every completed run, plus what `clear`
|
|
118
|
+
* kept in the decision log, minus what the records already hold. Only completed runs count; a partial or failed run's choices
|
|
119
|
+
* are often provisional. Writes nothing.
|
|
120
|
+
*/
|
|
121
|
+
export function listPromotable(project) {
|
|
122
|
+
const known = knownDecisionIds(project);
|
|
123
|
+
const out = new Map();
|
|
124
|
+
let runs = [];
|
|
125
|
+
try {
|
|
126
|
+
runs = fs.readdirSync(project.paths.runs, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name).sort();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// No runs yet.
|
|
130
|
+
}
|
|
131
|
+
for (const runId of runs) {
|
|
132
|
+
const built = buildEntry(project, runId);
|
|
133
|
+
if ("reason" in built || built.entry.outcome !== "completed" || known.has(built.entry.id))
|
|
134
|
+
continue;
|
|
135
|
+
out.set(built.entry.id, built.entry);
|
|
136
|
+
}
|
|
137
|
+
let kept = [];
|
|
138
|
+
try {
|
|
139
|
+
kept = fs.readdirSync(decisionLogDir(project)).filter((n) => ID_FILE_RE.test(n)).sort();
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// No decision log.
|
|
143
|
+
}
|
|
144
|
+
for (const n of kept) {
|
|
145
|
+
const id = n.slice(0, -3);
|
|
146
|
+
if (known.has(id))
|
|
147
|
+
continue;
|
|
148
|
+
let text;
|
|
149
|
+
try {
|
|
150
|
+
text = fs.readFileSync(path.join(decisionLogDir(project), n), "utf8");
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (!/^outcome:\s*completed\s*$/m.test(/^---\r?\n([\s\S]*?)\r?\n---/.exec(text)?.[1] ?? ""))
|
|
156
|
+
continue;
|
|
157
|
+
out.set(id, { id, text, outcome: "completed" });
|
|
158
|
+
}
|
|
159
|
+
return [...out.values()];
|
|
160
|
+
}
|
|
161
|
+
export const promotedManifest = (project, stamp) => path.join(project.paths.root, "tidy-promoted", `${stamp}.json`);
|
|
162
|
+
/** Write `listPromotable` into `records/decisions/` (never over an existing file) and note them under `stamp` so `tidy --undo` can take them back. */
|
|
163
|
+
export function promoteDecisions(project, stamp) {
|
|
164
|
+
const todo = listPromotable(project);
|
|
165
|
+
const dir = recordsKindDir(project, "decisions");
|
|
166
|
+
const done = [];
|
|
167
|
+
for (const e of todo) {
|
|
168
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
169
|
+
try {
|
|
170
|
+
fs.writeFileSync(path.join(dir, `${e.id}.md`), e.text, { flag: "wx" });
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
if (err.code === "EEXIST")
|
|
174
|
+
continue;
|
|
175
|
+
throw err;
|
|
176
|
+
}
|
|
177
|
+
done.push({ rel: `decisions/${e.id}.md`, sha: sha(e.text) });
|
|
178
|
+
}
|
|
179
|
+
if (done.length) {
|
|
180
|
+
fs.mkdirSync(path.dirname(promotedManifest(project, stamp)), { recursive: true });
|
|
181
|
+
fs.writeFileSync(promotedManifest(project, stamp), JSON.stringify({ files: done }, null, 2));
|
|
182
|
+
}
|
|
183
|
+
return done.map((d) => d.rel.slice("decisions/".length, -3));
|
|
184
|
+
}
|
|
185
|
+
/** Remove what the tidy `stamp` promoted, except files someone has changed since. */
|
|
186
|
+
export function undoPromoted(project, stamp) {
|
|
187
|
+
const removed = [];
|
|
188
|
+
const kept = [];
|
|
189
|
+
let files;
|
|
190
|
+
try {
|
|
191
|
+
files = JSON.parse(fs.readFileSync(promotedManifest(project, stamp), "utf8")).files;
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return { removed, kept };
|
|
195
|
+
}
|
|
196
|
+
for (const f of files) {
|
|
197
|
+
const file = path.join(recordsDir(project), f.rel);
|
|
198
|
+
let text;
|
|
199
|
+
try {
|
|
200
|
+
text = fs.readFileSync(file, "utf8");
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
kept.push({ rel: f.rel, reason: "it is gone already (archived or deleted)" });
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (sha(text) !== f.sha) {
|
|
207
|
+
kept.push({ rel: f.rel, reason: "it was changed after the tidy; it was not removed" });
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
fs.rmSync(file);
|
|
211
|
+
removed.push(f.rel);
|
|
212
|
+
}
|
|
213
|
+
fs.rmSync(promotedManifest(project, stamp), { force: true });
|
|
214
|
+
return { removed, kept };
|
|
215
|
+
}
|
package/dist/memory-tidy.d.ts
CHANGED
|
@@ -157,6 +157,14 @@ export interface RestoreResult {
|
|
|
157
157
|
reason: string;
|
|
158
158
|
}[];
|
|
159
159
|
};
|
|
160
|
+
/** The decisions that tidy had turned into records, taken back out (a file changed since is kept). */
|
|
161
|
+
promoted?: {
|
|
162
|
+
removed: string[];
|
|
163
|
+
kept: {
|
|
164
|
+
rel: string;
|
|
165
|
+
reason: string;
|
|
166
|
+
}[];
|
|
167
|
+
};
|
|
160
168
|
}
|
|
161
169
|
/**
|
|
162
170
|
* Move the files of one tidy's archive back to where they were. Nothing that exists now is overwritten.
|
package/dist/memory-tidy.js
CHANGED
|
@@ -7,6 +7,7 @@ import { atomicWrite } from "./fs-util.js";
|
|
|
7
7
|
import { ensureProjectDirs } from "./mailbox.js";
|
|
8
8
|
import { commonFile, memoryDirs, proposalsDir, recordsDir, repoDirFor, writePolicy } from "./policy.js";
|
|
9
9
|
import { parseProposal, PROPOSAL_KINDS } from "./proposals.js";
|
|
10
|
+
import { listPromotable, promoteDecisions, undoPromoted } from "./decision-log.js";
|
|
10
11
|
import { buildTidyPrompts, MEMORY_INDEX_LIMIT } from "./prompt.js";
|
|
11
12
|
import { activeRecordsBytes, effectiveActive, listRecords, RECORD_KINDS, RECORDS_BUDGET } from "./records.js";
|
|
12
13
|
import { git, isGitRepo } from "./worktree.js";
|
|
@@ -450,6 +451,20 @@ async function tidyOne(o, t, stamp, withRecords) {
|
|
|
450
451
|
/** Tidy the memory of the chosen agents, one after another. Only ever called by a person (`agent-lyceum tidy`). */
|
|
451
452
|
export async function tidyMemory(o) {
|
|
452
453
|
const say = o.say ?? ((s) => console.log(s));
|
|
454
|
+
const stamp = tidyStamp(o.now?.()); // one per tidy, for every agent
|
|
455
|
+
// Before the lead tidies the records, decisions that runs reported become records, so the tidy can merge them with what is there.
|
|
456
|
+
if (recordsInScope(o)) {
|
|
457
|
+
if (o.dryRun) {
|
|
458
|
+
const todo = listPromotable(o.project);
|
|
459
|
+
if (todo.length)
|
|
460
|
+
say(`decisions to promote into the project records: ${todo.map((e) => e.id).join(", ")}`);
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
const ids = promoteDecisions(o.project, stamp);
|
|
464
|
+
if (ids.length)
|
|
465
|
+
say(`promoted ${ids.length} decision(s) into the project records: ${ids.join(", ")}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
453
468
|
const withRecords = recordsInScope(o) && (liveRecordFiles(o.project).length > 0 || liveProposalFiles(o.project).length > 0);
|
|
454
469
|
const lead = o.project.lead;
|
|
455
470
|
let targets;
|
|
@@ -463,7 +478,6 @@ export async function tidyMemory(o) {
|
|
|
463
478
|
targets = [];
|
|
464
479
|
}
|
|
465
480
|
const results = [];
|
|
466
|
-
const stamp = tidyStamp(o.now?.()); // one per tidy, for every agent
|
|
467
481
|
const jobs = targets.map((t) => ({ ...t }));
|
|
468
482
|
if (withRecords && !jobs.some((j) => j.agent === lead))
|
|
469
483
|
jobs.push({ agent: lead, layer: o.layer, dir: undefined });
|
|
@@ -545,7 +559,9 @@ export function restoreMemory(o) {
|
|
|
545
559
|
}
|
|
546
560
|
const archive = dir ? path.join(dir, ARCHIVE_DIR, o.stamp) : undefined;
|
|
547
561
|
const memHas = !!archive && fs.existsSync(archive);
|
|
548
|
-
|
|
562
|
+
const promoted = isLead ? undoPromoted(o.project, o.stamp) : undefined;
|
|
563
|
+
const promotedAny = !!promoted && (promoted.removed.length > 0 || promoted.kept.length > 0);
|
|
564
|
+
if (!memHas && !recHas && !propHas && !promotedAny) {
|
|
549
565
|
const recs = isLead ? `${dir ? "; project records archives: " : " Project records archives: "}${listArchives(recDir).join(", ") || "(none)"}` : "";
|
|
550
566
|
throw new Error(`No archive ${o.stamp} for ${o.agent} (${o.layer}).${dir ? ` Archives: ${listArchives(dir).join(", ") || "(none)"}` : ""}${recs}.`);
|
|
551
567
|
}
|
|
@@ -557,7 +573,7 @@ export function restoreMemory(o) {
|
|
|
557
573
|
if (add.length)
|
|
558
574
|
atomicWrite(indexFile, `${index.replace(/\n*$/, index ? "\n" : "")}${add.join("\n")}\n`);
|
|
559
575
|
}
|
|
560
|
-
return { ...mem, ...(recHas ? { records: moveBack(recDir, recArchive) } : {}), ...(propHas ? { proposals: moveBack(pDir, propArchive) } : {}) };
|
|
576
|
+
return { ...mem, ...(recHas ? { records: moveBack(recDir, recArchive) } : {}), ...(propHas ? { proposals: moveBack(pDir, propArchive) } : {}), ...(promotedAny ? { promoted } : {}) };
|
|
561
577
|
}
|
|
562
578
|
/** The stamps of the archives in a memory directory, oldest first. */
|
|
563
579
|
export function listArchives(dir) {
|