Repository navigation
Close plugin run records from every terminal outcome of their thread - #7749
Merged
ymichael merged 5 commits intoNov 18, 2026
Merged
Conversation
ymichael
force-pushed
the
bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw
branch
2 times, most recently
from
November 18, 2026 21:14
d918b9c to
9c846fe
Compare
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw
branch
from
November 18, 2026 21:25
5e36fad to
913c22e
Compare
ymichael
deleted the
bb/class-fix-run-records-stuck-non-terminal-thr_6urhaxr3pw
branch
November 18, 2026 22:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.idleorthread.failed, for a thread id it learned from its own SDK call. Every other way the work can end left the record open:threads.spawn/threads.forkresponse, but core delivered the thread's events as soon as they fired. A turn that ended first was dropped, so the run stayedrunninguntil a restart.message.cancelled(apps/server/src/routes/threads/actions.ts:439), but no run-record plugin listened for it. The Automations run stayedrunningon apendingthread forever. Later runs deduped onto it, so the automation was wedged, and the restart reconcile kept it open (run.ts:456-457).thread.deletedhandler only disabled automations that target the thread and never closed runs, and nothing handled archive. Only a restart closed these runs.threads.geterror left the run open until the next restart (run.ts:435-438).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.PluginThreadSettledEventunion:completed(lastAssistantText),failed(error),cancelled(the removed queuedentry, whoseoriginPluginIdnames the sender),archived,deleted.apps/server/src/services/plugins/plugin-service.tsemits it from the same seams that already emitthread.idle,thread.failed,thread.archived,thread.deletedandmessage.cancelled, so it can't drift from them.switch:plugins/automations/src/run.tssettleAutomationRunsForThread)plugins/workflows/src/server.ts)plugins/tasks/lifecycle/index.ts)pendingwith nothing left queuedAutomations reconcile.
pendingthread with nothing left queued is closed as skipped.Event ordering, kept from the first commit. Core delivers a plugin's events for a thread its own in-flight
threads.spawn/threads.forkmay have created only after that call has returned.packages/plugin-sdk/src/internal/thread-event-gate.tsholds this, and core and the fake plugin host share it.pluginMetadata, for three reasons:thread.created/thread.activefor threads it spawns. Those aren't terminal outcomes, and it relies on the ordering.settledincluded. 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 inplugins/tasks/delegate/index.ts, plus their test-only stubs.SDK surface:
experimental_thread.settledandPluginThreadSettledEvent. Entry indocs/api_to_audit.md; callers are Automations, Workflows and Tasks.backend-events.md,backend-sdk.md,testing.md, thePluginEvents.onJSDoc, and the Guide'sthread-eventscard symbols.thread.idle/failed/archived/deletedwith their settled event, as the server does. Tests emitcancelleddirectly, because the fake has no thread record.Left out, each with a different owner
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.packages/db/src/data/plugin-schedules.ts:98-126). It waits on the plugin's own promise.cleanupPendingsessions waiting for a host reconnect (plugins/browser-automation/server.ts:166).taskThreadsAttach(delegate/index.ts:390-402) reads the status and then upserts it, for a thread that already exists.Decisions for review
runninguntil a restart closed it with the same reason.How you verified
Live, on
start:worktree. Claude Code was pointed at a stub API that fails every request.arun_otutef4oplmstuckrunninguntil restartfailedat +4.04 sconcurrency-limit global 0, then the held message deleted withbb thread queue deletearun_3onytgpfaeustuck on apendingthread through two restarts; later runs deduped onto itarun_kjn6kve3ouwclosedskippedat once; the next manual run got a new idarun_3onytgpfaeuas skippedbb thread archivearun_zvbtq6iohhiclosedskippedGuard tests:
Record<PluginThreadSettledEvent["outcome"], …>, so a new outcome kind fails typecheck until each plugin handles it. Every row runs for real.apps/server/test/services/plugins/plugin-thread-settled.test.tsis 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.tsuses a real server with the response held for spawn and fork. Both rows fail on main.Commands:
turbo run testpasses for these packages:vitest run test/services/plugins test/skillsin@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