Skip to content

Close plugin run records from every terminal outcome of their thread - #7749

Merged
ymichael merged 5 commits into
mainfrom
bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw
Nov 18, 2026
Merged

ymichael merged 5 commits into
mainfrom
bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw

Conversation

@ymichael

@ymichael ymichael commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Automations, Workflows and Tasks keep a record per piece of work: an automation run, a workflow call, or a task's delegated-thread status. Each closed that record only on thread.idle or thread.failed, for a thread id it learned from its own SDK call. Every other way the work can end left the record open:

  • Event before the link. A plugin learns a spawned thread's id only from the threads.spawn / threads.fork response, but core delivered the thread's events as soon as they fired. A turn that ended first was dropped, so the run stayed running until a restart.
  • Queued message deleted. Core does emit message.cancelled (apps/server/src/routes/threads/actions.ts:439), but no run-record plugin listened for it. The Automations run stayed running on a pending thread forever. Later runs deduped onto it, so the automation was wedged, and the restart reconcile kept it open (run.ts:456-457).
  • Run thread archived or deleted. Automations' thread.deleted handler only disabled automations that target the thread and never closed runs, and nothing handled archive. Only a restart closed these runs.
  • Reconcile gave up. One transient threads.get error left the run open until the next restart (run.ts:435-438).
  • Workflow correction send deleted. The call waited for the 24 h run timeout (service.ts:1131).

Found by @thread:thr_nkv2eyk7cp while working on #7717. No filed issue matches; #1914 is a different problem.

What changed

One outcome owner in core: experimental_thread.settled.

  • It delivers a PluginThreadSettledEvent union: completed (lastAssistantText), failed (error), cancelled (the removed queued entry, whose originPluginId names the sender), archived, deleted.
  • apps/server/src/services/plugins/plugin-service.ts emits it from the same seams that already emit thread.idle, thread.failed, thread.archived, thread.deleted and message.cancelled, so it can't drift from them.
  • The three plugins close their records from it with one exhaustive switch:
Record completed failed cancelled (own message) archived deleted
Automation run (plugins/automations/src/run.ts settleAutomationRunsForThread) succeeded failed skipped skipped skipped, plus target automations disabled as before
Workflow call (plugins/workflows/src/server.ts) result failed failed failed failed
Tasks delegated thread (plugins/tasks/lifecycle/index.ts) idle failed idle if the thread is still pending with nothing left queued idle completed

Automations reconcile.

  • A pending thread with nothing left queued is closed as skipped.
  • A run whose thread can't be read is retried on the next sweep tick, instead of waiting for the next restart.
  • Only runs that were open at startup are retried, so an in-flight spawn is never touched.

Event ordering, kept from the first commit. Core delivers a plugin's events for a thread its own in-flight threads.spawn / threads.fork may have created only after that call has returned. packages/plugin-sdk/src/internal/thread-event-gate.ts holds this, and core and the fake plugin host share it.

  • I kept it instead of correlating by pluginMetadata, for three reasons:
    • Metadata is per thread, not per message, so Automations' target-thread sends must match by thread anyway.
    • Putting the receiving plugin's metadata in the payload would mean building one payload per plugin.
    • Tasks mirrors thread.created / thread.active for threads it spawns. Those aren't terminal outcomes, and it relies on the ordering.
  • With the hold, matching by thread id is safe for every thread event, settled included. Its cost is that a spawning plugin hears about threads created during its own call only once the call returns.

Patches deleted: the post-attach re-reads in plugins/workflows/src/service.ts, which also invented the error "Workflow worker failed before attachment completed", and in plugins/tasks/delegate/index.ts, plus their test-only stubs.

SDK surface:

  • experimental_thread.settled and PluginThreadSettledEvent. Entry in docs/api_to_audit.md; callers are Automations, Workflows and Tasks.
  • Docs: backend-events.md, backend-sdk.md, testing.md, the PluginEvents.on JSDoc, and the Guide's thread-events card symbols.
  • The fake plugin host follows thread.idle / failed / archived / deleted with their settled event, as the server does. Tests emit cancelled directly, because the fake has no thread record.
  • Plugin SDK 0.6.160. No server/daemon wire change.

Left out, each with a different owner

  • A concurrency-limit hold whose only recheck lands inside the 1 s pacing window (apps/server/src/services/threads/queued-message-dispatch.ts:517). The record is core's queued row, released by plugin rechecks, not by a thread outcome.
  • A plugin schedule whose function never settles (packages/db/src/data/plugin-schedules.ts:98-126). It waits on the plugin's own promise.
  • Browser Automation cleanupPending sessions waiting for a host reconnect (plugins/browser-automation/server.ts:166).
  • Tasks taskThreadsAttach (delegate/index.ts:390-402) reads the status and then upserts it, for a thread that already exists.
  • Of these, I reproduced none live.

