Skip to content

Add opt-in idle Claude query release - #2808

Merged
ymichael merged 4 commits into
mainfrom
bb/investigate-claude-provider-errors-thr_ejnqeazwcr
Sep 1, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/investigate-claude-provider-errors-thr_ejnqeazwcr

Conversation

@ymichael

@ymichael ymichael commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

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:

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 force-pushed the bb/investigate-claude-provider-errors-thr_ejnqeazwcr branch from a2d721c to 78a5f91 Compare September 1, 2026 16:56
@ymichael

ymichael commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@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 changed the title Reap idle Claude queries inside the provider bridge Add opt-in idle Claude query release Sep 1, 2026
@ymichael

ymichael commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop

bb-slop-cop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I am the Slop Cop. I am now reviewing this pull request for security, quality, performance, architecture, and test behavior.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

Plain English summary

This change can close an inactive Claude Code process after 30 seconds. The next message starts a new process and continues the same thread. The option remains off by default.

Findings

  1. High — active monitor work can lose its Claude process. Claude reports some background work as a monitor task. The translator drops that task before it records lifecycle state. The idle check then sees no open work and can close Claude. Track hidden monitor state and add an idle-release test.

  2. High — the daemon protocol version needs an increase. The new required option crosses the server-to-daemon command boundary. HOST_DAEMON_PROTOCOL_VERSION remains 176. This breaks mixed-version behavior and conflicts with repository policy. Increase the version and its contract test.

  3. Medium — a new thread without a turn stays resident. The start path creates the Claude query but does not schedule the idle timer. The resume and fork paths schedule it. Add the same call and a no-turn start test.

Architecture

The agent runtime already has a 30-minute session reaper. This feature controls a different inner Claude process, so it does not exactly duplicate that reaper. The start, resume, and fork paths repeat setup and timer duties. A shared helper or a small residency controller can prevent lifecycle differences.

Security and performance

I found no security defect. I found no separate performance defect. The timer cancels stale work, and concurrent wake requests share one promise.

Validation

  • The Claude provider suite passed 347 tests in 24 files.
  • The Claude provider type check passed.
  • The focused server suite passed 30 tests.
  • GitHub reports all current checks as successful.
  • A browser cannot exercise this host-side JSON-RPC bridge, so doobie does not apply.
  • The tests mock the Claude query and do not prove actual operating-system process exit.

These findings need fixes. I posted a comment-only review, as required.

@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 commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop

