Skip to content

feat(runtime): single dispatch plan, canonical tool delivery and honest terminal accounting - #656

Open
guyu233 wants to merge 4 commits into
caigee-cmd:mainfrom
guyu233:pr/runtime-contract
Open

guyu233 wants to merge 4 commits into
caigee-cmd:mainfrom
guyu233:pr/runtime-contract

Conversation

@guyu233

@guyu233 guyu233 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

What this adds

The remaining backend contract: one dispatch plan per request, canonical tool delivery, and terminal accounting that cannot report success for a stream that never finished.

Dispatch plan (internal/accounts, internal/runtime, internal/executor)

  • One plan decides the serving account and the member (provider + model) before the upstream call; reserved accounts and rotation update the same pool state instead of two opinions.
  • A member whose provider/region cannot serve the requested model is skipped before any upstream request; a member without the requested tool capability is skipped with a recorded reason.
  • Cooldown and stream feedback are recorded per final account and concrete model.
  • max_attempts counts dispatches of the requested logical model, including the first one; aggregate ordering is preserved and entry rejections carry the request id.
  • accounts gains AttemptKind / RequestState / RequestSent / ErrorCode and CompilePipelineWithClassifier so every attempt carries its own state through the pipeline.
  • Explicit tool references are translated once (CompilePipelineWithClassifier) and the classifier path keeps its own budget.

Tool delivery (internal/gateway)

  • A malformed, truncated, or undeclared call is rejected before any success frame; call identity from the first delta through the terminal frame is preserved for anthropic / openai / responses.
  • Non-stream responses restore custom tool calls, and native call identity survives translation.

Terminal accounting (internal/logs, internal/store, internal/quality, internal/app, internal/control, internal/console, internal/server)

  • Upstream terminal state and downstream write result are recorded separately, so a client disconnect no longer cools down a healthy account and a flush failure propagates.
  • Timeouts are classified by transport phase, and public HTTP/SSE errors are scrubbed while Retry-After is surfaced.
  • New internal/quality model: TTFT, head wait, throughput, TPOT and completeness per attempt; store adds TTFT percentiles, incomplete rate and average throughput.
  • Attempt buffers are bounded and recycled; batch account operations run concurrently; console live state reflects runtime.
  • Public model list agrees with what the key is authorized to call.

Why this is one PR

I tried to slice this further and the intermediate states do not compile or do not pass tests, so a green PR is not available below this granularity:

  • internal/api has five tests that exercise the whole path end to end (TestRelayOpenAIStreamClassifiesAndSuppressesStructuredError, TestResponsesNonStreamRestoresCustomToolCall, TestAPIKeyAttributionAndFilters, TestOpenAINonStreamContractThroughHandler, TestPinnedMissingAccountFallsBackToPool). Updating them without the implementation leaves go test ./... red.
  • executor cannot build the new attempt state without the accounts fields, and gateway cannot build without internal/quality because openai.go derives relay quality.
  • Splitting internal/quality out on its own breaks gateway/openai.go compilation.

So this PR is the smallest backend unit that is green on its own. The two PRs before it are the pieces that can stand alone.

Stack

PR 3 of 3, based on #655 (pr/provider-tool-profiles) which is based on #654. Merge in order; this branch already contains both. The console PR in this set is independent and can land whenever.

Verification

  • go build ./...
  • go vet ./...
  • go test ./... (whole tree, including internal/api)
  • python3 scripts/release-notes.py validate

Backend tree matches the branch these PRs came from; frontend and internal/webui/static are untouched here.

Introduce internal/toolcall as the single model of tool declarations,
tool choice, argument repair, native call identity and rendering.

translate.ValidateChatRequest now delegates tool_choice validation to
toolcall.ParseRequest, which keeps an orphan `auto` degrading to
unspecified while a `required` or named choice fails instead of being
silently dropped.

translate's own tool-argument helpers stay in place until the delivery
path switches to the new package in a follow-up change.
Adapters now derive their tool payload from the canonical toolcall
profile instead of each package reshaping tools on its own, so declared
tools, tool_choice and parallel_tool_calls reach the upstream request
body unchanged.

Adapters also replay a transient failure in place and report the internal
retry count upward, keep the upstream Retry-After hint, and fail a
stream that ends without a terminal frame instead of relaying it as
success.
Each attempt now carries its kind, state, whether the request reached
upstream, and the error code, so a failed round can say where it
stopped instead of only that it failed.

Aggregation pipelines are compiled together with the classifier so a
logical model keeps its own target list and grant scope.
…est terminal accounting

Route through one dispatch plan: the account reservation and rotation
update together, the provider and region stay inside what the request
and API key allow, and a member that cannot enforce the requested tool
contract is skipped before any upstream attempt. Cooldowns and stream
feedback follow the selected account and concrete model, including
native Responses.

Tool calls reach the client from the canonical contract. A malformed,
truncated, or undeclared call is rejected before a success frame or
buffered message is written, and one call identity survives from the
upstream event to the final frame.

Upstream terminal state and the downstream terminal write are recorded
separately, flush failures propagate, and timeouts are attributed to the
transport stage that failed. Public HTTP and SSE errors are redacted,
native failure frames are sanitized, and the upstream Retry-After passes
through CORS.

Add a gateway quality model (TTFT, head wait, throughput, TPOT, delivery
completeness) plus request statistics for TTFT percentiles, incompletion
rate and average throughput. Attempt buffers are bounded and reclaimed,
attempt sequence is stable, and entry rejections carry the request ID.

Aggregation keeps member order, caps dispatches per requested model
across tiers, and keeps previews in sync with configured members. The
public model list follows API-key grants and routing, and a canceled
stream no longer cools a healthy account.
@guyu233
guyu233 force-pushed the pr/runtime-contract branch from b96da6e to 9dae282 Compare December 4, 2026 08:01
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