Skip to content

feat(agent-client): Codex process owner + per-surface views + pool#10

Open
huntharo wants to merge 1 commit into
mainfrom
feat/codex-process-owner-pool
Open

feat(agent-client): Codex process owner + per-surface views + pool#10
huntharo wants to merge 1 commit into
mainfrom
feat/codex-process-owner-pool

Conversation

@huntharo

Copy link
Copy Markdown
Contributor

What

A Codex process owner/pool for @pwrdrvr/agent-client, equivalent in spirit to the ACP pool — so a host stops accidentally spawning multiple codex app-server processes (one for chat, another for one-shot, another for a model-picker refresh).

New API

Why per-thread routing works

Every Codex notification and every server-request carries a threadId: DynamicToolCallParams (item/tool/call) and the v2 *RequestApprovalParams all have threadId+turnId; legacy v1 approvals carry conversationId. Plain CodexThreadClient just never used it — it routes server-requests by method into one global handler slot. That's the clobber this fixes (req #3).

Fold (no breaking changes)

CodexThreadClient and CodexOneShotClient are now thin shims:

  • CodexThreadClient → an owner + a single default view (its historical single-global-handler behavior, preserved).
  • CodexOneShotClientowner.runOneShot() / owner.listModels().

Their public APIs and behavior are unchanged; all pre-existing tests stay green. This deletes PwrSnap's need to reach into CodexThreadClient internals (getConnection()/initialize()/handleNotification()) for model-listing and structured one-shot — those are now supported owner API.

Tests

agent-client 49 passing (40 prior + 9 new). New coverage: per-view event/tool/approval routing isolation; view close() detaches routing without killing the process; one-shot structured turn + outputSchema + rollback + no leak to surface views; listModels() without opening a thread; pool dedup/keying/release/closeAll. Full workspace: typecheck (7), boundaries (no violations), all package tests green.

Consumer migration (PwrSnap)

Replace the local codex-agent-pool.ts compat layer:

const pool = new CodexProcessOwnerPool();
const key = `${command}::${codexHome ?? "default"}`;
const owner = await pool.acquire(key, () => new CodexProcessOwner({ command, env }));
const view = owner.createBackendView();        // per chat surface; drive with ChatThreadController
const models = await owner.listModels();       // model picker — no new process
const out = await owner.runOneShot({ prompt, outputSchema, imagePaths }); // enrichment — same process

🤖 Generated with Claude Code

Introduce CodexProcessOwner: one owned `codex app-server` process/connection
that hands out lightweight per-surface CodexBackendViews (each an AgentBackend).
Inbound notifications AND tool-call/approval server-requests are demultiplexed
by threadId to the owning view, so many chat surfaces share ONE Codex process
without clobbering each other's handlers — the gap plain CodexThreadClient has
(a single global handler slot). Feasible because every Codex notification and
every server-request carries threadId (DynamicToolCallParams, the v2
*RequestApprovalParams; legacy v1 carry conversationId).

The owner also exposes listModels() and a structured runOneShot() (outputSchema,
local images, per-turn rollback, token usage capture, abort) over the SAME
connection — so model-picker refreshes and capture enrichment stop spawning
their own App Server processes.

CodexProcessOwnerPool mirrors AcpAgentClientPool (acquire/warm/has/release/
closeAll), keyed on connection identity (command, CODEX_HOME/env): one process
per key, deduped concurrent warm-ups, app-level shutdown.

Fold the existing clients onto the owner:
- CodexThreadClient → thin shim over an owner + a single default view (its
  historical single-global-handler behavior, preserved).
- CodexOneShotClient → thin shim over owner.runOneShot()/listModels().
Both public APIs and behaviors unchanged; all existing tests stay green.

New tests cover per-view event/tool/approval routing isolation, view detach
(stops routing without killing the process), one-shot structured turn +
rollback + no-leak-to-views, model listing without a thread, and the pool.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant