Skip to content

fix(dispatch): stop blocking tasks when the Claude API refuses a session - #13

Merged
matthewtsmith merged 2 commits into
mainfrom
fix/api-refusal-handling
Nov 9, 2026
Merged

matthewtsmith merged 2 commits into
mainfrom
fix/api-refusal-handling

Conversation

@matthewtsmith

Copy link
Copy Markdown
Member

Problem

On 2026-11-09 the API stopped two builder sessions: a usage limit (#794) and a safeguard flag. Both claude -p results reported subtype: "success" with no outcome. Foreman treated each as "session ended without outcome" and resumed it 3 times. Every resume got the same refusal. It then labelled the issue blocked with the comment "success after retries". With limits.blockedPerPhase: 2, these two false blocks held the whole phase until someone stepped in.

Change

  • src/session-end.ts: classifyEnd(result, activity) returns usage_limit, safeguard or other, plus the request id and the reset time. It holds every match on the API's wording. It does not look at subtype.
  • Usage limit
    • No retry and no block. Nothing counts against limits.attempts.
    • The claim stays open, as after stop: assignee, worktree and session id are all kept. One note goes on the issue, and the first tick after the reset resumes the session with its context.
    • Dispatching pauses until the window's resetsAt plus 2 minutes, or for 30 minutes when the reset time is unknown. The pause is saved as pausedUntil/pauseReason in state.json and is kept across a restart.
    • While paused, preflight refuses to start a session and runForever sleeps until the pause ends. go (from the page, from ctl go via SIGUSR2, or written to state.json when no daemon is running) clears it.
    • foreman status and the page show paused: usage limit until 22:52Z.
    • A session that returns its outcome as its window runs out also pauses the next one.
  • Safeguard flag
    • The flagged session id is never resumed.
    • The next attempt is one new session (--session-id, new id) in the same worktree. Its prompt says the earlier work, committed and uncommitted, is already there.
    • A second flag blocks the issue with safeguard flag on 2 sessions (req_…).
    • The new session runs even when the flag used up the last allowed attempt.
  • Other: unchanged, including the <subtype> after retries wording.
  • Stream fold (stream.ts)
    • Keeps the seven-day reset time, and keeps an event that reports only the seven-day window.
    • Never reports a 1970 reset time.
    • Records the synthetic message's error field for each message.

Decisions for review

  • The claim stays open instead of being released. A fix-round builder has already had reviewer:changes/validator:failed removed by prepareSession. Releasing the claim would leave nothing to queue that fix round again. Resuming the session also keeps its context.
  • Pause after a finished session. A session that returns its outcome at utilization ≥ 1 pauses the next one. On an account with extra usage, foreman would pause after every such session until someone runs go.

Not verified

  • The test fixtures in test/fixtures/refusal-*.jsonl are built from foreman's normalized activity logs. No raw stream-json from a refusal was on disk.
    • Whether the result line sets is_error is unconfirmed.
    • The "status":"rejected" field in the rate-limit event is a guess.
    • The error: "rate_limit" field on the synthetic message comes from real transcripts.
  • The page change has no mockup. It adds a label, a banner fix text and one status span, all with existing styles.

Testing

pnpm lint && pnpm typecheck && pnpm test pass (746 tests). New tests cover:

  • classifyEnd for all three causes, and for a session that ends with neither refusal text.
  • No retry on a usage limit.
  • Exactly one new-session retry on a safeguard flag, then a block that names the cause.
  • The unchanged no-outcome path.
  • The tick loop sleeping until pausedUntil.
  • Preflight refusing while paused.
  • go clearing the pause.
  • The status text.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JrFSqqRUhJMJH3aK6TDP69

matthewtsmith and others added 2 commits November 9, 2026 14:00
A usage limit and a safeguard flag both end `claude -p` with subtype
"success" and no outcome. Foreman read either one as "the session ended
without an outcome", resumed it three times into the same refusal, and
then blocked the issue with "success after retries". Two of those blocks
held a whole phase through `limits.blockedPerPhase`.

- `src/session-end.ts` holds `classifyEnd`, the only place that matches
  the API's wording. It returns usage_limit, safeguard or other, the
  request id, and the reset time from the rate-limit event.
- Usage limit: no retry and no block. The claim stays open, as after
  `stop`, with one note on the issue. Dispatching pauses until the reset
  plus 2 minutes, or 30 minutes when the reset is unknown. The pause is
  persisted as `pausedUntil`/`pauseReason`, kept across a restart,
  refused by preflight, slept on by the tick loop, shown in `status` and
  on the page, and cleared by `go`. A session that returns an outcome
  as its window runs out also pauses the next one.
- Safeguard flag: the flagged session id is never resumed. One retry
  runs as a new session in the same worktree, and its prompt says the
  earlier work is already there. A second flag blocks the issue with
  "safeguard flag on 2 sessions (req_…)".
- Other: unchanged, including the "<subtype> after retries" wording.
- The stream fold keeps the seven-day reset, a seven-day-only event,
  and the synthetic message's `error` field, and never reports a 1970
  reset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrFSqqRUhJMJH3aK6TDP69
…tick lastApiError

- A safeguard flag on the last allowed attempt, or with `limits.attempts: 1`,
  now still gets its one fresh session instead of blocking after one flag.
- `activity.lastApiError` is set per assistant message, so an error the
  session recovered from no longer reads as a usage limit when the session
  ends for some other reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrFSqqRUhJMJH3aK6TDP69
@matthewtsmith
matthewtsmith merged commit b0079f3 into main Nov 9, 2026
1 check passed
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.

1 participant