Skip to content

Renew a run's model gateway pass when it runs out, and send the call again once - #3004

Merged
davidmckayv merged 3 commits into
mainfrom
parity-r7b-pass-refresh
Dec 5, 2026
Merged

davidmckayv merged 3 commits into
mainfrom
parity-r7b-pass-refresh

Conversation

@davidmckayv

@davidmckayv davidmckayv commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

For each run, the server gives an engine a short-lived pass to its model gateway (openbotModelRoute.token, about ten minutes). The server now mints a fresh one on every step it carries a run through. But inside one engine run, the pass could still outlast its life, and the next model call got the gateway's 401, which ended the run:

  • Claude Code's long query;
  • a Python harness's own tool loop;
  • a TypeScript step whose retries ran long.

Now every engine's own hop renews the pass and sends the call again once. That is how an OAuth client treats an expired bearer token:

The contract:

  • The route may carry "refresh": "obgr_…" beside token.
  • Renewal happens only on a 401 carrying x-openbot-pass: expired, never on any other 401. A provider's own key refusal carries no such header.
  • To renew, the hop calls POST <gateway root>/pass with the refresh credential as its bearer, gets { "token": … }, sends the call again once, and keeps the fresh pass for every later call of the run.
  • The renewal also carries the engine's own x-openbot-agent-token (MANAGED_AGENT_TOKEN), on that request and no other. An engine is a confidential client, and a confidential client authenticates when it renews (RFC 6749 §10.4; RFC 9700 §4.14, https://www.rfc-editor.org/rfc/rfc9700.html). With it unset, an engine doesn't renew. The server renews only for a run that is still going, and a refresh credential lasts two hours at most.
  • Calls that run out together wait on one renewal.
  • A renewal that fails, a 401 without the header, and a second expired 401 all hand back the gateway's 401 as it came.
  • A route with no refresh credential works as before, and neither credential is ever logged.

Where:

  • shared/run_model_key.py: the httpx hook every Python harness shares (adk, ag2, agno, antigravity, crewai, langgraph-agui, langroid, llamaindex, microsoft, openai-agents, pydantic-ai, strands). One holder per run, sync and async.
  • agent-claude-sdk/src/run_model_proxy.py: a thread's slot holds the run's pass, so Claude Code's calls renew and the slot keeps the fresh pass.
  • shared/model-pass.ts, a renewing fetch used in two places:
    • agent-langgraph-ts gives it to ChatOpenAI and ChatAnthropic on the gateway route. A renewed call is one call to retry.ts.
    • agent-mastra puts it innermost, under its signing and tool-call-index fetches, with one pass per run.

