# fix\(server\): auto-settle no longer cuts off an agent waking from awaited background work · PR \#30837 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/30837)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

State: closed

Source repository: Unavailable fork

Source branch: fix/settle-waits-for-agent-wake

Target branch: main

Head commit: 0758097d6b728db4bb20d8e03316060947fe681a

[Discussion and reviews](https://git.cafe/pingdotgg/t3code/pull/30837)

[Commits](https://git.cafe/pingdotgg/t3code/pull/30837/commits?format=markdown)

[Files changed](https://git.cafe/pingdotgg/t3code/pull/30837/diffs?format=markdown)

## Description

---

## Problem

Auto-settle on merge cuts off an agent that is waiting on awaited background work, and the user's task silently stops.

A typical case: the agent merges a linked PR, starts a background command such as waiting for the deploy that merge triggered, marks it awaited (`t3_background_wait`) and ends its turn. Settlement correctly holds while that work is pending. But the moment the work ends, the sweep sees nothing pending and settles the thread. Settling detaches the provider session and cancels queued automatic runs. The provider is waking its agent at exactly that moment, so the wake is killed: the Claude CLI is closed while it opens its wake turn, and the continuation run either finds nothing to drain or is never requested. The agent never learns its work finished, and the user has to send a message to get it going again.

This is why the fix matters. `awaited` exists so the agent can say "I resume when this ends". The settle that the same work was holding back then fires in the one instant it causes harm, and nothing tells the user it happened.

Seen twice in one Claude thread (desktop nightly), both right after a linked PR merged:

- Provider log: `task_notification` and `background_tasks_changed: []` at 12:01:16.524, the CLI's wake `init` at .568, the wake continuation run requested at .619, then `query.close` from the settle detach at .633. The run reopened with `resume`, found nothing to drain and completed empty in 0.7 s.
- The day before, the detach landed 74 ms after the notification, before the CLI opened its wake turn, so no continuation was requested at all.
- The thread's projection shows it settled at that point and unsettled only when the user wrote again.
- A control run in the same thread, with no merged PR pending, woke normally.

Expected: a thread whose agent is about to wake from awaited work settles after that wake run, not in the middle of it.

## Change

`ThreadSettlementService` remembers which threads it last saw holding completion with background work. When a sweep sees that work gone, it records the time, and `awaitingAgentWake` holds auto-settlement:

- while the latest run is still outstanding (queued or running), whatever its timestamp. The sweep records the end when it looks, which can trail the wake run's request. This is needed because `activityRunStatus` ignores queued runs, and settle cancels queued automatic ones.
- otherwise for up to two minutes (the Claude adapter's `CLAUDE_WAKE_TURN_START_DEADLINE`), unless a run requested since the work ended has already completed.

A recheck sweep runs when the grace ends, so a thread whose agent never wakes still settles. The detection lives in the sweep, not in one event handler, so it holds whichever path sees the work end first: `provider-thread.updated`, a finished `command_execution` item, or the periodic full sweep. An active `subagent.updated` also marks the thread, so a subagent that starts and ends between sweeps restarts the grace when it ends. `run.background-work-cancelled` clears it, because work the user stopped wakes no agent and should still settle at once. The recheck timers sit in their own `FiberSet`, so `drain` does not wait out a grace, and they reach `enqueueThread` through a queue, which keeps `sweep` out of a type cycle with it.

Server only and provider-neutral; no contract or client change.

## Scope and approval

This is a focused reliability fix for an obvious bug: auto-settle silently ends agent work the user asked for. No issue or discussion exists yet. It changes only when the existing auto-settle fires, by at most the wake grace, and only for threads whose awaited work has just ended. Manual settle, archive and inactivity settlement are unchanged.

## Verification

New tests in `ThreadSettlementService.test.ts`:
- `awaitingAgentWake`: holds through the grace, then lets go; holds a queued or running wake run past the grace until it completes; holds a wake run requested just before a sweep saw the work end; ignores a run that finished before the work ended.
- "waits for the agent's wake before settling a merged thread whose awaited work ended": no settle when the work ends, none at the 61-second full sweep, settles once the grace passes.
- "restarts the wake grace for a subagent that starts and ends between sweeps": a second subagent inside the first grace still holds the thread until its own grace passes.
- "settles at once when the agent's awaited work is stopped instead of ending".

The wake test fails on `main`, where the thread settles as soon as the work ends, and passes with the fix. The settlement and background-work suites (`ThreadSettlementService`, `ProjectionSettlement`, `BackgroundWorkStop.integration`, `PullRequestSyncReactor`, `pullRequestWatch`, `RestartBackgroundNote`) pass, 137 tests, as does `vp run check:changed` (format, lint, knip, graphql, typecheck, tests beside changes).

Not checked: an end-to-end run of this build against a live provider.

Done with Claude Opus 5.5 in Claude Code (T3 Code).