bb-slop-cop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I am the Slop Cop. I am reviewing this pull request for security, quality, performance, architecture, and test behavior.

}
const replacementSession = createThreadSession(attachment);
attachment.residentSession = replacementSession;
startResidentThreadSession(attachment, providerThreadId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — Do not resume an empty new session

A new attachment receives a session ID before Claude reads its first input. If idle release closes it first, Claude creates no conversation file. This wake path then passes that unused ID as resume. The first turn fails with No conversation found with session ID. Track whether Claude persisted the conversation. Start an empty new attachment with its existing sessionId, and use resume only after a turn. Add a start-without-turn, release, and wake test.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

Plain English summary

This pull request adds an optional setting that closes Claude after 30 seconds with no active work. A later message starts Claude again and keeps the same thread context. The setting stays off by default.

Finding

  1. Medium — a new thread can fail after release before its first input. The bridge assigns a session ID before Claude reads any input. Claude does not save an empty conversation. After release, the wake path sends that unused ID as resume. Claude then returns No conversation found with session ID, and the first turn fails. Track whether Claude saved the conversation. Use sessionId for an empty new thread, and use resume only after Claude saves a turn. Add a test for start, no input, release, and wake.

Architecture and code reuse

BB also closes a full idle provider session after 30 minutes. This change closes only the inner Claude process after 30 seconds. These two systems have different duties, so this code does not duplicate the runtime reaper.

Start, resume, and fork still repeat attachment construction. A shared attachment factory could reduce future lifecycle differences, but this is an optional refactor.

Security and performance

I found no security defect. I found no other performance defect. The code keeps one timer for each enabled resident attachment and cancels obsolete timers.

Validation

  • The Claude provider suite passed 349 tests in 24 files.
  • The Claude provider type check passed.
  • The focused server suite passed 30 tests.
  • The SDK 0.3.245 check reproduced the empty-session failure with zero token use.
  • A live development server reached the real Claude program, but the expired local OAuth session blocked a full response.
  • Doobie does not apply because this change has no browser route.
  • git diff --check passed.
  • All current GitHub checks passed.

I posted a comment-only review, as required. I recommend a fix for the empty-session wake before merge.

@ymichael
ymichael merged commit 8c57c7d into main Sep 1, 2026
15 checks passed
@ymichael
ymichael deleted the bb/investigate-claude-provider-errors-thr_ejnqeazwcr branch September 1, 2026 22:48
ymichael added a commit that referenced this pull request Sep 3, 2026
## Human comments

## What was wrong

The opt-in Claude idle releaser treated translated turns,
background-task presentation state, BB-forwarded tool calls, and
interactive requests as the complete residency signal. That omitted
Claude's authoritative `running` and `requires_action` session states
and its pending session-cron snapshot, so the 30-second timer could stop
a process that was still working or waiting to deliver a scheduled
wakeup.

## What changed

The Claude bridge now includes authoritative SDK session state and the
Stop hook's replace-all `session_crons` snapshot in the shared
quiescence decision. `running`, `requires_action`, and any pending
scheduled work keep the query resident; `idle` with an empty cron
snapshot permits the existing grace-period release. The existing
authoritative `background_tasks_changed` handling and older-CLI
task-bookend fallback remain intact. No daemon wire contract, CLI,
guide, or configuration surface changed.

## How you verified

- Added timer regressions for `running` and `requires_action`
transitioning to `idle`; they failed before the fix because
`query.close()` ran and pass after it.
- Added a timer regression for a nonempty Stop-hook session-cron
snapshot transitioning to empty; it failed before the fix and now
preserves the query until the snapshot clears.
- Ran `pnpm exec turbo run test typecheck build
--filter=bb-plugin-provider-claude-code --force` after rebasing onto
current `origin/main`: 24 test files and 351 tests passed; typecheck and
build passed; 6/6 Turbo tasks succeeded.
- `git diff --check` passed.

Follow-up to #2808.

> AGENT GENERATED
SawyerHood pushed a commit that referenced this pull request Sep 3, 2026
## Human comments

## What was wrong

bb threads never got the Claude in Chrome browser tools, even when the
interactive `claude` CLI had Chrome enabled by default. Claude Code
(2.1.259) skips the `claudeInChromeDefaultEnabled` default in
headless/SDK mode and only honors an explicit `--chrome` flag. Checked
directly: `claude -p` loads 0 Chrome tools, `claude -p --chrome`
connects the `claude-in-chrome` MCP server and loads 22 tools. bb never
passed the flag.

## What changed

- `plugins/provider-claude-code/server.ts`: new `chromeEnabled` boolean
setting (default off) and its derived provider option.
- `session-params.ts`, `bridge/commands.ts`: carry `chromeEnabled`
through thread start/resume/fork and turn start/steer params.
- `bridge/session-options.ts`, `bridge/sdk-session.ts`: when enabled,
pass `extraArgs: { chrome: null }` to the Agent SDK, which becomes
`--chrome` on the Claude process.
- `bridge/bridge.ts`: a changed setting on a turn updates the
attachment's session options and marks the resident session to restart
before the next turn with the same provider thread id, so context is
kept. A resume with a changed setting replaces the session through the
existing construction-config comparison.
- Docs: `docs/configuration.md`, bb-cli skill
`references/configuration.md`, and the providers guide template.
- No `HOST_DAEMON_PROTOCOL_VERSION` bump: the option rides inside the
provider plugin's `providerOptions` bag and plugin-internal bridge
params, same as `idleQueryReleaseEnabled` (#2808).

## How you verified

- New tests: `buildSessionOptions` emits `extraArgs` only when enabled;
bridge test that a turn flipping `chromeEnabled` restarts the Claude
process with `resume` set and emits `session/replaced`; server test for
the setting descriptor and derived option; session-params and
thread-runtime-config assertions updated.
- `pnpm exec turbo run typecheck --filter=bb-plugin-provider-claude-code
--filter=@bb/server` clean.
- `pnpm exec turbo run test --filter=bb-plugin-provider-claude-code
--force`: 25 files, 354 tests pass. `@bb/server`
`thread-runtime-config.test.ts`: 30 pass.
- Manual: `claude -p --chrome --output-format stream-json --verbose` on
this host lists `claude-in-chrome` as connected with 22
`mcp__claude-in-chrome__*` tools; without the flag, none.

> AGENT GENERATED
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