agent-lyceum 0.44.1 → 0.46.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/README.md +1 -0
- package/README.zh-TW.md +1 -0
- package/dist/call.d.ts +8 -0
- package/dist/call.js +29 -7
- package/dist/cli.js +70 -5
- package/dist/config.d.ts +11 -0
- package/dist/doctor.js +12 -1
- package/dist/guard.d.ts +45 -2
- package/dist/guard.js +117 -10
- package/dist/mailbox.js +13 -3
- package/dist/memory-tidy.d.ts +51 -0
- package/dist/memory-tidy.js +184 -75
- package/dist/policy.d.ts +8 -0
- package/dist/policy.js +14 -0
- package/dist/project-lock.d.ts +4 -0
- package/dist/project-lock.js +1 -0
- package/dist/prompt.d.ts +31 -1
- package/dist/prompt.js +118 -10
- package/dist/proposals-review.d.ts +26 -0
- package/dist/proposals-review.js +94 -0
- package/dist/proposals.d.ts +88 -0
- package/dist/proposals.js +306 -0
- package/dist/records.d.ts +5 -0
- package/dist/records.js +5 -1
- package/dist/run-session.js +9 -4
- package/dist/scaffold.js +2 -1
- package/dist/status.js +9 -0
- package/package.json +1 -1
- package/web/dist/public/assets/index-C8HSRh87.js +10 -0
- package/web/dist/public/index.html +1 -1
- package/web/dist/public/assets/index-CN8qa30v.js +0 -10
package/dist/memory-tidy.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Invoker } from "./adapters/index.js";
|
|
2
2
|
import type { ResolvedProject } from "./config.js";
|
|
3
|
+
import { type Proposal } from "./proposals.js";
|
|
3
4
|
export type Layer = "project" | "global";
|
|
4
5
|
export declare const ARCHIVE_DIR = ".archive";
|
|
5
6
|
export declare const TIDY_STATE_FILE = ".tidy-state.json";
|
|
@@ -86,10 +87,52 @@ export interface TidyResult {
|
|
|
86
87
|
archive?: string;
|
|
87
88
|
/** Set when the project records were tidied too: their folder (its archive is `<recordsDir>/.archive/<archive>`). */
|
|
88
89
|
recordsDir?: string;
|
|
90
|
+
/** Set when the proposals were tidied too: their folder (its archive is `<proposalsDir>/.archive/<archive>`). */
|
|
91
|
+
proposalsDir?: string;
|
|
89
92
|
problems: string[];
|
|
90
93
|
}
|
|
91
94
|
/** Scratch space of one tidy (snapshot and the invocation's logs); outside every memory dir and every run. */
|
|
92
95
|
export declare const tidyWorkDir: (project: ResolvedProject, stamp: string, agent: string) => string;
|
|
96
|
+
/** A decided proposal stays this many days before a tidy may archive it (spec 30, D-12). */
|
|
97
|
+
export declare const PROPOSAL_KEEP_DAYS = 30;
|
|
98
|
+
/** Decided (accepted or rejected) and decided more than `PROPOSAL_KEEP_DAYS` before `now`. The decision date counts, not the proposal date: an old proposal accepted today stays. */
|
|
99
|
+
export declare function proposalArchivable(p: Pick<Proposal, "status" | "decided">, now: Date): boolean;
|
|
100
|
+
/** What `wakeWithRollback` needs to run one maintenance wake of an agent and undo it when it goes wrong. */
|
|
101
|
+
export interface RollbackWake {
|
|
102
|
+
project: ResolvedProject;
|
|
103
|
+
/** The agent to wake. */
|
|
104
|
+
agentName: string;
|
|
105
|
+
/** Directories copied aside first and put back on any failure. */
|
|
106
|
+
dirs: string[];
|
|
107
|
+
/** The only directories the runtime may write (what it could read before stays readable). */
|
|
108
|
+
allowDirs: string[];
|
|
109
|
+
/** Directories that must come out exactly as they went in. */
|
|
110
|
+
others: string[];
|
|
111
|
+
/** Appended to "the agent changed <dir>, which is ..." */
|
|
112
|
+
othersLabel: string;
|
|
113
|
+
workDir: string;
|
|
114
|
+
prompts: {
|
|
115
|
+
systemPrompt: string;
|
|
116
|
+
userPrompt: string;
|
|
117
|
+
};
|
|
118
|
+
invoker?: Invoker;
|
|
119
|
+
signal?: AbortSignal;
|
|
120
|
+
/** Problems with what the wake left behind; a throw counts as a failed wake. */
|
|
121
|
+
check: () => string[];
|
|
122
|
+
/** Runs once the wake is accepted, before the snapshot is dropped. */
|
|
123
|
+
onSuccess?: () => void;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* One maintenance wake (a memory tidy, a proposals review) with all-or-nothing effect: the directories are copied aside, the
|
|
127
|
+
* agent may write only `allowDirs`, the shared COMMON.md and every directory in `others` must come out unchanged, and anything
|
|
128
|
+
* `check` reports puts everything back as it was. Never goes through the guard (spec 30: it has none on this path).
|
|
129
|
+
*/
|
|
130
|
+
export declare function wakeWithRollback(j: RollbackWake): Promise<{
|
|
131
|
+
ok: true;
|
|
132
|
+
} | {
|
|
133
|
+
ok: false;
|
|
134
|
+
problems: string[];
|
|
135
|
+
}>;
|
|
93
136
|
/** Tidy the memory of the chosen agents, one after another. Only ever called by a person (`agent-lyceum tidy`). */
|
|
94
137
|
export declare function tidyMemory(o: TidyOptions): Promise<TidyResult[]>;
|
|
95
138
|
export interface RestoreResult {
|
|
@@ -106,6 +149,14 @@ export interface RestoreResult {
|
|
|
106
149
|
reason: string;
|
|
107
150
|
}[];
|
|
108
151
|
};
|
|
152
|
+
/** The same for the proposals archive of that stamp. */
|
|
153
|
+
proposals?: {
|
|
154
|
+
restored: string[];
|
|
155
|
+
skipped: {
|
|
156
|
+
rel: string;
|
|
157
|
+
reason: string;
|
|
158
|
+
}[];
|
|
159
|
+
};
|
|
109
160
|
}
|
|
110
161
|
/**
|
|
111
162
|
* 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
|
@@ -5,7 +5,8 @@ import path from "node:path";
|
|
|
5
5
|
import { realInvoker } from "./adapters/index.js";
|
|
6
6
|
import { atomicWrite } from "./fs-util.js";
|
|
7
7
|
import { ensureProjectDirs } from "./mailbox.js";
|
|
8
|
-
import { commonFile, memoryDirs, recordsDir, repoDirFor, writePolicy } from "./policy.js";
|
|
8
|
+
import { commonFile, memoryDirs, proposalsDir, recordsDir, repoDirFor, writePolicy } from "./policy.js";
|
|
9
|
+
import { parseProposal, PROPOSAL_KINDS } from "./proposals.js";
|
|
9
10
|
import { buildTidyPrompts, MEMORY_INDEX_LIMIT } from "./prompt.js";
|
|
10
11
|
import { activeRecordsBytes, effectiveActive, listRecords, RECORD_KINDS, RECORDS_BUDGET } from "./records.js";
|
|
11
12
|
import { git, isGitRepo } from "./worktree.js";
|
|
@@ -206,6 +207,28 @@ function describeTarget(t, project, say) {
|
|
|
206
207
|
}
|
|
207
208
|
/** The live files of the project records (the archive is not listed). */
|
|
208
209
|
const liveRecordFiles = (project) => (isPlainDir(recordsDir(project)) ? listMemoryFiles(recordsDir(project)).filter((f) => !f.rel.startsWith(`${ARCHIVE_DIR}/`)) : []);
|
|
210
|
+
/** A decided proposal stays this many days before a tidy may archive it (spec 30, D-12). */
|
|
211
|
+
export const PROPOSAL_KEEP_DAYS = 30;
|
|
212
|
+
/** The live proposals (not `.archive/`, not the review reports in `.reviews/`). */
|
|
213
|
+
const liveProposalFiles = (project) => isPlainDir(proposalsDir(project)) ? listMemoryFiles(proposalsDir(project)).filter((f) => !f.rel.startsWith(`${ARCHIVE_DIR}/`) && !f.rel.startsWith(".reviews/")) : [];
|
|
214
|
+
/** Decided (accepted or rejected) and decided more than `PROPOSAL_KEEP_DAYS` before `now`. The decision date counts, not the proposal date: an old proposal accepted today stays. */
|
|
215
|
+
export function proposalArchivable(p, now) {
|
|
216
|
+
if ((p.status !== "accepted" && p.status !== "rejected") || !p.decided)
|
|
217
|
+
return false;
|
|
218
|
+
const t = Date.parse(`${p.decided}T00:00:00Z`);
|
|
219
|
+
return Number.isFinite(t) && (now.getTime() - t) / 86_400_000 > PROPOSAL_KEEP_DAYS;
|
|
220
|
+
}
|
|
221
|
+
function proposalsContext(project, archiveDir, live, now) {
|
|
222
|
+
const entries = [];
|
|
223
|
+
for (const f of live) {
|
|
224
|
+
const kind = PROPOSAL_KINDS.find((k) => f.rel.startsWith(`${k}/`));
|
|
225
|
+
if (!kind || !f.rel.endsWith(".md"))
|
|
226
|
+
continue;
|
|
227
|
+
const p = parseProposal(kind, path.join(proposalsDir(project), f.rel));
|
|
228
|
+
entries.push({ rel: f.rel, status: p.status ?? "unreadable", date: p.date, decided: p.decided ?? "", bytes: f.bytes, title: p.title, eligible: proposalArchivable(p, now) });
|
|
229
|
+
}
|
|
230
|
+
return { dir: proposalsDir(project), archiveDir, today: now.toISOString().slice(0, 10), keepDays: PROPOSAL_KEEP_DAYS, entries };
|
|
231
|
+
}
|
|
209
232
|
function isPlainDir(p) {
|
|
210
233
|
try {
|
|
211
234
|
return fs.lstatSync(p).isDirectory();
|
|
@@ -238,104 +261,98 @@ const hasReport = (archiveDir) => {
|
|
|
238
261
|
const f = path.join(archiveDir, TIDY_REPORT_FILE);
|
|
239
262
|
return fs.existsSync(f) && fs.readFileSync(f, "utf8").trim() !== "";
|
|
240
263
|
};
|
|
241
|
-
/**
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
264
|
+
/**
|
|
265
|
+
* What a tidy may do to the proposals (spec 30, D-12), checked after the wake: nothing disappears (`verifyConservation`), every
|
|
266
|
+
* file that moved went to this tidy's archive unchanged and is decided long enough ago, every file that stayed is byte for byte
|
|
267
|
+
* as it was (the lead may not edit a proposal while tidying), earlier archives are untouched, and the report was written.
|
|
268
|
+
*/
|
|
269
|
+
function checkProposalsTidy(pDir, before, hashBefore, earlierNow, earlierBefore, stamp, now) {
|
|
270
|
+
const problems = [];
|
|
271
|
+
const label = (m) => `proposals: ${m}`;
|
|
272
|
+
if (JSON.stringify(earlierNow) !== JSON.stringify(earlierBefore))
|
|
273
|
+
problems.push(label(`the agent changed an earlier archive under ${path.join(pDir, ARCHIVE_DIR)}`));
|
|
274
|
+
problems.push(...verifyConservation(before, pDir, stamp).map(label));
|
|
275
|
+
if (!hasReport(path.join(pDir, ARCHIVE_DIR, stamp)))
|
|
276
|
+
problems.push(label(`${ARCHIVE_DIR}/${stamp}/${TIDY_REPORT_FILE} is missing or empty`));
|
|
277
|
+
const hashNow = fingerprint(pDir);
|
|
278
|
+
for (const rel of before) {
|
|
279
|
+
if (rel.startsWith(`${ARCHIVE_DIR}/`))
|
|
280
|
+
continue; // earlier archives: checked above
|
|
281
|
+
if (rel in hashNow) {
|
|
282
|
+
if (hashNow[rel] !== hashBefore[rel])
|
|
283
|
+
problems.push(label(`${rel} was changed; a tidy may only move proposals`));
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const moved = `${ARCHIVE_DIR}/${stamp}/${rel}`;
|
|
287
|
+
if (!(moved in hashNow))
|
|
288
|
+
continue; // reported by verifyConservation
|
|
289
|
+
if (hashNow[moved] !== hashBefore[rel])
|
|
290
|
+
problems.push(label(`${rel} was changed while it was archived`));
|
|
291
|
+
const kind = PROPOSAL_KINDS.find((k) => rel.startsWith(`${k}/`));
|
|
292
|
+
if (!kind)
|
|
293
|
+
problems.push(label(`${rel} is not a proposal and may not be archived`));
|
|
294
|
+
else if (!proposalArchivable(parseProposal(kind, path.join(pDir, moved)), now))
|
|
295
|
+
problems.push(label(`${rel} was archived, but it is not decided more than ${PROPOSAL_KEEP_DAYS} days ago`));
|
|
296
|
+
}
|
|
297
|
+
return problems;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* One maintenance wake (a memory tidy, a proposals review) with all-or-nothing effect: the directories are copied aside, the
|
|
301
|
+
* agent may write only `allowDirs`, the shared COMMON.md and every directory in `others` must come out unchanged, and anything
|
|
302
|
+
* `check` reports puts everything back as it was. Never goes through the guard (spec 30: it has none on this path).
|
|
303
|
+
*/
|
|
304
|
+
export async function wakeWithRollback(j) {
|
|
305
|
+
const { project } = j;
|
|
306
|
+
const agent = project.agents[j.agentName];
|
|
258
307
|
const watched = [commonFile(project)].filter((f) => fs.existsSync(f));
|
|
259
308
|
const watchedBefore = watched.map((f) => fs.readFileSync(f, "utf8"));
|
|
260
|
-
const othersBefore = others.map((d) => fingerprint(d));
|
|
261
|
-
const
|
|
262
|
-
const archiveDir = t.dir ? path.join(t.dir, ARCHIVE_DIR, stamp) : undefined;
|
|
263
|
-
const recArchive = path.join(recDir, ARCHIVE_DIR, stamp);
|
|
264
|
-
const workDir = tidyWorkDir(project, stamp, t.agent);
|
|
265
|
-
const snapshot = path.join(workDir, "snapshot");
|
|
266
|
-
const state = t.dir ? readTidyState(t.dir) : undefined;
|
|
267
|
-
const changes = memPart && t.layer === "project" ? projectChanges(project.dir, state?.head) : undefined;
|
|
268
|
-
const missing = memPart && t.dir ? findMissingPaths(t.dir, t.layer === "project" ? project.dir : undefined) : [];
|
|
269
|
-
const index = t.dir && fs.existsSync(path.join(t.dir, "MEMORY.md")) ? fs.readFileSync(path.join(t.dir, "MEMORY.md"), "utf8") : "";
|
|
270
|
-
const prompts = buildTidyPrompts(agent, {
|
|
271
|
-
layer: t.layer,
|
|
272
|
-
...(memPart ? { dir: t.dir, archiveDir } : {}),
|
|
273
|
-
files: live,
|
|
274
|
-
index,
|
|
275
|
-
changes,
|
|
276
|
-
missing,
|
|
277
|
-
...(recPart ? { records: recordsContext(project, recArchive, recLive) } : {}),
|
|
278
|
-
});
|
|
309
|
+
const othersBefore = j.others.map((d) => fingerprint(d));
|
|
310
|
+
const snapshot = path.join(j.workDir, "snapshot");
|
|
279
311
|
ensureProjectDirs(project);
|
|
280
|
-
snapshotDirs(dirs, snapshot);
|
|
312
|
+
snapshotDirs(j.dirs, snapshot);
|
|
281
313
|
const fail = (problems) => {
|
|
282
|
-
restoreDirs(dirs, snapshot);
|
|
314
|
+
restoreDirs(j.dirs, snapshot);
|
|
283
315
|
for (const [i, f] of watched.entries())
|
|
284
316
|
atomicWrite(f, watchedBefore[i]);
|
|
285
317
|
fs.rmSync(snapshot, { recursive: true, force: true });
|
|
286
|
-
return {
|
|
318
|
+
return { ok: false, problems };
|
|
287
319
|
};
|
|
288
|
-
// The runtime may write only the directories being
|
|
320
|
+
// The runtime may write only the directories being worked on (and read what it could read before): not the repo, outbox or shared files.
|
|
289
321
|
const normal = writePolicy(project, agent);
|
|
290
|
-
const allowDirs = [...(memPart && t.dir ? [t.dir] : []), ...(recPart ? [recDir] : [])];
|
|
291
322
|
const policy = {
|
|
292
|
-
allowDirs,
|
|
323
|
+
allowDirs: j.allowDirs,
|
|
293
324
|
allowFiles: [],
|
|
294
|
-
readDirs: [...new Set([repoDirFor(project,
|
|
325
|
+
readDirs: [...new Set([repoDirFor(project, j.agentName), ...normal.readDirs, ...normal.allowDirs.filter((d) => !j.allowDirs.includes(d)), ...normal.allowFiles.map((f) => path.dirname(f))])],
|
|
295
326
|
deny: normal.deny,
|
|
296
327
|
};
|
|
297
|
-
|
|
298
|
-
const invoke = o.invoker ?? realInvoker;
|
|
328
|
+
const invoke = j.invoker ?? realInvoker;
|
|
299
329
|
const result = await invoke({
|
|
300
330
|
project,
|
|
301
331
|
agent,
|
|
302
|
-
workDir,
|
|
303
|
-
systemPrompt: prompts.systemPrompt,
|
|
304
|
-
userPrompt: prompts.userPrompt,
|
|
332
|
+
workDir: j.workDir,
|
|
333
|
+
systemPrompt: j.prompts.systemPrompt,
|
|
334
|
+
userPrompt: j.prompts.userPrompt,
|
|
305
335
|
timeoutSec: project.dispatcher.wake_timeout_sec,
|
|
306
|
-
signal:
|
|
307
|
-
logDir: path.join(workDir, "log"),
|
|
336
|
+
signal: j.signal,
|
|
337
|
+
logDir: path.join(j.workDir, "log"),
|
|
308
338
|
policy,
|
|
309
339
|
}).catch((e) => ({ ok: false, text: "", exitCode: null, timedOut: false, error: e.message }));
|
|
310
340
|
if (!result.ok)
|
|
311
341
|
return fail([`the wake-up failed: ${result.error ?? "unknown error"}`]);
|
|
312
|
-
// Anything that throws from here on (a report path that is a directory, a state file that cannot be written) is a failed
|
|
342
|
+
// Anything that throws from here on (a report path that is a directory, a state file that cannot be written) is a failed wake that is rolled back.
|
|
313
343
|
let problems = [];
|
|
314
344
|
try {
|
|
315
|
-
others.forEach((d, i) => {
|
|
345
|
+
j.others.forEach((d, i) => {
|
|
316
346
|
if (JSON.stringify(fingerprint(d)) !== JSON.stringify(othersBefore[i]))
|
|
317
|
-
problems.push(`the agent changed ${d}, which is
|
|
347
|
+
problems.push(`the agent changed ${d}, which is ${j.othersLabel}`);
|
|
318
348
|
});
|
|
319
349
|
watched.forEach((f, i) => {
|
|
320
350
|
if (!fs.existsSync(f) || fs.readFileSync(f, "utf8") !== watchedBefore[i])
|
|
321
351
|
problems.push(`the agent changed ${f}, which a tidy may not touch`);
|
|
322
352
|
});
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
problems.push(...verifyConservation(before, t.dir, stamp));
|
|
327
|
-
if (!hasReport(archiveDir))
|
|
328
|
-
problems.push(`${ARCHIVE_DIR}/${stamp}/${TIDY_REPORT_FILE} is missing or empty`);
|
|
329
|
-
}
|
|
330
|
-
if (recPart) {
|
|
331
|
-
if (JSON.stringify(earlierArchives(recDir, stamp)) !== JSON.stringify(archivesBefore.rec))
|
|
332
|
-
problems.push(`project records: the agent changed an earlier archive under ${path.join(recDir, ARCHIVE_DIR)}`);
|
|
333
|
-
problems.push(...verifyConservation(recBefore, recDir, stamp).map((p) => `project records: ${p}`));
|
|
334
|
-
if (!hasReport(recArchive))
|
|
335
|
-
problems.push(`project records: ${ARCHIVE_DIR}/${stamp}/${TIDY_REPORT_FILE} is missing or empty`);
|
|
336
|
-
}
|
|
337
|
-
if (!problems.length && memPart && t.dir)
|
|
338
|
-
writeTidyState(t.dir, { ...(changes?.head ? { head: changes.head } : {}), at: (o.now?.() ?? new Date()).toISOString(), archive: stamp });
|
|
353
|
+
problems.push(...j.check());
|
|
354
|
+
if (!problems.length)
|
|
355
|
+
j.onSuccess?.();
|
|
339
356
|
}
|
|
340
357
|
catch (e) {
|
|
341
358
|
problems = [...problems, `the tidy could not be checked or finished: ${e.message}`];
|
|
@@ -343,12 +360,97 @@ async function tidyOne(o, t, stamp, withRecords) {
|
|
|
343
360
|
if (problems.length)
|
|
344
361
|
return fail(problems);
|
|
345
362
|
fs.rmSync(snapshot, { recursive: true, force: true });
|
|
346
|
-
return {
|
|
363
|
+
return { ok: true };
|
|
364
|
+
}
|
|
365
|
+
/** Tidy one agent's memory directory (`dir`, if it has files) and, for the lead, the project records, in one wake-up. */
|
|
366
|
+
async function tidyOne(o, t, stamp, withRecords) {
|
|
367
|
+
const { project } = o;
|
|
368
|
+
const say = o.say ?? ((s) => console.log(s));
|
|
369
|
+
const base = { agent: t.agent, layer: t.layer, dir: t.dir ?? (!withRecords || liveRecordFiles(project).length || !liveProposalFiles(project).length ? recordsDir(project) : proposalsDir(project)) };
|
|
370
|
+
const agent = project.agents[t.agent];
|
|
371
|
+
const live = t.dir ? listMemoryFiles(t.dir).filter((f) => !f.rel.startsWith(`${ARCHIVE_DIR}/`)) : [];
|
|
372
|
+
const recLive = withRecords ? liveRecordFiles(project) : [];
|
|
373
|
+
const propLive = withRecords ? liveProposalFiles(project) : [];
|
|
374
|
+
const memPart = live.length > 0;
|
|
375
|
+
const recPart = recLive.length > 0;
|
|
376
|
+
const propPart = propLive.length > 0;
|
|
377
|
+
if (!memPart && !recPart && !propPart)
|
|
378
|
+
return { ...base, status: "skipped", problems: ["no memory files: nothing to tidy"] };
|
|
379
|
+
const recDir = recordsDir(project);
|
|
380
|
+
const pDir = proposalsDir(project);
|
|
381
|
+
const dirs = [...memoryDirs(agent), ...(recPart ? [recDir] : []), ...(propPart ? [pDir] : [])];
|
|
382
|
+
const others = dirs.filter((d) => d !== t.dir && d !== recDir && d !== pDir);
|
|
383
|
+
const before = t.dir && memPart ? listMemoryFiles(t.dir).map((f) => f.rel) : [];
|
|
384
|
+
const recBefore = recPart ? listMemoryFiles(recDir).map((f) => f.rel) : [];
|
|
385
|
+
const propBefore = propPart ? listMemoryFiles(pDir).map((f) => f.rel) : [];
|
|
386
|
+
const propHashBefore = propPart ? fingerprint(pDir) : {};
|
|
387
|
+
const today = o.now?.() ?? new Date();
|
|
388
|
+
const archivesBefore = { mem: t.dir && memPart ? earlierArchives(t.dir, stamp) : {}, rec: recPart ? earlierArchives(recDir, stamp) : {}, prop: propPart ? earlierArchives(pDir, stamp) : {} };
|
|
389
|
+
const archiveDir = t.dir ? path.join(t.dir, ARCHIVE_DIR, stamp) : undefined;
|
|
390
|
+
const recArchive = path.join(recDir, ARCHIVE_DIR, stamp);
|
|
391
|
+
const propArchive = path.join(pDir, ARCHIVE_DIR, stamp);
|
|
392
|
+
const workDir = tidyWorkDir(project, stamp, t.agent);
|
|
393
|
+
const state = t.dir ? readTidyState(t.dir) : undefined;
|
|
394
|
+
const changes = memPart && t.layer === "project" ? projectChanges(project.dir, state?.head) : undefined;
|
|
395
|
+
const missing = memPart && t.dir ? findMissingPaths(t.dir, t.layer === "project" ? project.dir : undefined) : [];
|
|
396
|
+
const index = t.dir && fs.existsSync(path.join(t.dir, "MEMORY.md")) ? fs.readFileSync(path.join(t.dir, "MEMORY.md"), "utf8") : "";
|
|
397
|
+
const prompts = buildTidyPrompts(agent, {
|
|
398
|
+
layer: t.layer,
|
|
399
|
+
...(memPart ? { dir: t.dir, archiveDir } : {}),
|
|
400
|
+
files: live,
|
|
401
|
+
index,
|
|
402
|
+
changes,
|
|
403
|
+
missing,
|
|
404
|
+
...(recPart ? { records: recordsContext(project, recArchive, recLive) } : {}),
|
|
405
|
+
...(propPart ? { proposals: proposalsContext(project, propArchive, propLive, today) } : {}),
|
|
406
|
+
});
|
|
407
|
+
const allowDirs = [...(memPart && t.dir ? [t.dir] : []), ...(recPart ? [recDir] : []), ...(propPart ? [pDir] : [])];
|
|
408
|
+
say(`tidying ${t.layer} memory of ${t.agent} (${live.length} file(s)${recPart ? `, ${recLive.length} project record file(s)` : ""}${propPart ? `, ${propLive.length} proposal file(s)` : ""}) …`);
|
|
409
|
+
const woke = await wakeWithRollback({
|
|
410
|
+
project,
|
|
411
|
+
agentName: t.agent,
|
|
412
|
+
dirs,
|
|
413
|
+
allowDirs,
|
|
414
|
+
others,
|
|
415
|
+
othersLabel: `not the ${t.layer} memory being tidied`,
|
|
416
|
+
workDir,
|
|
417
|
+
prompts,
|
|
418
|
+
invoker: o.invoker,
|
|
419
|
+
signal: o.signal,
|
|
420
|
+
check: () => {
|
|
421
|
+
const problems = [];
|
|
422
|
+
if (memPart && t.dir && archiveDir) {
|
|
423
|
+
if (JSON.stringify(earlierArchives(t.dir, stamp)) !== JSON.stringify(archivesBefore.mem))
|
|
424
|
+
problems.push(`the agent changed an earlier archive under ${path.join(t.dir, ARCHIVE_DIR)}`);
|
|
425
|
+
problems.push(...verifyConservation(before, t.dir, stamp));
|
|
426
|
+
if (!hasReport(archiveDir))
|
|
427
|
+
problems.push(`${ARCHIVE_DIR}/${stamp}/${TIDY_REPORT_FILE} is missing or empty`);
|
|
428
|
+
}
|
|
429
|
+
if (recPart) {
|
|
430
|
+
if (JSON.stringify(earlierArchives(recDir, stamp)) !== JSON.stringify(archivesBefore.rec))
|
|
431
|
+
problems.push(`project records: the agent changed an earlier archive under ${path.join(recDir, ARCHIVE_DIR)}`);
|
|
432
|
+
problems.push(...verifyConservation(recBefore, recDir, stamp).map((p) => `project records: ${p}`));
|
|
433
|
+
if (!hasReport(recArchive))
|
|
434
|
+
problems.push(`project records: ${ARCHIVE_DIR}/${stamp}/${TIDY_REPORT_FILE} is missing or empty`);
|
|
435
|
+
}
|
|
436
|
+
if (propPart)
|
|
437
|
+
problems.push(...checkProposalsTidy(pDir, propBefore, propHashBefore, earlierArchives(pDir, stamp), archivesBefore.prop, stamp, today));
|
|
438
|
+
return problems;
|
|
439
|
+
},
|
|
440
|
+
onSuccess: () => {
|
|
441
|
+
if (memPart && t.dir)
|
|
442
|
+
writeTidyState(t.dir, { ...(changes?.head ? { head: changes.head } : {}), at: (o.now?.() ?? new Date()).toISOString(), archive: stamp });
|
|
443
|
+
},
|
|
444
|
+
});
|
|
445
|
+
const parts = { ...(recPart ? { recordsDir: recDir } : {}), ...(propPart ? { proposalsDir: pDir } : {}) };
|
|
446
|
+
if (!woke.ok)
|
|
447
|
+
return { ...base, status: "failed", ...parts, problems: woke.problems };
|
|
448
|
+
return { ...base, status: "tidied", archive: stamp, ...parts, problems: [] };
|
|
347
449
|
}
|
|
348
450
|
/** Tidy the memory of the chosen agents, one after another. Only ever called by a person (`agent-lyceum tidy`). */
|
|
349
451
|
export async function tidyMemory(o) {
|
|
350
452
|
const say = o.say ?? ((s) => console.log(s));
|
|
351
|
-
const withRecords = recordsInScope(o) && liveRecordFiles(o.project).length > 0;
|
|
453
|
+
const withRecords = recordsInScope(o) && (liveRecordFiles(o.project).length > 0 || liveProposalFiles(o.project).length > 0);
|
|
352
454
|
const lead = o.project.lead;
|
|
353
455
|
let targets;
|
|
354
456
|
try {
|
|
@@ -369,8 +471,12 @@ export async function tidyMemory(o) {
|
|
|
369
471
|
if (o.dryRun) {
|
|
370
472
|
if (j.dir)
|
|
371
473
|
describeTarget(j, o.project, say);
|
|
372
|
-
if (withRecords && j.agent === lead)
|
|
474
|
+
if (withRecords && j.agent === lead) {
|
|
373
475
|
describeRecords(o.project, say);
|
|
476
|
+
const n = liveProposalFiles(o.project).length;
|
|
477
|
+
if (n)
|
|
478
|
+
say(`proposals ${proposalsDir(o.project)}: ${n} file(s)`);
|
|
479
|
+
}
|
|
374
480
|
results.push({ agent: j.agent, layer: j.layer, dir: j.dir ?? recordsDir(o.project), status: "dry-run", problems: [] });
|
|
375
481
|
continue;
|
|
376
482
|
}
|
|
@@ -426,6 +532,9 @@ export function restoreMemory(o) {
|
|
|
426
532
|
const recDir = recordsDir(o.project);
|
|
427
533
|
const recArchive = path.join(recDir, ARCHIVE_DIR, o.stamp);
|
|
428
534
|
const recHas = isLead && isPlainDir(recDir) && fs.existsSync(recArchive);
|
|
535
|
+
const pDir = proposalsDir(o.project);
|
|
536
|
+
const propArchive = path.join(pDir, ARCHIVE_DIR, o.stamp);
|
|
537
|
+
const propHas = isLead && isPlainDir(pDir) && fs.existsSync(propArchive);
|
|
429
538
|
let dir;
|
|
430
539
|
try {
|
|
431
540
|
dir = targetsFor(o.project, { layer: o.layer, agent: o.agent })[0].dir;
|
|
@@ -436,7 +545,7 @@ export function restoreMemory(o) {
|
|
|
436
545
|
}
|
|
437
546
|
const archive = dir ? path.join(dir, ARCHIVE_DIR, o.stamp) : undefined;
|
|
438
547
|
const memHas = !!archive && fs.existsSync(archive);
|
|
439
|
-
if (!memHas && !recHas) {
|
|
548
|
+
if (!memHas && !recHas && !propHas) {
|
|
440
549
|
const recs = isLead ? `${dir ? "; project records archives: " : " Project records archives: "}${listArchives(recDir).join(", ") || "(none)"}` : "";
|
|
441
550
|
throw new Error(`No archive ${o.stamp} for ${o.agent} (${o.layer}).${dir ? ` Archives: ${listArchives(dir).join(", ") || "(none)"}` : ""}${recs}.`);
|
|
442
551
|
}
|
|
@@ -448,7 +557,7 @@ export function restoreMemory(o) {
|
|
|
448
557
|
if (add.length)
|
|
449
558
|
atomicWrite(indexFile, `${index.replace(/\n*$/, index ? "\n" : "")}${add.join("\n")}\n`);
|
|
450
559
|
}
|
|
451
|
-
return { ...mem, ...(recHas ? { records: moveBack(recDir, recArchive) } : {}) };
|
|
560
|
+
return { ...mem, ...(recHas ? { records: moveBack(recDir, recArchive) } : {}), ...(propHas ? { proposals: moveBack(pDir, propArchive) } : {}) };
|
|
452
561
|
}
|
|
453
562
|
/** The stamps of the archives in a memory directory, oldest first. */
|
|
454
563
|
export function listArchives(dir) {
|
package/dist/policy.d.ts
CHANGED
|
@@ -26,6 +26,14 @@ export declare function commonFile(project: ResolvedProject): string;
|
|
|
26
26
|
/** Durable decisions and lessons of the project, one file per entry. Only the lead may write them. `.archive/` is tidy's output, never shown in prompts. */
|
|
27
27
|
export declare function recordsDir(project: ResolvedProject): string;
|
|
28
28
|
export declare function recordsKindDir(project: ResolvedProject, kind: "decisions" | "lessons"): string;
|
|
29
|
+
/**
|
|
30
|
+
* Proposals of lessons and decisions written by agents that run alone (spec 30). A sibling of `shared/`, not under `records/`:
|
|
31
|
+
* the records directory is denied to everyone but the lead, and a parent's deny beats a child's allow.
|
|
32
|
+
*/
|
|
33
|
+
export declare function proposalsDir(project: ResolvedProject): string;
|
|
34
|
+
/** Whether the agent of this solo project may write proposals: `run --agent`, and `call -p` (a plain `call` has no project). */
|
|
35
|
+
export declare const mayPropose: (project: ResolvedProject) => boolean;
|
|
36
|
+
export declare function proposalsKindDir(project: ResolvedProject, kind: "decisions" | "lessons"): string;
|
|
29
37
|
/** The lead-maintained files shared by every run of the project. */
|
|
30
38
|
export declare function sharedContextFiles(project: ResolvedProject): string[];
|
|
31
39
|
export declare function outboxDir(project: ResolvedProject, agent: string): string;
|
package/dist/policy.js
CHANGED
|
@@ -12,6 +12,18 @@ export function recordsDir(project) {
|
|
|
12
12
|
export function recordsKindDir(project, kind) {
|
|
13
13
|
return path.join(recordsDir(project), kind);
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Proposals of lessons and decisions written by agents that run alone (spec 30). A sibling of `shared/`, not under `records/`:
|
|
17
|
+
* the records directory is denied to everyone but the lead, and a parent's deny beats a child's allow.
|
|
18
|
+
*/
|
|
19
|
+
export function proposalsDir(project) {
|
|
20
|
+
return project.proposalsRef?.proposalsRoot ?? path.join(project.paths.root, "proposals");
|
|
21
|
+
}
|
|
22
|
+
/** Whether the agent of this solo project may write proposals: `run --agent`, and `call -p` (a plain `call` has no project). */
|
|
23
|
+
export const mayPropose = (project) => project.solo === "run" || (project.solo === "call" && !!project.proposalsRef);
|
|
24
|
+
export function proposalsKindDir(project, kind) {
|
|
25
|
+
return path.join(proposalsDir(project), kind);
|
|
26
|
+
}
|
|
15
27
|
/** The lead-maintained files shared by every run of the project. */
|
|
16
28
|
export function sharedContextFiles(project) {
|
|
17
29
|
return [commonFile(project)];
|
|
@@ -142,6 +154,8 @@ export function writePolicy(project, agent) {
|
|
|
142
154
|
...(ws ? [ws.gitDir] : []),
|
|
143
155
|
...(shared && mayWriteShared ? [shared] : []),
|
|
144
156
|
...(isLead ? [recordsDir(project)] : []),
|
|
157
|
+
// Proposals: the lead (it decides them) and a solo agent (it writes them); the guard holds the solo agent to its own `proposed` files.
|
|
158
|
+
...(isLead || mayPropose(project) ? [proposalsDir(project)] : []),
|
|
145
159
|
...(grantOwnDir ? [ownDir] : []),
|
|
146
160
|
// Spec 25: canonical, because the OS sandbox compares real paths (`/tmp` is `/private/tmp` on macOS).
|
|
147
161
|
...(agent.sandbox?.write ?? []).map(canonicalPath),
|
package/dist/project-lock.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export interface LockOptions {
|
|
|
8
8
|
checkout?: boolean;
|
|
9
9
|
/** The holder needs the whole project (e.g. a memory tidy): refused while any other lock is alive, and it keeps every other lock out while held. */
|
|
10
10
|
exclusive?: boolean;
|
|
11
|
+
/** The holder is a `call -p` (spec 30), not a run: it counts as active for an exclusive holder, but is not "another run" to a run's guard. */
|
|
12
|
+
call?: boolean;
|
|
11
13
|
/** For a checkout run's refusal message: the checkout directory, and why this run has no worktree. */
|
|
12
14
|
dir?: string;
|
|
13
15
|
reason?: string;
|
|
@@ -25,6 +27,8 @@ export interface LockInfo {
|
|
|
25
27
|
exclusive?: boolean;
|
|
26
28
|
/** `true`: works in the project checkout; `false`: in its own worktree (or holds no files, like `clear`). Absent: an older version's run, treated as a checkout run. */
|
|
27
29
|
checkout?: boolean;
|
|
30
|
+
/** Held by a `call -p` (see `LockOptions.call`). */
|
|
31
|
+
call?: boolean;
|
|
28
32
|
}
|
|
29
33
|
export type LockKind = "checkout" | "whole" | "worktree";
|
|
30
34
|
/** What a lock protects: the checkout, the whole project (tidy/restore), or nothing shared (a worktree run). Locks of older versions count as checkout runs. */
|
package/dist/project-lock.js
CHANGED
|
@@ -296,6 +296,7 @@ export function acquireProjectLock(root, runId, opts = {}) {
|
|
|
296
296
|
heartbeat_at: now,
|
|
297
297
|
...(opts.exclusive || opts.checkout ? { exclusive: true } : {}),
|
|
298
298
|
checkout: opts.checkout === true,
|
|
299
|
+
...(opts.call ? { call: true } : {}),
|
|
299
300
|
};
|
|
300
301
|
const kind = lockKind(info);
|
|
301
302
|
withAdmission(root, () => {
|
package/dist/prompt.d.ts
CHANGED
|
@@ -29,8 +29,15 @@ export declare const PERSONA_LIMIT: number;
|
|
|
29
29
|
export declare function readPersona(agent: ResolvedAgent): string;
|
|
30
30
|
/** The protocol of an agent working alone: no teammates, no ask. `run --agent` still ends with a `done` mail; `call` just answers. */
|
|
31
31
|
export declare function buildSoloSystemPrompt(project: ResolvedProject, agent: ResolvedAgent): string;
|
|
32
|
+
/** The lead's note about proposals waiting for a decision (spec 30, D-06, D-11, D-15); empty when there are none. */
|
|
33
|
+
export declare function pendingProposalLines(project: ResolvedProject): string[];
|
|
34
|
+
/** The prompts of a `proposals review` wake-up (spec 30, D-14): the lead decides the pending proposals and writes the records, nothing else. */
|
|
35
|
+
export declare function buildReviewPrompts(project: ResolvedProject, agent: ResolvedAgent, reportFile: string): {
|
|
36
|
+
systemPrompt: string;
|
|
37
|
+
userPrompt: string;
|
|
38
|
+
};
|
|
32
39
|
/** The prompt of a `call`: the memory indexes the agent is used to, then the task. */
|
|
33
|
-
export declare function buildCallUserPrompt(agent: ResolvedAgent, task: string): string;
|
|
40
|
+
export declare function buildCallUserPrompt(agent: ResolvedAgent, task: string, project?: ResolvedProject): string;
|
|
34
41
|
/** Everything that goes to the runtime's system prompt: the persona plus the team protocol. */
|
|
35
42
|
export declare function buildSystemPrompt(project: ResolvedProject, agent: ResolvedAgent): string;
|
|
36
43
|
/**
|
|
@@ -70,6 +77,27 @@ export interface TidyRecordsContext {
|
|
|
70
77
|
/** Live files that are not entries (kept as they are). */
|
|
71
78
|
otherFiles: MemoryFile[];
|
|
72
79
|
}
|
|
80
|
+
/** The proposals of lessons and decisions (spec 30, D-12): the lead's tidy also archives the ones decided long enough ago. */
|
|
81
|
+
export interface TidyProposalsContext {
|
|
82
|
+
/** The proposals folder (`<project>/proposals`). */
|
|
83
|
+
dir: string;
|
|
84
|
+
/** Absolute path of this tidy's archive folder for the proposals (the agent creates it). */
|
|
85
|
+
archiveDir: string;
|
|
86
|
+
/** Today, `YYYY-MM-DD`; the rule below counts from it. */
|
|
87
|
+
today: string;
|
|
88
|
+
/** Days a proposal stays after it was decided. */
|
|
89
|
+
keepDays: number;
|
|
90
|
+
/** One line per proposal: rel path, status, date, decided date, size and title. */
|
|
91
|
+
entries: {
|
|
92
|
+
rel: string;
|
|
93
|
+
status: string;
|
|
94
|
+
date: string;
|
|
95
|
+
decided: string;
|
|
96
|
+
bytes: number;
|
|
97
|
+
title: string;
|
|
98
|
+
eligible: boolean;
|
|
99
|
+
}[];
|
|
100
|
+
}
|
|
73
101
|
export interface TidyContext {
|
|
74
102
|
layer: Layer;
|
|
75
103
|
/** The memory directory being tidied; absent when only the project records are tidied (the lead has no memory directory). */
|
|
@@ -85,6 +113,8 @@ export interface TidyContext {
|
|
|
85
113
|
missing: MissingPath[];
|
|
86
114
|
/** Set only for the lead's project-layer tidy: the project records are tidied in the same wake-up. */
|
|
87
115
|
records?: TidyRecordsContext;
|
|
116
|
+
/** Set for the lead's project-layer tidy when the project has proposals. */
|
|
117
|
+
proposals?: TidyProposalsContext;
|
|
88
118
|
}
|
|
89
119
|
/** The prompts of a memory-tidy wake-up: a maintenance job, not a task, so it has none of the mail protocol. */
|
|
90
120
|
export declare function buildTidyPrompts(agent: ResolvedAgent, ctx: TidyContext): {
|