Repository navigation
Description
Context
Part of #4278.
The inference relay records one duration per model request. Its clock starts when the egress operation is created (const started = performance.now(), packages/ui-sdk/src/server/egress-proxy.ts:235) and ends when the upstream stream ends (durationMs, egress-proxy.ts:248). Inside that span the relay does, in order (async fetch(request), packages/ui-sdk/src/server/inference-relay.ts:112):
- admission, awaited (
operation.admit(),inference-relay.ts:125); - reads the whole request body chunk by chunk (
request.body?.getReader(),:136-158); - resolves the server-held credential, awaited (
:161-173); - second admission and
operation.commit(), thenfetch(target)(:194-198); - forwards the response through a pull-driven stream (
:222-245).
Nothing marks the boundaries between them, so a relay span cannot be split into host time before dispatch and the provider's time to first byte. In the measurement behind #4278 relay spans are 63.5% of a median turn, and thinking-first responses reach their first delta 0.65 to 0.78 s later than a month earlier, turn openers included. Neither the hold's length nor the time since the previous response moves that band, and no existing clock can say whether it is the relay path or the provider.
This is the smallest change that re-ranks the rest of the epic.
Scope
- Add monotonic marks to the record the relay already emits for an inference request, reported as durations from the operation's start:
admitMs: first admission returned;bodyMs: request body fully read;credentialMs: credential resolved;dispatchMs: immediately beforefetch(target);headersMs:fetch()resolved;firstChunkMs: first upstream read resolved;- the existing
durationMsstays the end of the stream.
- Add
loopLagMaxMs: the largest drift of a 50 ms interval timer between the operation's start anddispatchMs, so a busy event loop is distinguishable from a slow await. - Carry the fields on the
egress_connectionevent forinference: trueconnections. - A mark that cannot be taken leaves its field absent. It never fails or delays the request.
Out of scope
- Any change to what the relay forwards, to headers or to request bytes. Provider history is untouched.
- Marks for non-inference egress (CONNECT, plain proxying, provider relays). They can follow if these prove useful.
- Acting on what the marks show. That is ui-server: inference admission waits for every pending egress record commit, twice per model request #4293 and 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.
- Client-side clocks: ui-react: no client clock exists for send, first painted content and idle #4282.
Acceptance criteria
- An
egress_connectionevent for an inference request carries the seven fields above, each a non-negative number no larger thandurationMs, in non-decreasing order. - A test with a fake upstream that delays its response headers by a known time shows the delay in
headersMs - dispatchMsand not indispatchMs. - A test that delays the request body shows the delay in
bodyMs. - A test that blocks the event loop for a known time before dispatch reports it in
loopLagMaxMs. - A refused or failed request still records the marks it reached, with the existing outcome and reason.
- The request and response bytes of a relayed request are identical before and after the change (the existing provider-history guard passes unchanged).
-
docs/integration-contract/http.md(theegress_connectionkind, line 447) describes the new optional fields. - A test covering this fails on the tree before the change and passes after.
Verification
bun run test packages/ui-sdk/tests packages/ui-server/tests
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. Additive: new optional fields onegress_connection. - This changes a published package's behaviour, so it needs a changeset.
Activity
schlessera commentedon Nov 20, 2026
Claimed for implementation. Claim token:
claim-76224172. Branch:feat/4279-turn-clocks.Worked as part of the #4278 epic integration branch epic/4278-host-overhead (one main landing for the epic). Collisions: none recorded at claim.
schlessera commentedon Nov 20, 2026
Scope addition (maintainer, 2026-11-20): the inference relay's per-request record also carries the provider-reported prompt-cache usage of each request: input tokens, cache read tokens and cache creation tokens, read from the usage block at the start of the response stream, beside the timing marks above. The relay only reads bytes that already pass through it. Request and response bytes stay identical, and a usage block it cannot read leaves the fields absent without failing or delaying the request. Additive optional fields on
egress_connection, documented indocs/integration-contract/http.md. Today only per-run totals and the first request's cache diagnostics exist. Per-request values let a mid-turn cache miss (for example at a tool-search discovery, #4306, or on resume, #4324) be attributed to the request that caused it.Added acceptance criterion:
egress_connectionevent carries them; with a malformed or missing usage block the fields are absent and the relayed bytes are unchanged.