Skip to content

feat(step): queue Step model requests when the server asks to wait - #369

Merged
liuedcson merged 5 commits into
mainfrom
feat/step-model-queue
Nov 28, 2026
Merged

liuedcson merged 5 commits into
mainfrom
feat/step-model-queue

Conversation

@liuedcson

@liuedcson liuedcson commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What

StepCode requests to Step's API hosts can now wait in a server-side queue instead of failing when the pool is busy.

  • Requests to api.stepfun.com / api.stepfun.ai (any provider) carry X-StepCode-Queue-ID, a fresh UUID per model call.
  • When the server answers 429 with X-StepCode-Queue-State: waiting, the client:
    • polls GET https://api.stepfun.{com,ai}/stepcode/v1/queue with the same header, at the server's Retry-After-Ms / retry_after_ms (5 s when the server names none, 30 s cap);
    • resends the original request bytes under the same id once the place is ready;
    • ends with a non-retryable 410 queue_closed when the place is closed: a status answer, or a 429 with X-StepCode-Queue-State: closed on the first send or a resend (polling 404 counts as closed; 401/403 pass through; 429/5xx back off with a 30 s budget). The reason comes from a fixed list shared with the server, only reasons a user can act on: queue_full, user_limit, max_wait_exceeded; anything else reads as a generic text;
    • waits at most 20 minutes in all (across resends), then ends as max_wait_exceeded;
    • releases the place with POST .../stepcode/v1/queue/cancel whenever this side stops waiting after it was queued: a stop, the 20-minute bound, a status endpoint that keeps failing, or an unexpected status. A place the server closed is not cancelled.
  • The stream idle watchdog is paused only while waiting in line.
  • Each change of place in line, and the change of phase, is reported: the model_queue event carries queue.state (waiting while it keeps its place, ready once the server admitted it and the same bytes go out again). An admitted place has no position left, so the status says the request is starting instead of naming a place it no longer has.
  • Every request also carries x-step-client-version (the Step product version) next to x-step-client, so the server can admit releases per client.
  • A 429 carrying X-StepCode-Limit-Scope keeps its status, codes and headers (the retry policy is unchanged) and gains a leading sentence: user_quota (the user's own usage limit) or upstream_capacity (Step's capacity).
  • /btw side questions carry the same identity headers and go through the queue.

Switch

None on the client. The server alone decides: the queue engages only when it answers 429 with X-StepCode-Queue-State: waiting.

Compatibility

  • A server that does not queue ignores the headers: the only change is two extra request headers. No extra requests are sent unless the server actually answered "waiting".
  • x-step-client now defaults to stepcode-cli (was stepcode / cli); an explicit STEP_CLIENT is kept. Dashboards keyed on the old values need updating.

Pieces

  • core: createAgentSession({ modelFetch }); request teardowns that session/runtime dispose settle within a bound (so a stop during -p still releases the place); ephemeral model_queue session event (rpc/json emit it with --include-live-events, never persisted).
  • providers: StreamIdleHold; isStepProvider / isStepPlatformUrl as the single source for Step identity (telemetry endpoint classification keeps its previous coverage).
  • cli: TUI queue status line ("Queued: position N" → "Starting..." once admitted; restores the line it replaced), -p writes queue status to stderr.
  • subagent: a child's own model_queue frames (it runs as an rpc process started with --include-live-events) fold by queue id into a live queue on its record, so a host can show a child waiting in line; the lane snapshot validation accepts it and a settled turn drops it.

Tests

pnpm run check; targeted suites (queue client, session, teardowns, print/json/rpc, TUI, signal exit process test); ./test.sh passes except subagent-invocation.test.ts, which fails only on hosts where the isolated temp path exceeds the socket path limit.

Requests to Step's API hosts now carry X-StepCode-Queue-ID. When the
server answers 429 with X-StepCode-Queue-State: waiting, the request
holds its place: the client polls the region's queue endpoint, resends
the original bytes under the same id once ready, and releases the place
on stop. Servers that do not queue ignore the header, so nothing else
changes.

- core: modelFetch hook, request teardowns settled on dispose, and the
  ephemeral model_queue session event
- providers: StreamIdleHold pauses the idle watchdog while waiting;
  isStepProvider / isStepPlatformUrl as the single Step identity source
- step: queue client, model_queue setting (STEPCODE_MODEL_QUEUE env,
  default on), x-step-client defaults to stepcode-cli
- cli: TUI queue status, -p stderr line, json/rpc live events
… 20 minutes

- Drop the client switch (STEPCODE_MODEL_QUEUE and model_queue): the queue
  only engages when Step's server answers with a queued 429.
- Poll every 5s when the server names no interval (capped at 30s); one call
  waits in line at most 20 minutes across resends, then ends as wait_timeout.
- Leave the line with a cancel whenever this side stops waiting: a stop, the
  wait bound, a status endpoint that keeps failing, or an unexpected status.
- Send x-step-client-version with x-step-client so the server can admit
  client releases per client.
- /btw side questions carry the client identity and go through the queue.
- A 429 with X-StepCode-Queue-State: closed, on the first send or a resend,
  ends the call with the non-retryable 410 a closed poll gives. A resend the
  server closed used to go back to the retry policy, which re-entered the
  line at the tail under a new id.
- A 429 with X-StepCode-Limit-Scope keeps its status, codes and headers and
  gains a leading sentence: the user's own usage limit, or Step's capacity.
- Closed reasons and limit scopes are fixed lists shared with the server
  (StepQueueClosedReason, StepLimitScope). Only reasons a user can act on are
  named: queue_full, user_limit, max_wait_exceeded; the 20-minute wait on this
  side reports max_wait_exceeded too. Anything else reads as the generic text.
- A queued call now reports its phase, not just its place: `waiting` while it
  waits, `ready` once the server admitted it and the same bytes go out again.
  The resend used to leave the row showing the last place it had, then the row
  vanished with no way to tell an admitted call from one still in line.
- ModelQueueState carries `state` (`ModelQueuePhase`: waiting | ready) and the
  position only while waiting. Reports go out on a change of either, so an
  admitted place reaches live consumers and `--include-live-events` streams.
- Presentation follows: the TUI status row reads "Starting..." for an admitted
  place, and `-p` writes it like every other change.
- A child runs as an rpc process started with --include-live-events, so its
  model_queue frames already reach the parent, but the event projection
  ignored them: a child waiting in line looked like a child doing nothing.
- The projection now folds each child's frames by queue id and keeps the call
  updated last as `queue` on its live record (state and position only). One
  call leaving the line keeps another's place; two children never share a fold.
- The live roster carries `queue` field by field, and the lane snapshot
  validation accepts it (and refuses a malformed one whole), so a host that
  reads lane snapshots can show it. A settled turn drops it: a final result
  never carries a live field.
@liuedcson
liuedcson merged commit b8afa0a into main Nov 28, 2026
2 checks passed
@liuedcson
liuedcson deleted the feat/step-model-queue branch November 28, 2026 11:09
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