Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(import): verify a code import's read-back inside its transaction (#6011)

garrytand769c1f2ad45
diff --git a/src/core/import-file.ts b/src/core/import-file.ts
index 2cb6b82c14..188f502a09 100644
--- a/src/core/import-file.ts
+++ b/src/core/import-file.ts
@@ -1509,6 +1509,9 @@ export async function importCodeFile(
[MARKDOWN_CHUNKER_VERSION, txOpts.sourceId, slug]);
await sealPageTextProjection(tx, slug, txOpts.sourceId);
if (opts.prepare) await installCodeChunkEdges(tx, slug, txOpts.sourceId, code);
+ // Read-back inside the transaction, as the markdown path does (#6011): a page is not written until it reads back,
+ // and a concurrent writer that commits after this transaction can no longer fail an import that committed.
+ else await verifyPageReadable(tx, slug, hash, sourceId, 'importCodeFile');
};
if (opts.prepare) {
const result: ImportResult = { slug, status: 'imported', chunks: chunks.length };
@@ -1516,11 +1519,6 @@ export async function importCodeFile(
}
await maintenanceTransaction(engine, apply);
- // Post-write read-back verification.
- // Same guard as the markdown path: a code page write is not "done" until
- // it is readable back via getPage.
- await verifyPageReadable(engine, slug, hash, sourceId, 'importCodeFile');
-
// v0.20.0 Cathedral II Layer 5 (A1): extracted call-site edges persist
// in code_edges_symbol (unresolved — we don't attempt within-file target
// resolution here; getCallersOf / getCalleesOf match on to_symbol_qualified
diff --git a/test/import-code-readback-race.test.ts b/test/import-code-readback-race.test.ts
new file mode 100644
index 0000000000..753160e9b5
--- /dev/null
+++ b/test/import-code-readback-race.test.ts
@@ -0,0 +1,34 @@
+/**
+ * #6011: importCodeFile verified its write by reading the page back after its transaction committed, so a
+ * concurrent writer committing in between failed an import that had committed ("post-write read-back
+ * failed ... silent desync"). The read-back now runs inside the import's transaction, as the markdown path's
+ * does. Classic (unmanaged) PGLite brain; the competing writer commits right after the import's transaction.
+ */
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { importCodeFile } from '../src/core/import-file.ts';
+
+let engine: PGLiteEngine;
+beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 60_000);
+afterAll(async () => { await engine.disconnect(); });
+
+test('a write that commits right after the import transaction does not fail the committed code import', async () => {
+ const slug = 'src-a-ts';
+ let raced = false;
+ const racing = new Proxy(engine, { get(target, key) {
+ if (key === 'transaction') return async <T>(fn: (tx: BrainEngine) => Promise<T>) => {
+ const result = await target.transaction(fn);
+ if (!raced && await target.getPage(slug, { sourceId: 'default' })) {
+ raced = true;
+ await target.executeRaw("UPDATE pages SET content_hash='competing-writer', compiled_truth='// changed by another writer' WHERE slug=$1 AND source_id='default'", [slug]);
+ }
+ return result;
+ };
+ const value = Reflect.get(target, key);
+ return typeof value === 'function' ? value.bind(target) : value;
+ } }) as BrainEngine;
+ const result = await importCodeFile(racing, 'src/a.ts', 'export const a = 1;\n', { noEmbed: true });
+ expect(raced).toBe(true);
+ expect(result).toMatchObject({ slug, status: 'imported' });
+});