codex-subagent-rs
Delegate a task to your existing Codex CLI, continue its conversation, or run it in the background and receive the result in the original Antigravity chat. One native executable provides the MCP server, the Antigravity Hook, and the completion Sidecar.
The server provides eight tools for delegation, follow-ups, background jobs, model discovery and usage queries. It uses codex exec; Codex still owns authentication, model calls, sandboxing, tools, and conversation history.
Build and test without changing your setup
Requires Rust 1.88+ and a Unix host. macOS is the tested platform; Windows is not supported by this release. Build the native binary:
cargo build --release --locked --bin codex-subagent-rs
./target/release/codex-subagent-rs --help
Run the isolated tests:
cargo test --all-features --locked
cargo clippy --all-targets --all-features --locked -- -D warnings
cargo fmt --check
The test-fixtures feature builds a fake Codex executable. Integration tests clear their subprocess environments, use temporary homes, and substitute both Codex and the Antigravity message sender. They make no model calls, send no real notifications, and do not read your Codex credentials. On macOS, process-tree and filesystem-watcher tests need permission to inspect their own descendants and watch temporary directories.
No command installs a service, changes a client configuration, or restarts an existing MCP. The initial delivery is implementation-and-test only; authenticated Codex and live Antigravity acceptance remain a separate step before switching clients.
Use the three entrypoints
| Command |
Input and output |
codex-subagent-rs mcp |
MCP JSON-RPC on stdin/stdout; diagnostics on stderr. This is the default mode. |
codex-subagent-rs hook |
One Antigravity tool event on stdin; an approval decision JSON object on stdout. |
codex-subagent-rs sidecar |
Watches completion files and invokes Antigravity's agentapi send-message. Logs to stderr. |
Set CODEX_BIN to the native Codex executable when your GUI app does not inherit the right PATH.
MCP and Sidecar must share an absolute completion directory. Set CODEX_SUBAGENT_COMPLETION_DIR, or pass --events-dir ABSOLUTE_PATH to both. The default is:
~/.local/share/codex-subagent-rs/completion/events
The Hook supplies conversation context to codex_delegate and codex_follow_up calls on server codex. For either call in background mode, it also replaces reply_to with the host's conversation ID. Successful calls preserve decision: "ask".
The Sidecar uses agentapi from PATH. If Antigravity supplies ANTIGRAVITY_AGENTAPI_EXE, it invokes that binary with agentapi send-message .... Register the Sidecar with the host when switching; running the MCP alone does not start notifications.
Automatically share the Antigravity conversation
Register codex-subagent-rs hook as an Antigravity PreToolUse hook matching call_mcp_tool, or update the existing registration. Use the same CODEX_SUBAGENT_COMPLETION_DIR or --events-dir setting as the MCP. For example, in your Antigravity hooks.json:
{
"codex-context": {
"PreToolUse": [
{
"matcher": "call_mcp_tool",
"hooks": [{"type": "command", "command": "/absolute/path/to/codex-subagent-rs hook"}]
}
]
}
}
Each matching call saves the complete host-provided transcript and attaches a one-use reference. The MCP grants Codex read access to that file and denies access to other snapshots in the same directory. Codex sees the allowed path in its native permission context and can decide whether to read it. The Hook does not change prompt.
This applies to blocking and background delegations and to each follow-up, with a fresh snapshot for each call. Use a CLI that supports per-invocation permission profiles (verified with Codex CLI 0.165.0) and select read-only or workspace-write. Upgrade the Hook and MCP together; the Sidecar and global Codex configuration need no changes. Direct MCP calls without a Hook reference keep their existing behavior.
Snapshots are private (directory 0700, files 0600) and remain on disk until removed. The Hook saves snapshots/<uuid>.pending.jsonl; the MCP claims it by renaming it to snapshots/<uuid>.jsonl. A snapshot contains all bytes present at the start of the copy, not attachments absent from the log. A missing, unreadable, or non-regular transcript, or a snapshot write failure, denies the call with a reason.
The Hook uses Antigravity's documented transcriptPath and argument overwrite contract; it does not scan other conversations or modify the source transcript. Only enable it when the current conversation may be shared with Codex.
Native delegation follows the global AGENTS.md
Native subagent tools are enabled for new tasks, follow-ups and capacity continuations. A single prompt instruction says to follow the global AGENTS.md for subagent orchestration; Codex's built-in instruction loading handles the file. Project instructions continue to govern repository work.
The MCP adds no child concurrency cap, model allow-list, effort ceiling or child-model default. Existing Codex [agents] settings and custom roles remain in effect; CLI runtime limits still apply. The server's existing model/effort policy validates the parent delegation, not its native children. The eight-background-job limit also remains an outer-job limit.
Self-recursive MCP delegation stays disabled. The guard checks MCP commands and arguments for the codex-subagent-rs executable name or the current executable path. Other servers follow CODEX_SUBAGENT_MCP_SERVERS without special treatment. Native collaboration events, when emitted by Codex, appear in blocking progress and background-job activity; child reports do not overwrite the parent's final answer. Native threads remain owned by Codex, within the same CLI invocation and cancellation lifecycle.
The enabling flags were checked against Codex CLI 0.165.0. To repeat the configuration-only check with your native binary, set CODEX_NATIVE_TEST_BIN, then run:
cargo test --locked --test cli_config -- --ignored
This check uses temporary Codex homes, verifies both native backends' existing settings are preserved, and starts no model turn. It is skipped in the default test suite because a real Codex installation is not required for ordinary development.
The eight tools
| Tool |
Purpose |
codex_quota |
Fetch remaining usage percentages and reset times without a model call. |
codex_doctor |
Diagnose executable, version, login status and configuration on demand. |
list_codex_models |
List live models, efforts and context windows from the CLI catalog. |
codex_delegate |
Execute a self-contained task, blocking or in the background. |
codex_follow_up |
Continue a Codex thread_id in blocking or background mode, preserving its model, effort and directory unless overridden. |
codex_job_status |
Inspect a background job, or list known jobs when job_id is omitted. |
codex_job_result |
Read a finished job, including persisted callback reports after an MCP restart. |
codex_job_cancel |
Request cancellation and terminate the job's process tree. |
Tool descriptions and input schemas have one source in schemas. Runtime validation rejects invalid directories, thread IDs, schema sizes, model choices and policy violations before executing a model. Unknown fields are stripped per tool; they cannot switch a delegation into a follow-up.
A blocking codex_delegate call needs a prompt and either an explicit model or a configured default. Put the task, context, relevant paths and completion criteria together in prompt. Supply an absolute working_dir when the task belongs to a particular project. output_schema is passed directly to Codex’s native structured-output option; valid JSON is exposed in a delimited result only if the entire run succeeded. The delegated prompt is the global-subagent instruction followed by the caller's trimmed prompt.
A background call returns a job_id. If it says completion delivery is registered, let the Sidecar notify the originating chat; otherwise use codex_job_status and then codex_job_result. Continue a conversation with its thread_id, not its job_id.
Delegations and follow-ups default to a two-hour execution budget (7200 seconds). Set timeout_seconds to override it within 1–7200 seconds. Capacity continuations share the original deadline.
Follow-ups recover model, effort and directory from memory or Codex’s thread/read API after restart. The wrapper does not scan private session files. Settings in reports are requested settings; Codex enforces permissions. Worktree paths are read from native thread metadata when needed.
Model listing uses each model's context_window from the installed CLI catalog, not a configuration override; missing values remain unknown. Catalog failures return the CLI error rather than a hardcoded substitute. Sign-in checks run only when codex_doctor is called.
Token usage in task results
Blocking results, background results and saved completion reports include a token-usage block: total, input, cached/uncached input, cache-write input, output and reasoning output. Total is input + output; cached input and reasoning output are breakdowns, not extra tokens to add.
The report distinguishes this call from thread cumulative. Codex 0.165.0 emits cumulative usage on resume; the MCP subtracts its previous snapshot for a known thread. After an MCP restart or when a comparable snapshot is unavailable, this-call usage is unknown. Missing usage, including interrupted runs without a completion event, is not treated as zero.
These counts cover the parent thread only. Native child-agent usage is not included in Codex's root JSONL usage event and is not aggregated by this wrapper.
Query remaining usage quota separately
Call codex_quota with no arguments, or use the same directory as your delegation:
{"working_dir":"/absolute/path/to/project"}
The tool queries Codex's account/rateLimits/read on every call and returns each reported bucket's remaining percentage, window duration and UTC reset time. It also shows the backend's usage-allowed and limit-reached states when available; a positive percentage alone does not mean a request is permitted. Window lengths come from the account, rather than being fixed at five hours or one week.
It reuses Codex's sign-in and never starts a model turn. Unreported windows remain unknown; authentication, network and unsupported-endpoint errors are surfaced. It does not return money/credits, consume a quota reset, or estimate how many tokens remain. API-key-only accounts cannot use the ChatGPT quota endpoint.
Environment policy
| Variable |
Default / behavior |
CODEX_BIN |
codex on PATH |
CODEX_HOME |
Passed through to Codex; normally ~/.codex |
CODEX_SUBAGENT_DEFAULT_MODEL |
Unset; a single-entry allow-list can supply the default |
CODEX_SUBAGENT_ALLOWED_MODELS |
Unrestricted; comma-separated model slugs otherwise |
CODEX_SUBAGENT_DEFAULT_EFFORT |
Model default |
CODEX_SUBAGENT_MAX_EFFORT |
Unrestricted; clamps requested effort to supported levels below the ceiling |
CODEX_SUBAGENT_DEFAULT_SANDBOX |
read-only |
CODEX_SUBAGENT_MAX_SANDBOX |
workspace-write; requests exceeding it are refused |
CODEX_SUBAGENT_MCP_SERVERS |
none; accepts all or comma-separated names; self-delegation entries are disabled |
CODEX_SUBAGENT_PLUGINS |
none; accepts all or comma-separated plugin IDs |
CODEX_SUBAGENT_APPS |
off; accepts on |
CODEX_SUBAGENT_COMPLETION_DIR |
Private event directory described above |
ANTIGRAVITY_AGENTAPI_EXE |
Sidecar-only host executable override |
Supported effort vocabulary: none, minimal, low, medium, high, xhigh, max, ultra. Actual model support comes from the live catalog. Invalid environment settings remain visible through codex_doctor; they refuse new delegation rather than silently relaxing policy.
Completion and recovery
After a timeout, the caller decides whether to use the partial result and stop or to continue the task. To continue, take the thread_id from the timeout report and call codex_follow_up:
{"thread_id":"<thread_id from the report>","prompt":"Continue the original task from where it stopped. Keep completed work.","mode":"background"}
The follow-up starts a fresh execution budget on the same Codex thread and returns a new job_id. The original timeout report stays unchanged. Omit mode to wait for the result in the tool call. If the original task ran outside Git, pass skip_git_repo_check: true when resuming after an MCP restart. If it needs write access, request the sandbox for the new turn explicitly.
Timeouts do not trigger automatic follow-ups. Resume only once the result is available and contains a thread_id; do not resume cancelled jobs.
Every background job saves <job_id>.job.json in the existing completion directory, with or without reply_to