Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

repair: an item on a page with a pending write from the run waits for it, so a many-fact page applies in one run (#7436)

garrytand77df225d341
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index bdfbdab9ef..a03130b705 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1178,7 +1178,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
// #7341 (GBRA-133 wave 3). `since` is restamped at the merge slot.
{ since: '0.60.401.0', text: 'A nested folder-scaffolding file (`index.md`, `README.md`, `log.md`, `schema.md`, `RESOLVER.md`) whose frontmatter `slug:` names another page, for example `slug: projects/acme-example` or just `slug: acme-example` in `projects/acme-example/index.md`, is skipped by sync again, as before 0.60.337.0, even when it has `type:` or `title:`. Since then it was imported only to be held `frontmatter_slug_conflict`, so a folder of such hubs became one hold per file. The next sync releases those holds. One whose `slug:` is its own path slug, or that has none, is still a page; remove the `slug:` line to make a skipped one a page.' },
// #7436 (GBRA-151). `since` is restamped at the merge slot.
- { since: 'TBD', text: '`gbrain repair <kind> --apply` no longer stops with `idempotency_conflict` when two of its items write the same page (two facts of one page in `repair visibility`, two duplicate groups or two takes on one page) while the first write is still pending: each item now has its own request id, where before both used the page and revision. A rerun of the same approved set still replays its own pending writes; an item whose page an earlier item changed is skipped for the next full scan, as before (#7436).' },
+ { since: 'TBD', text: '`gbrain repair <kind> --apply` no longer stops with `idempotency_conflict` when two of its items write the same page (two facts of one page in `repair visibility`, two duplicate groups or two takes on one page) while the first write is still pending: each item now has its own request id, where before both used the page and revision, and an item whose page has a pending write from the same run waits for that write (up to the write wait, then the run stops resumable as `write_pending`) and writes the page\'s new revision, so every fact of a many-fact page applies in one run. A rerun of the same approved set still replays its own pending writes (#7436).' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/repair/core.ts b/src/core/repair/core.ts
index cc16c7dc6e..0c3fd9f858 100644
--- a/src/core/repair/core.ts
+++ b/src/core/repair/core.ts
@@ -452,7 +452,9 @@ async function runRepairScoped(ctx: OperationContext, handler: RepairHandler, sc
opts.progress?.tick(1, `${item.source_id}:${item.slug}`);
// A preview-bound kind may retire its approved set with its last item, so every earlier receipt settles before that item runs.
const lastApproved = !!opts.expect && index === plan.items.length - 1;
- if (unresolved.length && (unresolved.length >= window || lastApproved) && !await settle(lastApproved ? 0 : window - 1)) {
+ // An item on a page this run's pending write is still changing waits for it, so it writes the page's new revision.
+ const samePage = unresolved.some(open => open.item.source_id === item.source_id && open.item.slug === item.slug);
+ if (unresolved.length && (unresolved.length >= window || lastApproved || samePage) && !await settle(lastApproved || samePage ? 0 : window - 1)) {
result.stopped = pendingStop(settleMs ? `; nothing published for ${Math.round(settleMs / 1000)}s` : '');
return finish();
}
diff --git a/test/repair-request-id-per-item-7436.test.ts b/test/repair-request-id-per-item-7436.test.ts
index 51c04d945b..1e1886e1ee 100644
--- a/test/repair-request-id-per-item-7436.test.ts
+++ b/test/repair-request-id-per-item-7436.test.ts
@@ -1,8 +1,9 @@
/**
- * A repair item's request id named its page and revision, not the item. Two items of one run that write the same page
- * (two facts of one fence) at the same revision got one id: while the first write was still pending, the second item
- * reused its id for a different write, and admission refused it as idempotency_conflict, which an approved apply
- * (--expect) does not continue past (#7436). Each item now gets its own id.
+ * #7436: a repair item's request id named its page and revision, not the item. Two items of one run that write the same
+ * page (two facts of one fence) at the same revision got one id: while the first write was still pending, the second
+ * item reused its id for a different write, and admission refused it as idempotency_conflict, which an approved apply
+ * (--expect) does not continue past. Each item now gets its own id, and an item whose page has a pending write from the
+ * run waits for it, so every fact of a many-fact page is applied in one run.
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
@@ -29,38 +30,55 @@ const ctxFor = (sourceId: string, writeWaitMs?: number) => ({ engine, sourceId,
dryRun: false, logger: quiet, ...(writeWaitMs === undefined ? {} : { writeWaitMs }) }) as never;
const page = (body: string) => `---\ntype: note\ntitle: Example\n---\n${body}\n`;
-describe('repair request ids are per item', () => {
- test('a second item writing the same page while the first is pending is admitted under its own id, not refused as idempotency_conflict', async () => {
- const dir = mkdtempSync(join(dataDir, 'case-'));
- const source = `repair-${randomUUID().slice(0, 8)}`;
- await withEnv({ GBRAIN_HOME: join(dir, 'home') }, async () => {
- try {
- const root = join(dir, source); mkdirSync(root);
- await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', [source, root]);
- await claimWorktree(engine, source, root);
- await submitPageMutation(ctxFor(source), { operation: 'put_page', params: { slug: 'notes/a', content: page('Fact one. Fact two.'), request_id: randomUUID() } });
- const ids: string[] = [];
- const item = (id: number): RepairItem => ({ cursor: { phase: 0, id }, source_id: source, slug: 'notes/a', chars: 1, action: `mark fact ${id}` });
- const handler: RepairHandler = { kind: 'visibility', embeds: false,
- async plan(_engine, _scope, after) { return { items: [0, 1, 2].map(item).filter(i => !after || i.cursor.id > after.id), residuals: {}, preview_hash: 'h' }; },
- async apply(ctx, it) {
- const snapshot = (await engine.readPageSnapshot('notes/a', { sourceId: source }))!;
- const request_id = await repairRequestId(ctx, 'visibility', it, snapshot.revision);
- ids.push(request_id);
- await submitPageMutation(ctx, { operation: 'put_page', params: { slug: 'notes/a', source_id: source, expected_revision: snapshot.revision,
- content: page(`Fact one. Fact two. Marked ${it.cursor.id}.`), request_id } });
- return true;
- } };
- let held = true;
- installFaultHook(async point => { if (point === 'consumer:prepared' && held) { held = false; await new Promise(resolve => setTimeout(resolve, 1_500)); } });
- const scope = await resolveRepairScope(engine);
- const result = await runRepair(ctxFor(source, 1_000), handler, scope, { apply: true, spec: repairSpec('visibility'), embed: false, expect: 'h',
- publicationOwner: async () => ({ alive: true, owner: 'this process' }) });
- expect(new Set(ids).size).toBe(3);
- expect(result.failed).toEqual([]);
- expect(result.stopped).toBeUndefined();
- expect(result.applied + result.skipped).toBe(3);
- } finally { installFaultHook(undefined); await disposePersistenceConsumer(engine); }
- });
+/** Applies `count` approved items that each mark the same page, with the first publication held past the write wait. */
+async function markOnePage(count: number) {
+ const dir = mkdtempSync(join(dataDir, 'case-'));
+ const source = `repair-${randomUUID().slice(0, 8)}`;
+ return withEnv({ GBRAIN_HOME: join(dir, 'home') }, async () => {
+ try {
+ const root = join(dir, source); mkdirSync(root);
+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', [source, root]);
+ await claimWorktree(engine, source, root);
+ await submitPageMutation(ctxFor(source), { operation: 'put_page', params: { slug: 'people/alice-example', content: page('Facts.'), request_id: randomUUID() } });
+ const ids: string[] = [];
+ const item = (id: number): RepairItem => ({ cursor: { phase: 0, id }, source_id: source, slug: 'people/alice-example', chars: 1, action: `mark fact ${id}` });
+ const handler: RepairHandler = { kind: 'visibility', embeds: false,
+ async plan(_engine, _scope, after) { return { items: Array.from({ length: count }, (_, i) => item(i)).filter(i => !after || i.cursor.id > after.id), residuals: {}, preview_hash: 'h' }; },
+ async apply(ctx, it) {
+ const snapshot = (await engine.readPageSnapshot('people/alice-example', { sourceId: source }))!;
+ const request_id = await repairRequestId(ctx, 'visibility', it, snapshot.revision);
+ ids.push(request_id);
+ await submitPageMutation(ctx, { operation: 'put_page', params: { slug: 'people/alice-example', source_id: source, expected_revision: snapshot.revision,
+ content: page(`${snapshot.page.compiled_truth.trim()} M{${it.cursor.id}}.`), request_id } });
+ return true;
+ } };
+ let held = true;
+ installFaultHook(async point => { if (point === 'consumer:prepared' && held) { held = false; await new Promise(resolve => setTimeout(resolve, 1_500)); } });
+ const scope = await resolveRepairScope(engine);
+ const result = await runRepair(ctxFor(source, 1_000), handler, scope, { apply: true, spec: repairSpec('visibility'), embed: false, expect: 'h',
+ publicationOwner: async () => ({ alive: true, owner: 'this process' }) });
+ const body = (await engine.readPageSnapshot('people/alice-example', { sourceId: source }))!.page.compiled_truth;
+ return { ids, result, marks: [...body.matchAll(/M\{(\d+)\}/g)].map(m => Number(m[1])) };
+ } finally { installFaultHook(undefined); await disposePersistenceConsumer(engine); }
+ });
+}
+
+describe('#7436: repair request ids are per item', () => {
+ test('a second item on a page whose first write is pending gets its own id and writes after it, instead of idempotency_conflict', async () => {
+ const { ids, result, marks } = await markOnePage(3);
+ expect(new Set(ids).size).toBe(3);
+ expect(result.failed).toEqual([]);
+ expect(result.stopped).toBeUndefined();
+ expect(result).toMatchObject({ applied: 3, skipped: 0, complete: true });
+ expect(marks).toEqual([0, 1, 2]);
}, 120_000);
+
+ test('86 items on one page all apply in one run', async () => {
+ const { ids, result, marks } = await markOnePage(86);
+ expect(new Set(ids).size).toBe(86);
+ expect(result.failed).toEqual([]);
+ expect(result.stopped).toBeUndefined();
+ expect(result).toMatchObject({ applied: 86, skipped: 0, complete: true });
+ expect(marks).toEqual(Array.from({ length: 86 }, (_, i) => i));
+ }, 240_000);
});