Skip to content

fix(server): auto-settle sweep skips threads holding a queued user message - #48436

Open
juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-48419
Open

juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-48419

Conversation

@juliusmarminge

Copy link
Copy Markdown
Member

Every minute, the auto-settle sweep tried to settle threads that had a message the user sent waiting in their queue. The server always refused, so it logged a 'has a queued message' error each minute and the thread never settled. The sweep now skips those threads, using the same rule the settle command uses.

  • getSettlementCandidates reports hasQueuedUserMessage when a queued run waits on a message that is not automatic (not one sent by an agent, a scheduled task, a notification or a delegated completion).
  • isAutoSettlementCandidate skips threads with that flag set.

Fixes #48419

Reproduction on main

Commit origin/main 9ba2879. Ran the real server from source: T3CODE_DEV_SEED=0 node --import <clock-shift preload> apps/server/src/bin.ts serve --base-dir <scratch> --port 13919 --dev-url .... The preload only adds an offset from a file to Date.now / new Date(), so the clock could jump past the 7-day threshold. Default settings were used (sidebarAutoSettleAfterDays = 7). Added a scratch git repo as a project with t3 project add. Over the app's WS RPC (orchestration.dispatchCommand, the same client path the scenarios use), created a thread as a user on the built-in Replay (dev) provider (model bench-stream) and sent message 1 with start_immediately. While that run was streaming, sent a second user message with queue_after_active. Then killed the replay agent process (a child of my server, PID captured from the server's child list), so run 1 ended failed. The DB then showed run 1 = failed and run 2 = queued with queueHeld=1, which is the reporter's state. Set the clock offset to +8 days. server.trace.ndjson then logged one thread.auto-settle (command id server:auto-settle::) per one-minute ThreadSettlementServiceV2.sweep, at 20:30:40, 20:31:40, 20:32:40 and 20:33:40 shifted time. Each one ended in OrchestratorDispatchError: Failed to dispatch orchestration command thread.settle ... Thread <id> has a queued message. Send it or remove it from the queue before settling. (Orchestrator.ts:4972). settledAt stayed null. Control run: the same setup with messages sent through the t3 thread CLI (an MCP sender, so the queued message counts as automatic) auto-settled successfully on the first sweep, so the loop only happens when a user-sent message is queued. Failing test on branch factory/repro-48419 (d75ee4d): apps/server/src/orchestration-v2/ProjectionSettlement.test.ts, 'sql: a failed thread holding a queued user message is not a settlement candidate'. It builds a failed run plus a held queued run for a user message, and isAutoSettlementCandidate returns true on main.

Evidence

Being captured: before and after in the real app. This section is filled in before the pull request can merge.

Tests

  • vp test run src/orchestration-v2/ProjectionSettlement.test.ts before the fix (with the repro test): 1 failed, 10 passed
  • vp test run src/orchestration-v2/ProjectionSettlement.test.ts src/orchestration-v2/ThreadSettlementService.test.ts after the fix: 68 passed
  • vp run typecheck (apps/server): no errors (only existing suggestions)
  • vp run check:changed: knip, graphql, typecheck (3 packages), test (4 files) all pass; the pre-commit fmt and lint also passed

Not in this change

  • threadHasQueuedTurnStart still ignores queued messages on failed threads and messages older than the grace period; it is also used by storageCleanup, so it was left unchanged.
  • The client shell (OrchestrationV2ThreadShell) has no queued-message field, so isAutoSettlementCandidate run on a shell cannot see this obstacle; only the server sweep uses it.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

juliusmarminge and others added 2 commits December 3, 2026 20:18
…essage

Reproduces #48419: the sweep treats a failed thread holding a queued user
message as a settlement candidate, while thread.auto-settle rejects it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ssage

The sweep treated an old or failed-thread queued message as no obstacle,
while thread.settle rejects any thread whose queue holds a message the user
sent. The sweep then dispatched a doomed thread.auto-settle every minute.

Settlement candidates now carry whether a queued run waits on a
non-automatic message, using the same rule as thread.settle, and
isAutoSettlementCandidate skips those threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Dec 3, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 9e9c207

Macroscope's review found this PR approvable — The automatic settlement sweep now skips threads holding queued user messages, matching the existing settlement guard. This is a narrow filtering change with no new destructive behavior or changed product defaults.

You can add or adjust custom eligibility rules. Learn more.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.5 KiB 92.5 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 78.3 KiB 78.3 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 75 75 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 79.2 KiB 79.2 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 75 75 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: e915e31 · PR result: 9e9c207 · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The settlement candidate query now reports whether a queued run has a queued user message. Automatic settlement excludes threads with such messages. Tests cover held queued messages and an existing case with an older agent-created message.

Changes: Queued message settlement eligibility

Layer / File(s) Summary
Queued message detection
apps/server/src/orchestration-v2/ProjectionStore.ts
The settlement candidate data includes a flag for queued user messages. The query distinguishes automatic messages, including scheduled tasks, notifications, and delegated completions.
Settlement eligibility check
apps/server/src/orchestration-v2/ThreadSettlementService.ts, apps/server/src/orchestration-v2/ProjectionSettlement.test.ts
Automatic settlement excludes candidates with a queued user message. Tests cover a held queued message and an older agent-created message.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aymankhan9

