In the codex-rs folder where the rust code lives:
- Crate names are prefixed with
codex-. For example, thecorefolder's crate is namedcodex-core - When using format! and you can inline variables into {}, always do that.
- Install any commands the repo relies on (for example
just,rg, orcargo-insta) if they aren't already available before running instructions here. - For release CLI builds, always use
make release-codexorjust release-codexfrom repo root instead of rawcargo build --release. These targets clearCARGO_PROFILE_RELEASE_LTO,CARGO_PROFILE_RELEASE_CODEGEN_UNITS,CARGO_PROFILE_RELEASE_DEBUG, andCARGO_PROFILE_RELEASE_STRIPbefore build, then install to/Users/chenwenjie/bin/codexby default. - Never add or modify any code related to
CODEX_SANDBOX_NETWORK_DISABLED_ENV_VARorCODEX_SANDBOX_ENV_VAR.- You operate in a sandbox where
CODEX_SANDBOX_NETWORK_DISABLED=1will be set whenever you use theshelltool. Any existing code that usesCODEX_SANDBOX_NETWORK_DISABLED_ENV_VARwas authored with this fact in mind. It is often used to early exit out of tests that the author knew you would not be able to run given your sandbox limitations. - Similarly, when you spawn a process using Seatbelt (
/usr/bin/sandbox-exec),CODEX_SANDBOX=seatbeltwill be set on the child process. Integration tests that want to run Seatbelt themselves cannot be run under Seatbelt, so checks forCODEX_SANDBOX=seatbeltare also often used to early exit out of tests, as appropriate.
- You operate in a sandbox where
- Always collapse if statements per https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
- Always inline format! args when possible per https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
- Use method references over closures when possible per https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
- When possible, make
matchstatements exhaustive and avoid wildcard arms. - When writing tests, prefer comparing the equality of entire objects over fields one by one.
- When making a change that adds or changes an API, ensure that the documentation in the
docs/folder is up to date if applicable. - If you change
ConfigTomlor nested config types, runjust write-config-schemato updatecodex-rs/core/config.schema.json. - If you change Rust dependencies (
Cargo.tomlorCargo.lock), runjust bazel-lock-updatefrom the repo root to refreshMODULE.bazel.lock, and include that lockfile update in the same change. - After dependency changes, run
just bazel-lock-checkfrom the repo root so lockfile drift is caught locally before CI. - Do not create small helper methods that are referenced only once.
Run just fmt (in codex-rs directory) automatically after you have finished making Rust code changes; do not ask for approval to run it. Additionally, run the tests:
- Run the test for the specific project that was changed. For example, if changes were made in
codex-rs/tui, runcargo test -p codex-tui. - Once those pass, if any changes were made in common, core, or protocol, run the complete test suite with
cargo test(orjust testifcargo-nextestis installed). Avoid--all-featuresfor routine local runs because it expands the build matrix and can significantly increasetarget/disk usage; use it only when you specifically need full feature coverage. project-specific or individual tests can be run without asking the user, but do ask the user before running the complete test suite.
Before finalizing a large change to codex-rs, run just fix -p <project> (in codex-rs directory) to fix any linter issues in the code. Prefer scoping with -p to avoid slow workspace‑wide Clippy builds; only run just fix without -p if you changed shared crates. Do not re-run tests after running fix or fmt.
See codex-rs/tui/styles.md.
- Use concise styling helpers from ratatui’s Stylize trait.
- Basic spans: use "text".into()
- Styled spans: use "text".red(), "text".green(), "text".magenta(), "text".dim(), etc.
- Prefer these over constructing styles with
Span::styledandStyledirectly. - Example: patch summary file lines
- Desired: vec![" └ ".into(), "M".red(), " ".dim(), "tui/src/app.rs".dim()]
- Prefer Stylize helpers: use "text".dim(), .bold(), .cyan(), .italic(), .underlined() instead of manual Style where possible.
- Prefer simple conversions: use "text".into() for spans and vec![…].into() for lines; when inference is ambiguous (e.g., Paragraph::new/Cell::from), use Line::from(spans) or Span::from(text).
- Computed styles: if the Style is computed at runtime, using
Span::styledis OK (Span::from(text).set_style(style)is also acceptable). - Avoid hardcoded white: do not use
.white(); prefer the default foreground (no color). - Chaining: combine helpers by chaining for readability (e.g., url.cyan().underlined()).
- Single items: prefer "text".into(); use Line::from(text) or Span::from(text) only when the target type isn’t obvious from context, or when using .into() would require extra type annotations.
- Building lines: use vec![…].into() to construct a Line when the target type is obvious and no extra type annotations are needed; otherwise use Line::from(vec![…]).
- Avoid churn: don’t refactor between equivalent forms (Span::styled ↔ set_style, Line::from ↔ .into()) without a clear readability or functional gain; follow file‑local conventions and do not introduce type annotations solely to satisfy .into().
- Compactness: prefer the form that stays on one line after rustfmt; if only one of Line::from(vec![…]) or vec![…].into() avoids wrapping, choose that. If both wrap, pick the one with fewer wrapped lines.
- Use
textwrap::wrapfor plain strings. - If you need to wrap a
Line, use helpers intui/src/wrapping.rs, e.g.word_wrap_line/word_wrap_lineswithRtOptions. - For indenting wrapped lines, prefer
initial_indent/subsequent_indentonRtOptionsinstead of custom logic. - If you need to truncate a list of lines to a max height, prefer the file-local truncation helpers already used by that widget; there is no shared
truncate_lineshelper.
- This repository uses snapshot tests (via
insta) incodex-rs/tuito verify rendered output. - When intentionally changing rendered output, update snapshots with:
cargo test -p codex-tuicargo insta accept -p codex-tui
If you don’t have the tool:
cargo install cargo-insta
- Tests should use pretty_assertions::assert_eq for clearer diffs. Import this at the top of the test module if it isn't already.
- Prefer deep equals comparisons whenever possible. Perform
assert_eq!()on entire objects, rather than individual fields. - Avoid mutating process environment in tests; prefer passing environment-derived flags or dependencies from above.
- Prefer
codex_utils_cargo_bin::cargo_bin("...")overassert_cmd::Command::cargo_bin(...)orescargotwhen tests need to spawn first-party binaries.- Under Bazel, binaries and resources may live under runfiles; use
codex_utils_cargo_bin::cargo_binto resolve absolute paths that remain stable afterchdir.
- Under Bazel, binaries and resources may live under runfiles; use
- When locating fixture files or test resources under Bazel, avoid
env!("CARGO_MANIFEST_DIR"). Prefercodex_utils_cargo_bin::find_resource!so paths resolve correctly under both Cargo and Bazel runfiles.
-
Prefer the utilities in
core_test_support::responseswhen writing end-to-end Codex tests. -
All
mount_sse*helpers return aResponseMock; hold onto it so you can assert against outbound/responsesPOST bodies. -
Use
ResponseMock::single_request()when a test should only issue one POST, orResponseMock::requests()to inspect every capturedResponsesRequest. -
ResponsesRequestexposes helpers (body_json,input,function_call_output,custom_tool_call_output,call_output,header,path,query_param) so assertions can target structured payloads instead of manual JSON digging. -
Build SSE payloads with the provided
ev_*constructors and thesse(...). -
Prefer
wait_for_eventoverwait_for_event_with_timeout. -
Prefer
mount_sse_onceovermount_sse_once_matchormount_sse_sequence -
Typical pattern:
let mock = responses::mount_sse_once(&server, responses::sse(vec![ responses::ev_response_created("resp-1"), responses::ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), responses::ev_completed("resp-1"), ])).await; codex.submit(Op::UserTurn { ... }).await?; // Assert request body if needed. let request = mock.single_request(); // assert using request.function_call_output(call_id) or request.json_body() or other helpers.
These guidelines apply to app-server protocol work in codex-rs, especially:
app-server-protocol/src/protocol/common.rsapp-server-protocol/src/protocol/v2.rsapp-server/README.md
- All active API development should happen in app-server v2. Do not add new API surface area to v1.
- Follow payload naming consistently:
*Paramsfor request payloads,*Responsefor responses, and*Notificationfor notifications. - Expose RPC methods as
<resource>/<method>and keep<resource>singular (for example,thread/read,app/list). - Always expose fields as camelCase on the wire with
#[serde(rename_all = "camelCase")]unless a tagged union or explicit compatibility requirement needs a targeted rename. - Exception: config RPC payloads are expected to use snake_case to mirror config.toml keys (see the config read/write/list APIs in
app-server-protocol/src/protocol/v2.rs). - Always set
#[ts(export_to = "v2/")]on v2 request/response/notification types so generated TypeScript lands in the correct namespace. - Never use
#[serde(skip_serializing_if = "Option::is_none")]for v2 API payload fields. Exception: client->server requests that intentionally have no params may use:params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>. - Keep Rust and TS wire renames aligned. If a field or variant uses
#[serde(rename = "...")], add matching#[ts(rename = "...")]. - For discriminated unions, use explicit tagging in both serializers:
#[serde(tag = "type", ...)]and#[ts(tag = "type", ...)]. - Prefer plain
StringIDs at the API boundary (do UUID parsing/conversion internally if needed). - Timestamps should be integer Unix seconds (
i64) and named*_at(for example,created_at,updated_at,resets_at). - For experimental API surface area:
use
#[experimental("method/or/field")], deriveExperimentalApiwhen field-level gating is needed, and useinspect_params: trueincommon.rswhen only some fields of a method are experimental.
- Every optional field must be annotated with
#[ts(optional = nullable)]. Do not use#[ts(optional = nullable)]outside client->server request payloads (*Params). - Optional collection fields (for example
Vec,HashMap) must useOption<...>+#[ts(optional = nullable)]. Do not use#[serde(default)]to model optional collections, and do not useskip_serializing_ifon v2 payload fields. - When you want omission to mean
falsefor boolean fields, use#[serde(default, skip_serializing_if = "std::ops::Not::not")] pub field: booloverOption<bool>. - For new list methods, implement cursor pagination by default:
request fields
pub cursor: Option<String>andpub limit: Option<u32>, response fieldspub data: Vec<...>andpub next_cursor: Option<String>.
- Update docs/examples when API behavior changes (at minimum
app-server/README.md). - Regenerate schema fixtures when API shapes change:
just write-app-server-schema(andjust write-app-server-schema --experimentalwhen experimental API fixtures are affected). - Validate with
cargo test -p codex-app-server-protocol. - Avoid boilerplate tests that only assert experimental field markers for individual
request fields in
common.rs; rely on schema generation/tests and behavioral coverage instead.
- The stable current
codex githubbehavior is documented incodex-rs/docs/github-webhook.md. - Experimental GitHub-triggered orchestration guidance lives in
docs/github-outcome-first-overlay.md. - There is intentionally no active root
WORKFLOW.mdcontract for GitHub orchestration in this repository yet. - Do not assume the experimental overlay describes current native
codex githubbehavior unless the current session explicitly instructs the agent to use that overlay and referencesdocs/github-outcome-first-overlay.md, or a higher-level orchestrator/runtime explicitly activates it with an equivalent instruction.