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

Recording the same owner answer from two processes at once no longer
fails about one time in ten; the second add waits for the first to
finish writing.

Why: this was a daily flaky-test repair, and the owner-answers "two
processes adding at once" failure turned out to be a real race, not a
flaky test.

Nothing to do: tests only change, except the 11 lines in
`lib/owner-answers.ts`.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=testing
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-18 15:08:17 +00:00
1 parent bf2c126b55
commit 782f4595ed
6 files changed
+39 -6

No files matched your search

@@ -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.
+9 -2
View File
@@ -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` };
+2 -1
View File
@@ -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 {
+2 -1
View File
@@ -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);
+21
View File
@@ -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;
+4 -2
View File
@@ -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]> = [