Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript

Two owner-answer adds at once no longer collide, and three flaky tests get real fixes (GST-3084) (#4490)

Garry Tan782f4595eddd
diff --git a/changelog.d/owner-answer-adds-no-longer-collide.md b/changelog.d/owner-answer-adds-no-longer-collide.md
new file mode 100644
index 000000000..0ee128e22
--- /dev/null
+++ b/changelog.d/owner-answer-adds-no-longer-collide.md
@@ -0,0 +1 @@
+- **Two `gstack-owner-answers add` runs for the same answer no longer collide.** When two processes recorded the same owner answer at once, about one pair in ten failed: one could read the other's row while it was still empty and report a conflict, or see the record's folder appear between two checks and refuse it as "a link that cannot be resolved". The second add now waits up to 2 seconds for the first to finish writing, and the folder check looks for the folder before resolving it. Three tests that failed intermittently under load also got fixes. The sealed-guard streaming test now treats Bun's buffer-overflow error as a failure even when git exits 0, and the two `gstack-ship-receipt` test files use the same 30-second limit as CI.
diff --git a/lib/owner-answers.ts b/lib/owner-answers.ts
index 4efd11f4b..ba4acc60e 100644
--- a/lib/owner-answers.ts
+++ b/lib/owner-answers.ts
@@ -177,9 +177,9 @@ export function storeEscape(store: Store): AnswerError | undefined {
let realRoot: string;
try { realRoot = fs.realpathSync(root); } catch { return undefined; }
for (const d of [store.dir, ...['rows', 'verifications', 'log'].map(s => path.join(store.dir, s))]) {
+ try { fs.lstatSync(d); } catch { continue; }
let real: string;
try { real = fs.realpathSync(d); } catch {
- try { fs.lstatSync(d); } catch { continue; }
return { code: 'OWNER_ANSWERS_INVALID', message: `${path.relative(root, d) || '.'} is a link that cannot be resolved` };
}
if (real !== realRoot && !contained(realRoot, real)) return { code: 'OWNER_ANSWERS_INVALID', message: `${path.relative(root, d) || '.'} resolves outside the record's folder` };
@@ -268,6 +268,9 @@ export function writeExclusive(store: Store, file: string, value: unknown): 'wri
}
}
+/** How long an add that finds its row already created waits for a concurrent add to finish writing it. */
+const TWIN_WAIT_MS = 2_000;
+
export function addAnswer(store: Store, row: OwnerAnswer): 'written' | 'present' | AnswerError {
if (row.supersedes) {
const read = readStore(store);
@@ -279,7 +282,11 @@ export function addAnswer(store: Store, row: OwnerAnswer): 'written' | 'present'
const res = writeExclusive(store, file, row);
if (res !== 'present') return res;
let have: Partial<OwnerAnswer> = {};
- try { have = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { /* an unreadable twin is a conflict too */ }
+ for (const until = Date.now() + TWIN_WAIT_MS; ;) {
+ try { have = JSON.parse(fs.readFileSync(file, 'utf8')); break; } catch { /* an unreadable twin is a conflict too */ }
+ if (Date.now() >= until) break;
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
+ }
return have.words === row.words && have.words_by === row.words_by && have.limits === row.limits
? 'present'
: { code: 'OWNER_ANSWERS_CONFLICT', message: `${row.id} already records different words, author or limits for this source and action; supersede it instead` };
diff --git a/test/gstack-ship-receipt-docs.test.ts b/test/gstack-ship-receipt-docs.test.ts
index d7b310d46..ead7948c0 100644
--- a/test/gstack-ship-receipt-docs.test.ts
+++ b/test/gstack-ship-receipt-docs.test.ts
@@ -5,7 +5,7 @@
* floor, named patch-id refusal and branch-touched walk; the digit-bounded
* stamp replacement.
*/
-import { afterEach, describe, expect, test } from 'bun:test';
+import { afterEach, describe, expect, setDefaultTimeout, test } from 'bun:test';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -19,6 +19,7 @@ import { gitArgvIn } from './helpers/scratch-repo';
const repos: FixtureRepo[] = [];
const fixture = (opts: Parameters<typeof makeFixtureRepo>[0] = {}) => { const r = makeFixtureRepo({ prefix: 'ship-receipt-2-', ...opts }); repos.push(r); return r; };
afterEach(() => { while (repos.length) cleanup(repos.pop()!); });
+setDefaultTimeout(30_000);
/** A repository with no VERSION, no package.json, no CHANGELOG and no policy: the shape E18 names. */
function bareRepo(): FixtureRepo {
diff --git a/test/gstack-ship-receipt.test.ts b/test/gstack-ship-receipt.test.ts
index 509897eba..98e2b1511 100644
--- a/test/gstack-ship-receipt.test.ts
+++ b/test/gstack-ship-receipt.test.ts
@@ -4,7 +4,7 @@
* a PR (RECEIPT_MISSING / RECEIPT_INVALID); `census` computes the P7 adoption
* number over merged PRs, deduplicated by run and thread, against a cohort.
*/
-import { afterEach, describe, expect, test } from 'bun:test';
+import { afterEach, describe, expect, setDefaultTimeout, test } from 'bun:test';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -15,6 +15,7 @@ import { cleanup, ghShim, makeFixtureRepo, makePr, recordAndBundle, restamp, run
const repos: FixtureRepo[] = [];
const fixture = () => { const r = makeFixtureRepo({ prefix: 'ship-receipt-' }); repos.push(r); return r; };
afterEach(() => { while (repos.length) cleanup(repos.pop()!); });
+setDefaultTimeout(30_000);
const HEAD = 'a'.repeat(40);
const block = (receipt: Record<string, unknown>) => renderReceiptBlock(receipt as any);
diff --git a/test/owner-answers.test.ts b/test/owner-answers.test.ts
index 398b2d269..0cd613a6f 100644
--- a/test/owner-answers.test.ts
+++ b/test/owner-answers.test.ts
@@ -237,6 +237,27 @@ describe('fail closed', () => {
expect((addAnswer(s, twin) as any).code).toBe('OWNER_ANSWERS_CONFLICT');
expect(readStore(s).rows.map(r => r.words)).toEqual(['yes merge when green']);
});
+ test('an add that finds its row created but not yet written waits for the other add, then reports it present', async () => {
+ const s = store();
+ const row = buildAnswer(s, MERGE) as OwnerAnswer;
+ const file = path.join(s.dir, 'rows', `${row.id}.json`);
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(file, '');
+ const writer = spawn('bun', [...bunIsolationArgs(), '-e', 'console.log("ready"); setTimeout(() => require("fs").writeFileSync(process.env.F, process.env.J), 200)'], { env: { ...process.env, F: file, J: JSON.stringify(row) }, timeout: 60_000 });
+ const closed = new Promise(resolve => writer.on('close', resolve));
+ await new Promise(resolve => writer.stdout.once('data', resolve));
+ expect(addAnswer(s, row)).toBe('present');
+ await closed;
+ expect(readStore(s).rows.map(r => r.id)).toEqual([row.id]);
+ }, 120_000);
+ test('a row file that stays empty is still a conflict once the wait runs out', () => {
+ const s = store();
+ const row = buildAnswer(s, MERGE) as OwnerAnswer;
+ const file = path.join(s.dir, 'rows', `${row.id}.json`);
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(file, '');
+ expect((addAnswer(s, row) as any).code).toBe('OWNER_ANSWERS_CONFLICT');
+ });
test('supersede through the API refuses a missing row', () => {
const s = store();
const row = buildAnswer(s, { ...MERGE, supersedes: 'oa-ffffffffffffffff' }) as OwnerAnswer;
diff --git a/test/sealed-stream.test.ts b/test/sealed-stream.test.ts
index e78d2ddad..f98d0ac22 100644
--- a/test/sealed-stream.test.ts
+++ b/test/sealed-stream.test.ts
@@ -56,11 +56,13 @@ function repo() {
return { dir, git, run, write };
}
-// ---- The pre-fix buffered path (lib/sealed-guard.ts at 0ead11c), verbatim but for the injectable maxBuffer. ----
+// ---- The pre-fix buffered path (lib/sealed-guard.ts at 0ead11c), verbatim but for the injectable maxBuffer and one
+// change in oldGit: a spawn error counts as a failure. When git exits before the overflow kill lands, Bun reports
+// status 0 with error ENOBUFS and a truncated stdout, so a status-only check let a loaded runner read part of the diff. ----
const HEX = /^[0-9a-f]{40,64}$/;
function oldGit(repoRoot: string, args: string[], maxBuffer: number, input?: string, encoding: BufferEncoding = 'utf8') {
const r = spawnSync('git', args, { cwd: repoRoot, encoding, input, timeout: 120_000, maxBuffer });
- return { ok: r.status === 0, out: r.stdout ?? '', err: (r.stderr ?? '').trim() };
+ return { ok: r.status === 0 && !r.error, out: r.stdout ?? '', err: (r.stderr ?? '').trim() };
}
function oldTextFindings(repoRoot: string, commits: string[], p: SealedPolicy, maxBuffer: number): SealedFinding[] {
const rules: Array<[SealedFinding['rule'], RegExp]> = [