Skip to content

fix(#633): eliminate locks-cli takeover test flake via cross-host sentinel - #634

Merged
admarble merged 2 commits into
mainfrom
feature/633-bug-test-locks-cli-integration-test-ts-201-flake-s
May 13, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/633-bug-test-locks-cli-integration-test-ts-201-flake-s

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • Replace hostname: os.hostname() in the --signal-other takeover fixture (and its sibling fixture) with a sentinel non-host string so LockManager.signalOther takes the cross-host short-circuit (src/lib/locks/lock-manager.ts:289) and never invokes process.kill(9999, 'SIGTERM').
  • Adds an in-code comment explaining the prior brittleness and the branch the new fixture exercises (AC-5).
  • No production-code change; no widening of assertions (per feedback_flaky_test_fixes.md).

Root cause

On a busy CI runner, PID 9999 can be a live sibling/ancestor process (Linux pid_max default 32768; PIDs assigned sequentially). The same-host branch of signalOther sent a real SIGTERM to that PID. When the victim was an ancestor of the spawned CLI (vitest main/worker), the stdout pipe collapsed and the CLI died with SIGPIPE, surfacing as result.status === null at locks-cli.integration.test.ts:201. The setup looked safe locally (where PID 9999 is rarely allocated) and only flaked under CI process pressure.

AC Verification

AC Description Status Evidence
AC-1 Fixture no longer relies on hardcoded PID being dead ✅ Implemented signalOther short-circuits on the cross-host check before isPidAlive/process.kill — verifiable in src/lib/locks/lock-manager.ts:287-297
AC-2 --signal-other CLI wiring still exercised ✅ Implemented Test still passes --signal-other; CLI emits Could not signal PID 9999 for #77 (cross-host or already exited) via src/commands/locks.ts:189, matched by the existing regex /Could not signal PID 9999 for #77/
AC-3 No widening of assertions ✅ Implemented expect(result.status).toBe(0) preserved verbatim; stdout regex preserved; after.pid !== 9999 preserved (see diff)
AC-4 Determinism under repeat execution ✅ Implemented 20/20 local runs of the takeover test pass; architectural guarantee that process.kill is no longer invoked on a guessed PID
AC-5 Root cause documented in code ✅ Implemented 6-line comment above the fixture (lines 179-186 in the new file)

Coverage note

The integration test now exercises the cross-host branch of signalOther instead of the same-host-dead-PID branch. The same-host-dead-PID branch retains unit coverage in src/lib/locks/lock-manager.test.ts:294 (uses the isPidAlive constructor seam to stub process.kill cleanly). No coverage gap.

Test plan

  • npm run build — clean
  • npm run lint — clean
  • npx vitest run src/commands/__tests__/locks-cli.integration.test.ts — 13/13 pass
  • npx vitest run src/lib/locks src/commands/__tests__/locks-cli.integration.test.ts — 59/59 pass
  • Repeat-loop locally (for i in {1..20}; do npx vitest run … -t "skill takeover"; done) — 20/20 pass
  • CI green on first attempt (verified post-push)

Optional follow-up (not in scope)

The plan flagged Option C (defense-in-depth self-PID guard in LockManager.signalOther) as a potential follow-up. Not adopted here — the test fix is sufficient and doesn't require touching production code.

Closes #633

…tinel

The takeover test wrote a lock with pid: 9999 and hostname: os.hostname(),
then invoked `sequant locks acquire --force --signal-other`. signalOther's
same-host branch (lock-manager.ts:290) then called process.kill(9999,
'SIGTERM'). On a busy CI runner PID 9999 can be a live sibling/ancestor
process (linux pid_max default 32768; PIDs assigned sequentially), so the
SIGTERM landed on a real process — when that process was an ancestor of
the spawned CLI it tore down the stdout pipe and the CLI died with
SIGPIPE, surfacing as result.status === null.

Fix: set hostname to a sentinel non-host string so signalOther's cross-host
short-circuit (lock-manager.ts:289) returns false without ever calling
process.kill. The CLI emits the same 'Could not signal PID 9999 for #77
(cross-host or already exited)' line, matching the existing regex
assertion. Same fix applied to the sibling --force-without-signal-other
fixture for consistency. Same-host-dead-PID branch retains unit coverage
in lock-manager.test.ts:294 via the isPidAlive constructor seam.

Verified: 20/20 local repeats of the takeover test pass; full locks
surface (59 tests across 3 files) green.
The test title still claimed "for dead PID" after the cross-host fix even
though the test no longer exercises the dead-PID branch — signalOther
short-circuits on the cross-host check before isPidAlive runs. Rename to
match what the test actually verifies.

Tighten the stdout regex to anchor on the full
"(cross-host or already exited)" suffix so a future refactor that
accidentally takes the "Signaled ..." (sent) branch would fail loudly
instead of slipping through a loose substring match.

Expand the explanatory comment to document the CLI-message-level
limitation: the same suffix is printed for both signalOther false
branches (cross-host vs same-host-dead-PID), so the integration
assertion cannot distinguish which internal branch ran. The
same-host-dead-PID branch retains unit coverage at
lock-manager.test.ts:294 via the isPidAlive constructor seam.
@admarble
admarble merged commit 03e83c4 into main May 13, 2026
5 checks passed
@admarble
admarble deleted the feature/633-bug-test-locks-cli-integration-test-ts-201-flake-s branch May 13, 2026 23:11
admarble added a commit that referenced this pull request May 14, 2026
* fix(#637): guard signalOther against self/parent PID

Adds a self-PID / parent-PID short-circuit in `LockManager.signalOther`
before the `isPidAlive` probe. Defense-in-depth follow-up to the #633
flake fixed in #634 — production users can still hit the underlying
weakness if a recycled PID lands on the manager's own PID or its
parent shell.

- src/lib/locks/lock-manager.ts: insert guard after hostname check
- src/lib/locks/lock-manager.test.ts: two new tests assert the guard
  short-circuits BEFORE invoking the isPidAlive spy

Fixes #637

* refactor(#637): signalOther returns discriminated reason; accurate caller logs

Promotes `LockManager.signalOther` return from `boolean` to
`{ sent: boolean; reason: SignalReason }` so callers can produce a
correct log line in each refusal branch — including the new
self-or-parent guard added earlier in this PR, which previously
collapsed into the misleading "(cross-host or already exited)" suffix.

- src/lib/locks/types.ts: add SignalReason / SignalOtherResult
- src/lib/locks/index.ts: re-export new types
- src/lib/locks/lock-manager.ts: rewrite signalOther to return reason
- src/lib/locks/lock-manager.test.ts: assert reason discriminator in
  existing + #637 tests; AC-1 test now uses process.pid for the
  manager (mirrors production; pid=1 was unrealistic)
- src/commands/locks.ts + run-orchestrator.ts: per-branch log mapper
- src/commands/__tests__/locks-cli.integration.test.ts: anchor on
  the new "(cross-host holder)" suffix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(test): locks-cli.integration.test.ts:201 flake — signal-other takeover returns null status

1 participant