Where it runs

  • New state that outlives a request? One run's pass and refresh credential, held for that run in the engine's process (in the Claude engine, in the thread's existing slot, which already lives a day). An engine run lives in one process.
  • What happens on the second replica? Each replica's engine renews the runs it is serving. The gateway's /pass is stateless: a signed credential in, a signed pass out.
  • Anything serialised? Concurrent calls of one run in one process share one renewal. Nothing crosses processes.
  • Anything fanned out to a browser? No.
  • New listener, port, or schedule? None in the engines.

Boundary and audit

  • Every acting call still goes through the gateway: resolve, decide, audit, then act.
  • New refusals and new failures each write a row. None are new; a renewal that fails hands back the gateway's own 401.
  • Nothing new is trusted from the client that the server can resolve itself. The refresh credential is the server's own, signed, and only ever sent back to it.

API and database

  • This changes nothing under api/, no app's requests, no database change and no release-policy.json. The gateway is internal to a deployment.

Changelog

Bots.

  • A long run no longer stops when its pass to the AI model runs out. A Bot whose run outlasts the pass it was given renews it and carries on by itself, on every engine OpenBot runs, connected harnesses included. It needs a server that sends the renewal credential; with one that doesn't, nothing changes.

Proof

Each place has tests through its real hop against a gateway stand-in on loopback. They cover renewal, the next call on the fresh pass, one renewal for concurrent calls, and the four ways it must not renew: a 401 without the header, /pass refusing or unreachable, no refresh credential, and a second expired 401.

  • Shared Python hook: agent-claude-sdk/tests/test_run_model_pass.py drives the real Anthropic SDK, sync and async. Red 10 failed, green 14 passed. shared/harness_contract.py gains check_pass_refresh, run in every harness's contract test (red on pydantic-ai: 2 failed).
  • Claude engine: two tests in tests/test_run_model_key.py run the real Claude Code. Red 2 failed; the whole suite passes (198).
  • agent-langgraph-ts: 6 new tests in test/model-route.test.ts on both wires. Red: the renewal tests failed. Green: 133 passed, and typecheck is clean.
  • agent-mastra: 12 new tests through real Mastra probes. Red 8 failed; the whole suite passes (64).
  • Biome lint and format are clean. Every Python harness suite passes, except two atomic-writer tests in agent-langgraph-agui/test_provider_boundaries.py, which need Docker and fail the same way on main.
  • Engine token on renewal: every fake gateway refuses /pass without the engine's token, as the server does, and asserts no model call carries it. Renewal carries it trimmed; with it unset or blank, nothing is asked and the 401 comes back. Red then green: Python 12 failed → 203 passed; langgraph-ts 6/6 failed → 133 passed; Mastra 6 failed → 69 passed.
  • Never shown to the model: a Mastra test shows the OpenBot model route entry, its pass and its refresh credential never reach what the model is told or the request bodies it receives.
  • One existing test changed: langroid's contract test now clears langroid's process-wide client cache between fake gateways. It kept a client to the first gateway's closed port, which already broke two plain gateway checks run back to back.

…again once

On staging an unattended job ran past its run's ten-minute pass to the model gateway; its next
model call got the gateway's 401 and the run ended. The server mints a fresh pass on every step it
carries a run through, but inside one engine run a long query, a harness's own tool loop or a step's
retries can outlast it.

The route may now carry a refresh credential ("refresh": "obgr_..."). On a 401 with
`x-openbot-pass: expired`, and only that, each engine's own hop asks the gateway for a fresh pass
(POST <gateway root>/pass with the refresh credential as its bearer), sends the call again once with
it, and keeps it for every later call of the run. Calls that run out together wait on one renewal.
A renewal that fails, a 401 without the header, and a second expired 401 hand back the gateway's 401
as it came. No refresh credential: as before. Neither credential is logged. This is OAuth's way with
an expired token (RFC 6750 §3.1), as google-auth and Claude Code do it.

- shared/run_model_key.py: the httpx hook every Python harness shares keeps the run's pass and
  refresh credential in one holder in the context variable, and renews on sync and async clients.
- agent-claude-sdk: a thread's slot holds the run's pass, so the proxy renews Claude Code's calls
  and the slot keeps the fresh pass.
- shared/model-pass.ts: a fetch for the TypeScript engines. agent-langgraph-ts gives it to
  ChatOpenAI and ChatAnthropic on the gateway route (a renewed call is one call to retry.ts);
  agent-mastra puts it innermost under its signing and tool-call-index fetches, one pass a run.
- Tests through each engine's real hop against a gateway stand-in on loopback: renewal, the next
  call on the fresh pass, one renewal for concurrent calls, and the four ways it must not renew.
  The Python harness contract checks renewal in every harness that runs on the gateway.
…does not renew

A confidential client authenticates when it refreshes a token (RFC 6749 §6 and §10.4; RFC 9700
§4.14), so a refresh credential that leaked should be no use alone. Each engine already holds
MANAGED_AGENT_TOKEN, the token it checks on the server's runs, and the gateway will now refuse
`POST <gateway root>/pass` without it. So the renewal, and only the renewal, sends it trimmed as
`x-openbot-agent-token`; with it unset or blank the engine does not ask, and the call gets the
gateway's 401 as it came. It is never logged or shown on inspection.

- shared/run_model_key.py: the run's pass holds the engine's secret, read when the run starts;
  every Python harness and the Claude engine's proxy go through it.
- shared/model-pass.ts: RunPass takes the engine's secret; agent-langgraph-ts passes the engine's
  env, agent-mastra its process env.
- The fake gateways require the header as the server will and record any model request that
  carried it. New tests: renewal with the right header, no renewal with the secret unset or
  blank, a renewal the gateway refuses for another engine's secret, and Mastra never showing the
  model the `OpenBot model route` entry, its pass or its refresh credential.
@davidmckayv
davidmckayv added this pull request to the merge queue Dec 5, 2026
Merged via the queue into main with commit b321f0b Dec 5, 2026
56 checks passed
@davidmckayv
davidmckayv deleted the parity-r7b-pass-refresh branch December 5, 2026 10:48
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