Decisions for review

  • Automations: archived or deleted run thread. The run now closes right away as skipped ("interrupted: thread … was archived/deleted"). Before, it stayed running until a restart closed it with the same reason.
  • Automations: queued message deleted. The run closes as skipped ("cancelled: its message was deleted before it ran"), and the next manual run starts fresh.
  • Automations: pending thread with nothing queued. The startup reconcile now closes it as skipped instead of leaving it running.
  • Workflows: deleting a worker's queued message fails its call.
  • Tasks: an archived thread shows idle, and a pending delegated thread shows idle once its last queued message is deleted, instead of starting forever. Deleting one of several queued messages keeps it starting.
  • Delayed delivery. A spawning plugin gets events for threads created during its own spawn/fork only once that call returns, normally milliseconds and bounded by the SDK's 75 s timeout.

How you verified

Live, on start:worktree. Claude Code was pointed at a stub API that fails every request.

Case main this branch
Spawn response held 4 s by a fetch preload (no product code changed) turn failed at +1.5 s; run arun_otutef4oplm stuck running until restart turn ended at +1.4 s; run closed failed at +4.04 s
concurrency-limit global 0, then the held message deleted with bb thread queue delete run arun_3onytgpfaeu stuck on a pending thread through two restarts; later runs deduped onto it run arun_kjn6kve3ouw closed skipped at once; the next manual run got a new id
Startup with that main-era run still open — reconcile closed arun_3onytgpfaeu as skipped
Held run's thread archived with bb thread archive — run arun_zvbtq6iohhi closed skipped

Guard tests:

  • Each run-record plugin has a table typed Record<PluginThreadSettledEvent["outcome"], …>, so a new outcome kind fails typecheck until each plugin handles it. Every row runs for real.
    • Automations and Workflows run every outcome both before and after spawn returns.
    • Tasks runs every outcome.
    • Against main's handlers all 25 rows fail (Automations 10/10, Workflows 10/10, Tasks 5/5).
  • apps/server/test/services/plugins/plugin-thread-settled.test.ts is a core table, also typed by outcome. Each row drives core's real path: lifecycle moves, DELETE …/queued-messages/:id, archive-all, DELETE /threads/:id. It checks a fixture plugin receives exactly that outcome.
  • plugin-thread-creation-event-order.test.ts uses a real server with the response held for spawn and fork. Both rows fail on main.
  • Gate unit tests, and the Automations reconcile table (pending with and without queued rows; unreadable runs returned for retry).

Commands:

  • turbo run test passes for these packages:

    Package Tests
    SDK 437
    Tasks 409
    Workflows 279
    Automations 178
    Plugin API docs 75
    bb guide 7
  • vitest run test/services/plugins test/skills in @bb/server: 701 pass.

  • turbo run typecheck --filter='...[origin/main]': 108 tasks pass.

  • Lint and type-aware lint pass for the touched packages.

🤖 Generated with Claude Code

AGENT GENERATED

@ymichael ymichael changed the title Deliver a plugin's events for a thread it is creating only after its spawn returns Close plugin run records from every terminal outcome of their thread Nov 18, 2026
@ymichael
ymichael force-pushed the bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw branch 2 times, most recently from d918b9c to 9c846fe Compare November 18, 2026 21:14
ymichael and others added 5 commits November 18, 2026 13:24
…spawn returns

A plugin learns a spawned or forked thread's id only from the call's
response, but core delivered that thread's events the moment they fired.
When the turn ended first, the plugin's thread.idle or thread.failed
handler found no record for the thread and dropped the event, so an
automation run stayed running until the server restarted.

Core's plugin event dispatch now holds a plugin's events for any thread
created while that plugin's own threads.spawn or threads.fork call is in
flight, and releases them in order once the call returns. The fake plugin
host shares the same gate. Workflows and Tasks drop the post-attach
thread reads that patched the same race for their records.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Automations, Workflows and Tasks closed their run records only on
thread.idle or thread.failed. A queued message deleted before it ran, or a
run's thread archived or deleted, left the record running; for Automations
that wedged the automation, since later runs deduped onto the open one,
and the restart reconcile kept pending runs open.

Core now emits experimental_thread.settled, a PluginThreadSettledEvent union
of completed, failed, cancelled, archived and deleted, from the same seams as
the existing events. Each plugin closes its records from it with one
exhaustive switch, and each has a test table typed by the outcome union.
The Automations reconcile closes a pending run with nothing left queued and
retries runs it could not read instead of waiting for the next restart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The cache-integration harness called service.onThreadIdle directly as soon
as the spawn stub ran, which bypasses the thread event gate. The gate's spawn
wrapper adds a few microtask hops before the call attaches its worker, so the
idle arrived first and was dropped. Production delivers idle through the gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Deleting any queued message on a pending delegated thread moved its task
thread to idle, even when the thread's own message was still waiting to run,
for example after the user deleted a follow-up or core withdrew a coalesced
system notice. Tasks now lists the thread's queued messages and moves it to
idle only when the queue is empty, the same rule Automations' reconcile uses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw branch from 5e36fad to 913c22e Compare November 18, 2026 21:25
@ymichael
ymichael merged commit 53cb970 into main Nov 18, 2026
53 checks passed
@ymichael
ymichael deleted the bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw branch November 18, 2026 22:02
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