Skip to content

Commit abecdb0

Browse files
committed
fix(codex): keep user-home app-servers on native Codex auth
A prepared OpenClaw auth handoff was still resolved for connections that target the operator's native Codex home, so every turn failed with "Prepared Codex auth requires an isolated app-server home." once a stored OpenAI profile produced a prepared model route. User-home mode now keeps its native account for both subscription and Platform routes, matching the documented contract; the shared-client guard stays as the backstop.
1 parent 2242b96 commit abecdb0

9 files changed

Lines changed: 111 additions & 4 deletions

File tree

‎docs/plugins/codex-harness.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ user Codex home:
141141
User-home mode supports a local managed stdio process or the shared Unix-socket
142142
transport. It uses `$CODEX_HOME` when set and `~/.codex` otherwise, including
143143
that home's native Codex auth, config, plugins, and thread store. OpenClaw does
144-
not inject an OpenClaw auth profile into this app-server.
144+
not inject an OpenClaw auth profile into this app-server, even when the agent's
145+
model route has a stored OpenAI profile: a subscription route is verified
146+
against the native account instead. Log in with Codex itself if a turn reports
147+
missing subscription credentials.
145148

146149
Owner turns gain the `codex_threads` tool: list, search, read, fork, rename,
147150
archive, and restore native threads. Fork a thread to continue it in

‎extensions/codex/src/app-server/auth-bridge.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,44 @@ describe("bridgeCodexAppServerStartOptions", () => {
604604
});
605605
});
606606

