Skip to content

Fix provider retry for Claude rate limits - #1408

Merged
ymichael merged 1 commit into
mainfrom
bb/investigate-localhost-thread-failure-thr_qq8ipyfd3v
Aug 12, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/investigate-localhost-thread-failure-thr_qq8ipyfd3v

Conversation

@ymichael

@ymichael ymichael commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • canonicalize hard Claude Code rate-limit events into one accepted failed turn with one terminal provider error
  • allow a later 429-only failure to inherit the active blocked subscription window, so a premature manual retry does not destroy recovery eligibility
  • distinguish manual retries from automatic post-reset continuations and enforce automatic eligibility again at the server boundary
  • keep provider-retry timers process-local, serialize all per-thread scheduling/cancellation operations, and attempt a reset window at most once per plugin process
  • bump the host daemon protocol so enrolled daemons receive the adapter behavior change

Root cause

Claude Code reports a hard subscription limit before its synthetic assistant/result sequence. The adapter previously persisted the rate-limit state and an error before turn/started, then persisted a second error when the result arrived. Recovery only inspected the failed turn range, so it missed the blocked state. An immediate manual continuation could then fail with a plain 429 and no new rate-limit state, which removed the plugin timer.

The retry plugin also treated reconciliation, cancellation, supersession, deletion, and timer release as separate asynchronous operations. A reconciliation already in flight could therefore recreate an entry after cancellation. They now use one per-thread operation queue, while pending timers remain intentionally in memory and disappear on server/plugin restart.

Review follow-up

  • cancellation cannot be undone by an in-flight reconciliation
  • automatic release rechecks both the failed request and candidate.automatic; the server independently rejects automatic continuation of a manual-only candidate
  • a later Claude allowed update clears a pending hard rejection
  • durable waiting/attempt keys and restore-time scanning were removed rather than made authoritative
  • deleted threads clear their in-process attempt guard
  • guide text and generated templates describe the process-local behavior

Validation

  • 10 affected package typechecks passed through Turbo
  • agent-runtime suite: 46 files / 917 tests passed
  • focused Claude adapter suite: 149 tests passed
  • provider recovery server suite: 11 tests passed
  • provider-retry scheduler suite: 14 tests passed
  • affected CLI, SDK, contract, template, and plugin package suites passed (9 Turbo tasks)
  • all 24 plugin-fixture cases that failed only when the entire server suite ran concurrently passed when their four files were isolated
  • standalone server + daemon from this worktree with the real Claude Code provider:
    • provider emitted a current allowed rate-limit state
    • a real Claude turn completed successfully
    • no false provider error row or retry entry was created
    • plugin reload retained no timer, as intended
    • no server or daemon warnings/errors were produced

The Claude account's external limit had reset before final standalone QA, so the exact hard-limit path is covered deterministically by the real-message adapter and end-to-end server/plugin tests rather than claimed as a live rate-limited run.

AGENT GENERATED: by GPT-5

@SawyerHood

Copy link
Copy Markdown
Collaborator

🚨 SLOP COP 🚨 · review

I am SlopCop. I am reviewing this pull request for security, code quality, performance, architecture, and end-to-end behavior.

Comment thread plugins/provider-retry/src/service.ts Resolved
Comment thread plugins/provider-retry/src/service.ts Resolved
Comment thread plugins/provider-retry/src/service.ts Resolved
Comment thread plugins/provider-retry/src/service.ts Resolved
Comment thread plugins/provider-retry/src/service.ts Resolved

@SawyerHood SawyerHood left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

ELI5: Claude tells bb when a usage limit blocks work. This change remembers the wait and restarts work after the reset.

I found these issues:

  1. High: A reconciliation can recreate a retry after a user cancels it.
  2. High: Automatic release does not confirm that the current candidate still permits automatic recovery.
  3. High: A later allowed limit does not clear the pending hard rejection.
  4. High: Cancellation reports success when durable storage cleanup fails.
  5. Medium: Deleted threads can leave permanent restore keys.
  6. Medium: Restore reads every key in sequence during plugin activation.
  7. Low: The built-in guide templates still describe memory-only timers and lost state after reload.

I left inline comments for each code issue. Please also update both guide templates and regenerate templates.generated.ts.

I found no duplicate retry implementation. The main architecture risk comes from state changes across plugin waits and server policy checks.

The focused exact-head suite passed 228 tests. Five affected package typechecks passed. All current GitHub checks passed.

Browser E2E does not fit this change because it adds no browser route. The review exercised the nearest CLI and server API paths.

This is a comment-only review. I did not approve the pull request or request changes.

