Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Reset the scratch-promote trigger only when the promotion write lands (#1338)

Sina Matian52f65c46044b
diff --git a/src/memory/strategies/scratch-promote.ts b/src/memory/strategies/scratch-promote.ts
index 216c0a400..065e4b1fd 100644
--- a/src/memory/strategies/scratch-promote.ts
+++ b/src/memory/strategies/scratch-promote.ts
@@ -104,12 +104,6 @@ export function createScratchPromote(deps: ScratchPromoteDeps): { strategy: Memo
return m ? Number(m[1]) : 0;
}
- async function resetMarker(scopeId: ScopeId): Promise<void> {
- await rewriteMarker(scopeId, (body) =>
- MARKER_RE.test(body) ? body.replace(MARKER_RE, "<!-- captures-since-promote: 0 -->") : null,
- );
- }
-
async function readLogWindow(
scopeId: ScopeId,
now: number,
@@ -157,7 +151,6 @@ export function createScratchPromote(deps: ScratchPromoteDeps): { strategy: Memo
const count = await bumpMarker(scopeId, added.length);
if (deps.consolidateAfter > 0 && count >= deps.consolidateAfter) {
- await resetMarker(scopeId);
await strategy.maintain!(scopeId).catch(() => {});
}
return added.length;
@@ -204,18 +197,16 @@ export function createScratchPromote(deps: ScratchPromoteDeps): { strategy: Memo
const longTerm = stripMarker(raw);
const scratch = window.map(({ date, body }) => `## ${date}\n${body}`).join("\n\n");
const out = (
- (await deps.harness.oneShot(
- PROMOTION_PROMPT,
- `Current notebook:\n${longTerm || "(empty)"}\n\nScratch log:\n${scratch}`,
- )) ?? ""
+ (await deps.harness
+ .oneShot(PROMOTION_PROMPT, `Current notebook:\n${longTerm || "(empty)"}\n\nScratch log:\n${scratch}`)
+ .catch(() => "")) ?? ""
).trim();
- if (!out || out.length > MAX_PROMOTED_NOTEBOOK_CHARS) return;
- if (!/^none$/i.test(out)) {
- if (head) {
- await base.replaceIfRevision!(scopeId, out, head.revision);
- } else {
- await base.replace(scopeId, out);
- }
+ const promoted = out && out.length <= MAX_PROMOTED_NOTEBOOK_CHARS && !/^none$/i.test(out);
+ const next = promoted ? out : longTerm;
+ if (head) {
+ if (!(await base.replaceIfRevision!(scopeId, next, head.revision))) return;
+ } else {
+ await base.replace(scopeId, next);
}
}
const cutoff = dateStr(now - LOG_RETENTION_DAYS * 86_400_000);
diff --git a/test/memory-strategy-consolidation.test.ts b/test/memory-strategy-consolidation.test.ts
index 4b189a96c..1cb6ef8a9 100644
--- a/test/memory-strategy-consolidation.test.ts
+++ b/test/memory-strategy-consolidation.test.ts
@@ -197,44 +197,29 @@ test("an edit landing during consolidation survives without disabling later cons
assert.equal(calls, 2);
});
-test("an edit landing during consolidation survives without disabling later consolidation", async () => {
+test("a lost consolidation race leaves the after-N trigger armed, so the next capture retries", async () => {
const { memory } = freshMemory();
await memory.capture(SCOPE, ["original fact"], AT);
let calls = 0;
- let release!: () => void;
- const blocked = new Promise<void>((resolve) => {
- release = resolve;
- });
- let modelStarted!: () => void;
- const waiting = new Promise<void>((resolve) => {
- modelStarted = resolve;
- });
- const logs: string[] = [];
const consolidator = createConsolidator({
harness: {
async oneShot() {
calls++;
- modelStarted();
- await blocked;
+ if (calls === 1) await memory.replace(SCOPE, "# Memory\n\n- user edit");
return "UPDATE 1: consolidated fact";
},
},
memory,
- log: (message) => logs.push(message),
+ afterN: 1,
})!;
- const first = consolidator.maintain(SCOPE);
- await waiting;
- await memory.replace(SCOPE, "# Memory\n\n- user edit");
- release();
- await first;
-
- assert.match(await memory.read(SCOPE), /user edit/);
- assert.doesNotMatch(await memory.read(SCOPE), /consolidated fact/);
- assert.deepEqual(logs, []);
+ await consolidator.maybeMaintain(SCOPE);
+ assert.equal(calls, 1);
+ assert.doesNotMatch(await memory.read(SCOPE), /consolidated:/, "a dropped write lands no marker");
- await consolidator.maintain(SCOPE);
+ await consolidator.maybeMaintain(SCOPE);
assert.equal(calls, 2);
+ assert.match(await memory.read(SCOPE), /consolidated fact/);
});
test("degrades to capture-only when the store can't round-trip a rewrite: logs once, stops trying, never crashes", async () => {
diff --git a/test/memory-strategy-scratch-promote.test.ts b/test/memory-strategy-scratch-promote.test.ts
index 32293ea8c..5c0d57098 100644
--- a/test/memory-strategy-scratch-promote.test.ts
+++ b/test/memory-strategy-scratch-promote.test.ts
@@ -191,24 +191,31 @@ test("maintain promotes: one-shot judges the window, rewrites MEMORY.md, leaves
assert.equal(await workspace.read(SCOPE, logPath(TODAY)), logBefore, "log untouched");
});
-test("maintain refuses a runaway promotion: an oversized one-shot output leaves the notebook untouched and skips log pruning", async () => {
+test("a thrown or runaway promotion leaves the notebook's facts alone but still consumes the trigger, so the next capture does not refire", async () => {
const runaway = "I'll scan for new signed replies… ".repeat(2_000);
- const { workspace, strategy, memory } = fresh({ oneShot: () => Promise.resolve(runaway) });
- await memory.replace(SCOPE, "# Memory\n\n- keep me");
- await memory.capture(SCOPE, ["recent"], TODAY);
- const ancient = TODAY - 30 * DAY;
- await workspace.write(SCOPE, logPath(ancient), "- (2026-05-11) ancient\n");
-
- await withNow(TODAY, () => strategy.maintain!(SCOPE));
-
- const notebook = (await workspace.read(SCOPE, MEMORY_FILE)) ?? "";
- assert.match(notebook, /keep me/, "the existing notebook survives");
- assert.doesNotMatch(notebook, /scan for new signed replies/, "the runaway output is never persisted");
- assert.match(
- (await workspace.read(SCOPE, logPath(ancient))) ?? "",
- /ancient/,
- "unpromoted logs are not pruned when promotion was discarded",
- );
+ const verdicts = [() => Promise.reject(new Error("model down")), () => Promise.resolve(runaway)];
+ for (const verdict of verdicts) {
+ let calls = 0;
+ const { memory } = fresh({
+ oneShot: () => {
+ calls++;
+ return verdict();
+ },
+ consolidateAfter: 2,
+ });
+ await memory.replace(SCOPE, "# Memory\n\n- keep me");
+ await withNow(TODAY, async () => {
+ await memory.capture(SCOPE, ["first"], TODAY);
+ await memory.capture(SCOPE, ["second"], TODAY);
+ assert.equal(calls, 1);
+ const notebook = await memory.read(SCOPE);
+ assert.match(notebook, /keep me/, "the existing notebook survives");
+ assert.doesNotMatch(notebook, /scan for new signed replies/, "the runaway output is never persisted");
+ assert.doesNotMatch(notebook, /captures-since-promote/, "the trigger is consumed");
+ await memory.capture(SCOPE, ["third"], TODAY);
+ });
+ assert.equal(calls, 1, "a discarded pass does not refire on the next capture");
+ }
});
test("maintain is a no-op rewrite when the judge says NONE, and prunes logs past retention", async () => {
@@ -256,6 +263,47 @@ test("after-N marker trigger: the Nth capture fires promotion and resets the dur
);
});
+test("a NONE verdict consumes the trigger without touching the notebook's facts", async () => {
+ const { memory } = fresh({ oneShot: () => Promise.resolve("NONE"), consolidateAfter: 1 });
+ await memory.replace(SCOPE, "# Memory\n\n- keep me");
+ await withNow(TODAY, () => memory.capture(SCOPE, ["fact"], TODAY));
+ const notebook = await memory.read(SCOPE);
+ assert.match(notebook, /keep me/);
+ assert.doesNotMatch(notebook, /captures-since-promote/, "the marker is stripped with the write");
+});
+
+test("a capture landing during promotion leaves the trigger armed, so the next capture retries the dropped pass", async () => {
+ const workspace = createLocalWorkspaceStore(mkdtempSync(join(tmpdir(), "msp-")));
+ const base = createMemoryService(workspace);
+ const otherProcess = createScratchPromote({ harness: harnessOf(), memory: base, workspace, consolidateAfter: 0 });
+ let promotions = 0;
+ const { memory } = createScratchPromote({
+ harness: harnessOf(async () => {
+ promotions++;
+ if (promotions === 1) await otherProcess.memory.capture(SCOPE, ["landed mid-promotion"], TODAY);
+ return "# Memory\n\n- promoted";
+ }),
+ memory: base,
+ workspace,
+ consolidateAfter: 2,
+ });
+
+ await withNow(TODAY, async () => {
+ await memory.capture(SCOPE, ["fact one"], TODAY);
+ await memory.capture(SCOPE, ["fact two"], TODAY);
+ assert.equal(promotions, 1);
+ const notebook = await base.read(SCOPE);
+ assert.doesNotMatch(notebook, /promoted/, "the stale promotion is dropped");
+ assert.match(notebook, /captures-since-promote: 3/, "a lost race leaves the counter untouched");
+ await memory.capture(SCOPE, ["fact three"], TODAY);
+ });
+
+ assert.equal(promotions, 2, "the very next capture retries the dropped pass");
+ const notebook = await base.read(SCOPE);
+ assert.match(notebook, /promoted/);
+ assert.doesNotMatch(notebook, /captures-since-promote/, "a landed promotion consumes the trigger");
+});
+
test("concurrent captures retain both marker increments", async () => {
const { memory } = fresh();
await Promise.all([