Skip to content

feat(agent-runtime): provider-neutral agent runtime for every agentic surface - #577

Draft
frankxai wants to merge 5 commits into
mainfrom
agent/opencode/provider-neutral-agent-runtime
Draft

frankxai wants to merge 5 commits into
mainfrom
agent/opencode/provider-neutral-agent-runtime

Conversation

@frankxai

@frankxai frankxai commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

What

Adds @starlight-intelligence/agent-runtime: one provider-neutral runtime so the estate stops re-deriving the model, tool and attribution layers by hand in every app.

Five product repos were each doing this separately. That is how model ids drifted between them, how tool exposure grew ungoverned, and how nothing in the estate ever emitted a cost record despite an accepted architecture decision requiring one.

model.ts — provider-neutral routing

A route names a capability, not a vendor SDK call. It resolves through the Vercel AI Gateway, so Anthropic, OpenAI, Google and xAI are all reachable with one key and zero vendor-specific dependencies.

Vendor Gateway id
Anthropic anthropic/claude-opus-5.5
OpenAI openai/gpt-6-astra
Google google/gemini-4-argon
xAI spacexai/grok-4.7

Note the spacexai/ prefix on Grok: it is not xai/. An unknown route fails at resolution with a named error rather than at call time as an opaque provider failure.

tools.ts — MCP tools with the two controls the client leaves to the app

@ai-sdk/mcp already handles what three estate repos re-implemented by hand: 2026-07-28 discovery with legacy fallback, Mcp-Param-* header mirroring, elicitation, session reattach. What it does not do is decide whether a tool may run, or notice a server changed a tool after approval. This package supplies both:

  • Approval. MCP annotations are untrusted server-provided hints. Only an explicit readOnlyHint: true runs unattended; a destructive or open-world tool never does, whatever the hint claims.
  • Drift. fingerprintTools and detectToolDrift detect a server that mutated a tool's description or schema after a human approved it. Drifted tools are refused before they reach the model.

Tool exposure is bounded by an explicit allowlist. There is no implicit "allow everything".

attribution.ts — the contract nothing emitted

Every run produces a record carrying the eleven dimensions the estate's architecture_observability_and_cost_contract requires: brand_id, product_id, agent_id, workflow_id, provider, model, input_tokens, output_tokens, estimated_cost, latency_ms, outcome_status.

Cost and latency come from the gateway's own accounting for the generation, not from a local price table. A price table goes stale the day a vendor changes pricing and then quietly reports the wrong number, which is worse than reporting nothing.

Records are validated before they leave the process and passed through a credential redactor, because they are meant to be committed or shipped to a ledger.

packages/core — the types the JSON Schemas never declared

packages/core/schemas/cost-record.schema.json and agent-run.schema.json already described these shapes, but packages/core/src had no TypeScript declaration for them and no producer anywhere in the estate emitted one. The provider-neutral attribution types now live there. packages/core stays zero-dependency, which the release audit enforces.

Evidence

  • packages/agent-runtime: 14 tests, 14 pass
  • Release audit (npm-artifact-audit, npm-ecosystem-release): 26 tests, 26 pass
  • Track D v0.1 evals: 34 pass, 0 fail
  • Symmetry and conformance suite: pass
  • Root tsc build: clean under strict with noUnusedLocals

Wired into pnpm-workspace.yaml, the release audit targets and the npm consumer check, so it publishes through the existing OIDC workflow rather than a new mechanism.

Boundaries

  • Not a state store. Long-lived addressable actors belong on Cloudflare Agents with Durable Objects.
  • Not a credential authority. MCP is a tool interface, not an authority layer.
  • Not a chat transport. Streaming stays on the AI SDK 7 UI message stream protocol.
  • Not a second runtime. ADK, the OpenAI Agents SDK and the Claude Agent SDK are candidates, not layers under this one.

Review notes

  • The tier → model-id table is a deliberate one-line diff when a default is wrong. The "balanced" tier currently resolves to anthropic/claude-sonnet-5.5; that is a judgement call worth overriding if you prefer a different default.
  • ai is pinned to 7.0.170 in devDependencies because that is the version whose @ai-sdk/provider-utils matches @ai-sdk/mcp@2.0.97. Two copies of provider-utils produce incompatible nominal Schema types and a compile error that a cast would have hidden.
  • The ADR at docs/architecture/ADR-2026-11-12-provider-neutral-agent-runtime.md records the alternatives compared, including @arcanea/model-runtime, and why this one wins.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

… surface

Adds @starlight-intelligence/agent-runtime so apps stop re-deriving the
model, tool and attribution layers by hand. Five product repos were each
doing it separately, which is how model ids drifted between them, how tool
exposure grew ungoverned, and how nothing in the estate ever emitted a cost
record despite an accepted architecture decision requiring one.

- model.ts: capability routes resolve through the Vercel AI Gateway, so
  Anthropic, OpenAI, Google and xAI are reachable with one key and no
  vendor SDK. Unknown routes fail at resolution with a named error.
- tools.ts: MCP tools via the official AI SDK MCP client, plus the two
  controls the client leaves to the app: approval (only an explicit
  readOnlyHint runs unattended) and drift detection against an approved
  fingerprint baseline.
- attribution.ts: emits the eleven dimensions the estate observability
  contract requires. Cost and latency come from the gateway's own
  accounting, never a local price table.
