Skip to content

Add bb tasks detach and list live task threads first - #2167

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1761-tasks-detach
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1761-tasks-detach

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

Task thread attachments in the Tasks plugin were append-only. upsertTaskThread inserts one row per (task_id, thread_id) and the store's deleteTaskThread was dead code: no RPC in delegationRpcContract, no CLI command, and no UI control reached it. An orchestrator that respawns a worker and attaches each replacement therefore accumulates every dead predecessor on the task, and a thread that moves on to another task keeps its row on the old one. On top of that, listTaskThreads ordered rows ORDER BY attached_at, id (oldest first), so the first row in bb tasks threads, bb tasks show, --json, and the task page's "Agent threads" section was the dead predecessor. Issue: #1761. Report: https://get-bb.github.io/reports/issues/1761.html

What changed

  • plugins/tasks/db/store.ts: listTaskThreads now orders live threads (starting/working/idle) before terminal ones (completed/failed), newest first within each group (attached_at DESC, id DESC). This is the single read path, so the CLI table, --json, bb tasks show, the detail page cards, the PR-pill ordering, and task mentions all follow.
  • plugins/tasks/delegate/contract.ts, plugins/tasks/delegate/index.ts: new taskThreadsDetach RPC, the inverse of taskThreadsAttach. It deletes the (task, thread) row via the existing getTaskThreadByThreadId + deleteTaskThread, publishes threads:changed and tasks:changed like attach, and fails with Thread <id> is not attached to <KEY> when there is no row. Detaching from one task leaves the same thread's rows on other tasks alone. The reconcile service reads task_threads, so a detached thread is no longer tracked.
  • plugins/tasks/cli/index.ts: bb tasks detach <key> [--thread <thread-id>] [--json]. Thread resolution is shared with attach (--thread, then BB_THREAD_ID, then the CLI context thread) through a small resolveInvokingThreadId helper. Root help and the registered command list include detach.
  • plugins/tasks/views/detail/threads.tsx, plugins/tasks/views/detail/index.tsx: each thread card gets an X "Detach <title>" control that opens the plugin's ConfirmDialog and then calls taskThreadsDetach; the detail view refreshes the thread and PR queries. Errors surface through the existing detail toasts.
  • Docs: plugins/tasks/README.md (command table + delegation section), plugins/tasks/skills/tasks/SKILL.md (step 6 tells agents to detach on hand-off/respawn), packages/templates/src/templates/bb-guide-plugins.md, and apps/server/src/services/skills/builtin-skills/bb-cli/SKILL.md.

Plugin-internal only: nothing on the server/host-daemon wire changes, so no HOST_DAEMON_PROTOCOL_VERSION bump. attached_at is left untouched on re-attach (it still means "first attached"); a revived thread moves to the live group because the upsert refreshes live_status.

How you verified

New tests (all fail on origin/main, pass on this branch):

  • plugins/tasks/db.test.ts "lists live task threads before terminal ones, newest first" — before: AssertionError: expected [ 'thr_dead_first', …(3) ] to deeply equal [ Array(4) ].
  • plugins/tasks/delegate/delegate.test.ts "detaches an attached thread through taskThreadsDetach and invalidates" — before: plugin "tasks" has no rpc method "taskThreadsDetach".
  • plugins/tasks/cli/cli.test.ts "detaches a thread with bb tasks detach and lists live threads first" — before: expected 'Usage: bb tasks <command> [options]\n…' to contain 'detach …'.
  • plugins/tasks/views/detail/threads.test.tsx "detaches a thread from its card after confirmation and refetches the list" — before: Unable to find role="button" and name "Detach Worker".

Fail-before was produced by checking out the six source files from origin/main with the new tests in place (4 failed | 1 passed), then restoring the branch (4 passed).

From the committed tree (git status --porcelain empty):

  • pnpm exec turbo run typecheck --filter=bb-plugin-tasks --filter=@bb/templates --filter=@bb/server --filter=@bb/cli → Tasks: 8 successful, 8 total
  • pnpm exec turbo run test --filter=bb-plugin-tasks --filter=@bb/cli --force → Tasks: 7 successful, 7 total (tasks: 35 test files passed; cli: 48 passed)

Manual repro on my own dev instance with the builtin tasks plugin installed from this branch, mirroring the issue's transcript (one codex thread forced into error with a bogus model, one real idle thread, both attached to SC-1 in that order):

$ bb tasks threads SC-1
THREAD          STATUS  PRESET    TITLE
thr_tjd3ztqcgd  idle    Attached  Resuming SC-1 after predecessor died
thr_hxiffdtdpf  failed  Attached  SC-1 worker (phase 1)
$ bb tasks detach SC-1 --thread thr_hxiffdtdpf
Detached thr_hxiffdtdpf from SC-1
$ bb tasks detach SC-1 --thread thr_hxiffdtdpf
Thread thr_hxiffdtdpf is not attached to SC-1        (exit 1)
$ bb tasks attach SC-2 --thread thr_tjd3ztqcgd && bb tasks detach SC-1 --thread thr_tjd3ztqcgd
$ bb tasks threads SC-1
No attached threads.

In the app (headless Chrome via doobie), the SC-2 page listed the Idle thread above the Failed one, the X control opened "Detach thread?", and confirming removed the card; bb tasks threads SC-2 then showed only the live thread.