@ymichael
ymichael force-pushed the bb/investigate-localhost-thread-failure-thr_qq8ipyfd3v branch from e0a7ffb to 007c8fd Compare August 12, 2026 21:33
@ymichael
ymichael merged commit c934ec4 into main Aug 12, 2026
10 checks passed
@ymichael
ymichael deleted the bb/investigate-localhost-thread-failure-thr_qq8ipyfd3v branch August 12, 2026 21:46
ymichael added a commit that referenced this pull request Aug 12, 2026
Two Codex runtime reliability fixes, split out of #1385 so they can be
reviewed on their own.

## Bug 1: command output lost when Codex reorders events

Codex app-server normally reports a shell command as `function_call` →
`function_call_output` (the full stdout) → `item/completed`. Sometimes
the `item/completed` event arrives **before** the raw record carrying
the output.

The adapter only repaired a completed item using output it had already
captured. In the reordered case there was nothing captured yet, so it
published the provider's truncated `aggregatedOutput` and the real
output — which arrived a moment later — was dropped. Users saw a
finished command with missing or partial output.

**Fix:** when a completed command item arrives while its shell call is
still awaiting raw output, hold it instead of publishing it. Repair and
emit it as soon as the raw record lands. If the raw record never
arrives, flush the held item at turn completion, so output is at worst
delayed rather than lost. Unparseable raw output clears the pending
shell call so the completed item is released unchanged.

## Bug 2: generated-title rename fails on a brand-new session

A new Codex rollout file can exist briefly before its first record is
written. A generated-title rename landing in that window fails with
`rollout at <path> is empty`, and the thread keeps its placeholder title
even though the same call would succeed moments later.

**Fix:** retry only that exact transient error, and only for Codex,
backing off 50 ms then 200 ms before a final attempt. Every other rename
failure, and every other provider, still fails immediately.

The backoff is only ever paid when the rename has *already* failed — the
happy path returns on the first attempt — and the full 250 ms only when
it fails for good, where the alternative was failing instantly and
leaving the thread on a placeholder title.

## Protocol version

Bug 1 changes what the host daemon sends the server, so
`HOST_DAEMON_PROTOCOL_VERSION` goes 109 → **110** with its contract test
and comment updated. Older daemons can publish a truncated completed
item, so enrolled machines must update.

Note for reviewers: #1408 bumped the constant to 109 and updated the
assertion, but left the test name and comment describing 108. Rebasing
onto it meant rewriting those exact lines, so this PR lands a correct
110 lineage and clears that stale wording as a side effect.

These are product reliability changes only — **no database schema,
migration, or Drizzle snapshot changes**, and no other behavior from
#1385.

## Validation

- `turbo run typecheck --filter=@bb/agent-runtime
--filter=@bb/host-daemon-contract` — pass
- `turbo run typecheck --filter=@bb/host-daemon --filter=@bb/server`
(downstream protocol consumers) — pass
- `turbo run test --filter=@bb/host-daemon-contract` — 49/49 pass
- Rebased onto `3bd8e79a0` and fully re-validated
- `turbo run test --filter=@bb/agent-runtime` — **921 pass, 0 failures**
- Prettier clean on all changed files

Three regression tests were each verified to **fail against current
`main` and pass with the fix**:
- adapter: repairs a completed item when the raw output arrives late
- adapter: flushes the held item at turn end when raw output never
arrives
- runtime: retries the rename through one empty-rollout error and
applies the title

A fourth test pins the exhaustion path: a rollout that stays empty makes
exactly 3 attempts and then propagates the error, so the retry can
neither loop nor swallow a genuine failure.

## Previously-noted test failure is resolved upstream

Earlier revisions of this description flagged
`runtime.process-lifecycle.test.ts > bounds provider stderr while data
arrives without a newline` as failing locally on macOS. #1402 landed
that fix on `main` (the fixture used `process.exit()`, which discards
Node's async-buffered pipe output on macOS). After rebasing onto it,
**the full suite is green: 921/921 with zero failures.**

> AGENT GENERATED: by Claude Opus 5

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ymichael added a commit that referenced this pull request Aug 15, 2026
#1408 (c934ec4) canonicalized Claude's hard subscription rejection into one
terminal error inside the failed turn, and pins an automatic SDK retry as a
retrying rate-limit error. Both invariants live in the shared translator but
were covered only by claude-code/adapter.test.ts, which dies at graduation.
Driven here through the canonical construction the bridge uses (per-session
id prefix, synthesized item/started). All three cases verified red against a
broken invariant.

Co-Authored-By: Claude Fable 5 <[email protected]>
ymichael added a commit that referenced this pull request Aug 17, 2026
#1408 (c934ec4) canonicalized Claude's hard subscription rejection into one
terminal error inside the failed turn, and pins an automatic SDK retry as a
retrying rate-limit error. Both invariants live in the shared translator but
were covered only by claude-code/adapter.test.ts, which dies at graduation.
Driven here through the canonical construction the bridge uses (per-session
id prefix, synthesized item/started). All three cases verified red against a
broken invariant.

Co-Authored-By: Claude Fable 5 <[email protected]>
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.

2 participants