- packages/core: adds the provider-neutral attribution types the JSON
  Schemas already described but never declared. Stays zero-dependency.

Wired into pnpm-workspace, the release audit targets and the npm consumer
check, so it publishes through the existing OIDC workflow.
@frankxai
frankxai force-pushed the agent/opencode/provider-neutral-agent-runtime branch from 7590e91 to 7aa8bcc Compare November 12, 2026 18:39
… defects

An independent Codex review of PR #577 returned FAIL. It confirmed the
Gateway routing, the spacexai/ namespace and the dependency-alignment
rationale, and found eight real defects. All are closed here.

Release:
- build:packages and test:packages omitted agent-runtime, so CI never built
  or tested it and pnpm pack shipped no dist/. Added to both filters.
- the consumer check rewrites only ../dist/index.js, so the runtime test's
  subpath imports failed in the installed tarball. The tests now import the
  package's public entry point, which is also the better boundary.
- the foundry lock pinned digests of pnpm-lock.yaml and package.json, both of
  which this change modifies. Refreshed, as the lock's own updatePolicy
  requires a reviewed pull request.

Safety:
- requiresApproval accepted an omitted openWorldHint as closed-domain.
  The MCP spec defines openWorldHint's default as TRUE, so an omitted hint
  now means open-world and needs a human.
- the drift guard was skipped whenever no baseline was supplied, so a
  forgotten baseline silently disabled it. A baseline is now required unless
  the caller opts out explicitly, and that state is reported.
- redaction cleaned only provider, model, agent_id and workflow_id, so a
  credential in brand_id or product_id survived. Every string field is
  cleaned now, and the bearer pattern covers the full base64url alphabet.

Correctness:
- an explicit route validated only the id's syntax, so an unrouted vendor
  resolved successfully. The namespace is now checked at resolution.
- vendorOf read inherited object properties, so 'constructor/model' resolved
  to a function. It is an own-property lookup now.
- a multi-step run reported only its last generation's cost, and every run
  was recorded as completed. Calls are accumulated and the terminal state is
  taken from onEnd/onAbort/onError.
- a failed gateway lookup substituted a zero cost, indistinguishable from a
  free generation. Such a record now carries cost_known: false.
- tools were cast to Record<string, never>, suppressing type checking at the
  public boundary. They are typed as the SDK's ToolSet now.

Tests: 19 in the package (up from 14), each able to fail against the
defective behaviour.
…le defects

A second independent Codex review of PR #577 returned FAIL: the first
review's finding 8 was still open, and the fixes had introduced defects of
their own. All are closed here.

Attribution:
- a multi-step run was costed from its LAST generation alone. A run whose
  steps cost USD 0.10 and USD 0.20 was reported as USD 0.20. The run's cost
  is now the sum of every generation, and the Gateway token counts are
  summed the same way.
- cost_known was emitted only on the failure path, so a successfully priced
  generation was indistinguishable from an unpriced one. It is now emitted on
  both paths, and a partly priced run is a lower bound, not a known total.
- the Gateway response type was re-declared locally and cast. It now comes
  from the SDK own signature, so a field this module reads cannot drift
  from the field the Gateway returns.

Lifecycle:
- run state was allocated once per runtime, so a second run inherited the
  first run tokens and a failure survived into the next run. onStart fires
  exactly once per run and now starts a fresh accumulator.
- every run was recorded as completed. The terminal state now comes from
  onEnd, onError and onAbort.
- a run that failed before its first model call produced no record. It now
  produces a failed record, because it happened.
- two overlapping runs on one runtime cannot be attributed. The runtime says
  so rather than reporting a blended number.

Tools:
- the approval reason said a tool was "not read-only" even when it was
  read-only but open-world. It now names the condition that triggered it.
- the drift policy was inline in loadMcpTools and untested. It is now
  applyToolReviewPolicy, with the missing-baseline refusal, the explicit
  opt-out, and the added-tool case each covered.

Evidence and claims:
- the vendor test asserted constants against identical constants. It now
  checks the Gateway live catalog, and scripts/verify-gateway-models.mjs
  checks that the estate ids are served.
- the README example supplied MCP servers without a baseline, so it now
  rejects. It carries a baseline, and says what to do without one.
- the README and ADR claimed the MCP client handles "session reattach". The
  installed client does not; it does protocol negotiation, header mirroring
  and elicitation. Both documents now say that.
- the ADR claimed core gains the types its JSON Schemas already describe.
  Those schemas describe different shapes. The ADR now says what was added.
- the ADR code example used a route and scope shape that does not exist.

Tests: 42 in the package, up from 19. Each new test can fail against the
defective behaviour.

Copy link
Copy Markdown
Collaborator Author

Cross-stream integration handoff: https://github.com/frankxai/agentic-ops/pull/608, head d921dd5ef8058d63eef062f6563aa9ef948e17a1. Keep this PR as runtime owner. Before hosted execution integration: bind trusted tenant/workspace/principal, reuse web job/ticket/admission + atomic reservation; preserve protected API containment. Current USD attribution is not the existing EUR run-receipt schema: retain original currency and explicit conversion provenance, unknown cost stays unknown. Shared synthetic binding tests are in the integration PR; native runtime/crypto/live tests remain required. This is coordination, not approval or a permission grant.

Committed by storage maintenance so this work exists on a remote instead of only on one laptop. Not reviewed, not merged. Branch preserved.

This branch has not been deployed

No deployments
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