Skip to content

Read a thread by id and let the host hand focus between a segment's toggle and collapse controls - #7955

Merged
ymichael merged 2 commits into
mainfrom
bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp
Nov 23, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp

Conversation

@ymichael

@ymichael ymichael commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Core's parent-thread segment was built on private plumbing that plugins can't use. It read its parent through a banner-only React context (CoreThreadContext, filled by ThreadScreen → ThreadDetailPromptArea). Its focus handoff between the summary toggle and the details collapse row came from the banner's own useDisclosureFocusHandoff. A plugin segment gets neither.

Plugins that need one thread run threads.find((t) => t.id === id) over experimental_useSidebarThreads(), which re-renders on every list change and misses archived and unloaded threads, or write their own threads.get fetch. The bundled Git segment wrote its own handoff with a module-level map. That handoff never fired on keyboard expand: ChangedFilesSegmentView gives every non-summary part the "collapse" target, so the action mount's effect ran first, consumed the pending focus, and focused its null ref. Separately, with an archived parent the segment rendered the placeholder Parent thr_xxxx for a frame before hiding (2 of 3 cold loads on main).

What changed

  • experimental_useThread(threadId): ExperimentalThreadState (@get-bb/plugin-sdk/app). The state is { status: "loading" | "notFound" | "error", thread: null } or { status: "ready", thread: ExperimentalThread }. A thread the sidebar's per-thread entry store holds is ready at once with no request and re-renders the component only when that thread changes. Any other thread (archived, unloaded, before the sidebar's first load) is read through the same thread query useSdk().threads.get serves, then kept current by realtime. A 404 (unknown, deleted, or in a deleted project) is notFound, including for a thread deleted while watched, whose query still holds the earlier data; so is any data carrying deletedAt. Other failures are error. A thread that leaves the sidebar while watched (archived) stays ready with its last sidebar value until the fetch answers, with no loading in between. An empty id is notFound with no request.
  • ExperimentalThread is the domain ThreadWithRuntime: what threads.get returns minus the five fields only that request computes (activeBackgroundAgentCount, canRestoreEnvironment, canEditMessages, canSpawnChild, queuedMessageCount). Both sources are projected onto exactly those fields, so a held and a fetched thread have the same shape. This follows the sibling threads.get rather than PluginSidebarThread: it carries deletedAt and none of the row-only fields (indicator, activity). Returning the full threads.get type would have meant inventing the five request-only values for held threads or fetching every thread. The status union follows experimental_useSidebarThreads' status, with notFound split out of error.
  • toggleRef / collapseRef: RefCallback<HTMLElement> on ExperimentalThreadContextSegmentProps. Both banners get isExpanded, onToggle and the refs from one useThreadContextSegmentDisclosure (PluginThreadContextSegments.tsx). When onToggle runs while focus is on one control, the host focuses the other after the commit. Focus anywhere else (composer, another segment) stays put, and a segment that attaches neither ref behaves as before. The type is RefCallback<HTMLElement>, not the design's Ref<HTMLElement>: a RefObject<HTMLElement> member would not type-check on <button ref>.
  • Core on the same path. useParentThreadSection(threadId) calls useThreadState, the implementation behind experimental_useThread, for the current thread and its parent or fork source, with no fetch or fallback of its own. It renders nothing until the related thread is known (no placeholder). It feeds the parent segment (summary, details, useVisible, and the read-only banner) and the context card's parent row. CoreThreadContext, the core banner prop and the parentThreadSection prop chain are deleted. The parent segment attaches toggleRef/collapseRef and has no focus code.
  • Git attaches the refs and drops useFocusHandoff/pendingFocus. The shared-ui disclosure buttonRef/collapseRef props widen from RefObject to Ref (registry item regenerated).
  • Docs: docs/api_to_audit.md (new experimental_useThread entry; the context-segment entry describes the refs and core callers), Plugin Guide apiSymbols, and bb-plugin-authoring frontend-registration.md, frontend-thread-list.md and frontend-thread-context-card.md, and the plugin testing harness (renderSlot({ threads })). Plugin SDK 0.6.188.
  • Not in this PR: moving the parent segment behind the core-contributions boundary. It also needs the nav PR's thread link builder, experimental_threadHref(threadId, { experimental_messageSeq }), so it waits until both land.

Community callers that can migrate (not edited):

  • experimental_useThread: work-map (app.tsx:829,875,886,1395), attention-sidebar (lib/model.ts:150), and the other ~50 threads.find sites from the audit.
  • Refs: flexible-worktrees (statusbar/StatusBar.tsx:146 hand-rolls the collapse→toggle handoff).

How you verified

Kill-tests, run live (pnpm start:worktree, headless Chrome, a fixture project with a child thread, its parent and uncommitted changes):

  • 10.1 keyboard round trip. On main: parent segment toggle → collapse → toggle; Git stayed on its toggle after Enter (handoff broken, see above). On this branch: parent, Git and a fixture plugin segment with both refs all go toggle → collapse → toggle; a fixture segment with no refs keeps focus on its toggle. The read-only banner's parent passes too. A source read of all 13 community segments with details found that 12 keep focus on their expand button (none has a collapse control inside details). The design's "6 community segments lose focus" premise doesn't hold, so the refs' callers are core's parent segment, Git and flexible-worktrees. cache-watch can drop focus when its own button unmounts on collapse, which refs don't address.
  • 10.2 mouse. A real click in Chrome focuses the toggle natively, then the host moves focus to the collapse row (same as core on main). A toggle that doesn't move focus (Safari-style click, programmatic onToggle) leaves the composer focused, for parent, Git and the fixture.
  • 9.1 archived/unloaded parent. Main: placeholder flash in 2 of 3 cold loads with an archived parent. Branch: 0 of 5. An active parent shows its real title directly. An archived child (not in the store) shows its active parent, read through the fetch.
  • 9.2 render count. Fixture plugin counters: three renames of an unrelated thread → experimental_useThread +0 renders, threads.find +2 (React batched two of the updates). Renaming the watched thread → +1 with the new title.

Tests (fail before, pass after):

  • ThreadPromptContextBanner.segments.test.tsx: one focus-handoff case table run against core's parent (writable and read-only), Git and a plugin segment with refs, plus no-refs and focus-elsewhere cases. With main's Git segment code the Git row fails.
  • thread-state.test.tsx: a held thread is read with no request and re-renders only for itself against a whole-list subscriber (reading the whole list instead fails it); a fetched thread has a held one's exact shape; 404 is notFound, 500 is error; a fetched thread deleted while watched and data carrying deletedAt are notFound; a thread leaving the sidebar never reports loading before its fetch answers; an empty id makes no request. The last three fail on the hook's first version.
  • ThreadPromptContextBanner.test.tsx: no placeholder while a parent missing from the store loads, hidden once it reads as archived; an archived thread's parent read through the fallback.

pnpm exec turbo run typecheck lint lint:type-aware test for @bb/app, bb-plugin-bb--git, @get-bb/plugin-sdk, @bb/shared-ui, @bb/plugin-registry, bb-plugin-plugin-api-docs, bb-plugin-bb-guide, @bb/domain: all green (@bb/app 7343 passed), plus @bb/server's shipped-skills-quality.test.ts.

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp branch from 1b65181 to 00e51d3 Compare November 21, 2026 14:14
@ymichael ymichael changed the title Read one live thread by id and let the host hand focus between a segment's toggle and collapse controls Read a thread by id and let the host hand focus between a segment's toggle and collapse controls Nov 21, 2026
@ymichael
ymichael force-pushed the bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp branch 2 times, most recently from 4211cc8 to 176d0a6 Compare November 23, 2026 21:32
@ymichael
ymichael force-pushed the bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp branch from 176d0a6 to 288c92b Compare November 23, 2026 22:18
@ymichael
ymichael merged commit 9f3a35d into main Nov 23, 2026
52 checks passed
@ymichael
ymichael deleted the bb/implement-thread-api-gaps-9-and-10-thr_uk8ggvt9pp branch November 23, 2026 22:26
ymichael added a commit that referenced this pull request Nov 23, 2026
## Human comments