Fixes #1761

AGENT GENERATED: by Claude Opus 5

Independent verification

Verified on a fresh worktree at 4db9a918b (branch is directly on top of origin/main f6fb434ab; git merge-base --is-ancestor origin/main HEAD exit 0).

Commands:

  • pnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo run build -> Tasks: 18 successful, 18 total
  • Fail-before: git checkout origin/main -- plugins/tasks/cli/index.ts plugins/tasks/db/store.ts plugins/tasks/delegate/contract.ts plugins/tasks/delegate/index.ts plugins/tasks/views/detail/index.tsx plugins/tasks/views/detail/threads.tsx, then from plugins/tasks: pnpm exec vitest run db.test.ts delegate/delegate.test.ts cli/cli.test.ts views/detail/threads.test.tsx -t "live task threads before terminal|taskThreadsDetach|bb tasks detach|detaches a thread from its card" -> 4 failed:
    • AssertionError: expected [ 'thr_dead_first', …(3) ] to deeply equal [ Array(4) ] (received thr_dead_first first, expected thr_live_new)
    • Error: plugin "tasks" has no rpc method "taskThreadsDetach" (code: 'unknown_method')
    • AssertionError: expected 'Usage: bb tasks <command> [options]\n…' to contain 'detach …'
    • TestingLibraryElementError: Unable to find role="button" and name "Detach Worker"
  • Pass-after: git checkout HEAD -- <same files> (tree clean), same vitest command -> 4 passed.
  • pnpm exec turbo run typecheck --filter=bb-plugin-tasks --filter=@bb/templates --filter=@bb/server --filter=@bb/cli -> Tasks: 8 successful, 8 total
  • pnpm exec turbo run test --filter=bb-plugin-tasks --filter=@bb/cli --force -> Tasks: 7 successful, 7 total (tasks 35 files / 363 tests, cli 48 files / 453 tests)

Repro of the issue on the fixed branch (own dev instance, builtin:tasks installed from this branch, codex thread with a bogus model -> error, then a real idle thread; both attached to SC-1 dead-first):

$ bb tasks threads SC-1
THREAD          STATUS  PRESET    TITLE
thr_pty8zbfby4  idle    Attached  Resuming SC-1 after a machine crash killed your predecessor
thr_ymbbpzywuq  failed  Attached  SC-1 · OSS divergence audit (phase 1)
$ bb tasks attach SC-2 --thread thr_ymbbpzywuq
$ bb tasks detach SC-1 --thread thr_ymbbpzywuq
Detached thr_ymbbpzywuq from SC-1
$ bb tasks detach SC-1 --thread thr_ymbbpzywuq      # exit 1
Thread thr_ymbbpzywuq is not attached to SC-1
$ bb tasks threads SC-1    -> only thr_pty8zbfby4 (idle)
$ bb tasks threads SC-2    -> thr_ymbbpzywuq still attached (cross-task row untouched)

Headless Chrome (doobie) on the SC-2 task page: the Idle card renders above the Failed card although it was attached later; each card has an aria-label="Detach <title>" X control; clicking it opens the "Detach thread?" ConfirmDialog; confirming calls taskThreadsDetach and the Failed card disappears, confirmed by bb tasks threads SC-2 afterwards.

Review notes: fixes the root cause (ordering query plus the missing inverse at RPC/CLI/UI), plugin-internal so no HOST_DAEMON_PROTOCOL_VERSION change is needed, no casts, no accepted-but-ignored fields, doc surfaces per docs/cli-guide-and-skill.md updated. No consumer of listTaskThreads depends on chronological order (PR resolution dedupes by URL; board/list meta filter by status; mentions just format). Reuses the plugin's existing ConfirmDialog and useRpc<DelegationRpcContract> patterns.

CI: all checks green (gh pr checks 2167: Checks, Package Smoke x2, Tests app-1/2/3, integration, packages, server all pass).

Residual risks: a row reconciled into completed/failed moves down the list on the next refetch (cosmetic; lists are short). Re-attaching does not refresh attached_at. A reconcile tick in flight when a row is detached throws Task thread not found inside transitionThread; it is caught and logged as a warning and creates no comment. No in-app undo for detach; the dialog points at bb tasks attach.

AGENT GENERATED: by Claude Opus 5

Task thread attachments were append-only: the store had deleteTaskThread
but no RPC, CLI command, or UI control reached it, so an orchestrator
that respawned a worker accumulated every dead predecessor on the task.
listTaskThreads also ordered rows oldest-first, so the first row in
`bb tasks threads`, `bb tasks show`, and the task page was the dead one.

Add taskThreadsDetach to the delegation RPC contract (inverse of
taskThreadsAttach; fails when the thread is not attached), a
`bb tasks detach <key> [--thread <id>]` command that resolves the thread
the same way attach does, and a confirm-then-detach control on each
thread card. Order listTaskThreads live-first, newest-first so the
thread holding the work leads the list everywhere the array is rendered.

Fixes #1761

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 07:05
@SawyerHood
SawyerHood merged commit b0ac28c into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1761-tasks-detach branch August 21, 2026 14:39
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.

Task thread attachments are append-only, and the list returns a dead thread first

1 participant