Skip to content

Commit 22efdfa

Browse files
committed
docs: document codex app-server runtime utilities
1 parent b91ed08 commit 22efdfa

7 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎extensions/codex/src/app-server/managed-binary.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Resolves the managed Codex app-server binary shipped with or installed beside
3+
* the Codex plugin before stdio startup.
4+
*/
15
import { constants as fsConstants, readFileSync } from "node:fs";
26
import { access } from "node:fs/promises";
37
import { createRequire } from "node:module";
@@ -20,6 +24,7 @@ type ResolveManagedCodexAppServerOptions = {
2024
pathExists?: (filePath: string, platform: NodeJS.Platform) => Promise<boolean>;
2125
};
2226

27+
/** Rewrites managed stdio start options to point at an executable Codex binary path. */
2328
export async function resolveManagedCodexAppServerStartOptions(
2429
startOptions: CodexAppServerStartOptions,
2530
options: ResolveManagedCodexAppServerOptions = {},
@@ -47,6 +52,7 @@ export async function resolveManagedCodexAppServerStartOptions(
4752
};
4853
}
4954

55+
/** Returns the preferred and fallback managed Codex binary paths for a plugin root. */
5056
export function resolveManagedCodexAppServerPaths(params: {
5157
platform?: NodeJS.Platform;
5258
pluginRoot?: string;
@@ -170,6 +176,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
170176
return typeof value === "object" && value !== null;
171177
}
172178

179+
/** Internal helpers exposed for managed-binary path-resolution tests. */
173180
export const testing = {
174181
resolveDefaultCodexPluginRoot,
175182
};

‎extensions/codex/src/app-server/sandbox-guard.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Blocks direct Codex app-server requests that would bypass OpenClaw sandbox or
3+
* node-exec routing guarantees.
4+
*/
15
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
26
import { resolveSandboxRuntimeStatus } from "openclaw/plugin-sdk/sandbox";
37
import {
@@ -65,6 +69,7 @@ const NODE_EXEC_BLOCKED_CONTROL_PLANE_METHODS = new Set<string>([
6569
"config/mcpServer/reload",
6670
]);
6771

72+
/** Returns a block message when a direct app-server method would bypass OpenClaw execution policy. */
6873
export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
6974
method: string;
7075
requestParams?: unknown;
@@ -117,6 +122,7 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
117122
return sandboxBlock;
118123
}
119124

125+
/** Resolves the generic native-execution block for sandboxed or node-hosted sessions. */
120126
export function resolveCodexNativeExecutionBlock(params: {
121127
config?: OpenClawConfig;
122128
sessionKey?: string;
@@ -126,6 +132,7 @@ export function resolveCodexNativeExecutionBlock(params: {
126132
return resolveCodexNativeSandboxBlock(params) ?? resolveCodexNativeNodeExecBlock(params);
127133
}
128134

135+
/** Returns a block message when native Codex execution cannot honor active sandboxing. */
129136
export function resolveCodexNativeSandboxBlock(params: {
130137
config?: OpenClawConfig;
131138
sessionKey?: string;

‎extensions/codex/src/app-server/session-binding.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Persists and normalizes the Codex app-server thread binding associated with
3+
* an OpenClaw session file.
4+
*/
15
import fs from "node:fs/promises";
26
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
37
import {
@@ -21,13 +25,15 @@ const PUBLIC_OPENAI_MODEL_PROVIDER = "openai";
2125
type ProviderAuthAliasLookupParams = Parameters<typeof resolveProviderIdForAuth>[1];
2226
type ProviderAuthAliasConfig = NonNullable<ProviderAuthAliasLookupParams>["config"];
2327

28+
/** Inputs needed to resolve whether a binding's auth profile is native Codex/OpenAI auth. */
2429
export type CodexAppServerAuthProfileLookup = {
2530
authProfileId?: string;
2631
authProfileStore?: AuthProfileStore;
2732
agentDir?: string;
2833
config?: ProviderAuthAliasConfig;
2934
};
3035

36+
/** Durable sidecar binding connecting an OpenClaw session file to a Codex thread. */
3137
export type CodexAppServerThreadBinding = {
3238
schemaVersion: 1;
3339
threadId: string;
@@ -53,24 +59,28 @@ export type CodexAppServerThreadBinding = {
5359
updatedAt: string;
5460
};
5561

62+
/** Context-engine state persisted with a Codex app-server thread binding. */
5663
export type CodexAppServerContextEngineBinding = {
5764
schemaVersion: 1;
5865
engineId: string;
5966
policyFingerprint: string;
6067
projection?: CodexAppServerContextEngineProjectionBinding;
6168
};
6269

70+
/** Context-engine projection metadata used to guard resumed native threads. */
6371
export type CodexAppServerContextEngineProjectionBinding = {
6472
schemaVersion: 1;
6573
mode: "thread_bootstrap";
6674
epoch: string;
6775
fingerprint?: string;
6876
};
6977

78+
/** Returns the JSON sidecar path for the Codex app-server binding beside a session file. */
7079
export function resolveCodexAppServerBindingPath(sessionFile: string): string {
7180
return `${sessionFile}.codex-app-server.json`;
7281
}
7382

83+
/** Reads and normalizes a Codex app-server binding sidecar, returning undefined on stale data. */
7484
export async function readCodexAppServerBinding(
7585
sessionFile: string,
7686
lookup: Omit<CodexAppServerAuthProfileLookup, "authProfileId"> = {},
@@ -148,6 +158,7 @@ export async function readCodexAppServerBinding(
148158
}
149159
}
150160

161+
/** Writes the Codex app-server binding sidecar with normalized provider/auth metadata. */
151162
export async function writeCodexAppServerBinding(
152163
sessionFile: string,
153164
binding: Omit<
@@ -293,6 +304,7 @@ function readPluginAppPolicyContext(value: unknown): PluginAppPolicyContext | un
293304
};
294305
}
295306

307+
/** Removes the Codex app-server binding sidecar if present. */
296308
export async function clearCodexAppServerBinding(
297309
sessionFile: string,
298310
_lookup: Omit<CodexAppServerAuthProfileLookup, "authProfileId"> = {},
@@ -306,6 +318,7 @@ export async function clearCodexAppServerBinding(
306318
}
307319
}
308320

321+
/** Clears a binding only when it still points at the expected Codex thread id. */
309322
export async function clearCodexAppServerBindingForThread(
310323
sessionFile: string,
311324
threadId: string,
@@ -331,6 +344,7 @@ function isNotFound(error: unknown): boolean {
331344
return Boolean(error && typeof error === "object" && "code" in error && error.code === "ENOENT");
332345
}
333346

347+
/** Returns true when an auth profile uses native Codex/OpenAI app-server auth. */
334348
export function isCodexAppServerNativeAuthProfile(
335349
lookup: CodexAppServerAuthProfileLookup,
336350
): boolean {
@@ -356,6 +370,7 @@ export function isCodexAppServerNativeAuthProfile(
356370
}
357371
}
358372

373+
/** Hides redundant OpenAI provider attribution for native Codex auth bindings. */
359374
export function normalizeCodexAppServerBindingModelProvider(params: {
360375
authProfileId?: string;
361376
modelProvider?: string;

‎extensions/codex/src/app-server/startup-binding.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Guards Codex app-server thread reuse during startup by rotating bindings when
3+
* native transcripts exceed byte or token budgets.
4+
*/
15
import type { Dirent } from "node:fs";
26
import fs from "node:fs/promises";
37
import path from "node:path";
@@ -320,6 +324,7 @@ function hasContextEngineThreadBootstrapProjection(binding: CodexAppServerThread
320324
return binding.contextEngine?.projection?.mode === "thread_bootstrap";
321325
}
322326

327+
/** Clears and drops a binding when the native Codex thread is too large to resume safely. */
323328
export async function rotateOversizedCodexAppServerStartupBinding(params: {
324329
binding: CodexAppServerThreadBinding | undefined;
325330
sessionFile: string;
@@ -426,6 +431,7 @@ export async function rotateOversizedCodexAppServerStartupBinding(params: {
426431
return binding;
427432
}
428433

434+
/** Internal sizing helpers exposed for startup-binding regression tests. */
429435
export const testing = {
430436
parseCodexAppServerByteLimit,
431437
readCodexAppServerRolloutTokenSnapshotLine,

‎extensions/codex/src/app-server/transport-stdio.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Creates and configures stdio-backed Codex app-server transports, including
3+
* Windows spawn normalization and environment filtering.
4+
*/
15
import { spawn } from "node:child_process";
26
import {
37
materializeWindowsSpawnProgram,
@@ -20,6 +24,7 @@ const DEFAULT_SPAWN_RUNTIME: CodexAppServerSpawnRuntime = {
2024
execPath: process.execPath,
2125
};
2226

27+
/** Resolves the concrete command/argv/shell settings used to spawn Codex app-server. */
2328
export function resolveCodexAppServerSpawnInvocation(
2429
options: CodexAppServerStartOptions,
2530
runtime: CodexAppServerSpawnRuntime = DEFAULT_SPAWN_RUNTIME,
@@ -43,6 +48,7 @@ export function resolveCodexAppServerSpawnInvocation(
4348
};
4449
}
4550

51+
/** Merges app-server environment overrides while honoring clearEnv and unsafe key filtering. */
4652
export function resolveCodexAppServerSpawnEnv(
4753
options: Pick<CodexAppServerStartOptions, "env" | "clearEnv">,
4854
baseEnv: NodeJS.ProcessEnv = process.env,
@@ -90,6 +96,7 @@ function copySafeEnvironmentEntries(
9096
}
9197
}
9298

99+
/** Spawns the Codex app-server process and returns the shared transport interface. */
93100
export function createStdioTransport(options: CodexAppServerStartOptions): CodexAppServerTransport {
94101
const env = resolveCodexAppServerSpawnEnv(options);
95102
const invocation = resolveCodexAppServerSpawnInvocation(options, {

‎extensions/codex/src/app-server/transport-websocket.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
/**
2+
* Adapts a remote Codex app-server WebSocket endpoint to the shared stdio-like
3+
* transport interface.
4+
*/
15
import { EventEmitter } from "node:events";
26
import { PassThrough, Writable } from "node:stream";
37
import WebSocket, { type RawData } from "ws";
48
import type { CodexAppServerStartOptions } from "./config.js";
59
import type { CodexAppServerTransport } from "./transport.js";
610

11+
/** Opens a WebSocket app-server transport and maps newline-delimited frames to stdout/stdin. */
712
export function createWebSocketTransport(
813
options: CodexAppServerStartOptions,
914
): CodexAppServerTransport {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Shared transport lifecycle helpers for stdio and WebSocket Codex app-server
3+
* connections.
4+
*/
5+
/** Child-process-like transport shape consumed by the Codex app-server client. */
16
export type CodexAppServerTransport = {
27
stdin: {
38
write: (data: string, callback?: (error?: Error | null) => void) => unknown;
@@ -24,6 +29,7 @@ export type CodexAppServerTransport = {
2429
off?: (event: string, listener: (...args: unknown[]) => void) => unknown;
2530
};
2631

32+
/** Starts graceful transport shutdown and schedules a force kill fallback. */
2733
export function closeCodexAppServerTransport(
2834
child: CodexAppServerTransport,
2935
options: { forceKillDelayMs?: number } = {},
@@ -52,6 +58,7 @@ export function closeCodexAppServerTransport(
5258
child.stdin.unref?.();
5359
}
5460

61+
/** Closes a transport and waits briefly for an exit event. */
5562
export async function closeCodexAppServerTransportAndWait(
5663
child: CodexAppServerTransport,
5764
options: { exitTimeoutMs?: number; forceKillDelayMs?: number } = {},

0 commit comments

Comments
 (0)