Skip to content

Serialize Claude credential initialization by store - #2807

Closed
ymichael wants to merge 1 commit into
mainfrom
bb/serialize-claude-credential-initialization-thr_a3v6d8rr9g
Closed

ymichael wants to merge 1 commit into
mainfrom
bb/serialize-claude-credential-initialization-thr_a3v6d8rr9g

Conversation

@ymichael

@ymichael ymichael commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Separate bb Claude bridge workers could construct Agent SDK queries and run Claude's startup/auth initialization concurrently even when they resolved to the same credential store. Because Claude OAuth refresh credentials can rotate, that unnecessarily increases bb's exposure to the upstream concurrent-refresh race tracked in anthropics/claude-code#88583.

What changed

  • Add a Claude-provider-owned, cross-process file-lock coordinator keyed by a SHA-256 digest of the effective credential scope: the canonical CLAUDE_CONFIG_DIR/default Claude config directory on Linux and Windows, or the macOS Claude Keychain scope.
  • Hold that lock from Agent SDK query construction until initializationResult() settles (or the query ends first), for both thread sessions and the bridge model-discovery probe.
  • Keep unrelated credential stores concurrent. Explicit API/token credentials and Bedrock, Vertex, or Foundry modes bypass this OAuth-store coordination.
  • Heartbeat held locks and reclaim them after staleness so a crashed bridge worker cannot block future initialization indefinitely. Credential values and paths are not logged or used as lock filenames.

This serializes only bb-managed Claude startup/auth initialization among bridge workers sharing one host daemon's Claude-plugin data directory. It does not serialize ongoing work or later mid-session refreshes, standalone Claude CLI/Desktop processes, bridge workers belonging to another bb daemon data directory, explicit credential modes, or unrelated credential stores. It does not add general idle-session residency or auth-failure query eviction.

No host-daemon protocol or public plugin API changed, so HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

  • Added a cross-process harness first. Before the coordinator implementation, its same-store test failed because the second worker entered the initialization section while the first still held it.
  • The harness now verifies same-store serialization, independent-store concurrency, macOS Keychain scoping, explicit-credential bypass, and recovery after a lock holder is killed.
  • Added an SDK-session test proving query construction waits for acquisition and the lock remains held through initializationResult().
  • pnpm exec turbo run test --filter=bb-plugin-provider-claude-code --force — 24 files, 342 tests passed.
  • pnpm exec turbo run typecheck --filter=bb-plugin-provider-claude-code --force — 4 tasks passed.
  • bb plugin build plugins/provider-claude-code — passed.
  • git diff --check origin/main...HEAD — passed.

Related to anthropics/claude-code#88583.

AGENT GENERATED: by GPT-5

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
@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