Skip to content

fix(#1222): a watchdog-killed run writes no run log and half-deletes a clean worktree - #1238

Merged
admarble merged 7 commits into
mainfrom
feature/1222-fix-run-a-watchdog-killed-run-writes-no-run-log-an
Sep 30, 2026
Merged

admarble merged 7 commits into
mainfrom
feature/1222-fix-run-a-watchdog-killed-run-writes-no-run-log-an

Conversation

@admarble

@admarble admarble commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • ShutdownManager.registerCleanup now accepts { phase: "finalize" | "resource" } (default "resource"). gracefulShutdown runs every "finalize" task before any "resource" task, LIFO within each phase — so "Finalize run logs" (registered { phase: "finalize" }) always runs before worktree-removal cleanups, regardless of registration order.
  • shouldPreserveWorktree/cleanupWorktreeOnShutdown/registerWorktreeRemovalCleanup in worktree-manager.ts now also preserve a clean worktree when its issue has a phase in progress (a lazily-evaluated phaseInProgress callback, plumbed from RunOrchestrator.isPhaseInProgress) or when it has commits ahead of its base branch (hasCommitsAheadOfBase, base resolved via resolveBaseRef from phase-executor.ts) — not just when git status is dirty.
  • src/mcp/tools/run.ts's SIGKILL_GRACE_MS is now derived from ShutdownManager's new exported DEFAULT_FORCE_EXIT_TIMEOUT_MS (10000) plus a 1000ms buffer, instead of a standalone 5000 literal that was shorter than the manager's own force-exit timeout.
  • Added docs/adr/0003-cleanup-phase-ordering.md documenting the rejected aggregate-registration alternative from the spec's Design Review.
  • CHANGELOG ### Fixed entry under [Unreleased].

AC Verification

AC Description Status Evidence
AC-1 Finalize run logs precedes worktree-removal on signal shutdown ✅ Implemented src/lib/shutdown.ts (CleanupPhase, phase-sorted gracefulShutdown); src/lib/shutdown.test.ts
AC-2 A worktree with an in-progress phase or commits ahead of base is preserved ✅ Implemented src/lib/workflow/worktree-manager.ts (shouldPreserveWorktree, hasCommitsAheadOfBase); src/lib/workflow/worktree-manager.test.ts, src/lib/workflow/run-orchestrator.shutdown.test.ts
AC-3 SIGKILL grace ≥ shutdown manager's force-exit timeout ✅ Implemented src/mcp/tools/run.ts (SIGKILL_GRACE_MS); src/mcp/tools/run.test.ts

Mutation verification

Checks

  • npm run build: pass
  • npm run lint: pass (0 errors, 0 warnings)
  • Targeted suites (per owner's standard block, no full npm test): npx vitest run src/lib/shutdown.test.ts src/lib/workflow/run-orchestrator.test.ts src/lib/workflow/worktree-manager.test.ts src/mcp/tools/run.test.ts src/lib/workflow/run-orchestrator.shutdown.test.ts — 161 passed

Runner verification (tracker #1236)

QA verdict: NEEDS_VERIFICATION, 3/3. Its gaps were real, and the runner fixed them:

  • The orchestrator wiring was untested. QA removed { phase: "finalize" } from the run-log registration and every suite stayed green (40/40), so AC-1's actual fix had no guard. In fdc7c5fc, the runner extracted registerRunLogFinalizer(shutdown, writer) and phaseInProgressPredicate(ref, issueNum) from run() and tested them in run-orchestrator.shutdown.test.ts:
    • Mutation: dropping { phase: "finalize" } made src/lib/workflow/run-orchestrator.shutdown.test.ts > run-orchestrator shutdown wiring (#1222) > AC-1: the run-log finalizer runs, as aborted, before a worktree cleanup registered after it fail (1 failed / 4 passed); restored.
    • Mutation: phaseInProgressPredicate returning () => false made … > AC-2: the phase predicate reads the orchestrator lazily, false until it exists fail (1 failed / 4 passed); restored.
  • The preserve message misstated the reason. It always said "uncommitted changes". worktreePreserveReason now returns the rule that applied, so the log says preserved (uncommitted changes | phase in progress | commits ahead of base). A real-git test covers the feat(logs): run log records each phase's model and the sequant version #1198 shape (a clean tree with a committed WIP) and the phase-in-progress case.
  • Checked: currentPhase is cleared when a phase completes (run-orchestrator.ts, the phase-complete handler), so the predicate goes back to false between phases. The new worktree-manager → phase-executor import adds no import cycle (madge: 18 cycles on main and on the branch, the same set).
  • shutdown, run-orchestrator, run-orchestrator.shutdown, worktree-manager and mcp/tools/run tests: 164 passed. tsc and prettier are clean.
  • CI caught a runner regression, fixed in 4af86ba8: abort-logging.integration.test.ts (the Nested phase claude that hangs after its turn is killed at ~106s, taking down the parent sequant run #856 guard) scans run-orchestrator.ts for the finalizer block, and extracting the helper moved that block. The scan now targets registerRunLogFinalizer's block and checks that run() still calls it. Mutation: a bare writer.finalize() fails both that guard and the new wiring test (2 failed / 6 passed); restored.
  • Release ride-along: a CHANGELOG ### Fixed line for bug(metrics): resolvedModel records the Haiku helper, not the phase's model, since 2026-09-20 — first modelUsage key is no longer the main model #1227 (the resolvedModel fix, which shipped without one).

Test plan

  • shutdown.test.ts: new test asserts a finalize-phase task registered early still runs before resource-phase tasks registered later; existing LIFO/failure/abort-context tests unchanged.
  • worktree-manager.test.ts: new #1222 AC-2 describe block covers hasCommitsAheadOfBase, shouldPreserveWorktree (commits-ahead, phase-in-progress, and the still-removed base case), cleanupWorktreeOnShutdown, and registerWorktreeRemovalCleanup wiring, using real git worktrees against a bare remote.
  • run-orchestrator.shutdown.test.ts: existing fix(run): abort-path paper cuts — "work is preserved in <worktree>" is false under SIGTERM, and abort's doc comment cites the wrong issue #935 regression tests still pass against the extended shouldPreserveWorktree (fixture now adds a self-referential origin remote so origin/main resolves).
  • run.test.ts: new test pins SIGKILL_GRACE_MS >= DEFAULT_FORCE_EXIT_TIMEOUT_MS.

Closes #1222

Refs #1236, #1203

🤖 Generated with Claude Code

admarble and others added 5 commits September 30, 2026 11:14
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Entire-Checkpoint: 1a9a28ef19c6
Add a phase field to ShutdownManager cleanup tasks so log finalization always precedes worktree removal on SIGINT/SIGTERM, preserve worktrees with an in-progress phase or commits ahead of base, and raise the MCP run tool's SIGKILL grace above ShutdownManager's own force-exit timeout.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Entire-Checkpoint: ccbcae1d8a34
…rve log names its reason

QA test_gap: removing { phase: 'finalize' } from the orchestrator's run-log cleanup left every suite green. Extracts registerRunLogFinalizer and phaseInProgressPredicate so the wiring has tests; worktreePreserveReason makes the preserved line say why (was always 'uncommitted changes').

Entire-Checkpoint: 83db4e72cd7f
…along, tracker #1236)

Entire-Checkpoint: f832e377ad60
…egisterRunLogFinalizer

CI red on eeebba4: the source scan looked for the registration inside run(), which fdc7c5f moved into the exported helper. Scoped to the helper's block, plus a check that run() still calls it.

Entire-Checkpoint: 5e0a502f27a3
@admarble
admarble merged commit 5cd5b8b into main Sep 30, 2026
7 checks passed
@admarble
admarble deleted the feature/1222-fix-run-a-watchdog-killed-run-writes-no-run-log-an branch September 30, 2026 17:15
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.

fix(run): a watchdog-killed run writes no run log and half-deletes a clean worktree — worktree removal outruns the 5 s SIGKILL grace

1 participant