@anureo/adapter-codex
v0.3.16
Published
Anureo SDK adapter for the Codex CLI app-server protocol over stdio JSONL
Maintainers
Readme
@anureo/adapter-codex
Adapter connecting the Anureo SDK to the Codex CLI's app-server mode over stdio JSONL. It spawns the binary, speaks Codex's native protocol, and maps requests, events, items, and usage onto the unified AHP session model.
Part of the Anureo SDK workspace. Install with
npm install @anureo/adapter-codex.
Usage
This shows the revised workspace API, not the published 0.2.0 API. The lifecycle revision updates AHP v1 and package root entries in place. The root implementation and tests use the revised lifecycle; parallel versioned entries have been removed. Root migration and local validation are complete, including passing local conformance and clean-install checks.
import { createClient } from '@anureo/sdk'
import { createCodexAdapter } from '@anureo/adapter-codex'
const client = await createClient({
adapter: createCodexAdapter({
command: 'codex', // required; args default to ['app-server']
cwd: '/workspace',
env: { OPENAI_API_KEY: '...' },
}),
identity: { adapterId: 'codex', runtimeConfigId: 'local' },
})
try {
const session = await client.sessions.create({ cwd: '/workspace' })
const turn = await session.submit({ input: 'Summarize this repository.' })
console.log(await turn.result())
} finally {
await client.dispose()
}The adapter probes the binary's version during initialization (or trusts an explicit options.version from a host probe) and derives its capabilities from a native behavior profile.
Version profiles
Codex's native protocol changes between releases. The adapter selects the newest profile whose minimum version is no higher than the reported version. Below 0.148.0, nothing is known about the native schema: only the safe baseline capability set is published.
| Profile since | Native methods used |
|---|---|
| 0.148.0 | thread/read turns, thread/rollback revert, history from turns, per-turn settings |
| 0.164.0 | thread/turns/list, thread/revert(beforeTurnId), paged thread/items/list, live thread/settings/update |
- Versions
0.148.0and0.164.0are verified against the pinned native schemas inprotocol/—bun run protocol:check(also wired as the workspace-levelprotocol:check) validates their hashes and provenance on every run. - Any other version is reported as unverified; optional native methods that the runtime rejects as method-not-found are closed at call time, not guessed from the version number.
Use resolveCodexProfile(version) and codexCapabilities(version) to inspect the mapping without spawning a process.
Exports
| Entry | Contents |
|---|---|
| @anureo/adapter-codex | createCodexAdapter, CodexNativeAdapter (the factory result implementation), CodexAdapterOptions, CodexProfile, resolveCodexProfile, codexCapabilities, createCodexCatalogSession, CodexModel, CodexThreadSummary, CodexThreadListParams, CodexThreadPage, CodexJsonlClient, CodexRpcError, mapItems, mapUsage |
| @anureo/adapter-codex/jsonl | The low-level JSONL client over the child process |
| @anureo/adapter-codex/mapping | Native record → AHP item/usage mapping helpers |
| @anureo/adapter-codex/catalog | Model catalog session (createCodexCatalogSession) |
CodexModel preserves the native ID and display name, optional picker description, inputModalities, default/hidden flags, supported efforts with descriptions, defaultEffort, and serviceTiers. These describe discovery results, not account prices, measured quality, or the effective settings of a running thread. Applications may use them to build model selection guidance without inferring capability from model names.
CodexAdapterOptions extends the JSONL options (timeoutMs, maxFrameBytes, maxPending, maxServerRequests) with command, args, cwd, env, version, processFactory (for injected transports and tests), diagnostic, and maxHistoryItems.
Code structure
| Path | Responsibility |
|---|---|
| src/index.ts | Assembles the package root exports. The root also re-exports CodexNativeAdapter, the implementation returned by createCodexAdapter(). |
| src/adapter.ts, src/options.ts | Own adapter initialization, native client ownership and host launch configuration. |
| src/session.ts | Dispatches session requests and routes native notifications into SDK events. |
| src/jsonl.ts | Implements the ./jsonl entry: request correlation, server requests and stdio connection cleanup. |
| src/mapping.ts, src/usage.ts, src/file-changes.ts | Implement item, usage and file-change projections; ./mapping exposes the mapping helpers. |
| src/catalog.ts, src/models.ts, src/account.ts | Own the ./catalog query lifecycle and model/account projections. |
| src/profile.ts, src/version.ts, src/settings.ts | Select native behavior profiles, probe executable versions and translate session settings. |
| protocol/, scripts/protocol-check.ts | Retain and verify pinned native schemas and their provenance. |
Configuration options
The adapter exposes session-scoped config options through AHP: permissionMode (read-only / agent / agent-full-access, mapped onto Codex approval policies and sandboxes), model, effort, summary, approvalPolicy, and fastMode (mapped to Codex's priority service tier). Turn-level overrides are supported for model, effort, summary, approval/sandbox policy, and service tier.
Subagent protocol
Codex spawns subagent threads from the model through collab tools (there is no client-side spawn method). The adapter surfaces them on three planes, gated by the subagents capability (events: 'nested', plus threadDiscovery and tree flags per profile):
- Nested items.
collabAgentToolCallitems expand into one nestedSubagentItemper receiver thread (state,task,parentItemId), with raw evidence such astool,model,reasoningEffort,senderThreadIdand per-agenterrorMessage/resultMessageunderitem.extensions.native.subAgentActivitylifecycle items (started/interacted/interrupted/completed, carryingagentThreadId/agentPath) project into the same vocabulary. Documented singular legacy wire words (receiverThreadId,newThreadId,agentStatus) are tolerated as a fallback. - Discovery.
thread/startednotifications for spawned children surface as a sessionnoticeevent with codeSUBAGENT_THREAD_STARTED; itsextensionscarry the child'snativeThreadId,parentThreadId,agentNickname,agentRoleandforkedFromId. Spawn children that declarecanAcceptDirectInput: false(parent-owned Multi-Agent V2) degrade the advertisedinputcapability to no delivery — direct input is rejected natively for them, and the mapped error isSUBAGENT_DIRECT_INPUT_REJECTED(definite, non-retryable).turn/interruptremains available for such threads. - Thread tree.
adapter.listThreads(params)pages stored threads with optional relation filters (parentThreadIdfor direct children,ancestorThreadIdfor any-depth descendants — mutually exclusive),sourceKinds,cwdand cursor pagination, returningCodexThreadSummaryentries (parentThreadId/agentNickname/agentRolewhere native supplies them, enriched from locally observed spawns when the page omits them). Review and guardian threads never participate in relation filters (native behavior). Method-not-found downgrades every registered session'ssubagents.treecapability.
Session metadata exposes the same linkage on opened and resumed threads via snapshot.metadata.extensions (parentThreadId, agentNickname, agentRole, forkedFromId, nativeSource). Resumed child threads keep Codex's paginated-history semantics: their history contains only their own turns.
Late native completions (subAgentActivity completed arriving after the spawning turn completed) keep their original turn attribution; the core admits them as late completions of tracked items instead of rejecting the event stream.
Native hook events
Synchronous Codex hook runs with an active turn appear as hook items.
Session-level runs appear as NATIVE_HOOK_STARTED and
NATIVE_HOOK_COMPLETED notices. See Codex App Server
for native notification coverage and Codex Hooks
for configuration and trust management.
Code structure
| Source | Responsibility |
| --- | --- |
| src/index.ts, src/adapter.ts | Assemble the package exports and manage native App Server sessions. |
| src/session.ts, src/mapping.ts | Dispatch session requests and project native turns, items, and hook notifications. |
| src/jsonl.ts, src/profile.ts | Run the JSON-RPC transport and resolve version-specific native capabilities. |
| src/catalog.ts, src/threads.ts | Discover models and list native threads. |
Development
cd packages/adapter/codex
bun run typecheck
bun test tests
bun run protocol:check # verify pinned native schema hashes and mappings
bun run smoke # optional live smoke against a local binaryDocumentation
- Architecture — adapters' role in the SDK (currently in Chinese)
- AHP v1 protocol
AHP v1 lifecycle revision
Use await turn.steer({ input }) before await turn.result() for exact guidance.
The adapter maps the target to native expectedTurnId, preserving native target
validation; a stale target rejects. An input receipt alone does not prove that a
new execution has started or completed.
See the design and implementation plan. See the complete example.
