Skip to content

CodexAppServer

Transport for codex app-server: one process per connection, speaking newline-delimited JSON-RPC on stdio.

CodexAppServer.layer().pipe(Layer.provide(NodeServices.layer))

The process is spawned through ChildProcessSpawner, wrapped by the Launcher if one is provided (in the caller’s context or the layer’s), and started in its own process group. Closing the connection’s Scope sends SIGTERM to that group and SIGKILLs it after forceKillAfter.

Codex (0.162.1) runs each command in its own PTY session, so commands are not in that group. They stop because app-server stops them on SIGTERM (verified, including nohup ... & children of a running command).

If app-server has to be SIGKILLed, a running command’s foreground process dies with its PTY, but a nohuped background one survives. Use a Launcher that puts the process in a cgroup (systemd-run --scope) to catch those too.

Since v0.1.0

Spawns codex app-server through ChildProcessSpawner, one process per connection.

Signature

declare const make: (options?: Options) => Effect.Effect<
{
readonly connect: (
options: ConnectOptions,
) => Effect.Effect<Connection, AgentError, Scope.Scope>
readonly version: Effect.Effect<string | null, AgentError>
},
never,
ChildProcessSpawner.ChildProcessSpawner
>

Source

Since v0.1.0

A JSON-RPC error response from app-server.

Signature

declare class CodexRpcError

Source

Since v0.1.0

Signature

declare const layer: (
options?: Options,
) => Layer.Layer<CodexAppServer, never, ChildProcessSpawner.ChildProcessSpawner>

Source

Since v0.1.0

Options for CodexAppServer.connect.

Signature

export interface ConnectOptions {
readonly cwd: string
/** Applied over the host environment; `undefined` removes a variable. */
readonly env?: Readonly<Record<string, string | undefined>>
/** Extra arguments for this connection, after `Options.args`. */
readonly args?: ReadonlyArray<string>
/**
* Answers server-to-client requests. Defaults to `defaultServerRequestHandler`,
* which declines approvals, answers ask-user requests with no answers and
* rejects anything else, so app-server never waits on us.
*/
readonly onServerRequest?: (
request: P.ServerRequest,
) => Effect.Effect<unknown, CodexRpcError>
}

Source

Since v0.1.0

An initialized app-server connection, live or replayed. Closed with its Scope.

Signature

export interface Connection {
/** app-server's pid; null when replaying. */
readonly pid: number | null
readonly initialize: P.InitializeResponse
readonly request: <M extends P.ClientMethod>(
method: M,
params: P.ClientRequests[M]["params"],
) => Effect.Effect<P.ClientRequests[M]["result"], CodexRpcError | AgentError>
/**
* Notifications and server requests in arrival order. Single consumer:
* frames are buffered until read, and the stream ends when app-server's
* stdout closes.
*/
readonly incoming: Stream.Stream<Incoming>
/** Waits for app-server to exit. */
readonly exit: Effect.Effect<ProcessExit>
/** The exit, if app-server has exited. */
readonly exited: Effect.Effect<Option.Option<ProcessExit>>
/** SIGTERM app-server (SIGKILL after the timeout) and wait for it. Idempotent. */
readonly close: Effect.Effect<void>
/**
* Stop a server that stopped responding: SIGTERM its process group, SIGKILL
* after 2 seconds. (The SIGTERM matters: Codex runs each command in its own
* PTY session, outside the group, and only app-server's own shutdown
* reliably stops them; a `nohup`ed background command survives a SIGKILL.)
*/
readonly kill: Effect.Effect<void>
}

Source

Since v0.1.0

A notification or a server-to-client request, in arrival order.

Signature

type Incoming = P.Incoming

Source

Since v0.1.0

Options for make and layer.

Signature

export interface Options {
/** Path to the `codex` binary. Default `codex` (resolved on PATH). */
readonly codexPath?: string
/** Extra arguments after `app-server`, e.g. `["-c", "model=\"o4\""]`. */
readonly args?: ReadonlyArray<string>
/**
* Tee every frame to this JSONL file (`{"dir":"out"|"in","msg":...}` per
* line). A connection truncates the file and records until it closes; one
* that starts while another is recording isn't recorded (so a `status`
* call during a session leaves the session's recording alone).
*/
readonly record?: string
/** How long to wait after SIGTERM before SIGKILL. Default 10 seconds. */
readonly forceKillAfter?: Duration.Input
readonly clientName?: string
readonly clientVersion?: string
}

Source

Since v0.1.0

How app-server exited: an exit code, or the signal that killed it.

Signature

export interface ProcessExit {
readonly exitCode: number | null
readonly signal: string | null
}

Source

Since v0.1.0

Declines approvals and elicitations, answers ask-user requests with no answers and fails anything else with METHOD_NOT_FOUND.

Signature

declare const defaultServerRequestHandler: (
request: P.ServerRequest,
) => Effect.Effect<unknown, CodexRpcError>

Source

Since v0.1.0

Starts app-server connections.

Signature

declare class CodexAppServer

Source

Since v0.1.0