Skip to content

🤖 refactor: move terminal attention scheduling into TerminalAttentionNotifier - #7669

Merged
ThomasK33 merged 1 commit into
refactor/task-waitersfrom
refactor/task-terminal-attention-notifier
Oct 27, 2026
Merged

ThomasK33 merged 1 commit into
refactor/task-waitersfrom
refactor/task-terminal-attention-notifier

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Moves terminal-attention scheduling and bookkeeping into a new TerminalAttentionNotifier class (terminalAttentionNotifier.ts): notify_on_terminal persists, the per-owner drain chains, idle waits, fresh sub-agent edges and the wake-send backoff (6 fields). drainTerminalAttention stays on TaskService. taskService.ts loses 178 lines in this layer. After the whole stack (#7665 to #7669) it has 21,742 lines, down from 22,800.

Background

PR 2 to 5 of the TaskService split stack (#7665 is PR 1). Each layer moves one group of TaskService state and the methods that own it into a plain class that TaskService builds in its constructor. The rules are the same in every layer: public methods stay on TaskService as one-line facades, methods that tests spy on or patch stay reachable on TaskService and the new class calls them through call-time closures, and tests that cast a moved field change only the cast target. No behaviour changes.

Implementation

  • Moved: markBackgroundWorkNotifyOnTerminal, scheduleNotifyOnTerminalPersist, persistNotifyOnTerminalPolicy, schedulePendingTerminalAttentionOwnerDrains, suppressTerminalAttention, enqueueTerminalAttention, scheduleTerminalAttentionDrain, scheduleTerminalAttentionDrainAfterIdle, backOffTerminalWakeSends.
  • TaskService keeps facades for the public ones and for the ones tests spy on or cast-call (enqueueTerminalAttention, schedulePendingTerminalAttentionOwnerDrains). The facades return the inner promise, so they add no ticks.
  • The drain chain calls deps.drain, which goes through this.drainTerminalAttention, because the test harness replaces that method on the instance.
  • Code that stays uses accessors: takeFreshTurns (still synchronous, before the drain's first await), isWakeSendBackedOff (the drain's two backoff reads, same expire-on-read logic), trackDrain and untrackDrain (deferred descendant messages and the withdrawn-turn reconcile), inFlightNotifyOnTerminalPersists (handleStreamEnd).
  • WORKFLOW_TERMINAL_ATTENTION_SWEEP_INTERVAL_MS moves to the new file, because the backoff needs it. TaskService imports it back for the sweep timer.
  • cutBusyOwnerTurnForSubagentAttention, resolveTerminalWakeCallerSendRestrictions, acknowledgeAgentReports and the sweep stay. They mostly use other TaskService state.
  • Tests: the shared harness and terminalAttentionIdleWait.test.ts change only a cast target. The new terminalAttentionNotifier.test.ts (20 tests) covers per-owner drain order, the idle-wait slot release, fresh edges, backoff expiry and the persist paths.

Validation

  • Stack top: bun test on taskService, the seven new test files, workspaceTurnManager, workflows, tools and workspaceService: 4,898 pass, 0 fail. Typecheck passes on every layer.
  • make static-check, make lint-eslint, make dry-check: pass. make crap-check: moved methods CC 7 or less. drainTerminalAttention drops from CC 106 to 94.
  • Mutation on the new class: 23 killed, 4 equivalent.

Risks

Medium. Terminal attention decides when parents wake. Every ordering point listed in the plan moves unchanged, and the drain body itself does not move.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high • Cost: $53.33

…Notifier

Move-only: TaskService's terminal-attention bookkeeping (notify_on_terminal
persists, per-owner drain chains, idle waits, fresh sub-agent edges and the
wake-group send backoff) and the methods that schedule over it move into
src/node/services/terminalAttentionNotifier.ts. drainTerminalAttention stays
on TaskService and runs through a deps closure, so the test harness wrapper
still sees every drain. Public, spied and cast-called methods stay as
TaskService facades. No behaviour change.
@ThomasK33
ThomasK33 added this pull request to stack #7670 October 27, 2026 07:59
@ThomasK33
ThomasK33 marked this pull request as ready for review October 27, 2026 08:00
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T08:03:12.712229Z dcc63aa Manual request
🔒 Security Review ✅ Completed 2026-10-27T08:04:25.577114Z dcc63aa Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: dcc63aa58a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: dcc63aa58a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@github-actions

Copy link
Copy Markdown
Contributor

🟢 e2e stories (Haiku): 14 passed

22 agent steps · 7 replayed from cache · 15 model calls · 46.3k tokens (28% cached)
Spend: $0.09 of the $1.00 cap (proxy ledger)

All 14 tests in 9 files
Test Agent Time
🟢 chatRestart.e2e.ts · 1 passed 1 step · 1 call 5.4s
🟢 a chat survives a restart of the server 1 step · 1 call 5.4s
🟢 compaction.e2e.ts · 1 passed 1 step · 1 call 4.3s
🟢 /compact replaces the old chat with a summary 1 step · 1 call 4.3s
🟢 pluginTrustRestart.e2e.ts · 1 passed 4 steps · 2 calls 9.3s
🟢 a trusted plugin hook stays trusted after the server restarts 4 steps · 2 calls 9.3s
🟢 settingsRoundTrip.e2e.ts · 1 passed 2 steps · 1 call 8.7s
🟢 a chat survives trips through Settings 2 steps · 1 call 8.7s
🟢 sidebarPerWorkspace.e2e.ts · 1 passed 4 steps · 1 call 5.1s
🟢 a new workspace starts with its right sidebar collapsed while another keeps it open 4 steps · 1 call 5.1s
🟢 smoke.e2e.ts · 1 passed 2 steps · 1 call 3.3s
🟢 the agent opens the playground workspace 2 steps · 1 call 3.3s
🟢 streamErrors.e2e.ts · 1 passed 1 step · 1 call 7.1s
🟢 stream errors show in the chat, and the chat recovers 1 step · 1 call 7.1s
🟢 todoNudge.e2e.ts · 1 passed 1 step · 1 call 3.0s
🟢 the agent finishes open TODO items after the turn-end nudge 1 step · 1 call 3.0s
🟢 touchAlignment.e2e.ts · 6 passed 6 steps · 6 calls 22.9s
🟢 the Settings close button is level with the title on a touch screen 390px wide 1 step · 1 call 4.3s
🟢 the Settings close button is level with the title on a touch screen 768px wide 1 step · 1 call 3.7s
🟢 Backup settings checkboxes are small and level with their labels on a touch phone 1 step · 1 call 4.0s
🟢 the heartbeat checkbox is small and level with its label on a touch phone 1 step · 1 call 3.8s
🟢 a reply's model and time stay on one line on a touch phone 1 step · 1 call 3.9s
🟢 the workspace actions menu stays on the title row on a touch phone 1 step · 1 call 3.2s

e2e 0.20.0 · 1m 12s · web · run artifacts

🟢 e2e Electron stories (Haiku): 3 passed

3 agent steps · 3 model calls · 8.0k tokens (32% cached)
Spend: $0.01 of the $1.00 cap (proxy ledger)

All 3 tests in 3 files
Test Agent Time
🟢 electron/deepLink.e2e.ts · 1 passed 1 step · 1 call 5.4s
🟢 a deep link of a second launch starts a chat in the running app 1 step · 1 call 5.4s
🟢 electron/menuBar.e2e.ts · 1 passed 1 step · 1 call 6.1s
🟢 the workspace menu bar is the desktop title bar 1 step · 1 call 6.1s
🟢 electron/smoke.e2e.ts · 1 passed 1 step · 1 call 8.3s
🟢 Xum Electron opens the playground workspace 1 step · 1 call 8.3s

e2e 0.20.0 · 21.8s · electron · run artifacts

🟢 e2e agent repros (Haiku): no tests selected

Spend: $0.00 of the $1.00 cap (proxy ledger)

e2e 0.20.0 · 5.3s · 2 targets (web, phone) · run artifacts

@ThomasK33
ThomasK33 added this pull request to the merge queue Oct 27, 2026
Merged via the queue into main with commit 3cc5ae3 Oct 27, 2026
48 of 66 checks passed
@ThomasK33
ThomasK33 deleted the refactor/task-terminal-attention-notifier branch October 27, 2026 09:37
yermakoffivan pushed a commit to yermakoffivan/mux that referenced this pull request Oct 27, 2026
…rvice (coder#7674)

## Summary

Splits the five longest stop and attempt methods in `TaskService` into
smaller private steps, in place. No behaviour change, no new file, no
new class. This is PR 6 of the TaskService split (stack 1, coder#7665 to
coder#7669, has merged). It prepares PRs 7 to 10, which move the stop latch
and the attempt ledgers into their own classes.

## Background

PRs 7 and 9 move these methods to new files. The CRAP gate scores a
moved method as new, and its move relief allows at most CC 20 (CRAP 30
in CI). The maintainer chose to split all five methods above CC 15
first, so each later move fits with room to spare.

| Method (CC before -> after) | New steps (CC) |
|---|---|
| `recheckWorkspaceStopRelease` 21 -> 7 | `isWorkspaceStopRecordSettled`
(module fn) 5, `isStopReceiptDecisionPending` 5,
`startStopSettlementReceipt` 4, `releaseWorkspaceStopRecord` 5 |
| `createAdmittedSend` 19 -> 4 | `isAdmittedSendStale` 7,
`streamEndBlocksNewTurn` 2, `recordAdmittedSendTurn` 5,
`disposeSendBeforeAdmission` 5 |
| `admitTaskWorkspaceTurn` 17 -> 10 | `admitSendToCurrentAttempt` 8 |
| `evaluateAttemptLineage` 16 -> 9 | `ownedAttemptSettlementPhase` 3,
`lineageProofFromReceipt` (module fn) 4 |
| `readUnownedSettlementProof` 17 -> 10 | `unownedSettlementCandidate`
(module fn) 7, `unownedSettlementReceiptGap` (module fn) 4 |

## Implementation

- Each method splits along its early returns. The originals keep their
names and signatures, because tests spy on several of them.
- No await moved and none was added. The token callbacks in
`createAdmittedSend` are now one-line arrows that call the new methods,
and they still run synchronously.
- `evaluateAttemptLineage` now reads the settlement entry through
`ownedAttemptSettlementPhase` twice instead of re-reading a `let`.
Without the closing wait there is no await between the two reads, so
they return the same value.

## Validation

- `make crap-check BASE=<stack 1 head>` reports 0 violations and 0
warnings, with and without coverage. Every new helper above CC 4 has at
least 69 % line coverage from the existing tests.
- The test run `bun test src/node/services/taskService*
workspaceTurnManager workflows tools` passes: 3,579 tests, 0 failures.

## Risks

Low. The change is a mechanical split of stop-release and send-admission
code. A wrong split would show as a stuck stop latch or a refused send.
The settlement, stop and admission test suites cover those paths.

---

_Generated with `xum` • Model: `anthropic:claude-opus-5-5` • Thinking:
`high` • Cost: `$68.74`_

<!-- mux-attribution: model=anthropic:claude-opus-5-5 thinking=high
costs=68.74 -->
yermakoffivan pushed a commit to yermakoffivan/mux that referenced this pull request Oct 27, 2026
## Summary

Moves the stop latch out of `TaskService` into a new plain class,
`TaskStopLatch` (`src/node/services/taskStopLatch.ts`). The move is
move-only: no behaviour change, no Effect tag. `taskService.ts` drops
from 21,830 to 21,470 lines. PR 7 of the TaskService split, on top of
coder#7674.

## Background

`TaskService` is ~21,000 lines. Stack 1 (coder#7665 to coder#7669) moved five
state groups into owner classes. This stack moves the stop and attempt
state: PR 7 the stop latch, PRs 8 and 9 the attempt ledger, PR 10 the
stream-end decisions.

## Implementation

- **Fields that moved:** `workspaceStopEpochs`,
`workspaceStopsInProgress`, `queueRechecksOwedByStopLatch` and
`workspaceStopRecords`. `workspaceUserStopEpochs` stays on TaskService,
because it counts user Stops for `sendTreeMessage` and is not latch
state.
- **Methods that moved:** the stop cascade (`beginWorkspaceStop`,
`runWorkspaceStopCleanup`, `markWorkspaceStopPersisted`), the release
(`recheckWorkspaceStopRelease` and the four steps coder#7674 split out of
it), the latch and epoch queries, `deferLaunchWhileStopInProgress`,
`releaseRetainedStopLatches` and `waitForWorkspaceStopsToRelease`.
- **Facades:** TaskService keeps one-line facades for every seam method
(`bumpWorkspaceStopEpoch`, `getWorkspaceStopEpoch`,
`latchWorkspaceStopsInProgress`, `isWorkspaceStopInProgress`,
`releaseRetainedStopLatches`) and for the two methods tests spy on
(`runWorkspaceStopCleanup`, `waitForWorkspaceStopsToRelease`).
- **Coordinators stay:** `recordWorkspaceTurnSettled` and
`recordWorkspaceTurnSuperseded` stay on TaskService. They write three
owners in order (admitted sends, stream-end decisions, stop records).
Their stop-record tails are now `releaseCapturedTurn` /
`rebindCapturedTurn` calls.
- **Deps:** the latch reaches attempt state through call-time
TaskService closures (`TaskStopLatchDeps`), so test spies and patches on
TaskService still see every call. Stop records and admitted sends move
by reference.
- **Exported types:** `OwnedTaskAttempt`, `AdmittedSend`,
`StreamEndDecision` and `WorkspaceStopRecord` are now exported from
`taskService.ts` for `import type`. The new `SettlementReceiptDecision`
alias names the existing return type of `decideSettlementReceipt`.

## Validation

- I checked the moved blocks against the originals with a
whitespace-insensitive comparison, after the `this.x` -> `this.deps.x()`
swaps. They match. Statement order is the same, and no await was added.
- The new file `taskStopLatch.test.ts` has 23 tests on fake deps. They
cover latch refcounting, deferred-launch rechecks, capture by reference,
the release hold conditions, the receipt write before release, and the
wait's listener cleanup and deadline.
- Tests changed only their cast targets.
- CRAP: `beginWorkspaceStop` and `runWorkspaceStopCleanup` are CC 13 and
count as new functions. With coverage they score CRAP 13.0 and 13.5,
under the CI limit of 15.

## Risks

Medium-low. The stop latch gates reawakens, peer sends and queued
launches. A wiring mistake would leave a stuck latch or release one
early. The stop, reawakening, settlement-receipt and attempt-admission
suites cover those paths and pass unchanged.

---

_Generated with `xum` • Model: `anthropic:claude-opus-5-5` • Thinking:
`high` • Cost: `$68.74`_

<!-- mux-attribution: model=anthropic:claude-opus-5-5 thinking=high
costs=68.74 -->

Signed-off-by: Thomas Kosiewski <tk@coder.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