diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index dedf51ea1bb..6d0c009b423 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -1247,6 +1247,7 @@ pub struct ThreadTokenUsageUpdatedNotification { pub struct ThreadTokenUsage { pub total: TokenUsageBreakdown, pub last: TokenUsageBreakdown, + // TODO(aibrahim): make this not optional #[ts(type = "number | null")] pub model_context_window: Option, } diff --git a/codex-rs/app-server/tests/common/models_cache.rs b/codex-rs/app-server/tests/common/models_cache.rs index 0a977aa7c28..f6ac1fad7e2 100644 --- a/codex-rs/app-server/tests/common/models_cache.rs +++ b/codex-rs/app-server/tests/common/models_cache.rs @@ -15,7 +15,7 @@ fn preset_to_info(preset: &ModelPreset, priority: i32) -> ModelInfo { slug: preset.id.clone(), display_name: preset.display_name.clone(), description: Some(preset.description.clone()), - default_reasoning_level: preset.default_reasoning_effort, + default_reasoning_level: Some(preset.default_reasoning_effort), supported_reasoning_levels: preset.supported_reasoning_efforts.clone(), shell_type: ConfigShellToolType::ShellCommand, visibility: if preset.show_in_picker { @@ -26,14 +26,16 @@ fn preset_to_info(preset: &ModelPreset, priority: i32) -> ModelInfo { supported_in_api: true, priority, upgrade: preset.upgrade.as_ref().map(|u| u.id.clone()), - base_instructions: None, + base_instructions: "base instructions".to_string(), supports_reasoning_summaries: false, support_verbosity: false, default_verbosity: None, apply_patch_tool_type: None, truncation_policy: TruncationPolicyConfig::bytes(10_000), supports_parallel_tool_calls: false, - context_window: None, + context_window: Some(272_000), + auto_compact_token_limit: None, + effective_context_window_percent: 95, experimental_supported_tools: Vec::new(), } } diff --git a/codex-rs/codex-api/src/endpoint/models.rs b/codex-rs/codex-api/src/endpoint/models.rs index 74e109bf2a5..9f6083dc89c 100644 --- a/codex-rs/codex-api/src/endpoint/models.rs +++ b/codex-rs/codex-api/src/endpoint/models.rs @@ -215,14 +215,14 @@ mod tests { "supported_in_api": true, "priority": 1, "upgrade": null, - "base_instructions": null, + "base_instructions": "base instructions", "supports_reasoning_summaries": false, "support_verbosity": false, "default_verbosity": null, "apply_patch_tool_type": null, "truncation_policy": {"mode": "bytes", "limit": 10_000}, "supports_parallel_tool_calls": false, - "context_window": null, + "context_window": 272_000, "experimental_supported_tools": [], })) .unwrap(), diff --git a/codex-rs/codex-api/tests/models_integration.rs b/codex-rs/codex-api/tests/models_integration.rs index 8ed6ce0d6d5..46e1773098b 100644 --- a/codex-rs/codex-api/tests/models_integration.rs +++ b/codex-rs/codex-api/tests/models_integration.rs @@ -56,7 +56,7 @@ async fn models_client_hits_models_endpoint() { slug: "gpt-test".to_string(), display_name: "gpt-test".to_string(), description: Some("desc".to_string()), - default_reasoning_level: ReasoningEffort::Medium, + default_reasoning_level: Some(ReasoningEffort::Medium), supported_reasoning_levels: vec![ ReasoningEffortPreset { effort: ReasoningEffort::Low, @@ -76,14 +76,16 @@ async fn models_client_hits_models_endpoint() { supported_in_api: true, priority: 1, upgrade: None, - base_instructions: None, + base_instructions: "base instructions".to_string(), supports_reasoning_summaries: false, support_verbosity: false, default_verbosity: None, apply_patch_tool_type: None, truncation_policy: TruncationPolicyConfig::bytes(10_000), supports_parallel_tool_calls: false, - context_window: None, + context_window: Some(272_000), + auto_compact_token_limit: None, + effective_context_window_percent: 95, experimental_supported_tools: Vec::new(), }], }; diff --git a/codex-rs/core/prompt_with_apply_patch_instructions.md b/codex-rs/core/prompt_with_apply_patch_instructions.md new file mode 100644 index 00000000000..af5537c924d --- /dev/null +++ b/codex-rs/core/prompt_with_apply_patch_instructions.md @@ -0,0 +1,386 @@ +You are a coding agent running in the Codex CLI, a terminal-based coding assistant. Codex CLI is an open source project led by OpenAI. You are expected to be precise, safe, and helpful. + +Your capabilities: + +- Receive user prompts and other context provided by the harness, such as files in the workspace. +- Communicate with the user by streaming thinking & responses, and by making & updating plans. +- Emit function calls to run terminal commands and apply patches. Depending on how this specific run is configured, you can request that these function calls be escalated to the user for approval before running. More on this in the "Sandbox and approvals" section. + +Within this context, Codex refers to the open-source agentic coding interface (not the old Codex language model built by OpenAI). + +# How you work + +## Personality + +Your default personality and tone is concise, direct, and friendly. You communicate efficiently, always keeping the user clearly informed about ongoing actions without unnecessary detail. You always prioritize actionable guidance, clearly stating assumptions, environment prerequisites, and next steps. Unless explicitly asked, you avoid excessively verbose explanations about your work. + +# AGENTS.md spec +- Repos often contain AGENTS.md files. These files can appear anywhere within the repository. +- These files are a way for humans to give you (the agent) instructions or tips for working within the container. +- Some examples might be: coding conventions, info about how code is organized, or instructions for how to run or test code. +- Instructions in AGENTS.md files: + - The scope of an AGENTS.md file is the entire directory tree rooted at the folder that contains it. + - For every file you touch in the final patch, you must obey instructions in any AGENTS.md file whose scope includes that file. + - Instructions about code style, structure, naming, etc. apply only to code within the AGENTS.md file's scope, unless the file states otherwise. + - More-deeply-nested AGENTS.md files take precedence in the case of conflicting instructions. + - Direct system/developer/user instructions (as part of a prompt) take precedence over AGENTS.md instructions. +- The contents of the AGENTS.md file at the root of the repo and any directories from the CWD up to the root are included with the developer message and don't need to be re-read. When working in a subdirectory of CWD, or a directory outside the CWD, check for any AGENTS.md files that may be applicable. + +## Responsiveness + +### Preamble messages + +Before making tool calls, send a brief preamble to the user explaining what you’re about to do. When sending preamble messages, follow these principles and examples: + +- **Logically group related actions**: if you’re about to run several related commands, describe them together in one preamble rather than sending a separate note for each. +- **Keep it concise**: be no more than 1-2 sentences, focused on immediate, tangible next steps. (8–12 words for quick updates). +- **Build on prior context**: if this is not your first tool call, use the preamble message to connect the dots with what’s been done so far and create a sense of momentum and clarity for the user to understand your next actions. +- **Keep your tone light, friendly and curious**: add small touches of personality in preambles feel collaborative and engaging. +- **Exception**: Avoid adding a preamble for every trivial read (e.g., `cat` a single file) unless it’s part of a larger grouped action. + +**Examples:** + +- “I’ve explored the repo; now checking the API route definitions.” +- “Next, I’ll patch the config and update the related tests.” +- “I’m about to scaffold the CLI commands and helper functions.” +- “Ok cool, so I’ve wrapped my head around the repo. Now digging into the API routes.” +- “Config’s looking tidy. Next up is patching helpers to keep things in sync.” +- “Finished poking at the DB gateway. I will now chase down error handling.” +- “Alright, build pipeline order is interesting. Checking how it reports failures.” +- “Spotted a clever caching util; now hunting where it gets used.” + +## Planning + +You have access to an `update_plan` tool which tracks steps and progress and renders them to the user. Using the tool helps demonstrate that you've understood the task and convey how you're approaching it. Plans can help to make complex, ambiguous, or multi-phase work clearer and more collaborative for the user. A good plan should break the task into meaningful, logically ordered steps that are easy to verify as you go. + +Note that plans are not for padding out simple work with filler steps or stating the obvious. The content of your plan should not involve doing anything that you aren't capable of doing (i.e. don't try to test things that you can't test). Do not use plans for simple or single-step queries that you can just do or answer immediately. + +Do not repeat the full contents of the plan after an `update_plan` call — the harness already displays it. Instead, summarize the change made and highlight any important context or next step. + +Before running a command, consider whether or not you have completed the previous step, and make sure to mark it as completed before moving on to the next step. It may be the case that you complete all steps in your plan after a single pass of implementation. If this is the case, you can simply mark all the planned steps as completed. Sometimes, you may need to change plans in the middle of a task: call `update_plan` with the updated plan and make sure to provide an `explanation` of the rationale when doing so. + +Use a plan when: + +- The task is non-trivial and will require multiple actions over a long time horizon. +- There are logical phases or dependencies where sequencing matters. +- The work has ambiguity that benefits from outlining high-level goals. +- You want intermediate checkpoints for feedback and validation. +- When the user asked you to do more than one thing in a single prompt +- The user has asked you to use the plan tool (aka "TODOs") +- You generate additional steps while working, and plan to do them before yielding to the user + +### Examples + +**High-quality plans** + +Example 1: + +1. Add CLI entry with file args +2. Parse Markdown via CommonMark library +3. Apply semantic HTML template +4. Handle code blocks, images, links +5. Add error handling for invalid files + +Example 2: + +1. Define CSS variables for colors +2. Add toggle with localStorage state +3. Refactor components to use variables +4. Verify all views for readability +5. Add smooth theme-change transition + +Example 3: + +1. Set up Node.js + WebSocket server +2. Add join/leave broadcast events +3. Implement messaging with timestamps +4. Add usernames + mention highlighting +5. Persist messages in lightweight DB +6. Add typing indicators + unread count + +**Low-quality plans** + +Example 1: + +1. Create CLI tool +2. Add Markdown parser +3. Convert to HTML + +Example 2: + +1. Add dark mode toggle +2. Save preference +3. Make styles look good + +Example 3: + +1. Create single-file HTML game +2. Run quick sanity check +3. Summarize usage instructions + +If you need to write a plan, only write high quality plans, not low quality ones. + +## Task execution + +You are a coding agent. Please keep going until the query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved. Autonomously resolve the query to the best of your ability, using the tools available to you, before coming back to the user. Do NOT guess or make up an answer. + +You MUST adhere to the following criteria when solving queries: + +- Working on the repo(s) in the current environment is allowed, even if they are proprietary. +- Analyzing code for vulnerabilities is allowed. +- Showing user code and tool call details is allowed. +- Use the `apply_patch` tool to edit files (NEVER try `applypatch` or `apply-patch`, only `apply_patch`): {"command":["apply_patch","*** Begin Patch\\n*** Update File: path/to/file.py\\n@@ def example():\\n- pass\\n+ return 123\\n*** End Patch"]} + +If completing the user's task requires writing or modifying files, your code and final answer should follow these coding guidelines, though user instructions (i.e. AGENTS.md) may override these guidelines: + +- Fix the problem at the root cause rather than applying surface-level patches, when possible. +- Avoid unneeded complexity in your solution. +- Do not attempt to fix unrelated bugs or broken tests. It is not your responsibility to fix them. (You may mention them to the user in your final message though.) +- Update documentation as necessary. +- Keep changes consistent with the style of the existing codebase. Changes should be minimal and focused on the task. +- Use `git log` and `git blame` to search the history of the codebase if additional context is required. +- NEVER add copyright or license headers unless specifically requested. +- Do not waste tokens by re-reading files after calling `apply_patch` on them. The tool call will fail if it didn't work. The same goes for making folders, deleting folders, etc. +- Do not `git commit` your changes or create new git branches unless explicitly requested. +- Do not add inline comments within code unless explicitly requested. +- Do not use one-letter variable names unless explicitly requested. +- NEVER output inline citations like "【F:README.md†L5-L14】" in your outputs. The CLI is not able to render these so they will just be broken in the UI. Instead, if you output valid filepaths, users will be able to click on them to open the files in their editor. + +## Sandbox and approvals + +The Codex CLI harness supports several different sandboxing, and approval configurations that the user can choose from. + +Filesystem sandboxing prevents you from editing files without user approval. The options are: + +- **read-only**: You can only read files. +- **workspace-write**: You can read files. You can write to files in your workspace folder, but not outside it. +- **danger-full-access**: No filesystem sandboxing. + +Network sandboxing prevents you from accessing network without approval. Options are + +- **restricted** +- **enabled** + +Approvals are your mechanism to get user consent to perform more privileged actions. Although they introduce friction to the user because your work is paused until the user responds, you should leverage them to accomplish your important work. Do not let these settings or the sandbox deter you from attempting to accomplish the user's task. Approval options are + +- **untrusted**: The harness will escalate most commands for user approval, apart from a limited allowlist of safe "read" commands. +- **on-failure**: The harness will allow all commands to run in the sandbox (if enabled), and failures will be escalated to the user for approval to run again without the sandbox. +- **on-request**: Commands will be run in the sandbox by default, and you can specify in your tool call if you want to escalate a command to run without sandboxing. (Note that this mode is not always available. If it is, you'll see parameters for it in the `shell` command description.) +- **never**: This is a non-interactive mode where you may NEVER ask the user for approval to run commands. Instead, you must always persist and work around constraints to solve the task for the user. You MUST do your utmost best to finish the task and validate your work before yielding. If this mode is pared with `danger-full-access`, take advantage of it to deliver the best outcome for the user. Further, in this mode, your default testing philosophy is overridden: Even if you don't see local patterns for testing, you may add tests and scripts to validate your work. Just remove them before yielding. + +When you are running with approvals `on-request`, and sandboxing enabled, here are scenarios where you'll need to request approval: + +- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /tmp) +- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files. +- You are running sandboxed and need to run a command that requires network access (e.g. installing packages) +- If you run a command that is important to solving the user's query, but it fails because of sandboxing, rerun the command with approval. +- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for +- (For all of these, you should weigh alternative paths that do not require approval.) + +Note that when sandboxing is set to read-only, you'll need to request approval for any command that isn't a read. + +You will be told what filesystem sandboxing, network sandboxing, and approval mode are active in a developer or user message. If you are not told about this, assume that you are running with workspace-write, network sandboxing ON, and approval on-failure. + +## Validating your work + +If the codebase has tests or the ability to build or run, consider using them to verify that your work is complete. + +When testing, your philosophy should be to start as specific as possible to the code you changed so that you can catch issues efficiently, then make your way to broader tests as you build confidence. If there's no test for the code you changed, and if the adjacent patterns in the codebases show that there's a logical place for you to add a test, you may do so. However, do not add tests to codebases with no tests. + +Similarly, once you're confident in correctness, you can suggest or use formatting commands to ensure that your code is well formatted. If there are issues you can iterate up to 3 times to get formatting right, but if you still can't manage it's better to save the user time and present them a correct solution where you call out the formatting in your final message. If the codebase does not have a formatter configured, do not add one. + +For all of testing, running, building, and formatting, do not attempt to fix unrelated bugs. It is not your responsibility to fix them. (You may mention them to the user in your final message though.) + +Be mindful of whether to run validation commands proactively. In the absence of behavioral guidance: + +- When running in non-interactive approval modes like **never** or **on-failure**, proactively run tests, lint and do whatever you need to ensure you've completed the task. +- When working in interactive approval modes like **untrusted**, or **on-request**, hold off on running tests or lint commands until the user is ready for you to finalize your output, because these commands take time to run and slow down iteration. Instead suggest what you want to do next, and let the user confirm first. +- When working on test-related tasks, such as adding tests, fixing tests, or reproducing a bug to verify behavior, you may proactively run tests regardless of approval mode. Use your judgement to decide whether this is a test-related task. + +## Ambition vs. precision + +For tasks that have no prior context (i.e. the user is starting something brand new), you should feel free to be ambitious and demonstrate creativity with your implementation. + +If you're operating in an existing codebase, you should make sure you do exactly what the user asks with surgical precision. Treat the surrounding codebase with respect, and don't overstep (i.e. changing filenames or variables unnecessarily). You should balance being sufficiently ambitious and proactive when completing tasks of this nature. + +You should use judicious initiative to decide on the right level of detail and complexity to deliver based on the user's needs. This means showing good judgment that you're capable of doing the right extras without gold-plating. This might be demonstrated by high-value, creative touches when scope of the task is vague; while being surgical and targeted when scope is tightly specified. + +## Sharing progress updates + +For especially longer tasks that you work on (i.e. requiring many tool calls, or a plan with multiple steps), you should provide progress updates back to the user at reasonable intervals. These updates should be structured as a concise sentence or two (no more than 8-10 words long) recapping progress so far in plain language: this update demonstrates your understanding of what needs to be done, progress so far (i.e. files explores, subtasks complete), and where you're going next. + +Before doing large chunks of work that may incur latency as experienced by the user (i.e. writing a new file), you should send a concise message to the user with an update indicating what you're about to do to ensure they know what you're spending time on. Don't start editing or writing large files before informing the user what you are doing and why. + +The messages you send before tool calls should describe what is immediately about to be done next in very concise language. If there was previous work done, this preamble message should also include a note about the work done so far to bring the user along. + +## Presenting your work and final message + +Your final message should read naturally, like an update from a concise teammate. For casual conversation, brainstorming tasks, or quick questions from the user, respond in a friendly, conversational tone. You should ask questions, suggest ideas, and adapt to the user’s style. If you've finished a large amount of work, when describing what you've done to the user, you should follow the final answer formatting guidelines to communicate substantive changes. You don't need to add structured formatting for one-word answers, greetings, or purely conversational exchanges. + +You can skip heavy formatting for single, simple actions or confirmations. In these cases, respond in plain sentences with any relevant next step or quick option. Reserve multi-section structured responses for results that need grouping or explanation. + +The user is working on the same computer as you, and has access to your work. As such there's no need to show the full contents of large files you have already written unless the user explicitly asks for them. Similarly, if you've created or modified files using `apply_patch`, there's no need to tell users to "save the file" or "copy the code into a file"—just reference the file path. + +If there's something that you think you could help with as a logical next step, concisely ask the user if they want you to do so. Good examples of this are running tests, committing changes, or building out the next logical component. If there’s something that you couldn't do (even with approval) but that the user might want to do (such as verifying changes by running the app), include those instructions succinctly. + +Brevity is very important as a default. You should be very concise (i.e. no more than 10 lines), but can relax this requirement for tasks where additional detail and comprehensiveness is important for the user's understanding. + +### Final answer structure and style guidelines + +You are producing plain text that will later be styled by the CLI. Follow these rules exactly. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value. + +**Section Headers** + +- Use only when they improve clarity — they are not mandatory for every answer. +- Choose descriptive names that fit the content +- Keep headers short (1–3 words) and in `**Title Case**`. Always start headers with `**` and end with `**` +- Leave no blank line before the first bullet under a header. +- Section headers should only be used where they genuinely improve scanability; avoid fragmenting the answer. + +**Bullets** + +- Use `-` followed by a space for every bullet. +- Merge related points when possible; avoid a bullet for every trivial detail. +- Keep bullets to one line unless breaking for clarity is unavoidable. +- Group into short lists (4–6 bullets) ordered by importance. +- Use consistent keyword phrasing and formatting across sections. + +**Monospace** + +- Wrap all commands, file paths, env vars, and code identifiers in backticks (`` `...` ``). +- Apply to inline examples and to bullet keywords if the keyword itself is a literal file/command. +- Never mix monospace and bold markers; choose one based on whether it’s a keyword (`**`) or inline code/path (`` ` ``). + +**File References** +When referencing files in your response, make sure to include the relevant start line and always follow the below rules: + * Use inline code to make file paths clickable. + * Each reference should have a stand alone path. Even if it's the same file. + * Accepted: absolute, workspace‑relative, a/ or b/ diff prefixes, or bare filename/suffix. + * Line/column (1‑based, optional): :line[:column] or #Lline[Ccolumn] (column defaults to 1). + * Do not use URIs like file://, vscode://, or https://. + * Do not provide range of lines + * Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\repo\project\main.rs:12:5 + +**Structure** + +- Place related bullets together; don’t mix unrelated concepts in the same section. +- Order sections from general → specific → supporting info. +- For subsections (e.g., “Binaries” under “Rust Workspace”), introduce with a bolded keyword bullet, then list items under it. +- Match structure to complexity: + - Multi-part or detailed results → use clear headers and grouped bullets. + - Simple results → minimal headers, possibly just a short list or paragraph. + +**Tone** + +- Keep the voice collaborative and natural, like a coding partner handing off work. +- Be concise and factual — no filler or conversational commentary and avoid unnecessary repetition +- Use present tense and active voice (e.g., “Runs tests” not “This will run tests”). +- Keep descriptions self-contained; don’t refer to “above” or “below”. +- Use parallel structure in lists for consistency. + +**Don’t** + +- Don’t use literal words “bold” or “monospace” in the content. +- Don’t nest bullets or create deep hierarchies. +- Don’t output ANSI escape codes directly — the CLI renderer applies them. +- Don’t cram unrelated keywords into a single bullet; split for clarity. +- Don’t let keyword lists run long — wrap or reformat for scanability. + +Generally, ensure your final answers adapt their shape and depth to the request. For example, answers to code explanations should have a precise, structured explanation with code references that answer the question directly. For tasks with a simple implementation, lead with the outcome and supplement only with what’s needed for clarity. Larger changes can be presented as a logical walkthrough of your approach, grouping related steps, explaining rationale where it adds value, and highlighting next actions to accelerate the user. Your answers should provide the right level of detail while being easily scannable. + +For casual greetings, acknowledgements, or other one-off conversational messages that are not delivering substantive information or structured results, respond naturally without section headers or bullet formatting. + +# Tool Guidelines + +## Shell commands + +When using the shell, you must adhere to the following guidelines: + +- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.) +- Do not use python scripts to attempt to output larger chunks of a file. + +## `update_plan` + +A tool named `update_plan` is available to you. You can use it to keep an up‑to‑date, step‑by‑step plan for the task. + +To create a new plan, call `update_plan` with a short list of 1‑sentence steps (no more than 5-7 words each) with a `status` for each step (`pending`, `in_progress`, or `completed`). + +When steps have been completed, use `update_plan` to mark each finished step as `completed` and the next step you are working on as `in_progress`. There should always be exactly one `in_progress` step until everything is done. You can mark multiple items as complete in a single `update_plan` call. + +If all steps are complete, ensure you call `update_plan` to mark all steps as `completed`. + +## `apply_patch` + +Use the `apply_patch` shell command to edit files. +Your patch language is a stripped‑down, file‑oriented diff format designed to be easy to parse and safe to apply. You can think of it as a high‑level envelope: + +*** Begin Patch +[ one or more file sections ] +*** End Patch + +Within that envelope, you get a sequence of file operations. +You MUST include a header to specify the action you are taking. +Each operation starts with one of three headers: + +*** Add File: - create a new file. Every following line is a + line (the initial contents). +*** Delete File: - remove an existing file. Nothing follows. +*** Update File: - patch an existing file in place (optionally with a rename). + +May be immediately followed by *** Move to: if you want to rename the file. +Then one or more “hunks”, each introduced by @@ (optionally followed by a hunk header). +Within a hunk each line starts with: + +For instructions on [context_before] and [context_after]: +- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines. +- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have: +@@ class BaseClass +[3 lines of pre-context] +- [old_code] ++ [new_code] +[3 lines of post-context] + +- If a code block is repeated so many times in a class or function such that even a single `@@` statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance: + +@@ class BaseClass +@@ def method(): +[3 lines of pre-context] +- [old_code] ++ [new_code] +[3 lines of post-context] + +The full grammar definition is below: +Patch := Begin { FileOp } End +Begin := "*** Begin Patch" NEWLINE +End := "*** End Patch" NEWLINE +FileOp := AddFile | DeleteFile | UpdateFile +AddFile := "*** Add File: " path NEWLINE { "+" line NEWLINE } +DeleteFile := "*** Delete File: " path NEWLINE +UpdateFile := "*** Update File: " path NEWLINE [ MoveTo ] { Hunk } +MoveTo := "*** Move to: " newPath NEWLINE +Hunk := "@@" [ header ] NEWLINE { HunkLine } [ "*** End of File" NEWLINE ] +HunkLine := (" " | "-" | "+") text NEWLINE + +A full patch can combine several operations: + +*** Begin Patch +*** Add File: hello.txt ++Hello world +*** Update File: src/app.py +*** Move to: src/main.py +@@ def greet(): +-print("Hi") ++print("Hello, world!") +*** Delete File: obsolete.txt +*** End Patch + +It is important to remember: + +- You must include a header with your intended action (Add/Delete/Update) +- You must prefix new lines with `+` even when creating a new file +- File references can only be relative, NEVER ABSOLUTE. + +You can invoke apply_patch like: + +``` +shell {"command":["apply_patch","*** Begin Patch\n*** Add File: hello.txt\n+Hello, world!\n*** End Patch\n"]} +``` diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 785d0475b72..c43c074c44c 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -22,6 +22,7 @@ use codex_otel::otel_manager::OtelManager; use codex_protocol::ThreadId; use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; use codex_protocol::models::ResponseItem; +use codex_protocol::openai_models::ModelInfo; use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::protocol::SessionSource; use eventsource_stream::Event; @@ -49,7 +50,6 @@ use crate::features::FEATURES; use crate::flags::CODEX_RS_SSE_FIXTURE; use crate::model_provider_info::ModelProviderInfo; use crate::model_provider_info::WireApi; -use crate::models_manager::model_family::ModelFamily; use crate::tools::spec::create_tools_json_for_chat_completions_api; use crate::tools::spec::create_tools_json_for_responses_api; @@ -57,7 +57,7 @@ use crate::tools::spec::create_tools_json_for_responses_api; pub struct ModelClient { config: Arc, auth_manager: Option>, - model_family: ModelFamily, + model_info: ModelInfo, otel_manager: OtelManager, provider: ModelProviderInfo, conversation_id: ThreadId, @@ -71,7 +71,7 @@ impl ModelClient { pub fn new( config: Arc, auth_manager: Option>, - model_family: ModelFamily, + model_info: ModelInfo, otel_manager: OtelManager, provider: ModelProviderInfo, effort: Option, @@ -82,7 +82,7 @@ impl ModelClient { Self { config, auth_manager, - model_family, + model_info, otel_manager, provider, conversation_id, @@ -93,11 +93,11 @@ impl ModelClient { } pub fn get_model_context_window(&self) -> Option { - let model_family = self.get_model_family(); - let effective_context_window_percent = model_family.effective_context_window_percent; - model_family - .context_window - .map(|w| w.saturating_mul(effective_context_window_percent) / 100) + let model_info = self.get_model_info(); + let effective_context_window_percent = model_info.effective_context_window_percent; + model_info.context_window.map(|context_window| { + context_window.saturating_mul(effective_context_window_percent) / 100 + }) } pub fn config(&self) -> Arc { @@ -146,8 +146,8 @@ impl ModelClient { } let auth_manager = self.auth_manager.clone(); - let model_family = self.get_model_family(); - let instructions = prompt.get_full_instructions(&model_family).into_owned(); + let model_info = self.get_model_info(); + let instructions = prompt.get_full_instructions(&model_info).into_owned(); let tools_json = create_tools_json_for_chat_completions_api(&prompt.tools)?; let api_prompt = build_api_prompt(prompt, instructions, tools_json); let conversation_id = self.conversation_id.to_string(); @@ -200,13 +200,14 @@ impl ModelClient { } let auth_manager = self.auth_manager.clone(); - let model_family = self.get_model_family(); - let instructions = prompt.get_full_instructions(&model_family).into_owned(); + let model_info = self.get_model_info(); + let instructions = prompt.get_full_instructions(&model_info).into_owned(); let tools_json: Vec = create_tools_json_for_responses_api(&prompt.tools)?; - let reasoning = if model_family.supports_reasoning_summaries { + let default_reasoning_effort = model_info.default_reasoning_level; + let reasoning = if model_info.supports_reasoning_summaries { Some(Reasoning { - effort: self.effort.or(model_family.default_reasoning_effort), + effort: self.effort.or(default_reasoning_effort), summary: if self.summary == ReasoningSummaryConfig::None { None } else { @@ -223,15 +224,13 @@ impl ModelClient { vec![] }; - let verbosity = if model_family.support_verbosity { - self.config - .model_verbosity - .or(model_family.default_verbosity) + let verbosity = if model_info.support_verbosity { + self.config.model_verbosity.or(model_info.default_verbosity) } else { if self.config.model_verbosity.is_some() { warn!( "model_verbosity is set but ignored as the model does not support verbosity: {}", - model_family.family + model_info.slug ); } None @@ -298,12 +297,11 @@ impl ModelClient { /// Returns the currently configured model slug. pub fn get_model(&self) -> String { - self.get_model_family().get_model_slug().to_string() + self.model_info.slug.clone() } - /// Returns the currently configured model family. - pub fn get_model_family(&self) -> ModelFamily { - self.model_family.clone() + pub fn get_model_info(&self) -> ModelInfo { + self.model_info.clone() } /// Returns the current reasoning effort setting. @@ -340,7 +338,7 @@ impl ModelClient { .with_telemetry(Some(request_telemetry)); let instructions = prompt - .get_full_instructions(&self.get_model_family()) + .get_full_instructions(&self.get_model_info()) .into_owned(); let payload = ApiCompactionInput { model: &self.get_model(), diff --git a/codex-rs/core/src/client_common.rs b/codex-rs/core/src/client_common.rs index 6972ff7cece..7d7cabcfa61 100644 --- a/codex-rs/core/src/client_common.rs +++ b/codex-rs/core/src/client_common.rs @@ -1,15 +1,13 @@ use crate::client_common::tools::ToolSpec; use crate::error::Result; -use crate::models_manager::model_family::ModelFamily; pub use codex_api::common::ResponseEvent; -use codex_apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; use codex_protocol::models::ResponseItem; +use codex_protocol::openai_models::ModelInfo; use futures::Stream; use serde::Deserialize; use serde_json::Value; use std::borrow::Cow; use std::collections::HashSet; -use std::ops::Deref; use std::pin::Pin; use std::task::Context; use std::task::Poll; @@ -44,28 +42,12 @@ pub struct Prompt { } impl Prompt { - pub(crate) fn get_full_instructions<'a>(&'a self, model: &'a ModelFamily) -> Cow<'a, str> { - let base = self - .base_instructions_override - .as_deref() - .unwrap_or(model.base_instructions.deref()); - // When there are no custom instructions, add apply_patch_tool_instructions if: - // - the model needs special instructions (4.1) - // AND - // - there is no apply_patch tool present - let is_apply_patch_tool_present = self.tools.iter().any(|tool| match tool { - ToolSpec::Function(f) => f.name == "apply_patch", - ToolSpec::Freeform(f) => f.name == "apply_patch", - _ => false, - }); - if self.base_instructions_override.is_none() - && model.needs_special_apply_patch_instructions - && !is_apply_patch_tool_present - { - Cow::Owned(format!("{base}\n{APPLY_PATCH_TOOL_INSTRUCTIONS}")) - } else { - Cow::Borrowed(base) - } + pub(crate) fn get_full_instructions<'a>(&'a self, model: &'a ModelInfo) -> Cow<'a, str> { + Cow::Borrowed( + self.base_instructions_override + .as_deref() + .unwrap_or(model.base_instructions.as_str()), + ) } pub(crate) fn get_formatted_input(&self) -> Vec { @@ -277,6 +259,8 @@ mod tests { let prompt = Prompt { ..Default::default() }; + let prompt_with_apply_patch_instructions = + include_str!("../prompt_with_apply_patch_instructions.md"); let test_cases = vec![ InstructionsTestCase { slug: "gpt-3.5", @@ -317,19 +301,16 @@ mod tests { ]; for test_case in test_cases { let config = test_config(); - let model_family = - ModelsManager::construct_model_family_offline(test_case.slug, &config); - let expected = if test_case.expects_apply_patch_instructions { - format!( - "{}\n{}", - model_family.clone().base_instructions, - APPLY_PATCH_TOOL_INSTRUCTIONS - ) - } else { - model_family.clone().base_instructions - }; - - let full = prompt.get_full_instructions(&model_family); + let model_info = ModelsManager::construct_model_info_offline(test_case.slug, &config); + if test_case.expects_apply_patch_instructions { + assert_eq!( + model_info.base_instructions.as_str(), + prompt_with_apply_patch_instructions + ); + } + + let expected = model_info.base_instructions.as_str(); + let full = prompt.get_full_instructions(&model_info); assert_eq!(full, expected); } } diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 828a6bab017..a496d246ebf 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -20,7 +20,6 @@ use crate::exec_policy::ExecPolicyManager; use crate::features::Feature; use crate::features::Features; use crate::models_manager::manager::ModelsManager; -use crate::models_manager::model_family::ModelFamily; use crate::parse_command::parse_command; use crate::parse_turn_item; use crate::stream_events_utils::HandleOutputCtx; @@ -35,6 +34,7 @@ use async_channel::Sender; use codex_protocol::ThreadId; use codex_protocol::approvals::ExecPolicyAmendment; use codex_protocol::items::TurnItem; +use codex_protocol::openai_models::ModelInfo; use codex_protocol::protocol::FileChange; use codex_protocol::protocol::HasLegacyEvent; use codex_protocol::protocol::ItemCompletedEvent; @@ -508,20 +508,20 @@ impl Session { provider: ModelProviderInfo, session_configuration: &SessionConfiguration, per_turn_config: Config, - model_family: ModelFamily, + model_info: ModelInfo, conversation_id: ThreadId, sub_id: String, ) -> TurnContext { let otel_manager = otel_manager.clone().with_model( session_configuration.model.as_str(), - model_family.get_model_slug(), + model_info.slug.as_str(), ); let per_turn_config = Arc::new(per_turn_config); let client = ModelClient::new( per_turn_config.clone(), auth_manager, - model_family.clone(), + model_info.clone(), otel_manager, provider, session_configuration.model_reasoning_effort, @@ -531,7 +531,7 @@ impl Session { ); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &per_turn_config.features, }); @@ -553,7 +553,7 @@ impl Session { tool_call_gate: Arc::new(ReadinessFlag::new()), truncation_policy: TruncationPolicy::new( per_turn_config.as_ref(), - model_family.truncation_policy, + model_info.truncation_policy.into(), ), } } @@ -944,10 +944,10 @@ impl Session { } } - let model_family = self + let model_info = self .services .models_manager - .construct_model_family(session_configuration.model.as_str(), &per_turn_config) + .construct_model_info(session_configuration.model.as_str(), &per_turn_config) .await; let mut turn_context: TurnContext = Self::make_turn_context( Some(Arc::clone(&self.services.auth_manager)), @@ -955,7 +955,7 @@ impl Session { session_configuration.provider.clone(), &session_configuration, per_turn_config, - model_family, + model_info, self.conversation_id, sub_id, ); @@ -1448,14 +1448,11 @@ impl Session { } pub(crate) async fn set_total_tokens_full(&self, turn_context: &TurnContext) { - let context_window = turn_context.client.get_model_context_window(); - if let Some(context_window) = context_window { - { - let mut state = self.state.lock().await; - state.set_token_usage_full(context_window); - } - self.send_token_count_event(turn_context).await; + if let Some(context_window) = turn_context.client.get_model_context_window() { + let mut state = self.state.lock().await; + state.set_token_usage_full(context_window); } + self.send_token_count_event(turn_context).await; } pub(crate) async fn record_response_item_and_emit_turn_item( @@ -2207,10 +2204,10 @@ async fn spawn_review_thread( resolved: crate::review_prompts::ResolvedReviewRequest, ) { let model = config.review_model.clone(); - let review_model_family = sess + let review_model_info = sess .services .models_manager - .construct_model_family(&model, &config) + .construct_model_info(&model, &config) .await; // For reviews, disable web_search and view_image regardless of global settings. let mut review_features = sess.features.clone(); @@ -2219,7 +2216,7 @@ async fn spawn_review_thread( .disable(crate::features::Feature::WebSearchCached) .disable(crate::features::Feature::ViewImageTool); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &review_model_family, + model_info: &review_model_info, features: &review_features, }); @@ -2227,7 +2224,7 @@ async fn spawn_review_thread( let review_prompt = resolved.prompt.clone(); let provider = parent_turn_context.client.get_provider(); let auth_manager = parent_turn_context.client.get_auth_manager(); - let model_family = review_model_family.clone(); + let model_info = review_model_info.clone(); // Build per‑turn client with the requested model/family. let mut per_turn_config = (*config).clone(); @@ -2237,14 +2234,14 @@ async fn spawn_review_thread( let otel_manager = parent_turn_context.client.get_otel_manager().with_model( config.review_model.as_str(), - review_model_family.slug.as_str(), + review_model_info.slug.as_str(), ); let per_turn_config = Arc::new(per_turn_config); let client = ModelClient::new( per_turn_config.clone(), auth_manager, - model_family.clone(), + model_info.clone(), otel_manager, provider, per_turn_config.model_reasoning_effort, @@ -2269,7 +2266,10 @@ async fn spawn_review_thread( final_output_json_schema: None, codex_linux_sandbox_exe: parent_turn_context.codex_linux_sandbox_exe.clone(), tool_call_gate: Arc::new(ReadinessFlag::new()), - truncation_policy: TruncationPolicy::new(&per_turn_config, model_family.truncation_policy), + truncation_policy: TruncationPolicy::new( + &per_turn_config, + model_info.truncation_policy.into(), + ), }; // Seed the child task with the review prompt as the initial user message. @@ -2335,11 +2335,8 @@ pub(crate) async fn run_task( return None; } - let auto_compact_limit = turn_context - .client - .get_model_family() - .auto_compact_token_limit() - .unwrap_or(i64::MAX); + let model_info = turn_context.client.get_model_info(); + let auto_compact_limit = model_info.auto_compact_token_limit().unwrap_or(i64::MAX); let total_usage_tokens = sess.get_total_token_usage().await; if total_usage_tokens >= auto_compact_limit { run_auto_compact(&sess, &turn_context).await; @@ -2517,7 +2514,7 @@ async fn run_turn( let model_supports_parallel = turn_context .client - .get_model_family() + .get_model_info() .supports_parallel_tool_calls; let prompt = Prompt { @@ -3459,13 +3456,13 @@ mod tests { fn otel_manager( conversation_id: ThreadId, config: &Config, - model_family: &ModelFamily, + model_info: &ModelInfo, session_source: SessionSource, ) -> OtelManager { OtelManager::new( conversation_id, ModelsManager::get_model_offline(config.model.as_deref()).as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), Some(AuthMode::ChatGPT), @@ -3504,14 +3501,14 @@ mod tests { session_source: SessionSource::Exec, }; let per_turn_config = Session::build_per_turn_config(&session_configuration); - let model_family = ModelsManager::construct_model_family_offline( + let model_info = ModelsManager::construct_model_info_offline( session_configuration.model.as_str(), &per_turn_config, ); let otel_manager = otel_manager( conversation_id, config.as_ref(), - &model_family, + &model_info, session_configuration.session_source.clone(), ); @@ -3541,7 +3538,7 @@ mod tests { session_configuration.provider.clone(), &session_configuration, per_turn_config, - model_family, + model_info, conversation_id, "turn_id".to_string(), ); @@ -3595,14 +3592,14 @@ mod tests { session_source: SessionSource::Exec, }; let per_turn_config = Session::build_per_turn_config(&session_configuration); - let model_family = ModelsManager::construct_model_family_offline( + let model_info = ModelsManager::construct_model_info_offline( session_configuration.model.as_str(), &per_turn_config, ); let otel_manager = otel_manager( conversation_id, config.as_ref(), - &model_family, + &model_info, session_configuration.session_source.clone(), ); @@ -3632,7 +3629,7 @@ mod tests { session_configuration.provider.clone(), &session_configuration, per_turn_config, - model_family, + model_info, conversation_id, "turn_id".to_string(), )); diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 035794f4aa9..e2c3bb252d8 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -268,7 +268,7 @@ pub struct Config { /// Additional filenames to try when looking for project-level docs. pub project_doc_fallback_filenames: Vec, - // todo(aibrahim): this should be used in the override model family + // todo(aibrahim): this should be used in the override model info /// Token budget applied when storing tool/function outputs in the context manager. pub tool_output_token_limit: Option, @@ -316,7 +316,7 @@ pub struct Config { /// Include the `apply_patch` tool for models that benefit from invoking /// file edits as a structured tool call. When unset, this falls back to the - /// model family's default preference. + /// model info's default preference. pub include_apply_patch_tool: bool, pub tools_web_search_request: bool, diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 8dc55187a59..8f16f731f49 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -83,10 +83,9 @@ impl ContextManager { // Estimate token usage using byte-based heuristics from the truncation helpers. // This is a coarse lower bound, not a tokenizer-accurate count. pub(crate) fn estimate_token_count(&self, turn_context: &TurnContext) -> Option { - let model_family = turn_context.client.get_model_family(); - let base_tokens = - i64::try_from(approx_token_count(model_family.base_instructions.as_str())) - .unwrap_or(i64::MAX); + let model_info = turn_context.client.get_model_info(); + let base_instructions = model_info.base_instructions.as_str(); + let base_tokens = i64::try_from(approx_token_count(base_instructions)).unwrap_or(i64::MAX); let items_tokens = self.items.iter().fold(0i64, |acc, item| { acc + match item { diff --git a/codex-rs/core/src/models_manager/manager.rs b/codex-rs/core/src/models_manager/manager.rs index 29be88735be..c9185f0ff04 100644 --- a/codex-rs/core/src/models_manager/manager.rs +++ b/codex-rs/core/src/models_manager/manager.rs @@ -24,7 +24,7 @@ use crate::default_client::build_reqwest_client; use crate::error::Result as CoreResult; use crate::features::Feature; use crate::model_provider_info::ModelProviderInfo; -use crate::models_manager::model_family::ModelFamily; +use crate::models_manager::model_info; use crate::models_manager::model_presets::builtin_model_presets; const MODEL_CACHE_FILE: &str = "models_cache.json"; @@ -36,7 +36,6 @@ const CODEX_AUTO_BALANCED_MODEL: &str = "codex-auto-balanced"; /// Coordinates remote model discovery plus cached metadata on disk. #[derive(Debug)] pub struct ModelsManager { - // todo(aibrahim) merge available_models and model family creation into one struct local_models: Vec, remote_models: RwLock>, auth_manager: Arc, @@ -128,15 +127,19 @@ impl ModelsManager { Ok(self.build_available_models(remote_models)) } - fn find_family_for_model(slug: &str) -> ModelFamily { - super::model_family::find_family_for_model(slug) - } - - /// Look up the requested model family while applying remote metadata overrides. - pub async fn construct_model_family(&self, model: &str, config: &Config) -> ModelFamily { - Self::find_family_for_model(model) - .with_remote_overrides(self.remote_models(config).await) - .with_config_overrides(config) + /// Look up the requested model metadata while applying remote metadata overrides. + pub async fn construct_model_info(&self, model: &str, config: &Config) -> ModelInfo { + let remote = self + .remote_models(config) + .await + .into_iter() + .find(|m| m.slug == model); + let model = if let Some(remote) = remote { + remote + } else { + model_info::find_model_info_for_slug(model) + }; + model_info::with_config_overrides(model, config) } pub async fn get_model(&self, model: &Option, config: &Config) -> String { @@ -180,9 +183,9 @@ impl ModelsManager { } #[cfg(any(test, feature = "test-support"))] - /// Offline helper that builds a `ModelFamily` without consulting remote state. - pub fn construct_model_family_offline(model: &str, config: &Config) -> ModelFamily { - Self::find_family_for_model(model).with_config_overrides(config) + /// Offline helper that builds a `ModelInfo` without consulting remote state. + pub fn construct_model_info_offline(model: &str, config: &Config) -> ModelInfo { + model_info::with_config_overrides(model_info::find_model_info_for_slug(model), config) } async fn get_etag(&self) -> Option { @@ -363,14 +366,14 @@ mod tests { "supported_in_api": true, "priority": priority, "upgrade": null, - "base_instructions": null, + "base_instructions": "base instructions", "supports_reasoning_summaries": false, "support_verbosity": false, "default_verbosity": null, "apply_patch_tool_type": null, "truncation_policy": {"mode": "bytes", "limit": 10_000}, "supports_parallel_tool_calls": false, - "context_window": null, + "context_window": 272_000, "experimental_supported_tools": [], })) .expect("valid model") diff --git a/codex-rs/core/src/models_manager/mod.rs b/codex-rs/core/src/models_manager/mod.rs index 83ed30e8724..d0e3c8214a5 100644 --- a/codex-rs/core/src/models_manager/mod.rs +++ b/codex-rs/core/src/models_manager/mod.rs @@ -1,4 +1,4 @@ pub mod cache; pub mod manager; -pub mod model_family; +pub mod model_info; pub mod model_presets; diff --git a/codex-rs/core/src/models_manager/model_family.rs b/codex-rs/core/src/models_manager/model_family.rs deleted file mode 100644 index 9a6904cea1e..00000000000 --- a/codex-rs/core/src/models_manager/model_family.rs +++ /dev/null @@ -1,557 +0,0 @@ -use codex_protocol::config_types::Verbosity; -use codex_protocol::openai_models::ApplyPatchToolType; -use codex_protocol::openai_models::ConfigShellToolType; -use codex_protocol::openai_models::ModelInfo; -use codex_protocol::openai_models::ReasoningEffort; - -use crate::config::Config; -use crate::truncate::TruncationPolicy; - -/// The `instructions` field in the payload sent to a model should always start -/// with this content. -const BASE_INSTRUCTIONS: &str = include_str!("../../prompt.md"); - -const GPT_5_CODEX_INSTRUCTIONS: &str = include_str!("../../gpt_5_codex_prompt.md"); -const GPT_5_1_INSTRUCTIONS: &str = include_str!("../../gpt_5_1_prompt.md"); -const GPT_5_2_INSTRUCTIONS: &str = include_str!("../../gpt_5_2_prompt.md"); -const GPT_5_1_CODEX_MAX_INSTRUCTIONS: &str = include_str!("../../gpt-5.1-codex-max_prompt.md"); -const GPT_5_2_CODEX_INSTRUCTIONS: &str = include_str!("../../gpt-5.2-codex_prompt.md"); -pub(crate) const CONTEXT_WINDOW_272K: i64 = 272_000; - -/// A model family is a group of models that share certain characteristics. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ModelFamily { - /// The full model slug used to derive this model family, e.g. - /// "gpt-4.1-2025-04-14". - pub slug: String, - - /// The model family name, e.g. "gpt-4.1". This string is used when deriving - /// default metadata for the family, such as context windows. - pub family: String, - - /// True if the model needs additional instructions on how to use the - /// "virtual" `apply_patch` CLI. - pub needs_special_apply_patch_instructions: bool, - - /// Maximum supported context window, if known. - pub context_window: Option, - - /// Token threshold for automatic compaction if config does not override it. - auto_compact_token_limit: Option, - - // Whether the `reasoning` field can be set when making a request to this - // model family. Note it has `effort` and `summary` subfields (though - // `summary` is optional). - pub supports_reasoning_summaries: bool, - - // The reasoning effort to use for this model family when none is explicitly chosen. - pub default_reasoning_effort: Option, - - /// Whether this model supports parallel tool calls when using the - /// Responses API. - pub supports_parallel_tool_calls: bool, - - /// Present if the model performs better when `apply_patch` is provided as - /// a tool call instead of just a bash command - pub apply_patch_tool_type: Option, - - // Instructions to use for querying the model - pub base_instructions: String, - - /// Names of beta tools that should be exposed to this model family. - pub experimental_supported_tools: Vec, - - /// Percentage of the context window considered usable for inputs, after - /// reserving headroom for system prompts, tool overhead, and model output. - /// This is applied when computing the effective context window seen by - /// consumers. - pub effective_context_window_percent: i64, - - /// If the model family supports setting the verbosity level when using Responses API. - pub support_verbosity: bool, - - // The default verbosity level for this model family when using Responses API. - pub default_verbosity: Option, - - /// Preferred shell tool type for this model family when features do not override it. - pub shell_type: ConfigShellToolType, - - pub truncation_policy: TruncationPolicy, -} - -impl ModelFamily { - pub(super) fn with_config_overrides(mut self, config: &Config) -> Self { - if let Some(supports_reasoning_summaries) = config.model_supports_reasoning_summaries { - self.supports_reasoning_summaries = supports_reasoning_summaries; - } - if let Some(context_window) = config.model_context_window { - self.context_window = Some(context_window); - } - if let Some(auto_compact_token_limit) = config.model_auto_compact_token_limit { - self.auto_compact_token_limit = Some(auto_compact_token_limit); - } - self - } - pub(super) fn with_remote_overrides(mut self, remote_models: Vec) -> Self { - for model in remote_models { - if model.slug == self.slug { - self.apply_remote_overrides(model); - } - } - self - } - - fn apply_remote_overrides(&mut self, model: ModelInfo) { - let ModelInfo { - slug: _, - display_name: _, - description: _, - default_reasoning_level, - supported_reasoning_levels: _, - shell_type, - visibility: _, - supported_in_api: _, - priority: _, - upgrade: _, - base_instructions, - supports_reasoning_summaries, - support_verbosity, - default_verbosity, - apply_patch_tool_type, - truncation_policy, - supports_parallel_tool_calls, - context_window, - experimental_supported_tools, - } = model; - - self.default_reasoning_effort = Some(default_reasoning_level); - self.shell_type = shell_type; - if let Some(base) = base_instructions { - self.base_instructions = base; - } - self.supports_reasoning_summaries = supports_reasoning_summaries; - self.support_verbosity = support_verbosity; - self.default_verbosity = default_verbosity; - self.apply_patch_tool_type = apply_patch_tool_type; - self.truncation_policy = truncation_policy.into(); - self.supports_parallel_tool_calls = supports_parallel_tool_calls; - self.context_window = context_window; - self.experimental_supported_tools = experimental_supported_tools; - } - - pub fn auto_compact_token_limit(&self) -> Option { - self.auto_compact_token_limit - .or(self.context_window.map(Self::default_auto_compact_limit)) - } - - const fn default_auto_compact_limit(context_window: i64) -> i64 { - (context_window * 9) / 10 - } - - pub fn get_model_slug(&self) -> &str { - &self.slug - } -} - -macro_rules! model_family { - ( - $slug:expr, $family:expr $(, $key:ident : $value:expr )* $(,)? - ) => {{ - // defaults - #[allow(unused_mut)] - let mut mf = ModelFamily { - slug: $slug.to_string(), - family: $family.to_string(), - needs_special_apply_patch_instructions: false, - context_window: Some(CONTEXT_WINDOW_272K), - auto_compact_token_limit: None, - supports_reasoning_summaries: false, - supports_parallel_tool_calls: false, - apply_patch_tool_type: None, - base_instructions: BASE_INSTRUCTIONS.to_string(), - experimental_supported_tools: Vec::new(), - effective_context_window_percent: 95, - support_verbosity: false, - shell_type: ConfigShellToolType::Default, - default_verbosity: None, - default_reasoning_effort: None, - truncation_policy: TruncationPolicy::Bytes(10_000), - }; - - // apply overrides - $( - mf.$key = $value; - )* - mf - }}; -} - -/// Internal offline helper for `ModelsManager` that returns a `ModelFamily` for the given -/// model slug. -#[allow(clippy::if_same_then_else)] -pub(super) fn find_family_for_model(slug: &str) -> ModelFamily { - if slug.starts_with("o3") { - model_family!( - slug, "o3", - supports_reasoning_summaries: true, - needs_special_apply_patch_instructions: true, - context_window: Some(200_000), - ) - } else if slug.starts_with("o4-mini") { - model_family!( - slug, "o4-mini", - supports_reasoning_summaries: true, - needs_special_apply_patch_instructions: true, - context_window: Some(200_000), - ) - } else if slug.starts_with("codex-mini-latest") { - model_family!( - slug, "codex-mini-latest", - supports_reasoning_summaries: true, - needs_special_apply_patch_instructions: true, - shell_type: ConfigShellToolType::Local, - context_window: Some(200_000), - ) - } else if slug.starts_with("gpt-4.1") { - model_family!( - slug, "gpt-4.1", - needs_special_apply_patch_instructions: true, - context_window: Some(1_047_576), - ) - } else if slug.starts_with("gpt-oss") || slug.starts_with("openai/gpt-oss") { - model_family!( - slug, "gpt-oss", - apply_patch_tool_type: Some(ApplyPatchToolType::Function), - context_window: Some(96_000), - ) - } else if slug.starts_with("gpt-4o") { - model_family!( - slug, "gpt-4o", - needs_special_apply_patch_instructions: true, - context_window: Some(128_000), - ) - } else if slug.starts_with("gpt-3.5") { - model_family!( - slug, "gpt-3.5", - needs_special_apply_patch_instructions: true, - context_window: Some(16_385), - ) - } else if slug.starts_with("test-gpt-5") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), - experimental_supported_tools: vec![ - "grep_files".to_string(), - "list_dir".to_string(), - "read_file".to_string(), - "test_sync_tool".to_string(), - ], - supports_parallel_tool_calls: true, - shell_type: ConfigShellToolType::ShellCommand, - support_verbosity: true, - truncation_policy: TruncationPolicy::Tokens(10_000), - ) - - // Experimental models. - } else if slug.starts_with("exp-codex") || slug.starts_with("codex-1p") { - // Same as gpt-5.1-codex-max. - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_2_CODEX_INSTRUCTIONS.to_string(), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - support_verbosity: false, - truncation_policy: TruncationPolicy::Tokens(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("exp-") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - support_verbosity: true, - default_verbosity: Some(Verbosity::Low), - base_instructions: BASE_INSTRUCTIONS.to_string(), - default_reasoning_effort: Some(ReasoningEffort::Medium), - truncation_policy: TruncationPolicy::Bytes(10_000), - shell_type: ConfigShellToolType::UnifiedExec, - supports_parallel_tool_calls: true, - context_window: Some(CONTEXT_WINDOW_272K), - ) - - // Production models. - } else if slug.starts_with("gpt-5.2-codex") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_2_CODEX_INSTRUCTIONS.to_string(), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - support_verbosity: false, - truncation_policy: TruncationPolicy::Tokens(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("bengalfox") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_2_CODEX_INSTRUCTIONS.to_string(), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - support_verbosity: false, - truncation_policy: TruncationPolicy::Tokens(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("gpt-5.1-codex-max") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_1_CODEX_MAX_INSTRUCTIONS.to_string(), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: false, - support_verbosity: false, - truncation_policy: TruncationPolicy::Tokens(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("gpt-5-codex") - || slug.starts_with("gpt-5.1-codex") - || slug.starts_with("codex-") - { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: false, - support_verbosity: false, - truncation_policy: TruncationPolicy::Tokens(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("gpt-5.2") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - support_verbosity: true, - default_verbosity: Some(Verbosity::Low), - base_instructions: GPT_5_2_INSTRUCTIONS.to_string(), - default_reasoning_effort: Some(ReasoningEffort::Medium), - truncation_policy: TruncationPolicy::Bytes(10_000), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("boomslang") { - model_family!( - slug, slug, - supports_reasoning_summaries: true, - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - support_verbosity: true, - default_verbosity: Some(Verbosity::Low), - base_instructions: GPT_5_2_INSTRUCTIONS.to_string(), - default_reasoning_effort: Some(ReasoningEffort::Medium), - truncation_policy: TruncationPolicy::Bytes(10_000), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("gpt-5.1") { - model_family!( - slug, "gpt-5.1", - supports_reasoning_summaries: true, - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - support_verbosity: true, - default_verbosity: Some(Verbosity::Low), - base_instructions: GPT_5_1_INSTRUCTIONS.to_string(), - default_reasoning_effort: Some(ReasoningEffort::Medium), - truncation_policy: TruncationPolicy::Bytes(10_000), - shell_type: ConfigShellToolType::ShellCommand, - supports_parallel_tool_calls: true, - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else if slug.starts_with("gpt-5") { - model_family!( - slug, "gpt-5", - supports_reasoning_summaries: true, - needs_special_apply_patch_instructions: true, - shell_type: ConfigShellToolType::Default, - support_verbosity: true, - truncation_policy: TruncationPolicy::Bytes(10_000), - context_window: Some(CONTEXT_WINDOW_272K), - ) - } else { - derive_default_model_family(slug) - } -} - -fn derive_default_model_family(model: &str) -> ModelFamily { - tracing::warn!("Unknown model {model} is used. This will degrade the performance of Codex."); - ModelFamily { - slug: model.to_string(), - family: model.to_string(), - needs_special_apply_patch_instructions: false, - context_window: None, - auto_compact_token_limit: None, - supports_reasoning_summaries: false, - supports_parallel_tool_calls: false, - apply_patch_tool_type: None, - base_instructions: BASE_INSTRUCTIONS.to_string(), - experimental_supported_tools: Vec::new(), - effective_context_window_percent: 95, - support_verbosity: false, - shell_type: ConfigShellToolType::Default, - default_verbosity: None, - default_reasoning_effort: None, - truncation_policy: TruncationPolicy::Bytes(10_000), - } -} - -#[cfg(test)] -mod tests { - use super::*; - use codex_protocol::openai_models::ModelVisibility; - use codex_protocol::openai_models::ReasoningEffortPreset; - use codex_protocol::openai_models::TruncationPolicyConfig; - - fn remote(slug: &str, effort: ReasoningEffort, shell: ConfigShellToolType) -> ModelInfo { - ModelInfo { - slug: slug.to_string(), - display_name: slug.to_string(), - description: Some(format!("{slug} desc")), - default_reasoning_level: effort, - supported_reasoning_levels: vec![ReasoningEffortPreset { - effort, - description: effort.to_string(), - }], - shell_type: shell, - visibility: ModelVisibility::List, - supported_in_api: true, - priority: 1, - upgrade: None, - base_instructions: None, - supports_reasoning_summaries: false, - support_verbosity: false, - default_verbosity: None, - apply_patch_tool_type: None, - truncation_policy: TruncationPolicyConfig::bytes(10_000), - supports_parallel_tool_calls: false, - context_window: None, - experimental_supported_tools: Vec::new(), - } - } - - #[test] - fn remote_overrides_apply_when_slug_matches() { - let family = model_family!("gpt-4o-mini", "gpt-4o-mini"); - assert_ne!(family.default_reasoning_effort, Some(ReasoningEffort::High)); - - let updated = family.with_remote_overrides(vec![ - remote( - "gpt-4o-mini", - ReasoningEffort::High, - ConfigShellToolType::ShellCommand, - ), - remote( - "other-model", - ReasoningEffort::Low, - ConfigShellToolType::UnifiedExec, - ), - ]); - - assert_eq!( - updated.default_reasoning_effort, - Some(ReasoningEffort::High) - ); - assert_eq!(updated.shell_type, ConfigShellToolType::ShellCommand); - } - - #[test] - fn remote_overrides_skip_non_matching_models() { - let family = model_family!( - "codex-mini-latest", - "codex-mini-latest", - shell_type: ConfigShellToolType::Local - ); - - let updated = family.clone().with_remote_overrides(vec![remote( - "other", - ReasoningEffort::High, - ConfigShellToolType::ShellCommand, - )]); - - assert_eq!( - updated.default_reasoning_effort, - family.default_reasoning_effort - ); - assert_eq!(updated.shell_type, family.shell_type); - } - - #[test] - fn remote_overrides_apply_extended_metadata() { - let family = model_family!( - "gpt-5.1", - "gpt-5.1", - supports_reasoning_summaries: false, - support_verbosity: false, - default_verbosity: None, - apply_patch_tool_type: Some(ApplyPatchToolType::Function), - supports_parallel_tool_calls: false, - experimental_supported_tools: vec!["local".to_string()], - truncation_policy: TruncationPolicy::Bytes(10_000), - context_window: Some(100), - ); - - let updated = family.with_remote_overrides(vec![ModelInfo { - slug: "gpt-5.1".to_string(), - display_name: "gpt-5.1".to_string(), - description: Some("desc".to_string()), - default_reasoning_level: ReasoningEffort::High, - supported_reasoning_levels: vec![ReasoningEffortPreset { - effort: ReasoningEffort::High, - description: "High".to_string(), - }], - shell_type: ConfigShellToolType::ShellCommand, - visibility: ModelVisibility::List, - supported_in_api: true, - priority: 10, - upgrade: None, - base_instructions: Some("Remote instructions".to_string()), - supports_reasoning_summaries: true, - support_verbosity: true, - default_verbosity: Some(Verbosity::High), - apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), - truncation_policy: TruncationPolicyConfig::tokens(2_000), - supports_parallel_tool_calls: true, - context_window: Some(400_000), - experimental_supported_tools: vec!["alpha".to_string(), "beta".to_string()], - }]); - - assert_eq!( - updated.default_reasoning_effort, - Some(ReasoningEffort::High) - ); - assert!(updated.supports_reasoning_summaries); - assert!(updated.support_verbosity); - assert_eq!(updated.default_verbosity, Some(Verbosity::High)); - assert_eq!(updated.shell_type, ConfigShellToolType::ShellCommand); - assert_eq!( - updated.apply_patch_tool_type, - Some(ApplyPatchToolType::Freeform) - ); - assert_eq!(updated.truncation_policy, TruncationPolicy::Tokens(2_000)); - assert!(updated.supports_parallel_tool_calls); - assert_eq!(updated.context_window, Some(400_000)); - assert_eq!( - updated.experimental_supported_tools, - vec!["alpha".to_string(), "beta".to_string()] - ); - assert_eq!(updated.base_instructions, "Remote instructions"); - } -} diff --git a/codex-rs/core/src/models_manager/model_info.rs b/codex-rs/core/src/models_manager/model_info.rs new file mode 100644 index 00000000000..f907237f198 --- /dev/null +++ b/codex-rs/core/src/models_manager/model_info.rs @@ -0,0 +1,348 @@ +use codex_protocol::config_types::Verbosity; +use codex_protocol::openai_models::ApplyPatchToolType; +use codex_protocol::openai_models::ConfigShellToolType; +use codex_protocol::openai_models::ModelInfo; +use codex_protocol::openai_models::ModelVisibility; +use codex_protocol::openai_models::ReasoningEffort; +use codex_protocol::openai_models::ReasoningEffortPreset; +use codex_protocol::openai_models::TruncationPolicyConfig; + +use crate::config::Config; +use tracing::warn; + +const BASE_INSTRUCTIONS: &str = include_str!("../../prompt.md"); +const BASE_INSTRUCTIONS_WITH_APPLY_PATCH: &str = + include_str!("../../prompt_with_apply_patch_instructions.md"); + +const GPT_5_CODEX_INSTRUCTIONS: &str = include_str!("../../gpt_5_codex_prompt.md"); +const GPT_5_1_INSTRUCTIONS: &str = include_str!("../../gpt_5_1_prompt.md"); +const GPT_5_2_INSTRUCTIONS: &str = include_str!("../../gpt_5_2_prompt.md"); +const GPT_5_1_CODEX_MAX_INSTRUCTIONS: &str = include_str!("../../gpt-5.1-codex-max_prompt.md"); +const GPT_5_2_CODEX_INSTRUCTIONS: &str = include_str!("../../gpt-5.2-codex_prompt.md"); + +pub(crate) const CONTEXT_WINDOW_272K: i64 = 272_000; + +macro_rules! model_info { + ( + $slug:expr $(, $key:ident : $value:expr )* $(,)? + ) => {{ + #[allow(unused_mut)] + let mut model = ModelInfo { + slug: $slug.to_string(), + display_name: $slug.to_string(), + description: None, + // This is primarily used when remote metadata is available. When running + // offline, core generally omits the effort field unless explicitly + // configured by the user. + default_reasoning_level: None, + supported_reasoning_levels: supported_reasoning_level_low_medium_high(), + shell_type: ConfigShellToolType::Default, + visibility: ModelVisibility::None, + supported_in_api: true, + priority: 99, + upgrade: None, + base_instructions: BASE_INSTRUCTIONS.to_string(), + supports_reasoning_summaries: false, + support_verbosity: false, + default_verbosity: None, + apply_patch_tool_type: None, + truncation_policy: TruncationPolicyConfig::bytes(10_000), + supports_parallel_tool_calls: false, + context_window: Some(CONTEXT_WINDOW_272K), + auto_compact_token_limit: None, + effective_context_window_percent: 95, + experimental_supported_tools: Vec::new(), + }; + + $( + model.$key = $value; + )* + model + }}; +} + +pub(crate) fn with_config_overrides(mut model: ModelInfo, config: &Config) -> ModelInfo { + if let Some(supports_reasoning_summaries) = config.model_supports_reasoning_summaries { + model.supports_reasoning_summaries = supports_reasoning_summaries; + } + if let Some(context_window) = config.model_context_window { + model.context_window = Some(context_window); + } + if let Some(auto_compact_token_limit) = config.model_auto_compact_token_limit { + model.auto_compact_token_limit = Some(auto_compact_token_limit); + } + model +} + +// todo(aibrahim): remove most of the entries here when enabling models.json +pub(crate) fn find_model_info_for_slug(slug: &str) -> ModelInfo { + if slug.starts_with("o3") || slug.starts_with("o4-mini") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + supports_reasoning_summaries: true, + context_window: Some(200_000), + ) + } else if slug.starts_with("codex-mini-latest") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + shell_type: ConfigShellToolType::Local, + supports_reasoning_summaries: true, + context_window: Some(200_000), + ) + } else if slug.starts_with("gpt-4.1") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + supports_reasoning_summaries: false, + context_window: Some(1_047_576), + ) + } else if slug.starts_with("gpt-oss") || slug.starts_with("openai/gpt-oss") { + model_info!( + slug, + apply_patch_tool_type: Some(ApplyPatchToolType::Function), + context_window: Some(96_000), + ) + } else if slug.starts_with("gpt-4o") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + supports_reasoning_summaries: false, + context_window: Some(128_000), + ) + } else if slug.starts_with("gpt-3.5") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + supports_reasoning_summaries: false, + context_window: Some(16_385), + ) + } else if slug.starts_with("test-gpt-5") { + model_info!( + slug, + base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), + experimental_supported_tools: vec![ + "grep_files".to_string(), + "list_dir".to_string(), + "read_file".to_string(), + "test_sync_tool".to_string(), + ], + supports_parallel_tool_calls: true, + supports_reasoning_summaries: true, + shell_type: ConfigShellToolType::ShellCommand, + support_verbosity: true, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + ) + } else if slug.starts_with("exp-codex") || slug.starts_with("codex-1p") { + model_info!( + slug, + base_instructions: GPT_5_2_CODEX_INSTRUCTIONS.to_string(), + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: true, + supports_reasoning_summaries: true, + support_verbosity: false, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + ) + } else if slug.starts_with("exp-") { + model_info!( + slug, + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + supports_reasoning_summaries: true, + support_verbosity: true, + default_verbosity: Some(Verbosity::Low), + base_instructions: BASE_INSTRUCTIONS.to_string(), + default_reasoning_level: Some(ReasoningEffort::Medium), + truncation_policy: TruncationPolicyConfig::bytes(10_000), + shell_type: ConfigShellToolType::UnifiedExec, + supports_parallel_tool_calls: true, + context_window: Some(CONTEXT_WINDOW_272K), + ) + } else if slug.starts_with("gpt-5.2-codex") || slug.starts_with("bengalfox") { + model_info!( + slug, + base_instructions: GPT_5_2_CODEX_INSTRUCTIONS.to_string(), + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: true, + supports_reasoning_summaries: true, + support_verbosity: false, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + supported_reasoning_levels: supported_reasoning_level_low_medium_high_xhigh(), + ) + } else if slug.starts_with("gpt-5.1-codex-max") { + model_info!( + slug, + base_instructions: GPT_5_1_CODEX_MAX_INSTRUCTIONS.to_string(), + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: false, + supports_reasoning_summaries: true, + support_verbosity: false, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + supported_reasoning_levels: supported_reasoning_level_low_medium_high_xhigh(), + ) + } else if (slug.starts_with("gpt-5-codex") + || slug.starts_with("gpt-5.1-codex") + || slug.starts_with("codex-")) + && !slug.contains("-mini") + { + model_info!( + slug, + base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: false, + supports_reasoning_summaries: true, + support_verbosity: false, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + supported_reasoning_levels: supported_reasoning_level_low_medium_high(), + ) + } else if slug.starts_with("gpt-5-codex") + || slug.starts_with("gpt-5.1-codex") + || slug.starts_with("codex-") + { + model_info!( + slug, + base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: false, + supports_reasoning_summaries: true, + support_verbosity: false, + truncation_policy: TruncationPolicyConfig::tokens(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + ) + } else if (slug.starts_with("gpt-5.2") || slug.starts_with("boomslang")) + && !slug.contains("codex") + { + model_info!( + slug, + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + supports_reasoning_summaries: true, + support_verbosity: true, + default_verbosity: Some(Verbosity::Low), + base_instructions: GPT_5_2_INSTRUCTIONS.to_string(), + default_reasoning_level: Some(ReasoningEffort::Medium), + truncation_policy: TruncationPolicyConfig::bytes(10_000), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: true, + context_window: Some(CONTEXT_WINDOW_272K), + supported_reasoning_levels: supported_reasoning_level_low_medium_high_xhigh_non_codex(), + ) + } else if slug.starts_with("gpt-5.1") && !slug.contains("codex") { + model_info!( + slug, + apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + supports_reasoning_summaries: true, + support_verbosity: true, + default_verbosity: Some(Verbosity::Low), + base_instructions: GPT_5_1_INSTRUCTIONS.to_string(), + default_reasoning_level: Some(ReasoningEffort::Medium), + truncation_policy: TruncationPolicyConfig::bytes(10_000), + shell_type: ConfigShellToolType::ShellCommand, + supports_parallel_tool_calls: true, + context_window: Some(CONTEXT_WINDOW_272K), + supported_reasoning_levels: supported_reasoning_level_low_medium_high_non_codex(), + ) + } else if slug.starts_with("gpt-5") { + model_info!( + slug, + base_instructions: BASE_INSTRUCTIONS_WITH_APPLY_PATCH.to_string(), + shell_type: ConfigShellToolType::Default, + supports_reasoning_summaries: true, + support_verbosity: true, + truncation_policy: TruncationPolicyConfig::bytes(10_000), + context_window: Some(CONTEXT_WINDOW_272K), + ) + } else { + warn!("Unknown model {slug} is used. This will degrade the performance of Codex."); + model_info!( + slug, + context_window: None, + supported_reasoning_levels: Vec::new(), + default_reasoning_level: None + ) + } +} + +fn supported_reasoning_level_low_medium_high() -> Vec { + vec![ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Fast responses with lighter reasoning".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Balances speed and reasoning depth for everyday tasks".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Greater reasoning depth for complex problems".to_string(), + }, + ] +} + +fn supported_reasoning_level_low_medium_high_non_codex() -> Vec { + vec![ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Balances speed with some reasoning; useful for straightforward queries and short explanations".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Provides a solid balance of reasoning depth and latency for general-purpose tasks".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems".to_string(), + }, + ] +} + +fn supported_reasoning_level_low_medium_high_xhigh() -> Vec { + vec![ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Fast responses with lighter reasoning".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Balances speed and reasoning depth for everyday tasks".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Greater reasoning depth for complex problems".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::XHigh, + description: "Extra high reasoning depth for complex problems".to_string(), + }, + ] +} + +fn supported_reasoning_level_low_medium_high_xhigh_non_codex() -> Vec { + vec![ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Balances speed with some reasoning; useful for straightforward queries and short explanations".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Provides a solid balance of reasoning depth and latency for general-purpose tasks".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems".to_string(), + }, + ReasoningEffortPreset { + effort: ReasoningEffort::XHigh, + description: "Extra high reasoning for complex problems".to_string(), + }, + ] +} diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index d9e21bc24b7..a92a624a7e7 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -2,13 +2,13 @@ use crate::client_common::tools::ResponsesApiTool; use crate::client_common::tools::ToolSpec; use crate::features::Feature; use crate::features::Features; -use crate::models_manager::model_family::ModelFamily; use crate::tools::handlers::PLAN_TOOL; use crate::tools::handlers::apply_patch::create_apply_patch_freeform_tool; use crate::tools::handlers::apply_patch::create_apply_patch_json_tool; use crate::tools::registry::ToolRegistryBuilder; use codex_protocol::openai_models::ApplyPatchToolType; use codex_protocol::openai_models::ConfigShellToolType; +use codex_protocol::openai_models::ModelInfo; use serde::Deserialize; use serde::Serialize; use serde_json::Value as JsonValue; @@ -27,14 +27,14 @@ pub(crate) struct ToolsConfig { } pub(crate) struct ToolsConfigParams<'a> { - pub(crate) model_family: &'a ModelFamily, + pub(crate) model_info: &'a ModelInfo, pub(crate) features: &'a Features, } impl ToolsConfig { pub fn new(params: &ToolsConfigParams) -> Self { let ToolsConfigParams { - model_family, + model_info, features, } = params; let include_apply_patch_tool = features.enabled(Feature::ApplyPatchFreeform); @@ -52,10 +52,10 @@ impl ToolsConfig { ConfigShellToolType::ShellCommand } } else { - model_family.shell_type + model_info.shell_type }; - let apply_patch_tool_type = match model_family.apply_patch_tool_type { + let apply_patch_tool_type = match model_info.apply_patch_tool_type { Some(ApplyPatchToolType::Freeform) => Some(ApplyPatchToolType::Freeform), Some(ApplyPatchToolType::Function) => Some(ApplyPatchToolType::Function), None => { @@ -73,7 +73,7 @@ impl ToolsConfig { web_search_request: include_web_search_request, web_search_cached: include_web_search_cached, include_view_image_tool, - experimental_supported_tools: model_family.experimental_supported_tools.clone(), + experimental_supported_tools: model_info.experimental_supported_tools.clone(), } } } @@ -1232,13 +1232,13 @@ mod tests { #[test] fn test_full_toolset_specs_for_gpt5_codex_unified_exec_web_search() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); features.enable(Feature::ViewImageTool); let config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&config, None).build(); @@ -1294,9 +1294,9 @@ mod tests { fn assert_model_tools(model_slug: &str, features: &Features, expected_tools: &[&str]) { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline(model_slug, &config); + let model_info = ModelsManager::construct_model_info_offline(model_slug, &config); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features, }); let (tools, _) = build_specs(&tools_config, Some(HashMap::new())).build(); @@ -1307,12 +1307,12 @@ mod tests { #[test] fn web_search_cached_sets_external_web_access_false() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::WebSearchCached); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&tools_config, None).build(); @@ -1329,13 +1329,13 @@ mod tests { #[test] fn web_search_cached_takes_precedence_over_web_search_request() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::WebSearchCached); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&tools_config, None).build(); @@ -1532,12 +1532,12 @@ mod tests { #[test] fn test_build_specs_default_shell_present() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("o3", &config); + let model_info = ModelsManager::construct_model_info_offline("o3", &config); let mut features = Features::with_defaults(); features.enable(Feature::WebSearchRequest); features.enable(Feature::UnifiedExec); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&tools_config, Some(HashMap::new())).build(); @@ -1554,12 +1554,12 @@ mod tests { #[ignore] fn test_parallel_support_flags() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.disable(Feature::ViewImageTool); features.enable(Feature::UnifiedExec); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&tools_config, None).build(); @@ -1572,14 +1572,13 @@ mod tests { } #[test] - fn test_test_model_family_includes_sync_tool() { + fn test_test_model_info_includes_sync_tool() { let config = test_config(); - let model_family = - ModelsManager::construct_model_family_offline("test-gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("test-gpt-5-codex", &config); let mut features = Features::with_defaults(); features.disable(Feature::ViewImageTool); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs(&tools_config, None).build(); @@ -1605,12 +1604,12 @@ mod tests { #[test] fn test_build_specs_mcp_tools_converted() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("o3", &config); + let model_info = ModelsManager::construct_model_info_offline("o3", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs( @@ -1700,11 +1699,11 @@ mod tests { #[test] fn test_build_specs_mcp_tools_sorted_by_name() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("o3", &config); + let model_info = ModelsManager::construct_model_info_offline("o3", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); @@ -1776,12 +1775,12 @@ mod tests { #[test] fn test_mcp_tool_property_missing_type_defaults_to_string() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); @@ -1833,12 +1832,12 @@ mod tests { #[test] fn test_mcp_tool_integer_normalized_to_number() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); @@ -1886,13 +1885,13 @@ mod tests { #[test] fn test_mcp_tool_array_without_items_gets_default_string_items() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); features.enable(Feature::ApplyPatchFreeform); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); @@ -1943,12 +1942,12 @@ mod tests { #[test] fn test_mcp_tool_anyof_defaults_to_string() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); @@ -2055,12 +2054,12 @@ Examples of valid command strings: #[test] fn test_get_openai_tools_mcp_tools_with_additional_properties_schema() { let config = test_config(); - let model_family = ModelsManager::construct_model_family_offline("gpt-5-codex", &config); + let model_info = ModelsManager::construct_model_info_offline("gpt-5-codex", &config); let mut features = Features::with_defaults(); features.enable(Feature::UnifiedExec); features.enable(Feature::WebSearchRequest); let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, + model_info: &model_info, features: &features, }); let (tools, _) = build_specs( diff --git a/codex-rs/core/tests/chat_completions_payload.rs b/codex-rs/core/tests/chat_completions_payload.rs index 536de44e835..b449f200d2f 100644 --- a/codex-rs/core/tests/chat_completions_payload.rs +++ b/codex-rs/core/tests/chat_completions_payload.rs @@ -75,11 +75,11 @@ async fn run_request(input: Vec) -> Value { let conversation_id = ThreadId::new(); let model = ModelsManager::get_model_offline(config.model.as_deref()); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), Some(AuthMode::ApiKey), @@ -91,7 +91,7 @@ async fn run_request(input: Vec) -> Value { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, diff --git a/codex-rs/core/tests/chat_completions_sse.rs b/codex-rs/core/tests/chat_completions_sse.rs index 17d4a5a4abc..6c0e668e7be 100644 --- a/codex-rs/core/tests/chat_completions_sse.rs +++ b/codex-rs/core/tests/chat_completions_sse.rs @@ -76,11 +76,11 @@ async fn run_stream_with_bytes(sse_body: &[u8]) -> Vec { let auth_manager = AuthManager::from_auth_for_testing(CodexAuth::from_api_key("Test API Key")); let auth_mode = auth_manager.get_auth_mode(); let model = ModelsManager::get_model_offline(config.model.as_deref()); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), auth_mode, @@ -92,7 +92,7 @@ async fn run_stream_with_bytes(sse_body: &[u8]) -> Vec { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, diff --git a/codex-rs/core/tests/responses_headers.rs b/codex-rs/core/tests/responses_headers.rs index 188397fd24a..e1a2df12135 100644 --- a/codex-rs/core/tests/responses_headers.rs +++ b/codex-rs/core/tests/responses_headers.rs @@ -68,11 +68,11 @@ async fn responses_stream_includes_subagent_header_on_review() { let conversation_id = ThreadId::new(); let auth_mode = AuthMode::ChatGPT; let session_source = SessionSource::SubAgent(SubAgentSource::Review); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), Some(auth_mode), @@ -84,7 +84,7 @@ async fn responses_stream_includes_subagent_header_on_review() { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, @@ -162,12 +162,12 @@ async fn responses_stream_includes_subagent_header_on_other() { let conversation_id = ThreadId::new(); let auth_mode = AuthMode::ChatGPT; let session_source = SessionSource::SubAgent(SubAgentSource::Other("my-task".to_string())); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), Some(auth_mode), @@ -179,7 +179,7 @@ async fn responses_stream_includes_subagent_header_on_other() { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, @@ -212,7 +212,7 @@ async fn responses_stream_includes_subagent_header_on_other() { } #[tokio::test] -async fn responses_respects_model_family_overrides_from_config() { +async fn responses_respects_model_info_overrides_from_config() { core_test_support::skip_if_no_network!(); let server = responses::start_mock_server().await; @@ -256,11 +256,11 @@ async fn responses_respects_model_family_overrides_from_config() { AuthManager::from_auth_for_testing(CodexAuth::from_api_key("Test API Key")).get_auth_mode(); let session_source = SessionSource::SubAgent(SubAgentSource::Other("override-check".to_string())); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), auth_mode, @@ -272,7 +272,7 @@ async fn responses_respects_model_family_overrides_from_config() { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index 7d876ceb1f5..7e0a6e3fa0b 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -806,7 +806,7 @@ async fn includes_no_effort_in_request() -> anyhow::Result<()> { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn includes_default_reasoning_effort_in_request_when_defined_by_model_family() +async fn includes_default_reasoning_effort_in_request_when_defined_by_model_info() -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let server = MockServer::start().await; @@ -1142,13 +1142,13 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { let model = ModelsManager::get_model_offline(config.model.as_deref()); config.model = Some(model.clone()); let config = Arc::new(config); - let model_family = ModelsManager::construct_model_family_offline(model.as_str(), &config); + let model_info = ModelsManager::construct_model_info_offline(model.as_str(), &config); let conversation_id = ThreadId::new(); let auth_manager = AuthManager::from_auth_for_testing(CodexAuth::from_api_key("Test API Key")); let otel_manager = OtelManager::new( conversation_id, model.as_str(), - model_family.slug.as_str(), + model_info.slug.as_str(), None, Some("test@test.com".to_string()), auth_manager.get_auth_mode(), @@ -1160,7 +1160,7 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { let client = ModelClient::new( Arc::clone(&config), None, - model_family, + model_info, otel_manager, provider, effort, diff --git a/codex-rs/core/tests/suite/prompt_caching.rs b/codex-rs/core/tests/suite/prompt_caching.rs index 01c59089308..bde146ed47c 100644 --- a/codex-rs/core/tests/suite/prompt_caching.rs +++ b/codex-rs/core/tests/suite/prompt_caching.rs @@ -86,7 +86,7 @@ async fn prompt_tools_are_consistent_across_requests() -> anyhow::Result<()> { .await?; let base_instructions = thread_manager .get_models_manager() - .construct_model_family( + .construct_model_info( config .model .as_deref() @@ -94,8 +94,7 @@ async fn prompt_tools_are_consistent_across_requests() -> anyhow::Result<()> { &config, ) .await - .base_instructions - .clone(); + .base_instructions; codex .submit(Op::UserInput { @@ -132,7 +131,7 @@ async fn prompt_tools_are_consistent_across_requests() -> anyhow::Result<()> { base_instructions } else { [ - base_instructions.clone(), + base_instructions, include_str!("../../../apply-patch/apply_patch_tool_instructions.md").to_string(), ] .join("\n") diff --git a/codex-rs/core/tests/suite/remote_models.rs b/codex-rs/core/tests/suite/remote_models.rs index 6968285319a..d77ec7d3838 100644 --- a/codex-rs/core/tests/suite/remote_models.rs +++ b/codex-rs/core/tests/suite/remote_models.rs @@ -64,7 +64,7 @@ async fn remote_models_remote_model_uses_unified_exec() -> Result<()> { slug: REMOTE_MODEL_SLUG.to_string(), display_name: "Remote Test".to_string(), description: Some("A remote model that requires the test shell".to_string()), - default_reasoning_level: ReasoningEffort::Medium, + default_reasoning_level: Some(ReasoningEffort::Medium), supported_reasoning_levels: vec![ReasoningEffortPreset { effort: ReasoningEffort::Medium, description: ReasoningEffort::Medium.to_string(), @@ -74,14 +74,16 @@ async fn remote_models_remote_model_uses_unified_exec() -> Result<()> { supported_in_api: true, priority: 1, upgrade: None, - base_instructions: None, + base_instructions: "base instructions".to_string(), supports_reasoning_summaries: false, support_verbosity: false, default_verbosity: None, apply_patch_tool_type: None, truncation_policy: TruncationPolicyConfig::bytes(10_000), supports_parallel_tool_calls: false, - context_window: None, + context_window: Some(272_000), + auto_compact_token_limit: None, + effective_context_window_percent: 95, experimental_supported_tools: Vec::new(), }; @@ -121,10 +123,10 @@ async fn remote_models_remote_model_uses_unified_exec() -> Result<()> { ); assert_eq!(requests[0].url.path(), "/v1/models"); - let family = models_manager - .construct_model_family(REMOTE_MODEL_SLUG, &config) + let model_info = models_manager + .construct_model_info(REMOTE_MODEL_SLUG, &config) .await; - assert_eq!(family.shell_type, ConfigShellToolType::UnifiedExec); + assert_eq!(model_info.shell_type, ConfigShellToolType::UnifiedExec); codex .submit(Op::OverrideTurnContext { @@ -201,7 +203,7 @@ async fn remote_models_apply_remote_base_instructions() -> Result<()> { slug: model.to_string(), display_name: "Parallel Remote".to_string(), description: Some("A remote model with custom instructions".to_string()), - default_reasoning_level: ReasoningEffort::Medium, + default_reasoning_level: Some(ReasoningEffort::Medium), supported_reasoning_levels: vec![ReasoningEffortPreset { effort: ReasoningEffort::Medium, description: ReasoningEffort::Medium.to_string(), @@ -211,14 +213,16 @@ async fn remote_models_apply_remote_base_instructions() -> Result<()> { supported_in_api: true, priority: 1, upgrade: None, - base_instructions: Some(remote_base.to_string()), + base_instructions: remote_base.to_string(), supports_reasoning_summaries: false, support_verbosity: false, default_verbosity: None, apply_patch_tool_type: None, truncation_policy: TruncationPolicyConfig::bytes(10_000), supports_parallel_tool_calls: false, - context_window: None, + context_window: Some(272_000), + auto_compact_token_limit: None, + effective_context_window_percent: 95, experimental_supported_tools: Vec::new(), }; mount_models_once( @@ -459,7 +463,7 @@ fn test_remote_model(slug: &str, visibility: ModelVisibility, priority: i32) -> slug: slug.to_string(), display_name: format!("{slug} display"), description: Some(format!("{slug} description")), - default_reasoning_level: ReasoningEffort::Medium, + default_reasoning_level: Some(ReasoningEffort::Medium), supported_reasoning_levels: vec![ReasoningEffortPreset { effort: ReasoningEffort::Medium, description: ReasoningEffort::Medium.to_string(), @@ -469,14 +473,16 @@ fn test_remote_model(slug: &str, visibility: ModelVisibility, priority: i32) -> supported_in_api: true, priority, upgrade: None, - base_instructions: None, + base_instructions: "base instructions".to_string(), supports_reasoning_summaries: false, support_verbosity: false, default_verbosity: None, apply_patch_tool_type: None, truncation_policy: TruncationPolicyConfig::bytes(10_000), supports_parallel_tool_calls: false, - context_window: None, + context_window: Some(272_000), + auto_compact_token_limit: None, + effective_context_window_percent: 95, experimental_supported_tools: Vec::new(), } } diff --git a/codex-rs/mcp-types/schema/2025-03-26/schema.json b/codex-rs/mcp-types/schema/2025-03-26/schema.json index a1e3f26799d..328ff95f4b8 100644 --- a/codex-rs/mcp-types/schema/2025-03-26/schema.json +++ b/codex-rs/mcp-types/schema/2025-03-26/schema.json @@ -1167,7 +1167,7 @@ "description": "Hints to use for model selection.\n\nKeys not declared here are currently left unspecified by the spec and are up\nto the client to interpret.", "properties": { "name": { - "description": "A hint for a model name.\n\nThe client SHOULD treat this as a substring of a model name; for example:\n - `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022`\n - `sonnet` should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc.\n - `claude` should match any Claude model\n\nThe client MAY also map the string to a different provider's model name or a different model family, as long as it fills a similar niche; for example:\n - `gemini-1.5-flash` could match `claude-3-haiku-20240307`", + "description": "A hint for a model name.\n\nThe client SHOULD treat this as a substring of a model name; for example:\n - `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022`\n - `sonnet` should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc.\n - `claude` should match any Claude model\n\nThe client MAY also map the string to a different provider's model name or a different model info, as long as it fills a similar niche; for example:\n - `gemini-1.5-flash` could match `claude-3-haiku-20240307`", "type": "string" } }, @@ -2136,4 +2136,3 @@ } } } - diff --git a/codex-rs/mcp-types/schema/2025-06-18/schema.json b/codex-rs/mcp-types/schema/2025-06-18/schema.json index 24ba4f6309f..d5faee82cdb 100644 --- a/codex-rs/mcp-types/schema/2025-06-18/schema.json +++ b/codex-rs/mcp-types/schema/2025-06-18/schema.json @@ -1334,7 +1334,7 @@ "description": "Hints to use for model selection.\n\nKeys not declared here are currently left unspecified by the spec and are up\nto the client to interpret.", "properties": { "name": { - "description": "A hint for a model name.\n\nThe client SHOULD treat this as a substring of a model name; for example:\n - `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022`\n - `sonnet` should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc.\n - `claude` should match any Claude model\n\nThe client MAY also map the string to a different provider's model name or a different model family, as long as it fills a similar niche; for example:\n - `gemini-1.5-flash` could match `claude-3-haiku-20240307`", + "description": "A hint for a model name.\n\nThe client SHOULD treat this as a substring of a model name; for example:\n - `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022`\n - `sonnet` should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc.\n - `claude` should match any Claude model\n\nThe client MAY also map the string to a different provider's model name or a different model info, as long as it fills a similar niche; for example:\n - `gemini-1.5-flash` could match `claude-3-haiku-20240307`", "type": "string" } }, @@ -2514,4 +2514,3 @@ } } } - diff --git a/codex-rs/protocol/src/openai_models.rs b/codex-rs/protocol/src/openai_models.rs index ae426e62965..60c7cc74b00 100644 --- a/codex-rs/protocol/src/openai_models.rs +++ b/codex-rs/protocol/src/openai_models.rs @@ -159,30 +159,53 @@ impl TruncationPolicyConfig { #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema)] pub struct ClientVersion(pub i32, pub i32, pub i32); +const fn default_effective_context_window_percent() -> i64 { + 95 +} + /// Model metadata returned by the Codex backend `/models` endpoint. #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)] pub struct ModelInfo { pub slug: String, pub display_name: String, pub description: Option, - pub default_reasoning_level: ReasoningEffort, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub default_reasoning_level: Option, pub supported_reasoning_levels: Vec, pub shell_type: ConfigShellToolType, pub visibility: ModelVisibility, pub supported_in_api: bool, pub priority: i32, pub upgrade: Option, - pub base_instructions: Option, + pub base_instructions: String, pub supports_reasoning_summaries: bool, pub support_verbosity: bool, pub default_verbosity: Option, pub apply_patch_tool_type: Option, pub truncation_policy: TruncationPolicyConfig, pub supports_parallel_tool_calls: bool, + #[serde(default, skip_serializing_if = "Option::is_none")] pub context_window: Option, + /// Token threshold for automatic compaction. When omitted, core derives it + /// from `context_window` (90%). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub auto_compact_token_limit: Option, + /// Percentage of the context window considered usable for inputs, after + /// reserving headroom for system prompts, tool overhead, and model output. + #[serde(default = "default_effective_context_window_percent")] + pub effective_context_window_percent: i64, pub experimental_supported_tools: Vec, } +impl ModelInfo { + pub fn auto_compact_token_limit(&self) -> Option { + self.auto_compact_token_limit.or_else(|| { + self.context_window + .map(|context_window| (context_window * 9) / 10) + }) + } +} + /// Response wrapper for `/models`. #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema, Default)] pub struct ModelsResponse { @@ -197,7 +220,9 @@ impl From for ModelPreset { model: info.slug.clone(), display_name: info.display_name, description: info.description.unwrap_or_default(), - default_reasoning_effort: info.default_reasoning_level, + default_reasoning_effort: info + .default_reasoning_level + .unwrap_or(ReasoningEffort::None), supported_reasoning_efforts: info.supported_reasoning_levels.clone(), is_default: false, // default is the highest priority available model upgrade: info.upgrade.as_ref().map(|upgrade_slug| ModelUpgrade { diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index f5351576f9c..e3748bafc6b 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -127,7 +127,7 @@ pub enum Op { #[serde(skip_serializing_if = "Option::is_none")] sandbox_policy: Option, - /// Updated model slug. When set, the model family is derived + /// Updated model slug. When set, the model info is derived /// automatically. #[serde(skip_serializing_if = "Option::is_none")] model: Option, @@ -874,6 +874,7 @@ pub struct TaskCompleteEvent { #[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)] pub struct TaskStartedEvent { + // TODO(aibrahim): make this not optional pub model_context_window: Option, } @@ -895,6 +896,7 @@ pub struct TokenUsage { pub struct TokenUsageInfo { pub total_token_usage: TokenUsage, pub last_token_usage: TokenUsage, + // TODO(aibrahim): make this not optional #[ts(type = "number | null")] pub model_context_window: Option, } diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 370d3d3f6a7..cfb50a996d8 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -540,10 +540,10 @@ impl App { } async fn handle_event(&mut self, tui: &mut tui::Tui, event: AppEvent) -> Result { - let model_family = self + let model_info = self .server .get_models_manager() - .construct_model_family(self.current_model.as_str(), &self.config) + .construct_model_info(self.current_model.as_str(), &self.config) .await; match event { AppEvent::NewSession => { @@ -564,7 +564,7 @@ impl App { model: self.current_model.clone(), }; self.chat_widget = ChatWidget::new(init, self.server.clone()); - self.current_model = model_family.get_model_slug().to_string(); + self.current_model = model_info.slug.clone(); if let Some(summary) = summary { let mut lines: Vec> = vec![summary.usage_line.clone().into()]; if let Some(command) = summary.resume_command { @@ -618,7 +618,7 @@ impl App { resumed.thread, resumed.session_configured, ); - self.current_model = model_family.get_model_slug().to_string(); + self.current_model = model_info.slug.clone(); if let Some(summary) = summary { let mut lines: Vec> = vec![summary.usage_line.clone().into()]; diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index c6f6c735995..bb16f13328e 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -38,9 +38,8 @@ fn test_auth_manager(config: &Config) -> AuthManager { } fn token_info_for(model_slug: &str, config: &Config, usage: &TokenUsage) -> TokenUsageInfo { - let context_window = ModelsManager::construct_model_family_offline(model_slug, config) - .context_window - .or(config.model_context_window); + let context_window = + ModelsManager::construct_model_info_offline(model_slug, config).context_window; TokenUsageInfo { total_token_usage: usage.clone(), last_token_usage: usage.clone(), diff --git a/codex-rs/tui2/src/status/tests.rs b/codex-rs/tui2/src/status/tests.rs index 7eb18dd48bb..b16940a4def 100644 --- a/codex-rs/tui2/src/status/tests.rs +++ b/codex-rs/tui2/src/status/tests.rs @@ -38,9 +38,8 @@ fn test_auth_manager(config: &Config) -> AuthManager { } fn token_info_for(model_slug: &str, config: &Config, usage: &TokenUsage) -> TokenUsageInfo { - let context_window = ModelsManager::construct_model_family_offline(model_slug, config) - .context_window - .or(config.model_context_window); + let context_window = + ModelsManager::construct_model_info_offline(model_slug, config).context_window; TokenUsageInfo { total_token_usage: usage.clone(), last_token_usage: usage.clone(),