Skip to content

ui-sdk: the inference relay records one duration per request, so host time cannot be told from the provider's time to first byte #4279

Open

Description

@schlessera

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):

  1. admission, awaited (operation.admit(), inference-relay.ts:125);
  2. reads the whole request body chunk by chunk (request.body?.getReader(), :136-158);
  3. resolves the server-held credential, awaited (:161-173);
  4. second admission and operation.commit(), then fetch(target) (:194-198);
  5. 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 before fetch(target);
    • headersMs: fetch() resolved;
    • firstChunkMs: first upstream read resolved;
    • the existing durationMs stays the end of the stream.
  • Add loopLagMaxMs: the largest drift of a 50 ms interval timer between the operation's start and dispatchMs, so a busy event loop is distinguishable from a slow await.
  • Carry the fields on the egress_connection event for inference: true connections.
  • A mark that cannot be taken leaves its field absent. It never fails or delays the request.

Out of scope

Acceptance criteria

  • An egress_connection event for an inference request carries the seven fields above, each a non-negative number no larger than durationMs, in non-decreasing order.
  • A test with a fake upstream that delays its response headers by a known time shows the delay in headersMs - dispatchMs and not in dispatchMs.
  • 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 (the egress_connection kind, 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 lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit. Additive: new optional fields on egress_connection.
  • This changes a published package's behaviour, so it needs a changeset.

Activity

  1. added
    type: feat
    area: ui-sdk
    area: ui-server
    agent-ready
    on Nov 20, 2026
  2. added
    priority: p2
    on Nov 20, 2026
  3. self-assigned this
    on Nov 20, 2026
  4. schlessera commented on Nov 20, 2026

    @schlessera
    OwnerAuthor

    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.

  5. schlessera commented on Nov 20, 2026

    @schlessera
    OwnerAuthor

    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 in docs/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:

    • With a fake upstream whose stream reports known cache read and creation tokens, the request's egress_connection event carries them; with a malformed or missing usage block the fields are absent and the relayed bytes are unchanged.
  6. added this to the 0.45.0 milestone on Nov 21, 2026
  7. removed their assignment
    on Nov 23, 2026
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-readyarea: ui-sdkarea: ui-serverpriority: p2type: feat

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions