Skip to content

Commit cdc78ec

Browse files
thymikeeclaude
andcommitted
fix(gate): scope lock decisions to the gate environment
Pass the environment to acquireValidationLock so nested-owner detection and the gate runner read the same variables, skip the lock on unsupported hosts, cover signal cancellation, and tighten the lock documentation. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent f2ee0ac commit cdc78ec

4 files changed

Lines changed: 61 additions & 15 deletions

File tree

‎docs/agents/testing.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
Serialized validation requires macOS or Linux.
1111
On Windows, use plan mode or focused checks.
1212
Both broad commands acquire the same host lock.
13-
So do `pnpm gate` ids `unit`, `unit-ci`, `provider-integration`, `fuzz-parsers`, `mutation`, `mutation-affected`, and `concurrency-torture`.
14-
A heavy gate in a locked validation inherits the lock. GitHub runners skip it.
13+
These `pnpm gate` ids acquire the same lock: `unit`, `unit-ci`, `provider-integration`, `fuzz-parsers`, `mutation`, `mutation-affected`, and `concurrency-torture`.
14+
A gate inside a locked validation reuses that lock. A GitHub runner takes no lock.
1515
Wait for the owner to finish, or cancel your waiter with Ctrl-C.
16-
Do not delete lock files.
17-
A wait ends after two hours. Nested validation fails immediately.
16+
A wait ends after two hours. A nested `pnpm check` or `pnpm check:affected --run` fails immediately.
1817
Focused tests do not lock. Avoid subprocess-heavy tests beside a broad gate.
1918

2019
The runner prints progress and a receipt path. The receipt contains:
@@ -46,7 +45,6 @@ A full selection for these changes is intentional.
4645
Read `failOpenReasons` in plan JSON or `plan.failOpenReasons` in a receipt.
4746
Do not reduce selection to get a pass.
4847

49-
Read required-check status on the PR.
5048
The receipt lists skipped checks and owners in `results[].reason`.
5149
GitHub owns:
5250

‎scripts/check-affected/host-lock.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ export async function acquireValidationLock(
2929
signal: AbortSignal,
3030
lockDirPath = HOST_LOCK_PATH,
3131
report: (message: string) => void = (message) => process.stderr.write(`${message}\n`),
32+
env: NodeJS.ProcessEnv = process.env,
3233
): Promise<ValidationLock> {
3334
if (process.platform !== 'darwin' && process.platform !== 'linux') {
3435
throw new Error(
3536
'Serialized validation requires macOS or Linux. Use plan mode or focused checks on this host.',
3637
);
3738
}
38-
if (process.env[VALIDATION_PARENT_ENV]) {
39+
if (env[VALIDATION_PARENT_ENV]) {
3940
throw new Error(
40-
`Nested validation is not allowed. Parent receipt: ${process.env[VALIDATION_PARENT_ENV]}`,
41+
`Nested validation is not allowed. Parent receipt: ${env[VALIDATION_PARENT_ENV]}`,
4142
);
4243
}
4344
const owner = { ...readCurrentOwnerIdentity(), acquiredAtMs: Date.now() };

‎scripts/gate/host-locked.test.ts‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ function fixture() {
3636

3737
test('the locked gates are the whole-repo suites and worker-spawning sweeps', () => {
3838
assert.deepEqual(
39-
CHECK_CATALOG.filter((spec) => spec.hostLocked).map((spec) => spec.id),
39+
CHECK_CATALOG.filter((spec) => spec.hostLocked)
40+
.map((spec) => spec.id)
41+
.sort(),
4042
[
41-
'unit',
42-
'provider-integration',
43-
'unit-ci',
43+
'concurrency-torture',
4444
'fuzz-parsers',
4545
'mutation',
4646
'mutation-affected',
47-
'concurrency-torture',
47+
'provider-integration',
48+
'unit',
49+
'unit-ci',
4850
],
4951
);
5052
});
@@ -59,6 +61,7 @@ test(
5961
new AbortController().signal,
6062
lockDirPath,
6163
() => {},
64+
cleanEnv(),
6265
);
6366
let waitReport = '';
6467
const waiting = executeGate({
@@ -127,6 +130,7 @@ test(
127130
new AbortController().signal,
128131
lockDirPath,
129132
() => {},
133+
cleanEnv(),
130134
);
131135
const env = cleanEnv({ [VALIDATION_PARENT_ENV]: '/parent/receipt.json' });
132136
assert.equal(requiresHostLock('unit-ci', env), false);
@@ -151,7 +155,12 @@ test(
151155
test('cheap gates and GitHub runners do not wait for the lock', { skip }, async () => {
152156
const { lockDirPath, marker, writeMarker, cleanup } = fixture();
153157
try {
154-
const owner = await acquireValidationLock(new AbortController().signal, lockDirPath, () => {});
158+
const owner = await acquireValidationLock(
159+
new AbortController().signal,
160+
lockDirPath,
161+
() => {},
162+
cleanEnv(),
163+
);
155164
for (const [id, env] of [
156165
['lint', cleanEnv()],
157166
['unit-ci', cleanEnv({ GITHUB_ACTIONS: 'true' })],
@@ -175,3 +184,33 @@ test('cheap gates and GitHub runners do not wait for the lock', { skip }, async
175184
cleanup();
176185
}
177186
});
187+
188+
test(
189+
'a signal while waiting cancels the gate and leaves the owner in place',
190+
{ skip },
191+
async () => {
192+
const { lockDirPath, marker, writeMarker, cleanup } = fixture();
193+
try {
194+
const owner = await acquireValidationLock(
195+
new AbortController().signal,
196+
lockDirPath,
197+
() => {},
198+
cleanEnv(),
199+
);
200+
const waiting = executeGate({
201+
id: 'unit-ci',
202+
command: writeMarker,
203+
cwd: process.cwd(),
204+
env: cleanEnv(),
205+
lockDirPath,
206+
report: () => void process.emit('SIGTERM'),
207+
});
208+
assert.equal(await waiting, 143);
209+
assert.equal(fs.existsSync(marker), false);
210+
assert.ok(fs.existsSync(lockDirPath), 'cancelled waiter must not release the owner');
211+
await owner.release();
212+
} finally {
213+
cleanup();
214+
}
215+
},
216+
);

‎scripts/gate/host-locked.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export type GateExecution = {
2323

2424
export function requiresHostLock(id: string, env: NodeJS.ProcessEnv): boolean {
2525
return (
26-
getCheckSpec(id).hostLocked && env.GITHUB_ACTIONS !== 'true' && !env[VALIDATION_PARENT_ENV]
26+
getCheckSpec(id).hostLocked &&
27+
(process.platform === 'darwin' || process.platform === 'linux') &&
28+
env.GITHUB_ACTIONS !== 'true' &&
29+
!env[VALIDATION_PARENT_ENV]
2730
);
2831
}
2932

@@ -54,7 +57,12 @@ export async function executeGate(execution: GateExecution): Promise<number> {
5457
let lock: Awaited<ReturnType<typeof acquireValidationLock>> | undefined;
5558
let commandPid: number | undefined;
5659
try {
57-
lock = await acquireValidationLock(controller.signal, execution.lockDirPath, execution.report);
60+
lock = await acquireValidationLock(
61+
controller.signal,
62+
execution.lockDirPath,
63+
execution.report,
64+
env,
65+
);
5866
lock.recordCommand(receipt, null);
5967
const result = await runCmdStreaming(
6068
process.execPath,

0 commit comments

Comments
 (0)