Skip to content

ui-server: after each tool result the worker is stopped before it can send its next model request (0.8 s median per request) #4292

Open

Description

@schlessera

Context

Part of #4278.

After every tool result that is not proven read-only, the journal observes the brain with the workers stopped, and the worker cannot send its next model request until the observation has committed:

  • the bridge starts the observation when the result frame is handled, without awaiting it (void host.journal?.toolEnded(...), packages/ui-server/src/ws/bridge-capabilities/emission.ts:273);
  • toolEnded treats a call with no proof as unproven and observes (packages/ui-server/src/brain/turn-journal.ts:614-620). No Claude call can be proven read-only (backends: report Claude tool execution evidence where no project hook or brain MCP code can run #2875, docs/decisions/governed-writable-workers.md:1671), so on the Claude backend every result is observed;
  • held takes the hold synchronously in that same call, before yielding (turn-journal.ts:293), and the hold stops every active turn's workers (packages/ui-server/src/brain/turn-scope.ts:166), by SIGSTOP to the worker's process tree (packages/ui-sdk/src/server/worker-freezer.ts:40-44);
  • the workers resume when the observation has committed (turn-scope.ts:183).

The runtime needs 8 to 10 ms after a result to assemble and send its next request, so the synchronous hold wins almost every time. In the measurement behind #4278, 308 of 309 after-tool requests reached the relay more than 100 ms after the result: 0.76 s median, 2.0 s p90 per request; 2.76 s median, 10.1 s p90 per turn. Before governed turns existed the same runtime sent 7 to 9 ms after a result.

This is the schedule the maintainer ruled on #2834 (2026-10-24, Option A; docs/decisions/governed-writable-workers.md:1521-1529): observe after every result that is not proven read-only, with "no coalescing, overshoot relaxation or durability change". The ruling predates the measurement of what it costs on the Claude backend, where its read-only exemption never applies.

The stopped time buys nothing the model's thinking time could not also cover. Whichever option is chosen, every required observation still runs.

Options

A. Deferred freeze. Arm the observation at the tool result. Take the same hold when the turn's relay is about to dispatch the next request: at operation.commit(), the last synchronous point before fetch (packages/ui-sdk/src/server/egress-proxy.ts:316-321, "No await may intervene between this check and connect/request/fetch"; called at packages/ui-sdk/src/server/inference-relay.ts:194). The request goes upstream, and the model generates while the worker is stopped and the observation runs. The worker resumes when the observation commits, and only then can it read the response. A fallback timer takes today's hold if no request follows.

  • Local A/B on the real runtime, journal and relay: with nothing changed by the tool the gain is about 0.15 s per request; with twenty changed notes it is 0.2, 0.9 and 2.3 to 2.7 s at a model wait of 0.3, 1 and 3 s. Repeated over the worker's own socket transport: 18 of 18 deferred requests entered upstream between the confirmed stop and the resume.
  • Scenario on the reference cohort: 1.66 s median and 5.90 s p90 per turn over all 82 turns; 2.67 s and 6.11 s on the 61 that benefit. It is the only change in the epic that moves the whole-turn median (38.2 s to 35.4 s).
  • What changes in the guarantee. Today the host knows the observation's verdict before the next request leaves. With A, one request, carrying the tool's output, is sent and paid for before the host knows whether the observation trips a scope cap or fails. A response cannot be un-sent. The worker also runs unobserved from the result until dispatch or the fallback: tens of milliseconds, against a periodic bound that is already 15 s.

B. Proven read-only windows first. Keep the immediate hold. Extend the existing skip to intervals the host can prove read-only by authority instead of by tool evidence: no host writer, no other turn, no surviving writable child. Then pilot A in shadow mode (record eligibility, deadline and mismatches; send nothing differently) for turns whose next request is already authorized whatever the observation finds.

C. Leave it. Accept 2 to 3 s per tool-using turn as the price of the ruling.

Conditions any implementation of A must meet

Each of these came out of a cross-check between the investigators and is a condition, not an optimisation.

  1. Hold at dispatch, not at receipt. The relay's handler starts on headers and reads the body afterwards (inference-relay.ts:136-158). Stopping the worker at receipt can stop it in the middle of writing its request.
  2. A successful stop is a precondition of dispatch. The existing options.committed notification (egress-proxy.ts:321) fires and returns; a failed hold surfaces as a rejected promise. Dispatch needs a synchronous receipt that the stop succeeded, and a refusal otherwise.
  3. Arm only for the last pending call of the turn. With a sibling call pending, with a subagent's parent call pending, or with any ambiguity about which request follows which result (title or token-count requests, retries, parallel chains), use today's immediate hold.
  4. One deadline per armed observation, raced through one atomic claim against dispatch, a new tool or worker admission, a periodic tick, another turn's hold, finish, abort and shutdown. finish and fail flush it. No two result boundaries are coalesced.
  5. Other sessions. The hold stops every active turn's workers. Arming must consult the global set of active calls and unproven producers, not only this turn's.
  6. A stopped worker with a request in flight is new. A response larger than the socket buffer stalls the upstream read until resume (inference-relay.ts:222-245). Test with a 10 s observation and a large stream.
  7. Admission must not absorb the saving. See ui-server: inference admission waits for every pending egress record commit, twice per model request #4293.
  8. A cap that would have paused the turn. State which caps may be overshot by one request and record the refusal durably when the observation trips one.

Recommendation

A, with the eight conditions as acceptance criteria, behind #4279 and #4280 so the gain is read from clocks. Two of the four investigators recommended B first. The disagreement is exactly the guarantee described under A: whether one model request may leave before the verdict.

What is needed

A maintainer ruling, recorded here as **Decision (maintainer, <date>):**, choosing A, B or C. Decide explicitly whether an inference request may leave before the result observation returns its verdict, which restrictions still require a prior verdict, and how unknown evidence is handled. The existing immediate observation continues to bind until an amendment is recorded. If A is chosen, its timing qualification consumes #4279’s dispatch clock and #4280’s hold clock. A and B amend the "Per-result observation schedule" section of docs/decisions/governed-writable-workers.md (:1521). Draft text for A:

Keep a synchronous observation at every write-capable or unproven result. The hold may be taken when the turn's relay has fully accepted the next inference request and is about to dispatch it, instead of when the result is handled, provided the stop is confirmed before dispatch. The upstream response then overlaps the unchanged hold. A missing or ambiguous request, a pending sibling call, an unknown writer or an interruption takes the immediate path. Each required observation, its attribution, its durability and its terminal accounting are preserved. One inference request may therefore be sent before the observation's scope verdict is known.

Out of scope

Acceptance criteria

Written against the chosen option once ruled. For A, at least:

Verification

bun run test packages/ui-server/tests packages/ui-sdk/tests packages/ui-backend-claude
bun scripts/prove-native-host.ts --backend=claude --observation-timing

Contract impact

None expected. No provider-facing byte changes; the request is the one the runtime would have sent a moment later.

2026-11-20 triage — Verified the existing schedule and source entry points at 63bb400b3, linked the separate timing and latency work, and retained the p2 decision on the guarantee.

Activity

  1. added
    type: fixSomething is broken and must behave the way it is documented.
    area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.
    area: ui-serverHono app factory: turn coordinator, auth, session catalog, routes, cron.
    needs: decisionBlocked on a maintainer or designer ruling, not on code.
    on Nov 20, 2026
  2. added
    priority: p2Wanted, not scheduled. Pulled in when a milestone has room.
    on Nov 20, 2026
  3. schlessera commented on Nov 20, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-11-20): Option A, deferred freeze.

    An inference request may leave before the result observation returns its verdict, under exactly the eight conditions listed in this issue. Each condition is an acceptance criterion. Anything missing, ambiguous or unknown (a pending sibling call, a subagent's parent call, another session's unproven producer, a failed or unconfirmed stop, no next request before the fallback deadline, abort, finish, shutdown) takes today's immediate hold. Every required observation still runs, and none is skipped or coalesced. When a deferred observation trips a scope cap, its refusal is recorded durably and the turn pauses before the response reaches the worker.

    The draft amendment text in this issue goes into the "Per-result observation schedule" section of docs/decisions/governed-writable-workers.md in the implementing PR. The timing qualification reads #4279's dispatch marks and #4280's hold records, so implementation follows those two. #4293 is measured on the same branch.

    The acceptance criteria listed under "For A, at least" now bind this issue.

  4. added
    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.
    and removed
    needs: decisionBlocked on a maintainer or designer ruling, not on code.
    on Nov 20, 2026
  5. schlessera commented on Nov 21, 2026

    @schlessera
    OwnerAuthor

    Out of epic #4278 (2026-11-21). The deferred freeze (PR #4395) is taken out of the epic under the stop rule announced to the maintainer: any further path past the relay gate in the next review means it does not land in this epic.

    The fourth review, on the restructured gate at 435896e36, still found such paths, each recorded on the PR:

    • a continuation that arrives before its tool result is journaled;
    • a later cap that the response check does not see;
    • stream chunks delivered during an immediate observation;
    • Bun's own 413 and 100 Continue answers;
    • an asynchronous hand-off after the final check.

    The epic lands with today's immediate hold unchanged. PR #4395 stays open as a draft with its four review records, to be redesigned against main. The relay gate likely needs to sit at the transport's write path rather than in the handler. The maintainer's Option A ruling above still stands for that work.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.area: ui-serverHono app factory: turn coordinator, auth, session catalog, routes, cron.priority: p2Wanted, not scheduled. Pulled in when a milestone has room.type: fixSomething is broken and must behave the way it is documented.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions