Skip to content

Evict Claude queries after terminal auth failures - #2804

Closed
ymichael wants to merge 1 commit into
mainfrom
m5/claude-auth-failure-immediate-eviction
Closed

ymichael wants to merge 1 commit into
mainfrom
m5/claude-auth-failure-immediate-eviction

Conversation

@ymichael

@ymichael ymichael commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#2098 records restartBeforeNextTurnReason when Claude emits terminal authentication_failed or oauth_org_not_allowed errors, but the bridge only consumed that marker from the next turn/start. Even after the failed result settled, the stale SDK query therefore stayed resident with failed credentials until another user turn arrived.

What changed

  • Build this focused change on the attachment dormancy and isThreadSessionQuiescent path merged in Add opt-in idle Claude query release #2808; no duplicate pending-work counters or task predicates are added.
  • Release a terminal-auth-failed resident query as soon as its failed turn has settled and the shared quiescence check confirms there is no pending interaction, forwarded tool call, or provider-native background task.
  • Preserve the attachment and providerThreadId; the next normal turn creates a fresh query, resumes that provider thread, emits session/replaced, then emits thread identity and session.reset in the established order.
  • Preserve the typed provider/recovery notification with kind: "authRequired" and retryable: false before closing the stale query.
  • Share the exact-once resident-to-dormant release operation between idle release and auth-failure release.

Auth-failure eviction is independent of #2808's idleQueryReleaseEnabled setting: terminally failed queries still close when ordinary idle release is left at its default-off value.

This remains entirely within the Claude provider plugin. It does not add credential locking and makes no host-daemon wire, public Plugin API, CLI, configuration, or documentation change. HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

The focused bridge regression fails before the implementation because the stale query has zero close calls after the terminal failure result. It now covers both terminal authentication error codes, verifies failed-turn content and typed recovery are emitted before close, verifies eviction itself emits no replacement/reset, confirms the next turn resumes the same provider thread with replacement before reset, and checks exact-once close. A race test verifies eviction waits for both a pending user interaction and provider-native background task. These auth tests leave idle release disabled, covering the independence described above.

After rebasing directly onto current origin/main:

  • pnpm exec turbo run test --filter=bb-plugin-provider-claude-code --force -- src/bridge/__tests__/bridge.test.ts — 83 tests passed
  • pnpm exec turbo run test typecheck --filter=bb-plugin-provider-claude-code --force — 24 test files and 351 tests passed; typecheck passed
  • git diff --check origin/main...HEAD

Builds on #2808. Related to #2097 and #2098.

AGENT GENERATED

@ymichael
ymichael force-pushed the m5/claude-auth-failure-immediate-eviction branch from bb20a84 to c512d7a Compare September 1, 2026 17:02
@ymichael
ymichael changed the base branch from main to bb/investigate-claude-provider-errors-thr_ejnqeazwcr September 1, 2026 17:02
@ymichael
ymichael force-pushed the bb/investigate-claude-provider-errors-thr_ejnqeazwcr branch from 78a5f91 to f4bbd57 Compare September 1, 2026 19:57
@ymichael
ymichael force-pushed the m5/claude-auth-failure-immediate-eviction branch 2 times, most recently from 5f0335c to 003d7bc Compare September 1, 2026 20:02
@ymichael
ymichael force-pushed the bb/investigate-claude-provider-errors-thr_ejnqeazwcr branch from 126b6a7 to f41aff0 Compare September 1, 2026 21:55
@ymichael
ymichael force-pushed the m5/claude-auth-failure-immediate-eviction branch from 003d7bc to 0c3f684 Compare September 1, 2026 21:58
ymichael added a commit that referenced this pull request Sep 1, 2026
## Human comments

## What was wrong

The Claude provider represented a bb thread attachment and its resident
Claude SDK query as one `ThreadSession`. That forced an idle query and
Claude child process to remain alive until core released the entire
attachment after 30 minutes; closing the combined object earlier also
removed routing state, so the next `turn/start` failed with `No active
session`.

## What changed

- Split retained thread attachment state from the optional resident
`SdkSession` and query.
- Add the Claude provider setting `idleQueryReleaseEnabled`, defaulted
off for the bake period. Changes apply on the next start, resume, or
turn command.
- When enabled, release newly started, restored, forked, and post-turn
queries after a provider-owned 30-second quiescent grace period while
preserving the attachment and provider thread id.
- Lazily construct one replacement query on the next normal turn, resume
the same Claude conversation, and preserve `session/replaced` → identity
→ `session.reset` ordering.
- Coalesce simultaneous wakes with an attachment-local promise and
invalidate stale idle callbacks with a residency generation.
- Treat open turns, pending Claude-native background tasks and monitors,
pending interactions, and forwarded tool calls as non-quiescent. Monitor
tasks remain invisible in the timeline while protecting resident work.
- Keep dormant live settings and permission grants available for
reconstruction; `thread/stop` still deletes the attachment, allowing
core's existing 30-minute cleanup to retire the bridge worker.
- Reject invalid `thread/resume` requests before constructing a query.
- Document the provider setting in the guide, configuration reference,
and shipped bb CLI skill.

Enable the bake with:

```sh
bb plugin config provider-claude-code set idleQueryReleaseEnabled true
```

This is Claude-provider policy only. The option travels in the existing
opaque provider-options bag and old host daemons already forward that
bag, so the server/daemon wire contract and
`HOST_DAEMON_PROTOCOL_VERSION` are unchanged. Immediate auth-failure
eviction and credential-store startup serialization remain separate
follow-up PRs (#2804 and #2807).

## How you verified

The fake-clock regressions fail before the implementation and cover:

- release remaining disabled by default;
- a newly started or restored query receiving no turn;
- post-turn idle release and lazy context-preserving wake;
- disabling release on the next turn cancelling a scheduled eviction;
- warm reuse inside the grace period;
- provider-native background tasks and hidden monitor work blocking
release;
- monitor completion starting a fresh grace period; and
- simultaneous turns creating exactly one replacement query.

Commands run after rebasing onto current `origin/main`:

- `CI=1 pnpm exec turbo run test typecheck
--filter=bb-plugin-provider-claude-code --force` — 24 files, 349 tests
passed; typecheck passed
- `CI=1 pnpm exec turbo run test typecheck --filter=@bb/templates
--force` — 43 tests passed; typecheck passed
- `CI=1 pnpm exec turbo run test --filter=@bb/server --force --
test/skills/builtin-skills-copy.test.ts
test/skills/shipped-skills-quality.test.ts` — 14 tests passed
- `CI=1 pnpm exec turbo run test --filter=@bb/server --force --
test/threads/thread-runtime-config.test.ts` — 30 tests passed
- `git diff --check origin/main...HEAD`

Live dev CLI QA confirmed the setting schema reports `default: false`,
setting it to `true` is reflected immediately, and unsetting it restores
the effective value to `false`.

> AGENT GENERATED
Base automatically changed from bb/investigate-claude-provider-errors-thr_ejnqeazwcr to main September 1, 2026 22:48
@ymichael
ymichael force-pushed the m5/claude-auth-failure-immediate-eviction branch from 0c3f684 to 6120737 Compare September 1, 2026 22:59
@ymichael ymichael closed this Sep 14, 2026
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