## What was wrong

#7107 merged at 22:25:17 with plugin SDK 0.6.189. #7955 had been rebased
and bumped to 0.6.189 a few minutes earlier, while that number was still
free, and squash-merged at 22:26:18. Git saw its version line as
identical to main's, so the squash left `package.json` and
`plugin-sdk-version.ts` alone. Main now ships #7955's SDK additions
(`experimental_useThread`, `ExperimentalThreadState`,
`ExperimentalThread`, the segment `toggleRef`/`collapseRef`, and the
`renderSlot({ threads })` harness support) under the same 0.6.189 that
#7107 already used, so one version number names two different SDK
surfaces.

## What changed

- `node scripts/bump-plugin-sdk.mjs 0.6.190`:
`packages/plugin-sdk/package.json` and
`packages/domain/src/plugin-sdk-version.ts`.
- `docs/api_to_audit.md`: the two lines #7955 added that name its
required version ("Requires SDK 0.6.189" under `experimental_useThread`,
and the `toggleRef`/`collapseRef` note under thread context segments)
now say 0.6.190. The other 0.6.189 reference,
`plugins/storage-retention`'s `bbPluginSdk: ">=0.6.189"`, belongs to
#7107 and stays.

## How you verified

- `node .github/workflows/check-plugin-sdk-version.mjs` and `node
.github/workflows/check-version-lockstep.mjs` pass locally.
- `pnpm exec turbo run typecheck test --filter=@bb/domain
--filter=@get-bb/plugin-sdk` passes.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 25, 2026
…ver (#8087)

## Human comments

## What was wrong

Every surface that hosts thread actions built its own
`PluginThreadActionTarget`, and they disagreed on `environment`. The
thread header (`ThreadDetailView`) passed it only when a new thread
could reuse it: environment `ready`, host active, path set. The sidebar,
mobile recents, thread-list and Conductor passed any environment the
thread had. So one action saw different targets in different menus. Live
on main at phone width, for a thread whose worktree is still running its
setup script (`provisioning`, path set), the header menu has no "New
thread in environment" while the same thread's sidebar long-press menu
offers it. thread-list (`rows/threadActionTarget.ts`) and Conductor
(`thread-target.ts`) carried byte-identical copies of the mapper, and so
do five community plugins. Separately, a thread action's `useData`
received only ids. Git's Commit therefore called `sdk.threads.get` for
every open thread, plus one subscription per thread, and fetched again
on each environment change and reconnect, only to learn the environment
the target already carried (audit a-thread F8b and F18's git item).

## What changed

**One resolver, on `experimental_useThread`'s path.**
`apps/app/src/lib/thread-actions/thread-action-target.tsx` builds the
target on #7955's thread-by-id resolution in `thread-state.ts`, not on a
store of its own:
- `thread-state.ts` now has one `readThread` resolution, used by both
`useThreadState` and a new non-hook `threadReadSource`. It reads the
held sidebar entry, otherwise the thread query, and keeps the last entry
while a thread leaves the sidebar.
- The target is that thread plus its environment's path. A held entry
carries the path itself; a fetched thread reads the environment query.
- For ids the sidebar doesn't hold, `ThreadActionCollectors` mounts a
loader that calls `useThreadState` and `useEnvironment`. Those are the
same queries the thread page uses, so the open thread adds no request.
- Sidebar rows mount no query observers. There are no module-level
caches; value-stable targets live in each snapshot source's closure.

The registry evaluates `item`, `experimental_bulkItem` and `useData`
from that single resolution, so every surface gets the same target: the
header, row menus and quick actions, the long-press drawer, selection
menus, the pin/read/archive palette commands, mobile recents, and plugin
lists.

**SDK (0.6.202)**
- `PluginThreadActionDataInput.experimental_threads: readonly
PluginThreadActionTarget[]`: the targets for `threadIds`, in the same
order, leaving out threads not loaded yet.
- `experimental_useThreadActions(thread: { id: string }, options?)`,
`PluginThreadActionsMenuBaseProps.thread: { id: string }`, and
`experimental_bulkThreads?: readonly { id: string }[]`: bb reads only
`id`. Existing callers that pass hand-built targets or
`PluginSidebarThread`s still type-check.
- `PluginThreadActionTarget.environment` now means "the thread's
environment" on every surface: `{ id, path, status, hostLifecycle }`,
with `path` null while there is no checkout, or null when the thread has
none or bb cannot read it. The JSDoc said "when a new thread can reuse
it" but held something else on the sidebar.
- The SDK test fake's `TestThreadActionsResolver` and
`TestThreadBulkActionsResolver` now receive the `{ id }` the plugin
passed.

**Callers**
- Core: `ThreadDetailView` (header, header menu,
`PaneThreadActionCommandHandler`) and `RootComposeMobileRecents` pass
the thread they hold. `toThreadActionTarget` and
`threadListEntryActionTarget` are deleted.
- thread-list: `ThreadRow` and the selection store pass
`SidebarThread`s. `rows/threadActionTarget.ts` is deleted.
- Conductor: `WorkspaceTabs` and `WorkspaceSidebar` pass the thread.
`thread-target.ts` is deleted.
- Git's Commit `useData` reads `experimental_threads[].environment` and
no longer calls `threads.get` or subscribes per thread.
- The engine floors for git, thread-list and conductor are now
`>=0.6.202`.

**Visible changes.** One rule now decides where "New thread in
environment" shows: the environment has a checkout, is ready or still
provisioning, and its machine is active. Core's action reads it from the
target, and the thread page's own button uses the same condition.
- Compact header menu: now offers it while the environment provisions,
as the sidebar row already did. The new thread is created in that
environment and waits on "Preparing workspace".
- Sidebar long-press, mobile recents and header: no longer offer it when
setup failed or the machine is being removed or gone. Before, the
sidebar offered it there, and it ended in "Failed to create thread /
Workspace setup failed." or "Machine removal has begun; wait for a
replacement machine".
- Thread page composer button: now also shows while the environment
provisions (it required "ready").

To give every surface that state without extra requests,
`PluginThreadActionTarget.environment` gains `status` and
`hostLifecycle`. Sidebar entries get them from the server:
`ThreadListEntry` gains `environmentStatus` and
`environmentHostLifecycle`, joined from the environment and its host in
the thread list query. An environment `status-changed` event (host
lifecycle changes also send it) now refreshes the sidebar threads in
that environment. A thread outside the sidebar reads them from the
environment query, as before.

Git's Commit is not visible in any of these states. Its status RPC
answers "Workspace is still starting.", "Workspace setup failed." or
"Workspace status unavailable.", so the action hides from its own data.

**Docs**
- `docs/api_to_audit.md`: a "One target" paragraph naming the core
callers, `experimental_threads`, and an updated audit item 1.
- Plugin Guide: `PluginThreadActionDataInput.experimental_threads` in
`surfaces.ts`.
- bb-plugin-authoring: `frontend-registration.md` and
`frontend-sidebar-navigation.md` say to pass the thread and read
`experimental_threads`.

**Community callers (not edited).** All still compile. Their mappers are
now unnecessary, and their thread actions see the host-resolved target:
- nosenss/bb-plugin-attention-sidebar (`components/thread-row.tsx`
`toActionTarget`)
- grishaster80/bb-plugin-focus-sidebar (`app.tsx` `actionTarget`)
- nikushiks/bb-plugin-top-tabs
- tobi/bb-bots-sidebar (`lib/thread-action-target.ts`)
- OXI-717/oxi-thread-list (`rows/threadActionTarget.ts` and its
selection store). Its `ThreadRow.test` resolver reads target fields from
the object it passes, so it needs a lookup by id once it moves to SDK
0.6.202.
- `useData` users notpritam/bb-plugin-inbox, yusuf8834/bb-git-history
and jgnagy/bb-plugin-madmax are unaffected (the field is additive).

## How you verified

**Live repro, `pnpm start:worktree`.** The fixture is a git project
whose `.bb-env-setup.sh` sleeps, which keeps a worktree environment
`provisioning` with its path set.
- Main, 390×844 touch: the header menu for that thread lists Copy link,
Pin, … with no "New thread in environment". The sidebar row's long-press
menu for the same thread lists Copy link, **New thread in environment**,
Pin, ….
- Branch: both menus list "New thread in environment" while the
environment provisions and for a ready checkout. For a failed setup,
neither menu lists it. After killing a provisioning thread's setup
script, the already-open mobile recents row's long-press menu dropped it
without a reload.
- Creating a worktree thread: the environment `status-changed` events
arrived in the same batch as `metadata-changed` events, which already
refresh the sidebar, so the new refresh added no `sidebar-bootstrap`
fetch of its own.
- Branch, desktop: the right-click row menu is unchanged. A shift-click
selection gives "Actions for 3 threads" with Pin, Mark unread,
Notifications, Move to section, Archive 3 threads, Delete 3 threads,
Clear selection. Commit appears in the header for a dirty workspace with
`showCommitButton` on.

**Git requests.** Script: open a thread, then click 5 more sidebar rows,
and attribute each `GET /api/v1/threads/:id` by its request initiator
stack.
- Main: 6 such GETs, all from the `bb--git` bundle (one per opened
thread).
- Branch: 0, both with `showCommitButton` off and on. Total API requests
for the walk fell from 152 to 141.

**Sidebar render cost.** `sidebar.bench.test.tsx` with 3000 threads and
the real thread-list plugin, alternating main and branch under
`taskpolicy -a`.
- Quietest pair (host load average about 32): mount 571 ms main vs 479
ms branch; status patch 37 vs 34 ms; pin 62 vs 59 ms.
- Query observers: 15 on both (0.19 per row), so sidebar rows mount no
loaders.

**Tests**
- `ThreadActionsMenu.test.tsx` "thread action target": every menu
surface (desktop dropdown, desktop context menu, compact drawer, compact
long press), header entries, a two-thread selection and both placements'
`useData` receive the same target, resolved from an id. A thread outside
the sidebar resolves through the thread and environment queries.
- `ThreadActionsMenu.test.tsx` on both compact surfaces: New thread in
environment shows for ready and provisioning environments on an active
machine, and hides for failed setup and for machines being removed or
gone.
- `thread-runtime-display.test.ts`: list entries carry the environment's
status and the host's lifecycle, and follow both when they change.
- `realtime-cache-effects.test.ts`: an environment `status-changed`
event invalidates the sidebar when it holds a thread in that
environment.
- `ThreadDetailView.contract.test.tsx`: header actions get the thread's
environment while it provisions. On main that target's `environment` is
null, so the test fails there.
- `commit-action.test.tsx`: Commit appears from `experimental_threads`,
with `threads.get` rejecting and never called.
- Existing app tests now hold threads in the sidebar store through
`holdSidebarThreads` (active threads in navigation, archived ones in the
archive list), as the app does.

**Commands**
- `pnpm exec turbo run typecheck lint --filter=@bb/app
--filter=@get-bb/plugin-sdk --filter=bb-plugin-bb--git
--filter=bb-plugin-thread-list --filter=bb-plugin-conductor
--filter=bb-plugin-push-notifications --filter=bb-plugin-plugin-api-docs
--filter=bb-plugin-bb-guide`: green.
- `turbo run test`: green for the same packages.
- plugin-sdk passed 448/448 when run alone. Its two timeouts
(shared-declaration-emit, kill-processes) and one plugin-api-docs
timeout (mobile-guide) happened only while other suites ran in parallel.
- `@bb/app` passed 7453 tests with 3 failures, in composer-contract,
PromptBoxInternal and exclusive-pick-first-paint. All 313 tests in those
files pass when the three run alone.
- `ThreadDetailSecondaryContent.test.tsx` fails 7/8 when run directly
with `npx vitest` on both this branch and origin/main, and passes under
turbo.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.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.

1 participant