Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
fix(ingest): resume a bulk commit from committed_patch_ids on a plan-mismatch 409
KageBinaryc5e3cf821e9a
diff --git a/ix-cli/src/cli/__tests__/helpers/fake-backend.ts b/ix-cli/src/cli/__tests__/helpers/fake-backend.tsindex 681a867c..3ce062bc 100644--- a/ix-cli/src/cli/__tests__/helpers/fake-backend.ts+++ b/ix-cli/src/cli/__tests__/helpers/fake-backend.ts@@ -10,6 +10,17 @@ type SentPatch = {ops?: Array<Record<string, unknown>>;};+/** Ix-memory's ErrorHandler body for BulkPlanMismatchException (Ix-memory#273). */+function planMismatchBody(ids: string[]): Record<string, unknown> {+ const sorted = [...ids].sort();+ return {+ error: "conflict",+ message: `bulk patch IDs were previously committed by a different request plan (${sorted.length} patch IDs)`,+ committed_patch_ids: sorted,+ committed_count: sorted.length,+ };+}+/** A backend that answers the endpoints an ingest touches, and records them. */export class FakeBackend {readonly requests: Array<{ path: string; patches: number; code?: number }> = [];@@ -83,6 +94,12 @@ export class FakeBackend {}/** Answer a bulk with 409 naming every patch as already committed. */bulk409AllLanded = false;+ /**+ * Answer a bulk with Ix-memory#273's plan-mismatch 409: every id committed+ * by a different request plan, named in `committed_patch_ids`, and no+ * "partially committed" text.+ */+ bulk409PlanMismatch = false;/** Status for POST /v1/stitch. */stitchStatus = 200;/**@@ -239,7 +256,7 @@ export class FakeBackend {});}if (done.some((id) => this.stored.get(id)!.bulkGroup !== group)) {- return send(409, { error: "bulk patch IDs were previously committed by a different request plan" });+ return send(409, planMismatchBody(ids));}return send(200, { rev: Math.max(...done.map((id) => this.stored.get(id)!.rev)), applied: 0, status: "Idempotent" });}@@ -402,6 +419,9 @@ export class FakeBackend {if (path === "/v1/patch" && this.refuseReplays) {return send(500, { error: "500: already committed" });}+ if (path === "/v1/patches/bulk" && this.bulk409PlanMismatch) {+ return send(409, planMismatchBody(patches.map((p) => p.patchId ?? "")));+ }if (path === "/v1/patches/bulk" && this.bulk409AllLanded) {const ids = patches.map((p) => p.patchId).filter(Boolean);return send(409, { error: "bulk group partially committed", committed_patch_ids: ids });diff --git a/ix-cli/src/cli/__tests__/ingest-files.test.ts b/ix-cli/src/cli/__tests__/ingest-files.test.tsindex 37248988..3574ad79 100644--- a/ix-cli/src/cli/__tests__/ingest-files.test.ts+++ b/ix-cli/src/cli/__tests__/ingest-files.test.ts@@ -448,6 +448,23 @@ describe("ingestFiles against a fake backend", () => {expect(backend.singleCount, "bounded by the failure limit, not one per patch").toBeLessThan(15);});+ it("counts a plan-mismatch 409's patches as landed (Ix-memory#273)", async () => {+ // The backend says every patch of the bulk is already committed, under a+ // different grouping, and names them -- without the "partially committed"+ // text the partial-commit handling used to key on. Every re-send is refused+ // here, so the only way to zero commit errors is to credit the 409's ids.+ fixture(30);+ backend.bulk409PlanMismatch = true;+ backend.refuseReplays = true;++ const summary = await run();++ expect(summary.commitErrors, "confirmed landed, so not errors").toBe(0);+ expect(summary.patchesApplied).toBe(30);+ // Never re-bulked: the same set would land on the same refused group.+ expect(backend.bulkCount).toBe(1);+ });+it("refuses a second stitch to a backend still running the last one (Ix#568)", async () => {// The marker is written when a stitch STARTS and removed only on proof// nothing is running. A 500 is not proof -- ArangoDB keeps executing thediff --git a/ix-cli/src/cli/__tests__/ingest-retry.test.ts b/ix-cli/src/cli/__tests__/ingest-retry.test.tsindex 3fa08759..7e37564b 100644--- a/ix-cli/src/cli/__tests__/ingest-retry.test.ts+++ b/ix-cli/src/cli/__tests__/ingest-retry.test.ts@@ -8,6 +8,7 @@ import {DEFAULT_COMMIT_MAX_OPS,isAbortError,isBulkPartiallyCommittedError,+ isBulkPlanMismatchError,isPayloadTooLargeError,isRetryableCommitConflict,parseBulkCommittedPatchIds,@@ -387,6 +388,74 @@ const partialBody = (ids: string[], expected: number) =>const item = (id: string) => ({ patch: { patchId: id } });+// Ix-memory#273's answer to a bulk whose every id landed under another plan+// (ErrorHandler, BulkPlanMismatchException): the ids, no "partially committed".+const planMismatchBody = (ids: string[]) =>+ new Error(+ `409: ${JSON.stringify({+ error: 'conflict',+ message: `bulk patch IDs were previously committed by a different request plan (${ids.length} patch IDs)`,+ committed_patch_ids: ids,+ committed_count: ids.length,+ })}`+ );++describe('plan-mismatch bulk groups (Ix-memory#273)', () => {+ it('recognises the 409 by its ids, without the partial-commit text', () => {+ const err = planMismatchBody(['a', 'b']);+ expect(isBulkPlanMismatchError(err)).toBe(true);+ expect(isBulkPartiallyCommittedError(err)).toBe(true);+ expect(parseBulkCommittedPatchIds(err)).toEqual(new Set(['a', 'b']));+ });++ it('needs both the 409 and a list of ids it can trust', () => {+ // The pre-#273 body named no ids: still an ordinary conflict.+ expect(isBulkPlanMismatchError(new Error('409: {"error":"conflict","message":"bulk patch IDs were previously committed by a different request plan"}'))).toBe(false);+ expect(isBulkPlanMismatchError(new Error('409: {"committed_patch_ids":["a",7]}'))).toBe(false);+ expect(isBulkPlanMismatchError(new Error('500: {"committed_patch_ids":["a"]}'))).toBe(false);+ expect(isBulkPlanMismatchError(new Error('413: {"error":"payload_too_large"}'))).toBe(false);+ });++ it('counts every named patch as landed and never re-bulks them', async () => {+ const items = ['a', 'b', 'c'].map(item);+ const error = planMismatchBody(['a', 'b', 'c']);+ const commitBulk = vi.fn(async () => { throw error; });+ const commitIndividually = vi.fn(async () => {});++ await commitBulkWithPayloadSplit(items, {+ commitBulk,+ onBulkCommitted: vi.fn(),+ commitIndividually,+ patchIdOf: (i) => i.patch.patchId,+ shouldStop: () => true,+ });++ expect(commitBulk).toHaveBeenCalledOnce();+ // A replay, so a tripped cutoff still sends it and does not count errors.+ expect(commitIndividually).toHaveBeenCalledWith(items, error, { replay: true });+ });++ it('resends only what the body does not name', async () => {+ const items = ['a', 'b', 'c'].map(item);+ const bulkCalls: string[][] = [];+ const commitIndividually = vi.fn(async () => {});++ await commitBulkWithPayloadSplit(items, {+ commitBulk: async (batch) => {+ bulkCalls.push(batch.map((b) => b.patch.patchId));+ if (batch.length === 3) throw planMismatchBody(['a']);+ return 'ok';+ },+ onBulkCommitted: vi.fn(),+ commitIndividually,+ patchIdOf: (i) => i.patch.patchId,+ });++ expect(bulkCalls).toEqual([['a', 'b', 'c'], ['b', 'c']]);+ expect(commitIndividually).toHaveBeenCalledWith([items[0]], undefined, { replay: true });+ });+});+describe('partly-committed bulk groups', () => {it('recognises the 409 and reads the ids that landed', () => {const err = partialBody(['p1', 'p2'], 5);diff --git a/ix-cli/src/cli/commands/ingest.ts b/ix-cli/src/cli/commands/ingest.tsindex 976171a5..e9c41978 100644--- a/ix-cli/src/cli/commands/ingest.ts+++ b/ix-cli/src/cli/commands/ingest.ts@@ -601,7 +601,8 @@ const COMMIT_CONFLICT_RETRY_PATTERNS = [];/**- * A bulk commit the server accepted only in part.+ * A bulk commit the server accepted only in part, or whose patches all landed+ * under a different grouping.** Re-sending the same patches cannot fix this: the group id is derived from the* patch ids, so an identical retry lands on the identical rejected group. The@@ -612,7 +613,19 @@ const COMMIT_CONFLICT_RETRY_PATTERNS = [* JSON body is the tail of the error string.*/export function isBulkPartiallyCommittedError(err: unknown): boolean {- return String(err).toLowerCase().includes('partially committed');+ return String(err).toLowerCase().includes('partially committed') || isBulkPlanMismatchError(err);+}++/**+ * Ix-memory's 409 for a bulk whose every patch id was already committed by a+ * different request plan (BulkPlanMismatchException, Ix-memory#273): the body+ * is `{error: "conflict", message, committed_patch_ids, committed_count}`,+ * without the "partially committed" text, so it is recognised by its status+ * and the ids it names. Those ids landed, exactly as in a partial commit, and+ * are handled the same way: counted as committed, never re-bulked.+ */+export function isBulkPlanMismatchError(err: unknown): boolean {+ return /(^|\s)409: \{/.test(String(err)) && parseBulkCommittedPatchIds(err) !== undefined;}/**