Merge Risk: Minimal · up to 9e9c2

Auto-settle now skips threads that hold a queued user message, which stops repeated failed settle attempts and warnings. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive [#48419] requires that inactivity auto-settle stop dispatching failing thread.auto-settle commands for an idle thread whose queued run waits on a held user message after a failed run. The reviewed dif… Provide the ProjectionStore.ts hunk that computes hasQueuedUserMessage (the settlement-candidate query and its mapping), or a read of that hunk at the reviewed head, so the flag can be checked against the failed-run and held-queued-messag…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title names the fix (auto-settle sweep skipping threads that hold a queued user message) and the scope (server). The raw summary shows the change does exactly this: isAutoSettlementCandidate rejec…
Description check Passed The description follows the template well. It covers the problem (sweep repeatedly attempts to settle threads with a queued user message, and the server refuses), the change (getSettlementCandidates r…
Out of Scope Changes check Passed The whole-PR changes that could be read are connected to [#48419]. ThreadSettlementService.ts adds one guard line in isAutoSettlementCandidate. ProjectionStore.ts adds the optional `hasQueuedUserMes…
Full details: Title check

Explanation

The title names the fix (auto-settle sweep skipping threads that hold a queued user message) and the scope (server). The raw summary shows the change does exactly this: isAutoSettlementCandidate rejects threads when hasQueuedUserMessage is true. The title is a conventional commit, short and specific. It is 74 characters, which is within a reasonable length.

Full details: Description check

Explanation

The description follows the template well. It covers the problem (sweep repeatedly attempts to settle threads with a queued user message, and the server refuses), the change (getSettlementCandidates reports hasQueuedUserMessage and isAutoSettlementCandidate skips flagged threads), scope (links issue #48419), and verification (a repro on main, a failing regression test, test runs, typecheck, and check:changed). Remaining gaps: the Evidence section is explicitly unfinished (before/after evidence still being captured, which the template asks for in UI changes; this is a server change, so the requirement is weaker but the section says it must be filled before merge). The description also contains a long reproduction narrative that is not required by the template. The Scope section does not say why the fix needs no issue, but an issue is linked, so this is satisfied.

Resolution

Complete the Evidence section with the before and after results, or remove the placeholder text. Trim the reproduction narrative if it is not needed for review. Confirm the scope statement that the linked issue shows the bug.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes that could be read are connected to [#48419]. ThreadSettlementService.ts adds one guard line in isAutoSettlementCandidate. ProjectionStore.ts adds the optional hasQueuedUserMessage field and its documented purpose, which feeds that guard. ProjectionSettlement.test.ts adds a regression test and adjusts a fixture so the thread stays eligible after automatic settlement drops a message. These support the objective. The PR description lists threadHasQueuedTurnStart and the client shell as deliberately unchanged. No unrelated change was demonstrated.

Full details: Linked Issues check

Explanation

[#48419] requires that inactivity auto-settle stop dispatching failing thread.auto-settle commands for an idle thread whose queued run waits on a held user message after a failed run. The reviewed diff adds if (thread.hasQueuedUserMessage === true) return false; to isAutoSettlementCandidate in apps/server/src/orchestration-v2/ThreadSettlementService.ts. This line directly addresses the sweep side of the issue. The type field hasQueuedUserMessage?: boolean is declared in apps/server/src/orchestration-v2/ProjectionStore.ts at head, with a comment stating that a queued run waits on a user message and thread.settle rejects the thread. The code that populates this flag from queued runs could not be read. The ProjectionStore.ts diff was unavailable, and the file read was truncated before the population logic. The PR's test claims (the new 'sql: a failed thread holding a queued user message is not a settlement candidate' test and the 68 passing tests) are author-reported and were not re-run. Whether the flag is set in the reported scenario (failed run, held queued user message, no matching automatic message) is therefore not established from the evidence.

Resolution

Provide the ProjectionStore.ts hunk that computes hasQueuedUserMessage (the settlement-candidate query and its mapping), or a read of that hunk at the reviewed head, so the flag can be checked against the failed-run and held-queued-message case. Confirm the new regression test exists at head and asserts isAutoSettlementCandidate returns false for that fixture.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e915e31 and 9e9c207.

Files selected for processing: 3

  • apps/server/src/orchestration-v2/ProjectionSettlement.test.ts
  • apps/server/src/orchestration-v2/ProjectionStore.ts
  • apps/server/src/orchestration-v2/ThreadSettlementService.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@juliusmarminge

Copy link
Copy Markdown
Member Author

The automated pipeline stopped on this pull request and will not merge it:

the fix is written and reviewed, but proving it needs something no worker has:

  • Permanent blocker: the comparison could not be shown without a real server driver capable of terminating the Replay child process and advancing the server clock; no such control was available in the scenario runner used here.

It stays open for a person to finish or close.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Idle threads with a held user message repeatedly fail auto-settle

1 participant