607+
it("declines every prepared handoff for a user-home app-server", async () => {
608+
const authProfileStore: AuthProfileStore = {
609+
version: 1,
610+
profiles: {
611+
"openai:work": {
612+
type: "token",
613+
provider: "openai",
614+
token: "prepared-subscription-token",
615+
616+
},
617+
},
618+
};
619+
620+
await expect(
621+
resolveCodexAppServerPreparedAuthHandoff({
622+
authRequirement: "subscription",
623+
authProfileId: "openai:work",
624+
authProfileStore,
625+
agentDir: "/tmp/openclaw-agent",
626+
homeScope: "user",
627+
subscriptionProfileRequiredError: "profile required",
628+
subscriptionProfileUnusableError: "profile unusable",
629+
}),
630+
).resolves.toEqual({ authProfileId: "openai:work", nativeAuthProfile: true });
631+
632+
await expect(
633+
resolveCodexAppServerPreparedAuthHandoff({
634+
authRequirement: "api-key",
635+
authProfileId: "openai:work",
636+
authProfileStore,
637+
agentDir: "/tmp/openclaw-agent",
638+
homeScope: "user",
639+
subscriptionProfileRequiredError: "profile required",
640+
subscriptionProfileUnusableError: "profile unusable",
641+
}),
642+
).resolves.toEqual({ authProfileId: "openai:work", nativeAuthProfile: true });
643+
});
644+
607645
it("materializes one prepared subscription profile snapshot", async () => {
608646
const authProfileStore: AuthProfileStore = {
609647
version: 1,

‎extensions/codex/src/app-server/auth-bridge.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ensureCodexComputerUseServiceApp } from "./computer-use-service.js";
2929
import {
3030
resolveCodexAppServerUserHomeDir,
3131
resolveCodexComputerUseConfig,
32+
type CodexAppServerHomeScope,
3233
type CodexAppServerStartOptions,
3334
} from "./config.js";
3435
import {
@@ -259,11 +260,17 @@ export async function resolveCodexAppServerPreparedAuthHandoff(params: {
259260
authProfileId?: string;
260261
authProfileStore: AuthProfileStore;
261262
agentDir?: string;
263+
homeScope?: CodexAppServerHomeScope;
262264
config?: AuthProfileOrderConfig;
263265
subscriptionProfileRequiredError: string;
264266
subscriptionProfileUnusableError: string;
265267
}) {
266-
if (params.authRequirement === "api-key") {
268+
// A user-home app-server owns the operator's native Codex account. Codex persists
269+
// api-key logins into CODEX_HOME/auth.json and swaps the live account for external
270+
// token logins, so a prepared OpenClaw handoff here would rewrite the account that
271+
// Codex CLI and Desktop share. Native homes are verified, never logged into.
272+
const usesNativeHome = params.homeScope === "user";
273+
if (params.authRequirement === "api-key" && !usesNativeHome) {
267274
const apiKey = params.resolvedApiKey?.trim();
268275
if (!apiKey) {
269276
throw new Error("Prepared Codex API-key route is missing its resolved API key.");
@@ -281,7 +288,7 @@ export async function resolveCodexAppServerPreparedAuthHandoff(params: {
281288
agentDir: params.agentDir,
282289
config: params.config,
283290
});
284-
if (params.authRequirement !== "subscription") {
291+
if (usesNativeHome || params.authRequirement !== "subscription") {
285292
return { authProfileId, nativeAuthProfile };
286293
}
287294
if (!authProfileId || !nativeAuthProfile) {

‎extensions/codex/src/app-server/auth-profile-runtime-contract.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,51 @@ describe("Auth profile runtime contract - Codex app-server adapter", () => {
489489
await run;
490490
});
491491

492+
it.each([
493+
{ label: "a subscription route", authRequirement: "subscription" as const },
494+
{ label: "a Platform route", authRequirement: "api-key" as const },
495+
])(
496+
"keeps a user-home app-server on native Codex auth for $label",
497+
async ({ authRequirement }) => {
498+
const harness = createCodexAuthProfileHarness({ startMethod: "thread/start" });
499+
const sessionFile = path.join(tmpDir, "session.jsonl");
500+
const params = createParams(sessionFile, tmpDir);
501+
params.agentDir = tmpDir;
502+
params.authProfileStore = {
503+
version: 1,
504+
profiles: {
505+
"openai:chatgpt": {
506+
type: "oauth",
507+
provider: "openai",
508+
access: "subscription-token",
509+
refresh: "refresh-token",
510+
expires: Date.now() + 60 * 60_000,
511+
},
512+
},
513+
order: { openai: ["openai:chatgpt"] },
514+
};
515+
setPreparedOpenAIRoute(params, authRequirement, "openai:chatgpt");
516+
517+
const run = runCodexAppServerAttempt(params, {
518+
pluginConfig: {
519+
appServer: { homeScope: "user" },
520+
supervision: { enabled: true },
521+
},
522+
});
523+
await vi.waitFor(
524+
() => expect(harness.seenClientOptions).toHaveLength(1),
525+
APP_SERVER_START_WAIT,
526+
);
527+
expect(harness.seenClientOptions[0]).not.toHaveProperty("preparedAuth");
528+
expect(harness.seenClientOptions[0]).toMatchObject({
529+
startOptions: expect.objectContaining({ homeScope: "user" }),
530+
});
531+
await harness.waitForMethod("turn/start");
532+
await harness.completeTurn();
533+
await run;
534+
},
535+
);
536+
492537
it("fails before profile selection when a prepared Platform route has no key", async () => {
493538
const harness = createCodexAuthProfileHarness({ startMethod: "thread/start" });
494539
const sessionFile = path.join(tmpDir, "session.jsonl");

‎extensions/codex/src/app-server/compact.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,12 @@ async function compactCodexNativeThread(
558558
const shouldReleaseDefaultLease = !options.clientFactory;
559559
const clientFactory = options.clientFactory ?? getLeasedSharedCodexAppServerClient;
560560
const runtimeAuthPlan = params.runtimeAuthPlan ?? params.runtimePlan?.auth;
561+
// A user-home app-server keeps its native Codex account; injecting a prepared key
562+
// would rewrite the CODEX_HOME auth that Codex CLI and Desktop share.
561563
const usesPreparedApiKey =
562-
!usesSupervisionConnection && runtimeAuthPlan?.modelRoute?.authRequirement === "api-key";
564+
!usesSupervisionConnection &&
565+
appServer.start.homeScope !== "user" &&
566+
runtimeAuthPlan?.modelRoute?.authRequirement === "api-key";
563567
const preparedApiKey = usesPreparedApiKey ? params.resolvedApiKey?.trim() : undefined;
564568
if (usesPreparedApiKey && !preparedApiKey) {
565569
return {

‎extensions/codex/src/app-server/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
export type {
77
CodexAppServerApprovalPolicy,
88
CodexAppServerConnectionClass,
9+
CodexAppServerHomeScope,
910
CodexAppServerRuntimeOptions,
1011
CodexAppServerSandboxMode,
1112
CodexAppServerStartOptions,

‎extensions/codex/src/app-server/run-attempt-connection.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ export async function prepareCodexAttemptConnection({ params, options }: CodexRu
201201
authProfileId: resolvedStartupAuthProfileId,
202202
authProfileStore: params.authProfileStore,
203203
agentDir,
204+
...(pluginConfig.appServer?.homeScope
205+
? { homeScope: pluginConfig.appServer.homeScope }
206+
: {}),
204207
config: params.config,
205208
subscriptionProfileRequiredError:
206209
"Prepared Codex subscription route requires a forwarded OpenAI OAuth or token profile.",

‎extensions/codex/src/app-server/shared-client.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ async function resolveCodexAppServerClientStartContext(
298298
throw new Error("Prepared Codex API-key auth is missing its resolved key.");
299299
}
300300
if (preparedAuth && requestedStartOptions.homeScope === "user") {
301+
// Backstop for the auth-bridge handoff: an app-server on the operator's native
302+
// home persists api-key logins into CODEX_HOME/auth.json and replaces the live
303+
// ChatGPT account for token logins, so prepared auth must never reach it.
301304
throw new Error("Prepared Codex auth requires an isolated app-server home.");
302305
}
303306
const preparedAuthRequirement = inferAuthRequirement(preparedAuth);

‎extensions/codex/src/app-server/side-question.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ export async function runCodexAppServerSideQuestion(
199199
authProfileId: preparedRuntimeAuth.plan.forwardedAuthProfileId,
200200
authProfileStore: preparedRuntimeAuth.authProfileStore,
201201
agentDir: params.agentDir,
202+
...(pluginConfig.appServer?.homeScope
203+
? { homeScope: pluginConfig.appServer.homeScope }
204+
: {}),
202205
config: params.cfg,
203206
subscriptionProfileRequiredError:
204207
"Prepared Codex subscription route requires a scoped native OAuth or token profile.",

0 commit comments

Comments
 (0)