Skip to content

Latest commit

 

History

History
147 lines (98 loc) · 3.96 KB

ARCHITECTURE.md

File metadata and controls

147 lines (98 loc) · 3.96 KB

Architecture

This document explains how codex-bridge is structured for maintainers.

Runtime Shape

codex-bridge is an MCP server over stdio. The published npm package is @ndcorder/codex-bridge, and the binary is codex-bridge.

Entry points:

  • src/cli.ts: command dispatcher. codex-bridge init runs setup; any other invocation starts the MCP server.
  • src/index.ts: MCP server construction and tool registration.
  • src/tools.ts: tool handlers, session lookup, risk radar, research/debug orchestration.
  • src/codex.ts: Codex CLI process execution, timeouts, retries, model resolution, and output capture.
  • src/session.ts: in-memory session history, stats, and working memory.
  • src/research-artifacts.ts: durable research dossier writer.
  • src/prompts.ts: prompt builders and bridge system prompt.

MCP Server

src/index.ts creates an McpServer named codex-bridge at version 1.1.0 and connects it to StdioServerTransport.

Each tool registration defines:

  • tool name
  • description shown to the MCP client
  • Zod input schema
  • handler that returns MCP text content

Most Codex-backed tools catch errors and return an MCP error response with isError: true.

Codex CLI Execution

src/codex.ts shells out to the Codex CLI with execFile, not a shell string.

The wrapper:

  • resolves model from request, CODEX_BRIDGE_MODEL, CODEX_MODEL, or default gpt-5.3-codex
  • writes Codex output to a temporary file with -o
  • passes large prompts through stdin to avoid argv size limits
  • applies timeout and retry controls
  • caps captured stderr so huge prompt or command dumps do not flood errors
  • removes temporary directories in a finally block

The default timeout is 120000 milliseconds. The default retry count is 1. The default retry backoff is 500 milliseconds.

Sessions and Working Memory

src/tools.ts stores sessions in an in-memory Map.

Session key resolution order:

  1. explicit session_id
  2. working_dir
  3. default

Session controls:

  • CODEX_BRIDGE_MAX_SESSIONS: max session count, default 200
  • CODEX_BRIDGE_SESSION_TTL_MS: inactivity TTL, default 86400000

When max sessions is exceeded, the oldest touched sessions are evicted. TTL cleanup runs opportunistically when sessions are accessed or listed.

Session stores recent exchanges and extracts lightweight working memory:

  • current plan from codex_plan
  • high/critical findings from codex_review
  • risk radar summary from codex_risk_radar
  • research findings from codex_research
  • debug verdict from codex_debug

This context is injected into later prompts where relevant.

Research Artifacts

codex_research can write a durable dossier when working_dir is provided.

Path shape:

<working_dir>/.codex-bridge/research/<timestamp-topic-uuid>/

Files include:

  • metadata.json
  • report.md
  • sources.md
  • open-questions.md
  • follow-ups.md
  • turn-XX-<phase>.md
  • prompts/turn-XX-<phase>.md

Directory creation is exclusive and retries with a UUID suffix to avoid collisions.

Risk Radar

codex_risk_radar is local and does not call Codex.

It scans added diff lines and optional commit message text for categories such as:

  • auth/permissions
  • input parsing
  • async/concurrency
  • state/caches
  • crypto/secrets
  • database/queries
  • file/network I/O
  • error handling

It stores the risk summary in session memory for follow-on planning or review.

Tests

Primary test layers:

  • Unit tests in tests/*.test.ts
  • MCP stdio smoke test in tests/mcp-smoke.test.ts
  • Bake-off black-box tests under benchmark/bakeoff

Common commands:

npm run build
npm test
npm run test:bakeoff
npm run verify

Release verification uses:

npm run release:prepare

This runs verify, package dry-run, and release notes generation.

Package Contents

The npm package intentionally ships runtime output and docs only:

  • dist/src
  • README.md
  • CHANGELOG.md
  • docs/*.md

Build artifacts under dist/tests and benchmark sources are not published.