needsRefine: accept .done as idle state for refine dispatch#511
Merged
Conversation
The stateless needsRefine gate from #509 only accepted .idle, but a Claude Code agent's lifecycle is .idle → .working → … → .done. Once the first top-level Stop hook fires, the state stays at .done until the next prompt arrives — exactly when refine should dispatch. As a result, refine never fired after an agent's first task. Two PRs were observed stuck on this gate: corveil#1427 and shell-crm#202, both with hookState.activityState = .done and no commit since the reviewer's CHANGES_REQUESTED. Fix: accept both .idle (fresh agent) and .done (finished, waiting) in isManagedTerminalIdle. .working and .waiting still gate. Tests: doneAgentDispatches (regression for the bug), waitingAgentSuppressesDispatch (negative coverage for the remaining gated state). Updated busyAgentSuppressesDispatch and the makeTracker helper to take an AgentActivityState directly rather than a Bool. 🐦⬛ Generated with Claude Code, orchestrated by Crow Co-Authored-By: Claude <noreply@anthropic.com> Crow-Session: 5ABBAED5-0083-4F1C-BFB0-2EAF3019771D
dhilgaertner
approved these changes
Jun 15, 2026
dhilgaertner
left a comment
Contributor
There was a problem hiding this comment.
Code & Security Review
Critical Issues
None.
Security Review
Strengths:
- No security surface touched. The change is a pure in-memory predicate over local
AppStateenum values — no I/O, no auth, no user input, no external data. The new test helper is test-only.
Concerns:
- None.
Code Quality
- Correctness (Green): The new predicate
state == .idle || state == .donematches the real agent lifecycle. Verified againstClaudeHookSignalSource: the top-levelStopevent sets.done(ClaudeHookSignalSource.swift:92), leaving the agent at the prompt and able to accept the next prompt — precisely when refine should dispatch. The previously-only-accepted.idlestate never recurs after the agent's first task, which is the exact bug described (CROW-510). - Gating still sound (Green):
.working(active) and.waiting(blocked on a permission prompt / question /StopFailure, see:65,82,97) continue to gate, so refine won't interrupt a busy or blocked agent. The.agentLaunchedreadiness pre-check is preserved. - No false positives from
.done(Green):.doneis also reached viaSessionStartsource=resume— likewise a legitimately-available-at-prompt state, so accepting it does not introduce spurious dispatches. - Tests (Green): Adds
doneAgentDispatches(regression for the bug) andwaitingAgentSuppressesDispatch(negative coverage for the remaining gated state), and migratesbusyAgentSuppressesDispatchto the new helper. ThemakeTrackersignature change fromagentIdle: BooltoactivityState: AgentActivityStateis a strict improvement — all four states are now expressible. - Docs (Green): The doc comment on
isManagedTerminalIdlewas updated to match the new behavior.
Note: I could not execute the test suite in this review checkout — swift test fails at dependency resolution because the local GhosttyKit.xcframework binary artifact is not present in the worktree (an environment limitation, unrelated to this change). The logic was verified by reading the state machine directly.
Summary Table
| Color | Meaning | Verdict effect |
|---|---|---|
| Red | Must fix | Request changes |
| Yellow | Should fix | Request changes |
| Green | Consider | Approve allowed |
Recommendation: Approve — driven by [0 Red, 0 Yellow, 5 Green] findings. A minimal, correct, well-tested one-line fix with matching test and doc updates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #510. Patches #509.
Summary
The stateless
needsRefinegate landed in #509 only acceptedAgentActivityState.idle, but a Claude Code agent's lifecycle is.idle → .working → … → .done. Once the first top-level Stop hook fires, the state stays at.doneuntil the next prompt arrives — exactly when refine should dispatch. As a result, refine never fired after an agent's first task.Two PRs were observed stuck on this gate today, both with
hookState.activityState = .doneand no commit since the reviewer'sCHANGES_REQUESTED:Change
One-line predicate change in
isManagedTerminalIdle:.workingand.waitingstill gate. Doc comment updated to match. No other code moves.Tests
In
Tests/CrowTests/IssueTrackerNeedsRefineTests.swift:doneAgentDispatches— new, regression for the bug..done+ needs-refine PR → one dispatch.waitingAgentSuppressesDispatch— new, negative coverage for the remaining gated state.busyAgentSuppressesDispatch— existing test, updated to passactivityState: .workinginstead ofagentIdle: false.makeTrackerhelper signature:agentIdle: Bool→activityState: AgentActivityState.All 14 needsRefine tests pass; all 141 IssueTracker-suite tests pass.
Smoke test
Not yet run against the live store — recommend restarting the deployed Crow build against the same store after this lands and watching for corveil#1427 and shell-crm#202 to dispatch on the next poll without manual intervention.
Test plan
swift test --filter IssueTrackerNeedsRefineTests— all 14 passswift test --filter IssueTracker— all 141 passswift build --arch arm64— clean🤖 Generated with Claude Code