[Bug]: Pending self-settle is silently dropped when a queued run was cancelled via t3_queue(cancel) #48168
Before submitting#
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area#
apps/server
Steps to reproduce#
- Start an active turn on a thread (e.g. run
ordinal: 38). - While that turn is still running, send one or more regular messages to the thread (via UI or
t3_thread_send), so they queue as runsordinal: 39(and40). - Still inside the active turn (
ordinal: 38), read/handle the queued message(s) and cancel them viat3_queuewith{"action": "cancel", "queuedRunId": "..."}(or cancel the queued message from the UI), leaving the queue empty. - Still inside the active turn (
ordinal: 38), callt3_thread_organizewith{"action": "settle"}(omittingthreadIdto self-settle at turn end), then finish the turn cleanly (status: "completed").
Expected behavior#
Because all queued runs were explicitly cancelled before starting (startedAt: null, queue is empty) and the active turn completed cleanly, the deferred self-settle (settleAfterRun) should dispatch thread.settle and mark the thread settled: true when the turn ends.
Actual behavior#
t3_thread_organize succeeds (settlesWhenTurnEnds: true), orchestrationV2.threadManagement.settleAfterRun waits until the active turn completes, and then silently exits Success without dispatching thread.settle. The thread remains unsettled indefinitely (even though activeRunId is null and zero runs are queued).
Root cause (main @ e0661db2c851d1c8c2f126d37c40dc3e665e5ec6)#
In apps/server/src/orchestration-v2/ThreadManagementService.ts lines 1286–1341, settleAfterRun inspects all runs with candidate.ordinal > run.ordinal:
- Lines 1287–1304 exclude cancelled queued runs only when their
userMessageIdwas promoted to steer into the active run (steeredMessageIds, added for #36891). - A queued run cancelled before starting via
t3_queue(action: "cancel")/ UI queue cancel hasstatus === "cancelled"andstartedAt === null(cancelledBeforeStart(candidate) === true), but is not insteeredMessageIds, so it stays infollowing. - Lines 1306–1324 then check whether every run in
followingis an automatic wake (notification !== undefined || delegatedCompletion !== undefined). Because regular queued messages (t3_thread_sendor user messages) are not automatic, line 1323 hitsreturn true;and aborts settlement before ever reachingdispatch({ type: "thread.settle", ... }). - Notably, line 1337 already uses
cancelledBeforeStart(candidate)to treat pre-start cancelled runs as no-ops for automatic wakes, but non-automatic cancelled queued runs return early at line 1323 before reaching it.
Excluding cancelledBeforeStart(candidate) when constructing following (lines 1301–1304) would handle both steered-then-cancelled and explicitly cancelled queued runs consistently (while keeping latestRun(records) for expectedLatestRunId so any genuinely new run still blocks settlement).
Impact#
Minor bug or occasional failure
Version or commit#
v0.1.13 (verified still present on main @ e0661db2c851d1c8c2f126d37c40dc3e665e5ec6)
Environment#
Linux, T3 Code v0.1.13 (server + mobile client), MCP t3_queue + t3_thread_organize
Logs or stack traces#
# Sanitized server.trace.ndjson excerpt from one reproduction:
# 1. Active run ordinal:38 running; two queued runs (ordinal:39, ordinal:40) cancelled via queued-run.cancel:
orchestrationV2.dispatch.once command_type="queued-run.cancel" exit=Success
orchestrationV2.dispatch.once command_type="queued-run.cancel" exit=Success
# 2. Agent calls t3_thread_organize({ action: "settle" }) while ordinal:38 is still running:
mcp.tool.t3_thread_organize durationMs=7.86 exit=Success
orchestrationV2.threadManagement.settleThread durationMs=0.96 exit=Success
# 3. 10.5s later, ordinal:38 finishes cleanly (status="completed"); settleAfterRun wakes up,
# reads projection records, and exits Success WITHOUT dispatching thread.settle:
ThreadManagementService.waitForThread durationMs=10484.59 exit=Success
OrchestratorV2.getThreadRecords durationMs=1.85 exit=Success
OrchestratorV2.getThreadRecords durationMs=3.28 exit=Success
OrchestratorV2.getThreadRecords durationMs=3.11 exit=Success
OrchestratorV2.getThreadRecords durationMs=3.10 exit=Success
orchestrationV2.threadManagement.settleAfterRun durationMs=10497.56 exit=Success
# (No orchestrationV2.dispatch.once command_type="thread.settle" follows; thread stays settled:false
# with latestRunId=ordinal:40, status="cancelled", activeRunId=null.)
Workaround#
Have a different thread (e.g., a successor thread during thread handoff) wait for the retiring run to complete (t3_thread_wait) and then call t3_thread_organize(action: "settle", threadId: <retiringThreadId>) on the now-idle thread, which dispatches thread.settle directly instead of going through settleAfterRun.
tanstack-source added labelsbug
Sign in to join this conversation.