Multiplayer agent harness for work.
Hand Pi the ChatGPT subscription token as OAuth and send bare codex model ids (#1305)
Sina Matianfe3cda053586
diff --git a/src/api/runtime-config.ts b/src/api/runtime-config.tsindex 851b46110..3068f47ba 100644--- a/src/api/runtime-config.ts+++ b/src/api/runtime-config.ts@@ -15,6 +15,7 @@ import {modelUnavailableReason,thinkingLevelsForHarness,harnessSupportsFastMode,+ codexProviderModelId,type HarnessId,} from "../model/pi-models.ts";import { builtInModelCatalog, selectableCatalogForHarness, selectableModelCatalog } from "../model/model-catalog.ts";@@ -159,7 +160,7 @@ export function validateRuntimeChoice(choice: RuntimeChoice): string | null {}export async function webuiModelEnabled(ctx: { deps: RuntimeDeps }, modelId: string): Promise<boolean> {- modelId = modelId.replace(/^codex\//, "");+ modelId = codexProviderModelId(modelId);const config = ctx.deps.config!;const picker = await config.getWebuiModelsDurable(orgScope(ctx.deps));if (picker == null || picker.includes(modelId)) return true;diff --git a/src/core/individual-auth-routing.ts b/src/core/individual-auth-routing.tsindex f5f0e9d61..9e0360b63 100644--- a/src/core/individual-auth-routing.ts+++ b/src/core/individual-auth-routing.ts@@ -1,4 +1,5 @@import {+ codexProviderModelId,codexSubscriptionModelId,CODEX_SUBSCRIPTION_PROVIDER,DEFAULT_AGENT_MODEL_ID,@@ -86,7 +87,7 @@ export function resolveIndividualAuthRouting(harness: "codex",model:requestedModel && requestedProvider === "openai"- ? requestedModel.replace(/^codex\//, "")+ ? codexProviderModelId(requestedModel): defaultModelForHarness("codex", DEFAULT_CODEX_MODEL_ID),};}diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.tsindex 55eba93f9..4a7b60e08 100644--- a/src/harness/pi-harness.ts+++ b/src/harness/pi-harness.ts@@ -60,6 +60,8 @@ import {getRequiredModel,modelSupportsFastMode,contextTokenBudgetForModel,+ CODEX_SUBSCRIPTION_PROVIDER,+ codexProviderModelId,} from "../model/pi-models.ts";import { customModelsJson, customProvidersVersion } from "../model/custom-providers.ts";import { modelGatewayRequest, type ModelGatewayTransportConfig } from "../model/provider-endpoints.ts";@@ -1130,9 +1132,6 @@ export interface ProviderKeys {[provider: string]: string | undefined;}-// buildModelRuntime runs per turn; the models.json only changes when the-// custom-provider registry does, so cache the materialized file per registry-// version instead of leaking a temp dir per turn.let cachedCustomModels: { version: number; path: string | null } | null = null;function customModelsPath(): string | null {const version = customProvidersVersion() + gatewayModelsVersion();@@ -1148,22 +1147,25 @@ function customModelsPath(): string | null {return path;}-async function buildModelRuntime(+export async function buildModelRuntime(keys: ProviderKeys | string,modelGateway?: ModelGatewayTransportConfig,cacheRetention?: "long",): Promise<ModelRuntime> {await modelGateway?.refresh?.();- const k: ProviderKeys = typeof keys === "string" ? { anthropic: keys } : keys;- // Custom providers must exist in the runtime's own registry — a runtime- // API key alone is invisible to its availability checks. models.json is- // the sanctioned vocabulary, so materialize one when any are registered.+ const { [CODEX_SUBSCRIPTION_PROVIDER]: subscriptionToken, ...apiKeys }: ProviderKeys =+ typeof keys === "string" ? { anthropic: keys } : keys;+ const credentials = new InMemoryCredentialStore();+ if (subscriptionToken)+ await credentials.modify(CODEX_SUBSCRIPTION_PROVIDER, async () => ({+ type: "oauth",+ access: subscriptionToken,+ refresh: "",+ expires: Number.MAX_SAFE_INTEGER,+ }));const modelsPath = customModelsPath();- const runtime = await ModelRuntime.create({- credentials: new InMemoryCredentialStore(),- modelsPath,- });- for (const [provider, apiKey] of Object.entries(k)) {+ const runtime = await ModelRuntime.create({ credentials, modelsPath });+ for (const [provider, apiKey] of Object.entries(apiKeys)) {if (apiKey) await runtime.setRuntimeApiKey(provider, apiKey, { allowNetwork: false });}if (modelGateway) {@@ -1189,59 +1191,68 @@ async function buildModelRuntime(}const retained = <T extends object | undefined>(options: T): T =>cacheRetention ? ({ ...options, cacheRetention } as T) : options;- const stream = runtime.stream.bind(runtime);- runtime.stream = (<TApi extends Api>(- model: Model<TApi>,- context: Context,- options?: ModelsApiStreamOptions<TApi>,- ) => {- const request = modelGatewayRequest(modelGateway, model);- if (!request) return stream(model, context, retained(options));- const routedOptions = {- ...retained(options),- apiKey: request.apiKey,- transformHeaders: async (headers: ProviderHeaders) => ({- ...(options?.transformHeaders ? await options.transformHeaders(headers) : headers),- ...request.headers,- }),+ const wireModelId = <T extends Pick<ModelsSimpleStreamOptions, "onPayload"> | undefined>(+ options: T,+ model: Model<Api>,+ target: () => Promise<string>,+ ): T =>+ ({+ ...options,onPayload: async (payload: unknown) => {const transformed = options?.onPayload ? await options.onPayload(payload, model) : undefined;const body = transformed === undefined ? payload : transformed;if (!body || typeof body !== "object" || Array.isArray(body)) {- throw new Error("model gateway request payload must be an object");+ throw new Error("model request payload must be an object");}- await modelGateway?.refresh?.();- const current = modelGatewayRequest(modelGateway, model);- if (!current) throw new Error(`Gateway model is unavailable: ${model.id}`);- return { ...body, model: current.target };+ return { ...body, model: await target() };},- } as unknown as ModelsApiStreamOptions<TApi>;- return stream(request.model, context, routedOptions);- }) as typeof runtime.stream;- const streamSimple = runtime.streamSimple.bind(runtime);- runtime.streamSimple = ((model: Model<Api>, context: Context, options?: ModelsSimpleStreamOptions) => {+ }) as T;+ const route = <T extends ModelsSimpleStreamOptions | undefined>(+ model: Model<Api>,+ options: T,+ ): { model: Model<Api>; options: T } => {const request = modelGatewayRequest(modelGateway, model);- if (!request) return streamSimple(model, context, retained(options));- const routedOptions = {+ if (!request) {+ const providerModelId =+ model.provider === CODEX_SUBSCRIPTION_PROVIDER ? codexProviderModelId(model.id) : model.id;+ const passthrough = retained(options);+ return {+ model,+ options:+ providerModelId === model.id ? passthrough : wireModelId(passthrough, model, async () => providerModelId),+ };+ }+ const routed = {...retained(options),apiKey: request.apiKey,transformHeaders: async (headers: ProviderHeaders) => ({...(options?.transformHeaders ? await options.transformHeaders(headers) : headers),...request.headers,}),- onPayload: async (payload: unknown) => {- const transformed = options?.onPayload ? await options.onPayload(payload, model) : undefined;- const body = transformed === undefined ? payload : transformed;- if (!body || typeof body !== "object" || Array.isArray(body)) {- throw new Error("model gateway request payload must be an object");- }+ } as T;+ return {+ model: request.model,+ options: wireModelId(routed, model, async () => {await modelGateway?.refresh?.();const current = modelGatewayRequest(modelGateway, model);if (!current) throw new Error(`Gateway model is unavailable: ${model.id}`);- return { ...body, model: current.target };- },- } as ModelsSimpleStreamOptions;- return streamSimple(request.model, context, routedOptions);+ return current.target;+ }),+ };+ };+ const stream = runtime.stream.bind(runtime);+ runtime.stream = (<TApi extends Api>(+ model: Model<TApi>,+ context: Context,+ options?: ModelsApiStreamOptions<TApi>,+ ) => {+ const routed = route(model, options as ModelsSimpleStreamOptions | undefined);+ return stream(routed.model as Model<TApi>, context, routed.options as unknown as ModelsApiStreamOptions<TApi>);+ }) as typeof runtime.stream;+ const streamSimple = runtime.streamSimple.bind(runtime);+ runtime.streamSimple = ((model: Model<Api>, context: Context, options?: ModelsSimpleStreamOptions) => {+ const routed = route(model, options);+ return streamSimple(routed.model, context, routed.options);}) as typeof runtime.streamSimple;return runtime;}diff --git a/src/harness/runtime-control.ts b/src/harness/runtime-control.tsindex 23b4586e3..d9001ef5c 100644--- a/src/harness/runtime-control.ts+++ b/src/harness/runtime-control.ts@@ -15,6 +15,7 @@ import {thinkingLevelsForHarness,safeModelMetadata,modelSupportedByHarness,+ codexSubscriptionModelId,} from "../model/pi-models.ts";export function createRuntimeService(deps: RuntimeDeps, app: Pick<App, "authorizesCapabilityScope">): RuntimeService {@@ -36,8 +37,8 @@ export function createRuntimeService(deps: RuntimeDeps, app: Pick<App, "authorizif (snapshot.approvedHarnesses.includes("pi")) {const piModels = snapshot.modelsByHarness.pi ?? [];for (const id of piModels) {- if (id.startsWith("codex/")) continue;- const subscriptionId = `codex/${id}`;+ const subscriptionId = codexSubscriptionModelId(id);+ if (subscriptionId === id) continue;if (modelSupportedByHarness(subscriptionId, "pi") && !piModels.includes(subscriptionId)) {piModels.push(subscriptionId);const metadata = safeModelMetadata(subscriptionId);diff --git a/src/model/pi-models.ts b/src/model/pi-models.tsindex c223e440f..2366f61db 100644--- a/src/model/pi-models.ts+++ b/src/model/pi-models.ts@@ -21,6 +21,9 @@ const CODEX_SUBSCRIPTION_PREFIX = "codex/";export function codexSubscriptionModelId(id: string): string {return id.startsWith(CODEX_SUBSCRIPTION_PREFIX) ? id : CODEX_SUBSCRIPTION_PREFIX + id;}+export function codexProviderModelId(id: string): string {+ return id.startsWith(CODEX_SUBSCRIPTION_PREFIX) ? id.slice(CODEX_SUBSCRIPTION_PREFIX.length) : id;+}export const THINKING_LEVELS = ["auto", "low", "medium", "high", "xhigh", "max", "ultracode"] as const;export const HARNESS_IDS = ["pi", "opencode", "codex", "claude", "mock"] as const;export type HarnessId = (typeof HARNESS_IDS)[number];@@ -370,9 +373,7 @@ export function registerOpenRouterCatalogModel(definition: OpenRouterCatalogModeexport function resolveBuiltinModel(id: string): PiModel | undefined {if (id.startsWith(CODEX_SUBSCRIPTION_PREFIX)) {- const m = getModel(CODEX_SUBSCRIPTION_PROVIDER, id.slice(CODEX_SUBSCRIPTION_PREFIX.length));- // Keep the namespaced id: pi resolves the turn's model by this string,- // and the un-prefixed id belongs to the metered "openai" provider.+ const m = getModel(CODEX_SUBSCRIPTION_PROVIDER, codexProviderModelId(id));return m ? { ...m, id } : undefined;}const entry = REGISTRY_BY_ID.get(id);diff --git a/test/pi-codex-subscription.test.ts b/test/pi-codex-subscription.test.tsnew file mode 100644index 000000000..edcab3799--- /dev/null+++ b/test/pi-codex-subscription.test.ts@@ -0,0 +1,45 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import { zstdDecompressSync } from "node:zlib";+import { buildModelRuntime } from "../src/harness/pi-harness.ts";+import { CODEX_SUBSCRIPTION_PROVIDER, getRequiredModel } from "../src/model/pi-models.ts";++const subscriptionToken = `e30.${Buffer.from(+ JSON.stringify({ "https://api.openai.com/auth": { chatgpt_account_id: "test-account" } }),+).toString("base64url")}.test`;++test("a derived ChatGPT access token authenticates pi-ai's oauth-only codex provider for its own runtime only", async () => {+ const runtime = await buildModelRuntime({ [CODEX_SUBSCRIPTION_PROVIDER]: subscriptionToken });+ assert.equal(runtime.hasConfiguredAuth(CODEX_SUBSCRIPTION_PROVIDER), true);+ assert.equal((await runtime.getAuth(CODEX_SUBSCRIPTION_PROVIDER))?.auth.apiKey, subscriptionToken);+ const metered = await buildModelRuntime({ openai: "sk-metered-test-key" });+ assert.equal(metered.hasConfiguredAuth(CODEX_SUBSCRIPTION_PROVIDER), false);+ assert.equal(await metered.getAuth(CODEX_SUBSCRIPTION_PROVIDER), undefined);+});++test("codex subscription requests reach chatgpt.com with the bare model id on both streaming paths", async (t) => {+ const runtime = await buildModelRuntime({ [CODEX_SUBSCRIPTION_PROVIDER]: subscriptionToken });+ const model = getRequiredModel("codex/gpt-5.6-sol");+ const originalFetch = globalThis.fetch;+ t.after(() => {+ globalThis.fetch = originalFetch;+ });+ for (const method of ["stream", "streamSimple"] as const) {+ let request: { url: string; body: { model?: string } } | undefined;+ globalThis.fetch = (async (url: string | URL | Request, init?: RequestInit) => {+ const encoded = new Headers(init?.headers).get("content-encoding") === "zstd";+ const text = encoded ? zstdDecompressSync(init?.body as Uint8Array).toString() : String(init?.body);+ request = { url: String(url), body: JSON.parse(text) as { model?: string } };+ return new Response(JSON.stringify({ error: { message: "offline test response" } }), { status: 400 });+ }) as typeof fetch;+ const response = await runtime[method](+ model,+ { messages: [{ role: "user", content: "hello", timestamp: 0 }] },+ { transport: "sse", maxRetries: 0 },+ ).result();+ assert.ok(request, `${method} must reach the provider request: ${response.errorMessage ?? "no error"}`);+ assert.match(request.url, /^https:\/\/chatgpt\.com\/backend-api\//);+ assert.equal(request.body.model, "gpt-5.6-sol", `${method} must send the provider's own model id`);+ assert.equal(response.model, "codex/gpt-5.6-sol", `${method} keeps QM's namespaced id on the assistant message`);+ }+});diff --git a/test/pi-models.test.ts b/test/pi-models.test.tsindex 7b9a82dce..38f9f9781 100644--- a/test/pi-models.test.ts+++ b/test/pi-models.test.ts@@ -14,6 +14,8 @@ import {MODEL_PROVIDERS,SELECTABLE_BASE_MODELS,contextTokenBudgetForModel,+ codexProviderModelId,+ codexSubscriptionModelId,} from "../src/model/pi-models.ts";test("every selectable base model resolves against the pi-ai registry", () => {@@ -34,6 +36,17 @@ test("selectable models span providers (multi-provider is wired)", () => {assert.ok(providers.has("openrouter"), "expected an OpenRouter-hosted open-model option");});+test("codex subscription ids stay namespaced inside QM and bare toward the provider", () => {+ assert.equal(codexSubscriptionModelId("gpt-5.6-sol"), "codex/gpt-5.6-sol");+ assert.equal(codexSubscriptionModelId("codex/gpt-5.6-sol"), "codex/gpt-5.6-sol");+ assert.equal(codexProviderModelId("codex/gpt-5.6-sol"), "gpt-5.6-sol");+ assert.equal(codexProviderModelId("gpt-5.6-sol"), "gpt-5.6-sol");+ const subscription = getRequiredModel("codex/gpt-5.6-sol");+ assert.equal(subscription.id, "codex/gpt-5.6-sol");+ assert.equal(String(subscription.provider), "openai-codex");+ assert.equal(getRequiredModel(codexProviderModelId(subscription.id)).provider, "openai");+});+test("unknown models are not silently accepted", () => {assert.equal(resolveModel("claude-not-a-real-model"), undefined);assert.throws(() => getRequiredModel("claude-not-a-real-model"), /Unsupported model/);diff --git a/test/user-model-auth-injection.test.ts b/test/user-model-auth-injection.test.tsindex 375018af0..bcf4a2e0f 100644--- a/test/user-model-auth-injection.test.ts+++ b/test/user-model-auth-injection.test.ts@@ -240,5 +240,6 @@ test("individual OAuth routing preserves exact supported runtime selections", ());assert.equal(resolveIndividualAuthRouting(null, oauth("openai"), "gpt-6-astra", "codex")?.model, "gpt-6-astra");assert.equal(resolveIndividualAuthRouting(null, oauth("openai"), "codex/gpt-5.5", "pi")?.model, "codex/gpt-5.5");+ assert.equal(resolveIndividualAuthRouting(null, oauth("openai"), "codex/gpt-5.5", "codex")?.model, "gpt-5.5");assert.equal(resolveIndividualAuthRouting(null, apikey("openai", "test"), "codex/gpt-5.5", "pi"), null);});