Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/codex_preflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
MINIMUM_SUPPORTED_CODEX_CLI_VERSION,
readCodexLoginStatus,
} from "./codex_preflight.ts";
import {
RUNTIME_HOST_SERVICE_SOCKET_ENV,
RUNTIME_HOST_SERVICE_TOKEN_ENV,
} from "./runtime_host_service.ts";

const SUPPORTED_FAKE_CODEX_VERSION = MINIMUM_SUPPORTED_CODEX_CLI_VERSION;
const UNSUPPORTED_FAKE_CODEX_VERSION = "0.120.0";
Expand Down Expand Up @@ -113,6 +117,8 @@ done
Deno.test("codex preflight responds to app-server refresh RPCs before account/read completes", async () => {
const priorBin = Deno.env.get("GAMBIT_CODEX_BIN");
const priorBundle = Deno.env.get(CODEX_HOST_AUTH_BUNDLE_ENV);
const priorHostServiceSocket = Deno.env.get(RUNTIME_HOST_SERVICE_SOCKET_ENV);
const priorHostServiceToken = Deno.env.get(RUNTIME_HOST_SERVICE_TOKEN_ENV);
const root = await Deno.makeTempDir({
prefix: "codex-preflight-refresh-rpc-",
});
Expand Down Expand Up @@ -172,6 +178,8 @@ done
await Deno.chmod(fakeCodexPath, 0o755);

Deno.env.set("GAMBIT_CODEX_BIN", fakeCodexPath);
Deno.env.set(RUNTIME_HOST_SERVICE_SOCKET_ENV, join(root, "missing.sock"));
Deno.env.delete(RUNTIME_HOST_SERVICE_TOKEN_ENV);
Deno.env.set(
CODEX_HOST_AUTH_BUNDLE_ENV,
JSON.stringify({
Expand Down Expand Up @@ -219,6 +227,16 @@ done
} else {
Deno.env.set(CODEX_HOST_AUTH_BUNDLE_ENV, priorBundle);
}
if (priorHostServiceSocket == null) {
Deno.env.delete(RUNTIME_HOST_SERVICE_SOCKET_ENV);
} else {
Deno.env.set(RUNTIME_HOST_SERVICE_SOCKET_ENV, priorHostServiceSocket);
}
if (priorHostServiceToken == null) {
Deno.env.delete(RUNTIME_HOST_SERVICE_TOKEN_ENV);
} else {
Deno.env.set(RUNTIME_HOST_SERVICE_TOKEN_ENV, priorHostServiceToken);
}
await Deno.remove(root, { recursive: true }).catch(() => undefined);
}
});
Expand Down
25 changes: 13 additions & 12 deletions src/codex_preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CODEX_REFRESH_HOST_SERVICE_METHOD,
type CodexRefreshHostServiceResult,
RUNTIME_HOST_SERVICE_SOCKET_ENV,
RUNTIME_HOST_SERVICE_TOKEN_ENV,
} from "./runtime_host_service.ts";

const CODEX_BIN_ENV = "GAMBIT_CODEX_BIN";
Expand Down Expand Up @@ -75,6 +76,13 @@ function buildUnsupportedCodexVersionMessage(version: string | null): string {
return `Codex CLI ${rendered} is too old; require >= ${MINIMUM_SUPPORTED_CODEX_CLI_VERSION} for Gambit's app-server transport.`;
}

function hasRuntimeHostServiceRefreshConfig(): boolean {
return Boolean(
Deno.env.get(RUNTIME_HOST_SERVICE_SOCKET_ENV)?.trim() &&
Deno.env.get(RUNTIME_HOST_SERVICE_TOKEN_ENV)?.trim(),
);
}

async function appServerPreflightRequestResult(input: {
bundle: NonNullable<ReturnType<typeof readCodexAuthBundleFromEnv>>;
method: string;
Expand All @@ -95,17 +103,10 @@ async function appServerPreflightRequestResult(input: {
typeof input.params.reason === "string" && input.params.reason
? input.params.reason
: "account/chatgptAuthTokens/refresh";
const hostServiceSocket = Deno.env.get(RUNTIME_HOST_SERVICE_SOCKET_ENV)
?.trim();
const hostRefreshed = hostServiceSocket
? await callRuntimeHostService({
method: CODEX_REFRESH_HOST_SERVICE_METHOD,
params: {
previousAccountId,
reason,
},
})
: null;
const hostRefreshed = await refreshCodexPreflightViaHost({
previousAccountId,
reason,
});
const refreshed = hostRefreshed
? {
...input.bundle,
Expand Down Expand Up @@ -143,7 +144,7 @@ async function refreshCodexPreflightViaHost(input: {
previousAccountId?: string | null;
reason: string;
}): Promise<CodexRefreshHostServiceResult | null> {
if (!Deno.env.get(RUNTIME_HOST_SERVICE_SOCKET_ENV)?.trim()) return null;
if (!hasRuntimeHostServiceRefreshConfig()) return null;
return await callRuntimeHostService({
method: CODEX_REFRESH_HOST_SERVICE_METHOD,
params: input,
Expand Down
Loading