mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-04 06:34:06 +00:00
Retire timed-out Codex app-server clients with lease-aware cleanup and keep harness-owned timeouts out of provider fallback.
94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import type { resolveCodexAppServerAuthProfileIdForAgent } from "./auth-bridge.js";
|
|
import type { CodexAppServerStartOptions } from "./config.js";
|
|
import type {
|
|
CodexAppServerRequestMethod,
|
|
CodexAppServerRequestParams,
|
|
CodexAppServerRequestResult,
|
|
JsonValue,
|
|
} from "./protocol.js";
|
|
import { resolveCodexAppServerDirectSandboxBypassBlock } from "./sandbox-guard.js";
|
|
import {
|
|
createIsolatedCodexAppServerClient,
|
|
getLeasedSharedCodexAppServerClient,
|
|
releaseLeasedSharedCodexAppServerClient,
|
|
} from "./shared-client.js";
|
|
import { withTimeout } from "./timeout.js";
|
|
|
|
export async function requestCodexAppServerJson<M extends CodexAppServerRequestMethod>(params: {
|
|
method: M;
|
|
requestParams: CodexAppServerRequestParams<M>;
|
|
timeoutMs?: number;
|
|
startOptions?: CodexAppServerStartOptions;
|
|
authProfileId?: string | null;
|
|
agentDir?: string;
|
|
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
|
sessionKey?: string;
|
|
sessionId?: string;
|
|
isolated?: boolean;
|
|
}): Promise<CodexAppServerRequestResult<M>>;
|
|
export async function requestCodexAppServerJson<T = JsonValue | undefined>(params: {
|
|
method: string;
|
|
requestParams?: unknown;
|
|
timeoutMs?: number;
|
|
startOptions?: CodexAppServerStartOptions;
|
|
authProfileId?: string | null;
|
|
agentDir?: string;
|
|
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
|
sessionKey?: string;
|
|
sessionId?: string;
|
|
isolated?: boolean;
|
|
}): Promise<T>;
|
|
export async function requestCodexAppServerJson<T = JsonValue | undefined>(params: {
|
|
method: string;
|
|
requestParams?: unknown;
|
|
timeoutMs?: number;
|
|
startOptions?: CodexAppServerStartOptions;
|
|
authProfileId?: string | null;
|
|
agentDir?: string;
|
|
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
|
sessionKey?: string;
|
|
sessionId?: string;
|
|
isolated?: boolean;
|
|
}): Promise<T> {
|
|
const sandboxBlock = resolveCodexAppServerDirectSandboxBypassBlock({
|
|
method: params.method,
|
|
requestParams: params.requestParams,
|
|
config: params.config,
|
|
sessionKey: params.sessionKey,
|
|
sessionId: params.sessionId,
|
|
});
|
|
if (sandboxBlock) {
|
|
throw new Error(sandboxBlock);
|
|
}
|
|
const timeoutMs = params.timeoutMs ?? 60_000;
|
|
return await withTimeout(
|
|
(async () => {
|
|
const client = await (
|
|
params.isolated ? createIsolatedCodexAppServerClient : getLeasedSharedCodexAppServerClient
|
|
)({
|
|
startOptions: params.startOptions,
|
|
timeoutMs,
|
|
authProfileId: params.authProfileId,
|
|
agentDir: params.agentDir,
|
|
config: params.config,
|
|
});
|
|
try {
|
|
return await client.request<T>(params.method, params.requestParams, { timeoutMs });
|
|
} finally {
|
|
if (params.isolated) {
|
|
// Wait for the child to actually exit (with a SIGKILL fallback) so
|
|
// the parent process doesn't hang on an orphaned codex app-server.
|
|
// The stdio bin shim does not always propagate stdin EOF to the
|
|
// underlying codex binary, so the unref'd close() path can leave
|
|
// the child running and keep the parent's event loop alive.
|
|
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 });
|
|
} else {
|
|
releaseLeasedSharedCodexAppServerClient(client);
|
|
}
|
|
}
|
|
})(),
|
|
timeoutMs,
|
|
`codex app-server ${params.method} timed out`,
|
|
);
|
|
}
|