Skip to content

feat(#74): implement graceful shutdown for sequant run - #77

Merged
admarble merged 1 commit into
mainfrom
feature/74-feat-implement-graceful-shutdown-for-sequant-ru
Jan 12, 2026
Merged

admarble merged 1 commit into
mainfrom
feature/74-feat-implement-graceful-shutdown-for-sequant-ru

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • Add ShutdownManager class to handle SIGINT/SIGTERM signals gracefully
  • Register cleanup tasks for LogWriter finalization and worktree removal (newly created only)
  • Abort active phases via AbortController integration when shutdown is triggered
  • Support double Ctrl+C for force exit and configurable timeout (default 10s)

Implementation Details

New files:

  • src/lib/shutdown.ts - Core ShutdownManager class
  • src/lib/shutdown.test.ts - 18 unit tests covering all functionality

Modified:

  • src/commands/run.ts - Wire ShutdownManager into run workflow

Test Plan

  • Unit tests for ShutdownManager (18 tests, all passing)
  • Full test suite passes (332 tests)
  • Build succeeds
  • Manual testing: Ctrl+C during sequant run shows cleanup messages and exits cleanly

AC Coverage

  • AC-1: ✅ ShutdownManager initializes signal handlers on construction
  • AC-2: ✅ SIGINT triggers graceful shutdown
  • AC-3: ✅ LogWriter.finalize() registered as cleanup task
  • AC-4: ✅ Worktree cleanup registered for newly created worktrees
  • AC-5: ✅ Pre-existing worktrees preserved during cleanup
  • AC-6: ✅ User feedback during shutdown ("Received SIGINT...", cleanup status)
  • AC-7: ✅ Double Ctrl+C forces immediate exit (code 1)
  • AC-8: ✅ 10-second timeout (configurable) for cleanup operations
  • AC-9: ✅ SIGTERM handled identically to SIGINT
  • AC-10: ✅ 18 unit tests for ShutdownManager

Closes #74

🤖 Generated with Claude Code

- Add ShutdownManager class with SIGINT/SIGTERM handling
- Support graceful cleanup via registered tasks (LIFO order)
- Wire LogWriter finalization as cleanup task
- Wire worktree cleanup for newly created worktrees
- Abort active phase on shutdown via AbortController
- Double Ctrl+C forces immediate exit
- Configurable timeout (default 10s) for cleanup operations
- 18 unit tests for ShutdownManager

Closes #74

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@admarble
admarble merged commit 0706f45 into main Jan 12, 2026
@admarble
admarble deleted the feature/74-feat-implement-graceful-shutdown-for-sequant-ru branch January 12, 2026 01:20
admarble added a commit that referenced this pull request May 13, 2026
…tinel (#634)

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

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.

* fix(#633): rename takeover test + tighten cross-host assertion

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.
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.

feat: Implement graceful shutdown for sequant run

1 participant