Skip to content

fix(tots): report Completed only after awaited background work settles - #77

Merged
CarsonJF merged 1 commit into
customfrom
t3/tot-watch-awaited-background-report
Nov 19, 2026
Merged

CarsonJF merged 1 commit into
customfrom
t3/tot-watch-awaited-background-report

Conversation

@CarsonJF

Copy link
Copy Markdown
Owner

Problem

A Tot that launched a thread was woken with Completed: <thread> when the thread's run ended but the thread still held pendingBackgroundTasks with awaited work (a background shell or delegated task). During the nightly repair this produced four spurious wakes, each a full context reload, while the work was still running.

Change

watchedStateFromShell now reads a completed shell that still holds awaited background work as waiting, using backgroundWorkHoldsCompletion — the same rule threadStatus already uses in OrchestratorMcpService. watchTransition therefore emits no completed event at that moment; the watch survives to the real settle and reports Completed exactly once. Question, approval, failed, and ended reports are unchanged, and the tests pin that down.

Remote followers in TotWatchService.follow are unchanged: the forwarded t3_thread_wait result carries the run's status but no pending background tasks, so it cannot distinguish awaited work from a finished run. That limitation is recorded in docs/internals/tots.md.

The sent/watchReports delivery policy in totWatchReports.ts is untouched.

Scope

Small regression fix for an obvious bug found during the nightly repair; no issue filed. Requested by Carson.

Verification

  • Focused: vp test run apps/server/src/tots/TotWatchService.test.ts apps/server/src/tots/totWatchTransitions.test.ts → 2 files, 29 tests passed.
  • New cases: run ends with awaited command → no report; later settle → exactly one Completed, with a second flush adding none; question then approval while awaited work holds → both reported, then Completed on settle; failed run with awaited work → Failed.
  • Regression proof: with only TotWatchService.ts reverted to origin/custom, the three new awaited-work cases fail (a spurious Completed is emitted and the final Completed is missing); they pass with the fix.
  • vp run check:changed origin/custom → pass format, lint, knip 1/2 and 2/2, graphql, typecheck (3 packages), tests (2 files, 22 passed).
  • Caveat: the default vp run check:changed compares custom against upstream main, so it ran the whole fork's diff and hit 16 timeouts in apps/web browser tests. This branch changes no apps/web file — its diff against origin/custom is only the two Tot watch files and one docs line — so those tests run on code identical to untouched custom and are unrelated.

Agent: OpenCode harness, model opencode/deepseek-v4.1-flash-free.

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.
@CarsonJF
CarsonJF merged commit 79c9b0c into custom Nov 19, 2026
@CarsonJF
CarsonJF deleted the t3/tot-watch-awaited-background-report branch November 19, 2026 06:53
CarsonJF added a commit that referenced this pull request Nov 19, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF added a commit that referenced this pull request Nov 20, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 20, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 21, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 21, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 21, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 21, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 22, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 22, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 22, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 22, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 22, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 22, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 23, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 23, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 23, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 23, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 24, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 24, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 24, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 24, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 25, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 25, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 25, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 25, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 25, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 25, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 25, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 25, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CarsonJF added a commit that referenced this pull request Nov 25, 2026
#77)

A watched run that ended while its thread still held awaited background work was read as completed, so TotService woke the creating Tot with Completed: <thread> before the work was done. The nightly repair paid for four of those wake-ups.

Read such a shell as waiting, reusing backgroundWorkHoldsCompletion, the same rule the MCP thread status uses. The watch then survives to the real settle and reports Completed exactly once. Questions, approvals and failed runs still report as before.

Remote followers keep the old timing: a forwarded thread wait exposes the run status but not pending background tasks, so it cannot tell awaited work from a finished run.

Co-authored-by: OpenCode <opencode@local>
CarsonJF pushed a commit that referenced this pull request Nov 25, 2026
#77 made a completed run that still holds awaited work read as waiting, but
the watcher only looked at a thread on run, request and thread events, so
it never looked again when the work ended: the Tot got no Completed, or a
silent end when the thread later settled. It now also looks on the events
ThreadSettlementService treats as work ending (provider-thread, turn-item
and subagent updates that end work, and cancelled background work).

A sent watch whose run was passed by a later run, such as the wake the
finished work starts, now ends instead of leaking, and reports Completed
when that run was waiting on work. Idle-compaction runs no longer wake
watching Tots with a Completed of their own.

The new tests drive the real domain-event stream instead of calling
observe directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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