diff --git a/.codespellrc b/.codespellrc index eefde42a4e..da831d8957 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,6 +1,6 @@ [codespell] # Ref: https://github.com/codespell-project/codespell#using-a-config-file -skip = .git*,vendor,*-lock.yaml,*.lock,.codespellrc,*test.ts,*.jsonl +skip = .git*,vendor,*-lock.yaml,*.lock,.codespellrc,*test.ts,*.jsonl,frame*.txt check-hidden = true ignore-regex = ^\s*"image/\S+": ".*|\b(afterAll)\b ignore-words-list = ratatui,ser diff --git a/.github/ISSUE_TEMPLATE/2-bug-report.yml b/.github/ISSUE_TEMPLATE/2-bug-report.yml index b2feedc86e..109f026cb4 100644 --- a/.github/ISSUE_TEMPLATE/2-bug-report.yml +++ b/.github/ISSUE_TEMPLATE/2-bug-report.yml @@ -20,6 +20,14 @@ body: attributes: label: What version of Codex is running? description: Copy the output of `codex --version` + validations: + required: true + - type: input + id: plan + attributes: + label: What subscription do you have? + validations: + required: true - type: input id: model attributes: @@ -32,11 +40,18 @@ body: description: | For MacOS and Linux: copy the output of `uname -mprs` For Windows: copy the output of `"$([Environment]::OSVersion | ForEach-Object VersionString) $(if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" })"` in the PowerShell console + - type: textarea + id: actual + attributes: + label: What issue are you seeing? + description: Please include the full error messages and prompts with PII redacted. If possible, please provide text instead of a screenshot. + validations: + required: true - type: textarea id: steps attributes: label: What steps can reproduce the bug? - description: Explain the bug and provide a code snippet that can reproduce it. + description: Explain the bug and provide a code snippet that can reproduce it. Please include session id, token limit usage, context window usage if applicable. validations: required: true - type: textarea @@ -44,11 +59,6 @@ body: attributes: label: What is the expected behavior? description: If possible, please provide text instead of a screenshot. - - type: textarea - id: actual - attributes: - label: What do you see instead? - description: If possible, please provide text instead of a screenshot. - type: textarea id: notes attributes: diff --git a/.github/ISSUE_TEMPLATE/4-feature-request.yml b/.github/ISSUE_TEMPLATE/4-feature-request.yml index fc95a67ec2..fea86edd7b 100644 --- a/.github/ISSUE_TEMPLATE/4-feature-request.yml +++ b/.github/ISSUE_TEMPLATE/4-feature-request.yml @@ -2,7 +2,6 @@ name: 🎁 Feature Request description: Propose a new feature for Codex labels: - enhancement - - needs triage body: - type: markdown attributes: @@ -19,11 +18,6 @@ body: label: What feature would you like to see? validations: required: true - - type: textarea - id: author - attributes: - label: Are you interested in implementing this feature? - description: Please wait for acknowledgement before implementing or opening a PR. - type: textarea id: notes attributes: diff --git a/.github/ISSUE_TEMPLATE/5-vs-code-extension.yml b/.github/ISSUE_TEMPLATE/5-vs-code-extension.yml index f2ba251a1d..52da6a7cad 100644 --- a/.github/ISSUE_TEMPLATE/5-vs-code-extension.yml +++ b/.github/ISSUE_TEMPLATE/5-vs-code-extension.yml @@ -14,11 +14,21 @@ body: id: version attributes: label: What version of the VS Code extension are you using? + validations: + required: true + - type: input + id: plan + attributes: + label: What subscription do you have? + validations: + required: true - type: input id: ide attributes: label: Which IDE are you using? description: Like `VS Code`, `Cursor`, `Windsurf`, etc. + validations: + required: true - type: input id: platform attributes: @@ -26,11 +36,18 @@ body: description: | For MacOS and Linux: copy the output of `uname -mprs` For Windows: copy the output of `"$([Environment]::OSVersion | ForEach-Object VersionString) $(if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" })"` in the PowerShell console + - type: textarea + id: actual + attributes: + label: What issue are you seeing? + description: Please include the full error messages and prompts with PII redacted. If possible, please provide text instead of a screenshot. + validations: + required: true - type: textarea id: steps attributes: label: What steps can reproduce the bug? - description: Explain the bug and provide a code snippet that can reproduce it. + description: Explain the bug and provide a code snippet that can reproduce it. Please include session id, token limit usage, context window usage if applicable. validations: required: true - type: textarea @@ -38,11 +55,6 @@ body: attributes: label: What is the expected behavior? description: If possible, please provide text instead of a screenshot. - - type: textarea - id: actual - attributes: - label: What do you see instead? - description: If possible, please provide text instead of a screenshot. - type: textarea id: notes attributes: diff --git a/.github/codex/home/config.toml b/.github/codex/home/config.toml index 8e800ca068..7b065212a8 100644 --- a/.github/codex/home/config.toml +++ b/.github/codex/home/config.toml @@ -1,3 +1,3 @@ -model = "gpt-5" +model = "gpt-5.1" # Consider setting [mcp_servers] here! diff --git a/.github/dotslash-config.json b/.github/dotslash-config.json index 8566e03490..5e28cdf207 100644 --- a/.github/dotslash-config.json +++ b/.github/dotslash-config.json @@ -27,6 +27,34 @@ "path": "codex.exe" } } + }, + "codex-responses-api-proxy": { + "platforms": { + "macos-aarch64": { + "regex": "^codex-responses-api-proxy-aarch64-apple-darwin\\.zst$", + "path": "codex-responses-api-proxy" + }, + "macos-x86_64": { + "regex": "^codex-responses-api-proxy-x86_64-apple-darwin\\.zst$", + "path": "codex-responses-api-proxy" + }, + "linux-x86_64": { + "regex": "^codex-responses-api-proxy-x86_64-unknown-linux-musl\\.zst$", + "path": "codex-responses-api-proxy" + }, + "linux-aarch64": { + "regex": "^codex-responses-api-proxy-aarch64-unknown-linux-musl\\.zst$", + "path": "codex-responses-api-proxy" + }, + "windows-x86_64": { + "regex": "^codex-responses-api-proxy-x86_64-pc-windows-msvc\\.exe\\.zst$", + "path": "codex-responses-api-proxy.exe" + }, + "windows-aarch64": { + "regex": "^codex-responses-api-proxy-aarch64-pc-windows-msvc\\.exe\\.zst$", + "path": "codex-responses-api-proxy.exe" + } + } } } } diff --git a/.github/prompts/issue-deduplicator.txt b/.github/prompts/issue-deduplicator.txt new file mode 100644 index 0000000000..9bdd3af9ef --- /dev/null +++ b/.github/prompts/issue-deduplicator.txt @@ -0,0 +1,18 @@ +You are an assistant that triages new GitHub issues by identifying potential duplicates. + +You will receive the following JSON files located in the current working directory: +- `codex-current-issue.json`: JSON object describing the newly created issue (fields: number, title, body). +- `codex-existing-issues.json`: JSON array of recent issues (each element includes number, title, body, createdAt). + +Instructions: +- Load both files as JSON and review their contents carefully. The codex-existing-issues.json file is large, ensure you explore all of it. +- Compare the current issue against the existing issues to find up to five that appear to describe the same underlying problem or request. +- Only consider an issue a potential duplicate if there is a clear overlap in symptoms, feature requests, reproduction steps, or error messages. +- Prioritize newer issues when similarity is comparable. +- Ignore pull requests and issues whose similarity is tenuous. +- When unsure, prefer returning fewer matches. + +Output requirements: +- Respond with a JSON array of issue numbers (integers), ordered from most likely duplicate to least. +- Include at most five numbers. +- If you find no plausible duplicates, respond with `[]`. diff --git a/.github/prompts/issue-labeler.txt b/.github/prompts/issue-labeler.txt new file mode 100644 index 0000000000..7e880d307f --- /dev/null +++ b/.github/prompts/issue-labeler.txt @@ -0,0 +1,26 @@ +You are an assistant that reviews GitHub issues for the repository. + +Your job is to choose the most appropriate existing labels for the issue described later in this prompt. +Follow these rules: +- Only pick labels out of the list below. +- Prefer a small set of precise labels over many broad ones. +- If none of the labels fit, respond with an empty JSON array: [] +- Output must be a JSON array of label names (strings) with no additional commentary. + +Labels to apply: +1. bug — Reproducible defects in Codex products (CLI, VS Code extension, web, auth). +2. enhancement — Feature requests or usability improvements that ask for new capabilities, better ergonomics, or quality-of-life tweaks. +3. extension — VS Code (or other IDE) extension-specific issues. +4. windows-os — Bugs or friction specific to Windows environments (PowerShell behavior, path handling, copy/paste, OS-specific auth or tooling failures). +5. mcp — Topics involving Model Context Protocol servers/clients. +6. codex-web — Issues targeting the Codex web UI/Cloud experience. +8. azure — Problems or requests tied to Azure OpenAI deployments. +9. documentation — Updates or corrections needed in docs/README/config references (broken links, missing examples, outdated keys, clarification requests). +10. model-behavior — Undesirable LLM behavior: forgetting goals, refusing work, hallucinating environment details, quota misreports, or other reasoning/performance anomalies. + +Issue information is available in environment variables: + +ISSUE_NUMBER +ISSUE_TITLE +ISSUE_BODY +REPO_FULL_NAME diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 0fc0747dda..45322e4ffa 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,3 +4,5 @@ Before opening this Pull Request, please read the dedicated "Contributing" markd https://github.com/openai/codex/blob/main/docs/contributing.md If your PR conforms to our contribution guidelines, replace this text with a detailed and high quality description of your changes. + +Include a link to a bug report or enhancement request. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4dfd7596d1..38773bb9f1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ name: ci on: - pull_request: { branches: [main] } + pull_request: {} push: { branches: [main] } jobs: @@ -27,12 +27,29 @@ jobs: - name: Install dependencies run: pnpm install --frozen-lockfile - # Run all tasks using workspace filters + # stage_npm_packages.py requires DotSlash when staging releases. + - uses: facebook/install-dotslash@v2 - - name: Ensure staging a release works. + - name: Stage npm package + id: stage_npm_package env: GH_TOKEN: ${{ github.token }} - run: ./codex-cli/scripts/stage_release.sh + run: | + set -euo pipefail + CODEX_VERSION=0.40.0 + OUTPUT_DIR="${RUNNER_TEMP}" + python3 ./scripts/stage_npm_packages.py \ + --release-version "$CODEX_VERSION" \ + --package codex \ + --output-dir "$OUTPUT_DIR" + PACK_OUTPUT="${OUTPUT_DIR}/codex-npm-${CODEX_VERSION}.tgz" + echo "pack_output=$PACK_OUTPUT" >> "$GITHUB_OUTPUT" + + - name: Upload staged npm package artifact + uses: actions/upload-artifact@v5 + with: + name: codex-npm-staging + path: ${{ steps.stage_npm_package.outputs.pack_output }} - name: Ensure root README.md contains only ASCII and certain Unicode code points run: ./scripts/asciicheck.py README.md @@ -43,3 +60,6 @@ jobs: run: ./scripts/asciicheck.py codex-cli/README.md - name: Check codex-cli/README ToC run: python3 scripts/readme_toc.py codex-cli/README.md + + - name: Prettier (run `pnpm run format:fix` to fix) + run: pnpm run format diff --git a/.github/workflows/cla.yml b/.github/workflows/cla.yml index 521e917e95..17d54f214a 100644 --- a/.github/workflows/cla.yml +++ b/.github/workflows/cla.yml @@ -13,17 +13,39 @@ permissions: jobs: cla: + # Only run the CLA assistant for the canonical openai repo so forks are not blocked + # and contributors who signed previously do not receive duplicate CLA notifications. + if: ${{ github.repository_owner == 'openai' }} runs-on: ubuntu-latest steps: - uses: contributor-assistant/github-action@v2.6.1 + # Run on close only if the PR was merged. This will lock the PR to preserve + # the CLA agreement. We don't want to lock PRs that have been closed without + # merging because the contributor may want to respond with additional comments. + # This action has a "lock-pullrequest-aftermerge" option that can be set to false, + # but that would unconditionally skip locking even in cases where the PR was merged. if: | - github.event_name == 'pull_request_target' || - github.event.comment.body == 'recheck' || - github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA' + ( + github.event_name == 'pull_request_target' && + ( + github.event.action == 'opened' || + github.event.action == 'synchronize' || + (github.event.action == 'closed' && github.event.pull_request.merged == true) + ) + ) || + ( + github.event_name == 'issue_comment' && + ( + github.event.comment.body == 'recheck' || + github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA' + ) + ) env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: path-to-document: https://github.com/openai/codex/blob/main/docs/CLA.md path-to-signatures: signatures/cla.json branch: cla-signatures - allowlist: dependabot[bot] + allowlist: | + codex + dependabot[bot] diff --git a/.github/workflows/close-stale-contributor-prs.yml b/.github/workflows/close-stale-contributor-prs.yml new file mode 100644 index 0000000000..e01bc3881d --- /dev/null +++ b/.github/workflows/close-stale-contributor-prs.yml @@ -0,0 +1,105 @@ +name: Close stale contributor PRs + +on: + workflow_dispatch: + schedule: + - cron: "0 6 * * *" + +permissions: + contents: read + issues: write + pull-requests: write + +jobs: + close-stale-contributor-prs: + runs-on: ubuntu-latest + steps: + - name: Close inactive PRs from contributors + uses: actions/github-script@v8 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const DAYS_INACTIVE = 14; + const cutoff = new Date(Date.now() - DAYS_INACTIVE * 24 * 60 * 60 * 1000); + const { owner, repo } = context.repo; + const dryRun = false; + const stalePrs = []; + + core.info(`Dry run mode: ${dryRun}`); + + const prs = await github.paginate(github.rest.pulls.list, { + owner, + repo, + state: "open", + per_page: 100, + sort: "updated", + direction: "asc", + }); + + for (const pr of prs) { + const lastUpdated = new Date(pr.updated_at); + if (lastUpdated > cutoff) { + core.info(`PR ${pr.number} is fresh`); + continue; + } + + if (!pr.user || pr.user.type !== "User") { + core.info(`PR ${pr.number} wasn't created by a user`); + continue; + } + + let permission; + try { + const permissionResponse = await github.rest.repos.getCollaboratorPermissionLevel({ + owner, + repo, + username: pr.user.login, + }); + permission = permissionResponse.data.permission; + } catch (error) { + if (error.status === 404) { + core.info(`Author ${pr.user.login} is not a collaborator; skipping #${pr.number}`); + continue; + } + throw error; + } + + const hasContributorAccess = ["admin", "maintain", "write"].includes(permission); + if (!hasContributorAccess) { + core.info(`Author ${pr.user.login} has ${permission} access; skipping #${pr.number}`); + continue; + } + + stalePrs.push(pr); + } + + if (!stalePrs.length) { + core.info("No stale contributor pull requests found."); + return; + } + + for (const pr of stalePrs) { + const issue_number = pr.number; + const closeComment = `Closing this pull request because it has had no updates for more than ${DAYS_INACTIVE} days. If you plan to continue working on it, feel free to reopen or open a new PR.`; + + if (dryRun) { + core.info(`[dry-run] Would close contributor PR #${issue_number} from ${pr.user.login}`); + continue; + } + + await github.rest.issues.createComment({ + owner, + repo, + issue_number, + body: closeComment, + }); + + await github.rest.pulls.update({ + owner, + repo, + pull_number: issue_number, + state: "closed", + }); + + core.info(`Closed contributor PR #${issue_number} from ${pr.user.login}`); + } diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml index ebf55649b8..c036581325 100644 --- a/.github/workflows/codespell.yml +++ b/.github/workflows/codespell.yml @@ -22,7 +22,6 @@ jobs: - name: Annotate locations with typos uses: codespell-project/codespell-problem-matcher@b80729f885d32f78a716c2f107b4db1025001c42 # v1 - name: Codespell - uses: codespell-project/actions-codespell@406322ec52dd7b488e48c1c4b82e2a8b3a1bf630 # v2 + uses: codespell-project/actions-codespell@8f01853be192eb0f849a5c7d721450e7a467c579 # v2.2 with: ignore_words_file: .codespellignore - skip: frame*.txt diff --git a/.github/workflows/issue-deduplicator.yml b/.github/workflows/issue-deduplicator.yml new file mode 100644 index 0000000000..579b6a3685 --- /dev/null +++ b/.github/workflows/issue-deduplicator.yml @@ -0,0 +1,140 @@ +name: Issue Deduplicator + +on: + issues: + types: + - opened + - labeled + +jobs: + gather-duplicates: + name: Identify potential duplicates + if: ${{ github.event.action == 'opened' || (github.event.action == 'labeled' && github.event.label.name == 'codex-deduplicate') }} + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + codex_output: ${{ steps.codex.outputs.final-message }} + steps: + - uses: actions/checkout@v5 + + - name: Prepare Codex inputs + env: + GH_TOKEN: ${{ github.token }} + run: | + set -eo pipefail + + CURRENT_ISSUE_FILE=codex-current-issue.json + EXISTING_ISSUES_FILE=codex-existing-issues.json + + gh issue list --repo "${{ github.repository }}" \ + --json number,title,body,createdAt \ + --limit 1000 \ + --state all \ + --search "sort:created-desc" \ + | jq '.' \ + > "$EXISTING_ISSUES_FILE" + + gh issue view "${{ github.event.issue.number }}" \ + --repo "${{ github.repository }}" \ + --json number,title,body \ + | jq '.' \ + > "$CURRENT_ISSUE_FILE" + + - id: codex + uses: openai/codex-action@main + with: + openai-api-key: ${{ secrets.CODEX_OPENAI_API_KEY }} + allow-users: "*" + model: gpt-5.1 + prompt: | + You are an assistant that triages new GitHub issues by identifying potential duplicates. + + You will receive the following JSON files located in the current working directory: + - `codex-current-issue.json`: JSON object describing the newly created issue (fields: number, title, body). + - `codex-existing-issues.json`: JSON array of recent issues (each element includes number, title, body, createdAt). + + Instructions: + - Compare the current issue against the existing issues to find up to five that appear to describe the same underlying problem or request. + - Focus on the underlying intent and context of each issue—such as reported symptoms, feature requests, reproduction steps, or error messages—rather than relying solely on string similarity or synthetic metrics. + - After your analysis, validate your results in 1-2 lines explaining your decision to return the selected matches. + - When unsure, prefer returning fewer matches. + - Include at most five numbers. + + output-schema: | + { + "type": "object", + "properties": { + "issues": { + "type": "array", + "items": { + "type": "string" + } + }, + "reason": { "type": "string" } + }, + "required": ["issues", "reason"], + "additionalProperties": false + } + + comment-on-issue: + name: Comment with potential duplicates + needs: gather-duplicates + if: ${{ needs.gather-duplicates.result != 'skipped' }} + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + steps: + - name: Comment on issue + uses: actions/github-script@v8 + env: + CODEX_OUTPUT: ${{ needs.gather-duplicates.outputs.codex_output }} + with: + github-token: ${{ github.token }} + script: | + const raw = process.env.CODEX_OUTPUT ?? ''; + let parsed; + try { + parsed = JSON.parse(raw); + } catch (error) { + core.info(`Codex output was not valid JSON. Raw output: ${raw}`); + core.info(`Parse error: ${error.message}`); + return; + } + + const issues = Array.isArray(parsed?.issues) ? parsed.issues : []; + const currentIssueNumber = String(context.payload.issue.number); + + console.log(`Current issue number: ${currentIssueNumber}`); + console.log(issues); + + const filteredIssues = issues.filter((value) => String(value) !== currentIssueNumber); + + if (filteredIssues.length === 0) { + core.info('Codex reported no potential duplicates.'); + return; + } + + const lines = [ + 'Potential duplicates detected. Please review them and close your issue if it is a duplicate.', + '', + ...filteredIssues.map((value) => `- #${String(value)}`), + '', + '*Powered by [Codex Action](https://github.com/openai/codex-action)*']; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.issue.number, + body: lines.join("\n"), + }); + + - name: Remove codex-deduplicate label + if: ${{ always() && github.event.action == 'labeled' && github.event.label.name == 'codex-deduplicate' }} + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + run: | + gh issue edit "${{ github.event.issue.number }}" --remove-label codex-deduplicate || true + echo "Attempted to remove label: codex-deduplicate" diff --git a/.github/workflows/issue-labeler.yml b/.github/workflows/issue-labeler.yml new file mode 100644 index 0000000000..39f9d47f14 --- /dev/null +++ b/.github/workflows/issue-labeler.yml @@ -0,0 +1,130 @@ +name: Issue Labeler + +on: + issues: + types: + - opened + - labeled + +jobs: + gather-labels: + name: Generate label suggestions + if: ${{ github.event.action == 'opened' || (github.event.action == 'labeled' && github.event.label.name == 'codex-label') }} + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + codex_output: ${{ steps.codex.outputs.final-message }} + steps: + - uses: actions/checkout@v5 + + - id: codex + uses: openai/codex-action@main + with: + openai-api-key: ${{ secrets.CODEX_OPENAI_API_KEY }} + allow-users: "*" + prompt: | + You are an assistant that reviews GitHub issues for the repository. + + Your job is to choose the most appropriate labels for the issue described later in this prompt. + Follow these rules: + + - Add one (and only one) of the following three labels to distinguish the type of issue. Default to "bug" if unsure. + 1. bug — Reproducible defects in Codex products (CLI, VS Code extension, web, auth). + 2. enhancement — Feature requests or usability improvements that ask for new capabilities, better ergonomics, or quality-of-life tweaks. + 3. documentation — Updates or corrections needed in docs/README/config references (broken links, missing examples, outdated keys, clarification requests). + + - If applicable, add one of the following labels to specify which sub-product or product surface the issue relates to. + 1. CLI — the Codex command line interface. + 2. extension — VS Code (or other IDE) extension-specific issues. + 3. codex-web — Issues targeting the Codex web UI/Cloud experience. + 4. github-action — Issues with the Codex GitHub action. + 5. iOS — Issues with the Codex iOS app. + + - Additionally add zero or more of the following labels that are relevant to the issue content. Prefer a small set of precise labels over many broad ones. + 1. windows-os — Bugs or friction specific to Windows environments (always when PowerShell is mentioned, path handling, copy/paste, OS-specific auth or tooling failures). + 2. mcp — Topics involving Model Context Protocol servers/clients. + 3. mcp-server — Problems related to the codex mcp-server command, where codex runs as an MCP server. + 4. azure — Problems or requests tied to Azure OpenAI deployments. + 5. model-behavior — Undesirable LLM behavior: forgetting goals, refusing work, hallucinating environment details, quota misreports, or other reasoning/performance anomalies. + 6. code-review — Issues related to the code review feature or functionality. + 7. auth - Problems related to authentication, login, or access tokens. + 8. codex-exec - Problems related to the "codex exec" command or functionality. + 9. context-management - Problems related to compaction, context windows, or available context reporting. + 10. custom-model - Problems that involve using custom model providers, local models, or OSS models. + 11. rate-limits - Problems related to token limits, rate limits, or token usage reporting. + 12. sandbox - Issues related to local sandbox environments or tool call approvals to override sandbox restrictions. + 13. tool-calls - Problems related to specific tool call invocations including unexpected errors, failures, or hangs. + 14. TUI - Problems with the terminal user interface (TUI) including keyboard shortcuts, copy & pasting, menus, or screen update issues. + + Issue number: ${{ github.event.issue.number }} + + Issue title: + ${{ github.event.issue.title }} + + Issue body: + ${{ github.event.issue.body }} + + Repository full name: + ${{ github.repository }} + + output-schema: | + { + "type": "object", + "properties": { + "labels": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "required": ["labels"], + "additionalProperties": false + } + + apply-labels: + name: Apply labels from Codex output + needs: gather-labels + if: ${{ needs.gather-labels.result != 'skipped' }} + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + CODEX_OUTPUT: ${{ needs.gather-labels.outputs.codex_output }} + steps: + - name: Apply labels + run: | + json=${CODEX_OUTPUT//$'\r'/} + if [ -z "$json" ]; then + echo "Codex produced no output. Skipping label application." + exit 0 + fi + + if ! printf '%s' "$json" | jq -e 'type == "object" and (.labels | type == "array")' >/dev/null 2>&1; then + echo "Codex output did not include a labels array. Raw output: $json" + exit 0 + fi + + labels=$(printf '%s' "$json" | jq -r '.labels[] | tostring') + if [ -z "$labels" ]; then + echo "Codex returned an empty array. Nothing to do." + exit 0 + fi + + cmd=(gh issue edit "$ISSUE_NUMBER") + while IFS= read -r label; do + cmd+=(--add-label "$label") + done <<< "$labels" + + "${cmd[@]}" || true + + - name: Remove codex-label trigger + if: ${{ always() && github.event.action == 'labeled' && github.event.label.name == 'codex-label' }} + run: | + gh issue edit "$ISSUE_NUMBER" --remove-label codex-label || true + echo "Attempted to remove label: codex-label" diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index 280939c611..0bd91ca53b 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -9,7 +9,7 @@ on: # CI builds in debug (dev) for faster signal. jobs: - # --- Detect what changed (always runs) ------------------------------------- + # --- Detect what changed to detect which tests to run (always runs) ------------------------------------- changed: name: Detect changed areas runs-on: ubuntu-24.04 @@ -57,7 +57,7 @@ jobs: working-directory: codex-rs steps: - uses: actions/checkout@v5 - - uses: dtolnay/rust-toolchain@1.89 + - uses: dtolnay/rust-toolchain@1.90 with: components: rustfmt - name: cargo fmt @@ -75,8 +75,8 @@ jobs: working-directory: codex-rs steps: - uses: actions/checkout@v5 - - uses: dtolnay/rust-toolchain@1.89 - - uses: taiki-e/install-action@0c5db7f7f897c03b771660e91d065338615679f4 # v2 + - uses: dtolnay/rust-toolchain@1.90 + - uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 with: tool: cargo-shear version: 1.5.1 @@ -84,8 +84,8 @@ jobs: run: cargo shear # --- CI to validate on different os/targets -------------------------------- - lint_build_test: - name: ${{ matrix.runner }} - ${{ matrix.target }}${{ matrix.profile == 'release' && ' (release)' || '' }} + lint_build: + name: Lint/Build — ${{ matrix.runner }} - ${{ matrix.target }}${{ matrix.profile == 'release' && ' (release)' || '' }} runs-on: ${{ matrix.runner }} timeout-minutes: 30 needs: changed @@ -94,6 +94,11 @@ jobs: defaults: run: working-directory: codex-rs + env: + # Speed up repeated builds across CI runs by caching compiled objects (non-Windows). + USE_SCCACHE: ${{ startsWith(matrix.runner, 'windows') && 'false' || 'true' }} + CARGO_INCREMENTAL: "0" + SCCACHE_CACHE_SIZE: 10G strategy: fail-fast: false @@ -143,25 +148,106 @@ jobs: steps: - uses: actions/checkout@v5 - - uses: dtolnay/rust-toolchain@1.89 + - uses: dtolnay/rust-toolchain@1.90 with: targets: ${{ matrix.target }} components: clippy - - uses: actions/cache@v4 + # Explicit cache restore: split cargo home vs target, so we can + # avoid caching the large target dir on the gnu-dev job. + - name: Restore cargo home cache + id: cache_cargo_home_restore + uses: actions/cache/restore@v4 with: path: | ~/.cargo/bin/ ~/.cargo/registry/index/ ~/.cargo/registry/cache/ ~/.cargo/git/db/ - ${{ github.workspace }}/codex-rs/target/ - key: cargo-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }} + key: cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('codex-rs/rust-toolchain.toml') }} + restore-keys: | + cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}- + + # Install and restore sccache cache + - name: Install sccache + if: ${{ env.USE_SCCACHE == 'true' }} + uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 + with: + tool: sccache + version: 0.7.5 + + - name: Configure sccache backend + if: ${{ env.USE_SCCACHE == 'true' }} + shell: bash + run: | + set -euo pipefail + if [[ -n "${ACTIONS_CACHE_URL:-}" && -n "${ACTIONS_RUNTIME_TOKEN:-}" ]]; then + echo "SCCACHE_GHA_ENABLED=true" >> "$GITHUB_ENV" + echo "Using sccache GitHub backend" + else + echo "SCCACHE_GHA_ENABLED=false" >> "$GITHUB_ENV" + echo "SCCACHE_DIR=${{ github.workspace }}/.sccache" >> "$GITHUB_ENV" + echo "Using sccache local disk + actions/cache fallback" + fi + + - name: Enable sccache wrapper + if: ${{ env.USE_SCCACHE == 'true' }} + shell: bash + run: echo "RUSTC_WRAPPER=sccache" >> "$GITHUB_ENV" + + - name: Restore sccache cache (fallback) + if: ${{ env.USE_SCCACHE == 'true' && env.SCCACHE_GHA_ENABLED != 'true' }} + id: cache_sccache_restore + uses: actions/cache/restore@v4 + with: + path: ${{ github.workspace }}/.sccache/ + key: sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ github.run_id }} + restore-keys: | + sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}- + sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}- + + - if: ${{ matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-unknown-linux-musl'}} + name: Prepare APT cache directories (musl) + shell: bash + run: | + set -euo pipefail + sudo mkdir -p /var/cache/apt/archives /var/lib/apt/lists + sudo chown -R "$USER:$USER" /var/cache/apt /var/lib/apt/lists + + - if: ${{ matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-unknown-linux-musl'}} + name: Restore APT cache (musl) + id: cache_apt_restore + uses: actions/cache/restore@v4 + with: + path: | + /var/cache/apt + key: apt-${{ matrix.runner }}-${{ matrix.target }}-v1 - if: ${{ matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-unknown-linux-musl'}} name: Install musl build tools + env: + DEBIAN_FRONTEND: noninteractive + shell: bash run: | - sudo apt install -y musl-tools pkg-config && sudo rm -rf /var/lib/apt/lists/* + set -euo pipefail + sudo apt-get -y update -o Acquire::Retries=3 + sudo apt-get -y install --no-install-recommends musl-tools pkg-config + + - name: Install cargo-chef + if: ${{ matrix.profile == 'release' }} + uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 + with: + tool: cargo-chef + version: 0.1.71 + + - name: Pre-warm dependency cache (cargo-chef) + if: ${{ matrix.profile == 'release' }} + shell: bash + run: | + set -euo pipefail + RECIPE="${RUNNER_TEMP}/chef-recipe.json" + cargo chef prepare --recipe-path "$RECIPE" + cargo chef cook --recipe-path "$RECIPE" --target ${{ matrix.target }} --release --all-features - name: cargo clippy id: clippy @@ -180,34 +266,213 @@ jobs: find . -name Cargo.toml -mindepth 2 -maxdepth 2 -print0 \ | xargs -0 -n1 -I{} bash -c 'cd "$(dirname "{}")" && cargo check --profile ${{ matrix.profile }}' - - uses: taiki-e/install-action@0c5db7f7f897c03b771660e91d065338615679f4 # v2 + # Save caches explicitly; make non-fatal so cache packaging + # never fails the overall job. Only save when key wasn't hit. + - name: Save cargo home cache + if: always() && !cancelled() && steps.cache_cargo_home_restore.outputs.cache-hit != 'true' + continue-on-error: true + uses: actions/cache/save@v4 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + key: cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('codex-rs/rust-toolchain.toml') }} + + - name: Save sccache cache (fallback) + if: always() && !cancelled() && env.USE_SCCACHE == 'true' && env.SCCACHE_GHA_ENABLED != 'true' + continue-on-error: true + uses: actions/cache/save@v4 + with: + path: ${{ github.workspace }}/.sccache/ + key: sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ github.run_id }} + + - name: sccache stats + if: always() && env.USE_SCCACHE == 'true' + continue-on-error: true + run: sccache --show-stats || true + + - name: sccache summary + if: always() && env.USE_SCCACHE == 'true' + shell: bash + run: | + { + echo "### sccache stats — ${{ matrix.target }} (${{ matrix.profile }})"; + echo; + echo '```'; + sccache --show-stats || true; + echo '```'; + } >> "$GITHUB_STEP_SUMMARY" + + - name: Save APT cache (musl) + if: always() && !cancelled() && (matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-unknown-linux-musl') && steps.cache_apt_restore.outputs.cache-hit != 'true' + continue-on-error: true + uses: actions/cache/save@v4 + with: + path: | + /var/cache/apt + key: apt-${{ matrix.runner }}-${{ matrix.target }}-v1 + + # Fail the job if any of the previous steps failed. + - name: verify all steps passed + if: | + steps.clippy.outcome == 'failure' || + steps.cargo_check_all_crates.outcome == 'failure' + run: | + echo "One or more checks failed (clippy or cargo_check_all_crates). See logs for details." + exit 1 + + tests: + name: Tests — ${{ matrix.runner }} - ${{ matrix.target }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 30 + needs: changed + if: ${{ needs.changed.outputs.codex == 'true' || needs.changed.outputs.workflows == 'true' || github.event_name == 'push' }} + defaults: + run: + working-directory: codex-rs + env: + # Speed up repeated builds across CI runs by caching compiled objects (non-Windows). + USE_SCCACHE: ${{ startsWith(matrix.runner, 'windows') && 'false' || 'true' }} + CARGO_INCREMENTAL: "0" + SCCACHE_CACHE_SIZE: 10G + + strategy: + fail-fast: false + matrix: + include: + - runner: macos-14 + target: aarch64-apple-darwin + profile: dev + - runner: ubuntu-24.04 + target: x86_64-unknown-linux-gnu + profile: dev + - runner: ubuntu-24.04-arm + target: aarch64-unknown-linux-gnu + profile: dev + - runner: windows-latest + target: x86_64-pc-windows-msvc + profile: dev + - runner: windows-11-arm + target: aarch64-pc-windows-msvc + profile: dev + + steps: + - uses: actions/checkout@v5 + - uses: dtolnay/rust-toolchain@1.90 + with: + targets: ${{ matrix.target }} + + - name: Restore cargo home cache + id: cache_cargo_home_restore + uses: actions/cache/restore@v4 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + key: cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('codex-rs/rust-toolchain.toml') }} + restore-keys: | + cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}- + + - name: Install sccache + if: ${{ env.USE_SCCACHE == 'true' }} + uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 + with: + tool: sccache + version: 0.7.5 + + - name: Configure sccache backend + if: ${{ env.USE_SCCACHE == 'true' }} + shell: bash + run: | + set -euo pipefail + if [[ -n "${ACTIONS_CACHE_URL:-}" && -n "${ACTIONS_RUNTIME_TOKEN:-}" ]]; then + echo "SCCACHE_GHA_ENABLED=true" >> "$GITHUB_ENV" + echo "Using sccache GitHub backend" + else + echo "SCCACHE_GHA_ENABLED=false" >> "$GITHUB_ENV" + echo "SCCACHE_DIR=${{ github.workspace }}/.sccache" >> "$GITHUB_ENV" + echo "Using sccache local disk + actions/cache fallback" + fi + + - name: Enable sccache wrapper + if: ${{ env.USE_SCCACHE == 'true' }} + shell: bash + run: echo "RUSTC_WRAPPER=sccache" >> "$GITHUB_ENV" + + - name: Restore sccache cache (fallback) + if: ${{ env.USE_SCCACHE == 'true' && env.SCCACHE_GHA_ENABLED != 'true' }} + id: cache_sccache_restore + uses: actions/cache/restore@v4 + with: + path: ${{ github.workspace }}/.sccache/ + key: sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ github.run_id }} + restore-keys: | + sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}- + sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}- + + - uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 with: tool: nextest version: 0.9.103 - name: tests id: test - # Tests take too long for release builds to run them on every PR. - if: ${{ matrix.profile != 'release' }} - continue-on-error: true - run: cargo nextest run --all-features --no-fail-fast --target ${{ matrix.target }} + run: cargo nextest run --all-features --no-fail-fast --target ${{ matrix.target }} --cargo-profile ci-test env: RUST_BACKTRACE: 1 + NEXTEST_STATUS_LEVEL: leak - # Fail the job if any of the previous steps failed. - - name: verify all steps passed - if: | - steps.clippy.outcome == 'failure' || - steps.cargo_check_all_crates.outcome == 'failure' || - steps.test.outcome == 'failure' + - name: Save cargo home cache + if: always() && !cancelled() && steps.cache_cargo_home_restore.outputs.cache-hit != 'true' + continue-on-error: true + uses: actions/cache/save@v4 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + key: cargo-home-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('codex-rs/rust-toolchain.toml') }} + + - name: Save sccache cache (fallback) + if: always() && !cancelled() && env.USE_SCCACHE == 'true' && env.SCCACHE_GHA_ENABLED != 'true' + continue-on-error: true + uses: actions/cache/save@v4 + with: + path: ${{ github.workspace }}/.sccache/ + key: sccache-${{ matrix.runner }}-${{ matrix.target }}-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}-${{ github.run_id }} + + - name: sccache stats + if: always() && env.USE_SCCACHE == 'true' + continue-on-error: true + run: sccache --show-stats || true + + - name: sccache summary + if: always() && env.USE_SCCACHE == 'true' + shell: bash run: | - echo "One or more checks failed (clippy, cargo_check_all_crates, or test). See logs for details." + { + echo "### sccache stats — ${{ matrix.target }} (tests)"; + echo; + echo '```'; + sccache --show-stats || true; + echo '```'; + } >> "$GITHUB_STEP_SUMMARY" + + - name: verify tests passed + if: steps.test.outcome == 'failure' + run: | + echo "Tests failed. See logs for details." exit 1 # --- Gatherer job that you mark as the ONLY required status ----------------- results: name: CI results (required) - needs: [changed, general, cargo_shear, lint_build_test] + needs: [changed, general, cargo_shear, lint_build, tests] if: always() runs-on: ubuntu-24.04 steps: @@ -216,7 +481,8 @@ jobs: run: | echo "general: ${{ needs.general.result }}" echo "shear : ${{ needs.cargo_shear.result }}" - echo "matrix : ${{ needs.lint_build_test.result }}" + echo "lint : ${{ needs.lint_build.result }}" + echo "tests : ${{ needs.tests.result }}" # If nothing relevant changed (PR touching only root README, etc.), # declare success regardless of other jobs. @@ -228,4 +494,10 @@ jobs: # Otherwise require the jobs to have succeeded [[ '${{ needs.general.result }}' == 'success' ]] || { echo 'general failed'; exit 1; } [[ '${{ needs.cargo_shear.result }}' == 'success' ]] || { echo 'cargo_shear failed'; exit 1; } - [[ '${{ needs.lint_build_test.result }}' == 'success' ]] || { echo 'matrix failed'; exit 1; } + [[ '${{ needs.lint_build.result }}' == 'success' ]] || { echo 'lint_build failed'; exit 1; } + [[ '${{ needs.tests.result }}' == 'success' ]] || { echo 'tests failed'; exit 1; } + + - name: sccache summary note + if: always() + run: | + echo "Per-job sccache stats are attached to each matrix job's Step Summary." diff --git a/.github/workflows/rust-release.yml b/.github/workflows/rust-release.yml index e3776f149c..6f27fbf543 100644 --- a/.github/workflows/rust-release.yml +++ b/.github/workflows/rust-release.yml @@ -47,7 +47,7 @@ jobs: build: needs: tag-check - name: ${{ matrix.runner }} - ${{ matrix.target }} + name: Build - ${{ matrix.runner }} - ${{ matrix.target }} runs-on: ${{ matrix.runner }} timeout-minutes: 30 defaults: @@ -58,9 +58,9 @@ jobs: fail-fast: false matrix: include: - - runner: macos-14 + - runner: macos-15-xlarge target: aarch64-apple-darwin - - runner: macos-14 + - runner: macos-15-xlarge target: x86_64-apple-darwin - runner: ubuntu-24.04 target: x86_64-unknown-linux-musl @@ -77,7 +77,7 @@ jobs: steps: - uses: actions/checkout@v5 - - uses: dtolnay/rust-toolchain@1.89 + - uses: dtolnay/rust-toolchain@1.90 with: targets: ${{ matrix.target }} @@ -94,10 +94,180 @@ jobs: - if: ${{ matrix.target == 'x86_64-unknown-linux-musl' || matrix.target == 'aarch64-unknown-linux-musl'}} name: Install musl build tools run: | - sudo apt install -y musl-tools pkg-config + sudo apt-get update + sudo apt-get install -y musl-tools pkg-config - name: Cargo build - run: cargo build --target ${{ matrix.target }} --release --bin codex + run: cargo build --target ${{ matrix.target }} --release --bin codex --bin codex-responses-api-proxy + + - if: ${{ matrix.runner == 'macos-15-xlarge' }} + name: Configure Apple code signing + shell: bash + env: + KEYCHAIN_PASSWORD: actions + APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE_P12 }} + APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }} + run: | + set -euo pipefail + + if [[ -z "${APPLE_CERTIFICATE:-}" ]]; then + echo "APPLE_CERTIFICATE is required for macOS signing" + exit 1 + fi + + if [[ -z "${APPLE_CERTIFICATE_PASSWORD:-}" ]]; then + echo "APPLE_CERTIFICATE_PASSWORD is required for macOS signing" + exit 1 + fi + + cert_path="${RUNNER_TEMP}/apple_signing_certificate.p12" + echo "$APPLE_CERTIFICATE" | base64 -d > "$cert_path" + + keychain_path="${RUNNER_TEMP}/codex-signing.keychain-db" + security create-keychain -p "$KEYCHAIN_PASSWORD" "$keychain_path" + security set-keychain-settings -lut 21600 "$keychain_path" + security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$keychain_path" + + keychain_args=() + cleanup_keychain() { + if ((${#keychain_args[@]} > 0)); then + security list-keychains -s "${keychain_args[@]}" || true + security default-keychain -s "${keychain_args[0]}" || true + else + security list-keychains -s || true + fi + if [[ -f "$keychain_path" ]]; then + security delete-keychain "$keychain_path" || true + fi + } + + while IFS= read -r keychain; do + [[ -n "$keychain" ]] && keychain_args+=("$keychain") + done < <(security list-keychains | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/"//g') + + if ((${#keychain_args[@]} > 0)); then + security list-keychains -s "$keychain_path" "${keychain_args[@]}" + else + security list-keychains -s "$keychain_path" + fi + + security default-keychain -s "$keychain_path" + security import "$cert_path" -k "$keychain_path" -P "$APPLE_CERTIFICATE_PASSWORD" -T /usr/bin/codesign -T /usr/bin/security + security set-key-partition-list -S apple-tool:,apple: -s -k "$KEYCHAIN_PASSWORD" "$keychain_path" > /dev/null + + codesign_hashes=() + while IFS= read -r hash; do + [[ -n "$hash" ]] && codesign_hashes+=("$hash") + done < <(security find-identity -v -p codesigning "$keychain_path" \ + | sed -n 's/.*\([0-9A-F]\{40\}\).*/\1/p' \ + | sort -u) + + if ((${#codesign_hashes[@]} == 0)); then + echo "No signing identities found in $keychain_path" + cleanup_keychain + rm -f "$cert_path" + exit 1 + fi + + if ((${#codesign_hashes[@]} > 1)); then + echo "Multiple signing identities found in $keychain_path:" + printf ' %s\n' "${codesign_hashes[@]}" + cleanup_keychain + rm -f "$cert_path" + exit 1 + fi + + APPLE_CODESIGN_IDENTITY="${codesign_hashes[0]}" + + rm -f "$cert_path" + + echo "APPLE_CODESIGN_IDENTITY=$APPLE_CODESIGN_IDENTITY" >> "$GITHUB_ENV" + echo "APPLE_CODESIGN_KEYCHAIN=$keychain_path" >> "$GITHUB_ENV" + echo "::add-mask::$APPLE_CODESIGN_IDENTITY" + + - if: ${{ matrix.runner == 'macos-15-xlarge' }} + name: Sign macOS binaries + shell: bash + run: | + set -euo pipefail + + if [[ -z "${APPLE_CODESIGN_IDENTITY:-}" ]]; then + echo "APPLE_CODESIGN_IDENTITY is required for macOS signing" + exit 1 + fi + + keychain_args=() + if [[ -n "${APPLE_CODESIGN_KEYCHAIN:-}" && -f "${APPLE_CODESIGN_KEYCHAIN}" ]]; then + keychain_args+=(--keychain "${APPLE_CODESIGN_KEYCHAIN}") + fi + + for binary in codex codex-responses-api-proxy; do + path="target/${{ matrix.target }}/release/${binary}" + codesign --force --options runtime --timestamp --sign "$APPLE_CODESIGN_IDENTITY" "${keychain_args[@]}" "$path" + done + + - if: ${{ matrix.runner == 'macos-15-xlarge' }} + name: Notarize macOS binaries + shell: bash + env: + APPLE_NOTARIZATION_KEY_P8: ${{ secrets.APPLE_NOTARIZATION_KEY_P8 }} + APPLE_NOTARIZATION_KEY_ID: ${{ secrets.APPLE_NOTARIZATION_KEY_ID }} + APPLE_NOTARIZATION_ISSUER_ID: ${{ secrets.APPLE_NOTARIZATION_ISSUER_ID }} + run: | + set -euo pipefail + + for var in APPLE_NOTARIZATION_KEY_P8 APPLE_NOTARIZATION_KEY_ID APPLE_NOTARIZATION_ISSUER_ID; do + if [[ -z "${!var:-}" ]]; then + echo "$var is required for notarization" + exit 1 + fi + done + + notary_key_path="${RUNNER_TEMP}/notarytool.key.p8" + echo "$APPLE_NOTARIZATION_KEY_P8" | base64 -d > "$notary_key_path" + cleanup_notary() { + rm -f "$notary_key_path" + } + trap cleanup_notary EXIT + + notarize_binary() { + local binary="$1" + local source_path="target/${{ matrix.target }}/release/${binary}" + local archive_path="${RUNNER_TEMP}/${binary}.zip" + + if [[ ! -f "$source_path" ]]; then + echo "Binary $source_path not found" + exit 1 + fi + + rm -f "$archive_path" + ditto -c -k --keepParent "$source_path" "$archive_path" + + submission_json=$(xcrun notarytool submit "$archive_path" \ + --key "$notary_key_path" \ + --key-id "$APPLE_NOTARIZATION_KEY_ID" \ + --issuer "$APPLE_NOTARIZATION_ISSUER_ID" \ + --output-format json \ + --wait) + + status=$(printf '%s\n' "$submission_json" | jq -r '.status // "Unknown"') + submission_id=$(printf '%s\n' "$submission_json" | jq -r '.id // ""') + + if [[ -z "$submission_id" ]]; then + echo "Failed to retrieve submission ID for $binary" + exit 1 + fi + + echo "::notice title=Notarization::$binary submission ${submission_id} completed with status ${status}" + + if [[ "$status" != "Accepted" ]]; then + echo "Notarization failed for ${binary} (submission ${submission_id}, status ${status})" + exit 1 + fi + } + + notarize_binary "codex" + notarize_binary "codex-responses-api-proxy" - name: Stage artifacts shell: bash @@ -107,8 +277,10 @@ jobs: if [[ "${{ matrix.runner }}" == windows* ]]; then cp target/${{ matrix.target }}/release/codex.exe "$dest/codex-${{ matrix.target }}.exe" + cp target/${{ matrix.target }}/release/codex-responses-api-proxy.exe "$dest/codex-responses-api-proxy-${{ matrix.target }}.exe" else cp target/${{ matrix.target }}/release/codex "$dest/codex-${{ matrix.target }}" + cp target/${{ matrix.target }}/release/codex-responses-api-proxy "$dest/codex-responses-api-proxy-${{ matrix.target }}" fi - if: ${{ matrix.runner == 'windows-11-arm' }} @@ -123,6 +295,15 @@ jobs: # ${{ matrix.target }} dest="dist/${{ matrix.target }}" + # We want to ship the raw Windows executables in the GitHub Release + # in addition to the compressed archives. Keep the originals for + # Windows targets; remove them elsewhere to limit the number of + # artifacts that end up in the GitHub Release. + keep_originals=false + if [[ "${{ matrix.runner }}" == windows* ]]; then + keep_originals=true + fi + # For compatibility with environments that lack the `zstd` tool we # additionally create a `.tar.gz` for all platforms and `.zip` for # Windows alongside every single binary that we publish. The end result is: @@ -152,10 +333,37 @@ jobs: # Also create .zst (existing behaviour) *and* remove the original # uncompressed binary to keep the directory small. - zstd -T0 -19 --rm "$dest/$base" + zstd_args=(-T0 -19) + if [[ "${keep_originals}" == false ]]; then + zstd_args+=(--rm) + fi + zstd "${zstd_args[@]}" "$dest/$base" done - - uses: actions/upload-artifact@v4 + - name: Remove signing keychain + if: ${{ always() && matrix.runner == 'macos-15-xlarge' }} + shell: bash + env: + APPLE_CODESIGN_KEYCHAIN: ${{ env.APPLE_CODESIGN_KEYCHAIN }} + run: | + set -euo pipefail + if [[ -n "${APPLE_CODESIGN_KEYCHAIN:-}" ]]; then + keychain_args=() + while IFS= read -r keychain; do + [[ "$keychain" == "$APPLE_CODESIGN_KEYCHAIN" ]] && continue + [[ -n "$keychain" ]] && keychain_args+=("$keychain") + done < <(security list-keychains | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/"//g') + if ((${#keychain_args[@]} > 0)); then + security list-keychains -s "${keychain_args[@]}" + security default-keychain -s "${keychain_args[0]}" + fi + + if [[ -f "$APPLE_CODESIGN_KEYCHAIN" ]]; then + security delete-keychain "$APPLE_CODESIGN_KEYCHAIN" + fi + fi + + - uses: actions/upload-artifact@v5 with: name: ${{ matrix.target }} # Upload the per-binary .zst files as well as the new .tar.gz @@ -173,6 +381,8 @@ jobs: outputs: version: ${{ steps.release_name.outputs.name }} tag: ${{ github.ref_name }} + should_publish_npm: ${{ steps.npm_publish_settings.outputs.should_publish }} + npm_tag: ${{ steps.npm_publish_settings.outputs.npm_tag }} steps: - name: Checkout repository @@ -193,21 +403,49 @@ jobs: version="${GITHUB_REF_NAME#rust-v}" echo "name=${version}" >> $GITHUB_OUTPUT - - name: Stage npm package + - name: Determine npm publish settings + id: npm_publish_settings + env: + VERSION: ${{ steps.release_name.outputs.name }} + run: | + set -euo pipefail + version="${VERSION}" + + if [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "should_publish=true" >> "$GITHUB_OUTPUT" + echo "npm_tag=" >> "$GITHUB_OUTPUT" + elif [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+-alpha\.[0-9]+$ ]]; then + echo "should_publish=true" >> "$GITHUB_OUTPUT" + echo "npm_tag=alpha" >> "$GITHUB_OUTPUT" + else + echo "should_publish=false" >> "$GITHUB_OUTPUT" + echo "npm_tag=" >> "$GITHUB_OUTPUT" + fi + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + with: + run_install: false + + - name: Setup Node.js for npm packaging + uses: actions/setup-node@v5 + with: + node-version: 22 + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + # stage_npm_packages.py requires DotSlash when staging releases. + - uses: facebook/install-dotslash@v2 + - name: Stage npm packages env: GH_TOKEN: ${{ github.token }} run: | - set -euo pipefail - TMP_DIR="${RUNNER_TEMP}/npm-stage" - python3 codex-cli/scripts/stage_rust_release.py \ + ./scripts/stage_npm_packages.py \ --release-version "${{ steps.release_name.outputs.name }}" \ - --tmp "${TMP_DIR}" - mkdir -p dist/npm - # Produce an npm-ready tarball using `npm pack` and store it in dist/npm. - # We then rename it to a stable name used by our publishing script. - (cd "$TMP_DIR" && npm pack --pack-destination "${GITHUB_WORKSPACE}/dist/npm") - mv "${GITHUB_WORKSPACE}"/dist/npm/*.tgz \ - "${GITHUB_WORKSPACE}/dist/npm/codex-npm-${{ steps.release_name.outputs.name }}.tgz" + --package codex \ + --package codex-responses-api-proxy \ + --package codex-sdk - name: Create GitHub Release uses: softprops/action-gh-release@v2 @@ -230,8 +468,8 @@ jobs: # July 31, 2025: https://github.blog/changelog/2025-07-31-npm-trusted-publishing-with-oidc-is-generally-available/ # npm docs: https://docs.npmjs.com/trusted-publishers publish-npm: - # Skip this step for pre-releases (alpha/beta). - if: ${{ !contains(needs.release.outputs.version, '-') }} + # Publish to npm for stable releases and alpha pre-releases with numeric suffixes. + if: ${{ needs.release.outputs.should_publish_npm == 'true' }} name: publish-npm needs: release runs-on: ubuntu-latest @@ -251,7 +489,7 @@ jobs: - name: Update npm run: npm install -g npm@latest - - name: Download npm tarball from release + - name: Download npm tarballs from release env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -263,10 +501,36 @@ jobs: --repo "${GITHUB_REPOSITORY}" \ --pattern "codex-npm-${version}.tgz" \ --dir dist/npm + gh release download "$tag" \ + --repo "${GITHUB_REPOSITORY}" \ + --pattern "codex-responses-api-proxy-npm-${version}.tgz" \ + --dir dist/npm + gh release download "$tag" \ + --repo "${GITHUB_REPOSITORY}" \ + --pattern "codex-sdk-npm-${version}.tgz" \ + --dir dist/npm # No NODE_AUTH_TOKEN needed because we use OIDC. - name: Publish to npm - run: npm publish "${GITHUB_WORKSPACE}/dist/npm/codex-npm-${{ needs.release.outputs.version }}.tgz" + env: + VERSION: ${{ needs.release.outputs.version }} + NPM_TAG: ${{ needs.release.outputs.npm_tag }} + run: | + set -euo pipefail + tag_args=() + if [[ -n "${NPM_TAG}" ]]; then + tag_args+=(--tag "${NPM_TAG}") + fi + + tarballs=( + "codex-npm-${VERSION}.tgz" + "codex-responses-api-proxy-npm-${VERSION}.tgz" + "codex-sdk-npm-${VERSION}.tgz" + ) + + for tarball in "${tarballs[@]}"; do + npm publish "${GITHUB_WORKSPACE}/dist/npm/${tarball}" "${tag_args[@]}" + done update-branch: name: Update latest-alpha-cli branch diff --git a/.github/workflows/sdk.yml b/.github/workflows/sdk.yml new file mode 100644 index 0000000000..0f3a7a194b --- /dev/null +++ b/.github/workflows/sdk.yml @@ -0,0 +1,43 @@ +name: sdk + +on: + push: + branches: [main] + pull_request: {} + +jobs: + sdks: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Checkout repository + uses: actions/checkout@v5 + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + with: + run_install: false + + - name: Setup Node.js + uses: actions/setup-node@v5 + with: + node-version: 22 + cache: pnpm + + - uses: dtolnay/rust-toolchain@1.90 + + - name: build codex + run: cargo build --bin codex + working-directory: codex-rs + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build SDK packages + run: pnpm -r --filter ./sdk/typescript run build + + - name: Lint SDK packages + run: pnpm -r --filter ./sdk/typescript run lint + + - name: Test SDK packages + run: pnpm -r --filter ./sdk/typescript run test diff --git a/.gitignore b/.gitignore index a264d91822..a58e9dfb7b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ result # cli tools CLAUDE.md .claude/ +AGENTS.override.md # caches .cache/ @@ -63,6 +64,9 @@ apply_patch/ # coverage coverage/ +# personal files +personal/ + # os .DS_Store Thumbs.db diff --git a/.vscode/settings.json b/.vscode/settings.json index aadeca0867..5adec04a83 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "rust-analyzer.check.command": "clippy", "rust-analyzer.check.extraArgs": ["--all-features", "--tests"], "rust-analyzer.rustfmt.extraArgs": ["--config", "imports_granularity=Item"], + "rust-analyzer.cargo.targetDir": "${workspaceFolder}/codex-rs/target/rust-analyzer", "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true, diff --git a/AGENTS.md b/AGENTS.md index 288719447e..aaebd0dfd3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -8,11 +8,18 @@ In the codex-rs folder where the rust code lives: - Never add or modify any code related to `CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR` or `CODEX_SANDBOX_ENV_VAR`. - You operate in a sandbox where `CODEX_SANDBOX_NETWORK_DISABLED=1` will be set whenever you use the `shell` tool. Any existing code that uses `CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR` was authored with this fact in mind. It is often used to early exit out of tests that the author knew you would not be able to run given your sandbox limitations. - Similarly, when you spawn a process using Seatbelt (`/usr/bin/sandbox-exec`), `CODEX_SANDBOX=seatbelt` will be set on the child process. Integration tests that want to run Seatbelt themselves cannot be run under Seatbelt, so checks for `CODEX_SANDBOX=seatbelt` are also often used to early exit out of tests, as appropriate. +- Always collapse if statements per https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if +- Always inline format! args when possible per https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args +- Use method references over closures when possible per https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls +- Do not use unsigned integer even if the number cannot be negative. +- When writing tests, prefer comparing the equality of entire objects over fields one by one. +- When making a change that adds or changes an API, ensure that the documentation in the `docs/` folder is up to date if applicable. Run `just fmt` (in `codex-rs` directory) automatically after making Rust code changes; do not ask for approval to run it. Before finalizing a change to `codex-rs`, run `just fix -p ` (in `codex-rs` directory) to fix any linter issues in the code. Prefer scoping with `-p` to avoid slow workspace‑wide Clippy builds; only run `just fix` without `-p` if you changed shared crates. Additionally, run the tests: + 1. Run the test for the specific project that was changed. For example, if changes were made in `codex-rs/tui`, run `cargo test -p codex-tui`. 2. Once those pass, if any changes were made in common, core, or protocol, run the complete test suite with `cargo test --all-features`. -When running interactively, ask the user before running `just fix` to finalize. `just fmt` does not require approval. project-specific or individual tests can be run without asking the user, but do ask the user before running the complete test suite. + When running interactively, ask the user before running `just fix` to finalize. `just fmt` does not require approval. project-specific or individual tests can be run without asking the user, but do ask the user before running the complete test suite. ## TUI style conventions @@ -28,6 +35,7 @@ See `codex-rs/tui/styles.md`. - Desired: vec![" └ ".into(), "M".red(), " ".dim(), "tui/src/app.rs".dim()] ### TUI Styling (ratatui) + - Prefer Stylize helpers: use "text".dim(), .bold(), .cyan(), .italic(), .underlined() instead of manual Style where possible. - Prefer simple conversions: use "text".into() for spans and vec![…].into() for lines; when inference is ambiguous (e.g., Paragraph::new/Cell::from), use Line::from(spans) or Span::from(text). - Computed styles: if the Style is computed at runtime, using `Span::styled` is OK (`Span::from(text).set_style(style)` is also acceptable). @@ -39,6 +47,7 @@ See `codex-rs/tui/styles.md`. - Compactness: prefer the form that stays on one line after rustfmt; if only one of Line::from(vec![…]) or vec![…].into() avoids wrapping, choose that. If both wrap, pick the one with fewer wrapped lines. ### Text wrapping + - Always use textwrap::wrap to wrap plain strings. - If you have a ratatui Line and you want to wrap it, use the helpers in tui/src/wrapping.rs, e.g. word_wrap_lines / word_wrap_line. - If you need to indent wrapped lines, use the initial_indent / subsequent_indent options from RtOptions if you can, rather than writing custom logic. @@ -60,8 +69,36 @@ This repo uses snapshot tests (via `insta`), especially in `codex-rs/tui`, to va - `cargo insta accept -p codex-tui` If you don’t have the tool: + - `cargo install cargo-insta` ### Test assertions - Tests should use pretty_assertions::assert_eq for clearer diffs. Import this at the top of the test module if it isn't already. + +### Integration tests (core) + +- Prefer the utilities in `core_test_support::responses` when writing end-to-end Codex tests. + +- All `mount_sse*` helpers return a `ResponseMock`; hold onto it so you can assert against outbound `/responses` POST bodies. +- Use `ResponseMock::single_request()` when a test should only issue one POST, or `ResponseMock::requests()` to inspect every captured `ResponsesRequest`. +- `ResponsesRequest` exposes helpers (`body_json`, `input`, `function_call_output`, `custom_tool_call_output`, `call_output`, `header`, `path`, `query_param`) so assertions can target structured payloads instead of manual JSON digging. +- Build SSE payloads with the provided `ev_*` constructors and the `sse(...)`. +- Prefer `wait_for_event` over `wait_for_event_with_timeout`. +- Prefer `mount_sse_once` over `mount_sse_once_match` or `mount_sse_sequence` + +- Typical pattern: + + ```rust + let mock = responses::mount_sse_once(&server, responses::sse(vec![ + responses::ev_response_created("resp-1"), + responses::ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), + responses::ev_completed("resp-1"), + ])).await; + + codex.submit(Op::UserTurn { ... }).await?; + + // Assert request body if needed. + let request = mock.single_request(); + // assert using request.function_call_output(call_id) or request.json_body() or other helpers. + ``` diff --git a/CHANGELOG.md b/CHANGELOG.md index ed8bb45518..2eb564c560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1 @@ -The changelog can be found on the [releases page](https://github.com/openai/codex/releases) +The changelog can be found on the [releases page](https://github.com/openai/codex/releases). diff --git a/README.md b/README.md index ab93ecad22..8141610035 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -

OpenAI Codex CLI

- -

npm i -g @openai/codex
or brew install codex

+

npm i -g @openai/codex
or brew install --cask codex

Codex CLI is a coding agent from OpenAI that runs locally on your computer.
@@ -26,7 +24,7 @@ npm install -g @openai/codex Alternatively, if you use Homebrew: ```shell -brew install codex +brew install --cask codex ``` Then simply run `codex` to get started: @@ -35,6 +33,8 @@ Then simply run `codex` to get started: codex ``` +If you're running into upgrade issues with Homebrew, see the [FAQ entry on brew upgrade codex](./docs/faq.md#brew-upgrade-codex-isnt-upgrading-me). +

You can also go to the latest GitHub Release and download the appropriate binary for your platform. @@ -63,8 +63,7 @@ You can also use Codex with an API key, but this requires [additional setup](./d ### Model Context Protocol (MCP) -Codex CLI supports [MCP servers](./docs/advanced.md#model-context-protocol-mcp). Enable by adding an `mcp_servers` section to your `~/.codex/config.toml`. - +Codex can access MCP servers. To configure them, refer to the [config docs](./docs/config.md#mcp_servers). ### Configuration @@ -76,16 +75,22 @@ Codex CLI supports a rich set of configuration options, with preferences stored - [**Getting started**](./docs/getting-started.md) - [CLI usage](./docs/getting-started.md#cli-usage) + - [Slash Commands](./docs/slash_commands.md) - [Running with a prompt as input](./docs/getting-started.md#running-with-a-prompt-as-input) - [Example prompts](./docs/getting-started.md#example-prompts) + - [Custom prompts](./docs/prompts.md) - [Memory with AGENTS.md](./docs/getting-started.md#memory-with-agentsmd) - - [Configuration](./docs/config.md) +- [**Configuration**](./docs/config.md) + - [Example config](./docs/example-config.md) - [**Sandbox & approvals**](./docs/sandbox.md) - [**Authentication**](./docs/authentication.md) - [Auth methods](./docs/authentication.md#forcing-a-specific-auth-method-advanced) - [Login on a "Headless" machine](./docs/authentication.md#connecting-on-a-headless-machine) +- **Automating Codex** + - [GitHub Action](https://github.com/openai/codex-action) + - [TypeScript SDK](./sdk/typescript/README.md) + - [Non-interactive mode (`codex exec`)](./docs/exec.md) - [**Advanced**](./docs/advanced.md) - - [Non-interactive / CI mode](./docs/advanced.md#non-interactive--ci-mode) - [Tracing / verbose logging](./docs/advanced.md#tracing--verbose-logging) - [Model Context Protocol (MCP)](./docs/advanced.md#model-context-protocol-mcp) - [**Zero data retention (ZDR)**](./docs/zdr.md) @@ -102,4 +107,3 @@ Codex CLI supports a rich set of configuration options, with preferences stored ## License This repository is licensed under the [Apache-2.0 License](LICENSE). - diff --git a/cliff.toml b/cliff.toml index 30aa1f1ac6..f31e1bd89c 100644 --- a/cliff.toml +++ b/cliff.toml @@ -4,7 +4,7 @@ header = """ # Changelog -You can install any of these versions: `npm install -g codex@version` +You can install any of these versions: `npm install -g @openai/codex@` """ body = """ diff --git a/codex-cli/.gitignore b/codex-cli/.gitignore index f886e64f46..57872d0f1e 100644 --- a/codex-cli/.gitignore +++ b/codex-cli/.gitignore @@ -1,7 +1 @@ -# Added by ./scripts/install_native_deps.sh -/bin/codex-aarch64-apple-darwin -/bin/codex-aarch64-unknown-linux-musl -/bin/codex-linux-sandbox-arm64 -/bin/codex-linux-sandbox-x64 -/bin/codex-x86_64-apple-darwin -/bin/codex-x86_64-unknown-linux-musl +/vendor/ diff --git a/codex-cli/README.md b/codex-cli/README.md index e988b384ab..f3414f1c4b 100644 --- a/codex-cli/README.md +++ b/codex-cli/README.md @@ -208,7 +208,7 @@ The hardening mechanism Codex uses depends on your OS: | Requirement | Details | | --------------------------- | --------------------------------------------------------------- | | Operating systems | macOS 12+, Ubuntu 20.04+/Debian 10+, or Windows 11 **via WSL2** | -| Node.js | **22 or newer** (LTS recommended) | +| Node.js | **16 or newer** (Node 20 LTS recommended) | | Git (optional, recommended) | 2.23+ for built-in PR helpers | | RAM | 4-GB minimum (8-GB recommended) | @@ -513,7 +513,7 @@ Codex runs model-generated commands in a sandbox. If a proposed command or file
Does it work on Windows? -Not directly. It requires [Windows Subsystem for Linux (WSL2)](https://learn.microsoft.com/en-us/windows/wsl/install) - Codex has been tested on macOS and Linux with Node 22. +Not directly. It requires [Windows Subsystem for Linux (WSL2)](https://learn.microsoft.com/en-us/windows/wsl/install) - Codex is regularly tested on macOS and Linux with Node 20+, and also supports Node 16.
diff --git a/codex-cli/bin/codex.js b/codex-cli/bin/codex.js old mode 100755 new mode 100644 index f24065170d..805be85af8 --- a/codex-cli/bin/codex.js +++ b/codex-cli/bin/codex.js @@ -1,6 +1,8 @@ #!/usr/bin/env node // Unified entry point for the Codex CLI. +import { spawn } from "node:child_process"; +import { existsSync } from "fs"; import path from "path"; import { fileURLToPath } from "url"; @@ -40,10 +42,10 @@ switch (platform) { case "win32": switch (arch) { case "x64": - targetTriple = "x86_64-pc-windows-msvc.exe"; + targetTriple = "x86_64-pc-windows-msvc"; break; case "arm64": - targetTriple = "aarch64-pc-windows-msvc.exe"; + targetTriple = "aarch64-pc-windows-msvc"; break; default: break; @@ -57,31 +59,16 @@ if (!targetTriple) { throw new Error(`Unsupported platform: ${platform} (${arch})`); } -const binaryPath = path.join(__dirname, "..", "bin", `codex-${targetTriple}`); +const vendorRoot = path.join(__dirname, "..", "vendor"); +const archRoot = path.join(vendorRoot, targetTriple); +const codexBinaryName = process.platform === "win32" ? "codex.exe" : "codex"; +const binaryPath = path.join(archRoot, "codex", codexBinaryName); // Use an asynchronous spawn instead of spawnSync so that Node is able to // respond to signals (e.g. Ctrl-C / SIGINT) while the native binary is // executing. This allows us to forward those signals to the child process // and guarantees that when either the child terminates or the parent // receives a fatal signal, both processes exit in a predictable manner. -const { spawn } = await import("child_process"); - -async function tryImport(moduleName) { - try { - // eslint-disable-next-line node/no-unsupported-features/es-syntax - return await import(moduleName); - } catch (err) { - return null; - } -} - -async function resolveRgDir() { - const ripgrep = await tryImport("@vscode/ripgrep"); - if (!ripgrep?.rgPath) { - return null; - } - return path.dirname(ripgrep.rgPath); -} function getUpdatedPath(newDirs) { const pathSep = process.platform === "win32" ? ";" : ":"; @@ -93,16 +80,49 @@ function getUpdatedPath(newDirs) { return updatedPath; } +/** + * Use heuristics to detect the package manager that was used to install Codex + * in order to give the user a hint about how to update it. + */ +function detectPackageManager() { + const userAgent = process.env.npm_config_user_agent || ""; + if (/\bbun\//.test(userAgent)) { + return "bun"; + } + + const execPath = process.env.npm_execpath || ""; + if (execPath.includes("bun")) { + return "bun"; + } + + if ( + process.env.BUN_INSTALL || + process.env.BUN_INSTALL_GLOBAL_DIR || + process.env.BUN_INSTALL_BIN_DIR + ) { + return "bun"; + } + + return userAgent ? "npm" : null; +} + const additionalDirs = []; -const rgDir = await resolveRgDir(); -if (rgDir) { - additionalDirs.push(rgDir); +const pathDir = path.join(archRoot, "path"); +if (existsSync(pathDir)) { + additionalDirs.push(pathDir); } const updatedPath = getUpdatedPath(additionalDirs); +const env = { ...process.env, PATH: updatedPath }; +const packageManagerEnvVar = + detectPackageManager() === "bun" + ? "CODEX_MANAGED_BY_BUN" + : "CODEX_MANAGED_BY_NPM"; +env[packageManagerEnvVar] = "1"; + const child = spawn(binaryPath, process.argv.slice(2), { stdio: "inherit", - env: { ...process.env, PATH: updatedPath, CODEX_MANAGED_BY_NPM: "1" }, + env, }); child.on("error", (err) => { diff --git a/codex-cli/bin/rg b/codex-cli/bin/rg new file mode 100755 index 0000000000..5a992570a9 --- /dev/null +++ b/codex-cli/bin/rg @@ -0,0 +1,79 @@ +#!/usr/bin/env dotslash + +{ + "name": "rg", + "platforms": { + "macos-aarch64": { + "size": 1787248, + "hash": "blake3", + "digest": "8d9942032585ea8ee805937634238d9aee7b210069f4703c88fbe568e26fb78a", + "format": "tar.gz", + "path": "ripgrep-14.1.1-aarch64-apple-darwin/rg", + "providers": [ + { + "url": "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-aarch64-apple-darwin.tar.gz" + } + ] + }, + "linux-aarch64": { + "size": 2047405, + "hash": "blake3", + "digest": "0b670b8fa0a3df2762af2fc82cc4932f684ca4c02dbd1260d4f3133fd4b2a515", + "format": "tar.gz", + "path": "ripgrep-14.1.1-aarch64-unknown-linux-gnu/rg", + "providers": [ + { + "url": "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-aarch64-unknown-linux-gnu.tar.gz" + } + ] + }, + "macos-x86_64": { + "size": 2082672, + "hash": "blake3", + "digest": "e9b862fc8da3127f92791f0ff6a799504154ca9d36c98bf3e60a81c6b1f7289e", + "format": "tar.gz", + "path": "ripgrep-14.1.1-x86_64-apple-darwin/rg", + "providers": [ + { + "url": "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-x86_64-apple-darwin.tar.gz" + } + ] + }, + "linux-x86_64": { + "size": 2566310, + "hash": "blake3", + "digest": "f73cca4e54d78c31f832c7f6e2c0b4db8b04fa3eaa747915727d570893dbee76", + "format": "tar.gz", + "path": "ripgrep-14.1.1-x86_64-unknown-linux-musl/rg", + "providers": [ + { + "url": "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-x86_64-unknown-linux-musl.tar.gz" + } + ] + }, + "windows-x86_64": { + "size": 2058893, + "hash": "blake3", + "digest": "a8ce1a6fed4f8093ee997e57f33254e94b2cd18e26358b09db599c89882eadbd", + "format": "zip", + "path": "ripgrep-14.1.1-x86_64-pc-windows-msvc/rg.exe", + "providers": [ + { + "url": "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-x86_64-pc-windows-msvc.zip" + } + ] + }, + "windows-aarch64": { + "size": 1667740, + "hash": "blake3", + "digest": "47b971a8c4fca1d23a4e7c19bd4d88465ebc395598458133139406d3bf85f3fa", + "format": "zip", + "path": "rg.exe", + "providers": [ + { + "url": "https://github.com/microsoft/ripgrep-prebuilt/releases/download/v13.0.0-13/ripgrep-v13.0.0-13-aarch64-pc-windows-msvc.zip" + } + ] + } + } +} diff --git a/codex-cli/package-lock.json b/codex-cli/package-lock.json index a1c840ade0..58ee846306 100644 --- a/codex-cli/package-lock.json +++ b/codex-cli/package-lock.json @@ -2,117 +2,16 @@ "name": "@openai/codex", "version": "0.0.0-dev", "lockfileVersion": 3, - "requires": true, "packages": { "": { "name": "@openai/codex", "version": "0.0.0-dev", "license": "Apache-2.0", - "dependencies": { - "@vscode/ripgrep": "^1.15.14" - }, "bin": { "codex": "bin/codex.js" }, "engines": { - "node": ">=20" - } - }, - "node_modules/@vscode/ripgrep": { - "version": "1.15.14", - "resolved": "https://registry.npmjs.org/@vscode/ripgrep/-/ripgrep-1.15.14.tgz", - "integrity": "sha512-/G1UJPYlm+trBWQ6cMO3sv6b8D1+G16WaJH1/DSqw32JOVlzgZbLkDxRyzIpTpv30AcYGMkCf5tUqGlW6HbDWw==", - "hasInstallScript": true, - "license": "MIT", - "dependencies": { - "https-proxy-agent": "^7.0.2", - "proxy-from-env": "^1.1.0", - "yauzl": "^2.9.2" - } - }, - "node_modules/agent-base": { - "version": "7.1.4", - "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz", - "integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==", - "license": "MIT", - "engines": { - "node": ">= 14" - } - }, - "node_modules/buffer-crc32": { - "version": "0.2.13", - "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", - "license": "MIT", - "engines": { - "node": "*" - } - }, - "node_modules/debug": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", - "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", - "license": "MIT", - "dependencies": { - "ms": "^2.1.3" - }, - "engines": { - "node": ">=6.0" - }, - "peerDependenciesMeta": { - "supports-color": { - "optional": true - } - } - }, - "node_modules/fd-slicer": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", - "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", - "license": "MIT", - "dependencies": { - "pend": "~1.2.0" - } - }, - "node_modules/https-proxy-agent": { - "version": "7.0.6", - "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", - "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", - "license": "MIT", - "dependencies": { - "agent-base": "^7.1.2", - "debug": "4" - }, - "engines": { - "node": ">= 14" - } - }, - "node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", - "license": "MIT" - }, - "node_modules/pend": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", - "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==", - "license": "MIT" - }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "license": "MIT" - }, - "node_modules/yauzl": { - "version": "2.10.0", - "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", - "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", - "license": "MIT", - "dependencies": { - "buffer-crc32": "~0.2.3", - "fd-slicer": "~1.1.0" + "node": ">=16" } } } diff --git a/codex-cli/package.json b/codex-cli/package.json index 02124f3257..b83309e42b 100644 --- a/codex-cli/package.json +++ b/codex-cli/package.json @@ -7,21 +7,15 @@ }, "type": "module", "engines": { - "node": ">=20" + "node": ">=16" }, "files": [ "bin", - "dist" + "vendor" ], "repository": { "type": "git", "url": "git+https://github.com/openai/codex.git", "directory": "codex-cli" - }, - "dependencies": { - "@vscode/ripgrep": "^1.15.14" - }, - "devDependencies": { - "prettier": "^3.3.3" } } diff --git a/codex-cli/scripts/README.md b/codex-cli/scripts/README.md index 21e4f3e883..052cf81a37 100644 --- a/codex-cli/scripts/README.md +++ b/codex-cli/scripts/README.md @@ -1,9 +1,19 @@ # npm releases -Run the following: - -To build the 0.2.x or later version of the npm module, which runs the Rust version of the CLI, build it as follows: +Use the staging helper in the repo root to generate npm tarballs for a release. For +example, to stage the CLI, responses proxy, and SDK packages for version `0.6.0`: ```bash -./codex-cli/scripts/stage_rust_release.py --release-version 0.6.0 +./scripts/stage_npm_packages.py \ + --release-version 0.6.0 \ + --package codex \ + --package codex-responses-api-proxy \ + --package codex-sdk ``` + +This downloads the native artifacts once, hydrates `vendor/` for each package, and writes +tarballs to `dist/npm/`. + +If you need to invoke `build_npm_package.py` directly, run +`codex-cli/scripts/install_native_deps.py` first and pass `--vendor-src` pointing to the +directory that contains the populated `vendor/` tree. diff --git a/codex-cli/scripts/build_npm_package.py b/codex-cli/scripts/build_npm_package.py new file mode 100755 index 0000000000..ef96bef2ee --- /dev/null +++ b/codex-cli/scripts/build_npm_package.py @@ -0,0 +1,308 @@ +#!/usr/bin/env python3 +"""Stage and optionally package the @openai/codex npm module.""" + +import argparse +import json +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path + +SCRIPT_DIR = Path(__file__).resolve().parent +CODEX_CLI_ROOT = SCRIPT_DIR.parent +REPO_ROOT = CODEX_CLI_ROOT.parent +RESPONSES_API_PROXY_NPM_ROOT = REPO_ROOT / "codex-rs" / "responses-api-proxy" / "npm" +CODEX_SDK_ROOT = REPO_ROOT / "sdk" / "typescript" + +PACKAGE_NATIVE_COMPONENTS: dict[str, list[str]] = { + "codex": ["codex", "rg"], + "codex-responses-api-proxy": ["codex-responses-api-proxy"], + "codex-sdk": ["codex"], +} +COMPONENT_DEST_DIR: dict[str, str] = { + "codex": "codex", + "codex-responses-api-proxy": "codex-responses-api-proxy", + "rg": "path", +} + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Build or stage the Codex CLI npm package.") + parser.add_argument( + "--package", + choices=("codex", "codex-responses-api-proxy", "codex-sdk"), + default="codex", + help="Which npm package to stage (default: codex).", + ) + parser.add_argument( + "--version", + help="Version number to write to package.json inside the staged package.", + ) + parser.add_argument( + "--release-version", + help=( + "Version to stage for npm release." + ), + ) + parser.add_argument( + "--staging-dir", + type=Path, + help=( + "Directory to stage the package contents. Defaults to a new temporary directory " + "if omitted. The directory must be empty when provided." + ), + ) + parser.add_argument( + "--tmp", + dest="staging_dir", + type=Path, + help=argparse.SUPPRESS, + ) + parser.add_argument( + "--pack-output", + type=Path, + help="Path where the generated npm tarball should be written.", + ) + parser.add_argument( + "--vendor-src", + type=Path, + help="Directory containing pre-installed native binaries to bundle (vendor root).", + ) + return parser.parse_args() + + +def main() -> int: + args = parse_args() + + package = args.package + version = args.version + release_version = args.release_version + if release_version: + if version and version != release_version: + raise RuntimeError("--version and --release-version must match when both are provided.") + version = release_version + + if not version: + raise RuntimeError("Must specify --version or --release-version.") + + staging_dir, created_temp = prepare_staging_dir(args.staging_dir) + + try: + stage_sources(staging_dir, version, package) + + vendor_src = args.vendor_src.resolve() if args.vendor_src else None + native_components = PACKAGE_NATIVE_COMPONENTS.get(package, []) + + if native_components: + if vendor_src is None: + components_str = ", ".join(native_components) + raise RuntimeError( + "Native components " + f"({components_str}) required for package '{package}'. Provide --vendor-src " + "pointing to a directory containing pre-installed binaries." + ) + + copy_native_binaries(vendor_src, staging_dir, native_components) + + if release_version: + staging_dir_str = str(staging_dir) + if package == "codex": + print( + f"Staged version {version} for release in {staging_dir_str}\n\n" + "Verify the CLI:\n" + f" node {staging_dir_str}/bin/codex.js --version\n" + f" node {staging_dir_str}/bin/codex.js --help\n\n" + ) + elif package == "codex-responses-api-proxy": + print( + f"Staged version {version} for release in {staging_dir_str}\n\n" + "Verify the responses API proxy:\n" + f" node {staging_dir_str}/bin/codex-responses-api-proxy.js --help\n\n" + ) + else: + print( + f"Staged version {version} for release in {staging_dir_str}\n\n" + "Verify the SDK contents:\n" + f" ls {staging_dir_str}/dist\n" + f" ls {staging_dir_str}/vendor\n" + " node -e \"import('./dist/index.js').then(() => console.log('ok'))\"\n\n" + ) + else: + print(f"Staged package in {staging_dir}") + + if args.pack_output is not None: + output_path = run_npm_pack(staging_dir, args.pack_output) + print(f"npm pack output written to {output_path}") + finally: + if created_temp: + # Preserve the staging directory for further inspection. + pass + + return 0 + + +def prepare_staging_dir(staging_dir: Path | None) -> tuple[Path, bool]: + if staging_dir is not None: + staging_dir = staging_dir.resolve() + staging_dir.mkdir(parents=True, exist_ok=True) + if any(staging_dir.iterdir()): + raise RuntimeError(f"Staging directory {staging_dir} is not empty.") + return staging_dir, False + + temp_dir = Path(tempfile.mkdtemp(prefix="codex-npm-stage-")) + return temp_dir, True + + +def stage_sources(staging_dir: Path, version: str, package: str) -> None: + if package == "codex": + bin_dir = staging_dir / "bin" + bin_dir.mkdir(parents=True, exist_ok=True) + shutil.copy2(CODEX_CLI_ROOT / "bin" / "codex.js", bin_dir / "codex.js") + rg_manifest = CODEX_CLI_ROOT / "bin" / "rg" + if rg_manifest.exists(): + shutil.copy2(rg_manifest, bin_dir / "rg") + + readme_src = REPO_ROOT / "README.md" + if readme_src.exists(): + shutil.copy2(readme_src, staging_dir / "README.md") + + package_json_path = CODEX_CLI_ROOT / "package.json" + elif package == "codex-responses-api-proxy": + bin_dir = staging_dir / "bin" + bin_dir.mkdir(parents=True, exist_ok=True) + launcher_src = RESPONSES_API_PROXY_NPM_ROOT / "bin" / "codex-responses-api-proxy.js" + shutil.copy2(launcher_src, bin_dir / "codex-responses-api-proxy.js") + + readme_src = RESPONSES_API_PROXY_NPM_ROOT / "README.md" + if readme_src.exists(): + shutil.copy2(readme_src, staging_dir / "README.md") + + package_json_path = RESPONSES_API_PROXY_NPM_ROOT / "package.json" + elif package == "codex-sdk": + package_json_path = CODEX_SDK_ROOT / "package.json" + stage_codex_sdk_sources(staging_dir) + else: + raise RuntimeError(f"Unknown package '{package}'.") + + with open(package_json_path, "r", encoding="utf-8") as fh: + package_json = json.load(fh) + package_json["version"] = version + + if package == "codex-sdk": + scripts = package_json.get("scripts") + if isinstance(scripts, dict): + scripts.pop("prepare", None) + + files = package_json.get("files") + if isinstance(files, list): + if "vendor" not in files: + files.append("vendor") + else: + package_json["files"] = ["dist", "vendor"] + + with open(staging_dir / "package.json", "w", encoding="utf-8") as out: + json.dump(package_json, out, indent=2) + out.write("\n") + + +def run_command(cmd: list[str], cwd: Path | None = None) -> None: + print("+", " ".join(cmd)) + subprocess.run(cmd, cwd=cwd, check=True) + + +def stage_codex_sdk_sources(staging_dir: Path) -> None: + package_root = CODEX_SDK_ROOT + + run_command(["pnpm", "install", "--frozen-lockfile"], cwd=package_root) + run_command(["pnpm", "run", "build"], cwd=package_root) + + dist_src = package_root / "dist" + if not dist_src.exists(): + raise RuntimeError("codex-sdk build did not produce a dist directory.") + + shutil.copytree(dist_src, staging_dir / "dist") + + readme_src = package_root / "README.md" + if readme_src.exists(): + shutil.copy2(readme_src, staging_dir / "README.md") + + license_src = REPO_ROOT / "LICENSE" + if license_src.exists(): + shutil.copy2(license_src, staging_dir / "LICENSE") + + +def copy_native_binaries(vendor_src: Path, staging_dir: Path, components: list[str]) -> None: + vendor_src = vendor_src.resolve() + if not vendor_src.exists(): + raise RuntimeError(f"Vendor source directory not found: {vendor_src}") + + components_set = {component for component in components if component in COMPONENT_DEST_DIR} + if not components_set: + return + + vendor_dest = staging_dir / "vendor" + if vendor_dest.exists(): + shutil.rmtree(vendor_dest) + vendor_dest.mkdir(parents=True, exist_ok=True) + + for target_dir in vendor_src.iterdir(): + if not target_dir.is_dir(): + continue + + dest_target_dir = vendor_dest / target_dir.name + dest_target_dir.mkdir(parents=True, exist_ok=True) + + for component in components_set: + dest_dir_name = COMPONENT_DEST_DIR.get(component) + if dest_dir_name is None: + continue + + src_component_dir = target_dir / dest_dir_name + if not src_component_dir.exists(): + raise RuntimeError( + f"Missing native component '{component}' in vendor source: {src_component_dir}" + ) + + dest_component_dir = dest_target_dir / dest_dir_name + if dest_component_dir.exists(): + shutil.rmtree(dest_component_dir) + shutil.copytree(src_component_dir, dest_component_dir) + + +def run_npm_pack(staging_dir: Path, output_path: Path) -> Path: + output_path = output_path.resolve() + output_path.parent.mkdir(parents=True, exist_ok=True) + + with tempfile.TemporaryDirectory(prefix="codex-npm-pack-") as pack_dir_str: + pack_dir = Path(pack_dir_str) + stdout = subprocess.check_output( + ["npm", "pack", "--json", "--pack-destination", str(pack_dir)], + cwd=staging_dir, + text=True, + ) + try: + pack_output = json.loads(stdout) + except json.JSONDecodeError as exc: + raise RuntimeError("Failed to parse npm pack output.") from exc + + if not pack_output: + raise RuntimeError("npm pack did not produce an output tarball.") + + tarball_name = pack_output[0].get("filename") or pack_output[0].get("name") + if not tarball_name: + raise RuntimeError("Unable to determine npm pack output filename.") + + tarball_path = pack_dir / tarball_name + if not tarball_path.exists(): + raise RuntimeError(f"Expected npm pack output not found: {tarball_path}") + + shutil.move(str(tarball_path), output_path) + + return output_path + + +if __name__ == "__main__": + import sys + + sys.exit(main()) diff --git a/codex-cli/scripts/install_native_deps.py b/codex-cli/scripts/install_native_deps.py new file mode 100755 index 0000000000..8d3909c9e1 --- /dev/null +++ b/codex-cli/scripts/install_native_deps.py @@ -0,0 +1,383 @@ +#!/usr/bin/env python3 +"""Install Codex native binaries (Rust CLI plus ripgrep helpers).""" + +import argparse +import json +import os +import shutil +import subprocess +import tarfile +import tempfile +import zipfile +from dataclasses import dataclass +from concurrent.futures import ThreadPoolExecutor, as_completed +from pathlib import Path +from typing import Iterable, Sequence +from urllib.parse import urlparse +from urllib.request import urlopen + +SCRIPT_DIR = Path(__file__).resolve().parent +CODEX_CLI_ROOT = SCRIPT_DIR.parent +DEFAULT_WORKFLOW_URL = "https://github.com/openai/codex/actions/runs/17952349351" # rust-v0.40.0 +VENDOR_DIR_NAME = "vendor" +RG_MANIFEST = CODEX_CLI_ROOT / "bin" / "rg" +BINARY_TARGETS = ( + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl", + "x86_64-apple-darwin", + "aarch64-apple-darwin", + "x86_64-pc-windows-msvc", + "aarch64-pc-windows-msvc", +) + + +@dataclass(frozen=True) +class BinaryComponent: + artifact_prefix: str # matches the artifact filename prefix (e.g. codex-.zst) + dest_dir: str # directory under vendor// where the binary is installed + binary_basename: str # executable name inside dest_dir (before optional .exe) + + +BINARY_COMPONENTS = { + "codex": BinaryComponent( + artifact_prefix="codex", + dest_dir="codex", + binary_basename="codex", + ), + "codex-responses-api-proxy": BinaryComponent( + artifact_prefix="codex-responses-api-proxy", + dest_dir="codex-responses-api-proxy", + binary_basename="codex-responses-api-proxy", + ), +} + +RG_TARGET_PLATFORM_PAIRS: list[tuple[str, str]] = [ + ("x86_64-unknown-linux-musl", "linux-x86_64"), + ("aarch64-unknown-linux-musl", "linux-aarch64"), + ("x86_64-apple-darwin", "macos-x86_64"), + ("aarch64-apple-darwin", "macos-aarch64"), + ("x86_64-pc-windows-msvc", "windows-x86_64"), + ("aarch64-pc-windows-msvc", "windows-aarch64"), +] +RG_TARGET_TO_PLATFORM = {target: platform for target, platform in RG_TARGET_PLATFORM_PAIRS} +DEFAULT_RG_TARGETS = [target for target, _ in RG_TARGET_PLATFORM_PAIRS] + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Install native Codex binaries.") + parser.add_argument( + "--workflow-url", + help=( + "GitHub Actions workflow URL that produced the artifacts. Defaults to a " + "known good run when omitted." + ), + ) + parser.add_argument( + "--component", + dest="components", + action="append", + choices=tuple(list(BINARY_COMPONENTS) + ["rg"]), + help=( + "Limit installation to the specified components." + " May be repeated. Defaults to 'codex' and 'rg'." + ), + ) + parser.add_argument( + "root", + nargs="?", + type=Path, + help=( + "Directory containing package.json for the staged package. If omitted, the " + "repository checkout is used." + ), + ) + return parser.parse_args() + + +def main() -> int: + args = parse_args() + + codex_cli_root = (args.root or CODEX_CLI_ROOT).resolve() + vendor_dir = codex_cli_root / VENDOR_DIR_NAME + vendor_dir.mkdir(parents=True, exist_ok=True) + + components = args.components or ["codex", "rg"] + + workflow_url = (args.workflow_url or DEFAULT_WORKFLOW_URL).strip() + if not workflow_url: + workflow_url = DEFAULT_WORKFLOW_URL + + workflow_id = workflow_url.rstrip("/").split("/")[-1] + print(f"Downloading native artifacts from workflow {workflow_id}...") + + with tempfile.TemporaryDirectory(prefix="codex-native-artifacts-") as artifacts_dir_str: + artifacts_dir = Path(artifacts_dir_str) + _download_artifacts(workflow_id, artifacts_dir) + install_binary_components( + artifacts_dir, + vendor_dir, + BINARY_TARGETS, + [name for name in components if name in BINARY_COMPONENTS], + ) + + if "rg" in components: + print("Fetching ripgrep binaries...") + fetch_rg(vendor_dir, DEFAULT_RG_TARGETS, manifest_path=RG_MANIFEST) + + print(f"Installed native dependencies into {vendor_dir}") + return 0 + + +def fetch_rg( + vendor_dir: Path, + targets: Sequence[str] | None = None, + *, + manifest_path: Path, +) -> list[Path]: + """Download ripgrep binaries described by the DotSlash manifest.""" + + if targets is None: + targets = DEFAULT_RG_TARGETS + + if not manifest_path.exists(): + raise FileNotFoundError(f"DotSlash manifest not found: {manifest_path}") + + manifest = _load_manifest(manifest_path) + platforms = manifest.get("platforms", {}) + + vendor_dir.mkdir(parents=True, exist_ok=True) + + targets = list(targets) + if not targets: + return [] + + task_configs: list[tuple[str, str, dict]] = [] + for target in targets: + platform_key = RG_TARGET_TO_PLATFORM.get(target) + if platform_key is None: + raise ValueError(f"Unsupported ripgrep target '{target}'.") + + platform_info = platforms.get(platform_key) + if platform_info is None: + raise RuntimeError(f"Platform '{platform_key}' not found in manifest {manifest_path}.") + + task_configs.append((target, platform_key, platform_info)) + + results: dict[str, Path] = {} + max_workers = min(len(task_configs), max(1, (os.cpu_count() or 1))) + + print("Installing ripgrep binaries for targets: " + ", ".join(targets)) + + with ThreadPoolExecutor(max_workers=max_workers) as executor: + future_map = { + executor.submit( + _fetch_single_rg, + vendor_dir, + target, + platform_key, + platform_info, + manifest_path, + ): target + for target, platform_key, platform_info in task_configs + } + + for future in as_completed(future_map): + target = future_map[future] + results[target] = future.result() + print(f" installed ripgrep for {target}") + + return [results[target] for target in targets] + + +def _download_artifacts(workflow_id: str, dest_dir: Path) -> None: + cmd = [ + "gh", + "run", + "download", + "--dir", + str(dest_dir), + "--repo", + "openai/codex", + workflow_id, + ] + subprocess.check_call(cmd) + + +def install_binary_components( + artifacts_dir: Path, + vendor_dir: Path, + targets: Iterable[str], + component_names: Sequence[str], +) -> None: + selected_components = [BINARY_COMPONENTS[name] for name in component_names if name in BINARY_COMPONENTS] + if not selected_components: + return + + targets = list(targets) + if not targets: + return + + for component in selected_components: + print( + f"Installing {component.binary_basename} binaries for targets: " + + ", ".join(targets) + ) + max_workers = min(len(targets), max(1, (os.cpu_count() or 1))) + with ThreadPoolExecutor(max_workers=max_workers) as executor: + futures = { + executor.submit( + _install_single_binary, + artifacts_dir, + vendor_dir, + target, + component, + ): target + for target in targets + } + for future in as_completed(futures): + installed_path = future.result() + print(f" installed {installed_path}") + + +def _install_single_binary( + artifacts_dir: Path, + vendor_dir: Path, + target: str, + component: BinaryComponent, +) -> Path: + artifact_subdir = artifacts_dir / target + archive_name = _archive_name_for_target(component.artifact_prefix, target) + archive_path = artifact_subdir / archive_name + if not archive_path.exists(): + raise FileNotFoundError(f"Expected artifact not found: {archive_path}") + + dest_dir = vendor_dir / target / component.dest_dir + dest_dir.mkdir(parents=True, exist_ok=True) + + binary_name = ( + f"{component.binary_basename}.exe" if "windows" in target else component.binary_basename + ) + dest = dest_dir / binary_name + dest.unlink(missing_ok=True) + extract_archive(archive_path, "zst", None, dest) + if "windows" not in target: + dest.chmod(0o755) + return dest + + +def _archive_name_for_target(artifact_prefix: str, target: str) -> str: + if "windows" in target: + return f"{artifact_prefix}-{target}.exe.zst" + return f"{artifact_prefix}-{target}.zst" + + +def _fetch_single_rg( + vendor_dir: Path, + target: str, + platform_key: str, + platform_info: dict, + manifest_path: Path, +) -> Path: + providers = platform_info.get("providers", []) + if not providers: + raise RuntimeError(f"No providers listed for platform '{platform_key}' in {manifest_path}.") + + url = providers[0]["url"] + archive_format = platform_info.get("format", "zst") + archive_member = platform_info.get("path") + + dest_dir = vendor_dir / target / "path" + dest_dir.mkdir(parents=True, exist_ok=True) + + is_windows = platform_key.startswith("win") + binary_name = "rg.exe" if is_windows else "rg" + dest = dest_dir / binary_name + + with tempfile.TemporaryDirectory() as tmp_dir_str: + tmp_dir = Path(tmp_dir_str) + archive_filename = os.path.basename(urlparse(url).path) + download_path = tmp_dir / archive_filename + _download_file(url, download_path) + + dest.unlink(missing_ok=True) + extract_archive(download_path, archive_format, archive_member, dest) + + if not is_windows: + dest.chmod(0o755) + + return dest + + +def _download_file(url: str, dest: Path) -> None: + dest.parent.mkdir(parents=True, exist_ok=True) + with urlopen(url) as response, open(dest, "wb") as out: + shutil.copyfileobj(response, out) + + +def extract_archive( + archive_path: Path, + archive_format: str, + archive_member: str | None, + dest: Path, +) -> None: + dest.parent.mkdir(parents=True, exist_ok=True) + + if archive_format == "zst": + output_path = archive_path.parent / dest.name + subprocess.check_call( + ["zstd", "-f", "-d", str(archive_path), "-o", str(output_path)] + ) + shutil.move(str(output_path), dest) + return + + if archive_format == "tar.gz": + if not archive_member: + raise RuntimeError("Missing 'path' for tar.gz archive in DotSlash manifest.") + with tarfile.open(archive_path, "r:gz") as tar: + try: + member = tar.getmember(archive_member) + except KeyError as exc: + raise RuntimeError( + f"Entry '{archive_member}' not found in archive {archive_path}." + ) from exc + tar.extract(member, path=archive_path.parent, filter="data") + extracted = archive_path.parent / archive_member + shutil.move(str(extracted), dest) + return + + if archive_format == "zip": + if not archive_member: + raise RuntimeError("Missing 'path' for zip archive in DotSlash manifest.") + with zipfile.ZipFile(archive_path) as archive: + try: + with archive.open(archive_member) as src, open(dest, "wb") as out: + shutil.copyfileobj(src, out) + except KeyError as exc: + raise RuntimeError( + f"Entry '{archive_member}' not found in archive {archive_path}." + ) from exc + return + + raise RuntimeError(f"Unsupported archive format '{archive_format}'.") + + +def _load_manifest(manifest_path: Path) -> dict: + cmd = ["dotslash", "--", "parse", str(manifest_path)] + stdout = subprocess.check_output(cmd, text=True) + try: + manifest = json.loads(stdout) + except json.JSONDecodeError as exc: + raise RuntimeError(f"Invalid DotSlash manifest output from {manifest_path}.") from exc + + if not isinstance(manifest, dict): + raise RuntimeError( + f"Unexpected DotSlash manifest structure for {manifest_path}: {type(manifest)!r}" + ) + + return manifest + + +if __name__ == "__main__": + import sys + + sys.exit(main()) diff --git a/codex-cli/scripts/install_native_deps.sh b/codex-cli/scripts/install_native_deps.sh deleted file mode 100755 index b9fda2543e..0000000000 --- a/codex-cli/scripts/install_native_deps.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env bash - -# Install native runtime dependencies for codex-cli. -# -# Usage -# install_native_deps.sh [--workflow-url URL] [CODEX_CLI_ROOT] -# -# The optional RELEASE_ROOT is the path that contains package.json. Omitting -# it installs the binaries into the repository's own bin/ folder to support -# local development. - -set -euo pipefail - -# ------------------ -# Parse arguments -# ------------------ - -CODEX_CLI_ROOT="" - -# Until we start publishing stable GitHub releases, we have to grab the binaries -# from the GitHub Action that created them. Update the URL below to point to the -# appropriate workflow run: -WORKFLOW_URL="https://github.com/openai/codex/actions/runs/17417194663" # rust-v0.28.0 - -while [[ $# -gt 0 ]]; do - case "$1" in - --workflow-url) - shift || { echo "--workflow-url requires an argument"; exit 1; } - if [ -n "$1" ]; then - WORKFLOW_URL="$1" - fi - ;; - *) - if [[ -z "$CODEX_CLI_ROOT" ]]; then - CODEX_CLI_ROOT="$1" - else - echo "Unexpected argument: $1" >&2 - exit 1 - fi - ;; - esac - shift -done - -# ---------------------------------------------------------------------------- -# Determine where the binaries should be installed. -# ---------------------------------------------------------------------------- - -if [ -n "$CODEX_CLI_ROOT" ]; then - # The caller supplied a release root directory. - BIN_DIR="$CODEX_CLI_ROOT/bin" -else - # No argument; fall back to the repo’s own bin directory. - # Resolve the path of this script, then walk up to the repo root. - SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - CODEX_CLI_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" - BIN_DIR="$CODEX_CLI_ROOT/bin" -fi - -# Make sure the destination directory exists. -mkdir -p "$BIN_DIR" - -# ---------------------------------------------------------------------------- -# Download and decompress the artifacts from the GitHub Actions workflow. -# ---------------------------------------------------------------------------- - -WORKFLOW_ID="${WORKFLOW_URL##*/}" - -ARTIFACTS_DIR="$(mktemp -d)" -trap 'rm -rf "$ARTIFACTS_DIR"' EXIT - -# NB: The GitHub CLI `gh` must be installed and authenticated. -gh run download --dir "$ARTIFACTS_DIR" --repo openai/codex "$WORKFLOW_ID" - -# x64 Linux -zstd -d "$ARTIFACTS_DIR/x86_64-unknown-linux-musl/codex-x86_64-unknown-linux-musl.zst" \ - -o "$BIN_DIR/codex-x86_64-unknown-linux-musl" -# ARM64 Linux -zstd -d "$ARTIFACTS_DIR/aarch64-unknown-linux-musl/codex-aarch64-unknown-linux-musl.zst" \ - -o "$BIN_DIR/codex-aarch64-unknown-linux-musl" -# x64 macOS -zstd -d "$ARTIFACTS_DIR/x86_64-apple-darwin/codex-x86_64-apple-darwin.zst" \ - -o "$BIN_DIR/codex-x86_64-apple-darwin" -# ARM64 macOS -zstd -d "$ARTIFACTS_DIR/aarch64-apple-darwin/codex-aarch64-apple-darwin.zst" \ - -o "$BIN_DIR/codex-aarch64-apple-darwin" -# x64 Windows -zstd -d "$ARTIFACTS_DIR/x86_64-pc-windows-msvc/codex-x86_64-pc-windows-msvc.exe.zst" \ - -o "$BIN_DIR/codex-x86_64-pc-windows-msvc.exe" -# ARM64 Windows -zstd -d "$ARTIFACTS_DIR/aarch64-pc-windows-msvc/codex-aarch64-pc-windows-msvc.exe.zst" \ - -o "$BIN_DIR/codex-aarch64-pc-windows-msvc.exe" - -echo "Installed native dependencies into $BIN_DIR" diff --git a/codex-cli/scripts/stage_release.sh b/codex-cli/scripts/stage_release.sh deleted file mode 100755 index 96236fc53c..0000000000 --- a/codex-cli/scripts/stage_release.sh +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env bash -# ----------------------------------------------------------------------------- -# stage_release.sh -# ----------------------------------------------------------------------------- -# Stages an npm release for @openai/codex. -# -# Usage: -# -# --tmp : Use instead of a freshly created temp directory. -# -h|--help : Print usage. -# -# ----------------------------------------------------------------------------- - -set -euo pipefail - -# Helper - usage / flag parsing - -usage() { - cat <&2 - usage 1 - ;; - *) - echo "Unexpected extra argument: $1" >&2 - usage 1 - ;; - esac - shift -done - -# Fallback when the caller did not specify a directory. -# If no directory was specified create a fresh temporary one. -if [[ -z "$TMPDIR" ]]; then - TMPDIR="$(mktemp -d)" -fi - -# Ensure the directory exists, then resolve to an absolute path. -mkdir -p "$TMPDIR" -TMPDIR="$(cd "$TMPDIR" && pwd)" - -# Main build logic - -echo "Staging release in $TMPDIR" - -# The script lives in codex-cli/scripts/ - change into codex-cli root so that -# relative paths keep working. -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -CODEX_CLI_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" - -pushd "$CODEX_CLI_ROOT" >/dev/null - -# 1. Build the JS artifacts --------------------------------------------------- - -# Paths inside the staged package -mkdir -p "$TMPDIR/bin" - -cp -r bin/codex.js "$TMPDIR/bin/codex.js" -cp ../README.md "$TMPDIR" || true # README is one level up - ignore if missing - -# Modify package.json - bump version and optionally add the native directory to -# the files array so that the binaries are published to npm. - -jq --arg version "$VERSION" \ - '.version = $version' \ - package.json > "$TMPDIR/package.json" - -# 2. Native runtime deps (sandbox plus optional Rust binaries) - -./scripts/install_native_deps.sh --workflow-url "$WORKFLOW_URL" "$TMPDIR" - -popd >/dev/null - -echo "Staged version $VERSION for release in $TMPDIR" - -echo "Verify the CLI:" -echo " node ${TMPDIR}/bin/codex.js --version" -echo " node ${TMPDIR}/bin/codex.js --help" - -# Print final hint for convenience -echo "Next: cd \"$TMPDIR\" && npm publish" diff --git a/codex-cli/scripts/stage_rust_release.py b/codex-cli/scripts/stage_rust_release.py deleted file mode 100755 index 9a554b77d0..0000000000 --- a/codex-cli/scripts/stage_rust_release.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 - -import json -import subprocess -import sys -import argparse -from pathlib import Path - - -def main() -> int: - parser = argparse.ArgumentParser( - description="""Stage a release for the npm module. - -Run this after the GitHub Release has been created and use -`--release-version` to specify the version to release. - -Optionally pass `--tmp` to control the temporary staging directory that will be -forwarded to stage_release.sh. -""" - ) - parser.add_argument( - "--release-version", required=True, help="Version to release, e.g., 0.3.0" - ) - parser.add_argument( - "--tmp", - help="Optional path to stage the npm package; forwarded to stage_release.sh", - ) - args = parser.parse_args() - version = args.release_version - - gh_run = subprocess.run( - [ - "gh", - "run", - "list", - "--branch", - f"rust-v{version}", - "--json", - "workflowName,url,headSha", - "--jq", - 'first(.[] | select(.workflowName == "rust-release"))', - ], - stdout=subprocess.PIPE, - check=True, - ) - gh_run.check_returncode() - workflow = json.loads(gh_run.stdout) - sha = workflow["headSha"] - - print(f"should `git checkout {sha}`") - - current_dir = Path(__file__).parent.resolve() - cmd = [ - str(current_dir / "stage_release.sh"), - "--version", - version, - "--workflow-url", - workflow["url"], - ] - if args.tmp: - cmd.extend(["--tmp", args.tmp]) - - stage_release = subprocess.run(cmd) - stage_release.check_returncode() - - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/codex-rs/.cargo/config.toml b/codex-rs/.cargo/config.toml new file mode 100644 index 0000000000..f5107cf73e --- /dev/null +++ b/codex-rs/.cargo/config.toml @@ -0,0 +1,5 @@ +[target.'cfg(all(windows, target_env = "msvc"))'] +rustflags = ["-C", "link-arg=/STACK:8388608"] + +[target.'cfg(all(windows, target_env = "gnu"))'] +rustflags = ["-C", "link-arg=-Wl,--stack,8388608"] diff --git a/codex-rs/.config/nextest.toml b/codex-rs/.config/nextest.toml new file mode 100644 index 0000000000..3ca7cfe503 --- /dev/null +++ b/codex-rs/.config/nextest.toml @@ -0,0 +1,9 @@ +[profile.default] +# Do not increase, fix your test instead +slow-timeout = { period = "15s", terminate-after = 2 } + + +[[profile.default.overrides]] +# Do not add new tests here +filter = 'test(rmcp_client) | test(humanlike_typing_1000_chars_appears_live_no_placeholder)' +slow-timeout = { period = "1m", terminate-after = 4 } diff --git a/codex-rs/.gitignore b/codex-rs/.gitignore index e996253768..e31566047d 100644 --- a/codex-rs/.gitignore +++ b/codex-rs/.gitignore @@ -1,4 +1,5 @@ /target/ +/target-*/ # Recommended value of CARGO_TARGET_DIR when using Docker as explained in .devcontainer/README.md. /target-amd64/ diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 73c1117af1..0ed45ddb23 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -27,6 +27,17 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + [[package]] name = "ahash" version = "0.8.12" @@ -56,7 +67,7 @@ checksum = "8fac2ce611db8b8cee9b2aa886ca03c924e9da5e5295d0dbd0526e5d0b0710f7" dependencies = [ "allocative_derive", "bumpalo", - "ctor", + "ctor 0.1.26", "hashbrown 0.14.5", "num-bigint", ] @@ -78,12 +89,6 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - [[package]] name = "android_system_properties" version = "0.1.5" @@ -167,9 +172,27 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.99" +version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" + +[[package]] +name = "app_test_support" +version = "0.0.0" +dependencies = [ + "anyhow", + "assert_cmd", + "base64", + "chrono", + "codex-app-server-protocol", + "codex-core", + "codex-protocol", + "serde", + "serde_json", + "tokio", + "uuid", + "wiremock", +] [[package]] name = "arboard" @@ -188,6 +211,7 @@ dependencies = [ "parking_lot", "percent-encoding", "windows-sys 0.59.0", + "wl-clipboard-rs", "x11rb", ] @@ -214,46 +238,44 @@ dependencies = [ [[package]] name = "askama" -version = "0.12.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28" +checksum = "f75363874b771be265f4ffe307ca705ef6f3baa19011c149da8674a87f1b75c4" dependencies = [ "askama_derive", - "askama_escape", - "humansize", - "num-traits", + "itoa", "percent-encoding", + "serde", + "serde_json", ] [[package]] name = "askama_derive" -version = "0.12.5" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83" +checksum = "129397200fe83088e8a68407a8e2b1f826cf0086b21ccdb866a722c8bcd3a94f" dependencies = [ "askama_parser", "basic-toml", - "mime", - "mime_guess", + "memchr", "proc-macro2", "quote", + "rustc-hash 2.1.1", "serde", + "serde_derive", "syn 2.0.104", ] -[[package]] -name = "askama_escape" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" - [[package]] name = "askama_parser" -version = "0.2.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0" +checksum = "d6ab5630b3d5eaf232620167977f95eb51f3432fc76852328774afbd242d4358" dependencies = [ - "nom", + "memchr", + "serde", + "serde_derive", + "winnow", ] [[package]] @@ -282,6 +304,24 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "assert_matches" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" + +[[package]] +name = "async-broadcast" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435a87a52755b8f27fcf321ac4f04b2802e337c8c4872923137471ec39c37532" +dependencies = [ + "event-listener", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + [[package]] name = "async-channel" version = "2.5.0" @@ -294,6 +334,107 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "async-executor" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497c00e0fd83a72a79a39fcbd8e3e2f055d6f6c7e025f3b3d91f4f8e76527fb8" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "pin-project-lite", + "slab", +] + +[[package]] +name = "async-fs" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5" +dependencies = [ + "async-lock", + "blocking", + "futures-lite", +] + +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix 1.0.8", + "slab", + "windows-sys 0.61.1", +] + +[[package]] +name = "async-lock" +version = "3.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd03604047cee9b6ce9de9f70c6cd540a0520c813cbd49bae61f33ab80ed1dc" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-process" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75" +dependencies = [ + "async-channel", + "async-io", + "async-lock", + "async-signal", + "async-task", + "blocking", + "cfg-if", + "event-listener", + "futures-lite", + "rustix 1.0.8", +] + +[[package]] +name = "async-recursion" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + +[[package]] +name = "async-signal" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43c070bbf59cd3570b6b2dd54cd772527c7c3620fce8be898406dd3ed6adc64c" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "futures-core", + "futures-io", + "rustix 1.0.8", + "signal-hook-registry", + "slab", + "windows-sys 0.61.1", +] + [[package]] name = "async-stream" version = "0.3.6" @@ -316,6 +457,23 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -328,6 +486,54 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "axum" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5" +dependencies = [ + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.75" @@ -387,9 +593,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.9.1" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "block-buffer" @@ -400,6 +606,28 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blocking" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + [[package]] name = "bstr" version = "1.12.0" @@ -456,6 +684,15 @@ dependencies = [ "rustversion", ] +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + [[package]] name = "cc" version = "1.2.30" @@ -484,18 +721,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" [[package]] -name = "chrono" -version = "0.4.41" +name = "cfg_aliases" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" dependencies = [ - "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "serde", "wasm-bindgen", - "windows-link", + "windows-link 0.2.0", ] [[package]] @@ -504,6 +746,16 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clap" version = "4.5.47" @@ -578,17 +830,83 @@ dependencies = [ "tracing", ] +[[package]] +name = "codex-app-server" +version = "0.0.0" +dependencies = [ + "anyhow", + "app_test_support", + "assert_cmd", + "base64", + "chrono", + "codex-app-server-protocol", + "codex-arg0", + "codex-backend-client", + "codex-common", + "codex-core", + "codex-feedback", + "codex-file-search", + "codex-login", + "codex-protocol", + "codex-utils-json-to-toml", + "core_test_support", + "mcp-types", + "opentelemetry-appender-tracing", + "os_info", + "pretty_assertions", + "serde", + "serde_json", + "serial_test", + "tempfile", + "tokio", + "toml", + "tracing", + "tracing-subscriber", + "uuid", + "wiremock", +] + +[[package]] +name = "codex-app-server-protocol" +version = "0.0.0" +dependencies = [ + "anyhow", + "clap", + "codex-protocol", + "mcp-types", + "pretty_assertions", + "schemars 0.8.22", + "serde", + "serde_json", + "strum_macros 0.27.2", + "ts-rs", + "uuid", +] + +[[package]] +name = "codex-app-server-test-client" +version = "0.0.0" +dependencies = [ + "anyhow", + "clap", + "codex-app-server-protocol", + "codex-protocol", + "serde", + "serde_json", + "uuid", +] + [[package]] name = "codex-apply-patch" version = "0.0.0" dependencies = [ "anyhow", "assert_cmd", - "once_cell", + "assert_matches", "pretty_assertions", "similar", "tempfile", - "thiserror 2.0.16", + "thiserror 2.0.17", "tree-sitter", "tree-sitter-bash", ] @@ -606,6 +924,39 @@ dependencies = [ "tokio", ] +[[package]] +name = "codex-async-utils" +version = "0.0.0" +dependencies = [ + "async-trait", + "pretty_assertions", + "tokio", + "tokio-util", +] + +[[package]] +name = "codex-backend-client" +version = "0.0.0" +dependencies = [ + "anyhow", + "codex-backend-openapi-models", + "codex-core", + "codex-protocol", + "pretty_assertions", + "reqwest", + "serde", + "serde_json", +] + +[[package]] +name = "codex-backend-openapi-models" +version = "0.0.0" +dependencies = [ + "serde", + "serde_json", + "serde_with", +] + [[package]] name = "codex-chatgpt" version = "0.0.0" @@ -614,6 +965,7 @@ dependencies = [ "clap", "codex-common", "codex-core", + "codex-git", "serde", "serde_json", "tempfile", @@ -626,25 +978,79 @@ version = "0.0.0" dependencies = [ "anyhow", "assert_cmd", + "assert_matches", "clap", "clap_complete", + "codex-app-server", + "codex-app-server-protocol", "codex-arg0", "codex-chatgpt", + "codex-cloud-tasks", "codex-common", "codex-core", "codex-exec", "codex-login", "codex-mcp-server", + "codex-process-hardening", "codex-protocol", - "codex-protocol-ts", + "codex-responses-api-proxy", + "codex-rmcp-client", + "codex-stdio-to-uds", "codex-tui", + "codex-windows-sandbox", + "ctor 0.5.0", + "libc", + "owo-colors", "predicates", "pretty_assertions", + "regex-lite", "serde_json", + "supports-color", "tempfile", "tokio", + "toml", + "tracing", +] + +[[package]] +name = "codex-cloud-tasks" +version = "0.0.0" +dependencies = [ + "anyhow", + "async-trait", + "base64", + "chrono", + "clap", + "codex-cloud-tasks-client", + "codex-common", + "codex-core", + "codex-login", + "codex-tui", + "crossterm", + "ratatui", + "reqwest", + "serde", + "serde_json", + "tokio", + "tokio-stream", "tracing", "tracing-subscriber", + "unicode-width 0.2.1", +] + +[[package]] +name = "codex-cloud-tasks-client" +version = "0.0.0" +dependencies = [ + "anyhow", + "async-trait", + "chrono", + "codex-backend-client", + "codex-git", + "diffy", + "serde", + "serde_json", + "thiserror 2.0.17", ] [[package]] @@ -652,8 +1058,12 @@ name = "codex-common" version = "0.0.0" dependencies = [ "clap", + "codex-app-server-protocol", "codex-core", + "codex-lmstudio", + "codex-ollama", "codex-protocol", + "once_cell", "serde", "toml", ] @@ -665,26 +1075,46 @@ dependencies = [ "anyhow", "askama", "assert_cmd", + "assert_matches", "async-channel", + "async-trait", "base64", "bytes", "chrono", + "codex-app-server-protocol", "codex-apply-patch", + "codex-arg0", + "codex-async-utils", "codex-file-search", - "codex-mcp-client", + "codex-git", + "codex-keyring-store", + "codex-otel", "codex-protocol", + "codex-rmcp-client", + "codex-utils-pty", + "codex-utils-readiness", + "codex-utils-string", + "codex-utils-tokenizer", + "codex-windows-sandbox", + "core-foundation 0.9.4", "core_test_support", + "ctor 0.5.0", "dirs", + "dunce", "env-flags", + "escargot", "eventsource-stream", "futures", + "http", + "image", + "indexmap 2.12.0", + "keyring", "landlock", "libc", "maplit", "mcp-types", "openssl-sys", "os_info", - "portable-pty", "predicates", "pretty_assertions", "rand 0.9.2", @@ -693,12 +1123,16 @@ dependencies = [ "seccompiler", "serde", "serde_json", + "serial_test", "sha1", + "sha2", "shlex", "similar", "strum_macros 0.27.2", "tempfile", - "thiserror 2.0.16", + "test-case", + "test-log", + "thiserror 2.0.17", "time", "tokio", "tokio-test", @@ -706,6 +1140,7 @@ dependencies = [ "toml", "toml_edit", "tracing", + "tracing-test", "tree-sitter", "tree-sitter-bash", "uuid", @@ -721,23 +1156,27 @@ version = "0.0.0" dependencies = [ "anyhow", "assert_cmd", - "chrono", "clap", "codex-arg0", "codex-common", "codex-core", - "codex-ollama", "codex-protocol", "core_test_support", "libc", + "mcp-types", + "opentelemetry-appender-tracing", "owo-colors", "predicates", + "pretty_assertions", + "serde", "serde_json", "shlex", + "supports-color", "tempfile", "tokio", "tracing", "tracing-subscriber", + "ts-rs", "uuid", "walkdir", "wiremock", @@ -763,6 +1202,32 @@ dependencies = [ "tempfile", ] +[[package]] +name = "codex-execpolicy2" +version = "0.0.0" +dependencies = [ + "anyhow", + "clap", + "multimap", + "pretty_assertions", + "serde", + "serde_json", + "shlex", + "starlark", + "thiserror 2.0.17", +] + +[[package]] +name = "codex-feedback" +version = "0.0.0" +dependencies = [ + "anyhow", + "codex-protocol", + "pretty_assertions", + "sentry", + "tracing-subscriber", +] + [[package]] name = "codex-file-search" version = "0.0.0" @@ -776,6 +1241,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "codex-git" +version = "0.0.0" +dependencies = [ + "assert_matches", + "once_cell", + "pretty_assertions", + "regex", + "schemars 0.8.22", + "serde", + "tempfile", + "thiserror 2.0.17", + "ts-rs", + "walkdir", +] + +[[package]] +name = "codex-keyring-store" +version = "0.0.0" +dependencies = [ + "keyring", + "tracing", +] + [[package]] name = "codex-linux-sandbox" version = "0.0.0" @@ -789,15 +1278,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "codex-lmstudio" +version = "0.0.0" +dependencies = [ + "codex-core", + "reqwest", + "serde_json", + "tokio", + "tracing", + "which", + "wiremock", +] + [[package]] name = "codex-login" version = "0.0.0" dependencies = [ + "anyhow", "base64", "chrono", + "codex-app-server-protocol", "codex-core", - "codex-protocol", - "rand 0.8.5", + "core_test_support", + "rand 0.9.2", "reqwest", "serde", "serde_json", @@ -808,19 +1312,7 @@ dependencies = [ "url", "urlencoding", "webbrowser", -] - -[[package]] -name = "codex-mcp-client" -version = "0.0.0" -dependencies = [ - "anyhow", - "mcp-types", - "serde", - "serde_json", - "tokio", - "tracing", - "tracing-subscriber", + "wiremock", ] [[package]] @@ -829,12 +1321,12 @@ version = "0.0.0" dependencies = [ "anyhow", "assert_cmd", - "base64", "codex-arg0", "codex-common", "codex-core", - "codex-login", "codex-protocol", + "codex-utils-json-to-toml", + "core_test_support", "mcp-types", "mcp_test_support", "os_info", @@ -845,10 +1337,8 @@ dependencies = [ "shlex", "tempfile", "tokio", - "toml", "tracing", "tracing-subscriber", - "uuid", "wiremock", ] @@ -856,6 +1346,7 @@ dependencies = [ name = "codex-ollama" version = "0.0.0" dependencies = [ + "assert_matches", "async-stream", "bytes", "codex-core", @@ -867,16 +1358,48 @@ dependencies = [ "wiremock", ] +[[package]] +name = "codex-otel" +version = "0.0.0" +dependencies = [ + "chrono", + "codex-app-server-protocol", + "codex-protocol", + "eventsource-stream", + "opentelemetry", + "opentelemetry-otlp", + "opentelemetry-semantic-conventions", + "opentelemetry_sdk", + "reqwest", + "serde", + "serde_json", + "strum_macros 0.27.2", + "tokio", + "tonic", + "tracing", +] + +[[package]] +name = "codex-process-hardening" +version = "0.0.0" +dependencies = [ + "libc", +] + [[package]] name = "codex-protocol" version = "0.0.0" dependencies = [ + "anyhow", "base64", + "codex-git", + "codex-utils-image", "icu_decimal", "icu_locale_core", + "icu_provider", "mcp-types", "mime_guess", - "pretty_assertions", + "schemars 0.8.22", "serde", "serde_json", "serde_with", @@ -890,14 +1413,60 @@ dependencies = [ ] [[package]] -name = "codex-protocol-ts" +name = "codex-responses-api-proxy" version = "0.0.0" dependencies = [ "anyhow", "clap", + "codex-process-hardening", + "ctor 0.5.0", + "libc", + "reqwest", + "serde", + "serde_json", + "tiny_http", + "zeroize", +] + +[[package]] +name = "codex-rmcp-client" +version = "0.0.0" +dependencies = [ + "anyhow", + "axum", + "codex-keyring-store", "codex-protocol", + "dirs", + "escargot", + "futures", + "keyring", "mcp-types", - "ts-rs", + "oauth2", + "pretty_assertions", + "reqwest", + "rmcp", + "serde", + "serde_json", + "serial_test", + "sha2", + "tempfile", + "tiny_http", + "tokio", + "tracing", + "urlencoding", + "webbrowser", + "which", +] + +[[package]] +name = "codex-stdio-to-uds" +version = "0.0.0" +dependencies = [ + "anyhow", + "assert_cmd", + "pretty_assertions", + "tempfile", + "uds_windows", ] [[package]] @@ -906,39 +1475,46 @@ version = "0.0.0" dependencies = [ "anyhow", "arboard", + "assert_matches", "async-stream", "base64", "chrono", "clap", "codex-ansi-escape", + "codex-app-server-protocol", "codex-arg0", + "codex-backend-client", "codex-common", "codex-core", + "codex-feedback", "codex-file-search", "codex-login", - "codex-ollama", "codex-protocol", + "codex-windows-sandbox", "color-eyre", "crossterm", + "derive_more 2.0.1", "diffy", "dirs", + "dunce", "image", "insta", "itertools 0.14.0", "lazy_static", "libc", "mcp-types", - "once_cell", - "owo-colors", - "path-clean", + "opentelemetry-appender-tracing", "pathdiff", "pretty_assertions", "pulldown-cmark", "rand 0.9.2", "ratatui", + "ratatui-macros", "regex-lite", + "reqwest", "serde", "serde_json", + "serial_test", "shlex", "strum 0.27.2", "strum_macros 0.27.2", @@ -947,15 +1523,97 @@ dependencies = [ "textwrap 0.16.2", "tokio", "tokio-stream", + "toml", "tracing", "tracing-appender", "tracing-subscriber", + "tree-sitter-bash", + "tree-sitter-highlight", "unicode-segmentation", - "unicode-width 0.1.14", + "unicode-width 0.2.1", "url", "vt100", ] +[[package]] +name = "codex-utils-cache" +version = "0.0.0" +dependencies = [ + "lru", + "sha1", + "tokio", +] + +[[package]] +name = "codex-utils-image" +version = "0.0.0" +dependencies = [ + "base64", + "codex-utils-cache", + "image", + "tempfile", + "thiserror 2.0.17", + "tokio", +] + +[[package]] +name = "codex-utils-json-to-toml" +version = "0.0.0" +dependencies = [ + "pretty_assertions", + "serde_json", + "toml", +] + +[[package]] +name = "codex-utils-pty" +version = "0.0.0" +dependencies = [ + "anyhow", + "portable-pty", + "tokio", +] + +[[package]] +name = "codex-utils-readiness" +version = "0.0.0" +dependencies = [ + "assert_matches", + "async-trait", + "thiserror 2.0.17", + "time", + "tokio", +] + +[[package]] +name = "codex-utils-string" +version = "0.0.0" + +[[package]] +name = "codex-utils-tokenizer" +version = "0.0.0" +dependencies = [ + "anyhow", + "codex-utils-cache", + "pretty_assertions", + "thiserror 2.0.17", + "tiktoken-rs", + "tokio", +] + +[[package]] +name = "codex-windows-sandbox" +version = "0.1.0" +dependencies = [ + "anyhow", + "dirs-next", + "dunce", + "rand 0.8.5", + "serde", + "serde_json", + "windows-sys 0.52.0", +] + [[package]] name = "color-eyre" version = "0.6.5" @@ -1043,6 +1701,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "convert_case" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb402b8d4c85569410425650ce3eddc7d698ed96d39a73f941b08fb63082f1e7" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -1073,10 +1740,16 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" name = "core_test_support" version = "0.0.0" dependencies = [ + "anyhow", + "assert_cmd", "codex-core", + "codex-protocol", + "notify", + "regex-lite", "serde_json", "tempfile", "tokio", + "walkdir", "wiremock", ] @@ -1135,10 +1808,9 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crossterm" version = "0.28.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" +source = "git+https://github.com/nornagon/crossterm?branch=nornagon%2Fcolor-query#87db8bfa6dc99427fd3b071681b07fc31c6ce995" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "crossterm_winapi", "futures-core", "mio", @@ -1184,14 +1856,40 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ctor" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67773048316103656a637612c4a62477603b777d91d9c62ff2290f9cde178fdb" +dependencies = [ + "ctor-proc-macro", + "dtor", +] + +[[package]] +name = "ctor-proc-macro" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2931af7e13dc045d8e9d26afccc6fa115d64e115c9c84b1166288b46f6782c2" + [[package]] name = "darling" version = "0.20.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" dependencies = [ - "darling_core", - "darling_macro", + "darling_core 0.20.11", + "darling_macro 0.20.11", +] + +[[package]] +name = "darling" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" +dependencies = [ + "darling_core 0.21.3", + "darling_macro 0.21.3", ] [[package]] @@ -1208,17 +1906,71 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "darling_core" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.11.1", + "syn 2.0.104", +] + [[package]] name = "darling_macro" version = "0.20.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ - "darling_core", + "darling_core 0.20.11", "quote", "syn 2.0.104", ] +[[package]] +name = "darling_macro" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" +dependencies = [ + "darling_core 0.21.3", + "quote", + "syn 2.0.104", +] + +[[package]] +name = "dbus" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190b6255e8ab55a7b568df5a883e9497edc3e4821c06396612048b430e5ad1e9" +dependencies = [ + "libc", + "libdbus-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "dbus-secret-service" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "708b509edf7889e53d7efb0ffadd994cc6c2345ccb62f55cfd6b0682165e4fa6" +dependencies = [ + "aes", + "block-padding", + "cbc", + "dbus", + "fastrand", + "hkdf", + "num", + "once_cell", + "sha2", + "zeroize", +] + [[package]] name = "deadpool" version = "0.12.3" @@ -1237,6 +1989,16 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "092966b41edc516079bdf31ec78a2e0588d1d0c08f78b91d8307215928642b2b" +[[package]] +name = "debugid" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef552e6f588e446098f6ba40d89ac146c8c7b64aade83c051ee00bb5d2bc18d" +dependencies = [ + "serde", + "uuid", +] + [[package]] name = "debugserver-types" version = "0.5.0" @@ -1250,12 +2012,12 @@ dependencies = [ [[package]] name = "deranged" -version = "0.4.0" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e" +checksum = "a41953f86f8a05768a6cda24def994fd2f424b04ec5c719cf89989779f199071" dependencies = [ "powerfmt", - "serde", + "serde_core", ] [[package]] @@ -1293,7 +2055,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" dependencies = [ - "convert_case", + "convert_case 0.6.0", "proc-macro2", "quote", "syn 2.0.104", @@ -1306,6 +2068,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3" dependencies = [ + "convert_case 0.7.1", "proc-macro2", "quote", "syn 2.0.104", @@ -1341,6 +2104,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -1371,7 +2135,7 @@ dependencies = [ "libc", "option-ext", "redox_users 0.5.0", - "windows-sys 0.60.2", + "windows-sys 0.61.1", ] [[package]] @@ -1391,7 +2155,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "objc2", ] @@ -1434,6 +2198,27 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" +[[package]] +name = "dtor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e58a0764cddb55ab28955347b45be00ade43d4d6f3ba4bf3dc354e4ec9432934" +dependencies = [ + "dtor-proc-macro", +] + +[[package]] +name = "dtor-proc-macro" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f678cf4a922c215c63e0de95eb1ff08a958a81d47e485cf9da1e27bf6305cfa5" + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "dupe" version = "0.9.1" @@ -1490,6 +2275,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "endi" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d8a32ae18130a3c84dd492d4215c3d913c3b07c6b63c2eb3eb7ff1101ab7bf" + [[package]] name = "endian-type" version = "0.1.2" @@ -1503,6 +2294,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1027f7680c853e056ebcec683615fb6fbbc07dbaa13b4d5d9442b146ded4ecef" dependencies = [ "enumflags2_derive", + "serde", ] [[package]] @@ -1576,6 +2368,17 @@ version = "3.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59" +[[package]] +name = "escargot" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c3aea32bc97b500c9ca6a72b768a26e558264303d101d3409cf6d57a9ed0cf" +dependencies = [ + "log", + "serde", + "serde_json", +] + [[package]] name = "event-listener" version = "5.4.0" @@ -1618,6 +2421,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "fancy-regex" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531e46835a22af56d1e3b66f04844bed63158bc094a628bec1d321d9b4c44bf2" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax 0.8.5", +] + [[package]] name = "fastrand" version = "2.3.0" @@ -1675,6 +2489,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "findshlibs" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40b9e59cd0f7e0806cca4be089683ecb6434e602038df21fe6bf6711b2f07f64" +dependencies = [ + "cc", + "lazy_static", + "libc", + "winapi", +] + [[package]] name = "fixed_decimal" version = "0.7.0" @@ -1747,6 +2573,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fsevent-sys" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" +dependencies = [ + "libc", +] + [[package]] name = "futures" version = "0.3.31" @@ -1795,6 +2630,19 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + [[package]] name = "futures-macro" version = "0.3.31" @@ -1881,8 +2729,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.1+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1892,9 +2742,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi", "wasi 0.14.2+wasi-0.2.4", + "wasm-bindgen", ] [[package]] @@ -1928,7 +2780,7 @@ dependencies = [ "futures-core", "futures-sink", "http", - "indexmap 2.10.0", + "indexmap 2.12.0", "slab", "tokio", "tokio-util", @@ -1972,6 +2824,12 @@ dependencies = [ "foldhash", ] +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" + [[package]] name = "heck" version = "0.5.0" @@ -1990,6 +2848,24 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "home" version = "0.5.11" @@ -1999,6 +2875,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "hostname" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56f203cd1c76362b69e3863fd987520ac36cf70a8c92627449b2f64a8cf7d65" +dependencies = [ + "cfg-if", + "libc", + "windows-link 0.1.3", +] + [[package]] name = "http" version = "1.3.1" @@ -2045,15 +2932,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humansize" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" -dependencies = [ - "libm", -] - [[package]] name = "hyper" version = "1.7.0" @@ -2087,10 +2965,25 @@ dependencies = [ "hyper", "hyper-util", "rustls", + "rustls-native-certs", "rustls-pki-types", "tokio", "tokio-rustls", "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", ] [[package]] @@ -2127,7 +3020,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2", + "socket2 0.6.0", "system-configuration", "tokio", "tower-service", @@ -2161,9 +3054,9 @@ dependencies = [ [[package]] name = "icu_collections" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "200072f5d0e3614556f94a9930d5dc3e0662a652823904c3a75dc3b0af7fee47" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" dependencies = [ "displaydoc", "potential_utf", @@ -2174,34 +3067,31 @@ dependencies = [ [[package]] name = "icu_decimal" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fec61c43fdc4e368a9f450272833123a8ef0d7083a44597660ce94d791b8a2e2" +checksum = "a38c52231bc348f9b982c1868a2af3195199623007ba2c7650f432038f5b3e8e" dependencies = [ - "displaydoc", "fixed_decimal", "icu_decimal_data", "icu_locale", "icu_locale_core", "icu_provider", - "tinystr", "writeable", "zerovec", ] [[package]] name = "icu_decimal_data" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b70963bc35f9bdf1bc66a5c1f458f4991c1dc71760e00fa06016b2c76b2738d5" +checksum = "2905b4044eab2dd848fe84199f9195567b63ab3a93094711501363f63546fef7" [[package]] name = "icu_locale" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ae5921528335e91da1b6c695dbf1ec37df5ac13faa3f91e5640be93aa2fbefd" +checksum = "532b11722e350ab6bf916ba6eb0efe3ee54b932666afec989465f9243fe6dd60" dependencies = [ - "displaydoc", "icu_collections", "icu_locale_core", "icu_locale_data", @@ -2213,12 +3103,13 @@ dependencies = [ [[package]] name = "icu_locale_core" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cde2700ccaed3872079a65fb1a78f6c0a36c91570f28755dda67bc8f7d9f00a" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" dependencies = [ "displaydoc", "litemap", + "serde", "tinystr", "writeable", "zerovec", @@ -2226,17 +3117,16 @@ dependencies = [ [[package]] name = "icu_locale_data" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fdef0c124749d06a743c69e938350816554eb63ac979166590e2b4ee4252765" +checksum = "f03e2fcaefecdf05619f3d6f91740e79ab969b4dd54f77cbf546b1d0d28e3147" [[package]] name = "icu_normalizer" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "436880e8e18df4d7bbc06d58432329d6458cc84531f7ac5f024e93deadb37979" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" dependencies = [ - "displaydoc", "icu_collections", "icu_normalizer_data", "icu_properties", @@ -2247,42 +3137,40 @@ dependencies = [ [[package]] name = "icu_normalizer_data" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00210d6893afc98edb752b664b8890f0ef174c8adbb8d0be9710fa66fbbf72d3" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" [[package]] name = "icu_properties" -version = "2.0.1" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "016c619c1eeb94efb86809b015c58f479963de65bdb6253345c1a1276f22e32b" +checksum = "e93fcd3157766c0c8da2f8cff6ce651a31f0810eaa1c51ec363ef790bbb5fb99" dependencies = [ - "displaydoc", "icu_collections", "icu_locale_core", "icu_properties_data", "icu_provider", - "potential_utf", "zerotrie", "zerovec", ] [[package]] name = "icu_properties_data" -version = "2.0.1" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "298459143998310acd25ffe6810ed544932242d3f07083eee1084d83a71bd632" +checksum = "02845b3647bb045f1100ecd6480ff52f34c35f82d9880e029d329c21d1054899" [[package]] name = "icu_provider" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03c80da27b5f4187909049ee2d72f276f0d9f99a42c306bd0131ecfe04d8e5af" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" dependencies = [ "displaydoc", "icu_locale_core", + "serde", "stable_deref_trait", - "tinystr", "writeable", "yoke", "zerofrom", @@ -2368,13 +3256,14 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.10.0" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661" +checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" dependencies = [ "equivalent", - "hashbrown 0.15.4", + "hashbrown 0.16.0", "serde", + "serde_core", ] [[package]] @@ -2383,6 +3272,36 @@ version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd" +[[package]] +name = "inotify" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3" +dependencies = [ + "bitflags 2.10.0", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" +dependencies = [ + "libc", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "block-padding", + "generic-array", +] + [[package]] name = "insta" version = "1.43.2" @@ -2400,7 +3319,7 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "435d80800b936787d62688c927b6490e887c7ef5ff9ce922c6c6050fca75eb9a" dependencies = [ - "darling", + "darling 0.20.11", "indoc", "proc-macro2", "quote", @@ -2422,7 +3341,7 @@ version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "cfg-if", "libc", ] @@ -2555,6 +3474,44 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keyring" +version = "3.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eebcc3aff044e5944a8fbaf69eb277d11986064cba30c468730e8b9909fb551c" +dependencies = [ + "byteorder", + "dbus-secret-service", + "linux-keyutils", + "log", + "secret-service", + "security-framework 2.11.1", + "security-framework 3.5.1", + "windows-sys 0.60.2", + "zbus", + "zeroize", +] + +[[package]] +name = "kqueue" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a" +dependencies = [ + "kqueue-sys", + "libc", +] + +[[package]] +name = "kqueue-sys" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + [[package]] name = "lalrpop" version = "0.19.12" @@ -2594,7 +3551,7 @@ checksum = "b3d2ef408b88e913bfc6594f5e693d57676f6463ded7d8bf994175364320c706" dependencies = [ "enumflags2", "libc", - "thiserror 2.0.16", + "thiserror 2.0.17", ] [[package]] @@ -2610,10 +3567,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" [[package]] -name = "libm" -version = "0.2.15" +name = "libdbus-sys" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" +checksum = "5cbe856efeb50e4681f010e9aaa2bf0a644e10139e54cde10fc83a307c23bd9f" +dependencies = [ + "pkg-config", +] [[package]] name = "libredox" @@ -2621,7 +3581,17 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4488594b9328dee448adb906d8b126d9b7deb7cf5c22161ee591610bb1be83c0" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", + "libc", +] + +[[package]] +name = "linux-keyutils" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "761e49ec5fd8a5a463f9b84e877c373d888935b71c6be78f3767fe2ae6bed18e" +dependencies = [ + "bitflags 2.10.0", "libc", ] @@ -2655,9 +3625,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "logos" @@ -2691,6 +3661,12 @@ dependencies = [ "hashbrown 0.15.4", ] +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + [[package]] name = "lsp-types" version = "0.94.1" @@ -2719,10 +3695,17 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "mcp-types" version = "0.0.0" dependencies = [ + "schemars 0.8.22", "serde", "serde_json", "ts-rs", @@ -2736,7 +3719,6 @@ dependencies = [ "assert_cmd", "codex-core", "codex-mcp-server", - "codex-protocol", "mcp-types", "os_info", "pretty_assertions", @@ -2761,6 +3743,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -2836,7 +3827,7 @@ dependencies = [ "openssl-probe", "openssl-sys", "schannel", - "security-framework", + "security-framework 2.11.1", "security-framework-sys", "tempfile", ] @@ -2868,9 +3859,34 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "cfg-if", - "cfg_aliases", + "cfg_aliases 0.1.1", + "libc", +] + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "cfg_aliases 0.2.1", + "libc", + "memoffset 0.9.1", +] + +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "cfg_aliases 0.2.1", "libc", ] @@ -2890,6 +3906,30 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[package]] +name = "notify" +version = "8.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3" +dependencies = [ + "bitflags 2.10.0", + "fsevent-sys", + "inotify", + "kqueue", + "libc", + "log", + "mio", + "notify-types", + "walkdir", + "windows-sys 0.60.2", +] + +[[package]] +name = "notify-types" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d" + [[package]] name = "nu-ansi-term" version = "0.50.1" @@ -2909,6 +3949,20 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -2919,6 +3973,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -2934,6 +3997,28 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -2962,6 +4047,26 @@ dependencies = [ "libc", ] +[[package]] +name = "oauth2" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e219e79014df21a225b1860a479e2dcd7cbd9130f4defd4bd0e191ea31d67d" +dependencies = [ + "base64", + "chrono", + "getrandom 0.2.16", + "http", + "rand 0.8.5", + "reqwest", + "serde", + "serde_json", + "serde_path_to_error", + "sha2", + "thiserror 1.0.69", + "url", +] + [[package]] name = "objc2" version = "0.6.2" @@ -2977,7 +4082,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6f29f568bec459b0ddff777cec4fe3fd8666d82d5a40ebd0ff7e66134f89bcc" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "objc2", "objc2-core-graphics", "objc2-foundation", @@ -2989,7 +4094,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c10c2894a6fed806ade6027bcd50662746363a9589d3ec9d9bef30a4e4bc166" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "dispatch2", "objc2", ] @@ -3000,7 +4105,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "989c6c68c13021b5c2d6b71456ebb0f9dc78d752e86a98da7c716f4f9470f5a4" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "dispatch2", "objc2", "objc2-core-foundation", @@ -3019,7 +4124,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "900831247d2fe1a09a683278e5384cfb8c80c79fe6b166f9d14bfdde0ea1b03c" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "objc2", "objc2-core-foundation", ] @@ -3030,7 +4135,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7282e9ac92529fa3457ce90ebb15f4ecbc383e8338060960760fa2cf75420c3c" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "objc2", "objc2-core-foundation", ] @@ -3062,7 +4167,7 @@ version = "0.10.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "cfg-if", "foreign-types", "libc", @@ -3110,12 +4215,120 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "opentelemetry" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf416e4cb72756655126f7dd7bb0af49c674f4c1b9903e80c009e0c37e552e6" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "pin-project-lite", + "thiserror 2.0.17", + "tracing", +] + +[[package]] +name = "opentelemetry-appender-tracing" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e68f63eca5fad47e570e00e893094fc17be959c80c79a7d6ec1abdd5ae6ffc16" +dependencies = [ + "opentelemetry", + "tracing", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "opentelemetry-http" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f6639e842a97dbea8886e3439710ae463120091e2e064518ba8e716e6ac36d" +dependencies = [ + "async-trait", + "bytes", + "http", + "opentelemetry", + "reqwest", +] + +[[package]] +name = "opentelemetry-otlp" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbee664a43e07615731afc539ca60c6d9f1a9425e25ca09c57bc36c87c55852b" +dependencies = [ + "http", + "opentelemetry", + "opentelemetry-http", + "opentelemetry-proto", + "opentelemetry_sdk", + "prost", + "reqwest", + "serde_json", + "thiserror 2.0.17", + "tokio", + "tonic", + "tracing", +] + +[[package]] +name = "opentelemetry-proto" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e046fd7660710fe5a05e8748e70d9058dc15c94ba914e7c4faa7c728f0e8ddc" +dependencies = [ + "base64", + "hex", + "opentelemetry", + "opentelemetry_sdk", + "prost", + "serde", + "tonic", +] + +[[package]] +name = "opentelemetry-semantic-conventions" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83d059a296a47436748557a353c5e6c5705b9470ef6c95cfc52c21a8814ddac2" + +[[package]] +name = "opentelemetry_sdk" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f644aa9e5e31d11896e024305d7e3c98a88884d9f8919dbf37a9991bc47a4b" +dependencies = [ + "futures-channel", + "futures-executor", + "futures-util", + "opentelemetry", + "percent-encoding", + "rand 0.9.2", + "serde_json", + "thiserror 2.0.17", + "tokio", + "tokio-stream", +] + [[package]] name = "option-ext" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "ordered-stream" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +dependencies = [ + "futures-core", + "pin-project-lite", +] + [[package]] name = "os_info" version = "3.12.0" @@ -3128,6 +4341,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "os_pipe" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db335f4760b14ead6290116f2427bf33a14d4f0617d49f78a246de10c1831224" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "owo-colors" version = "4.2.2" @@ -3178,12 +4401,6 @@ dependencies = [ "path-dedot", ] -[[package]] -name = "path-clean" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17359afc20d7ab31fdb42bb844c8b3bb1dabd7dcf7e68428492da7f16966fcef" - [[package]] name = "path-dedot" version = "3.1.1" @@ -3212,7 +4429,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ "fixedbitset", - "indexmap 2.10.0", + "indexmap 2.12.0", ] [[package]] @@ -3224,6 +4441,26 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pin-project" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -3236,6 +4473,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + [[package]] name = "pkg-config" version = "0.3.32" @@ -3249,8 +4497,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3af6b589e163c5a788fab00ce0c0366f6efbb9959c2f9874b224936af7fce7e1" dependencies = [ "base64", - "indexmap 2.10.0", - "quick-xml", + "indexmap 2.12.0", + "quick-xml 0.38.0", "serde", "time", ] @@ -3261,13 +4509,27 @@ version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97baced388464909d42d89643fe4361939af9b7ce7a31ee32a168f832a70f2a0" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "crc32fast", "fdeflate", "flate2", "miniz_oxide", ] +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix 1.0.8", + "windows-sys 0.61.1", +] + [[package]] name = "portable-atomic" version = "1.11.1" @@ -3296,7 +4558,7 @@ dependencies = [ "lazy_static", "libc", "log", - "nix", + "nix 0.28.0", "serial2", "shared_library", "shell-words", @@ -3306,11 +4568,12 @@ dependencies = [ [[package]] name = "potential_utf" -version = "0.1.2" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5a7c30837279ca13e7c867e9e40053bc68740f988cb07f7ca6df43cc734b585" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" dependencies = [ - "serde", + "serde_core", + "writeable", "zerovec", ] @@ -3375,6 +4638,15 @@ dependencies = [ "yansi", ] +[[package]] +name = "proc-macro-crate" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983" +dependencies = [ + "toml_edit", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -3384,13 +4656,50 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "process-wrap" +version = "8.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ef4f2f0422f23a82ec9f628ea2acd12871c81a9362b02c43c1aa86acfc3ba1" +dependencies = [ + "futures", + "indexmap 2.12.0", + "nix 0.30.1", + "tokio", + "tracing", + "windows", +] + +[[package]] +name = "prost" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-derive" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" +dependencies = [ + "anyhow", + "itertools 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "pulldown-cmark" version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "getopts", "memchr", "pulldown-cmark-escape", @@ -3418,6 +4727,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" +[[package]] +name = "quick-xml" +version = "0.37.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "331e97a1af0bf59823e6eadffe373d7b27f485be8748f71471c662c1f269b7fb" +dependencies = [ + "memchr", +] + [[package]] name = "quick-xml" version = "0.38.0" @@ -3427,6 +4745,61 @@ dependencies = [ "memchr", ] +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases 0.2.1", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash 2.1.1", + "rustls", + "socket2 0.6.0", + "thiserror 2.0.17", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" +dependencies = [ + "bytes", + "getrandom 0.3.3", + "lru-slab", + "rand 0.9.2", + "ring", + "rustc-hash 2.1.1", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.17", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases 0.2.1", + "libc", + "once_cell", + "socket2 0.6.0", + "tracing", + "windows-sys 0.60.2", +] + [[package]] name = "quote" version = "1.0.40" @@ -3516,7 +4889,7 @@ name = "ratatui" version = "0.29.0" source = "git+https://github.com/nornagon/ratatui?branch=nornagon-v0.29.0-patch#9b2ad1298408c45918ee9f8241a6f95498cdbed2" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "cassowary", "compact_str", "crossterm", @@ -3531,13 +4904,22 @@ dependencies = [ "unicode-width 0.2.1", ] +[[package]] +name = "ratatui-macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fef540f80dbe8a0773266fa6077788ceb65ef624cdbf36e131aaf90b4a52df4" +dependencies = [ + "ratatui", +] + [[package]] name = "redox_syscall" version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e8af0dde094006011e6a740d4879319439489813bd0bcdc7d821beaeeff48ec" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", ] [[package]] @@ -3559,7 +4941,7 @@ checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b" dependencies = [ "getrandom 0.2.16", "libredox", - "thiserror 2.0.16", + "thiserror 2.0.17", ] [[package]] @@ -3649,6 +5031,9 @@ dependencies = [ "native-tls", "percent-encoding", "pin-project-lite", + "quinn", + "rustls", + "rustls-native-certs", "rustls-pki-types", "serde", "serde_json", @@ -3656,6 +5041,7 @@ dependencies = [ "sync_wrapper", "tokio", "tokio-native-tls", + "tokio-rustls", "tokio-util", "tower", "tower-http", @@ -3665,6 +5051,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", + "webpki-roots", ] [[package]] @@ -3681,19 +5068,87 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rmcp" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5947688160b56fb6c827e3c20a72c90392a1d7e9dec74749197aa1780ac42ca" +dependencies = [ + "base64", + "bytes", + "chrono", + "futures", + "http", + "http-body", + "http-body-util", + "oauth2", + "paste", + "pin-project-lite", + "process-wrap", + "rand 0.9.2", + "reqwest", + "rmcp-macros", + "schemars 1.0.4", + "serde", + "serde_json", + "sse-stream", + "thiserror 2.0.17", + "tokio", + "tokio-stream", + "tokio-util", + "tower-service", + "tracing", + "url", + "uuid", +] + +[[package]] +name = "rmcp-macros" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01263441d3f8635c628e33856c468b96ebbce1af2d3699ea712ca71432d4ee7a" +dependencies = [ + "darling 0.21.3", + "proc-macro2", + "quote", + "serde_json", + "syn 2.0.104", +] + [[package]] name = "rustc-demangle" version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "errno", "libc", "linux-raw-sys 0.4.15", @@ -3706,7 +5161,7 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "errno", "libc", "linux-raw-sys 0.9.4", @@ -3720,18 +5175,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2491382039b29b9b11ff08b76ff6c97cf287671dbb74f0be44bda389fffe9bd1" dependencies = [ "once_cell", + "ring", "rustls-pki-types", "rustls-webpki", "subtle", "zeroize", ] +[[package]] +name = "rustls-native-certs" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcff2dd52b58a8d98a70243663a0d234c4e2b79235637849d15913394a247d3" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework 3.5.1", +] + [[package]] name = "rustls-pki-types" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "229a4a4c221013e7e1f1a043678c5cc39fe5171437c88fb47151a21e6f5b5c79" dependencies = [ + "web-time", "zeroize", ] @@ -3758,7 +5227,7 @@ version = "14.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7803e8936da37efd9b6d4478277f4b2b9bb5cdb37a113e8d63222e58da647e63" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "cfg-if", "clipboard-win", "fd-lock", @@ -3766,7 +5235,7 @@ dependencies = [ "libc", "log", "memchr", - "nix", + "nix 0.28.0", "radix_trie", "unicode-segmentation", "unicode-width 0.1.14", @@ -3790,12 +5259,21 @@ dependencies = [ ] [[package]] -name = "schannel" -version = "0.1.27" +name = "scc" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +checksum = "46e6f046b7fef48e2660c57ed794263155d713de679057f2d0c169bfc6e756cc" dependencies = [ - "windows-sys 0.59.0", + "sdd", +] + +[[package]] +name = "schannel" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891d81b926048e76efe18581bf793546b4c0eaf8448d72be8de2bbee5fd166e1" +dependencies = [ + "windows-sys 0.61.1", ] [[package]] @@ -3847,7 +5325,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fbf2ae1b8bc8e02df939598064d22402220cd5bbcca1c76f7d6a310974d5615" dependencies = [ "dyn-clone", - "schemars_derive", + "schemars_derive 0.8.22", "serde", "serde_json", ] @@ -3870,8 +5348,10 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0" dependencies = [ + "chrono", "dyn-clone", "ref-cast", + "schemars_derive 1.0.4", "serde", "serde_json", ] @@ -3888,12 +5368,30 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "schemars_derive" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d020396d1d138dc19f1165df7545479dcd58d93810dc5d646a16e55abefa80" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 2.0.104", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sdd" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490dcfcbfef26be6800d11870ff2df8774fa6e86d047e3e8c8a76b25655e41ca" + [[package]] name = "seccompiler" version = "0.5.0" @@ -3903,13 +5401,32 @@ dependencies = [ "libc", ] +[[package]] +name = "secret-service" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4d35ad99a181be0a60ffcbe85d680d98f87bdc4d7644ade319b87076b9dbfd4" +dependencies = [ + "aes", + "cbc", + "futures-util", + "generic-array", + "hkdf", + "num", + "once_cell", + "rand 0.8.5", + "serde", + "sha2", + "zbus", +] + [[package]] name = "security-framework" version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "core-foundation 0.9.4", "core-foundation-sys", "libc", @@ -3917,20 +5434,147 @@ dependencies = [ ] [[package]] -name = "security-framework-sys" -version = "2.14.0" +name = "security-framework" +version = "3.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32" +checksum = "b3297343eaf830f66ede390ea39da1d462b6b0c1b000f420d0a83f898bbbe6ef" +dependencies = [ + "bitflags 2.10.0", + "core-foundation 0.10.1", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc1f0cbffaac4852523ce30d8bd3c5cdc873501d96ff467ca09b6767bb8cd5c0" dependencies = [ "core-foundation-sys", "libc", ] [[package]] -name = "serde" -version = "1.0.224" +name = "semver" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6aaeb1e94f53b16384af593c71e20b095e958dab1d26939c1b70645c5cfbcc0b" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + +[[package]] +name = "sentry" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5484316556650182f03b43d4c746ce0e3e48074a21e2f51244b648b6542e1066" +dependencies = [ + "httpdate", + "native-tls", + "reqwest", + "sentry-backtrace", + "sentry-contexts", + "sentry-core", + "sentry-debug-images", + "sentry-panic", + "sentry-tracing", + "tokio", + "ureq", +] + +[[package]] +name = "sentry-backtrace" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40aa225bb41e2ec9d7c90886834367f560efc1af028f1c5478a6cce6a59c463a" +dependencies = [ + "backtrace", + "once_cell", + "regex", + "sentry-core", +] + +[[package]] +name = "sentry-contexts" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a8dd746da3d16cb8c39751619cefd4fcdbd6df9610f3310fd646b55f6e39910" +dependencies = [ + "hostname", + "libc", + "os_info", + "rustc_version", + "sentry-core", + "uname", +] + +[[package]] +name = "sentry-core" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "161283cfe8e99c8f6f236a402b9ccf726b201f365988b5bb637ebca0abbd4a30" +dependencies = [ + "once_cell", + "rand 0.8.5", + "sentry-types", + "serde", + "serde_json", +] + +[[package]] +name = "sentry-debug-images" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc6b25e945fcaa5e97c43faee0267eebda9f18d4b09a251775d8fef1086238a" +dependencies = [ + "findshlibs", + "once_cell", + "sentry-core", +] + +[[package]] +name = "sentry-panic" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc74f229c7186dd971a9491ffcbe7883544aa064d1589bd30b83fb856cd22d63" +dependencies = [ + "sentry-backtrace", + "sentry-core", +] + +[[package]] +name = "sentry-tracing" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3c5faf2103cd01eeda779ea439b68c4ee15adcdb16600836e97feafab362ec" +dependencies = [ + "sentry-backtrace", + "sentry-core", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "sentry-types" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d68cdf6bc41b8ff3ae2a9c4671e97426dcdd154cc1d4b6b72813f285d6b163f" +dependencies = [ + "debugid", + "hex", + "rand 0.8.5", + "serde", + "serde_json", + "thiserror 1.0.69", + "time", + "url", + "uuid", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", "serde_derive", @@ -3938,18 +5582,18 @@ dependencies = [ [[package]] name = "serde_core" -version = "1.0.224" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32f39390fa6346e24defbcdd3d9544ba8a19985d0af74df8501fbfe9a64341ab" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.224" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ff78ab5e8561c9a675bfc1785cb07ae721f0ee53329a595cefd8c04c2ac4e0" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", @@ -3973,7 +5617,7 @@ version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" dependencies = [ - "indexmap 2.10.0", + "indexmap 2.12.0", "itoa", "memchr", "ryu", @@ -3981,6 +5625,17 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + [[package]] name = "serde_repr" version = "0.1.20" @@ -4023,7 +5678,7 @@ dependencies = [ "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.10.0", + "indexmap 2.12.0", "schemars 0.9.0", "schemars 1.0.4", "serde", @@ -4039,7 +5694,7 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de90945e6565ce0d9a25098082ed4ee4002e047cb59892c318d66821e14bb30f" dependencies = [ - "darling", + "darling 0.20.11", "proc-macro2", "quote", "syn 2.0.104", @@ -4056,6 +5711,31 @@ dependencies = [ "winapi", ] +[[package]] +name = "serial_test" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b258109f244e1d6891bf1053a55d63a5cd4f8f4c30cf9a1280989f80e7a1fa9" +dependencies = [ + "futures", + "log", + "once_cell", + "parking_lot", + "scc", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "sha1" version = "0.10.6" @@ -4067,6 +5747,12 @@ dependencies = [ "digest", ] +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + [[package]] name = "sha2" version = "0.10.9" @@ -4181,6 +5867,16 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "socket2" version = "0.6.0" @@ -4191,6 +5887,19 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "sse-stream" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb4dc4d33c68ec1f27d386b5610a351922656e1fdf5c05bbaad930cd1519479a" +dependencies = [ + "bytes", + "futures-util", + "http-body", + "http-body-util", + "pin-project-lite", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -4218,7 +5927,7 @@ dependencies = [ "inventory", "itertools 0.13.0", "maplit", - "memoffset", + "memoffset 0.6.5", "num-bigint", "num-traits", "once_cell", @@ -4435,7 +6144,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "core-foundation 0.9.4", "system-configuration-sys", ] @@ -4452,15 +6161,15 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.20.0" +version = "3.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" +checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" dependencies = [ "fastrand", "getrandom 0.3.3", "once_cell", "rustix 1.0.8", - "windows-sys 0.59.0", + "windows-sys 0.61.1", ] [[package]] @@ -4499,6 +6208,61 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" +[[package]] +name = "test-case" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" +dependencies = [ + "test-case-macros", +] + +[[package]] +name = "test-case-core" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn 2.0.104", +] + +[[package]] +name = "test-case-macros" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", + "test-case-core", +] + +[[package]] +name = "test-log" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e33b98a582ea0be1168eba097538ee8dd4bbe0f2b01b22ac92ea30054e5be7b" +dependencies = [ + "env_logger", + "test-log-macros", + "tracing-subscriber", +] + +[[package]] +name = "test-log-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451b374529930d7601b1eef8d32bc79ae870b6079b069401709c2a8bf9e75f36" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -4530,11 +6294,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.16" +version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" dependencies = [ - "thiserror-impl 2.0.16", + "thiserror-impl 2.0.17", ] [[package]] @@ -4550,9 +6314,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.16" +version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5e1be1c48b9172ee610da68fd9cd2770e7a4056cb3fc98710ee6906f0c7960" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" dependencies = [ "proc-macro2", "quote", @@ -4583,10 +6347,25 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.3.41" +name = "tiktoken-rs" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40" +checksum = "3a19830747d9034cd9da43a60eaa8e552dfda7712424aebf187b7a60126bae0d" +dependencies = [ + "anyhow", + "base64", + "bstr", + "fancy-regex", + "lazy_static", + "regex", + "rustc-hash 1.1.0", +] + +[[package]] +name = "time" +version = "0.3.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" dependencies = [ "deranged", "itoa", @@ -4601,15 +6380,15 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.4" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c" +checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b" [[package]] name = "time-macros" -version = "0.2.22" +version = "0.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49" +checksum = "30cfb0125f12d9c277f35663a0a33f8c30190f4e4574868a330595412d34ebf3" dependencies = [ "num-conv", "time-core", @@ -4646,6 +6425,21 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.47.1" @@ -4661,7 +6455,7 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "slab", - "socket2", + "socket2 0.6.0", "tokio-macros", "windows-sys 0.59.0", ] @@ -4730,6 +6524,7 @@ dependencies = [ "bytes", "futures-core", "futures-sink", + "futures-util", "pin-project-lite", "tokio", ] @@ -4740,7 +6535,7 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75129e1dc5000bfbaa9fee9d1b21f974f9fbad9daec557a521ee6e080825f6e8" dependencies = [ - "indexmap 2.10.0", + "indexmap 2.12.0", "serde", "serde_spanned", "toml_datetime", @@ -4764,7 +6559,7 @@ version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7211ff1b8f0d3adae1663b7da9ffe396eabe1ca25f0b0bee42b0da29a9ddce93" dependencies = [ - "indexmap 2.10.0", + "indexmap 2.12.0", "toml_datetime", "toml_parser", "toml_writer", @@ -4786,6 +6581,35 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64" +[[package]] +name = "tonic" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e581ba15a835f4d9ea06c55ab1bd4dce26fc53752c69a04aac00703bfb49ba9" +dependencies = [ + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "prost", + "socket2 0.5.10", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower" version = "0.5.2" @@ -4794,11 +6618,15 @@ checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ "futures-core", "futures-util", + "indexmap 2.12.0", "pin-project-lite", + "slab", "sync_wrapper", "tokio", + "tokio-util", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -4807,7 +6635,7 @@ version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "bytes", "futures-util", "http", @@ -4916,10 +6744,31 @@ dependencies = [ ] [[package]] -name = "tree-sitter" -version = "0.25.9" +name = "tracing-test" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccd2a058a86cfece0bf96f7cce1021efef9c8ed0e892ab74639173e5ed7a34fa" +checksum = "557b891436fe0d5e0e363427fc7f217abf9ccd510d5136549847bdcbcd011d68" +dependencies = [ + "tracing-core", + "tracing-subscriber", + "tracing-test-macro", +] + +[[package]] +name = "tracing-test-macro" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04659ddb06c87d233c566112c1c9c5b9e98256d9af50ec3bc9c8327f873a7568" +dependencies = [ + "quote", + "syn 2.0.104", +] + +[[package]] +name = "tree-sitter" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78f873475d258561b06f1c595d93308a7ed124d9977cb26b148c2084a4a3cc87" dependencies = [ "cc", "regex", @@ -4939,12 +6788,36 @@ dependencies = [ "tree-sitter-language", ] +[[package]] +name = "tree-sitter-highlight" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adc5f880ad8d8f94e88cb81c3557024cf1a8b75e3b504c50481ed4f5a6006ff3" +dependencies = [ + "regex", + "streaming-iterator", + "thiserror 2.0.17", + "tree-sitter", +] + [[package]] name = "tree-sitter-language" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4013970217383f67b18aef68f6fb2e8d409bc5755227092d32efb0422ba24b8" +[[package]] +name = "tree_magic_mini" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f943391d896cdfe8eec03a04d7110332d445be7df856db382dd96a730667562c" +dependencies = [ + "memchr", + "nom", + "once_cell", + "petgraph", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -4958,7 +6831,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ef1b7a6d914a34127ed8e1fa927eb7088903787bcded4fa3eef8f85ee1568be" dependencies = [ "serde_json", - "thiserror 2.0.16", + "thiserror 2.0.17", "ts-rs-macros", "uuid", ] @@ -4981,6 +6854,26 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" +[[package]] +name = "uds_windows" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89daebc3e6fd160ac4aa9fc8b3bf71e1f74fbf92367ae71fb83a037e8bf164b9" +dependencies = [ + "memoffset 0.9.1", + "tempfile", + "winapi", +] + +[[package]] +name = "uname" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b72f89f0ca32e4db1c04e2a72f5345d59796d4866a1ee0609084569f73683dc8" +dependencies = [ + "libc", +] + [[package]] name = "unicase" version = "2.8.1" @@ -5040,6 +6933,19 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" +dependencies = [ + "base64", + "log", + "native-tls", + "once_cell", + "url", +] + [[package]] name = "url" version = "2.5.4" @@ -5079,6 +6985,7 @@ dependencies = [ "getrandom 0.3.3", "js-sys", "serde", + "sha1_smol", "wasm-bindgen", ] @@ -5248,6 +7155,76 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wayland-backend" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673a33c33048a5ade91a6b139580fa174e19fb0d23f396dca9fa15f2e1e49b35" +dependencies = [ + "cc", + "downcast-rs", + "rustix 1.0.8", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66a47e840dc20793f2264eb4b3e4ecb4b75d91c0dd4af04b456128e0bdd449d" +dependencies = [ + "bitflags 2.10.0", + "rustix 1.0.8", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efa790ed75fbfd71283bd2521a1cfdc022aabcc28bdcff00851f9e4ae88d9901" +dependencies = [ + "bitflags 2.10.0", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd94963ed43cf9938a090ca4f7da58eb55325ec8200c3848963e98dc25b78ec" +dependencies = [ + "bitflags 2.10.0", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54cb1e9dc49da91950bdfd8b848c49330536d9d1fb03d4bfec8cae50caa50ae3" +dependencies = [ + "proc-macro2", + "quick-xml 0.37.5", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34949b42822155826b41db8e5d0c1be3a2bd296c747577a43a3e6daefc296142" +dependencies = [ + "pkg-config", +] + [[package]] name = "web-sys" version = "0.3.77" @@ -5258,6 +7235,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webbrowser" version = "1.0.5" @@ -5274,6 +7261,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "webpki-roots" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8983c3ab33d6fb807cfcdad2491c4ea8cbc8ed839181c7dfd9c67c83e261b2" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "weezl" version = "0.1.10" @@ -5329,6 +7325,28 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-link 0.1.3", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core", +] + [[package]] name = "windows-core" version = "0.61.2" @@ -5337,11 +7355,22 @@ checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" dependencies = [ "windows-implement", "windows-interface", - "windows-link", + "windows-link 0.1.3", "windows-result", "windows-strings", ] +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core", + "windows-link 0.1.3", + "windows-threading", +] + [[package]] name = "windows-implement" version = "0.60.0" @@ -5370,13 +7399,29 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" +[[package]] +name = "windows-link" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" + +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core", + "windows-link 0.1.3", +] + [[package]] name = "windows-registry" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b8a9ed28765efc97bbc954883f4e6796c33a06546ebafacbabee9696967499e" dependencies = [ - "windows-link", + "windows-link 0.1.3", "windows-result", "windows-strings", ] @@ -5387,7 +7432,7 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" dependencies = [ - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -5396,7 +7441,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" dependencies = [ - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -5435,6 +7480,15 @@ dependencies = [ "windows-targets 0.53.2", ] +[[package]] +name = "windows-sys" +version = "0.61.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f109e41dd4a3c848907eb83d5a42ea98b3769495597450cf6d153507b166f0f" +dependencies = [ + "windows-link 0.2.0", +] + [[package]] name = "windows-targets" version = "0.42.2" @@ -5497,6 +7551,15 @@ dependencies = [ "windows_x86_64_msvc 0.53.0", ] +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -5730,14 +7793,33 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", +] + +[[package]] +name = "wl-clipboard-rs" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5ff8d0e60065f549fafd9d6cb626203ea64a798186c80d8e7df4f8af56baeb" +dependencies = [ + "libc", + "log", + "os_pipe", + "rustix 0.38.44", + "tempfile", + "thiserror 2.0.17", + "tree_magic_mini", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-protocols-wlr", ] [[package]] name = "writeable" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" [[package]] name = "x11rb" @@ -5756,6 +7838,16 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec107c4503ea0b4a98ef47356329af139c0a4f7750e621cf2973cd3385ebcb3d" +[[package]] +name = "xdg-home" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec1cdab258fb55c0da61328dc52c8764709b249011b2cad0454c72f0bf10a1f6" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "yansi" version = "1.0.1" @@ -5786,6 +7878,68 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zbus" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb97012beadd29e654708a0fdb4c84bc046f537aecfde2c3ee0a9e4b4d48c725" +dependencies = [ + "async-broadcast", + "async-executor", + "async-fs", + "async-io", + "async-lock", + "async-process", + "async-recursion", + "async-task", + "async-trait", + "blocking", + "enumflags2", + "event-listener", + "futures-core", + "futures-sink", + "futures-util", + "hex", + "nix 0.29.0", + "ordered-stream", + "rand 0.8.5", + "serde", + "serde_repr", + "sha1", + "static_assertions", + "tracing", + "uds_windows", + "windows-sys 0.52.0", + "xdg-home", + "zbus_macros", + "zbus_names", + "zvariant", +] + +[[package]] +name = "zbus_macros" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "267db9407081e90bbfa46d841d3cbc60f59c0351838c4bc65199ecd79ab1983e" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.104", + "zvariant_utils", +] + +[[package]] +name = "zbus_names" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b9b1fef7d021261cc16cba64c351d291b715febe0fa10dc3a443ac5a5022e6c" +dependencies = [ + "serde", + "static_assertions", + "zvariant", +] + [[package]] name = "zerocopy" version = "0.8.26" @@ -5829,9 +7983,23 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.8.1" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] [[package]] name = "zerotrie" @@ -5846,10 +8014,11 @@ dependencies = [ [[package]] name = "zerovec" -version = "0.11.2" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a05eb080e015ba39cc9e23bbe5e7fb04d5fb040350f99f34e338d5fdd294428" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" dependencies = [ + "serde", "yoke", "zerofrom", "zerovec-derive", @@ -5880,3 +8049,40 @@ checksum = "2c9e525af0a6a658e031e95f14b7f889976b74a11ba0eca5a5fc9ac8a1c43a6a" dependencies = [ "zune-core", ] + +[[package]] +name = "zvariant" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2084290ab9a1c471c38fc524945837734fbf124487e105daec2bb57fd48c81fe" +dependencies = [ + "endi", + "enumflags2", + "serde", + "static_assertions", + "zvariant_derive", +] + +[[package]] +name = "zvariant_derive" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73e2ba546bda683a90652bac4a279bc146adad1386f25379cf73200d2002c449" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.104", + "zvariant_utils", +] + +[[package]] +name = "zvariant_utils" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 1502e62f2f..b19bf76608 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -1,23 +1,46 @@ [workspace] members = [ + "backend-client", "ansi-escape", + "async-utils", + "app-server", + "app-server-protocol", + "app-server-test-client", "apply-patch", "arg0", + "feedback", + "codex-backend-openapi-models", + "cloud-tasks", + "cloud-tasks-client", "cli", "common", "core", "exec", "execpolicy", + "execpolicy2", + "keyring-store", "file-search", "linux-sandbox", + "lmstudio", "login", - "mcp-client", "mcp-server", "mcp-types", "ollama", + "process-hardening", "protocol", - "protocol-ts", + "rmcp-client", + "responses-api-proxy", + "stdio-to-uds", + "otel", "tui", + "utils/git", + "utils/cache", + "utils/image", + "utils/json-to-toml", + "utils/pty", + "utils/readiness", + "utils/string", + "utils/tokenizer", ] resolver = "2" @@ -29,15 +52,220 @@ version = "0.0.0" # edition. edition = "2024" +[workspace.dependencies] +# Internal +app_test_support = { path = "app-server/tests/common" } +codex-ansi-escape = { path = "ansi-escape" } +codex-app-server = { path = "app-server" } +codex-app-server-protocol = { path = "app-server-protocol" } +codex-apply-patch = { path = "apply-patch" } +codex-arg0 = { path = "arg0" } +codex-async-utils = { path = "async-utils" } +codex-backend-client = { path = "backend-client" } +codex-chatgpt = { path = "chatgpt" } +codex-common = { path = "common" } +codex-core = { path = "core" } +codex-exec = { path = "exec" } +codex-feedback = { path = "feedback" } +codex-file-search = { path = "file-search" } +codex-git = { path = "utils/git" } +codex-keyring-store = { path = "keyring-store" } +codex-linux-sandbox = { path = "linux-sandbox" } +codex-lmstudio = { path = "lmstudio" } +codex-login = { path = "login" } +codex-mcp-server = { path = "mcp-server" } +codex-ollama = { path = "ollama" } +codex-otel = { path = "otel" } +codex-process-hardening = { path = "process-hardening" } +codex-protocol = { path = "protocol" } +codex-responses-api-proxy = { path = "responses-api-proxy" } +codex-rmcp-client = { path = "rmcp-client" } +codex-stdio-to-uds = { path = "stdio-to-uds" } +codex-tui = { path = "tui" } +codex-utils-cache = { path = "utils/cache" } +codex-utils-image = { path = "utils/image" } +codex-utils-json-to-toml = { path = "utils/json-to-toml" } +codex-utils-pty = { path = "utils/pty" } +codex-utils-readiness = { path = "utils/readiness" } +codex-utils-string = { path = "utils/string" } +codex-utils-tokenizer = { path = "utils/tokenizer" } +codex-windows-sandbox = { path = "windows-sandbox-rs" } +core_test_support = { path = "core/tests/common" } +mcp-types = { path = "mcp-types" } +mcp_test_support = { path = "mcp-server/tests/common" } + +# External +allocative = "0.3.3" +ansi-to-tui = "7.0.0" +anyhow = "1" +arboard = { version = "3", features = ["wayland-data-control"] } +askama = "0.14" +assert_cmd = "2" +assert_matches = "1.5.0" +async-channel = "2.3.1" +async-stream = "0.3.6" +async-trait = "0.1.89" +axum = { version = "0.8", default-features = false } +base64 = "0.22.1" +bytes = "1.10.1" +chrono = "0.4.42" +clap = "4" +clap_complete = "4" +color-eyre = "0.6.3" +crossterm = "0.28.1" +ctor = "0.5.0" +derive_more = "2" +diffy = "0.4.2" +dirs = "6" +dotenvy = "0.15.7" +dunce = "1.0.4" +env-flags = "0.1.1" +env_logger = "0.11.5" +escargot = "0.5" +eventsource-stream = "0.2.3" +futures = { version = "0.3", default-features = false } +http = "1.3.1" +icu_decimal = "2.1" +icu_locale_core = "2.1" +icu_provider = { version = "2.1", features = ["sync"] } +ignore = "0.4.23" +image = { version = "^0.25.8", default-features = false } +indexmap = "2.12.0" +insta = "1.43.2" +itertools = "0.14.0" +keyring = { version = "3.6", default-features = false } +landlock = "0.4.1" +lazy_static = "1" +libc = "0.2.175" +log = "0.4" +lru = "0.12.5" +maplit = "1.0.2" +mime_guess = "2.0.5" +multimap = "0.10.0" +notify = "8.2.0" +nucleo-matcher = "0.3.1" +once_cell = "1" +openssl-sys = "*" +opentelemetry = "0.30.0" +opentelemetry-appender-tracing = "0.30.0" +opentelemetry-otlp = "0.30.0" +opentelemetry-semantic-conventions = "0.30.0" +opentelemetry_sdk = "0.30.0" +os_info = "3.12.0" +owo-colors = "4.2.0" +path-absolutize = "3.1.1" +pathdiff = "0.2" +portable-pty = "0.9.0" +predicates = "3" +pretty_assertions = "1.4.1" +pulldown-cmark = "0.10" +rand = "0.9" +ratatui = "0.29.0" +ratatui-macros = "0.6.0" +regex-lite = "0.1.7" +reqwest = "0.12" +rmcp = { version = "0.8.5", default-features = false } +schemars = "0.8.22" +seccompiler = "0.5.0" +sentry = "0.34.0" +serde = "1" +serde_json = "1" +serde_with = "3.14" +serial_test = "3.2.0" +sha1 = "0.10.6" +sha2 = "0.10" +shlex = "1.3.0" +similar = "2.7.0" +starlark = "0.13.0" +strum = "0.27.2" +strum_macros = "0.27.2" +supports-color = "3.0.2" +sys-locale = "0.3.2" +tempfile = "3.23.0" +test-log = "0.2.18" +textwrap = "0.16.2" +thiserror = "2.0.17" +tiktoken-rs = "0.9" +time = "0.3" +tiny_http = "0.12" +tokio = "1" +tokio-stream = "0.1.17" +tokio-test = "0.4" +tokio-util = "0.7.16" +toml = "0.9.5" +toml_edit = "0.23.4" +tonic = "0.13.1" +tracing = "0.1.41" +tracing-appender = "0.2.3" +tracing-subscriber = "0.3.20" +tracing-test = "0.2.5" +tree-sitter = "0.25.10" +tree-sitter-bash = "0.25" +tree-sitter-highlight = "0.25.10" +ts-rs = "11" +uds_windows = "1.1.0" +unicode-segmentation = "1.12.0" +unicode-width = "0.2" +url = "2" +urlencoding = "2.1" +uuid = "1" +vt100 = "0.16.2" +walkdir = "2.5.0" +webbrowser = "1.0" +which = "6" +wildmatch = "2.5.0" + +wiremock = "0.6" +zeroize = "1.8.2" + [workspace.lints] rust = {} [workspace.lints.clippy] expect_used = "deny" +identity_op = "deny" +manual_clamp = "deny" +manual_filter = "deny" +manual_find = "deny" +manual_flatten = "deny" +manual_map = "deny" +manual_memcpy = "deny" +manual_non_exhaustive = "deny" +manual_ok_or = "deny" +manual_range_contains = "deny" +manual_retain = "deny" +manual_strip = "deny" +manual_try_fold = "deny" +manual_unwrap_or = "deny" +needless_borrow = "deny" +needless_borrowed_reference = "deny" +needless_collect = "deny" +needless_late_init = "deny" +needless_option_as_deref = "deny" +needless_question_mark = "deny" +needless_update = "deny" redundant_clone = "deny" +redundant_closure = "deny" +redundant_closure_for_method_calls = "deny" +redundant_static_lifetimes = "deny" +trivially_copy_pass_by_ref = "deny" uninlined_format_args = "deny" +unnecessary_filter_map = "deny" +unnecessary_lazy_evaluations = "deny" +unnecessary_sort_by = "deny" +unnecessary_to_owned = "deny" unwrap_used = "deny" +# cargo-shear cannot see the platform-specific openssl-sys usage, so we +# silence the false positive here instead of deleting a real dependency. +[workspace.metadata.cargo-shear] +ignored = [ + "icu_provider", + "openssl-sys", + "codex-utils-readiness", + "codex-utils-tokenizer", +] + [profile.release] lto = "fat" # Because we bundle some of these executables with the TypeScript CLI, we @@ -47,6 +275,16 @@ strip = "symbols" # See https://github.com/openai/codex/issues/1411 for details. codegen-units = 1 +[profile.ci-test] +debug = 1 # Reduce debug symbol size +inherits = "test" +opt-level = 0 + [patch.crates-io] +# Uncomment to debug local changes. # ratatui = { path = "../../ratatui" } +crossterm = { git = "https://github.com/nornagon/crossterm", branch = "nornagon/color-query" } ratatui = { git = "https://github.com/nornagon/ratatui", branch = "nornagon-v0.29.0-patch" } + +# Uncomment to debug local changes. +# rmcp = { path = "../../rust-sdk/crates/rmcp" } diff --git a/codex-rs/README.md b/codex-rs/README.md index 043d872afe..385b4c62e5 100644 --- a/codex-rs/README.md +++ b/codex-rs/README.md @@ -4,18 +4,23 @@ We provide Codex CLI as a standalone, native executable to ensure a zero-depende ## Installing Codex -Today, the easiest way to install Codex is via `npm`, though we plan to publish Codex to other package managers soon. +Today, the easiest way to install Codex is via `npm`: ```shell -npm i -g @openai/codex@native +npm i -g @openai/codex codex ``` -You can also download a platform-specific release directly from our [GitHub Releases](https://github.com/openai/codex/releases). +You can also install via Homebrew (`brew install --cask codex`) or download a platform-specific release directly from our [GitHub Releases](https://github.com/openai/codex/releases). + +## Documentation quickstart + +- First run with Codex? Follow the walkthrough in [`docs/getting-started.md`](../docs/getting-started.md) for prompts, keyboard shortcuts, and session management. +- Already shipping with Codex and want deeper control? Jump to [`docs/advanced.md`](../docs/advanced.md) and the configuration reference at [`docs/config.md`](../docs/config.md). ## What's new in the Rust CLI -While we are [working to close the gap between the TypeScript and Rust implementations of Codex CLI](https://github.com/openai/codex/issues/1262), note that the Rust CLI has a number of features that the TypeScript CLI does not! +The Rust implementation is now the maintained Codex CLI and serves as the default experience. It includes a number of features that the legacy TypeScript CLI never supported. ### Config @@ -23,14 +28,22 @@ Codex supports a rich set of configuration options. Note that the Rust CLI uses ### Model Context Protocol Support -Codex CLI functions as an MCP client that can connect to MCP servers on startup. See the [`mcp_servers`](../docs/config.md#mcp_servers) section in the configuration documentation for details. +#### MCP client -It is still experimental, but you can also launch Codex as an MCP _server_ by running `codex mcp`. Use the [`@modelcontextprotocol/inspector`](https://github.com/modelcontextprotocol/inspector) to try it out: +Codex CLI functions as an MCP client that allows the Codex CLI and IDE extension to connect to MCP servers on startup. See the [`configuration documentation`](../docs/config.md#mcp_servers) for details. + +#### MCP server (experimental) + +Codex can be launched as an MCP _server_ by running `codex mcp-server`. This allows _other_ MCP clients to use Codex as a tool for another agent. + +Use the [`@modelcontextprotocol/inspector`](https://github.com/modelcontextprotocol/inspector) to try it out: ```shell -npx @modelcontextprotocol/inspector codex mcp +npx @modelcontextprotocol/inspector codex mcp-server ``` +Use `codex mcp` to add/list/get/remove MCP server launchers defined in `config.toml`, and `codex mcp-server` to run the MCP server directly. + ### Notifications You can enable notifications by configuring a script that is run whenever the agent finishes a turn. The [notify documentation](../docs/config.md#notify) includes a detailed example that explains how to get desktop notifications via [terminal-notifier](https://github.com/julienXX/terminal-notifier) on macOS. @@ -39,39 +52,22 @@ You can enable notifications by configuring a script that is run whenever the ag To run Codex non-interactively, run `codex exec PROMPT` (you can also pass the prompt via `stdin`) and Codex will work on your task until it decides that it is done and exits. Output is printed to the terminal directly. You can set the `RUST_LOG` environment variable to see more about what's going on. -### Use `@` for file search - -Typing `@` triggers a fuzzy-filename search over the workspace root. Use up/down to select among the results and Tab or Enter to replace the `@` with the selected path. You can use Esc to cancel the search. - -### Esc–Esc to edit a previous message - -When the chat composer is empty, press Esc to prime “backtrack” mode. Press Esc again to open a transcript preview highlighting the last user message; press Esc repeatedly to step to older user messages. Press Enter to confirm and Codex will fork the conversation from that point, trim the visible transcript accordingly, and pre‑fill the composer with the selected user message so you can edit and resubmit it. - -In the transcript preview, the footer shows an `Esc edit prev` hint while editing is active. - -### `--cd`/`-C` flag - -Sometimes it is not convenient to `cd` to the directory you want Codex to use as the "working root" before running Codex. Fortunately, `codex` supports a `--cd` option so you can specify whatever folder you want. You can confirm that Codex is honoring `--cd` by double-checking the **workdir** it reports in the TUI at the start of a new session. - -### Shell completions - -Generate shell completion scripts via: - -```shell -codex completion bash -codex completion zsh -codex completion fish -``` - ### Experimenting with the Codex Sandbox To test to see what happens when a command is run under the sandbox provided by Codex, we provide the following subcommands in Codex CLI: ``` # macOS -codex debug seatbelt [--full-auto] [COMMAND]... +codex sandbox macos [--full-auto] [--log-denials] [COMMAND]... # Linux +codex sandbox linux [--full-auto] [COMMAND]... + +# Windows +codex sandbox windows [--full-auto] [COMMAND]... + +# Legacy aliases +codex debug seatbelt [--full-auto] [--log-denials] [COMMAND]... codex debug landlock [--full-auto] [COMMAND]... ``` diff --git a/codex-rs/ansi-escape/Cargo.toml b/codex-rs/ansi-escape/Cargo.toml index ada675380d..4107a72754 100644 --- a/codex-rs/ansi-escape/Cargo.toml +++ b/codex-rs/ansi-escape/Cargo.toml @@ -8,9 +8,9 @@ name = "codex_ansi_escape" path = "src/lib.rs" [dependencies] -ansi-to-tui = "7.0.0" -ratatui = { version = "0.29.0", features = [ +ansi-to-tui = { workspace = true } +ratatui = { workspace = true, features = [ "unstable-rendered-line-info", "unstable-widget-ref", ] } -tracing = { version = "0.1.41", features = ["log"] } +tracing = { workspace = true, features = ["log"] } diff --git a/codex-rs/ansi-escape/src/lib.rs b/codex-rs/ansi-escape/src/lib.rs index 68ea5e9aa9..b47cf14f8e 100644 --- a/codex-rs/ansi-escape/src/lib.rs +++ b/codex-rs/ansi-escape/src/lib.rs @@ -3,11 +3,30 @@ use ansi_to_tui::IntoText; use ratatui::text::Line; use ratatui::text::Text; +// Expand tabs in a best-effort way for transcript rendering. +// Tabs can interact poorly with left-gutter prefixes in our TUI and CLI +// transcript views (e.g., `nl` separates line numbers from content with a tab). +// Replacing tabs with spaces avoids odd visual artifacts without changing +// semantics for our use cases. +fn expand_tabs(s: &str) -> std::borrow::Cow<'_, str> { + if s.contains('\t') { + // Keep it simple: replace each tab with 4 spaces. + // We do not try to align to tab stops since most usages (like `nl`) + // look acceptable with a fixed substitution and this avoids stateful math + // across spans. + std::borrow::Cow::Owned(s.replace('\t', " ")) + } else { + std::borrow::Cow::Borrowed(s) + } +} + /// This function should be used when the contents of `s` are expected to match /// a single line. If multiple lines are found, a warning is logged and only the /// first line is returned. pub fn ansi_escape_line(s: &str) -> Line<'static> { - let text = ansi_escape(s); + // Normalize tabs to spaces to avoid odd gutter collisions in transcript mode. + let s = expand_tabs(s); + let text = ansi_escape(&s); match text.lines.as_slice() { [] => "".into(), [only] => only.clone(), diff --git a/codex-rs/app-server-protocol/Cargo.toml b/codex-rs/app-server-protocol/Cargo.toml new file mode 100644 index 0000000000..4d1afadaa1 --- /dev/null +++ b/codex-rs/app-server-protocol/Cargo.toml @@ -0,0 +1,27 @@ +[package] +edition = "2024" +name = "codex-app-server-protocol" +version = { workspace = true } + +[lib] +name = "codex_app_server_protocol" +path = "src/lib.rs" + +[lints] +workspace = true + +[dependencies] +anyhow = { workspace = true } +clap = { workspace = true, features = ["derive"] } +codex-protocol = { workspace = true } +mcp-types = { workspace = true } +schemars = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +strum_macros = { workspace = true } +ts-rs = { workspace = true } +uuid = { workspace = true, features = ["serde", "v7"] } + +[dev-dependencies] +anyhow = { workspace = true } +pretty_assertions = { workspace = true } diff --git a/codex-rs/protocol-ts/src/main.rs b/codex-rs/app-server-protocol/src/bin/export.rs similarity index 50% rename from codex-rs/protocol-ts/src/main.rs rename to codex-rs/app-server-protocol/src/bin/export.rs index f477b9f5a6..d029ecbffa 100644 --- a/codex-rs/protocol-ts/src/main.rs +++ b/codex-rs/app-server-protocol/src/bin/export.rs @@ -3,18 +3,20 @@ use clap::Parser; use std::path::PathBuf; #[derive(Parser, Debug)] -#[command(about = "Generate TypeScript bindings for the Codex protocol")] +#[command( + about = "Generate TypeScript bindings and JSON Schemas for the Codex app-server protocol" +)] struct Args { - /// Output directory where .ts files will be written + /// Output directory where generated files will be written #[arg(short = 'o', long = "out", value_name = "DIR")] out_dir: PathBuf, - /// Optional path to the Prettier executable to format generated files + /// Optional Prettier executable path to format generated TypeScript files #[arg(short = 'p', long = "prettier", value_name = "PRETTIER_BIN")] prettier: Option, } fn main() -> Result<()> { let args = Args::parse(); - codex_protocol_ts::generate_ts(&args.out_dir, args.prettier.as_deref()) + codex_app_server_protocol::generate_types(&args.out_dir, args.prettier.as_deref()) } diff --git a/codex-rs/app-server-protocol/src/export.rs b/codex-rs/app-server-protocol/src/export.rs new file mode 100644 index 0000000000..11296e8e54 --- /dev/null +++ b/codex-rs/app-server-protocol/src/export.rs @@ -0,0 +1,901 @@ +use crate::ClientNotification; +use crate::ClientRequest; +use crate::ServerNotification; +use crate::ServerRequest; +use crate::export_client_notification_schemas; +use crate::export_client_param_schemas; +use crate::export_client_response_schemas; +use crate::export_client_responses; +use crate::export_server_notification_schemas; +use crate::export_server_param_schemas; +use crate::export_server_response_schemas; +use crate::export_server_responses; +use anyhow::Context; +use anyhow::Result; +use anyhow::anyhow; +use codex_protocol::protocol::EventMsg; +use schemars::JsonSchema; +use schemars::schema_for; +use serde::Serialize; +use serde_json::Map; +use serde_json::Value; +use std::collections::HashMap; +use std::collections::HashSet; +use std::ffi::OsStr; +use std::fs; +use std::io::Read; +use std::io::Write; +use std::path::Path; +use std::path::PathBuf; +use std::process::Command; +use ts_rs::TS; + +const HEADER: &str = "// GENERATED CODE! DO NOT MODIFY BY HAND!\n\n"; + +#[derive(Clone)] +pub struct GeneratedSchema { + namespace: Option, + logical_name: String, + value: Value, + in_v1_dir: bool, +} + +impl GeneratedSchema { + fn namespace(&self) -> Option<&str> { + self.namespace.as_deref() + } + + fn logical_name(&self) -> &str { + &self.logical_name + } + + fn value(&self) -> &Value { + &self.value + } +} + +type JsonSchemaEmitter = fn(&Path) -> Result; +pub fn generate_types(out_dir: &Path, prettier: Option<&Path>) -> Result<()> { + generate_ts(out_dir, prettier)?; + generate_json(out_dir)?; + Ok(()) +} + +pub fn generate_ts(out_dir: &Path, prettier: Option<&Path>) -> Result<()> { + let v2_out_dir = out_dir.join("v2"); + ensure_dir(out_dir)?; + ensure_dir(&v2_out_dir)?; + + ClientRequest::export_all_to(out_dir)?; + export_client_responses(out_dir)?; + ClientNotification::export_all_to(out_dir)?; + + ServerRequest::export_all_to(out_dir)?; + export_server_responses(out_dir)?; + ServerNotification::export_all_to(out_dir)?; + + generate_index_ts(out_dir)?; + generate_index_ts(&v2_out_dir)?; + + // Ensure our header is present on all TS files (root + subdirs like v2/). + let ts_files = ts_files_in_recursive(out_dir)?; + for file in &ts_files { + prepend_header_if_missing(file)?; + } + + // Optionally run Prettier on all generated TS files. + if let Some(prettier_bin) = prettier + && !ts_files.is_empty() + { + let status = Command::new(prettier_bin) + .arg("--write") + .arg("--log-level") + .arg("warn") + .args(ts_files.iter().map(|p| p.as_os_str())) + .status() + .with_context(|| format!("Failed to invoke Prettier at {}", prettier_bin.display()))?; + if !status.success() { + return Err(anyhow!("Prettier failed with status {status}")); + } + } + + Ok(()) +} + +pub fn generate_json(out_dir: &Path) -> Result<()> { + ensure_dir(out_dir)?; + let envelope_emitters: &[JsonSchemaEmitter] = &[ + |d| write_json_schema_with_return::(d, "RequestId"), + |d| write_json_schema_with_return::(d, "JSONRPCMessage"), + |d| write_json_schema_with_return::(d, "JSONRPCRequest"), + |d| write_json_schema_with_return::(d, "JSONRPCNotification"), + |d| write_json_schema_with_return::(d, "JSONRPCResponse"), + |d| write_json_schema_with_return::(d, "JSONRPCError"), + |d| write_json_schema_with_return::(d, "JSONRPCErrorError"), + |d| write_json_schema_with_return::(d, "ClientRequest"), + |d| write_json_schema_with_return::(d, "ServerRequest"), + |d| write_json_schema_with_return::(d, "ClientNotification"), + |d| write_json_schema_with_return::(d, "ServerNotification"), + |d| write_json_schema_with_return::(d, "EventMsg"), + ]; + + let mut schemas: Vec = Vec::new(); + for emit in envelope_emitters { + schemas.push(emit(out_dir)?); + } + + schemas.extend(export_client_param_schemas(out_dir)?); + schemas.extend(export_client_response_schemas(out_dir)?); + schemas.extend(export_server_param_schemas(out_dir)?); + schemas.extend(export_server_response_schemas(out_dir)?); + schemas.extend(export_client_notification_schemas(out_dir)?); + schemas.extend(export_server_notification_schemas(out_dir)?); + + let bundle = build_schema_bundle(schemas)?; + write_pretty_json( + out_dir.join("codex_app_server_protocol.schemas.json"), + &bundle, + )?; + + Ok(()) +} + +fn build_schema_bundle(schemas: Vec) -> Result { + const SPECIAL_DEFINITIONS: &[&str] = &[ + "ClientNotification", + "ClientRequest", + "EventMsg", + "ServerNotification", + "ServerRequest", + ]; + const IGNORED_DEFINITIONS: &[&str] = &["Option<()>"]; + + let namespaced_types = collect_namespaced_types(&schemas); + let mut definitions = Map::new(); + + for schema in schemas { + let GeneratedSchema { + namespace, + logical_name, + mut value, + in_v1_dir, + } = schema; + + if IGNORED_DEFINITIONS.contains(&logical_name.as_str()) { + continue; + } + + if let Some(ref ns) = namespace { + rewrite_refs_to_namespace(&mut value, ns); + } + + let mut forced_namespace_refs: Vec<(String, String)> = Vec::new(); + if let Value::Object(ref mut obj) = value + && let Some(defs) = obj.remove("definitions") + && let Value::Object(defs_obj) = defs + { + for (def_name, mut def_schema) in defs_obj { + if IGNORED_DEFINITIONS.contains(&def_name.as_str()) { + continue; + } + if SPECIAL_DEFINITIONS.contains(&def_name.as_str()) { + continue; + } + annotate_schema(&mut def_schema, Some(def_name.as_str())); + let target_namespace = match namespace { + Some(ref ns) => Some(ns.clone()), + None => namespace_for_definition(&def_name, &namespaced_types) + .cloned() + .filter(|_| !in_v1_dir), + }; + if let Some(ref ns) = target_namespace { + if namespace.as_deref() == Some(ns.as_str()) { + rewrite_refs_to_namespace(&mut def_schema, ns); + insert_into_namespace(&mut definitions, ns, def_name.clone(), def_schema)?; + } else if !forced_namespace_refs + .iter() + .any(|(name, existing_ns)| name == &def_name && existing_ns == ns) + { + forced_namespace_refs.push((def_name.clone(), ns.clone())); + } + } else { + definitions.insert(def_name, def_schema); + } + } + } + + for (name, ns) in forced_namespace_refs { + rewrite_named_ref_to_namespace(&mut value, &ns, &name); + } + + if let Some(ref ns) = namespace { + insert_into_namespace(&mut definitions, ns, logical_name.clone(), value)?; + } else { + definitions.insert(logical_name, value); + } + } + + let mut root = Map::new(); + root.insert( + "$schema".to_string(), + Value::String("http://json-schema.org/draft-07/schema#".into()), + ); + root.insert( + "title".to_string(), + Value::String("CodexAppServerProtocol".into()), + ); + root.insert("type".to_string(), Value::String("object".into())); + root.insert("definitions".to_string(), Value::Object(definitions)); + + Ok(Value::Object(root)) +} + +fn insert_into_namespace( + definitions: &mut Map, + namespace: &str, + name: String, + schema: Value, +) -> Result<()> { + let entry = definitions + .entry(namespace.to_string()) + .or_insert_with(|| Value::Object(Map::new())); + match entry { + Value::Object(map) => { + map.insert(name, schema); + Ok(()) + } + _ => Err(anyhow!("expected namespace {namespace} to be an object")), + } +} + +fn write_json_schema_with_return(out_dir: &Path, name: &str) -> Result +where + T: JsonSchema, +{ + let file_stem = name.trim(); + let schema = schema_for!(T); + let mut schema_value = serde_json::to_value(schema)?; + annotate_schema(&mut schema_value, Some(file_stem)); + // If the name looks like a namespaced path (e.g., "v2::Type"), mirror + // the TypeScript layout and write to out_dir/v2/Type.json. Otherwise + // write alongside the legacy files. + let (raw_namespace, logical_name) = split_namespace(file_stem); + let out_path = if let Some(ns) = raw_namespace { + let dir = out_dir.join(ns); + ensure_dir(&dir)?; + dir.join(format!("{logical_name}.json")) + } else { + out_dir.join(format!("{file_stem}.json")) + }; + + write_pretty_json(out_path, &schema_value) + .with_context(|| format!("Failed to write JSON schema for {file_stem}"))?; + let namespace = match raw_namespace { + Some("v1") | None => None, + Some(ns) => Some(ns.to_string()), + }; + Ok(GeneratedSchema { + in_v1_dir: raw_namespace == Some("v1"), + namespace, + logical_name: logical_name.to_string(), + value: schema_value, + }) +} + +pub(crate) fn write_json_schema(out_dir: &Path, name: &str) -> Result +where + T: JsonSchema, +{ + write_json_schema_with_return::(out_dir, name) +} + +fn write_pretty_json(path: PathBuf, value: &impl Serialize) -> Result<()> { + let json = serde_json::to_vec_pretty(value) + .with_context(|| format!("Failed to serialize JSON schema to {}", path.display()))?; + fs::write(&path, json).with_context(|| format!("Failed to write {}", path.display()))?; + Ok(()) +} + +/// Split a fully-qualified type name like "v2::Type" into its namespace and logical name. +fn split_namespace(name: &str) -> (Option<&str>, &str) { + name.split_once("::") + .map_or((None, name), |(ns, rest)| (Some(ns), rest)) +} + +/// Recursively rewrite $ref values that point at "#/definitions/..." so that +/// they point to a namespaced location under the bundle. +fn rewrite_refs_to_namespace(value: &mut Value, ns: &str) { + match value { + Value::Object(obj) => { + if let Some(Value::String(r)) = obj.get_mut("$ref") + && let Some(suffix) = r.strip_prefix("#/definitions/") + { + let prefix = format!("{ns}/"); + if !suffix.starts_with(&prefix) { + *r = format!("#/definitions/{ns}/{suffix}"); + } + } + for v in obj.values_mut() { + rewrite_refs_to_namespace(v, ns); + } + } + Value::Array(items) => { + for v in items.iter_mut() { + rewrite_refs_to_namespace(v, ns); + } + } + _ => {} + } +} + +fn collect_namespaced_types(schemas: &[GeneratedSchema]) -> HashMap { + let mut types = HashMap::new(); + for schema in schemas { + if let Some(ns) = schema.namespace() { + types + .entry(schema.logical_name().to_string()) + .or_insert_with(|| ns.to_string()); + if let Some(Value::Object(defs)) = schema.value().get("definitions") { + for key in defs.keys() { + types.entry(key.clone()).or_insert_with(|| ns.to_string()); + } + } + if let Some(Value::Object(defs)) = schema.value().get("$defs") { + for key in defs.keys() { + types.entry(key.clone()).or_insert_with(|| ns.to_string()); + } + } + } + } + types +} + +fn namespace_for_definition<'a>( + name: &str, + types: &'a HashMap, +) -> Option<&'a String> { + if let Some(ns) = types.get(name) { + return Some(ns); + } + let trimmed = name.trim_end_matches(|c: char| c.is_ascii_digit()); + if trimmed != name { + return types.get(trimmed); + } + None +} + +fn variant_definition_name(base: &str, variant: &Value) -> Option { + if let Some(props) = variant.get("properties").and_then(Value::as_object) { + if let Some(method_literal) = literal_from_property(props, "method") { + let pascal = to_pascal_case(method_literal); + return Some(match base { + "ClientRequest" | "ServerRequest" => format!("{pascal}Request"), + "ClientNotification" | "ServerNotification" => format!("{pascal}Notification"), + _ => format!("{pascal}{base}"), + }); + } + + if let Some(type_literal) = literal_from_property(props, "type") { + let pascal = to_pascal_case(type_literal); + return Some(match base { + "EventMsg" => format!("{pascal}EventMsg"), + _ => format!("{pascal}{base}"), + }); + } + + if props.len() == 1 + && let Some(key) = props.keys().next() + { + let pascal = to_pascal_case(key); + return Some(format!("{pascal}{base}")); + } + } + + if let Some(required) = variant.get("required").and_then(Value::as_array) + && required.len() == 1 + && let Some(key) = required[0].as_str() + { + let pascal = to_pascal_case(key); + return Some(format!("{pascal}{base}")); + } + + None +} + +fn literal_from_property<'a>(props: &'a Map, key: &str) -> Option<&'a str> { + props.get(key).and_then(string_literal) +} + +fn string_literal(value: &Value) -> Option<&str> { + value.get("const").and_then(Value::as_str).or_else(|| { + value + .get("enum") + .and_then(Value::as_array) + .and_then(|arr| arr.first()) + .and_then(Value::as_str) + }) +} + +fn annotate_schema(value: &mut Value, base: Option<&str>) { + match value { + Value::Object(map) => annotate_object(map, base), + Value::Array(items) => { + for item in items { + annotate_schema(item, base); + } + } + _ => {} + } +} + +fn annotate_object(map: &mut Map, base: Option<&str>) { + let owner = map.get("title").and_then(Value::as_str).map(str::to_owned); + if let Some(owner) = owner.as_deref() + && let Some(Value::Object(props)) = map.get_mut("properties") + { + set_discriminator_titles(props, owner); + } + + if let Some(Value::Array(variants)) = map.get_mut("oneOf") { + annotate_variant_list(variants, base); + } + if let Some(Value::Array(variants)) = map.get_mut("anyOf") { + annotate_variant_list(variants, base); + } + + if let Some(Value::Object(defs)) = map.get_mut("definitions") { + for (name, schema) in defs.iter_mut() { + annotate_schema(schema, Some(name.as_str())); + } + } + + if let Some(Value::Object(defs)) = map.get_mut("$defs") { + for (name, schema) in defs.iter_mut() { + annotate_schema(schema, Some(name.as_str())); + } + } + + if let Some(Value::Object(props)) = map.get_mut("properties") { + for value in props.values_mut() { + annotate_schema(value, base); + } + } + + if let Some(items) = map.get_mut("items") { + annotate_schema(items, base); + } + + if let Some(additional) = map.get_mut("additionalProperties") { + annotate_schema(additional, base); + } + + for (key, child) in map.iter_mut() { + match key.as_str() { + "oneOf" + | "anyOf" + | "definitions" + | "$defs" + | "properties" + | "items" + | "additionalProperties" => {} + _ => annotate_schema(child, base), + } + } +} + +fn annotate_variant_list(variants: &mut [Value], base: Option<&str>) { + let mut seen = HashSet::new(); + + for variant in variants.iter() { + if let Some(name) = variant_title(variant) { + seen.insert(name.to_owned()); + } + } + + for variant in variants.iter_mut() { + let mut variant_name = variant_title(variant).map(str::to_owned); + + if variant_name.is_none() + && let Some(base_name) = base + && let Some(name) = variant_definition_name(base_name, variant) + { + let mut candidate = name.clone(); + let mut index = 2; + while seen.contains(&candidate) { + candidate = format!("{name}{index}"); + index += 1; + } + if let Some(obj) = variant.as_object_mut() { + obj.insert("title".into(), Value::String(candidate.clone())); + } + seen.insert(candidate.clone()); + variant_name = Some(candidate); + } + + if let Some(name) = variant_name.as_deref() + && let Some(obj) = variant.as_object_mut() + && let Some(Value::Object(props)) = obj.get_mut("properties") + { + set_discriminator_titles(props, name); + } + + annotate_schema(variant, base); + } +} + +const DISCRIMINATOR_KEYS: &[&str] = &["type", "method", "mode", "status", "role", "reason"]; + +fn set_discriminator_titles(props: &mut Map, owner: &str) { + for key in DISCRIMINATOR_KEYS { + if let Some(prop_schema) = props.get_mut(*key) + && string_literal(prop_schema).is_some() + && let Value::Object(prop_obj) = prop_schema + { + if prop_obj.contains_key("title") { + continue; + } + let suffix = to_pascal_case(key); + prop_obj.insert("title".into(), Value::String(format!("{owner}{suffix}"))); + } + } +} + +fn variant_title(value: &Value) -> Option<&str> { + value + .as_object() + .and_then(|obj| obj.get("title")) + .and_then(Value::as_str) +} + +fn to_pascal_case(input: &str) -> String { + let mut result = String::new(); + let mut capitalize_next = true; + + for c in input.chars() { + if c == '_' || c == '-' { + capitalize_next = true; + continue; + } + + if capitalize_next { + result.extend(c.to_uppercase()); + capitalize_next = false; + } else { + result.push(c); + } + } + + result +} + +fn ensure_dir(dir: &Path) -> Result<()> { + fs::create_dir_all(dir) + .with_context(|| format!("Failed to create output directory {}", dir.display())) +} + +fn rewrite_named_ref_to_namespace(value: &mut Value, ns: &str, name: &str) { + let direct = format!("#/definitions/{name}"); + let prefixed = format!("{direct}/"); + let replacement = format!("#/definitions/{ns}/{name}"); + let replacement_prefixed = format!("{replacement}/"); + match value { + Value::Object(obj) => { + if let Some(Value::String(reference)) = obj.get_mut("$ref") { + if reference == &direct { + *reference = replacement; + } else if let Some(rest) = reference.strip_prefix(&prefixed) { + *reference = format!("{replacement_prefixed}{rest}"); + } + } + for child in obj.values_mut() { + rewrite_named_ref_to_namespace(child, ns, name); + } + } + Value::Array(items) => { + for child in items { + rewrite_named_ref_to_namespace(child, ns, name); + } + } + _ => {} + } +} + +fn prepend_header_if_missing(path: &Path) -> Result<()> { + let mut content = String::new(); + { + let mut f = fs::File::open(path) + .with_context(|| format!("Failed to open {} for reading", path.display()))?; + f.read_to_string(&mut content) + .with_context(|| format!("Failed to read {}", path.display()))?; + } + + if content.starts_with(HEADER) { + return Ok(()); + } + + let mut f = fs::File::create(path) + .with_context(|| format!("Failed to open {} for writing", path.display()))?; + f.write_all(HEADER.as_bytes()) + .with_context(|| format!("Failed to write header to {}", path.display()))?; + f.write_all(content.as_bytes()) + .with_context(|| format!("Failed to write content to {}", path.display()))?; + Ok(()) +} + +fn ts_files_in(dir: &Path) -> Result> { + let mut files = Vec::new(); + for entry in + fs::read_dir(dir).with_context(|| format!("Failed to read dir {}", dir.display()))? + { + let entry = entry?; + let path = entry.path(); + if path.is_file() && path.extension() == Some(OsStr::new("ts")) { + files.push(path); + } + } + files.sort(); + Ok(files) +} + +fn ts_files_in_recursive(dir: &Path) -> Result> { + let mut files = Vec::new(); + let mut stack = vec![dir.to_path_buf()]; + while let Some(d) = stack.pop() { + for entry in + fs::read_dir(&d).with_context(|| format!("Failed to read dir {}", d.display()))? + { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + stack.push(path); + } else if path.is_file() && path.extension() == Some(OsStr::new("ts")) { + files.push(path); + } + } + } + files.sort(); + Ok(files) +} + +/// Generate an index.ts file that re-exports all generated types. +/// This allows consumers to import all types from a single file. +fn generate_index_ts(out_dir: &Path) -> Result { + let mut entries: Vec = Vec::new(); + let mut stems: Vec = ts_files_in(out_dir)? + .into_iter() + .filter_map(|p| { + let stem = p.file_stem()?.to_string_lossy().into_owned(); + if stem == "index" { None } else { Some(stem) } + }) + .collect(); + stems.sort(); + stems.dedup(); + + for name in stems { + entries.push(format!("export type {{ {name} }} from \"./{name}\";\n")); + } + + // If this is the root out_dir and a ./v2 folder exists with TS files, + // expose it as a namespace to avoid symbol collisions at the root. + let v2_dir = out_dir.join("v2"); + let has_v2_ts = ts_files_in(&v2_dir).map(|v| !v.is_empty()).unwrap_or(false); + if has_v2_ts { + entries.push("export * as v2 from \"./v2\";\n".to_string()); + } + + let mut content = + String::with_capacity(HEADER.len() + entries.iter().map(String::len).sum::()); + content.push_str(HEADER); + for line in &entries { + content.push_str(line); + } + + let index_path = out_dir.join("index.ts"); + let mut f = fs::File::create(&index_path) + .with_context(|| format!("Failed to create {}", index_path.display()))?; + f.write_all(content.as_bytes()) + .with_context(|| format!("Failed to write {}", index_path.display()))?; + Ok(index_path) +} + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::Result; + use std::collections::BTreeSet; + use std::fs; + use std::path::PathBuf; + use uuid::Uuid; + + #[test] + fn generated_ts_has_no_optional_nullable_fields() -> Result<()> { + // Assert that there are no types of the form "?: T | null" in the generated TS files. + let output_dir = std::env::temp_dir().join(format!("codex_ts_types_{}", Uuid::now_v7())); + fs::create_dir(&output_dir)?; + + struct TempDirGuard(PathBuf); + + impl Drop for TempDirGuard { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + let _guard = TempDirGuard(output_dir.clone()); + + generate_ts(&output_dir, None)?; + + let mut undefined_offenders = Vec::new(); + let mut optional_nullable_offenders = BTreeSet::new(); + let mut stack = vec![output_dir]; + while let Some(dir) = stack.pop() { + for entry in fs::read_dir(&dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + stack.push(path); + continue; + } + + if matches!(path.extension().and_then(|ext| ext.to_str()), Some("ts")) { + let contents = fs::read_to_string(&path)?; + if contents.contains("| undefined") { + undefined_offenders.push(path.clone()); + } + + const SKIP_PREFIXES: &[&str] = &[ + "const ", + "let ", + "var ", + "export const ", + "export let ", + "export var ", + ]; + + let mut search_start = 0; + while let Some(idx) = contents[search_start..].find("| null") { + let abs_idx = search_start + idx; + // Find the property-colon for this field by scanning forward + // from the start of the segment and ignoring nested braces, + // brackets, and parens. This avoids colons inside nested + // type literals like `{ [k in string]?: string }`. + + let line_start_idx = + contents[..abs_idx].rfind('\n').map(|i| i + 1).unwrap_or(0); + + let mut segment_start_idx = line_start_idx; + if let Some(rel_idx) = contents[line_start_idx..abs_idx].rfind(',') { + segment_start_idx = segment_start_idx.max(line_start_idx + rel_idx + 1); + } + if let Some(rel_idx) = contents[line_start_idx..abs_idx].rfind('{') { + segment_start_idx = segment_start_idx.max(line_start_idx + rel_idx + 1); + } + if let Some(rel_idx) = contents[line_start_idx..abs_idx].rfind('}') { + segment_start_idx = segment_start_idx.max(line_start_idx + rel_idx + 1); + } + + // Scan forward for the colon that separates the field name from its type. + let mut level_brace = 0_i32; + let mut level_brack = 0_i32; + let mut level_paren = 0_i32; + let mut in_single = false; + let mut in_double = false; + let mut escape = false; + let mut prop_colon_idx = None; + for (i, ch) in contents[segment_start_idx..abs_idx].char_indices() { + let idx_abs = segment_start_idx + i; + if escape { + escape = false; + continue; + } + match ch { + '\\' => { + // Only treat as escape when inside a string. + if in_single || in_double { + escape = true; + } + } + '\'' => { + if !in_double { + in_single = !in_single; + } + } + '"' => { + if !in_single { + in_double = !in_double; + } + } + '{' if !in_single && !in_double => level_brace += 1, + '}' if !in_single && !in_double => level_brace -= 1, + '[' if !in_single && !in_double => level_brack += 1, + ']' if !in_single && !in_double => level_brack -= 1, + '(' if !in_single && !in_double => level_paren += 1, + ')' if !in_single && !in_double => level_paren -= 1, + ':' if !in_single + && !in_double + && level_brace == 0 + && level_brack == 0 + && level_paren == 0 => + { + prop_colon_idx = Some(idx_abs); + break; + } + _ => {} + } + } + + let Some(colon_idx) = prop_colon_idx else { + search_start = abs_idx + 5; + continue; + }; + + let mut field_prefix = contents[segment_start_idx..colon_idx].trim(); + if field_prefix.is_empty() { + search_start = abs_idx + 5; + continue; + } + + if let Some(comment_idx) = field_prefix.rfind("*/") { + field_prefix = field_prefix[comment_idx + 2..].trim_start(); + } + + if field_prefix.is_empty() { + search_start = abs_idx + 5; + continue; + } + + if SKIP_PREFIXES + .iter() + .any(|prefix| field_prefix.starts_with(prefix)) + { + search_start = abs_idx + 5; + continue; + } + + if field_prefix.contains('(') { + search_start = abs_idx + 5; + continue; + } + + // If the last non-whitespace before ':' is '?', then this is an + // optional field with a nullable type (i.e., "?: T | null"), + // which we explicitly disallow. + if field_prefix.chars().rev().find(|c| !c.is_whitespace()) == Some('?') { + let line_number = + contents[..abs_idx].chars().filter(|c| *c == '\n').count() + 1; + let offending_line_end = contents[line_start_idx..] + .find('\n') + .map(|i| line_start_idx + i) + .unwrap_or(contents.len()); + let offending_snippet = + contents[line_start_idx..offending_line_end].trim(); + + optional_nullable_offenders.insert(format!( + "{}:{}: {offending_snippet}", + path.display(), + line_number + )); + } + + search_start = abs_idx + 5; + } + } + } + } + + assert!( + undefined_offenders.is_empty(), + "Generated TypeScript still includes unions with `undefined` in {undefined_offenders:?}" + ); + + // If this assertion fails, it means a field was generated as + // "?: T | null" — i.e., both optional (undefined) and nullable (null). + // We only want either "?: T" or ": T | null". + assert!( + optional_nullable_offenders.is_empty(), + "Generated TypeScript has optional fields with nullable types (disallowed '?: T | null'), add #[ts(optional)] to fix:\n{optional_nullable_offenders:?}" + ); + + Ok(()) + } +} diff --git a/codex-rs/app-server-protocol/src/jsonrpc_lite.rs b/codex-rs/app-server-protocol/src/jsonrpc_lite.rs new file mode 100644 index 0000000000..65ffa2a005 --- /dev/null +++ b/codex-rs/app-server-protocol/src/jsonrpc_lite.rs @@ -0,0 +1,71 @@ +//! We do not do true JSON-RPC 2.0, as we neither send nor expect the +//! "jsonrpc": "2.0" field. + +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; +use ts_rs::TS; + +pub const JSONRPC_VERSION: &str = "2.0"; + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq, JsonSchema, TS)] +#[serde(untagged)] +pub enum RequestId { + String(String), + #[ts(type = "number")] + Integer(i64), +} + +pub type Result = serde_json::Value; + +/// Refers to any valid JSON-RPC object that can be decoded off the wire, or encoded to be sent. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +#[serde(untagged)] +pub enum JSONRPCMessage { + Request(JSONRPCRequest), + Notification(JSONRPCNotification), + Response(JSONRPCResponse), + Error(JSONRPCError), +} + +/// A request that expects a response. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct JSONRPCRequest { + pub id: RequestId, + pub method: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub params: Option, +} + +/// A notification which does not expect a response. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct JSONRPCNotification { + pub method: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub params: Option, +} + +/// A successful (non-error) response to a request. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct JSONRPCResponse { + pub id: RequestId, + pub result: Result, +} + +/// A response to a request that indicates an error occurred. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct JSONRPCError { + pub error: JSONRPCErrorError, + pub id: RequestId, +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)] +pub struct JSONRPCErrorError { + pub code: i64, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub data: Option, + pub message: String, +} diff --git a/codex-rs/app-server-protocol/src/lib.rs b/codex-rs/app-server-protocol/src/lib.rs new file mode 100644 index 0000000000..9c02ea9246 --- /dev/null +++ b/codex-rs/app-server-protocol/src/lib.rs @@ -0,0 +1,11 @@ +mod export; +mod jsonrpc_lite; +mod protocol; + +pub use export::generate_json; +pub use export::generate_ts; +pub use export::generate_types; +pub use jsonrpc_lite::*; +pub use protocol::common::*; +pub use protocol::v1::*; +pub use protocol::v2::*; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs new file mode 100644 index 0000000000..db9bed1112 --- /dev/null +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -0,0 +1,782 @@ +use std::path::Path; + +use crate::JSONRPCNotification; +use crate::JSONRPCRequest; +use crate::RequestId; +use crate::export::GeneratedSchema; +use crate::export::write_json_schema; +use crate::protocol::v1; +use crate::protocol::v2; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; +use strum_macros::Display; +use ts_rs::TS; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, TS)] +#[ts(type = "string")] +pub struct GitSha(pub String); + +impl GitSha { + pub fn new(sha: &str) -> Self { + Self(sha.to_string()) + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Display, JsonSchema, TS)] +#[serde(rename_all = "lowercase")] +pub enum AuthMode { + ApiKey, + ChatGPT, +} + +/// Generates an `enum ClientRequest` where each variant is a request that the +/// client can send to the server. Each variant has associated `params` and +/// `response` types. Also generates a `export_client_responses()` function to +/// export all response types to TypeScript. +macro_rules! client_request_definitions { + ( + $( + $(#[$variant_meta:meta])* + $variant:ident $(=> $wire:literal)? { + params: $(#[$params_meta:meta])* $params:ty, + response: $response:ty, + } + ),* $(,)? + ) => { + /// Request from the client to the server. + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] + #[serde(tag = "method", rename_all = "camelCase")] + pub enum ClientRequest { + $( + $(#[$variant_meta])* + $(#[serde(rename = $wire)] #[ts(rename = $wire)])? + $variant { + #[serde(rename = "id")] + request_id: RequestId, + $(#[$params_meta])* + params: $params, + }, + )* + } + + pub fn export_client_responses( + out_dir: &::std::path::Path, + ) -> ::std::result::Result<(), ::ts_rs::ExportError> { + $( + <$response as ::ts_rs::TS>::export_all_to(out_dir)?; + )* + Ok(()) + } + + #[allow(clippy::vec_init_then_push)] + pub fn export_client_response_schemas( + out_dir: &::std::path::Path, + ) -> ::anyhow::Result> { + let mut schemas = Vec::new(); + $( + schemas.push(write_json_schema::<$response>(out_dir, stringify!($response))?); + )* + Ok(schemas) + } + + #[allow(clippy::vec_init_then_push)] + pub fn export_client_param_schemas( + out_dir: &::std::path::Path, + ) -> ::anyhow::Result> { + let mut schemas = Vec::new(); + $( + schemas.push(write_json_schema::<$params>(out_dir, stringify!($params))?); + )* + Ok(schemas) + } + }; +} + +client_request_definitions! { + Initialize { + params: v1::InitializeParams, + response: v1::InitializeResponse, + }, + + /// NEW APIs + // Thread lifecycle + ThreadStart => "thread/start" { + params: v2::ThreadStartParams, + response: v2::ThreadStartResponse, + }, + ThreadResume => "thread/resume" { + params: v2::ThreadResumeParams, + response: v2::ThreadResumeResponse, + }, + ThreadArchive => "thread/archive" { + params: v2::ThreadArchiveParams, + response: v2::ThreadArchiveResponse, + }, + ThreadList => "thread/list" { + params: v2::ThreadListParams, + response: v2::ThreadListResponse, + }, + ThreadCompact => "thread/compact" { + params: v2::ThreadCompactParams, + response: v2::ThreadCompactResponse, + }, + TurnStart => "turn/start" { + params: v2::TurnStartParams, + response: v2::TurnStartResponse, + }, + TurnInterrupt => "turn/interrupt" { + params: v2::TurnInterruptParams, + response: v2::TurnInterruptResponse, + }, + + ModelList => "model/list" { + params: v2::ModelListParams, + response: v2::ModelListResponse, + }, + + LoginAccount => "account/login/start" { + params: v2::LoginAccountParams, + response: v2::LoginAccountResponse, + }, + + CancelLoginAccount => "account/login/cancel" { + params: v2::CancelLoginAccountParams, + response: v2::CancelLoginAccountResponse, + }, + + LogoutAccount => "account/logout" { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v2::LogoutAccountResponse, + }, + + GetAccountRateLimits => "account/rateLimits/read" { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v2::GetAccountRateLimitsResponse, + }, + + FeedbackUpload => "feedback/upload" { + params: v2::FeedbackUploadParams, + response: v2::FeedbackUploadResponse, + }, + + GetAccount => "account/read" { + params: v2::GetAccountParams, + response: v2::GetAccountResponse, + }, + + /// DEPRECATED APIs below + NewConversation { + params: v1::NewConversationParams, + response: v1::NewConversationResponse, + }, + GetConversationSummary { + params: v1::GetConversationSummaryParams, + response: v1::GetConversationSummaryResponse, + }, + /// List recorded Codex conversations (rollouts) with optional pagination and search. + ListConversations { + params: v1::ListConversationsParams, + response: v1::ListConversationsResponse, + }, + /// Resume a recorded Codex conversation from a rollout file. + ResumeConversation { + params: v1::ResumeConversationParams, + response: v1::ResumeConversationResponse, + }, + ArchiveConversation { + params: v1::ArchiveConversationParams, + response: v1::ArchiveConversationResponse, + }, + SendUserMessage { + params: v1::SendUserMessageParams, + response: v1::SendUserMessageResponse, + }, + SendUserTurn { + params: v1::SendUserTurnParams, + response: v1::SendUserTurnResponse, + }, + InterruptConversation { + params: v1::InterruptConversationParams, + response: v1::InterruptConversationResponse, + }, + AddConversationListener { + params: v1::AddConversationListenerParams, + response: v1::AddConversationSubscriptionResponse, + }, + RemoveConversationListener { + params: v1::RemoveConversationListenerParams, + response: v1::RemoveConversationSubscriptionResponse, + }, + GitDiffToRemote { + params: v1::GitDiffToRemoteParams, + response: v1::GitDiffToRemoteResponse, + }, + LoginApiKey { + params: v1::LoginApiKeyParams, + response: v1::LoginApiKeyResponse, + }, + LoginChatGpt { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v1::LoginChatGptResponse, + }, + // DEPRECATED in favor of CancelLoginAccount + CancelLoginChatGpt { + params: v1::CancelLoginChatGptParams, + response: v1::CancelLoginChatGptResponse, + }, + LogoutChatGpt { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v1::LogoutChatGptResponse, + }, + /// DEPRECATED in favor of GetAccount + GetAuthStatus { + params: v1::GetAuthStatusParams, + response: v1::GetAuthStatusResponse, + }, + GetUserSavedConfig { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v1::GetUserSavedConfigResponse, + }, + SetDefaultModel { + params: v1::SetDefaultModelParams, + response: v1::SetDefaultModelResponse, + }, + GetUserAgent { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v1::GetUserAgentResponse, + }, + UserInfo { + params: #[ts(type = "undefined")] #[serde(skip_serializing_if = "Option::is_none")] Option<()>, + response: v1::UserInfoResponse, + }, + FuzzyFileSearch { + params: FuzzyFileSearchParams, + response: FuzzyFileSearchResponse, + }, + /// Execute a command (argv vector) under the server's sandbox. + ExecOneOffCommand { + params: v1::ExecOneOffCommandParams, + response: v1::ExecOneOffCommandResponse, + }, +} + +/// Generates an `enum ServerRequest` where each variant is a request that the +/// server can send to the client along with the corresponding params and +/// response types. It also generates helper types used by the app/server +/// infrastructure (payload enum, request constructor, and export helpers). +macro_rules! server_request_definitions { + ( + $( + $(#[$variant_meta:meta])* + $variant:ident $(=> $wire:literal)? { + params: $params:ty, + response: $response:ty, + } + ),* $(,)? + ) => { + /// Request initiated from the server and sent to the client. + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] + #[serde(tag = "method", rename_all = "camelCase")] + pub enum ServerRequest { + $( + $(#[$variant_meta])* + $(#[serde(rename = $wire)] #[ts(rename = $wire)])? + $variant { + #[serde(rename = "id")] + request_id: RequestId, + params: $params, + }, + )* + } + + #[derive(Debug, Clone, PartialEq, JsonSchema)] + pub enum ServerRequestPayload { + $( $variant($params), )* + } + + impl ServerRequestPayload { + pub fn request_with_id(self, request_id: RequestId) -> ServerRequest { + match self { + $(Self::$variant(params) => ServerRequest::$variant { request_id, params },)* + } + } + } + + pub fn export_server_responses( + out_dir: &::std::path::Path, + ) -> ::std::result::Result<(), ::ts_rs::ExportError> { + $( + <$response as ::ts_rs::TS>::export_all_to(out_dir)?; + )* + Ok(()) + } + + #[allow(clippy::vec_init_then_push)] + pub fn export_server_response_schemas( + out_dir: &Path, + ) -> ::anyhow::Result> { + let mut schemas = Vec::new(); + $( + schemas.push(crate::export::write_json_schema::<$response>( + out_dir, + concat!(stringify!($variant), "Response"), + )?); + )* + Ok(schemas) + } + + #[allow(clippy::vec_init_then_push)] + pub fn export_server_param_schemas( + out_dir: &Path, + ) -> ::anyhow::Result> { + let mut schemas = Vec::new(); + $( + schemas.push(crate::export::write_json_schema::<$params>( + out_dir, + concat!(stringify!($variant), "Params"), + )?); + )* + Ok(schemas) + } + }; +} + +/// Generates `ServerNotification` enum and helpers, including a JSON Schema +/// exporter for each notification. +macro_rules! server_notification_definitions { + ( + $( + $(#[$variant_meta:meta])* + $variant:ident $(=> $wire:literal)? ( $payload:ty ) + ),* $(,)? + ) => { + /// Notification sent from the server to the client. + #[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS, Display)] + #[serde(tag = "method", content = "params", rename_all = "camelCase")] + #[strum(serialize_all = "camelCase")] + pub enum ServerNotification { + $( + $(#[$variant_meta])* + $(#[serde(rename = $wire)] #[ts(rename = $wire)] #[strum(serialize = $wire)])? + $variant($payload), + )* + } + + impl ServerNotification { + pub fn to_params(self) -> Result { + match self { + $(Self::$variant(params) => serde_json::to_value(params),)* + } + } + } + + impl TryFrom for ServerNotification { + type Error = serde_json::Error; + + fn try_from(value: JSONRPCNotification) -> Result { + serde_json::from_value(serde_json::to_value(value)?) + } + } + + #[allow(clippy::vec_init_then_push)] + pub fn export_server_notification_schemas( + out_dir: &::std::path::Path, + ) -> ::anyhow::Result> { + let mut schemas = Vec::new(); + $(schemas.push(crate::export::write_json_schema::<$payload>(out_dir, stringify!($payload))?);)* + Ok(schemas) + } + }; +} +/// Notifications sent from the client to the server. +macro_rules! client_notification_definitions { + ( + $( + $(#[$variant_meta:meta])* + $variant:ident $( ( $payload:ty ) )? + ),* $(,)? + ) => { + #[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS, Display)] + #[serde(tag = "method", content = "params", rename_all = "camelCase")] + #[strum(serialize_all = "camelCase")] + pub enum ClientNotification { + $( + $(#[$variant_meta])* + $variant $( ( $payload ) )?, + )* + } + + pub fn export_client_notification_schemas( + _out_dir: &::std::path::Path, + ) -> ::anyhow::Result> { + let schemas = Vec::new(); + $( $(schemas.push(crate::export::write_json_schema::<$payload>(_out_dir, stringify!($payload))?);)? )* + Ok(schemas) + } + }; +} + +impl TryFrom for ServerRequest { + type Error = serde_json::Error; + + fn try_from(value: JSONRPCRequest) -> Result { + serde_json::from_value(serde_json::to_value(value)?) + } +} + +server_request_definitions! { + /// NEW APIs + /// Sent when approval is requested for a specific command execution. + /// This request is used for Turns started via turn/start. + CommandExecutionRequestApproval => "item/commandExecution/requestApproval" { + params: v2::CommandExecutionRequestApprovalParams, + response: v2::CommandExecutionRequestApprovalResponse, + }, + + /// DEPRECATED APIs below + /// Request to approve a patch. + /// This request is used for Turns started via the legacy APIs (i.e. SendUserTurn, SendUserMessage). + ApplyPatchApproval { + params: v1::ApplyPatchApprovalParams, + response: v1::ApplyPatchApprovalResponse, + }, + /// Request to exec a command. + /// This request is used for Turns started via the legacy APIs (i.e. SendUserTurn, SendUserMessage). + ExecCommandApproval { + params: v1::ExecCommandApprovalParams, + response: v1::ExecCommandApprovalResponse, + }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(rename_all = "camelCase")] +pub struct FuzzyFileSearchParams { + pub query: String, + pub roots: Vec, + // if provided, will cancel any previous request that used the same value + pub cancellation_token: Option, +} + +/// Superset of [`codex_file_search::FileMatch`] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +pub struct FuzzyFileSearchResult { + pub root: String, + pub path: String, + pub file_name: String, + pub score: u32, + pub indices: Option>, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +pub struct FuzzyFileSearchResponse { + pub files: Vec, +} + +server_notification_definitions! { + /// NEW NOTIFICATIONS + ThreadStarted => "thread/started" (v2::ThreadStartedNotification), + TurnStarted => "turn/started" (v2::TurnStartedNotification), + TurnCompleted => "turn/completed" (v2::TurnCompletedNotification), + ItemStarted => "item/started" (v2::ItemStartedNotification), + ItemCompleted => "item/completed" (v2::ItemCompletedNotification), + AgentMessageDelta => "item/agentMessage/delta" (v2::AgentMessageDeltaNotification), + CommandExecutionOutputDelta => "item/commandExecution/outputDelta" (v2::CommandExecutionOutputDeltaNotification), + McpToolCallProgress => "item/mcpToolCall/progress" (v2::McpToolCallProgressNotification), + AccountUpdated => "account/updated" (v2::AccountUpdatedNotification), + AccountRateLimitsUpdated => "account/rateLimits/updated" (v2::AccountRateLimitsUpdatedNotification), + ReasoningSummaryTextDelta => "item/reasoning/summaryTextDelta" (v2::ReasoningSummaryTextDeltaNotification), + ReasoningSummaryPartAdded => "item/reasoning/summaryPartAdded" (v2::ReasoningSummaryPartAddedNotification), + ReasoningTextDelta => "item/reasoning/textDelta" (v2::ReasoningTextDeltaNotification), + + #[serde(rename = "account/login/completed")] + #[ts(rename = "account/login/completed")] + #[strum(serialize = "account/login/completed")] + AccountLoginCompleted(v2::AccountLoginCompletedNotification), + + /// DEPRECATED NOTIFICATIONS below + AuthStatusChange(v1::AuthStatusChangeNotification), + + /// Deprecated: use `account/login/completed` instead. + LoginChatGptComplete(v1::LoginChatGptCompleteNotification), + SessionConfigured(v1::SessionConfiguredNotification), +} + +client_notification_definitions! { + Initialized, +} + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::Result; + use codex_protocol::ConversationId; + use codex_protocol::account::PlanType; + use codex_protocol::parse_command::ParsedCommand; + use codex_protocol::protocol::AskForApproval; + use pretty_assertions::assert_eq; + use serde_json::json; + use std::path::PathBuf; + + #[test] + fn serialize_new_conversation() -> Result<()> { + let request = ClientRequest::NewConversation { + request_id: RequestId::Integer(42), + params: v1::NewConversationParams { + model: Some("gpt-5.1-codex".to_string()), + model_provider: None, + profile: None, + cwd: None, + approval_policy: Some(AskForApproval::OnRequest), + sandbox: None, + config: None, + base_instructions: None, + developer_instructions: None, + compact_prompt: None, + include_apply_patch_tool: None, + }, + }; + assert_eq!( + json!({ + "method": "newConversation", + "id": 42, + "params": { + "model": "gpt-5.1-codex", + "modelProvider": null, + "profile": null, + "cwd": null, + "approvalPolicy": "on-request", + "sandbox": null, + "config": null, + "baseInstructions": null, + "includeApplyPatchTool": null + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn conversation_id_serializes_as_plain_string() -> Result<()> { + let id = ConversationId::from_string("67e55044-10b1-426f-9247-bb680e5fe0c8")?; + + assert_eq!( + json!("67e55044-10b1-426f-9247-bb680e5fe0c8"), + serde_json::to_value(id)? + ); + Ok(()) + } + + #[test] + fn conversation_id_deserializes_from_plain_string() -> Result<()> { + let id: ConversationId = + serde_json::from_value(json!("67e55044-10b1-426f-9247-bb680e5fe0c8"))?; + + assert_eq!( + ConversationId::from_string("67e55044-10b1-426f-9247-bb680e5fe0c8")?, + id, + ); + Ok(()) + } + + #[test] + fn serialize_client_notification() -> Result<()> { + let notification = ClientNotification::Initialized; + // Note there is no "params" field for this notification. + assert_eq!( + json!({ + "method": "initialized", + }), + serde_json::to_value(¬ification)?, + ); + Ok(()) + } + + #[test] + fn serialize_server_request() -> Result<()> { + let conversation_id = ConversationId::from_string("67e55044-10b1-426f-9247-bb680e5fe0c8")?; + let params = v1::ExecCommandApprovalParams { + conversation_id, + call_id: "call-42".to_string(), + command: vec!["echo".to_string(), "hello".to_string()], + cwd: PathBuf::from("/tmp"), + reason: Some("because tests".to_string()), + risk: None, + parsed_cmd: vec![ParsedCommand::Unknown { + cmd: "echo hello".to_string(), + }], + }; + let request = ServerRequest::ExecCommandApproval { + request_id: RequestId::Integer(7), + params: params.clone(), + }; + + assert_eq!( + json!({ + "method": "execCommandApproval", + "id": 7, + "params": { + "conversationId": "67e55044-10b1-426f-9247-bb680e5fe0c8", + "callId": "call-42", + "command": ["echo", "hello"], + "cwd": "/tmp", + "reason": "because tests", + "risk": null, + "parsedCmd": [ + { + "type": "unknown", + "cmd": "echo hello" + } + ] + } + }), + serde_json::to_value(&request)?, + ); + + let payload = ServerRequestPayload::ExecCommandApproval(params); + assert_eq!(payload.request_with_id(RequestId::Integer(7)), request); + Ok(()) + } + + #[test] + fn serialize_get_account_rate_limits() -> Result<()> { + let request = ClientRequest::GetAccountRateLimits { + request_id: RequestId::Integer(1), + params: None, + }; + assert_eq!( + json!({ + "method": "account/rateLimits/read", + "id": 1, + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn serialize_account_login_api_key() -> Result<()> { + let request = ClientRequest::LoginAccount { + request_id: RequestId::Integer(2), + params: v2::LoginAccountParams::ApiKey { + api_key: "secret".to_string(), + }, + }; + assert_eq!( + json!({ + "method": "account/login/start", + "id": 2, + "params": { + "type": "apiKey", + "apiKey": "secret" + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn serialize_account_login_chatgpt() -> Result<()> { + let request = ClientRequest::LoginAccount { + request_id: RequestId::Integer(3), + params: v2::LoginAccountParams::Chatgpt, + }; + assert_eq!( + json!({ + "method": "account/login/start", + "id": 3, + "params": { + "type": "chatgpt" + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn serialize_account_logout() -> Result<()> { + let request = ClientRequest::LogoutAccount { + request_id: RequestId::Integer(4), + params: None, + }; + assert_eq!( + json!({ + "method": "account/logout", + "id": 4, + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn serialize_get_account() -> Result<()> { + let request = ClientRequest::GetAccount { + request_id: RequestId::Integer(5), + params: v2::GetAccountParams { + refresh_token: false, + }, + }; + assert_eq!( + json!({ + "method": "account/read", + "id": 5, + "params": { + "refreshToken": false + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + + #[test] + fn account_serializes_fields_in_camel_case() -> Result<()> { + let api_key = v2::Account::ApiKey {}; + assert_eq!( + json!({ + "type": "apiKey", + }), + serde_json::to_value(&api_key)?, + ); + + let chatgpt = v2::Account::Chatgpt { + email: "user@example.com".to_string(), + plan_type: PlanType::Plus, + }; + assert_eq!( + json!({ + "type": "chatgpt", + "email": "user@example.com", + "planType": "plus", + }), + serde_json::to_value(&chatgpt)?, + ); + + Ok(()) + } + + #[test] + fn serialize_list_models() -> Result<()> { + let request = ClientRequest::ModelList { + request_id: RequestId::Integer(6), + params: v2::ModelListParams::default(), + }; + assert_eq!( + json!({ + "method": "model/list", + "id": 6, + "params": { + "limit": null, + "cursor": null + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } +} diff --git a/codex-rs/app-server-protocol/src/protocol/mod.rs b/codex-rs/app-server-protocol/src/protocol/mod.rs new file mode 100644 index 0000000000..11edf04cc1 --- /dev/null +++ b/codex-rs/app-server-protocol/src/protocol/mod.rs @@ -0,0 +1,6 @@ +// Module declarations for the app-server protocol namespace. +// Exposes protocol pieces used by `lib.rs` via `pub use protocol::common::*;`. + +pub mod common; +pub mod v1; +pub mod v2; diff --git a/codex-rs/app-server-protocol/src/protocol/v1.rs b/codex-rs/app-server-protocol/src/protocol/v1.rs new file mode 100644 index 0000000000..54f80c9fd4 --- /dev/null +++ b/codex-rs/app-server-protocol/src/protocol/v1.rs @@ -0,0 +1,462 @@ +use std::collections::HashMap; +use std::path::PathBuf; + +use codex_protocol::ConversationId; +use codex_protocol::config_types::ForcedLoginMethod; +use codex_protocol::config_types::ReasoningEffort; +use codex_protocol::config_types::ReasoningSummary; +use codex_protocol::config_types::SandboxMode; +use codex_protocol::config_types::Verbosity; +use codex_protocol::models::ResponseItem; +use codex_protocol::parse_command::ParsedCommand; +use codex_protocol::protocol::AskForApproval; +use codex_protocol::protocol::EventMsg; +use codex_protocol::protocol::FileChange; +use codex_protocol::protocol::ReviewDecision; +use codex_protocol::protocol::SandboxCommandAssessment; +use codex_protocol::protocol::SandboxPolicy; +use codex_protocol::protocol::SessionSource; +use codex_protocol::protocol::TurnAbortReason; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; +use ts_rs::TS; +use uuid::Uuid; + +// Reuse shared types defined in `common.rs`. +use crate::protocol::common::AuthMode; +use crate::protocol::common::GitSha; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct InitializeParams { + pub client_info: ClientInfo, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ClientInfo { + pub name: String, + pub title: Option, + pub version: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct InitializeResponse { + pub user_agent: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct NewConversationParams { + pub model: Option, + pub model_provider: Option, + pub profile: Option, + pub cwd: Option, + pub approval_policy: Option, + pub sandbox: Option, + pub config: Option>, + pub base_instructions: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub developer_instructions: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub compact_prompt: Option, + pub include_apply_patch_tool: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct NewConversationResponse { + pub conversation_id: ConversationId, + pub model: String, + pub reasoning_effort: Option, + pub rollout_path: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ResumeConversationResponse { + pub conversation_id: ConversationId, + pub model: String, + pub initial_messages: Option>, + pub rollout_path: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(untagged)] +pub enum GetConversationSummaryParams { + RolloutPath { + #[serde(rename = "rolloutPath")] + rollout_path: PathBuf, + }, + ConversationId { + #[serde(rename = "conversationId")] + conversation_id: ConversationId, + }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GetConversationSummaryResponse { + pub summary: ConversationSummary, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ListConversationsParams { + pub page_size: Option, + pub cursor: Option, + pub model_providers: Option>, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ConversationSummary { + pub conversation_id: ConversationId, + pub path: PathBuf, + pub preview: String, + pub timestamp: Option, + pub model_provider: String, + pub cwd: PathBuf, + pub cli_version: String, + pub source: SessionSource, + pub git_info: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +pub struct ConversationGitInfo { + pub sha: Option, + pub branch: Option, + pub origin_url: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ListConversationsResponse { + pub items: Vec, + pub next_cursor: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ResumeConversationParams { + pub path: Option, + pub conversation_id: Option, + pub history: Option>, + pub overrides: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct AddConversationSubscriptionResponse { + #[schemars(with = "String")] + pub subscription_id: Uuid, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ArchiveConversationParams { + pub conversation_id: ConversationId, + pub rollout_path: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ArchiveConversationResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct RemoveConversationSubscriptionResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct LoginApiKeyParams { + pub api_key: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct LoginApiKeyResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct LoginChatGptResponse { + #[schemars(with = "String")] + pub login_id: Uuid, + pub auth_url: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GitDiffToRemoteResponse { + pub sha: GitSha, + pub diff: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ApplyPatchApprovalParams { + pub conversation_id: ConversationId, + /// Use to correlate this with [codex_core::protocol::PatchApplyBeginEvent] + /// and [codex_core::protocol::PatchApplyEndEvent]. + pub call_id: String, + pub file_changes: HashMap, + /// Optional explanatory reason (e.g. request for extra write access). + pub reason: Option, + /// When set, the agent is asking the user to allow writes under this root + /// for the remainder of the session (unclear if this is honored today). + pub grant_root: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ApplyPatchApprovalResponse { + pub decision: ReviewDecision, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ExecCommandApprovalParams { + pub conversation_id: ConversationId, + /// Use to correlate this with [codex_core::protocol::ExecCommandBeginEvent] + /// and [codex_core::protocol::ExecCommandEndEvent]. + pub call_id: String, + pub command: Vec, + pub cwd: PathBuf, + pub reason: Option, + pub risk: Option, + pub parsed_cmd: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +pub struct ExecCommandApprovalResponse { + pub decision: ReviewDecision, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct CancelLoginChatGptParams { + #[schemars(with = "String")] + pub login_id: Uuid, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GitDiffToRemoteParams { + pub cwd: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct CancelLoginChatGptResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct LogoutChatGptParams {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct LogoutChatGptResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GetAuthStatusParams { + pub include_token: Option, + pub refresh_token: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ExecOneOffCommandParams { + pub command: Vec, + pub timeout_ms: Option, + pub cwd: Option, + pub sandbox_policy: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct ExecOneOffCommandResponse { + pub exit_code: i32, + pub stdout: String, + pub stderr: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GetAuthStatusResponse { + pub auth_method: Option, + pub auth_token: Option, + pub requires_openai_auth: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GetUserAgentResponse { + pub user_agent: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct UserInfoResponse { + pub alleged_user_email: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct GetUserSavedConfigResponse { + pub config: UserSavedConfig, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SetDefaultModelParams { + pub model: Option, + pub reasoning_effort: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SetDefaultModelResponse {} + +#[derive(Deserialize, Debug, Clone, PartialEq, Serialize, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct UserSavedConfig { + pub approval_policy: Option, + pub sandbox_mode: Option, + pub sandbox_settings: Option, + pub forced_chatgpt_workspace_id: Option, + pub forced_login_method: Option, + pub model: Option, + pub model_reasoning_effort: Option, + pub model_reasoning_summary: Option, + pub model_verbosity: Option, + pub tools: Option, + pub profile: Option, + pub profiles: HashMap, +} + +#[derive(Deserialize, Debug, Clone, PartialEq, Serialize, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct Profile { + pub model: Option, + pub model_provider: Option, + pub approval_policy: Option, + pub model_reasoning_effort: Option, + pub model_reasoning_summary: Option, + pub model_verbosity: Option, + pub chatgpt_base_url: Option, +} + +#[derive(Deserialize, Debug, Clone, PartialEq, Serialize, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct Tools { + pub web_search: Option, + pub view_image: Option, +} + +#[derive(Deserialize, Debug, Clone, PartialEq, Serialize, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SandboxSettings { + #[serde(default)] + pub writable_roots: Vec, + pub network_access: Option, + pub exclude_tmpdir_env_var: Option, + pub exclude_slash_tmp: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SendUserMessageParams { + pub conversation_id: ConversationId, + pub items: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SendUserTurnParams { + pub conversation_id: ConversationId, + pub items: Vec, + pub cwd: PathBuf, + pub approval_policy: AskForApproval, + pub sandbox_policy: SandboxPolicy, + pub model: String, + pub effort: Option, + pub summary: ReasoningSummary, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SendUserTurnResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct InterruptConversationParams { + pub conversation_id: ConversationId, +} + +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct InterruptConversationResponse { + pub abort_reason: TurnAbortReason, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SendUserMessageResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct AddConversationListenerParams { + pub conversation_id: ConversationId, + #[serde(default)] + pub experimental_raw_events: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct RemoveConversationListenerParams { + #[schemars(with = "String")] + pub subscription_id: Uuid, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[serde(tag = "type", content = "data")] +pub enum InputItem { + Text { text: String }, + Image { image_url: String }, + LocalImage { path: PathBuf }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +/// Deprecated in favor of AccountLoginCompletedNotification. +pub struct LoginChatGptCompleteNotification { + #[schemars(with = "String")] + pub login_id: Uuid, + pub success: bool, + pub error: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +pub struct SessionConfiguredNotification { + pub session_id: ConversationId, + pub model: String, + pub reasoning_effort: Option, + pub history_log_id: u64, + #[ts(type = "number")] + pub history_entry_count: usize, + pub initial_messages: Option>, + pub rollout_path: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +/// Deprecated notification. Use AccountUpdatedNotification instead. +pub struct AuthStatusChangeNotification { + pub auth_method: Option, +} diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs new file mode 100644 index 0000000000..a2b9cee3fe --- /dev/null +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -0,0 +1,1069 @@ +use std::collections::HashMap; +use std::path::PathBuf; + +use crate::protocol::common::AuthMode; +use codex_protocol::ConversationId; +use codex_protocol::account::PlanType; +use codex_protocol::approvals::SandboxCommandAssessment as CoreSandboxCommandAssessment; +use codex_protocol::config_types::ReasoningEffort; +use codex_protocol::config_types::ReasoningSummary; +use codex_protocol::items::AgentMessageContent as CoreAgentMessageContent; +use codex_protocol::items::TurnItem as CoreTurnItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::parse_command::ParsedCommand as CoreParsedCommand; +use codex_protocol::protocol::RateLimitSnapshot as CoreRateLimitSnapshot; +use codex_protocol::protocol::RateLimitWindow as CoreRateLimitWindow; +use codex_protocol::user_input::UserInput as CoreUserInput; +use mcp_types::ContentBlock as McpContentBlock; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value as JsonValue; +use ts_rs::TS; + +// Macro to declare a camelCased API v2 enum mirroring a core enum which +// tends to use either snake_case or kebab-case. +macro_rules! v2_enum_from_core { + ( + pub enum $Name:ident from $Src:path { $( $Variant:ident ),+ $(,)? } + ) => { + #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] + #[serde(rename_all = "camelCase")] + #[ts(export_to = "v2/")] + pub enum $Name { $( $Variant ),+ } + + impl $Name { + pub fn to_core(self) -> $Src { + match self { $( $Name::$Variant => <$Src>::$Variant ),+ } + } + } + + impl From<$Src> for $Name { + fn from(value: $Src) -> Self { + match value { $( <$Src>::$Variant => $Name::$Variant ),+ } + } + } + }; +} + +v2_enum_from_core!( + pub enum AskForApproval from codex_protocol::protocol::AskForApproval { + UnlessTrusted, OnFailure, OnRequest, Never + } +); + +v2_enum_from_core!( + pub enum SandboxMode from codex_protocol::config_types::SandboxMode { + ReadOnly, WorkspaceWrite, DangerFullAccess + } +); + +v2_enum_from_core!( + pub enum CommandRiskLevel from codex_protocol::approvals::SandboxRiskLevel { + Low, + Medium, + High + } +); + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum ApprovalDecision { + Accept, + Decline, + Cancel, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum SandboxPolicy { + DangerFullAccess, + ReadOnly, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + WorkspaceWrite { + #[serde(default)] + writable_roots: Vec, + #[serde(default)] + network_access: bool, + #[serde(default)] + exclude_tmpdir_env_var: bool, + #[serde(default)] + exclude_slash_tmp: bool, + }, +} + +impl SandboxPolicy { + pub fn to_core(&self) -> codex_protocol::protocol::SandboxPolicy { + match self { + SandboxPolicy::DangerFullAccess => { + codex_protocol::protocol::SandboxPolicy::DangerFullAccess + } + SandboxPolicy::ReadOnly => codex_protocol::protocol::SandboxPolicy::ReadOnly, + SandboxPolicy::WorkspaceWrite { + writable_roots, + network_access, + exclude_tmpdir_env_var, + exclude_slash_tmp, + } => codex_protocol::protocol::SandboxPolicy::WorkspaceWrite { + writable_roots: writable_roots.clone(), + network_access: *network_access, + exclude_tmpdir_env_var: *exclude_tmpdir_env_var, + exclude_slash_tmp: *exclude_slash_tmp, + }, + } + } +} + +impl From for SandboxPolicy { + fn from(value: codex_protocol::protocol::SandboxPolicy) -> Self { + match value { + codex_protocol::protocol::SandboxPolicy::DangerFullAccess => { + SandboxPolicy::DangerFullAccess + } + codex_protocol::protocol::SandboxPolicy::ReadOnly => SandboxPolicy::ReadOnly, + codex_protocol::protocol::SandboxPolicy::WorkspaceWrite { + writable_roots, + network_access, + exclude_tmpdir_env_var, + exclude_slash_tmp, + } => SandboxPolicy::WorkspaceWrite { + writable_roots, + network_access, + exclude_tmpdir_env_var, + exclude_slash_tmp, + }, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct SandboxCommandAssessment { + pub description: String, + pub risk_level: CommandRiskLevel, +} + +impl SandboxCommandAssessment { + pub fn into_core(self) -> CoreSandboxCommandAssessment { + CoreSandboxCommandAssessment { + description: self.description, + risk_level: self.risk_level.to_core(), + } + } +} + +impl From for SandboxCommandAssessment { + fn from(value: CoreSandboxCommandAssessment) -> Self { + Self { + description: value.description, + risk_level: CommandRiskLevel::from(value.risk_level), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum CommandAction { + Read { + command: String, + name: String, + path: PathBuf, + }, + ListFiles { + command: String, + path: Option, + }, + Search { + command: String, + query: Option, + path: Option, + }, + Unknown { + command: String, + }, +} + +impl CommandAction { + pub fn into_core(self) -> CoreParsedCommand { + match self { + CommandAction::Read { + command: cmd, + name, + path, + } => CoreParsedCommand::Read { cmd, name, path }, + CommandAction::ListFiles { command: cmd, path } => { + CoreParsedCommand::ListFiles { cmd, path } + } + CommandAction::Search { + command: cmd, + query, + path, + } => CoreParsedCommand::Search { cmd, query, path }, + CommandAction::Unknown { command: cmd } => CoreParsedCommand::Unknown { cmd }, + } + } +} + +impl From for CommandAction { + fn from(value: CoreParsedCommand) -> Self { + match value { + CoreParsedCommand::Read { cmd, name, path } => CommandAction::Read { + command: cmd, + name, + path, + }, + CoreParsedCommand::ListFiles { cmd, path } => { + CommandAction::ListFiles { command: cmd, path } + } + CoreParsedCommand::Search { cmd, query, path } => CommandAction::Search { + command: cmd, + query, + path, + }, + CoreParsedCommand::Unknown { cmd } => CommandAction::Unknown { command: cmd }, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum Account { + #[serde(rename = "apiKey", rename_all = "camelCase")] + #[ts(rename = "apiKey", rename_all = "camelCase")] + ApiKey {}, + + #[serde(rename = "chatgpt", rename_all = "camelCase")] + #[ts(rename = "chatgpt", rename_all = "camelCase")] + Chatgpt { email: String, plan_type: PlanType }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum LoginAccountParams { + #[serde(rename = "apiKey", rename_all = "camelCase")] + #[ts(rename = "apiKey", rename_all = "camelCase")] + ApiKey { + #[serde(rename = "apiKey")] + #[ts(rename = "apiKey")] + api_key: String, + }, + #[serde(rename = "chatgpt")] + #[ts(rename = "chatgpt")] + Chatgpt, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum LoginAccountResponse { + #[serde(rename = "apiKey", rename_all = "camelCase")] + #[ts(rename = "apiKey", rename_all = "camelCase")] + ApiKey {}, + #[serde(rename = "chatgpt", rename_all = "camelCase")] + #[ts(rename = "chatgpt", rename_all = "camelCase")] + Chatgpt { + // Use plain String for identifiers to avoid TS/JSON Schema quirks around uuid-specific types. + // Convert to/from UUIDs at the application layer as needed. + login_id: String, + /// URL the client should open in a browser to initiate the OAuth flow. + auth_url: String, + }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CancelLoginAccountParams { + pub login_id: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CancelLoginAccountResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct LogoutAccountResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct GetAccountRateLimitsResponse { + pub rate_limits: RateLimitSnapshot, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct GetAccountParams { + #[serde(default)] + pub refresh_token: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct GetAccountResponse { + pub account: Option, + pub requires_openai_auth: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ModelListParams { + /// Opaque pagination cursor returned by a previous call. + pub cursor: Option, + /// Optional page size; defaults to a reasonable server-side value. + pub limit: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct Model { + pub id: String, + pub model: String, + pub display_name: String, + pub description: String, + pub supported_reasoning_efforts: Vec, + pub default_reasoning_effort: ReasoningEffort, + // Only one model should be marked as default. + pub is_default: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ReasoningEffortOption { + pub reasoning_effort: ReasoningEffort, + pub description: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ModelListResponse { + pub data: Vec, + /// Opaque cursor to pass to the next call to continue after the last item. + /// If None, there are no more items to return. + pub next_cursor: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct FeedbackUploadParams { + pub classification: String, + pub reason: Option, + pub conversation_id: Option, + pub include_logs: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct FeedbackUploadResponse { + pub thread_id: String, +} + +// === Threads, Turns, and Items === +// Thread APIs +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadStartParams { + pub model: Option, + pub model_provider: Option, + pub cwd: Option, + pub approval_policy: Option, + pub sandbox: Option, + pub config: Option>, + pub base_instructions: Option, + pub developer_instructions: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadStartResponse { + pub thread: Thread, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +/// There are three ways to resume a thread: +/// 1. By thread_id: load the thread from disk by thread_id and resume it. +/// 2. By history: instantiate the thread from memory and resume it. +/// 3. By path: load the thread from disk by path and resume it. +/// +/// The precedence is: history > path > thread_id. +/// If using history or path, the thread_id param will be ignored. +/// +/// Prefer using thread_id whenever possible. +pub struct ThreadResumeParams { + pub thread_id: String, + + /// [UNSTABLE] FOR CODEX CLOUD - DO NOT USE. + /// If specified, the thread will be resumed with the provided history + /// instead of loaded from disk. + pub history: Option>, + + /// [UNSTABLE] Specify the rollout path to resume from. + /// If specified, the thread_id param will be ignored. + pub path: Option, + + /// Configuration overrides for the resumed thread, if any. + pub model: Option, + pub model_provider: Option, + pub cwd: Option, + pub approval_policy: Option, + pub sandbox: Option, + pub config: Option>, + pub base_instructions: Option, + pub developer_instructions: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadResumeResponse { + pub thread: Thread, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadArchiveParams { + pub thread_id: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadArchiveResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadListParams { + /// Opaque pagination cursor returned by a previous call. + pub cursor: Option, + /// Optional page size; defaults to a reasonable server-side value. + pub limit: Option, + /// Optional provider filter; when set, only sessions recorded under these + /// providers are returned. When present but empty, includes all providers. + pub model_providers: Option>, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadListResponse { + pub data: Vec, + /// Opaque cursor to pass to the next call to continue after the last item. + /// if None, there are no more items to return. + pub next_cursor: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadCompactParams { + pub thread_id: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadCompactResponse {} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct Thread { + pub id: String, + /// Usually the first user message in the thread, if available. + pub preview: String, + pub model_provider: String, + /// Unix timestamp (in seconds) when the thread was created. + pub created_at: i64, + /// [UNSTABLE] Path to the thread on disk. + pub path: PathBuf, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct AccountUpdatedNotification { + pub auth_mode: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct Turn { + pub id: String, + pub items: Vec, + pub status: TurnStatus, + pub error: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnError { + pub message: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum TurnStatus { + Completed, + Interrupted, + Failed, + InProgress, +} + +// Turn APIs +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnStartParams { + pub thread_id: String, + pub input: Vec, + /// Override the working directory for this turn and subsequent turns. + pub cwd: Option, + /// Override the approval policy for this turn and subsequent turns. + pub approval_policy: Option, + /// Override the sandbox policy for this turn and subsequent turns. + pub sandbox_policy: Option, + /// Override the model for this turn and subsequent turns. + pub model: Option, + /// Override the reasoning effort for this turn and subsequent turns. + pub effort: Option, + /// Override the reasoning summary for this turn and subsequent turns. + pub summary: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnStartResponse { + pub turn: Turn, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnInterruptParams { + pub thread_id: String, + pub turn_id: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnInterruptResponse {} + +// User input types +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum UserInput { + Text { text: String }, + Image { url: String }, + LocalImage { path: PathBuf }, +} + +impl UserInput { + pub fn into_core(self) -> CoreUserInput { + match self { + UserInput::Text { text } => CoreUserInput::Text { text }, + UserInput::Image { url } => CoreUserInput::Image { image_url: url }, + UserInput::LocalImage { path } => CoreUserInput::LocalImage { path }, + } + } +} + +impl From for UserInput { + fn from(value: CoreUserInput) -> Self { + match value { + CoreUserInput::Text { text } => UserInput::Text { text }, + CoreUserInput::Image { image_url } => UserInput::Image { url: image_url }, + CoreUserInput::LocalImage { path } => UserInput::LocalImage { path }, + _ => unreachable!("unsupported user input variant"), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type")] +#[ts(export_to = "v2/")] +pub enum ThreadItem { + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + UserMessage { id: String, content: Vec }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + AgentMessage { id: String, text: String }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + Reasoning { + id: String, + #[serde(default)] + summary: Vec, + #[serde(default)] + content: Vec, + }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + CommandExecution { + id: String, + /// The command to be executed. + command: String, + /// The command's working directory. + cwd: PathBuf, + status: CommandExecutionStatus, + /// A best-effort parsing of the command to understand the action(s) it will perform. + /// This returns a list of CommandAction objects because a single shell command may + /// be composed of many commands piped together. + command_actions: Vec, + /// The command's output, aggregated from stdout and stderr. + aggregated_output: Option, + /// The command's exit code. + exit_code: Option, + /// The duration of the command execution in milliseconds. + duration_ms: Option, + }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + FileChange { + id: String, + changes: Vec, + status: PatchApplyStatus, + }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + McpToolCall { + id: String, + server: String, + tool: String, + status: McpToolCallStatus, + arguments: JsonValue, + result: Option, + error: Option, + }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + WebSearch { id: String, query: String }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + TodoList { id: String, items: Vec }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + ImageView { id: String, path: String }, + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + CodeReview { id: String, review: String }, +} + +impl From for ThreadItem { + fn from(value: CoreTurnItem) -> Self { + match value { + CoreTurnItem::UserMessage(user) => ThreadItem::UserMessage { + id: user.id, + content: user.content.into_iter().map(UserInput::from).collect(), + }, + CoreTurnItem::AgentMessage(agent) => { + let text = agent + .content + .into_iter() + .map(|entry| match entry { + CoreAgentMessageContent::Text { text } => text, + }) + .collect::(); + ThreadItem::AgentMessage { id: agent.id, text } + } + CoreTurnItem::Reasoning(reasoning) => ThreadItem::Reasoning { + id: reasoning.id, + summary: reasoning.summary_text, + content: reasoning.raw_content, + }, + CoreTurnItem::WebSearch(search) => ThreadItem::WebSearch { + id: search.id, + query: search.query, + }, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum CommandExecutionStatus { + InProgress, + Completed, + Failed, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct FileUpdateChange { + pub path: String, + pub kind: PatchChangeKind, + pub diff: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum PatchChangeKind { + Add, + Delete, + Update, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum PatchApplyStatus { + Completed, + Failed, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub enum McpToolCallStatus { + InProgress, + Completed, + Failed, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct McpToolCallResult { + pub content: Vec, + pub structured_content: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct McpToolCallError { + pub message: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TodoItem { + pub id: String, + pub text: String, + pub completed: bool, +} + +// === Server Notifications === +// Thread/Turn lifecycle notifications and item progress events +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadStartedNotification { + pub thread: Thread, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnStartedNotification { + pub turn: Turn, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct Usage { + pub input_tokens: i32, + pub cached_input_tokens: i32, + pub output_tokens: i32, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct TurnCompletedNotification { + pub turn: Turn, + // TODO: should usage be stored on the Turn object, and we return that instead? + pub usage: Usage, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ItemStartedNotification { + pub item: ThreadItem, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ItemCompletedNotification { + pub item: ThreadItem, +} + +// Item-specific progress notifications +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct AgentMessageDeltaNotification { + pub item_id: String, + pub delta: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ReasoningSummaryTextDeltaNotification { + pub item_id: String, + pub delta: String, + pub summary_index: i64, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ReasoningSummaryPartAddedNotification { + pub item_id: String, + pub summary_index: i64, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ReasoningTextDeltaNotification { + pub item_id: String, + pub delta: String, + pub content_index: i64, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CommandExecutionOutputDeltaNotification { + pub item_id: String, + pub delta: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct McpToolCallProgressNotification { + pub item_id: String, + pub message: String, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CommandExecutionRequestApprovalParams { + pub thread_id: String, + pub turn_id: String, + pub item_id: String, + /// Optional explanatory reason (e.g. request for network access). + pub reason: Option, + /// Optional model-provided risk assessment describing the blocked command. + pub risk: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CommandExecutionRequestAcceptSettings { + /// If true, automatically approve this command for the duration of the session. + #[serde(default)] + pub for_session: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CommandExecutionRequestApprovalResponse { + pub decision: ApprovalDecision, + /// Optional approval settings for when the decision is `accept`. + /// Ignored if the decision is `decline` or `cancel`. + #[serde(default)] + pub accept_settings: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct AccountRateLimitsUpdatedNotification { + pub rate_limits: RateLimitSnapshot, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct RateLimitSnapshot { + pub primary: Option, + pub secondary: Option, +} + +impl From for RateLimitSnapshot { + fn from(value: CoreRateLimitSnapshot) -> Self { + Self { + primary: value.primary.map(RateLimitWindow::from), + secondary: value.secondary.map(RateLimitWindow::from), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct RateLimitWindow { + pub used_percent: i32, + pub window_duration_mins: Option, + pub resets_at: Option, +} + +impl From for RateLimitWindow { + fn from(value: CoreRateLimitWindow) -> Self { + Self { + used_percent: value.used_percent.round() as i32, + window_duration_mins: value.window_minutes, + resets_at: value.resets_at, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct AccountLoginCompletedNotification { + // Use plain String for identifiers to avoid TS/JSON Schema quirks around uuid-specific types. + // Convert to/from UUIDs at the application layer as needed. + pub login_id: Option, + pub success: bool, + pub error: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + use codex_protocol::items::AgentMessageContent; + use codex_protocol::items::AgentMessageItem; + use codex_protocol::items::ReasoningItem; + use codex_protocol::items::TurnItem; + use codex_protocol::items::UserMessageItem; + use codex_protocol::items::WebSearchItem; + use codex_protocol::user_input::UserInput as CoreUserInput; + use pretty_assertions::assert_eq; + use std::path::PathBuf; + + #[test] + fn core_turn_item_into_thread_item_converts_supported_variants() { + let user_item = TurnItem::UserMessage(UserMessageItem { + id: "user-1".to_string(), + content: vec![ + CoreUserInput::Text { + text: "hello".to_string(), + }, + CoreUserInput::Image { + image_url: "https://example.com/image.png".to_string(), + }, + CoreUserInput::LocalImage { + path: PathBuf::from("local/image.png"), + }, + ], + }); + + assert_eq!( + ThreadItem::from(user_item), + ThreadItem::UserMessage { + id: "user-1".to_string(), + content: vec![ + UserInput::Text { + text: "hello".to_string(), + }, + UserInput::Image { + url: "https://example.com/image.png".to_string(), + }, + UserInput::LocalImage { + path: PathBuf::from("local/image.png"), + }, + ], + } + ); + + let agent_item = TurnItem::AgentMessage(AgentMessageItem { + id: "agent-1".to_string(), + content: vec![ + AgentMessageContent::Text { + text: "Hello ".to_string(), + }, + AgentMessageContent::Text { + text: "world".to_string(), + }, + ], + }); + + assert_eq!( + ThreadItem::from(agent_item), + ThreadItem::AgentMessage { + id: "agent-1".to_string(), + text: "Hello world".to_string(), + } + ); + + let reasoning_item = TurnItem::Reasoning(ReasoningItem { + id: "reasoning-1".to_string(), + summary_text: vec!["line one".to_string(), "line two".to_string()], + raw_content: vec![], + }); + + assert_eq!( + ThreadItem::from(reasoning_item), + ThreadItem::Reasoning { + id: "reasoning-1".to_string(), + summary: vec!["line one".to_string(), "line two".to_string()], + content: vec![], + } + ); + + let search_item = TurnItem::WebSearch(WebSearchItem { + id: "search-1".to_string(), + query: "docs".to_string(), + }); + + assert_eq!( + ThreadItem::from(search_item), + ThreadItem::WebSearch { + id: "search-1".to_string(), + query: "docs".to_string(), + } + ); + } +} diff --git a/codex-rs/app-server-test-client/Cargo.lock b/codex-rs/app-server-test-client/Cargo.lock new file mode 100644 index 0000000000..1720850cd2 --- /dev/null +++ b/codex-rs/app-server-test-client/Cargo.lock @@ -0,0 +1,1298 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + +[[package]] +name = "cc" +version = "1.2.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d05d92f4b1fd76aad469d46cdd858ca761576082cd37df81416691e50199fb" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "iana-time-zone", + "num-traits", + "serde", + "windows-link", +] + +[[package]] +name = "clap" +version = "4.5.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2134bb3ea021b78629caa971416385309e0131b351b25e01dc16fb54e1b5fae" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2ba64afa3c0a6df7fa517765e31314e983f51dda798ffba27b988194fb65dc9" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfd7eae0b0f1a6e63d4b13c9c478de77c2eb546fba158ad50b4203dc24b9f9c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" + +[[package]] +name = "codex-app-server-protocol" +version = "0.45.0" +source = "git+https://github.com/openai/codex.git?tag=rust-v0.45.0#a7c7869c23f88f6c468281e6f438ba4a91b81f26" +dependencies = [ + "codex-protocol", + "paste", + "serde", + "serde_json", + "strum_macros", + "ts-rs", + "uuid", +] + +[[package]] +name = "codex-protocol" +version = "0.45.0" +source = "git+https://github.com/openai/codex.git?tag=rust-v0.45.0#a7c7869c23f88f6c468281e6f438ba4a91b81f26" +dependencies = [ + "base64", + "icu_decimal", + "icu_locale_core", + "mcp-types", + "mime_guess", + "serde", + "serde_json", + "serde_with", + "strum", + "strum_macros", + "sys-locale", + "tracing", + "ts-rs", + "uuid", +] + +[[package]] +name = "codex_toy_app_server" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "codex-app-server-protocol", + "codex-protocol", + "serde", + "serde_json", + "uuid", +] + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "darling" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "deranged" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a41953f86f8a05768a6cda24def994fd2f424b04ec5c719cf89989779f199071" +dependencies = [ + "powerfmt", + "serde_core", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "find-msvc-tools" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0399f9d26e5191ce32c498bebd31e7a3ceabc2745f0ac54af3f335126c3f24b3" + +[[package]] +name = "fixed_decimal" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35943d22b2f19c0cb198ecf915910a8158e94541c89dcc63300d7799d46c2c5e" +dependencies = [ + "displaydoc", + "smallvec", + "writeable", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "200072f5d0e3614556f94a9930d5dc3e0662a652823904c3a75dc3b0af7fee47" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_decimal" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fec61c43fdc4e368a9f450272833123a8ef0d7083a44597660ce94d791b8a2e2" +dependencies = [ + "displaydoc", + "fixed_decimal", + "icu_decimal_data", + "icu_locale", + "icu_locale_core", + "icu_provider", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_decimal_data" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b70963bc35f9bdf1bc66a5c1f458f4991c1dc71760e00fa06016b2c76b2738d5" + +[[package]] +name = "icu_locale" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ae5921528335e91da1b6c695dbf1ec37df5ac13faa3f91e5640be93aa2fbefd" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_locale_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde2700ccaed3872079a65fb1a78f6c0a36c91570f28755dda67bc8f7d9f00a" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locale_data" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fdef0c124749d06a743c69e938350816554eb63ac979166590e2b4ee4252765" + +[[package]] +name = "icu_provider" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03c80da27b5f4187909049ee2d72f276f0d9f99a42c306bd0131ecfe04d8e5af" +dependencies = [ + "displaydoc", + "icu_locale_core", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" +dependencies = [ + "equivalent", + "hashbrown 0.16.0", + "serde", + "serde_core", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "js-sys" +version = "0.3.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.176" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174" + +[[package]] +name = "litemap" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956" + +[[package]] +name = "log" +version = "0.4.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" + +[[package]] +name = "mcp-types" +version = "0.45.0" +source = "git+https://github.com/openai/codex.git?tag=rust-v0.45.0#a7c7869c23f88f6c468281e6f438ba4a91b81f26" +dependencies = [ + "serde", + "serde_json", + "ts-rs", +] + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "potential_utf" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84df19adbe5b5a0782edcab45899906947ab039ccf4573713735ee7de1e6b08a" +dependencies = [ + "serde", + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "proc-macro2" +version = "1.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "ref-cast" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f354300ae66f76f1c85c5f84693f0ce81d747e2c3f21a45fef496d89c960bf7d" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7186006dcb21920990093f30e3dea63b7d6e977bf1256be20c3563a5db070da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "schemars" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd191f9397d57d581cddd31014772520aa448f65ef991055d7f61582c65165f" +dependencies = [ + "dyn-clone", + "ref-cast", + "serde", + "serde_json", +] + +[[package]] +name = "schemars" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0" +dependencies = [ + "dyn-clone", + "ref-cast", + "serde", + "serde_json", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", + "serde_core", +] + +[[package]] +name = "serde_with" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093cd8c01b25262b84927e0f7151692158fab02d961e04c979d3903eba7ecc5" +dependencies = [ + "base64", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.11.4", + "schemars 0.9.0", + "schemars 1.0.4", + "serde_core", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7e6c180db0816026a61afa1cff5344fb7ebded7e4d3062772179f2501481c27" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "strum" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af23d6f6c1a224baef9d3f61e287d2761385a5b88fdab4eb4c6f11aeb54c4bcf" + +[[package]] +name = "strum_macros" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "sys-locale" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4" +dependencies = [ + "libc", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.3.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b" + +[[package]] +name = "time-macros" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30cfb0125f12d9c277f35663a0a33f8c30190f4e4574868a330595412d34ebf3" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tinystr" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d4f6d1145dcb577acf783d4e601bc1d76a13337bb54e6233add580b07344c8b" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678" +dependencies = [ + "once_cell", +] + +[[package]] +name = "ts-rs" +version = "11.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef1b7a6d914a34127ed8e1fa927eb7088903787bcded4fa3eef8f85ee1568be" +dependencies = [ + "serde_json", + "thiserror", + "ts-rs-macros", + "uuid", +] + +[[package]] +name = "ts-rs-macros" +version = "11.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d4ed7b4c18cc150a6a0a1e9ea1ecfa688791220781af6e119f9599a8502a0a" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "termcolor", +] + +[[package]] +name = "unicase" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" + +[[package]] +name = "unicode-ident" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uuid" +version = "1.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f87b8aa10b915a06587d0dec516c282ff295b475d94abf425d62b57710070a2" +dependencies = [ + "getrandom", + "js-sys", + "serde", + "wasm-bindgen", +] + +[[package]] +name = "wasi" +version = "0.14.7+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "883478de20367e224c0090af9cf5f9fa85bed63a95c1abf3afc5c083ebc06e8c" +dependencies = [ + "wasip2", +] + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "writeable" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb" + +[[package]] +name = "yoke" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f41bb01b8226ef4bfd589436a297c53d118f65921786300e427be8d487695cc" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerotrie" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36f0bbd478583f79edad978b407914f61b2972f5af6fa089686016be8f9af595" +dependencies = [ + "displaydoc", +] + +[[package]] +name = "zerovec" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7aa2bd55086f1ab526693ecbe444205da57e25f4489879da80635a46d90e73b" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/codex-rs/app-server-test-client/Cargo.toml b/codex-rs/app-server-test-client/Cargo.toml new file mode 100644 index 0000000000..2fd14fb158 --- /dev/null +++ b/codex-rs/app-server-test-client/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "codex-app-server-test-client" +version = { workspace = true } +edition = "2024" + +[lints] +workspace = true + +[dependencies] +anyhow = { workspace = true } +clap = { workspace = true, features = ["derive", "env"] } +codex-app-server-protocol = { workspace = true } +codex-protocol = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +uuid = { workspace = true, features = ["v4"] } diff --git a/codex-rs/app-server-test-client/README.md b/codex-rs/app-server-test-client/README.md new file mode 100644 index 0000000000..ee75348c98 --- /dev/null +++ b/codex-rs/app-server-test-client/README.md @@ -0,0 +1,2 @@ +# App Server Test Client +Exercises simple `codex app-server` flows end-to-end, logging JSON-RPC messages sent between client and server to stdout. diff --git a/codex-rs/app-server-test-client/src/main.rs b/codex-rs/app-server-test-client/src/main.rs new file mode 100644 index 0000000000..a243937b28 --- /dev/null +++ b/codex-rs/app-server-test-client/src/main.rs @@ -0,0 +1,691 @@ +use std::collections::VecDeque; +use std::io::BufRead; +use std::io::BufReader; +use std::io::Write; +use std::process::Child; +use std::process::ChildStdin; +use std::process::ChildStdout; +use std::process::Command; +use std::process::Stdio; +use std::thread; +use std::time::Duration; + +use anyhow::Context; +use anyhow::Result; +use anyhow::bail; +use clap::Parser; +use clap::Subcommand; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::AddConversationSubscriptionResponse; +use codex_app_server_protocol::AskForApproval; +use codex_app_server_protocol::ClientInfo; +use codex_app_server_protocol::ClientRequest; +use codex_app_server_protocol::GetAccountRateLimitsResponse; +use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::InitializeResponse; +use codex_app_server_protocol::InputItem; +use codex_app_server_protocol::JSONRPCMessage; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::LoginChatGptCompleteNotification; +use codex_app_server_protocol::LoginChatGptResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SandboxPolicy; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_app_server_protocol::TurnStartParams; +use codex_app_server_protocol::TurnStartResponse; +use codex_app_server_protocol::UserInput as V2UserInput; +use codex_protocol::ConversationId; +use codex_protocol::protocol::Event; +use codex_protocol::protocol::EventMsg; +use serde::de::DeserializeOwned; +use serde_json::Value; +use uuid::Uuid; + +/// Minimal launcher that initializes the Codex app-server and logs the handshake. +#[derive(Parser)] +#[command(author = "Codex", version, about = "Bootstrap Codex app-server", long_about = None)] +struct Cli { + /// Path to the `codex` CLI binary. + #[arg(long, env = "CODEX_BIN", default_value = "codex")] + codex_bin: String, + + #[command(subcommand)] + command: CliCommand, +} + +#[derive(Subcommand)] +enum CliCommand { + /// Send a user message through the Codex app-server. + SendMessage { + /// User message to send to Codex. + #[arg()] + user_message: String, + }, + /// Send a user message through the app-server V2 thread/turn APIs. + SendMessageV2 { + /// User message to send to Codex. + #[arg()] + user_message: String, + }, + /// Start a V2 turn that elicits an ExecCommand approval. + #[command(name = "trigger-cmd-approval")] + TriggerCmdApproval { + /// Optional prompt; defaults to a simple python command. + #[arg()] + user_message: Option, + }, + /// Start a V2 turn that elicits an ApplyPatch approval. + #[command(name = "trigger-patch-approval")] + TriggerPatchApproval { + /// Optional prompt; defaults to creating a file via apply_patch. + #[arg()] + user_message: Option, + }, + /// Start a V2 turn that should not elicit an ExecCommand approval. + #[command(name = "no-trigger-cmd-approval")] + NoTriggerCmdApproval, + /// Trigger the ChatGPT login flow and wait for completion. + TestLogin, + /// Fetch the current account rate limits from the Codex app-server. + GetAccountRateLimits, +} + +fn main() -> Result<()> { + let Cli { codex_bin, command } = Cli::parse(); + + match command { + CliCommand::SendMessage { user_message } => send_message(codex_bin, user_message), + CliCommand::SendMessageV2 { user_message } => send_message_v2(codex_bin, user_message), + CliCommand::TriggerCmdApproval { user_message } => { + trigger_cmd_approval(codex_bin, user_message) + } + CliCommand::TriggerPatchApproval { user_message } => { + trigger_patch_approval(codex_bin, user_message) + } + CliCommand::NoTriggerCmdApproval => no_trigger_cmd_approval(codex_bin), + CliCommand::TestLogin => test_login(codex_bin), + CliCommand::GetAccountRateLimits => get_account_rate_limits(codex_bin), + } +} + +fn send_message(codex_bin: String, user_message: String) -> Result<()> { + let mut client = CodexClient::spawn(codex_bin)?; + + let initialize = client.initialize()?; + println!("< initialize response: {initialize:?}"); + + let conversation = client.new_conversation()?; + println!("< newConversation response: {conversation:?}"); + + let subscription = client.add_conversation_listener(&conversation.conversation_id)?; + println!("< addConversationListener response: {subscription:?}"); + + let send_response = client.send_user_message(&conversation.conversation_id, &user_message)?; + println!("< sendUserMessage response: {send_response:?}"); + + client.stream_conversation(&conversation.conversation_id)?; + + client.remove_conversation_listener(subscription.subscription_id)?; + + Ok(()) +} + +fn send_message_v2(codex_bin: String, user_message: String) -> Result<()> { + send_message_v2_with_policies(codex_bin, user_message, None, None) +} + +fn trigger_cmd_approval(codex_bin: String, user_message: Option) -> Result<()> { + let default_prompt = + "Run `touch /tmp/should-trigger-approval` so I can confirm the file exists."; + let message = user_message.unwrap_or_else(|| default_prompt.to_string()); + send_message_v2_with_policies( + codex_bin, + message, + Some(AskForApproval::OnRequest), + Some(SandboxPolicy::ReadOnly), + ) +} + +fn trigger_patch_approval(codex_bin: String, user_message: Option) -> Result<()> { + let default_prompt = + "Create a file named APPROVAL_DEMO.txt containing a short hello message using apply_patch."; + let message = user_message.unwrap_or_else(|| default_prompt.to_string()); + send_message_v2_with_policies( + codex_bin, + message, + Some(AskForApproval::OnRequest), + Some(SandboxPolicy::ReadOnly), + ) +} + +fn no_trigger_cmd_approval(codex_bin: String) -> Result<()> { + let prompt = "Run `touch should_not_trigger_approval.txt`"; + send_message_v2_with_policies(codex_bin, prompt.to_string(), None, None) +} + +fn send_message_v2_with_policies( + codex_bin: String, + user_message: String, + approval_policy: Option, + sandbox_policy: Option, +) -> Result<()> { + let mut client = CodexClient::spawn(codex_bin)?; + + let initialize = client.initialize()?; + println!("< initialize response: {initialize:?}"); + + let thread_response = client.thread_start(ThreadStartParams::default())?; + println!("< thread/start response: {thread_response:?}"); + let mut turn_params = TurnStartParams { + thread_id: thread_response.thread.id.clone(), + input: vec![V2UserInput::Text { text: user_message }], + ..Default::default() + }; + turn_params.approval_policy = approval_policy; + turn_params.sandbox_policy = sandbox_policy; + + let turn_response = client.turn_start(turn_params)?; + println!("< turn/start response: {turn_response:?}"); + + client.stream_turn(&thread_response.thread.id, &turn_response.turn.id)?; + + Ok(()) +} + +fn test_login(codex_bin: String) -> Result<()> { + let mut client = CodexClient::spawn(codex_bin)?; + + let initialize = client.initialize()?; + println!("< initialize response: {initialize:?}"); + + let login_response = client.login_chat_gpt()?; + println!("< loginChatGpt response: {login_response:?}"); + println!( + "Open the following URL in your browser to continue:\n{}", + login_response.auth_url + ); + + let completion = client.wait_for_login_completion(&login_response.login_id)?; + println!("< loginChatGptComplete notification: {completion:?}"); + + if completion.success { + println!("Login succeeded."); + Ok(()) + } else { + bail!( + "login failed: {}", + completion + .error + .as_deref() + .unwrap_or("unknown error from loginChatGptComplete") + ); + } +} + +fn get_account_rate_limits(codex_bin: String) -> Result<()> { + let mut client = CodexClient::spawn(codex_bin)?; + + let initialize = client.initialize()?; + println!("< initialize response: {initialize:?}"); + + let response = client.get_account_rate_limits()?; + println!("< account/rateLimits/read response: {response:?}"); + + Ok(()) +} + +struct CodexClient { + child: Child, + stdin: Option, + stdout: BufReader, + pending_notifications: VecDeque, +} + +impl CodexClient { + fn spawn(codex_bin: String) -> Result { + let mut codex_app_server = Command::new(&codex_bin) + .arg("app-server") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::inherit()) + .spawn() + .with_context(|| format!("failed to start `{codex_bin}` app-server"))?; + + let stdin = codex_app_server + .stdin + .take() + .context("codex app-server stdin unavailable")?; + let stdout = codex_app_server + .stdout + .take() + .context("codex app-server stdout unavailable")?; + + Ok(Self { + child: codex_app_server, + stdin: Some(stdin), + stdout: BufReader::new(stdout), + pending_notifications: VecDeque::new(), + }) + } + + fn initialize(&mut self) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::Initialize { + request_id: request_id.clone(), + params: InitializeParams { + client_info: ClientInfo { + name: "codex-toy-app-server".to_string(), + title: Some("Codex Toy App Server".to_string()), + version: env!("CARGO_PKG_VERSION").to_string(), + }, + }, + }; + + self.send_request(request, request_id, "initialize") + } + + fn new_conversation(&mut self) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::NewConversation { + request_id: request_id.clone(), + params: NewConversationParams::default(), + }; + + self.send_request(request, request_id, "newConversation") + } + + fn add_conversation_listener( + &mut self, + conversation_id: &ConversationId, + ) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::AddConversationListener { + request_id: request_id.clone(), + params: AddConversationListenerParams { + conversation_id: *conversation_id, + experimental_raw_events: false, + }, + }; + + self.send_request(request, request_id, "addConversationListener") + } + + fn remove_conversation_listener(&mut self, subscription_id: Uuid) -> Result<()> { + let request_id = self.request_id(); + let request = ClientRequest::RemoveConversationListener { + request_id: request_id.clone(), + params: codex_app_server_protocol::RemoveConversationListenerParams { subscription_id }, + }; + + self.send_request::( + request, + request_id, + "removeConversationListener", + )?; + + Ok(()) + } + + fn send_user_message( + &mut self, + conversation_id: &ConversationId, + message: &str, + ) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::SendUserMessage { + request_id: request_id.clone(), + params: SendUserMessageParams { + conversation_id: *conversation_id, + items: vec![InputItem::Text { + text: message.to_string(), + }], + }, + }; + + self.send_request(request, request_id, "sendUserMessage") + } + + fn thread_start(&mut self, params: ThreadStartParams) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::ThreadStart { + request_id: request_id.clone(), + params, + }; + + self.send_request(request, request_id, "thread/start") + } + + fn turn_start(&mut self, params: TurnStartParams) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::TurnStart { + request_id: request_id.clone(), + params, + }; + + self.send_request(request, request_id, "turn/start") + } + + fn login_chat_gpt(&mut self) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::LoginChatGpt { + request_id: request_id.clone(), + params: None, + }; + + self.send_request(request, request_id, "loginChatGpt") + } + + fn get_account_rate_limits(&mut self) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::GetAccountRateLimits { + request_id: request_id.clone(), + params: None, + }; + + self.send_request(request, request_id, "account/rateLimits/read") + } + + fn stream_conversation(&mut self, conversation_id: &ConversationId) -> Result<()> { + loop { + let notification = self.next_notification()?; + + if !notification.method.starts_with("codex/event/") { + continue; + } + + if let Some(event) = self.extract_event(notification, conversation_id)? { + match &event.msg { + EventMsg::AgentMessage(event) => { + println!("{}", event.message); + } + EventMsg::AgentMessageDelta(event) => { + print!("{}", event.delta); + std::io::stdout().flush().ok(); + } + EventMsg::TaskComplete(event) => { + println!("\n[task complete: {event:?}]"); + break; + } + EventMsg::TurnAborted(event) => { + println!("\n[turn aborted: {:?}]", event.reason); + break; + } + EventMsg::Error(event) => { + println!("[error] {event:?}"); + } + _ => { + println!("[UNKNOWN EVENT] {:?}", event.msg); + } + } + } + } + + Ok(()) + } + + fn wait_for_login_completion( + &mut self, + expected_login_id: &Uuid, + ) -> Result { + loop { + let notification = self.next_notification()?; + + if let Ok(server_notification) = ServerNotification::try_from(notification) { + match server_notification { + ServerNotification::LoginChatGptComplete(completion) => { + if &completion.login_id == expected_login_id { + return Ok(completion); + } + + println!( + "[ignoring loginChatGptComplete for unexpected login_id: {}]", + completion.login_id + ); + } + ServerNotification::AuthStatusChange(status) => { + println!("< authStatusChange notification: {status:?}"); + } + ServerNotification::AccountRateLimitsUpdated(snapshot) => { + println!("< accountRateLimitsUpdated notification: {snapshot:?}"); + } + ServerNotification::SessionConfigured(_) => { + // SessionConfigured notifications are unrelated to login; skip. + } + _ => {} + } + } + + // Not a server notification (likely a conversation event); keep waiting. + } + } + + fn stream_turn(&mut self, thread_id: &str, turn_id: &str) -> Result<()> { + loop { + let notification = self.next_notification()?; + + let Ok(server_notification) = ServerNotification::try_from(notification) else { + continue; + }; + + match server_notification { + ServerNotification::ThreadStarted(payload) => { + if payload.thread.id == thread_id { + println!("< thread/started notification: {:?}", payload.thread); + } + } + ServerNotification::TurnStarted(payload) => { + if payload.turn.id == turn_id { + println!("< turn/started notification: {:?}", payload.turn.status); + } + } + ServerNotification::AgentMessageDelta(delta) => { + print!("{}", delta.delta); + std::io::stdout().flush().ok(); + } + ServerNotification::CommandExecutionOutputDelta(delta) => { + print!("{}", delta.delta); + std::io::stdout().flush().ok(); + } + ServerNotification::ItemStarted(payload) => { + println!("\n< item started: {:?}", payload.item); + } + ServerNotification::ItemCompleted(payload) => { + println!("< item completed: {:?}", payload.item); + } + ServerNotification::TurnCompleted(payload) => { + if payload.turn.id == turn_id { + println!("\n< turn/completed notification: {:?}", payload.turn.status); + if let Some(error) = payload.turn.error { + println!("[turn error] {}", error.message); + } + println!("< usage: {:?}", payload.usage); + break; + } + } + ServerNotification::McpToolCallProgress(payload) => { + println!("< MCP tool progress: {}", payload.message); + } + _ => { + println!("[UNKNOWN SERVER NOTIFICATION] {server_notification:?}"); + } + } + } + + Ok(()) + } + + fn extract_event( + &self, + notification: JSONRPCNotification, + conversation_id: &ConversationId, + ) -> Result> { + let params = notification + .params + .context("event notification missing params")?; + + let mut map = match params { + Value::Object(map) => map, + other => bail!("unexpected params shape: {other:?}"), + }; + + let conversation_value = map + .remove("conversationId") + .context("event missing conversationId")?; + let notification_conversation: ConversationId = serde_json::from_value(conversation_value) + .context("conversationId was not a valid UUID")?; + + if ¬ification_conversation != conversation_id { + return Ok(None); + } + + let event_value = Value::Object(map); + let event: Event = + serde_json::from_value(event_value).context("failed to decode event payload")?; + Ok(Some(event)) + } + + fn send_request( + &mut self, + request: ClientRequest, + request_id: RequestId, + method: &str, + ) -> Result + where + T: DeserializeOwned, + { + self.write_request(&request)?; + self.wait_for_response(request_id, method) + } + + fn write_request(&mut self, request: &ClientRequest) -> Result<()> { + let request_json = serde_json::to_string(request)?; + let request_pretty = serde_json::to_string_pretty(request)?; + print_multiline_with_prefix("> ", &request_pretty); + + if let Some(stdin) = self.stdin.as_mut() { + writeln!(stdin, "{request_json}")?; + stdin + .flush() + .context("failed to flush request to codex app-server")?; + } else { + bail!("codex app-server stdin closed"); + } + + Ok(()) + } + + fn wait_for_response(&mut self, request_id: RequestId, method: &str) -> Result + where + T: DeserializeOwned, + { + loop { + let message = self.read_jsonrpc_message()?; + + match message { + JSONRPCMessage::Response(JSONRPCResponse { id, result }) => { + if id == request_id { + return serde_json::from_value(result) + .with_context(|| format!("{method} response missing payload")); + } + } + JSONRPCMessage::Error(err) => { + if err.id == request_id { + bail!("{method} failed: {err:?}"); + } + } + JSONRPCMessage::Notification(notification) => { + self.pending_notifications.push_back(notification); + } + JSONRPCMessage::Request(_) => { + bail!("unexpected request from codex app-server"); + } + } + } + } + + fn next_notification(&mut self) -> Result { + if let Some(notification) = self.pending_notifications.pop_front() { + return Ok(notification); + } + + loop { + let message = self.read_jsonrpc_message()?; + + match message { + JSONRPCMessage::Notification(notification) => return Ok(notification), + JSONRPCMessage::Response(_) | JSONRPCMessage::Error(_) => { + // No outstanding requests, so ignore stray responses/errors for now. + continue; + } + JSONRPCMessage::Request(_) => { + bail!("unexpected request from codex app-server"); + } + } + } + } + + fn read_jsonrpc_message(&mut self) -> Result { + loop { + let mut response_line = String::new(); + let bytes = self + .stdout + .read_line(&mut response_line) + .context("failed to read from codex app-server")?; + + if bytes == 0 { + bail!("codex app-server closed stdout"); + } + + let trimmed = response_line.trim(); + if trimmed.is_empty() { + continue; + } + + let parsed: Value = + serde_json::from_str(trimmed).context("response was not valid JSON-RPC")?; + let pretty = serde_json::to_string_pretty(&parsed)?; + print_multiline_with_prefix("< ", &pretty); + let message: JSONRPCMessage = serde_json::from_value(parsed) + .context("response was not a valid JSON-RPC message")?; + return Ok(message); + } + } + + fn request_id(&self) -> RequestId { + RequestId::String(Uuid::new_v4().to_string()) + } +} + +fn print_multiline_with_prefix(prefix: &str, payload: &str) { + for line in payload.lines() { + println!("{prefix}{line}"); + } +} + +impl Drop for CodexClient { + fn drop(&mut self) { + let _ = self.stdin.take(); + + if let Ok(Some(status)) = self.child.try_wait() { + println!("[codex app-server exited: {status}]"); + return; + } + + thread::sleep(Duration::from_millis(100)); + + if let Ok(Some(status)) = self.child.try_wait() { + println!("[codex app-server exited: {status}]"); + return; + } + + let _ = self.child.kill(); + let _ = self.child.wait(); + } +} diff --git a/codex-rs/app-server/Cargo.toml b/codex-rs/app-server/Cargo.toml new file mode 100644 index 0000000000..96f64afdf5 --- /dev/null +++ b/codex-rs/app-server/Cargo.toml @@ -0,0 +1,55 @@ +[package] +edition = "2024" +name = "codex-app-server" +version = { workspace = true } + +[[bin]] +name = "codex-app-server" +path = "src/main.rs" + +[lib] +name = "codex_app_server" +path = "src/lib.rs" + +[lints] +workspace = true + +[dependencies] +anyhow = { workspace = true } +codex-arg0 = { workspace = true } +codex-common = { workspace = true, features = ["cli"] } +codex-core = { workspace = true } +codex-backend-client = { workspace = true } +codex-file-search = { workspace = true } +codex-login = { workspace = true } +codex-protocol = { workspace = true } +codex-app-server-protocol = { workspace = true } +codex-feedback = { workspace = true } +codex-utils-json-to-toml = { workspace = true } +chrono = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tokio = { workspace = true, features = [ + "io-std", + "macros", + "process", + "rt-multi-thread", + "signal", +] } +tracing = { workspace = true, features = ["log"] } +tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] } +opentelemetry-appender-tracing = { workspace = true } +uuid = { workspace = true, features = ["serde", "v7"] } + +[dev-dependencies] +app_test_support = { workspace = true } +assert_cmd = { workspace = true } +base64 = { workspace = true } +core_test_support = { workspace = true } +mcp-types = { workspace = true } +os_info = { workspace = true } +pretty_assertions = { workspace = true } +serial_test = { workspace = true } +tempfile = { workspace = true } +toml = { workspace = true } +wiremock = { workspace = true } diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md new file mode 100644 index 0000000000..5f9b87458f --- /dev/null +++ b/codex-rs/app-server/README.md @@ -0,0 +1,308 @@ +# codex-app-server + +`codex app-server` is the interface Codex uses to power rich interfaces such as the [Codex VS Code extension](https://marketplace.visualstudio.com/items?itemName=openai.chatgpt). The message schema is currently unstable, but those who wish to build experimental UIs on top of Codex may find it valuable. + +## Table of Contents +- [Protocol](#protocol) +- [Message Schema](#message-schema) +- [Lifecycle Overview](#lifecycle-overview) +- [Initialization](#initialization) +- [Core primitives](#core-primitives) +- [Thread & turn endpoints](#thread--turn-endpoints) +- [Auth endpoints](#auth-endpoints) +- [Events (work-in-progress)](#v2-streaming-events-work-in-progress) + +## Protocol + +Similar to [MCP](https://modelcontextprotocol.io/), `codex app-server` supports bidirectional communication, streaming JSONL over stdio. The protocol is JSON-RPC 2.0, though the `"jsonrpc":"2.0"` header is omitted. + +## Message Schema + +Currently, you can dump a TypeScript version of the schema using `codex app-server generate-ts`, or a JSON Schema bundle via `codex app-server generate-json-schema`. Each output is specific to the version of Codex you used to run the command, so the generated artifacts are guaranteed to match that version. + +``` +codex app-server generate-ts --out DIR +codex app-server generate-json-schema --out DIR +``` + +## Lifecycle Overview + +- Initialize once: Immediately after launching the codex app-server process, send an `initialize` request with your client metadata, then emit an `initialized` notification. Any other request before this handshake gets rejected. +- Start (or resume) a thread: Call `thread/start` to open a fresh conversation. The response returns the thread object and you’ll also get a `thread/started` notification. If you’re continuing an existing conversation, call `thread/resume` with its ID instead. +- Begin a turn: To send user input, call `turn/start` with the target `threadId` and the user's input. Optional fields let you override model, cwd, sandbox policy, etc. This immediately returns the new turn object and triggers a `turn/started` notification. +- Stream events: After `turn/start`, keep reading JSON-RPC notifications on stdout. You’ll see `item/started`, `item/completed`, deltas like `item/agentMessage/delta`, tool progress, etc. These represent streaming model output plus any side effects (commands, tool calls, reasoning notes). +- Finish the turn: When the model is done (or the turn is interrupted via making the `turn/interrupt` call), the server sends `turn/completed` with the final turn state and token usage. + +## Initialization + +Clients must send a single `initialize` request before invoking any other method, then acknowledge with an `initialized` notification. The server returns the user agent string it will present to upstream services; subsequent requests issued before initialization receive a `"Not initialized"` error, and repeated `initialize` calls receive an `"Already initialized"` error. + +Example: + +```json +{ "method": "initialize", "id": 0, "params": { + "clientInfo": { "name": "codex-vscode", "title": "Codex VS Code Extension", "version": "0.1.0" } +} } +{ "id": 0, "result": { "userAgent": "codex-app-server/0.1.0 codex-vscode/0.1.0" } } +{ "method": "initialized" } +``` + +## Core primitives + +We have 3 top level primitives: +- Thread - a conversation between the Codex agent and a user. Each thread contains multiple turns. +- Turn - one turn of the conversation, typically starting with a user message and finishing with an agent message. Each turn contains multiple items. +- Item - represents user inputs and agent outputs as part of the turn, persisted and used as the context for future conversations. + +## Thread & turn endpoints + +The JSON-RPC API exposes dedicated methods for managing Codex conversations. Threads store long-lived conversation metadata, and turns store the per-message exchange (input → Codex output, including streamed items). Use the thread APIs to create, list, or archive sessions, then drive the conversation with turn APIs and notifications. + +### Quick reference +- `thread/start` — create a new thread; emits `thread/started` and auto-subscribes you to turn/item events for that thread. +- `thread/resume` — reopen an existing thread by id so subsequent `turn/start` calls append to it. +- `thread/list` — page through stored rollouts; supports cursor-based pagination and optional `modelProviders` filtering. +- `thread/archive` — move a thread’s rollout file into the archived directory; returns `{}` on success. +- `turn/start` — add user input to a thread and begin Codex generation; responds with the initial `turn` object and streams `turn/started`, `item/*`, and `turn/completed` notifications. +- `turn/interrupt` — request cancellation of an in-flight turn by `(thread_id, turn_id)`; success is an empty `{}` response and the turn finishes with `status: "interrupted"`. + +### 1) Start or resume a thread + +Start a fresh thread when you need a new Codex conversation. + +```json +{ "method": "thread/start", "id": 10, "params": { + // Optionally set config settings. If not specified, will use the user's + // current config settings. + "model": "gpt-5.1-codex", + "cwd": "/Users/me/project", + "approvalPolicy": "never", + "sandbox": "workspaceWrite", +} } +{ "id": 10, "result": { + "thread": { + "id": "thr_123", + "preview": "", + "modelProvider": "openai", + "createdAt": 1730910000 + } +} } +{ "method": "thread/started", "params": { "thread": { … } } } +``` + +To continue a stored session, call `thread/resume` with the `thread.id` you previously recorded. The response shape matches `thread/start`, and no additional notifications are emitted: + +```json +{ "method": "thread/resume", "id": 11, "params": { "threadId": "thr_123" } } +{ "id": 11, "result": { "thread": { "id": "thr_123", … } } } +``` + +### 2) List threads (pagination & filters) + +`thread/list` lets you render a history UI. Pass any combination of: +- `cursor` — opaque string from a prior response; omit for the first page. +- `limit` — server defaults to a reasonable page size if unset. +- `modelProviders` — restrict results to specific providers; unset, null, or an empty array will include all providers. + +Example: + +```json +{ "method": "thread/list", "id": 20, "params": { + "cursor": null, + "limit": 25, +} } +{ "id": 20, "result": { + "data": [ + { "id": "thr_a", "preview": "Create a TUI", "modelProvider": "openai", "createdAt": 1730831111 }, + { "id": "thr_b", "preview": "Fix tests", "modelProvider": "openai", "createdAt": 1730750000 } + ], + "nextCursor": "opaque-token-or-null" +} } +``` + +When `nextCursor` is `null`, you’ve reached the final page. + +### 3) Archive a thread + +Use `thread/archive` to move the persisted rollout (stored as a JSONL file on disk) into the archived sessions directory. + +```json +{ "method": "thread/archive", "id": 21, "params": { "threadId": "thr_b" } } +{ "id": 21, "result": {} } +``` + +An archived thread will not appear in future calls to `thread/list`. + +### 4) Start a turn (send user input) + +Turns attach user input (text or images) to a thread and trigger Codex generation. The `input` field is a list of discriminated unions: + +- `{"type":"text","text":"Explain this diff"}` +- `{"type":"image","url":"https://…png"}` +- `{"type":"localImage","path":"/tmp/screenshot.png"}` + +You can optionally specify config overrides on the new turn. If specified, these settings become the default for subsequent turns on the same thread. + +```json +{ "method": "turn/start", "id": 30, "params": { + "threadId": "thr_123", + "input": [ { "type": "text", "text": "Run tests" } ], + // Below are optional config overrides + "cwd": "/Users/me/project", + "approvalPolicy": "unlessTrusted", + "sandboxPolicy": { + "mode": "workspaceWrite", + "writableRoots": ["/Users/me/project"], + "networkAccess": true + }, + "model": "gpt-5.1-codex", + "effort": "medium", + "summary": "concise" +} } +{ "id": 30, "result": { "turn": { + "id": "turn_456", + "status": "inProgress", + "items": [], + "error": null +} } } +``` + +### 5) Interrupt an active turn + +You can cancel a running Turn with `turn/interrupt`. + +```json +{ "method": "turn/interrupt", "id": 31, "params": { + "threadId": "thr_123", + "turnId": "turn_456" +} } +{ "id": 31, "result": {} } +``` + +The server requests cancellations for running subprocesses, then emits a `turn/completed` event with `status: "interrupted"`. Rely on the `turn/completed` to know when Codex-side cleanup is done. + +## Auth endpoints + +The JSON-RPC auth/account surface exposes request/response methods plus server-initiated notifications (no `id`). Use these to determine auth state, start or cancel logins, logout, and inspect ChatGPT rate limits. + +### Quick reference +- `account/read` — fetch current account info; optionally refresh tokens. +- `account/login/start` — begin login (`apiKey` or `chatgpt`). +- `account/login/completed` (notify) — emitted when a login attempt finishes (success or error). +- `account/login/cancel` — cancel a pending ChatGPT login by `loginId`. +- `account/logout` — sign out; triggers `account/updated`. +- `account/updated` (notify) — emitted whenever auth mode changes (`authMode`: `apikey`, `chatgpt`, or `null`). +- `account/rateLimits/read` — fetch ChatGPT rate limits; updates arrive via `account/rateLimits/updated` (notify). + +### 1) Check auth state + +Request: +```json +{ "method": "account/read", "id": 1, "params": { "refreshToken": false } } +``` + +Response examples: +```json +{ "id": 1, "result": { "account": null, "requiresOpenaiAuth": false } } // No OpenAI auth needed (e.g., OSS/local models) +{ "id": 1, "result": { "account": null, "requiresOpenaiAuth": true } } // OpenAI auth required (typical for OpenAI-hosted models) +{ "id": 1, "result": { "account": { "type": "apiKey" }, "requiresOpenaiAuth": true } } +{ "id": 1, "result": { "account": { "type": "chatgpt", "email": "user@example.com", "planType": "pro" }, "requiresOpenaiAuth": true } } +``` + +Field notes: +- `refreshToken` (bool): set `true` to force a token refresh. +- `requiresOpenaiAuth` reflects the active provider; when `false`, Codex can run without OpenAI credentials. + +### 2) Log in with an API key + +1. Send: + ```json + { "method": "account/login/start", "id": 2, "params": { "type": "apiKey", "apiKey": "sk-…" } } + ``` +2. Expect: + ```json + { "id": 2, "result": { "type": "apiKey" } } + ``` +3. Notifications: + ```json + { "method": "account/login/completed", "params": { "loginId": null, "success": true, "error": null } } + { "method": "account/updated", "params": { "authMode": "apikey" } } + ``` + +### 3) Log in with ChatGPT (browser flow) + +1. Start: + ```json + { "method": "account/login/start", "id": 3, "params": { "type": "chatgpt" } } + { "id": 3, "result": { "type": "chatgpt", "loginId": "", "authUrl": "https://chatgpt.com/…&redirect_uri=http%3A%2F%2Flocalhost%3A%2Fauth%2Fcallback" } } + ``` +2. Open `authUrl` in a browser; the app-server hosts the local callback. +3. Wait for notifications: + ```json + { "method": "account/login/completed", "params": { "loginId": "", "success": true, "error": null } } + { "method": "account/updated", "params": { "authMode": "chatgpt" } } + ``` + +### 4) Cancel a ChatGPT login + +```json +{ "method": "account/login/cancel", "id": 4, "params": { "loginId": "" } } +{ "method": "account/login/completed", "params": { "loginId": "", "success": false, "error": "…" } } +``` + +### 5) Logout + +```json +{ "method": "account/logout", "id": 5 } +{ "id": 5, "result": {} } +{ "method": "account/updated", "params": { "authMode": null } } +``` + +### 6) Rate limits (ChatGPT) + +```json +{ "method": "account/rateLimits/read", "id": 6 } +{ "id": 6, "result": { "rateLimits": { "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null } } } +{ "method": "account/rateLimits/updated", "params": { "rateLimits": { … } } } +``` + +Field notes: +- `usedPercent` is current usage within the OpenAI quota window. +- `windowDurationMins` is the quota window length. +- `resetsAt` is a Unix timestamp (seconds) for the next reset. + +### Dev notes + +- `codex app-server generate-ts --out ` emits v2 types under `v2/`. +- `codex app-server generate-json-schema --out ` outputs `codex_app_server_protocol.schemas.json`. +- See [“Authentication and authorization” in the config docs](../../docs/config.md#authentication-and-authorization) for configuration knobs. + + +## Events (work-in-progress) + +Event notifications are the server-initiated event stream for thread lifecycles, turn lifecycles, and the items within them. After you start or resume a thread, keep reading stdout for `thread/started`, `turn/*`, and `item/*` notifications. + +### Turn events + +The app-server streams JSON-RPC notifications while a turn is running. Each turn starts with `turn/started` (initial `turn`) and ends with `turn/completed` (final `turn` plus token `usage`), and clients subscribe to the events they care about, rendering each item incrementally as updates arrive. The per-item lifecycle is always: `item/started` → zero or more item-specific deltas → `item/completed`. + +#### Thread items + +`ThreadItem` is the tagged union carried in turn responses and `item/*` notifications. Currently we support events for the following items: +- `userMessage` — `{id, content}` where `content` is a list of user inputs (`text`, `image`, or `localImage`). +- `agentMessage` — `{id, text}` containing the accumulated agent reply. +- `reasoning` — `{id, summary, content}` where `summary` holds streamed reasoning summaries (applicable for most OpenAI models) and `content` holds raw reasoning blocks (applicable for e.g. open source models). +- `mcpToolCall` — `{id, server, tool, status, arguments, result?, error?}` describing MCP calls; `status` is `inProgress`, `completed`, or `failed`. +- `webSearch` — `{id, query}` for a web search request issued by the agent. + +All items emit two shared lifecycle events: +- `item/started` — emits the full `item` when a new unit of work begins so the UI can render it immediately; the `item.id` in this payload matches the `itemId` used by deltas. +- `item/completed` — sends the final `item` once that work finishes (e.g., after a tool call or message completes); treat this as the authoritative state. + +There are additional item-specific events: +#### agentMessage +- `item/agentMessage/delta` — appends streamed text for the agent message; concatenate `delta` values for the same `itemId` in order to reconstruct the full reply. +#### reasoning +- `item/reasoning/summaryTextDelta` — streams readable reasoning summaries; `summaryIndex` increments when a new summary section opens. +- `item/reasoning/summaryPartAdded` — marks the boundary between reasoning summary sections for an `itemId`; subsequent `summaryTextDelta` entries share the same `summaryIndex`. +- `item/reasoning/textDelta` — streams raw reasoning text (only applicable for e.g. open source models); use `contentIndex` to group deltas that belong together before showing them in the UI. diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs new file mode 100644 index 0000000000..8ed343f03a --- /dev/null +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -0,0 +1,628 @@ +use crate::codex_message_processor::ApiVersion; +use crate::codex_message_processor::PendingInterrupts; +use crate::outgoing_message::OutgoingMessageSender; +use codex_app_server_protocol::AccountRateLimitsUpdatedNotification; +use codex_app_server_protocol::AgentMessageDeltaNotification; +use codex_app_server_protocol::ApplyPatchApprovalParams; +use codex_app_server_protocol::ApplyPatchApprovalResponse; +use codex_app_server_protocol::ApprovalDecision; +use codex_app_server_protocol::CommandAction as V2ParsedCommand; +use codex_app_server_protocol::CommandExecutionOutputDeltaNotification; +use codex_app_server_protocol::CommandExecutionRequestApprovalParams; +use codex_app_server_protocol::CommandExecutionRequestApprovalResponse; +use codex_app_server_protocol::CommandExecutionStatus; +use codex_app_server_protocol::ExecCommandApprovalParams; +use codex_app_server_protocol::ExecCommandApprovalResponse; +use codex_app_server_protocol::InterruptConversationResponse; +use codex_app_server_protocol::ItemCompletedNotification; +use codex_app_server_protocol::ItemStartedNotification; +use codex_app_server_protocol::McpToolCallError; +use codex_app_server_protocol::McpToolCallResult; +use codex_app_server_protocol::McpToolCallStatus; +use codex_app_server_protocol::ReasoningSummaryPartAddedNotification; +use codex_app_server_protocol::ReasoningSummaryTextDeltaNotification; +use codex_app_server_protocol::ReasoningTextDeltaNotification; +use codex_app_server_protocol::SandboxCommandAssessment as V2SandboxCommandAssessment; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::ServerRequestPayload; +use codex_app_server_protocol::ThreadItem; +use codex_app_server_protocol::TurnInterruptResponse; +use codex_core::CodexConversation; +use codex_core::parse_command::shlex_join; +use codex_core::protocol::ApplyPatchApprovalRequestEvent; +use codex_core::protocol::Event; +use codex_core::protocol::EventMsg; +use codex_core::protocol::ExecApprovalRequestEvent; +use codex_core::protocol::ExecCommandEndEvent; +use codex_core::protocol::McpToolCallBeginEvent; +use codex_core::protocol::McpToolCallEndEvent; +use codex_core::protocol::Op; +use codex_core::protocol::ReviewDecision; +use codex_protocol::ConversationId; +use std::convert::TryFrom; +use std::sync::Arc; +use tokio::sync::oneshot; +use tracing::error; + +type JsonValue = serde_json::Value; + +pub(crate) async fn apply_bespoke_event_handling( + event: Event, + conversation_id: ConversationId, + conversation: Arc, + outgoing: Arc, + pending_interrupts: PendingInterrupts, + api_version: ApiVersion, +) { + let Event { id: event_id, msg } = event; + match msg { + EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent { + call_id, + changes, + reason, + grant_root, + }) => { + let params = ApplyPatchApprovalParams { + conversation_id, + call_id, + file_changes: changes, + reason, + grant_root, + }; + let rx = outgoing + .send_request(ServerRequestPayload::ApplyPatchApproval(params)) + .await; + tokio::spawn(async move { + on_patch_approval_response(event_id, rx, conversation).await; + }); + } + EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent { + call_id, + turn_id, + command, + cwd, + reason, + risk, + parsed_cmd, + }) => match api_version { + ApiVersion::V1 => { + let params = ExecCommandApprovalParams { + conversation_id, + call_id, + command, + cwd, + reason, + risk, + parsed_cmd, + }; + let rx = outgoing + .send_request(ServerRequestPayload::ExecCommandApproval(params)) + .await; + tokio::spawn(async move { + on_exec_approval_response(event_id, rx, conversation).await; + }); + } + ApiVersion::V2 => { + let params = CommandExecutionRequestApprovalParams { + thread_id: conversation_id.to_string(), + turn_id: turn_id.clone(), + // Until we migrate the core to be aware of a first class CommandExecutionItem + // and emit the corresponding EventMsg, we repurpose the call_id as the item_id. + item_id: call_id.clone(), + reason, + risk: risk.map(V2SandboxCommandAssessment::from), + }; + let rx = outgoing + .send_request(ServerRequestPayload::CommandExecutionRequestApproval( + params, + )) + .await; + tokio::spawn(async move { + on_command_execution_request_approval_response(event_id, rx, conversation) + .await; + }); + } + }, + // TODO(celia): properly construct McpToolCall TurnItem in core. + EventMsg::McpToolCallBegin(begin_event) => { + let notification = construct_mcp_tool_call_notification(begin_event).await; + outgoing + .send_server_notification(ServerNotification::ItemStarted(notification)) + .await; + } + EventMsg::McpToolCallEnd(end_event) => { + let notification = construct_mcp_tool_call_end_notification(end_event).await; + outgoing + .send_server_notification(ServerNotification::ItemCompleted(notification)) + .await; + } + EventMsg::AgentMessageContentDelta(event) => { + let notification = AgentMessageDeltaNotification { + item_id: event.item_id, + delta: event.delta, + }; + outgoing + .send_server_notification(ServerNotification::AgentMessageDelta(notification)) + .await; + } + EventMsg::ReasoningContentDelta(event) => { + let notification = ReasoningSummaryTextDeltaNotification { + item_id: event.item_id, + delta: event.delta, + summary_index: event.summary_index, + }; + outgoing + .send_server_notification(ServerNotification::ReasoningSummaryTextDelta( + notification, + )) + .await; + } + EventMsg::ReasoningRawContentDelta(event) => { + let notification = ReasoningTextDeltaNotification { + item_id: event.item_id, + delta: event.delta, + content_index: event.content_index, + }; + outgoing + .send_server_notification(ServerNotification::ReasoningTextDelta(notification)) + .await; + } + EventMsg::AgentReasoningSectionBreak(event) => { + let notification = ReasoningSummaryPartAddedNotification { + item_id: event.item_id, + summary_index: event.summary_index, + }; + outgoing + .send_server_notification(ServerNotification::ReasoningSummaryPartAdded( + notification, + )) + .await; + } + EventMsg::TokenCount(token_count_event) => { + if let Some(rate_limits) = token_count_event.rate_limits { + outgoing + .send_server_notification(ServerNotification::AccountRateLimitsUpdated( + AccountRateLimitsUpdatedNotification { + rate_limits: rate_limits.into(), + }, + )) + .await; + } + } + EventMsg::ItemStarted(item_started_event) => { + let item: ThreadItem = item_started_event.item.clone().into(); + let notification = ItemStartedNotification { item }; + outgoing + .send_server_notification(ServerNotification::ItemStarted(notification)) + .await; + } + EventMsg::ItemCompleted(item_completed_event) => { + let item: ThreadItem = item_completed_event.item.clone().into(); + let notification = ItemCompletedNotification { item }; + outgoing + .send_server_notification(ServerNotification::ItemCompleted(notification)) + .await; + } + EventMsg::ExecCommandBegin(exec_command_begin_event) => { + let item = ThreadItem::CommandExecution { + id: exec_command_begin_event.call_id.clone(), + command: shlex_join(&exec_command_begin_event.command), + cwd: exec_command_begin_event.cwd, + status: CommandExecutionStatus::InProgress, + command_actions: exec_command_begin_event + .parsed_cmd + .into_iter() + .map(V2ParsedCommand::from) + .collect(), + aggregated_output: None, + exit_code: None, + duration_ms: None, + }; + let notification = ItemStartedNotification { item }; + outgoing + .send_server_notification(ServerNotification::ItemStarted(notification)) + .await; + } + EventMsg::ExecCommandOutputDelta(exec_command_output_delta_event) => { + let notification = CommandExecutionOutputDeltaNotification { + item_id: exec_command_output_delta_event.call_id.clone(), + delta: String::from_utf8_lossy(&exec_command_output_delta_event.chunk).to_string(), + }; + outgoing + .send_server_notification(ServerNotification::CommandExecutionOutputDelta( + notification, + )) + .await; + } + EventMsg::ExecCommandEnd(exec_command_end_event) => { + let ExecCommandEndEvent { + call_id, + command, + cwd, + parsed_cmd, + aggregated_output, + exit_code, + duration, + .. + } = exec_command_end_event; + + let status = if exit_code == 0 { + CommandExecutionStatus::Completed + } else { + CommandExecutionStatus::Failed + }; + + let aggregated_output = if aggregated_output.is_empty() { + None + } else { + Some(aggregated_output) + }; + + let duration_ms = i64::try_from(duration.as_millis()).unwrap_or(i64::MAX); + + let item = ThreadItem::CommandExecution { + id: call_id, + command: shlex_join(&command), + cwd, + status, + command_actions: parsed_cmd.into_iter().map(V2ParsedCommand::from).collect(), + aggregated_output, + exit_code: Some(exit_code), + duration_ms: Some(duration_ms), + }; + + let notification = ItemCompletedNotification { item }; + outgoing + .send_server_notification(ServerNotification::ItemCompleted(notification)) + .await; + } + // If this is a TurnAborted, reply to any pending interrupt requests. + EventMsg::TurnAborted(turn_aborted_event) => { + let pending = { + let mut map = pending_interrupts.lock().await; + map.remove(&conversation_id).unwrap_or_default() + }; + if !pending.is_empty() { + for (rid, ver) in pending { + match ver { + ApiVersion::V1 => { + let response = InterruptConversationResponse { + abort_reason: turn_aborted_event.reason.clone(), + }; + outgoing.send_response(rid, response).await; + } + ApiVersion::V2 => { + let response = TurnInterruptResponse {}; + outgoing.send_response(rid, response).await; + } + } + } + } + } + + _ => {} + } +} + +async fn on_patch_approval_response( + event_id: String, + receiver: oneshot::Receiver, + codex: Arc, +) { + let response = receiver.await; + let value = match response { + Ok(value) => value, + Err(err) => { + error!("request failed: {err:?}"); + if let Err(submit_err) = codex + .submit(Op::PatchApproval { + id: event_id.clone(), + decision: ReviewDecision::Denied, + }) + .await + { + error!("failed to submit denied PatchApproval after request failure: {submit_err}"); + } + return; + } + }; + + let response = + serde_json::from_value::(value).unwrap_or_else(|err| { + error!("failed to deserialize ApplyPatchApprovalResponse: {err}"); + ApplyPatchApprovalResponse { + decision: ReviewDecision::Denied, + } + }); + + if let Err(err) = codex + .submit(Op::PatchApproval { + id: event_id, + decision: response.decision, + }) + .await + { + error!("failed to submit PatchApproval: {err}"); + } +} + +async fn on_exec_approval_response( + event_id: String, + receiver: oneshot::Receiver, + conversation: Arc, +) { + let response = receiver.await; + let value = match response { + Ok(value) => value, + Err(err) => { + error!("request failed: {err:?}"); + return; + } + }; + + // Try to deserialize `value` and then make the appropriate call to `codex`. + let response = + serde_json::from_value::(value).unwrap_or_else(|err| { + error!("failed to deserialize ExecCommandApprovalResponse: {err}"); + // If we cannot deserialize the response, we deny the request to be + // conservative. + ExecCommandApprovalResponse { + decision: ReviewDecision::Denied, + } + }); + + if let Err(err) = conversation + .submit(Op::ExecApproval { + id: event_id, + decision: response.decision, + }) + .await + { + error!("failed to submit ExecApproval: {err}"); + } +} + +async fn on_command_execution_request_approval_response( + event_id: String, + receiver: oneshot::Receiver, + conversation: Arc, +) { + let response = receiver.await; + let value = match response { + Ok(value) => value, + Err(err) => { + error!("request failed: {err:?}"); + return; + } + }; + + let response = serde_json::from_value::(value) + .unwrap_or_else(|err| { + error!("failed to deserialize CommandExecutionRequestApprovalResponse: {err}"); + CommandExecutionRequestApprovalResponse { + decision: ApprovalDecision::Decline, + accept_settings: None, + } + }); + + let CommandExecutionRequestApprovalResponse { + decision, + accept_settings, + } = response; + + let decision = match (decision, accept_settings) { + (ApprovalDecision::Accept, Some(settings)) if settings.for_session => { + ReviewDecision::ApprovedForSession + } + (ApprovalDecision::Accept, _) => ReviewDecision::Approved, + (ApprovalDecision::Decline, _) => ReviewDecision::Denied, + (ApprovalDecision::Cancel, _) => ReviewDecision::Abort, + }; + if let Err(err) = conversation + .submit(Op::ExecApproval { + id: event_id, + decision, + }) + .await + { + error!("failed to submit ExecApproval: {err}"); + } +} + +/// similar to handle_mcp_tool_call_begin in exec +async fn construct_mcp_tool_call_notification( + begin_event: McpToolCallBeginEvent, +) -> ItemStartedNotification { + let item = ThreadItem::McpToolCall { + id: begin_event.call_id, + server: begin_event.invocation.server, + tool: begin_event.invocation.tool, + status: McpToolCallStatus::InProgress, + arguments: begin_event.invocation.arguments.unwrap_or(JsonValue::Null), + result: None, + error: None, + }; + ItemStartedNotification { item } +} + +/// simiilar to handle_mcp_tool_call_end in exec +async fn construct_mcp_tool_call_end_notification( + end_event: McpToolCallEndEvent, +) -> ItemCompletedNotification { + let status = if end_event.is_success() { + McpToolCallStatus::Completed + } else { + McpToolCallStatus::Failed + }; + + let (result, error) = match &end_event.result { + Ok(value) => ( + Some(McpToolCallResult { + content: value.content.clone(), + structured_content: value.structured_content.clone(), + }), + None, + ), + Err(message) => ( + None, + Some(McpToolCallError { + message: message.clone(), + }), + ), + }; + + let item = ThreadItem::McpToolCall { + id: end_event.call_id, + server: end_event.invocation.server, + tool: end_event.invocation.tool, + status, + arguments: end_event.invocation.arguments.unwrap_or(JsonValue::Null), + result, + error, + }; + ItemCompletedNotification { item } +} + +#[cfg(test)] +mod tests { + use super::*; + use codex_core::protocol::McpInvocation; + use mcp_types::CallToolResult; + use mcp_types::ContentBlock; + use mcp_types::TextContent; + use pretty_assertions::assert_eq; + use serde_json::Value as JsonValue; + use std::time::Duration; + + #[tokio::test] + async fn test_construct_mcp_tool_call_begin_notification_with_args() { + let begin_event = McpToolCallBeginEvent { + call_id: "call_123".to_string(), + invocation: McpInvocation { + server: "codex".to_string(), + tool: "list_mcp_resources".to_string(), + arguments: Some(serde_json::json!({"server": ""})), + }, + }; + + let notification = construct_mcp_tool_call_notification(begin_event.clone()).await; + + let expected = ItemStartedNotification { + item: ThreadItem::McpToolCall { + id: begin_event.call_id, + server: begin_event.invocation.server, + tool: begin_event.invocation.tool, + status: McpToolCallStatus::InProgress, + arguments: serde_json::json!({"server": ""}), + result: None, + error: None, + }, + }; + + assert_eq!(notification, expected); + } + + #[tokio::test] + async fn test_construct_mcp_tool_call_begin_notification_without_args() { + let begin_event = McpToolCallBeginEvent { + call_id: "call_456".to_string(), + invocation: McpInvocation { + server: "codex".to_string(), + tool: "list_mcp_resources".to_string(), + arguments: None, + }, + }; + + let notification = construct_mcp_tool_call_notification(begin_event.clone()).await; + + let expected = ItemStartedNotification { + item: ThreadItem::McpToolCall { + id: begin_event.call_id, + server: begin_event.invocation.server, + tool: begin_event.invocation.tool, + status: McpToolCallStatus::InProgress, + arguments: JsonValue::Null, + result: None, + error: None, + }, + }; + + assert_eq!(notification, expected); + } + + #[tokio::test] + async fn test_construct_mcp_tool_call_end_notification_success() { + let content = vec![ContentBlock::TextContent(TextContent { + annotations: None, + text: "{\"resources\":[]}".to_string(), + r#type: "text".to_string(), + })]; + let result = CallToolResult { + content: content.clone(), + is_error: Some(false), + structured_content: None, + }; + + let end_event = McpToolCallEndEvent { + call_id: "call_789".to_string(), + invocation: McpInvocation { + server: "codex".to_string(), + tool: "list_mcp_resources".to_string(), + arguments: Some(serde_json::json!({"server": ""})), + }, + duration: Duration::from_nanos(92708), + result: Ok(result), + }; + + let notification = construct_mcp_tool_call_end_notification(end_event.clone()).await; + + let expected = ItemCompletedNotification { + item: ThreadItem::McpToolCall { + id: end_event.call_id, + server: end_event.invocation.server, + tool: end_event.invocation.tool, + status: McpToolCallStatus::Completed, + arguments: serde_json::json!({"server": ""}), + result: Some(McpToolCallResult { + content, + structured_content: None, + }), + error: None, + }, + }; + + assert_eq!(notification, expected); + } + + #[tokio::test] + async fn test_construct_mcp_tool_call_end_notification_error() { + let end_event = McpToolCallEndEvent { + call_id: "call_err".to_string(), + invocation: McpInvocation { + server: "codex".to_string(), + tool: "list_mcp_resources".to_string(), + arguments: None, + }, + duration: Duration::from_millis(1), + result: Err("boom".to_string()), + }; + + let notification = construct_mcp_tool_call_end_notification(end_event.clone()).await; + + let expected = ItemCompletedNotification { + item: ThreadItem::McpToolCall { + id: end_event.call_id, + server: end_event.invocation.server, + tool: end_event.invocation.tool, + status: McpToolCallStatus::Failed, + arguments: JsonValue::Null, + result: None, + error: Some(McpToolCallError { + message: "boom".to_string(), + }), + }, + }; + + assert_eq!(notification, expected); + } +} diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs new file mode 100644 index 0000000000..c5ac6a14af --- /dev/null +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -0,0 +1,2909 @@ +use crate::bespoke_event_handling::apply_bespoke_event_handling; +use crate::error_code::INTERNAL_ERROR_CODE; +use crate::error_code::INVALID_REQUEST_ERROR_CODE; +use crate::fuzzy_file_search::run_fuzzy_file_search; +use crate::models::supported_models; +use crate::outgoing_message::OutgoingMessageSender; +use crate::outgoing_message::OutgoingNotification; +use chrono::DateTime; +use chrono::Utc; +use codex_app_server_protocol::Account; +use codex_app_server_protocol::AccountLoginCompletedNotification; +use codex_app_server_protocol::AccountUpdatedNotification; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::AddConversationSubscriptionResponse; +use codex_app_server_protocol::ArchiveConversationParams; +use codex_app_server_protocol::ArchiveConversationResponse; +use codex_app_server_protocol::AskForApproval; +use codex_app_server_protocol::AuthMode; +use codex_app_server_protocol::AuthStatusChangeNotification; +use codex_app_server_protocol::CancelLoginAccountParams; +use codex_app_server_protocol::CancelLoginAccountResponse; +use codex_app_server_protocol::CancelLoginChatGptResponse; +use codex_app_server_protocol::ClientRequest; +use codex_app_server_protocol::ConversationGitInfo; +use codex_app_server_protocol::ConversationSummary; +use codex_app_server_protocol::ExecOneOffCommandParams; +use codex_app_server_protocol::ExecOneOffCommandResponse; +use codex_app_server_protocol::FeedbackUploadParams; +use codex_app_server_protocol::FeedbackUploadResponse; +use codex_app_server_protocol::FuzzyFileSearchParams; +use codex_app_server_protocol::FuzzyFileSearchResponse; +use codex_app_server_protocol::GetAccountParams; +use codex_app_server_protocol::GetAccountRateLimitsResponse; +use codex_app_server_protocol::GetAccountResponse; +use codex_app_server_protocol::GetAuthStatusParams; +use codex_app_server_protocol::GetAuthStatusResponse; +use codex_app_server_protocol::GetConversationSummaryParams; +use codex_app_server_protocol::GetConversationSummaryResponse; +use codex_app_server_protocol::GetUserAgentResponse; +use codex_app_server_protocol::GetUserSavedConfigResponse; +use codex_app_server_protocol::GitDiffToRemoteResponse; +use codex_app_server_protocol::InputItem as WireInputItem; +use codex_app_server_protocol::InterruptConversationParams; +use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::ListConversationsParams; +use codex_app_server_protocol::ListConversationsResponse; +use codex_app_server_protocol::LoginAccountParams; +use codex_app_server_protocol::LoginApiKeyParams; +use codex_app_server_protocol::LoginApiKeyResponse; +use codex_app_server_protocol::LoginChatGptCompleteNotification; +use codex_app_server_protocol::LoginChatGptResponse; +use codex_app_server_protocol::LogoutAccountResponse; +use codex_app_server_protocol::LogoutChatGptResponse; +use codex_app_server_protocol::ModelListParams; +use codex_app_server_protocol::ModelListResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RemoveConversationListenerParams; +use codex_app_server_protocol::RemoveConversationSubscriptionResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ResumeConversationParams; +use codex_app_server_protocol::ResumeConversationResponse; +use codex_app_server_protocol::SandboxMode; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; +use codex_app_server_protocol::SendUserTurnParams; +use codex_app_server_protocol::SendUserTurnResponse; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::SessionConfiguredNotification; +use codex_app_server_protocol::SetDefaultModelParams; +use codex_app_server_protocol::SetDefaultModelResponse; +use codex_app_server_protocol::Thread; +use codex_app_server_protocol::ThreadArchiveParams; +use codex_app_server_protocol::ThreadArchiveResponse; +use codex_app_server_protocol::ThreadItem; +use codex_app_server_protocol::ThreadListParams; +use codex_app_server_protocol::ThreadListResponse; +use codex_app_server_protocol::ThreadResumeParams; +use codex_app_server_protocol::ThreadResumeResponse; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_app_server_protocol::ThreadStartedNotification; +use codex_app_server_protocol::Turn; +use codex_app_server_protocol::TurnInterruptParams; +use codex_app_server_protocol::TurnStartParams; +use codex_app_server_protocol::TurnStartResponse; +use codex_app_server_protocol::TurnStartedNotification; +use codex_app_server_protocol::TurnStatus; +use codex_app_server_protocol::UserInfoResponse; +use codex_app_server_protocol::UserInput as V2UserInput; +use codex_app_server_protocol::UserSavedConfig; +use codex_backend_client::Client as BackendClient; +use codex_core::AuthManager; +use codex_core::CodexConversation; +use codex_core::ConversationManager; +use codex_core::Cursor as RolloutCursor; +use codex_core::INTERACTIVE_SESSION_SOURCES; +use codex_core::InitialHistory; +use codex_core::NewConversation; +use codex_core::RolloutRecorder; +use codex_core::SessionMeta; +use codex_core::auth::CLIENT_ID; +use codex_core::auth::login_with_api_key; +use codex_core::config::Config; +use codex_core::config::ConfigOverrides; +use codex_core::config::ConfigToml; +use codex_core::config::edit::ConfigEditsBuilder; +use codex_core::config_loader::load_config_as_toml; +use codex_core::default_client::get_codex_user_agent; +use codex_core::exec::ExecParams; +use codex_core::exec_env::create_env; +use codex_core::find_conversation_path_by_id_str; +use codex_core::get_platform_sandbox; +use codex_core::git_info::git_diff_to_remote; +use codex_core::parse_cursor; +use codex_core::protocol::EventMsg; +use codex_core::protocol::Op; +use codex_core::read_head_for_summary; +use codex_feedback::CodexFeedback; +use codex_login::ServerOptions as LoginServerOptions; +use codex_login::ShutdownHandle; +use codex_login::run_login_server; +use codex_protocol::ConversationId; +use codex_protocol::config_types::ForcedLoginMethod; +use codex_protocol::items::TurnItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::protocol::GitInfo; +use codex_protocol::protocol::RateLimitSnapshot as CoreRateLimitSnapshot; +use codex_protocol::protocol::RolloutItem; +use codex_protocol::protocol::SessionMetaLine; +use codex_protocol::protocol::USER_MESSAGE_BEGIN; +use codex_protocol::user_input::UserInput as CoreInputItem; +use codex_utils_json_to_toml::json_to_toml; +use std::collections::HashMap; +use std::ffi::OsStr; +use std::io::Error as IoError; +use std::path::Path; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::time::Duration; +use tokio::select; +use tokio::sync::Mutex; +use tokio::sync::oneshot; +use tracing::error; +use tracing::info; +use tracing::warn; +use uuid::Uuid; + +type PendingInterruptQueue = Vec<(RequestId, ApiVersion)>; +pub(crate) type PendingInterrupts = Arc>>; + +// Duration before a ChatGPT login attempt is abandoned. +const LOGIN_CHATGPT_TIMEOUT: Duration = Duration::from_secs(10 * 60); +struct ActiveLogin { + shutdown_handle: ShutdownHandle, + login_id: Uuid, +} + +impl ActiveLogin { + fn drop(&self) { + self.shutdown_handle.shutdown(); + } +} + +/// Handles JSON-RPC messages for Codex conversations. +pub(crate) struct CodexMessageProcessor { + auth_manager: Arc, + conversation_manager: Arc, + outgoing: Arc, + codex_linux_sandbox_exe: Option, + config: Arc, + conversation_listeners: HashMap>, + active_login: Arc>>, + // Queue of pending interrupt requests per conversation. We reply when TurnAborted arrives. + pending_interrupts: PendingInterrupts, + pending_fuzzy_searches: Arc>>>, + feedback: CodexFeedback, +} + +#[derive(Clone, Copy, Debug)] +pub(crate) enum ApiVersion { + V1, + V2, +} + +impl CodexMessageProcessor { + async fn conversation_from_thread_id( + &self, + thread_id: &str, + ) -> Result<(ConversationId, Arc), JSONRPCErrorError> { + // Resolve conversation id from v2 thread id string. + let conversation_id = + ConversationId::from_string(thread_id).map_err(|err| JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid thread id: {err}"), + data: None, + })?; + + let conversation = self + .conversation_manager + .get_conversation(conversation_id) + .await + .map_err(|_| JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("conversation not found: {conversation_id}"), + data: None, + })?; + + Ok((conversation_id, conversation)) + } + pub fn new( + auth_manager: Arc, + conversation_manager: Arc, + outgoing: Arc, + codex_linux_sandbox_exe: Option, + config: Arc, + feedback: CodexFeedback, + ) -> Self { + Self { + auth_manager, + conversation_manager, + outgoing, + codex_linux_sandbox_exe, + config, + conversation_listeners: HashMap::new(), + active_login: Arc::new(Mutex::new(None)), + pending_interrupts: Arc::new(Mutex::new(HashMap::new())), + pending_fuzzy_searches: Arc::new(Mutex::new(HashMap::new())), + feedback, + } + } + + pub async fn process_request(&mut self, request: ClientRequest) { + match request { + ClientRequest::Initialize { .. } => { + panic!("Initialize should be handled in MessageProcessor"); + } + // === v2 Thread/Turn APIs === + ClientRequest::ThreadStart { request_id, params } => { + self.thread_start(request_id, params).await; + } + ClientRequest::ThreadResume { request_id, params } => { + self.thread_resume(request_id, params).await; + } + ClientRequest::ThreadArchive { request_id, params } => { + self.thread_archive(request_id, params).await; + } + ClientRequest::ThreadList { request_id, params } => { + self.thread_list(request_id, params).await; + } + ClientRequest::ThreadCompact { + request_id, + params: _, + } => { + self.send_unimplemented_error(request_id, "thread/compact") + .await; + } + ClientRequest::TurnStart { request_id, params } => { + self.turn_start(request_id, params).await; + } + ClientRequest::TurnInterrupt { request_id, params } => { + self.turn_interrupt(request_id, params).await; + } + ClientRequest::NewConversation { request_id, params } => { + // Do not tokio::spawn() to process new_conversation() + // asynchronously because we need to ensure the conversation is + // created before processing any subsequent messages. + self.process_new_conversation(request_id, params).await; + } + ClientRequest::GetConversationSummary { request_id, params } => { + self.get_conversation_summary(request_id, params).await; + } + ClientRequest::ListConversations { request_id, params } => { + self.handle_list_conversations(request_id, params).await; + } + ClientRequest::ModelList { request_id, params } => { + self.list_models(request_id, params).await; + } + ClientRequest::LoginAccount { request_id, params } => { + self.login_v2(request_id, params).await; + } + ClientRequest::LogoutAccount { + request_id, + params: _, + } => { + self.logout_v2(request_id).await; + } + ClientRequest::CancelLoginAccount { request_id, params } => { + self.cancel_login_v2(request_id, params).await; + } + ClientRequest::GetAccount { request_id, params } => { + self.get_account(request_id, params).await; + } + ClientRequest::ResumeConversation { request_id, params } => { + self.handle_resume_conversation(request_id, params).await; + } + ClientRequest::ArchiveConversation { request_id, params } => { + self.archive_conversation(request_id, params).await; + } + ClientRequest::SendUserMessage { request_id, params } => { + self.send_user_message(request_id, params).await; + } + ClientRequest::SendUserTurn { request_id, params } => { + self.send_user_turn(request_id, params).await; + } + ClientRequest::InterruptConversation { request_id, params } => { + self.interrupt_conversation(request_id, params).await; + } + ClientRequest::AddConversationListener { request_id, params } => { + self.add_conversation_listener(request_id, params).await; + } + ClientRequest::RemoveConversationListener { request_id, params } => { + self.remove_conversation_listener(request_id, params).await; + } + ClientRequest::GitDiffToRemote { request_id, params } => { + self.git_diff_to_origin(request_id, params.cwd).await; + } + ClientRequest::LoginApiKey { request_id, params } => { + self.login_api_key_v1(request_id, params).await; + } + ClientRequest::LoginChatGpt { + request_id, + params: _, + } => { + self.login_chatgpt_v1(request_id).await; + } + ClientRequest::CancelLoginChatGpt { request_id, params } => { + self.cancel_login_chatgpt(request_id, params.login_id).await; + } + ClientRequest::LogoutChatGpt { + request_id, + params: _, + } => { + self.logout_v1(request_id).await; + } + ClientRequest::GetAuthStatus { request_id, params } => { + self.get_auth_status(request_id, params).await; + } + ClientRequest::GetUserSavedConfig { + request_id, + params: _, + } => { + self.get_user_saved_config(request_id).await; + } + ClientRequest::SetDefaultModel { request_id, params } => { + self.set_default_model(request_id, params).await; + } + ClientRequest::GetUserAgent { + request_id, + params: _, + } => { + self.get_user_agent(request_id).await; + } + ClientRequest::UserInfo { + request_id, + params: _, + } => { + self.get_user_info(request_id).await; + } + ClientRequest::FuzzyFileSearch { request_id, params } => { + self.fuzzy_file_search(request_id, params).await; + } + ClientRequest::ExecOneOffCommand { request_id, params } => { + self.exec_one_off_command(request_id, params).await; + } + ClientRequest::GetAccountRateLimits { + request_id, + params: _, + } => { + self.get_account_rate_limits(request_id).await; + } + ClientRequest::FeedbackUpload { request_id, params } => { + self.upload_feedback(request_id, params).await; + } + } + } + + async fn send_unimplemented_error(&self, request_id: RequestId, method: &str) { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("{method} is not implemented yet"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + + async fn login_v2(&mut self, request_id: RequestId, params: LoginAccountParams) { + match params { + LoginAccountParams::ApiKey { api_key } => { + self.login_api_key_v2(request_id, LoginApiKeyParams { api_key }) + .await; + } + LoginAccountParams::Chatgpt => { + self.login_chatgpt_v2(request_id).await; + } + } + } + + async fn login_api_key_common( + &mut self, + params: &LoginApiKeyParams, + ) -> std::result::Result<(), JSONRPCErrorError> { + if matches!( + self.config.forced_login_method, + Some(ForcedLoginMethod::Chatgpt) + ) { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "API key login is disabled. Use ChatGPT login instead.".to_string(), + data: None, + }); + } + + // Cancel any active login attempt. + { + let mut guard = self.active_login.lock().await; + if let Some(active) = guard.take() { + active.drop(); + } + } + + match login_with_api_key( + &self.config.codex_home, + ¶ms.api_key, + self.config.cli_auth_credentials_store_mode, + ) { + Ok(()) => { + self.auth_manager.reload(); + Ok(()) + } + Err(err) => Err(JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to save api key: {err}"), + data: None, + }), + } + } + + async fn login_api_key_v1(&mut self, request_id: RequestId, params: LoginApiKeyParams) { + match self.login_api_key_common(¶ms).await { + Ok(()) => { + self.outgoing + .send_response(request_id, LoginApiKeyResponse {}) + .await; + + let payload = AuthStatusChangeNotification { + auth_method: self.auth_manager.auth().map(|auth| auth.mode), + }; + self.outgoing + .send_server_notification(ServerNotification::AuthStatusChange(payload)) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn login_api_key_v2(&mut self, request_id: RequestId, params: LoginApiKeyParams) { + match self.login_api_key_common(¶ms).await { + Ok(()) => { + let response = codex_app_server_protocol::LoginAccountResponse::ApiKey {}; + self.outgoing.send_response(request_id, response).await; + + let payload_login_completed = AccountLoginCompletedNotification { + login_id: None, + success: true, + error: None, + }; + self.outgoing + .send_server_notification(ServerNotification::AccountLoginCompleted( + payload_login_completed, + )) + .await; + + let payload_v2 = AccountUpdatedNotification { + auth_mode: self.auth_manager.auth().map(|auth| auth.mode), + }; + self.outgoing + .send_server_notification(ServerNotification::AccountUpdated(payload_v2)) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + // Build options for a ChatGPT login attempt; performs validation. + async fn login_chatgpt_common( + &self, + ) -> std::result::Result { + let config = self.config.as_ref(); + + if matches!(config.forced_login_method, Some(ForcedLoginMethod::Api)) { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "ChatGPT login is disabled. Use API key login instead.".to_string(), + data: None, + }); + } + + Ok(LoginServerOptions { + open_browser: false, + ..LoginServerOptions::new( + config.codex_home.clone(), + CLIENT_ID.to_string(), + config.forced_chatgpt_workspace_id.clone(), + config.cli_auth_credentials_store_mode, + ) + }) + } + + // Deprecated in favor of login_chatgpt_v2. + async fn login_chatgpt_v1(&mut self, request_id: RequestId) { + match self.login_chatgpt_common().await { + Ok(opts) => match run_login_server(opts) { + Ok(server) => { + let login_id = Uuid::new_v4(); + let shutdown_handle = server.cancel_handle(); + + // Replace active login if present. + { + let mut guard = self.active_login.lock().await; + if let Some(existing) = guard.take() { + existing.drop(); + } + *guard = Some(ActiveLogin { + shutdown_handle: shutdown_handle.clone(), + login_id, + }); + } + + // Spawn background task to monitor completion. + let outgoing_clone = self.outgoing.clone(); + let active_login = self.active_login.clone(); + let auth_manager = self.auth_manager.clone(); + let auth_url = server.auth_url.clone(); + tokio::spawn(async move { + let (success, error_msg) = match tokio::time::timeout( + LOGIN_CHATGPT_TIMEOUT, + server.block_until_done(), + ) + .await + { + Ok(Ok(())) => (true, None), + Ok(Err(err)) => (false, Some(format!("Login server error: {err}"))), + Err(_elapsed) => { + shutdown_handle.shutdown(); + (false, Some("Login timed out".to_string())) + } + }; + + let payload = LoginChatGptCompleteNotification { + login_id, + success, + error: error_msg.clone(), + }; + outgoing_clone + .send_server_notification(ServerNotification::LoginChatGptComplete( + payload, + )) + .await; + + if success { + auth_manager.reload(); + + // Notify clients with the actual current auth mode. + let current_auth_method = auth_manager.auth().map(|a| a.mode); + let payload = AuthStatusChangeNotification { + auth_method: current_auth_method, + }; + outgoing_clone + .send_server_notification(ServerNotification::AuthStatusChange( + payload, + )) + .await; + } + + // Clear the active login if it matches this attempt. It may have been replaced or cancelled. + let mut guard = active_login.lock().await; + if guard.as_ref().map(|l| l.login_id) == Some(login_id) { + *guard = None; + } + }); + + let response = LoginChatGptResponse { login_id, auth_url }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to start login server: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + }, + Err(err) => { + self.outgoing.send_error(request_id, err).await; + } + } + } + + async fn login_chatgpt_v2(&mut self, request_id: RequestId) { + match self.login_chatgpt_common().await { + Ok(opts) => match run_login_server(opts) { + Ok(server) => { + let login_id = Uuid::new_v4(); + let shutdown_handle = server.cancel_handle(); + + // Replace active login if present. + { + let mut guard = self.active_login.lock().await; + if let Some(existing) = guard.take() { + existing.drop(); + } + *guard = Some(ActiveLogin { + shutdown_handle: shutdown_handle.clone(), + login_id, + }); + } + + // Spawn background task to monitor completion. + let outgoing_clone = self.outgoing.clone(); + let active_login = self.active_login.clone(); + let auth_manager = self.auth_manager.clone(); + let auth_url = server.auth_url.clone(); + tokio::spawn(async move { + let (success, error_msg) = match tokio::time::timeout( + LOGIN_CHATGPT_TIMEOUT, + server.block_until_done(), + ) + .await + { + Ok(Ok(())) => (true, None), + Ok(Err(err)) => (false, Some(format!("Login server error: {err}"))), + Err(_elapsed) => { + shutdown_handle.shutdown(); + (false, Some("Login timed out".to_string())) + } + }; + + let payload_v2 = AccountLoginCompletedNotification { + login_id: Some(login_id.to_string()), + success, + error: error_msg, + }; + outgoing_clone + .send_server_notification(ServerNotification::AccountLoginCompleted( + payload_v2, + )) + .await; + + if success { + auth_manager.reload(); + + // Notify clients with the actual current auth mode. + let current_auth_method = auth_manager.auth().map(|a| a.mode); + let payload_v2 = AccountUpdatedNotification { + auth_mode: current_auth_method, + }; + outgoing_clone + .send_server_notification(ServerNotification::AccountUpdated( + payload_v2, + )) + .await; + } + + // Clear the active login if it matches this attempt. It may have been replaced or cancelled. + let mut guard = active_login.lock().await; + if guard.as_ref().map(|l| l.login_id) == Some(login_id) { + *guard = None; + } + }); + + let response = codex_app_server_protocol::LoginAccountResponse::Chatgpt { + login_id: login_id.to_string(), + auth_url, + }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to start login server: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + }, + Err(err) => { + self.outgoing.send_error(request_id, err).await; + } + } + } + + async fn cancel_login_chatgpt_common( + &mut self, + login_id: Uuid, + ) -> std::result::Result<(), JSONRPCErrorError> { + let mut guard = self.active_login.lock().await; + if guard.as_ref().map(|l| l.login_id) == Some(login_id) { + if let Some(active) = guard.take() { + active.drop(); + } + Ok(()) + } else { + Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("login id not found: {login_id}"), + data: None, + }) + } + } + + async fn cancel_login_chatgpt(&mut self, request_id: RequestId, login_id: Uuid) { + match self.cancel_login_chatgpt_common(login_id).await { + Ok(()) => { + self.outgoing + .send_response(request_id, CancelLoginChatGptResponse {}) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn cancel_login_v2(&mut self, request_id: RequestId, params: CancelLoginAccountParams) { + let login_id = params.login_id; + match Uuid::parse_str(&login_id) { + Ok(uuid) => match self.cancel_login_chatgpt_common(uuid).await { + Ok(()) => { + self.outgoing + .send_response(request_id, CancelLoginAccountResponse {}) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + }, + Err(_) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid login id: {login_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn logout_common(&mut self) -> std::result::Result, JSONRPCErrorError> { + // Cancel any active login attempt. + { + let mut guard = self.active_login.lock().await; + if let Some(active) = guard.take() { + active.drop(); + } + } + + if let Err(err) = self.auth_manager.logout() { + return Err(JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("logout failed: {err}"), + data: None, + }); + } + + // Reflect the current auth method after logout (likely None). + Ok(self.auth_manager.auth().map(|auth| auth.mode)) + } + + async fn logout_v1(&mut self, request_id: RequestId) { + match self.logout_common().await { + Ok(current_auth_method) => { + self.outgoing + .send_response(request_id, LogoutChatGptResponse {}) + .await; + + let payload = AuthStatusChangeNotification { + auth_method: current_auth_method, + }; + self.outgoing + .send_server_notification(ServerNotification::AuthStatusChange(payload)) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn logout_v2(&mut self, request_id: RequestId) { + match self.logout_common().await { + Ok(current_auth_method) => { + self.outgoing + .send_response(request_id, LogoutAccountResponse {}) + .await; + + let payload_v2 = AccountUpdatedNotification { + auth_mode: current_auth_method, + }; + self.outgoing + .send_server_notification(ServerNotification::AccountUpdated(payload_v2)) + .await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn refresh_token_if_requested(&self, do_refresh: bool) { + if do_refresh && let Err(err) = self.auth_manager.refresh_token().await { + tracing::warn!("failed to refresh token whilte getting account: {err}"); + } + } + + async fn get_auth_status(&self, request_id: RequestId, params: GetAuthStatusParams) { + let include_token = params.include_token.unwrap_or(false); + let do_refresh = params.refresh_token.unwrap_or(false); + + self.refresh_token_if_requested(do_refresh).await; + + // Determine whether auth is required based on the active model provider. + // If a custom provider is configured with `requires_openai_auth == false`, + // then no auth step is required; otherwise, default to requiring auth. + let requires_openai_auth = self.config.model_provider.requires_openai_auth; + + let response = if !requires_openai_auth { + GetAuthStatusResponse { + auth_method: None, + auth_token: None, + requires_openai_auth: Some(false), + } + } else { + match self.auth_manager.auth() { + Some(auth) => { + let auth_mode = auth.mode; + let (reported_auth_method, token_opt) = match auth.get_token().await { + Ok(token) if !token.is_empty() => { + let tok = if include_token { Some(token) } else { None }; + (Some(auth_mode), tok) + } + Ok(_) => (None, None), + Err(err) => { + tracing::warn!("failed to get token for auth status: {err}"); + (None, None) + } + }; + GetAuthStatusResponse { + auth_method: reported_auth_method, + auth_token: token_opt, + requires_openai_auth: Some(true), + } + } + None => GetAuthStatusResponse { + auth_method: None, + auth_token: None, + requires_openai_auth: Some(true), + }, + } + }; + + self.outgoing.send_response(request_id, response).await; + } + + async fn get_account(&self, request_id: RequestId, params: GetAccountParams) { + let do_refresh = params.refresh_token; + + self.refresh_token_if_requested(do_refresh).await; + + // Whether auth is required for the active model provider. + let requires_openai_auth = self.config.model_provider.requires_openai_auth; + + if !requires_openai_auth { + let response = GetAccountResponse { + account: None, + requires_openai_auth, + }; + self.outgoing.send_response(request_id, response).await; + return; + } + + let account = match self.auth_manager.auth() { + Some(auth) => Some(match auth.mode { + AuthMode::ApiKey => Account::ApiKey {}, + AuthMode::ChatGPT => { + let email = auth.get_account_email(); + let plan_type = auth.account_plan_type(); + + match (email, plan_type) { + (Some(email), Some(plan_type)) => Account::Chatgpt { email, plan_type }, + _ => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: + "email and plan type are required for chatgpt authentication" + .to_string(), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + } + } + }), + None => None, + }; + + let response = GetAccountResponse { + account, + requires_openai_auth, + }; + self.outgoing.send_response(request_id, response).await; + } + + async fn get_user_agent(&self, request_id: RequestId) { + let user_agent = get_codex_user_agent(); + let response = GetUserAgentResponse { user_agent }; + self.outgoing.send_response(request_id, response).await; + } + + async fn get_account_rate_limits(&self, request_id: RequestId) { + match self.fetch_account_rate_limits().await { + Ok(rate_limits) => { + let response = GetAccountRateLimitsResponse { + rate_limits: rate_limits.into(), + }; + self.outgoing.send_response(request_id, response).await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn fetch_account_rate_limits(&self) -> Result { + let Some(auth) = self.auth_manager.auth() else { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "codex account authentication required to read rate limits".to_string(), + data: None, + }); + }; + + if auth.mode != AuthMode::ChatGPT { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "chatgpt authentication required to read rate limits".to_string(), + data: None, + }); + } + + let client = BackendClient::from_auth(self.config.chatgpt_base_url.clone(), &auth) + .await + .map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to construct backend client: {err}"), + data: None, + })?; + + client + .get_rate_limits() + .await + .map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to fetch codex rate limits: {err}"), + data: None, + }) + } + + async fn get_user_saved_config(&self, request_id: RequestId) { + let toml_value = match load_config_as_toml(&self.config.codex_home).await { + Ok(val) => val, + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to load config.toml: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let cfg: ConfigToml = match toml_value.try_into() { + Ok(cfg) => cfg, + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to parse config.toml: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let user_saved_config: UserSavedConfig = cfg.into(); + + let response = GetUserSavedConfigResponse { + config: user_saved_config, + }; + self.outgoing.send_response(request_id, response).await; + } + + async fn get_user_info(&self, request_id: RequestId) { + // Read alleged user email from cached auth (best-effort; not verified). + let alleged_user_email = self.auth_manager.auth().and_then(|a| a.get_account_email()); + + let response = UserInfoResponse { alleged_user_email }; + self.outgoing.send_response(request_id, response).await; + } + + async fn set_default_model(&self, request_id: RequestId, params: SetDefaultModelParams) { + let SetDefaultModelParams { + model, + reasoning_effort, + } = params; + + match ConfigEditsBuilder::new(&self.config.codex_home) + .with_profile(self.config.active_profile.as_deref()) + .set_model(model.as_deref(), reasoning_effort) + .apply() + .await + { + Ok(()) => { + let response = SetDefaultModelResponse {}; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to persist model selection: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn exec_one_off_command(&self, request_id: RequestId, params: ExecOneOffCommandParams) { + tracing::debug!("ExecOneOffCommand params: {params:?}"); + + if params.command.is_empty() { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "command must not be empty".to_string(), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + + let cwd = params.cwd.unwrap_or_else(|| self.config.cwd.clone()); + let env = create_env(&self.config.shell_environment_policy); + let timeout_ms = params.timeout_ms; + let exec_params = ExecParams { + command: params.command, + cwd, + timeout_ms, + env, + with_escalated_permissions: None, + justification: None, + arg0: None, + }; + + let effective_policy = params + .sandbox_policy + .unwrap_or_else(|| self.config.sandbox_policy.clone()); + + let sandbox_type = match &effective_policy { + codex_core::protocol::SandboxPolicy::DangerFullAccess => { + codex_core::exec::SandboxType::None + } + _ => get_platform_sandbox().unwrap_or(codex_core::exec::SandboxType::None), + }; + tracing::debug!("Sandbox type: {sandbox_type:?}"); + let codex_linux_sandbox_exe = self.config.codex_linux_sandbox_exe.clone(); + let outgoing = self.outgoing.clone(); + let req_id = request_id; + let sandbox_cwd = self.config.cwd.clone(); + + tokio::spawn(async move { + match codex_core::exec::process_exec_tool_call( + exec_params, + sandbox_type, + &effective_policy, + sandbox_cwd.as_path(), + &codex_linux_sandbox_exe, + None, + ) + .await + { + Ok(output) => { + let response = ExecOneOffCommandResponse { + exit_code: output.exit_code, + stdout: output.stdout.text, + stderr: output.stderr.text, + }; + outgoing.send_response(req_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("exec failed: {err}"), + data: None, + }; + outgoing.send_error(req_id, error).await; + } + } + }); + } + + async fn process_new_conversation(&self, request_id: RequestId, params: NewConversationParams) { + let NewConversationParams { + model, + model_provider, + profile, + cwd, + approval_policy, + sandbox: sandbox_mode, + config: cli_overrides, + base_instructions, + developer_instructions, + compact_prompt, + include_apply_patch_tool, + } = params; + + let overrides = ConfigOverrides { + model, + config_profile: profile, + cwd: cwd.map(PathBuf::from), + approval_policy, + sandbox_mode, + model_provider, + codex_linux_sandbox_exe: self.codex_linux_sandbox_exe.clone(), + base_instructions, + developer_instructions, + compact_prompt, + include_apply_patch_tool, + ..Default::default() + }; + + let config = match derive_config_from_params(overrides, cli_overrides).await { + Ok(config) => config, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("error deriving config: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + match self.conversation_manager.new_conversation(config).await { + Ok(conversation_id) => { + let NewConversation { + conversation_id, + session_configured, + .. + } = conversation_id; + let response = NewConversationResponse { + conversation_id, + model: session_configured.model, + reasoning_effort: session_configured.reasoning_effort, + rollout_path: session_configured.rollout_path, + }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("error creating conversation: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn thread_start(&mut self, request_id: RequestId, params: ThreadStartParams) { + let overrides = self.build_thread_config_overrides( + params.model, + params.model_provider, + params.cwd, + params.approval_policy, + params.sandbox, + params.base_instructions, + params.developer_instructions, + ); + + let config = match derive_config_from_params(overrides, params.config).await { + Ok(config) => config, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("error deriving config: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + match self.conversation_manager.new_conversation(config).await { + Ok(new_conv) => { + let conversation_id = new_conv.conversation_id; + let rollout_path = new_conv.session_configured.rollout_path.clone(); + let fallback_provider = self.config.model_provider_id.as_str(); + + // A bit hacky, but the summary contains a lot of useful information for the thread + // that unfortunately does not get returned from conversation_manager.new_conversation(). + let thread = match read_summary_from_rollout( + rollout_path.as_path(), + fallback_provider, + ) + .await + { + Ok(summary) => summary_to_thread(summary), + Err(err) => { + self.send_internal_error( + request_id, + format!( + "failed to load rollout `{}` for conversation {conversation_id}: {err}", + rollout_path.display() + ), + ) + .await; + return; + } + }; + + let response = ThreadStartResponse { + thread: thread.clone(), + }; + + // Auto-attach a conversation listener when starting a thread. + // Use the same behavior as the v1 API with experimental_raw_events=false. + if let Err(err) = self + .attach_conversation_listener(conversation_id, false, ApiVersion::V2) + .await + { + tracing::warn!( + "failed to attach listener for conversation {}: {}", + conversation_id, + err.message + ); + } + + self.outgoing.send_response(request_id, response).await; + + let notif = ThreadStartedNotification { thread }; + self.outgoing + .send_server_notification(ServerNotification::ThreadStarted(notif)) + .await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("error creating thread: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + #[allow(clippy::too_many_arguments)] + fn build_thread_config_overrides( + &self, + model: Option, + model_provider: Option, + cwd: Option, + approval_policy: Option, + sandbox: Option, + base_instructions: Option, + developer_instructions: Option, + ) -> ConfigOverrides { + ConfigOverrides { + model, + model_provider, + cwd: cwd.map(PathBuf::from), + approval_policy: approval_policy + .map(codex_app_server_protocol::AskForApproval::to_core), + sandbox_mode: sandbox.map(SandboxMode::to_core), + codex_linux_sandbox_exe: self.codex_linux_sandbox_exe.clone(), + base_instructions, + developer_instructions, + ..Default::default() + } + } + + async fn thread_archive(&mut self, request_id: RequestId, params: ThreadArchiveParams) { + let conversation_id = match ConversationId::from_string(¶ms.thread_id) { + Ok(id) => id, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid thread id: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let rollout_path = match find_conversation_path_by_id_str( + &self.config.codex_home, + &conversation_id.to_string(), + ) + .await + { + Ok(Some(p)) => p, + Ok(None) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("no rollout found for conversation id {conversation_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("failed to locate conversation id {conversation_id}: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + match self + .archive_conversation_common(conversation_id, &rollout_path) + .await + { + Ok(()) => { + let response = ThreadArchiveResponse {}; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + self.outgoing.send_error(request_id, err).await; + } + } + } + + async fn thread_list(&self, request_id: RequestId, params: ThreadListParams) { + let ThreadListParams { + cursor, + limit, + model_providers, + } = params; + + let page_size = limit.unwrap_or(25).max(1) as usize; + + let (summaries, next_cursor) = match self + .list_conversations_common(page_size, cursor, model_providers) + .await + { + Ok(r) => r, + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let data = summaries.into_iter().map(summary_to_thread).collect(); + + let response = ThreadListResponse { data, next_cursor }; + self.outgoing.send_response(request_id, response).await; + } + + async fn thread_resume(&mut self, request_id: RequestId, params: ThreadResumeParams) { + let ThreadResumeParams { + thread_id, + history, + path, + model, + model_provider, + cwd, + approval_policy, + sandbox, + config: cli_overrides, + base_instructions, + developer_instructions, + } = params; + + let overrides_requested = model.is_some() + || model_provider.is_some() + || cwd.is_some() + || approval_policy.is_some() + || sandbox.is_some() + || cli_overrides.is_some() + || base_instructions.is_some() + || developer_instructions.is_some(); + + let config = if overrides_requested { + let overrides = self.build_thread_config_overrides( + model, + model_provider, + cwd, + approval_policy, + sandbox, + base_instructions, + developer_instructions, + ); + match derive_config_from_params(overrides, cli_overrides).await { + Ok(config) => config, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("error deriving config: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + } + } else { + self.config.as_ref().clone() + }; + + let conversation_history = if let Some(history) = history { + if history.is_empty() { + self.send_invalid_request_error( + request_id, + "history must not be empty".to_string(), + ) + .await; + return; + } + InitialHistory::Forked(history.into_iter().map(RolloutItem::ResponseItem).collect()) + } else if let Some(path) = path { + match RolloutRecorder::get_rollout_history(&path).await { + Ok(initial_history) => initial_history, + Err(err) => { + self.send_invalid_request_error( + request_id, + format!("failed to load rollout `{}`: {err}", path.display()), + ) + .await; + return; + } + } + } else { + let existing_conversation_id = match ConversationId::from_string(&thread_id) { + Ok(id) => id, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid thread id: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let path = match find_conversation_path_by_id_str( + &self.config.codex_home, + &existing_conversation_id.to_string(), + ) + .await + { + Ok(Some(p)) => p, + Ok(None) => { + self.send_invalid_request_error( + request_id, + format!("no rollout found for conversation id {existing_conversation_id}"), + ) + .await; + return; + } + Err(err) => { + self.send_invalid_request_error( + request_id, + format!( + "failed to locate conversation id {existing_conversation_id}: {err}" + ), + ) + .await; + return; + } + }; + + match RolloutRecorder::get_rollout_history(&path).await { + Ok(initial_history) => initial_history, + Err(err) => { + self.send_invalid_request_error( + request_id, + format!("failed to load rollout `{}`: {err}", path.display()), + ) + .await; + return; + } + } + }; + + let fallback_model_provider = config.model_provider_id.clone(); + + match self + .conversation_manager + .resume_conversation_with_history( + config, + conversation_history, + self.auth_manager.clone(), + ) + .await + { + Ok(NewConversation { + conversation_id, + session_configured, + .. + }) => { + // Auto-attach a conversation listener when resuming a thread. + if let Err(err) = self + .attach_conversation_listener(conversation_id, false, ApiVersion::V2) + .await + { + tracing::warn!( + "failed to attach listener for conversation {}: {}", + conversation_id, + err.message + ); + } + + let thread = match read_summary_from_rollout( + session_configured.rollout_path.as_path(), + fallback_model_provider.as_str(), + ) + .await + { + Ok(summary) => summary_to_thread(summary), + Err(err) => { + self.send_internal_error( + request_id, + format!( + "failed to load rollout `{}` for conversation {conversation_id}: {err}", + session_configured.rollout_path.display() + ), + ) + .await; + return; + } + }; + let response = ThreadResumeResponse { thread }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("error resuming thread: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn get_conversation_summary( + &self, + request_id: RequestId, + params: GetConversationSummaryParams, + ) { + let path = match params { + GetConversationSummaryParams::RolloutPath { rollout_path } => { + if rollout_path.is_relative() { + self.config.codex_home.join(&rollout_path) + } else { + rollout_path + } + } + GetConversationSummaryParams::ConversationId { conversation_id } => { + match codex_core::find_conversation_path_by_id_str( + &self.config.codex_home, + &conversation_id.to_string(), + ) + .await + { + Ok(Some(p)) => p, + _ => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!( + "no rollout found for conversation id {conversation_id}" + ), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + } + } + }; + + let fallback_provider = self.config.model_provider_id.as_str(); + + match read_summary_from_rollout(&path, fallback_provider).await { + Ok(summary) => { + let response = GetConversationSummaryResponse { summary }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!( + "failed to load conversation summary from {}: {}", + path.display(), + err + ), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn handle_list_conversations( + &self, + request_id: RequestId, + params: ListConversationsParams, + ) { + let ListConversationsParams { + page_size, + cursor, + model_providers, + } = params; + let page_size = page_size.unwrap_or(25).max(1); + + match self + .list_conversations_common(page_size, cursor, model_providers) + .await + { + Ok((items, next_cursor)) => { + let response = ListConversationsResponse { items, next_cursor }; + self.outgoing.send_response(request_id, response).await; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + } + }; + } + + async fn list_conversations_common( + &self, + page_size: usize, + cursor: Option, + model_providers: Option>, + ) -> Result<(Vec, Option), JSONRPCErrorError> { + let cursor_obj: Option = cursor.as_ref().and_then(|s| parse_cursor(s)); + let cursor_ref = cursor_obj.as_ref(); + + let model_provider_filter = match model_providers { + Some(providers) => { + if providers.is_empty() { + None + } else { + Some(providers) + } + } + None => Some(vec![self.config.model_provider_id.clone()]), + }; + let fallback_provider = self.config.model_provider_id.clone(); + + let page = match RolloutRecorder::list_conversations( + &self.config.codex_home, + page_size, + cursor_ref, + INTERACTIVE_SESSION_SOURCES, + model_provider_filter.as_deref(), + fallback_provider.as_str(), + ) + .await + { + Ok(p) => p, + Err(err) => { + return Err(JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to list conversations: {err}"), + data: None, + }); + } + }; + + let items = page + .items + .into_iter() + .filter_map(|it| { + let session_meta_line = it.head.first().and_then(|first| { + serde_json::from_value::(first.clone()).ok() + })?; + extract_conversation_summary( + it.path, + &it.head, + &session_meta_line.meta, + session_meta_line.git.as_ref(), + fallback_provider.as_str(), + ) + }) + .collect::>(); + + // Encode next_cursor as a plain string + let next_cursor = page + .next_cursor + .and_then(|cursor| serde_json::to_value(&cursor).ok()) + .and_then(|value| value.as_str().map(str::to_owned)); + + Ok((items, next_cursor)) + } + + async fn list_models(&self, request_id: RequestId, params: ModelListParams) { + let ModelListParams { limit, cursor } = params; + let auth_mode = self.auth_manager.auth().map(|auth| auth.mode); + let models = supported_models(auth_mode); + let total = models.len(); + + if total == 0 { + let response = ModelListResponse { + data: Vec::new(), + next_cursor: None, + }; + self.outgoing.send_response(request_id, response).await; + return; + } + + let effective_limit = limit.unwrap_or(total as u32).max(1) as usize; + let effective_limit = effective_limit.min(total); + let start = match cursor { + Some(cursor) => match cursor.parse::() { + Ok(idx) => idx, + Err(_) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid cursor: {cursor}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }, + None => 0, + }; + + if start > total { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("cursor {start} exceeds total models {total}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + + let end = start.saturating_add(effective_limit).min(total); + let items = models[start..end].to_vec(); + let next_cursor = if end < total { + Some(end.to_string()) + } else { + None + }; + let response = ModelListResponse { + data: items, + next_cursor, + }; + self.outgoing.send_response(request_id, response).await; + } + + async fn handle_resume_conversation( + &self, + request_id: RequestId, + params: ResumeConversationParams, + ) { + let ResumeConversationParams { + path, + conversation_id, + history, + overrides, + } = params; + + // Derive a Config using the same logic as new conversation, honoring overrides if provided. + let config = match overrides { + Some(overrides) => { + let NewConversationParams { + model, + model_provider, + profile, + cwd, + approval_policy, + sandbox: sandbox_mode, + config: cli_overrides, + base_instructions, + developer_instructions, + compact_prompt, + include_apply_patch_tool, + } = overrides; + + let overrides = ConfigOverrides { + model, + config_profile: profile, + cwd: cwd.map(PathBuf::from), + approval_policy, + sandbox_mode, + model_provider, + codex_linux_sandbox_exe: self.codex_linux_sandbox_exe.clone(), + base_instructions, + developer_instructions, + compact_prompt, + include_apply_patch_tool, + ..Default::default() + }; + + derive_config_from_params(overrides, cli_overrides).await + } + None => Ok(self.config.as_ref().clone()), + }; + let config = match config { + Ok(cfg) => cfg, + Err(err) => { + self.send_invalid_request_error( + request_id, + format!("error deriving config: {err}"), + ) + .await; + return; + } + }; + + let conversation_history = if let Some(path) = path { + match RolloutRecorder::get_rollout_history(&path).await { + Ok(initial_history) => initial_history, + Err(err) => { + self.send_invalid_request_error( + request_id, + format!("failed to load rollout `{}`: {err}", path.display()), + ) + .await; + return; + } + } + } else if let Some(conversation_id) = conversation_id { + match find_conversation_path_by_id_str( + &self.config.codex_home, + &conversation_id.to_string(), + ) + .await + { + Ok(Some(found_path)) => { + match RolloutRecorder::get_rollout_history(&found_path).await { + Ok(initial_history) => initial_history, + Err(err) => { + self.send_invalid_request_error( + request_id, + format!( + "failed to load rollout `{}` for conversation {conversation_id}: {err}", + found_path.display() + ), + ).await; + return; + } + } + } + Ok(None) => { + self.send_invalid_request_error( + request_id, + format!("no rollout found for conversation id {conversation_id}"), + ) + .await; + return; + } + Err(err) => { + self.send_invalid_request_error( + request_id, + format!("failed to locate conversation id {conversation_id}: {err}"), + ) + .await; + return; + } + } + } else { + match history { + Some(history) if !history.is_empty() => InitialHistory::Forked( + history.into_iter().map(RolloutItem::ResponseItem).collect(), + ), + Some(_) | None => { + self.send_invalid_request_error( + request_id, + "either path, conversation id or non empty history must be provided" + .to_string(), + ) + .await; + return; + } + } + }; + + match self + .conversation_manager + .resume_conversation_with_history( + config, + conversation_history, + self.auth_manager.clone(), + ) + .await + { + Ok(NewConversation { + conversation_id, + session_configured, + .. + }) => { + self.outgoing + .send_server_notification(ServerNotification::SessionConfigured( + SessionConfiguredNotification { + session_id: session_configured.session_id, + model: session_configured.model.clone(), + reasoning_effort: session_configured.reasoning_effort, + history_log_id: session_configured.history_log_id, + history_entry_count: session_configured.history_entry_count, + initial_messages: session_configured.initial_messages.clone(), + rollout_path: session_configured.rollout_path.clone(), + }, + )) + .await; + let initial_messages = session_configured + .initial_messages + .map(|msgs| msgs.into_iter().collect()); + + // Reply with conversation id + model and initial messages (when present) + let response = ResumeConversationResponse { + conversation_id, + model: session_configured.model.clone(), + initial_messages, + rollout_path: session_configured.rollout_path.clone(), + }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("error resuming conversation: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn send_invalid_request_error(&self, request_id: RequestId, message: String) { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message, + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + + async fn send_internal_error(&self, request_id: RequestId, message: String) { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message, + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + + async fn archive_conversation( + &mut self, + request_id: RequestId, + params: ArchiveConversationParams, + ) { + let ArchiveConversationParams { + conversation_id, + rollout_path, + } = params; + + match self + .archive_conversation_common(conversation_id, &rollout_path) + .await + { + Ok(()) => { + tracing::info!("thread/archive succeeded for {conversation_id}"); + let response = ArchiveConversationResponse {}; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + tracing::warn!( + "thread/archive failed for {conversation_id}: {}", + err.message + ); + self.outgoing.send_error(request_id, err).await; + } + } + } + + async fn archive_conversation_common( + &mut self, + conversation_id: ConversationId, + rollout_path: &Path, + ) -> Result<(), JSONRPCErrorError> { + // Verify rollout_path is under sessions dir. + let rollout_folder = self.config.codex_home.join(codex_core::SESSIONS_SUBDIR); + + let canonical_sessions_dir = match tokio::fs::canonicalize(&rollout_folder).await { + Ok(path) => path, + Err(err) => { + return Err(JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!( + "failed to archive conversation: unable to resolve sessions directory: {err}" + ), + data: None, + }); + } + }; + let canonical_rollout_path = tokio::fs::canonicalize(rollout_path).await; + let canonical_rollout_path = if let Ok(path) = canonical_rollout_path + && path.starts_with(&canonical_sessions_dir) + { + path + } else { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!( + "rollout path `{}` must be in sessions directory", + rollout_path.display() + ), + data: None, + }); + }; + + // Verify file name matches conversation id. + let required_suffix = format!("{conversation_id}.jsonl"); + let Some(file_name) = canonical_rollout_path.file_name().map(OsStr::to_owned) else { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!( + "rollout path `{}` missing file name", + rollout_path.display() + ), + data: None, + }); + }; + if !file_name + .to_string_lossy() + .ends_with(required_suffix.as_str()) + { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!( + "rollout path `{}` does not match conversation id {conversation_id}", + rollout_path.display() + ), + data: None, + }); + } + + // If the conversation is active, request shutdown and wait briefly. + if let Some(conversation) = self + .conversation_manager + .remove_conversation(&conversation_id) + .await + { + info!("conversation {conversation_id} was active; shutting down"); + let conversation_clone = conversation.clone(); + let notify = Arc::new(tokio::sync::Notify::new()); + let notify_clone = notify.clone(); + + // Establish the listener for ShutdownComplete before submitting + // Shutdown so it is not missed. + let is_shutdown = tokio::spawn(async move { + // Create the notified future outside the loop to avoid losing notifications. + let notified = notify_clone.notified(); + tokio::pin!(notified); + loop { + select! { + _ = &mut notified => { break; } + event = conversation_clone.next_event() => { + match event { + Ok(event) => { + if matches!(event.msg, EventMsg::ShutdownComplete) { break; } + } + // Break on errors to avoid tight loops when the agent loop has exited. + Err(_) => { break; } + } + } + } + } + }); + // Request shutdown. + match conversation.submit(Op::Shutdown).await { + Ok(_) => { + // Successfully submitted Shutdown; wait before proceeding. + select! { + _ = is_shutdown => { + // Normal shutdown: proceed with archive. + } + _ = tokio::time::sleep(Duration::from_secs(10)) => { + warn!("conversation {conversation_id} shutdown timed out; proceeding with archive"); + // Wake any waiter; use notify_waiters to avoid missing the signal. + notify.notify_waiters(); + // Perhaps we lost a shutdown race, so let's continue to + // clean up the .jsonl file. + } + } + } + Err(err) => { + error!("failed to submit Shutdown to conversation {conversation_id}: {err}"); + notify.notify_waiters(); + } + } + } + + // Move the rollout file to archived. + let result: std::io::Result<()> = async { + let archive_folder = self + .config + .codex_home + .join(codex_core::ARCHIVED_SESSIONS_SUBDIR); + tokio::fs::create_dir_all(&archive_folder).await?; + tokio::fs::rename(&canonical_rollout_path, &archive_folder.join(&file_name)).await?; + Ok(()) + } + .await; + + result.map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to archive conversation: {err}"), + data: None, + }) + } + + async fn send_user_message(&self, request_id: RequestId, params: SendUserMessageParams) { + let SendUserMessageParams { + conversation_id, + items, + } = params; + let Ok(conversation) = self + .conversation_manager + .get_conversation(conversation_id) + .await + else { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("conversation not found: {conversation_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + }; + + let mapped_items: Vec = items + .into_iter() + .map(|item| match item { + WireInputItem::Text { text } => CoreInputItem::Text { text }, + WireInputItem::Image { image_url } => CoreInputItem::Image { image_url }, + WireInputItem::LocalImage { path } => CoreInputItem::LocalImage { path }, + }) + .collect(); + + // Submit user input to the conversation. + let _ = conversation + .submit(Op::UserInput { + items: mapped_items, + }) + .await; + + // Acknowledge with an empty result. + self.outgoing + .send_response(request_id, SendUserMessageResponse {}) + .await; + } + + async fn send_user_turn(&self, request_id: RequestId, params: SendUserTurnParams) { + let SendUserTurnParams { + conversation_id, + items, + cwd, + approval_policy, + sandbox_policy, + model, + effort, + summary, + } = params; + + let Ok(conversation) = self + .conversation_manager + .get_conversation(conversation_id) + .await + else { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("conversation not found: {conversation_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + }; + + let mapped_items: Vec = items + .into_iter() + .map(|item| match item { + WireInputItem::Text { text } => CoreInputItem::Text { text }, + WireInputItem::Image { image_url } => CoreInputItem::Image { image_url }, + WireInputItem::LocalImage { path } => CoreInputItem::LocalImage { path }, + }) + .collect(); + + let _ = conversation + .submit(Op::UserTurn { + items: mapped_items, + cwd, + approval_policy, + sandbox_policy, + model, + effort, + summary, + final_output_json_schema: None, + }) + .await; + + self.outgoing + .send_response(request_id, SendUserTurnResponse {}) + .await; + } + + async fn interrupt_conversation( + &mut self, + request_id: RequestId, + params: InterruptConversationParams, + ) { + let InterruptConversationParams { conversation_id } = params; + let Ok(conversation) = self + .conversation_manager + .get_conversation(conversation_id) + .await + else { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("conversation not found: {conversation_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + }; + + // Record the pending interrupt so we can reply when TurnAborted arrives. + { + let mut map = self.pending_interrupts.lock().await; + map.entry(conversation_id) + .or_default() + .push((request_id, ApiVersion::V1)); + } + + // Submit the interrupt; we'll respond upon TurnAborted. + let _ = conversation.submit(Op::Interrupt).await; + } + + async fn turn_start(&self, request_id: RequestId, params: TurnStartParams) { + let (_, conversation) = match self.conversation_from_thread_id(¶ms.thread_id).await { + Ok(v) => v, + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + // Keep a copy of v2 inputs for the notification payload. + let v2_inputs_for_notif = params.input.clone(); + + // Map v2 input items to core input items. + let mapped_items: Vec = params + .input + .into_iter() + .map(V2UserInput::into_core) + .collect(); + + let has_any_overrides = params.cwd.is_some() + || params.approval_policy.is_some() + || params.sandbox_policy.is_some() + || params.model.is_some() + || params.effort.is_some() + || params.summary.is_some(); + + // If any overrides are provided, update the session turn context first. + if has_any_overrides { + let _ = conversation + .submit(Op::OverrideTurnContext { + cwd: params.cwd, + approval_policy: params.approval_policy.map(AskForApproval::to_core), + sandbox_policy: params.sandbox_policy.map(|p| p.to_core()), + model: params.model, + effort: params.effort.map(Some), + summary: params.summary, + }) + .await; + } + + // Start the turn by submitting the user input. Return its submission id as turn_id. + let turn_id = conversation + .submit(Op::UserInput { + items: mapped_items, + }) + .await; + + match turn_id { + Ok(turn_id) => { + let turn = Turn { + id: turn_id.clone(), + items: vec![ThreadItem::UserMessage { + id: turn_id, + content: v2_inputs_for_notif, + }], + status: TurnStatus::InProgress, + error: None, + }; + + let response = TurnStartResponse { turn: turn.clone() }; + self.outgoing.send_response(request_id, response).await; + + // Emit v2 turn/started notification. + let notif = TurnStartedNotification { turn }; + self.outgoing + .send_server_notification(ServerNotification::TurnStarted(notif)) + .await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to start turn: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn turn_interrupt(&mut self, request_id: RequestId, params: TurnInterruptParams) { + let TurnInterruptParams { thread_id, .. } = params; + + let (conversation_id, conversation) = + match self.conversation_from_thread_id(&thread_id).await { + Ok(v) => v, + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + // Record the pending interrupt so we can reply when TurnAborted arrives. + { + let mut map = self.pending_interrupts.lock().await; + map.entry(conversation_id) + .or_default() + .push((request_id, ApiVersion::V2)); + } + + // Submit the interrupt; we'll respond upon TurnAborted. + let _ = conversation.submit(Op::Interrupt).await; + } + + async fn add_conversation_listener( + &mut self, + request_id: RequestId, + params: AddConversationListenerParams, + ) { + let AddConversationListenerParams { + conversation_id, + experimental_raw_events, + } = params; + match self + .attach_conversation_listener(conversation_id, experimental_raw_events, ApiVersion::V1) + .await + { + Ok(subscription_id) => { + let response = AddConversationSubscriptionResponse { subscription_id }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + self.outgoing.send_error(request_id, err).await; + } + } + } + + async fn remove_conversation_listener( + &mut self, + request_id: RequestId, + params: RemoveConversationListenerParams, + ) { + let RemoveConversationListenerParams { subscription_id } = params; + match self.conversation_listeners.remove(&subscription_id) { + Some(sender) => { + // Signal the spawned task to exit and acknowledge. + let _ = sender.send(()); + let response = RemoveConversationSubscriptionResponse {}; + self.outgoing.send_response(request_id, response).await; + } + None => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("subscription not found: {subscription_id}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn attach_conversation_listener( + &mut self, + conversation_id: ConversationId, + experimental_raw_events: bool, + api_version: ApiVersion, + ) -> Result { + let conversation = match self + .conversation_manager + .get_conversation(conversation_id) + .await + { + Ok(conv) => conv, + Err(_) => { + return Err(JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("conversation not found: {conversation_id}"), + data: None, + }); + } + }; + + let subscription_id = Uuid::new_v4(); + let (cancel_tx, mut cancel_rx) = oneshot::channel(); + self.conversation_listeners + .insert(subscription_id, cancel_tx); + + let outgoing_for_task = self.outgoing.clone(); + let pending_interrupts = self.pending_interrupts.clone(); + let api_version_for_task = api_version; + tokio::spawn(async move { + loop { + tokio::select! { + _ = &mut cancel_rx => { + // User has unsubscribed, so exit this task. + break; + } + event = conversation.next_event() => { + let event = match event { + Ok(event) => event, + Err(err) => { + tracing::warn!("conversation.next_event() failed with: {err}"); + break; + } + }; + + if let EventMsg::RawResponseItem(_) = &event.msg + && !experimental_raw_events { + continue; + } + + // For now, we send a notification for every event, + // JSON-serializing the `Event` as-is, but these should + // be migrated to be variants of `ServerNotification` + // instead. + let method = format!("codex/event/{}", event.msg); + let mut params = match serde_json::to_value(event.clone()) { + Ok(serde_json::Value::Object(map)) => map, + Ok(_) => { + error!("event did not serialize to an object"); + continue; + } + Err(err) => { + error!("failed to serialize event: {err}"); + continue; + } + }; + params.insert( + "conversationId".to_string(), + conversation_id.to_string().into(), + ); + + outgoing_for_task + .send_notification(OutgoingNotification { + method, + params: Some(params.into()), + }) + .await; + + apply_bespoke_event_handling( + event.clone(), + conversation_id, + conversation.clone(), + outgoing_for_task.clone(), + pending_interrupts.clone(), + api_version_for_task, + ) + .await; + } + } + } + }); + Ok(subscription_id) + } + + async fn git_diff_to_origin(&self, request_id: RequestId, cwd: PathBuf) { + let diff = git_diff_to_remote(&cwd).await; + match diff { + Some(value) => { + let response = GitDiffToRemoteResponse { + sha: value.sha, + diff: value.diff, + }; + self.outgoing.send_response(request_id, response).await; + } + None => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("failed to compute git diff to remote for cwd: {cwd:?}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn fuzzy_file_search(&mut self, request_id: RequestId, params: FuzzyFileSearchParams) { + let FuzzyFileSearchParams { + query, + roots, + cancellation_token, + } = params; + + let cancel_flag = match cancellation_token.clone() { + Some(token) => { + let mut pending_fuzzy_searches = self.pending_fuzzy_searches.lock().await; + // if a cancellation_token is provided and a pending_request exists for + // that token, cancel it + if let Some(existing) = pending_fuzzy_searches.get(&token) { + existing.store(true, Ordering::Relaxed); + } + let flag = Arc::new(AtomicBool::new(false)); + pending_fuzzy_searches.insert(token.clone(), flag.clone()); + flag + } + None => Arc::new(AtomicBool::new(false)), + }; + + let results = match query.as_str() { + "" => vec![], + _ => run_fuzzy_file_search(query, roots, cancel_flag.clone()).await, + }; + + if let Some(token) = cancellation_token { + let mut pending_fuzzy_searches = self.pending_fuzzy_searches.lock().await; + if let Some(current_flag) = pending_fuzzy_searches.get(&token) + && Arc::ptr_eq(current_flag, &cancel_flag) + { + pending_fuzzy_searches.remove(&token); + } + } + + let response = FuzzyFileSearchResponse { files: results }; + self.outgoing.send_response(request_id, response).await; + } + + async fn upload_feedback(&self, request_id: RequestId, params: FeedbackUploadParams) { + let FeedbackUploadParams { + classification, + reason, + conversation_id, + include_logs, + } = params; + + let snapshot = self.feedback.snapshot(conversation_id); + let thread_id = snapshot.thread_id.clone(); + + let validated_rollout_path = if include_logs { + match conversation_id { + Some(conv_id) => self.resolve_rollout_path(conv_id).await, + None => None, + } + } else { + None + }; + + let upload_result = tokio::task::spawn_blocking(move || { + let rollout_path_ref = validated_rollout_path.as_deref(); + snapshot.upload_feedback( + &classification, + reason.as_deref(), + include_logs, + rollout_path_ref, + ) + }) + .await; + + let upload_result = match upload_result { + Ok(result) => result, + Err(join_err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to upload feedback: {join_err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + match upload_result { + Ok(()) => { + let response = FeedbackUploadResponse { thread_id }; + self.outgoing.send_response(request_id, response).await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to upload feedback: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + + async fn resolve_rollout_path(&self, conversation_id: ConversationId) -> Option { + match self + .conversation_manager + .get_conversation(conversation_id) + .await + { + Ok(conv) => Some(conv.rollout_path()), + Err(_) => None, + } + } +} + +async fn derive_config_from_params( + overrides: ConfigOverrides, + cli_overrides: Option>, +) -> std::io::Result { + let cli_overrides = cli_overrides + .unwrap_or_default() + .into_iter() + .map(|(k, v)| (k, json_to_toml(v))) + .collect(); + + Config::load_with_cli_overrides(cli_overrides, overrides).await +} + +async fn read_summary_from_rollout( + path: &Path, + fallback_provider: &str, +) -> std::io::Result { + let head = read_head_for_summary(path).await?; + + let Some(first) = head.first() else { + return Err(IoError::other(format!( + "rollout at {} is empty", + path.display() + ))); + }; + + let session_meta_line = + serde_json::from_value::(first.clone()).map_err(|_| { + IoError::other(format!( + "rollout at {} does not start with session metadata", + path.display() + )) + })?; + let SessionMetaLine { + meta: session_meta, + git, + } = session_meta_line; + + if let Some(summary) = extract_conversation_summary( + path.to_path_buf(), + &head, + &session_meta, + git.as_ref(), + fallback_provider, + ) { + return Ok(summary); + } + + let timestamp = if session_meta.timestamp.is_empty() { + None + } else { + Some(session_meta.timestamp.clone()) + }; + let model_provider = session_meta + .model_provider + .clone() + .unwrap_or_else(|| fallback_provider.to_string()); + let git_info = git.as_ref().map(map_git_info); + + Ok(ConversationSummary { + conversation_id: session_meta.id, + timestamp, + path: path.to_path_buf(), + preview: String::new(), + model_provider, + cwd: session_meta.cwd, + cli_version: session_meta.cli_version, + source: session_meta.source, + git_info, + }) +} + +fn extract_conversation_summary( + path: PathBuf, + head: &[serde_json::Value], + session_meta: &SessionMeta, + git: Option<&GitInfo>, + fallback_provider: &str, +) -> Option { + let preview = head + .iter() + .filter_map(|value| serde_json::from_value::(value.clone()).ok()) + .find_map(|item| match codex_core::parse_turn_item(&item) { + Some(TurnItem::UserMessage(user)) => Some(user.message()), + _ => None, + })?; + + let preview = match preview.find(USER_MESSAGE_BEGIN) { + Some(idx) => preview[idx + USER_MESSAGE_BEGIN.len()..].trim(), + None => preview.as_str(), + }; + + let timestamp = if session_meta.timestamp.is_empty() { + None + } else { + Some(session_meta.timestamp.clone()) + }; + let conversation_id = session_meta.id; + let model_provider = session_meta + .model_provider + .clone() + .unwrap_or_else(|| fallback_provider.to_string()); + let git_info = git.map(map_git_info); + + Some(ConversationSummary { + conversation_id, + timestamp, + path, + preview: preview.to_string(), + model_provider, + cwd: session_meta.cwd.clone(), + cli_version: session_meta.cli_version.clone(), + source: session_meta.source.clone(), + git_info, + }) +} + +fn map_git_info(git_info: &GitInfo) -> ConversationGitInfo { + ConversationGitInfo { + sha: git_info.commit_hash.clone(), + branch: git_info.branch.clone(), + origin_url: git_info.repository_url.clone(), + } +} + +fn parse_datetime(timestamp: Option<&str>) -> Option> { + timestamp.and_then(|ts| { + chrono::DateTime::parse_from_rfc3339(ts) + .ok() + .map(|dt| dt.with_timezone(&chrono::Utc)) + }) +} + +fn summary_to_thread(summary: ConversationSummary) -> Thread { + let ConversationSummary { + conversation_id, + path, + preview, + timestamp, + model_provider, + .. + } = summary; + + let created_at = parse_datetime(timestamp.as_deref()); + + Thread { + id: conversation_id.to_string(), + preview, + model_provider, + created_at: created_at.map(|dt| dt.timestamp()).unwrap_or(0), + path, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::Result; + use codex_protocol::protocol::SessionSource; + use pretty_assertions::assert_eq; + use serde_json::json; + use tempfile::TempDir; + + #[test] + fn extract_conversation_summary_prefers_plain_user_messages() -> Result<()> { + let conversation_id = ConversationId::from_string("3f941c35-29b3-493b-b0a4-e25800d9aeb0")?; + let timestamp = Some("2025-09-05T16:53:11.850Z".to_string()); + let path = PathBuf::from("rollout.jsonl"); + + let head = vec![ + json!({ + "id": conversation_id.to_string(), + "timestamp": timestamp, + "cwd": "/", + "originator": "codex", + "cli_version": "0.0.0", + "instructions": null, + "model_provider": "test-provider" + }), + json!({ + "type": "message", + "role": "user", + "content": [{ + "type": "input_text", + "text": "\n\n".to_string(), + }], + }), + json!({ + "type": "message", + "role": "user", + "content": [{ + "type": "input_text", + "text": format!(" {USER_MESSAGE_BEGIN}Count to 5"), + }], + }), + ]; + + let session_meta = serde_json::from_value::(head[0].clone())?; + + let summary = + extract_conversation_summary(path.clone(), &head, &session_meta, None, "test-provider") + .expect("summary"); + + let expected = ConversationSummary { + conversation_id, + timestamp, + path, + preview: "Count to 5".to_string(), + model_provider: "test-provider".to_string(), + cwd: PathBuf::from("/"), + cli_version: "0.0.0".to_string(), + source: SessionSource::VSCode, + git_info: None, + }; + + assert_eq!(summary, expected); + Ok(()) + } + + #[tokio::test] + async fn read_summary_from_rollout_returns_empty_preview_when_no_user_message() -> Result<()> { + use codex_protocol::protocol::RolloutItem; + use codex_protocol::protocol::RolloutLine; + use codex_protocol::protocol::SessionMetaLine; + use std::fs; + + let temp_dir = TempDir::new()?; + let path = temp_dir.path().join("rollout.jsonl"); + + let conversation_id = ConversationId::from_string("bfd12a78-5900-467b-9bc5-d3d35df08191")?; + let timestamp = "2025-09-05T16:53:11.850Z".to_string(); + + let session_meta = SessionMeta { + id: conversation_id, + timestamp: timestamp.clone(), + model_provider: None, + ..SessionMeta::default() + }; + + let line = RolloutLine { + timestamp: timestamp.clone(), + item: RolloutItem::SessionMeta(SessionMetaLine { + meta: session_meta.clone(), + git: None, + }), + }; + + fs::write(&path, format!("{}\n", serde_json::to_string(&line)?))?; + + let summary = read_summary_from_rollout(path.as_path(), "fallback").await?; + + let expected = ConversationSummary { + conversation_id, + timestamp: Some(timestamp), + path: path.clone(), + preview: String::new(), + model_provider: "fallback".to_string(), + cwd: PathBuf::new(), + cli_version: String::new(), + source: SessionSource::VSCode, + git_info: None, + }; + + assert_eq!(summary, expected); + Ok(()) + } +} diff --git a/codex-rs/app-server/src/error_code.rs b/codex-rs/app-server/src/error_code.rs new file mode 100644 index 0000000000..1ffd889d40 --- /dev/null +++ b/codex-rs/app-server/src/error_code.rs @@ -0,0 +1,2 @@ +pub(crate) const INVALID_REQUEST_ERROR_CODE: i64 = -32600; +pub(crate) const INTERNAL_ERROR_CODE: i64 = -32603; diff --git a/codex-rs/app-server/src/fuzzy_file_search.rs b/codex-rs/app-server/src/fuzzy_file_search.rs new file mode 100644 index 0000000000..fcb05852ba --- /dev/null +++ b/codex-rs/app-server/src/fuzzy_file_search.rs @@ -0,0 +1,93 @@ +use std::num::NonZero; +use std::num::NonZeroUsize; +use std::path::Path; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; + +use codex_app_server_protocol::FuzzyFileSearchResult; +use codex_file_search as file_search; +use tokio::task::JoinSet; +use tracing::warn; + +const LIMIT_PER_ROOT: usize = 50; +const MAX_THREADS: usize = 12; +const COMPUTE_INDICES: bool = true; + +pub(crate) async fn run_fuzzy_file_search( + query: String, + roots: Vec, + cancellation_flag: Arc, +) -> Vec { + #[expect(clippy::expect_used)] + let limit_per_root = + NonZero::new(LIMIT_PER_ROOT).expect("LIMIT_PER_ROOT should be a valid non-zero usize"); + + let cores = std::thread::available_parallelism() + .map(std::num::NonZero::get) + .unwrap_or(1); + let threads = cores.min(MAX_THREADS); + let threads_per_root = (threads / roots.len()).max(1); + let threads = NonZero::new(threads_per_root).unwrap_or(NonZeroUsize::MIN); + + let mut files: Vec = Vec::new(); + let mut join_set = JoinSet::new(); + + for root in roots { + let search_dir = PathBuf::from(&root); + let query = query.clone(); + let cancel_flag = cancellation_flag.clone(); + join_set.spawn_blocking(move || { + match file_search::run( + query.as_str(), + limit_per_root, + &search_dir, + Vec::new(), + threads, + cancel_flag, + COMPUTE_INDICES, + true, + ) { + Ok(res) => Ok((root, res)), + Err(err) => Err((root, err)), + } + }); + } + + while let Some(res) = join_set.join_next().await { + match res { + Ok(Ok((root, res))) => { + for m in res.matches { + let path = m.path; + //TODO(shijie): Move file name generation to file_search lib. + let file_name = Path::new(&path) + .file_name() + .map(|name| name.to_string_lossy().into_owned()) + .unwrap_or_else(|| path.clone()); + let result = FuzzyFileSearchResult { + root: root.clone(), + path, + file_name, + score: m.score, + indices: m.indices, + }; + files.push(result); + } + } + Ok(Err((root, err))) => { + warn!("fuzzy-file-search in dir '{root}' failed: {err}"); + } + Err(err) => { + warn!("fuzzy-file-search join_next failed: {err}"); + } + } + } + + files.sort_by(file_search::cmp_by_score_desc_then_path_asc::< + FuzzyFileSearchResult, + _, + _, + >(|f| f.score, |f| f.path.as_str())); + + files +} diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs new file mode 100644 index 0000000000..4b65e66d2c --- /dev/null +++ b/codex-rs/app-server/src/lib.rs @@ -0,0 +1,173 @@ +#![deny(clippy::print_stdout, clippy::print_stderr)] + +use codex_common::CliConfigOverrides; +use codex_core::config::Config; +use codex_core::config::ConfigOverrides; +use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; +use std::io::ErrorKind; +use std::io::Result as IoResult; +use std::path::PathBuf; + +use crate::message_processor::MessageProcessor; +use crate::outgoing_message::OutgoingMessage; +use crate::outgoing_message::OutgoingMessageSender; +use codex_app_server_protocol::JSONRPCMessage; +use codex_feedback::CodexFeedback; +use tokio::io::AsyncBufReadExt; +use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; +use tokio::io::{self}; +use tokio::sync::mpsc; +use tracing::Level; +use tracing::debug; +use tracing::error; +use tracing::info; +use tracing_subscriber::EnvFilter; +use tracing_subscriber::Layer; +use tracing_subscriber::filter::Targets; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; + +mod bespoke_event_handling; +mod codex_message_processor; +mod error_code; +mod fuzzy_file_search; +mod message_processor; +mod models; +mod outgoing_message; + +/// Size of the bounded channels used to communicate between tasks. The value +/// is a balance between throughput and memory usage – 128 messages should be +/// plenty for an interactive CLI. +const CHANNEL_CAPACITY: usize = 128; + +pub async fn run_main( + codex_linux_sandbox_exe: Option, + cli_config_overrides: CliConfigOverrides, +) -> IoResult<()> { + // Set up channels. + let (incoming_tx, mut incoming_rx) = mpsc::channel::(CHANNEL_CAPACITY); + let (outgoing_tx, mut outgoing_rx) = mpsc::unbounded_channel::(); + + // Task: read from stdin, push to `incoming_tx`. + let stdin_reader_handle = tokio::spawn({ + async move { + let stdin = io::stdin(); + let reader = BufReader::new(stdin); + let mut lines = reader.lines(); + + while let Some(line) = lines.next_line().await.unwrap_or_default() { + match serde_json::from_str::(&line) { + Ok(msg) => { + if incoming_tx.send(msg).await.is_err() { + // Receiver gone – nothing left to do. + break; + } + } + Err(e) => error!("Failed to deserialize JSONRPCMessage: {e}"), + } + } + + debug!("stdin reader finished (EOF)"); + } + }); + + // Parse CLI overrides once and derive the base Config eagerly so later + // components do not need to work with raw TOML values. + let cli_kv_overrides = cli_config_overrides.parse_overrides().map_err(|e| { + std::io::Error::new( + ErrorKind::InvalidInput, + format!("error parsing -c overrides: {e}"), + ) + })?; + let config = Config::load_with_cli_overrides(cli_kv_overrides, ConfigOverrides::default()) + .await + .map_err(|e| { + std::io::Error::new(ErrorKind::InvalidData, format!("error loading config: {e}")) + })?; + + let feedback = CodexFeedback::new(); + + let otel = + codex_core::otel_init::build_provider(&config, env!("CARGO_PKG_VERSION")).map_err(|e| { + std::io::Error::new( + ErrorKind::InvalidData, + format!("error loading otel config: {e}"), + ) + })?; + + // Install a simple subscriber so `tracing` output is visible. Users can + // control the log level with `RUST_LOG`. + let stderr_fmt = tracing_subscriber::fmt::layer() + .with_writer(std::io::stderr) + .with_filter(EnvFilter::from_default_env()); + + let feedback_layer = tracing_subscriber::fmt::layer() + .with_writer(feedback.make_writer()) + .with_ansi(false) + .with_target(false) + .with_filter(Targets::new().with_default(Level::TRACE)); + + let _ = tracing_subscriber::registry() + .with(stderr_fmt) + .with(feedback_layer) + .with(otel.as_ref().map(|provider| { + OpenTelemetryTracingBridge::new(&provider.logger).with_filter( + tracing_subscriber::filter::filter_fn(codex_core::otel_init::codex_export_filter), + ) + })) + .try_init(); + + // Task: process incoming messages. + let processor_handle = tokio::spawn({ + let outgoing_message_sender = OutgoingMessageSender::new(outgoing_tx); + let mut processor = MessageProcessor::new( + outgoing_message_sender, + codex_linux_sandbox_exe, + std::sync::Arc::new(config), + feedback.clone(), + ); + async move { + while let Some(msg) = incoming_rx.recv().await { + match msg { + JSONRPCMessage::Request(r) => processor.process_request(r).await, + JSONRPCMessage::Response(r) => processor.process_response(r).await, + JSONRPCMessage::Notification(n) => processor.process_notification(n).await, + JSONRPCMessage::Error(e) => processor.process_error(e), + } + } + + info!("processor task exited (channel closed)"); + } + }); + + // Task: write outgoing messages to stdout. + let stdout_writer_handle = tokio::spawn(async move { + let mut stdout = io::stdout(); + while let Some(outgoing_message) = outgoing_rx.recv().await { + let Ok(value) = serde_json::to_value(outgoing_message) else { + error!("Failed to convert OutgoingMessage to JSON value"); + continue; + }; + match serde_json::to_string(&value) { + Ok(mut json) => { + json.push('\n'); + if let Err(e) = stdout.write_all(json.as_bytes()).await { + error!("Failed to write to stdout: {e}"); + break; + } + } + Err(e) => error!("Failed to serialize JSONRPCMessage: {e}"), + } + } + + info!("stdout writer exited (channel closed)"); + }); + + // Wait for all tasks to finish. The typical exit path is the stdin reader + // hitting EOF which, once it drops `incoming_tx`, propagates shutdown to + // the processor and then to the stdout task. + let _ = tokio::join!(stdin_reader_handle, processor_handle, stdout_writer_handle); + + Ok(()) +} diff --git a/codex-rs/app-server/src/main.rs b/codex-rs/app-server/src/main.rs new file mode 100644 index 0000000000..689ec0877a --- /dev/null +++ b/codex-rs/app-server/src/main.rs @@ -0,0 +1,10 @@ +use codex_app_server::run_main; +use codex_arg0::arg0_dispatch_or_else; +use codex_common::CliConfigOverrides; + +fn main() -> anyhow::Result<()> { + arg0_dispatch_or_else(|codex_linux_sandbox_exe| async move { + run_main(codex_linux_sandbox_exe, CliConfigOverrides::default()).await?; + Ok(()) + }) +} diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs new file mode 100644 index 0000000000..a97b037be0 --- /dev/null +++ b/codex-rs/app-server/src/message_processor.rs @@ -0,0 +1,159 @@ +use std::path::PathBuf; + +use crate::codex_message_processor::CodexMessageProcessor; +use crate::error_code::INVALID_REQUEST_ERROR_CODE; +use crate::outgoing_message::OutgoingMessageSender; +use codex_app_server_protocol::ClientInfo; +use codex_app_server_protocol::ClientRequest; +use codex_app_server_protocol::InitializeResponse; + +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCRequest; +use codex_app_server_protocol::JSONRPCResponse; +use codex_core::AuthManager; +use codex_core::ConversationManager; +use codex_core::config::Config; +use codex_core::default_client::USER_AGENT_SUFFIX; +use codex_core::default_client::get_codex_user_agent; +use codex_feedback::CodexFeedback; +use codex_protocol::protocol::SessionSource; +use std::sync::Arc; + +pub(crate) struct MessageProcessor { + outgoing: Arc, + codex_message_processor: CodexMessageProcessor, + initialized: bool, +} + +impl MessageProcessor { + /// Create a new `MessageProcessor`, retaining a handle to the outgoing + /// `Sender` so handlers can enqueue messages to be written to stdout. + pub(crate) fn new( + outgoing: OutgoingMessageSender, + codex_linux_sandbox_exe: Option, + config: Arc, + feedback: CodexFeedback, + ) -> Self { + let outgoing = Arc::new(outgoing); + let auth_manager = AuthManager::shared( + config.codex_home.clone(), + false, + config.cli_auth_credentials_store_mode, + ); + let conversation_manager = Arc::new(ConversationManager::new( + auth_manager.clone(), + SessionSource::VSCode, + )); + let codex_message_processor = CodexMessageProcessor::new( + auth_manager, + conversation_manager, + outgoing.clone(), + codex_linux_sandbox_exe, + config, + feedback, + ); + + Self { + outgoing, + codex_message_processor, + initialized: false, + } + } + + pub(crate) async fn process_request(&mut self, request: JSONRPCRequest) { + let request_id = request.id.clone(); + let request_json = match serde_json::to_value(&request) { + Ok(request_json) => request_json, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("Invalid request: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let codex_request = match serde_json::from_value::(request_json) { + Ok(codex_request) => codex_request, + Err(err) => { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("Invalid request: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + match codex_request { + // Handle Initialize internally so CodexMessageProcessor does not have to concern + // itself with the `initialized` bool. + ClientRequest::Initialize { request_id, params } => { + if self.initialized { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "Already initialized".to_string(), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } else { + let ClientInfo { + name, + title: _title, + version, + } = params.client_info; + let user_agent_suffix = format!("{name}; {version}"); + if let Ok(mut suffix) = USER_AGENT_SUFFIX.lock() { + *suffix = Some(user_agent_suffix); + } + + let user_agent = get_codex_user_agent(); + let response = InitializeResponse { user_agent }; + self.outgoing.send_response(request_id, response).await; + + self.initialized = true; + return; + } + } + _ => { + if !self.initialized { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "Not initialized".to_string(), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + return; + } + } + } + + self.codex_message_processor + .process_request(codex_request) + .await; + } + + pub(crate) async fn process_notification(&self, notification: JSONRPCNotification) { + // Currently, we do not expect to receive any notifications from the + // client, so we just log them. + tracing::info!("<- notification: {:?}", notification); + } + + /// Handle a standalone JSON-RPC response originating from the peer. + pub(crate) async fn process_response(&mut self, response: JSONRPCResponse) { + tracing::info!("<- response: {:?}", response); + let JSONRPCResponse { id, result, .. } = response; + self.outgoing.notify_client_response(id, result).await + } + + /// Handle an error object received from the peer. + pub(crate) fn process_error(&mut self, err: JSONRPCError) { + tracing::error!("<- error: {:?}", err); + } +} diff --git a/codex-rs/app-server/src/models.rs b/codex-rs/app-server/src/models.rs new file mode 100644 index 0000000000..d03795c2d4 --- /dev/null +++ b/codex-rs/app-server/src/models.rs @@ -0,0 +1,39 @@ +use codex_app_server_protocol::AuthMode; +use codex_app_server_protocol::Model; +use codex_app_server_protocol::ReasoningEffortOption; +use codex_common::model_presets::ModelPreset; +use codex_common::model_presets::ReasoningEffortPreset; +use codex_common::model_presets::builtin_model_presets; + +pub fn supported_models(auth_mode: Option) -> Vec { + builtin_model_presets(auth_mode) + .into_iter() + .map(model_from_preset) + .collect() +} + +fn model_from_preset(preset: ModelPreset) -> Model { + Model { + id: preset.id.to_string(), + model: preset.model.to_string(), + display_name: preset.display_name.to_string(), + description: preset.description.to_string(), + supported_reasoning_efforts: reasoning_efforts_from_preset( + preset.supported_reasoning_efforts, + ), + default_reasoning_effort: preset.default_reasoning_effort, + is_default: preset.is_default, + } +} + +fn reasoning_efforts_from_preset( + efforts: &'static [ReasoningEffortPreset], +) -> Vec { + efforts + .iter() + .map(|preset| ReasoningEffortOption { + reasoning_effort: preset.effort, + description: preset.description.to_string(), + }) + .collect() +} diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs new file mode 100644 index 0000000000..f0ee6cf939 --- /dev/null +++ b/codex-rs/app-server/src/outgoing_message.rs @@ -0,0 +1,261 @@ +use std::collections::HashMap; +use std::sync::atomic::AtomicI64; +use std::sync::atomic::Ordering; + +use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::Result; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::ServerRequest; +use codex_app_server_protocol::ServerRequestPayload; +use serde::Serialize; +use tokio::sync::Mutex; +use tokio::sync::mpsc; +use tokio::sync::oneshot; +use tracing::warn; + +use crate::error_code::INTERNAL_ERROR_CODE; + +/// Sends messages to the client and manages request callbacks. +pub(crate) struct OutgoingMessageSender { + next_request_id: AtomicI64, + sender: mpsc::UnboundedSender, + request_id_to_callback: Mutex>>, +} + +impl OutgoingMessageSender { + pub(crate) fn new(sender: mpsc::UnboundedSender) -> Self { + Self { + next_request_id: AtomicI64::new(0), + sender, + request_id_to_callback: Mutex::new(HashMap::new()), + } + } + + pub(crate) async fn send_request( + &self, + request: ServerRequestPayload, + ) -> oneshot::Receiver { + let id = RequestId::Integer(self.next_request_id.fetch_add(1, Ordering::Relaxed)); + let outgoing_message_id = id.clone(); + let (tx_approve, rx_approve) = oneshot::channel(); + { + let mut request_id_to_callback = self.request_id_to_callback.lock().await; + request_id_to_callback.insert(id, tx_approve); + } + + let outgoing_message = + OutgoingMessage::Request(request.request_with_id(outgoing_message_id)); + let _ = self.sender.send(outgoing_message); + rx_approve + } + + pub(crate) async fn notify_client_response(&self, id: RequestId, result: Result) { + let entry = { + let mut request_id_to_callback = self.request_id_to_callback.lock().await; + request_id_to_callback.remove_entry(&id) + }; + + match entry { + Some((id, sender)) => { + if let Err(err) = sender.send(result) { + warn!("could not notify callback for {id:?} due to: {err:?}"); + } + } + None => { + warn!("could not find callback for {id:?}"); + } + } + } + + pub(crate) async fn send_response(&self, id: RequestId, response: T) { + match serde_json::to_value(response) { + Ok(result) => { + let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result }); + let _ = self.sender.send(outgoing_message); + } + Err(err) => { + self.send_error( + id, + JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to serialize response: {err}"), + data: None, + }, + ) + .await; + } + } + } + + pub(crate) async fn send_server_notification(&self, notification: ServerNotification) { + let _ = self + .sender + .send(OutgoingMessage::AppServerNotification(notification)); + } + + /// All notifications should be migrated to [`ServerNotification`] and + /// [`OutgoingMessage::Notification`] should be removed. + pub(crate) async fn send_notification(&self, notification: OutgoingNotification) { + let outgoing_message = OutgoingMessage::Notification(notification); + let _ = self.sender.send(outgoing_message); + } + + pub(crate) async fn send_error(&self, id: RequestId, error: JSONRPCErrorError) { + let outgoing_message = OutgoingMessage::Error(OutgoingError { id, error }); + let _ = self.sender.send(outgoing_message); + } +} + +/// Outgoing message from the server to the client. +#[derive(Debug, Clone, Serialize)] +#[serde(untagged)] +pub(crate) enum OutgoingMessage { + Request(ServerRequest), + Notification(OutgoingNotification), + /// AppServerNotification is specific to the case where this is run as an + /// "app server" as opposed to an MCP server. + AppServerNotification(ServerNotification), + Response(OutgoingResponse), + Error(OutgoingError), +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingNotification { + pub method: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub params: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingResponse { + pub id: RequestId, + pub result: Result, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingError { + pub error: JSONRPCErrorError, + pub id: RequestId, +} + +#[cfg(test)] +mod tests { + use codex_app_server_protocol::AccountLoginCompletedNotification; + use codex_app_server_protocol::AccountRateLimitsUpdatedNotification; + use codex_app_server_protocol::AccountUpdatedNotification; + use codex_app_server_protocol::AuthMode; + use codex_app_server_protocol::LoginChatGptCompleteNotification; + use codex_app_server_protocol::RateLimitSnapshot; + use codex_app_server_protocol::RateLimitWindow; + use pretty_assertions::assert_eq; + use serde_json::json; + use uuid::Uuid; + + use super::*; + + #[test] + fn verify_server_notification_serialization() { + let notification = + ServerNotification::LoginChatGptComplete(LoginChatGptCompleteNotification { + login_id: Uuid::nil(), + success: true, + error: None, + }); + + let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification); + assert_eq!( + json!({ + "method": "loginChatGptComplete", + "params": { + "loginId": Uuid::nil(), + "success": true, + "error": null, + }, + }), + serde_json::to_value(jsonrpc_notification) + .expect("ensure the strum macros serialize the method field correctly"), + "ensure the strum macros serialize the method field correctly" + ); + } + + #[test] + fn verify_account_login_completed_notification_serialization() { + let notification = + ServerNotification::AccountLoginCompleted(AccountLoginCompletedNotification { + login_id: Some(Uuid::nil().to_string()), + success: true, + error: None, + }); + + let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification); + assert_eq!( + json!({ + "method": "account/login/completed", + "params": { + "loginId": Uuid::nil().to_string(), + "success": true, + "error": null, + }, + }), + serde_json::to_value(jsonrpc_notification) + .expect("ensure the notification serializes correctly"), + "ensure the notification serializes correctly" + ); + } + + #[test] + fn verify_account_rate_limits_notification_serialization() { + let notification = + ServerNotification::AccountRateLimitsUpdated(AccountRateLimitsUpdatedNotification { + rate_limits: RateLimitSnapshot { + primary: Some(RateLimitWindow { + used_percent: 25, + window_duration_mins: Some(15), + resets_at: Some(123), + }), + secondary: None, + }, + }); + + let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification); + assert_eq!( + json!({ + "method": "account/rateLimits/updated", + "params": { + "rateLimits": { + "primary": { + "usedPercent": 25, + "windowDurationMins": 15, + "resetsAt": 123 + }, + "secondary": null + } + }, + }), + serde_json::to_value(jsonrpc_notification) + .expect("ensure the notification serializes correctly"), + "ensure the notification serializes correctly" + ); + } + + #[test] + fn verify_account_updated_notification_serialization() { + let notification = ServerNotification::AccountUpdated(AccountUpdatedNotification { + auth_mode: Some(AuthMode::ApiKey), + }); + + let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification); + assert_eq!( + json!({ + "method": "account/updated", + "params": { + "authMode": "apikey" + }, + }), + serde_json::to_value(jsonrpc_notification) + .expect("ensure the notification serializes correctly"), + "ensure the notification serializes correctly" + ); + } +} diff --git a/codex-rs/app-server/tests/all.rs b/codex-rs/app-server/tests/all.rs new file mode 100644 index 0000000000..7e136e4cce --- /dev/null +++ b/codex-rs/app-server/tests/all.rs @@ -0,0 +1,3 @@ +// Single integration test binary that aggregates all test modules. +// The submodules live in `tests/suite/`. +mod suite; diff --git a/codex-rs/app-server/tests/common/Cargo.toml b/codex-rs/app-server/tests/common/Cargo.toml new file mode 100644 index 0000000000..6240f755ee --- /dev/null +++ b/codex-rs/app-server/tests/common/Cargo.toml @@ -0,0 +1,26 @@ +[package] +edition = "2024" +name = "app_test_support" +version = { workspace = true } + +[lib] +path = "lib.rs" + +[dependencies] +anyhow = { workspace = true } +assert_cmd = { workspace = true } +base64 = { workspace = true } +chrono = { workspace = true } +codex-app-server-protocol = { workspace = true } +codex-core = { workspace = true } +codex-protocol = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +tokio = { workspace = true, features = [ + "io-std", + "macros", + "process", + "rt-multi-thread", +] } +uuid = { workspace = true } +wiremock = { workspace = true } diff --git a/codex-rs/app-server/tests/common/auth_fixtures.rs b/codex-rs/app-server/tests/common/auth_fixtures.rs new file mode 100644 index 0000000000..071a920b89 --- /dev/null +++ b/codex-rs/app-server/tests/common/auth_fixtures.rs @@ -0,0 +1,135 @@ +use std::path::Path; + +use anyhow::Context; +use anyhow::Result; +use base64::Engine; +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use chrono::DateTime; +use chrono::Utc; +use codex_core::auth::AuthCredentialsStoreMode; +use codex_core::auth::AuthDotJson; +use codex_core::auth::save_auth; +use codex_core::token_data::TokenData; +use codex_core::token_data::parse_id_token; +use serde_json::json; + +/// Builder for writing a fake ChatGPT auth.json in tests. +#[derive(Debug, Clone)] +pub struct ChatGptAuthFixture { + access_token: String, + refresh_token: String, + account_id: Option, + claims: ChatGptIdTokenClaims, + last_refresh: Option>>, +} + +impl ChatGptAuthFixture { + pub fn new(access_token: impl Into) -> Self { + Self { + access_token: access_token.into(), + refresh_token: "refresh-token".to_string(), + account_id: None, + claims: ChatGptIdTokenClaims::default(), + last_refresh: None, + } + } + + pub fn refresh_token(mut self, refresh_token: impl Into) -> Self { + self.refresh_token = refresh_token.into(); + self + } + + pub fn account_id(mut self, account_id: impl Into) -> Self { + self.account_id = Some(account_id.into()); + self + } + + pub fn plan_type(mut self, plan_type: impl Into) -> Self { + self.claims.plan_type = Some(plan_type.into()); + self + } + + pub fn email(mut self, email: impl Into) -> Self { + self.claims.email = Some(email.into()); + self + } + + pub fn last_refresh(mut self, last_refresh: Option>) -> Self { + self.last_refresh = Some(last_refresh); + self + } + + pub fn claims(mut self, claims: ChatGptIdTokenClaims) -> Self { + self.claims = claims; + self + } +} + +#[derive(Debug, Clone, Default)] +pub struct ChatGptIdTokenClaims { + pub email: Option, + pub plan_type: Option, +} + +impl ChatGptIdTokenClaims { + pub fn new() -> Self { + Self::default() + } + + pub fn email(mut self, email: impl Into) -> Self { + self.email = Some(email.into()); + self + } + + pub fn plan_type(mut self, plan_type: impl Into) -> Self { + self.plan_type = Some(plan_type.into()); + self + } +} + +pub fn encode_id_token(claims: &ChatGptIdTokenClaims) -> Result { + let header = json!({ "alg": "none", "typ": "JWT" }); + let mut payload = serde_json::Map::new(); + if let Some(email) = &claims.email { + payload.insert("email".to_string(), json!(email)); + } + if let Some(plan_type) = &claims.plan_type { + payload.insert( + "https://api.openai.com/auth".to_string(), + json!({ "chatgpt_plan_type": plan_type }), + ); + } + let payload = serde_json::Value::Object(payload); + + let header_b64 = + URL_SAFE_NO_PAD.encode(serde_json::to_vec(&header).context("serialize jwt header")?); + let payload_b64 = + URL_SAFE_NO_PAD.encode(serde_json::to_vec(&payload).context("serialize jwt payload")?); + let signature_b64 = URL_SAFE_NO_PAD.encode(b"signature"); + Ok(format!("{header_b64}.{payload_b64}.{signature_b64}")) +} + +pub fn write_chatgpt_auth( + codex_home: &Path, + fixture: ChatGptAuthFixture, + cli_auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> Result<()> { + let id_token_raw = encode_id_token(&fixture.claims)?; + let id_token = parse_id_token(&id_token_raw).context("parse id token")?; + let tokens = TokenData { + id_token, + access_token: fixture.access_token, + refresh_token: fixture.refresh_token, + account_id: fixture.account_id, + }; + + let last_refresh = fixture.last_refresh.unwrap_or_else(|| Some(Utc::now())); + + let auth = AuthDotJson { + openai_api_key: None, + tokens: Some(tokens), + last_refresh, + }; + + save_auth(codex_home, &auth, cli_auth_credentials_store_mode).context("write auth.json") +} diff --git a/codex-rs/app-server/tests/common/lib.rs b/codex-rs/app-server/tests/common/lib.rs new file mode 100644 index 0000000000..dc3d24ccae --- /dev/null +++ b/codex-rs/app-server/tests/common/lib.rs @@ -0,0 +1,25 @@ +mod auth_fixtures; +mod mcp_process; +mod mock_model_server; +mod responses; +mod rollout; + +pub use auth_fixtures::ChatGptAuthFixture; +pub use auth_fixtures::ChatGptIdTokenClaims; +pub use auth_fixtures::encode_id_token; +pub use auth_fixtures::write_chatgpt_auth; +use codex_app_server_protocol::JSONRPCResponse; +pub use mcp_process::McpProcess; +pub use mock_model_server::create_mock_chat_completions_server; +pub use mock_model_server::create_mock_chat_completions_server_unchecked; +pub use responses::create_apply_patch_sse_response; +pub use responses::create_final_assistant_message_sse_response; +pub use responses::create_shell_sse_response; +pub use rollout::create_fake_rollout; +use serde::de::DeserializeOwned; + +pub fn to_response(response: JSONRPCResponse) -> anyhow::Result { + let value = serde_json::to_value(response.result)?; + let codex_response = serde_json::from_value(value)?; + Ok(codex_response) +} diff --git a/codex-rs/app-server/tests/common/mcp_process.rs b/codex-rs/app-server/tests/common/mcp_process.rs new file mode 100644 index 0000000000..75851eda2c --- /dev/null +++ b/codex-rs/app-server/tests/common/mcp_process.rs @@ -0,0 +1,631 @@ +use std::collections::VecDeque; +use std::path::Path; +use std::process::Stdio; +use std::sync::atomic::AtomicI64; +use std::sync::atomic::Ordering; +use tokio::io::AsyncBufReadExt; +use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; +use tokio::process::Child; +use tokio::process::ChildStdin; +use tokio::process::ChildStdout; + +use anyhow::Context; +use assert_cmd::prelude::*; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::ArchiveConversationParams; +use codex_app_server_protocol::CancelLoginAccountParams; +use codex_app_server_protocol::CancelLoginChatGptParams; +use codex_app_server_protocol::ClientInfo; +use codex_app_server_protocol::ClientNotification; +use codex_app_server_protocol::FeedbackUploadParams; +use codex_app_server_protocol::GetAccountParams; +use codex_app_server_protocol::GetAuthStatusParams; +use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::InterruptConversationParams; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCMessage; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCRequest; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::ListConversationsParams; +use codex_app_server_protocol::LoginApiKeyParams; +use codex_app_server_protocol::ModelListParams; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::RemoveConversationListenerParams; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ResumeConversationParams; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserTurnParams; +use codex_app_server_protocol::ServerRequest; +use codex_app_server_protocol::SetDefaultModelParams; +use codex_app_server_protocol::ThreadArchiveParams; +use codex_app_server_protocol::ThreadListParams; +use codex_app_server_protocol::ThreadResumeParams; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::TurnInterruptParams; +use codex_app_server_protocol::TurnStartParams; +use std::process::Command as StdCommand; +use tokio::process::Command; + +pub struct McpProcess { + next_request_id: AtomicI64, + /// Retain this child process until the client is dropped. The Tokio runtime + /// will make a "best effort" to reap the process after it exits, but it is + /// not a guarantee. See the `kill_on_drop` documentation for details. + #[allow(dead_code)] + process: Child, + stdin: ChildStdin, + stdout: BufReader, + pending_user_messages: VecDeque, +} + +impl McpProcess { + pub async fn new(codex_home: &Path) -> anyhow::Result { + Self::new_with_env(codex_home, &[]).await + } + + /// Creates a new MCP process, allowing tests to override or remove + /// specific environment variables for the child process only. + /// + /// Pass a tuple of (key, Some(value)) to set/override, or (key, None) to + /// remove a variable from the child's environment. + pub async fn new_with_env( + codex_home: &Path, + env_overrides: &[(&str, Option<&str>)], + ) -> anyhow::Result { + // Use assert_cmd to locate the binary path and then switch to tokio::process::Command + let std_cmd = StdCommand::cargo_bin("codex-app-server") + .context("should find binary for codex-mcp-server")?; + + let program = std_cmd.get_program().to_owned(); + + let mut cmd = Command::new(program); + + cmd.stdin(Stdio::piped()); + cmd.stdout(Stdio::piped()); + cmd.stderr(Stdio::piped()); + cmd.env("CODEX_HOME", codex_home); + cmd.env("RUST_LOG", "debug"); + + for (k, v) in env_overrides { + match v { + Some(val) => { + cmd.env(k, val); + } + None => { + cmd.env_remove(k); + } + } + } + + let mut process = cmd + .kill_on_drop(true) + .spawn() + .context("codex-mcp-server proc should start")?; + let stdin = process + .stdin + .take() + .ok_or_else(|| anyhow::format_err!("mcp should have stdin fd"))?; + let stdout = process + .stdout + .take() + .ok_or_else(|| anyhow::format_err!("mcp should have stdout fd"))?; + let stdout = BufReader::new(stdout); + + // Forward child's stderr to our stderr so failures are visible even + // when stdout/stderr are captured by the test harness. + if let Some(stderr) = process.stderr.take() { + let mut stderr_reader = BufReader::new(stderr).lines(); + tokio::spawn(async move { + while let Ok(Some(line)) = stderr_reader.next_line().await { + eprintln!("[mcp stderr] {line}"); + } + }); + } + Ok(Self { + next_request_id: AtomicI64::new(0), + process, + stdin, + stdout, + pending_user_messages: VecDeque::new(), + }) + } + + /// Performs the initialization handshake with the MCP server. + pub async fn initialize(&mut self) -> anyhow::Result<()> { + let params = Some(serde_json::to_value(InitializeParams { + client_info: ClientInfo { + name: "codex-app-server-tests".to_string(), + title: None, + version: "0.1.0".to_string(), + }, + })?); + let req_id = self.send_request("initialize", params).await?; + let initialized = self.read_jsonrpc_message().await?; + let JSONRPCMessage::Response(response) = initialized else { + unreachable!("expected JSONRPCMessage::Response for initialize, got {initialized:?}"); + }; + if response.id != RequestId::Integer(req_id) { + anyhow::bail!( + "initialize response id mismatch: expected {}, got {:?}", + req_id, + response.id + ); + } + + // Send notifications/initialized to ack the response. + self.send_notification(ClientNotification::Initialized) + .await?; + + Ok(()) + } + + /// Send a `newConversation` JSON-RPC request. + pub async fn send_new_conversation_request( + &mut self, + params: NewConversationParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("newConversation", params).await + } + + /// Send an `archiveConversation` JSON-RPC request. + pub async fn send_archive_conversation_request( + &mut self, + params: ArchiveConversationParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("archiveConversation", params).await + } + + /// Send an `addConversationListener` JSON-RPC request. + pub async fn send_add_conversation_listener_request( + &mut self, + params: AddConversationListenerParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("addConversationListener", params).await + } + + /// Send a `sendUserMessage` JSON-RPC request with a single text item. + pub async fn send_send_user_message_request( + &mut self, + params: SendUserMessageParams, + ) -> anyhow::Result { + // Wire format expects variants in camelCase; text item uses external tagging. + let params = Some(serde_json::to_value(params)?); + self.send_request("sendUserMessage", params).await + } + + /// Send a `removeConversationListener` JSON-RPC request. + pub async fn send_remove_conversation_listener_request( + &mut self, + params: RemoveConversationListenerParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("removeConversationListener", params) + .await + } + + /// Send a `sendUserTurn` JSON-RPC request. + pub async fn send_send_user_turn_request( + &mut self, + params: SendUserTurnParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("sendUserTurn", params).await + } + + /// Send a `interruptConversation` JSON-RPC request. + pub async fn send_interrupt_conversation_request( + &mut self, + params: InterruptConversationParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("interruptConversation", params).await + } + + /// Send a `getAuthStatus` JSON-RPC request. + pub async fn send_get_auth_status_request( + &mut self, + params: GetAuthStatusParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("getAuthStatus", params).await + } + + /// Send a `getUserSavedConfig` JSON-RPC request. + pub async fn send_get_user_saved_config_request(&mut self) -> anyhow::Result { + self.send_request("getUserSavedConfig", None).await + } + + /// Send a `getUserAgent` JSON-RPC request. + pub async fn send_get_user_agent_request(&mut self) -> anyhow::Result { + self.send_request("getUserAgent", None).await + } + + /// Send an `account/rateLimits/read` JSON-RPC request. + pub async fn send_get_account_rate_limits_request(&mut self) -> anyhow::Result { + self.send_request("account/rateLimits/read", None).await + } + + /// Send an `account/read` JSON-RPC request. + pub async fn send_get_account_request( + &mut self, + params: GetAccountParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("account/read", params).await + } + + /// Send a `feedback/upload` JSON-RPC request. + pub async fn send_feedback_upload_request( + &mut self, + params: FeedbackUploadParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("feedback/upload", params).await + } + + /// Send a `userInfo` JSON-RPC request. + pub async fn send_user_info_request(&mut self) -> anyhow::Result { + self.send_request("userInfo", None).await + } + + /// Send a `setDefaultModel` JSON-RPC request. + pub async fn send_set_default_model_request( + &mut self, + params: SetDefaultModelParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("setDefaultModel", params).await + } + + /// Send a `listConversations` JSON-RPC request. + pub async fn send_list_conversations_request( + &mut self, + params: ListConversationsParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("listConversations", params).await + } + + /// Send a `thread/start` JSON-RPC request. + pub async fn send_thread_start_request( + &mut self, + params: ThreadStartParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("thread/start", params).await + } + + /// Send a `thread/resume` JSON-RPC request. + pub async fn send_thread_resume_request( + &mut self, + params: ThreadResumeParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("thread/resume", params).await + } + + /// Send a `thread/archive` JSON-RPC request. + pub async fn send_thread_archive_request( + &mut self, + params: ThreadArchiveParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("thread/archive", params).await + } + + /// Send a `thread/list` JSON-RPC request. + pub async fn send_thread_list_request( + &mut self, + params: ThreadListParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("thread/list", params).await + } + + /// Send a `model/list` JSON-RPC request. + pub async fn send_list_models_request( + &mut self, + params: ModelListParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("model/list", params).await + } + + /// Send a `resumeConversation` JSON-RPC request. + pub async fn send_resume_conversation_request( + &mut self, + params: ResumeConversationParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("resumeConversation", params).await + } + + /// Send a `loginApiKey` JSON-RPC request. + pub async fn send_login_api_key_request( + &mut self, + params: LoginApiKeyParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("loginApiKey", params).await + } + + /// Send a `loginChatGpt` JSON-RPC request. + pub async fn send_login_chat_gpt_request(&mut self) -> anyhow::Result { + self.send_request("loginChatGpt", None).await + } + + /// Send a `turn/start` JSON-RPC request (v2). + pub async fn send_turn_start_request( + &mut self, + params: TurnStartParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("turn/start", params).await + } + + /// Send a `turn/interrupt` JSON-RPC request (v2). + pub async fn send_turn_interrupt_request( + &mut self, + params: TurnInterruptParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("turn/interrupt", params).await + } + + /// Send a `cancelLoginChatGpt` JSON-RPC request. + pub async fn send_cancel_login_chat_gpt_request( + &mut self, + params: CancelLoginChatGptParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("cancelLoginChatGpt", params).await + } + + /// Send a `logoutChatGpt` JSON-RPC request. + pub async fn send_logout_chat_gpt_request(&mut self) -> anyhow::Result { + self.send_request("logoutChatGpt", None).await + } + + /// Send an `account/logout` JSON-RPC request. + pub async fn send_logout_account_request(&mut self) -> anyhow::Result { + self.send_request("account/logout", None).await + } + + /// Send an `account/login/start` JSON-RPC request for API key login. + pub async fn send_login_account_api_key_request( + &mut self, + api_key: &str, + ) -> anyhow::Result { + let params = serde_json::json!({ + "type": "apiKey", + "apiKey": api_key, + }); + self.send_request("account/login/start", Some(params)).await + } + + /// Send an `account/login/start` JSON-RPC request for ChatGPT login. + pub async fn send_login_account_chatgpt_request(&mut self) -> anyhow::Result { + let params = serde_json::json!({ + "type": "chatgpt" + }); + self.send_request("account/login/start", Some(params)).await + } + + /// Send an `account/login/cancel` JSON-RPC request. + pub async fn send_cancel_login_account_request( + &mut self, + params: CancelLoginAccountParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("account/login/cancel", params).await + } + + /// Send a `fuzzyFileSearch` JSON-RPC request. + pub async fn send_fuzzy_file_search_request( + &mut self, + query: &str, + roots: Vec, + cancellation_token: Option, + ) -> anyhow::Result { + let mut params = serde_json::json!({ + "query": query, + "roots": roots, + }); + if let Some(token) = cancellation_token { + params["cancellationToken"] = serde_json::json!(token); + } + self.send_request("fuzzyFileSearch", Some(params)).await + } + + async fn send_request( + &mut self, + method: &str, + params: Option, + ) -> anyhow::Result { + let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed); + + let message = JSONRPCMessage::Request(JSONRPCRequest { + id: RequestId::Integer(request_id), + method: method.to_string(), + params, + }); + self.send_jsonrpc_message(message).await?; + Ok(request_id) + } + + pub async fn send_response( + &mut self, + id: RequestId, + result: serde_json::Value, + ) -> anyhow::Result<()> { + self.send_jsonrpc_message(JSONRPCMessage::Response(JSONRPCResponse { id, result })) + .await + } + + pub async fn send_notification( + &mut self, + notification: ClientNotification, + ) -> anyhow::Result<()> { + let value = serde_json::to_value(notification)?; + self.send_jsonrpc_message(JSONRPCMessage::Notification(JSONRPCNotification { + method: value + .get("method") + .and_then(|m| m.as_str()) + .ok_or_else(|| anyhow::format_err!("notification missing method field"))? + .to_string(), + params: value.get("params").cloned(), + })) + .await + } + + async fn send_jsonrpc_message(&mut self, message: JSONRPCMessage) -> anyhow::Result<()> { + eprintln!("writing message to stdin: {message:?}"); + let payload = serde_json::to_string(&message)?; + self.stdin.write_all(payload.as_bytes()).await?; + self.stdin.write_all(b"\n").await?; + self.stdin.flush().await?; + Ok(()) + } + + async fn read_jsonrpc_message(&mut self) -> anyhow::Result { + let mut line = String::new(); + self.stdout.read_line(&mut line).await?; + let message = serde_json::from_str::(&line)?; + eprintln!("read message from stdout: {message:?}"); + Ok(message) + } + + pub async fn read_stream_until_request_message(&mut self) -> anyhow::Result { + eprintln!("in read_stream_until_request_message()"); + + loop { + let message = self.read_jsonrpc_message().await?; + + match message { + JSONRPCMessage::Notification(notification) => { + eprintln!("notification: {notification:?}"); + self.enqueue_user_message(notification); + } + JSONRPCMessage::Request(jsonrpc_request) => { + return jsonrpc_request.try_into().with_context( + || "failed to deserialize ServerRequest from JSONRPCRequest", + ); + } + JSONRPCMessage::Error(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Error: {message:?}"); + } + JSONRPCMessage::Response(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Response: {message:?}"); + } + } + } + } + + pub async fn read_stream_until_response_message( + &mut self, + request_id: RequestId, + ) -> anyhow::Result { + eprintln!("in read_stream_until_response_message({request_id:?})"); + + loop { + let message = self.read_jsonrpc_message().await?; + match message { + JSONRPCMessage::Notification(notification) => { + eprintln!("notification: {notification:?}"); + self.enqueue_user_message(notification); + } + JSONRPCMessage::Request(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); + } + JSONRPCMessage::Error(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Error: {message:?}"); + } + JSONRPCMessage::Response(jsonrpc_response) => { + if jsonrpc_response.id == request_id { + return Ok(jsonrpc_response); + } + } + } + } + } + + pub async fn read_stream_until_error_message( + &mut self, + request_id: RequestId, + ) -> anyhow::Result { + loop { + let message = self.read_jsonrpc_message().await?; + match message { + JSONRPCMessage::Notification(notification) => { + eprintln!("notification: {notification:?}"); + self.enqueue_user_message(notification); + } + JSONRPCMessage::Request(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); + } + JSONRPCMessage::Response(_) => { + // Keep scanning; we're waiting for an error with matching id. + } + JSONRPCMessage::Error(err) => { + if err.id == request_id { + return Ok(err); + } + } + } + } + } + + pub async fn read_stream_until_notification_message( + &mut self, + method: &str, + ) -> anyhow::Result { + eprintln!("in read_stream_until_notification_message({method})"); + + if let Some(notification) = self.take_pending_notification_by_method(method) { + return Ok(notification); + } + + loop { + let message = self.read_jsonrpc_message().await?; + match message { + JSONRPCMessage::Notification(notification) => { + if notification.method == method { + return Ok(notification); + } + self.enqueue_user_message(notification); + } + JSONRPCMessage::Request(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); + } + JSONRPCMessage::Error(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Error: {message:?}"); + } + JSONRPCMessage::Response(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Response: {message:?}"); + } + } + } + } + + fn take_pending_notification_by_method(&mut self, method: &str) -> Option { + if let Some(pos) = self + .pending_user_messages + .iter() + .position(|notification| notification.method == method) + { + return self.pending_user_messages.remove(pos); + } + None + } + + fn enqueue_user_message(&mut self, notification: JSONRPCNotification) { + if notification.method == "codex/event/user_message" { + self.pending_user_messages.push_back(notification); + } + } +} diff --git a/codex-rs/app-server/tests/common/mock_model_server.rs b/codex-rs/app-server/tests/common/mock_model_server.rs new file mode 100644 index 0000000000..08765338a9 --- /dev/null +++ b/codex-rs/app-server/tests/common/mock_model_server.rs @@ -0,0 +1,66 @@ +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; + +use wiremock::Mock; +use wiremock::MockServer; +use wiremock::Respond; +use wiremock::ResponseTemplate; +use wiremock::matchers::method; +use wiremock::matchers::path; + +/// Create a mock server that will provide the responses, in order, for +/// requests to the `/v1/chat/completions` endpoint. +pub async fn create_mock_chat_completions_server(responses: Vec) -> MockServer { + let server = MockServer::start().await; + + let num_calls = responses.len(); + let seq_responder = SeqResponder { + num_calls: AtomicUsize::new(0), + responses, + }; + + Mock::given(method("POST")) + .and(path("/v1/chat/completions")) + .respond_with(seq_responder) + .expect(num_calls as u64) + .mount(&server) + .await; + + server +} + +/// Same as `create_mock_chat_completions_server` but does not enforce an +/// expectation on the number of calls. +pub async fn create_mock_chat_completions_server_unchecked(responses: Vec) -> MockServer { + let server = MockServer::start().await; + + let seq_responder = SeqResponder { + num_calls: AtomicUsize::new(0), + responses, + }; + + Mock::given(method("POST")) + .and(path("/v1/chat/completions")) + .respond_with(seq_responder) + .mount(&server) + .await; + + server +} + +struct SeqResponder { + num_calls: AtomicUsize, + responses: Vec, +} + +impl Respond for SeqResponder { + fn respond(&self, _: &wiremock::Request) -> ResponseTemplate { + let call_num = self.num_calls.fetch_add(1, Ordering::SeqCst); + match self.responses.get(call_num) { + Some(response) => ResponseTemplate::new(200) + .insert_header("content-type", "text/event-stream") + .set_body_raw(response.clone(), "text/event-stream"), + None => panic!("no response for {call_num}"), + } + } +} diff --git a/codex-rs/app-server/tests/common/responses.rs b/codex-rs/app-server/tests/common/responses.rs new file mode 100644 index 0000000000..9a827fb986 --- /dev/null +++ b/codex-rs/app-server/tests/common/responses.rs @@ -0,0 +1,95 @@ +use serde_json::json; +use std::path::Path; + +pub fn create_shell_sse_response( + command: Vec, + workdir: Option<&Path>, + timeout_ms: Option, + call_id: &str, +) -> anyhow::Result { + // The `arguments`` for the `shell` tool is a serialized JSON object. + let tool_call_arguments = serde_json::to_string(&json!({ + "command": command, + "workdir": workdir.map(|w| w.to_string_lossy()), + "timeout": timeout_ms + }))?; + let tool_call = json!({ + "choices": [ + { + "delta": { + "tool_calls": [ + { + "id": call_id, + "function": { + "name": "shell", + "arguments": tool_call_arguments + } + } + ] + }, + "finish_reason": "tool_calls" + } + ] + }); + + let sse = format!( + "data: {}\n\ndata: DONE\n\n", + serde_json::to_string(&tool_call)? + ); + Ok(sse) +} + +pub fn create_final_assistant_message_sse_response(message: &str) -> anyhow::Result { + let assistant_message = json!({ + "choices": [ + { + "delta": { + "content": message + }, + "finish_reason": "stop" + } + ] + }); + + let sse = format!( + "data: {}\n\ndata: DONE\n\n", + serde_json::to_string(&assistant_message)? + ); + Ok(sse) +} + +pub fn create_apply_patch_sse_response( + patch_content: &str, + call_id: &str, +) -> anyhow::Result { + // Use shell command to call apply_patch with heredoc format + let shell_command = format!("apply_patch <<'EOF'\n{patch_content}\nEOF"); + let tool_call_arguments = serde_json::to_string(&json!({ + "command": ["bash", "-lc", shell_command] + }))?; + + let tool_call = json!({ + "choices": [ + { + "delta": { + "tool_calls": [ + { + "id": call_id, + "function": { + "name": "shell", + "arguments": tool_call_arguments + } + } + ] + }, + "finish_reason": "tool_calls" + } + ] + }); + + let sse = format!( + "data: {}\n\ndata: DONE\n\n", + serde_json::to_string(&tool_call)? + ); + Ok(sse) +} diff --git a/codex-rs/app-server/tests/common/rollout.rs b/codex-rs/app-server/tests/common/rollout.rs new file mode 100644 index 0000000000..c8197a046d --- /dev/null +++ b/codex-rs/app-server/tests/common/rollout.rs @@ -0,0 +1,82 @@ +use anyhow::Result; +use codex_protocol::ConversationId; +use codex_protocol::protocol::SessionMeta; +use codex_protocol::protocol::SessionSource; +use serde_json::json; +use std::fs; +use std::path::Path; +use std::path::PathBuf; +use uuid::Uuid; + +/// Create a minimal rollout file under `CODEX_HOME/sessions/YYYY/MM/DD/`. +/// +/// - `filename_ts` is the filename timestamp component in `YYYY-MM-DDThh-mm-ss` format. +/// - `meta_rfc3339` is the envelope timestamp used in JSON lines. +/// - `preview` is the user message preview text. +/// - `model_provider` optionally sets the provider in the session meta payload. +/// +/// Returns the generated conversation/session UUID as a string. +pub fn create_fake_rollout( + codex_home: &Path, + filename_ts: &str, + meta_rfc3339: &str, + preview: &str, + model_provider: Option<&str>, +) -> Result { + let uuid = Uuid::new_v4(); + let uuid_str = uuid.to_string(); + let conversation_id = ConversationId::from_string(&uuid_str)?; + + // sessions/YYYY/MM/DD derived from filename_ts (YYYY-MM-DDThh-mm-ss) + let year = &filename_ts[0..4]; + let month = &filename_ts[5..7]; + let day = &filename_ts[8..10]; + let dir = codex_home.join("sessions").join(year).join(month).join(day); + fs::create_dir_all(&dir)?; + + let file_path = dir.join(format!("rollout-{filename_ts}-{uuid}.jsonl")); + + // Build JSONL lines + let payload = serde_json::to_value(SessionMeta { + id: conversation_id, + timestamp: meta_rfc3339.to_string(), + cwd: PathBuf::from("/"), + originator: "codex".to_string(), + cli_version: "0.0.0".to_string(), + instructions: None, + source: SessionSource::Cli, + model_provider: model_provider.map(str::to_string), + })?; + + let lines = [ + json!({ + "timestamp": meta_rfc3339, + "type": "session_meta", + "payload": payload + }) + .to_string(), + json!({ + "timestamp": meta_rfc3339, + "type":"response_item", + "payload": { + "type":"message", + "role":"user", + "content":[{"type":"input_text","text": preview}] + } + }) + .to_string(), + json!({ + "timestamp": meta_rfc3339, + "type":"event_msg", + "payload": { + "type":"user_message", + "message": preview, + "kind": "plain" + } + }) + .to_string(), + ]; + + fs::write(file_path, lines.join("\n") + "\n")?; + Ok(uuid_str) +} diff --git a/codex-rs/mcp-server/tests/suite/archive_conversation.rs b/codex-rs/app-server/tests/suite/archive_conversation.rs similarity index 63% rename from codex-rs/mcp-server/tests/suite/archive_conversation.rs rename to codex-rs/app-server/tests/suite/archive_conversation.rs index e54a99896c..b6e85936fb 100644 --- a/codex-rs/mcp-server/tests/suite/archive_conversation.rs +++ b/codex-rs/app-server/tests/suite/archive_conversation.rs @@ -1,53 +1,44 @@ -use std::path::Path; - +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::ArchiveConversationParams; +use codex_app_server_protocol::ArchiveConversationResponse; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RequestId; use codex_core::ARCHIVED_SESSIONS_SUBDIR; -use codex_protocol::mcp_protocol::ArchiveConversationParams; -use codex_protocol::mcp_protocol::ArchiveConversationResponse; -use codex_protocol::mcp_protocol::NewConversationParams; -use codex_protocol::mcp_protocol::NewConversationResponse; -use mcp_test_support::McpProcess; -use mcp_test_support::to_response; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; +use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; -const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20); #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn archive_conversation_moves_rollout_into_archived_directory() { - let codex_home = TempDir::new().expect("create temp dir"); - create_config_toml(codex_home.path()).expect("write config.toml"); +async fn archive_conversation_moves_rollout_into_archived_directory() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("initialize timeout") - .expect("initialize request"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let new_request_id = mcp .send_new_conversation_request(NewConversationParams { model: Some("mock-model".to_string()), ..Default::default() }) - .await - .expect("send newConversation"); + .await?; let new_response: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(new_request_id)), ) - .await - .expect("newConversation timeout") - .expect("newConversation response"); + .await??; let NewConversationResponse { conversation_id, rollout_path, .. - } = to_response::(new_response) - .expect("deserialize newConversation response"); + } = to_response::(new_response)?; assert!( rollout_path.exists(), @@ -60,19 +51,15 @@ async fn archive_conversation_moves_rollout_into_archived_directory() { conversation_id, rollout_path: rollout_path.clone(), }) - .await - .expect("send archiveConversation"); + .await?; let archive_response: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(archive_request_id)), ) - .await - .expect("archiveConversation timeout") - .expect("archiveConversation response"); + .await??; let _: ArchiveConversationResponse = - to_response::(archive_response) - .expect("deserialize archiveConversation response"); + to_response::(archive_response)?; let archived_directory = codex_home.path().join(ARCHIVED_SESSIONS_SUBDIR); let archived_rollout_path = @@ -90,6 +77,8 @@ async fn archive_conversation_moves_rollout_into_archived_directory() { "expected archived rollout path {} to exist", archived_rollout_path.display() ); + + Ok(()) } fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { diff --git a/codex-rs/mcp-server/tests/suite/auth.rs b/codex-rs/app-server/tests/suite/auth.rs similarity index 53% rename from codex-rs/mcp-server/tests/suite/auth.rs rename to codex-rs/app-server/tests/suite/auth.rs index 6681fd7546..72912362fe 100644 --- a/codex-rs/mcp-server/tests/suite/auth.rs +++ b/codex-rs/app-server/tests/suite/auth.rs @@ -1,15 +1,16 @@ -use std::path::Path; - -use codex_protocol::mcp_protocol::AuthMode; -use codex_protocol::mcp_protocol::GetAuthStatusParams; -use codex_protocol::mcp_protocol::GetAuthStatusResponse; -use codex_protocol::mcp_protocol::LoginApiKeyParams; -use codex_protocol::mcp_protocol::LoginApiKeyResponse; -use mcp_test_support::McpProcess; -use mcp_test_support::to_response; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::AuthMode; +use codex_app_server_protocol::GetAuthStatusParams; +use codex_app_server_protocol::GetAuthStatusResponse; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::LoginApiKeyParams; +use codex_app_server_protocol::LoginApiKeyResponse; +use codex_app_server_protocol::RequestId; use pretty_assertions::assert_eq; +use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; @@ -57,125 +58,112 @@ sandbox_mode = "danger-full-access" ) } -async fn login_with_api_key_via_request(mcp: &mut McpProcess, api_key: &str) { +fn create_config_toml_forced_login(codex_home: &Path, forced_method: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + let contents = format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" +forced_login_method = "{forced_method}" +"# + ); + std::fs::write(config_toml, contents) +} + +async fn login_with_api_key_via_request(mcp: &mut McpProcess, api_key: &str) -> Result<()> { let request_id = mcp .send_login_api_key_request(LoginApiKeyParams { api_key: api_key.to_string(), }) - .await - .unwrap_or_else(|e| panic!("send loginApiKey: {e}")); + .await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .unwrap_or_else(|e| panic!("loginApiKey timeout: {e}")) - .unwrap_or_else(|e| panic!("loginApiKey response: {e}")); - let _: LoginApiKeyResponse = - to_response(resp).unwrap_or_else(|e| panic!("deserialize login response: {e}")); + .await??; + let _: LoginApiKeyResponse = to_response(resp)?; + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn get_auth_status_no_auth() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); - create_config_toml(codex_home.path()).unwrap_or_else(|err| panic!("write config.toml: {err}")); +async fn get_auth_status_no_auth() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let request_id = mcp .send_get_auth_status_request(GetAuthStatusParams { include_token: Some(true), refresh_token: Some(false), }) - .await - .expect("send getAuthStatus"); + .await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getAuthStatus timeout") - .expect("getAuthStatus response"); - let status: GetAuthStatusResponse = to_response(resp).expect("deserialize status"); + .await??; + let status: GetAuthStatusResponse = to_response(resp)?; assert_eq!(status.auth_method, None, "expected no auth method"); assert_eq!(status.auth_token, None, "expected no token"); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn get_auth_status_with_api_key() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); - create_config_toml(codex_home.path()).unwrap_or_else(|err| panic!("write config.toml: {err}")); +async fn get_auth_status_with_api_key() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; - login_with_api_key_via_request(&mut mcp, "sk-test-key").await; + login_with_api_key_via_request(&mut mcp, "sk-test-key").await?; let request_id = mcp .send_get_auth_status_request(GetAuthStatusParams { include_token: Some(true), refresh_token: Some(false), }) - .await - .expect("send getAuthStatus"); + .await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getAuthStatus timeout") - .expect("getAuthStatus response"); - let status: GetAuthStatusResponse = to_response(resp).expect("deserialize status"); + .await??; + let status: GetAuthStatusResponse = to_response(resp)?; assert_eq!(status.auth_method, Some(AuthMode::ApiKey)); assert_eq!(status.auth_token, Some("sk-test-key".to_string())); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn get_auth_status_with_api_key_when_auth_not_required() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); - create_config_toml_custom_provider(codex_home.path(), false) - .unwrap_or_else(|err| panic!("write config.toml: {err}")); +async fn get_auth_status_with_api_key_when_auth_not_required() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml_custom_provider(codex_home.path(), false)?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; - login_with_api_key_via_request(&mut mcp, "sk-test-key").await; + login_with_api_key_via_request(&mut mcp, "sk-test-key").await?; let request_id = mcp .send_get_auth_status_request(GetAuthStatusParams { include_token: Some(true), refresh_token: Some(false), }) - .await - .expect("send getAuthStatus"); + .await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getAuthStatus timeout") - .expect("getAuthStatus response"); - let status: GetAuthStatusResponse = to_response(resp).expect("deserialize status"); + .await??; + let status: GetAuthStatusResponse = to_response(resp)?; assert_eq!(status.auth_method, None, "expected no auth method"); assert_eq!(status.auth_token, None, "expected no token"); assert_eq!( @@ -183,41 +171,60 @@ async fn get_auth_status_with_api_key_when_auth_not_required() { Some(false), "requires_openai_auth should be false", ); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn get_auth_status_with_api_key_no_include_token() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); - create_config_toml(codex_home.path()).unwrap_or_else(|err| panic!("write config.toml: {err}")); +async fn get_auth_status_with_api_key_no_include_token() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; - login_with_api_key_via_request(&mut mcp, "sk-test-key").await; + login_with_api_key_via_request(&mut mcp, "sk-test-key").await?; // Build params via struct so None field is omitted in wire JSON. let params = GetAuthStatusParams { include_token: None, refresh_token: Some(false), }; - let request_id = mcp - .send_get_auth_status_request(params) - .await - .expect("send getAuthStatus"); + let request_id = mcp.send_get_auth_status_request(params).await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getAuthStatus timeout") - .expect("getAuthStatus response"); - let status: GetAuthStatusResponse = to_response(resp).expect("deserialize status"); + .await??; + let status: GetAuthStatusResponse = to_response(resp)?; assert_eq!(status.auth_method, Some(AuthMode::ApiKey)); assert!(status.auth_token.is_none(), "token must be omitted"); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn login_api_key_rejected_when_forced_chatgpt() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml_forced_login(codex_home.path(), "chatgpt")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_login_api_key_request(LoginApiKeyParams { + api_key: "sk-test-key".to_string(), + }) + .await?; + + let err: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!( + err.error.message, + "API key login is disabled. Use ChatGPT login instead." + ); + Ok(()) } diff --git a/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs b/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs new file mode 100644 index 0000000000..1feda42841 --- /dev/null +++ b/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs @@ -0,0 +1,523 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_final_assistant_message_sse_response; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::create_shell_sse_response; +use app_test_support::to_response; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::AddConversationSubscriptionResponse; +use codex_app_server_protocol::ExecCommandApprovalParams; +use codex_app_server_protocol::InputItem; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RemoveConversationListenerParams; +use codex_app_server_protocol::RemoveConversationSubscriptionResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; +use codex_app_server_protocol::SendUserTurnParams; +use codex_app_server_protocol::SendUserTurnResponse; +use codex_app_server_protocol::ServerRequest; +use codex_core::protocol::AskForApproval; +use codex_core::protocol::SandboxPolicy; +use codex_core::protocol_config_types::ReasoningEffort; +use codex_core::protocol_config_types::ReasoningSummary; +use codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; +use codex_protocol::config_types::SandboxMode; +use codex_protocol::parse_command::ParsedCommand; +use codex_protocol::protocol::Event; +use codex_protocol::protocol::EventMsg; +use pretty_assertions::assert_eq; +use std::env; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn test_codex_jsonrpc_conversation_flow() -> Result<()> { + if env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return Ok(()); + } + + let tmp = TempDir::new()?; + // Temporary Codex home with config pointing at the mock server. + let codex_home = tmp.path().join("codex_home"); + std::fs::create_dir(&codex_home)?; + let working_directory = tmp.path().join("workdir"); + std::fs::create_dir(&working_directory)?; + + // Create a mock model server that immediately ends each turn. + // Two turns are expected: initial session configure + one user message. + let responses = vec![ + create_shell_sse_response( + vec!["ls".to_string()], + Some(&working_directory), + Some(5000), + "call1234", + )?, + create_final_assistant_message_sse_response("Enjoy your new git repo!")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + create_config_toml(&codex_home, &server.uri())?; + + // Start MCP server and initialize. + let mut mcp = McpProcess::new(&codex_home).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // 1) newConversation + let new_conv_id = mcp + .send_new_conversation_request(NewConversationParams { + cwd: Some(working_directory.to_string_lossy().into_owned()), + ..Default::default() + }) + .await?; + let new_conv_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), + ) + .await??; + let new_conv_resp = to_response::(new_conv_resp)?; + let NewConversationResponse { + conversation_id, + model, + reasoning_effort: _, + rollout_path: _, + } = new_conv_resp; + assert_eq!(model, "mock-model"); + + // 2) addConversationListener + let add_listener_id = mcp + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) + .await?; + let add_listener_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), + ) + .await??; + let AddConversationSubscriptionResponse { subscription_id } = + to_response::(add_listener_resp)?; + + // 3) sendUserMessage (should trigger notifications; we only validate an OK response) + let send_user_id = mcp + .send_send_user_message_request(SendUserMessageParams { + conversation_id, + items: vec![codex_app_server_protocol::InputItem::Text { + text: "text".to_string(), + }], + }) + .await?; + let send_user_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(send_user_id)), + ) + .await??; + let SendUserMessageResponse {} = to_response::(send_user_resp)?; + + // Verify the task_finished notification is received. + // Note this also ensures that the final request to the server was made. + let task_finished_notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + let serde_json::Value::Object(map) = task_finished_notification + .params + .expect("notification should have params") + else { + panic!("task_finished_notification should have params"); + }; + assert_eq!( + map.get("conversationId") + .expect("should have conversationId"), + &serde_json::Value::String(conversation_id.to_string()) + ); + + // 4) removeConversationListener + let remove_listener_id = mcp + .send_remove_conversation_listener_request(RemoveConversationListenerParams { + subscription_id, + }) + .await?; + let remove_listener_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(remove_listener_id)), + ) + .await??; + let RemoveConversationSubscriptionResponse {} = to_response(remove_listener_resp)?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn test_send_user_turn_changes_approval_policy_behavior() -> Result<()> { + if env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return Ok(()); + } + + let tmp = TempDir::new()?; + let codex_home = tmp.path().join("codex_home"); + std::fs::create_dir(&codex_home)?; + let working_directory = tmp.path().join("workdir"); + std::fs::create_dir(&working_directory)?; + + // Mock server will request a python shell call for the first and second turn, then finish. + let responses = vec![ + create_shell_sse_response( + vec![ + "python3".to_string(), + "-c".to_string(), + "print(42)".to_string(), + ], + Some(&working_directory), + Some(5000), + "call1", + )?, + create_final_assistant_message_sse_response("done 1")?, + create_shell_sse_response( + vec![ + "python3".to_string(), + "-c".to_string(), + "print(42)".to_string(), + ], + Some(&working_directory), + Some(5000), + "call2", + )?, + create_final_assistant_message_sse_response("done 2")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + create_config_toml(&codex_home, &server.uri())?; + + // Start MCP server and initialize. + let mut mcp = McpProcess::new(&codex_home).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // 1) Start conversation with approval_policy=untrusted + let new_conv_id = mcp + .send_new_conversation_request(NewConversationParams { + cwd: Some(working_directory.to_string_lossy().into_owned()), + ..Default::default() + }) + .await?; + let new_conv_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), + ) + .await??; + let NewConversationResponse { + conversation_id, .. + } = to_response::(new_conv_resp)?; + + // 2) addConversationListener + let add_listener_id = mcp + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) + .await?; + let _: AddConversationSubscriptionResponse = to_response::( + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), + ) + .await??, + )?; + + // 3) sendUserMessage triggers a shell call; approval policy is Untrusted so we should get an elicitation + let send_user_id = mcp + .send_send_user_message_request(SendUserMessageParams { + conversation_id, + items: vec![codex_app_server_protocol::InputItem::Text { + text: "run python".to_string(), + }], + }) + .await?; + let _send_user_resp: SendUserMessageResponse = to_response::( + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(send_user_id)), + ) + .await??, + )?; + + // Expect an ExecCommandApproval request (elicitation) + let request = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_request_message(), + ) + .await??; + let ServerRequest::ExecCommandApproval { request_id, params } = request else { + panic!("expected ExecCommandApproval request, got: {request:?}"); + }; + + assert_eq!( + ExecCommandApprovalParams { + conversation_id, + call_id: "call1".to_string(), + command: vec![ + "python3".to_string(), + "-c".to_string(), + "print(42)".to_string(), + ], + cwd: working_directory.clone(), + reason: None, + risk: None, + parsed_cmd: vec![ParsedCommand::Unknown { + cmd: "python3 -c 'print(42)'".to_string() + }], + }, + params + ); + + // Approve so the first turn can complete + mcp.send_response( + request_id, + serde_json::json!({ "decision": codex_core::protocol::ReviewDecision::Approved }), + ) + .await?; + + // Wait for first TaskComplete + let _ = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + // 4) sendUserTurn with approval_policy=never should run without elicitation + let send_turn_id = mcp + .send_send_user_turn_request(SendUserTurnParams { + conversation_id, + items: vec![codex_app_server_protocol::InputItem::Text { + text: "run python again".to_string(), + }], + cwd: working_directory.clone(), + approval_policy: AskForApproval::Never, + sandbox_policy: SandboxPolicy::new_read_only_policy(), + model: "mock-model".to_string(), + effort: Some(ReasoningEffort::Medium), + summary: ReasoningSummary::Auto, + }) + .await?; + // Acknowledge sendUserTurn + let _send_turn_resp: SendUserTurnResponse = to_response::( + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(send_turn_id)), + ) + .await??, + )?; + + // Ensure we do NOT receive an ExecCommandApproval request before the task completes. + // If any Request is seen while waiting for task_complete, the helper will error and the test fails. + let _ = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + Ok(()) +} + +// Helper: minimal config.toml pointing at mock provider. + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn test_send_user_turn_updates_sandbox_and_cwd_between_turns() -> Result<()> { + if env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return Ok(()); + } + + let tmp = TempDir::new()?; + let codex_home = tmp.path().join("codex_home"); + std::fs::create_dir(&codex_home)?; + let workspace_root = tmp.path().join("workspace"); + std::fs::create_dir(&workspace_root)?; + let first_cwd = workspace_root.join("turn1"); + let second_cwd = workspace_root.join("turn2"); + std::fs::create_dir(&first_cwd)?; + std::fs::create_dir(&second_cwd)?; + + let responses = vec![ + create_shell_sse_response( + vec![ + "bash".to_string(), + "-lc".to_string(), + "echo first turn".to_string(), + ], + None, + Some(5000), + "call-first", + )?, + create_final_assistant_message_sse_response("done first")?, + create_shell_sse_response( + vec![ + "bash".to_string(), + "-lc".to_string(), + "echo second turn".to_string(), + ], + None, + Some(5000), + "call-second", + )?, + create_final_assistant_message_sse_response("done second")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + create_config_toml(&codex_home, &server.uri())?; + + let mut mcp = McpProcess::new(&codex_home).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let new_conv_id = mcp + .send_new_conversation_request(NewConversationParams { + cwd: Some(first_cwd.to_string_lossy().into_owned()), + approval_policy: Some(AskForApproval::Never), + sandbox: Some(SandboxMode::WorkspaceWrite), + ..Default::default() + }) + .await?; + let new_conv_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), + ) + .await??; + let NewConversationResponse { + conversation_id, + model, + .. + } = to_response::(new_conv_resp)?; + + let add_listener_id = mcp + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), + ) + .await??; + + let first_turn_id = mcp + .send_send_user_turn_request(SendUserTurnParams { + conversation_id, + items: vec![InputItem::Text { + text: "first turn".to_string(), + }], + cwd: first_cwd.clone(), + approval_policy: AskForApproval::Never, + sandbox_policy: SandboxPolicy::WorkspaceWrite { + writable_roots: vec![first_cwd.clone()], + network_access: false, + exclude_tmpdir_env_var: false, + exclude_slash_tmp: false, + }, + model: model.clone(), + effort: Some(ReasoningEffort::Medium), + summary: ReasoningSummary::Auto, + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(first_turn_id)), + ) + .await??; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + let second_turn_id = mcp + .send_send_user_turn_request(SendUserTurnParams { + conversation_id, + items: vec![InputItem::Text { + text: "second turn".to_string(), + }], + cwd: second_cwd.clone(), + approval_policy: AskForApproval::Never, + sandbox_policy: SandboxPolicy::DangerFullAccess, + model: model.clone(), + effort: Some(ReasoningEffort::Medium), + summary: ReasoningSummary::Auto, + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(second_turn_id)), + ) + .await??; + + let exec_begin_notification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/exec_command_begin"), + ) + .await??; + let params = exec_begin_notification + .params + .clone() + .expect("exec_command_begin params"); + let event: Event = serde_json::from_value(params).expect("deserialize exec begin event"); + let exec_begin = match event.msg { + EventMsg::ExecCommandBegin(exec_begin) => exec_begin, + other => panic!("expected ExecCommandBegin event, got {other:?}"), + }; + assert_eq!( + exec_begin.cwd, second_cwd, + "exec turn should run from updated cwd" + ); + assert_eq!( + exec_begin.command, + vec![ + "bash".to_string(), + "-lc".to_string(), + "echo second turn".to_string() + ], + "exec turn should run expected command" + ); + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + Ok(()) +} + +fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "untrusted" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/mcp-server/tests/suite/config.rs b/codex-rs/app-server/tests/suite/config.rs similarity index 66% rename from codex-rs/mcp-server/tests/suite/config.rs rename to codex-rs/app-server/tests/suite/config.rs index bc8789ce78..281d54927b 100644 --- a/codex-rs/mcp-server/tests/suite/config.rs +++ b/codex-rs/app-server/tests/suite/config.rs @@ -1,21 +1,22 @@ -use std::collections::HashMap; -use std::path::Path; - +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::GetUserSavedConfigResponse; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::Profile; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SandboxSettings; +use codex_app_server_protocol::Tools; +use codex_app_server_protocol::UserSavedConfig; use codex_core::protocol::AskForApproval; +use codex_protocol::config_types::ForcedLoginMethod; use codex_protocol::config_types::ReasoningEffort; use codex_protocol::config_types::ReasoningSummary; use codex_protocol::config_types::SandboxMode; use codex_protocol::config_types::Verbosity; -use codex_protocol::mcp_protocol::GetUserSavedConfigResponse; -use codex_protocol::mcp_protocol::Profile; -use codex_protocol::mcp_protocol::SandboxSettings; -use codex_protocol::mcp_protocol::Tools; -use codex_protocol::mcp_protocol::UserSavedConfig; -use mcp_test_support::McpProcess; -use mcp_test_support::to_response; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; use pretty_assertions::assert_eq; +use std::collections::HashMap; +use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; @@ -26,13 +27,15 @@ fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { std::fs::write( config_toml, r#" -model = "gpt-5" +model = "gpt-5.1-codex" approval_policy = "on-request" sandbox_mode = "workspace-write" model_reasoning_summary = "detailed" model_reasoning_effort = "high" model_verbosity = "medium" profile = "test" +forced_chatgpt_workspace_id = "12345678-0000-0000-0000-000000000000" +forced_login_method = "chatgpt" [sandbox_workspace_write] writable_roots = ["/tmp"] @@ -57,31 +60,21 @@ chatgpt_base_url = "https://api.chatgpt.com" } #[tokio::test(flavor = "multi_thread", worker_threads = 4)] -async fn get_config_toml_parses_all_fields() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); - create_config_toml(codex_home.path()).expect("write config.toml"); +async fn get_config_toml_parses_all_fields() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; - let request_id = mcp - .send_get_user_saved_config_request() - .await - .expect("send getUserSavedConfig"); + let request_id = mcp.send_get_user_saved_config_request().await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getUserSavedConfig timeout") - .expect("getUserSavedConfig response"); + .await??; - let config: GetUserSavedConfigResponse = to_response(resp).expect("deserialize config"); + let config: GetUserSavedConfigResponse = to_response(resp)?; let expected = GetUserSavedConfigResponse { config: UserSavedConfig { approval_policy: Some(AskForApproval::OnRequest), @@ -92,7 +85,9 @@ async fn get_config_toml_parses_all_fields() { exclude_tmpdir_env_var: Some(true), exclude_slash_tmp: Some(true), }), - model: Some("gpt-5".into()), + forced_chatgpt_workspace_id: Some("12345678-0000-0000-0000-000000000000".into()), + forced_login_method: Some(ForcedLoginMethod::Chatgpt), + model: Some("gpt-5.1-codex".into()), model_reasoning_effort: Some(ReasoningEffort::High), model_reasoning_summary: Some(ReasoningSummary::Detailed), model_verbosity: Some(Verbosity::Medium), @@ -117,38 +112,31 @@ async fn get_config_toml_parses_all_fields() { }; assert_eq!(config, expected); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn get_config_toml_empty() { - let codex_home = TempDir::new().unwrap_or_else(|e| panic!("create tempdir: {e}")); +async fn get_config_toml_empty() -> Result<()> { + let codex_home = TempDir::new()?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; - let request_id = mcp - .send_get_user_saved_config_request() - .await - .expect("send getUserSavedConfig"); + let request_id = mcp.send_get_user_saved_config_request().await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("getUserSavedConfig timeout") - .expect("getUserSavedConfig response"); + .await??; - let config: GetUserSavedConfigResponse = to_response(resp).expect("deserialize config"); + let config: GetUserSavedConfigResponse = to_response(resp)?; let expected = GetUserSavedConfigResponse { config: UserSavedConfig { approval_policy: None, sandbox_mode: None, sandbox_settings: None, + forced_chatgpt_workspace_id: None, + forced_login_method: None, model: None, model_reasoning_effort: None, model_reasoning_summary: None, @@ -160,4 +148,5 @@ async fn get_config_toml_empty() { }; assert_eq!(config, expected); + Ok(()) } diff --git a/codex-rs/mcp-server/tests/suite/create_conversation.rs b/codex-rs/app-server/tests/suite/create_conversation.rs similarity index 62% rename from codex-rs/mcp-server/tests/suite/create_conversation.rs rename to codex-rs/app-server/tests/suite/create_conversation.rs index 1b62d01d46..7788b8f382 100644 --- a/codex-rs/mcp-server/tests/suite/create_conversation.rs +++ b/codex-rs/app-server/tests/suite/create_conversation.rs @@ -1,45 +1,38 @@ -use std::path::Path; - -use codex_protocol::mcp_protocol::AddConversationListenerParams; -use codex_protocol::mcp_protocol::AddConversationSubscriptionResponse; -use codex_protocol::mcp_protocol::InputItem; -use codex_protocol::mcp_protocol::NewConversationParams; -use codex_protocol::mcp_protocol::NewConversationResponse; -use codex_protocol::mcp_protocol::SendUserMessageParams; -use codex_protocol::mcp_protocol::SendUserMessageResponse; -use mcp_test_support::McpProcess; -use mcp_test_support::create_final_assistant_message_sse_response; -use mcp_test_support::create_mock_chat_completions_server; -use mcp_test_support::to_response; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_final_assistant_message_sse_response; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::to_response; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::AddConversationSubscriptionResponse; +use codex_app_server_protocol::InputItem; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; use pretty_assertions::assert_eq; use serde_json::json; +use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn test_conversation_create_and_send_message_ok() { +async fn test_conversation_create_and_send_message_ok() -> Result<()> { // Mock server – we won't strictly rely on it, but provide one to satisfy any model wiring. - let responses = vec![ - create_final_assistant_message_sse_response("Done").expect("build mock assistant message"), - ]; + let responses = vec![create_final_assistant_message_sse_response("Done")?]; let server = create_mock_chat_completions_server(responses).await; // Temporary Codex home with config pointing at the mock server. - let codex_home = TempDir::new().expect("create temp dir"); - create_config_toml(codex_home.path(), &server.uri()).expect("write config.toml"); + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; // Start MCP server process and initialize. - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; // Create a conversation via the new JSON-RPC API. let new_conv_id = mcp @@ -47,40 +40,35 @@ async fn test_conversation_create_and_send_message_ok() { model: Some("o3".to_string()), ..Default::default() }) - .await - .expect("send newConversation"); + .await?; let new_conv_resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), ) - .await - .expect("newConversation timeout") - .expect("newConversation resp"); + .await??; let NewConversationResponse { conversation_id, model, reasoning_effort: _, rollout_path: _, - } = to_response::(new_conv_resp) - .expect("deserialize newConversation response"); + } = to_response::(new_conv_resp)?; assert_eq!(model, "o3"); // Add a listener so we receive notifications for this conversation (not strictly required for this test). let add_listener_id = mcp - .send_add_conversation_listener_request(AddConversationListenerParams { conversation_id }) - .await - .expect("send addConversationListener"); + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) + .await?; let _sub: AddConversationSubscriptionResponse = to_response::( timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), ) - .await - .expect("addConversationListener timeout") - .expect("addConversationListener resp"), - ) - .expect("deserialize addConversationListener response"); + .await??, + )?; // Now send a user message via the wire API and expect an OK (empty object) result. let send_id = mcp @@ -90,36 +78,32 @@ async fn test_conversation_create_and_send_message_ok() { text: "Hello".to_string(), }], }) - .await - .expect("send sendUserMessage"); + .await?; let send_resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(send_id)), ) - .await - .expect("sendUserMessage timeout") - .expect("sendUserMessage resp"); - let _ok: SendUserMessageResponse = to_response::(send_resp) - .expect("deserialize sendUserMessage response"); + .await??; + let _ok: SendUserMessageResponse = to_response::(send_resp)?; // avoid race condition by waiting for the mock server to receive the chat.completions request let deadline = std::time::Instant::now() + DEFAULT_READ_TIMEOUT; - loop { + let requests = loop { let requests = server.received_requests().await.unwrap_or_default(); if !requests.is_empty() { - break; + break requests; } if std::time::Instant::now() >= deadline { panic!("mock server did not receive the chat.completions request in time"); } tokio::time::sleep(std::time::Duration::from_millis(10)).await; - } + }; // Verify the outbound request body matches expectations for Chat Completions. - let request = &server.received_requests().await.unwrap()[0]; - let body = request - .body_json::() - .expect("parse request body as JSON"); + let request = requests + .first() + .expect("mock server should have received at least one request"); + let body = request.body_json::()?; assert_eq!(body["model"], json!("o3")); assert!(body["stream"].as_bool().unwrap_or(false)); let messages = body["messages"] @@ -130,6 +114,7 @@ async fn test_conversation_create_and_send_message_ok() { assert_eq!(last["content"], json!("Hello")); drop(server); + Ok(()) } // Helper to create a config.toml pointing at the mock model server. diff --git a/codex-rs/app-server/tests/suite/fuzzy_file_search.rs b/codex-rs/app-server/tests/suite/fuzzy_file_search.rs new file mode 100644 index 0000000000..9c95e3de34 --- /dev/null +++ b/codex-rs/app-server/tests/suite/fuzzy_file_search.rs @@ -0,0 +1,128 @@ +use anyhow::Result; +use anyhow::anyhow; +use app_test_support::McpProcess; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use pretty_assertions::assert_eq; +use serde_json::json; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_fuzzy_file_search_sorts_and_includes_indices() -> Result<()> { + // Prepare a temporary Codex home and a separate root with test files. + let codex_home = TempDir::new()?; + let root = TempDir::new()?; + + // Create files designed to have deterministic ordering for query "abe". + std::fs::write(root.path().join("abc"), "x")?; + std::fs::write(root.path().join("abcde"), "x")?; + std::fs::write(root.path().join("abexy"), "x")?; + std::fs::write(root.path().join("zzz.txt"), "x")?; + let sub_dir = root.path().join("sub"); + std::fs::create_dir_all(&sub_dir)?; + let sub_abce_path = sub_dir.join("abce"); + std::fs::write(&sub_abce_path, "x")?; + let sub_abce_rel = sub_abce_path + .strip_prefix(root.path())? + .to_string_lossy() + .to_string(); + + // Start MCP server and initialize. + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let root_path = root.path().to_string_lossy().to_string(); + // Send fuzzyFileSearch request. + let request_id = mcp + .send_fuzzy_file_search_request("abe", vec![root_path.clone()], None) + .await?; + + // Read response and verify shape and ordering. + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let value = resp.result; + // The path separator on Windows affects the score. + let expected_score = if cfg!(windows) { 69 } else { 72 }; + + assert_eq!( + value, + json!({ + "files": [ + { + "root": root_path.clone(), + "path": "abexy", + "file_name": "abexy", + "score": 88, + "indices": [0, 1, 2], + }, + { + "root": root_path.clone(), + "path": "abcde", + "file_name": "abcde", + "score": 74, + "indices": [0, 1, 4], + }, + { + "root": root_path.clone(), + "path": sub_abce_rel, + "file_name": "abce", + "score": expected_score, + "indices": [4, 5, 7], + }, + ] + }) + ); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_fuzzy_file_search_accepts_cancellation_token() -> Result<()> { + let codex_home = TempDir::new()?; + let root = TempDir::new()?; + + std::fs::write(root.path().join("alpha.txt"), "contents")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let root_path = root.path().to_string_lossy().to_string(); + let request_id = mcp + .send_fuzzy_file_search_request("alp", vec![root_path.clone()], None) + .await?; + + let request_id_2 = mcp + .send_fuzzy_file_search_request( + "alp", + vec![root_path.clone()], + Some(request_id.to_string()), + ) + .await?; + + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id_2)), + ) + .await??; + + let files = resp + .result + .get("files") + .ok_or_else(|| anyhow!("files key missing"))? + .as_array() + .ok_or_else(|| anyhow!("files not array"))? + .clone(); + + assert_eq!(files.len(), 1); + assert_eq!(files[0]["root"], root_path); + assert_eq!(files[0]["path"], "alpha.txt"); + + Ok(()) +} diff --git a/codex-rs/mcp-server/tests/suite/interrupt.rs b/codex-rs/app-server/tests/suite/interrupt.rs similarity index 81% rename from codex-rs/mcp-server/tests/suite/interrupt.rs rename to codex-rs/app-server/tests/suite/interrupt.rs index 113a8dd291..86b0a3f3f8 100644 --- a/codex-rs/mcp-server/tests/suite/interrupt.rs +++ b/codex-rs/app-server/tests/suite/interrupt.rs @@ -1,37 +1,32 @@ #![cfg(unix)] -// Support code lives in the `mcp_test_support` crate under tests/common. +// Support code lives in the `app_test_support` crate under tests/common. use std::path::Path; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::InterruptConversationParams; +use codex_app_server_protocol::InterruptConversationResponse; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; use codex_core::protocol::TurnAbortReason; -use codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; -use codex_protocol::mcp_protocol::AddConversationListenerParams; -use codex_protocol::mcp_protocol::InterruptConversationParams; -use codex_protocol::mcp_protocol::InterruptConversationResponse; -use codex_protocol::mcp_protocol::NewConversationParams; -use codex_protocol::mcp_protocol::NewConversationResponse; -use codex_protocol::mcp_protocol::SendUserMessageParams; -use codex_protocol::mcp_protocol::SendUserMessageResponse; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; +use core_test_support::skip_if_no_network; use tempfile::TempDir; use tokio::time::timeout; -use mcp_test_support::McpProcess; -use mcp_test_support::create_mock_chat_completions_server; -use mcp_test_support::create_shell_sse_response; -use mcp_test_support::to_response; +use app_test_support::McpProcess; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::create_shell_sse_response; +use app_test_support::to_response; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_shell_command_interruption() { - if std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { - println!( - "Skipping test because it cannot execute when network is disabled in a Codex sandbox." - ); - return; - } + skip_if_no_network!(); if let Err(err) = shell_command_interruption().await { panic!("failure: {err}"); @@ -93,7 +88,10 @@ async fn shell_command_interruption() -> anyhow::Result<()> { // 2) addConversationListener let add_listener_id = mcp - .send_add_conversation_listener_request(AddConversationListenerParams { conversation_id }) + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) .await?; let _add_listener_resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, @@ -105,7 +103,7 @@ async fn shell_command_interruption() -> anyhow::Result<()> { let send_user_id = mcp .send_send_user_message_request(SendUserMessageParams { conversation_id, - items: vec![codex_protocol::mcp_protocol::InputItem::Text { + items: vec![codex_app_server_protocol::InputItem::Text { text: "run first sleep command".to_string(), }], }) @@ -148,7 +146,7 @@ fn create_config_toml(codex_home: &Path, server_uri: String) -> std::io::Result< r#" model = "mock-model" approval_policy = "never" -sandbox_mode = "danger-full-access" +sandbox_mode = "read-only" model_provider = "mock_provider" diff --git a/codex-rs/app-server/tests/suite/list_resume.rs b/codex-rs/app-server/tests/suite/list_resume.rs new file mode 100644 index 0000000000..30be93a2e6 --- /dev/null +++ b/codex-rs/app-server/tests/suite/list_resume.rs @@ -0,0 +1,356 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_fake_rollout; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::ListConversationsParams; +use codex_app_server_protocol::ListConversationsResponse; +use codex_app_server_protocol::NewConversationParams; // reused for overrides shape +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ResumeConversationParams; +use codex_app_server_protocol::ResumeConversationResponse; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::SessionConfiguredNotification; +use codex_core::protocol::EventMsg; +use codex_protocol::models::ContentItem; +use codex_protocol::models::ResponseItem; +use pretty_assertions::assert_eq; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_list_and_resume_conversations() -> Result<()> { + // Prepare a temporary CODEX_HOME with a few fake rollout files. + let codex_home = TempDir::new()?; + create_fake_rollout( + codex_home.path(), + "2025-01-02T12-00-00", + "2025-01-02T12:00:00Z", + "Hello A", + Some("openai"), + )?; + create_fake_rollout( + codex_home.path(), + "2025-01-01T13-00-00", + "2025-01-01T13:00:00Z", + "Hello B", + Some("openai"), + )?; + create_fake_rollout( + codex_home.path(), + "2025-01-01T12-00-00", + "2025-01-01T12:00:00Z", + "Hello C", + None, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Request first page with size 2 + let req_id = mcp + .send_list_conversations_request(ListConversationsParams { + page_size: Some(2), + cursor: None, + model_providers: None, + }) + .await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(req_id)), + ) + .await??; + let ListConversationsResponse { items, next_cursor } = + to_response::(resp)?; + + assert_eq!(items.len(), 2); + // Newest first; preview text should match + assert_eq!(items[0].preview, "Hello A"); + assert_eq!(items[1].preview, "Hello B"); + assert_eq!(items[0].model_provider, "openai"); + assert_eq!(items[1].model_provider, "openai"); + assert!(items[0].path.is_absolute()); + assert!(next_cursor.is_some()); + + // Request the next page using the cursor + let req_id2 = mcp + .send_list_conversations_request(ListConversationsParams { + page_size: Some(2), + cursor: next_cursor, + model_providers: None, + }) + .await?; + let resp2: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(req_id2)), + ) + .await??; + let ListConversationsResponse { + items: items2, + next_cursor: next2, + .. + } = to_response::(resp2)?; + assert_eq!(items2.len(), 1); + assert_eq!(items2[0].preview, "Hello C"); + assert_eq!(items2[0].model_provider, "openai"); + assert_eq!(next2, None); + + // Add a conversation with an explicit non-OpenAI provider for filter tests. + create_fake_rollout( + codex_home.path(), + "2025-01-01T11-30-00", + "2025-01-01T11:30:00Z", + "Hello TP", + Some("test-provider"), + )?; + + // Filtering by model provider should return only matching sessions. + let filter_req_id = mcp + .send_list_conversations_request(ListConversationsParams { + page_size: Some(10), + cursor: None, + model_providers: Some(vec!["test-provider".to_string()]), + }) + .await?; + let filter_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(filter_req_id)), + ) + .await??; + let ListConversationsResponse { + items: filtered_items, + next_cursor: filtered_next, + } = to_response::(filter_resp)?; + assert_eq!(filtered_items.len(), 1); + assert_eq!(filtered_next, None); + assert_eq!(filtered_items[0].preview, "Hello TP"); + assert_eq!(filtered_items[0].model_provider, "test-provider"); + + // Empty filter should include every session regardless of provider metadata. + let unfiltered_req_id = mcp + .send_list_conversations_request(ListConversationsParams { + page_size: Some(10), + cursor: None, + model_providers: Some(Vec::new()), + }) + .await?; + let unfiltered_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(unfiltered_req_id)), + ) + .await??; + let ListConversationsResponse { + items: unfiltered_items, + next_cursor: unfiltered_next, + } = to_response::(unfiltered_resp)?; + assert_eq!(unfiltered_items.len(), 4); + assert!(unfiltered_next.is_none()); + + let empty_req_id = mcp + .send_list_conversations_request(ListConversationsParams { + page_size: Some(10), + cursor: None, + model_providers: Some(vec!["other".to_string()]), + }) + .await?; + let empty_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(empty_req_id)), + ) + .await??; + let ListConversationsResponse { + items: empty_items, + next_cursor: empty_next, + } = to_response::(empty_resp)?; + assert!(empty_items.is_empty()); + assert!(empty_next.is_none()); + + let first_item = &items[0]; + + // Now resume one of the sessions from an explicit rollout path. + let resume_req_id = mcp + .send_resume_conversation_request(ResumeConversationParams { + path: Some(first_item.path.clone()), + conversation_id: None, + history: None, + overrides: Some(NewConversationParams { + model: Some("o3".to_string()), + ..Default::default() + }), + }) + .await?; + + // Expect a codex/event notification with msg.type == sessionConfigured + let notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("sessionConfigured"), + ) + .await??; + let session_configured: ServerNotification = notification.try_into()?; + let ServerNotification::SessionConfigured(SessionConfiguredNotification { + model, + rollout_path, + initial_messages: session_initial_messages, + .. + }) = session_configured + else { + unreachable!("expected sessionConfigured notification"); + }; + assert_eq!(model, "o3"); + assert_eq!(rollout_path, first_item.path.clone()); + let session_initial_messages = session_initial_messages + .expect("expected initial messages when resuming from rollout path"); + match session_initial_messages.as_slice() { + [EventMsg::UserMessage(message)] => { + assert_eq!(message.message, first_item.preview.clone()); + } + other => panic!("unexpected initial messages from rollout resume: {other:#?}"), + } + + // Then the response for resumeConversation + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_req_id)), + ) + .await??; + let ResumeConversationResponse { + conversation_id, + model: resume_model, + initial_messages: response_initial_messages, + .. + } = to_response::(resume_resp)?; + // conversation id should be a valid UUID + assert!(!conversation_id.to_string().is_empty()); + assert_eq!(resume_model, "o3"); + let response_initial_messages = + response_initial_messages.expect("expected initial messages in resume response"); + match response_initial_messages.as_slice() { + [EventMsg::UserMessage(message)] => { + assert_eq!(message.message, first_item.preview.clone()); + } + other => panic!("unexpected initial messages in resume response: {other:#?}"), + } + + // Resuming with only a conversation id should locate the rollout automatically. + let resume_by_id_req_id = mcp + .send_resume_conversation_request(ResumeConversationParams { + path: None, + conversation_id: Some(first_item.conversation_id), + history: None, + overrides: Some(NewConversationParams { + model: Some("o3".to_string()), + ..Default::default() + }), + }) + .await?; + let notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("sessionConfigured"), + ) + .await??; + let session_configured: ServerNotification = notification.try_into()?; + let ServerNotification::SessionConfigured(SessionConfiguredNotification { + model, + rollout_path, + initial_messages: session_initial_messages, + .. + }) = session_configured + else { + unreachable!("expected sessionConfigured notification"); + }; + assert_eq!(model, "o3"); + assert_eq!(rollout_path, first_item.path.clone()); + let session_initial_messages = session_initial_messages + .expect("expected initial messages when resuming from conversation id"); + match session_initial_messages.as_slice() { + [EventMsg::UserMessage(message)] => { + assert_eq!(message.message, first_item.preview.clone()); + } + other => panic!("unexpected initial messages from conversation id resume: {other:#?}"), + } + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_by_id_req_id)), + ) + .await??; + let ResumeConversationResponse { + conversation_id: by_id_conversation_id, + model: by_id_model, + initial_messages: by_id_initial_messages, + .. + } = to_response::(resume_resp)?; + assert!(!by_id_conversation_id.to_string().is_empty()); + assert_eq!(by_id_model, "o3"); + let by_id_initial_messages = by_id_initial_messages + .expect("expected initial messages when resuming from conversation id response"); + match by_id_initial_messages.as_slice() { + [EventMsg::UserMessage(message)] => { + assert_eq!(message.message, first_item.preview.clone()); + } + other => { + panic!("unexpected initial messages in conversation id resume response: {other:#?}") + } + } + + // Resuming with explicit history should succeed even without a stored rollout. + let fork_history_text = "Hello from history"; + let history = vec![ResponseItem::Message { + id: None, + role: "user".to_string(), + content: vec![ContentItem::InputText { + text: fork_history_text.to_string(), + }], + }]; + let resume_with_history_req_id = mcp + .send_resume_conversation_request(ResumeConversationParams { + path: None, + conversation_id: None, + history: Some(history), + overrides: Some(NewConversationParams { + model: Some("o3".to_string()), + ..Default::default() + }), + }) + .await?; + let notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("sessionConfigured"), + ) + .await??; + let session_configured: ServerNotification = notification.try_into()?; + let ServerNotification::SessionConfigured(SessionConfiguredNotification { + model, + initial_messages: session_initial_messages, + .. + }) = session_configured + else { + unreachable!("expected sessionConfigured notification"); + }; + assert_eq!(model, "o3"); + assert!( + session_initial_messages.as_ref().is_none_or(Vec::is_empty), + "expected no initial messages when resuming from explicit history but got {session_initial_messages:#?}" + ); + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_with_history_req_id)), + ) + .await??; + let ResumeConversationResponse { + conversation_id: history_conversation_id, + model: history_model, + initial_messages: history_initial_messages, + .. + } = to_response::(resume_resp)?; + assert!(!history_conversation_id.to_string().is_empty()); + assert_eq!(history_model, "o3"); + assert!( + history_initial_messages.as_ref().is_none_or(Vec::is_empty), + "expected no initial messages in resume response when history is provided but got {history_initial_messages:#?}" + ); + + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/login.rs b/codex-rs/app-server/tests/suite/login.rs new file mode 100644 index 0000000000..c5470c3ec4 --- /dev/null +++ b/codex-rs/app-server/tests/suite/login.rs @@ -0,0 +1,206 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::CancelLoginChatGptParams; +use codex_app_server_protocol::CancelLoginChatGptResponse; +use codex_app_server_protocol::GetAuthStatusParams; +use codex_app_server_protocol::GetAuthStatusResponse; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::LoginChatGptResponse; +use codex_app_server_protocol::LogoutChatGptResponse; +use codex_app_server_protocol::RequestId; +use codex_core::auth::AuthCredentialsStoreMode; +use codex_login::login_with_api_key; +use serial_test::serial; +use std::path::Path; +use std::time::Duration; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +// Helper to create a config.toml; mirrors create_conversation.rs +fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "http://127.0.0.1:0/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"#, + ) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn logout_chatgpt_removes_auth() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; + login_with_api_key( + codex_home.path(), + "sk-test-key", + AuthCredentialsStoreMode::File, + )?; + assert!(codex_home.path().join("auth.json").exists()); + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let id = mcp.send_logout_chat_gpt_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(id)), + ) + .await??; + let _ok: LogoutChatGptResponse = to_response(resp)?; + + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should be deleted" + ); + + // Verify status reflects signed-out state. + let status_id = mcp + .send_get_auth_status_request(GetAuthStatusParams { + include_token: Some(true), + refresh_token: Some(false), + }) + .await?; + let status_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(status_id)), + ) + .await??; + let status: GetAuthStatusResponse = to_response(status_resp)?; + assert_eq!(status.auth_method, None); + assert_eq!(status.auth_token, None); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +// Serialize tests that launch the login server since it binds to a fixed port. +#[serial(login_port)] +async fn login_and_cancel_chatgpt() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let login_id = mcp.send_login_chat_gpt_request().await?; + let login_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(login_id)), + ) + .await??; + let login: LoginChatGptResponse = to_response(login_resp)?; + + let cancel_id = mcp + .send_cancel_login_chat_gpt_request(CancelLoginChatGptParams { + login_id: login.login_id, + }) + .await?; + let cancel_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(cancel_id)), + ) + .await??; + let _ok: CancelLoginChatGptResponse = to_response(cancel_resp)?; + + // Optionally observe the completion notification; do not fail if it races. + let maybe_note = timeout( + Duration::from_secs(2), + mcp.read_stream_until_notification_message("codex/event/login_chat_gpt_complete"), + ) + .await; + if maybe_note.is_err() { + eprintln!("warning: did not observe login_chat_gpt_complete notification after cancel"); + } + Ok(()) +} + +fn create_config_toml_forced_login(codex_home: &Path, forced_method: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + let contents = format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" +forced_login_method = "{forced_method}" +"# + ); + std::fs::write(config_toml, contents) +} + +fn create_config_toml_forced_workspace( + codex_home: &Path, + workspace_id: &str, +) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + let contents = format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" +forced_chatgpt_workspace_id = "{workspace_id}" +"# + ); + std::fs::write(config_toml, contents) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn login_chatgpt_rejected_when_forced_api() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml_forced_login(codex_home.path(), "api")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_chat_gpt_request().await?; + let err: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!( + err.error.message, + "ChatGPT login is disabled. Use API key login instead." + ); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +// Serialize tests that launch the login server since it binds to a fixed port. +#[serial(login_port)] +async fn login_chatgpt_includes_forced_workspace_query_param() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml_forced_workspace(codex_home.path(), "ws-forced")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_chat_gpt_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let login: LoginChatGptResponse = to_response(resp)?; + assert!( + login.auth_url.contains("allowed_workspace_id=ws-forced"), + "auth URL should include forced workspace" + ); + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/mod.rs b/codex-rs/app-server/tests/suite/mod.rs new file mode 100644 index 0000000000..37f7659f42 --- /dev/null +++ b/codex-rs/app-server/tests/suite/mod.rs @@ -0,0 +1,14 @@ +mod archive_conversation; +mod auth; +mod codex_message_processor_flow; +mod config; +mod create_conversation; +mod fuzzy_file_search; +mod interrupt; +mod list_resume; +mod login; +mod send_message; +mod set_default_model; +mod user_agent; +mod user_info; +mod v2; diff --git a/codex-rs/app-server/tests/suite/send_message.rs b/codex-rs/app-server/tests/suite/send_message.rs new file mode 100644 index 0000000000..8d2b36af2f --- /dev/null +++ b/codex-rs/app-server/tests/suite/send_message.rs @@ -0,0 +1,391 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_final_assistant_message_sse_response; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::to_response; +use codex_app_server_protocol::AddConversationListenerParams; +use codex_app_server_protocol::AddConversationSubscriptionResponse; +use codex_app_server_protocol::InputItem; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::NewConversationParams; +use codex_app_server_protocol::NewConversationResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SendUserMessageParams; +use codex_app_server_protocol::SendUserMessageResponse; +use codex_protocol::ConversationId; +use codex_protocol::models::ContentItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::protocol::RawResponseItemEvent; +use pretty_assertions::assert_eq; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn test_send_message_success() -> Result<()> { + // Spin up a mock completions server that immediately ends the Codex turn. + // Two Codex turns hit the mock model (session start + send-user-message). Provide two SSE responses. + let responses = vec![ + create_final_assistant_message_sse_response("Done")?, + create_final_assistant_message_sse_response("Done")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + + // Create a temporary Codex home with config pointing at the mock server. + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + // Start MCP server process and initialize. + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a conversation using the new wire API. + let new_conv_id = mcp + .send_new_conversation_request(NewConversationParams { + ..Default::default() + }) + .await?; + let new_conv_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), + ) + .await??; + let NewConversationResponse { + conversation_id, .. + } = to_response::<_>(new_conv_resp)?; + + // 2) addConversationListener + let add_listener_id = mcp + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: false, + }) + .await?; + let add_listener_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), + ) + .await??; + let AddConversationSubscriptionResponse { subscription_id: _ } = + to_response::<_>(add_listener_resp)?; + + // Now exercise sendUserMessage twice. + send_message("Hello", conversation_id, &mut mcp).await?; + send_message("Hello again", conversation_id, &mut mcp).await?; + Ok(()) +} + +#[expect(clippy::expect_used)] +async fn send_message( + message: &str, + conversation_id: ConversationId, + mcp: &mut McpProcess, +) -> Result<()> { + // Now exercise sendUserMessage. + let send_id = mcp + .send_send_user_message_request(SendUserMessageParams { + conversation_id, + items: vec![InputItem::Text { + text: message.to_string(), + }], + }) + .await?; + + let response: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(send_id)), + ) + .await??; + + let _ok: SendUserMessageResponse = to_response::(response)?; + + // Verify the task_finished notification is received. + // Note this also ensures that the final request to the server was made. + let task_finished_notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + let serde_json::Value::Object(map) = task_finished_notification + .params + .expect("notification should have params") + else { + panic!("task_finished_notification should have params"); + }; + assert_eq!( + map.get("conversationId") + .expect("should have conversationId"), + &serde_json::Value::String(conversation_id.to_string()) + ); + + let raw_attempt = tokio::time::timeout( + std::time::Duration::from_millis(200), + mcp.read_stream_until_notification_message("codex/event/raw_response_item"), + ) + .await; + assert!( + raw_attempt.is_err(), + "unexpected raw item notification when not opted in" + ); + Ok(()) +} + +#[tokio::test] +async fn test_send_message_raw_notifications_opt_in() -> Result<()> { + let responses = vec![create_final_assistant_message_sse_response("Done")?]; + let server = create_mock_chat_completions_server(responses).await; + + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let new_conv_id = mcp + .send_new_conversation_request(NewConversationParams { + developer_instructions: Some("Use the test harness tools.".to_string()), + ..Default::default() + }) + .await?; + let new_conv_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), + ) + .await??; + let NewConversationResponse { + conversation_id, .. + } = to_response::<_>(new_conv_resp)?; + + let add_listener_id = mcp + .send_add_conversation_listener_request(AddConversationListenerParams { + conversation_id, + experimental_raw_events: true, + }) + .await?; + let add_listener_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), + ) + .await??; + let AddConversationSubscriptionResponse { subscription_id: _ } = + to_response::<_>(add_listener_resp)?; + + let send_id = mcp + .send_send_user_message_request(SendUserMessageParams { + conversation_id, + items: vec![InputItem::Text { + text: "Hello".to_string(), + }], + }) + .await?; + + let developer = read_raw_response_item(&mut mcp, conversation_id).await; + assert_developer_message(&developer, "Use the test harness tools."); + + let instructions = read_raw_response_item(&mut mcp, conversation_id).await; + assert_instructions_message(&instructions); + + let environment = read_raw_response_item(&mut mcp, conversation_id).await; + assert_environment_message(&environment); + + let response: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(send_id)), + ) + .await??; + let _ok: SendUserMessageResponse = to_response::(response)?; + + let user_message = read_raw_response_item(&mut mcp, conversation_id).await; + assert_user_message(&user_message, "Hello"); + + let assistant_message = read_raw_response_item(&mut mcp, conversation_id).await; + assert_assistant_message(&assistant_message, "Done"); + + let _ = tokio::time::timeout( + std::time::Duration::from_millis(250), + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await; + + Ok(()) +} + +#[tokio::test] +async fn test_send_message_session_not_found() -> Result<()> { + // Start MCP without creating a Codex session + let codex_home = TempDir::new()?; + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let unknown = ConversationId::new(); + let req_id = mcp + .send_send_user_message_request(SendUserMessageParams { + conversation_id: unknown, + items: vec![InputItem::Text { + text: "ping".to_string(), + }], + }) + .await?; + + // Expect an error response for unknown conversation. + let err = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(req_id)), + ) + .await??; + assert_eq!(err.id, RequestId::Integer(req_id)); + Ok(()) +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} + +#[expect(clippy::expect_used)] +async fn read_raw_response_item( + mcp: &mut McpProcess, + conversation_id: ConversationId, +) -> ResponseItem { + let raw_notification: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/raw_response_item"), + ) + .await + .expect("codex/event/raw_response_item notification timeout") + .expect("codex/event/raw_response_item notification resp"); + + let serde_json::Value::Object(params) = raw_notification + .params + .expect("codex/event/raw_response_item should have params") + else { + panic!("codex/event/raw_response_item should have params"); + }; + + let conversation_id_value = params + .get("conversationId") + .and_then(|value| value.as_str()) + .expect("raw response item should include conversationId"); + + assert_eq!( + conversation_id_value, + conversation_id.to_string(), + "raw response item conversation mismatch" + ); + + let msg_value = params + .get("msg") + .cloned() + .expect("raw response item should include msg payload"); + + let event: RawResponseItemEvent = + serde_json::from_value(msg_value).expect("deserialize raw response item"); + event.item +} + +fn assert_instructions_message(item: &ResponseItem) { + match item { + ResponseItem::Message { role, content, .. } => { + assert_eq!(role, "user"); + let texts = content_texts(content); + let is_instructions = texts + .iter() + .any(|text| text.starts_with("# AGENTS.md instructions for ")); + assert!( + is_instructions, + "expected instructions message, got {texts:?}" + ); + } + other => panic!("expected instructions message, got {other:?}"), + } +} + +fn assert_developer_message(item: &ResponseItem, expected_text: &str) { + match item { + ResponseItem::Message { role, content, .. } => { + assert_eq!(role, "developer"); + let texts = content_texts(content); + assert_eq!( + texts, + vec![expected_text], + "expected developer instructions message, got {texts:?}" + ); + } + other => panic!("expected developer instructions message, got {other:?}"), + } +} + +fn assert_environment_message(item: &ResponseItem) { + match item { + ResponseItem::Message { role, content, .. } => { + assert_eq!(role, "user"); + let texts = content_texts(content); + assert!( + texts + .iter() + .any(|text| text.contains("")), + "expected environment context message, got {texts:?}" + ); + } + other => panic!("expected environment message, got {other:?}"), + } +} + +fn assert_user_message(item: &ResponseItem, expected_text: &str) { + match item { + ResponseItem::Message { role, content, .. } => { + assert_eq!(role, "user"); + let texts = content_texts(content); + assert_eq!(texts, vec![expected_text]); + } + other => panic!("expected user message, got {other:?}"), + } +} + +fn assert_assistant_message(item: &ResponseItem, expected_text: &str) { + match item { + ResponseItem::Message { role, content, .. } => { + assert_eq!(role, "assistant"); + let texts = content_texts(content); + assert_eq!(texts, vec![expected_text]); + } + other => panic!("expected assistant message, got {other:?}"), + } +} + +fn content_texts(content: &[ContentItem]) -> Vec<&str> { + content + .iter() + .filter_map(|item| match item { + ContentItem::InputText { text } | ContentItem::OutputText { text } => { + Some(text.as_str()) + } + _ => None, + }) + .collect() +} diff --git a/codex-rs/mcp-server/tests/suite/set_default_model.rs b/codex-rs/app-server/tests/suite/set_default_model.rs similarity index 53% rename from codex-rs/mcp-server/tests/suite/set_default_model.rs rename to codex-rs/app-server/tests/suite/set_default_model.rs index 7ffee3438d..f3af141c07 100644 --- a/codex-rs/mcp-server/tests/suite/set_default_model.rs +++ b/codex-rs/app-server/tests/suite/set_default_model.rs @@ -1,57 +1,44 @@ -use std::path::Path; - +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::SetDefaultModelParams; +use codex_app_server_protocol::SetDefaultModelResponse; use codex_core::config::ConfigToml; -use codex_protocol::mcp_protocol::SetDefaultModelParams; -use codex_protocol::mcp_protocol::SetDefaultModelResponse; -use mcp_test_support::McpProcess; -use mcp_test_support::to_response; -use mcp_types::JSONRPCResponse; -use mcp_types::RequestId; use pretty_assertions::assert_eq; +use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn set_default_model_persists_overrides() { - let codex_home = TempDir::new().expect("create tempdir"); - create_config_toml(codex_home.path()).expect("write config.toml"); +async fn set_default_model_persists_overrides() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; - let mut mcp = McpProcess::new(codex_home.path()) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let params = SetDefaultModelParams { model: Some("gpt-4.1".to_string()), reasoning_effort: None, }; - let request_id = mcp - .send_set_default_model_request(params) - .await - .expect("send setDefaultModel"); + let request_id = mcp.send_set_default_model_request(params).await?; let resp: JSONRPCResponse = timeout( DEFAULT_READ_TIMEOUT, mcp.read_stream_until_response_message(RequestId::Integer(request_id)), ) - .await - .expect("setDefaultModel timeout") - .expect("setDefaultModel response"); + .await??; - let _: SetDefaultModelResponse = - to_response(resp).expect("deserialize setDefaultModel response"); + let _: SetDefaultModelResponse = to_response(resp)?; let config_path = codex_home.path().join("config.toml"); - let config_contents = tokio::fs::read_to_string(&config_path) - .await - .expect("read config.toml"); - let config_toml: ConfigToml = toml::from_str(&config_contents).expect("parse config.toml"); + let config_contents = tokio::fs::read_to_string(&config_path).await?; + let config_toml: ConfigToml = toml::from_str(&config_contents)?; assert_eq!( ConfigToml { @@ -61,6 +48,7 @@ async fn set_default_model_persists_overrides() { }, config_toml, ); + Ok(()) } // Helper to create a config.toml; mirrors create_conversation.rs @@ -69,7 +57,7 @@ fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { std::fs::write( config_toml, r#" -model = "gpt-5" +model = "gpt-5.1-codex" model_reasoning_effort = "medium" "#, ) diff --git a/codex-rs/app-server/tests/suite/user_agent.rs b/codex-rs/app-server/tests/suite/user_agent.rs new file mode 100644 index 0000000000..52ba6e56aa --- /dev/null +++ b/codex-rs/app-server/tests/suite/user_agent.rs @@ -0,0 +1,41 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::GetUserAgentResponse; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use pretty_assertions::assert_eq; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn get_user_agent_returns_current_codex_user_agent() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_get_user_agent_request().await?; + let response: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let os_info = os_info::get(); + let user_agent = format!( + "codex_cli_rs/0.0.0 ({} {}; {}) {} (codex-app-server-tests; 0.1.0)", + os_info.os_type(), + os_info.version(), + os_info.architecture().unwrap_or("unknown"), + codex_core::terminal::user_agent() + ); + + let received: GetUserAgentResponse = to_response(response)?; + let expected = GetUserAgentResponse { user_agent }; + + assert_eq!(received, expected); + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/user_info.rs b/codex-rs/app-server/tests/suite/user_info.rs new file mode 100644 index 0000000000..6a44f1a3ab --- /dev/null +++ b/codex-rs/app-server/tests/suite/user_info.rs @@ -0,0 +1,46 @@ +use anyhow::Result; +use app_test_support::ChatGptAuthFixture; +use app_test_support::McpProcess; +use app_test_support::to_response; +use app_test_support::write_chatgpt_auth; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::UserInfoResponse; +use codex_core::auth::AuthCredentialsStoreMode; +use pretty_assertions::assert_eq; +use std::time::Duration; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(10); + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn user_info_returns_email_from_auth_json() -> Result<()> { + let codex_home = TempDir::new()?; + + write_chatgpt_auth( + codex_home.path(), + ChatGptAuthFixture::new("access") + .refresh_token("refresh") + .email("user@example.com"), + AuthCredentialsStoreMode::File, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_user_info_request().await?; + let response: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let received: UserInfoResponse = to_response(response)?; + let expected = UserInfoResponse { + alleged_user_email: Some("user@example.com".to_string()), + }; + + assert_eq!(received, expected); + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/v2/account.rs b/codex-rs/app-server/tests/suite/v2/account.rs new file mode 100644 index 0000000000..dd59270739 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/account.rs @@ -0,0 +1,492 @@ +use anyhow::Result; +use anyhow::bail; +use app_test_support::McpProcess; +use app_test_support::to_response; + +use app_test_support::ChatGptAuthFixture; +use app_test_support::write_chatgpt_auth; +use codex_app_server_protocol::Account; +use codex_app_server_protocol::AuthMode; +use codex_app_server_protocol::CancelLoginAccountParams; +use codex_app_server_protocol::CancelLoginAccountResponse; +use codex_app_server_protocol::GetAccountParams; +use codex_app_server_protocol::GetAccountResponse; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::LoginAccountResponse; +use codex_app_server_protocol::LogoutAccountResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ServerNotification; +use codex_core::auth::AuthCredentialsStoreMode; +use codex_login::login_with_api_key; +use codex_protocol::account::PlanType as AccountPlanType; +use pretty_assertions::assert_eq; +use serial_test::serial; +use std::path::Path; +use std::time::Duration; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +// Helper to create a minimal config.toml for the app server +#[derive(Default)] +struct CreateConfigTomlParams { + forced_method: Option, + forced_workspace_id: Option, + requires_openai_auth: Option, +} + +fn create_config_toml(codex_home: &Path, params: CreateConfigTomlParams) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + let forced_line = if let Some(method) = params.forced_method { + format!("forced_login_method = \"{method}\"\n") + } else { + String::new() + }; + let forced_workspace_line = if let Some(ws) = params.forced_workspace_id { + format!("forced_chatgpt_workspace_id = \"{ws}\"\n") + } else { + String::new() + }; + let requires_line = match params.requires_openai_auth { + Some(true) => "requires_openai_auth = true\n".to_string(), + Some(false) => String::new(), + None => String::new(), + }; + let contents = format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "danger-full-access" +{forced_line} +{forced_workspace_line} + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "http://127.0.0.1:0/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +{requires_line} +"# + ); + std::fs::write(config_toml, contents) +} + +#[tokio::test] +async fn logout_account_removes_auth_and_notifies() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), CreateConfigTomlParams::default())?; + + login_with_api_key( + codex_home.path(), + "sk-test-key", + AuthCredentialsStoreMode::File, + )?; + assert!(codex_home.path().join("auth.json").exists()); + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let id = mcp.send_logout_account_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(id)), + ) + .await??; + let _ok: LogoutAccountResponse = to_response(resp)?; + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/updated"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountUpdated(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + assert!( + payload.auth_mode.is_none(), + "auth_method should be None after logout" + ); + + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should be deleted" + ); + + let get_id = mcp + .send_get_account_request(GetAccountParams { + refresh_token: false, + }) + .await?; + let get_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(get_id)), + ) + .await??; + let account: GetAccountResponse = to_response(get_resp)?; + assert_eq!(account.account, None); + Ok(()) +} + +#[tokio::test] +async fn login_account_api_key_succeeds_and_notifies() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), CreateConfigTomlParams::default())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let req_id = mcp + .send_login_account_api_key_request("sk-test-key") + .await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(req_id)), + ) + .await??; + let login: LoginAccountResponse = to_response(resp)?; + assert_eq!(login, LoginAccountResponse::ApiKey {}); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountLoginCompleted(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + pretty_assertions::assert_eq!(payload.login_id, None); + pretty_assertions::assert_eq!(payload.success, true); + pretty_assertions::assert_eq!(payload.error, None); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/updated"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountUpdated(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + pretty_assertions::assert_eq!(payload.auth_mode, Some(AuthMode::ApiKey)); + + assert!(codex_home.path().join("auth.json").exists()); + Ok(()) +} + +#[tokio::test] +async fn login_account_api_key_rejected_when_forced_chatgpt() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + forced_method: Some("chatgpt".to_string()), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_login_account_api_key_request("sk-test-key") + .await?; + let err: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!( + err.error.message, + "API key login is disabled. Use ChatGPT login instead." + ); + Ok(()) +} + +#[tokio::test] +async fn login_account_chatgpt_rejected_when_forced_api() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + forced_method: Some("api".to_string()), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_request().await?; + let err: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!( + err.error.message, + "ChatGPT login is disabled. Use API key login instead." + ); + Ok(()) +} + +#[tokio::test] +// Serialize tests that launch the login server since it binds to a fixed port. +#[serial(login_port)] +async fn login_account_chatgpt_start() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), CreateConfigTomlParams::default())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let login: LoginAccountResponse = to_response(resp)?; + let LoginAccountResponse::Chatgpt { login_id, auth_url } = login else { + bail!("unexpected login response: {login:?}"); + }; + assert!( + auth_url.contains("redirect_uri=http%3A%2F%2Flocalhost"), + "auth_url should contain a redirect_uri to localhost" + ); + + let cancel_id = mcp + .send_cancel_login_account_request(CancelLoginAccountParams { + login_id: login_id.clone(), + }) + .await?; + let cancel_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(cancel_id)), + ) + .await??; + let _ok: CancelLoginAccountResponse = to_response(cancel_resp)?; + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountLoginCompleted(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + pretty_assertions::assert_eq!(payload.login_id, Some(login_id)); + pretty_assertions::assert_eq!(payload.success, false); + assert!( + payload.error.is_some(), + "expected a non-empty error on cancel" + ); + + let maybe_updated = timeout( + Duration::from_millis(500), + mcp.read_stream_until_notification_message("account/updated"), + ) + .await; + assert!( + maybe_updated.is_err(), + "account/updated should not be emitted when login is cancelled" + ); + Ok(()) +} + +#[tokio::test] +// Serialize tests that launch the login server since it binds to a fixed port. +#[serial(login_port)] +async fn login_account_chatgpt_includes_forced_workspace_query_param() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + forced_workspace_id: Some("ws-forced".to_string()), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let login: LoginAccountResponse = to_response(resp)?; + let LoginAccountResponse::Chatgpt { auth_url, .. } = login else { + bail!("unexpected login response: {login:?}"); + }; + assert!( + auth_url.contains("allowed_workspace_id=ws-forced"), + "auth URL should include forced workspace" + ); + Ok(()) +} + +#[tokio::test] +async fn get_account_no_auth() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let params = GetAccountParams { + refresh_token: false, + }; + let request_id = mcp.send_get_account_request(params).await?; + + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let account: GetAccountResponse = to_response(resp)?; + + assert_eq!(account.account, None, "expected no account"); + assert_eq!(account.requires_openai_auth, true); + Ok(()) +} + +#[tokio::test] +async fn get_account_with_api_key() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let req_id = mcp + .send_login_account_api_key_request("sk-test-key") + .await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(req_id)), + ) + .await??; + let _login_ok = to_response::(resp)?; + + let params = GetAccountParams { + refresh_token: false, + }; + let request_id = mcp.send_get_account_request(params).await?; + + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let received: GetAccountResponse = to_response(resp)?; + + let expected = GetAccountResponse { + account: Some(Account::ApiKey {}), + requires_openai_auth: true, + }; + assert_eq!(received, expected); + Ok(()) +} + +#[tokio::test] +async fn get_account_when_auth_not_required() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(false), + ..Default::default() + }, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let params = GetAccountParams { + refresh_token: false, + }; + let request_id = mcp.send_get_account_request(params).await?; + + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let received: GetAccountResponse = to_response(resp)?; + + let expected = GetAccountResponse { + account: None, + requires_openai_auth: false, + }; + assert_eq!(received, expected); + Ok(()) +} + +#[tokio::test] +async fn get_account_with_chatgpt() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + ..Default::default() + }, + )?; + write_chatgpt_auth( + codex_home.path(), + ChatGptAuthFixture::new("access-chatgpt") + .email("user@example.com") + .plan_type("pro"), + AuthCredentialsStoreMode::File, + )?; + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let params = GetAccountParams { + refresh_token: false, + }; + let request_id = mcp.send_get_account_request(params).await?; + + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let received: GetAccountResponse = to_response(resp)?; + + let expected = GetAccountResponse { + account: Some(Account::Chatgpt { + email: "user@example.com".to_string(), + plan_type: AccountPlanType::Pro, + }), + requires_openai_auth: true, + }; + assert_eq!(received, expected); + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/v2/mod.rs b/codex-rs/app-server/tests/suite/v2/mod.rs new file mode 100644 index 0000000000..587afef108 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/mod.rs @@ -0,0 +1,9 @@ +mod account; +mod model_list; +mod rate_limits; +mod thread_archive; +mod thread_list; +mod thread_resume; +mod thread_start; +mod turn_interrupt; +mod turn_start; diff --git a/codex-rs/app-server/tests/suite/v2/model_list.rs b/codex-rs/app-server/tests/suite/v2/model_list.rs new file mode 100644 index 0000000000..8b17185f44 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/model_list.rs @@ -0,0 +1,223 @@ +use std::time::Duration; + +use anyhow::Result; +use anyhow::anyhow; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::Model; +use codex_app_server_protocol::ModelListParams; +use codex_app_server_protocol::ModelListResponse; +use codex_app_server_protocol::ReasoningEffortOption; +use codex_app_server_protocol::RequestId; +use codex_protocol::config_types::ReasoningEffort; +use pretty_assertions::assert_eq; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); +const INVALID_REQUEST_ERROR_CODE: i64 = -32600; + +#[tokio::test] +async fn list_models_returns_all_models_with_large_limit() -> Result<()> { + let codex_home = TempDir::new()?; + let mut mcp = McpProcess::new(codex_home.path()).await?; + + timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_list_models_request(ModelListParams { + limit: Some(100), + cursor: None, + }) + .await?; + + let response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let ModelListResponse { + data: items, + next_cursor, + } = to_response::(response)?; + + let expected_models = vec![ + Model { + id: "gpt-5.1-codex".to_string(), + model: "gpt-5.1-codex".to_string(), + display_name: "gpt-5.1-codex".to_string(), + description: "Optimized for codex.".to_string(), + supported_reasoning_efforts: vec![ + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::Low, + description: "Fastest responses with limited reasoning".to_string(), + }, + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task".to_string(), + }, + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems" + .to_string(), + }, + ], + default_reasoning_effort: ReasoningEffort::Medium, + is_default: true, + }, + Model { + id: "gpt-5.1-codex-mini".to_string(), + model: "gpt-5.1-codex-mini".to_string(), + display_name: "gpt-5.1-codex-mini".to_string(), + description: "Optimized for codex. Cheaper, faster, but less capable.".to_string(), + supported_reasoning_efforts: vec![ + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task".to_string(), + }, + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems" + .to_string(), + }, + ], + default_reasoning_effort: ReasoningEffort::Medium, + is_default: false, + }, + Model { + id: "gpt-5.1".to_string(), + model: "gpt-5.1".to_string(), + display_name: "gpt-5.1".to_string(), + description: "Broad world knowledge with strong general reasoning.".to_string(), + supported_reasoning_efforts: vec![ + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::Low, + description: "Balances speed with some reasoning; useful for straightforward \ + queries and short explanations" + .to_string(), + }, + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::Medium, + description: "Provides a solid balance of reasoning depth and latency for \ + general-purpose tasks" + .to_string(), + }, + ReasoningEffortOption { + reasoning_effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems" + .to_string(), + }, + ], + default_reasoning_effort: ReasoningEffort::Medium, + is_default: false, + }, + ]; + + assert_eq!(items, expected_models); + assert!(next_cursor.is_none()); + Ok(()) +} + +#[tokio::test] +async fn list_models_pagination_works() -> Result<()> { + let codex_home = TempDir::new()?; + let mut mcp = McpProcess::new(codex_home.path()).await?; + + timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??; + + let first_request = mcp + .send_list_models_request(ModelListParams { + limit: Some(1), + cursor: None, + }) + .await?; + + let first_response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(first_request)), + ) + .await??; + + let ModelListResponse { + data: first_items, + next_cursor: first_cursor, + } = to_response::(first_response)?; + + assert_eq!(first_items.len(), 1); + assert_eq!(first_items[0].id, "gpt-5.1-codex"); + let next_cursor = first_cursor.ok_or_else(|| anyhow!("cursor for second page"))?; + + let second_request = mcp + .send_list_models_request(ModelListParams { + limit: Some(1), + cursor: Some(next_cursor.clone()), + }) + .await?; + + let second_response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(second_request)), + ) + .await??; + + let ModelListResponse { + data: second_items, + next_cursor: second_cursor, + } = to_response::(second_response)?; + + assert_eq!(second_items.len(), 1); + assert_eq!(second_items[0].id, "gpt-5.1-codex-mini"); + let third_cursor = second_cursor.ok_or_else(|| anyhow!("cursor for third page"))?; + + let third_request = mcp + .send_list_models_request(ModelListParams { + limit: Some(1), + cursor: Some(third_cursor.clone()), + }) + .await?; + + let third_response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(third_request)), + ) + .await??; + + let ModelListResponse { + data: third_items, + next_cursor: third_cursor, + } = to_response::(third_response)?; + + assert_eq!(third_items.len(), 1); + assert_eq!(third_items[0].id, "gpt-5.1"); + assert!(third_cursor.is_none()); + Ok(()) +} + +#[tokio::test] +async fn list_models_rejects_invalid_cursor() -> Result<()> { + let codex_home = TempDir::new()?; + let mut mcp = McpProcess::new(codex_home.path()).await?; + + timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_list_models_request(ModelListParams { + limit: None, + cursor: Some("invalid".to_string()), + }) + .await?; + + let error: JSONRPCError = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!(error.id, RequestId::Integer(request_id)); + assert_eq!(error.error.code, INVALID_REQUEST_ERROR_CODE); + assert_eq!(error.error.message, "invalid cursor: invalid"); + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/v2/rate_limits.rs b/codex-rs/app-server/tests/suite/v2/rate_limits.rs new file mode 100644 index 0000000000..d0cba83667 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/rate_limits.rs @@ -0,0 +1,181 @@ +use anyhow::Result; +use app_test_support::ChatGptAuthFixture; +use app_test_support::McpProcess; +use app_test_support::to_response; +use app_test_support::write_chatgpt_auth; +use codex_app_server_protocol::GetAccountRateLimitsResponse; +use codex_app_server_protocol::JSONRPCError; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::LoginApiKeyParams; +use codex_app_server_protocol::RateLimitSnapshot; +use codex_app_server_protocol::RateLimitWindow; +use codex_app_server_protocol::RequestId; +use codex_core::auth::AuthCredentialsStoreMode; +use pretty_assertions::assert_eq; +use serde_json::json; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; +use wiremock::Mock; +use wiremock::MockServer; +use wiremock::ResponseTemplate; +use wiremock::matchers::header; +use wiremock::matchers::method; +use wiremock::matchers::path; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); +const INVALID_REQUEST_ERROR_CODE: i64 = -32600; + +#[tokio::test] +async fn get_account_rate_limits_requires_auth() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_get_account_rate_limits_request().await?; + + let error: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!(error.id, RequestId::Integer(request_id)); + assert_eq!(error.error.code, INVALID_REQUEST_ERROR_CODE); + assert_eq!( + error.error.message, + "codex account authentication required to read rate limits" + ); + + Ok(()) +} + +#[tokio::test] +async fn get_account_rate_limits_requires_chatgpt_auth() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + login_with_api_key(&mut mcp, "sk-test-key").await?; + + let request_id = mcp.send_get_account_rate_limits_request().await?; + + let error: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + + assert_eq!(error.id, RequestId::Integer(request_id)); + assert_eq!(error.error.code, INVALID_REQUEST_ERROR_CODE); + assert_eq!( + error.error.message, + "chatgpt authentication required to read rate limits" + ); + + Ok(()) +} + +#[tokio::test] +async fn get_account_rate_limits_returns_snapshot() -> Result<()> { + let codex_home = TempDir::new()?; + write_chatgpt_auth( + codex_home.path(), + ChatGptAuthFixture::new("chatgpt-token") + .account_id("account-123") + .plan_type("pro"), + AuthCredentialsStoreMode::File, + )?; + + let server = MockServer::start().await; + let server_url = server.uri(); + write_chatgpt_base_url(codex_home.path(), &server_url)?; + + let primary_reset_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T00:02:00Z") + .expect("parse primary reset timestamp") + .timestamp(); + let secondary_reset_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T01:00:00Z") + .expect("parse secondary reset timestamp") + .timestamp(); + let response_body = json!({ + "plan_type": "pro", + "rate_limit": { + "allowed": true, + "limit_reached": false, + "primary_window": { + "used_percent": 42, + "limit_window_seconds": 3600, + "reset_after_seconds": 120, + "reset_at": primary_reset_timestamp, + }, + "secondary_window": { + "used_percent": 5, + "limit_window_seconds": 86400, + "reset_after_seconds": 43200, + "reset_at": secondary_reset_timestamp, + } + } + }); + + Mock::given(method("GET")) + .and(path("/api/codex/usage")) + .and(header("authorization", "Bearer chatgpt-token")) + .and(header("chatgpt-account-id", "account-123")) + .respond_with(ResponseTemplate::new(200).set_body_json(response_body)) + .mount(&server) + .await; + + let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_get_account_rate_limits_request().await?; + + let response: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let received: GetAccountRateLimitsResponse = to_response(response)?; + + let expected = GetAccountRateLimitsResponse { + rate_limits: RateLimitSnapshot { + primary: Some(RateLimitWindow { + used_percent: 42, + window_duration_mins: Some(60), + resets_at: Some(primary_reset_timestamp), + }), + secondary: Some(RateLimitWindow { + used_percent: 5, + window_duration_mins: Some(1440), + resets_at: Some(secondary_reset_timestamp), + }), + }, + }; + assert_eq!(received, expected); + + Ok(()) +} + +async fn login_with_api_key(mcp: &mut McpProcess, api_key: &str) -> Result<()> { + let request_id = mcp + .send_login_api_key_request(LoginApiKeyParams { + api_key: api_key.to_string(), + }) + .await?; + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + Ok(()) +} + +fn write_chatgpt_base_url(codex_home: &Path, base_url: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write(config_toml, format!("chatgpt_base_url = \"{base_url}\"\n")) +} diff --git a/codex-rs/app-server/tests/suite/v2/thread_archive.rs b/codex-rs/app-server/tests/suite/v2/thread_archive.rs new file mode 100644 index 0000000000..083f3da901 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/thread_archive.rs @@ -0,0 +1,93 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ThreadArchiveParams; +use codex_app_server_protocol::ThreadArchiveResponse; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_core::ARCHIVED_SESSIONS_SUBDIR; +use codex_core::find_conversation_path_by_id_str; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn thread_archive_moves_rollout_into_archived_directory() -> Result<()> { + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a thread. + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + assert!(!thread.id.is_empty()); + + // Locate the rollout path recorded for this thread id. + let rollout_path = find_conversation_path_by_id_str(codex_home.path(), &thread.id) + .await? + .expect("expected rollout path for thread id to exist"); + assert!( + rollout_path.exists(), + "expected {} to exist", + rollout_path.display() + ); + + // Archive the thread. + let archive_id = mcp + .send_thread_archive_request(ThreadArchiveParams { + thread_id: thread.id.clone(), + }) + .await?; + let archive_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(archive_id)), + ) + .await??; + let _: ThreadArchiveResponse = to_response::(archive_resp)?; + + // Verify file moved. + let archived_directory = codex_home.path().join(ARCHIVED_SESSIONS_SUBDIR); + // The archived file keeps the original filename (rollout-...-.jsonl). + let archived_rollout_path = + archived_directory.join(rollout_path.file_name().expect("rollout file name")); + assert!( + !rollout_path.exists(), + "expected rollout path {} to be moved", + rollout_path.display() + ); + assert!( + archived_rollout_path.exists(), + "expected archived rollout path {} to exist", + archived_rollout_path.display() + ); + + Ok(()) +} + +fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write(config_toml, config_contents()) +} + +fn config_contents() -> &'static str { + r#"model = "mock-model" +approval_policy = "never" +sandbox_mode = "read-only" +"# +} diff --git a/codex-rs/app-server/tests/suite/v2/thread_list.rs b/codex-rs/app-server/tests/suite/v2/thread_list.rs new file mode 100644 index 0000000000..464fb4eee8 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/thread_list.rs @@ -0,0 +1,185 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_fake_rollout; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ThreadListParams; +use codex_app_server_protocol::ThreadListResponse; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn thread_list_basic_empty() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // List threads in an empty CODEX_HOME; should return an empty page with nextCursor: null. + let list_id = mcp + .send_thread_list_request(ThreadListParams { + cursor: None, + limit: Some(10), + model_providers: None, + }) + .await?; + let list_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(list_id)), + ) + .await??; + let ThreadListResponse { data, next_cursor } = to_response::(list_resp)?; + assert!(data.is_empty()); + assert_eq!(next_cursor, None); + + Ok(()) +} + +// Minimal config.toml for listing. +fn create_minimal_config(codex_home: &std::path::Path) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + r#" +model = "mock-model" +approval_policy = "never" +"#, + ) +} + +#[tokio::test] +async fn thread_list_pagination_next_cursor_none_on_last_page() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + // Create three rollouts so we can paginate with limit=2. + let _a = create_fake_rollout( + codex_home.path(), + "2025-01-02T12-00-00", + "2025-01-02T12:00:00Z", + "Hello", + Some("mock_provider"), + )?; + let _b = create_fake_rollout( + codex_home.path(), + "2025-01-01T13-00-00", + "2025-01-01T13:00:00Z", + "Hello", + Some("mock_provider"), + )?; + let _c = create_fake_rollout( + codex_home.path(), + "2025-01-01T12-00-00", + "2025-01-01T12:00:00Z", + "Hello", + Some("mock_provider"), + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Page 1: limit 2 → expect next_cursor Some. + let page1_id = mcp + .send_thread_list_request(ThreadListParams { + cursor: None, + limit: Some(2), + model_providers: Some(vec!["mock_provider".to_string()]), + }) + .await?; + let page1_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(page1_id)), + ) + .await??; + let ThreadListResponse { + data: data1, + next_cursor: cursor1, + } = to_response::(page1_resp)?; + assert_eq!(data1.len(), 2); + for thread in &data1 { + assert_eq!(thread.preview, "Hello"); + assert_eq!(thread.model_provider, "mock_provider"); + assert!(thread.created_at > 0); + } + let cursor1 = cursor1.expect("expected nextCursor on first page"); + + // Page 2: with cursor → expect next_cursor None when no more results. + let page2_id = mcp + .send_thread_list_request(ThreadListParams { + cursor: Some(cursor1), + limit: Some(2), + model_providers: Some(vec!["mock_provider".to_string()]), + }) + .await?; + let page2_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(page2_id)), + ) + .await??; + let ThreadListResponse { + data: data2, + next_cursor: cursor2, + } = to_response::(page2_resp)?; + assert!(data2.len() <= 2); + for thread in &data2 { + assert_eq!(thread.preview, "Hello"); + assert_eq!(thread.model_provider, "mock_provider"); + assert!(thread.created_at > 0); + } + assert_eq!(cursor2, None, "expected nextCursor to be null on last page"); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_respects_provider_filter() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + // Create rollouts under two providers. + let _a = create_fake_rollout( + codex_home.path(), + "2025-01-02T10-00-00", + "2025-01-02T10:00:00Z", + "X", + Some("mock_provider"), + )?; // mock_provider + let _b = create_fake_rollout( + codex_home.path(), + "2025-01-02T11-00-00", + "2025-01-02T11:00:00Z", + "X", + Some("other_provider"), + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Filter to only other_provider; expect 1 item, nextCursor None. + let list_id = mcp + .send_thread_list_request(ThreadListParams { + cursor: None, + limit: Some(10), + model_providers: Some(vec!["other_provider".to_string()]), + }) + .await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(list_id)), + ) + .await??; + let ThreadListResponse { data, next_cursor } = to_response::(resp)?; + assert_eq!(data.len(), 1); + assert_eq!(next_cursor, None); + let thread = &data[0]; + assert_eq!(thread.preview, "X"); + assert_eq!(thread.model_provider, "other_provider"); + let expected_ts = chrono::DateTime::parse_from_rfc3339("2025-01-02T11:00:00Z")?.timestamp(); + assert_eq!(thread.created_at, expected_ts); + + Ok(()) +} diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs new file mode 100644 index 0000000000..bda2d14172 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -0,0 +1,181 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ThreadResumeParams; +use codex_app_server_protocol::ThreadResumeResponse; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_protocol::models::ContentItem; +use codex_protocol::models::ResponseItem; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn thread_resume_returns_original_thread() -> Result<()> { + let server = create_mock_chat_completions_server(vec![]).await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a thread. + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("gpt-5.1-codex".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + + // Resume it via v2 API. + let resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: thread.id.clone(), + ..Default::default() + }) + .await?; + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_id)), + ) + .await??; + let ThreadResumeResponse { thread: resumed } = + to_response::(resume_resp)?; + assert_eq!(resumed, thread); + + Ok(()) +} + +#[tokio::test] +async fn thread_resume_prefers_path_over_thread_id() -> Result<()> { + let server = create_mock_chat_completions_server(vec![]).await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("gpt-5.1-codex".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + + let thread_path = thread.path.clone(); + let resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: "not-a-valid-thread-id".to_string(), + path: Some(thread_path), + ..Default::default() + }) + .await?; + + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_id)), + ) + .await??; + let ThreadResumeResponse { thread: resumed } = + to_response::(resume_resp)?; + assert_eq!(resumed, thread); + + Ok(()) +} + +#[tokio::test] +async fn thread_resume_supports_history_and_overrides() -> Result<()> { + let server = create_mock_chat_completions_server(vec![]).await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a thread. + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("gpt-5.1-codex".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + + let history_text = "Hello from history"; + let history = vec![ResponseItem::Message { + id: None, + role: "user".to_string(), + content: vec![ContentItem::InputText { + text: history_text.to_string(), + }], + }]; + + // Resume with explicit history and override the model. + let resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: thread.id, + history: Some(history), + model: Some("mock-model".to_string()), + model_provider: Some("mock_provider".to_string()), + ..Default::default() + }) + .await?; + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_id)), + ) + .await??; + let ThreadResumeResponse { thread: resumed } = + to_response::(resume_resp)?; + assert!(!resumed.id.is_empty()); + assert_eq!(resumed.model_provider, "mock_provider"); + assert_eq!(resumed.preview, history_text); + + Ok(()) +} + +// Helper to create a config.toml pointing at the mock model server. +fn create_config_toml(codex_home: &std::path::Path, server_uri: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "read-only" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/app-server/tests/suite/v2/thread_start.rs b/codex-rs/app-server/tests/suite/v2/thread_start.rs new file mode 100644 index 0000000000..a5e4c0d487 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/thread_start.rs @@ -0,0 +1,90 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_app_server_protocol::ThreadStartedNotification; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn thread_start_creates_thread_and_emits_started() -> Result<()> { + // Provide a mock server and config so model wiring is valid. + let server = create_mock_chat_completions_server(vec![]).await; + + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + // Start server and initialize. + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a v2 thread with an explicit model override. + let req_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("gpt-5.1".to_string()), + ..Default::default() + }) + .await?; + + // Expect a proper JSON-RPC response with a thread id. + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(req_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(resp)?; + assert!(!thread.id.is_empty(), "thread id should not be empty"); + assert!( + thread.preview.is_empty(), + "new threads should start with an empty preview" + ); + assert_eq!(thread.model_provider, "mock_provider"); + assert!( + thread.created_at > 0, + "created_at should be a positive UNIX timestamp" + ); + + // A corresponding thread/started notification should arrive. + let notif: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("thread/started"), + ) + .await??; + let started: ThreadStartedNotification = + serde_json::from_value(notif.params.expect("params must be present"))?; + assert_eq!(started.thread, thread); + + Ok(()) +} + +// Helper to create a config.toml pointing at the mock model server. +fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "read-only" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs new file mode 100644 index 0000000000..d1deb60801 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs @@ -0,0 +1,128 @@ +#![cfg(unix)] + +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::create_shell_sse_response; +use app_test_support::to_response; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_app_server_protocol::TurnInterruptParams; +use codex_app_server_protocol::TurnInterruptResponse; +use codex_app_server_protocol::TurnStartParams; +use codex_app_server_protocol::TurnStartResponse; +use codex_app_server_protocol::UserInput as V2UserInput; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn turn_interrupt_aborts_running_turn() -> Result<()> { + // Use a portable sleep command to keep the turn running. + #[cfg(target_os = "windows")] + let shell_command = vec![ + "powershell".to_string(), + "-Command".to_string(), + "Start-Sleep -Seconds 10".to_string(), + ]; + #[cfg(not(target_os = "windows"))] + let shell_command = vec!["sleep".to_string(), "10".to_string()]; + + let tmp = TempDir::new()?; + let codex_home = tmp.path().join("codex_home"); + std::fs::create_dir(&codex_home)?; + let working_directory = tmp.path().join("workdir"); + std::fs::create_dir(&working_directory)?; + + // Mock server: long-running shell command then (after abort) nothing else needed. + let server = create_mock_chat_completions_server(vec![create_shell_sse_response( + shell_command.clone(), + Some(&working_directory), + Some(10_000), + "call_sleep", + )?]) + .await; + create_config_toml(&codex_home, &server.uri())?; + + let mut mcp = McpProcess::new(&codex_home).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a v2 thread and capture its id. + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let thread_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(thread_resp)?; + + // Start a turn that triggers a long-running command. + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "run sleep".to_string(), + }], + cwd: Some(working_directory.clone()), + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let TurnStartResponse { turn } = to_response::(turn_resp)?; + + // Give the command a brief moment to start. + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + + // Interrupt the in-progress turn by id (v2 API). + let interrupt_id = mcp + .send_turn_interrupt_request(TurnInterruptParams { + thread_id: thread.id, + turn_id: turn.id, + }) + .await?; + let interrupt_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(interrupt_id)), + ) + .await??; + let _resp: TurnInterruptResponse = to_response::(interrupt_resp)?; + + // No fields to assert on; successful deserialization confirms proper response shape. + Ok(()) +} + +// Helper to create a config.toml pointing at the mock model server. +fn create_config_toml(codex_home: &std::path::Path, server_uri: &str) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "workspace-write" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/app-server/tests/suite/v2/turn_start.rs b/codex-rs/app-server/tests/suite/v2/turn_start.rs new file mode 100644 index 0000000000..433c7b4486 --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/turn_start.rs @@ -0,0 +1,484 @@ +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::create_final_assistant_message_sse_response; +use app_test_support::create_mock_chat_completions_server; +use app_test_support::create_mock_chat_completions_server_unchecked; +use app_test_support::create_shell_sse_response; +use app_test_support::to_response; +use codex_app_server_protocol::CommandExecutionStatus; +use codex_app_server_protocol::ItemStartedNotification; +use codex_app_server_protocol::JSONRPCNotification; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ServerRequest; +use codex_app_server_protocol::ThreadItem; +use codex_app_server_protocol::ThreadStartParams; +use codex_app_server_protocol::ThreadStartResponse; +use codex_app_server_protocol::TurnStartParams; +use codex_app_server_protocol::TurnStartResponse; +use codex_app_server_protocol::TurnStartedNotification; +use codex_app_server_protocol::UserInput as V2UserInput; +use codex_core::protocol_config_types::ReasoningEffort; +use codex_core::protocol_config_types::ReasoningSummary; +use core_test_support::skip_if_no_network; +use pretty_assertions::assert_eq; +use std::path::Path; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +#[tokio::test] +async fn turn_start_emits_notifications_and_accepts_model_override() -> Result<()> { + // Provide a mock server and config so model wiring is valid. + // Three Codex turns hit the mock model (session start + two turn/start calls). + let responses = vec![ + create_final_assistant_message_sse_response("Done")?, + create_final_assistant_message_sse_response("Done")?, + create_final_assistant_message_sse_response("Done")?, + ]; + let server = create_mock_chat_completions_server_unchecked(responses).await; + + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri(), "never")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // Start a thread (v2) and capture its id. + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let thread_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(thread_resp)?; + + // Start a turn with only input and thread_id set (no overrides). + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "Hello".to_string(), + }], + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let TurnStartResponse { turn } = to_response::(turn_resp)?; + assert!(!turn.id.is_empty()); + + // Expect a turn/started notification. + let notif: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("turn/started"), + ) + .await??; + let started: TurnStartedNotification = + serde_json::from_value(notif.params.expect("params must be present"))?; + assert_eq!( + started.turn.status, + codex_app_server_protocol::TurnStatus::InProgress + ); + + // Send a second turn that exercises the overrides path: change the model. + let turn_req2 = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "Second".to_string(), + }], + model: Some("mock-model-override".to_string()), + ..Default::default() + }) + .await?; + let turn_resp2: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req2)), + ) + .await??; + let TurnStartResponse { turn: turn2 } = to_response::(turn_resp2)?; + assert!(!turn2.id.is_empty()); + // Ensure the second turn has a different id than the first. + assert_ne!(turn.id, turn2.id); + + // Expect a second turn/started notification as well. + let _notif2: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("turn/started"), + ) + .await??; + + // And we should ultimately get a task_complete without having to add a + // legacy conversation listener explicitly (auto-attached by thread/start). + let _task_complete: JSONRPCNotification = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + Ok(()) +} + +#[tokio::test] +async fn turn_start_accepts_local_image_input() -> Result<()> { + // Two Codex turns hit the mock model (session start + turn/start). + let responses = vec![ + create_final_assistant_message_sse_response("Done")?, + create_final_assistant_message_sse_response("Done")?, + ]; + // Use the unchecked variant because the request payload includes a LocalImage + // which the strict matcher does not currently cover. + let server = create_mock_chat_completions_server_unchecked(responses).await; + + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri(), "never")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let thread_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(thread_resp)?; + + let image_path = codex_home.path().join("image.png"); + // No need to actually write the file; we just exercise the input path. + + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::LocalImage { path: image_path }], + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let TurnStartResponse { turn } = to_response::(turn_resp)?; + assert!(!turn.id.is_empty()); + + // This test only validates that turn/start responds and returns a turn. + Ok(()) +} + +#[tokio::test] +async fn turn_start_exec_approval_toggle_v2() -> Result<()> { + skip_if_no_network!(Ok(())); + + let tmp = TempDir::new()?; + let codex_home = tmp.path().to_path_buf(); + + // Mock server: first turn requests a shell call (elicitation), then completes. + // Second turn same, but we'll set approval_policy=never to avoid elicitation. + let responses = vec![ + create_shell_sse_response( + vec![ + "python3".to_string(), + "-c".to_string(), + "print(42)".to_string(), + ], + None, + Some(5000), + "call1", + )?, + create_final_assistant_message_sse_response("done 1")?, + create_shell_sse_response( + vec![ + "python3".to_string(), + "-c".to_string(), + "print(42)".to_string(), + ], + None, + Some(5000), + "call2", + )?, + create_final_assistant_message_sse_response("done 2")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + // Default approval is untrusted to force elicitation on first turn. + create_config_toml(codex_home.as_path(), &server.uri(), "untrusted")?; + + let mut mcp = McpProcess::new(codex_home.as_path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // thread/start + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + + // turn/start — expect CommandExecutionRequestApproval request from server + let first_turn_id = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "run python".to_string(), + }], + ..Default::default() + }) + .await?; + // Acknowledge RPC + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(first_turn_id)), + ) + .await??; + + // Receive elicitation + let server_req = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_request_message(), + ) + .await??; + let ServerRequest::CommandExecutionRequestApproval { request_id, params } = server_req else { + panic!("expected CommandExecutionRequestApproval request"); + }; + assert_eq!(params.item_id, "call1"); + + // Approve and wait for task completion + mcp.send_response( + request_id, + serde_json::json!({ "decision": codex_core::protocol::ReviewDecision::Approved }), + ) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + // Second turn with approval_policy=never should not elicit approval + let second_turn_id = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "run python again".to_string(), + }], + approval_policy: Some(codex_app_server_protocol::AskForApproval::Never), + sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::DangerFullAccess), + model: Some("mock-model".to_string()), + effort: Some(ReasoningEffort::Medium), + summary: Some(ReasoningSummary::Auto), + ..Default::default() + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(second_turn_id)), + ) + .await??; + + // Ensure we do NOT receive a CommandExecutionRequestApproval request before task completes + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + Ok(()) +} + +#[tokio::test] +async fn turn_start_updates_sandbox_and_cwd_between_turns_v2() -> Result<()> { + skip_if_no_network!(Ok(())); + + let tmp = TempDir::new()?; + let codex_home = tmp.path().join("codex_home"); + std::fs::create_dir(&codex_home)?; + let workspace_root = tmp.path().join("workspace"); + std::fs::create_dir(&workspace_root)?; + let first_cwd = workspace_root.join("turn1"); + let second_cwd = workspace_root.join("turn2"); + std::fs::create_dir(&first_cwd)?; + std::fs::create_dir(&second_cwd)?; + + let responses = vec![ + create_shell_sse_response( + vec![ + "bash".to_string(), + "-lc".to_string(), + "echo first turn".to_string(), + ], + None, + Some(5000), + "call-first", + )?, + create_final_assistant_message_sse_response("done first")?, + create_shell_sse_response( + vec![ + "bash".to_string(), + "-lc".to_string(), + "echo second turn".to_string(), + ], + None, + Some(5000), + "call-second", + )?, + create_final_assistant_message_sse_response("done second")?, + ]; + let server = create_mock_chat_completions_server(responses).await; + create_config_toml(&codex_home, &server.uri(), "untrusted")?; + + let mut mcp = McpProcess::new(&codex_home).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + // thread/start + let start_id = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread } = to_response::(start_resp)?; + + // first turn with workspace-write sandbox and first_cwd + let first_turn = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "first turn".to_string(), + }], + cwd: Some(first_cwd.clone()), + approval_policy: Some(codex_app_server_protocol::AskForApproval::Never), + sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::WorkspaceWrite { + writable_roots: vec![first_cwd.clone()], + network_access: false, + exclude_tmpdir_env_var: false, + exclude_slash_tmp: false, + }), + model: Some("mock-model".to_string()), + effort: Some(ReasoningEffort::Medium), + summary: Some(ReasoningSummary::Auto), + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(first_turn)), + ) + .await??; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + // second turn with workspace-write and second_cwd, ensure exec begins in second_cwd + let second_turn = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "second turn".to_string(), + }], + cwd: Some(second_cwd.clone()), + approval_policy: Some(codex_app_server_protocol::AskForApproval::Never), + sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::DangerFullAccess), + model: Some("mock-model".to_string()), + effort: Some(ReasoningEffort::Medium), + summary: Some(ReasoningSummary::Auto), + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(second_turn)), + ) + .await??; + + let command_exec_item = timeout(DEFAULT_READ_TIMEOUT, async { + loop { + let item_started_notification = mcp + .read_stream_until_notification_message("item/started") + .await?; + let params = item_started_notification + .params + .clone() + .expect("item/started params"); + let item_started: ItemStartedNotification = + serde_json::from_value(params).expect("deserialize item/started notification"); + if matches!(item_started.item, ThreadItem::CommandExecution { .. }) { + return Ok::(item_started.item); + } + } + }) + .await??; + let ThreadItem::CommandExecution { + cwd, + command, + status, + .. + } = command_exec_item + else { + unreachable!("loop ensures we break on command execution items"); + }; + assert_eq!(cwd, second_cwd); + assert_eq!(command, "bash -lc 'echo second turn'"); + assert_eq!(status, CommandExecutionStatus::InProgress); + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("codex/event/task_complete"), + ) + .await??; + + Ok(()) +} + +// Helper to create a config.toml pointing at the mock model server. +fn create_config_toml( + codex_home: &Path, + server_uri: &str, + approval_policy: &str, +) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "{approval_policy}" +sandbox_mode = "read-only" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/apply-patch/Cargo.toml b/codex-rs/apply-patch/Cargo.toml index 7b5919a323..a239cd6310 100644 --- a/codex-rs/apply-patch/Cargo.toml +++ b/codex-rs/apply-patch/Cargo.toml @@ -15,14 +15,14 @@ path = "src/main.rs" workspace = true [dependencies] -anyhow = "1" -similar = "2.7.0" -thiserror = "2.0.16" -tree-sitter = "0.25.9" -tree-sitter-bash = "0.25.0" -once_cell = "1" +anyhow = { workspace = true } +similar = { workspace = true } +thiserror = { workspace = true } +tree-sitter = { workspace = true } +tree-sitter-bash = { workspace = true } [dev-dependencies] -assert_cmd = "2" -pretty_assertions = "1.4.1" -tempfile = "3.13.0" +assert_cmd = { workspace = true } +assert_matches = { workspace = true } +pretty_assertions = { workspace = true } +tempfile = { workspace = true } diff --git a/codex-rs/apply-patch/src/lib.rs b/codex-rs/apply-patch/src/lib.rs index 8735832561..bf904cf0a8 100644 --- a/codex-rs/apply-patch/src/lib.rs +++ b/codex-rs/apply-patch/src/lib.rs @@ -6,10 +6,10 @@ use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; use std::str::Utf8Error; +use std::sync::LazyLock; use anyhow::Context; use anyhow::Result; -use once_cell::sync::Lazy; pub use parser::Hunk; pub use parser::ParseError; use parser::ParseError::*; @@ -288,7 +288,7 @@ pub fn maybe_parse_apply_patch_verified(argv: &[String], cwd: &Path) -> MaybeApp path, ApplyPatchFileChange::Update { unified_diff, - move_path: move_path.map(|p| cwd.join(p)), + move_path: move_path.map(|p| effective_cwd.join(p)), new_content: contents, }, ); @@ -351,7 +351,7 @@ fn extract_apply_patch_from_bash( // also run an arbitrary query against the AST. This is useful for understanding // how tree-sitter parses the script and whether the query syntax is correct. Be sure // to test both positive and negative cases. - static APPLY_PATCH_QUERY: Lazy = Lazy::new(|| { + static APPLY_PATCH_QUERY: LazyLock = LazyLock::new(|| { let language = BASH.into(); #[expect(clippy::expect_used)] Query::new( @@ -678,14 +678,14 @@ fn derive_new_contents_from_chunks( // Drop the trailing empty element that results from the final newline so // that line counts match the behaviour of standard `diff`. - if original_lines.last().is_some_and(|s| s.is_empty()) { + if original_lines.last().is_some_and(String::is_empty) { original_lines.pop(); } let replacements = compute_replacements(&original_lines, path, chunks)?; let new_lines = apply_replacements(original_lines, &replacements); let mut new_lines = new_lines; - if !new_lines.last().is_some_and(|s| s.is_empty()) { + if !new_lines.last().is_some_and(String::is_empty) { new_lines.push(String::new()); } let new_contents = if uses_crlf { @@ -733,7 +733,7 @@ fn compute_replacements( if chunk.old_lines.is_empty() { // Pure addition (no old lines). We'll add them at the end or just // before the final empty line if one exists. - let insertion_idx = if original_lines.last().is_some_and(|s| s.is_empty()) { + let insertion_idx = if original_lines.last().is_some_and(String::is_empty) { original_lines.len() - 1 } else { original_lines.len() @@ -759,11 +759,11 @@ fn compute_replacements( let mut new_slice: &[String] = &chunk.new_lines; - if found.is_none() && pattern.last().is_some_and(|s| s.is_empty()) { + if found.is_none() && pattern.last().is_some_and(String::is_empty) { // Retry without the trailing empty line which represents the final // newline in the file. pattern = &pattern[..pattern.len() - 1]; - if new_slice.last().is_some_and(|s| s.is_empty()) { + if new_slice.last().is_some_and(String::is_empty) { new_slice = &new_slice[..new_slice.len() - 1]; } @@ -873,8 +873,10 @@ pub fn print_summary( #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; use pretty_assertions::assert_eq; use std::fs; + use std::string::ToString; use tempfile::tempdir; /// Helper to construct a patch with the given body. @@ -883,7 +885,7 @@ mod tests { } fn strs_to_strings(strs: &[&str]) -> Vec { - strs.iter().map(|s| s.to_string()).collect() + strs.iter().map(ToString::to_string).collect() } // Test helpers to reduce repetition when building bash -lc heredoc scripts @@ -923,10 +925,10 @@ mod tests { fn assert_not_match(script: &str) { let args = args_bash(script); - assert!(matches!( + assert_matches!( maybe_parse_apply_patch(&args), MaybeApplyPatch::NotApplyPatch - )); + ); } #[test] @@ -934,10 +936,10 @@ mod tests { let patch = "*** Begin Patch\n*** Add File: foo\n+hi\n*** End Patch".to_string(); let args = vec![patch]; let dir = tempdir().unwrap(); - assert!(matches!( + assert_matches!( maybe_parse_apply_patch_verified(&args, dir.path()), MaybeApplyPatchVerified::CorrectnessError(ApplyPatchError::ImplicitInvocation) - )); + ); } #[test] @@ -945,10 +947,10 @@ mod tests { let script = "*** Begin Patch\n*** Add File: foo\n+hi\n*** End Patch"; let args = args_bash(script); let dir = tempdir().unwrap(); - assert!(matches!( + assert_matches!( maybe_parse_apply_patch_verified(&args, dir.path()), MaybeApplyPatchVerified::CorrectnessError(ApplyPatchError::ImplicitInvocation) - )); + ); } #[test] @@ -1668,6 +1670,53 @@ g ); } + #[test] + fn test_apply_patch_resolves_move_path_with_effective_cwd() { + let session_dir = tempdir().unwrap(); + let worktree_rel = "alt"; + let worktree_dir = session_dir.path().join(worktree_rel); + fs::create_dir_all(&worktree_dir).unwrap(); + + let source_name = "old.txt"; + let dest_name = "renamed.txt"; + let source_path = worktree_dir.join(source_name); + fs::write(&source_path, "before\n").unwrap(); + + let patch = wrap_patch(&format!( + r#"*** Update File: {source_name} +*** Move to: {dest_name} +@@ +-before ++after"# + )); + + let shell_script = format!("cd {worktree_rel} && apply_patch <<'PATCH'\n{patch}\nPATCH"); + let argv = vec!["bash".into(), "-lc".into(), shell_script]; + + let result = maybe_parse_apply_patch_verified(&argv, session_dir.path()); + let action = match result { + MaybeApplyPatchVerified::Body(action) => action, + other => panic!("expected verified body, got {other:?}"), + }; + + assert_eq!(action.cwd, worktree_dir); + + let change = action + .changes() + .get(&worktree_dir.join(source_name)) + .expect("source file change present"); + + match change { + ApplyPatchFileChange::Update { move_path, .. } => { + assert_eq!( + move_path.as_deref(), + Some(worktree_dir.join(dest_name).as_path()) + ); + } + other => panic!("expected update change, got {other:?}"), + } + } + #[test] fn test_apply_patch_fails_on_write_error() { let dir = tempdir().unwrap(); diff --git a/codex-rs/apply-patch/src/seek_sequence.rs b/codex-rs/apply-patch/src/seek_sequence.rs index 0144580f9b..b005b08c75 100644 --- a/codex-rs/apply-patch/src/seek_sequence.rs +++ b/codex-rs/apply-patch/src/seek_sequence.rs @@ -112,9 +112,10 @@ pub(crate) fn seek_sequence( #[cfg(test)] mod tests { use super::seek_sequence; + use std::string::ToString; fn to_vec(strings: &[&str]) -> Vec { - strings.iter().map(|s| s.to_string()).collect() + strings.iter().map(ToString::to_string).collect() } #[test] diff --git a/codex-rs/apply-patch/tests/suite/mod.rs b/codex-rs/apply-patch/tests/suite/mod.rs index 26710c101c..882c5a6ffd 100644 --- a/codex-rs/apply-patch/tests/suite/mod.rs +++ b/codex-rs/apply-patch/tests/suite/mod.rs @@ -1 +1,3 @@ mod cli; +#[cfg(not(target_os = "windows"))] +mod tool; diff --git a/codex-rs/apply-patch/tests/suite/tool.rs b/codex-rs/apply-patch/tests/suite/tool.rs new file mode 100644 index 0000000000..c937719da0 --- /dev/null +++ b/codex-rs/apply-patch/tests/suite/tool.rs @@ -0,0 +1,257 @@ +use assert_cmd::Command; +use pretty_assertions::assert_eq; +use std::fs; +use std::path::Path; +use tempfile::tempdir; + +fn run_apply_patch_in_dir(dir: &Path, patch: &str) -> anyhow::Result { + let mut cmd = Command::cargo_bin("apply_patch")?; + cmd.current_dir(dir); + Ok(cmd.arg(patch).assert()) +} + +fn apply_patch_command(dir: &Path) -> anyhow::Result { + let mut cmd = Command::cargo_bin("apply_patch")?; + cmd.current_dir(dir); + Ok(cmd) +} + +#[test] +fn test_apply_patch_cli_applies_multiple_operations() -> anyhow::Result<()> { + let tmp = tempdir()?; + let modify_path = tmp.path().join("modify.txt"); + let delete_path = tmp.path().join("delete.txt"); + + fs::write(&modify_path, "line1\nline2\n")?; + fs::write(&delete_path, "obsolete\n")?; + + let patch = "*** Begin Patch\n*** Add File: nested/new.txt\n+created\n*** Delete File: delete.txt\n*** Update File: modify.txt\n@@\n-line2\n+changed\n*** End Patch"; + + run_apply_patch_in_dir(tmp.path(), patch)?.success().stdout( + "Success. Updated the following files:\nA nested/new.txt\nM modify.txt\nD delete.txt\n", + ); + + assert_eq!( + fs::read_to_string(tmp.path().join("nested/new.txt"))?, + "created\n" + ); + assert_eq!(fs::read_to_string(&modify_path)?, "line1\nchanged\n"); + assert!(!delete_path.exists()); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_applies_multiple_chunks() -> anyhow::Result<()> { + let tmp = tempdir()?; + let target_path = tmp.path().join("multi.txt"); + fs::write(&target_path, "line1\nline2\nline3\nline4\n")?; + + let patch = "*** Begin Patch\n*** Update File: multi.txt\n@@\n-line2\n+changed2\n@@\n-line4\n+changed4\n*** End Patch"; + + run_apply_patch_in_dir(tmp.path(), patch)? + .success() + .stdout("Success. Updated the following files:\nM multi.txt\n"); + + assert_eq!( + fs::read_to_string(&target_path)?, + "line1\nchanged2\nline3\nchanged4\n" + ); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_moves_file_to_new_directory() -> anyhow::Result<()> { + let tmp = tempdir()?; + let original_path = tmp.path().join("old/name.txt"); + let new_path = tmp.path().join("renamed/dir/name.txt"); + fs::create_dir_all(original_path.parent().expect("parent should exist"))?; + fs::write(&original_path, "old content\n")?; + + let patch = "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-old content\n+new content\n*** End Patch"; + + run_apply_patch_in_dir(tmp.path(), patch)? + .success() + .stdout("Success. Updated the following files:\nM renamed/dir/name.txt\n"); + + assert!(!original_path.exists()); + assert_eq!(fs::read_to_string(&new_path)?, "new content\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_rejects_empty_patch() -> anyhow::Result<()> { + let tmp = tempdir()?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** End Patch") + .assert() + .failure() + .stderr("No files were modified.\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_reports_missing_context() -> anyhow::Result<()> { + let tmp = tempdir()?; + let target_path = tmp.path().join("modify.txt"); + fs::write(&target_path, "line1\nline2\n")?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Update File: modify.txt\n@@\n-missing\n+changed\n*** End Patch") + .assert() + .failure() + .stderr("Failed to find expected lines in modify.txt:\nmissing\n"); + assert_eq!(fs::read_to_string(&target_path)?, "line1\nline2\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_rejects_missing_file_delete() -> anyhow::Result<()> { + let tmp = tempdir()?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Delete File: missing.txt\n*** End Patch") + .assert() + .failure() + .stderr("Failed to delete file missing.txt\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_rejects_empty_update_hunk() -> anyhow::Result<()> { + let tmp = tempdir()?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Update File: foo.txt\n*** End Patch") + .assert() + .failure() + .stderr("Invalid patch hunk on line 2: Update file hunk for path 'foo.txt' is empty\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_requires_existing_file_for_update() -> anyhow::Result<()> { + let tmp = tempdir()?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Update File: missing.txt\n@@\n-old\n+new\n*** End Patch") + .assert() + .failure() + .stderr( + "Failed to read file to update missing.txt: No such file or directory (os error 2)\n", + ); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_move_overwrites_existing_destination() -> anyhow::Result<()> { + let tmp = tempdir()?; + let original_path = tmp.path().join("old/name.txt"); + let destination = tmp.path().join("renamed/dir/name.txt"); + fs::create_dir_all(original_path.parent().expect("parent should exist"))?; + fs::create_dir_all(destination.parent().expect("parent should exist"))?; + fs::write(&original_path, "from\n")?; + fs::write(&destination, "existing\n")?; + + run_apply_patch_in_dir( + tmp.path(), + "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-from\n+new\n*** End Patch", + )? + .success() + .stdout("Success. Updated the following files:\nM renamed/dir/name.txt\n"); + + assert!(!original_path.exists()); + assert_eq!(fs::read_to_string(&destination)?, "new\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_add_overwrites_existing_file() -> anyhow::Result<()> { + let tmp = tempdir()?; + let path = tmp.path().join("duplicate.txt"); + fs::write(&path, "old content\n")?; + + run_apply_patch_in_dir( + tmp.path(), + "*** Begin Patch\n*** Add File: duplicate.txt\n+new content\n*** End Patch", + )? + .success() + .stdout("Success. Updated the following files:\nA duplicate.txt\n"); + + assert_eq!(fs::read_to_string(&path)?, "new content\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_delete_directory_fails() -> anyhow::Result<()> { + let tmp = tempdir()?; + fs::create_dir(tmp.path().join("dir"))?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Delete File: dir\n*** End Patch") + .assert() + .failure() + .stderr("Failed to delete file dir\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_rejects_invalid_hunk_header() -> anyhow::Result<()> { + let tmp = tempdir()?; + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch") + .assert() + .failure() + .stderr("Invalid patch hunk on line 2: '*** Frobnicate File: foo' is not a valid hunk header. Valid hunk headers: '*** Add File: {path}', '*** Delete File: {path}', '*** Update File: {path}'\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_updates_file_appends_trailing_newline() -> anyhow::Result<()> { + let tmp = tempdir()?; + let target_path = tmp.path().join("no_newline.txt"); + fs::write(&target_path, "no newline at end")?; + + run_apply_patch_in_dir( + tmp.path(), + "*** Begin Patch\n*** Update File: no_newline.txt\n@@\n-no newline at end\n+first line\n+second line\n*** End Patch", + )? + .success() + .stdout("Success. Updated the following files:\nM no_newline.txt\n"); + + let contents = fs::read_to_string(&target_path)?; + assert!(contents.ends_with('\n')); + assert_eq!(contents, "first line\nsecond line\n"); + + Ok(()) +} + +#[test] +fn test_apply_patch_cli_failure_after_partial_success_leaves_changes() -> anyhow::Result<()> { + let tmp = tempdir()?; + let new_file = tmp.path().join("created.txt"); + + apply_patch_command(tmp.path())? + .arg("*** Begin Patch\n*** Add File: created.txt\n+hello\n*** Update File: missing.txt\n@@\n-old\n+new\n*** End Patch") + .assert() + .failure() + .stdout("") + .stderr("Failed to read file to update missing.txt: No such file or directory (os error 2)\n"); + + assert_eq!(fs::read_to_string(&new_file)?, "hello\n"); + + Ok(()) +} diff --git a/codex-rs/arg0/Cargo.toml b/codex-rs/arg0/Cargo.toml index a01120b798..10d09e4a4b 100644 --- a/codex-rs/arg0/Cargo.toml +++ b/codex-rs/arg0/Cargo.toml @@ -11,10 +11,10 @@ path = "src/lib.rs" workspace = true [dependencies] -anyhow = "1" -codex-apply-patch = { path = "../apply-patch" } -codex-core = { path = "../core" } -codex-linux-sandbox = { path = "../linux-sandbox" } -dotenvy = "0.15.7" -tempfile = "3" -tokio = { version = "1", features = ["rt-multi-thread"] } +anyhow = { workspace = true } +codex-apply-patch = { workspace = true } +codex-core = { workspace = true } +codex-linux-sandbox = { workspace = true } +dotenvy = { workspace = true } +tempfile = { workspace = true } +tokio = { workspace = true, features = ["rt-multi-thread"] } diff --git a/codex-rs/arg0/src/lib.rs b/codex-rs/arg0/src/lib.rs index d5df68e554..6b60536413 100644 --- a/codex-rs/arg0/src/lib.rs +++ b/codex-rs/arg0/src/lib.rs @@ -11,6 +11,57 @@ const LINUX_SANDBOX_ARG0: &str = "codex-linux-sandbox"; const APPLY_PATCH_ARG0: &str = "apply_patch"; const MISSPELLED_APPLY_PATCH_ARG0: &str = "applypatch"; +pub fn arg0_dispatch() -> Option { + // Determine if we were invoked via the special alias. + let mut args = std::env::args_os(); + let argv0 = args.next().unwrap_or_default(); + let exe_name = Path::new(&argv0) + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or(""); + + if exe_name == LINUX_SANDBOX_ARG0 { + // Safety: [`run_main`] never returns. + codex_linux_sandbox::run_main(); + } else if exe_name == APPLY_PATCH_ARG0 || exe_name == MISSPELLED_APPLY_PATCH_ARG0 { + codex_apply_patch::main(); + } + + let argv1 = args.next().unwrap_or_default(); + if argv1 == CODEX_APPLY_PATCH_ARG1 { + let patch_arg = args.next().and_then(|s| s.to_str().map(str::to_owned)); + let exit_code = match patch_arg { + Some(patch_arg) => { + let mut stdout = std::io::stdout(); + let mut stderr = std::io::stderr(); + match codex_apply_patch::apply_patch(&patch_arg, &mut stdout, &mut stderr) { + Ok(()) => 0, + Err(_) => 1, + } + } + None => { + eprintln!("Error: {CODEX_APPLY_PATCH_ARG1} requires a UTF-8 PATCH argument."); + 1 + } + }; + std::process::exit(exit_code); + } + + // This modifies the environment, which is not thread-safe, so do this + // before creating any threads/the Tokio runtime. + load_dotenv(); + + match prepend_path_entry_for_codex_aliases() { + Ok(path_entry) => Some(path_entry), + Err(err) => { + // It is possible that Codex will proceed successfully even if + // updating the PATH fails, so warn the user and move on. + eprintln!("WARNING: proceeding, even though we could not update PATH: {err}"); + None + } + } +} + /// While we want to deploy the Codex CLI as a single executable for simplicity, /// we also want to expose some of its functionality as distinct CLIs, so we use /// the "arg0 trick" to determine which CLI to dispatch. This effectively allows @@ -37,57 +88,10 @@ where F: FnOnce(Option) -> Fut, Fut: Future>, { - // Determine if we were invoked via the special alias. - let mut args = std::env::args_os(); - let argv0 = args.next().unwrap_or_default(); - let exe_name = Path::new(&argv0) - .file_name() - .and_then(|s| s.to_str()) - .unwrap_or(""); - - if exe_name == LINUX_SANDBOX_ARG0 { - // Safety: [`run_main`] never returns. - codex_linux_sandbox::run_main(); - } else if exe_name == APPLY_PATCH_ARG0 || exe_name == MISSPELLED_APPLY_PATCH_ARG0 { - codex_apply_patch::main(); - } - - let argv1 = args.next().unwrap_or_default(); - if argv1 == CODEX_APPLY_PATCH_ARG1 { - let patch_arg = args.next().and_then(|s| s.to_str().map(|s| s.to_owned())); - let exit_code = match patch_arg { - Some(patch_arg) => { - let mut stdout = std::io::stdout(); - let mut stderr = std::io::stderr(); - match codex_apply_patch::apply_patch(&patch_arg, &mut stdout, &mut stderr) { - Ok(()) => 0, - Err(_) => 1, - } - } - None => { - eprintln!("Error: {CODEX_APPLY_PATCH_ARG1} requires a UTF-8 PATCH argument."); - 1 - } - }; - std::process::exit(exit_code); - } - - // This modifies the environment, which is not thread-safe, so do this - // before creating any threads/the Tokio runtime. - load_dotenv(); - // Retain the TempDir so it exists for the lifetime of the invocation of // this executable. Admittedly, we could invoke `keep()` on it, but it // would be nice to avoid leaving temporary directories behind, if possible. - let _path_entry = match prepend_path_entry_for_apply_patch() { - Ok(path_entry) => Some(path_entry), - Err(err) => { - // It is possible that Codex will proceed successfully even if - // updating the PATH fails, so warn the user and move on. - eprintln!("WARNING: proceeding, even though we could not update PATH: {err}"); - None - } - }; + let _path_entry = arg0_dispatch(); // Regular invocation – create a Tokio runtime and execute the provided // async entry-point. @@ -144,11 +148,16 @@ where /// /// IMPORTANT: This function modifies the PATH environment variable, so it MUST /// be called before multiple threads are spawned. -fn prepend_path_entry_for_apply_patch() -> std::io::Result { +pub fn prepend_path_entry_for_codex_aliases() -> std::io::Result { let temp_dir = TempDir::new()?; let path = temp_dir.path(); - for filename in &[APPLY_PATCH_ARG0, MISSPELLED_APPLY_PATCH_ARG0] { + for filename in &[ + APPLY_PATCH_ARG0, + MISSPELLED_APPLY_PATCH_ARG0, + #[cfg(target_os = "linux")] + LINUX_SANDBOX_ARG0, + ] { let exe = std::env::current_exe()?; #[cfg(unix)] diff --git a/codex-rs/async-utils/Cargo.toml b/codex-rs/async-utils/Cargo.toml new file mode 100644 index 0000000000..5203db0f5d --- /dev/null +++ b/codex-rs/async-utils/Cargo.toml @@ -0,0 +1,15 @@ +[package] +edition.workspace = true +name = "codex-async-utils" +version.workspace = true + +[lints] +workspace = true + +[dependencies] +async-trait.workspace = true +tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "time"] } +tokio-util.workspace = true + +[dev-dependencies] +pretty_assertions.workspace = true diff --git a/codex-rs/async-utils/src/lib.rs b/codex-rs/async-utils/src/lib.rs new file mode 100644 index 0000000000..bd880ae1fb --- /dev/null +++ b/codex-rs/async-utils/src/lib.rs @@ -0,0 +1,86 @@ +use async_trait::async_trait; +use std::future::Future; +use tokio_util::sync::CancellationToken; + +#[derive(Debug, PartialEq, Eq)] +pub enum CancelErr { + Cancelled, +} + +#[async_trait] +pub trait OrCancelExt: Sized { + type Output; + + async fn or_cancel(self, token: &CancellationToken) -> Result; +} + +#[async_trait] +impl OrCancelExt for F +where + F: Future + Send, + F::Output: Send, +{ + type Output = F::Output; + + async fn or_cancel(self, token: &CancellationToken) -> Result { + tokio::select! { + _ = token.cancelled() => Err(CancelErr::Cancelled), + res = self => Ok(res), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + use std::time::Duration; + use tokio::task; + use tokio::time::sleep; + + #[tokio::test] + async fn returns_ok_when_future_completes_first() { + let token = CancellationToken::new(); + let value = async { 42 }; + + let result = value.or_cancel(&token).await; + + assert_eq!(Ok(42), result); + } + + #[tokio::test] + async fn returns_err_when_token_cancelled_first() { + let token = CancellationToken::new(); + let token_clone = token.clone(); + + let cancel_handle = task::spawn(async move { + sleep(Duration::from_millis(10)).await; + token_clone.cancel(); + }); + + let result = async { + sleep(Duration::from_millis(100)).await; + 7 + } + .or_cancel(&token) + .await; + + cancel_handle.await.expect("cancel task panicked"); + assert_eq!(Err(CancelErr::Cancelled), result); + } + + #[tokio::test] + async fn returns_err_when_token_already_cancelled() { + let token = CancellationToken::new(); + token.cancel(); + + let result = async { + sleep(Duration::from_millis(50)).await; + 5 + } + .or_cancel(&token) + .await; + + assert_eq!(Err(CancelErr::Cancelled), result); + } +} diff --git a/codex-rs/backend-client/Cargo.toml b/codex-rs/backend-client/Cargo.toml new file mode 100644 index 0000000000..0cf802399c --- /dev/null +++ b/codex-rs/backend-client/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "codex-backend-client" +version = "0.0.0" +edition = "2024" +publish = false + +[lib] +path = "src/lib.rs" + +[dependencies] +anyhow = "1" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] } +codex-backend-openapi-models = { path = "../codex-backend-openapi-models" } +codex-protocol = { workspace = true } +codex-core = { workspace = true } + +[dev-dependencies] +pretty_assertions = "1" diff --git a/codex-rs/backend-client/src/client.rs b/codex-rs/backend-client/src/client.rs new file mode 100644 index 0000000000..28a51598eb --- /dev/null +++ b/codex-rs/backend-client/src/client.rs @@ -0,0 +1,317 @@ +use crate::types::CodeTaskDetailsResponse; +use crate::types::PaginatedListTaskListItem; +use crate::types::RateLimitStatusPayload; +use crate::types::RateLimitWindowSnapshot; +use crate::types::TurnAttemptsSiblingTurnsResponse; +use anyhow::Result; +use codex_core::auth::CodexAuth; +use codex_core::default_client::get_codex_user_agent; +use codex_protocol::protocol::RateLimitSnapshot; +use codex_protocol::protocol::RateLimitWindow; +use reqwest::header::AUTHORIZATION; +use reqwest::header::CONTENT_TYPE; +use reqwest::header::HeaderMap; +use reqwest::header::HeaderName; +use reqwest::header::HeaderValue; +use reqwest::header::USER_AGENT; +use serde::de::DeserializeOwned; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PathStyle { + /// /api/codex/… + CodexApi, + /// /wham/… + ChatGptApi, +} + +impl PathStyle { + pub fn from_base_url(base_url: &str) -> Self { + if base_url.contains("/backend-api") { + PathStyle::ChatGptApi + } else { + PathStyle::CodexApi + } + } +} + +#[derive(Clone, Debug)] +pub struct Client { + base_url: String, + http: reqwest::Client, + bearer_token: Option, + user_agent: Option, + chatgpt_account_id: Option, + path_style: PathStyle, +} + +impl Client { + pub fn new(base_url: impl Into) -> Result { + let mut base_url = base_url.into(); + // Normalize common ChatGPT hostnames to include /backend-api so we hit the WHAM paths. + // Also trim trailing slashes for consistent URL building. + while base_url.ends_with('/') { + base_url.pop(); + } + if (base_url.starts_with("https://chatgpt.com") + || base_url.starts_with("https://chat.openai.com")) + && !base_url.contains("/backend-api") + { + base_url = format!("{base_url}/backend-api"); + } + let http = reqwest::Client::builder().build()?; + let path_style = PathStyle::from_base_url(&base_url); + Ok(Self { + base_url, + http, + bearer_token: None, + user_agent: None, + chatgpt_account_id: None, + path_style, + }) + } + + pub async fn from_auth(base_url: impl Into, auth: &CodexAuth) -> Result { + let token = auth.get_token().await.map_err(anyhow::Error::from)?; + let mut client = Self::new(base_url)? + .with_user_agent(get_codex_user_agent()) + .with_bearer_token(token); + if let Some(account_id) = auth.get_account_id() { + client = client.with_chatgpt_account_id(account_id); + } + Ok(client) + } + + pub fn with_bearer_token(mut self, token: impl Into) -> Self { + self.bearer_token = Some(token.into()); + self + } + + pub fn with_user_agent(mut self, ua: impl Into) -> Self { + if let Ok(hv) = HeaderValue::from_str(&ua.into()) { + self.user_agent = Some(hv); + } + self + } + + pub fn with_chatgpt_account_id(mut self, account_id: impl Into) -> Self { + self.chatgpt_account_id = Some(account_id.into()); + self + } + + pub fn with_path_style(mut self, style: PathStyle) -> Self { + self.path_style = style; + self + } + + fn headers(&self) -> HeaderMap { + let mut h = HeaderMap::new(); + if let Some(ua) = &self.user_agent { + h.insert(USER_AGENT, ua.clone()); + } else { + h.insert(USER_AGENT, HeaderValue::from_static("codex-cli")); + } + if let Some(token) = &self.bearer_token { + let value = format!("Bearer {token}"); + if let Ok(hv) = HeaderValue::from_str(&value) { + h.insert(AUTHORIZATION, hv); + } + } + if let Some(acc) = &self.chatgpt_account_id + && let Ok(name) = HeaderName::from_bytes(b"ChatGPT-Account-Id") + && let Ok(hv) = HeaderValue::from_str(acc) + { + h.insert(name, hv); + } + h + } + + async fn exec_request( + &self, + req: reqwest::RequestBuilder, + method: &str, + url: &str, + ) -> Result<(String, String)> { + let res = req.send().await?; + let status = res.status(); + let ct = res + .headers() + .get(CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .to_string(); + let body = res.text().await.unwrap_or_default(); + if !status.is_success() { + anyhow::bail!("{method} {url} failed: {status}; content-type={ct}; body={body}"); + } + Ok((body, ct)) + } + + fn decode_json(&self, url: &str, ct: &str, body: &str) -> Result { + match serde_json::from_str::(body) { + Ok(v) => Ok(v), + Err(e) => { + anyhow::bail!("Decode error for {url}: {e}; content-type={ct}; body={body}"); + } + } + } + + pub async fn get_rate_limits(&self) -> Result { + let url = match self.path_style { + PathStyle::CodexApi => format!("{}/api/codex/usage", self.base_url), + PathStyle::ChatGptApi => format!("{}/wham/usage", self.base_url), + }; + let req = self.http.get(&url).headers(self.headers()); + let (body, ct) = self.exec_request(req, "GET", &url).await?; + let payload: RateLimitStatusPayload = self.decode_json(&url, &ct, &body)?; + Ok(Self::rate_limit_snapshot_from_payload(payload)) + } + + pub async fn list_tasks( + &self, + limit: Option, + task_filter: Option<&str>, + environment_id: Option<&str>, + ) -> Result { + let url = match self.path_style { + PathStyle::CodexApi => format!("{}/api/codex/tasks/list", self.base_url), + PathStyle::ChatGptApi => format!("{}/wham/tasks/list", self.base_url), + }; + let req = self.http.get(&url).headers(self.headers()); + let req = if let Some(lim) = limit { + req.query(&[("limit", lim)]) + } else { + req + }; + let req = if let Some(tf) = task_filter { + req.query(&[("task_filter", tf)]) + } else { + req + }; + let req = if let Some(id) = environment_id { + req.query(&[("environment_id", id)]) + } else { + req + }; + let (body, ct) = self.exec_request(req, "GET", &url).await?; + self.decode_json::(&url, &ct, &body) + } + + pub async fn get_task_details(&self, task_id: &str) -> Result { + let (parsed, _body, _ct) = self.get_task_details_with_body(task_id).await?; + Ok(parsed) + } + + pub async fn get_task_details_with_body( + &self, + task_id: &str, + ) -> Result<(CodeTaskDetailsResponse, String, String)> { + let url = match self.path_style { + PathStyle::CodexApi => format!("{}/api/codex/tasks/{}", self.base_url, task_id), + PathStyle::ChatGptApi => format!("{}/wham/tasks/{}", self.base_url, task_id), + }; + let req = self.http.get(&url).headers(self.headers()); + let (body, ct) = self.exec_request(req, "GET", &url).await?; + let parsed: CodeTaskDetailsResponse = self.decode_json(&url, &ct, &body)?; + Ok((parsed, body, ct)) + } + + pub async fn list_sibling_turns( + &self, + task_id: &str, + turn_id: &str, + ) -> Result { + let url = match self.path_style { + PathStyle::CodexApi => format!( + "{}/api/codex/tasks/{}/turns/{}/sibling_turns", + self.base_url, task_id, turn_id + ), + PathStyle::ChatGptApi => format!( + "{}/wham/tasks/{}/turns/{}/sibling_turns", + self.base_url, task_id, turn_id + ), + }; + let req = self.http.get(&url).headers(self.headers()); + let (body, ct) = self.exec_request(req, "GET", &url).await?; + self.decode_json::(&url, &ct, &body) + } + + /// Create a new task (user turn) by POSTing to the appropriate backend path + /// based on `path_style`. Returns the created task id. + pub async fn create_task(&self, request_body: serde_json::Value) -> Result { + let url = match self.path_style { + PathStyle::CodexApi => format!("{}/api/codex/tasks", self.base_url), + PathStyle::ChatGptApi => format!("{}/wham/tasks", self.base_url), + }; + let req = self + .http + .post(&url) + .headers(self.headers()) + .header(CONTENT_TYPE, HeaderValue::from_static("application/json")) + .json(&request_body); + let (body, ct) = self.exec_request(req, "POST", &url).await?; + // Extract id from JSON: prefer `task.id`; fallback to top-level `id` when present. + match serde_json::from_str::(&body) { + Ok(v) => { + if let Some(id) = v + .get("task") + .and_then(|t| t.get("id")) + .and_then(|s| s.as_str()) + { + Ok(id.to_string()) + } else if let Some(id) = v.get("id").and_then(|s| s.as_str()) { + Ok(id.to_string()) + } else { + anyhow::bail!( + "POST {url} succeeded but no task id found; content-type={ct}; body={body}" + ); + } + } + Err(e) => anyhow::bail!("Decode error for {url}: {e}; content-type={ct}; body={body}"), + } + } + + // rate limit helpers + fn rate_limit_snapshot_from_payload(payload: RateLimitStatusPayload) -> RateLimitSnapshot { + let Some(details) = payload + .rate_limit + .and_then(|inner| inner.map(|boxed| *boxed)) + else { + return RateLimitSnapshot { + primary: None, + secondary: None, + }; + }; + + RateLimitSnapshot { + primary: Self::map_rate_limit_window(details.primary_window), + secondary: Self::map_rate_limit_window(details.secondary_window), + } + } + + fn map_rate_limit_window( + window: Option>>, + ) -> Option { + let snapshot = match window { + Some(Some(snapshot)) => *snapshot, + _ => return None, + }; + + let used_percent = f64::from(snapshot.used_percent); + let window_minutes = Self::window_minutes_from_seconds(snapshot.limit_window_seconds); + let resets_at = Some(i64::from(snapshot.reset_at)); + Some(RateLimitWindow { + used_percent, + window_minutes, + resets_at, + }) + } + + fn window_minutes_from_seconds(seconds: i32) -> Option { + if seconds <= 0 { + return None; + } + + let seconds_i64 = i64::from(seconds); + Some((seconds_i64 + 59) / 60) + } +} diff --git a/codex-rs/backend-client/src/lib.rs b/codex-rs/backend-client/src/lib.rs new file mode 100644 index 0000000000..29fe9f3c6b --- /dev/null +++ b/codex-rs/backend-client/src/lib.rs @@ -0,0 +1,9 @@ +mod client; +pub mod types; + +pub use client::Client; +pub use types::CodeTaskDetailsResponse; +pub use types::CodeTaskDetailsResponseExt; +pub use types::PaginatedListTaskListItem; +pub use types::TaskListItem; +pub use types::TurnAttemptsSiblingTurnsResponse; diff --git a/codex-rs/backend-client/src/types.rs b/codex-rs/backend-client/src/types.rs new file mode 100644 index 0000000000..9f196f9c2f --- /dev/null +++ b/codex-rs/backend-client/src/types.rs @@ -0,0 +1,373 @@ +pub use codex_backend_openapi_models::models::PaginatedListTaskListItem; +pub use codex_backend_openapi_models::models::PlanType; +pub use codex_backend_openapi_models::models::RateLimitStatusDetails; +pub use codex_backend_openapi_models::models::RateLimitStatusPayload; +pub use codex_backend_openapi_models::models::RateLimitWindowSnapshot; +pub use codex_backend_openapi_models::models::TaskListItem; + +use serde::Deserialize; +use serde::de::Deserializer; +use serde_json::Value; +use std::collections::HashMap; + +/// Hand-rolled models for the Cloud Tasks task-details response. +/// The generated OpenAPI models are pretty bad. This is a half-step +/// towards hand-rolling them. +#[derive(Clone, Debug, Deserialize)] +pub struct CodeTaskDetailsResponse { + #[serde(default)] + pub current_user_turn: Option, + #[serde(default)] + pub current_assistant_turn: Option, + #[serde(default)] + pub current_diff_task_turn: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct Turn { + #[serde(default)] + pub id: Option, + #[serde(default)] + pub attempt_placement: Option, + #[serde(default, rename = "turn_status")] + pub turn_status: Option, + #[serde(default, deserialize_with = "deserialize_vec")] + pub sibling_turn_ids: Vec, + #[serde(default, deserialize_with = "deserialize_vec")] + pub input_items: Vec, + #[serde(default, deserialize_with = "deserialize_vec")] + pub output_items: Vec, + #[serde(default)] + pub worklog: Option, + #[serde(default)] + pub error: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct TurnItem { + #[serde(rename = "type", default)] + pub kind: String, + #[serde(default)] + pub role: Option, + #[serde(default, deserialize_with = "deserialize_vec")] + pub content: Vec, + #[serde(default)] + pub diff: Option, + #[serde(default)] + pub output_diff: Option, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(untagged)] +pub enum ContentFragment { + Structured(StructuredContent), + Text(String), +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct StructuredContent { + #[serde(rename = "content_type", default)] + pub content_type: Option, + #[serde(default)] + pub text: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct DiffPayload { + #[serde(default)] + pub diff: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct Worklog { + #[serde(default, deserialize_with = "deserialize_vec")] + pub messages: Vec, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct WorklogMessage { + #[serde(default)] + pub author: Option, + #[serde(default)] + pub content: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct Author { + #[serde(default)] + pub role: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct WorklogContent { + #[serde(default)] + pub parts: Vec, +} + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct TurnError { + #[serde(default)] + pub code: Option, + #[serde(default)] + pub message: Option, +} + +impl ContentFragment { + fn text(&self) -> Option<&str> { + match self { + ContentFragment::Structured(inner) => { + if inner + .content_type + .as_deref() + .map(|ct| ct.eq_ignore_ascii_case("text")) + .unwrap_or(false) + { + inner.text.as_deref().filter(|s| !s.is_empty()) + } else { + None + } + } + ContentFragment::Text(raw) => { + if raw.trim().is_empty() { + None + } else { + Some(raw.as_str()) + } + } + } + } +} + +impl TurnItem { + fn text_values(&self) -> Vec { + self.content + .iter() + .filter_map(|fragment| fragment.text().map(str::to_string)) + .collect() + } + + fn diff_text(&self) -> Option { + if self.kind == "output_diff" { + if let Some(diff) = &self.diff + && !diff.is_empty() + { + return Some(diff.clone()); + } + } else if self.kind == "pr" + && let Some(payload) = &self.output_diff + && let Some(diff) = &payload.diff + && !diff.is_empty() + { + return Some(diff.clone()); + } + None + } +} + +impl Turn { + fn unified_diff(&self) -> Option { + self.output_items.iter().find_map(TurnItem::diff_text) + } + + fn message_texts(&self) -> Vec { + let mut out: Vec = self + .output_items + .iter() + .filter(|item| item.kind == "message") + .flat_map(TurnItem::text_values) + .collect(); + + if let Some(log) = &self.worklog { + for message in &log.messages { + if message.is_assistant() { + out.extend(message.text_values()); + } + } + } + + out + } + + fn user_prompt(&self) -> Option { + let parts: Vec = self + .input_items + .iter() + .filter(|item| item.kind == "message") + .filter(|item| { + item.role + .as_deref() + .map(|r| r.eq_ignore_ascii_case("user")) + .unwrap_or(true) + }) + .flat_map(TurnItem::text_values) + .collect(); + + if parts.is_empty() { + None + } else { + Some(parts.join( + " + +", + )) + } + } + + fn error_summary(&self) -> Option { + self.error.as_ref().and_then(TurnError::summary) + } +} + +impl WorklogMessage { + fn is_assistant(&self) -> bool { + self.author + .as_ref() + .and_then(|a| a.role.as_deref()) + .map(|role| role.eq_ignore_ascii_case("assistant")) + .unwrap_or(false) + } + + fn text_values(&self) -> Vec { + self.content + .as_ref() + .map(|content| { + content + .parts + .iter() + .filter_map(|fragment| fragment.text().map(str::to_string)) + .collect() + }) + .unwrap_or_default() + } +} + +impl TurnError { + fn summary(&self) -> Option { + let code = self.code.as_deref().unwrap_or(""); + let message = self.message.as_deref().unwrap_or(""); + match (code.is_empty(), message.is_empty()) { + (true, true) => None, + (false, true) => Some(code.to_string()), + (true, false) => Some(message.to_string()), + (false, false) => Some(format!("{code}: {message}")), + } + } +} + +pub trait CodeTaskDetailsResponseExt { + /// Attempt to extract a unified diff string from the assistant or diff turn. + fn unified_diff(&self) -> Option; + /// Extract assistant text output messages (no diff) from current turns. + fn assistant_text_messages(&self) -> Vec; + /// Extract the user's prompt text from the current user turn, when present. + fn user_text_prompt(&self) -> Option; + /// Extract an assistant error message (if the turn failed and provided one). + fn assistant_error_message(&self) -> Option; +} + +impl CodeTaskDetailsResponseExt for CodeTaskDetailsResponse { + fn unified_diff(&self) -> Option { + [ + self.current_diff_task_turn.as_ref(), + self.current_assistant_turn.as_ref(), + ] + .into_iter() + .flatten() + .find_map(Turn::unified_diff) + } + + fn assistant_text_messages(&self) -> Vec { + let mut out = Vec::new(); + for turn in [ + self.current_diff_task_turn.as_ref(), + self.current_assistant_turn.as_ref(), + ] + .into_iter() + .flatten() + { + out.extend(turn.message_texts()); + } + out + } + + fn user_text_prompt(&self) -> Option { + self.current_user_turn.as_ref().and_then(Turn::user_prompt) + } + + fn assistant_error_message(&self) -> Option { + self.current_assistant_turn + .as_ref() + .and_then(Turn::error_summary) + } +} + +fn deserialize_vec<'de, D, T>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, + T: Deserialize<'de>, +{ + Option::>::deserialize(deserializer).map(|opt| opt.unwrap_or_default()) +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TurnAttemptsSiblingTurnsResponse { + #[serde(default)] + pub sibling_turns: Vec>, +} + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + fn fixture(name: &str) -> CodeTaskDetailsResponse { + let json = match name { + "diff" => include_str!("../tests/fixtures/task_details_with_diff.json"), + "error" => include_str!("../tests/fixtures/task_details_with_error.json"), + other => panic!("unknown fixture {other}"), + }; + serde_json::from_str(json).expect("fixture should deserialize") + } + + #[test] + fn unified_diff_prefers_current_diff_task_turn() { + let details = fixture("diff"); + let diff = details.unified_diff().expect("diff present"); + assert!(diff.contains("diff --git")); + } + + #[test] + fn unified_diff_falls_back_to_pr_output_diff() { + let details = fixture("error"); + let diff = details.unified_diff().expect("diff from pr output"); + assert!(diff.contains("lib.rs")); + } + + #[test] + fn assistant_text_messages_extracts_text_content() { + let details = fixture("diff"); + let messages = details.assistant_text_messages(); + assert_eq!(messages, vec!["Assistant response".to_string()]); + } + + #[test] + fn user_text_prompt_joins_parts_with_spacing() { + let details = fixture("diff"); + let prompt = details.user_text_prompt().expect("prompt present"); + assert_eq!( + prompt, + "First line + +Second line" + ); + } + + #[test] + fn assistant_error_message_combines_code_and_message() { + let details = fixture("error"); + let msg = details + .assistant_error_message() + .expect("error should be present"); + assert_eq!(msg, "APPLY_FAILED: Patch could not be applied"); + } +} diff --git a/codex-rs/backend-client/tests/fixtures/task_details_with_diff.json b/codex-rs/backend-client/tests/fixtures/task_details_with_diff.json new file mode 100644 index 0000000000..3a06b04c1c --- /dev/null +++ b/codex-rs/backend-client/tests/fixtures/task_details_with_diff.json @@ -0,0 +1,38 @@ +{ + "task": { + "id": "task_123", + "title": "Refactor cloud task client", + "archived": false, + "external_pull_requests": [] + }, + "current_user_turn": { + "input_items": [ + { + "type": "message", + "role": "user", + "content": [ + { "content_type": "text", "text": "First line" }, + { "content_type": "text", "text": "Second line" } + ] + } + ] + }, + "current_assistant_turn": { + "output_items": [ + { + "type": "message", + "content": [ + { "content_type": "text", "text": "Assistant response" } + ] + } + ] + }, + "current_diff_task_turn": { + "output_items": [ + { + "type": "output_diff", + "diff": "diff --git a/src/main.rs b/src/main.rs\n+fn main() { println!(\"hi\"); }\n" + } + ] + } +} diff --git a/codex-rs/backend-client/tests/fixtures/task_details_with_error.json b/codex-rs/backend-client/tests/fixtures/task_details_with_error.json new file mode 100644 index 0000000000..6f6b66a72c --- /dev/null +++ b/codex-rs/backend-client/tests/fixtures/task_details_with_error.json @@ -0,0 +1,22 @@ +{ + "task": { + "id": "task_456", + "title": "Investigate failure", + "archived": false, + "external_pull_requests": [] + }, + "current_assistant_turn": { + "output_items": [ + { + "type": "pr", + "output_diff": { + "diff": "diff --git a/lib.rs b/lib.rs\n+pub fn hello() {}\n" + } + } + ], + "error": { + "code": "APPLY_FAILED", + "message": "Patch could not be applied" + } + } +} diff --git a/codex-rs/chatgpt/Cargo.toml b/codex-rs/chatgpt/Cargo.toml index af5f910efe..c46046b1f6 100644 --- a/codex-rs/chatgpt/Cargo.toml +++ b/codex-rs/chatgpt/Cargo.toml @@ -7,13 +7,14 @@ version = { workspace = true } workspace = true [dependencies] -anyhow = "1" -clap = { version = "4", features = ["derive"] } -codex-common = { path = "../common", features = ["cli"] } -codex-core = { path = "../core" } -serde = { version = "1", features = ["derive"] } -serde_json = "1" -tokio = { version = "1", features = ["full"] } +anyhow = { workspace = true } +clap = { workspace = true, features = ["derive"] } +codex-common = { workspace = true, features = ["cli"] } +codex-core = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tokio = { workspace = true, features = ["full"] } +codex-git = { workspace = true } [dev-dependencies] -tempfile = "3" +tempfile = { workspace = true } diff --git a/codex-rs/chatgpt/src/apply_command.rs b/codex-rs/chatgpt/src/apply_command.rs index 52ab205a0c..ffd460e293 100644 --- a/codex-rs/chatgpt/src/apply_command.rs +++ b/codex-rs/chatgpt/src/apply_command.rs @@ -29,9 +29,11 @@ pub async fn run_apply_command( .parse_overrides() .map_err(anyhow::Error::msg)?, ConfigOverrides::default(), - )?; + ) + .await?; - init_chatgpt_token_from_auth(&config.codex_home).await?; + init_chatgpt_token_from_auth(&config.codex_home, config.cli_auth_credentials_store_mode) + .await?; let task_response = get_task(&config, apply_cli.task_id).await?; apply_diff_from_task(task_response, cwd).await @@ -56,46 +58,24 @@ pub async fn apply_diff_from_task( } async fn apply_diff(diff: &str, cwd: Option) -> anyhow::Result<()> { - let mut cmd = tokio::process::Command::new("git"); - if let Some(cwd) = cwd { - cmd.current_dir(cwd); - } - let toplevel_output = cmd - .args(vec!["rev-parse", "--show-toplevel"]) - .output() - .await?; - - if !toplevel_output.status.success() { - anyhow::bail!("apply must be run from a git repository."); - } - - let repo_root = String::from_utf8(toplevel_output.stdout)? - .trim() - .to_string(); - - let mut git_apply_cmd = tokio::process::Command::new("git") - .args(vec!["apply", "--3way"]) - .current_dir(&repo_root) - .stdin(std::process::Stdio::piped()) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()) - .spawn()?; - - if let Some(mut stdin) = git_apply_cmd.stdin.take() { - tokio::io::AsyncWriteExt::write_all(&mut stdin, diff.as_bytes()).await?; - drop(stdin); - } - - let output = git_apply_cmd.wait_with_output().await?; - - if !output.status.success() { + let cwd = cwd.unwrap_or(std::env::current_dir().unwrap_or_else(|_| std::env::temp_dir())); + let req = codex_git::ApplyGitRequest { + cwd, + diff: diff.to_string(), + revert: false, + preflight: false, + }; + let res = codex_git::apply_git_patch(&req)?; + if res.exit_code != 0 { anyhow::bail!( - "Git apply failed with status {}: {}", - output.status, - String::from_utf8_lossy(&output.stderr) + "Git apply failed (applied={}, skipped={}, conflicts={})\nstdout:\n{}\nstderr:\n{}", + res.applied_paths.len(), + res.skipped_paths.len(), + res.conflicted_paths.len(), + res.stdout, + res.stderr ); } - println!("Successfully applied diff"); Ok(()) } diff --git a/codex-rs/chatgpt/src/chatgpt_client.rs b/codex-rs/chatgpt/src/chatgpt_client.rs index a4545f075e..7528631981 100644 --- a/codex-rs/chatgpt/src/chatgpt_client.rs +++ b/codex-rs/chatgpt/src/chatgpt_client.rs @@ -13,7 +13,8 @@ pub(crate) async fn chatgpt_get_request( path: String, ) -> anyhow::Result { let chatgpt_base_url = &config.chatgpt_base_url; - init_chatgpt_token_from_auth(&config.codex_home).await?; + init_chatgpt_token_from_auth(&config.codex_home, config.cli_auth_credentials_store_mode) + .await?; // Make direct HTTP request to ChatGPT backend API with the token let client = create_client(); @@ -44,6 +45,6 @@ pub(crate) async fn chatgpt_get_request( } else { let status = response.status(); let body = response.text().await.unwrap_or_default(); - anyhow::bail!("Request failed with status {}: {}", status, body) + anyhow::bail!("Request failed with status {status}: {body}") } } diff --git a/codex-rs/chatgpt/src/chatgpt_token.rs b/codex-rs/chatgpt/src/chatgpt_token.rs index ce9b7475c9..e8879ad21c 100644 --- a/codex-rs/chatgpt/src/chatgpt_token.rs +++ b/codex-rs/chatgpt/src/chatgpt_token.rs @@ -3,6 +3,7 @@ use std::path::Path; use std::sync::LazyLock; use std::sync::RwLock; +use codex_core::auth::AuthCredentialsStoreMode; use codex_core::token_data::TokenData; static CHATGPT_TOKEN: LazyLock>> = LazyLock::new(|| RwLock::new(None)); @@ -18,8 +19,11 @@ pub fn set_chatgpt_token_data(value: TokenData) { } /// Initialize the ChatGPT token from auth.json file -pub async fn init_chatgpt_token_from_auth(codex_home: &Path) -> std::io::Result<()> { - let auth = CodexAuth::from_codex_home(codex_home)?; +pub async fn init_chatgpt_token_from_auth( + codex_home: &Path, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result<()> { + let auth = CodexAuth::from_auth_storage(codex_home, auth_credentials_store_mode)?; if let Some(auth) = auth { let token_data = auth.get_token_data().await?; set_chatgpt_token_data(token_data); diff --git a/codex-rs/cli/Cargo.toml b/codex-rs/cli/Cargo.toml index 291ef47748..deddc068cd 100644 --- a/codex-rs/cli/Cargo.toml +++ b/codex-rs/cli/Cargo.toml @@ -15,32 +15,47 @@ path = "src/lib.rs" workspace = true [dependencies] -anyhow = "1" -clap = { version = "4", features = ["derive"] } -clap_complete = "4" -codex-arg0 = { path = "../arg0" } -codex-chatgpt = { path = "../chatgpt" } -codex-common = { path = "../common", features = ["cli"] } -codex-core = { path = "../core" } -codex-exec = { path = "../exec" } -codex-login = { path = "../login" } -codex-mcp-server = { path = "../mcp-server" } -codex-protocol = { path = "../protocol" } -codex-tui = { path = "../tui" } -serde_json = "1" -tokio = { version = "1", features = [ +anyhow = { workspace = true } +clap = { workspace = true, features = ["derive"] } +clap_complete = { workspace = true } +codex-app-server = { workspace = true } +codex-app-server-protocol = { workspace = true } +codex-arg0 = { workspace = true } +codex-chatgpt = { workspace = true } +codex-cloud-tasks = { path = "../cloud-tasks" } +codex-common = { workspace = true, features = ["cli"] } +codex-core = { workspace = true } +codex-exec = { workspace = true } +codex-login = { workspace = true } +codex-mcp-server = { workspace = true } +codex-process-hardening = { workspace = true } +codex-protocol = { workspace = true } +codex-responses-api-proxy = { workspace = true } +codex-rmcp-client = { workspace = true } +codex-stdio-to-uds = { workspace = true } +codex-tui = { workspace = true } +ctor = { workspace = true } +libc = { workspace = true } +owo-colors = { workspace = true } +regex-lite = { workspace = true} +serde_json = { workspace = true } +supports-color = { workspace = true } +toml = { workspace = true } +tokio = { workspace = true, features = [ "io-std", "macros", "process", "rt-multi-thread", "signal", ] } -tracing = "0.1.41" -tracing-subscriber = "0.3.20" -codex-protocol-ts = { path = "../protocol-ts" } +tracing = { workspace = true } + +[target.'cfg(target_os = "windows")'.dependencies] +codex_windows_sandbox = { package = "codex-windows-sandbox", path = "../windows-sandbox-rs" } [dev-dependencies] -assert_cmd = "2" -predicates = "3" -pretty_assertions = "1" -tempfile = "3" +assert_cmd = { workspace = true } +assert_matches = { workspace = true } +predicates = { workspace = true } +pretty_assertions = { workspace = true } +tempfile = { workspace = true } diff --git a/codex-rs/cli/src/debug_sandbox.rs b/codex-rs/cli/src/debug_sandbox.rs index a7d7103c2f..df4c2e97c9 100644 --- a/codex-rs/cli/src/debug_sandbox.rs +++ b/codex-rs/cli/src/debug_sandbox.rs @@ -1,3 +1,8 @@ +#[cfg(target_os = "macos")] +mod pid_tracker; +#[cfg(target_os = "macos")] +mod seatbelt; + use std::path::PathBuf; use codex_common::CliConfigOverrides; @@ -5,20 +10,27 @@ use codex_core::config::Config; use codex_core::config::ConfigOverrides; use codex_core::exec_env::create_env; use codex_core::landlock::spawn_command_under_linux_sandbox; +#[cfg(target_os = "macos")] use codex_core::seatbelt::spawn_command_under_seatbelt; use codex_core::spawn::StdioPolicy; use codex_protocol::config_types::SandboxMode; use crate::LandlockCommand; use crate::SeatbeltCommand; +use crate::WindowsCommand; use crate::exit_status::handle_exit_status; +#[cfg(target_os = "macos")] +use seatbelt::DenialLogger; + +#[cfg(target_os = "macos")] pub async fn run_command_under_seatbelt( command: SeatbeltCommand, codex_linux_sandbox_exe: Option, ) -> anyhow::Result<()> { let SeatbeltCommand { full_auto, + log_denials, config_overrides, command, } = command; @@ -28,10 +40,19 @@ pub async fn run_command_under_seatbelt( config_overrides, codex_linux_sandbox_exe, SandboxType::Seatbelt, + log_denials, ) .await } +#[cfg(not(target_os = "macos"))] +pub async fn run_command_under_seatbelt( + _command: SeatbeltCommand, + _codex_linux_sandbox_exe: Option, +) -> anyhow::Result<()> { + anyhow::bail!("Seatbelt sandbox is only available on macOS"); +} + pub async fn run_command_under_landlock( command: LandlockCommand, codex_linux_sandbox_exe: Option, @@ -47,13 +68,36 @@ pub async fn run_command_under_landlock( config_overrides, codex_linux_sandbox_exe, SandboxType::Landlock, + false, + ) + .await +} + +pub async fn run_command_under_windows( + command: WindowsCommand, + codex_linux_sandbox_exe: Option, +) -> anyhow::Result<()> { + let WindowsCommand { + full_auto, + config_overrides, + command, + } = command; + run_command_under_sandbox( + full_auto, + command, + config_overrides, + codex_linux_sandbox_exe, + SandboxType::Windows, + false, ) .await } enum SandboxType { + #[cfg(target_os = "macos")] Seatbelt, Landlock, + Windows, } async fn run_command_under_sandbox( @@ -62,6 +106,7 @@ async fn run_command_under_sandbox( config_overrides: CliConfigOverrides, codex_linux_sandbox_exe: Option, sandbox_type: SandboxType, + log_denials: bool, ) -> anyhow::Result<()> { let sandbox_mode = create_sandbox_mode(full_auto); let config = Config::load_with_cli_overrides( @@ -73,7 +118,8 @@ async fn run_command_under_sandbox( codex_linux_sandbox_exe, ..Default::default() }, - )?; + ) + .await?; // In practice, this should be `std::env::current_dir()` because this CLI // does not support `--cwd`, but let's use the config value for consistency. @@ -86,7 +132,74 @@ async fn run_command_under_sandbox( let stdio_policy = StdioPolicy::Inherit; let env = create_env(&config.shell_environment_policy); + // Special-case Windows sandbox: execute and exit the process to emulate inherited stdio. + if let SandboxType::Windows = sandbox_type { + #[cfg(target_os = "windows")] + { + use codex_windows_sandbox::run_windows_sandbox_capture; + + let policy_str = match &config.sandbox_policy { + codex_core::protocol::SandboxPolicy::DangerFullAccess => "workspace-write", + codex_core::protocol::SandboxPolicy::ReadOnly => "read-only", + codex_core::protocol::SandboxPolicy::WorkspaceWrite { .. } => "workspace-write", + }; + + let sandbox_cwd = sandbox_policy_cwd.clone(); + let cwd_clone = cwd.clone(); + let env_map = env.clone(); + let command_vec = command.clone(); + let base_dir = config.codex_home.clone(); + + // Preflight audit is invoked elsewhere at the appropriate times. + let res = tokio::task::spawn_blocking(move || { + run_windows_sandbox_capture( + policy_str, + &sandbox_cwd, + base_dir.as_path(), + command_vec, + &cwd_clone, + env_map, + None, + ) + }) + .await; + + let capture = match res { + Ok(Ok(v)) => v, + Ok(Err(err)) => { + eprintln!("windows sandbox failed: {err}"); + std::process::exit(1); + } + Err(join_err) => { + eprintln!("windows sandbox join error: {join_err}"); + std::process::exit(1); + } + }; + + if !capture.stdout.is_empty() { + use std::io::Write; + let _ = std::io::stdout().write_all(&capture.stdout); + } + if !capture.stderr.is_empty() { + use std::io::Write; + let _ = std::io::stderr().write_all(&capture.stderr); + } + + std::process::exit(capture.exit_code); + } + #[cfg(not(target_os = "windows"))] + { + anyhow::bail!("Windows sandbox is only available on Windows"); + } + } + + #[cfg(target_os = "macos")] + let mut denial_logger = log_denials.then(DenialLogger::new).flatten(); + #[cfg(not(target_os = "macos"))] + let _ = log_denials; + let mut child = match sandbox_type { + #[cfg(target_os = "macos")] SandboxType::Seatbelt => { spawn_command_under_seatbelt( command, @@ -114,9 +227,31 @@ async fn run_command_under_sandbox( ) .await? } + SandboxType::Windows => { + unreachable!("Windows sandbox should have been handled above"); + } }; + + #[cfg(target_os = "macos")] + if let Some(denial_logger) = &mut denial_logger { + denial_logger.on_child_spawn(&child); + } + let status = child.wait().await?; + #[cfg(target_os = "macos")] + if let Some(denial_logger) = denial_logger { + let denials = denial_logger.finish().await; + eprintln!("\n=== Sandbox denials ==="); + if denials.is_empty() { + eprintln!("None found."); + } else { + for seatbelt::SandboxDenial { name, capability } in denials { + eprintln!("({name}) {capability}"); + } + } + } + handle_exit_status(status); } diff --git a/codex-rs/cli/src/debug_sandbox/pid_tracker.rs b/codex-rs/cli/src/debug_sandbox/pid_tracker.rs new file mode 100644 index 0000000000..07bd2fd046 --- /dev/null +++ b/codex-rs/cli/src/debug_sandbox/pid_tracker.rs @@ -0,0 +1,372 @@ +use std::collections::HashSet; +use tokio::task::JoinHandle; +use tracing::warn; + +/// Tracks the (recursive) descendants of a process by using `kqueue` to watch for fork events, and +/// `proc_listchildpids` to list the children of a process. +pub(crate) struct PidTracker { + kq: libc::c_int, + handle: JoinHandle>, +} + +impl PidTracker { + pub(crate) fn new(root_pid: i32) -> Option { + if root_pid <= 0 { + return None; + } + + let kq = unsafe { libc::kqueue() }; + let handle = tokio::task::spawn_blocking(move || track_descendants(kq, root_pid)); + + Some(Self { kq, handle }) + } + + pub(crate) async fn stop(self) -> HashSet { + trigger_stop_event(self.kq); + self.handle.await.unwrap_or_default() + } +} + +unsafe extern "C" { + fn proc_listchildpids( + ppid: libc::c_int, + buffer: *mut libc::c_void, + buffersize: libc::c_int, + ) -> libc::c_int; +} + +/// Wrap proc_listchildpids. +fn list_child_pids(parent: i32) -> Vec { + unsafe { + let mut capacity: usize = 16; + loop { + let mut buf: Vec = vec![0; capacity]; + let count = proc_listchildpids( + parent as libc::c_int, + buf.as_mut_ptr() as *mut libc::c_void, + (buf.len() * std::mem::size_of::()) as libc::c_int, + ); + if count <= 0 { + return Vec::new(); + } + let returned = count as usize; + if returned < capacity { + buf.truncate(returned); + return buf; + } + capacity = capacity.saturating_mul(2).max(returned + 16); + } + } +} + +fn pid_is_alive(pid: i32) -> bool { + if pid <= 0 { + return false; + } + let res = unsafe { libc::kill(pid as libc::pid_t, 0) }; + if res == 0 { + true + } else { + matches!( + std::io::Error::last_os_error().raw_os_error(), + Some(libc::EPERM) + ) + } +} + +enum WatchPidError { + ProcessGone, + Other(std::io::Error), +} + +/// Add `pid` to the watch list in `kq`. +fn watch_pid(kq: libc::c_int, pid: i32) -> Result<(), WatchPidError> { + if pid <= 0 { + return Err(WatchPidError::ProcessGone); + } + + let kev = libc::kevent { + ident: pid as libc::uintptr_t, + filter: libc::EVFILT_PROC, + flags: libc::EV_ADD | libc::EV_CLEAR, + fflags: libc::NOTE_FORK | libc::NOTE_EXEC | libc::NOTE_EXIT, + data: 0, + udata: std::ptr::null_mut(), + }; + + let res = unsafe { libc::kevent(kq, &kev, 1, std::ptr::null_mut(), 0, std::ptr::null()) }; + if res < 0 { + let err = std::io::Error::last_os_error(); + if err.raw_os_error() == Some(libc::ESRCH) { + Err(WatchPidError::ProcessGone) + } else { + Err(WatchPidError::Other(err)) + } + } else { + Ok(()) + } +} + +fn watch_children( + kq: libc::c_int, + parent: i32, + seen: &mut HashSet, + active: &mut HashSet, +) { + for child_pid in list_child_pids(parent) { + add_pid_watch(kq, child_pid, seen, active); + } +} + +/// Watch `pid` and its children, updating `seen` and `active` sets. +fn add_pid_watch(kq: libc::c_int, pid: i32, seen: &mut HashSet, active: &mut HashSet) { + if pid <= 0 { + return; + } + + let newly_seen = seen.insert(pid); + let mut should_recurse = newly_seen; + + if active.insert(pid) { + match watch_pid(kq, pid) { + Ok(()) => { + should_recurse = true; + } + Err(WatchPidError::ProcessGone) => { + active.remove(&pid); + return; + } + Err(WatchPidError::Other(err)) => { + warn!("failed to watch pid {pid}: {err}"); + active.remove(&pid); + return; + } + } + } + + if should_recurse { + watch_children(kq, pid, seen, active); + } +} +const STOP_IDENT: libc::uintptr_t = 1; + +fn register_stop_event(kq: libc::c_int) -> bool { + let kev = libc::kevent { + ident: STOP_IDENT, + filter: libc::EVFILT_USER, + flags: libc::EV_ADD | libc::EV_CLEAR, + fflags: 0, + data: 0, + udata: std::ptr::null_mut(), + }; + + let res = unsafe { libc::kevent(kq, &kev, 1, std::ptr::null_mut(), 0, std::ptr::null()) }; + res >= 0 +} + +fn trigger_stop_event(kq: libc::c_int) { + if kq < 0 { + return; + } + + let kev = libc::kevent { + ident: STOP_IDENT, + filter: libc::EVFILT_USER, + flags: 0, + fflags: libc::NOTE_TRIGGER, + data: 0, + udata: std::ptr::null_mut(), + }; + + let _ = unsafe { libc::kevent(kq, &kev, 1, std::ptr::null_mut(), 0, std::ptr::null()) }; +} + +/// Put all of the above together to track all the descendants of `root_pid`. +fn track_descendants(kq: libc::c_int, root_pid: i32) -> HashSet { + if kq < 0 { + let mut seen = HashSet::new(); + seen.insert(root_pid); + return seen; + } + + if !register_stop_event(kq) { + let mut seen = HashSet::new(); + seen.insert(root_pid); + let _ = unsafe { libc::close(kq) }; + return seen; + } + + let mut seen: HashSet = HashSet::new(); + let mut active: HashSet = HashSet::new(); + + add_pid_watch(kq, root_pid, &mut seen, &mut active); + + const EVENTS_CAP: usize = 32; + let mut events: [libc::kevent; EVENTS_CAP] = + unsafe { std::mem::MaybeUninit::zeroed().assume_init() }; + + let mut stop_requested = false; + loop { + if active.is_empty() { + if !pid_is_alive(root_pid) { + break; + } + add_pid_watch(kq, root_pid, &mut seen, &mut active); + if active.is_empty() { + continue; + } + } + + let nev = unsafe { + libc::kevent( + kq, + std::ptr::null::(), + 0, + events.as_mut_ptr(), + EVENTS_CAP as libc::c_int, + std::ptr::null(), + ) + }; + + if nev < 0 { + let err = std::io::Error::last_os_error(); + if err.kind() == std::io::ErrorKind::Interrupted { + continue; + } + break; + } + + if nev == 0 { + continue; + } + + for ev in events.iter().take(nev as usize) { + let pid = ev.ident as i32; + + if ev.filter == libc::EVFILT_USER && ev.ident == STOP_IDENT { + stop_requested = true; + break; + } + + if (ev.flags & libc::EV_ERROR) != 0 { + if ev.data == libc::ESRCH as isize { + active.remove(&pid); + } + continue; + } + + if (ev.fflags & libc::NOTE_FORK) != 0 { + watch_children(kq, pid, &mut seen, &mut active); + } + + if (ev.fflags & libc::NOTE_EXIT) != 0 { + active.remove(&pid); + } + } + + if stop_requested { + break; + } + } + + let _ = unsafe { libc::close(kq) }; + + seen +} + +#[cfg(test)] +mod tests { + use super::*; + use std::process::Command; + use std::process::Stdio; + use std::time::Duration; + + #[test] + fn pid_is_alive_detects_current_process() { + let pid = std::process::id() as i32; + assert!(pid_is_alive(pid)); + } + + #[cfg(target_os = "macos")] + #[test] + fn list_child_pids_includes_spawned_child() { + let mut child = Command::new("/bin/sleep") + .arg("5") + .stdin(Stdio::null()) + .spawn() + .expect("failed to spawn child process"); + + let child_pid = child.id() as i32; + let parent_pid = std::process::id() as i32; + + let mut found = false; + for _ in 0..100 { + if list_child_pids(parent_pid).contains(&child_pid) { + found = true; + break; + } + std::thread::sleep(Duration::from_millis(10)); + } + + let _ = child.kill(); + let _ = child.wait(); + + assert!(found, "expected to find child pid {child_pid} in list"); + } + + #[cfg(target_os = "macos")] + #[tokio::test] + async fn pid_tracker_collects_spawned_children() { + let tracker = PidTracker::new(std::process::id() as i32).expect("failed to create tracker"); + + let mut child = Command::new("/bin/sleep") + .arg("0.1") + .stdin(Stdio::null()) + .spawn() + .expect("failed to spawn child process"); + + let child_pid = child.id() as i32; + let parent_pid = std::process::id() as i32; + + let _ = child.wait(); + + let seen = tracker.stop().await; + + assert!( + seen.contains(&parent_pid), + "expected tracker to include parent pid {parent_pid}" + ); + assert!( + seen.contains(&child_pid), + "expected tracker to include child pid {child_pid}" + ); + } + + #[cfg(target_os = "macos")] + #[tokio::test] + async fn pid_tracker_collects_bash_subshell_descendants() { + let tracker = PidTracker::new(std::process::id() as i32).expect("failed to create tracker"); + + let child = Command::new("/bin/bash") + .arg("-c") + .arg("(sleep 0.1 & echo $!; wait)") + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .expect("failed to spawn bash"); + + let output = child.wait_with_output().unwrap().stdout; + let subshell_pid = String::from_utf8_lossy(&output) + .trim() + .parse::() + .expect("failed to parse subshell pid"); + + let seen = tracker.stop().await; + + assert!( + seen.contains(&subshell_pid), + "expected tracker to include subshell pid {subshell_pid}" + ); + } +} diff --git a/codex-rs/cli/src/debug_sandbox/seatbelt.rs b/codex-rs/cli/src/debug_sandbox/seatbelt.rs new file mode 100644 index 0000000000..a1d6435b51 --- /dev/null +++ b/codex-rs/cli/src/debug_sandbox/seatbelt.rs @@ -0,0 +1,114 @@ +use std::collections::HashSet; +use tokio::io::AsyncBufReadExt; +use tokio::process::Child; +use tokio::task::JoinHandle; + +use super::pid_tracker::PidTracker; + +pub struct SandboxDenial { + pub name: String, + pub capability: String, +} + +pub struct DenialLogger { + log_stream: Child, + pid_tracker: Option, + log_reader: Option>>, +} + +impl DenialLogger { + pub(crate) fn new() -> Option { + let mut log_stream = start_log_stream()?; + let stdout = log_stream.stdout.take()?; + let log_reader = tokio::spawn(async move { + let mut reader = tokio::io::BufReader::new(stdout); + let mut logs = Vec::new(); + let mut chunk = Vec::new(); + loop { + match reader.read_until(b'\n', &mut chunk).await { + Ok(0) | Err(_) => break, + Ok(_) => { + logs.extend_from_slice(&chunk); + chunk.clear(); + } + } + } + logs + }); + + Some(Self { + log_stream, + pid_tracker: None, + log_reader: Some(log_reader), + }) + } + + pub(crate) fn on_child_spawn(&mut self, child: &Child) { + if let Some(root_pid) = child.id() { + self.pid_tracker = PidTracker::new(root_pid as i32); + } + } + + pub(crate) async fn finish(mut self) -> Vec { + let pid_set = match self.pid_tracker { + Some(tracker) => tracker.stop().await, + None => Default::default(), + }; + + if pid_set.is_empty() { + return Vec::new(); + } + + let _ = self.log_stream.kill().await; + let _ = self.log_stream.wait().await; + + let logs_bytes = match self.log_reader.take() { + Some(handle) => handle.await.unwrap_or_default(), + None => Vec::new(), + }; + let logs = String::from_utf8_lossy(&logs_bytes); + + let mut seen: HashSet<(String, String)> = HashSet::new(); + let mut denials: Vec = Vec::new(); + for line in logs.lines() { + if let Ok(json) = serde_json::from_str::(line) + && let Some(msg) = json.get("eventMessage").and_then(|v| v.as_str()) + && let Some((pid, name, capability)) = parse_message(msg) + && pid_set.contains(&pid) + && seen.insert((name.clone(), capability.clone())) + { + denials.push(SandboxDenial { name, capability }); + } + } + denials + } +} + +fn start_log_stream() -> Option { + use std::process::Stdio; + + const PREDICATE: &str = r#"(((processID == 0) AND (senderImagePath CONTAINS "/Sandbox")) OR (subsystem == "com.apple.sandbox.reporting"))"#; + + tokio::process::Command::new("log") + .args(["stream", "--style", "ndjson", "--predicate", PREDICATE]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .kill_on_drop(true) + .spawn() + .ok() +} + +fn parse_message(msg: &str) -> Option<(i32, String, String)> { + // Example message: + // Sandbox: processname(1234) deny(1) capability-name args... + static RE: std::sync::OnceLock = std::sync::OnceLock::new(); + let re = RE.get_or_init(|| { + #[expect(clippy::unwrap_used)] + regex_lite::Regex::new(r"^Sandbox:\s*(.+?)\((\d+)\)\s+deny\(.*?\)\s*(.+)$").unwrap() + }); + + let (_, [name, pid_str, capability]) = re.captures(msg)?.extract(); + let pid = pid_str.trim().parse::().ok()?; + Some((pid, name.to_string(), capability.to_string())) +} diff --git a/codex-rs/cli/src/lib.rs b/codex-rs/cli/src/lib.rs index c6d80c0adf..e9f60eba76 100644 --- a/codex-rs/cli/src/lib.rs +++ b/codex-rs/cli/src/lib.rs @@ -1,7 +1,6 @@ pub mod debug_sandbox; mod exit_status; pub mod login; -pub mod proto; use clap::Parser; use codex_common::CliConfigOverrides; @@ -12,6 +11,10 @@ pub struct SeatbeltCommand { #[arg(long = "full-auto", default_value_t = false)] pub full_auto: bool, + /// While the command runs, capture macOS sandbox denials via `log stream` and print them after exit + #[arg(long = "log-denials", default_value_t = false)] + pub log_denials: bool, + #[clap(skip)] pub config_overrides: CliConfigOverrides, @@ -33,3 +36,17 @@ pub struct LandlockCommand { #[arg(trailing_var_arg = true)] pub command: Vec, } + +#[derive(Debug, Parser)] +pub struct WindowsCommand { + /// Convenience alias for low-friction sandboxed automatic execution (network-disabled sandbox that can write to cwd and TMPDIR) + #[arg(long = "full-auto", default_value_t = false)] + pub full_auto: bool, + + #[clap(skip)] + pub config_overrides: CliConfigOverrides, + + /// Full command args to run under Windows restricted token sandbox. + #[arg(trailing_var_arg = true)] + pub command: Vec, +} diff --git a/codex-rs/cli/src/login.rs b/codex-rs/cli/src/login.rs index f0816d0b29..6681ab20c8 100644 --- a/codex-rs/cli/src/login.rs +++ b/codex-rs/cli/src/login.rs @@ -1,17 +1,31 @@ +use codex_app_server_protocol::AuthMode; use codex_common::CliConfigOverrides; use codex_core::CodexAuth; +use codex_core::auth::AuthCredentialsStoreMode; use codex_core::auth::CLIENT_ID; use codex_core::auth::login_with_api_key; use codex_core::auth::logout; use codex_core::config::Config; use codex_core::config::ConfigOverrides; use codex_login::ServerOptions; +use codex_login::run_device_code_login; use codex_login::run_login_server; -use codex_protocol::mcp_protocol::AuthMode; +use codex_protocol::config_types::ForcedLoginMethod; +use std::io::IsTerminal; +use std::io::Read; use std::path::PathBuf; -pub async fn login_with_chatgpt(codex_home: PathBuf) -> std::io::Result<()> { - let opts = ServerOptions::new(codex_home, CLIENT_ID.to_string()); +pub async fn login_with_chatgpt( + codex_home: PathBuf, + forced_chatgpt_workspace_id: Option, + cli_auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result<()> { + let opts = ServerOptions::new( + codex_home, + CLIENT_ID.to_string(), + forced_chatgpt_workspace_id, + cli_auth_credentials_store_mode, + ); let server = run_login_server(opts)?; eprintln!( @@ -23,9 +37,22 @@ pub async fn login_with_chatgpt(codex_home: PathBuf) -> std::io::Result<()> { } pub async fn run_login_with_chatgpt(cli_config_overrides: CliConfigOverrides) -> ! { - let config = load_config_or_exit(cli_config_overrides); + let config = load_config_or_exit(cli_config_overrides).await; - match login_with_chatgpt(config.codex_home).await { + if matches!(config.forced_login_method, Some(ForcedLoginMethod::Api)) { + eprintln!("ChatGPT login is disabled. Use API key login instead."); + std::process::exit(1); + } + + let forced_chatgpt_workspace_id = config.forced_chatgpt_workspace_id.clone(); + + match login_with_chatgpt( + config.codex_home, + forced_chatgpt_workspace_id, + config.cli_auth_credentials_store_mode, + ) + .await + { Ok(_) => { eprintln!("Successfully logged in"); std::process::exit(0); @@ -41,9 +68,18 @@ pub async fn run_login_with_api_key( cli_config_overrides: CliConfigOverrides, api_key: String, ) -> ! { - let config = load_config_or_exit(cli_config_overrides); + let config = load_config_or_exit(cli_config_overrides).await; - match login_with_api_key(&config.codex_home, &api_key) { + if matches!(config.forced_login_method, Some(ForcedLoginMethod::Chatgpt)) { + eprintln!("API key login is disabled. Use ChatGPT login instead."); + std::process::exit(1); + } + + match login_with_api_key( + &config.codex_home, + &api_key, + config.cli_auth_credentials_store_mode, + ) { Ok(_) => { eprintln!("Successfully logged in"); std::process::exit(0); @@ -55,10 +91,70 @@ pub async fn run_login_with_api_key( } } -pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! { - let config = load_config_or_exit(cli_config_overrides); +pub fn read_api_key_from_stdin() -> String { + let mut stdin = std::io::stdin(); - match CodexAuth::from_codex_home(&config.codex_home) { + if stdin.is_terminal() { + eprintln!( + "--with-api-key expects the API key on stdin. Try piping it, e.g. `printenv OPENAI_API_KEY | codex login --with-api-key`." + ); + std::process::exit(1); + } + + eprintln!("Reading API key from stdin..."); + + let mut buffer = String::new(); + if let Err(err) = stdin.read_to_string(&mut buffer) { + eprintln!("Failed to read API key from stdin: {err}"); + std::process::exit(1); + } + + let api_key = buffer.trim().to_string(); + if api_key.is_empty() { + eprintln!("No API key provided via stdin."); + std::process::exit(1); + } + + api_key +} + +/// Login using the OAuth device code flow. +pub async fn run_login_with_device_code( + cli_config_overrides: CliConfigOverrides, + issuer_base_url: Option, + client_id: Option, +) -> ! { + let config = load_config_or_exit(cli_config_overrides).await; + if matches!(config.forced_login_method, Some(ForcedLoginMethod::Api)) { + eprintln!("ChatGPT login is disabled. Use API key login instead."); + std::process::exit(1); + } + let forced_chatgpt_workspace_id = config.forced_chatgpt_workspace_id.clone(); + let mut opts = ServerOptions::new( + config.codex_home, + client_id.unwrap_or(CLIENT_ID.to_string()), + forced_chatgpt_workspace_id, + config.cli_auth_credentials_store_mode, + ); + if let Some(iss) = issuer_base_url { + opts.issuer = iss; + } + match run_device_code_login(opts).await { + Ok(()) => { + eprintln!("Successfully logged in"); + std::process::exit(0); + } + Err(e) => { + eprintln!("Error logging in with device code: {e}"); + std::process::exit(1); + } + } +} + +pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! { + let config = load_config_or_exit(cli_config_overrides).await; + + match CodexAuth::from_auth_storage(&config.codex_home, config.cli_auth_credentials_store_mode) { Ok(Some(auth)) => match auth.mode { AuthMode::ApiKey => match auth.get_token().await { Ok(api_key) => { @@ -87,9 +183,9 @@ pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! { } pub async fn run_logout(cli_config_overrides: CliConfigOverrides) -> ! { - let config = load_config_or_exit(cli_config_overrides); + let config = load_config_or_exit(cli_config_overrides).await; - match logout(&config.codex_home) { + match logout(&config.codex_home, config.cli_auth_credentials_store_mode) { Ok(true) => { eprintln!("Successfully logged out"); std::process::exit(0); @@ -105,7 +201,7 @@ pub async fn run_logout(cli_config_overrides: CliConfigOverrides) -> ! { } } -fn load_config_or_exit(cli_config_overrides: CliConfigOverrides) -> Config { +async fn load_config_or_exit(cli_config_overrides: CliConfigOverrides) -> Config { let cli_overrides = match cli_config_overrides.parse_overrides() { Ok(v) => v, Err(e) => { @@ -115,7 +211,7 @@ fn load_config_or_exit(cli_config_overrides: CliConfigOverrides) -> Config { }; let config_overrides = ConfigOverrides::default(); - match Config::load_with_cli_overrides(cli_overrides, config_overrides) { + match Config::load_with_cli_overrides(cli_overrides, config_overrides).await { Ok(config) => config, Err(e) => { eprintln!("Error loading configuration: {e}"); diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index e66855fe73..6a3b24aa98 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1,3 +1,4 @@ +use clap::Args; use clap::CommandFactory; use clap::Parser; use clap_complete::Shell; @@ -7,20 +8,33 @@ use codex_chatgpt::apply_command::ApplyCommand; use codex_chatgpt::apply_command::run_apply_command; use codex_cli::LandlockCommand; use codex_cli::SeatbeltCommand; +use codex_cli::WindowsCommand; +use codex_cli::login::read_api_key_from_stdin; use codex_cli::login::run_login_status; use codex_cli::login::run_login_with_api_key; use codex_cli::login::run_login_with_chatgpt; +use codex_cli::login::run_login_with_device_code; use codex_cli::login::run_logout; -use codex_cli::proto; +use codex_cloud_tasks::Cli as CloudTasksCli; use codex_common::CliConfigOverrides; use codex_exec::Cli as ExecCli; +use codex_responses_api_proxy::Args as ResponsesApiProxyArgs; +use codex_tui::AppExitInfo; use codex_tui::Cli as TuiCli; +use codex_tui::update_action::UpdateAction; +use owo_colors::OwoColorize; use std::path::PathBuf; +use supports_color::Stream; mod mcp_cmd; +#[cfg(not(windows))] +mod wsl_paths; use crate::mcp_cmd::McpCli; -use crate::proto::ProtoCli; + +use codex_core::config::Config; +use codex_core::config::ConfigOverrides; +use codex_core::features::is_known_feature_key; /// Codex CLI /// @@ -34,12 +48,16 @@ use crate::proto::ProtoCli; // The executable is sometimes invoked via a platform‑specific name like // `codex-x86_64-unknown-linux-musl`, but the help output should always use // the generic `codex` command name that users run. - bin_name = "codex" + bin_name = "codex", + override_usage = "codex [OPTIONS] [PROMPT]\n codex [OPTIONS] [ARGS]" )] struct MultitoolCli { #[clap(flatten)] pub config_overrides: CliConfigOverrides, + #[clap(flatten)] + pub feature_toggles: FeatureToggles, + #[clap(flatten)] interactive: TuiCli, @@ -62,15 +80,18 @@ enum Subcommand { /// [experimental] Run Codex as an MCP server and manage MCP servers. Mcp(McpCli), - /// Run the Protocol stream via stdin/stdout - #[clap(visible_alias = "p")] - Proto(ProtoCli), + /// [experimental] Run the Codex MCP server (stdio transport). + McpServer, + + /// [experimental] Run the app server or related tooling. + AppServer(AppServerCommand), /// Generate shell completion scripts. Completion(CompletionCommand), - /// Internal debugging commands. - Debug(DebugArgs), + /// Run commands within a Codex-provided sandbox. + #[clap(visible_alias = "debug")] + Sandbox(SandboxArgs), /// Apply the latest diff produced by Codex agent as a `git apply` to your local working tree. #[clap(visible_alias = "a")] @@ -79,9 +100,20 @@ enum Subcommand { /// Resume a previous interactive session (picker by default; use --last to continue the most recent). Resume(ResumeCommand), - /// Internal: generate TypeScript protocol bindings. + /// [EXPERIMENTAL] Browse tasks from Codex Cloud and apply changes locally. + #[clap(name = "cloud", alias = "cloud-tasks")] + Cloud(CloudTasksCli), + + /// Internal: run the responses API proxy. #[clap(hide = true)] - GenerateTs(GenerateTsCommand), + ResponsesApiProxy(ResponsesApiProxyArgs), + + /// Internal: relay stdio to a Unix domain socket. + #[clap(hide = true, name = "stdio-to-uds")] + StdioToUds(StdioToUdsCommand), + + /// Inspect feature flags. + Features(FeaturesCli), } #[derive(Debug, Parser)] @@ -107,18 +139,23 @@ struct ResumeCommand { } #[derive(Debug, Parser)] -struct DebugArgs { +struct SandboxArgs { #[command(subcommand)] - cmd: DebugCommand, + cmd: SandboxCommand, } #[derive(Debug, clap::Subcommand)] -enum DebugCommand { +enum SandboxCommand { /// Run a command under Seatbelt (macOS only). - Seatbelt(SeatbeltCommand), + #[clap(visible_alias = "seatbelt")] + Macos(SeatbeltCommand), /// Run a command under Landlock+seccomp (Linux only). - Landlock(LandlockCommand), + #[clap(visible_alias = "landlock")] + Linux(LandlockCommand), + + /// Run a command under Windows restricted token (Windows only). + Windows(WindowsCommand), } #[derive(Debug, Parser)] @@ -126,9 +163,32 @@ struct LoginCommand { #[clap(skip)] config_overrides: CliConfigOverrides, - #[arg(long = "api-key", value_name = "API_KEY")] + #[arg( + long = "with-api-key", + help = "Read the API key from stdin (e.g. `printenv OPENAI_API_KEY | codex login --with-api-key`)" + )] + with_api_key: bool, + + #[arg( + long = "api-key", + value_name = "API_KEY", + help = "(deprecated) Previously accepted the API key directly; now exits with guidance to use --with-api-key", + hide = true + )] api_key: Option, + #[arg(long = "device-auth")] + use_device_code: bool, + + /// EXPERIMENTAL: Use custom OAuth issuer base URL (advanced) + /// Override the OAuth issuer base URL (advanced) + #[arg(long = "experimental_issuer", value_name = "URL", hide = true)] + issuer_base_url: Option, + + /// EXPERIMENTAL: Use custom OAuth client ID (advanced) + #[arg(long = "experimental_client-id", value_name = "CLIENT_ID", hide = true)] + client_id: Option, + #[command(subcommand)] action: Option, } @@ -146,6 +206,22 @@ struct LogoutCommand { } #[derive(Debug, Parser)] +struct AppServerCommand { + /// Omit to run the app server; specify a subcommand for tooling. + #[command(subcommand)] + subcommand: Option, +} + +#[derive(Debug, clap::Subcommand)] +enum AppServerSubcommand { + /// [experimental] Generate TypeScript bindings for the app server protocol. + GenerateTs(GenerateTsCommand), + + /// [experimental] Generate JSON Schema for the app server protocol. + GenerateJsonSchema(GenerateJsonSchemaCommand), +} + +#[derive(Debug, Args)] struct GenerateTsCommand { /// Output directory where .ts files will be written #[arg(short = 'o', long = "out", value_name = "DIR")] @@ -156,6 +232,162 @@ struct GenerateTsCommand { prettier: Option, } +#[derive(Debug, Args)] +struct GenerateJsonSchemaCommand { + /// Output directory where the schema bundle will be written + #[arg(short = 'o', long = "out", value_name = "DIR")] + out_dir: PathBuf, +} + +#[derive(Debug, Parser)] +struct StdioToUdsCommand { + /// Path to the Unix domain socket to connect to. + #[arg(value_name = "SOCKET_PATH")] + socket_path: PathBuf, +} + +fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec { + let AppExitInfo { + token_usage, + conversation_id, + .. + } = exit_info; + + if token_usage.is_zero() { + return Vec::new(); + } + + let mut lines = vec![format!( + "{}", + codex_core::protocol::FinalOutput::from(token_usage) + )]; + + if let Some(session_id) = conversation_id { + let resume_cmd = format!("codex resume {session_id}"); + let command = if color_enabled { + resume_cmd.cyan().to_string() + } else { + resume_cmd + }; + lines.push(format!("To continue this session, run {command}")); + } + + lines +} + +/// Handle the app exit and print the results. Optionally run the update action. +fn handle_app_exit(exit_info: AppExitInfo) -> anyhow::Result<()> { + let update_action = exit_info.update_action; + let color_enabled = supports_color::on(Stream::Stdout).is_some(); + for line in format_exit_messages(exit_info, color_enabled) { + println!("{line}"); + } + if let Some(action) = update_action { + run_update_action(action)?; + } + Ok(()) +} + +/// Run the update action and print the result. +fn run_update_action(action: UpdateAction) -> anyhow::Result<()> { + println!(); + let cmd_str = action.command_str(); + println!("Updating Codex via `{cmd_str}`..."); + + let status = { + #[cfg(windows)] + { + // On Windows, run via cmd.exe so .CMD/.BAT are correctly resolved (PATHEXT semantics). + std::process::Command::new("cmd") + .args(["/C", &cmd_str]) + .status()? + } + #[cfg(not(windows))] + { + let (cmd, args) = action.command_args(); + let command_path = crate::wsl_paths::normalize_for_wsl(cmd); + let normalized_args: Vec = args + .iter() + .map(crate::wsl_paths::normalize_for_wsl) + .collect(); + std::process::Command::new(&command_path) + .args(&normalized_args) + .status()? + } + }; + if !status.success() { + anyhow::bail!("`{cmd_str}` failed with status {status}"); + } + println!(); + println!("🎉 Update ran successfully! Please restart Codex."); + Ok(()) +} + +#[derive(Debug, Default, Parser, Clone)] +struct FeatureToggles { + /// Enable a feature (repeatable). Equivalent to `-c features.=true`. + #[arg(long = "enable", value_name = "FEATURE", action = clap::ArgAction::Append, global = true)] + enable: Vec, + + /// Disable a feature (repeatable). Equivalent to `-c features.=false`. + #[arg(long = "disable", value_name = "FEATURE", action = clap::ArgAction::Append, global = true)] + disable: Vec, +} + +impl FeatureToggles { + fn to_overrides(&self) -> anyhow::Result> { + let mut v = Vec::new(); + for feature in &self.enable { + Self::validate_feature(feature)?; + v.push(format!("features.{feature}=true")); + } + for feature in &self.disable { + Self::validate_feature(feature)?; + v.push(format!("features.{feature}=false")); + } + Ok(v) + } + + fn validate_feature(feature: &str) -> anyhow::Result<()> { + if is_known_feature_key(feature) { + Ok(()) + } else { + anyhow::bail!("Unknown feature flag: {feature}") + } + } +} + +#[derive(Debug, Parser)] +struct FeaturesCli { + #[command(subcommand)] + sub: FeaturesSubcommand, +} + +#[derive(Debug, Parser)] +enum FeaturesSubcommand { + /// List known features with their stage and effective state. + List, +} + +fn stage_str(stage: codex_core::features::Stage) -> &'static str { + use codex_core::features::Stage; + match stage { + Stage::Experimental => "experimental", + Stage::Beta => "beta", + Stage::Stable => "stable", + Stage::Deprecated => "deprecated", + Stage::Removed => "removed", + } +} + +/// As early as possible in the process lifecycle, apply hardening measures. We +/// skip this in debug builds to avoid interfering with debugging. +#[ctor::ctor] +#[cfg(not(debug_assertions))] +fn pre_main_hardening() { + codex_process_hardening::pre_main_hardening(); +} + fn main() -> anyhow::Result<()> { arg0_dispatch_or_else(|codex_linux_sandbox_exe| async move { cli_main(codex_linux_sandbox_exe).await?; @@ -165,24 +397,24 @@ fn main() -> anyhow::Result<()> { async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<()> { let MultitoolCli { - config_overrides: root_config_overrides, + config_overrides: mut root_config_overrides, + feature_toggles, mut interactive, subcommand, } = MultitoolCli::parse(); + // Fold --enable/--disable into config overrides so they flow to all subcommands. + let toggle_overrides = feature_toggles.to_overrides()?; + root_config_overrides.raw_overrides.extend(toggle_overrides); + match subcommand { None => { prepend_config_flags( &mut interactive.config_overrides, root_config_overrides.clone(), ); - let usage = codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?; - if !usage.token_usage.is_zero() { - println!( - "{}", - codex_core::protocol::FinalOutput::from(usage.token_usage) - ); - } + let exit_info = codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?; + handle_app_exit(exit_info)?; } Some(Subcommand::Exec(mut exec_cli)) => { prepend_config_flags( @@ -191,11 +423,28 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() ); codex_exec::run_main(exec_cli, codex_linux_sandbox_exe).await?; } + Some(Subcommand::McpServer) => { + codex_mcp_server::run_main(codex_linux_sandbox_exe, root_config_overrides).await?; + } Some(Subcommand::Mcp(mut mcp_cli)) => { // Propagate any root-level config overrides (e.g. `-c key=value`). prepend_config_flags(&mut mcp_cli.config_overrides, root_config_overrides.clone()); - mcp_cli.run(codex_linux_sandbox_exe).await?; + mcp_cli.run().await?; } + Some(Subcommand::AppServer(app_server_cli)) => match app_server_cli.subcommand { + None => { + codex_app_server::run_main(codex_linux_sandbox_exe, root_config_overrides).await?; + } + Some(AppServerSubcommand::GenerateTs(gen_cli)) => { + codex_app_server_protocol::generate_ts( + &gen_cli.out_dir, + gen_cli.prettier.as_deref(), + )?; + } + Some(AppServerSubcommand::GenerateJsonSchema(gen_cli)) => { + codex_app_server_protocol::generate_json(&gen_cli.out_dir)?; + } + }, Some(Subcommand::Resume(ResumeCommand { session_id, last, @@ -208,7 +457,8 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() last, config_overrides, ); - codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?; + let exit_info = codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?; + handle_app_exit(exit_info)?; } Some(Subcommand::Login(mut login_cli)) => { prepend_config_flags( @@ -220,7 +470,20 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() run_login_status(login_cli.config_overrides).await; } None => { - if let Some(api_key) = login_cli.api_key { + if login_cli.use_device_code { + run_login_with_device_code( + login_cli.config_overrides, + login_cli.issuer_base_url, + login_cli.client_id, + ) + .await; + } else if login_cli.api_key.is_some() { + eprintln!( + "The --api-key flag is no longer supported. Pipe the key instead, e.g. `printenv OPENAI_API_KEY | codex login --with-api-key`." + ); + std::process::exit(1); + } else if login_cli.with_api_key { + let api_key = read_api_key_from_stdin(); run_login_with_api_key(login_cli.config_overrides, api_key).await; } else { run_login_with_chatgpt(login_cli.config_overrides).await; @@ -235,18 +498,18 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() ); run_logout(logout_cli.config_overrides).await; } - Some(Subcommand::Proto(mut proto_cli)) => { - prepend_config_flags( - &mut proto_cli.config_overrides, - root_config_overrides.clone(), - ); - proto::run_main(proto_cli).await?; - } Some(Subcommand::Completion(completion_cli)) => { print_completion(completion_cli); } - Some(Subcommand::Debug(debug_args)) => match debug_args.cmd { - DebugCommand::Seatbelt(mut seatbelt_cli) => { + Some(Subcommand::Cloud(mut cloud_cli)) => { + prepend_config_flags( + &mut cloud_cli.config_overrides, + root_config_overrides.clone(), + ); + codex_cloud_tasks::run_main(cloud_cli, codex_linux_sandbox_exe).await?; + } + Some(Subcommand::Sandbox(sandbox_args)) => match sandbox_args.cmd { + SandboxCommand::Macos(mut seatbelt_cli) => { prepend_config_flags( &mut seatbelt_cli.config_overrides, root_config_overrides.clone(), @@ -257,7 +520,7 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() ) .await?; } - DebugCommand::Landlock(mut landlock_cli) => { + SandboxCommand::Linux(mut landlock_cli) => { prepend_config_flags( &mut landlock_cli.config_overrides, root_config_overrides.clone(), @@ -268,6 +531,17 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() ) .await?; } + SandboxCommand::Windows(mut windows_cli) => { + prepend_config_flags( + &mut windows_cli.config_overrides, + root_config_overrides.clone(), + ); + codex_cli::debug_sandbox::run_command_under_windows( + windows_cli, + codex_linux_sandbox_exe, + ) + .await?; + } }, Some(Subcommand::Apply(mut apply_cli)) => { prepend_config_flags( @@ -276,9 +550,45 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() ); run_apply_command(apply_cli, None).await?; } - Some(Subcommand::GenerateTs(gen_cli)) => { - codex_protocol_ts::generate_ts(&gen_cli.out_dir, gen_cli.prettier.as_deref())?; + Some(Subcommand::ResponsesApiProxy(args)) => { + tokio::task::spawn_blocking(move || codex_responses_api_proxy::run_main(args)) + .await??; } + Some(Subcommand::StdioToUds(cmd)) => { + let socket_path = cmd.socket_path; + tokio::task::spawn_blocking(move || codex_stdio_to_uds::run(socket_path.as_path())) + .await??; + } + Some(Subcommand::Features(FeaturesCli { sub })) => match sub { + FeaturesSubcommand::List => { + // Respect root-level `-c` overrides plus top-level flags like `--profile`. + let mut cli_kv_overrides = root_config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; + + // Honor `--search` via the new feature toggle. + if interactive.web_search { + cli_kv_overrides.push(( + "features.web_search_request".to_string(), + toml::Value::Boolean(true), + )); + } + + // Thread through relevant top-level flags (at minimum, `--profile`). + let overrides = ConfigOverrides { + config_profile: interactive.config_profile.clone(), + ..Default::default() + }; + + let config = Config::load_with_cli_overrides(cli_kv_overrides, overrides).await?; + for def in codex_core::features::FEATURES.iter() { + let name = def.key; + let stage = stage_str(def.stage); + let enabled = config.features.enabled(def.id); + println!("{name}\t{stage}\t{enabled}"); + } + } + }, } Ok(()) @@ -353,6 +663,9 @@ fn merge_resume_cli_flags(interactive: &mut TuiCli, resume_cli: TuiCli) { if !resume_cli.images.is_empty() { interactive.images = resume_cli.images; } + if !resume_cli.add_dir.is_empty() { + interactive.add_dir.extend(resume_cli.add_dir); + } if let Some(prompt) = resume_cli.prompt { interactive.prompt = Some(prompt); } @@ -372,6 +685,10 @@ fn print_completion(cmd: CompletionCommand) { #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; + use codex_core::protocol::TokenUsage; + use codex_protocol::ConversationId; + use pretty_assertions::assert_eq; fn finalize_from_args(args: &[&str]) -> TuiCli { let cli = MultitoolCli::try_parse_from(args).expect("parse"); @@ -379,6 +696,7 @@ mod tests { interactive, config_overrides: root_overrides, subcommand, + feature_toggles: _, } = cli; let Subcommand::Resume(ResumeCommand { @@ -393,11 +711,59 @@ mod tests { finalize_resume_interactive(interactive, root_overrides, session_id, last, resume_cli) } + fn sample_exit_info(conversation: Option<&str>) -> AppExitInfo { + let token_usage = TokenUsage { + output_tokens: 2, + total_tokens: 2, + ..Default::default() + }; + AppExitInfo { + token_usage, + conversation_id: conversation + .map(ConversationId::from_string) + .map(Result::unwrap), + update_action: None, + } + } + + #[test] + fn format_exit_messages_skips_zero_usage() { + let exit_info = AppExitInfo { + token_usage: TokenUsage::default(), + conversation_id: None, + update_action: None, + }; + let lines = format_exit_messages(exit_info, false); + assert!(lines.is_empty()); + } + + #[test] + fn format_exit_messages_includes_resume_hint_without_color() { + let exit_info = sample_exit_info(Some("123e4567-e89b-12d3-a456-426614174000")); + let lines = format_exit_messages(exit_info, false); + assert_eq!( + lines, + vec![ + "Token usage: total=2 input=0 output=2".to_string(), + "To continue this session, run codex resume 123e4567-e89b-12d3-a456-426614174000" + .to_string(), + ] + ); + } + + #[test] + fn format_exit_messages_applies_color_when_enabled() { + let exit_info = sample_exit_info(Some("123e4567-e89b-12d3-a456-426614174000")); + let lines = format_exit_messages(exit_info, true); + assert_eq!(lines.len(), 2); + assert!(lines[1].contains("\u{1b}[36m")); + } + #[test] fn resume_model_flag_applies_when_no_root_flags() { - let interactive = finalize_from_args(["codex", "resume", "-m", "gpt-5-test"].as_ref()); + let interactive = finalize_from_args(["codex", "resume", "-m", "gpt-5.1-test"].as_ref()); - assert_eq!(interactive.model.as_deref(), Some("gpt-5-test")); + assert_eq!(interactive.model.as_deref(), Some("gpt-5.1-test")); assert!(interactive.resume_picker); assert!(!interactive.resume_last); assert_eq!(interactive.resume_session_id, None); @@ -442,7 +808,7 @@ mod tests { "--ask-for-approval", "on-request", "-m", - "gpt-5-test", + "gpt-5.1-test", "-p", "my-profile", "-C", @@ -453,17 +819,17 @@ mod tests { .as_ref(), ); - assert_eq!(interactive.model.as_deref(), Some("gpt-5-test")); + assert_eq!(interactive.model.as_deref(), Some("gpt-5.1-test")); assert!(interactive.oss); assert_eq!(interactive.config_profile.as_deref(), Some("my-profile")); - assert!(matches!( + assert_matches!( interactive.sandbox_mode, Some(codex_common::SandboxModeCliArg::WorkspaceWrite) - )); - assert!(matches!( + ); + assert_matches!( interactive.approval_policy, Some(codex_common::ApprovalModeCliArg::OnRequest) - )); + ); assert!(interactive.full_auto); assert_eq!( interactive.cwd.as_deref(), @@ -499,4 +865,32 @@ mod tests { assert!(!interactive.resume_last); assert_eq!(interactive.resume_session_id, None); } + + #[test] + fn feature_toggles_known_features_generate_overrides() { + let toggles = FeatureToggles { + enable: vec!["web_search_request".to_string()], + disable: vec!["unified_exec".to_string()], + }; + let overrides = toggles.to_overrides().expect("valid features"); + assert_eq!( + overrides, + vec![ + "features.web_search_request=true".to_string(), + "features.unified_exec=false".to_string(), + ] + ); + } + + #[test] + fn feature_toggles_unknown_feature_errors() { + let toggles = FeatureToggles { + enable: vec!["does_not_exist".to_string()], + disable: Vec::new(), + }; + let err = toggles + .to_overrides() + .expect_err("feature should be rejected"); + assert_eq!(err.to_string(), "Unknown feature flag: does_not_exist"); + } } diff --git a/codex-rs/cli/src/mcp_cmd.rs b/codex-rs/cli/src/mcp_cmd.rs index 437511ad57..ec37c3a6bd 100644 --- a/codex-rs/cli/src/mcp_cmd.rs +++ b/codex-rs/cli/src/mcp_cmd.rs @@ -1,18 +1,25 @@ -use std::collections::BTreeMap; use std::collections::HashMap; -use std::path::PathBuf; use anyhow::Context; use anyhow::Result; use anyhow::anyhow; use anyhow::bail; +use clap::ArgGroup; use codex_common::CliConfigOverrides; +use codex_common::format_env_display::format_env_display; use codex_core::config::Config; use codex_core::config::ConfigOverrides; +use codex_core::config::edit::ConfigEditsBuilder; use codex_core::config::find_codex_home; use codex_core::config::load_global_mcp_servers; -use codex_core::config::write_global_mcp_servers; -use codex_core::config_types::McpServerConfig; +use codex_core::config::types::McpServerConfig; +use codex_core::config::types::McpServerTransportConfig; +use codex_core::features::Feature; +use codex_core::mcp::auth::compute_auth_statuses; +use codex_core::protocol::McpAuthStatus; +use codex_rmcp_client::delete_oauth_tokens; +use codex_rmcp_client::perform_oauth_login; +use codex_rmcp_client::supports_oauth_login; /// [experimental] Launch Codex as an MCP server or manage configured MCP servers. /// @@ -28,14 +35,11 @@ pub struct McpCli { pub config_overrides: CliConfigOverrides, #[command(subcommand)] - pub cmd: Option, + pub subcommand: McpSubcommand, } #[derive(Debug, clap::Subcommand)] pub enum McpSubcommand { - /// [experimental] Run the Codex MCP server (stdio transport). - Serve, - /// [experimental] List configured MCP servers. List(ListArgs), @@ -47,6 +51,14 @@ pub enum McpSubcommand { /// [experimental] Remove a global MCP server entry. Remove(RemoveArgs), + + /// [experimental] Authenticate with a configured MCP server via OAuth. + /// Requires experimental_use_rmcp_client = true in config.toml. + Login(LoginArgs), + + /// [experimental] Remove stored OAuth credentials for a server. + /// Requires experimental_use_rmcp_client = true in config.toml. + Logout(LogoutArgs), } #[derive(Debug, clap::Parser)] @@ -71,13 +83,61 @@ pub struct AddArgs { /// Name for the MCP server configuration. pub name: String, - /// Environment variables to set when launching the server. - #[arg(long, value_parser = parse_env_pair, value_name = "KEY=VALUE")] - pub env: Vec<(String, String)>, + #[command(flatten)] + pub transport_args: AddMcpTransportArgs, +} +#[derive(Debug, clap::Args)] +#[command( + group( + ArgGroup::new("transport") + .args(["command", "url"]) + .required(true) + .multiple(false) + ) +)] +pub struct AddMcpTransportArgs { + #[command(flatten)] + pub stdio: Option, + + #[command(flatten)] + pub streamable_http: Option, +} + +#[derive(Debug, clap::Args)] +pub struct AddMcpStdioArgs { /// Command to launch the MCP server. - #[arg(trailing_var_arg = true, num_args = 1..)] + /// Use --url for a streamable HTTP server. + #[arg( + trailing_var_arg = true, + num_args = 0.., + )] pub command: Vec, + + /// Environment variables to set when launching the server. + /// Only valid with stdio servers. + #[arg( + long, + value_parser = parse_env_pair, + value_name = "KEY=VALUE", + )] + pub env: Vec<(String, String)>, +} + +#[derive(Debug, clap::Args)] +pub struct AddMcpStreamableHttpArgs { + /// URL for a streamable HTTP MCP server. + #[arg(long)] + pub url: String, + + /// Optional environment variable to read for a bearer token. + /// Only valid with streamable HTTP servers. + #[arg( + long = "bearer-token-env-var", + value_name = "ENV_VAR", + requires = "url" + )] + pub bearer_token_env_var: Option, } #[derive(Debug, clap::Parser)] @@ -86,29 +146,47 @@ pub struct RemoveArgs { pub name: String, } +#[derive(Debug, clap::Parser)] +pub struct LoginArgs { + /// Name of the MCP server to authenticate with oauth. + pub name: String, + + /// Comma-separated list of OAuth scopes to request. + #[arg(long, value_delimiter = ',', value_name = "SCOPE,SCOPE")] + pub scopes: Vec, +} + +#[derive(Debug, clap::Parser)] +pub struct LogoutArgs { + /// Name of the MCP server to deauthenticate. + pub name: String, +} + impl McpCli { - pub async fn run(self, codex_linux_sandbox_exe: Option) -> Result<()> { + pub async fn run(self) -> Result<()> { let McpCli { config_overrides, - cmd, + subcommand, } = self; - let subcommand = cmd.unwrap_or(McpSubcommand::Serve); match subcommand { - McpSubcommand::Serve => { - codex_mcp_server::run_main(codex_linux_sandbox_exe, config_overrides).await?; - } McpSubcommand::List(args) => { - run_list(&config_overrides, args)?; + run_list(&config_overrides, args).await?; } McpSubcommand::Get(args) => { - run_get(&config_overrides, args)?; + run_get(&config_overrides, args).await?; } McpSubcommand::Add(args) => { - run_add(&config_overrides, args)?; + run_add(&config_overrides, args).await?; } McpSubcommand::Remove(args) => { - run_remove(&config_overrides, args)?; + run_remove(&config_overrides, args).await?; + } + McpSubcommand::Login(args) => { + run_login(&config_overrides, args).await?; + } + McpSubcommand::Logout(args) => { + run_logout(&config_overrides, args).await?; } } @@ -116,53 +194,127 @@ impl McpCli { } } -fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Result<()> { +async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Result<()> { // Validate any provided overrides even though they are not currently applied. - config_overrides.parse_overrides().map_err(|e| anyhow!(e))?; + let overrides = config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; + let config = Config::load_with_cli_overrides(overrides, ConfigOverrides::default()) + .await + .context("failed to load configuration")?; - let AddArgs { name, env, command } = add_args; + let AddArgs { + name, + transport_args, + } = add_args; validate_server_name(&name)?; - let mut command_parts = command.into_iter(); - let command_bin = command_parts - .next() - .ok_or_else(|| anyhow!("command is required"))?; - let command_args: Vec = command_parts.collect(); - - let env_map = if env.is_empty() { - None - } else { - let mut map = HashMap::new(); - for (key, value) in env { - map.insert(key, value); - } - Some(map) - }; - let codex_home = find_codex_home().context("failed to resolve CODEX_HOME")?; let mut servers = load_global_mcp_servers(&codex_home) + .await .with_context(|| format!("failed to load MCP servers from {}", codex_home.display()))?; + let transport = match transport_args { + AddMcpTransportArgs { + stdio: Some(stdio), .. + } => { + let mut command_parts = stdio.command.into_iter(); + let command_bin = command_parts + .next() + .ok_or_else(|| anyhow!("command is required"))?; + let command_args: Vec = command_parts.collect(); + + let env_map = if stdio.env.is_empty() { + None + } else { + Some(stdio.env.into_iter().collect::>()) + }; + McpServerTransportConfig::Stdio { + command: command_bin, + args: command_args, + env: env_map, + env_vars: Vec::new(), + cwd: None, + } + } + AddMcpTransportArgs { + streamable_http: + Some(AddMcpStreamableHttpArgs { + url, + bearer_token_env_var, + }), + .. + } => McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers: None, + env_http_headers: None, + }, + AddMcpTransportArgs { .. } => bail!("exactly one of --command or --url must be provided"), + }; + let new_entry = McpServerConfig { - command: command_bin, - args: command_args, - env: env_map, - startup_timeout_ms: None, + transport: transport.clone(), + enabled: true, + startup_timeout_sec: None, + tool_timeout_sec: None, + enabled_tools: None, + disabled_tools: None, }; servers.insert(name.clone(), new_entry); - write_global_mcp_servers(&codex_home, &servers) + ConfigEditsBuilder::new(&codex_home) + .replace_mcp_servers(&servers) + .apply() + .await .with_context(|| format!("failed to write MCP servers to {}", codex_home.display()))?; println!("Added global MCP server '{name}'."); + if let McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var: None, + http_headers, + env_http_headers, + } = transport + { + match supports_oauth_login(&url).await { + Ok(true) => { + if !config.features.enabled(Feature::RmcpClient) { + println!( + "MCP server supports login. Add `experimental_use_rmcp_client = true` \ + to your config.toml and run `codex mcp login {name}` to login." + ); + } else { + println!("Detected OAuth support. Starting OAuth flow…"); + perform_oauth_login( + &name, + &url, + config.mcp_oauth_credentials_store_mode, + http_headers.clone(), + env_http_headers.clone(), + &Vec::new(), + ) + .await?; + println!("Successfully logged in."); + } + } + Ok(false) => {} + Err(_) => println!( + "MCP server may or may not require login. Run `codex mcp login {name}` to login." + ), + } + } + Ok(()) } -fn run_remove(config_overrides: &CliConfigOverrides, remove_args: RemoveArgs) -> Result<()> { - config_overrides.parse_overrides().map_err(|e| anyhow!(e))?; +async fn run_remove(config_overrides: &CliConfigOverrides, remove_args: RemoveArgs) -> Result<()> { + config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; let RemoveArgs { name } = remove_args; @@ -170,12 +322,16 @@ fn run_remove(config_overrides: &CliConfigOverrides, remove_args: RemoveArgs) -> let codex_home = find_codex_home().context("failed to resolve CODEX_HOME")?; let mut servers = load_global_mcp_servers(&codex_home) + .await .with_context(|| format!("failed to load MCP servers from {}", codex_home.display()))?; let removed = servers.remove(&name).is_some(); if removed { - write_global_mcp_servers(&codex_home, &servers) + ConfigEditsBuilder::new(&codex_home) + .replace_mcp_servers(&servers) + .apply() + .await .with_context(|| format!("failed to write MCP servers to {}", codex_home.display()))?; } @@ -188,29 +344,144 @@ fn run_remove(config_overrides: &CliConfigOverrides, remove_args: RemoveArgs) -> Ok(()) } -fn run_list(config_overrides: &CliConfigOverrides, list_args: ListArgs) -> Result<()> { - let overrides = config_overrides.parse_overrides().map_err(|e| anyhow!(e))?; +async fn run_login(config_overrides: &CliConfigOverrides, login_args: LoginArgs) -> Result<()> { + let overrides = config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; let config = Config::load_with_cli_overrides(overrides, ConfigOverrides::default()) + .await + .context("failed to load configuration")?; + + if !config.features.enabled(Feature::RmcpClient) { + bail!( + "OAuth login is only supported when [features].rmcp_client is true in config.toml. See https://github.com/openai/codex/blob/main/docs/config.md#feature-flags for details." + ); + } + + let LoginArgs { name, scopes } = login_args; + + let Some(server) = config.mcp_servers.get(&name) else { + bail!("No MCP server named '{name}' found."); + }; + + let (url, http_headers, env_http_headers) = match &server.transport { + McpServerTransportConfig::StreamableHttp { + url, + http_headers, + env_http_headers, + .. + } => (url.clone(), http_headers.clone(), env_http_headers.clone()), + _ => bail!("OAuth login is only supported for streamable HTTP servers."), + }; + + perform_oauth_login( + &name, + &url, + config.mcp_oauth_credentials_store_mode, + http_headers, + env_http_headers, + &scopes, + ) + .await?; + println!("Successfully logged in to MCP server '{name}'."); + Ok(()) +} + +async fn run_logout(config_overrides: &CliConfigOverrides, logout_args: LogoutArgs) -> Result<()> { + let overrides = config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; + let config = Config::load_with_cli_overrides(overrides, ConfigOverrides::default()) + .await + .context("failed to load configuration")?; + + let LogoutArgs { name } = logout_args; + + let server = config + .mcp_servers + .get(&name) + .ok_or_else(|| anyhow!("No MCP server named '{name}' found in configuration."))?; + + let url = match &server.transport { + McpServerTransportConfig::StreamableHttp { url, .. } => url.clone(), + _ => bail!("OAuth logout is only supported for streamable_http transports."), + }; + + match delete_oauth_tokens(&name, &url, config.mcp_oauth_credentials_store_mode) { + Ok(true) => println!("Removed OAuth credentials for '{name}'."), + Ok(false) => println!("No OAuth credentials stored for '{name}'."), + Err(err) => return Err(anyhow!("failed to delete OAuth credentials: {err}")), + } + + Ok(()) +} + +async fn run_list(config_overrides: &CliConfigOverrides, list_args: ListArgs) -> Result<()> { + let overrides = config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; + let config = Config::load_with_cli_overrides(overrides, ConfigOverrides::default()) + .await .context("failed to load configuration")?; let mut entries: Vec<_> = config.mcp_servers.iter().collect(); entries.sort_by(|(a, _), (b, _)| a.cmp(b)); + let auth_statuses = compute_auth_statuses( + config.mcp_servers.iter(), + config.mcp_oauth_credentials_store_mode, + ) + .await; if list_args.json { let json_entries: Vec<_> = entries .into_iter() .map(|(name, cfg)| { - let env = cfg.env.as_ref().map(|env| { - env.iter() - .map(|(k, v)| (k.clone(), v.clone())) - .collect::>() - }); + let auth_status = auth_statuses + .get(name.as_str()) + .map(|entry| entry.auth_status) + .unwrap_or(McpAuthStatus::Unsupported); + let transport = match &cfg.transport { + McpServerTransportConfig::Stdio { + command, + args, + env, + env_vars, + cwd, + } => serde_json::json!({ + "type": "stdio", + "command": command, + "args": args, + "env": env, + "env_vars": env_vars, + "cwd": cwd, + }), + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers, + env_http_headers, + } => { + serde_json::json!({ + "type": "streamable_http", + "url": url, + "bearer_token_env_var": bearer_token_env_var, + "http_headers": http_headers, + "env_http_headers": env_http_headers, + }) + } + }; + serde_json::json!({ "name": name, - "command": cfg.command, - "args": cfg.args, - "env": env, - "startup_timeout_ms": cfg.startup_timeout_ms, + "enabled": cfg.enabled, + "transport": transport, + "startup_timeout_sec": cfg + .startup_timeout_sec + .map(|timeout| timeout.as_secs_f64()), + "tool_timeout_sec": cfg + .tool_timeout_sec + .map(|timeout| timeout.as_secs_f64()), + "auth_status": auth_status, }) }) .collect(); @@ -224,70 +495,190 @@ fn run_list(config_overrides: &CliConfigOverrides, list_args: ListArgs) -> Resul return Ok(()); } - let mut rows: Vec<[String; 4]> = Vec::new(); + let mut stdio_rows: Vec<[String; 7]> = Vec::new(); + let mut http_rows: Vec<[String; 5]> = Vec::new(); + for (name, cfg) in entries { - let args = if cfg.args.is_empty() { - "-".to_string() - } else { - cfg.args.join(" ") - }; - - let env = match cfg.env.as_ref() { - None => "-".to_string(), - Some(map) if map.is_empty() => "-".to_string(), - Some(map) => { - let mut pairs: Vec<_> = map.iter().collect(); - pairs.sort_by(|(a, _), (b, _)| a.cmp(b)); - pairs - .into_iter() - .map(|(k, v)| format!("{k}={v}")) - .collect::>() - .join(", ") + match &cfg.transport { + McpServerTransportConfig::Stdio { + command, + args, + env, + env_vars, + cwd, + } => { + let args_display = if args.is_empty() { + "-".to_string() + } else { + args.join(" ") + }; + let env_display = format_env_display(env.as_ref(), env_vars); + let cwd_display = cwd + .as_ref() + .map(|path| path.display().to_string()) + .filter(|value| !value.is_empty()) + .unwrap_or_else(|| "-".to_string()); + let status = if cfg.enabled { + "enabled".to_string() + } else { + "disabled".to_string() + }; + let auth_status = auth_statuses + .get(name.as_str()) + .map(|entry| entry.auth_status) + .unwrap_or(McpAuthStatus::Unsupported) + .to_string(); + stdio_rows.push([ + name.clone(), + command.clone(), + args_display, + env_display, + cwd_display, + status, + auth_status, + ]); + } + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + .. + } => { + let status = if cfg.enabled { + "enabled".to_string() + } else { + "disabled".to_string() + }; + let auth_status = auth_statuses + .get(name.as_str()) + .map(|entry| entry.auth_status) + .unwrap_or(McpAuthStatus::Unsupported) + .to_string(); + let bearer_token_display = + bearer_token_env_var.as_deref().unwrap_or("-").to_string(); + http_rows.push([ + name.clone(), + url.clone(), + bearer_token_display, + status, + auth_status, + ]); } - }; - - rows.push([name.clone(), cfg.command.clone(), args, env]); - } - - let mut widths = ["Name".len(), "Command".len(), "Args".len(), "Env".len()]; - for row in &rows { - for (i, cell) in row.iter().enumerate() { - widths[i] = widths[i].max(cell.len()); } } - println!( - "{: Result<()> { - let overrides = config_overrides.parse_overrides().map_err(|e| anyhow!(e))?; +async fn run_get(config_overrides: &CliConfigOverrides, get_args: GetArgs) -> Result<()> { + let overrides = config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; let config = Config::load_with_cli_overrides(overrides, ConfigOverrides::default()) + .await .context("failed to load configuration")?; let Some(server) = config.mcp_servers.get(&get_args.name) else { @@ -295,46 +686,141 @@ fn run_get(config_overrides: &CliConfigOverrides, get_args: GetArgs) -> Result<( }; if get_args.json { - let env = server.env.as_ref().map(|env| { - env.iter() - .map(|(k, v)| (k.clone(), v.clone())) - .collect::>() - }); + let transport = match &server.transport { + McpServerTransportConfig::Stdio { + command, + args, + env, + env_vars, + cwd, + } => serde_json::json!({ + "type": "stdio", + "command": command, + "args": args, + "env": env, + "env_vars": env_vars, + "cwd": cwd, + }), + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers, + env_http_headers, + } => serde_json::json!({ + "type": "streamable_http", + "url": url, + "bearer_token_env_var": bearer_token_env_var, + "http_headers": http_headers, + "env_http_headers": env_http_headers, + }), + }; let output = serde_json::to_string_pretty(&serde_json::json!({ "name": get_args.name, - "command": server.command, - "args": server.args, - "env": env, - "startup_timeout_ms": server.startup_timeout_ms, + "enabled": server.enabled, + "transport": transport, + "enabled_tools": server.enabled_tools.clone(), + "disabled_tools": server.disabled_tools.clone(), + "startup_timeout_sec": server + .startup_timeout_sec + .map(|timeout| timeout.as_secs_f64()), + "tool_timeout_sec": server + .tool_timeout_sec + .map(|timeout| timeout.as_secs_f64()), }))?; println!("{output}"); return Ok(()); } + if !server.enabled { + println!("{} (disabled)", get_args.name); + return Ok(()); + } + println!("{}", get_args.name); - println!(" command: {}", server.command); - let args = if server.args.is_empty() { - "-".to_string() - } else { - server.args.join(" ") - }; - println!(" args: {args}"); - let env_display = match server.env.as_ref() { - None => "-".to_string(), - Some(map) if map.is_empty() => "-".to_string(), - Some(map) => { - let mut pairs: Vec<_> = map.iter().collect(); - pairs.sort_by(|(a, _), (b, _)| a.cmp(b)); - pairs - .into_iter() - .map(|(k, v)| format!("{k}={v}")) - .collect::>() - .join(", ") + println!(" enabled: {}", server.enabled); + let format_tool_list = |tools: &Option>| -> String { + match tools { + Some(list) if list.is_empty() => "[]".to_string(), + Some(list) => list.join(", "), + None => "-".to_string(), } }; - println!(" env: {env_display}"); - if let Some(timeout) = server.startup_timeout_ms { - println!(" startup_timeout_ms: {timeout}"); + if server.enabled_tools.is_some() { + let enabled_tools_display = format_tool_list(&server.enabled_tools); + println!(" enabled_tools: {enabled_tools_display}"); + } + if server.disabled_tools.is_some() { + let disabled_tools_display = format_tool_list(&server.disabled_tools); + println!(" disabled_tools: {disabled_tools_display}"); + } + match &server.transport { + McpServerTransportConfig::Stdio { + command, + args, + env, + env_vars, + cwd, + } => { + println!(" transport: stdio"); + println!(" command: {command}"); + let args_display = if args.is_empty() { + "-".to_string() + } else { + args.join(" ") + }; + println!(" args: {args_display}"); + let cwd_display = cwd + .as_ref() + .map(|path| path.display().to_string()) + .filter(|value| !value.is_empty()) + .unwrap_or_else(|| "-".to_string()); + println!(" cwd: {cwd_display}"); + let env_display = format_env_display(env.as_ref(), env_vars); + println!(" env: {env_display}"); + } + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers, + env_http_headers, + } => { + println!(" transport: streamable_http"); + println!(" url: {url}"); + let bearer_token_display = bearer_token_env_var.as_deref().unwrap_or("-"); + println!(" bearer_token_env_var: {bearer_token_display}"); + let headers_display = match http_headers { + Some(map) if !map.is_empty() => { + let mut pairs: Vec<_> = map.iter().collect(); + pairs.sort_by(|(a, _), (b, _)| a.cmp(b)); + pairs + .into_iter() + .map(|(k, _)| format!("{k}=*****")) + .collect::>() + .join(", ") + } + _ => "-".to_string(), + }; + println!(" http_headers: {headers_display}"); + let env_headers_display = match env_http_headers { + Some(map) if !map.is_empty() => { + let mut pairs: Vec<_> = map.iter().collect(); + pairs.sort_by(|(a, _), (b, _)| a.cmp(b)); + pairs + .into_iter() + .map(|(k, var)| format!("{k}={var}")) + .collect::>() + .join(", ") + } + _ => "-".to_string(), + }; + println!(" env_http_headers: {env_headers_display}"); + } + } + if let Some(timeout) = server.startup_timeout_sec { + println!(" startup_timeout_sec: {}", timeout.as_secs_f64()); + } + if let Some(timeout) = server.tool_timeout_sec { + println!(" tool_timeout_sec: {}", timeout.as_secs_f64()); } println!(" remove: codex mcp remove {}", get_args.name); diff --git a/codex-rs/cli/src/proto.rs b/codex-rs/cli/src/proto.rs deleted file mode 100644 index 623edca5a8..0000000000 --- a/codex-rs/cli/src/proto.rs +++ /dev/null @@ -1,133 +0,0 @@ -use std::io::IsTerminal; - -use clap::Parser; -use codex_common::CliConfigOverrides; -use codex_core::AuthManager; -use codex_core::ConversationManager; -use codex_core::NewConversation; -use codex_core::config::Config; -use codex_core::config::ConfigOverrides; -use codex_core::protocol::Event; -use codex_core::protocol::EventMsg; -use codex_core::protocol::Submission; -use tokio::io::AsyncBufReadExt; -use tokio::io::BufReader; -use tracing::error; -use tracing::info; - -#[derive(Debug, Parser)] -pub struct ProtoCli { - #[clap(skip)] - pub config_overrides: CliConfigOverrides, -} - -pub async fn run_main(opts: ProtoCli) -> anyhow::Result<()> { - if std::io::stdin().is_terminal() { - anyhow::bail!("Protocol mode expects stdin to be a pipe, not a terminal"); - } - - tracing_subscriber::fmt() - .with_writer(std::io::stderr) - .init(); - - let ProtoCli { config_overrides } = opts; - let overrides_vec = config_overrides - .parse_overrides() - .map_err(anyhow::Error::msg)?; - - let config = Config::load_with_cli_overrides(overrides_vec, ConfigOverrides::default())?; - // Use conversation_manager API to start a conversation - let conversation_manager = - ConversationManager::new(AuthManager::shared(config.codex_home.clone())); - let NewConversation { - conversation_id: _, - conversation, - session_configured, - } = conversation_manager.new_conversation(config).await?; - - // Simulate streaming the session_configured event. - let synthetic_event = Event { - // Fake id value. - id: "".to_string(), - msg: EventMsg::SessionConfigured(session_configured), - }; - let session_configured_event = match serde_json::to_string(&synthetic_event) { - Ok(s) => s, - Err(e) => { - error!("Failed to serialize session_configured: {e}"); - return Err(anyhow::Error::from(e)); - } - }; - println!("{session_configured_event}"); - - // Task that reads JSON lines from stdin and forwards to Submission Queue - let sq_fut = { - let conversation = conversation.clone(); - async move { - let stdin = BufReader::new(tokio::io::stdin()); - let mut lines = stdin.lines(); - loop { - let result = tokio::select! { - _ = tokio::signal::ctrl_c() => { - break - }, - res = lines.next_line() => res, - }; - - match result { - Ok(Some(line)) => { - let line = line.trim(); - if line.is_empty() { - continue; - } - match serde_json::from_str::(line) { - Ok(sub) => { - if let Err(e) = conversation.submit_with_id(sub).await { - error!("{e:#}"); - break; - } - } - Err(e) => { - error!("invalid submission: {e}"); - } - } - } - _ => { - info!("Submission queue closed"); - break; - } - } - } - } - }; - - // Task that reads events from the agent and prints them as JSON lines to stdout - let eq_fut = async move { - loop { - let event = tokio::select! { - _ = tokio::signal::ctrl_c() => break, - event = conversation.next_event() => event, - }; - match event { - Ok(event) => { - let event_str = match serde_json::to_string(&event) { - Ok(s) => s, - Err(e) => { - error!("Failed to serialize event: {e}"); - continue; - } - }; - println!("{event_str}"); - } - Err(e) => { - error!("{e:#}"); - break; - } - } - } - info!("Event queue closed"); - }; - - tokio::join!(sq_fut, eq_fut); - Ok(()) -} diff --git a/codex-rs/cli/src/wsl_paths.rs b/codex-rs/cli/src/wsl_paths.rs new file mode 100644 index 0000000000..56ce8668cf --- /dev/null +++ b/codex-rs/cli/src/wsl_paths.rs @@ -0,0 +1,76 @@ +use std::ffi::OsStr; + +/// WSL-specific path helpers used by the updater logic. +/// +/// See https://github.com/openai/codex/issues/6086. +pub fn is_wsl() -> bool { + #[cfg(target_os = "linux")] + { + if std::env::var_os("WSL_DISTRO_NAME").is_some() { + return true; + } + match std::fs::read_to_string("/proc/version") { + Ok(version) => version.to_lowercase().contains("microsoft"), + Err(_) => false, + } + } + #[cfg(not(target_os = "linux"))] + { + false + } +} + +/// Convert a Windows absolute path (`C:\foo\bar` or `C:/foo/bar`) to a WSL mount path (`/mnt/c/foo/bar`). +/// Returns `None` if the input does not look like a Windows drive path. +pub fn win_path_to_wsl(path: &str) -> Option { + let bytes = path.as_bytes(); + if bytes.len() < 3 + || bytes[1] != b':' + || !(bytes[2] == b'\\' || bytes[2] == b'/') + || !bytes[0].is_ascii_alphabetic() + { + return None; + } + let drive = (bytes[0] as char).to_ascii_lowercase(); + let tail = path[3..].replace('\\', "/"); + if tail.is_empty() { + return Some(format!("/mnt/{drive}")); + } + Some(format!("/mnt/{drive}/{tail}")) +} + +/// If under WSL and given a Windows-style path, return the equivalent `/mnt//…` path. +/// Otherwise returns the input unchanged. +pub fn normalize_for_wsl>(path: P) -> String { + let value = path.as_ref().to_string_lossy().to_string(); + if !is_wsl() { + return value; + } + if let Some(mapped) = win_path_to_wsl(&value) { + return mapped; + } + value +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn win_to_wsl_basic() { + assert_eq!( + win_path_to_wsl(r"C:\Temp\codex.zip").as_deref(), + Some("/mnt/c/Temp/codex.zip") + ); + assert_eq!( + win_path_to_wsl("D:/Work/codex.tgz").as_deref(), + Some("/mnt/d/Work/codex.tgz") + ); + assert!(win_path_to_wsl("/home/user/codex").is_none()); + } + + #[test] + fn normalize_is_noop_on_unix_paths() { + assert_eq!(normalize_for_wsl("/home/u/x"), "/home/u/x"); + } +} diff --git a/codex-rs/cli/tests/mcp_add_remove.rs b/codex-rs/cli/tests/mcp_add_remove.rs index 9e54f0d867..2911637331 100644 --- a/codex-rs/cli/tests/mcp_add_remove.rs +++ b/codex-rs/cli/tests/mcp_add_remove.rs @@ -2,6 +2,7 @@ use std::path::Path; use anyhow::Result; use codex_core::config::load_global_mcp_servers; +use codex_core::config::types::McpServerTransportConfig; use predicates::str::contains; use pretty_assertions::assert_eq; use tempfile::TempDir; @@ -12,8 +13,8 @@ fn codex_command(codex_home: &Path) -> Result { Ok(cmd) } -#[test] -fn add_and_remove_server_updates_global_config() -> Result<()> { +#[tokio::test] +async fn add_and_remove_server_updates_global_config() -> Result<()> { let codex_home = TempDir::new()?; let mut add_cmd = codex_command(codex_home.path())?; @@ -23,12 +24,26 @@ fn add_and_remove_server_updates_global_config() -> Result<()> { .success() .stdout(contains("Added global MCP server 'docs'.")); - let servers = load_global_mcp_servers(codex_home.path())?; + let servers = load_global_mcp_servers(codex_home.path()).await?; assert_eq!(servers.len(), 1); let docs = servers.get("docs").expect("server should exist"); - assert_eq!(docs.command, "echo"); - assert_eq!(docs.args, vec!["hello".to_string()]); - assert!(docs.env.is_none()); + match &docs.transport { + McpServerTransportConfig::Stdio { + command, + args, + env, + env_vars, + cwd, + } => { + assert_eq!(command, "echo"); + assert_eq!(args, &vec!["hello".to_string()]); + assert!(env.is_none()); + assert!(env_vars.is_empty()); + assert!(cwd.is_none()); + } + other => panic!("unexpected transport: {other:?}"), + } + assert!(docs.enabled); let mut remove_cmd = codex_command(codex_home.path())?; remove_cmd @@ -37,7 +52,7 @@ fn add_and_remove_server_updates_global_config() -> Result<()> { .success() .stdout(contains("Removed global MCP server 'docs'.")); - let servers = load_global_mcp_servers(codex_home.path())?; + let servers = load_global_mcp_servers(codex_home.path()).await?; assert!(servers.is_empty()); let mut remove_again_cmd = codex_command(codex_home.path())?; @@ -47,14 +62,14 @@ fn add_and_remove_server_updates_global_config() -> Result<()> { .success() .stdout(contains("No MCP server named 'docs' found.")); - let servers = load_global_mcp_servers(codex_home.path())?; + let servers = load_global_mcp_servers(codex_home.path()).await?; assert!(servers.is_empty()); Ok(()) } -#[test] -fn add_with_env_preserves_key_order_and_values() -> Result<()> { +#[tokio::test] +async fn add_with_env_preserves_key_order_and_values() -> Result<()> { let codex_home = TempDir::new()?; let mut add_cmd = codex_command(codex_home.path())?; @@ -74,13 +89,140 @@ fn add_with_env_preserves_key_order_and_values() -> Result<()> { .assert() .success(); - let servers = load_global_mcp_servers(codex_home.path())?; + let servers = load_global_mcp_servers(codex_home.path()).await?; let envy = servers.get("envy").expect("server should exist"); - let env = envy.env.as_ref().expect("env should be present"); + let env = match &envy.transport { + McpServerTransportConfig::Stdio { env: Some(env), .. } => env, + other => panic!("unexpected transport: {other:?}"), + }; assert_eq!(env.len(), 2); assert_eq!(env.get("FOO"), Some(&"bar".to_string())); assert_eq!(env.get("ALPHA"), Some(&"beta".to_string())); + assert!(envy.enabled); + + Ok(()) +} + +#[tokio::test] +async fn add_streamable_http_without_manual_token() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut add_cmd = codex_command(codex_home.path())?; + add_cmd + .args(["mcp", "add", "github", "--url", "https://example.com/mcp"]) + .assert() + .success(); + + let servers = load_global_mcp_servers(codex_home.path()).await?; + let github = servers.get("github").expect("github server should exist"); + match &github.transport { + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers, + env_http_headers, + } => { + assert_eq!(url, "https://example.com/mcp"); + assert!(bearer_token_env_var.is_none()); + assert!(http_headers.is_none()); + assert!(env_http_headers.is_none()); + } + other => panic!("unexpected transport: {other:?}"), + } + assert!(github.enabled); + + assert!(!codex_home.path().join(".credentials.json").exists()); + assert!(!codex_home.path().join(".env").exists()); + + Ok(()) +} + +#[tokio::test] +async fn add_streamable_http_with_custom_env_var() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut add_cmd = codex_command(codex_home.path())?; + add_cmd + .args([ + "mcp", + "add", + "issues", + "--url", + "https://example.com/issues", + "--bearer-token-env-var", + "GITHUB_TOKEN", + ]) + .assert() + .success(); + + let servers = load_global_mcp_servers(codex_home.path()).await?; + let issues = servers.get("issues").expect("issues server should exist"); + match &issues.transport { + McpServerTransportConfig::StreamableHttp { + url, + bearer_token_env_var, + http_headers, + env_http_headers, + } => { + assert_eq!(url, "https://example.com/issues"); + assert_eq!(bearer_token_env_var.as_deref(), Some("GITHUB_TOKEN")); + assert!(http_headers.is_none()); + assert!(env_http_headers.is_none()); + } + other => panic!("unexpected transport: {other:?}"), + } + assert!(issues.enabled); + Ok(()) +} + +#[tokio::test] +async fn add_streamable_http_rejects_removed_flag() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut add_cmd = codex_command(codex_home.path())?; + add_cmd + .args([ + "mcp", + "add", + "github", + "--url", + "https://example.com/mcp", + "--with-bearer-token", + ]) + .assert() + .failure() + .stderr(contains("--with-bearer-token")); + + let servers = load_global_mcp_servers(codex_home.path()).await?; + assert!(servers.is_empty()); + + Ok(()) +} + +#[tokio::test] +async fn add_cant_add_command_and_url() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut add_cmd = codex_command(codex_home.path())?; + add_cmd + .args([ + "mcp", + "add", + "github", + "--url", + "https://example.com/mcp", + "--command", + "--", + "echo", + "hello", + ]) + .assert() + .failure() + .stderr(contains("unexpected argument '--command' found")); + + let servers = load_global_mcp_servers(codex_home.path()).await?; + assert!(servers.is_empty()); Ok(()) } diff --git a/codex-rs/cli/tests/mcp_list.rs b/codex-rs/cli/tests/mcp_list.rs index e53f42cc8f..1492365afb 100644 --- a/codex-rs/cli/tests/mcp_list.rs +++ b/codex-rs/cli/tests/mcp_list.rs @@ -1,9 +1,14 @@ use std::path::Path; use anyhow::Result; +use codex_core::config::edit::ConfigEditsBuilder; +use codex_core::config::load_global_mcp_servers; +use codex_core::config::types::McpServerTransportConfig; +use predicates::prelude::PredicateBooleanExt; use predicates::str::contains; use pretty_assertions::assert_eq; use serde_json::Value as JsonValue; +use serde_json::json; use tempfile::TempDir; fn codex_command(codex_home: &Path) -> Result { @@ -25,8 +30,8 @@ fn list_shows_empty_state() -> Result<()> { Ok(()) } -#[test] -fn list_and_get_render_expected_output() -> Result<()> { +#[tokio::test] +async fn list_and_get_render_expected_output() -> Result<()> { let codex_home = TempDir::new()?; let mut add = codex_command(codex_home.path())?; @@ -44,6 +49,20 @@ fn list_and_get_render_expected_output() -> Result<()> { .assert() .success(); + let mut servers = load_global_mcp_servers(codex_home.path()).await?; + let docs_entry = servers + .get_mut("docs") + .expect("docs server should exist after add"); + match &mut docs_entry.transport { + McpServerTransportConfig::Stdio { env_vars, .. } => { + *env_vars = vec!["APP_TOKEN".to_string(), "WORKSPACE_ID".to_string()]; + } + other => panic!("unexpected transport: {other:?}"), + } + ConfigEditsBuilder::new(codex_home.path()) + .replace_mcp_servers(&servers) + .apply_blocking()?; + let mut list_cmd = codex_command(codex_home.path())?; let list_output = list_cmd.args(["mcp", "list"]).output()?; assert!(list_output.status.success()); @@ -51,48 +70,61 @@ fn list_and_get_render_expected_output() -> Result<()> { assert!(stdout.contains("Name")); assert!(stdout.contains("docs")); assert!(stdout.contains("docs-server")); - assert!(stdout.contains("TOKEN=secret")); + assert!(stdout.contains("TOKEN=*****")); + assert!(stdout.contains("APP_TOKEN=*****")); + assert!(stdout.contains("WORKSPACE_ID=*****")); + assert!(stdout.contains("Status")); + assert!(stdout.contains("Auth")); + assert!(stdout.contains("enabled")); + assert!(stdout.contains("Unsupported")); let mut list_json_cmd = codex_command(codex_home.path())?; let json_output = list_json_cmd.args(["mcp", "list", "--json"]).output()?; assert!(json_output.status.success()); let stdout = String::from_utf8(json_output.stdout)?; let parsed: JsonValue = serde_json::from_str(&stdout)?; - let array = parsed.as_array().expect("expected array"); - assert_eq!(array.len(), 1); - let entry = &array[0]; - assert_eq!(entry.get("name"), Some(&JsonValue::String("docs".into()))); assert_eq!( - entry.get("command"), - Some(&JsonValue::String("docs-server".into())) - ); - - let args = entry - .get("args") - .and_then(|v| v.as_array()) - .expect("args array"); - assert_eq!( - args, - &vec![ - JsonValue::String("--port".into()), - JsonValue::String("4000".into()) + parsed, + json!([ + { + "name": "docs", + "enabled": true, + "transport": { + "type": "stdio", + "command": "docs-server", + "args": [ + "--port", + "4000" + ], + "env": { + "TOKEN": "secret" + }, + "env_vars": [ + "APP_TOKEN", + "WORKSPACE_ID" + ], + "cwd": null + }, + "startup_timeout_sec": null, + "tool_timeout_sec": null, + "auth_status": "unsupported" + } ] + ) ); - let env = entry - .get("env") - .and_then(|v| v.as_object()) - .expect("env map"); - assert_eq!(env.get("TOKEN"), Some(&JsonValue::String("secret".into()))); - let mut get_cmd = codex_command(codex_home.path())?; let get_output = get_cmd.args(["mcp", "get", "docs"]).output()?; assert!(get_output.status.success()); let stdout = String::from_utf8(get_output.stdout)?; assert!(stdout.contains("docs")); + assert!(stdout.contains("transport: stdio")); assert!(stdout.contains("command: docs-server")); assert!(stdout.contains("args: --port 4000")); - assert!(stdout.contains("env: TOKEN=secret")); + assert!(stdout.contains("env: TOKEN=*****")); + assert!(stdout.contains("APP_TOKEN=*****")); + assert!(stdout.contains("WORKSPACE_ID=*****")); + assert!(stdout.contains("enabled: true")); assert!(stdout.contains("remove: codex mcp remove docs")); let mut get_json_cmd = codex_command(codex_home.path())?; @@ -100,7 +132,34 @@ fn list_and_get_render_expected_output() -> Result<()> { .args(["mcp", "get", "docs", "--json"]) .assert() .success() - .stdout(contains("\"name\": \"docs\"")); + .stdout(contains("\"name\": \"docs\"").and(contains("\"enabled\": true"))); + + Ok(()) +} + +#[tokio::test] +async fn get_disabled_server_shows_single_line() -> Result<()> { + let codex_home = TempDir::new()?; + + let mut add = codex_command(codex_home.path())?; + add.args(["mcp", "add", "docs", "--", "docs-server"]) + .assert() + .success(); + + let mut servers = load_global_mcp_servers(codex_home.path()).await?; + let docs = servers + .get_mut("docs") + .expect("docs server should exist after add"); + docs.enabled = false; + ConfigEditsBuilder::new(codex_home.path()) + .replace_mcp_servers(&servers) + .apply_blocking()?; + + let mut get_cmd = codex_command(codex_home.path())?; + let get_output = get_cmd.args(["mcp", "get", "docs"]).output()?; + assert!(get_output.status.success()); + let stdout = String::from_utf8(get_output.stdout)?; + assert_eq!(stdout.trim_end(), "docs (disabled)"); Ok(()) } diff --git a/codex-rs/clippy.toml b/codex-rs/clippy.toml index 5a6ff7f052..a2aae6591b 100644 --- a/codex-rs/clippy.toml +++ b/codex-rs/clippy.toml @@ -7,3 +7,7 @@ disallowed-methods = [ { path = "ratatui::style::Stylize::black", reason = "Avoid hardcoding black; prefer default fg or dim/bold. Exception: Disable this rule if rendering over a hardcoded ANSI background." }, { path = "ratatui::style::Stylize::yellow", reason = "Avoid yellow; prefer other colors in `tui/styles.md`." }, ] + +# Increase the size threshold for result_large_err to accommodate +# richer error variants. +large-error-threshold = 256 diff --git a/codex-rs/cloud-tasks-client/Cargo.toml b/codex-rs/cloud-tasks-client/Cargo.toml new file mode 100644 index 0000000000..1a4eaa7aa8 --- /dev/null +++ b/codex-rs/cloud-tasks-client/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "codex-cloud-tasks-client" +version = { workspace = true } +edition = "2024" + +[lib] +name = "codex_cloud_tasks_client" +path = "src/lib.rs" + +[lints] +workspace = true + +[features] +default = ["online"] +online = ["dep:codex-backend-client"] +mock = [] + +[dependencies] +anyhow = "1" +async-trait = "0.1" +chrono = { version = "0.4", features = ["serde"] } +diffy = "0.4.2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +thiserror = "2.0.17" +codex-backend-client = { path = "../backend-client", optional = true } +codex-git = { workspace = true } diff --git a/codex-rs/cloud-tasks-client/src/api.rs b/codex-rs/cloud-tasks-client/src/api.rs new file mode 100644 index 0000000000..4bd12939e8 --- /dev/null +++ b/codex-rs/cloud-tasks-client/src/api.rs @@ -0,0 +1,158 @@ +use chrono::DateTime; +use chrono::Utc; +use serde::Deserialize; +use serde::Serialize; + +pub type Result = std::result::Result; + +#[derive(Debug, thiserror::Error)] +pub enum CloudTaskError { + #[error("unimplemented: {0}")] + Unimplemented(&'static str), + #[error("http error: {0}")] + Http(String), + #[error("io error: {0}")] + Io(String), + #[error("{0}")] + Msg(String), +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct TaskId(pub String); + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum TaskStatus { + Pending, + Ready, + Applied, + Error, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct TaskSummary { + pub id: TaskId, + pub title: String, + pub status: TaskStatus, + pub updated_at: DateTime, + /// Backend environment identifier (when available) + pub environment_id: Option, + /// Human-friendly environment label (when available) + pub environment_label: Option, + pub summary: DiffSummary, + /// True when the backend reports this task as a code review. + #[serde(default)] + pub is_review: bool, + /// Number of assistant attempts (best-of-N), when reported by the backend. + #[serde(default)] + pub attempt_total: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub enum AttemptStatus { + Pending, + InProgress, + Completed, + Failed, + Cancelled, + #[default] + Unknown, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TurnAttempt { + pub turn_id: String, + pub attempt_placement: Option, + pub created_at: Option>, + pub status: AttemptStatus, + pub diff: Option, + pub messages: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ApplyStatus { + Success, + Partial, + Error, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ApplyOutcome { + pub applied: bool, + pub status: ApplyStatus, + pub message: String, + #[serde(default)] + pub skipped_paths: Vec, + #[serde(default)] + pub conflict_paths: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct CreatedTask { + pub id: TaskId, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub struct DiffSummary { + pub files_changed: usize, + pub lines_added: usize, + pub lines_removed: usize, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TaskText { + pub prompt: Option, + pub messages: Vec, + pub turn_id: Option, + pub sibling_turn_ids: Vec, + pub attempt_placement: Option, + pub attempt_status: AttemptStatus, +} + +impl Default for TaskText { + fn default() -> Self { + Self { + prompt: None, + messages: Vec::new(), + turn_id: None, + sibling_turn_ids: Vec::new(), + attempt_placement: None, + attempt_status: AttemptStatus::Unknown, + } + } +} + +#[async_trait::async_trait] +pub trait CloudBackend: Send + Sync { + async fn list_tasks(&self, env: Option<&str>) -> Result>; + async fn get_task_diff(&self, id: TaskId) -> Result>; + /// Return assistant output messages (no diff) when available. + async fn get_task_messages(&self, id: TaskId) -> Result>; + /// Return the creating prompt and assistant messages (when available). + async fn get_task_text(&self, id: TaskId) -> Result; + /// Return any sibling attempts (best-of-N) for the given assistant turn. + async fn list_sibling_attempts( + &self, + task: TaskId, + turn_id: String, + ) -> Result>; + /// Dry-run apply (preflight) that validates whether the patch would apply cleanly. + /// Never modifies the working tree. When `diff_override` is supplied, the provided diff is + /// used instead of re-fetching the task details so callers can apply alternate attempts. + async fn apply_task_preflight( + &self, + id: TaskId, + diff_override: Option, + ) -> Result; + async fn apply_task(&self, id: TaskId, diff_override: Option) -> Result; + async fn create_task( + &self, + env_id: &str, + prompt: &str, + git_ref: &str, + qa_mode: bool, + best_of_n: usize, + ) -> Result; +} diff --git a/codex-rs/cloud-tasks-client/src/http.rs b/codex-rs/cloud-tasks-client/src/http.rs new file mode 100644 index 0000000000..57d39b7bda --- /dev/null +++ b/codex-rs/cloud-tasks-client/src/http.rs @@ -0,0 +1,769 @@ +use crate::ApplyOutcome; +use crate::ApplyStatus; +use crate::AttemptStatus; +use crate::CloudBackend; +use crate::CloudTaskError; +use crate::DiffSummary; +use crate::Result; +use crate::TaskId; +use crate::TaskStatus; +use crate::TaskSummary; +use crate::TurnAttempt; +use crate::api::TaskText; +use chrono::DateTime; +use chrono::Utc; + +use codex_backend_client as backend; +use codex_backend_client::CodeTaskDetailsResponseExt; + +#[derive(Clone)] +pub struct HttpClient { + pub base_url: String, + backend: backend::Client, +} + +impl HttpClient { + pub fn new(base_url: impl Into) -> anyhow::Result { + let base_url = base_url.into(); + let backend = backend::Client::new(base_url.clone())?; + Ok(Self { base_url, backend }) + } + + pub fn with_bearer_token(mut self, token: impl Into) -> Self { + self.backend = self.backend.clone().with_bearer_token(token); + self + } + + pub fn with_user_agent(mut self, ua: impl Into) -> Self { + self.backend = self.backend.clone().with_user_agent(ua); + self + } + + pub fn with_chatgpt_account_id(mut self, account_id: impl Into) -> Self { + self.backend = self.backend.clone().with_chatgpt_account_id(account_id); + self + } + + fn tasks_api(&self) -> api::Tasks<'_> { + api::Tasks::new(self) + } + + fn attempts_api(&self) -> api::Attempts<'_> { + api::Attempts::new(self) + } + + fn apply_api(&self) -> api::Apply<'_> { + api::Apply::new(self) + } +} + +#[async_trait::async_trait] +impl CloudBackend for HttpClient { + async fn list_tasks(&self, env: Option<&str>) -> Result> { + self.tasks_api().list(env).await + } + + async fn get_task_diff(&self, id: TaskId) -> Result> { + self.tasks_api().diff(id).await + } + + async fn get_task_messages(&self, id: TaskId) -> Result> { + self.tasks_api().messages(id).await + } + + async fn get_task_text(&self, id: TaskId) -> Result { + self.tasks_api().task_text(id).await + } + + async fn list_sibling_attempts( + &self, + task: TaskId, + turn_id: String, + ) -> Result> { + self.attempts_api().list(task, turn_id).await + } + + async fn apply_task(&self, id: TaskId, diff_override: Option) -> Result { + self.apply_api().run(id, diff_override, false).await + } + + async fn apply_task_preflight( + &self, + id: TaskId, + diff_override: Option, + ) -> Result { + self.apply_api().run(id, diff_override, true).await + } + + async fn create_task( + &self, + env_id: &str, + prompt: &str, + git_ref: &str, + qa_mode: bool, + best_of_n: usize, + ) -> Result { + self.tasks_api() + .create(env_id, prompt, git_ref, qa_mode, best_of_n) + .await + } +} + +mod api { + use super::*; + use serde_json::Value; + use std::cmp::Ordering; + use std::collections::HashMap; + + pub(crate) struct Tasks<'a> { + base_url: &'a str, + backend: &'a backend::Client, + } + + impl<'a> Tasks<'a> { + pub(crate) fn new(client: &'a HttpClient) -> Self { + Self { + base_url: &client.base_url, + backend: &client.backend, + } + } + + pub(crate) async fn list(&self, env: Option<&str>) -> Result> { + let resp = self + .backend + .list_tasks(Some(20), Some("current"), env) + .await + .map_err(|e| CloudTaskError::Http(format!("list_tasks failed: {e}")))?; + + let tasks: Vec = resp + .items + .into_iter() + .map(map_task_list_item_to_summary) + .collect(); + + append_error_log(&format!( + "http.list_tasks: env={} items={}", + env.unwrap_or(""), + tasks.len() + )); + Ok(tasks) + } + + pub(crate) async fn diff(&self, id: TaskId) -> Result> { + let (details, body, ct) = self + .details_with_body(&id.0) + .await + .map_err(|e| CloudTaskError::Http(format!("get_task_details failed: {e}")))?; + if let Some(diff) = details.unified_diff() { + return Ok(Some(diff)); + } + let _ = (body, ct); + Ok(None) + } + + pub(crate) async fn messages(&self, id: TaskId) -> Result> { + let (details, body, ct) = self + .details_with_body(&id.0) + .await + .map_err(|e| CloudTaskError::Http(format!("get_task_details failed: {e}")))?; + + let mut msgs = details.assistant_text_messages(); + if msgs.is_empty() { + msgs.extend(extract_assistant_messages_from_body(&body)); + } + if !msgs.is_empty() { + return Ok(msgs); + } + if let Some(err) = details.assistant_error_message() { + return Ok(vec![format!("Task failed: {err}")]); + } + + let url = match details_path(self.base_url, &id.0) { + Some(url) => url, + None => format!("{}/api/codex/tasks/{}", self.base_url, id.0), + }; + Err(CloudTaskError::Http(format!( + "No assistant text messages in response. GET {url}; content-type={ct}; body={body}" + ))) + } + + pub(crate) async fn task_text(&self, id: TaskId) -> Result { + let (details, body, _ct) = self + .details_with_body(&id.0) + .await + .map_err(|e| CloudTaskError::Http(format!("get_task_details failed: {e}")))?; + let prompt = details.user_text_prompt(); + let mut messages = details.assistant_text_messages(); + if messages.is_empty() { + messages.extend(extract_assistant_messages_from_body(&body)); + } + let assistant_turn = details.current_assistant_turn.as_ref(); + let turn_id = assistant_turn.and_then(|turn| turn.id.clone()); + let sibling_turn_ids = assistant_turn + .map(|turn| turn.sibling_turn_ids.clone()) + .unwrap_or_default(); + let attempt_placement = assistant_turn.and_then(|turn| turn.attempt_placement); + let attempt_status = attempt_status_from_str( + assistant_turn.and_then(|turn| turn.turn_status.as_deref()), + ); + Ok(TaskText { + prompt, + messages, + turn_id, + sibling_turn_ids, + attempt_placement, + attempt_status, + }) + } + + pub(crate) async fn create( + &self, + env_id: &str, + prompt: &str, + git_ref: &str, + qa_mode: bool, + best_of_n: usize, + ) -> Result { + let mut input_items: Vec = Vec::new(); + input_items.push(serde_json::json!({ + "type": "message", + "role": "user", + "content": [{ "content_type": "text", "text": prompt }] + })); + + if let Ok(diff) = std::env::var("CODEX_STARTING_DIFF") + && !diff.is_empty() + { + input_items.push(serde_json::json!({ + "type": "pre_apply_patch", + "output_diff": { "diff": diff } + })); + } + + let mut request_body = serde_json::json!({ + "new_task": { + "environment_id": env_id, + "branch": git_ref, + "run_environment_in_qa_mode": qa_mode, + }, + "input_items": input_items, + }); + + if best_of_n > 1 + && let Some(obj) = request_body.as_object_mut() + { + obj.insert( + "metadata".to_string(), + serde_json::json!({ "best_of_n": best_of_n }), + ); + } + + match self.backend.create_task(request_body).await { + Ok(id) => { + append_error_log(&format!( + "new_task: created id={id} env={} prompt_chars={}", + env_id, + prompt.chars().count() + )); + Ok(crate::CreatedTask { id: TaskId(id) }) + } + Err(e) => { + append_error_log(&format!( + "new_task: create failed env={} prompt_chars={}: {}", + env_id, + prompt.chars().count(), + e + )); + Err(CloudTaskError::Http(format!("create_task failed: {e}"))) + } + } + } + + async fn details_with_body( + &self, + id: &str, + ) -> anyhow::Result<(backend::CodeTaskDetailsResponse, String, String)> { + let (parsed, body, ct) = self.backend.get_task_details_with_body(id).await?; + Ok((parsed, body, ct)) + } + } + + pub(crate) struct Attempts<'a> { + backend: &'a backend::Client, + } + + impl<'a> Attempts<'a> { + pub(crate) fn new(client: &'a HttpClient) -> Self { + Self { + backend: &client.backend, + } + } + + pub(crate) async fn list(&self, task: TaskId, turn_id: String) -> Result> { + let resp = self + .backend + .list_sibling_turns(&task.0, &turn_id) + .await + .map_err(|e| CloudTaskError::Http(format!("list_sibling_turns failed: {e}")))?; + + let mut attempts: Vec = resp + .sibling_turns + .iter() + .filter_map(turn_attempt_from_map) + .collect(); + attempts.sort_by(compare_attempts); + Ok(attempts) + } + } + + pub(crate) struct Apply<'a> { + backend: &'a backend::Client, + } + + impl<'a> Apply<'a> { + pub(crate) fn new(client: &'a HttpClient) -> Self { + Self { + backend: &client.backend, + } + } + + pub(crate) async fn run( + &self, + task_id: TaskId, + diff_override: Option, + preflight: bool, + ) -> Result { + let id = task_id.0.clone(); + let diff = match diff_override { + Some(diff) => diff, + None => { + let details = self.backend.get_task_details(&id).await.map_err(|e| { + CloudTaskError::Http(format!("get_task_details failed: {e}")) + })?; + details.unified_diff().ok_or_else(|| { + CloudTaskError::Msg(format!("No diff available for task {id}")) + })? + } + }; + + if !is_unified_diff(&diff) { + let summary = summarize_patch_for_logging(&diff); + let mode = if preflight { "preflight" } else { "apply" }; + append_error_log(&format!( + "apply_error: id={id} mode={mode} format=non-unified; {summary}" + )); + return Ok(ApplyOutcome { + applied: false, + status: ApplyStatus::Error, + message: "Expected unified git diff; backend returned an incompatible format." + .to_string(), + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + }); + } + + let req = codex_git::ApplyGitRequest { + cwd: std::env::current_dir().unwrap_or_else(|_| std::env::temp_dir()), + diff: diff.clone(), + revert: false, + preflight, + }; + let r = codex_git::apply_git_patch(&req) + .map_err(|e| CloudTaskError::Io(format!("git apply failed to run: {e}")))?; + + let status = if r.exit_code == 0 { + ApplyStatus::Success + } else if !r.applied_paths.is_empty() || !r.conflicted_paths.is_empty() { + ApplyStatus::Partial + } else { + ApplyStatus::Error + }; + let applied = matches!(status, ApplyStatus::Success) && !preflight; + + let message = if preflight { + match status { + ApplyStatus::Success => { + format!("Preflight passed for task {id} (applies cleanly)") + } + ApplyStatus::Partial => format!( + "Preflight: patch does not fully apply for task {id} (applied={}, skipped={}, conflicts={})", + r.applied_paths.len(), + r.skipped_paths.len(), + r.conflicted_paths.len() + ), + ApplyStatus::Error => format!( + "Preflight failed for task {id} (applied={}, skipped={}, conflicts={})", + r.applied_paths.len(), + r.skipped_paths.len(), + r.conflicted_paths.len() + ), + } + } else { + match status { + ApplyStatus::Success => format!( + "Applied task {id} locally ({} files)", + r.applied_paths.len() + ), + ApplyStatus::Partial => format!( + "Apply partially succeeded for task {id} (applied={}, skipped={}, conflicts={})", + r.applied_paths.len(), + r.skipped_paths.len(), + r.conflicted_paths.len() + ), + ApplyStatus::Error => format!( + "Apply failed for task {id} (applied={}, skipped={}, conflicts={})", + r.applied_paths.len(), + r.skipped_paths.len(), + r.conflicted_paths.len() + ), + } + }; + + if matches!(status, ApplyStatus::Partial | ApplyStatus::Error) + || (preflight && !matches!(status, ApplyStatus::Success)) + { + let mut log = String::new(); + let summary = summarize_patch_for_logging(&diff); + let mode = if preflight { "preflight" } else { "apply" }; + use std::fmt::Write as _; + let _ = writeln!( + &mut log, + "apply_result: mode={} id={} status={:?} applied={} skipped={} conflicts={} cmd={}", + mode, + id, + status, + r.applied_paths.len(), + r.skipped_paths.len(), + r.conflicted_paths.len(), + r.cmd_for_log + ); + let _ = writeln!( + &mut log, + "stdout_tail=\n{}\nstderr_tail=\n{}", + tail(&r.stdout, 2000), + tail(&r.stderr, 2000) + ); + let _ = writeln!(&mut log, "{summary}"); + let _ = writeln!( + &mut log, + "----- PATCH BEGIN -----\n{diff}\n----- PATCH END -----" + ); + append_error_log(&log); + } + + Ok(ApplyOutcome { + applied, + status, + message, + skipped_paths: r.skipped_paths, + conflict_paths: r.conflicted_paths, + }) + } + } + + fn details_path(base_url: &str, id: &str) -> Option { + if base_url.contains("/backend-api") { + Some(format!("{base_url}/wham/tasks/{id}")) + } else if base_url.contains("/api/codex") { + Some(format!("{base_url}/tasks/{id}")) + } else { + None + } + } + + fn extract_assistant_messages_from_body(body: &str) -> Vec { + let mut msgs = Vec::new(); + if let Ok(full) = serde_json::from_str::(body) + && let Some(arr) = full + .get("current_assistant_turn") + .and_then(|v| v.get("worklog")) + .and_then(|v| v.get("messages")) + .and_then(|v| v.as_array()) + { + for m in arr { + let is_assistant = m + .get("author") + .and_then(|a| a.get("role")) + .and_then(|r| r.as_str()) + == Some("assistant"); + if !is_assistant { + continue; + } + if let Some(parts) = m + .get("content") + .and_then(|c| c.get("parts")) + .and_then(|p| p.as_array()) + { + for p in parts { + if let Some(s) = p.as_str() { + if !s.is_empty() { + msgs.push(s.to_string()); + } + continue; + } + if let Some(obj) = p.as_object() + && obj.get("content_type").and_then(|t| t.as_str()) == Some("text") + && let Some(txt) = obj.get("text").and_then(|t| t.as_str()) + { + msgs.push(txt.to_string()); + } + } + } + } + } + msgs + } + + fn turn_attempt_from_map(turn: &HashMap) -> Option { + let turn_id = turn.get("id").and_then(Value::as_str)?.to_string(); + let attempt_placement = turn.get("attempt_placement").and_then(Value::as_i64); + let created_at = parse_timestamp_value(turn.get("created_at")); + let status = attempt_status_from_str(turn.get("turn_status").and_then(Value::as_str)); + let diff = extract_diff_from_turn(turn); + let messages = extract_assistant_messages_from_turn(turn); + Some(TurnAttempt { + turn_id, + attempt_placement, + created_at, + status, + diff, + messages, + }) + } + + fn compare_attempts(a: &TurnAttempt, b: &TurnAttempt) -> Ordering { + match (a.attempt_placement, b.attempt_placement) { + (Some(lhs), Some(rhs)) => lhs.cmp(&rhs), + (Some(_), None) => Ordering::Less, + (None, Some(_)) => Ordering::Greater, + (None, None) => match (a.created_at, b.created_at) { + (Some(lhs), Some(rhs)) => lhs.cmp(&rhs), + (Some(_), None) => Ordering::Less, + (None, Some(_)) => Ordering::Greater, + (None, None) => a.turn_id.cmp(&b.turn_id), + }, + } + } + + fn extract_diff_from_turn(turn: &HashMap) -> Option { + let items = turn.get("output_items").and_then(Value::as_array)?; + for item in items { + match item.get("type").and_then(Value::as_str) { + Some("output_diff") => { + if let Some(diff) = item.get("diff").and_then(Value::as_str) + && !diff.is_empty() + { + return Some(diff.to_string()); + } + } + Some("pr") => { + if let Some(diff) = item + .get("output_diff") + .and_then(Value::as_object) + .and_then(|od| od.get("diff")) + .and_then(Value::as_str) + && !diff.is_empty() + { + return Some(diff.to_string()); + } + } + _ => {} + } + } + None + } + + fn extract_assistant_messages_from_turn(turn: &HashMap) -> Vec { + let mut msgs = Vec::new(); + if let Some(items) = turn.get("output_items").and_then(Value::as_array) { + for item in items { + if item.get("type").and_then(Value::as_str) != Some("message") { + continue; + } + if let Some(content) = item.get("content").and_then(Value::as_array) { + for part in content { + if part.get("content_type").and_then(Value::as_str) == Some("text") + && let Some(txt) = part.get("text").and_then(Value::as_str) + && !txt.is_empty() + { + msgs.push(txt.to_string()); + } + } + } + } + } + msgs + } + + fn attempt_status_from_str(raw: Option<&str>) -> AttemptStatus { + match raw.unwrap_or_default() { + "failed" => AttemptStatus::Failed, + "completed" => AttemptStatus::Completed, + "in_progress" => AttemptStatus::InProgress, + "pending" => AttemptStatus::Pending, + _ => AttemptStatus::Pending, + } + } + + fn parse_timestamp_value(v: Option<&Value>) -> Option> { + let ts = v?.as_f64()?; + let secs = ts as i64; + let nanos = ((ts - secs as f64) * 1_000_000_000.0) as u32; + Some(DateTime::::from( + std::time::UNIX_EPOCH + std::time::Duration::new(secs.max(0) as u64, nanos), + )) + } + + fn map_task_list_item_to_summary(src: backend::TaskListItem) -> TaskSummary { + let status_display = src.task_status_display.as_ref(); + TaskSummary { + id: TaskId(src.id), + title: src.title, + status: map_status(status_display), + updated_at: parse_updated_at(src.updated_at.as_ref()), + environment_id: None, + environment_label: env_label_from_status_display(status_display), + summary: diff_summary_from_status_display(status_display), + is_review: src + .pull_requests + .as_ref() + .is_some_and(|prs| !prs.is_empty()), + attempt_total: attempt_total_from_status_display(status_display), + } + } + + fn map_status(v: Option<&HashMap>) -> TaskStatus { + if let Some(val) = v { + if let Some(turn) = val + .get("latest_turn_status_display") + .and_then(Value::as_object) + && let Some(s) = turn.get("turn_status").and_then(Value::as_str) + { + return match s { + "failed" => TaskStatus::Error, + "completed" => TaskStatus::Ready, + "in_progress" => TaskStatus::Pending, + "pending" => TaskStatus::Pending, + "cancelled" => TaskStatus::Error, + _ => TaskStatus::Pending, + }; + } + if let Some(state) = val.get("state").and_then(Value::as_str) { + return match state { + "pending" => TaskStatus::Pending, + "ready" => TaskStatus::Ready, + "applied" => TaskStatus::Applied, + "error" => TaskStatus::Error, + _ => TaskStatus::Pending, + }; + } + } + TaskStatus::Pending + } + + fn parse_updated_at(ts: Option<&f64>) -> DateTime { + if let Some(v) = ts { + let secs = *v as i64; + let nanos = ((*v - secs as f64) * 1_000_000_000.0) as u32; + return DateTime::::from( + std::time::UNIX_EPOCH + std::time::Duration::new(secs.max(0) as u64, nanos), + ); + } + Utc::now() + } + + fn env_label_from_status_display(v: Option<&HashMap>) -> Option { + let map = v?; + map.get("environment_label") + .and_then(Value::as_str) + .map(str::to_string) + } + + fn diff_summary_from_status_display(v: Option<&HashMap>) -> DiffSummary { + let mut out = DiffSummary::default(); + let Some(map) = v else { return out }; + let latest = map + .get("latest_turn_status_display") + .and_then(Value::as_object); + let Some(latest) = latest else { return out }; + if let Some(ds) = latest.get("diff_stats").and_then(Value::as_object) { + if let Some(n) = ds.get("files_modified").and_then(Value::as_i64) { + out.files_changed = n.max(0) as usize; + } + if let Some(n) = ds.get("lines_added").and_then(Value::as_i64) { + out.lines_added = n.max(0) as usize; + } + if let Some(n) = ds.get("lines_removed").and_then(Value::as_i64) { + out.lines_removed = n.max(0) as usize; + } + } + out + } + + fn attempt_total_from_status_display(v: Option<&HashMap>) -> Option { + let map = v?; + let latest = map + .get("latest_turn_status_display") + .and_then(Value::as_object)?; + let siblings = latest.get("sibling_turn_ids").and_then(Value::as_array)?; + Some(siblings.len().saturating_add(1)) + } + + fn is_unified_diff(diff: &str) -> bool { + let t = diff.trim_start(); + if t.starts_with("diff --git ") { + return true; + } + let has_dash_headers = diff.contains("\n--- ") && diff.contains("\n+++ "); + let has_hunk = diff.contains("\n@@ ") || diff.starts_with("@@ "); + has_dash_headers && has_hunk + } + + fn tail(s: &str, max: usize) -> String { + if s.len() <= max { + s.to_string() + } else { + s[s.len() - max..].to_string() + } + } + + fn summarize_patch_for_logging(patch: &str) -> String { + let trimmed = patch.trim_start(); + let kind = if trimmed.starts_with("*** Begin Patch") { + "codex-patch" + } else if trimmed.starts_with("diff --git ") || trimmed.contains("\n*** End Patch\n") { + "git-diff" + } else if trimmed.starts_with("@@ ") || trimmed.contains("\n@@ ") { + "unified-diff" + } else { + "unknown" + }; + let lines = patch.lines().count(); + let chars = patch.len(); + let cwd = std::env::current_dir() + .ok() + .map(|p| p.display().to_string()) + .unwrap_or_else(|| "".to_string()); + let head: String = patch.lines().take(20).collect::>().join("\n"); + let head_trunc = if head.len() > 800 { + format!("{}…", &head[..800]) + } else { + head + }; + format!( + "patch_summary: kind={kind} lines={lines} chars={chars} cwd={cwd} ; head=\n{head_trunc}" + ) + } +} + +fn append_error_log(message: &str) { + let ts = Utc::now().to_rfc3339(); + if let Ok(mut f) = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open("error.log") + { + use std::io::Write as _; + let _ = writeln!(f, "[{ts}] {message}"); + } +} diff --git a/codex-rs/cloud-tasks-client/src/lib.rs b/codex-rs/cloud-tasks-client/src/lib.rs new file mode 100644 index 0000000000..a723512f8a --- /dev/null +++ b/codex-rs/cloud-tasks-client/src/lib.rs @@ -0,0 +1,29 @@ +mod api; + +pub use api::ApplyOutcome; +pub use api::ApplyStatus; +pub use api::AttemptStatus; +pub use api::CloudBackend; +pub use api::CloudTaskError; +pub use api::CreatedTask; +pub use api::DiffSummary; +pub use api::Result; +pub use api::TaskId; +pub use api::TaskStatus; +pub use api::TaskSummary; +pub use api::TaskText; +pub use api::TurnAttempt; + +#[cfg(feature = "mock")] +mod mock; + +#[cfg(feature = "online")] +mod http; + +#[cfg(feature = "mock")] +pub use mock::MockClient; + +#[cfg(feature = "online")] +pub use http::HttpClient; + +// Reusable apply engine now lives in the shared crate `codex-git`. diff --git a/codex-rs/cloud-tasks-client/src/mock.rs b/codex-rs/cloud-tasks-client/src/mock.rs new file mode 100644 index 0000000000..97bc5520a8 --- /dev/null +++ b/codex-rs/cloud-tasks-client/src/mock.rs @@ -0,0 +1,180 @@ +use crate::ApplyOutcome; +use crate::AttemptStatus; +use crate::CloudBackend; +use crate::DiffSummary; +use crate::Result; +use crate::TaskId; +use crate::TaskStatus; +use crate::TaskSummary; +use crate::TurnAttempt; +use crate::api::TaskText; +use chrono::Utc; + +#[derive(Clone, Default)] +pub struct MockClient; + +#[async_trait::async_trait] +impl CloudBackend for MockClient { + async fn list_tasks(&self, _env: Option<&str>) -> Result> { + // Slightly vary content by env to aid tests that rely on the mock + let rows = match _env { + Some("env-A") => vec![("T-2000", "A: First", TaskStatus::Ready)], + Some("env-B") => vec![ + ("T-3000", "B: One", TaskStatus::Ready), + ("T-3001", "B: Two", TaskStatus::Pending), + ], + _ => vec![ + ("T-1000", "Update README formatting", TaskStatus::Ready), + ("T-1001", "Fix clippy warnings in core", TaskStatus::Pending), + ("T-1002", "Add contributing guide", TaskStatus::Ready), + ], + }; + let environment_id = _env.map(str::to_string); + let environment_label = match _env { + Some("env-A") => Some("Env A".to_string()), + Some("env-B") => Some("Env B".to_string()), + Some(other) => Some(other.to_string()), + None => Some("Global".to_string()), + }; + let mut out = Vec::new(); + for (id_str, title, status) in rows { + let id = TaskId(id_str.to_string()); + let diff = mock_diff_for(&id); + let (a, d) = count_from_unified(&diff); + out.push(TaskSummary { + id, + title: title.to_string(), + status, + updated_at: Utc::now(), + environment_id: environment_id.clone(), + environment_label: environment_label.clone(), + summary: DiffSummary { + files_changed: 1, + lines_added: a, + lines_removed: d, + }, + is_review: false, + attempt_total: Some(if id_str == "T-1000" { 2 } else { 1 }), + }); + } + Ok(out) + } + + async fn get_task_diff(&self, id: TaskId) -> Result> { + Ok(Some(mock_diff_for(&id))) + } + + async fn get_task_messages(&self, _id: TaskId) -> Result> { + Ok(vec![ + "Mock assistant output: this task contains no diff.".to_string(), + ]) + } + + async fn get_task_text(&self, _id: TaskId) -> Result { + Ok(TaskText { + prompt: Some("Why is there no diff?".to_string()), + messages: vec!["Mock assistant output: this task contains no diff.".to_string()], + turn_id: Some("mock-turn".to_string()), + sibling_turn_ids: Vec::new(), + attempt_placement: Some(0), + attempt_status: AttemptStatus::Completed, + }) + } + + async fn apply_task(&self, id: TaskId, _diff_override: Option) -> Result { + Ok(ApplyOutcome { + applied: true, + status: crate::ApplyStatus::Success, + message: format!("Applied task {} locally (mock)", id.0), + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + }) + } + + async fn apply_task_preflight( + &self, + id: TaskId, + _diff_override: Option, + ) -> Result { + Ok(ApplyOutcome { + applied: false, + status: crate::ApplyStatus::Success, + message: format!("Preflight passed for task {} (mock)", id.0), + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + }) + } + + async fn list_sibling_attempts( + &self, + task: TaskId, + _turn_id: String, + ) -> Result> { + if task.0 == "T-1000" { + return Ok(vec![TurnAttempt { + turn_id: "T-1000-attempt-2".to_string(), + attempt_placement: Some(1), + created_at: Some(Utc::now()), + status: AttemptStatus::Completed, + diff: Some(mock_diff_for(&task)), + messages: vec!["Mock alternate attempt".to_string()], + }]); + } + Ok(Vec::new()) + } + + async fn create_task( + &self, + env_id: &str, + prompt: &str, + git_ref: &str, + qa_mode: bool, + best_of_n: usize, + ) -> Result { + let _ = (env_id, prompt, git_ref, qa_mode, best_of_n); + let id = format!("task_local_{}", chrono::Utc::now().timestamp_millis()); + Ok(crate::CreatedTask { id: TaskId(id) }) + } +} + +fn mock_diff_for(id: &TaskId) -> String { + match id.0.as_str() { + "T-1000" => { + "diff --git a/README.md b/README.md\nindex 000000..111111 100644\n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n Intro\n-Hello\n+Hello, world!\n+Task: T-1000\n".to_string() + } + "T-1001" => { + "diff --git a/core/src/lib.rs b/core/src/lib.rs\nindex 000000..111111 100644\n--- a/core/src/lib.rs\n+++ b/core/src/lib.rs\n@@ -1,2 +1,1 @@\n-use foo;\n use bar;\n".to_string() + } + _ => { + "diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md\nindex 000000..111111 100644\n--- /dev/null\n+++ b/CONTRIBUTING.md\n@@ -0,0 +1,3 @@\n+## Contributing\n+Please open PRs.\n+Thanks!\n".to_string() + } + } +} + +fn count_from_unified(diff: &str) -> (usize, usize) { + if let Ok(patch) = diffy::Patch::from_str(diff) { + patch + .hunks() + .iter() + .flat_map(diffy::Hunk::lines) + .fold((0, 0), |(a, d), l| match l { + diffy::Line::Insert(_) => (a + 1, d), + diffy::Line::Delete(_) => (a, d + 1), + _ => (a, d), + }) + } else { + let mut a = 0; + let mut d = 0; + for l in diff.lines() { + if l.starts_with("+++") || l.starts_with("---") || l.starts_with("@@") { + continue; + } + match l.as_bytes().first() { + Some(b'+') => a += 1, + Some(b'-') => d += 1, + _ => {} + } + } + (a, d) + } +} diff --git a/codex-rs/cloud-tasks/Cargo.toml b/codex-rs/cloud-tasks/Cargo.toml new file mode 100644 index 0000000000..46044fbb8d --- /dev/null +++ b/codex-rs/cloud-tasks/Cargo.toml @@ -0,0 +1,38 @@ +[package] +edition = "2024" +name = "codex-cloud-tasks" +version = { workspace = true } + +[lib] +name = "codex_cloud_tasks" +path = "src/lib.rs" + +[lints] +workspace = true + +[dependencies] +anyhow = { workspace = true } +base64 = { workspace = true } +chrono = { workspace = true, features = ["serde"] } +clap = { workspace = true, features = ["derive"] } +codex-cloud-tasks-client = { path = "../cloud-tasks-client", features = [ + "mock", + "online", +] } +codex-common = { path = "../common", features = ["cli"] } +codex-core = { path = "../core" } +codex-login = { path = "../login" } +codex-tui = { path = "../tui" } +crossterm = { workspace = true, features = ["event-stream"] } +ratatui = { workspace = true } +reqwest = { workspace = true, features = ["json"] } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } +tokio-stream = { workspace = true } +tracing = { workspace = true, features = ["log"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } +unicode-width = { workspace = true } + +[dev-dependencies] +async-trait = { workspace = true } diff --git a/codex-rs/cloud-tasks/src/app.rs b/codex-rs/cloud-tasks/src/app.rs new file mode 100644 index 0000000000..612c5f6be4 --- /dev/null +++ b/codex-rs/cloud-tasks/src/app.rs @@ -0,0 +1,481 @@ +use std::time::Duration; +use std::time::Instant; + +// Environment filter data models for the TUI +#[derive(Clone, Debug, Default)] +pub struct EnvironmentRow { + pub id: String, + pub label: Option, + pub is_pinned: bool, + pub repo_hints: Option, // e.g., "openai/codex" +} + +#[derive(Clone, Debug, Default)] +pub struct EnvModalState { + pub query: String, + pub selected: usize, +} + +#[derive(Clone, Debug, Default)] +pub struct BestOfModalState { + pub selected: usize, +} + +#[derive(Clone, Debug, Copy, PartialEq, Eq)] +pub enum ApplyResultLevel { + Success, + Partial, + Error, +} + +#[derive(Clone, Debug)] +pub struct ApplyModalState { + pub task_id: TaskId, + pub title: String, + pub result_message: Option, + pub result_level: Option, + pub skipped_paths: Vec, + pub conflict_paths: Vec, + pub diff_override: Option, +} + +use crate::scrollable_diff::ScrollableDiff; +use codex_cloud_tasks_client::CloudBackend; +use codex_cloud_tasks_client::TaskId; +use codex_cloud_tasks_client::TaskSummary; +#[derive(Default)] +pub struct App { + pub tasks: Vec, + pub selected: usize, + pub status: String, + pub diff_overlay: Option, + pub spinner_start: Option, + pub refresh_inflight: bool, + pub details_inflight: bool, + // Environment filter state + pub env_filter: Option, + pub env_modal: Option, + pub apply_modal: Option, + pub best_of_modal: Option, + pub environments: Vec, + pub env_last_loaded: Option, + pub env_loading: bool, + pub env_error: Option, + // New Task page + pub new_task: Option, + pub best_of_n: usize, + // Apply preflight spinner state + pub apply_preflight_inflight: bool, + // Apply action spinner state + pub apply_inflight: bool, + // Background enrichment coordination + pub list_generation: u64, + pub in_flight: std::collections::HashSet, + // Background enrichment caches were planned; currently unused. +} + +impl App { + pub fn new() -> Self { + Self { + tasks: Vec::new(), + selected: 0, + status: "Press r to refresh".to_string(), + diff_overlay: None, + spinner_start: None, + refresh_inflight: false, + details_inflight: false, + env_filter: None, + env_modal: None, + apply_modal: None, + best_of_modal: None, + environments: Vec::new(), + env_last_loaded: None, + env_loading: false, + env_error: None, + new_task: None, + best_of_n: 1, + apply_preflight_inflight: false, + apply_inflight: false, + list_generation: 0, + in_flight: std::collections::HashSet::new(), + } + } + + pub fn next(&mut self) { + if self.tasks.is_empty() { + return; + } + self.selected = (self.selected + 1).min(self.tasks.len().saturating_sub(1)); + } + + pub fn prev(&mut self) { + if self.tasks.is_empty() { + return; + } + if self.selected > 0 { + self.selected -= 1; + } + } +} + +pub async fn load_tasks( + backend: &dyn CloudBackend, + env: Option<&str>, +) -> anyhow::Result> { + // In later milestones, add a small debounce, spinner, and error display. + let tasks = tokio::time::timeout(Duration::from_secs(5), backend.list_tasks(env)).await??; + // Hide review-only tasks from the main list. + let filtered: Vec = tasks.into_iter().filter(|t| !t.is_review).collect(); + Ok(filtered) +} + +pub struct DiffOverlay { + pub title: String, + pub task_id: TaskId, + pub sd: ScrollableDiff, + pub base_can_apply: bool, + pub diff_lines: Vec, + pub text_lines: Vec, + pub prompt: Option, + pub attempts: Vec, + pub selected_attempt: usize, + pub current_view: DetailView, + pub base_turn_id: Option, + pub sibling_turn_ids: Vec, + pub attempt_total_hint: Option, +} + +#[derive(Clone, Debug, Default)] +pub struct AttemptView { + pub turn_id: Option, + pub status: codex_cloud_tasks_client::AttemptStatus, + pub attempt_placement: Option, + pub diff_lines: Vec, + pub text_lines: Vec, + pub prompt: Option, + pub diff_raw: Option, +} + +impl AttemptView { + pub fn has_diff(&self) -> bool { + !self.diff_lines.is_empty() + } + + pub fn has_text(&self) -> bool { + !self.text_lines.is_empty() || self.prompt.is_some() + } +} + +impl DiffOverlay { + pub fn new(task_id: TaskId, title: String, attempt_total_hint: Option) -> Self { + let mut sd = ScrollableDiff::new(); + sd.set_content(Vec::new()); + Self { + title, + task_id, + sd, + base_can_apply: false, + diff_lines: Vec::new(), + text_lines: Vec::new(), + prompt: None, + attempts: vec![AttemptView::default()], + selected_attempt: 0, + current_view: DetailView::Prompt, + base_turn_id: None, + sibling_turn_ids: Vec::new(), + attempt_total_hint, + } + } + + pub fn current_attempt(&self) -> Option<&AttemptView> { + self.attempts.get(self.selected_attempt) + } + + pub fn base_attempt_mut(&mut self) -> &mut AttemptView { + if self.attempts.is_empty() { + self.attempts.push(AttemptView::default()); + } + &mut self.attempts[0] + } + + pub fn set_view(&mut self, view: DetailView) { + self.current_view = view; + self.apply_selection_to_fields(); + } + + pub fn expected_attempts(&self) -> Option { + self.attempt_total_hint.or({ + if self.attempts.is_empty() { + None + } else { + Some(self.attempts.len()) + } + }) + } + + pub fn attempt_count(&self) -> usize { + self.attempts.len() + } + + pub fn attempt_display_total(&self) -> usize { + self.expected_attempts() + .unwrap_or_else(|| self.attempts.len().max(1)) + } + + pub fn step_attempt(&mut self, delta: isize) -> bool { + let total = self.attempts.len(); + if total <= 1 { + return false; + } + let total_isize = total as isize; + let current = self.selected_attempt as isize; + let mut next = current + delta; + next = ((next % total_isize) + total_isize) % total_isize; + let next = next as usize; + self.selected_attempt = next; + self.apply_selection_to_fields(); + true + } + + pub fn current_can_apply(&self) -> bool { + matches!(self.current_view, DetailView::Diff) + && self + .current_attempt() + .and_then(|attempt| attempt.diff_raw.as_ref()) + .map(|diff| !diff.is_empty()) + .unwrap_or(false) + } + + pub fn apply_selection_to_fields(&mut self) { + let (diff_lines, text_lines, prompt) = if let Some(attempt) = self.current_attempt() { + ( + attempt.diff_lines.clone(), + attempt.text_lines.clone(), + attempt.prompt.clone(), + ) + } else { + self.diff_lines.clear(); + self.text_lines.clear(); + self.prompt = None; + self.sd.set_content(vec!["".to_string()]); + return; + }; + + self.diff_lines = diff_lines.clone(); + self.text_lines = text_lines.clone(); + self.prompt = prompt; + + match self.current_view { + DetailView::Diff => { + if diff_lines.is_empty() { + self.sd.set_content(vec!["".to_string()]); + } else { + self.sd.set_content(diff_lines); + } + } + DetailView::Prompt => { + if text_lines.is_empty() { + self.sd.set_content(vec!["".to_string()]); + } else { + self.sd.set_content(text_lines); + } + } + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DetailView { + Diff, + Prompt, +} + +/// Internal app events delivered from background tasks. +/// These let the UI event loop remain responsive and keep the spinner animating. +#[derive(Debug)] +pub enum AppEvent { + TasksLoaded { + env: Option, + result: anyhow::Result>, + }, + // Background diff summary events were planned; removed for now to keep code minimal. + /// Autodetection of a likely environment id finished + EnvironmentAutodetected(anyhow::Result), + /// Background completion of environment list fetch + EnvironmentsLoaded(anyhow::Result>), + DetailsDiffLoaded { + id: TaskId, + title: String, + diff: String, + }, + DetailsMessagesLoaded { + id: TaskId, + title: String, + messages: Vec, + prompt: Option, + turn_id: Option, + sibling_turn_ids: Vec, + attempt_placement: Option, + attempt_status: codex_cloud_tasks_client::AttemptStatus, + }, + DetailsFailed { + id: TaskId, + title: String, + error: String, + }, + AttemptsLoaded { + id: TaskId, + attempts: Vec, + }, + /// Background completion of new task submission + NewTaskSubmitted(Result), + /// Background completion of apply preflight when opening modal or on demand + ApplyPreflightFinished { + id: TaskId, + title: String, + message: String, + level: ApplyResultLevel, + skipped: Vec, + conflicts: Vec, + }, + /// Background completion of apply action (actual patch application) + ApplyFinished { + id: TaskId, + result: std::result::Result, + }, +} + +// Convenience aliases; currently unused. +#[cfg(test)] +mod tests { + use super::*; + use chrono::Utc; + + struct FakeBackend { + // maps env key to titles + by_env: std::collections::HashMap, Vec<&'static str>>, + } + + #[async_trait::async_trait] + impl codex_cloud_tasks_client::CloudBackend for FakeBackend { + async fn list_tasks( + &self, + env: Option<&str>, + ) -> codex_cloud_tasks_client::Result> { + let key = env.map(str::to_string); + let titles = self + .by_env + .get(&key) + .cloned() + .unwrap_or_else(|| vec!["default-a", "default-b"]); + let mut out = Vec::new(); + for (i, t) in titles.into_iter().enumerate() { + out.push(TaskSummary { + id: TaskId(format!("T-{i}")), + title: t.to_string(), + status: codex_cloud_tasks_client::TaskStatus::Ready, + updated_at: Utc::now(), + environment_id: env.map(str::to_string), + environment_label: None, + summary: codex_cloud_tasks_client::DiffSummary::default(), + is_review: false, + attempt_total: Some(1), + }); + } + Ok(out) + } + + async fn get_task_diff( + &self, + _id: TaskId, + ) -> codex_cloud_tasks_client::Result> { + Err(codex_cloud_tasks_client::CloudTaskError::Unimplemented( + "not used in test", + )) + } + + async fn get_task_messages( + &self, + _id: TaskId, + ) -> codex_cloud_tasks_client::Result> { + Ok(vec![]) + } + async fn get_task_text( + &self, + _id: TaskId, + ) -> codex_cloud_tasks_client::Result { + Ok(codex_cloud_tasks_client::TaskText { + prompt: Some("Example prompt".to_string()), + messages: Vec::new(), + turn_id: Some("fake-turn".to_string()), + sibling_turn_ids: Vec::new(), + attempt_placement: Some(0), + attempt_status: codex_cloud_tasks_client::AttemptStatus::Completed, + }) + } + + async fn list_sibling_attempts( + &self, + _task: TaskId, + _turn_id: String, + ) -> codex_cloud_tasks_client::Result> { + Ok(Vec::new()) + } + + async fn apply_task( + &self, + _id: TaskId, + _diff_override: Option, + ) -> codex_cloud_tasks_client::Result { + Err(codex_cloud_tasks_client::CloudTaskError::Unimplemented( + "not used in test", + )) + } + + async fn apply_task_preflight( + &self, + _id: TaskId, + _diff_override: Option, + ) -> codex_cloud_tasks_client::Result { + Err(codex_cloud_tasks_client::CloudTaskError::Unimplemented( + "not used in test", + )) + } + + async fn create_task( + &self, + _env_id: &str, + _prompt: &str, + _git_ref: &str, + _qa_mode: bool, + _best_of_n: usize, + ) -> codex_cloud_tasks_client::Result { + Err(codex_cloud_tasks_client::CloudTaskError::Unimplemented( + "not used in test", + )) + } + } + + #[tokio::test] + async fn load_tasks_uses_env_parameter() { + // Arrange: env-specific task titles + let mut by_env = std::collections::HashMap::new(); + by_env.insert(None, vec!["root-1", "root-2"]); + by_env.insert(Some("env-A".to_string()), vec!["A-1"]); + by_env.insert(Some("env-B".to_string()), vec!["B-1", "B-2", "B-3"]); + let backend = FakeBackend { by_env }; + + // Act + Assert + let root = load_tasks(&backend, None).await.unwrap(); + assert_eq!(root.len(), 2); + assert_eq!(root[0].title, "root-1"); + + let a = load_tasks(&backend, Some("env-A")).await.unwrap(); + assert_eq!(a.len(), 1); + assert_eq!(a[0].title, "A-1"); + + let b = load_tasks(&backend, Some("env-B")).await.unwrap(); + assert_eq!(b.len(), 3); + assert_eq!(b[2].title, "B-3"); + } +} diff --git a/codex-rs/cloud-tasks/src/cli.rs b/codex-rs/cloud-tasks/src/cli.rs new file mode 100644 index 0000000000..4122aeff68 --- /dev/null +++ b/codex-rs/cloud-tasks/src/cli.rs @@ -0,0 +1,49 @@ +use clap::Args; +use clap::Parser; +use codex_common::CliConfigOverrides; + +#[derive(Parser, Debug, Default)] +#[command(version)] +pub struct Cli { + #[clap(skip)] + pub config_overrides: CliConfigOverrides, + + #[command(subcommand)] + pub command: Option, +} + +#[derive(Debug, clap::Subcommand)] +pub enum Command { + /// Submit a new Codex Cloud task without launching the TUI. + Exec(ExecCommand), +} + +#[derive(Debug, Args)] +pub struct ExecCommand { + /// Task prompt to run in Codex Cloud. + #[arg(value_name = "QUERY")] + pub query: Option, + + /// Target environment identifier (see `codex cloud` to browse). + #[arg(long = "env", value_name = "ENV_ID")] + pub environment: String, + + /// Number of assistant attempts (best-of-N). + #[arg( + long = "attempts", + default_value_t = 1usize, + value_parser = parse_attempts + )] + pub attempts: usize, +} + +fn parse_attempts(input: &str) -> Result { + let value: usize = input + .parse() + .map_err(|_| "attempts must be an integer between 1 and 4".to_string())?; + if (1..=4).contains(&value) { + Ok(value) + } else { + Err("attempts must be between 1 and 4".to_string()) + } +} diff --git a/codex-rs/cloud-tasks/src/env_detect.rs b/codex-rs/cloud-tasks/src/env_detect.rs new file mode 100644 index 0000000000..e7e8fb6b16 --- /dev/null +++ b/codex-rs/cloud-tasks/src/env_detect.rs @@ -0,0 +1,361 @@ +use reqwest::header::CONTENT_TYPE; +use reqwest::header::HeaderMap; +use std::collections::HashMap; +use tracing::info; +use tracing::warn; + +#[derive(Debug, Clone, serde::Deserialize)] +struct CodeEnvironment { + id: String, + #[serde(default)] + label: Option, + #[serde(default)] + is_pinned: Option, + #[serde(default)] + task_count: Option, +} + +#[derive(Debug, Clone)] +pub struct AutodetectSelection { + pub id: String, + pub label: Option, +} + +pub async fn autodetect_environment_id( + base_url: &str, + headers: &HeaderMap, + desired_label: Option, +) -> anyhow::Result { + // 1) Try repo-specific environments based on local git origins (GitHub only, like VSCode) + let origins = get_git_origins(); + crate::append_error_log(format!("env: git origins: {origins:?}")); + let mut by_repo_envs: Vec = Vec::new(); + for origin in &origins { + if let Some((owner, repo)) = parse_owner_repo(origin) { + let url = if base_url.contains("/backend-api") { + format!( + "{}/wham/environments/by-repo/{}/{}/{}", + base_url, "github", owner, repo + ) + } else { + format!( + "{}/api/codex/environments/by-repo/{}/{}/{}", + base_url, "github", owner, repo + ) + }; + crate::append_error_log(format!("env: GET {url}")); + match get_json::>(&url, headers).await { + Ok(mut list) => { + crate::append_error_log(format!( + "env: by-repo returned {} env(s) for {owner}/{repo}", + list.len(), + )); + by_repo_envs.append(&mut list); + } + Err(e) => crate::append_error_log(format!( + "env: by-repo fetch failed for {owner}/{repo}: {e}" + )), + } + } + } + if let Some(env) = pick_environment_row(&by_repo_envs, desired_label.as_deref()) { + return Ok(AutodetectSelection { + id: env.id.clone(), + label: env.label.as_deref().map(str::to_owned), + }); + } + + // 2) Fallback to the full list + let list_url = if base_url.contains("/backend-api") { + format!("{base_url}/wham/environments") + } else { + format!("{base_url}/api/codex/environments") + }; + crate::append_error_log(format!("env: GET {list_url}")); + // Fetch and log the full environments JSON for debugging + let http = reqwest::Client::builder().build()?; + let res = http.get(&list_url).headers(headers.clone()).send().await?; + let status = res.status(); + let ct = res + .headers() + .get(CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .to_string(); + let body = res.text().await.unwrap_or_default(); + crate::append_error_log(format!("env: status={status} content-type={ct}")); + match serde_json::from_str::(&body) { + Ok(v) => { + let pretty = serde_json::to_string_pretty(&v).unwrap_or(body.clone()); + crate::append_error_log(format!("env: /environments JSON (pretty):\n{pretty}")); + } + Err(_) => crate::append_error_log(format!("env: /environments (raw):\n{body}")), + } + if !status.is_success() { + anyhow::bail!("GET {list_url} failed: {status}; content-type={ct}; body={body}"); + } + let all_envs: Vec = serde_json::from_str(&body).map_err(|e| { + anyhow::anyhow!("Decode error for {list_url}: {e}; content-type={ct}; body={body}") + })?; + if let Some(env) = pick_environment_row(&all_envs, desired_label.as_deref()) { + return Ok(AutodetectSelection { + id: env.id.clone(), + label: env.label.as_deref().map(str::to_owned), + }); + } + anyhow::bail!("no environments available") +} + +fn pick_environment_row( + envs: &[CodeEnvironment], + desired_label: Option<&str>, +) -> Option { + if envs.is_empty() { + return None; + } + if let Some(label) = desired_label { + let lc = label.to_lowercase(); + if let Some(e) = envs + .iter() + .find(|e| e.label.as_deref().unwrap_or("").to_lowercase() == lc) + { + crate::append_error_log(format!("env: matched by label: {label} -> {}", e.id)); + return Some(e.clone()); + } + } + if envs.len() == 1 { + crate::append_error_log("env: single environment available; selecting it"); + return Some(envs[0].clone()); + } + if let Some(e) = envs.iter().find(|e| e.is_pinned.unwrap_or(false)) { + crate::append_error_log(format!("env: selecting pinned environment: {}", e.id)); + return Some(e.clone()); + } + // Highest task_count as heuristic + if let Some(e) = envs + .iter() + .max_by_key(|e| e.task_count.unwrap_or(0)) + .or_else(|| envs.first()) + { + crate::append_error_log(format!("env: selecting by task_count/first: {}", e.id)); + return Some(e.clone()); + } + None +} + +async fn get_json( + url: &str, + headers: &HeaderMap, +) -> anyhow::Result { + let http = reqwest::Client::builder().build()?; + let res = http.get(url).headers(headers.clone()).send().await?; + let status = res.status(); + let ct = res + .headers() + .get(CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .to_string(); + let body = res.text().await.unwrap_or_default(); + crate::append_error_log(format!("env: status={status} content-type={ct}")); + if !status.is_success() { + anyhow::bail!("GET {url} failed: {status}; content-type={ct}; body={body}"); + } + let parsed = serde_json::from_str::(&body).map_err(|e| { + anyhow::anyhow!("Decode error for {url}: {e}; content-type={ct}; body={body}") + })?; + Ok(parsed) +} + +fn get_git_origins() -> Vec { + // Prefer: git config --get-regexp remote\..*\.url + let out = std::process::Command::new("git") + .args(["config", "--get-regexp", "remote\\..*\\.url"]) + .output(); + if let Ok(ok) = out + && ok.status.success() + { + let s = String::from_utf8_lossy(&ok.stdout); + let mut urls = Vec::new(); + for line in s.lines() { + if let Some((_, url)) = line.split_once(' ') { + urls.push(url.trim().to_string()); + } + } + if !urls.is_empty() { + return uniq(urls); + } + } + // Fallback: git remote -v + let out = std::process::Command::new("git") + .args(["remote", "-v"]) + .output(); + if let Ok(ok) = out + && ok.status.success() + { + let s = String::from_utf8_lossy(&ok.stdout); + let mut urls = Vec::new(); + for line in s.lines() { + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() >= 2 { + urls.push(parts[1].to_string()); + } + } + if !urls.is_empty() { + return uniq(urls); + } + } + Vec::new() +} + +fn uniq(mut v: Vec) -> Vec { + v.sort(); + v.dedup(); + v +} + +fn parse_owner_repo(url: &str) -> Option<(String, String)> { + // Normalize common prefixes and handle multiple SSH/HTTPS variants. + let mut s = url.trim().to_string(); + // Drop protocol scheme for ssh URLs + if let Some(rest) = s.strip_prefix("ssh://") { + s = rest.to_string(); + } + // Accept any user before @github.com (e.g., git@, org-123@) + if let Some(idx) = s.find("@github.com:") { + let rest = &s[idx + "@github.com:".len()..]; + let rest = rest.trim_start_matches('/').trim_end_matches(".git"); + let mut parts = rest.splitn(2, '/'); + let owner = parts.next()?.to_string(); + let repo = parts.next()?.to_string(); + crate::append_error_log(format!("env: parsed SSH GitHub origin => {owner}/{repo}")); + return Some((owner, repo)); + } + // HTTPS or git protocol + for prefix in [ + "https://github.com/", + "http://github.com/", + "git://github.com/", + "github.com/", + ] { + if let Some(rest) = s.strip_prefix(prefix) { + let rest = rest.trim_start_matches('/').trim_end_matches(".git"); + let mut parts = rest.splitn(2, '/'); + let owner = parts.next()?.to_string(); + let repo = parts.next()?.to_string(); + crate::append_error_log(format!("env: parsed HTTP GitHub origin => {owner}/{repo}")); + return Some((owner, repo)); + } + } + None +} + +/// List environments for the current repo(s) with a fallback to the global list. +/// Returns a de-duplicated, sorted set suitable for the TUI modal. +pub async fn list_environments( + base_url: &str, + headers: &HeaderMap, +) -> anyhow::Result> { + let mut map: HashMap = HashMap::new(); + + // 1) By-repo lookup for each parsed GitHub origin + let origins = get_git_origins(); + for origin in &origins { + if let Some((owner, repo)) = parse_owner_repo(origin) { + let url = if base_url.contains("/backend-api") { + format!( + "{}/wham/environments/by-repo/{}/{}/{}", + base_url, "github", owner, repo + ) + } else { + format!( + "{}/api/codex/environments/by-repo/{}/{}/{}", + base_url, "github", owner, repo + ) + }; + match get_json::>(&url, headers).await { + Ok(list) => { + info!("env_tui: by-repo {}:{} -> {} envs", owner, repo, list.len()); + for e in list { + let entry = + map.entry(e.id.clone()) + .or_insert_with(|| crate::app::EnvironmentRow { + id: e.id.clone(), + label: e.label.clone(), + is_pinned: e.is_pinned.unwrap_or(false), + repo_hints: Some(format!("{owner}/{repo}")), + }); + // Merge: keep label if present, or use new; accumulate pinned flag + if entry.label.is_none() { + entry.label = e.label.clone(); + } + entry.is_pinned = entry.is_pinned || e.is_pinned.unwrap_or(false); + if entry.repo_hints.is_none() { + entry.repo_hints = Some(format!("{owner}/{repo}")); + } + } + } + Err(e) => { + warn!( + "env_tui: by-repo fetch failed for {}/{}: {}", + owner, repo, e + ); + } + } + } + } + + // 2) Fallback to the full list; on error return what we have if any. + let list_url = if base_url.contains("/backend-api") { + format!("{base_url}/wham/environments") + } else { + format!("{base_url}/api/codex/environments") + }; + match get_json::>(&list_url, headers).await { + Ok(list) => { + info!("env_tui: global list -> {} envs", list.len()); + for e in list { + let entry = map + .entry(e.id.clone()) + .or_insert_with(|| crate::app::EnvironmentRow { + id: e.id.clone(), + label: e.label.clone(), + is_pinned: e.is_pinned.unwrap_or(false), + repo_hints: None, + }); + if entry.label.is_none() { + entry.label = e.label.clone(); + } + entry.is_pinned = entry.is_pinned || e.is_pinned.unwrap_or(false); + } + } + Err(e) => { + if map.is_empty() { + return Err(e); + } else { + warn!( + "env_tui: global list failed; using by-repo results only: {}", + e + ); + } + } + } + + let mut rows: Vec = map.into_values().collect(); + rows.sort_by(|a, b| { + // pinned first + let p = b.is_pinned.cmp(&a.is_pinned); + if p != std::cmp::Ordering::Equal { + return p; + } + // then label (ci), then id + let al = a.label.as_deref().unwrap_or("").to_lowercase(); + let bl = b.label.as_deref().unwrap_or("").to_lowercase(); + let l = al.cmp(&bl); + if l != std::cmp::Ordering::Equal { + return l; + } + a.id.cmp(&b.id) + }); + Ok(rows) +} diff --git a/codex-rs/cloud-tasks/src/lib.rs b/codex-rs/cloud-tasks/src/lib.rs new file mode 100644 index 0000000000..6fc721404b --- /dev/null +++ b/codex-rs/cloud-tasks/src/lib.rs @@ -0,0 +1,1751 @@ +mod app; +mod cli; +pub mod env_detect; +mod new_task; +pub mod scrollable_diff; +mod ui; +pub mod util; +pub use cli::Cli; + +use anyhow::anyhow; +use codex_login::AuthManager; +use std::io::IsTerminal; +use std::io::Read; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Duration; +use std::time::Instant; +use tokio::sync::mpsc::UnboundedSender; +use tracing::info; +use tracing_subscriber::EnvFilter; +use util::append_error_log; +use util::set_user_agent_suffix; + +struct ApplyJob { + task_id: codex_cloud_tasks_client::TaskId, + diff_override: Option, +} + +struct BackendContext { + backend: Arc, + base_url: String, +} + +async fn init_backend(user_agent_suffix: &str) -> anyhow::Result { + let use_mock = matches!( + std::env::var("CODEX_CLOUD_TASKS_MODE").ok().as_deref(), + Some("mock") | Some("MOCK") + ); + let base_url = std::env::var("CODEX_CLOUD_TASKS_BASE_URL") + .unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string()); + + set_user_agent_suffix(user_agent_suffix); + + if use_mock { + return Ok(BackendContext { + backend: Arc::new(codex_cloud_tasks_client::MockClient), + base_url, + }); + } + + let ua = codex_core::default_client::get_codex_user_agent(); + let mut http = codex_cloud_tasks_client::HttpClient::new(base_url.clone())?.with_user_agent(ua); + let style = if base_url.contains("/backend-api") { + "wham" + } else { + "codex-api" + }; + append_error_log(format!("startup: base_url={base_url} path_style={style}")); + + let auth_manager = util::load_auth_manager().await; + let auth = match auth_manager.as_ref().and_then(AuthManager::auth) { + Some(auth) => auth, + None => { + eprintln!( + "Not signed in. Please run 'codex login' to sign in with ChatGPT, then re-run 'codex cloud'." + ); + std::process::exit(1); + } + }; + + if let Some(acc) = auth.get_account_id() { + append_error_log(format!("auth: mode=ChatGPT account_id={acc}")); + } + + let token = match auth.get_token().await { + Ok(t) if !t.is_empty() => t, + _ => { + eprintln!( + "Not signed in. Please run 'codex login' to sign in with ChatGPT, then re-run 'codex cloud'." + ); + std::process::exit(1); + } + }; + + http = http.with_bearer_token(token.clone()); + if let Some(acc) = auth + .get_account_id() + .or_else(|| util::extract_chatgpt_account_id(&token)) + { + append_error_log(format!("auth: set ChatGPT-Account-Id header: {acc}")); + http = http.with_chatgpt_account_id(acc); + } + + Ok(BackendContext { + backend: Arc::new(http), + base_url, + }) +} + +async fn run_exec_command(args: crate::cli::ExecCommand) -> anyhow::Result<()> { + let crate::cli::ExecCommand { + query, + environment, + attempts, + } = args; + let ctx = init_backend("codex_cloud_tasks_exec").await?; + let prompt = resolve_query_input(query)?; + let env_id = resolve_environment_id(&ctx, &environment).await?; + let created = codex_cloud_tasks_client::CloudBackend::create_task( + &*ctx.backend, + &env_id, + &prompt, + "main", + false, + attempts, + ) + .await?; + let url = util::task_url(&ctx.base_url, &created.id.0); + println!("{url}"); + Ok(()) +} + +async fn resolve_environment_id(ctx: &BackendContext, requested: &str) -> anyhow::Result { + let trimmed = requested.trim(); + if trimmed.is_empty() { + return Err(anyhow!("environment id must not be empty")); + } + let normalized = util::normalize_base_url(&ctx.base_url); + let headers = util::build_chatgpt_headers().await; + let environments = crate::env_detect::list_environments(&normalized, &headers).await?; + if environments.is_empty() { + return Err(anyhow!( + "no cloud environments are available for this workspace" + )); + } + + if let Some(row) = environments.iter().find(|row| row.id == trimmed) { + return Ok(row.id.clone()); + } + + let label_matches = environments + .iter() + .filter(|row| { + row.label + .as_deref() + .map(|label| label.eq_ignore_ascii_case(trimmed)) + .unwrap_or(false) + }) + .collect::>(); + match label_matches.as_slice() { + [] => Err(anyhow!( + "environment '{trimmed}' not found; run `codex cloud` to list available environments" + )), + [single] => Ok(single.id.clone()), + [first, rest @ ..] => { + let first_id = &first.id; + if rest.iter().all(|row| row.id == *first_id) { + Ok(first_id.clone()) + } else { + Err(anyhow!( + "environment label '{trimmed}' is ambiguous; run `codex cloud` to pick the desired environment id" + )) + } + } + } +} + +fn resolve_query_input(query_arg: Option) -> anyhow::Result { + match query_arg { + Some(q) if q != "-" => Ok(q), + maybe_dash => { + let force_stdin = matches!(maybe_dash.as_deref(), Some("-")); + if std::io::stdin().is_terminal() && !force_stdin { + return Err(anyhow!( + "no query provided. Pass one as an argument or pipe it via stdin." + )); + } + if !force_stdin { + eprintln!("Reading query from stdin..."); + } + let mut buffer = String::new(); + std::io::stdin() + .read_to_string(&mut buffer) + .map_err(|e| anyhow!("failed to read query from stdin: {e}"))?; + if buffer.trim().is_empty() { + return Err(anyhow!( + "no query provided via stdin (received empty input)." + )); + } + Ok(buffer) + } + } +} + +fn level_from_status(status: codex_cloud_tasks_client::ApplyStatus) -> app::ApplyResultLevel { + match status { + codex_cloud_tasks_client::ApplyStatus::Success => app::ApplyResultLevel::Success, + codex_cloud_tasks_client::ApplyStatus::Partial => app::ApplyResultLevel::Partial, + codex_cloud_tasks_client::ApplyStatus::Error => app::ApplyResultLevel::Error, + } +} + +fn spawn_preflight( + app: &mut app::App, + backend: &Arc, + tx: &UnboundedSender, + frame_tx: &UnboundedSender, + title: String, + job: ApplyJob, +) -> bool { + if app.apply_inflight { + app.status = "An apply is already running; wait for it to finish first.".to_string(); + return false; + } + if app.apply_preflight_inflight { + app.status = "A preflight is already running; wait for it to finish first.".to_string(); + return false; + } + + app.apply_preflight_inflight = true; + let _ = frame_tx.send(Instant::now() + Duration::from_millis(100)); + + let backend = backend.clone(); + let tx = tx.clone(); + tokio::spawn(async move { + let ApplyJob { + task_id, + diff_override, + } = job; + let result = codex_cloud_tasks_client::CloudBackend::apply_task_preflight( + &*backend, + task_id.clone(), + diff_override, + ) + .await; + + let event = match result { + Ok(outcome) => { + let level = level_from_status(outcome.status); + app::AppEvent::ApplyPreflightFinished { + id: task_id, + title, + message: outcome.message, + level, + skipped: outcome.skipped_paths, + conflicts: outcome.conflict_paths, + } + } + Err(e) => app::AppEvent::ApplyPreflightFinished { + id: task_id, + title, + message: format!("Preflight failed: {e}"), + level: app::ApplyResultLevel::Error, + skipped: Vec::new(), + conflicts: Vec::new(), + }, + }; + + let _ = tx.send(event); + }); + + true +} + +fn spawn_apply( + app: &mut app::App, + backend: &Arc, + tx: &UnboundedSender, + frame_tx: &UnboundedSender, + job: ApplyJob, +) -> bool { + if app.apply_inflight { + app.status = "An apply is already running; wait for it to finish first.".to_string(); + return false; + } + if app.apply_preflight_inflight { + app.status = "Finish the current preflight before starting another apply.".to_string(); + return false; + } + + app.apply_inflight = true; + let _ = frame_tx.send(Instant::now() + Duration::from_millis(100)); + + let backend = backend.clone(); + let tx = tx.clone(); + tokio::spawn(async move { + let ApplyJob { + task_id, + diff_override, + } = job; + let result = codex_cloud_tasks_client::CloudBackend::apply_task( + &*backend, + task_id.clone(), + diff_override, + ) + .await; + + let event = match result { + Ok(outcome) => app::AppEvent::ApplyFinished { + id: task_id, + result: Ok(outcome), + }, + Err(e) => app::AppEvent::ApplyFinished { + id: task_id, + result: Err(format!("{e}")), + }, + }; + + let _ = tx.send(event); + }); + + true +} + +// logging helper lives in util module + +// (no standalone patch summarizer needed – UI displays raw diffs) + +/// Entry point for the `codex cloud` subcommand. +pub async fn run_main(cli: Cli, _codex_linux_sandbox_exe: Option) -> anyhow::Result<()> { + if let Some(command) = cli.command { + return match command { + crate::cli::Command::Exec(args) => run_exec_command(args).await, + }; + } + let Cli { .. } = cli; + + // Very minimal logging setup; mirrors other crates' pattern. + let default_level = "error"; + let _ = tracing_subscriber::fmt() + .with_env_filter( + EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new(default_level)) + .unwrap_or_else(|_| EnvFilter::new(default_level)), + ) + .with_ansi(std::io::stderr().is_terminal()) + .with_writer(std::io::stderr) + .try_init(); + + info!("Launching Cloud Tasks list UI"); + let BackendContext { backend, .. } = init_backend("codex_cloud_tasks_tui").await?; + let backend = backend; + + // Terminal setup + use crossterm::ExecutableCommand; + use crossterm::event::DisableBracketedPaste; + use crossterm::event::EnableBracketedPaste; + use crossterm::event::KeyboardEnhancementFlags; + use crossterm::event::PopKeyboardEnhancementFlags; + use crossterm::event::PushKeyboardEnhancementFlags; + use crossterm::terminal::EnterAlternateScreen; + use crossterm::terminal::LeaveAlternateScreen; + use crossterm::terminal::disable_raw_mode; + use crossterm::terminal::enable_raw_mode; + use ratatui::Terminal; + use ratatui::backend::CrosstermBackend; + let mut stdout = std::io::stdout(); + enable_raw_mode()?; + stdout.execute(EnterAlternateScreen)?; + stdout.execute(EnableBracketedPaste)?; + // Enable enhanced key reporting so Shift+Enter is distinguishable from Enter. + // Some terminals may not support these flags; ignore errors if enabling fails. + let _ = crossterm::execute!( + std::io::stdout(), + PushKeyboardEnhancementFlags( + KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES + | KeyboardEnhancementFlags::REPORT_EVENT_TYPES + | KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS + ) + ); + let backend_ui = CrosstermBackend::new(stdout); + let mut terminal = Terminal::new(backend_ui)?; + terminal.clear()?; + + // App state + let mut app = app::App::new(); + // Initial load + let force_internal = matches!( + std::env::var("CODEX_CLOUD_TASKS_FORCE_INTERNAL") + .ok() + .as_deref(), + Some("1") | Some("true") | Some("TRUE") + ); + append_error_log(format!( + "startup: wham_force_internal={} ua={}", + force_internal, + codex_core::default_client::get_codex_user_agent() + )); + // Non-blocking initial load so the in-box spinner can animate + app.status = "Loading tasks…".to_string(); + app.refresh_inflight = true; + // New list generation; reset background enrichment coordination + app.list_generation = app.list_generation.saturating_add(1); + app.in_flight.clear(); + // reset any in-flight enrichment state + + // Event stream + use crossterm::event::Event; + use crossterm::event::EventStream; + use crossterm::event::KeyCode; + use crossterm::event::KeyEventKind; + use crossterm::event::KeyModifiers; + use tokio_stream::StreamExt; + let mut events = EventStream::new(); + + // Channel for non-blocking background loads + use tokio::sync::mpsc::unbounded_channel; + let (tx, mut rx) = unbounded_channel::(); + // Kick off the initial load in background + { + let backend = Arc::clone(&backend); + let tx = tx.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, None).await; + let _ = tx.send(app::AppEvent::TasksLoaded { + env: None, + result: res, + }); + }); + } + // Fetch environment list in parallel so the header can show friendly names quickly. + { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = util::normalize_base_url( + &std::env::var("CODEX_CLOUD_TASKS_BASE_URL") + .unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string()), + ); + let headers = util::build_chatgpt_headers().await; + let res = crate::env_detect::list_environments(&base_url, &headers).await; + let _ = tx.send(app::AppEvent::EnvironmentsLoaded(res)); + }); + } + + // Try to auto-detect a likely environment id on startup and refresh if found. + // Do this concurrently so the initial list shows quickly; on success we refetch with filter. + { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = util::normalize_base_url( + &std::env::var("CODEX_CLOUD_TASKS_BASE_URL") + .unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string()), + ); + // Build headers: UA + ChatGPT auth if available + let headers = util::build_chatgpt_headers().await; + + // Run autodetect. If it fails, we keep using "All". + let res = crate::env_detect::autodetect_environment_id(&base_url, &headers, None).await; + let _ = tx.send(app::AppEvent::EnvironmentAutodetected(res)); + }); + } + + // Event-driven redraws with a tiny coalescing scheduler (snappy UI, no fixed 250ms tick). + let mut needs_redraw = true; + use std::time::Instant; + use tokio::time::Instant as TokioInstant; + use tokio::time::sleep_until; + let (frame_tx, mut frame_rx) = tokio::sync::mpsc::unbounded_channel::(); + let (redraw_tx, mut redraw_rx) = tokio::sync::mpsc::unbounded_channel::<()>(); + + // Coalesce frame requests to the earliest deadline; emit a single redraw signal. + tokio::spawn(async move { + let mut next_deadline: Option = None; + loop { + let target = + next_deadline.unwrap_or_else(|| Instant::now() + Duration::from_secs(24 * 60 * 60)); + let sleeper = sleep_until(TokioInstant::from_std(target)); + tokio::pin!(sleeper); + tokio::select! { + recv = frame_rx.recv() => { + match recv { + Some(at) => { + if next_deadline.is_none_or(|cur| at < cur) { + next_deadline = Some(at); + } + continue; // recompute sleep target + } + None => break, + } + } + _ = &mut sleeper => { + if next_deadline.take().is_some() { + let _ = redraw_tx.send(()); + } + } + } + } + }); + // Kick an initial draw so the UI appears immediately. + let _ = frame_tx.send(Instant::now()); + + // Render helper to centralize immediate redraws after handling events. + let render_if_needed = |terminal: &mut Terminal>, + app: &mut app::App, + needs_redraw: &mut bool| + -> anyhow::Result<()> { + if *needs_redraw { + terminal.draw(|f| ui::draw(f, app))?; + *needs_redraw = false; + } + Ok(()) + }; + + let exit_code = loop { + tokio::select! { + // Coalesced redraw requests: spinner animation and paste-burst micro‑flush. + Some(()) = redraw_rx.recv() => { + // Micro‑flush pending first key held by paste‑burst. + if let Some(page) = app.new_task.as_mut() { + if page.composer.flush_paste_burst_if_due() { needs_redraw = true; } + if page.composer.is_in_paste_burst() { + let _ = frame_tx.send(Instant::now() + codex_tui::ComposerInput::recommended_flush_delay()); + } + } + // Keep spinner pulsing only while loading. + if app.refresh_inflight + || app.details_inflight + || app.env_loading + || app.apply_preflight_inflight + || app.apply_inflight + { + if app.spinner_start.is_none() { + app.spinner_start = Some(Instant::now()); + } + needs_redraw = true; + let _ = frame_tx.send(Instant::now() + Duration::from_millis(600)); + } else { + app.spinner_start = None; + } + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + maybe_app_event = rx.recv() => { + if let Some(ev) = maybe_app_event { + match ev { + app::AppEvent::TasksLoaded { env, result } => { + // Only apply results for the current filter to avoid races. + if env.as_deref() != app.env_filter.as_deref() { + append_error_log(format!( + "refresh.drop: env={} current={}", + env.clone().unwrap_or_else(|| "".to_string()), + app.env_filter.clone().unwrap_or_else(|| "".to_string()) + )); + continue; + } + app.refresh_inflight = false; + match result { + Ok(tasks) => { + append_error_log(format!( + "refresh.apply: env={} count={}", + env.clone().unwrap_or_else(|| "".to_string()), + tasks.len() + )); + app.tasks = tasks; + if app.selected >= app.tasks.len() { app.selected = app.tasks.len().saturating_sub(1); } + app.status = "Loaded tasks".to_string(); + } + Err(e) => { + append_error_log(format!("refresh load_tasks failed: {e}")); + app.status = format!("Failed to load tasks: {e}"); + } + } + needs_redraw = true; + let _ = frame_tx.send(Instant::now()); + } + app::AppEvent::NewTaskSubmitted(result) => { + match result { + Ok(created) => { + append_error_log(format!("new-task: created id={}", created.id.0)); + app.status = format!("Submitted as {}", created.id.0); + app.new_task = None; + // Refresh tasks in background for current filter + app.status = format!("Submitted as {} — refreshing…", created.id.0); + app.refresh_inflight = true; + app.list_generation = app.list_generation.saturating_add(1); + needs_redraw = true; + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let env_sel = app.env_filter.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, env_sel.as_deref()).await; + let _ = tx.send(app::AppEvent::TasksLoaded { env: env_sel, result: res }); + }); + let _ = frame_tx.send(Instant::now()); + } + Err(msg) => { + append_error_log(format!("new-task: submit failed: {msg}")); + if let Some(page) = app.new_task.as_mut() { page.submitting = false; } + app.status = format!("Submit failed: {msg}. See error.log for details."); + needs_redraw = true; + let _ = frame_tx.send(Instant::now()); + } + } + } + // (removed TaskSummaryUpdated; unused in this prototype) + app::AppEvent::ApplyPreflightFinished { id, title, message, level, skipped, conflicts } => { + // Only update if modal is still open and ids match + if let Some(m) = app.apply_modal.as_mut() + && m.task_id == id + { + m.title = title; + m.result_message = Some(message); + m.result_level = Some(level); + m.skipped_paths = skipped; + m.conflict_paths = conflicts; + app.apply_preflight_inflight = false; + needs_redraw = true; + let _ = frame_tx.send(Instant::now()); + } + } + app::AppEvent::EnvironmentsLoaded(result) => { + app.env_loading = false; + match result { + Ok(list) => { + app.environments = list; + app.env_error = None; + app.env_last_loaded = Some(std::time::Instant::now()); + } + Err(e) => { + app.env_error = Some(e.to_string()); + } + } + needs_redraw = true; + let _ = frame_tx.send(Instant::now()); + } + app::AppEvent::EnvironmentAutodetected(result) => { + if let Ok(sel) = result { + // Only apply if user hasn't set a filter yet or it's different. + if app.env_filter.as_deref() != Some(sel.id.as_str()) { + append_error_log(format!( + "env.select: autodetected id={} label={}", + sel.id, + sel.label.clone().unwrap_or_else(|| "".to_string()) + )); + // Preseed environments with detected label so header can show it even before list arrives + if let Some(lbl) = sel.label.clone() { + let present = app.environments.iter().any(|r| r.id == sel.id); + if !present { + app.environments.push(app::EnvironmentRow { id: sel.id.clone(), label: Some(lbl), is_pinned: false, repo_hints: None }); + } + } + app.env_filter = Some(sel.id); + app.status = "Loading tasks…".to_string(); + app.refresh_inflight = true; + app.list_generation = app.list_generation.saturating_add(1); + app.in_flight.clear(); + // reset spinner state + needs_redraw = true; + { + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let env_sel = app.env_filter.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, env_sel.as_deref()).await; + let _ = tx.send(app::AppEvent::TasksLoaded { env: env_sel, result: res }); + }); + } + // Proactively fetch environments to resolve a friendly name for the header. + app.env_loading = true; + { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = crate::util::normalize_base_url( + &std::env::var("CODEX_CLOUD_TASKS_BASE_URL") + .unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string()), + ); + let headers = crate::util::build_chatgpt_headers().await; + let res = crate::env_detect::list_environments(&base_url, &headers).await; + let _ = tx.send(app::AppEvent::EnvironmentsLoaded(res)); + }); + } + let _ = frame_tx.send(Instant::now()); + } + } + // on Err, silently continue with All + } + app::AppEvent::DetailsDiffLoaded { id, title, diff } => { + if let Some(ov) = &app.diff_overlay + && ov.task_id != id { + continue; + } + let diff_lines: Vec = diff.lines().map(str::to_string).collect(); + if let Some(ov) = app.diff_overlay.as_mut() { + ov.title = title; + { + let base = ov.base_attempt_mut(); + base.diff_lines = diff_lines.clone(); + base.diff_raw = Some(diff.clone()); + } + ov.base_can_apply = true; + ov.apply_selection_to_fields(); + } else { + let mut overlay = app::DiffOverlay::new(id.clone(), title, None); + { + let base = overlay.base_attempt_mut(); + base.diff_lines = diff_lines.clone(); + base.diff_raw = Some(diff.clone()); + } + overlay.base_can_apply = true; + overlay.current_view = app::DetailView::Diff; + overlay.apply_selection_to_fields(); + app.diff_overlay = Some(overlay); + } + app.details_inflight = false; + app.status.clear(); + needs_redraw = true; + } + app::AppEvent::DetailsMessagesLoaded { + id, + title, + messages, + prompt, + turn_id, + sibling_turn_ids, + attempt_placement, + attempt_status, + } => { + if let Some(ov) = &app.diff_overlay + && ov.task_id != id { + continue; + } + let conv = conversation_lines(prompt.clone(), &messages); + if let Some(ov) = app.diff_overlay.as_mut() { + ov.title = title.clone(); + { + let base = ov.base_attempt_mut(); + base.text_lines = conv.clone(); + base.prompt = prompt.clone(); + base.turn_id = turn_id.clone(); + base.status = attempt_status; + base.attempt_placement = attempt_placement; + } + ov.base_turn_id = turn_id.clone(); + ov.sibling_turn_ids = sibling_turn_ids.clone(); + ov.attempt_total_hint = Some(sibling_turn_ids.len().saturating_add(1)); + if !ov.base_can_apply { + ov.current_view = app::DetailView::Prompt; + } + ov.apply_selection_to_fields(); + if let (Some(turn_id), true) = (turn_id.clone(), !sibling_turn_ids.is_empty()) + && ov.attempts.len() == 1 { + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let task_id = id.clone(); + tokio::spawn(async move { + match codex_cloud_tasks_client::CloudBackend::list_sibling_attempts( + &*backend, + task_id.clone(), + turn_id, + ) + .await + { + Ok(attempts) => { + let _ = tx.send(app::AppEvent::AttemptsLoaded { id: task_id, attempts }); + } + Err(e) => { + crate::util::append_error_log(format!( + "attempts.load failed for {}: {e}", + task_id.0 + )); + } + } + }); + } + } else { + let mut overlay = app::DiffOverlay::new(id.clone(), title, None); + { + let base = overlay.base_attempt_mut(); + base.text_lines = conv.clone(); + base.prompt = prompt.clone(); + base.turn_id = turn_id.clone(); + base.status = attempt_status; + base.attempt_placement = attempt_placement; + } + overlay.base_turn_id = turn_id.clone(); + overlay.sibling_turn_ids = sibling_turn_ids.clone(); + overlay.attempt_total_hint = Some(sibling_turn_ids.len().saturating_add(1)); + overlay.current_view = app::DetailView::Prompt; + overlay.apply_selection_to_fields(); + app.diff_overlay = Some(overlay); + } + app.details_inflight = false; + app.status.clear(); + needs_redraw = true; + } + app::AppEvent::AttemptsLoaded { id, attempts } => { + if let Some(ov) = app.diff_overlay.as_mut() { + if ov.task_id != id { + continue; + } + for attempt in attempts { + if ov + .attempts + .iter() + .any(|existing| existing.turn_id.as_deref() == Some(attempt.turn_id.as_str())) + { + continue; + } + let diff_lines = attempt + .diff + .as_ref() + .map(|d| d.lines().map(str::to_string).collect()) + .unwrap_or_default(); + let text_lines = conversation_lines(None, &attempt.messages); + ov.attempts.push(app::AttemptView { + turn_id: Some(attempt.turn_id.clone()), + status: attempt.status, + attempt_placement: attempt.attempt_placement, + diff_lines, + text_lines, + prompt: None, + diff_raw: attempt.diff.clone(), + }); + } + if ov.attempts.len() > 1 { + let (_, rest) = ov.attempts.split_at_mut(1); + rest.sort_by(|a, b| match (a.attempt_placement, b.attempt_placement) { + (Some(lhs), Some(rhs)) => lhs.cmp(&rhs), + (Some(_), None) => std::cmp::Ordering::Less, + (None, Some(_)) => std::cmp::Ordering::Greater, + (None, None) => a.turn_id.cmp(&b.turn_id), + }); + } + if ov.selected_attempt >= ov.attempts.len() { + ov.selected_attempt = ov.attempts.len().saturating_sub(1); + } + ov.attempt_total_hint = Some(ov.attempts.len()); + ov.apply_selection_to_fields(); + needs_redraw = true; + } + } + app::AppEvent::DetailsFailed { id, title, error } => { + if let Some(ov) = &app.diff_overlay + && ov.task_id != id { + continue; + } + append_error_log(format!("details failed for {}: {error}", id.0)); + let pretty = pretty_lines_from_error(&error); + if let Some(ov) = app.diff_overlay.as_mut() { + ov.title = title.clone(); + { + let base = ov.base_attempt_mut(); + base.diff_lines.clear(); + base.text_lines = pretty.clone(); + base.prompt = None; + } + ov.base_can_apply = false; + ov.current_view = app::DetailView::Prompt; + ov.apply_selection_to_fields(); + } else { + let mut overlay = app::DiffOverlay::new(id.clone(), title, None); + { + let base = overlay.base_attempt_mut(); + base.text_lines = pretty; + } + overlay.base_can_apply = false; + overlay.current_view = app::DetailView::Prompt; + overlay.apply_selection_to_fields(); + app.diff_overlay = Some(overlay); + } + app.details_inflight = false; + needs_redraw = true; + } + app::AppEvent::ApplyFinished { id, result } => { + // Only update if the modal still corresponds to this id. + if let Some(m) = &app.apply_modal { + if m.task_id != id { continue; } + } else { + continue; + } + app.apply_inflight = false; + match result { + Ok(outcome) => { + app.status = outcome.message.clone(); + if matches!(outcome.status, codex_cloud_tasks_client::ApplyStatus::Success) { + app.apply_modal = None; + app.diff_overlay = None; + // Refresh tasks after successful apply + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let env_sel = app.env_filter.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, env_sel.as_deref()).await; + let _ = tx.send(app::AppEvent::TasksLoaded { env: env_sel, result: res }); + }); + } + } + Err(e) => { + append_error_log(format!("apply_task failed for {}: {e}", id.0)); + app.status = format!("Apply failed: {e}"); + } + } + needs_redraw = true; + } + } + } + // Render immediately after processing app events. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + maybe_event = events.next() => { + match maybe_event { + Some(Ok(Event::Paste(pasted))) => { + if app.env_modal.is_some() { + if let Some(m) = app.env_modal.as_mut() { + for ch in pasted.chars() { + match ch { + '\r' | '\n' => continue, + '\t' => m.query.push(' '), + _ => m.query.push(ch), + } + } + } + needs_redraw = true; + } else if let Some(page) = app.new_task.as_mut() + && !page.submitting + { + if page.composer.handle_paste(pasted) { + needs_redraw = true; + } + let _ = frame_tx.send(Instant::now()); + } + } + Some(Ok(Event::Key(key))) if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) => { + // Treat Ctrl-C like pressing 'q' in the current context. + if key.modifiers.contains(KeyModifiers::CONTROL) + && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('C')) + { + if app.env_modal.is_some() { + // Close environment selector if open (don’t quit composer). + app.env_modal = None; + needs_redraw = true; + } else if app.best_of_modal.is_some() { + app.best_of_modal = None; + needs_redraw = true; + } else if app.apply_modal.is_some() { + app.apply_modal = None; + app.status = "Apply canceled".to_string(); + needs_redraw = true; + } else if app.new_task.is_some() { + app.new_task = None; + app.status = "Canceled new task".to_string(); + needs_redraw = true; + } else if app.diff_overlay.is_some() { + app.diff_overlay = None; + needs_redraw = true; + } else { + break 0; + } + // Render updated state immediately before continuing to next loop iteration. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + // Render after New Task branch to reflect input changes immediately. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + continue; + } + let is_ctrl_n = key.modifiers.contains(KeyModifiers::CONTROL) + && matches!(key.code, KeyCode::Char('n') | KeyCode::Char('N')) + || matches!(key.code, KeyCode::Char('\u{000E}')); + if is_ctrl_n { + if app.new_task.is_none() { + continue; + } + if app.best_of_modal.is_some() { + app.best_of_modal = None; + needs_redraw = true; + } else { + let selected = app.best_of_n.saturating_sub(1).min(3); + app.best_of_modal = Some(app::BestOfModalState { selected }); + app.status = format!( + "Select best-of attempts (current: {} attempt{})", + app.best_of_n, + if app.best_of_n == 1 { "" } else { "s" } + ); + needs_redraw = true; + } + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + continue; + } + if app.best_of_modal.is_some() { + match key.code { + KeyCode::Esc => { + app.best_of_modal = None; + needs_redraw = true; + } + KeyCode::Down | KeyCode::Char('j') => { + if let Some(m) = app.best_of_modal.as_mut() { + m.selected = (m.selected + 1).min(3); + } + needs_redraw = true; + } + KeyCode::Up | KeyCode::Char('k') => { + if let Some(m) = app.best_of_modal.as_mut() { + m.selected = m.selected.saturating_sub(1); + } + needs_redraw = true; + } + KeyCode::Char('1') | KeyCode::Char('2') | KeyCode::Char('3') | KeyCode::Char('4') => { + if let Some(m) = app.best_of_modal.as_mut() { + let val = match key.code { + KeyCode::Char('1') => 0, + KeyCode::Char('2') => 1, + KeyCode::Char('3') => 2, + KeyCode::Char('4') => 3, + _ => m.selected, + }; + m.selected = val; + } + needs_redraw = true; + } + KeyCode::Enter => { + if let Some(state) = app.best_of_modal.take() { + let new_value = state.selected + 1; + app.best_of_n = new_value; + if let Some(page) = app.new_task.as_mut() { + page.best_of_n = new_value; + } + append_error_log(format!("best-of.select: attempts={new_value}")); + app.status = format!( + "Best-of updated to {new_value} attempt{}", + if new_value == 1 { "" } else { "s" } + ); + needs_redraw = true; + } + } + _ => {} + } + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + continue; + } + // New Task page: Ctrl+O opens environment switcher while composing. + let is_ctrl_o = key.modifiers.contains(KeyModifiers::CONTROL) + && matches!(key.code, KeyCode::Char('o') | KeyCode::Char('O')) + || matches!(key.code, KeyCode::Char('\u{000F}')); + if is_ctrl_o && app.new_task.is_some() { + // Close task modal/pending apply if present before opening env modal + app.diff_overlay = None; + app.env_modal = Some(app::EnvModalState { query: String::new(), selected: 0 }); + // Cache environments while the modal is open to avoid repeated fetches. + let should_fetch = app.environments.is_empty(); + if should_fetch { + app.env_loading = true; + app.env_error = None; + // Ensure spinner animates while loading environments. + let _ = frame_tx.send(Instant::now() + Duration::from_millis(100)); + } + needs_redraw = true; + if should_fetch { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = crate::util::normalize_base_url(&std::env::var("CODEX_CLOUD_TASKS_BASE_URL").unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string())); + let headers = crate::util::build_chatgpt_headers().await; + let res = crate::env_detect::list_environments(&base_url, &headers).await; + let _ = tx.send(app::AppEvent::EnvironmentsLoaded(res)); + }); + } + // Render after opening env modal to show it instantly. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + continue; + } + + // New Task page has priority when active, unless an env modal is open. + if let Some(page) = app.new_task.as_mut() { + if app.env_modal.is_some() { + // Defer handling to env-modal branch below. + } else { + match key.code { + KeyCode::Esc => { + app.new_task = None; + app.status = "Canceled new task".to_string(); + needs_redraw = true; + } + _ => { + if page.submitting { + // Ignore input while submitting + } else if let codex_tui::ComposerAction::Submitted(text) = page.composer.input(key) { + // Submit only if we have an env id + if let Some(env) = page.env_id.clone() { + append_error_log(format!( + "new-task: submit env={} size={}", + env, + text.chars().count() + )); + page.submitting = true; + app.status = "Submitting new task…".to_string(); + let tx = tx.clone(); + let backend = Arc::clone(&backend); + let best_of_n = page.best_of_n; + tokio::spawn(async move { + let git_ref = if let Ok(cwd) = std::env::current_dir() { + if let Some(branch) = codex_core::git_info::default_branch_name(&cwd).await { + branch + } else if let Some(branch) = codex_core::git_info::current_branch_name(&cwd).await { + branch + } else { + "main".to_string() + } + } else { + "main".to_string() + }; + + let result = codex_cloud_tasks_client::CloudBackend::create_task(&*backend, &env, &text, &git_ref, false, best_of_n).await; + let evt = match result { + Ok(ok) => app::AppEvent::NewTaskSubmitted(Ok(ok)), + Err(e) => app::AppEvent::NewTaskSubmitted(Err(format!("{e}"))), + }; + let _ = tx.send(evt); + }); + } else { + app.status = "No environment selected".to_string(); + } + } + needs_redraw = true; + // If paste‑burst is active, schedule a micro‑flush frame. + if page.composer.is_in_paste_burst() { + let _ = frame_tx.send(Instant::now() + codex_tui::ComposerInput::recommended_flush_delay()); + } + // Always schedule an immediate redraw for key edits in the composer. + let _ = frame_tx.send(Instant::now()); + // Draw now so non-char edits (e.g., Option+Delete) reflect instantly. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + } + continue; + } + } + // If a diff overlay is open, handle its keys first. + if app.apply_modal.is_some() { + // Simple apply confirmation modal: y apply, p preflight, n/Esc cancel + match key.code { + KeyCode::Char('y') => { + if let Some(m) = app.apply_modal.as_ref() { + let title = m.title.clone(); + let job = ApplyJob { + task_id: m.task_id.clone(), + diff_override: m.diff_override.clone(), + }; + if spawn_apply(&mut app, &backend, &tx, &frame_tx, job) { + app.status = format!("Applying '{title}'..."); + } + needs_redraw = true; + } + } + KeyCode::Char('p') => { + if let Some(m) = app.apply_modal.take() { + let title = m.title.clone(); + let job = ApplyJob { + task_id: m.task_id.clone(), + diff_override: m.diff_override.clone(), + }; + if spawn_preflight(&mut app, &backend, &tx, &frame_tx, title.clone(), job) { + app.apply_modal = Some(app::ApplyModalState { + task_id: m.task_id, + title: title.clone(), + result_message: None, + result_level: None, + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + diff_override: m.diff_override, + }); + app.status = format!("Preflighting '{title}'..."); + } else { + app.apply_modal = Some(m); + } + needs_redraw = true; + } + } + KeyCode::Esc + | KeyCode::Char('n') + | KeyCode::Char('q') + | KeyCode::Char('Q') => { app.apply_modal = None; app.status = "Apply canceled".to_string(); needs_redraw = true; } + _ => {} + } + } else if app.diff_overlay.is_some() { + let mut cycle_attempt = |delta: isize| { + if let Some(ov) = app.diff_overlay.as_mut() + && ov.attempt_count() > 1 { + ov.step_attempt(delta); + let total = ov.attempt_display_total(); + let current = ov.selected_attempt + 1; + app.status = format!("Viewing attempt {current} of {total}"); + ov.sd.to_top(); + needs_redraw = true; + } + }; + + match key.code { + KeyCode::Char('a') => { + if app.apply_inflight || app.apply_preflight_inflight { + app.status = "Finish the current apply/preflight before starting another.".to_string(); + needs_redraw = true; + continue; + } + let snapshot = app.diff_overlay.as_ref().map(|ov| { + ( + ov.task_id.clone(), + ov.title.clone(), + ov.current_can_apply(), + ov.current_attempt().and_then(|attempt| attempt.diff_raw.clone()), + ) + }); + if let Some((task_id, title, can_apply, diff_override)) = snapshot { + if can_apply { + let job = ApplyJob { + task_id: task_id.clone(), + diff_override: diff_override.clone(), + }; + if spawn_preflight(&mut app, &backend, &tx, &frame_tx, title.clone(), job) { + app.apply_modal = Some(app::ApplyModalState { + task_id, + title: title.clone(), + result_message: None, + result_level: None, + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + diff_override, + }); + app.status = format!("Preflighting '{title}'..."); + } + } else { + app.status = "No diff available to apply.".to_string(); + } + needs_redraw = true; + } + } + KeyCode::Tab => { + cycle_attempt(1); + } + KeyCode::BackTab => { + cycle_attempt(-1); + } + // From task modal, 'o' should close it and open the env selector + KeyCode::Char('o') | KeyCode::Char('O') => { + app.diff_overlay = None; + app.env_modal = Some(app::EnvModalState { query: String::new(), selected: 0 }); + // Use cached environments unless empty + if app.environments.is_empty() { app.env_loading = true; app.env_error = None; } + needs_redraw = true; + if app.environments.is_empty() { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = crate::util::normalize_base_url( + &std::env::var("CODEX_CLOUD_TASKS_BASE_URL") + .unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string()), + ); + let headers = crate::util::build_chatgpt_headers().await; + let res = crate::env_detect::list_environments(&base_url, &headers).await; + let _ = tx.send(app::AppEvent::EnvironmentsLoaded(res)); + }); + } + } + KeyCode::Left => { + if let Some(ov) = &mut app.diff_overlay { + let has_text = ov.current_attempt().is_some_and(app::AttemptView::has_text); + let has_diff = ov.current_attempt().is_some_and(app::AttemptView::has_diff) || ov.base_can_apply; + if has_text && has_diff { + ov.set_view(app::DetailView::Prompt); + ov.sd.to_top(); + needs_redraw = true; + } + } + } + KeyCode::Right => { + if let Some(ov) = &mut app.diff_overlay { + let has_text = ov.current_attempt().is_some_and(app::AttemptView::has_text); + let has_diff = ov.current_attempt().is_some_and(app::AttemptView::has_diff) || ov.base_can_apply; + if has_text && has_diff { + ov.set_view(app::DetailView::Diff); + ov.sd.to_top(); + needs_redraw = true; + } + } + } + KeyCode::Char(']') | KeyCode::Char('}') => { + cycle_attempt(1); + } + KeyCode::Char('[') | KeyCode::Char('{') => { + cycle_attempt(-1); + } + KeyCode::Esc | KeyCode::Char('q') => { + app.diff_overlay = None; + needs_redraw = true; + } + KeyCode::Down | KeyCode::Char('j') => { + if let Some(ov) = &mut app.diff_overlay { ov.sd.scroll_by(1); } + needs_redraw = true; + } + KeyCode::Up | KeyCode::Char('k') => { + if let Some(ov) = &mut app.diff_overlay { ov.sd.scroll_by(-1); } + needs_redraw = true; + } + KeyCode::PageDown | KeyCode::Char(' ') => { + if let Some(ov) = &mut app.diff_overlay { let step = ov.sd.state.viewport_h.saturating_sub(1) as i16; ov.sd.page_by(step); } + needs_redraw = true; + } + KeyCode::PageUp => { + if let Some(ov) = &mut app.diff_overlay { let step = ov.sd.state.viewport_h.saturating_sub(1) as i16; ov.sd.page_by(-step); } + needs_redraw = true; + } + KeyCode::Home => { if let Some(ov) = &mut app.diff_overlay { ov.sd.to_top(); } needs_redraw = true; } + KeyCode::End => { if let Some(ov) = &mut app.diff_overlay { ov.sd.to_bottom(); } needs_redraw = true; } + _ => {} + } + } else if app.env_modal.is_some() { + // Environment modal key handling + match key.code { + KeyCode::Esc => { app.env_modal = None; needs_redraw = true; } + KeyCode::Char(ch) if !key.modifiers.contains(KeyModifiers::CONTROL) && !key.modifiers.contains(KeyModifiers::ALT) => { + if let Some(m) = app.env_modal.as_mut() { m.query.push(ch); } + needs_redraw = true; + } + KeyCode::Backspace => { if let Some(m) = app.env_modal.as_mut() { m.query.pop(); } needs_redraw = true; } + KeyCode::Down | KeyCode::Char('j') => { if let Some(m) = app.env_modal.as_mut() { m.selected = m.selected.saturating_add(1); } needs_redraw = true; } + KeyCode::Up | KeyCode::Char('k') => { if let Some(m) = app.env_modal.as_mut() { m.selected = m.selected.saturating_sub(1); } needs_redraw = true; } + KeyCode::Home => { if let Some(m) = app.env_modal.as_mut() { m.selected = 0; } needs_redraw = true; } + KeyCode::End => { if let Some(m) = app.env_modal.as_mut() { m.selected = app.environments.len(); } needs_redraw = true; } + KeyCode::PageDown | KeyCode::Char(' ') => { if let Some(m) = app.env_modal.as_mut() { let step = 10usize; m.selected = m.selected.saturating_add(step); } needs_redraw = true; } + KeyCode::PageUp => { if let Some(m) = app.env_modal.as_mut() { let step = 10usize; m.selected = m.selected.saturating_sub(step); } needs_redraw = true; } + KeyCode::Char('n') => { + if app.env_filter.is_none() { + app.new_task = Some(crate::new_task::NewTaskPage::new(None, app.best_of_n)); + } else { + app.new_task = Some(crate::new_task::NewTaskPage::new(app.env_filter.clone(), app.best_of_n)); + } + app.status = "New Task: Enter to submit; Esc to cancel".to_string(); + needs_redraw = true; + } + KeyCode::Enter => { + // Resolve selection over filtered set + if let Some(state) = app.env_modal.take() { + let q = state.query.to_lowercase(); + let filtered: Vec<&app::EnvironmentRow> = app.environments.iter().filter(|r| { + if q.is_empty() { return true; } + let mut hay = String::new(); + if let Some(l) = &r.label { hay.push_str(&l.to_lowercase()); hay.push(' '); } + hay.push_str(&r.id.to_lowercase()); + if let Some(h) = &r.repo_hints { hay.push(' '); hay.push_str(&h.to_lowercase()); } + hay.contains(&q) + }).collect(); + // Keep original order (already sorted) — no need to re-sort + let idx = state.selected; + if idx == 0 { app.env_filter = None; append_error_log("env.select: All"); } + else { + let env_idx = idx.saturating_sub(1); + if let Some(row) = filtered.get(env_idx) { + append_error_log(format!( + "env.select: id={} label={}", + row.id, + row.label.clone().unwrap_or_else(|| "".to_string()) + )); + app.env_filter = Some(row.id.clone()); + } + } + // If New Task page is open, reflect the new selection in its header immediately. + if let Some(page) = app.new_task.as_mut() { + page.env_id = app.env_filter.clone(); + } + // Trigger tasks refresh with the selected filter + app.status = "Loading tasks…".to_string(); + app.refresh_inflight = true; + app.list_generation = app.list_generation.saturating_add(1); + app.in_flight.clear(); + // reset spinner state + needs_redraw = true; + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let env_sel = app.env_filter.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, env_sel.as_deref()).await; + let _ = tx.send(app::AppEvent::TasksLoaded { env: env_sel, result: res }); + }); + } + } + _ => {} + } + } else { + // Base list view keys + match key.code { + KeyCode::Char('q') | KeyCode::Esc => { + break 0; + } + KeyCode::Down | KeyCode::Char('j') => { + app.next(); + needs_redraw = true; + } + KeyCode::Up | KeyCode::Char('k') => { + app.prev(); + needs_redraw = true; + } + // Ensure 'r' does not refresh tasks when the env modal is open. + KeyCode::Char('r') | KeyCode::Char('R') => { + if app.env_modal.is_some() { break 0; } + append_error_log(format!( + "refresh.request: env={}", + app.env_filter.clone().unwrap_or_else(|| "".to_string()) + )); + app.status = "Refreshing…".to_string(); + app.refresh_inflight = true; + app.list_generation = app.list_generation.saturating_add(1); + app.in_flight.clear(); + // reset spinner state + needs_redraw = true; + // Spawn background refresh + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let env_sel = app.env_filter.clone(); + tokio::spawn(async move { + let res = app::load_tasks(&*backend, env_sel.as_deref()).await; + let _ = tx.send(app::AppEvent::TasksLoaded { env: env_sel, result: res }); + }); + } + KeyCode::Char('o') | KeyCode::Char('O') => { + app.env_modal = Some(app::EnvModalState { query: String::new(), selected: 0 }); + // Cache environments while the modal is open to avoid repeated fetches. + let should_fetch = app.environments.is_empty(); + if should_fetch { app.env_loading = true; app.env_error = None; } + needs_redraw = true; + if should_fetch { + let tx = tx.clone(); + tokio::spawn(async move { + let base_url = crate::util::normalize_base_url(&std::env::var("CODEX_CLOUD_TASKS_BASE_URL").unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string())); + let headers = crate::util::build_chatgpt_headers().await; + let res = crate::env_detect::list_environments(&base_url, &headers).await; + let _ = tx.send(app::AppEvent::EnvironmentsLoaded(res)); + }); + } + } + KeyCode::Char('n') => { + let env_opt = app.env_filter.clone(); + app.new_task = Some(crate::new_task::NewTaskPage::new(env_opt, app.best_of_n)); + app.status = "New Task: Enter to submit; Esc to cancel".to_string(); + needs_redraw = true; + } + KeyCode::Enter => { + if let Some(task) = app.tasks.get(app.selected).cloned() { + app.status = format!("Loading details for {title}…", title = task.title); + app.details_inflight = true; + // Open empty overlay immediately; content arrives via events + let overlay = app::DiffOverlay::new( + task.id.clone(), + task.title.clone(), + task.attempt_total, + ); + app.diff_overlay = Some(overlay); + needs_redraw = true; + // Spawn background details load (diff first, then messages fallback) + let id = task.id.clone(); + let title = task.title.clone(); + { + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let diff_id = id.clone(); + let diff_title = title.clone(); + tokio::spawn(async move { + match codex_cloud_tasks_client::CloudBackend::get_task_diff(&*backend, diff_id.clone()).await { + Ok(Some(diff)) => { + let _ = tx.send(app::AppEvent::DetailsDiffLoaded { id: diff_id, title: diff_title, diff }); + } + Ok(None) => { + match codex_cloud_tasks_client::CloudBackend::get_task_text(&*backend, diff_id.clone()).await { + Ok(text) => { + let evt = app::AppEvent::DetailsMessagesLoaded { + id: diff_id, + title: diff_title, + messages: text.messages, + prompt: text.prompt, + turn_id: text.turn_id, + sibling_turn_ids: text.sibling_turn_ids, + attempt_placement: text.attempt_placement, + attempt_status: text.attempt_status, + }; + let _ = tx.send(evt); + } + Err(e2) => { + let _ = tx.send(app::AppEvent::DetailsFailed { id: diff_id, title: diff_title, error: format!("{e2}") }); + } + } + } + Err(e) => { + append_error_log(format!("get_task_diff failed for {}: {e}", diff_id.0)); + match codex_cloud_tasks_client::CloudBackend::get_task_text(&*backend, diff_id.clone()).await { + Ok(text) => { + let evt = app::AppEvent::DetailsMessagesLoaded { + id: diff_id, + title: diff_title, + messages: text.messages, + prompt: text.prompt, + turn_id: text.turn_id, + sibling_turn_ids: text.sibling_turn_ids, + attempt_placement: text.attempt_placement, + attempt_status: text.attempt_status, + }; + let _ = tx.send(evt); + } + Err(e2) => { + let _ = tx.send(app::AppEvent::DetailsFailed { id: diff_id, title: diff_title, error: format!("{e2}") }); + } + } + } + } + }); + } + // Also fetch conversation text even when diff exists + { + let backend = Arc::clone(&backend); + let tx = tx.clone(); + let msg_id = id; + let msg_title = title; + tokio::spawn(async move { + if let Ok(text) = codex_cloud_tasks_client::CloudBackend::get_task_text(&*backend, msg_id.clone()).await { + let evt = app::AppEvent::DetailsMessagesLoaded { + id: msg_id, + title: msg_title, + messages: text.messages, + prompt: text.prompt, + turn_id: text.turn_id, + sibling_turn_ids: text.sibling_turn_ids, + attempt_placement: text.attempt_placement, + attempt_status: text.attempt_status, + }; + let _ = tx.send(evt); + } + }); + } + // Animate spinner while details load. + let _ = frame_tx.send(Instant::now() + Duration::from_millis(100)); + } + } + KeyCode::Char('a') => { + if app.apply_inflight || app.apply_preflight_inflight { + app.status = "Finish the current apply/preflight before starting another.".to_string(); + needs_redraw = true; + continue; + } + + if let Some(task) = app.tasks.get(app.selected).cloned() { + match codex_cloud_tasks_client::CloudBackend::get_task_diff(&*backend, task.id.clone()).await { + Ok(Some(diff)) => { + let diff_override = Some(diff.clone()); + let task_id = task.id.clone(); + let title = task.title.clone(); + let job = ApplyJob { + task_id: task_id.clone(), + diff_override: diff_override.clone(), + }; + if spawn_preflight( + &mut app, + &backend, + &tx, + &frame_tx, + title.clone(), + job, + ) { + app.apply_modal = Some(app::ApplyModalState { + task_id, + title: title.clone(), + result_message: None, + result_level: None, + skipped_paths: Vec::new(), + conflict_paths: Vec::new(), + diff_override, + }); + app.status = format!("Preflighting '{title}'..."); + } + } + Ok(None) | Err(_) => { + app.status = "No diff available to apply".to_string(); + } + } + needs_redraw = true; + } + } + _ => {} + } + } + // Render after handling a key event (when not quitting). + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + Some(Ok(Event::Resize(_, _))) => { + needs_redraw = true; + // Redraw immediately on resize for snappier UX. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + Some(Err(_)) | None => {} + _ => {} + } + // Fallback: if any other event path requested a redraw, render now. + render_if_needed(&mut terminal, &mut app, &mut needs_redraw)?; + } + } + }; + + // Restore terminal + disable_raw_mode().ok(); + terminal.show_cursor().ok(); + let _ = crossterm::execute!(std::io::stdout(), DisableBracketedPaste); + // Best-effort restore of keyboard enhancement flags before leaving alt screen. + let _ = crossterm::execute!(std::io::stdout(), PopKeyboardEnhancementFlags); + let _ = crossterm::execute!(std::io::stdout(), LeaveAlternateScreen); + + if exit_code != 0 { + std::process::exit(exit_code); + } + Ok(()) +} + +// extract_chatgpt_account_id moved to util.rs + +/// Build plain-text conversation lines: a labeled user prompt followed by assistant messages. +fn conversation_lines(prompt: Option, messages: &[String]) -> Vec { + let mut out: Vec = Vec::new(); + if let Some(p) = prompt { + out.push("user:".to_string()); + for l in p.lines() { + out.push(l.to_string()); + } + out.push(String::new()); + } + if !messages.is_empty() { + out.push("assistant:".to_string()); + for (i, m) in messages.iter().enumerate() { + for l in m.lines() { + out.push(l.to_string()); + } + if i + 1 < messages.len() { + out.push(String::new()); + } + } + } + if out.is_empty() { + out.push("".to_string()); + } + out +} + +/// Convert a verbose HTTP error with embedded JSON body into concise, user-friendly lines +/// for the details overlay. Falls back to a short raw message when parsing fails. +fn pretty_lines_from_error(raw: &str) -> Vec { + let mut lines: Vec = Vec::new(); + let is_no_diff = raw.contains("No output_diff in response."); + let is_no_msgs = raw.contains("No assistant text messages in response."); + if is_no_diff { + lines.push("No diff available for this task.".to_string()); + } else if is_no_msgs { + lines.push("No assistant messages found for this task.".to_string()); + } else { + lines.push("Failed to load task details.".to_string()); + } + + // Try to parse the embedded JSON body: find the first '{' after " body=" and decode. + if let Some(body_idx) = raw.find(" body=") + && let Some(json_start_rel) = raw[body_idx..].find('{') + { + let json_start = body_idx + json_start_rel; + let json_str = raw[json_start..].trim(); + if let Ok(v) = serde_json::from_str::(json_str) { + // Prefer assistant turn context. + let turn = v + .get("current_assistant_turn") + .and_then(|x| x.as_object()) + .cloned() + .or_else(|| { + v.get("current_diff_task_turn") + .and_then(|x| x.as_object()) + .cloned() + }); + if let Some(t) = turn { + if let Some(err) = t.get("error").and_then(|e| e.as_object()) { + let code = err.get("code").and_then(|s| s.as_str()).unwrap_or(""); + let msg = err.get("message").and_then(|s| s.as_str()).unwrap_or(""); + if !code.is_empty() || !msg.is_empty() { + let summary = if code.is_empty() { + msg.to_string() + } else if msg.is_empty() { + code.to_string() + } else { + format!("{code}: {msg}") + }; + lines.push(format!("Assistant error: {summary}")); + } + } + if let Some(status) = t.get("turn_status").and_then(|s| s.as_str()) { + lines.push(format!("Status: {status}")); + } + if let Some(text) = t + .get("latest_event") + .and_then(|e| e.get("text")) + .and_then(|s| s.as_str()) + && !text.trim().is_empty() + { + lines.push(format!("Latest event: {}", text.trim())); + } + } + } + } + + if lines.len() == 1 { + // Parsing yielded nothing; include a trimmed, short raw message tail for context. + let tail = if raw.len() > 320 { + format!("{}…", &raw[..320]) + } else { + raw.to_string() + }; + lines.push(tail); + } else if lines.len() >= 2 { + // Add a hint to refresh when still in progress. + if lines.iter().any(|l| l.contains("in_progress")) { + lines.push("This task may still be running. Press 'r' to refresh.".to_string()); + } + // Avoid an empty overlay + lines.push(String::new()); + } + lines +} + +#[cfg(test)] +mod tests { + use codex_tui::ComposerAction; + use codex_tui::ComposerInput; + use crossterm::event::KeyCode; + use crossterm::event::KeyEvent; + use crossterm::event::KeyModifiers; + use ratatui::buffer::Buffer; + use ratatui::layout::Rect; + + #[test] + #[ignore = "very slow"] + fn composer_input_renders_typed_characters() { + let mut composer = ComposerInput::new(); + let key = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE); + match composer.input(key) { + ComposerAction::Submitted(_) => panic!("unexpected submission"), + ComposerAction::None => {} + } + + let area = Rect::new(0, 0, 20, 5); + let mut buf = Buffer::empty(area); + composer.render_ref(area, &mut buf); + + let found = buf.content().iter().any(|cell| cell.symbol() == "a"); + assert!(found, "typed character was not rendered: {buf:?}"); + + composer.set_hint_items(vec![("⌃O", "env"), ("⌃C", "quit")]); + composer.render_ref(area, &mut buf); + let footer = buf + .content() + .iter() + .skip((area.width as usize) * (area.height as usize - 1)) + .map(ratatui::buffer::Cell::symbol) + .collect::>() + .join(""); + assert!(footer.contains("⌃O env")); + } +} diff --git a/codex-rs/cloud-tasks/src/new_task.rs b/codex-rs/cloud-tasks/src/new_task.rs new file mode 100644 index 0000000000..162fd3bb3a --- /dev/null +++ b/codex-rs/cloud-tasks/src/new_task.rs @@ -0,0 +1,35 @@ +use codex_tui::ComposerInput; + +pub struct NewTaskPage { + pub composer: ComposerInput, + pub submitting: bool, + pub env_id: Option, + pub best_of_n: usize, +} + +impl NewTaskPage { + pub fn new(env_id: Option, best_of_n: usize) -> Self { + let mut composer = ComposerInput::new(); + composer.set_hint_items(vec![ + ("⏎", "send"), + ("Shift+⏎", "newline"), + ("Ctrl+O", "env"), + ("Ctrl+N", "attempts"), + ("Ctrl+C", "quit"), + ]); + Self { + composer, + submitting: false, + env_id, + best_of_n, + } + } + + // Additional helpers can be added as usage evolves. +} + +impl Default for NewTaskPage { + fn default() -> Self { + Self::new(None, 1) + } +} diff --git a/codex-rs/cloud-tasks/src/scrollable_diff.rs b/codex-rs/cloud-tasks/src/scrollable_diff.rs new file mode 100644 index 0000000000..97dfb24895 --- /dev/null +++ b/codex-rs/cloud-tasks/src/scrollable_diff.rs @@ -0,0 +1,176 @@ +use unicode_width::UnicodeWidthChar; +use unicode_width::UnicodeWidthStr; + +/// Scroll position and geometry for a vertical scroll view. +#[derive(Clone, Copy, Debug, Default)] +pub struct ScrollViewState { + pub scroll: u16, + pub viewport_h: u16, + pub content_h: u16, +} + +impl ScrollViewState { + pub fn clamp(&mut self) { + let max_scroll = self.content_h.saturating_sub(self.viewport_h); + if self.scroll > max_scroll { + self.scroll = max_scroll; + } + } +} + +/// A simple, local scrollable view for diffs or message text. +/// +/// Owns raw lines, caches wrapped lines for a given width, and maintains +/// a small scroll state that is clamped whenever geometry shrinks. +#[derive(Clone, Debug, Default)] +pub struct ScrollableDiff { + raw: Vec, + wrapped: Vec, + wrapped_src_idx: Vec, + wrap_cols: Option, + pub state: ScrollViewState, +} + +impl ScrollableDiff { + pub fn new() -> Self { + Self::default() + } + + /// Replace the raw content lines. Does not rewrap immediately; call `set_width` next. + pub fn set_content(&mut self, lines: Vec) { + self.raw = lines; + self.wrapped.clear(); + self.wrapped_src_idx.clear(); + self.state.content_h = 0; + // Force rewrap on next set_width even if width is unchanged + self.wrap_cols = None; + } + + /// Set the wrap width. If changed, rebuild wrapped lines and clamp scroll. + pub fn set_width(&mut self, width: u16) { + if self.wrap_cols == Some(width) { + return; + } + self.wrap_cols = Some(width); + self.rewrap(width); + self.state.clamp(); + } + + /// Update viewport height and clamp scroll if needed. + pub fn set_viewport(&mut self, height: u16) { + self.state.viewport_h = height; + self.state.clamp(); + } + + /// Return the cached wrapped lines. Call `set_width` first when area changes. + pub fn wrapped_lines(&self) -> &[String] { + &self.wrapped + } + + pub fn wrapped_src_indices(&self) -> &[usize] { + &self.wrapped_src_idx + } + + pub fn raw_line_at(&self, idx: usize) -> &str { + self.raw.get(idx).map(String::as_str).unwrap_or("") + } + + /// Scroll by a signed delta; clamps to content. + pub fn scroll_by(&mut self, delta: i16) { + let s = self.state.scroll as i32 + delta as i32; + self.state.scroll = s.clamp(0, self.max_scroll() as i32) as u16; + } + + /// Page by a signed delta; typically viewport_h - 1. + pub fn page_by(&mut self, delta: i16) { + self.scroll_by(delta); + } + + pub fn to_top(&mut self) { + self.state.scroll = 0; + } + + pub fn to_bottom(&mut self) { + self.state.scroll = self.max_scroll(); + } + + /// Optional percent scrolled; None when not enough geometry is known. + pub fn percent_scrolled(&self) -> Option { + if self.state.content_h == 0 || self.state.viewport_h == 0 { + return None; + } + if self.state.content_h <= self.state.viewport_h { + return None; + } + let visible_bottom = self.state.scroll.saturating_add(self.state.viewport_h) as f32; + let pct = (visible_bottom / self.state.content_h as f32 * 100.0).round(); + Some(pct.clamp(0.0, 100.0) as u8) + } + + fn max_scroll(&self) -> u16 { + self.state.content_h.saturating_sub(self.state.viewport_h) + } + + fn rewrap(&mut self, width: u16) { + if width == 0 { + self.wrapped = self.raw.clone(); + self.state.content_h = self.wrapped.len() as u16; + return; + } + let max_cols = width as usize; + let mut out: Vec = Vec::new(); + let mut out_idx: Vec = Vec::new(); + for (raw_idx, raw) in self.raw.iter().enumerate() { + // Normalize tabs for width accounting (MVP: 4 spaces). + let raw = raw.replace('\t', " "); + if raw.is_empty() { + out.push(String::new()); + out_idx.push(raw_idx); + continue; + } + let mut line = String::new(); + let mut line_cols = 0usize; + let mut last_soft_idx: Option = None; // last whitespace or punctuation break + for (_i, ch) in raw.char_indices() { + if ch == '\n' { + out.push(std::mem::take(&mut line)); + out_idx.push(raw_idx); + line_cols = 0; + last_soft_idx = None; + continue; + } + let w = UnicodeWidthChar::width(ch).unwrap_or(0); + if line_cols.saturating_add(w) > max_cols { + if let Some(split) = last_soft_idx { + let (prefix, rest) = line.split_at(split); + out.push(prefix.trim_end().to_string()); + out_idx.push(raw_idx); + line = rest.trim_start().to_string(); + last_soft_idx = None; + // retry add current ch now that line may be shorter + } else if !line.is_empty() { + out.push(std::mem::take(&mut line)); + out_idx.push(raw_idx); + } + } + if ch.is_whitespace() + || matches!( + ch, + ',' | ';' | '.' | ':' | ')' | ']' | '}' | '|' | '/' | '?' | '!' | '-' | '_' + ) + { + last_soft_idx = Some(line.len()); + } + line.push(ch); + line_cols = UnicodeWidthStr::width(line.as_str()); + } + if !line.is_empty() { + out.push(line); + out_idx.push(raw_idx); + } + } + self.wrapped = out; + self.wrapped_src_idx = out_idx; + self.state.content_h = self.wrapped.len() as u16; + } +} diff --git a/codex-rs/cloud-tasks/src/ui.rs b/codex-rs/cloud-tasks/src/ui.rs new file mode 100644 index 0000000000..e3a97aeb3f --- /dev/null +++ b/codex-rs/cloud-tasks/src/ui.rs @@ -0,0 +1,1065 @@ +use ratatui::layout::Constraint; +use ratatui::layout::Direction; +use ratatui::layout::Layout; +use ratatui::prelude::*; +use ratatui::style::Color; +use ratatui::style::Modifier; +use ratatui::style::Style; +use ratatui::style::Stylize; +use ratatui::widgets::Block; +use ratatui::widgets::BorderType; +use ratatui::widgets::Borders; +use ratatui::widgets::Clear; +use ratatui::widgets::List; +use ratatui::widgets::ListItem; +use ratatui::widgets::ListState; +use ratatui::widgets::Padding; +use ratatui::widgets::Paragraph; +use std::sync::OnceLock; +use std::time::Instant; + +use crate::app::App; +use crate::app::AttemptView; +use chrono::Local; +use chrono::Utc; +use codex_cloud_tasks_client::AttemptStatus; +use codex_cloud_tasks_client::TaskStatus; +use codex_tui::render_markdown_text; + +pub fn draw(frame: &mut Frame, app: &mut App) { + let area = frame.area(); + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Min(1), // list + Constraint::Length(2), // two-line footer (help + status) + ]) + .split(area); + if app.new_task.is_some() { + draw_new_task_page(frame, chunks[0], app); + draw_footer(frame, chunks[1], app); + } else { + draw_list(frame, chunks[0], app); + draw_footer(frame, chunks[1], app); + } + + if app.diff_overlay.is_some() { + draw_diff_overlay(frame, area, app); + } + if app.env_modal.is_some() { + draw_env_modal(frame, area, app); + } + if app.best_of_modal.is_some() { + draw_best_of_modal(frame, area, app); + } + if app.apply_modal.is_some() { + draw_apply_modal(frame, area, app); + } +} + +// ===== Overlay helpers (geometry + styling) ===== +static ROUNDED: OnceLock = OnceLock::new(); + +fn rounded_enabled() -> bool { + *ROUNDED.get_or_init(|| { + std::env::var("CODEX_TUI_ROUNDED") + .ok() + .map(|v| v == "1") + .unwrap_or(true) + }) +} + +fn overlay_outer(area: Rect) -> Rect { + let outer_v = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Percentage(10), + Constraint::Percentage(80), + Constraint::Percentage(10), + ]) + .split(area)[1]; + Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage(10), + Constraint::Percentage(80), + Constraint::Percentage(10), + ]) + .split(outer_v)[1] +} + +fn overlay_block() -> Block<'static> { + let base = Block::default().borders(Borders::ALL); + let base = if rounded_enabled() { + base.border_type(BorderType::Rounded) + } else { + base + }; + base.padding(Padding::new(2, 2, 1, 1)) +} + +fn overlay_content(area: Rect) -> Rect { + overlay_block().inner(area) +} + +pub fn draw_new_task_page(frame: &mut Frame, area: Rect, app: &mut App) { + let title_spans = { + let mut spans: Vec = vec!["New Task".magenta().bold()]; + if let Some(id) = app + .new_task + .as_ref() + .and_then(|p| p.env_id.as_ref()) + .cloned() + { + spans.push(" • ".into()); + // Try to map id to label + let label = app + .environments + .iter() + .find(|r| r.id == id) + .and_then(|r| r.label.clone()) + .unwrap_or(id); + spans.push(label.dim()); + } else { + spans.push(" • ".into()); + spans.push("Env: none (press ctrl-o to choose)".red()); + } + if let Some(page) = app.new_task.as_ref() { + spans.push(" • ".into()); + let attempts = page.best_of_n; + let label = format!( + "{} attempt{}", + attempts, + if attempts == 1 { "" } else { "s" } + ); + spans.push(label.cyan()); + } + spans + }; + let block = Block::default() + .borders(Borders::ALL) + .title(Line::from(title_spans)); + + frame.render_widget(Clear, area); + frame.render_widget(block.clone(), area); + let content = block.inner(area); + + // Expand composer height up to (terminal height - 6), with a 3-line minimum. + let max_allowed = frame.area().height.saturating_sub(6).max(3); + let desired = app + .new_task + .as_ref() + .map(|p| p.composer.desired_height(content.width)) + .unwrap_or(3) + .clamp(3, max_allowed); + + // Anchor the composer to the bottom-left by allocating a flexible spacer + // above it and a fixed `desired`-height area for the composer. + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Min(1), Constraint::Length(desired)]) + .split(content); + let composer_area = rows[1]; + + if let Some(page) = app.new_task.as_ref() { + page.composer.render_ref(composer_area, frame.buffer_mut()); + // Composer renders its own footer hints; no extra row here. + } + + // Place cursor where composer wants it + if let Some(page) = app.new_task.as_ref() + && let Some((x, y)) = page.composer.cursor_pos(composer_area) + { + frame.set_cursor_position((x, y)); + } +} + +fn draw_list(frame: &mut Frame, area: Rect, app: &mut App) { + let items: Vec = app.tasks.iter().map(|t| render_task_item(app, t)).collect(); + + // Selection reflects the actual task index (no artificial spacer item). + let mut state = ListState::default().with_selected(Some(app.selected)); + // Dim task list when a modal/overlay is active to emphasize focus. + let dim_bg = app.env_modal.is_some() + || app.apply_modal.is_some() + || app.best_of_modal.is_some() + || app.diff_overlay.is_some(); + // Dynamic title includes current environment filter + let suffix_span = if let Some(ref id) = app.env_filter { + let label = app + .environments + .iter() + .find(|r| &r.id == id) + .and_then(|r| r.label.clone()) + .unwrap_or_else(|| "Selected".to_string()); + format!(" • {label}").dim() + } else { + " • All".dim() + }; + // Percent scrolled based on selection position in the list (0% at top, 100% at bottom). + let percent_span = if app.tasks.len() <= 1 { + " • 0%".dim() + } else { + let p = ((app.selected as f32) / ((app.tasks.len() - 1) as f32) * 100.0).round() as i32; + format!(" • {}%", p.clamp(0, 100)).dim() + }; + let title_line = { + let base = Line::from(vec!["Cloud Tasks".into(), suffix_span, percent_span]); + if dim_bg { + base.style(Style::default().add_modifier(Modifier::DIM)) + } else { + base + } + }; + let block = Block::default().borders(Borders::ALL).title(title_line); + // Render the outer block first + frame.render_widget(block.clone(), area); + // Draw list inside with a persistent top spacer row + let inner = block.inner(area); + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Length(1), Constraint::Min(1)]) + .split(inner); + let mut list = List::new(items) + .highlight_symbol("› ") + .highlight_style(Style::default().bold()); + if dim_bg { + list = list.style(Style::default().add_modifier(Modifier::DIM)); + } + frame.render_stateful_widget(list, rows[1], &mut state); + + // In-box spinner during initial/refresh loads + if app.refresh_inflight { + draw_centered_spinner(frame, inner, &mut app.spinner_start, "Loading tasks…"); + } +} + +fn draw_footer(frame: &mut Frame, area: Rect, app: &mut App) { + let mut help = vec![ + "↑/↓".dim(), + ": Move ".dim(), + "r".dim(), + ": Refresh ".dim(), + "Enter".dim(), + ": Open ".dim(), + ]; + // Apply hint; show disabled note when overlay is open without a diff. + if let Some(ov) = app.diff_overlay.as_ref() { + if !ov.current_can_apply() { + help.push("a".dim()); + help.push(": Apply (disabled) ".dim()); + } else { + help.push("a".dim()); + help.push(": Apply ".dim()); + } + if ov.attempt_count() > 1 { + help.push("Tab".dim()); + help.push(": Next attempt ".dim()); + help.push("[ ]".dim()); + help.push(": Cycle attempts ".dim()); + } + } else { + help.push("a".dim()); + help.push(": Apply ".dim()); + } + help.push("o : Set Env ".dim()); + if app.new_task.is_some() { + help.push("Ctrl+N".dim()); + help.push(format!(": Attempts {}x ", app.best_of_n).dim()); + help.push("(editing new task) ".dim()); + } else { + help.push("n : New Task ".dim()); + } + help.extend(vec!["q".dim(), ": Quit ".dim()]); + // Split footer area into two rows: help+spinner (top) and status (bottom) + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Length(1), Constraint::Length(1)]) + .split(area); + + // Top row: help text + spinner at right + let top = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Fill(1), Constraint::Length(18)]) + .split(rows[0]); + let para = Paragraph::new(Line::from(help)); + // Draw help text; avoid clearing the whole footer area every frame. + frame.render_widget(para, top[0]); + // Right side: spinner or clear the spinner area if idle to prevent stale glyphs. + if app.refresh_inflight + || app.details_inflight + || app.env_loading + || app.apply_preflight_inflight + || app.apply_inflight + { + draw_inline_spinner(frame, top[1], &mut app.spinner_start, "Loading…"); + } else { + frame.render_widget(Clear, top[1]); + } + + // Bottom row: status/log text across full width (single-line; sanitize newlines) + let mut status_line = app.status.replace('\n', " "); + if status_line.len() > 2000 { + // hard cap to avoid TUI noise + status_line.truncate(2000); + status_line.push('…'); + } + // Clear the status row to avoid trailing characters when the message shrinks. + frame.render_widget(Clear, rows[1]); + let status = Paragraph::new(status_line); + frame.render_widget(status, rows[1]); +} + +fn draw_diff_overlay(frame: &mut Frame, area: Rect, app: &mut App) { + let inner = overlay_outer(area); + if app.diff_overlay.is_none() { + return; + } + let ov_can_apply = app + .diff_overlay + .as_ref() + .map(super::app::DiffOverlay::current_can_apply) + .unwrap_or(false); + let is_error = app + .diff_overlay + .as_ref() + .and_then(|o| o.sd.wrapped_lines().first().cloned()) + .map(|s| s.trim_start().starts_with("Task failed:")) + .unwrap_or(false) + && !ov_can_apply; + let title = app + .diff_overlay + .as_ref() + .map(|o| o.title.clone()) + .unwrap_or_default(); + + // Title block + let title_ref = title.as_str(); + let mut title_spans: Vec = if is_error { + vec![ + "Details ".magenta(), + "[FAILED]".red().bold(), + " ".into(), + title_ref.magenta(), + ] + } else if ov_can_apply { + vec!["Diff: ".magenta(), title_ref.magenta()] + } else { + vec!["Details: ".magenta(), title_ref.magenta()] + }; + if let Some(p) = app + .diff_overlay + .as_ref() + .and_then(|o| o.sd.percent_scrolled()) + { + title_spans.push(" • ".dim()); + title_spans.push(format!("{p}%").dim()); + } + frame.render_widget(Clear, inner); + frame.render_widget( + overlay_block().title(Line::from(title_spans)).clone(), + inner, + ); + + // Content area and optional status bar + let content_full = overlay_content(inner); + let mut content_area = content_full; + if let Some(ov) = app.diff_overlay.as_mut() { + let has_text = ov.current_attempt().is_some_and(AttemptView::has_text); + let has_diff = ov.current_attempt().is_some_and(AttemptView::has_diff) || ov.base_can_apply; + if has_diff || has_text { + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Length(1), Constraint::Min(1)]) + .split(content_full); + // Status bar label + let mut spans: Vec = Vec::new(); + if has_diff && has_text { + let prompt_lbl = if matches!(ov.current_view, crate::app::DetailView::Prompt) { + "[Prompt]".magenta().bold() + } else { + "Prompt".dim() + }; + let diff_lbl = if matches!(ov.current_view, crate::app::DetailView::Diff) { + "[Diff]".magenta().bold() + } else { + "Diff".dim() + }; + spans.extend(vec![ + prompt_lbl, + " ".into(), + diff_lbl, + " ".into(), + "(← → to switch view)".dim(), + ]); + } else if has_text { + spans.push("Conversation".magenta().bold()); + } else { + spans.push("Diff".magenta().bold()); + } + if let Some(total) = ov.expected_attempts().or({ + if ov.attempts.is_empty() { + None + } else { + Some(ov.attempts.len()) + } + }) && total > 1 + { + spans.extend(vec![ + " ".into(), + format!("Attempt {}/{}", ov.selected_attempt + 1, total) + .bold() + .dim(), + " ".into(), + "(Tab/Shift-Tab or [ ] to cycle attempts)".dim(), + ]); + } + frame.render_widget(Paragraph::new(Line::from(spans)), rows[0]); + ov.sd.set_width(rows[1].width); + ov.sd.set_viewport(rows[1].height); + content_area = rows[1]; + } else { + ov.sd.set_width(content_full.width); + ov.sd.set_viewport(content_full.height); + content_area = content_full; + } + } + + // Styled content render + // Choose styling by the active view, not just presence of a diff + let is_diff_view = app + .diff_overlay + .as_ref() + .map(|o| matches!(o.current_view, crate::app::DetailView::Diff)) + .unwrap_or(false); + let styled_lines: Vec> = if is_diff_view { + let raw = app.diff_overlay.as_ref().map(|o| o.sd.wrapped_lines()); + raw.unwrap_or(&[]) + .iter() + .map(|l| style_diff_line(l)) + .collect() + } else { + app.diff_overlay + .as_ref() + .map(|o| style_conversation_lines(&o.sd, o.current_attempt())) + .unwrap_or_default() + }; + let raw_empty = app + .diff_overlay + .as_ref() + .map(|o| o.sd.wrapped_lines().is_empty()) + .unwrap_or(true); + if app.details_inflight && raw_empty { + draw_centered_spinner( + frame, + content_area, + &mut app.spinner_start, + "Loading details…", + ); + } else { + let scroll = app + .diff_overlay + .as_ref() + .map(|o| o.sd.state.scroll) + .unwrap_or(0); + let content = Paragraph::new(Text::from(styled_lines)).scroll((scroll, 0)); + frame.render_widget(content, content_area); + } +} + +pub fn draw_apply_modal(frame: &mut Frame, area: Rect, app: &mut App) { + use ratatui::widgets::Wrap; + let inner = overlay_outer(area); + let title = Line::from("Apply Changes?".magenta().bold()); + let block = overlay_block().title(title); + frame.render_widget(Clear, inner); + frame.render_widget(block.clone(), inner); + let content = overlay_content(inner); + + if let Some(m) = &app.apply_modal { + // Header + let header = Paragraph::new(Line::from( + format!("Apply '{}' ?", m.title).magenta().bold(), + )) + .wrap(Wrap { trim: true }); + // Footer instructions + let footer = + Paragraph::new(Line::from("Press Y to apply, P to preflight, N to cancel.").dim()) + .wrap(Wrap { trim: true }); + + // Split into header/body/footer + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Length(1), + Constraint::Min(1), + Constraint::Length(1), + ]) + .split(content); + + frame.render_widget(header, rows[0]); + // Body: spinner while preflight/apply runs; otherwise show result message and path lists + if app.apply_preflight_inflight { + draw_centered_spinner(frame, rows[1], &mut app.spinner_start, "Checking…"); + } else if app.apply_inflight { + draw_centered_spinner(frame, rows[1], &mut app.spinner_start, "Applying…"); + } else if m.result_message.is_none() { + draw_centered_spinner(frame, rows[1], &mut app.spinner_start, "Loading…"); + } else if let Some(msg) = &m.result_message { + let mut body_lines: Vec = Vec::new(); + let first = match m.result_level { + Some(crate::app::ApplyResultLevel::Success) => msg.clone().green(), + Some(crate::app::ApplyResultLevel::Partial) => msg.clone().magenta(), + Some(crate::app::ApplyResultLevel::Error) => msg.clone().red(), + None => msg.clone().into(), + }; + body_lines.push(Line::from(first)); + + // On partial or error, show conflicts/skips if present + if !matches!(m.result_level, Some(crate::app::ApplyResultLevel::Success)) { + use ratatui::text::Span; + if !m.conflict_paths.is_empty() { + body_lines.push(Line::from("")); + body_lines.push( + Line::from(format!("Conflicts ({}):", m.conflict_paths.len())) + .red() + .bold(), + ); + for p in &m.conflict_paths { + body_lines + .push(Line::from(vec![" • ".into(), Span::raw(p.clone()).dim()])); + } + } + if !m.skipped_paths.is_empty() { + body_lines.push(Line::from("")); + body_lines.push( + Line::from(format!("Skipped ({}):", m.skipped_paths.len())) + .magenta() + .bold(), + ); + for p in &m.skipped_paths { + body_lines + .push(Line::from(vec![" • ".into(), Span::raw(p.clone()).dim()])); + } + } + } + let body = Paragraph::new(body_lines).wrap(Wrap { trim: true }); + frame.render_widget(body, rows[1]); + } + frame.render_widget(footer, rows[2]); + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ConversationSpeaker { + User, + Assistant, +} + +fn style_conversation_lines( + sd: &crate::scrollable_diff::ScrollableDiff, + attempt: Option<&AttemptView>, +) -> Vec> { + use ratatui::text::Span; + + let wrapped = sd.wrapped_lines(); + if wrapped.is_empty() { + return Vec::new(); + } + + let indices = sd.wrapped_src_indices(); + let mut styled: Vec> = Vec::new(); + let mut speaker: Option = None; + let mut in_code = false; + let mut last_src: Option = None; + let mut bullet_indent: Option = None; + + for (display, &src_idx) in wrapped.iter().zip(indices.iter()) { + let raw = sd.raw_line_at(src_idx); + let trimmed = raw.trim(); + let is_new_raw = last_src.map(|prev| prev != src_idx).unwrap_or(true); + + if trimmed.eq_ignore_ascii_case("user:") { + speaker = Some(ConversationSpeaker::User); + in_code = false; + bullet_indent = None; + styled.push(conversation_header_line(ConversationSpeaker::User, None)); + last_src = Some(src_idx); + continue; + } + if trimmed.eq_ignore_ascii_case("assistant:") { + speaker = Some(ConversationSpeaker::Assistant); + in_code = false; + bullet_indent = None; + styled.push(conversation_header_line( + ConversationSpeaker::Assistant, + attempt, + )); + last_src = Some(src_idx); + continue; + } + if raw.is_empty() { + let mut spans: Vec = Vec::new(); + if let Some(role) = speaker { + spans.push(conversation_gutter_span(role)); + } else { + spans.push(Span::raw(String::new())); + } + styled.push(Line::from(spans)); + last_src = Some(src_idx); + bullet_indent = None; + continue; + } + + if is_new_raw { + let trimmed_start = raw.trim_start(); + if trimmed_start.starts_with("```") { + in_code = !in_code; + bullet_indent = None; + } else if !in_code + && (trimmed_start.starts_with("- ") || trimmed_start.starts_with("* ")) + { + let indent = raw.chars().take_while(|c| c.is_whitespace()).count(); + bullet_indent = Some(indent); + } else if !in_code { + bullet_indent = None; + } + } + + let mut spans: Vec = Vec::new(); + if let Some(role) = speaker { + spans.push(conversation_gutter_span(role)); + } + + spans.extend(conversation_text_spans( + display, + in_code, + is_new_raw, + bullet_indent, + )); + + styled.push(Line::from(spans)); + last_src = Some(src_idx); + } + + if styled.is_empty() { + wrapped.iter().map(|l| Line::from(l.to_string())).collect() + } else { + styled + } +} + +fn conversation_header_line( + speaker: ConversationSpeaker, + attempt: Option<&AttemptView>, +) -> Line<'static> { + use ratatui::text::Span; + + let mut spans: Vec = vec!["╭ ".dim()]; + match speaker { + ConversationSpeaker::User => { + spans.push("User".cyan().bold()); + spans.push(" prompt".dim()); + } + ConversationSpeaker::Assistant => { + spans.push("Assistant".magenta().bold()); + spans.push(" response".dim()); + if let Some(attempt) = attempt + && let Some(status_span) = attempt_status_span(attempt.status) + { + spans.push(" • ".dim()); + spans.push(status_span); + } + } + } + Line::from(spans) +} + +fn conversation_gutter_span(speaker: ConversationSpeaker) -> ratatui::text::Span<'static> { + match speaker { + ConversationSpeaker::User => "│ ".cyan().dim(), + ConversationSpeaker::Assistant => "│ ".magenta().dim(), + } +} + +fn conversation_text_spans( + display: &str, + in_code: bool, + is_new_raw: bool, + bullet_indent: Option, +) -> Vec> { + use ratatui::text::Span; + + if in_code { + return vec![Span::styled( + display.to_string(), + Style::default().fg(Color::Cyan), + )]; + } + + let trimmed = display.trim_start(); + + if let Some(indent) = bullet_indent { + if is_new_raw { + let rest = trimmed.get(2..).unwrap_or("").trim_start(); + let mut spans: Vec = Vec::new(); + if indent > 0 { + spans.push(Span::raw(" ".repeat(indent))); + } + spans.push("• ".into()); + spans.push(Span::raw(rest.to_string())); + return spans; + } + let mut continuation = String::new(); + continuation.push_str(&" ".repeat(indent + 2)); + continuation.push_str(trimmed); + return vec![Span::raw(continuation)]; + } + + if is_new_raw + && (trimmed.starts_with("### ") || trimmed.starts_with("## ") || trimmed.starts_with("# ")) + { + return vec![Span::styled( + display.to_string(), + Style::default() + .fg(Color::Magenta) + .add_modifier(Modifier::BOLD), + )]; + } + + let mut rendered = render_markdown_text(display); + if rendered.lines.is_empty() { + return vec![Span::raw(display.to_string())]; + } + // `render_markdown_text` can yield multiple lines when the input contains + // explicit breaks. We only expect a single line here; join the spans of the + // first rendered line for styling. + rendered.lines.remove(0).spans.into_iter().collect() +} + +fn attempt_status_span(status: AttemptStatus) -> Option> { + match status { + AttemptStatus::Completed => Some("Completed".green()), + AttemptStatus::Failed => Some("Failed".red().bold()), + AttemptStatus::InProgress => Some("In progress".magenta()), + AttemptStatus::Pending => Some("Pending".cyan()), + AttemptStatus::Cancelled => Some("Cancelled".dim()), + AttemptStatus::Unknown => None, + } +} + +fn style_diff_line(raw: &str) -> Line<'static> { + use ratatui::style::Color; + use ratatui::style::Modifier; + use ratatui::style::Style; + use ratatui::text::Span; + + if raw.starts_with("@@") { + return Line::from(vec![Span::styled( + raw.to_string(), + Style::default() + .fg(Color::Magenta) + .add_modifier(Modifier::BOLD), + )]); + } + if raw.starts_with("+++") || raw.starts_with("---") { + return Line::from(vec![Span::styled( + raw.to_string(), + Style::default().add_modifier(Modifier::DIM), + )]); + } + if raw.starts_with('+') { + return Line::from(vec![Span::styled( + raw.to_string(), + Style::default().fg(Color::Green), + )]); + } + if raw.starts_with('-') { + return Line::from(vec![Span::styled( + raw.to_string(), + Style::default().fg(Color::Red), + )]); + } + Line::from(vec![Span::raw(raw.to_string())]) +} + +fn render_task_item(_app: &App, t: &codex_cloud_tasks_client::TaskSummary) -> ListItem<'static> { + let status = match t.status { + TaskStatus::Ready => "READY".green(), + TaskStatus::Pending => "PENDING".magenta(), + TaskStatus::Applied => "APPLIED".blue(), + TaskStatus::Error => "ERROR".red(), + }; + + // Title line: [STATUS] Title + let title = Line::from(vec![ + "[".into(), + status, + "] ".into(), + t.title.clone().into(), + ]); + + // Meta line: environment label and relative time (dim) + let mut meta: Vec = Vec::new(); + if let Some(lbl) = t.environment_label.as_ref().filter(|s| !s.is_empty()) { + meta.push(lbl.clone().dim()); + } + let when = format_relative_time(t.updated_at).dim(); + if !meta.is_empty() { + meta.push(" ".into()); + meta.push("•".dim()); + meta.push(" ".into()); + } + meta.push(when); + let meta_line = Line::from(meta); + + // Subline: summary when present; otherwise show "no diff" + let sub = if t.summary.files_changed > 0 + || t.summary.lines_added > 0 + || t.summary.lines_removed > 0 + { + let adds = t.summary.lines_added; + let dels = t.summary.lines_removed; + let files = t.summary.files_changed; + Line::from(vec![ + format!("+{adds}").green(), + "/".into(), + format!("−{dels}").red(), + " ".into(), + "•".dim(), + " ".into(), + format!("{files}").into(), + " ".into(), + "files".dim(), + ]) + } else { + Line::from("no diff".to_string().dim()) + }; + + // Insert a blank spacer line after the summary to separate tasks + let spacer = Line::from(""); + ListItem::new(vec![title, meta_line, sub, spacer]) +} + +fn format_relative_time(ts: chrono::DateTime) -> String { + let now = Utc::now(); + let mut secs = (now - ts).num_seconds(); + if secs < 0 { + secs = 0; + } + if secs < 60 { + return format!("{secs}s ago"); + } + let mins = secs / 60; + if mins < 60 { + return format!("{mins}m ago"); + } + let hours = mins / 60; + if hours < 24 { + return format!("{hours}h ago"); + } + let local = ts.with_timezone(&Local); + local.format("%b %e %H:%M").to_string() +} + +fn draw_inline_spinner( + frame: &mut Frame, + area: Rect, + spinner_start: &mut Option, + label: &str, +) { + use ratatui::widgets::Paragraph; + let start = spinner_start.get_or_insert_with(Instant::now); + let blink_on = (start.elapsed().as_millis() / 600).is_multiple_of(2); + let dot = if blink_on { + "• ".into() + } else { + "◦ ".dim() + }; + let label = label.cyan(); + let line = Line::from(vec![dot, label]); + frame.render_widget(Paragraph::new(line), area); +} + +fn draw_centered_spinner( + frame: &mut Frame, + area: Rect, + spinner_start: &mut Option, + label: &str, +) { + // Center a 1xN spinner within the given rect + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Percentage(50), + Constraint::Length(1), + Constraint::Percentage(49), + ]) + .split(area); + let cols = Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage(50), + Constraint::Length(18), + Constraint::Percentage(50), + ]) + .split(rows[1]); + draw_inline_spinner(frame, cols[1], spinner_start, label); +} + +// Styling helpers for diff rendering live inline where used. + +pub fn draw_env_modal(frame: &mut Frame, area: Rect, app: &mut App) { + use ratatui::widgets::Wrap; + + // Use shared overlay geometry and padding. + let inner = overlay_outer(area); + + // Title: primary only; move long hints to a subheader inside content. + let title = Line::from(vec!["Select Environment".magenta().bold()]); + let block = overlay_block().title(title); + + frame.render_widget(Clear, inner); + frame.render_widget(block.clone(), inner); + let content = overlay_content(inner); + + if app.env_loading { + draw_centered_spinner( + frame, + content, + &mut app.spinner_start, + "Loading environments…", + ); + return; + } + + // Layout: subheader + search + results list + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Length(1), // subheader + Constraint::Length(1), // search + Constraint::Min(1), // list + ]) + .split(content); + + // Subheader with usage hints (dim cyan) + let subheader = Paragraph::new(Line::from( + "Type to search, Enter select, Esc cancel".cyan().dim(), + )) + .wrap(Wrap { trim: true }); + frame.render_widget(subheader, rows[0]); + + let query = app + .env_modal + .as_ref() + .map(|m| m.query.clone()) + .unwrap_or_default(); + let ql = query.to_lowercase(); + let search = Paragraph::new(format!("Search: {query}")).wrap(Wrap { trim: true }); + frame.render_widget(search, rows[1]); + + // Filter environments by query (case-insensitive substring over label/id/hints) + let envs: Vec<&crate::app::EnvironmentRow> = app + .environments + .iter() + .filter(|e| { + if ql.is_empty() { + return true; + } + let mut hay = String::new(); + if let Some(l) = &e.label { + hay.push_str(&l.to_lowercase()); + hay.push(' '); + } + hay.push_str(&e.id.to_lowercase()); + if let Some(h) = &e.repo_hints { + hay.push(' '); + hay.push_str(&h.to_lowercase()); + } + hay.contains(&ql) + }) + .collect(); + + let mut items: Vec = Vec::new(); + items.push(ListItem::new(Line::from("All Environments (Global)"))); + for env in envs.iter() { + let primary = env.label.clone().unwrap_or_else(|| "".to_string()); + let mut spans: Vec = vec![primary.into()]; + if env.is_pinned { + spans.push(" ".into()); + spans.push("PINNED".magenta().bold()); + } + spans.push(" ".into()); + spans.push(env.id.clone().dim()); + if let Some(hint) = &env.repo_hints { + spans.push(" ".into()); + spans.push(hint.clone().dim()); + } + items.push(ListItem::new(Line::from(spans))); + } + + let sel_desired = app.env_modal.as_ref().map(|m| m.selected).unwrap_or(0); + let sel = sel_desired.min(envs.len()); + let mut list_state = ListState::default().with_selected(Some(sel)); + let list = List::new(items) + .highlight_symbol("› ") + .highlight_style(Style::default().bold()) + .block(Block::default().borders(Borders::NONE)); + frame.render_stateful_widget(list, rows[2], &mut list_state); +} + +pub fn draw_best_of_modal(frame: &mut Frame, area: Rect, app: &mut App) { + use ratatui::widgets::Wrap; + + let inner = overlay_outer(area); + const MAX_WIDTH: u16 = 40; + const MIN_WIDTH: u16 = 20; + const MAX_HEIGHT: u16 = 12; + const MIN_HEIGHT: u16 = 6; + let modal_width = inner.width.min(MAX_WIDTH).max(inner.width.min(MIN_WIDTH)); + let modal_height = inner + .height + .min(MAX_HEIGHT) + .max(inner.height.min(MIN_HEIGHT)); + let modal_x = inner.x + (inner.width.saturating_sub(modal_width)) / 2; + let modal_y = inner.y + (inner.height.saturating_sub(modal_height)) / 2; + let modal_area = Rect::new(modal_x, modal_y, modal_width, modal_height); + let title = Line::from(vec!["Parallel Attempts".magenta().bold()]); + let block = overlay_block().title(title); + + frame.render_widget(Clear, modal_area); + frame.render_widget(block.clone(), modal_area); + let content = overlay_content(modal_area); + + let rows = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Length(2), Constraint::Min(1)]) + .split(content); + + let hint = Paragraph::new(Line::from("Use ↑/↓ to choose, 1-4 jump".cyan().dim())) + .wrap(Wrap { trim: true }); + frame.render_widget(hint, rows[0]); + + let selected = app.best_of_modal.as_ref().map(|m| m.selected).unwrap_or(0); + let options = [1usize, 2, 3, 4]; + let mut items: Vec = Vec::new(); + for &attempts in &options { + let noun = if attempts == 1 { "attempt" } else { "attempts" }; + let mut spans: Vec = vec![format!("{attempts} {noun:<8}").into()]; + spans.push(" ".into()); + spans.push(format!("{attempts}x parallel").dim()); + if attempts == app.best_of_n { + spans.push(" ".into()); + spans.push("Current".magenta().bold()); + } + items.push(ListItem::new(Line::from(spans))); + } + let sel = selected.min(options.len().saturating_sub(1)); + let mut list_state = ListState::default().with_selected(Some(sel)); + let list = List::new(items) + .highlight_symbol("› ") + .highlight_style(Style::default().bold()) + .block(Block::default().borders(Borders::NONE)); + frame.render_stateful_widget(list, rows[1], &mut list_state); +} diff --git a/codex-rs/cloud-tasks/src/util.rs b/codex-rs/cloud-tasks/src/util.rs new file mode 100644 index 0000000000..1c690b26c0 --- /dev/null +++ b/codex-rs/cloud-tasks/src/util.rs @@ -0,0 +1,122 @@ +use base64::Engine as _; +use chrono::Utc; +use reqwest::header::HeaderMap; + +use codex_core::config::Config; +use codex_core::config::ConfigOverrides; +use codex_login::AuthManager; + +pub fn set_user_agent_suffix(suffix: &str) { + if let Ok(mut guard) = codex_core::default_client::USER_AGENT_SUFFIX.lock() { + guard.replace(suffix.to_string()); + } +} + +pub fn append_error_log(message: impl AsRef) { + let ts = Utc::now().to_rfc3339(); + if let Ok(mut f) = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open("error.log") + { + use std::io::Write as _; + let _ = writeln!(f, "[{ts}] {}", message.as_ref()); + } +} + +/// Normalize the configured base URL to a canonical form used by the backend client. +/// - trims trailing '/' +/// - appends '/backend-api' for ChatGPT hosts when missing +pub fn normalize_base_url(input: &str) -> String { + let mut base_url = input.to_string(); + while base_url.ends_with('/') { + base_url.pop(); + } + if (base_url.starts_with("https://chatgpt.com") + || base_url.starts_with("https://chat.openai.com")) + && !base_url.contains("/backend-api") + { + base_url = format!("{base_url}/backend-api"); + } + base_url +} + +/// Extract the ChatGPT account id from a JWT token, when present. +pub fn extract_chatgpt_account_id(token: &str) -> Option { + let mut parts = token.split('.'); + let (_h, payload_b64, _s) = match (parts.next(), parts.next(), parts.next()) { + (Some(h), Some(p), Some(s)) if !h.is_empty() && !p.is_empty() && !s.is_empty() => (h, p, s), + _ => return None, + }; + let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD + .decode(payload_b64) + .ok()?; + let v: serde_json::Value = serde_json::from_slice(&payload_bytes).ok()?; + v.get("https://api.openai.com/auth") + .and_then(|auth| auth.get("chatgpt_account_id")) + .and_then(|id| id.as_str()) + .map(str::to_string) +} + +pub async fn load_auth_manager() -> Option { + // TODO: pass in cli overrides once cloud tasks properly support them. + let config = Config::load_with_cli_overrides(Vec::new(), ConfigOverrides::default()) + .await + .ok()?; + Some(AuthManager::new( + config.codex_home, + false, + config.cli_auth_credentials_store_mode, + )) +} + +/// Build headers for ChatGPT-backed requests: `User-Agent`, optional `Authorization`, +/// and optional `ChatGPT-Account-Id`. +pub async fn build_chatgpt_headers() -> HeaderMap { + use reqwest::header::AUTHORIZATION; + use reqwest::header::HeaderName; + use reqwest::header::HeaderValue; + use reqwest::header::USER_AGENT; + + set_user_agent_suffix("codex_cloud_tasks_tui"); + let ua = codex_core::default_client::get_codex_user_agent(); + let mut headers = HeaderMap::new(); + headers.insert( + USER_AGENT, + HeaderValue::from_str(&ua).unwrap_or(HeaderValue::from_static("codex-cli")), + ); + if let Some(am) = load_auth_manager().await + && let Some(auth) = am.auth() + && let Ok(tok) = auth.get_token().await + && !tok.is_empty() + { + let v = format!("Bearer {tok}"); + if let Ok(hv) = HeaderValue::from_str(&v) { + headers.insert(AUTHORIZATION, hv); + } + if let Some(acc) = auth + .get_account_id() + .or_else(|| extract_chatgpt_account_id(&tok)) + && let Ok(name) = HeaderName::from_bytes(b"ChatGPT-Account-Id") + && let Ok(hv) = HeaderValue::from_str(&acc) + { + headers.insert(name, hv); + } + } + headers +} + +/// Construct a browser-friendly task URL for the given backend base URL. +pub fn task_url(base_url: &str, task_id: &str) -> String { + let normalized = normalize_base_url(base_url); + if let Some(root) = normalized.strip_suffix("/backend-api") { + return format!("{root}/codex/tasks/{task_id}"); + } + if let Some(root) = normalized.strip_suffix("/api/codex") { + return format!("{root}/codex/tasks/{task_id}"); + } + if normalized.ends_with("/codex") { + return format!("{normalized}/tasks/{task_id}"); + } + format!("{normalized}/codex/tasks/{task_id}") +} diff --git a/codex-rs/cloud-tasks/tests/env_filter.rs b/codex-rs/cloud-tasks/tests/env_filter.rs new file mode 100644 index 0000000000..8c737c6c28 --- /dev/null +++ b/codex-rs/cloud-tasks/tests/env_filter.rs @@ -0,0 +1,22 @@ +use codex_cloud_tasks_client::CloudBackend; +use codex_cloud_tasks_client::MockClient; + +#[tokio::test] +async fn mock_backend_varies_by_env() { + let client = MockClient; + + let root = CloudBackend::list_tasks(&client, None).await.unwrap(); + assert!(root.iter().any(|t| t.title.contains("Update README"))); + + let a = CloudBackend::list_tasks(&client, Some("env-A")) + .await + .unwrap(); + assert_eq!(a.len(), 1); + assert_eq!(a[0].title, "A: First"); + + let b = CloudBackend::list_tasks(&client, Some("env-B")) + .await + .unwrap(); + assert_eq!(b.len(), 2); + assert!(b[0].title.starts_with("B: ")); +} diff --git a/codex-rs/code b/codex-rs/code new file mode 100644 index 0000000000..e69de29bb2 diff --git a/codex-rs/codex-backend-openapi-models/Cargo.toml b/codex-rs/codex-backend-openapi-models/Cargo.toml new file mode 100644 index 0000000000..1a600495b9 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "codex-backend-openapi-models" +version = { workspace = true } +edition = "2024" + +[lib] +name = "codex_backend_openapi_models" +path = "src/lib.rs" + +# Important: generated code often violates our workspace lints. +# Allow unwrap/expect in this crate so the workspace builds cleanly +# after models are regenerated. +# Lint overrides are applied in src/lib.rs via crate attributes + +[dependencies] +serde = { version = "1", features = ["derive"] } +serde_json = "1" +serde_with = "3" + +[package.metadata.cargo-shear] +ignored = ["serde_with"] diff --git a/codex-rs/codex-backend-openapi-models/src/lib.rs b/codex-rs/codex-backend-openapi-models/src/lib.rs new file mode 100644 index 0000000000..f9e6d52fa8 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/lib.rs @@ -0,0 +1,6 @@ +#![allow(clippy::unwrap_used, clippy::expect_used)] + +// Re-export generated OpenAPI models. +// The regen script populates `src/models/*.rs` and writes `src/models/mod.rs`. +// This module intentionally contains no hand-written types. +pub mod models; diff --git a/codex-rs/codex-backend-openapi-models/src/models/code_task_details_response.rs b/codex-rs/codex-backend-openapi-models/src/models/code_task_details_response.rs new file mode 100644 index 0000000000..725b3a3737 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/code_task_details_response.rs @@ -0,0 +1,42 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct CodeTaskDetailsResponse { + #[serde(rename = "task")] + pub task: Box, + #[serde(rename = "current_user_turn", skip_serializing_if = "Option::is_none")] + pub current_user_turn: Option>, + #[serde( + rename = "current_assistant_turn", + skip_serializing_if = "Option::is_none" + )] + pub current_assistant_turn: Option>, + #[serde( + rename = "current_diff_task_turn", + skip_serializing_if = "Option::is_none" + )] + pub current_diff_task_turn: Option>, +} + +impl CodeTaskDetailsResponse { + pub fn new(task: models::TaskResponse) -> CodeTaskDetailsResponse { + CodeTaskDetailsResponse { + task: Box::new(task), + current_user_turn: None, + current_assistant_turn: None, + current_diff_task_turn: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/external_pull_request_response.rs b/codex-rs/codex-backend-openapi-models/src/models/external_pull_request_response.rs new file mode 100644 index 0000000000..92b56db2ca --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/external_pull_request_response.rs @@ -0,0 +1,40 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct ExternalPullRequestResponse { + #[serde(rename = "id")] + pub id: String, + #[serde(rename = "assistant_turn_id")] + pub assistant_turn_id: String, + #[serde(rename = "pull_request")] + pub pull_request: Box, + #[serde(rename = "codex_updated_sha", skip_serializing_if = "Option::is_none")] + pub codex_updated_sha: Option, +} + +impl ExternalPullRequestResponse { + pub fn new( + id: String, + assistant_turn_id: String, + pull_request: models::GitPullRequest, + ) -> ExternalPullRequestResponse { + ExternalPullRequestResponse { + id, + assistant_turn_id, + pull_request: Box::new(pull_request), + codex_updated_sha: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/git_pull_request.rs b/codex-rs/codex-backend-openapi-models/src/models/git_pull_request.rs new file mode 100644 index 0000000000..a7e995f3ef --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/git_pull_request.rs @@ -0,0 +1,77 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct GitPullRequest { + #[serde(rename = "number")] + pub number: i32, + #[serde(rename = "url")] + pub url: String, + #[serde(rename = "state")] + pub state: String, + #[serde(rename = "merged")] + pub merged: bool, + #[serde(rename = "mergeable")] + pub mergeable: bool, + #[serde(rename = "draft", skip_serializing_if = "Option::is_none")] + pub draft: Option, + #[serde(rename = "title", skip_serializing_if = "Option::is_none")] + pub title: Option, + #[serde(rename = "body", skip_serializing_if = "Option::is_none")] + pub body: Option, + #[serde(rename = "base", skip_serializing_if = "Option::is_none")] + pub base: Option, + #[serde(rename = "head", skip_serializing_if = "Option::is_none")] + pub head: Option, + #[serde(rename = "base_sha", skip_serializing_if = "Option::is_none")] + pub base_sha: Option, + #[serde(rename = "head_sha", skip_serializing_if = "Option::is_none")] + pub head_sha: Option, + #[serde(rename = "merge_commit_sha", skip_serializing_if = "Option::is_none")] + pub merge_commit_sha: Option, + #[serde(rename = "comments", skip_serializing_if = "Option::is_none")] + pub comments: Option, + #[serde(rename = "diff", skip_serializing_if = "Option::is_none")] + pub diff: Option, + #[serde(rename = "user", skip_serializing_if = "Option::is_none")] + pub user: Option, +} + +impl GitPullRequest { + pub fn new( + number: i32, + url: String, + state: String, + merged: bool, + mergeable: bool, + ) -> GitPullRequest { + GitPullRequest { + number, + url, + state, + merged, + mergeable, + draft: None, + title: None, + body: None, + base: None, + head: None, + base_sha: None, + head_sha: None, + merge_commit_sha: None, + comments: None, + diff: None, + user: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/mod.rs b/codex-rs/codex-backend-openapi-models/src/models/mod.rs new file mode 100644 index 0000000000..96348d72c2 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/mod.rs @@ -0,0 +1,34 @@ +// Curated minimal export list for current workspace usage. +// NOTE: This file was previously auto-generated by the OpenAPI generator. +// Currently export only the types referenced by the workspace +// The process for this will change + +// Cloud Tasks +pub mod code_task_details_response; +pub use self::code_task_details_response::CodeTaskDetailsResponse; + +pub mod task_response; +pub use self::task_response::TaskResponse; + +pub mod external_pull_request_response; +pub use self::external_pull_request_response::ExternalPullRequestResponse; + +pub mod git_pull_request; +pub use self::git_pull_request::GitPullRequest; + +pub mod task_list_item; +pub use self::task_list_item::TaskListItem; + +pub mod paginated_list_task_list_item_; +pub use self::paginated_list_task_list_item_::PaginatedListTaskListItem; + +// Rate Limits +pub mod rate_limit_status_payload; +pub use self::rate_limit_status_payload::PlanType; +pub use self::rate_limit_status_payload::RateLimitStatusPayload; + +pub mod rate_limit_status_details; +pub use self::rate_limit_status_details::RateLimitStatusDetails; + +pub mod rate_limit_window_snapshot; +pub use self::rate_limit_window_snapshot::RateLimitWindowSnapshot; diff --git a/codex-rs/codex-backend-openapi-models/src/models/paginated_list_task_list_item_.rs b/codex-rs/codex-backend-openapi-models/src/models/paginated_list_task_list_item_.rs new file mode 100644 index 0000000000..5af75afaab --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/paginated_list_task_list_item_.rs @@ -0,0 +1,30 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct PaginatedListTaskListItem { + #[serde(rename = "items")] + pub items: Vec, + #[serde(rename = "cursor", skip_serializing_if = "Option::is_none")] + pub cursor: Option, +} + +impl PaginatedListTaskListItem { + pub fn new(items: Vec) -> PaginatedListTaskListItem { + PaginatedListTaskListItem { + items, + cursor: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_details.rs b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_details.rs new file mode 100644 index 0000000000..ca9fdfe240 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_details.rs @@ -0,0 +1,46 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct RateLimitStatusDetails { + #[serde(rename = "allowed")] + pub allowed: bool, + #[serde(rename = "limit_reached")] + pub limit_reached: bool, + #[serde( + rename = "primary_window", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub primary_window: Option>>, + #[serde( + rename = "secondary_window", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub secondary_window: Option>>, +} + +impl RateLimitStatusDetails { + pub fn new(allowed: bool, limit_reached: bool) -> RateLimitStatusDetails { + RateLimitStatusDetails { + allowed, + limit_reached, + primary_window: None, + secondary_window: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs new file mode 100644 index 0000000000..d2af76f4d7 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs @@ -0,0 +1,65 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct RateLimitStatusPayload { + #[serde(rename = "plan_type")] + pub plan_type: PlanType, + #[serde( + rename = "rate_limit", + default, + with = "::serde_with::rust::double_option", + skip_serializing_if = "Option::is_none" + )] + pub rate_limit: Option>>, +} + +impl RateLimitStatusPayload { + pub fn new(plan_type: PlanType) -> RateLimitStatusPayload { + RateLimitStatusPayload { + plan_type, + rate_limit: None, + } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum PlanType { + #[serde(rename = "free")] + Free, + #[serde(rename = "go")] + Go, + #[serde(rename = "plus")] + Plus, + #[serde(rename = "pro")] + Pro, + #[serde(rename = "team")] + Team, + #[serde(rename = "business")] + Business, + #[serde(rename = "education")] + Education, + #[serde(rename = "quorum")] + Quorum, + #[serde(rename = "enterprise")] + Enterprise, + #[serde(rename = "edu")] + Edu, +} + +impl Default for PlanType { + fn default() -> PlanType { + Self::Free + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_window_snapshot.rs b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_window_snapshot.rs new file mode 100644 index 0000000000..4fc04f4be6 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_window_snapshot.rs @@ -0,0 +1,40 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct RateLimitWindowSnapshot { + #[serde(rename = "used_percent")] + pub used_percent: i32, + #[serde(rename = "limit_window_seconds")] + pub limit_window_seconds: i32, + #[serde(rename = "reset_after_seconds")] + pub reset_after_seconds: i32, + #[serde(rename = "reset_at")] + pub reset_at: i32, +} + +impl RateLimitWindowSnapshot { + pub fn new( + used_percent: i32, + limit_window_seconds: i32, + reset_after_seconds: i32, + reset_at: i32, + ) -> RateLimitWindowSnapshot { + RateLimitWindowSnapshot { + used_percent, + limit_window_seconds, + reset_after_seconds, + reset_at, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/task_list_item.rs b/codex-rs/codex-backend-openapi-models/src/models/task_list_item.rs new file mode 100644 index 0000000000..5f34738a43 --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/task_list_item.rs @@ -0,0 +1,63 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct TaskListItem { + #[serde(rename = "id")] + pub id: String, + #[serde(rename = "title")] + pub title: String, + #[serde( + rename = "has_generated_title", + skip_serializing_if = "Option::is_none" + )] + pub has_generated_title: Option, + #[serde(rename = "updated_at", skip_serializing_if = "Option::is_none")] + pub updated_at: Option, + #[serde(rename = "created_at", skip_serializing_if = "Option::is_none")] + pub created_at: Option, + #[serde( + rename = "task_status_display", + skip_serializing_if = "Option::is_none" + )] + pub task_status_display: Option>, + #[serde(rename = "archived")] + pub archived: bool, + #[serde(rename = "has_unread_turn")] + pub has_unread_turn: bool, + #[serde(rename = "pull_requests", skip_serializing_if = "Option::is_none")] + pub pull_requests: Option>, +} + +impl TaskListItem { + pub fn new( + id: String, + title: String, + has_generated_title: Option, + archived: bool, + has_unread_turn: bool, + ) -> TaskListItem { + TaskListItem { + id, + title, + has_generated_title, + updated_at: None, + created_at: None, + task_status_display: None, + archived, + has_unread_turn, + pull_requests: None, + } + } +} diff --git a/codex-rs/codex-backend-openapi-models/src/models/task_response.rs b/codex-rs/codex-backend-openapi-models/src/models/task_response.rs new file mode 100644 index 0000000000..6251b56b7e --- /dev/null +++ b/codex-rs/codex-backend-openapi-models/src/models/task_response.rs @@ -0,0 +1,62 @@ +/* + * codex-backend + * + * codex-backend + * + * The version of the OpenAPI document: 0.0.1 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct TaskResponse { + #[serde(rename = "id")] + pub id: String, + #[serde(rename = "created_at", skip_serializing_if = "Option::is_none")] + pub created_at: Option, + #[serde(rename = "title")] + pub title: String, + #[serde( + rename = "has_generated_title", + skip_serializing_if = "Option::is_none" + )] + pub has_generated_title: Option, + #[serde(rename = "current_turn_id", skip_serializing_if = "Option::is_none")] + pub current_turn_id: Option, + #[serde(rename = "has_unread_turn", skip_serializing_if = "Option::is_none")] + pub has_unread_turn: Option, + #[serde( + rename = "denormalized_metadata", + skip_serializing_if = "Option::is_none" + )] + pub denormalized_metadata: Option>, + #[serde(rename = "archived")] + pub archived: bool, + #[serde(rename = "external_pull_requests")] + pub external_pull_requests: Vec, +} + +impl TaskResponse { + pub fn new( + id: String, + title: String, + archived: bool, + external_pull_requests: Vec, + ) -> TaskResponse { + TaskResponse { + id, + created_at: None, + title, + has_generated_title: None, + current_turn_id: None, + has_unread_turn: None, + denormalized_metadata: None, + archived, + external_pull_requests, + } + } +} diff --git a/codex-rs/common/Cargo.toml b/codex-rs/common/Cargo.toml index b10600574c..cff9c4b307 100644 --- a/codex-rs/common/Cargo.toml +++ b/codex-rs/common/Cargo.toml @@ -7,11 +7,15 @@ version = { workspace = true } workspace = true [dependencies] -clap = { version = "4", features = ["derive", "wrap_help"], optional = true } -codex-core = { path = "../core" } -codex-protocol = { path = "../protocol" } -serde = { version = "1", optional = true } -toml = { version = "0.9", optional = true } +clap = { workspace = true, features = ["derive", "wrap_help"], optional = true } +codex-app-server-protocol = { workspace = true } +codex-core = { workspace = true } +codex-lmstudio = { workspace = true } +codex-ollama = { workspace = true } +codex-protocol = { workspace = true } +once_cell = { workspace = true } +serde = { workspace = true, optional = true } +toml = { workspace = true, optional = true } [features] # Separate feature so that `clap` is not a mandatory dependency. diff --git a/codex-rs/common/src/approval_presets.rs b/codex-rs/common/src/approval_presets.rs index 6dc0cff0ee..6c3bf395ad 100644 --- a/codex-rs/common/src/approval_presets.rs +++ b/codex-rs/common/src/approval_presets.rs @@ -24,21 +24,21 @@ pub fn builtin_approval_presets() -> Vec { ApprovalPreset { id: "read-only", label: "Read Only", - description: "Codex can read files and answer questions. Codex requires approval to make edits, run commands, or access network", + description: "Codex can read files and answer questions. Codex requires approval to make edits, run commands, or access network.", approval: AskForApproval::OnRequest, sandbox: SandboxPolicy::ReadOnly, }, ApprovalPreset { id: "auto", label: "Auto", - description: "Codex can read files, make edits, and run commands in the workspace. Codex requires approval to work outside the workspace or access network", + description: "Codex can read files, make edits, and run commands in the workspace. Codex requires approval to work outside the workspace or access network.", approval: AskForApproval::OnRequest, sandbox: SandboxPolicy::new_workspace_write_policy(), }, ApprovalPreset { id: "full-access", label: "Full Access", - description: "Codex can read files, make edits, and run commands with network access, without approval. Exercise caution", + description: "Codex can read files, make edits, and run commands with network access, without approval. Exercise caution.", approval: AskForApproval::Never, sandbox: SandboxPolicy::DangerFullAccess, }, diff --git a/codex-rs/common/src/config_override.rs b/codex-rs/common/src/config_override.rs index 6b77099524..59dde92a22 100644 --- a/codex-rs/common/src/config_override.rs +++ b/codex-rs/common/src/config_override.rs @@ -19,8 +19,8 @@ use toml::Value; pub struct CliConfigOverrides { /// Override a configuration value that would otherwise be loaded from /// `~/.codex/config.toml`. Use a dotted path (`foo.bar.baz`) to override - /// nested values. The `value` portion is parsed as JSON. If it fails to - /// parse as JSON, the raw string is used as a literal. + /// nested values. The `value` portion is parsed as TOML. If it fails to + /// parse as TOML, the raw string is used as a literal. /// /// Examples: /// - `-c model="o3"` @@ -59,7 +59,7 @@ impl CliConfigOverrides { return Err(format!("Empty key in override: {s}")); } - // Attempt to parse as JSON. If that fails, treat it as a raw + // Attempt to parse as TOML. If that fails, treat it as a raw // string. This allows convenient usage such as // `-c model=o3` without the quotes. let value: Value = match parse_toml_value(value_str) { @@ -151,6 +151,15 @@ mod tests { assert_eq!(v.as_integer(), Some(42)); } + #[test] + fn parses_bool() { + let true_literal = parse_toml_value("true").expect("parse"); + assert_eq!(true_literal.as_bool(), Some(true)); + + let false_literal = parse_toml_value("false").expect("parse"); + assert_eq!(false_literal.as_bool(), Some(false)); + } + #[test] fn fails_on_unquoted_string() { assert!(parse_toml_value("hello").is_err()); diff --git a/codex-rs/common/src/format_env_display.rs b/codex-rs/common/src/format_env_display.rs new file mode 100644 index 0000000000..fa35bdce25 --- /dev/null +++ b/codex-rs/common/src/format_env_display.rs @@ -0,0 +1,62 @@ +use std::collections::HashMap; + +pub fn format_env_display(env: Option<&HashMap>, env_vars: &[String]) -> String { + let mut parts: Vec = Vec::new(); + + if let Some(map) = env { + let mut pairs: Vec<_> = map.iter().collect(); + pairs.sort_by(|(a, _), (b, _)| a.cmp(b)); + parts.extend(pairs.into_iter().map(|(key, _)| format!("{key}=*****"))); + } + + if !env_vars.is_empty() { + parts.extend(env_vars.iter().map(|var| format!("{var}=*****"))); + } + + if parts.is_empty() { + "-".to_string() + } else { + parts.join(", ") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn returns_dash_when_empty() { + assert_eq!(format_env_display(None, &[]), "-"); + + let empty_map = HashMap::new(); + assert_eq!(format_env_display(Some(&empty_map), &[]), "-"); + } + + #[test] + fn formats_sorted_env_pairs() { + let mut env = HashMap::new(); + env.insert("B".to_string(), "two".to_string()); + env.insert("A".to_string(), "one".to_string()); + + assert_eq!(format_env_display(Some(&env), &[]), "A=*****, B=*****"); + } + + #[test] + fn formats_env_vars_with_dollar_prefix() { + let vars = vec!["TOKEN".to_string(), "PATH".to_string()]; + + assert_eq!(format_env_display(None, &vars), "TOKEN=*****, PATH=*****"); + } + + #[test] + fn combines_env_pairs_and_vars() { + let mut env = HashMap::new(); + env.insert("HOME".to_string(), "/tmp".to_string()); + let vars = vec!["TOKEN".to_string()]; + + assert_eq!( + format_env_display(Some(&env), &vars), + "HOME=*****, TOKEN=*****" + ); + } +} diff --git a/codex-rs/common/src/lib.rs b/codex-rs/common/src/lib.rs index 292503f77e..5092b3be24 100644 --- a/codex-rs/common/src/lib.rs +++ b/codex-rs/common/src/lib.rs @@ -13,6 +13,9 @@ mod sandbox_mode_cli_arg; #[cfg(feature = "cli")] pub use sandbox_mode_cli_arg::SandboxModeCliArg; +#[cfg(feature = "cli")] +pub mod format_env_display; + #[cfg(any(feature = "cli", test))] mod config_override; @@ -34,3 +37,5 @@ pub mod model_presets; // Shared approval presets (AskForApproval + Sandbox) used by TUI and MCP server // Not to be confused with AskForApproval, which we should probably rename to EscalationPolicy. pub mod approval_presets; +// Shared OSS provider utilities used by TUI and exec +pub mod oss; diff --git a/codex-rs/common/src/model_presets.rs b/codex-rs/common/src/model_presets.rs index 065bb1e340..9921f969a8 100644 --- a/codex-rs/common/src/model_presets.rs +++ b/codex-rs/common/src/model_presets.rs @@ -1,81 +1,217 @@ -use codex_core::config::GPT_5_CODEX_MEDIUM_MODEL; -use codex_core::protocol_config_types::ReasoningEffort; -use codex_protocol::mcp_protocol::AuthMode; +use std::collections::HashMap; -/// A simple preset pairing a model slug with a reasoning effort. +use codex_app_server_protocol::AuthMode; +use codex_core::protocol_config_types::ReasoningEffort; +use once_cell::sync::Lazy; + +/// A reasoning effort option that can be surfaced for a model. #[derive(Debug, Clone, Copy)] +pub struct ReasoningEffortPreset { + /// Effort level that the model supports. + pub effort: ReasoningEffort, + /// Short human description shown next to the effort in UIs. + pub description: &'static str, +} + +#[derive(Debug, Clone)] +pub struct ModelUpgrade { + pub id: &'static str, + pub reasoning_effort_mapping: Option>, +} + +/// Metadata describing a Codex-supported model. +#[derive(Debug, Clone)] pub struct ModelPreset { /// Stable identifier for the preset. pub id: &'static str, - /// Display label shown in UIs. - pub label: &'static str, - /// Short human description shown next to the label in UIs. - pub description: &'static str, /// Model slug (e.g., "gpt-5"). pub model: &'static str, - /// Reasoning effort to apply for this preset. - pub effort: Option, + /// Display name shown in UIs. + pub display_name: &'static str, + /// Short human description shown in UIs. + pub description: &'static str, + /// Reasoning effort applied when none is explicitly chosen. + pub default_reasoning_effort: ReasoningEffort, + /// Supported reasoning effort options. + pub supported_reasoning_efforts: &'static [ReasoningEffortPreset], + /// Whether this is the default model for new users. + pub is_default: bool, + /// recommended upgrade model + pub upgrade: Option, } -const PRESETS: &[ModelPreset] = &[ - ModelPreset { - id: "gpt-5-codex-low", - label: "gpt-5-codex low", - description: "", - model: "gpt-5-codex", - effort: Some(ReasoningEffort::Low), - }, - ModelPreset { - id: "gpt-5-codex-medium", - label: "gpt-5-codex medium", - description: "", - model: "gpt-5-codex", - effort: None, - }, - ModelPreset { - id: "gpt-5-codex-high", - label: "gpt-5-codex high", - description: "", - model: "gpt-5-codex", - effort: Some(ReasoningEffort::High), - }, - ModelPreset { - id: "gpt-5-minimal", - label: "gpt-5 minimal", - description: "— fastest responses with limited reasoning; ideal for coding, instructions, or lightweight tasks", - model: "gpt-5", - effort: Some(ReasoningEffort::Minimal), - }, - ModelPreset { - id: "gpt-5-low", - label: "gpt-5 low", - description: "— balances speed with some reasoning; useful for straightforward queries and short explanations", - model: "gpt-5", - effort: Some(ReasoningEffort::Low), - }, - ModelPreset { - id: "gpt-5-medium", - label: "gpt-5 medium", - description: "— default setting; provides a solid balance of reasoning depth and latency for general-purpose tasks", - model: "gpt-5", - effort: Some(ReasoningEffort::Medium), - }, - ModelPreset { - id: "gpt-5-high", - label: "gpt-5 high", - description: "— maximizes reasoning depth for complex or ambiguous problems", - model: "gpt-5", - effort: Some(ReasoningEffort::High), - }, -]; +static PRESETS: Lazy> = Lazy::new(|| { + vec![ + ModelPreset { + id: "gpt-5.1-codex", + model: "gpt-5.1-codex", + display_name: "gpt-5.1-codex", + description: "Optimized for codex.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Fastest responses with limited reasoning", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: true, + upgrade: None, + }, + ModelPreset { + id: "gpt-5.1-codex-mini", + model: "gpt-5.1-codex-mini", + display_name: "gpt-5.1-codex-mini", + description: "Optimized for codex. Cheaper, faster, but less capable.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: false, + upgrade: None, + }, + ModelPreset { + id: "gpt-5.1", + model: "gpt-5.1", + display_name: "gpt-5.1", + description: "Broad world knowledge with strong general reasoning.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Balances speed with some reasoning; useful for straightforward queries and short explanations", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Provides a solid balance of reasoning depth and latency for general-purpose tasks", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: false, + upgrade: None, + }, + // Deprecated models. + ModelPreset { + id: "gpt-5-codex", + model: "gpt-5-codex", + display_name: "gpt-5-codex", + description: "Optimized for codex.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Fastest responses with limited reasoning", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: false, + upgrade: Some(ModelUpgrade { + id: "gpt-5.1-codex", + reasoning_effort_mapping: None, + }), + }, + ModelPreset { + id: "gpt-5-codex-mini", + model: "gpt-5-codex-mini", + display_name: "gpt-5-codex-mini", + description: "Optimized for codex. Cheaper, faster, but less capable.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Dynamically adjusts reasoning based on the task", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: false, + upgrade: Some(ModelUpgrade { + id: "gpt-5.1-codex-mini", + reasoning_effort_mapping: None, + }), + }, + ModelPreset { + id: "gpt-5", + model: "gpt-5", + display_name: "gpt-5", + description: "Broad world knowledge with strong general reasoning.", + default_reasoning_effort: ReasoningEffort::Medium, + supported_reasoning_efforts: &[ + ReasoningEffortPreset { + effort: ReasoningEffort::Minimal, + description: "Fastest responses with little reasoning", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Low, + description: "Balances speed with some reasoning; useful for straightforward queries and short explanations", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::Medium, + description: "Provides a solid balance of reasoning depth and latency for general-purpose tasks", + }, + ReasoningEffortPreset { + effort: ReasoningEffort::High, + description: "Maximizes reasoning depth for complex or ambiguous problems", + }, + ], + is_default: false, + upgrade: Some(ModelUpgrade { + id: "gpt-5.1", + reasoning_effort_mapping: Some(HashMap::from([( + ReasoningEffort::Minimal, + ReasoningEffort::Low, + )])), + }), + }, + ] +}); -pub fn builtin_model_presets(auth_mode: Option) -> Vec { - match auth_mode { - Some(AuthMode::ApiKey) => PRESETS - .iter() - .copied() - .filter(|p| p.model != GPT_5_CODEX_MEDIUM_MODEL) - .collect(), - _ => PRESETS.to_vec(), +pub fn builtin_model_presets(_auth_mode: Option) -> Vec { + // leave auth mode for later use + PRESETS + .iter() + .filter(|preset| preset.upgrade.is_none()) + .cloned() + .collect() +} + +pub fn all_model_presets() -> &'static Vec { + &PRESETS +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_one_default_model_is_configured() { + let default_models = PRESETS.iter().filter(|preset| preset.is_default).count(); + assert!(default_models == 1); } } diff --git a/codex-rs/common/src/oss.rs b/codex-rs/common/src/oss.rs new file mode 100644 index 0000000000..b2f511e478 --- /dev/null +++ b/codex-rs/common/src/oss.rs @@ -0,0 +1,60 @@ +//! OSS provider utilities shared between TUI and exec. + +use codex_core::LMSTUDIO_OSS_PROVIDER_ID; +use codex_core::OLLAMA_OSS_PROVIDER_ID; +use codex_core::config::Config; + +/// Returns the default model for a given OSS provider. +pub fn get_default_model_for_oss_provider(provider_id: &str) -> Option<&'static str> { + match provider_id { + LMSTUDIO_OSS_PROVIDER_ID => Some(codex_lmstudio::DEFAULT_OSS_MODEL), + OLLAMA_OSS_PROVIDER_ID => Some(codex_ollama::DEFAULT_OSS_MODEL), + _ => None, + } +} + +/// Ensures the specified OSS provider is ready (models downloaded, service reachable). +pub async fn ensure_oss_provider_ready( + provider_id: &str, + config: &Config, +) -> Result<(), std::io::Error> { + match provider_id { + LMSTUDIO_OSS_PROVIDER_ID => { + codex_lmstudio::ensure_oss_ready(config) + .await + .map_err(|e| std::io::Error::other(format!("OSS setup failed: {e}")))?; + } + OLLAMA_OSS_PROVIDER_ID => { + codex_ollama::ensure_oss_ready(config) + .await + .map_err(|e| std::io::Error::other(format!("OSS setup failed: {e}")))?; + } + _ => { + // Unknown provider, skip setup + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_default_model_for_provider_lmstudio() { + let result = get_default_model_for_oss_provider(LMSTUDIO_OSS_PROVIDER_ID); + assert_eq!(result, Some(codex_lmstudio::DEFAULT_OSS_MODEL)); + } + + #[test] + fn test_get_default_model_for_provider_ollama() { + let result = get_default_model_for_oss_provider(OLLAMA_OSS_PROVIDER_ID); + assert_eq!(result, Some(codex_ollama::DEFAULT_OSS_MODEL)); + } + + #[test] + fn test_get_default_model_for_provider_unknown() { + let result = get_default_model_for_oss_provider("unknown-provider"); + assert_eq!(result, None); + } +} diff --git a/codex-rs/config.md b/codex-rs/config.md index af51df4197..5de2a2d9c4 100644 --- a/codex-rs/config.md +++ b/codex-rs/config.md @@ -3,4 +3,4 @@ This file has moved. Please see the latest configuration documentation here: - Full config docs: [docs/config.md](../docs/config.md) -- MCP servers section: [docs/config.md#mcp_servers](../docs/config.md#mcp_servers) \ No newline at end of file +- MCP servers section: [docs/config.md#connecting-to-mcp-servers](../docs/config.md#connecting-to-mcp-servers) diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 9973a00442..4d8f43778c 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -12,81 +12,116 @@ path = "src/lib.rs" workspace = true [dependencies] -anyhow = "1" -askama = "0.12" -async-channel = "2.3.1" -base64 = "0.22" -bytes = "1.10.1" -chrono = { version = "0.4", features = ["serde"] } -codex-apply-patch = { path = "../apply-patch" } -codex-file-search = { path = "../file-search" } -codex-mcp-client = { path = "../mcp-client" } -codex-protocol = { path = "../protocol" } -dirs = "6" -env-flags = "0.1.1" -eventsource-stream = "0.2.3" -futures = "0.3" -libc = "0.2.175" -mcp-types = { path = "../mcp-types" } -os_info = "3.12.0" -portable-pty = "0.9.0" -rand = "0.9" -regex-lite = "0.1.7" -reqwest = { version = "0.12", features = ["json", "stream"] } -serde = { version = "1", features = ["derive"] } -serde_json = "1" -sha1 = "0.10.6" -shlex = "1.3.0" -similar = "2.7.0" -strum_macros = "0.27.2" -tempfile = "3" -thiserror = "2.0.16" -time = { version = "0.3", features = [ +anyhow = { workspace = true } +askama = { workspace = true } +async-channel = { workspace = true } +async-trait = { workspace = true } +base64 = { workspace = true } +bytes = { workspace = true } +chrono = { workspace = true, features = ["serde"] } +codex-app-server-protocol = { workspace = true } +codex-apply-patch = { workspace = true } +codex-async-utils = { workspace = true } +codex-file-search = { workspace = true } +codex-git = { workspace = true } +codex-keyring-store = { workspace = true } +codex-otel = { workspace = true, features = ["otel"] } +codex-protocol = { workspace = true } +codex-rmcp-client = { workspace = true } +codex-utils-pty = { workspace = true } +codex-utils-readiness = { workspace = true } +codex-utils-string = { workspace = true } +codex-utils-tokenizer = { workspace = true } +codex-windows-sandbox = { package = "codex-windows-sandbox", path = "../windows-sandbox-rs" } +dirs = { workspace = true } +dunce = { workspace = true } +env-flags = { workspace = true } +eventsource-stream = { workspace = true } +futures = { workspace = true } +http = { workspace = true } +indexmap = { workspace = true } +keyring = { workspace = true, features = ["crypto-rust"] } +libc = { workspace = true } +mcp-types = { workspace = true } +os_info = { workspace = true } +rand = { workspace = true } +regex-lite = { workspace = true } +reqwest = { workspace = true, features = ["json", "stream"] } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +sha1 = { workspace = true } +sha2 = { workspace = true } +shlex = { workspace = true } +similar = { workspace = true } +strum_macros = { workspace = true } +tempfile = { workspace = true } +test-case = "3.3.1" +test-log = { workspace = true } +thiserror = { workspace = true } +time = { workspace = true, features = [ "formatting", "parsing", "local-offset", "macros", ] } -tokio = { version = "1", features = [ +tokio = { workspace = true, features = [ "io-std", "macros", "process", "rt-multi-thread", "signal", ] } -tokio-util = "0.7.16" -toml = "0.9.5" -toml_edit = "0.23.4" -tracing = { version = "0.1.41", features = ["log"] } -tree-sitter = "0.25.9" -tree-sitter-bash = "0.25.0" -uuid = { version = "1", features = ["serde", "v4"] } -which = "6" -wildmatch = "2.5.0" +tokio-util = { workspace = true, features = ["rt"] } +toml = { workspace = true } +toml_edit = { workspace = true } +tracing = { workspace = true, features = ["log"] } +tree-sitter = { workspace = true } +tree-sitter-bash = { workspace = true } +uuid = { workspace = true, features = ["serde", "v4", "v5"] } +which = { workspace = true } +wildmatch = { workspace = true } [target.'cfg(target_os = "linux")'.dependencies] -landlock = "0.4.1" -seccompiler = "0.5.0" +landlock = { workspace = true } +seccompiler = { workspace = true } +keyring = { workspace = true, features = ["linux-native-async-persistent"] } + +[target.'cfg(target_os = "macos")'.dependencies] +core-foundation = "0.9" +keyring = { workspace = true, features = ["apple-native"] } # Build OpenSSL from source for musl builds. [target.x86_64-unknown-linux-musl.dependencies] -openssl-sys = { version = "*", features = ["vendored"] } +openssl-sys = { workspace = true, features = ["vendored"] } # Build OpenSSL from source for musl builds. [target.aarch64-unknown-linux-musl.dependencies] -openssl-sys = { version = "*", features = ["vendored"] } +openssl-sys = { workspace = true, features = ["vendored"] } + +[target.'cfg(target_os = "windows")'.dependencies] +keyring = { workspace = true, features = ["windows-native"] } + +[target.'cfg(any(target_os = "freebsd", target_os = "openbsd"))'.dependencies] +keyring = { workspace = true, features = ["sync-secret-service"] } [dev-dependencies] -assert_cmd = "2" -core_test_support = { path = "tests/common" } -maplit = "1.0.2" -predicates = "3" -pretty_assertions = "1.4.1" -tempfile = "3" -tokio-test = "0.4" -walkdir = "2.5.0" -wiremock = "0.6" +assert_cmd = { workspace = true } +assert_matches = { workspace = true } +codex-arg0 = { workspace = true } +core_test_support = { workspace = true } +ctor = { workspace = true } +escargot = { workspace = true } +image = { workspace = true, features = ["jpeg", "png"] } +maplit = { workspace = true } +predicates = { workspace = true } +pretty_assertions = { workspace = true } +serial_test = { workspace = true } +tempfile = { workspace = true } +tokio-test = { workspace = true } +tracing-test = { workspace = true, features = ["no-env-filter"] } +walkdir = { workspace = true } +wiremock = { workspace = true } [package.metadata.cargo-shear] ignored = ["openssl-sys"] diff --git a/codex-rs/core/README.md b/codex-rs/core/README.md index 9a4c255abe..5d4911b022 100644 --- a/codex-rs/core/README.md +++ b/codex-rs/core/README.md @@ -4,7 +4,7 @@ This crate implements the business logic for Codex. It is designed to be used by ## Dependencies -Note that `codex-core` makes some assumptions about certain helper utilities being available in the environment. Currently, this +Note that `codex-core` makes some assumptions about certain helper utilities being available in the environment. Currently, this support matrix is: ### macOS @@ -12,7 +12,7 @@ Expects `/usr/bin/sandbox-exec` to be present. ### Linux -Expects the binary containing `codex-core` to run the equivalent of `codex debug landlock` when `arg0` is `codex-linux-sandbox`. See the `codex-arg0` crate for details. +Expects the binary containing `codex-core` to run the equivalent of `codex sandbox linux` (legacy alias: `codex debug landlock`) when `arg0` is `codex-linux-sandbox`. See the `codex-arg0` crate for details. ### All Platforms diff --git a/codex-rs/core/gpt_5_1_prompt.md b/codex-rs/core/gpt_5_1_prompt.md new file mode 100644 index 0000000000..97a3875fe5 --- /dev/null +++ b/codex-rs/core/gpt_5_1_prompt.md @@ -0,0 +1,368 @@ +You are GPT-5.1 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. + +## Autonomy and Persistence +Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you. + +Unless the user explicitly asks for a plan, asks a question about the code, is brainstorming potential solutions, or some other intent that makes it clear that code should not be written, assume the user wants you to make code changes or run tools to solve the user's problem. In these cases, it's bad to output your proposed solution in a message, you should go ahead and actually implement the change. If you encounter challenges or blockers, you should attempt to resolve them yourself. + +## Responsiveness + +### User Updates Spec +You'll work for stretches with tool calls — it's critical to keep the user updated as you work. + +Frequency & Length: +- Send short updates (1–2 sentences) whenever there is a meaningful, important insight you need to share with the user to keep them informed. +- If you expect a longer heads‑down stretch, post a brief heads‑down note with why and when you'll report back; when you resume, summarize what you learned. +- Only the initial plan, plan updates, and final recap can be longer, with multiple bullets and paragraphs + +Tone: +- Friendly, confident, senior-engineer energy. Positive, collaborative, humble; fix mistakes quickly. + +Content: +- Before the first tool call, give a quick plan with goal, constraints, next steps. +- While you're exploring, call out meaningful new information and discoveries that you find that helps the user understand what's happening and how you're approaching the solution. +- If you change the plan (e.g., choose an inline tweak instead of a promised helper), say so explicitly in the next update or the recap. + +**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. + +Maintain statuses in the tool: exactly one item in_progress at a time; mark items complete when done; post timely status transitions. Do not jump an item from pending to completed: always set it to in_progress first. Do not batch-complete multiple items after the fact. Finish with all items completed or explicitly canceled/deferred before ending the turn. Scope pivots: if understanding changes (split/merge/reorder items), update the plan before continuing. Do not let the plan go stale while coding. + +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. You must keep going until the query or task is completely resolved, before ending your turn and yielding back to the user. Persist until the task is fully handled end-to-end within the current turn whenever feasible and persevere even when function calls fail. 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`). This is a FREEFORM tool, so do not wrap the patch in JSON. + +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. + +## Codex CLI harness, sandboxing, and approvals + +The Codex CLI harness supports several different configurations for sandboxing and escalation approvals that the user can choose from. + +Filesystem sandboxing defines which files can be read or written. The options for `sandbox_mode` are: +- **read-only**: The sandbox only permits reading files. +- **workspace-write**: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. +- **danger-full-access**: No filesystem sandboxing - all commands are permitted. + +Network sandboxing defines whether network can be accessed without approval. Options for `network_access` are: +- **restricted**: Requires approval +- **enabled**: No approval needed + +Approvals are your mechanism to get user consent to run shell commands without the sandbox. Possible configuration options for `approval_policy` 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 escalating in the tool definition.) +- **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 paired 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 `approval_policy == 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 /var) +- 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. ALWAYS proceed to use the `with_escalated_permissions` and `justification` parameters. Within this harness, prefer requesting approval via the tool over asking in natural language. +- 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) + +When `sandbox_mode` 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 enabled, and approval on-failure. + +Although they introduce friction to the user because your work is paused until the user responds, you should leverage them when necessary to accomplish important work. If the completing the task requires escalated permissions, Do not let these settings or the sandbox deter you from attempting to accomplish the user's task unless it is set to "never", in which case never ask for approvals. + +When requesting approval to execute a command that will require escalated privileges: + - Provide the `with_escalated_permissions` parameter with the boolean value true + - Include a short, 1 sentence explanation for why you need to enable `with_escalated_permissions` in the justification parameter + +## Validating your work + +If the codebase has tests or the ability to build or run, consider using them to verify changes once 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**, you can proactively run tests, lint and do whatever you need to ensure you've completed the task. If you are unable to run tests, you must still do your utmost best to complete 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 contents of 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, code identifiers, and code samples 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. + +**Verbosity** +- Final answer compactness rules (enforced): + - Tiny/small single-file change (≤ ~10 lines): 2–5 sentences or ≤3 bullets. No headings. 0–1 short snippet (≤3 lines) only if essential. + - Medium change (single area or a few files): ≤6 bullets or 6–10 sentences. At most 1–2 short snippets total (≤8 lines each). + - Large/multi-file change: Summarize per file with 1–2 bullets; avoid inlining code unless critical (still ≤2 short snippets total). + - Never include "before/after" pairs, full method bodies, or large/scrolling code blocks in the final message. Prefer referencing file/symbol names instead. + +**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.) +- Read files in chunks with a max chunk size of 250 lines. Do not use python scripts to attempt to output larger chunks of a file. Command line output will be truncated after 10 kilobytes or 256 lines of output, regardless of the command used. + +## apply_patch + +Use the `apply_patch` tool 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). + +Example patch: + +``` +*** 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 + +## `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`. diff --git a/codex-rs/core/gpt_5_codex_prompt.md b/codex-rs/core/gpt_5_codex_prompt.md index 9a298f460f..57d06761ba 100644 --- a/codex-rs/core/gpt_5_codex_prompt.md +++ b/codex-rs/core/gpt_5_codex_prompt.md @@ -2,20 +2,21 @@ You are Codex, based on GPT-5. You are running as a coding agent in the Codex CL ## General -- The arguments to `shell` will be passed to execvp(). Most terminal commands should be prefixed with ["bash", "-lc"]. -- Always set the `workdir` param when using the shell function. Do not use `cd` unless absolutely necessary. - 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.) ## Editing constraints - Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them. - Add succinct code comments that explain what is going on if code is not self-explanatory. You should not add comments like "Assigns the value to the variable", but a brief comment might be useful ahead of a complex code block that the user would otherwise have to spend time parsing out. Usage of these comments should be rare. +- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase). - You may be in a dirty git worktree. * NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user. * If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes. * If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them. * If the changes are in unrelated files, just ignore them and don't revert them. +- Do not amend a commit unless explicitly requested to do so. - While you are working, you might notice unexpected changes that you didn't make. If this happens, STOP IMMEDIATELY and ask the user how they would like to proceed. +- **NEVER** use destructive commands like `git reset --hard` or `git checkout --` unless specifically requested or approved by the user. ## Plan tool @@ -89,7 +90,7 @@ You are producing plain text that will later be styled by the CLI. Follow these - Headers: optional; short Title Case (1-3 words) wrapped in **…**; no blank line before the first bullet; add only if they truly help. - Bullets: use - ; merge related points; keep to one line when possible; 4–6 per list ordered by importance; keep phrasing consistent. - Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **. -- Code samples or multi-line snippets should be wrapped in fenced code blocks; add a language hint whenever obvious. +- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible. - Structure: group related bullets; order sections general → specific → supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task. - Tone: collaborative, concise, factual; present tense, active voice; self‑contained; no "above/below"; parallel wording. - Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short—wrap/reformat if long; avoid naming formatting styles in answers. diff --git a/codex-rs/core/review_prompt.md b/codex-rs/core/review_prompt.md index 01d93598a7..040f06ba94 100644 --- a/codex-rs/core/review_prompt.md +++ b/codex-rs/core/review_prompt.md @@ -82,6 +82,6 @@ OUTPUT FORMAT: * **Do not** wrap the JSON in markdown fences or extra prose. * The code_location field is required and must include absolute_file_path and line_range. -*Line ranges must be as short as possible for interpreting the issue (avoid ranges over 5–10 lines; pick the most suitable subrange). +* Line ranges must be as short as possible for interpreting the issue (avoid ranges over 5–10 lines; pick the most suitable subrange). * The code_location should overlap with the diff. -* Do not generate a PR fix. \ No newline at end of file +* Do not generate a PR fix. diff --git a/codex-rs/core/src/apply_patch.rs b/codex-rs/core/src/apply_patch.rs index 0afc06e99c..dffe94be61 100644 --- a/codex-rs/core/src/apply_patch.rs +++ b/codex-rs/core/src/apply_patch.rs @@ -1,13 +1,12 @@ use crate::codex::Session; use crate::codex::TurnContext; +use crate::function_tool::FunctionCallError; use crate::protocol::FileChange; use crate::protocol::ReviewDecision; use crate::safety::SafetyCheck; use crate::safety::assess_patch_safety; use codex_apply_patch::ApplyPatchAction; use codex_apply_patch::ApplyPatchFileChange; -use codex_protocol::models::FunctionCallOutputPayload; -use codex_protocol::models::ResponseInputItem; use std::collections::HashMap; use std::path::PathBuf; @@ -17,7 +16,7 @@ pub(crate) enum InternalApplyPatchInvocation { /// The `apply_patch` call was handled programmatically, without any sort /// of sandbox, because the user explicitly approved it. This is the /// result to use with the `shell` function call that contained `apply_patch`. - Output(ResponseInputItem), + Output(Result), /// The `apply_patch` call was approved, either automatically because it /// appears that it should be allowed based on the user's sandbox policy @@ -28,21 +27,15 @@ pub(crate) enum InternalApplyPatchInvocation { DelegateToExec(ApplyPatchExec), } +#[derive(Debug)] pub(crate) struct ApplyPatchExec { pub(crate) action: ApplyPatchAction, pub(crate) user_explicitly_approved_this_action: bool, } -impl From for InternalApplyPatchInvocation { - fn from(item: ResponseInputItem) -> Self { - InternalApplyPatchInvocation::Output(item) - } -} - pub(crate) async fn apply_patch( sess: &Session, turn_context: &TurnContext, - sub_id: &str, call_id: &str, action: ApplyPatchAction, ) -> InternalApplyPatchInvocation { @@ -52,12 +45,13 @@ pub(crate) async fn apply_patch( &turn_context.sandbox_policy, &turn_context.cwd, ) { - SafetyCheck::AutoApprove { .. } => { - InternalApplyPatchInvocation::DelegateToExec(ApplyPatchExec { - action, - user_explicitly_approved_this_action: false, - }) - } + SafetyCheck::AutoApprove { + user_explicitly_approved, + .. + } => InternalApplyPatchInvocation::DelegateToExec(ApplyPatchExec { + action, + user_explicitly_approved_this_action: user_explicitly_approved, + }), SafetyCheck::AskUser => { // Compute a readable summary of path changes to include in the // approval request so the user can make an informed decision. @@ -67,7 +61,13 @@ pub(crate) async fn apply_patch( // that similar patches can be auto-approved in the future during // this session. let rx_approve = sess - .request_patch_approval(sub_id.to_owned(), call_id.to_owned(), &action, None, None) + .request_patch_approval( + turn_context, + call_id.to_owned(), + convert_apply_patch_to_protocol(&action), + None, + None, + ) .await; match rx_approve.await.unwrap_or_default() { ReviewDecision::Approved | ReviewDecision::ApprovedForSession => { @@ -77,25 +77,15 @@ pub(crate) async fn apply_patch( }) } ReviewDecision::Denied | ReviewDecision::Abort => { - ResponseInputItem::FunctionCallOutput { - call_id: call_id.to_owned(), - output: FunctionCallOutputPayload { - content: "patch rejected by user".to_string(), - success: Some(false), - }, - } - .into() + InternalApplyPatchInvocation::Output(Err(FunctionCallError::RespondToModel( + "patch rejected by user".to_string(), + ))) } } } - SafetyCheck::Reject { reason } => ResponseInputItem::FunctionCallOutput { - call_id: call_id.to_owned(), - output: FunctionCallOutputPayload { - content: format!("patch rejected: {reason}"), - success: Some(false), - }, - } - .into(), + SafetyCheck::Reject { reason } => InternalApplyPatchInvocation::Output(Err( + FunctionCallError::RespondToModel(format!("patch rejected: {reason}")), + )), } } @@ -125,3 +115,28 @@ pub(crate) fn convert_apply_patch_to_protocol( } result } + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + use tempfile::tempdir; + + #[test] + fn convert_apply_patch_maps_add_variant() { + let tmp = tempdir().expect("tmp"); + let p = tmp.path().join("a.txt"); + // Create an action with a single Add change + let action = ApplyPatchAction::new_add_for_test(&p, "hello".to_string()); + + let got = convert_apply_patch_to_protocol(&action); + + assert_eq!( + got.get(&p), + Some(&FileChange::Add { + content: "hello".to_string() + }) + ); + } +} diff --git a/codex-rs/core/src/auth.rs b/codex-rs/core/src/auth.rs index a2158310dc..d874435e8e 100644 --- a/codex-rs/core/src/auth.rs +++ b/codex-rs/core/src/auth.rs @@ -1,25 +1,39 @@ -use chrono::DateTime; +mod storage; + use chrono::Utc; +use reqwest::StatusCode; use serde::Deserialize; use serde::Serialize; +#[cfg(test)] +use serial_test::serial; use std::env; -use std::fs::File; -use std::fs::OpenOptions; -use std::io::Read; -use std::io::Write; -#[cfg(unix)] -use std::os::unix::fs::OpenOptionsExt; +use std::fmt::Debug; +use std::io::ErrorKind; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use std::sync::Mutex; use std::time::Duration; -use codex_protocol::mcp_protocol::AuthMode; +use codex_app_server_protocol::AuthMode; +use codex_protocol::config_types::ForcedLoginMethod; -use crate::token_data::PlanType; +pub use crate::auth::storage::AuthCredentialsStoreMode; +pub use crate::auth::storage::AuthDotJson; +use crate::auth::storage::AuthStorageBackend; +use crate::auth::storage::create_auth_storage; +use crate::config::Config; +use crate::default_client::CodexHttpClient; +use crate::error::RefreshTokenFailedError; +use crate::error::RefreshTokenFailedReason; +use crate::token_data::KnownPlan as InternalKnownPlan; +use crate::token_data::PlanType as InternalPlanType; use crate::token_data::TokenData; use crate::token_data::parse_id_token; +use crate::util::try_parse_error_message; +use codex_protocol::account::PlanType as AccountPlanType; +use serde_json::Value; +use thiserror::Error; #[derive(Debug, Clone)] pub struct CodexAuth { @@ -27,8 +41,8 @@ pub struct CodexAuth { pub(crate) api_key: Option, pub(crate) auth_dot_json: Arc>>, - pub(crate) auth_file: PathBuf, - pub(crate) client: reqwest::Client, + storage: Arc, + pub(crate) client: CodexHttpClient, } impl PartialEq for CodexAuth { @@ -37,24 +51,66 @@ impl PartialEq for CodexAuth { } } +// TODO(pakrym): use token exp field to check for expiration instead +const TOKEN_REFRESH_INTERVAL: i64 = 8; + +const REFRESH_TOKEN_EXPIRED_MESSAGE: &str = "Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again."; +const REFRESH_TOKEN_REUSED_MESSAGE: &str = "Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again."; +const REFRESH_TOKEN_INVALIDATED_MESSAGE: &str = "Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again."; +const REFRESH_TOKEN_UNKNOWN_MESSAGE: &str = + "Your access token could not be refreshed. Please log out and sign in again."; +const REFRESH_TOKEN_URL: &str = "https://auth.openai.com/oauth/token"; +pub const REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR: &str = "CODEX_REFRESH_TOKEN_URL_OVERRIDE"; + +#[derive(Debug, Error)] +pub enum RefreshTokenError { + #[error("{0}")] + Permanent(#[from] RefreshTokenFailedError), + #[error(transparent)] + Transient(#[from] std::io::Error), +} + +impl RefreshTokenError { + pub fn failed_reason(&self) -> Option { + match self { + Self::Permanent(error) => Some(error.reason), + Self::Transient(_) => None, + } + } + + fn other_with_message(message: impl Into) -> Self { + Self::Transient(std::io::Error::other(message.into())) + } +} + +impl From for std::io::Error { + fn from(err: RefreshTokenError) -> Self { + match err { + RefreshTokenError::Permanent(failed) => std::io::Error::other(failed), + RefreshTokenError::Transient(inner) => inner, + } + } +} + impl CodexAuth { - pub async fn refresh_token(&self) -> Result { - let token_data = self - .get_current_token_data() - .ok_or(std::io::Error::other("Token data is not available."))?; + pub async fn refresh_token(&self) -> Result { + tracing::info!("Refreshing token"); + + let token_data = self.get_current_token_data().ok_or_else(|| { + RefreshTokenError::Transient(std::io::Error::other("Token data is not available.")) + })?; let token = token_data.refresh_token; - let refresh_response = try_refresh_token(token, &self.client) - .await - .map_err(std::io::Error::other)?; + let refresh_response = try_refresh_token(token, &self.client).await?; let updated = update_tokens( - &self.auth_file, + &self.storage, refresh_response.id_token, refresh_response.access_token, refresh_response.refresh_token, ) - .await?; + .await + .map_err(RefreshTokenError::from)?; if let Ok(mut auth_lock) = self.auth_dot_json.lock() { *auth_lock = Some(updated.clone()); @@ -63,7 +119,7 @@ impl CodexAuth { let access = match updated.tokens { Some(t) => t.access_token, None => { - return Err(std::io::Error::other( + return Err(RefreshTokenError::other_with_message( "Token data is not available after refresh.", )); } @@ -71,9 +127,12 @@ impl CodexAuth { Ok(access) } - /// Loads the available auth information from the auth.json. - pub fn from_codex_home(codex_home: &Path) -> std::io::Result> { - load_auth(codex_home) + /// Loads the available auth information from auth storage. + pub fn from_auth_storage( + codex_home: &Path, + auth_credentials_store_mode: AuthCredentialsStoreMode, + ) -> std::io::Result> { + load_auth(codex_home, false, auth_credentials_store_mode) } pub async fn get_token_data(&self) -> Result { @@ -84,19 +143,25 @@ impl CodexAuth { last_refresh: Some(last_refresh), .. }) => { - if last_refresh < Utc::now() - chrono::Duration::days(28) { - let refresh_response = tokio::time::timeout( + if last_refresh < Utc::now() - chrono::Duration::days(TOKEN_REFRESH_INTERVAL) { + let refresh_result = tokio::time::timeout( Duration::from_secs(60), try_refresh_token(tokens.refresh_token.clone(), &self.client), ) - .await - .map_err(|_| { - std::io::Error::other("timed out while refreshing OpenAI API key") - })? - .map_err(std::io::Error::other)?; + .await; + let refresh_response = match refresh_result { + Ok(Ok(response)) => response, + Ok(Err(err)) => return Err(err.into()), + Err(_) => { + return Err(std::io::Error::new( + ErrorKind::TimedOut, + "timed out while refreshing OpenAI API key", + )); + } + }; let updated_auth_dot_json = update_tokens( - &self.auth_file, + &self.storage, refresh_response.id_token, refresh_response.access_token, refresh_response.refresh_token, @@ -135,7 +200,46 @@ impl CodexAuth { self.get_current_token_data().and_then(|t| t.account_id) } - pub(crate) fn get_plan_type(&self) -> Option { + pub fn get_account_email(&self) -> Option { + self.get_current_token_data().and_then(|t| t.id_token.email) + } + + /// Account-facing plan classification derived from the current token. + /// Returns a high-level `AccountPlanType` (e.g., Free/Plus/Pro/Team/…) + /// mapped from the ID token's internal plan value. Prefer this when you + /// need to make UI or product decisions based on the user's subscription. + pub fn account_plan_type(&self) -> Option { + let map_known = |kp: &InternalKnownPlan| match kp { + InternalKnownPlan::Free => AccountPlanType::Free, + InternalKnownPlan::Plus => AccountPlanType::Plus, + InternalKnownPlan::Pro => AccountPlanType::Pro, + InternalKnownPlan::Team => AccountPlanType::Team, + InternalKnownPlan::Business => AccountPlanType::Business, + InternalKnownPlan::Enterprise => AccountPlanType::Enterprise, + InternalKnownPlan::Edu => AccountPlanType::Edu, + }; + + self.get_current_token_data() + .and_then(|t| t.id_token.chatgpt_plan_type) + .map(|pt| match pt { + InternalPlanType::Known(k) => map_known(&k), + InternalPlanType::Unknown(_) => AccountPlanType::Unknown, + }) + } + + /// Raw plan string from the ID token (including unknown/new plan types). + pub fn raw_plan_type(&self) -> Option { + self.get_plan_type().map(|plan| match plan { + InternalPlanType::Known(k) => format!("{k:?}"), + InternalPlanType::Unknown(raw) => raw, + }) + } + + /// Raw internal plan value from the ID token. + /// Exposes the underlying `token_data::PlanType` without mapping it to the + /// public `AccountPlanType`. Use this when downstream code needs to inspect + /// internal/unknown plan strings exactly as issued in the token. + pub(crate) fn get_plan_type(&self) -> Option { self.get_current_token_data() .and_then(|t| t.id_token.chatgpt_plan_type) } @@ -166,17 +270,17 @@ impl CodexAuth { Self { api_key: None, mode: AuthMode::ChatGPT, - auth_file: PathBuf::new(), + storage: create_auth_storage(PathBuf::new(), AuthCredentialsStoreMode::File), auth_dot_json, client: crate::default_client::create_client(), } } - fn from_api_key_with_client(api_key: &str, client: reqwest::Client) -> Self { + fn from_api_key_with_client(api_key: &str, client: CodexHttpClient) -> Self { Self { api_key: Some(api_key.to_owned()), mode: AuthMode::ApiKey, - auth_file: PathBuf::new(), + storage: create_auth_storage(PathBuf::new(), AuthCredentialsStoreMode::File), auth_dot_json: Arc::new(Mutex::new(None)), client, } @@ -188,6 +292,7 @@ impl CodexAuth { } pub const OPENAI_API_KEY_ENV_VAR: &str = "OPENAI_API_KEY"; +pub const CODEX_API_KEY_ENV_VAR: &str = "CODEX_API_KEY"; pub fn read_openai_api_key_from_env() -> Option { env::var(OPENAI_API_KEY_ENV_VAR) @@ -196,39 +301,165 @@ pub fn read_openai_api_key_from_env() -> Option { .filter(|value| !value.is_empty()) } -pub fn get_auth_file(codex_home: &Path) -> PathBuf { - codex_home.join("auth.json") +pub fn read_codex_api_key_from_env() -> Option { + env::var(CODEX_API_KEY_ENV_VAR) + .ok() + .map(|value| value.trim().to_string()) + .filter(|value| !value.is_empty()) } /// Delete the auth.json file inside `codex_home` if it exists. Returns `Ok(true)` /// if a file was removed, `Ok(false)` if no auth file was present. -pub fn logout(codex_home: &Path) -> std::io::Result { - let auth_file = get_auth_file(codex_home); - match std::fs::remove_file(&auth_file) { - Ok(_) => Ok(true), - Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false), - Err(err) => Err(err), - } +pub fn logout( + codex_home: &Path, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result { + let storage = create_auth_storage(codex_home.to_path_buf(), auth_credentials_store_mode); + storage.delete() } /// Writes an `auth.json` that contains only the API key. -pub fn login_with_api_key(codex_home: &Path, api_key: &str) -> std::io::Result<()> { +pub fn login_with_api_key( + codex_home: &Path, + api_key: &str, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result<()> { let auth_dot_json = AuthDotJson { openai_api_key: Some(api_key.to_string()), tokens: None, last_refresh: None, }; - write_auth_json(&get_auth_file(codex_home), &auth_dot_json) + save_auth(codex_home, &auth_dot_json, auth_credentials_store_mode) } -fn load_auth(codex_home: &Path) -> std::io::Result> { - let auth_file = get_auth_file(codex_home); - let client = crate::default_client::create_client(); - let auth_dot_json = match try_read_auth_json(&auth_file) { - Ok(auth) => auth, - Err(e) => { - return Err(e); +/// Persist the provided auth payload using the specified backend. +pub fn save_auth( + codex_home: &Path, + auth: &AuthDotJson, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result<()> { + let storage = create_auth_storage(codex_home.to_path_buf(), auth_credentials_store_mode); + storage.save(auth) +} + +/// Load CLI auth data using the configured credential store backend. +/// Returns `None` when no credentials are stored. This function is +/// provided only for tests. Production code should not directly load +/// from the auth.json storage. It should use the AuthManager abstraction +/// instead. +pub fn load_auth_dot_json( + codex_home: &Path, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result> { + let storage = create_auth_storage(codex_home.to_path_buf(), auth_credentials_store_mode); + storage.load() +} + +pub async fn enforce_login_restrictions(config: &Config) -> std::io::Result<()> { + let Some(auth) = load_auth( + &config.codex_home, + true, + config.cli_auth_credentials_store_mode, + )? + else { + return Ok(()); + }; + + if let Some(required_method) = config.forced_login_method { + let method_violation = match (required_method, auth.mode) { + (ForcedLoginMethod::Api, AuthMode::ApiKey) => None, + (ForcedLoginMethod::Chatgpt, AuthMode::ChatGPT) => None, + (ForcedLoginMethod::Api, AuthMode::ChatGPT) => Some( + "API key login is required, but ChatGPT is currently being used. Logging out." + .to_string(), + ), + (ForcedLoginMethod::Chatgpt, AuthMode::ApiKey) => Some( + "ChatGPT login is required, but an API key is currently being used. Logging out." + .to_string(), + ), + }; + + if let Some(message) = method_violation { + return logout_with_message( + &config.codex_home, + message, + config.cli_auth_credentials_store_mode, + ); } + } + + if let Some(expected_account_id) = config.forced_chatgpt_workspace_id.as_deref() { + if auth.mode != AuthMode::ChatGPT { + return Ok(()); + } + + let token_data = match auth.get_token_data().await { + Ok(data) => data, + Err(err) => { + return logout_with_message( + &config.codex_home, + format!( + "Failed to load ChatGPT credentials while enforcing workspace restrictions: {err}. Logging out." + ), + config.cli_auth_credentials_store_mode, + ); + } + }; + + // workspace is the external identifier for account id. + let chatgpt_account_id = token_data.id_token.chatgpt_account_id.as_deref(); + if chatgpt_account_id != Some(expected_account_id) { + let message = match chatgpt_account_id { + Some(actual) => format!( + "Login is restricted to workspace {expected_account_id}, but current credentials belong to {actual}. Logging out." + ), + None => format!( + "Login is restricted to workspace {expected_account_id}, but current credentials lack a workspace identifier. Logging out." + ), + }; + return logout_with_message( + &config.codex_home, + message, + config.cli_auth_credentials_store_mode, + ); + } + } + + Ok(()) +} + +fn logout_with_message( + codex_home: &Path, + message: String, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result<()> { + match logout(codex_home, auth_credentials_store_mode) { + Ok(_) => Err(std::io::Error::other(message)), + Err(err) => Err(std::io::Error::other(format!( + "{message}. Failed to remove auth.json: {err}" + ))), + } +} + +fn load_auth( + codex_home: &Path, + enable_codex_api_key_env: bool, + auth_credentials_store_mode: AuthCredentialsStoreMode, +) -> std::io::Result> { + if enable_codex_api_key_env && let Some(api_key) = read_codex_api_key_from_env() { + let client = crate::default_client::create_client(); + return Ok(Some(CodexAuth::from_api_key_with_client( + api_key.as_str(), + client, + ))); + } + + let storage = create_auth_storage(codex_home.to_path_buf(), auth_credentials_store_mode); + + let client = crate::default_client::create_client(); + let auth_dot_json = match storage.load()? { + Some(auth) => auth, + None => return Ok(None), }; let AuthDotJson { @@ -245,7 +476,7 @@ fn load_auth(codex_home: &Path) -> std::io::Result> { Ok(Some(CodexAuth { api_key: None, mode: AuthMode::ChatGPT, - auth_file, + storage: storage.clone(), auth_dot_json: Arc::new(Mutex::new(Some(AuthDotJson { openai_api_key: None, tokens, @@ -255,41 +486,20 @@ fn load_auth(codex_home: &Path) -> std::io::Result> { })) } -/// Attempt to read and refresh the `auth.json` file in the given `CODEX_HOME` directory. -/// Returns the full AuthDotJson structure after refreshing if necessary. -pub fn try_read_auth_json(auth_file: &Path) -> std::io::Result { - let mut file = File::open(auth_file)?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - let auth_dot_json: AuthDotJson = serde_json::from_str(&contents)?; - - Ok(auth_dot_json) -} - -pub fn write_auth_json(auth_file: &Path, auth_dot_json: &AuthDotJson) -> std::io::Result<()> { - let json_data = serde_json::to_string_pretty(auth_dot_json)?; - let mut options = OpenOptions::new(); - options.truncate(true).write(true).create(true); - #[cfg(unix)] - { - options.mode(0o600); - } - let mut file = options.open(auth_file)?; - file.write_all(json_data.as_bytes())?; - file.flush()?; - Ok(()) -} - async fn update_tokens( - auth_file: &Path, - id_token: String, + storage: &Arc, + id_token: Option, access_token: Option, refresh_token: Option, ) -> std::io::Result { - let mut auth_dot_json = try_read_auth_json(auth_file)?; + let mut auth_dot_json = storage + .load()? + .ok_or(std::io::Error::other("Token data is not available."))?; let tokens = auth_dot_json.tokens.get_or_insert_with(TokenData::default); - tokens.id_token = parse_id_token(&id_token).map_err(std::io::Error::other)?; + if let Some(id_token) = id_token { + tokens.id_token = parse_id_token(&id_token).map_err(std::io::Error::other)?; + } if let Some(access_token) = access_token { tokens.access_token = access_token; } @@ -297,14 +507,14 @@ async fn update_tokens( tokens.refresh_token = refresh_token; } auth_dot_json.last_refresh = Some(Utc::now()); - write_auth_json(auth_file, &auth_dot_json)?; + storage.save(&auth_dot_json)?; Ok(auth_dot_json) } async fn try_refresh_token( refresh_token: String, - client: &reqwest::Client, -) -> std::io::Result { + client: &CodexHttpClient, +) -> Result { let refresh_request = RefreshRequest { client_id: CLIENT_ID, grant_type: "refresh_token", @@ -312,29 +522,93 @@ async fn try_refresh_token( scope: "openid profile email", }; + let endpoint = refresh_token_endpoint(); + // Use shared client factory to include standard headers let response = client - .post("https://auth.openai.com/oauth/token") + .post(endpoint.as_str()) .header("Content-Type", "application/json") .json(&refresh_request) .send() .await - .map_err(std::io::Error::other)?; + .map_err(|err| RefreshTokenError::Transient(std::io::Error::other(err)))?; - if response.status().is_success() { + let status = response.status(); + if status.is_success() { let refresh_response = response .json::() .await - .map_err(std::io::Error::other)?; + .map_err(|err| RefreshTokenError::Transient(std::io::Error::other(err)))?; Ok(refresh_response) } else { - Err(std::io::Error::other(format!( - "Failed to refresh token: {}", - response.status() - ))) + let body = response.text().await.unwrap_or_default(); + if status == StatusCode::UNAUTHORIZED { + let failed = classify_refresh_token_failure(&body); + Err(RefreshTokenError::Permanent(failed)) + } else { + let message = try_parse_error_message(&body); + Err(RefreshTokenError::Transient(std::io::Error::other( + format!("Failed to refresh token: {status}: {message}"), + ))) + } } } +fn classify_refresh_token_failure(body: &str) -> RefreshTokenFailedError { + let code = extract_refresh_token_error_code(body); + + let normalized_code = code.as_deref().map(str::to_ascii_lowercase); + let reason = match normalized_code.as_deref() { + Some("refresh_token_expired") => RefreshTokenFailedReason::Expired, + Some("refresh_token_reused") => RefreshTokenFailedReason::Exhausted, + Some("refresh_token_invalidated") => RefreshTokenFailedReason::Revoked, + _ => RefreshTokenFailedReason::Other, + }; + + if reason == RefreshTokenFailedReason::Other { + tracing::warn!( + backend_code = normalized_code.as_deref(), + backend_body = body, + "Encountered unknown 401 response while refreshing token" + ); + } + + let message = match reason { + RefreshTokenFailedReason::Expired => REFRESH_TOKEN_EXPIRED_MESSAGE.to_string(), + RefreshTokenFailedReason::Exhausted => REFRESH_TOKEN_REUSED_MESSAGE.to_string(), + RefreshTokenFailedReason::Revoked => REFRESH_TOKEN_INVALIDATED_MESSAGE.to_string(), + RefreshTokenFailedReason::Other => REFRESH_TOKEN_UNKNOWN_MESSAGE.to_string(), + }; + + RefreshTokenFailedError::new(reason, message) +} + +fn extract_refresh_token_error_code(body: &str) -> Option { + if body.trim().is_empty() { + return None; + } + + let Value::Object(map) = serde_json::from_str::(body).ok()? else { + return None; + }; + + if let Some(error_value) = map.get("error") { + match error_value { + Value::Object(obj) => { + if let Some(code) = obj.get("code").and_then(Value::as_str) { + return Some(code.to_string()); + } + } + Value::String(code) => { + return Some(code.to_string()); + } + _ => {} + } + } + + map.get("code").and_then(Value::as_str).map(str::to_string) +} + #[derive(Serialize)] struct RefreshRequest { client_id: &'static str, @@ -345,27 +619,19 @@ struct RefreshRequest { #[derive(Deserialize, Clone)] struct RefreshResponse { - id_token: String, + id_token: Option, access_token: Option, refresh_token: Option, } -/// Expected structure for $CODEX_HOME/auth.json. -#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] -pub struct AuthDotJson { - #[serde(rename = "OPENAI_API_KEY")] - pub openai_api_key: Option, - - #[serde(default, skip_serializing_if = "Option::is_none")] - pub tokens: Option, - - #[serde(default, skip_serializing_if = "Option::is_none")] - pub last_refresh: Option>, -} - // Shared constant for token refresh (client id used for oauth token refresh flow) pub const CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann"; +fn refresh_token_endpoint() -> String { + std::env::var(REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR) + .unwrap_or_else(|_| REFRESH_TOKEN_URL.to_string()) +} + use std::sync::RwLock; /// Internal cached auth state. @@ -377,35 +643,53 @@ struct CachedAuth { #[cfg(test)] mod tests { use super::*; + use crate::auth::storage::FileAuthStorage; + use crate::auth::storage::get_auth_file; + use crate::config::Config; + use crate::config::ConfigOverrides; + use crate::config::ConfigToml; use crate::token_data::IdTokenInfo; - use crate::token_data::KnownPlan; - use crate::token_data::PlanType; + use crate::token_data::KnownPlan as InternalKnownPlan; + use crate::token_data::PlanType as InternalPlanType; + use codex_protocol::account::PlanType as AccountPlanType; + use base64::Engine; + use codex_protocol::config_types::ForcedLoginMethod; use pretty_assertions::assert_eq; use serde::Serialize; use serde_json::json; use tempfile::tempdir; - const LAST_REFRESH: &str = "2025-08-06T20:41:36.232376Z"; - #[tokio::test] - async fn roundtrip_auth_dot_json() { + async fn refresh_without_id_token() { let codex_home = tempdir().unwrap(); - let _ = write_auth_file( + let fake_jwt = write_auth_file( AuthFileParams { openai_api_key: None, chatgpt_plan_type: "pro".to_string(), + chatgpt_account_id: None, }, codex_home.path(), ) .expect("failed to write auth file"); - let file = get_auth_file(codex_home.path()); - let auth_dot_json = try_read_auth_json(&file).unwrap(); - write_auth_json(&file, &auth_dot_json).unwrap(); + let storage = create_auth_storage( + codex_home.path().to_path_buf(), + AuthCredentialsStoreMode::File, + ); + let updated = super::update_tokens( + &storage, + None, + Some("new-access-token".to_string()), + Some("new-refresh-token".to_string()), + ) + .await + .expect("update_tokens should succeed"); - let same_auth_dot_json = try_read_auth_json(&file).unwrap(); - assert_eq!(auth_dot_json, same_auth_dot_json); + let tokens = updated.tokens.expect("tokens should exist"); + assert_eq!(tokens.id_token.raw_jwt, fake_jwt); + assert_eq!(tokens.access_token, "new-access-token"); + assert_eq!(tokens.refresh_token, "new-refresh-token"); } #[test] @@ -427,20 +711,34 @@ mod tests { ) .unwrap(); - super::login_with_api_key(dir.path(), "sk-new").expect("login_with_api_key should succeed"); + super::login_with_api_key(dir.path(), "sk-new", AuthCredentialsStoreMode::File) + .expect("login_with_api_key should succeed"); - let auth = super::try_read_auth_json(&auth_path).expect("auth.json should parse"); + let storage = FileAuthStorage::new(dir.path().to_path_buf()); + let auth = storage + .try_read_auth_json(&auth_path) + .expect("auth.json should parse"); assert_eq!(auth.openai_api_key.as_deref(), Some("sk-new")); assert!(auth.tokens.is_none(), "tokens should be cleared"); } + #[test] + fn missing_auth_json_returns_none() { + let dir = tempdir().unwrap(); + let auth = CodexAuth::from_auth_storage(dir.path(), AuthCredentialsStoreMode::File) + .expect("call should succeed"); + assert_eq!(auth, None); + } + #[tokio::test] + #[serial(codex_api_key)] async fn pro_account_with_no_api_key_uses_chatgpt_auth() { let codex_home = tempdir().unwrap(); let fake_jwt = write_auth_file( AuthFileParams { openai_api_key: None, chatgpt_plan_type: "pro".to_string(), + chatgpt_account_id: None, }, codex_home.path(), ) @@ -450,38 +748,42 @@ mod tests { api_key, mode, auth_dot_json, - auth_file: _, + storage: _, .. - } = super::load_auth(codex_home.path()).unwrap().unwrap(); + } = super::load_auth(codex_home.path(), false, AuthCredentialsStoreMode::File) + .unwrap() + .unwrap(); assert_eq!(None, api_key); assert_eq!(AuthMode::ChatGPT, mode); let guard = auth_dot_json.lock().unwrap(); let auth_dot_json = guard.as_ref().expect("AuthDotJson should exist"); + let last_refresh = auth_dot_json + .last_refresh + .expect("last_refresh should be recorded"); + assert_eq!( &AuthDotJson { openai_api_key: None, tokens: Some(TokenData { id_token: IdTokenInfo { email: Some("user@example.com".to_string()), - chatgpt_plan_type: Some(PlanType::Known(KnownPlan::Pro)), + chatgpt_plan_type: Some(InternalPlanType::Known(InternalKnownPlan::Pro)), + chatgpt_account_id: None, raw_jwt: fake_jwt, }, access_token: "test-access-token".to_string(), refresh_token: "test-refresh-token".to_string(), account_id: None, }), - last_refresh: Some( - DateTime::parse_from_rfc3339(LAST_REFRESH) - .unwrap() - .with_timezone(&Utc) - ), + last_refresh: Some(last_refresh), }, auth_dot_json - ) + ); } #[tokio::test] + #[serial(codex_api_key)] async fn loads_api_key_from_auth_json() { let dir = tempdir().unwrap(); let auth_file = dir.path().join("auth.json"); @@ -491,7 +793,9 @@ mod tests { ) .unwrap(); - let auth = super::load_auth(dir.path()).unwrap().unwrap(); + let auth = super::load_auth(dir.path(), false, AuthCredentialsStoreMode::File) + .unwrap() + .unwrap(); assert_eq!(auth.mode, AuthMode::ApiKey); assert_eq!(auth.api_key, Some("sk-test-key".to_string())); @@ -506,17 +810,18 @@ mod tests { tokens: None, last_refresh: None, }; - write_auth_json(&get_auth_file(dir.path()), &auth_dot_json)?; - assert!(dir.path().join("auth.json").exists()); - let removed = logout(dir.path())?; - assert!(removed); - assert!(!dir.path().join("auth.json").exists()); + super::save_auth(dir.path(), &auth_dot_json, AuthCredentialsStoreMode::File)?; + let auth_file = get_auth_file(dir.path()); + assert!(auth_file.exists()); + assert!(logout(dir.path(), AuthCredentialsStoreMode::File)?); + assert!(!auth_file.exists()); Ok(()) } struct AuthFileParams { openai_api_key: Option, chatgpt_plan_type: String, + chatgpt_account_id: Option, } fn write_auth_file(params: AuthFileParams, codex_home: &Path) -> std::io::Result { @@ -531,15 +836,21 @@ mod tests { alg: "none", typ: "JWT", }; + let mut auth_payload = serde_json::json!({ + "chatgpt_plan_type": params.chatgpt_plan_type, + "chatgpt_user_id": "user-12345", + "user_id": "user-12345", + }); + + if let Some(chatgpt_account_id) = params.chatgpt_account_id { + let org_value = serde_json::Value::String(chatgpt_account_id); + auth_payload["chatgpt_account_id"] = org_value; + } + let payload = serde_json::json!({ "email": "user@example.com", "email_verified": true, - "https://api.openai.com/auth": { - "chatgpt_account_id": "bc3618e3-489d-4d49-9362-1561dc53ba53", - "chatgpt_plan_type": params.chatgpt_plan_type, - "chatgpt_user_id": "user-12345", - "user_id": "user-12345", - } + "https://api.openai.com/auth": auth_payload, }); let b64 = |b: &[u8]| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(b); let header_b64 = b64(&serde_json::to_vec(&header)?); @@ -554,12 +865,211 @@ mod tests { "access_token": "test-access-token", "refresh_token": "test-refresh-token" }, - "last_refresh": LAST_REFRESH, + "last_refresh": Utc::now(), }); let auth_json = serde_json::to_string_pretty(&auth_json_data)?; std::fs::write(auth_file, auth_json)?; Ok(fake_jwt) } + + fn build_config( + codex_home: &Path, + forced_login_method: Option, + forced_chatgpt_workspace_id: Option, + ) -> Config { + let mut config = Config::load_from_base_config_with_overrides( + ConfigToml::default(), + ConfigOverrides::default(), + codex_home.to_path_buf(), + ) + .expect("config should load"); + config.forced_login_method = forced_login_method; + config.forced_chatgpt_workspace_id = forced_chatgpt_workspace_id; + config + } + + /// Use sparingly. + /// TODO (gpeal): replace this with an injectable env var provider. + #[cfg(test)] + struct EnvVarGuard { + key: &'static str, + original: Option, + } + + #[cfg(test)] + impl EnvVarGuard { + fn set(key: &'static str, value: &str) -> Self { + let original = env::var_os(key); + unsafe { + env::set_var(key, value); + } + Self { key, original } + } + } + + #[cfg(test)] + impl Drop for EnvVarGuard { + fn drop(&mut self) { + unsafe { + match &self.original { + Some(value) => env::set_var(self.key, value), + None => env::remove_var(self.key), + } + } + } + } + + #[tokio::test] + async fn enforce_login_restrictions_logs_out_for_method_mismatch() { + let codex_home = tempdir().unwrap(); + login_with_api_key(codex_home.path(), "sk-test", AuthCredentialsStoreMode::File) + .expect("seed api key"); + + let config = build_config(codex_home.path(), Some(ForcedLoginMethod::Chatgpt), None); + + let err = super::enforce_login_restrictions(&config) + .await + .expect_err("expected method mismatch to error"); + assert!(err.to_string().contains("ChatGPT login is required")); + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should be removed on mismatch" + ); + } + + #[tokio::test] + #[serial(codex_api_key)] + async fn enforce_login_restrictions_logs_out_for_workspace_mismatch() { + let codex_home = tempdir().unwrap(); + let _jwt = write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: "pro".to_string(), + chatgpt_account_id: Some("org_another_org".to_string()), + }, + codex_home.path(), + ) + .expect("failed to write auth file"); + + let config = build_config(codex_home.path(), None, Some("org_mine".to_string())); + + let err = super::enforce_login_restrictions(&config) + .await + .expect_err("expected workspace mismatch to error"); + assert!(err.to_string().contains("workspace org_mine")); + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should be removed on mismatch" + ); + } + + #[tokio::test] + #[serial(codex_api_key)] + async fn enforce_login_restrictions_allows_matching_workspace() { + let codex_home = tempdir().unwrap(); + let _jwt = write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: "pro".to_string(), + chatgpt_account_id: Some("org_mine".to_string()), + }, + codex_home.path(), + ) + .expect("failed to write auth file"); + + let config = build_config(codex_home.path(), None, Some("org_mine".to_string())); + + super::enforce_login_restrictions(&config) + .await + .expect("matching workspace should succeed"); + assert!( + codex_home.path().join("auth.json").exists(), + "auth.json should remain when restrictions pass" + ); + } + + #[tokio::test] + async fn enforce_login_restrictions_allows_api_key_if_login_method_not_set_but_forced_chatgpt_workspace_id_is_set() + { + let codex_home = tempdir().unwrap(); + login_with_api_key(codex_home.path(), "sk-test", AuthCredentialsStoreMode::File) + .expect("seed api key"); + + let config = build_config(codex_home.path(), None, Some("org_mine".to_string())); + + super::enforce_login_restrictions(&config) + .await + .expect("matching workspace should succeed"); + assert!( + codex_home.path().join("auth.json").exists(), + "auth.json should remain when restrictions pass" + ); + } + + #[tokio::test] + #[serial(codex_api_key)] + async fn enforce_login_restrictions_blocks_env_api_key_when_chatgpt_required() { + let _guard = EnvVarGuard::set(CODEX_API_KEY_ENV_VAR, "sk-env"); + let codex_home = tempdir().unwrap(); + + let config = build_config(codex_home.path(), Some(ForcedLoginMethod::Chatgpt), None); + + let err = super::enforce_login_restrictions(&config) + .await + .expect_err("environment API key should not satisfy forced ChatGPT login"); + assert!( + err.to_string() + .contains("ChatGPT login is required, but an API key is currently being used.") + ); + } + + #[test] + fn plan_type_maps_known_plan() { + let codex_home = tempdir().unwrap(); + let _jwt = write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: "pro".to_string(), + chatgpt_account_id: None, + }, + codex_home.path(), + ) + .expect("failed to write auth file"); + + let auth = super::load_auth(codex_home.path(), false, AuthCredentialsStoreMode::File) + .expect("load auth") + .expect("auth available"); + + pretty_assertions::assert_eq!(auth.account_plan_type(), Some(AccountPlanType::Pro)); + pretty_assertions::assert_eq!( + auth.get_plan_type(), + Some(InternalPlanType::Known(InternalKnownPlan::Pro)) + ); + } + + #[test] + fn plan_type_maps_unknown_to_unknown() { + let codex_home = tempdir().unwrap(); + let _jwt = write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: "mystery-tier".to_string(), + chatgpt_account_id: None, + }, + codex_home.path(), + ) + .expect("failed to write auth file"); + + let auth = super::load_auth(codex_home.path(), false, AuthCredentialsStoreMode::File) + .expect("load auth") + .expect("auth available"); + + pretty_assertions::assert_eq!(auth.account_plan_type(), Some(AccountPlanType::Unknown)); + pretty_assertions::assert_eq!( + auth.get_plan_type(), + Some(InternalPlanType::Unknown("mystery-tier".to_string())) + ); + } } /// Central manager providing a single source of truth for auth.json derived @@ -574,6 +1084,8 @@ mod tests { pub struct AuthManager { codex_home: PathBuf, inner: RwLock, + enable_codex_api_key_env: bool, + auth_credentials_store_mode: AuthCredentialsStoreMode, } impl AuthManager { @@ -581,11 +1093,23 @@ impl AuthManager { /// preferred auth method. Errors loading auth are swallowed; `auth()` will /// simply return `None` in that case so callers can treat it as an /// unauthenticated state. - pub fn new(codex_home: PathBuf) -> Self { - let auth = CodexAuth::from_codex_home(&codex_home).ok().flatten(); + pub fn new( + codex_home: PathBuf, + enable_codex_api_key_env: bool, + auth_credentials_store_mode: AuthCredentialsStoreMode, + ) -> Self { + let auth = load_auth( + &codex_home, + enable_codex_api_key_env, + auth_credentials_store_mode, + ) + .ok() + .flatten(); Self { codex_home, inner: RwLock::new(CachedAuth { auth }), + enable_codex_api_key_env, + auth_credentials_store_mode, } } @@ -595,6 +1119,8 @@ impl AuthManager { Arc::new(Self { codex_home: PathBuf::new(), inner: RwLock::new(cached), + enable_codex_api_key_env: false, + auth_credentials_store_mode: AuthCredentialsStoreMode::File, }) } @@ -606,7 +1132,13 @@ impl AuthManager { /// Force a reload of the auth information from auth.json. Returns /// whether the auth value changed. pub fn reload(&self) -> bool { - let new_auth = CodexAuth::from_codex_home(&self.codex_home).ok().flatten(); + let new_auth = load_auth( + &self.codex_home, + self.enable_codex_api_key_env, + self.auth_credentials_store_mode, + ) + .ok() + .flatten(); if let Ok(mut guard) = self.inner.write() { let changed = !AuthManager::auths_equal(&guard.auth, &new_auth); guard.auth = new_auth; @@ -625,13 +1157,23 @@ impl AuthManager { } /// Convenience constructor returning an `Arc` wrapper. - pub fn shared(codex_home: PathBuf) -> Arc { - Arc::new(Self::new(codex_home)) + pub fn shared( + codex_home: PathBuf, + enable_codex_api_key_env: bool, + auth_credentials_store_mode: AuthCredentialsStoreMode, + ) -> Arc { + Arc::new(Self::new( + codex_home, + enable_codex_api_key_env, + auth_credentials_store_mode, + )) } /// Attempt to refresh the current auth token (if any). On success, reload /// the auth state from disk so other components observe refreshed token. - pub async fn refresh_token(&self) -> std::io::Result> { + /// If the token refresh fails in a permanent (non‑transient) way, logs out + /// to clear invalid auth state. + pub async fn refresh_token(&self) -> Result, RefreshTokenError> { let auth = match self.auth() { Some(a) => a, None => return Ok(None), @@ -642,7 +1184,10 @@ impl AuthManager { self.reload(); Ok(Some(token)) } - Err(e) => Err(e), + Err(e) => { + tracing::error!("Failed to refresh token: {}", e); + Err(e) + } } } @@ -651,7 +1196,7 @@ impl AuthManager { /// reloads the in‑memory auth cache so callers immediately observe the /// unauthenticated state. pub fn logout(&self) -> std::io::Result { - let removed = super::auth::logout(&self.codex_home)?; + let removed = super::auth::logout(&self.codex_home, self.auth_credentials_store_mode)?; // Always reload to clear any cached auth (even if file absent). self.reload(); Ok(removed) diff --git a/codex-rs/core/src/auth/storage.rs b/codex-rs/core/src/auth/storage.rs new file mode 100644 index 0000000000..a238eb9c38 --- /dev/null +++ b/codex-rs/core/src/auth/storage.rs @@ -0,0 +1,672 @@ +use chrono::DateTime; +use chrono::Utc; +use serde::Deserialize; +use serde::Serialize; +use sha2::Digest; +use sha2::Sha256; +use std::fmt::Debug; +use std::fs::File; +use std::fs::OpenOptions; +use std::io::Read; +use std::io::Write; +#[cfg(unix)] +use std::os::unix::fs::OpenOptionsExt; +use std::path::Path; +use std::path::PathBuf; +use std::sync::Arc; +use tracing::warn; + +use crate::token_data::TokenData; +use codex_keyring_store::DefaultKeyringStore; +use codex_keyring_store::KeyringStore; + +/// Determine where Codex should store CLI auth credentials. +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum AuthCredentialsStoreMode { + #[default] + /// Persist credentials in CODEX_HOME/auth.json. + File, + /// Persist credentials in the keyring. Fail if unavailable. + Keyring, + /// Use keyring when available; otherwise, fall back to a file in CODEX_HOME. + Auto, +} + +/// Expected structure for $CODEX_HOME/auth.json. +#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] +pub struct AuthDotJson { + #[serde(rename = "OPENAI_API_KEY")] + pub openai_api_key: Option, + + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tokens: Option, + + #[serde(default, skip_serializing_if = "Option::is_none")] + pub last_refresh: Option>, +} + +pub(super) fn get_auth_file(codex_home: &Path) -> PathBuf { + codex_home.join("auth.json") +} + +pub(super) fn delete_file_if_exists(codex_home: &Path) -> std::io::Result { + let auth_file = get_auth_file(codex_home); + match std::fs::remove_file(&auth_file) { + Ok(()) => Ok(true), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(err) => Err(err), + } +} + +pub(super) trait AuthStorageBackend: Debug + Send + Sync { + fn load(&self) -> std::io::Result>; + fn save(&self, auth: &AuthDotJson) -> std::io::Result<()>; + fn delete(&self) -> std::io::Result; +} + +#[derive(Clone, Debug)] +pub(super) struct FileAuthStorage { + codex_home: PathBuf, +} + +impl FileAuthStorage { + pub(super) fn new(codex_home: PathBuf) -> Self { + Self { codex_home } + } + + /// Attempt to read and refresh the `auth.json` file in the given `CODEX_HOME` directory. + /// Returns the full AuthDotJson structure after refreshing if necessary. + pub(super) fn try_read_auth_json(&self, auth_file: &Path) -> std::io::Result { + let mut file = File::open(auth_file)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + let auth_dot_json: AuthDotJson = serde_json::from_str(&contents)?; + + Ok(auth_dot_json) + } +} + +impl AuthStorageBackend for FileAuthStorage { + fn load(&self) -> std::io::Result> { + let auth_file = get_auth_file(&self.codex_home); + let auth_dot_json = match self.try_read_auth_json(&auth_file) { + Ok(auth) => auth, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(err) => return Err(err), + }; + Ok(Some(auth_dot_json)) + } + + fn save(&self, auth_dot_json: &AuthDotJson) -> std::io::Result<()> { + let auth_file = get_auth_file(&self.codex_home); + + if let Some(parent) = auth_file.parent() { + std::fs::create_dir_all(parent)?; + } + let json_data = serde_json::to_string_pretty(auth_dot_json)?; + let mut options = OpenOptions::new(); + options.truncate(true).write(true).create(true); + #[cfg(unix)] + { + options.mode(0o600); + } + let mut file = options.open(auth_file)?; + file.write_all(json_data.as_bytes())?; + file.flush()?; + Ok(()) + } + + fn delete(&self) -> std::io::Result { + delete_file_if_exists(&self.codex_home) + } +} + +const KEYRING_SERVICE: &str = "Codex Auth"; + +// turns codex_home path into a stable, short key string +fn compute_store_key(codex_home: &Path) -> std::io::Result { + let canonical = codex_home + .canonicalize() + .unwrap_or_else(|_| codex_home.to_path_buf()); + let path_str = canonical.to_string_lossy(); + let mut hasher = Sha256::new(); + hasher.update(path_str.as_bytes()); + let digest = hasher.finalize(); + let hex = format!("{digest:x}"); + let truncated = hex.get(..16).unwrap_or(&hex); + Ok(format!("cli|{truncated}")) +} + +#[derive(Clone, Debug)] +struct KeyringAuthStorage { + codex_home: PathBuf, + keyring_store: Arc, +} + +impl KeyringAuthStorage { + fn new(codex_home: PathBuf, keyring_store: Arc) -> Self { + Self { + codex_home, + keyring_store, + } + } + + fn load_from_keyring(&self, key: &str) -> std::io::Result> { + match self.keyring_store.load(KEYRING_SERVICE, key) { + Ok(Some(serialized)) => serde_json::from_str(&serialized).map(Some).map_err(|err| { + std::io::Error::other(format!( + "failed to deserialize CLI auth from keyring: {err}" + )) + }), + Ok(None) => Ok(None), + Err(error) => Err(std::io::Error::other(format!( + "failed to load CLI auth from keyring: {}", + error.message() + ))), + } + } + + fn save_to_keyring(&self, key: &str, value: &str) -> std::io::Result<()> { + match self.keyring_store.save(KEYRING_SERVICE, key, value) { + Ok(()) => Ok(()), + Err(error) => { + let message = format!( + "failed to write OAuth tokens to keyring: {}", + error.message() + ); + warn!("{message}"); + Err(std::io::Error::other(message)) + } + } + } +} + +impl AuthStorageBackend for KeyringAuthStorage { + fn load(&self) -> std::io::Result> { + let key = compute_store_key(&self.codex_home)?; + self.load_from_keyring(&key) + } + + fn save(&self, auth: &AuthDotJson) -> std::io::Result<()> { + let key = compute_store_key(&self.codex_home)?; + // Simpler error mapping per style: prefer method reference over closure + let serialized = serde_json::to_string(auth).map_err(std::io::Error::other)?; + self.save_to_keyring(&key, &serialized)?; + if let Err(err) = delete_file_if_exists(&self.codex_home) { + warn!("failed to remove CLI auth fallback file: {err}"); + } + Ok(()) + } + + fn delete(&self) -> std::io::Result { + let key = compute_store_key(&self.codex_home)?; + let keyring_removed = self + .keyring_store + .delete(KEYRING_SERVICE, &key) + .map_err(|err| { + std::io::Error::other(format!("failed to delete auth from keyring: {err}")) + })?; + let file_removed = delete_file_if_exists(&self.codex_home)?; + Ok(keyring_removed || file_removed) + } +} + +#[derive(Clone, Debug)] +struct AutoAuthStorage { + keyring_storage: Arc, + file_storage: Arc, +} + +impl AutoAuthStorage { + fn new(codex_home: PathBuf, keyring_store: Arc) -> Self { + Self { + keyring_storage: Arc::new(KeyringAuthStorage::new(codex_home.clone(), keyring_store)), + file_storage: Arc::new(FileAuthStorage::new(codex_home)), + } + } +} + +impl AuthStorageBackend for AutoAuthStorage { + fn load(&self) -> std::io::Result> { + match self.keyring_storage.load() { + Ok(Some(auth)) => Ok(Some(auth)), + Ok(None) => self.file_storage.load(), + Err(err) => { + warn!("failed to load CLI auth from keyring, falling back to file storage: {err}"); + self.file_storage.load() + } + } + } + + fn save(&self, auth: &AuthDotJson) -> std::io::Result<()> { + match self.keyring_storage.save(auth) { + Ok(()) => Ok(()), + Err(err) => { + warn!("failed to save auth to keyring, falling back to file storage: {err}"); + self.file_storage.save(auth) + } + } + } + + fn delete(&self) -> std::io::Result { + // Keyring storage will delete from disk as well + self.keyring_storage.delete() + } +} + +pub(super) fn create_auth_storage( + codex_home: PathBuf, + mode: AuthCredentialsStoreMode, +) -> Arc { + let keyring_store: Arc = Arc::new(DefaultKeyringStore); + create_auth_storage_with_keyring_store(codex_home, mode, keyring_store) +} + +fn create_auth_storage_with_keyring_store( + codex_home: PathBuf, + mode: AuthCredentialsStoreMode, + keyring_store: Arc, +) -> Arc { + match mode { + AuthCredentialsStoreMode::File => Arc::new(FileAuthStorage::new(codex_home)), + AuthCredentialsStoreMode::Keyring => { + Arc::new(KeyringAuthStorage::new(codex_home, keyring_store)) + } + AuthCredentialsStoreMode::Auto => Arc::new(AutoAuthStorage::new(codex_home, keyring_store)), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::token_data::IdTokenInfo; + use anyhow::Context; + use base64::Engine; + use pretty_assertions::assert_eq; + use serde_json::json; + use tempfile::tempdir; + + use codex_keyring_store::tests::MockKeyringStore; + use keyring::Error as KeyringError; + + #[tokio::test] + async fn file_storage_load_returns_auth_dot_json() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let storage = FileAuthStorage::new(codex_home.path().to_path_buf()); + let auth_dot_json = AuthDotJson { + openai_api_key: Some("test-key".to_string()), + tokens: None, + last_refresh: Some(Utc::now()), + }; + + storage + .save(&auth_dot_json) + .context("failed to save auth file")?; + + let loaded = storage.load().context("failed to load auth file")?; + assert_eq!(Some(auth_dot_json), loaded); + Ok(()) + } + + #[tokio::test] + async fn file_storage_save_persists_auth_dot_json() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let storage = FileAuthStorage::new(codex_home.path().to_path_buf()); + let auth_dot_json = AuthDotJson { + openai_api_key: Some("test-key".to_string()), + tokens: None, + last_refresh: Some(Utc::now()), + }; + + let file = get_auth_file(codex_home.path()); + storage + .save(&auth_dot_json) + .context("failed to save auth file")?; + + let same_auth_dot_json = storage + .try_read_auth_json(&file) + .context("failed to read auth file after save")?; + assert_eq!(auth_dot_json, same_auth_dot_json); + Ok(()) + } + + #[test] + fn file_storage_delete_removes_auth_file() -> anyhow::Result<()> { + let dir = tempdir()?; + let auth_dot_json = AuthDotJson { + openai_api_key: Some("sk-test-key".to_string()), + tokens: None, + last_refresh: None, + }; + let storage = create_auth_storage(dir.path().to_path_buf(), AuthCredentialsStoreMode::File); + storage.save(&auth_dot_json)?; + assert!(dir.path().join("auth.json").exists()); + let storage = FileAuthStorage::new(dir.path().to_path_buf()); + let removed = storage.delete()?; + assert!(removed); + assert!(!dir.path().join("auth.json").exists()); + Ok(()) + } + + fn seed_keyring_and_fallback_auth_file_for_delete( + mock_keyring: &MockKeyringStore, + codex_home: &Path, + compute_key: F, + ) -> anyhow::Result<(String, PathBuf)> + where + F: FnOnce() -> std::io::Result, + { + let key = compute_key()?; + mock_keyring.save(KEYRING_SERVICE, &key, "{}")?; + let auth_file = get_auth_file(codex_home); + std::fs::write(&auth_file, "stale")?; + Ok((key, auth_file)) + } + + fn seed_keyring_with_auth( + mock_keyring: &MockKeyringStore, + compute_key: F, + auth: &AuthDotJson, + ) -> anyhow::Result<()> + where + F: FnOnce() -> std::io::Result, + { + let key = compute_key()?; + let serialized = serde_json::to_string(auth)?; + mock_keyring.save(KEYRING_SERVICE, &key, &serialized)?; + Ok(()) + } + + fn assert_keyring_saved_auth_and_removed_fallback( + mock_keyring: &MockKeyringStore, + key: &str, + codex_home: &Path, + expected: &AuthDotJson, + ) { + let saved_value = mock_keyring + .saved_value(key) + .expect("keyring entry should exist"); + let expected_serialized = serde_json::to_string(expected).expect("serialize expected auth"); + assert_eq!(saved_value, expected_serialized); + let auth_file = get_auth_file(codex_home); + assert!( + !auth_file.exists(), + "fallback auth.json should be removed after keyring save" + ); + } + + fn id_token_with_prefix(prefix: &str) -> IdTokenInfo { + #[derive(Serialize)] + struct Header { + alg: &'static str, + typ: &'static str, + } + + let header = Header { + alg: "none", + typ: "JWT", + }; + let payload = json!({ + "email": format!("{prefix}@example.com"), + "https://api.openai.com/auth": { + "chatgpt_account_id": format!("{prefix}-account"), + }, + }); + let encode = |bytes: &[u8]| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes); + let header_b64 = encode(&serde_json::to_vec(&header).expect("serialize header")); + let payload_b64 = encode(&serde_json::to_vec(&payload).expect("serialize payload")); + let signature_b64 = encode(b"sig"); + let fake_jwt = format!("{header_b64}.{payload_b64}.{signature_b64}"); + + crate::token_data::parse_id_token(&fake_jwt).expect("fake JWT should parse") + } + + fn auth_with_prefix(prefix: &str) -> AuthDotJson { + AuthDotJson { + openai_api_key: Some(format!("{prefix}-api-key")), + tokens: Some(TokenData { + id_token: id_token_with_prefix(prefix), + access_token: format!("{prefix}-access"), + refresh_token: format!("{prefix}-refresh"), + account_id: Some(format!("{prefix}-account-id")), + }), + last_refresh: None, + } + } + + #[test] + fn keyring_auth_storage_load_returns_deserialized_auth() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = KeyringAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let expected = AuthDotJson { + openai_api_key: Some("sk-test".to_string()), + tokens: None, + last_refresh: None, + }; + seed_keyring_with_auth( + &mock_keyring, + || compute_store_key(codex_home.path()), + &expected, + )?; + + let loaded = storage.load()?; + assert_eq!(Some(expected), loaded); + Ok(()) + } + + #[test] + fn keyring_auth_storage_compute_store_key_for_home_directory() -> anyhow::Result<()> { + let codex_home = PathBuf::from("~/.codex"); + + let key = compute_store_key(codex_home.as_path())?; + + assert_eq!(key, "cli|940db7b1d0e4eb40"); + Ok(()) + } + + #[test] + fn keyring_auth_storage_save_persists_and_removes_fallback_file() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = KeyringAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let auth_file = get_auth_file(codex_home.path()); + std::fs::write(&auth_file, "stale")?; + let auth = AuthDotJson { + openai_api_key: None, + tokens: Some(TokenData { + id_token: Default::default(), + access_token: "access".to_string(), + refresh_token: "refresh".to_string(), + account_id: Some("account".to_string()), + }), + last_refresh: Some(Utc::now()), + }; + + storage.save(&auth)?; + + let key = compute_store_key(codex_home.path())?; + assert_keyring_saved_auth_and_removed_fallback( + &mock_keyring, + &key, + codex_home.path(), + &auth, + ); + Ok(()) + } + + #[test] + fn keyring_auth_storage_delete_removes_keyring_and_file() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = KeyringAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let (key, auth_file) = seed_keyring_and_fallback_auth_file_for_delete( + &mock_keyring, + codex_home.path(), + || compute_store_key(codex_home.path()), + )?; + + let removed = storage.delete()?; + + assert!(removed, "delete should report removal"); + assert!( + !mock_keyring.contains(&key), + "keyring entry should be removed" + ); + assert!( + !auth_file.exists(), + "fallback auth.json should be removed after keyring delete" + ); + Ok(()) + } + + #[test] + fn auto_auth_storage_load_prefers_keyring_value() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let keyring_auth = auth_with_prefix("keyring"); + seed_keyring_with_auth( + &mock_keyring, + || compute_store_key(codex_home.path()), + &keyring_auth, + )?; + + let file_auth = auth_with_prefix("file"); + storage.file_storage.save(&file_auth)?; + + let loaded = storage.load()?; + assert_eq!(loaded, Some(keyring_auth)); + Ok(()) + } + + #[test] + fn auto_auth_storage_load_uses_file_when_keyring_empty() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new(codex_home.path().to_path_buf(), Arc::new(mock_keyring)); + + let expected = auth_with_prefix("file-only"); + storage.file_storage.save(&expected)?; + + let loaded = storage.load()?; + assert_eq!(loaded, Some(expected)); + Ok(()) + } + + #[test] + fn auto_auth_storage_load_falls_back_when_keyring_errors() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let key = compute_store_key(codex_home.path())?; + mock_keyring.set_error(&key, KeyringError::Invalid("error".into(), "load".into())); + + let expected = auth_with_prefix("fallback"); + storage.file_storage.save(&expected)?; + + let loaded = storage.load()?; + assert_eq!(loaded, Some(expected)); + Ok(()) + } + + #[test] + fn auto_auth_storage_save_prefers_keyring() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let key = compute_store_key(codex_home.path())?; + + let stale = auth_with_prefix("stale"); + storage.file_storage.save(&stale)?; + + let expected = auth_with_prefix("to-save"); + storage.save(&expected)?; + + assert_keyring_saved_auth_and_removed_fallback( + &mock_keyring, + &key, + codex_home.path(), + &expected, + ); + Ok(()) + } + + #[test] + fn auto_auth_storage_save_falls_back_when_keyring_errors() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let key = compute_store_key(codex_home.path())?; + mock_keyring.set_error(&key, KeyringError::Invalid("error".into(), "save".into())); + + let auth = auth_with_prefix("fallback"); + storage.save(&auth)?; + + let auth_file = get_auth_file(codex_home.path()); + assert!( + auth_file.exists(), + "fallback auth.json should be created when keyring save fails" + ); + let saved = storage + .file_storage + .load()? + .context("fallback auth should exist")?; + assert_eq!(saved, auth); + assert!( + mock_keyring.saved_value(&key).is_none(), + "keyring should not contain value when save fails" + ); + Ok(()) + } + + #[test] + fn auto_auth_storage_delete_removes_keyring_and_file() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let mock_keyring = MockKeyringStore::default(); + let storage = AutoAuthStorage::new( + codex_home.path().to_path_buf(), + Arc::new(mock_keyring.clone()), + ); + let (key, auth_file) = seed_keyring_and_fallback_auth_file_for_delete( + &mock_keyring, + codex_home.path(), + || compute_store_key(codex_home.path()), + )?; + + let removed = storage.delete()?; + + assert!(removed, "delete should report removal"); + assert!( + !mock_keyring.contains(&key), + "keyring entry should be removed" + ); + assert!( + !auth_file.exists(), + "fallback auth.json should be removed after delete" + ); + Ok(()) + } +} diff --git a/codex-rs/core/src/bash.rs b/codex-rs/core/src/bash.rs index 4bed3a9c16..0ffb8e785e 100644 --- a/codex-rs/core/src/bash.rs +++ b/codex-rs/core/src/bash.rs @@ -1,16 +1,22 @@ +use std::path::PathBuf; + +use tree_sitter::Node; use tree_sitter::Parser; use tree_sitter::Tree; use tree_sitter_bash::LANGUAGE as BASH; +use crate::shell::ShellType; +use crate::shell::detect_shell_type; + /// Parse the provided bash source using tree-sitter-bash, returning a Tree on /// success or None if parsing failed. -pub fn try_parse_bash(bash_lc_arg: &str) -> Option { +pub fn try_parse_shell(shell_lc_arg: &str) -> Option { let lang = BASH.into(); let mut parser = Parser::new(); #[expect(clippy::expect_used)] parser.set_language(&lang).expect("load bash grammar"); let old_tree: Option<&Tree> = None; - parser.parse(bash_lc_arg, old_tree) + parser.parse(shell_lc_arg, old_tree) } /// Parse a script which may contain multiple simple commands joined only by @@ -74,7 +80,7 @@ pub fn try_parse_word_only_commands_sequence(tree: &Tree, src: &str) -> Option Option Option<(&str, &str)> { + let [shell, flag, script] = command else { + return None; + }; + if !matches!(flag.as_str(), "-lc" | "-c") + || !matches!( + detect_shell_type(&PathBuf::from(shell)), + Some(ShellType::Zsh) | Some(ShellType::Bash) + ) + { + return None; + } + Some((shell, script)) +} + +/// Returns the sequence of plain commands within a `bash -lc "..."` or +/// `zsh -lc "..."` invocation when the script only contains word-only commands +/// joined by safe operators. +pub fn parse_shell_lc_plain_commands(command: &[String]) -> Option>> { + let (_, script) = extract_bash_command(command)?; + + let tree = try_parse_shell(script)?; + try_parse_word_only_commands_sequence(&tree, script) +} + fn parse_plain_command_from_node(cmd: tree_sitter::Node, src: &str) -> Option> { if cmd.kind() != "command" { return None; @@ -138,7 +169,7 @@ mod tests { use super::*; fn parse_seq(src: &str) -> Option>> { - let tree = try_parse_bash(src)?; + let tree = try_parse_shell(src)?; try_parse_word_only_commands_sequence(&tree, src) } @@ -218,4 +249,11 @@ mod tests { fn rejects_trailing_operator_parse_error() { assert!(parse_seq("ls &&").is_none()); } + + #[test] + fn parse_zsh_lc_plain_commands() { + let command = vec!["zsh".to_string(), "-lc".to_string(), "ls".to_string()]; + let parsed = parse_shell_lc_plain_commands(&command).unwrap(); + assert_eq!(parsed, vec![vec!["ls".to_string()]]); + } } diff --git a/codex-rs/core/src/chat_completions.rs b/codex-rs/core/src/chat_completions.rs index f666cc119f..a60db89d83 100644 --- a/codex-rs/core/src/chat_completions.rs +++ b/codex-rs/core/src/chat_completions.rs @@ -1,6 +1,27 @@ use std::time::Duration; +use crate::ModelProviderInfo; +use crate::client_common::Prompt; +use crate::client_common::ResponseEvent; +use crate::client_common::ResponseStream; +use crate::default_client::CodexHttpClient; +use crate::error::CodexErr; +use crate::error::ConnectionFailedError; +use crate::error::ResponseStreamFailed; +use crate::error::Result; +use crate::error::RetryLimitReachedError; +use crate::error::UnexpectedResponseError; +use crate::model_family::ModelFamily; +use crate::tools::spec::create_tools_json_for_chat_completions_api; +use crate::util::backoff; use bytes::Bytes; +use codex_otel::otel_event_manager::OtelEventManager; +use codex_protocol::models::ContentItem; +use codex_protocol::models::FunctionCallOutputContentItem; +use codex_protocol::models::ReasoningItemContent; +use codex_protocol::models::ResponseItem; +use codex_protocol::protocol::SessionSource; +use codex_protocol::protocol::SubAgentSource; use eventsource_stream::Eventsource; use futures::Stream; use futures::StreamExt; @@ -15,26 +36,21 @@ use tokio::time::timeout; use tracing::debug; use tracing::trace; -use crate::ModelProviderInfo; -use crate::client_common::Prompt; -use crate::client_common::ResponseEvent; -use crate::client_common::ResponseStream; -use crate::error::CodexErr; -use crate::error::Result; -use crate::model_family::ModelFamily; -use crate::openai_tools::create_tools_json_for_chat_completions_api; -use crate::util::backoff; -use codex_protocol::models::ContentItem; -use codex_protocol::models::ReasoningItemContent; -use codex_protocol::models::ResponseItem; - /// Implementation for the classic Chat Completions API. pub(crate) async fn stream_chat_completions( prompt: &Prompt, model_family: &ModelFamily, - client: &reqwest::Client, + client: &CodexHttpClient, provider: &ModelProviderInfo, + otel_event_manager: &OtelEventManager, + session_source: &SessionSource, ) -> Result { + if prompt.output_schema.is_some() { + return Err(CodexErr::UnsupportedOperation( + "output_schema is not supported for Chat Completions API".to_string(), + )); + } + // Build messages array let mut messages = Vec::::new(); @@ -64,6 +80,7 @@ pub(crate) async fn stream_chat_completions( ResponseItem::CustomToolCall { .. } => {} ResponseItem::CustomToolCallOutput { .. } => {} ResponseItem::WebSearchCall { .. } => {} + ResponseItem::GhostSnapshot { .. } => {} } } @@ -93,10 +110,10 @@ pub(crate) async fn stream_chat_completions( } = item { let mut text = String::new(); - for c in items { - match c { - ReasoningItemContent::ReasoningText { text: t } - | ReasoningItemContent::Text { text: t } => text.push_str(t), + for entry in items { + match entry { + ReasoningItemContent::ReasoningText { text: segment } + | ReasoningItemContent::Text { text: segment } => text.push_str(segment), } } if text.trim().is_empty() { @@ -146,16 +163,26 @@ pub(crate) async fn stream_chat_completions( for (idx, item) in input.iter().enumerate() { match item { ResponseItem::Message { role, content, .. } => { + // Build content either as a plain string (typical for assistant text) + // or as an array of content items when images are present (user/tool multimodal). let mut text = String::new(); + let mut items: Vec = Vec::new(); + let mut saw_image = false; + for c in content { match c { ContentItem::InputText { text: t } | ContentItem::OutputText { text: t } => { text.push_str(t); + items.push(json!({"type":"text","text": t})); + } + ContentItem::InputImage { image_url } => { + saw_image = true; + items.push(json!({"type":"image_url","image_url": {"url": image_url}})); } - _ => {} } } + // Skip exact-duplicate assistant messages. if role == "assistant" { if let Some(prev) = &last_assistant_text @@ -166,7 +193,17 @@ pub(crate) async fn stream_chat_completions( last_assistant_text = Some(text.clone()); } - let mut msg = json!({"role": role, "content": text}); + // For assistant messages, always send a plain string for compatibility. + // For user messages, if an image is present, send an array of content items. + let content_value = if role == "assistant" { + json!(text) + } else if saw_image { + json!(items) + } else { + json!(text) + }; + + let mut msg = json!({"role": role, "content": content_value}); if role == "assistant" && let Some(reasoning) = reasoning_by_anchor_index.get(&idx) && let Some(obj) = msg.as_object_mut() @@ -225,10 +262,29 @@ pub(crate) async fn stream_chat_completions( messages.push(msg); } ResponseItem::FunctionCallOutput { call_id, output } => { + // Prefer structured content items when available (e.g., images) + // otherwise fall back to the legacy plain-string content. + let content_value = if let Some(items) = &output.content_items { + let mapped: Vec = items + .iter() + .map(|it| match it { + FunctionCallOutputContentItem::InputText { text } => { + json!({"type":"text","text": text}) + } + FunctionCallOutputContentItem::InputImage { image_url } => { + json!({"type":"image_url","image_url": {"url": image_url}}) + } + }) + .collect(); + json!(mapped) + } else { + json!(output.content) + }; + messages.push(json!({ "role": "tool", "tool_call_id": call_id, - "content": output.content, + "content": content_value, })); } ResponseItem::CustomToolCall { @@ -258,6 +314,10 @@ pub(crate) async fn stream_chat_completions( "content": output, })); } + ResponseItem::GhostSnapshot { .. } => { + // Ghost snapshots annotate history but are not sent to the model. + continue; + } ResponseItem::Reasoning { .. } | ResponseItem::WebSearchCall { .. } | ResponseItem::Other => { @@ -278,7 +338,7 @@ pub(crate) async fn stream_chat_completions( debug!( "POST to {}: {}", provider.get_full_url(&None), - serde_json::to_string_pretty(&payload).unwrap_or_default() + payload.to_string() ); let mut attempt = 0; @@ -286,22 +346,44 @@ pub(crate) async fn stream_chat_completions( loop { attempt += 1; - let req_builder = provider.create_request_builder(client, &None).await?; + let mut req_builder = provider.create_request_builder(client, &None).await?; - let res = req_builder - .header(reqwest::header::ACCEPT, "text/event-stream") - .json(&payload) - .send() + // Include subagent header only for subagent sessions. + if let SessionSource::SubAgent(sub) = session_source.clone() { + let subagent = if let SubAgentSource::Other(label) = sub { + label + } else { + serde_json::to_value(&sub) + .ok() + .and_then(|v| v.as_str().map(std::string::ToString::to_string)) + .unwrap_or_else(|| "other".to_string()) + }; + req_builder = req_builder.header("x-openai-subagent", subagent); + } + + let res = otel_event_manager + .log_request(attempt, || { + req_builder + .header(reqwest::header::ACCEPT, "text/event-stream") + .json(&payload) + .send() + }) .await; match res { Ok(resp) if resp.status().is_success() => { let (tx_event, rx_event) = mpsc::channel::>(1600); - let stream = resp.bytes_stream().map_err(CodexErr::Reqwest); + let stream = resp.bytes_stream().map_err(|e| { + CodexErr::ResponseStreamFailed(ResponseStreamFailed { + source: e, + request_id: None, + }) + }); tokio::spawn(process_chat_sse( stream, tx_event, provider.stream_idle_timeout(), + otel_event_manager.clone(), )); return Ok(ResponseStream { rx_event }); } @@ -309,11 +391,18 @@ pub(crate) async fn stream_chat_completions( let status = res.status(); if !(status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error()) { let body = (res.text().await).unwrap_or_default(); - return Err(CodexErr::UnexpectedStatus(status, body)); + return Err(CodexErr::UnexpectedStatus(UnexpectedResponseError { + status, + body, + request_id: None, + })); } if attempt > max_retries { - return Err(CodexErr::RetryLimit(status)); + return Err(CodexErr::RetryLimit(RetryLimitReachedError { + status, + request_id: None, + })); } let retry_after_secs = res @@ -329,7 +418,9 @@ pub(crate) async fn stream_chat_completions( } Err(e) => { if attempt > max_retries { - return Err(e.into()); + return Err(CodexErr::ConnectionFailed(ConnectionFailedError { + source: e, + })); } let delay = backoff(attempt); tokio::time::sleep(delay).await; @@ -338,6 +429,65 @@ pub(crate) async fn stream_chat_completions( } } +async fn append_assistant_text( + tx_event: &mpsc::Sender>, + assistant_item: &mut Option, + text: String, +) { + if assistant_item.is_none() { + let item = ResponseItem::Message { + id: None, + role: "assistant".to_string(), + content: vec![], + }; + *assistant_item = Some(item.clone()); + let _ = tx_event + .send(Ok(ResponseEvent::OutputItemAdded(item))) + .await; + } + + if let Some(ResponseItem::Message { content, .. }) = assistant_item { + content.push(ContentItem::OutputText { text: text.clone() }); + let _ = tx_event + .send(Ok(ResponseEvent::OutputTextDelta(text.clone()))) + .await; + } +} + +async fn append_reasoning_text( + tx_event: &mpsc::Sender>, + reasoning_item: &mut Option, + text: String, +) { + if reasoning_item.is_none() { + let item = ResponseItem::Reasoning { + id: String::new(), + summary: Vec::new(), + content: Some(vec![]), + encrypted_content: None, + }; + *reasoning_item = Some(item.clone()); + let _ = tx_event + .send(Ok(ResponseEvent::OutputItemAdded(item))) + .await; + } + + if let Some(ResponseItem::Reasoning { + content: Some(content), + .. + }) = reasoning_item + { + let content_index = content.len() as i64; + content.push(ReasoningItemContent::ReasoningText { text: text.clone() }); + + let _ = tx_event + .send(Ok(ResponseEvent::ReasoningContentDelta { + delta: text.clone(), + content_index, + })) + .await; + } +} /// Lightweight SSE processor for the Chat Completions streaming format. The /// output is mapped onto Codex's internal [`ResponseEvent`] so that the rest /// of the pipeline can stay agnostic of the underlying wire format. @@ -345,6 +495,7 @@ async fn process_chat_sse( stream: S, tx_event: mpsc::Sender>, idle_timeout: Duration, + otel_event_manager: OtelEventManager, ) where S: Stream> + Unpin, { @@ -364,11 +515,16 @@ async fn process_chat_sse( } let mut fn_call_state = FunctionCallState::default(); - let mut assistant_text = String::new(); - let mut reasoning_text = String::new(); + let mut assistant_item: Option = None; + let mut reasoning_item: Option = None; loop { - let sse = match timeout(idle_timeout, stream.next()).await { + let start = std::time::Instant::now(); + let response = timeout(idle_timeout, stream.next()).await; + let duration = start.elapsed(); + otel_event_manager.log_sse_event(&response, duration); + + let sse = match response { Ok(Some(Ok(ev))) => ev, Ok(Some(Err(e))) => { let _ = tx_event @@ -401,26 +557,11 @@ async fn process_chat_sse( if sse.data.trim() == "[DONE]" { // Emit any finalized items before closing so downstream consumers receive // terminal events for both assistant content and raw reasoning. - if !assistant_text.is_empty() { - let item = ResponseItem::Message { - role: "assistant".to_string(), - content: vec![ContentItem::OutputText { - text: std::mem::take(&mut assistant_text), - }], - id: None, - }; + if let Some(item) = assistant_item { let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await; } - if !reasoning_text.is_empty() { - let item = ResponseItem::Reasoning { - id: String::new(), - summary: Vec::new(), - content: Some(vec![ReasoningItemContent::ReasoningText { - text: std::mem::take(&mut reasoning_text), - }]), - encrypted_content: None, - }; + if let Some(item) = reasoning_item { let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await; } @@ -450,10 +591,7 @@ async fn process_chat_sse( .and_then(|c| c.as_str()) && !content.is_empty() { - assistant_text.push_str(content); - let _ = tx_event - .send(Ok(ResponseEvent::OutputTextDelta(content.to_string()))) - .await; + append_assistant_text(&tx_event, &mut assistant_item, content.to_string()).await; } // Forward any reasoning/thinking deltas if present. @@ -462,7 +600,7 @@ async fn process_chat_sse( if let Some(reasoning_val) = choice.get("delta").and_then(|d| d.get("reasoning")) { let mut maybe_text = reasoning_val .as_str() - .map(|s| s.to_string()) + .map(str::to_string) .filter(|s| !s.is_empty()); if maybe_text.is_none() && reasoning_val.is_object() { @@ -483,10 +621,7 @@ async fn process_chat_sse( if let Some(reasoning) = maybe_text { // Accumulate so we can emit a terminal Reasoning item at the end. - reasoning_text.push_str(&reasoning); - let _ = tx_event - .send(Ok(ResponseEvent::ReasoningContentDelta(reasoning))) - .await; + append_reasoning_text(&tx_event, &mut reasoning_item, reasoning).await; } } @@ -496,10 +631,7 @@ async fn process_chat_sse( // Accept either a plain string or an object with { text | content } if let Some(s) = message_reasoning.as_str() { if !s.is_empty() { - reasoning_text.push_str(s); - let _ = tx_event - .send(Ok(ResponseEvent::ReasoningContentDelta(s.to_string()))) - .await; + append_reasoning_text(&tx_event, &mut reasoning_item, s.to_string()).await; } } else if let Some(obj) = message_reasoning.as_object() && let Some(s) = obj @@ -508,10 +640,7 @@ async fn process_chat_sse( .or_else(|| obj.get("content").and_then(|v| v.as_str())) && !s.is_empty() { - reasoning_text.push_str(s); - let _ = tx_event - .send(Ok(ResponseEvent::ReasoningContentDelta(s.to_string()))) - .await; + append_reasoning_text(&tx_event, &mut reasoning_item, s.to_string()).await; } } @@ -544,20 +673,14 @@ async fn process_chat_sse( } // Emit end-of-turn when finish_reason signals completion. - if let Some(finish_reason) = choice.get("finish_reason").and_then(|v| v.as_str()) { + if let Some(finish_reason) = choice.get("finish_reason").and_then(|v| v.as_str()) + && !finish_reason.is_empty() + { match finish_reason { "tool_calls" if fn_call_state.active => { // First, flush the terminal raw reasoning so UIs can finalize // the reasoning stream before any exec/tool events begin. - if !reasoning_text.is_empty() { - let item = ResponseItem::Reasoning { - id: String::new(), - summary: Vec::new(), - content: Some(vec![ReasoningItemContent::ReasoningText { - text: std::mem::take(&mut reasoning_text), - }]), - encrypted_content: None, - }; + if let Some(item) = reasoning_item.take() { let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await; } @@ -574,26 +697,11 @@ async fn process_chat_sse( "stop" => { // Regular turn without tool-call. Emit the final assistant message // as a single OutputItemDone so non-delta consumers see the result. - if !assistant_text.is_empty() { - let item = ResponseItem::Message { - role: "assistant".to_string(), - content: vec![ContentItem::OutputText { - text: std::mem::take(&mut assistant_text), - }], - id: None, - }; + if let Some(item) = assistant_item.take() { let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await; } // Also emit a terminal Reasoning item so UIs can finalize raw reasoning. - if !reasoning_text.is_empty() { - let item = ResponseItem::Reasoning { - id: String::new(), - summary: Vec::new(), - content: Some(vec![ReasoningItemContent::ReasoningText { - text: std::mem::take(&mut reasoning_text), - }]), - encrypted_content: None, - }; + if let Some(item) = reasoning_item.take() { let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await; } } @@ -796,24 +904,30 @@ where continue; } } - Poll::Ready(Some(Ok(ResponseEvent::ReasoningContentDelta(delta)))) => { + Poll::Ready(Some(Ok(ResponseEvent::ReasoningContentDelta { + delta, + content_index, + }))) => { // Always accumulate reasoning deltas so we can emit a final Reasoning item at Completed. this.cumulative_reasoning.push_str(&delta); if matches!(this.mode, AggregateMode::Streaming) { // In streaming mode, also forward the delta immediately. - return Poll::Ready(Some(Ok(ResponseEvent::ReasoningContentDelta(delta)))); + return Poll::Ready(Some(Ok(ResponseEvent::ReasoningContentDelta { + delta, + content_index, + }))); } else { continue; } } - Poll::Ready(Some(Ok(ResponseEvent::ReasoningSummaryDelta(_)))) => { + Poll::Ready(Some(Ok(ResponseEvent::ReasoningSummaryDelta { .. }))) => { continue; } - Poll::Ready(Some(Ok(ResponseEvent::ReasoningSummaryPartAdded))) => { + Poll::Ready(Some(Ok(ResponseEvent::ReasoningSummaryPartAdded { .. }))) => { continue; } - Poll::Ready(Some(Ok(ResponseEvent::WebSearchCallBegin { call_id }))) => { - return Poll::Ready(Some(Ok(ResponseEvent::WebSearchCallBegin { call_id }))); + Poll::Ready(Some(Ok(ResponseEvent::OutputItemAdded(item)))) => { + return Poll::Ready(Some(Ok(ResponseEvent::OutputItemAdded(item)))); } } } diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 57848d388b..4614458646 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -1,12 +1,19 @@ use std::io::BufRead; use std::path::Path; +use std::sync::Arc; use std::sync::OnceLock; use std::time::Duration; -use crate::AuthManager; use bytes::Bytes; -use codex_protocol::mcp_protocol::AuthMode; -use codex_protocol::mcp_protocol::ConversationId; +use chrono::DateTime; +use chrono::Utc; +use codex_app_server_protocol::AuthMode; +use codex_otel::otel_event_manager::OtelEventManager; +use codex_protocol::ConversationId; +use codex_protocol::config_types::ReasoningEffort as ReasoningEffortConfig; +use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; +use codex_protocol::models::ResponseItem; +use codex_protocol::protocol::SessionSource; use eventsource_stream::Eventsource; use futures::prelude::*; use regex_lite::Regex; @@ -22,33 +29,38 @@ use tracing::debug; use tracing::trace; use tracing::warn; +use crate::AuthManager; +use crate::auth::CodexAuth; +use crate::auth::RefreshTokenError; use crate::chat_completions::AggregateStreamExt; use crate::chat_completions::stream_chat_completions; use crate::client_common::Prompt; +use crate::client_common::Reasoning; use crate::client_common::ResponseEvent; use crate::client_common::ResponseStream; use crate::client_common::ResponsesApiRequest; -use crate::client_common::create_reasoning_param_for_request; use crate::client_common::create_text_param_for_request; use crate::config::Config; +use crate::default_client::CodexHttpClient; use crate::default_client::create_client; use crate::error::CodexErr; +use crate::error::ConnectionFailedError; +use crate::error::ResponseStreamFailed; use crate::error::Result; +use crate::error::RetryLimitReachedError; +use crate::error::UnexpectedResponseError; use crate::error::UsageLimitReachedError; use crate::flags::CODEX_RS_SSE_FIXTURE; use crate::model_family::ModelFamily; use crate::model_provider_info::ModelProviderInfo; use crate::model_provider_info::WireApi; use crate::openai_model_info::get_model_info; -use crate::openai_tools::create_tools_json_for_responses_api; -use crate::protocol::RateLimitSnapshotEvent; +use crate::protocol::RateLimitSnapshot; +use crate::protocol::RateLimitWindow; use crate::protocol::TokenUsage; use crate::token_data::PlanType; +use crate::tools::spec::create_tools_json_for_responses_api; use crate::util::backoff; -use codex_protocol::config_types::ReasoningEffort as ReasoningEffortConfig; -use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; -use codex_protocol::models::ResponseItem; -use std::sync::Arc; #[derive(Debug, Deserialize)] struct ErrorResponse { @@ -58,52 +70,60 @@ struct ErrorResponse { #[derive(Debug, Deserialize)] struct Error { r#type: Option, - #[allow(dead_code)] code: Option, message: Option, // Optional fields available on "usage_limit_reached" and "usage_not_included" errors plan_type: Option, - resets_in_seconds: Option, + resets_at: Option, } #[derive(Debug, Clone)] pub struct ModelClient { config: Arc, auth_manager: Option>, - client: reqwest::Client, + otel_event_manager: OtelEventManager, + client: CodexHttpClient, provider: ModelProviderInfo, conversation_id: ConversationId, effort: Option, summary: ReasoningSummaryConfig, + session_source: SessionSource, } +#[allow(clippy::too_many_arguments)] impl ModelClient { pub fn new( config: Arc, auth_manager: Option>, + otel_event_manager: OtelEventManager, provider: ModelProviderInfo, effort: Option, summary: ReasoningSummaryConfig, conversation_id: ConversationId, + session_source: SessionSource, ) -> Self { let client = create_client(); Self { config, auth_manager, + otel_event_manager, client, provider, conversation_id, effort, summary, + session_source, } } - pub fn get_model_context_window(&self) -> Option { + pub fn get_model_context_window(&self) -> Option { + let pct = self.config.model_family.effective_context_window_percent; self.config .model_context_window .or_else(|| get_model_info(&self.config.model_family).map(|info| info.context_window)) + .map(|w| w.saturating_mul(pct) / 100) } pub fn get_auto_compact_token_limit(&self) -> Option { @@ -112,9 +132,14 @@ impl ModelClient { }) } - /// Dispatches to either the Responses or Chat implementation depending on - /// the provider config. Public callers always invoke `stream()` – the - /// specialised helpers are private to avoid accidental misuse. + pub fn config(&self) -> Arc { + Arc::clone(&self.config) + } + + pub fn provider(&self) -> &ModelProviderInfo { + &self.provider + } + pub async fn stream(&self, prompt: &Prompt) -> Result { match self.provider.wire_api { WireApi::Responses => self.stream_responses(prompt).await, @@ -125,6 +150,8 @@ impl ModelClient { &self.config.model_family, &self.client, &self.provider, + &self.otel_event_manager, + &self.session_source, ) .await?; @@ -161,18 +188,29 @@ impl ModelClient { if let Some(path) = &*CODEX_RS_SSE_FIXTURE { // short circuit for tests warn!(path, "Streaming from fixture"); - return stream_from_fixture(path, self.provider.clone()).await; + return stream_from_fixture( + path, + self.provider.clone(), + self.otel_event_manager.clone(), + ) + .await; } let auth_manager = self.auth_manager.clone(); let full_instructions = prompt.get_full_instructions(&self.config.model_family); - let tools_json = create_tools_json_for_responses_api(&prompt.tools)?; - let reasoning = create_reasoning_param_for_request( - &self.config.model_family, - self.effort, - self.summary, - ); + let tools_json: Vec = create_tools_json_for_responses_api(&prompt.tools)?; + + let reasoning = if self.config.model_family.supports_reasoning_summaries { + Some(Reasoning { + effort: self + .effort + .or(self.config.model_family.default_reasoning_effort), + summary: Some(self.summary), + }) + } else { + None + }; let include: Vec = if reasoning.is_some() { vec!["reasoning.encrypted_content".to_string()] @@ -182,19 +220,23 @@ impl ModelClient { let input_with_instructions = prompt.get_formatted_input(); - // Only include `text.verbosity` for GPT-5 family models - let text = if self.config.model_family.family == "gpt-5" { - create_text_param_for_request(self.config.model_verbosity) + let verbosity = if self.config.model_family.support_verbosity { + self.config + .model_verbosity + .or(self.config.model_family.default_verbosity) } else { if self.config.model_verbosity.is_some() { warn!( - "model_verbosity is set but ignored for non-gpt-5 model family: {}", + "model_verbosity is set but ignored as the model does not support verbosity: {}", self.config.model_family.family ); } None }; + // Only include `text.verbosity` for GPT-5 family models + let text = create_text_param_for_request(verbosity, &prompt.output_schema); + // In general, we want to explicitly send `store: false` when using the Responses API, // but in practice, the Azure Responses API rejects `store: false`: // @@ -210,7 +252,7 @@ impl ModelClient { input: &input_with_instructions, tools: &tools_json, tool_choice: "auto", - parallel_tool_calls: false, + parallel_tool_calls: prompt.parallel_tool_calls, reasoning, store: azure_workaround, stream: true, @@ -223,153 +265,210 @@ impl ModelClient { if azure_workaround { attach_item_ids(&mut payload_json, &input_with_instructions); } - let payload_body = serde_json::to_string(&payload_json)?; - let mut attempt = 0; - let max_retries = self.provider.request_max_retries(); - - loop { - attempt += 1; - - // Always fetch the latest auth in case a prior attempt refreshed the token. - let auth = auth_manager.as_ref().and_then(|m| m.auth()); - - trace!( - "POST to {}: {}", - self.provider.get_full_url(&auth), - payload_body.as_str() - ); - - let mut req_builder = self - .provider - .create_request_builder(&self.client, &auth) - .await?; - - req_builder = req_builder - .header("OpenAI-Beta", "responses=experimental") - // Send session_id for compatibility. - .header("conversation_id", self.conversation_id.to_string()) - .header("session_id", self.conversation_id.to_string()) - .header(reqwest::header::ACCEPT, "text/event-stream") - .json(&payload_json); - - if let Some(auth) = auth.as_ref() - && auth.mode == AuthMode::ChatGPT - && let Some(account_id) = auth.get_account_id() + let max_attempts = self.provider.request_max_retries(); + for attempt in 0..=max_attempts { + match self + .attempt_stream_responses(attempt, &payload_json, &auth_manager) + .await { - req_builder = req_builder.header("chatgpt-account-id", account_id); - } - - let res = req_builder.send().await; - if let Ok(resp) = &res { - trace!( - "Response status: {}, cf-ray: {}", - resp.status(), - resp.headers() - .get("cf-ray") - .map(|v| v.to_str().unwrap_or_default()) - .unwrap_or_default() - ); - } - - match res { - Ok(resp) if resp.status().is_success() => { - let (tx_event, rx_event) = mpsc::channel::>(1600); - - if let Some(snapshot) = parse_rate_limit_snapshot(resp.headers()) - && tx_event - .send(Ok(ResponseEvent::RateLimits(snapshot))) - .await - .is_err() - { - debug!("receiver dropped rate limit snapshot event"); - } - - // spawn task to process SSE - let stream = resp.bytes_stream().map_err(CodexErr::Reqwest); - tokio::spawn(process_sse( - stream, - tx_event, - self.provider.stream_idle_timeout(), - )); - - return Ok(ResponseStream { rx_event }); + Ok(stream) => { + return Ok(stream); } - Ok(res) => { - let status = res.status(); - - // Pull out Retry‑After header if present. - let retry_after_secs = res - .headers() - .get(reqwest::header::RETRY_AFTER) - .and_then(|v| v.to_str().ok()) - .and_then(|s| s.parse::().ok()); - - if status == StatusCode::UNAUTHORIZED - && let Some(manager) = auth_manager.as_ref() - && manager.auth().is_some() - { - let _ = manager.refresh_token().await; + Err(StreamAttemptError::Fatal(e)) => { + return Err(e); + } + Err(retryable_attempt_error) => { + if attempt == max_attempts { + return Err(retryable_attempt_error.into_error()); } - // The OpenAI Responses endpoint returns structured JSON bodies even for 4xx/5xx - // errors. When we bubble early with only the HTTP status the caller sees an opaque - // "unexpected status 400 Bad Request" which makes debugging nearly impossible. - // Instead, read (and include) the response text so higher layers and users see the - // exact error message (e.g. "Unknown parameter: 'input[0].metadata'"). The body is - // small and this branch only runs on error paths so the extra allocation is - // negligible. - if !(status == StatusCode::TOO_MANY_REQUESTS - || status == StatusCode::UNAUTHORIZED - || status.is_server_error()) - { - // Surface the error body to callers. Use `unwrap_or_default` per Clippy. - let body = res.text().await.unwrap_or_default(); - return Err(CodexErr::UnexpectedStatus(status, body)); - } + tokio::time::sleep(retryable_attempt_error.delay(attempt)).await; + } + } + } - if status == StatusCode::TOO_MANY_REQUESTS { - let body = res.json::().await.ok(); - if let Some(ErrorResponse { error }) = body { - if error.r#type.as_deref() == Some("usage_limit_reached") { - // Prefer the plan_type provided in the error message if present - // because it's more up to date than the one encoded in the auth - // token. - let plan_type = error - .plan_type - .or_else(|| auth.as_ref().and_then(|a| a.get_plan_type())); - let resets_in_seconds = error.resets_in_seconds; - return Err(CodexErr::UsageLimitReached(UsageLimitReachedError { - plan_type, - resets_in_seconds, - })); - } else if error.r#type.as_deref() == Some("usage_not_included") { - return Err(CodexErr::UsageNotIncluded); - } + unreachable!("stream_responses_attempt should always return"); + } + + /// Single attempt to start a streaming Responses API call. + async fn attempt_stream_responses( + &self, + attempt: u64, + payload_json: &Value, + auth_manager: &Option>, + ) -> std::result::Result { + // Always fetch the latest auth in case a prior attempt refreshed the token. + let auth = auth_manager.as_ref().and_then(|m| m.auth()); + + trace!( + "POST to {}: {}", + self.provider.get_full_url(&auth), + payload_json.to_string() + ); + + let mut req_builder = self + .provider + .create_request_builder(&self.client, &auth) + .await + .map_err(StreamAttemptError::Fatal)?; + + // Include subagent header only for subagent sessions. + if let SessionSource::SubAgent(sub) = &self.session_source { + let subagent = if let crate::protocol::SubAgentSource::Other(label) = sub { + label.clone() + } else { + serde_json::to_value(sub) + .ok() + .and_then(|v| v.as_str().map(std::string::ToString::to_string)) + .unwrap_or_else(|| "other".to_string()) + }; + req_builder = req_builder.header("x-openai-subagent", subagent); + } + + req_builder = req_builder + // Send session_id for compatibility. + .header("conversation_id", self.conversation_id.to_string()) + .header("session_id", self.conversation_id.to_string()) + .header(reqwest::header::ACCEPT, "text/event-stream") + .json(payload_json); + + if let Some(auth) = auth.as_ref() + && auth.mode == AuthMode::ChatGPT + && let Some(account_id) = auth.get_account_id() + { + req_builder = req_builder.header("chatgpt-account-id", account_id); + } + + let res = self + .otel_event_manager + .log_request(attempt, || req_builder.send()) + .await; + + let mut request_id = None; + if let Ok(resp) = &res { + request_id = resp + .headers() + .get("cf-ray") + .map(|v| v.to_str().unwrap_or_default().to_string()); + } + + match res { + Ok(resp) if resp.status().is_success() => { + let (tx_event, rx_event) = mpsc::channel::>(1600); + + if let Some(snapshot) = parse_rate_limit_snapshot(resp.headers()) + && tx_event + .send(Ok(ResponseEvent::RateLimits(snapshot))) + .await + .is_err() + { + debug!("receiver dropped rate limit snapshot event"); + } + + // spawn task to process SSE + let stream = resp.bytes_stream().map_err(move |e| { + CodexErr::ResponseStreamFailed(ResponseStreamFailed { + source: e, + request_id: request_id.clone(), + }) + }); + tokio::spawn(process_sse( + stream, + tx_event, + self.provider.stream_idle_timeout(), + self.otel_event_manager.clone(), + )); + + Ok(ResponseStream { rx_event }) + } + Ok(res) => { + let status = res.status(); + + // Pull out Retry‑After header if present. + let retry_after_secs = res + .headers() + .get(reqwest::header::RETRY_AFTER) + .and_then(|v| v.to_str().ok()) + .and_then(|s| s.parse::().ok()); + let retry_after = retry_after_secs.map(|s| Duration::from_millis(s * 1_000)); + + if status == StatusCode::UNAUTHORIZED + && let Some(manager) = auth_manager.as_ref() + && let Some(auth) = auth.as_ref() + && auth.mode == AuthMode::ChatGPT + && let Err(err) = manager.refresh_token().await + { + let stream_error = match err { + RefreshTokenError::Permanent(failed) => { + StreamAttemptError::Fatal(CodexErr::RefreshTokenFailed(failed)) + } + RefreshTokenError::Transient(other) => { + StreamAttemptError::RetryableTransportError(CodexErr::Io(other)) + } + }; + return Err(stream_error); + } + + // The OpenAI Responses endpoint returns structured JSON bodies even for 4xx/5xx + // errors. When we bubble early with only the HTTP status the caller sees an opaque + // "unexpected status 400 Bad Request" which makes debugging nearly impossible. + // Instead, read (and include) the response text so higher layers and users see the + // exact error message (e.g. "Unknown parameter: 'input[0].metadata'"). The body is + // small and this branch only runs on error paths so the extra allocation is + // negligible. + if !(status == StatusCode::TOO_MANY_REQUESTS + || status == StatusCode::UNAUTHORIZED + || status.is_server_error()) + { + // Surface the error body to callers. Use `unwrap_or_default` per Clippy. + let body = res.text().await.unwrap_or_default(); + return Err(StreamAttemptError::Fatal(CodexErr::UnexpectedStatus( + UnexpectedResponseError { + status, + body, + request_id: None, + }, + ))); + } + + if status == StatusCode::TOO_MANY_REQUESTS { + let rate_limit_snapshot = parse_rate_limit_snapshot(res.headers()); + let body = res.json::().await.ok(); + if let Some(ErrorResponse { error }) = body { + if error.r#type.as_deref() == Some("usage_limit_reached") { + // Prefer the plan_type provided in the error message if present + // because it's more up to date than the one encoded in the auth + // token. + let plan_type = error + .plan_type + .or_else(|| auth.as_ref().and_then(CodexAuth::get_plan_type)); + let resets_at = error + .resets_at + .and_then(|seconds| DateTime::::from_timestamp(seconds, 0)); + let codex_err = CodexErr::UsageLimitReached(UsageLimitReachedError { + plan_type, + resets_at, + rate_limits: rate_limit_snapshot, + }); + return Err(StreamAttemptError::Fatal(codex_err)); + } else if error.r#type.as_deref() == Some("usage_not_included") { + return Err(StreamAttemptError::Fatal(CodexErr::UsageNotIncluded)); + } else if is_quota_exceeded_error(&error) { + return Err(StreamAttemptError::Fatal(CodexErr::QuotaExceeded)); } } - - if attempt > max_retries { - if status == StatusCode::INTERNAL_SERVER_ERROR { - return Err(CodexErr::InternalServerError); - } - - return Err(CodexErr::RetryLimit(status)); - } - - let delay = retry_after_secs - .map(|s| Duration::from_millis(s * 1_000)) - .unwrap_or_else(|| backoff(attempt)); - tokio::time::sleep(delay).await; - } - Err(e) => { - if attempt > max_retries { - return Err(e.into()); - } - let delay = backoff(attempt); - tokio::time::sleep(delay).await; } + + Err(StreamAttemptError::RetryableHttpError { + status, + retry_after, + request_id, + }) } + Err(e) => Err(StreamAttemptError::RetryableTransportError( + CodexErr::ConnectionFailed(ConnectionFailedError { source: e }), + )), } } @@ -377,6 +476,14 @@ impl ModelClient { self.provider.clone() } + pub fn get_otel_event_manager(&self) -> OtelEventManager { + self.otel_event_manager.clone() + } + + pub fn get_session_source(&self) -> SessionSource { + self.session_source.clone() + } + /// Returns the currently configured model slug. pub fn get_model(&self) -> String { self.config.model.clone() @@ -402,6 +509,50 @@ impl ModelClient { } } +enum StreamAttemptError { + RetryableHttpError { + status: StatusCode, + retry_after: Option, + request_id: Option, + }, + RetryableTransportError(CodexErr), + Fatal(CodexErr), +} + +impl StreamAttemptError { + /// attempt is 0-based. + fn delay(&self, attempt: u64) -> Duration { + // backoff() uses 1-based attempts. + let backoff_attempt = attempt + 1; + match self { + Self::RetryableHttpError { retry_after, .. } => { + retry_after.unwrap_or_else(|| backoff(backoff_attempt)) + } + Self::RetryableTransportError { .. } => backoff(backoff_attempt), + Self::Fatal(_) => { + // Should not be called on Fatal errors. + Duration::from_secs(0) + } + } + } + + fn into_error(self) -> CodexErr { + match self { + Self::RetryableHttpError { + status, request_id, .. + } => { + if status == StatusCode::INTERNAL_SERVER_ERROR { + CodexErr::InternalServerError + } else { + CodexErr::RetryLimit(RetryLimitReachedError { status, request_id }) + } + } + Self::RetryableTransportError(error) => error, + Self::Fatal(error) => error, + } + } +} + #[derive(Debug, Deserialize, Serialize)] struct SseEvent { #[serde(rename = "type")] @@ -409,11 +560,10 @@ struct SseEvent { response: Option, item: Option, delta: Option, + summary_index: Option, + content_index: Option, } -#[derive(Debug, Deserialize)] -struct ResponseCreated {} - #[derive(Debug, Deserialize)] struct ResponseCompleted { id: String, @@ -422,11 +572,11 @@ struct ResponseCompleted { #[derive(Debug, Deserialize)] struct ResponseCompletedUsage { - input_tokens: u64, + input_tokens: i64, input_tokens_details: Option, - output_tokens: u64, + output_tokens: i64, output_tokens_details: Option, - total_tokens: u64, + total_tokens: i64, } impl From for TokenUsage { @@ -449,12 +599,12 @@ impl From for TokenUsage { #[derive(Debug, Deserialize)] struct ResponseCompletedInputTokensDetails { - cached_tokens: u64, + cached_tokens: i64, } #[derive(Debug, Deserialize)] struct ResponseCompletedOutputTokensDetails { - reasoning_tokens: u64, + reasoning_tokens: i64, } fn attach_item_ids(payload_json: &mut Value, original_items: &[ResponseItem]) { @@ -484,20 +634,45 @@ fn attach_item_ids(payload_json: &mut Value, original_items: &[ResponseItem]) { } } -fn parse_rate_limit_snapshot(headers: &HeaderMap) -> Option { - let primary_used_percent = parse_header_f64(headers, "x-codex-primary-used-percent")?; - let weekly_used_percent = parse_header_f64(headers, "x-codex-protection-used-percent")?; - let primary_to_weekly_ratio_percent = - parse_header_f64(headers, "x-codex-primary-over-protection-limit-percent")?; - let primary_window_minutes = parse_header_u64(headers, "x-codex-primary-window-minutes")?; - let weekly_window_minutes = parse_header_u64(headers, "x-codex-protection-window-minutes")?; +fn parse_rate_limit_snapshot(headers: &HeaderMap) -> Option { + let primary = parse_rate_limit_window( + headers, + "x-codex-primary-used-percent", + "x-codex-primary-window-minutes", + "x-codex-primary-reset-at", + ); - Some(RateLimitSnapshotEvent { - primary_used_percent, - weekly_used_percent, - primary_to_weekly_ratio_percent, - primary_window_minutes, - weekly_window_minutes, + let secondary = parse_rate_limit_window( + headers, + "x-codex-secondary-used-percent", + "x-codex-secondary-window-minutes", + "x-codex-secondary-reset-at", + ); + + Some(RateLimitSnapshot { primary, secondary }) +} + +fn parse_rate_limit_window( + headers: &HeaderMap, + used_percent_header: &str, + window_minutes_header: &str, + resets_at_header: &str, +) -> Option { + let used_percent: Option = parse_header_f64(headers, used_percent_header); + + used_percent.and_then(|used_percent| { + let window_minutes = parse_header_i64(headers, window_minutes_header); + let resets_at = parse_header_i64(headers, resets_at_header); + + let has_data = used_percent != 0.0 + || window_minutes.is_some_and(|minutes| minutes != 0) + || resets_at.is_some(); + + has_data.then_some(RateLimitWindow { + used_percent, + window_minutes, + resets_at, + }) }) } @@ -508,8 +683,8 @@ fn parse_header_f64(headers: &HeaderMap, name: &str) -> Option { .filter(|v| v.is_finite()) } -fn parse_header_u64(headers: &HeaderMap, name: &str) -> Option { - parse_header_str(headers, name)?.parse::().ok() +fn parse_header_i64(headers: &HeaderMap, name: &str) -> Option { + parse_header_str(headers, name)?.parse::().ok() } fn parse_header_str<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> { @@ -520,6 +695,7 @@ async fn process_sse( stream: S, tx_event: mpsc::Sender>, idle_timeout: Duration, + otel_event_manager: OtelEventManager, ) where S: Stream> + Unpin, { @@ -531,7 +707,12 @@ async fn process_sse( let mut response_error: Option = None; loop { - let sse = match timeout(idle_timeout, stream.next()).await { + let start = std::time::Instant::now(); + let response = timeout(idle_timeout, stream.next()).await; + let duration = start.elapsed(); + otel_event_manager.log_sse_event(&response, duration); + + let sse = match response { Ok(Some(Ok(sse))) => sse, Ok(Some(Err(e))) => { debug!("SSE Error: {e:#}"); @@ -545,6 +726,21 @@ async fn process_sse( id: response_id, usage, }) => { + if let Some(token_usage) = &usage { + otel_event_manager.sse_event_completed( + token_usage.input_tokens, + token_usage.output_tokens, + token_usage + .input_tokens_details + .as_ref() + .map(|d| d.cached_tokens), + token_usage + .output_tokens_details + .as_ref() + .map(|d| d.reasoning_tokens), + token_usage.total_tokens, + ); + } let event = ResponseEvent::Completed { response_id, token_usage: usage.map(Into::into), @@ -552,12 +748,13 @@ async fn process_sse( let _ = tx_event.send(Ok(event)).await; } None => { - let _ = tx_event - .send(Err(response_error.unwrap_or(CodexErr::Stream( - "stream closed before response.completed".into(), - None, - )))) - .await; + let error = response_error.unwrap_or(CodexErr::Stream( + "stream closed before response.completed".into(), + None, + )); + otel_event_manager.see_event_completed_failed(&error); + + let _ = tx_event.send(Err(error)).await; } } return; @@ -624,16 +821,22 @@ async fn process_sse( } } "response.reasoning_summary_text.delta" => { - if let Some(delta) = event.delta { - let event = ResponseEvent::ReasoningSummaryDelta(delta); + if let (Some(delta), Some(summary_index)) = (event.delta, event.summary_index) { + let event = ResponseEvent::ReasoningSummaryDelta { + delta, + summary_index, + }; if tx_event.send(Ok(event)).await.is_err() { return; } } } "response.reasoning_text.delta" => { - if let Some(delta) = event.delta { - let event = ResponseEvent::ReasoningContentDelta(delta); + if let (Some(delta), Some(content_index)) = (event.delta, event.content_index) { + let event = ResponseEvent::ReasoningContentDelta { + delta, + content_index, + }; if tx_event.send(Ok(event)).await.is_err() { return; } @@ -656,12 +859,20 @@ async fn process_sse( if let Some(error) = error { match serde_json::from_value::(error.clone()) { Ok(error) => { - let delay = try_parse_retry_after(&error); - let message = error.message.unwrap_or_default(); - response_error = Some(CodexErr::Stream(message, delay)); + if is_context_window_error(&error) { + response_error = Some(CodexErr::ContextWindowExceeded); + } else if is_quota_exceeded_error(&error) { + response_error = Some(CodexErr::QuotaExceeded); + } else { + let delay = try_parse_retry_after(&error); + let message = error.message.clone().unwrap_or_default(); + response_error = Some(CodexErr::Stream(message, delay)); + } } Err(e) => { - debug!("failed to parse ErrorResponse: {e}"); + let error = format!("failed to parse ErrorResponse: {e}"); + debug!(error); + response_error = Some(CodexErr::Stream(error, None)) } } } @@ -675,7 +886,9 @@ async fn process_sse( response_completed = Some(r); } Err(e) => { - debug!("failed to parse ResponseCompleted: {e}"); + let error = format!("failed to parse ResponseCompleted: {e}"); + debug!(error); + response_error = Some(CodexErr::Stream(error, None)); continue; } }; @@ -688,28 +901,24 @@ async fn process_sse( | "response.in_progress" | "response.output_text.done" => {} "response.output_item.added" => { - if let Some(item) = event.item.as_ref() { - // Detect web_search_call begin and forward a synthetic event upstream. - if let Some(ty) = item.get("type").and_then(|v| v.as_str()) - && ty == "web_search_call" - { - let call_id = item - .get("id") - .and_then(|v| v.as_str()) - .unwrap_or("") - .to_string(); - let ev = ResponseEvent::WebSearchCallBegin { call_id }; - if tx_event.send(Ok(ev)).await.is_err() { - return; - } - } + let Some(item_val) = event.item else { continue }; + let Ok(item) = serde_json::from_value::(item_val) else { + debug!("failed to parse ResponseItem from output_item.done"); + continue; + }; + + let event = ResponseEvent::OutputItemAdded(item); + if tx_event.send(Ok(event)).await.is_err() { + return; } } "response.reasoning_summary_part.added" => { - // Boundary between reasoning summary sections (e.g., titles). - let event = ResponseEvent::ReasoningSummaryPartAdded; - if tx_event.send(Ok(event)).await.is_err() { - return; + if let Some(summary_index) = event.summary_index { + // Boundary between reasoning summary sections (e.g., titles). + let event = ResponseEvent::ReasoningSummaryPartAdded { summary_index }; + if tx_event.send(Ok(event)).await.is_err() { + return; + } } } "response.reasoning_summary_text.done" => {} @@ -722,6 +931,7 @@ async fn process_sse( async fn stream_from_fixture( path: impl AsRef, provider: ModelProviderInfo, + otel_event_manager: OtelEventManager, ) -> Result { let (tx_event, rx_event) = mpsc::channel::>(1600); let f = std::fs::File::open(path.as_ref())?; @@ -740,6 +950,7 @@ async fn stream_from_fixture( stream, tx_event, provider.stream_idle_timeout(), + otel_event_manager, )); Ok(ResponseStream { rx_event }) } @@ -747,8 +958,10 @@ async fn stream_from_fixture( fn rate_limit_regex() -> &'static Regex { static RE: OnceLock = OnceLock::new(); + // Match both OpenAI-style messages like "Please try again in 1.898s" + // and Azure OpenAI-style messages like "Try again in 35 seconds". #[expect(clippy::unwrap_used)] - RE.get_or_init(|| Regex::new(r"Please try again in (\d+(?:\.\d+)?)(s|ms)").unwrap()) + RE.get_or_init(|| Regex::new(r"(?i)try again in\s*(\d+(?:\.\d+)?)\s*(s|ms|seconds?)").unwrap()) } fn try_parse_retry_after(err: &Error) -> Option { @@ -756,7 +969,8 @@ fn try_parse_retry_after(err: &Error) -> Option { return None; } - // parse the Please try again in 1.898s format using regex + // parse retry hints like "try again in 1.898s" or + // "Try again in 35 seconds" using regex let re = rate_limit_regex(); if let Some(message) = &err.message && let Some(captures) = re.captures(message) @@ -766,9 +980,9 @@ fn try_parse_retry_after(err: &Error) -> Option { if let (Some(value), Some(unit)) = (seconds, unit) { let value = value.as_str().parse::().ok()?; - let unit = unit.as_str(); + let unit = unit.as_str().to_ascii_lowercase(); - if unit == "s" { + if unit == "s" || unit.starts_with("second") { return Some(Duration::from_secs_f64(value)); } else if unit == "ms" { return Some(Duration::from_millis(value as u64)); @@ -778,9 +992,18 @@ fn try_parse_retry_after(err: &Error) -> Option { None } +fn is_context_window_error(error: &Error) -> bool { + error.code.as_deref() == Some("context_length_exceeded") +} + +fn is_quota_exceeded_error(error: &Error) -> bool { + error.code.as_deref() == Some("insufficient_quota") +} + #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; use serde_json::json; use tokio::sync::mpsc; use tokio_test::io::Builder as IoBuilder; @@ -795,6 +1018,7 @@ mod tests { async fn collect_events( chunks: &[&[u8]], provider: ModelProviderInfo, + otel_event_manager: OtelEventManager, ) -> Vec> { let mut builder = IoBuilder::new(); for chunk in chunks { @@ -804,7 +1028,12 @@ mod tests { let reader = builder.build(); let stream = ReaderStream::new(reader).map_err(CodexErr::Io); let (tx, mut rx) = mpsc::channel::>(16); - tokio::spawn(process_sse(stream, tx, provider.stream_idle_timeout())); + tokio::spawn(process_sse( + stream, + tx, + provider.stream_idle_timeout(), + otel_event_manager, + )); let mut events = Vec::new(); while let Some(ev) = rx.recv().await { @@ -818,6 +1047,7 @@ mod tests { async fn run_sse( events: Vec, provider: ModelProviderInfo, + otel_event_manager: OtelEventManager, ) -> Vec { let mut body = String::new(); for e in events { @@ -834,7 +1064,12 @@ mod tests { let (tx, mut rx) = mpsc::channel::>(8); let stream = ReaderStream::new(std::io::Cursor::new(body)).map_err(CodexErr::Io); - tokio::spawn(process_sse(stream, tx, provider.stream_idle_timeout())); + tokio::spawn(process_sse( + stream, + tx, + provider.stream_idle_timeout(), + otel_event_manager, + )); let mut out = Vec::new(); while let Some(ev) = rx.recv().await { @@ -843,6 +1078,19 @@ mod tests { out } + fn otel_event_manager() -> OtelEventManager { + OtelEventManager::new( + ConversationId::new(), + "test", + "test", + None, + Some("test@test.com".to_string()), + Some(AuthMode::ChatGPT), + false, + "test".to_string(), + ) + } + // ──────────────────────────── // Tests from `implement-test-for-responses-api-sse-parser` // ──────────────────────────── @@ -884,6 +1132,7 @@ mod tests { base_url: Some("https://test.com".to_string()), env_key: Some("TEST_API_KEY".to_string()), env_key_instructions: None, + experimental_bearer_token: None, wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -894,9 +1143,12 @@ mod tests { requires_openai_auth: false, }; + let otel_event_manager = otel_event_manager(); + let events = collect_events( &[sse1.as_bytes(), sse2.as_bytes(), sse3.as_bytes()], provider, + otel_event_manager, ) .await; @@ -944,6 +1196,7 @@ mod tests { base_url: Some("https://test.com".to_string()), env_key: Some("TEST_API_KEY".to_string()), env_key_instructions: None, + experimental_bearer_token: None, wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -954,7 +1207,9 @@ mod tests { requires_openai_auth: false, }; - let events = collect_events(&[sse1.as_bytes()], provider).await; + let otel_event_manager = otel_event_manager(); + + let events = collect_events(&[sse1.as_bytes()], provider, otel_event_manager).await; assert_eq!(events.len(), 2); @@ -970,7 +1225,7 @@ mod tests { #[tokio::test] async fn error_when_error_event() { - let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_689bcf18d7f08194bf3440ba62fe05d803fee0cdac429894","object":"response","created_at":1755041560,"status":"failed","background":false,"error":{"code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more."}, "usage":null,"user":null,"metadata":{}}}"#; + let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_689bcf18d7f08194bf3440ba62fe05d803fee0cdac429894","object":"response","created_at":1755041560,"status":"failed","background":false,"error":{"code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5.1 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more."}, "usage":null,"user":null,"metadata":{}}}"#; let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); let provider = ModelProviderInfo { @@ -978,6 +1233,7 @@ mod tests { base_url: Some("https://test.com".to_string()), env_key: Some("TEST_API_KEY".to_string()), env_key_instructions: None, + experimental_bearer_token: None, wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -988,7 +1244,9 @@ mod tests { requires_openai_auth: false, }; - let events = collect_events(&[sse1.as_bytes()], provider).await; + let otel_event_manager = otel_event_manager(); + + let events = collect_events(&[sse1.as_bytes()], provider, otel_event_manager).await; assert_eq!(events.len(), 1); @@ -996,7 +1254,7 @@ mod tests { Err(CodexErr::Stream(msg, delay)) => { assert_eq!( msg, - "Rate limit reached for gpt-5 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more." + "Rate limit reached for gpt-5.1 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more." ); assert_eq!(*delay, Some(Duration::from_secs_f64(11.054))); } @@ -1004,6 +1262,111 @@ mod tests { } } + #[tokio::test] + async fn context_window_error_is_fatal() { + let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_5c66275b97b9baef1ed95550adb3b7ec13b17aafd1d2f11b","object":"response","created_at":1759510079,"status":"failed","background":false,"error":{"code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again."},"usage":null,"user":null,"metadata":{}}}"#; + + let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: Some("https://test.com".to_string()), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + experimental_bearer_token: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + requires_openai_auth: false, + }; + + let otel_event_manager = otel_event_manager(); + + let events = collect_events(&[sse1.as_bytes()], provider, otel_event_manager).await; + + assert_eq!(events.len(), 1); + + match &events[0] { + Err(err @ CodexErr::ContextWindowExceeded) => { + assert_eq!(err.to_string(), CodexErr::ContextWindowExceeded.to_string()); + } + other => panic!("unexpected context window event: {other:?}"), + } + } + + #[tokio::test] + async fn context_window_error_with_newline_is_fatal() { + let raw_error = r#"{"type":"response.failed","sequence_number":4,"response":{"id":"resp_fatal_newline","object":"response","created_at":1759510080,"status":"failed","background":false,"error":{"code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try\nagain."},"usage":null,"user":null,"metadata":{}}}"#; + + let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: Some("https://test.com".to_string()), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + experimental_bearer_token: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + requires_openai_auth: false, + }; + + let otel_event_manager = otel_event_manager(); + + let events = collect_events(&[sse1.as_bytes()], provider, otel_event_manager).await; + + assert_eq!(events.len(), 1); + + match &events[0] { + Err(err @ CodexErr::ContextWindowExceeded) => { + assert_eq!(err.to_string(), CodexErr::ContextWindowExceeded.to_string()); + } + other => panic!("unexpected context window event: {other:?}"), + } + } + + #[tokio::test] + async fn quota_exceeded_error_is_fatal() { + let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_fatal_quota","object":"response","created_at":1759771626,"status":"failed","background":false,"error":{"code":"insufficient_quota","message":"You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors."},"incomplete_details":null}}"#; + + let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: Some("https://test.com".to_string()), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + experimental_bearer_token: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + requires_openai_auth: false, + }; + + let otel_event_manager = otel_event_manager(); + + let events = collect_events(&[sse1.as_bytes()], provider, otel_event_manager).await; + + assert_eq!(events.len(), 1); + + match &events[0] { + Err(err @ CodexErr::QuotaExceeded) => { + assert_eq!(err.to_string(), CodexErr::QuotaExceeded.to_string()); + } + other => panic!("unexpected quota exceeded event: {other:?}"), + } + } + // ──────────────────────────── // Table-driven test from `main` // ──────────────────────────── @@ -1083,6 +1446,7 @@ mod tests { base_url: Some("https://test.com".to_string()), env_key: Some("TEST_API_KEY".to_string()), env_key_instructions: None, + experimental_bearer_token: None, wire_api: WireApi::Responses, query_params: None, http_headers: None, @@ -1093,7 +1457,9 @@ mod tests { requires_openai_auth: false, }; - let out = run_sse(evs, provider).await; + let otel_event_manager = otel_event_manager(); + + let out = run_sse(evs, provider, otel_event_manager).await; assert_eq!(out.len(), case.expected_len, "case {}", case.name); assert!( (case.expect_first)(&out[0]), @@ -1107,10 +1473,10 @@ mod tests { fn test_try_parse_retry_after() { let err = Error { r#type: None, - message: Some("Rate limit reached for gpt-5 in organization org- on tokens per min (TPM): Limit 1, Used 1, Requested 19304. Please try again in 28ms. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), + message: Some("Rate limit reached for gpt-5.1 in organization org- on tokens per min (TPM): Limit 1, Used 1, Requested 19304. Please try again in 28ms. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), code: Some("rate_limit_exceeded".to_string()), plan_type: None, - resets_in_seconds: None + resets_at: None }; let delay = try_parse_retry_after(&err); @@ -1121,43 +1487,52 @@ mod tests { fn test_try_parse_retry_after_no_delay() { let err = Error { r#type: None, - message: Some("Rate limit reached for gpt-5 in organization on tokens per min (TPM): Limit 30000, Used 6899, Requested 24050. Please try again in 1.898s. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), + message: Some("Rate limit reached for gpt-5.1 in organization on tokens per min (TPM): Limit 30000, Used 6899, Requested 24050. Please try again in 1.898s. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), code: Some("rate_limit_exceeded".to_string()), plan_type: None, - resets_in_seconds: None + resets_at: None }; let delay = try_parse_retry_after(&err); assert_eq!(delay, Some(Duration::from_secs_f64(1.898))); } #[test] - fn error_response_deserializes_old_schema_known_plan_type_and_serializes_back() { + fn test_try_parse_retry_after_azure() { + let err = Error { + r#type: None, + message: Some("Rate limit exceeded. Try again in 35 seconds.".to_string()), + code: Some("rate_limit_exceeded".to_string()), + plan_type: None, + resets_at: None, + }; + let delay = try_parse_retry_after(&err); + assert_eq!(delay, Some(Duration::from_secs(35))); + } + + #[test] + fn error_response_deserializes_schema_known_plan_type_and_serializes_back() { use crate::token_data::KnownPlan; use crate::token_data::PlanType; - let json = r#"{"error":{"type":"usage_limit_reached","plan_type":"pro","resets_in_seconds":3600}}"#; - let resp: ErrorResponse = - serde_json::from_str(json).expect("should deserialize old schema"); + let json = + r#"{"error":{"type":"usage_limit_reached","plan_type":"pro","resets_at":1704067200}}"#; + let resp: ErrorResponse = serde_json::from_str(json).expect("should deserialize schema"); - assert!(matches!( - resp.error.plan_type, - Some(PlanType::Known(KnownPlan::Pro)) - )); + assert_matches!(resp.error.plan_type, Some(PlanType::Known(KnownPlan::Pro))); let plan_json = serde_json::to_string(&resp.error.plan_type).expect("serialize plan_type"); assert_eq!(plan_json, "\"pro\""); } #[test] - fn error_response_deserializes_old_schema_unknown_plan_type_and_serializes_back() { + fn error_response_deserializes_schema_unknown_plan_type_and_serializes_back() { use crate::token_data::PlanType; let json = - r#"{"error":{"type":"usage_limit_reached","plan_type":"vip","resets_in_seconds":60}}"#; - let resp: ErrorResponse = - serde_json::from_str(json).expect("should deserialize old schema"); + r#"{"error":{"type":"usage_limit_reached","plan_type":"vip","resets_at":1704067260}}"#; + let resp: ErrorResponse = serde_json::from_str(json).expect("should deserialize schema"); - assert!(matches!(resp.error.plan_type, Some(PlanType::Unknown(ref s)) if s == "vip")); + assert_matches!(resp.error.plan_type, Some(PlanType::Unknown(ref s)) if s == "vip"); let plan_json = serde_json::to_string(&resp.error.plan_type).expect("serialize plan_type"); assert_eq!(plan_json, "\"vip\""); diff --git a/codex-rs/core/src/client_common.rs b/codex-rs/core/src/client_common.rs index 15bfb5d400..09cc769220 100644 --- a/codex-rs/core/src/client_common.rs +++ b/codex-rs/core/src/client_common.rs @@ -1,7 +1,7 @@ +use crate::client_common::tools::ToolSpec; use crate::error::Result; use crate::model_family::ModelFamily; -use crate::openai_tools::OpenAiTool; -use crate::protocol::RateLimitSnapshotEvent; +use crate::protocol::RateLimitSnapshot; use crate::protocol::TokenUsage; use codex_apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; use codex_protocol::config_types::ReasoningEffort as ReasoningEffortConfig; @@ -9,8 +9,11 @@ use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; use codex_protocol::config_types::Verbosity as VerbosityConfig; use codex_protocol::models::ResponseItem; use futures::Stream; +use serde::Deserialize; use serde::Serialize; +use serde_json::Value; use std::borrow::Cow; +use std::collections::HashSet; use std::ops::Deref; use std::pin::Pin; use std::task::Context; @@ -20,6 +23,11 @@ use tokio::sync::mpsc; /// Review thread system prompt. Edit `core/src/review_prompt.md` to customize. pub const REVIEW_PROMPT: &str = include_str!("../review_prompt.md"); +// Centralized templates for review-related user messages +pub const REVIEW_EXIT_SUCCESS_TMPL: &str = include_str!("../templates/review/exit_success.xml"); +pub const REVIEW_EXIT_INTERRUPTED_TMPL: &str = + include_str!("../templates/review/exit_interrupted.xml"); + /// API request payload for a single model turn #[derive(Default, Debug, Clone)] pub struct Prompt { @@ -28,10 +36,16 @@ pub struct Prompt { /// Tools available to the model, including additional tools sourced from /// external MCP servers. - pub(crate) tools: Vec, + pub(crate) tools: Vec, + + /// Whether parallel tool calls are permitted for this prompt. + pub(crate) parallel_tool_calls: bool, /// Optional override for the built-in BASE_INSTRUCTIONS. pub base_instructions_override: Option, + + /// Optional the output schema for the model's response. + pub output_schema: Option, } impl Prompt { @@ -45,8 +59,8 @@ impl Prompt { // AND // - there is no apply_patch tool present let is_apply_patch_tool_present = self.tools.iter().any(|tool| match tool { - OpenAiTool::Function(f) => f.name == "apply_patch", - OpenAiTool::Freeform(f) => f.name == "apply_patch", + ToolSpec::Function(f) => f.name == "apply_patch", + ToolSpec::Freeform(f) => f.name == "apply_patch", _ => false, }); if self.base_instructions_override.is_none() @@ -60,26 +74,147 @@ impl Prompt { } pub(crate) fn get_formatted_input(&self) -> Vec { - self.input.clone() + let mut input = self.input.clone(); + + // when using the *Freeform* apply_patch tool specifically, tool outputs + // should be structured text, not json. Do NOT reserialize when using + // the Function tool - note that this differs from the check above for + // instructions. We declare the result as a named variable for clarity. + let is_freeform_apply_patch_tool_present = self.tools.iter().any(|tool| match tool { + ToolSpec::Freeform(f) => f.name == "apply_patch", + _ => false, + }); + if is_freeform_apply_patch_tool_present { + reserialize_shell_outputs(&mut input); + } + + input } } +fn reserialize_shell_outputs(items: &mut [ResponseItem]) { + let mut shell_call_ids: HashSet = HashSet::new(); + + items.iter_mut().for_each(|item| match item { + ResponseItem::LocalShellCall { call_id, id, .. } => { + if let Some(identifier) = call_id.clone().or_else(|| id.clone()) { + shell_call_ids.insert(identifier); + } + } + ResponseItem::CustomToolCall { + id: _, + status: _, + call_id, + name, + input: _, + } => { + if name == "apply_patch" { + shell_call_ids.insert(call_id.clone()); + } + } + ResponseItem::CustomToolCallOutput { call_id, output } => { + if shell_call_ids.remove(call_id) + && let Some(structured) = parse_structured_shell_output(output) + { + *output = structured + } + } + ResponseItem::FunctionCall { name, call_id, .. } + if is_shell_tool_name(name) || name == "apply_patch" => + { + shell_call_ids.insert(call_id.clone()); + } + ResponseItem::FunctionCallOutput { call_id, output } => { + if shell_call_ids.remove(call_id) + && let Some(structured) = parse_structured_shell_output(&output.content) + { + output.content = structured + } + } + _ => {} + }) +} + +fn is_shell_tool_name(name: &str) -> bool { + matches!(name, "shell" | "container.exec" | "shell_command") +} + +#[derive(Deserialize)] +struct ExecOutputJson { + output: String, + metadata: ExecOutputMetadataJson, +} + +#[derive(Deserialize)] +struct ExecOutputMetadataJson { + exit_code: i32, + duration_seconds: f32, +} + +fn parse_structured_shell_output(raw: &str) -> Option { + let parsed: ExecOutputJson = serde_json::from_str(raw).ok()?; + Some(build_structured_output(&parsed)) +} + +fn build_structured_output(parsed: &ExecOutputJson) -> String { + let mut sections = Vec::new(); + sections.push(format!("Exit code: {}", parsed.metadata.exit_code)); + sections.push(format!( + "Wall time: {} seconds", + parsed.metadata.duration_seconds + )); + + let mut output = parsed.output.clone(); + if let Some(total_lines) = extract_total_output_lines(&parsed.output) { + sections.push(format!("Total output lines: {total_lines}")); + if let Some(stripped) = strip_total_output_header(&output) { + output = stripped.to_string(); + } + } + + sections.push("Output:".to_string()); + sections.push(output); + + sections.join("\n") +} + +fn extract_total_output_lines(output: &str) -> Option { + let marker_start = output.find("[... omitted ")?; + let marker = &output[marker_start..]; + let (_, after_of) = marker.split_once(" of ")?; + let (total_segment, _) = after_of.split_once(' ')?; + total_segment.parse::().ok() +} + +fn strip_total_output_header(output: &str) -> Option<&str> { + let after_prefix = output.strip_prefix("Total output lines: ")?; + let (_, remainder) = after_prefix.split_once('\n')?; + let remainder = remainder.strip_prefix('\n').unwrap_or(remainder); + Some(remainder) +} + #[derive(Debug)] pub enum ResponseEvent { Created, OutputItemDone(ResponseItem), + OutputItemAdded(ResponseItem), Completed { response_id: String, token_usage: Option, }, OutputTextDelta(String), - ReasoningSummaryDelta(String), - ReasoningContentDelta(String), - ReasoningSummaryPartAdded, - WebSearchCallBegin { - call_id: String, + ReasoningSummaryDelta { + delta: String, + summary_index: i64, }, - RateLimits(RateLimitSnapshotEvent), + ReasoningContentDelta { + delta: String, + content_index: i64, + }, + ReasoningSummaryPartAdded { + summary_index: i64, + }, + RateLimits(RateLimitSnapshot), } #[derive(Debug, Serialize)] @@ -90,14 +225,31 @@ pub(crate) struct Reasoning { pub(crate) summary: Option, } +#[derive(Debug, Serialize, Default, Clone)] +#[serde(rename_all = "snake_case")] +pub(crate) enum TextFormatType { + #[default] + JsonSchema, +} + +#[derive(Debug, Serialize, Default, Clone)] +pub(crate) struct TextFormat { + pub(crate) r#type: TextFormatType, + pub(crate) strict: bool, + pub(crate) schema: Value, + pub(crate) name: String, +} + /// Controls under the `text` field in the Responses API for GPT-5. -#[derive(Debug, Serialize, Default, Clone, Copy)] +#[derive(Debug, Serialize, Default, Clone)] pub(crate) struct TextControls { #[serde(skip_serializing_if = "Option::is_none")] pub(crate) verbosity: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) format: Option, } -#[derive(Debug, Serialize, Default, Clone, Copy)] +#[derive(Debug, Serialize, Default, Clone)] #[serde(rename_all = "lowercase")] pub(crate) enum OpenAiVerbosity { Low, @@ -139,26 +291,81 @@ pub(crate) struct ResponsesApiRequest<'a> { pub(crate) text: Option, } -pub(crate) fn create_reasoning_param_for_request( - model_family: &ModelFamily, - effort: Option, - summary: ReasoningSummaryConfig, -) -> Option { - if !model_family.supports_reasoning_summaries { - return None; +pub(crate) mod tools { + use crate::tools::spec::JsonSchema; + use serde::Deserialize; + use serde::Serialize; + + /// When serialized as JSON, this produces a valid "Tool" in the OpenAI + /// Responses API. + #[derive(Debug, Clone, Serialize, PartialEq)] + #[serde(tag = "type")] + pub(crate) enum ToolSpec { + #[serde(rename = "function")] + Function(ResponsesApiTool), + #[serde(rename = "local_shell")] + LocalShell {}, + // TODO: Understand why we get an error on web_search although the API docs say it's supported. + // https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses#:~:text=%7B%20type%3A%20%22web_search%22%20%7D%2C + #[serde(rename = "web_search")] + WebSearch {}, + #[serde(rename = "custom")] + Freeform(FreeformTool), } - Some(Reasoning { - effort, - summary: Some(summary), - }) + impl ToolSpec { + pub(crate) fn name(&self) -> &str { + match self { + ToolSpec::Function(tool) => tool.name.as_str(), + ToolSpec::LocalShell {} => "local_shell", + ToolSpec::WebSearch {} => "web_search", + ToolSpec::Freeform(tool) => tool.name.as_str(), + } + } + } + + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] + pub struct FreeformTool { + pub(crate) name: String, + pub(crate) description: String, + pub(crate) format: FreeformToolFormat, + } + + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] + pub struct FreeformToolFormat { + pub(crate) r#type: String, + pub(crate) syntax: String, + pub(crate) definition: String, + } + + #[derive(Debug, Clone, Serialize, PartialEq)] + pub struct ResponsesApiTool { + pub(crate) name: String, + pub(crate) description: String, + /// TODO: Validation. When strict is set to true, the JSON schema, + /// `required` and `additional_properties` must be present. All fields in + /// `properties` must be present in `required`. + pub(crate) strict: bool, + pub(crate) parameters: JsonSchema, + } } pub(crate) fn create_text_param_for_request( verbosity: Option, + output_schema: &Option, ) -> Option { - verbosity.map(|v| TextControls { - verbosity: Some(v.into()), + if verbosity.is_none() && output_schema.is_none() { + return None; + } + + Some(TextControls { + verbosity: verbosity.map(std::convert::Into::into), + format: output_schema.as_ref().map(|schema| TextFormat { + r#type: TextFormatType::JsonSchema, + strict: true, + schema: schema.clone(), + name: "codex_output_schema".to_string(), + }), }) } @@ -207,6 +414,10 @@ mod tests { slug: "gpt-5", expects_apply_patch_instructions: true, }, + InstructionsTestCase { + slug: "gpt-5.1", + expects_apply_patch_instructions: false, + }, InstructionsTestCase { slug: "codex-mini-latest", expects_apply_patch_instructions: true, @@ -216,7 +427,11 @@ mod tests { expects_apply_patch_instructions: false, }, InstructionsTestCase { - slug: "gpt-5-codex", + slug: "gpt-5.1-codex", + expects_apply_patch_instructions: false, + }, + InstructionsTestCase { + slug: "gpt-5.1-codex", expects_apply_patch_instructions: false, }, ]; @@ -242,12 +457,12 @@ mod tests { let input: Vec = vec![]; let tools: Vec = vec![]; let req = ResponsesApiRequest { - model: "gpt-5", + model: "gpt-5.1", instructions: "i", input: &input, tools: &tools, tool_choice: "auto", - parallel_tool_calls: false, + parallel_tool_calls: true, reasoning: None, store: false, stream: true, @@ -255,6 +470,7 @@ mod tests { prompt_cache_key: None, text: Some(TextControls { verbosity: Some(OpenAiVerbosity::Low), + format: None, }), }; @@ -268,16 +484,62 @@ mod tests { } #[test] - fn omits_text_when_not_set() { + fn serializes_text_schema_with_strict_format() { let input: Vec = vec![]; let tools: Vec = vec![]; + let schema = serde_json::json!({ + "type": "object", + "properties": { + "answer": {"type": "string"} + }, + "required": ["answer"], + }); + let text_controls = + create_text_param_for_request(None, &Some(schema.clone())).expect("text controls"); + let req = ResponsesApiRequest { - model: "gpt-5", + model: "gpt-5.1", instructions: "i", input: &input, tools: &tools, tool_choice: "auto", - parallel_tool_calls: false, + parallel_tool_calls: true, + reasoning: None, + store: false, + stream: true, + include: vec![], + prompt_cache_key: None, + text: Some(text_controls), + }; + + let v = serde_json::to_value(&req).expect("json"); + let text = v.get("text").expect("text field"); + assert!(text.get("verbosity").is_none()); + let format = text.get("format").expect("format field"); + + assert_eq!( + format.get("name"), + Some(&serde_json::Value::String("codex_output_schema".into())) + ); + assert_eq!( + format.get("type"), + Some(&serde_json::Value::String("json_schema".into())) + ); + assert_eq!(format.get("strict"), Some(&serde_json::Value::Bool(true))); + assert_eq!(format.get("schema"), Some(&schema)); + } + + #[test] + fn omits_text_when_not_set() { + let input: Vec = vec![]; + let tools: Vec = vec![]; + let req = ResponsesApiRequest { + model: "gpt-5.1", + instructions: "i", + input: &input, + tools: &tools, + tool_choice: "auto", + parallel_tool_calls: true, reasoning: None, store: false, stream: true, diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 05ef09377e..3196176823 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1,148 +1,144 @@ -use std::borrow::Cow; use std::collections::HashMap; -use std::collections::HashSet; -use std::path::Path; +use std::fmt::Debug; use std::path::PathBuf; use std::sync::Arc; use std::sync::atomic::AtomicU64; -use std::time::Duration; use crate::AuthManager; use crate::client_common::REVIEW_PROMPT; -use crate::event_mapping::map_response_item_to_event_messages; -use crate::review_format::format_review_findings_block; +use crate::compact; +use crate::features::Feature; +use crate::function_tool::FunctionCallError; +use crate::parse_command::parse_command; +use crate::parse_turn_item; +use crate::response_processing::process_items; +use crate::terminal; +use crate::user_notification::UserNotifier; +use crate::util::error_or_panic; use async_channel::Receiver; use async_channel::Sender; -use codex_apply_patch::ApplyPatchAction; -use codex_apply_patch::MaybeApplyPatchVerified; -use codex_apply_patch::maybe_parse_apply_patch_verified; -use codex_protocol::mcp_protocol::ConversationId; -use codex_protocol::protocol::ConversationPathResponseEvent; -use codex_protocol::protocol::ExitedReviewModeEvent; +use codex_protocol::ConversationId; +use codex_protocol::items::TurnItem; +use codex_protocol::protocol::FileChange; +use codex_protocol::protocol::HasLegacyEvent; +use codex_protocol::protocol::ItemCompletedEvent; +use codex_protocol::protocol::ItemStartedEvent; +use codex_protocol::protocol::RawResponseItemEvent; use codex_protocol::protocol::ReviewRequest; use codex_protocol::protocol::RolloutItem; +use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::TaskStartedEvent; use codex_protocol::protocol::TurnAbortReason; -use codex_protocol::protocol::TurnAbortedEvent; use codex_protocol::protocol::TurnContextItem; +use futures::future::BoxFuture; use futures::prelude::*; +use futures::stream::FuturesOrdered; use mcp_types::CallToolResult; -use serde::Deserialize; -use serde::Serialize; +use mcp_types::ListResourceTemplatesRequestParams; +use mcp_types::ListResourceTemplatesResult; +use mcp_types::ListResourcesRequestParams; +use mcp_types::ListResourcesResult; +use mcp_types::ReadResourceRequestParams; +use mcp_types::ReadResourceResult; use serde_json; +use serde_json::Value; use tokio::sync::Mutex; +use tokio::sync::RwLock; use tokio::sync::oneshot; -use tokio::task::AbortHandle; +use tokio_util::sync::CancellationToken; use tracing::debug; use tracing::error; use tracing::info; -use tracing::trace; use tracing::warn; use crate::ModelProviderInfo; -use crate::apply_patch; -use crate::apply_patch::ApplyPatchExec; -use crate::apply_patch::CODEX_APPLY_PATCH_ARG1; -use crate::apply_patch::InternalApplyPatchInvocation; -use crate::apply_patch::convert_apply_patch_to_protocol; use crate::client::ModelClient; use crate::client_common::Prompt; use crate::client_common::ResponseEvent; use crate::config::Config; -use crate::config_types::ShellEnvironmentPolicy; -use crate::conversation_history::ConversationHistory; +use crate::config::types::ShellEnvironmentPolicy; +use crate::context_manager::ContextManager; use crate::environment_context::EnvironmentContext; use crate::error::CodexErr; use crate::error::Result as CodexResult; -use crate::error::SandboxErr; -use crate::error::get_error_message_ui; -use crate::exec::ExecParams; -use crate::exec::ExecToolCallOutput; -use crate::exec::SandboxType; -use crate::exec::StdoutStream; +#[cfg(test)] use crate::exec::StreamOutput; -use crate::exec::process_exec_tool_call; -use crate::exec_command::EXEC_COMMAND_TOOL_NAME; -use crate::exec_command::ExecCommandParams; -use crate::exec_command::ExecSessionManager; -use crate::exec_command::WRITE_STDIN_TOOL_NAME; -use crate::exec_command::WriteStdinParams; -use crate::exec_env::create_env; +// Removed: legacy executor wiring replaced by ToolOrchestrator flows. +// legacy normalize_exec_result no longer used after orchestrator migration +use crate::compact::build_compacted_history; +use crate::compact::collect_user_messages; +use crate::mcp::auth::compute_auth_statuses; use crate::mcp_connection_manager::McpConnectionManager; -use crate::mcp_tool_call::handle_mcp_tool_call; use crate::model_family::find_family_for_model; use crate::openai_model_info::get_model_info; -use crate::openai_tools::ApplyPatchToolArgs; -use crate::openai_tools::ToolsConfig; -use crate::openai_tools::ToolsConfigParams; -use crate::openai_tools::get_openai_tools; -use crate::parse_command::parse_command; -use crate::plan_tool::handle_update_plan; use crate::project_doc::get_user_instructions; -use crate::protocol::AgentMessageDeltaEvent; -use crate::protocol::AgentReasoningDeltaEvent; -use crate::protocol::AgentReasoningRawContentDeltaEvent; +use crate::protocol::AgentMessageContentDeltaEvent; use crate::protocol::AgentReasoningSectionBreakEvent; use crate::protocol::ApplyPatchApprovalRequestEvent; use crate::protocol::AskForApproval; use crate::protocol::BackgroundEventEvent; +use crate::protocol::DeprecationNoticeEvent; use crate::protocol::ErrorEvent; use crate::protocol::Event; use crate::protocol::EventMsg; use crate::protocol::ExecApprovalRequestEvent; -use crate::protocol::ExecCommandBeginEvent; -use crate::protocol::ExecCommandEndEvent; -use crate::protocol::FileChange; -use crate::protocol::InputItem; -use crate::protocol::ListCustomPromptsResponseEvent; use crate::protocol::Op; -use crate::protocol::PatchApplyBeginEvent; -use crate::protocol::PatchApplyEndEvent; -use crate::protocol::RateLimitSnapshotEvent; +use crate::protocol::RateLimitSnapshot; +use crate::protocol::ReasoningContentDeltaEvent; +use crate::protocol::ReasoningRawContentDeltaEvent; use crate::protocol::ReviewDecision; -use crate::protocol::ReviewOutputEvent; +use crate::protocol::SandboxCommandAssessment; use crate::protocol::SandboxPolicy; use crate::protocol::SessionConfiguredEvent; use crate::protocol::StreamErrorEvent; use crate::protocol::Submission; -use crate::protocol::TaskCompleteEvent; use crate::protocol::TokenCountEvent; use crate::protocol::TokenUsage; use crate::protocol::TokenUsageInfo; use crate::protocol::TurnDiffEvent; -use crate::protocol::WebSearchBeginEvent; +use crate::protocol::WarningEvent; use crate::rollout::RolloutRecorder; use crate::rollout::RolloutRecorderParams; -use crate::safety::SafetyCheck; -use crate::safety::assess_command_safety; -use crate::safety::assess_safety_for_untrusted_command; use crate::shell; +use crate::state::ActiveTurn; +use crate::state::SessionServices; +use crate::state::SessionState; +use crate::tasks::GhostSnapshotTask; +use crate::tasks::ReviewTask; +use crate::tasks::SessionTask; +use crate::tasks::SessionTaskContext; +use crate::tools::ToolRouter; +use crate::tools::context::SharedTurnDiffTracker; +use crate::tools::parallel::ToolCallRuntime; +use crate::tools::sandboxing::ApprovalStore; +use crate::tools::spec::ToolsConfig; +use crate::tools::spec::ToolsConfigParams; use crate::turn_diff_tracker::TurnDiffTracker; use crate::unified_exec::UnifiedExecSessionManager; +use crate::user_instructions::DeveloperInstructions; use crate::user_instructions::UserInstructions; use crate::user_notification::UserNotification; use crate::util::backoff; +use codex_async_utils::OrCancelExt; +use codex_otel::otel_event_manager::OtelEventManager; use codex_protocol::config_types::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; -use codex_protocol::custom_prompts::CustomPrompt; use codex_protocol::models::ContentItem; use codex_protocol::models::FunctionCallOutputPayload; -use codex_protocol::models::LocalShellAction; use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ResponseItem; -use codex_protocol::models::ShellToolCallParams; use codex_protocol::protocol::InitialHistory; - -pub mod compact; -use self::compact::build_compacted_history; -use self::compact::collect_user_messages; +use codex_protocol::user_input::UserInput; +use codex_utils_readiness::Readiness; +use codex_utils_readiness::ReadinessFlag; +use codex_utils_tokenizer::warm_model_cache; /// The high-level interface to the Codex system. /// It operates as a queue pair where you send submissions and receive events. pub struct Codex { - next_id: AtomicU64, - tx_sub: Sender, - rx_event: Receiver, + pub(crate) next_id: AtomicU64, + pub(crate) tx_sub: Sender, + pub(crate) rx_event: Receiver, } /// Wrapper returned by [`Codex::spawn`] containing the spawned [`Codex`], @@ -156,19 +152,13 @@ pub struct CodexSpawnOk { pub(crate) const INITIAL_SUBMIT_ID: &str = ""; pub(crate) const SUBMISSION_CHANNEL_CAPACITY: usize = 64; -// Model-formatting limits: clients get full streams; oonly content sent to the model is truncated. -pub(crate) const MODEL_FORMAT_MAX_BYTES: usize = 10 * 1024; // 10 KiB -pub(crate) const MODEL_FORMAT_MAX_LINES: usize = 256; // lines -pub(crate) const MODEL_FORMAT_HEAD_LINES: usize = MODEL_FORMAT_MAX_LINES / 2; -pub(crate) const MODEL_FORMAT_TAIL_LINES: usize = MODEL_FORMAT_MAX_LINES - MODEL_FORMAT_HEAD_LINES; // 128 -pub(crate) const MODEL_FORMAT_HEAD_BYTES: usize = MODEL_FORMAT_MAX_BYTES / 2; - impl Codex { /// Spawn a new [`Codex`] and initialize the session. pub async fn spawn( config: Config, auth_manager: Arc, conversation_history: InitialHistory, + session_source: SessionSource, ) -> CodexResult { let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); let (tx_event, rx_event) = async_channel::unbounded(); @@ -177,26 +167,32 @@ impl Codex { let config = Arc::new(config); - let configure_session = ConfigureSession { + let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), model: config.model.clone(), model_reasoning_effort: config.model_reasoning_effort, model_reasoning_summary: config.model_reasoning_summary, + developer_instructions: config.developer_instructions.clone(), user_instructions, base_instructions: config.base_instructions.clone(), + compact_prompt: config.compact_prompt.clone(), approval_policy: config.approval_policy, sandbox_policy: config.sandbox_policy.clone(), - notify: config.notify.clone(), cwd: config.cwd.clone(), + original_config_do_not_use: Arc::clone(&config), + features: config.features.clone(), + session_source, }; // Generate a unique ID for the lifetime of this Codex session. - let (session, turn_context) = Session::new( - configure_session, + let session_source_clone = session_configuration.session_source.clone(); + let session = Session::new( + session_configuration, config.clone(), auth_manager.clone(), tx_event.clone(), conversation_history, + session_source_clone, ) .await .map_err(|e| { @@ -206,7 +202,7 @@ impl Codex { let conversation_id = session.conversation_id; // This task will run until Op::Shutdown is received. - tokio::spawn(submission_loop(session, turn_context, config, rx_sub)); + tokio::spawn(submission_loop(session, config, rx_sub)); let codex = Codex { next_id: AtomicU64::new(0), tx_sub, @@ -250,71 +246,57 @@ impl Codex { } } -/// Mutable state of the agent -#[derive(Default)] -struct State { - approved_commands: HashSet>, - current_task: Option, - pending_approvals: HashMap>, - pending_input: Vec, - history: ConversationHistory, - token_info: Option, - latest_rate_limits: Option, -} - /// Context for an initialized model agent /// /// A session has at most 1 running task at a time, and can be interrupted by user input. pub(crate) struct Session { conversation_id: ConversationId, tx_event: Sender, - - /// Manager for external MCP servers/tools. - mcp_connection_manager: McpConnectionManager, - session_manager: ExecSessionManager, - unified_exec_manager: UnifiedExecSessionManager, - - /// External notifier command (will be passed as args to exec()). When - /// `None` this feature is disabled. - notify: Option>, - - /// Optional rollout recorder for persisting the conversation transcript so - /// sessions can be replayed or inspected later. - rollout: Mutex>, - state: Mutex, - codex_linux_sandbox_exe: Option, - user_shell: shell::Shell, - show_raw_agent_reasoning: bool, + state: Mutex, + pub(crate) active_turn: Mutex>, + pub(crate) services: SessionServices, next_internal_sub_id: AtomicU64, } /// The context needed for a single turn of the conversation. #[derive(Debug)] pub(crate) struct TurnContext { + pub(crate) sub_id: String, pub(crate) client: ModelClient, /// The session's current working directory. All relative paths provided by /// the model as well as sandbox policies are resolved against this path /// instead of `std::env::current_dir()`. pub(crate) cwd: PathBuf, + pub(crate) developer_instructions: Option, pub(crate) base_instructions: Option, + pub(crate) compact_prompt: Option, pub(crate) user_instructions: Option, pub(crate) approval_policy: AskForApproval, pub(crate) sandbox_policy: SandboxPolicy, pub(crate) shell_environment_policy: ShellEnvironmentPolicy, pub(crate) tools_config: ToolsConfig, - pub(crate) is_review_mode: bool, + pub(crate) final_output_json_schema: Option, + pub(crate) codex_linux_sandbox_exe: Option, + pub(crate) tool_call_gate: Arc, } impl TurnContext { - fn resolve_path(&self, path: Option) -> PathBuf { + pub(crate) fn resolve_path(&self, path: Option) -> PathBuf { path.as_ref() .map(PathBuf::from) .map_or_else(|| self.cwd.clone(), |p| self.cwd.join(p)) } + + pub(crate) fn compact_prompt(&self) -> &str { + self.compact_prompt + .as_deref() + .unwrap_or(compact::SUMMARIZATION_PROMPT) + } } -/// Configure the model session. -struct ConfigureSession { +#[allow(dead_code)] +#[derive(Clone)] +pub(crate) struct SessionConfiguration { /// Provider identifier ("openai", "openrouter", ...). provider: ModelProviderInfo, @@ -324,22 +306,23 @@ struct ConfigureSession { model_reasoning_effort: Option, model_reasoning_summary: ReasoningSummaryConfig, + /// Developer instructions that supplement the base instructions. + developer_instructions: Option, + /// Model instructions that are appended to the base instructions. user_instructions: Option, /// Base instructions override. base_instructions: Option, + /// Compact prompt override. + compact_prompt: Option, + /// When to escalate for approval for execution approval_policy: AskForApproval, /// How to sandbox commands executed in the system sandbox_policy: SandboxPolicy, - /// Optional external notifier command tokens. Present only when the - /// client wants the agent to spawn a program after each completed - /// turn. - notify: Option>, - /// Working directory that should be treated as the *root* of the /// session. All relative paths supplied by the model as well as the /// execution sandbox are resolved against this directory **instead** @@ -348,31 +331,129 @@ struct ConfigureSession { /// `ConfigureSession` operation so that the business-logic layer can /// operate deterministically. cwd: PathBuf, + + /// Set of feature flags for this session + features: Features, + + // TODO(pakrym): Remove config from here + original_config_do_not_use: Arc, + /// Source of the session (cli, vscode, exec, mcp, ...) + session_source: SessionSource, +} + +impl SessionConfiguration { + pub(crate) fn apply(&self, updates: &SessionSettingsUpdate) -> Self { + let mut next_configuration = self.clone(); + if let Some(model) = updates.model.clone() { + next_configuration.model = model; + } + if let Some(effort) = updates.reasoning_effort { + next_configuration.model_reasoning_effort = effort; + } + if let Some(summary) = updates.reasoning_summary { + next_configuration.model_reasoning_summary = summary; + } + if let Some(approval_policy) = updates.approval_policy { + next_configuration.approval_policy = approval_policy; + } + if let Some(sandbox_policy) = updates.sandbox_policy.clone() { + next_configuration.sandbox_policy = sandbox_policy; + } + if let Some(cwd) = updates.cwd.clone() { + next_configuration.cwd = cwd; + } + next_configuration + } +} + +#[derive(Default, Clone)] +pub(crate) struct SessionSettingsUpdate { + pub(crate) cwd: Option, + pub(crate) approval_policy: Option, + pub(crate) sandbox_policy: Option, + pub(crate) model: Option, + pub(crate) reasoning_effort: Option>, + pub(crate) reasoning_summary: Option, + pub(crate) final_output_json_schema: Option>, } impl Session { + fn make_turn_context( + auth_manager: Option>, + otel_event_manager: &OtelEventManager, + provider: ModelProviderInfo, + session_configuration: &SessionConfiguration, + conversation_id: ConversationId, + sub_id: String, + ) -> TurnContext { + let config = session_configuration.original_config_do_not_use.clone(); + let model_family = find_family_for_model(&session_configuration.model) + .unwrap_or_else(|| config.model_family.clone()); + let mut per_turn_config = (*config).clone(); + per_turn_config.model = session_configuration.model.clone(); + per_turn_config.model_family = model_family.clone(); + per_turn_config.model_reasoning_effort = session_configuration.model_reasoning_effort; + per_turn_config.model_reasoning_summary = session_configuration.model_reasoning_summary; + if let Some(model_info) = get_model_info(&model_family) { + per_turn_config.model_context_window = Some(model_info.context_window); + } + + let otel_event_manager = otel_event_manager.clone().with_model( + session_configuration.model.as_str(), + session_configuration.model.as_str(), + ); + + let client = ModelClient::new( + Arc::new(per_turn_config), + auth_manager, + otel_event_manager, + provider, + session_configuration.model_reasoning_effort, + session_configuration.model_reasoning_summary, + conversation_id, + session_configuration.session_source.clone(), + ); + + let tools_config = ToolsConfig::new(&ToolsConfigParams { + model_family: &model_family, + features: &config.features, + }); + + TurnContext { + sub_id, + client, + cwd: session_configuration.cwd.clone(), + developer_instructions: session_configuration.developer_instructions.clone(), + base_instructions: session_configuration.base_instructions.clone(), + compact_prompt: session_configuration.compact_prompt.clone(), + user_instructions: session_configuration.user_instructions.clone(), + approval_policy: session_configuration.approval_policy, + sandbox_policy: session_configuration.sandbox_policy.clone(), + shell_environment_policy: config.shell_environment_policy.clone(), + tools_config, + final_output_json_schema: None, + codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), + tool_call_gate: Arc::new(ReadinessFlag::new()), + } + } + async fn new( - configure_session: ConfigureSession, + session_configuration: SessionConfiguration, config: Arc, auth_manager: Arc, tx_event: Sender, initial_history: InitialHistory, - ) -> anyhow::Result<(Arc, TurnContext)> { - let ConfigureSession { - provider, - model, - model_reasoning_effort, - model_reasoning_summary, - user_instructions, - base_instructions, - approval_policy, - sandbox_policy, - notify, - cwd, - } = configure_session; - debug!("Configuring session: model={model}; provider={provider:?}"); - if !cwd.is_absolute() { - return Err(anyhow::anyhow!("cwd is not absolute: {cwd:?}")); + session_source: SessionSource, + ) -> anyhow::Result> { + debug!( + "Configuring session: model={}; provider={:?}", + session_configuration.model, session_configuration.provider + ); + if !session_configuration.cwd.is_absolute() { + return Err(anyhow::anyhow!( + "cwd is not absolute: {:?}", + session_configuration.cwd + )); } let (conversation_id, rollout_params) = match &initial_history { @@ -380,7 +461,11 @@ impl Session { let conversation_id = ConversationId::default(); ( conversation_id, - RolloutRecorderParams::new(conversation_id, user_instructions.clone()), + RolloutRecorderParams::new( + conversation_id, + session_configuration.user_instructions.clone(), + session_source, + ), ) } InitialHistory::Resumed(resumed_history) => ( @@ -389,146 +474,157 @@ impl Session { ), }; - // Error messages to dispatch after SessionConfigured is sent. - let mut post_session_configured_error_events = Vec::::new(); - // Kick off independent async setup tasks in parallel to reduce startup latency. // // - initialize RolloutRecorder with new or resumed session info - // - spin up MCP connection manager // - perform default shell discovery // - load history metadata let rollout_fut = RolloutRecorder::new(&config, rollout_params); - let mcp_fut = McpConnectionManager::new(config.mcp_servers.clone()); let default_shell_fut = shell::default_user_shell(); let history_meta_fut = crate::message_history::history_metadata(&config); + let auth_statuses_fut = compute_auth_statuses( + config.mcp_servers.iter(), + config.mcp_oauth_credentials_store_mode, + ); // Join all independent futures. - let (rollout_recorder, mcp_res, default_shell, (history_log_id, history_entry_count)) = - tokio::join!(rollout_fut, mcp_fut, default_shell_fut, history_meta_fut); + let (rollout_recorder, default_shell, (history_log_id, history_entry_count), auth_statuses) = tokio::join!( + rollout_fut, + default_shell_fut, + history_meta_fut, + auth_statuses_fut + ); let rollout_recorder = rollout_recorder.map_err(|e| { error!("failed to initialize rollout recorder: {e:#}"); anyhow::anyhow!("failed to initialize rollout recorder: {e:#}") })?; let rollout_path = rollout_recorder.rollout_path.clone(); - // Create the mutable state for the Session. - let state = State { - history: ConversationHistory::new(), - ..Default::default() - }; - // Handle MCP manager result and record any startup failures. - let (mcp_connection_manager, failed_clients) = match mcp_res { - Ok((mgr, failures)) => (mgr, failures), - Err(e) => { - let message = format!("Failed to create MCP connection manager: {e:#}"); - error!("{message}"); - post_session_configured_error_events.push(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::Error(ErrorEvent { message }), - }); - (McpConnectionManager::default(), Default::default()) - } - }; + let mut post_session_configured_events = Vec::::new(); - // Surface individual client start-up failures to the user. - if !failed_clients.is_empty() { - for (server_name, err) in failed_clients { - let message = format!("MCP client for `{server_name}` failed to start: {err:#}"); - error!("{message}"); - post_session_configured_error_events.push(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::Error(ErrorEvent { message }), - }); - } + for (alias, feature) in session_configuration.features.legacy_feature_usages() { + let canonical = feature.key(); + let summary = format!("`{alias}` is deprecated. Use `{canonical}` instead."); + let details = if alias == canonical { + None + } else { + Some(format!( + "Enable it with `--enable {canonical}` or `[features].{canonical}` in config.toml. See https://github.com/openai/codex/blob/main/docs/config.md#feature-flags for details." + )) + }; + post_session_configured_events.push(Event { + id: INITIAL_SUBMIT_ID.to_owned(), + msg: EventMsg::DeprecationNotice(DeprecationNoticeEvent { summary, details }), + }); } - // Now that the conversation id is final (may have been updated by resume), - // construct the model client. - let client = ModelClient::new( - config.clone(), - Some(auth_manager.clone()), - provider.clone(), - model_reasoning_effort, - model_reasoning_summary, + let otel_event_manager = OtelEventManager::new( conversation_id, + config.model.as_str(), + config.model_family.slug.as_str(), + auth_manager.auth().and_then(|a| a.get_account_id()), + auth_manager.auth().and_then(|a| a.get_account_email()), + auth_manager.auth().map(|a| a.mode), + config.otel.log_user_prompt, + terminal::user_agent(), ); - let turn_context = TurnContext { - client, - tools_config: ToolsConfig::new(&ToolsConfigParams { - model_family: &config.model_family, - include_plan_tool: config.include_plan_tool, - include_apply_patch_tool: config.include_apply_patch_tool, - include_web_search_request: config.tools_web_search_request, - use_streamable_shell_tool: config.use_experimental_streamable_shell_tool, - include_view_image_tool: config.include_view_image_tool, - experimental_unified_exec_tool: config.use_experimental_unified_exec_tool, - }), - user_instructions, - base_instructions, - approval_policy, - sandbox_policy, - shell_environment_policy: config.shell_environment_policy.clone(), - cwd, - is_review_mode: false, + + otel_event_manager.conversation_starts( + config.model_provider.name.as_str(), + config.model_reasoning_effort, + config.model_reasoning_summary, + config.model_context_window, + config.model_max_output_tokens, + config.model_auto_compact_token_limit, + config.approval_policy, + config.sandbox_policy.clone(), + config.mcp_servers.keys().map(String::as_str).collect(), + config.active_profile.clone(), + ); + + // Create the mutable state for the Session. + let state = SessionState::new(session_configuration.clone()); + + // Warm the tokenizer cache for the session model without blocking startup. + warm_model_cache(&session_configuration.model); + + let services = SessionServices { + mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::default())), + mcp_startup_cancellation_token: CancellationToken::new(), + unified_exec_manager: UnifiedExecSessionManager::default(), + notifier: UserNotifier::new(config.notify.clone()), + rollout: Mutex::new(Some(rollout_recorder)), + user_shell: default_shell, + show_raw_agent_reasoning: config.show_raw_agent_reasoning, + auth_manager: Arc::clone(&auth_manager), + otel_event_manager, + tool_approvals: Mutex::new(ApprovalStore::default()), }; + let sess = Arc::new(Session { conversation_id, tx_event: tx_event.clone(), - mcp_connection_manager, - session_manager: ExecSessionManager::default(), - unified_exec_manager: UnifiedExecSessionManager::default(), - notify, state: Mutex::new(state), - rollout: Mutex::new(Some(rollout_recorder)), - codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), - user_shell: default_shell, - show_raw_agent_reasoning: config.show_raw_agent_reasoning, + active_turn: Mutex::new(None), + services, next_internal_sub_id: AtomicU64::new(0), }); // Dispatch the SessionConfiguredEvent first and then report any errors. // If resuming, include converted initial messages in the payload so UIs can render them immediately. let initial_messages = initial_history.get_event_msgs(); - sess.record_initial_history(&turn_context, initial_history) - .await; let events = std::iter::once(Event { id: INITIAL_SUBMIT_ID.to_owned(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: conversation_id, - model, - reasoning_effort: model_reasoning_effort, + model: session_configuration.model.clone(), + reasoning_effort: session_configuration.model_reasoning_effort, history_log_id, history_entry_count, initial_messages, rollout_path, }), }) - .chain(post_session_configured_error_events.into_iter()); + .chain(post_session_configured_events.into_iter()); for event in events { - sess.send_event(event).await; + sess.send_event_raw(event).await; } + sess.services + .mcp_connection_manager + .write() + .await + .initialize( + config.mcp_servers.clone(), + config.mcp_oauth_credentials_store_mode, + auth_statuses.clone(), + tx_event.clone(), + sess.services.mcp_startup_cancellation_token.clone(), + ) + .await; - Ok((sess, turn_context)) + // record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted. + sess.record_initial_history(initial_history).await; + + Ok(sess) } - pub async fn set_task(&self, task: AgentTask) { - let mut state = self.state.lock().await; - if let Some(current_task) = state.current_task.take() { - current_task.abort(TurnAbortReason::Replaced); - } - state.current_task = Some(task); + pub(crate) fn get_tx_event(&self) -> Sender { + self.tx_event.clone() } - pub async fn remove_task(&self, sub_id: &str) { - let mut state = self.state.lock().await; - if let Some(task) = &state.current_task - && task.sub_id == sub_id + /// Ensure all rollout writes are durably flushed. + pub(crate) async fn flush_rollout(&self) { + let recorder = { + let guard = self.services.rollout.lock().await; + guard.clone() + }; + if let Some(rec) = recorder + && let Err(e) = rec.flush().await { - state.current_task.take(); + warn!("failed to flush rollout recorder: {e}"); } } @@ -539,24 +635,51 @@ impl Session { format!("auto-compact-{id}") } - async fn record_initial_history( - &self, - turn_context: &TurnContext, - conversation_history: InitialHistory, - ) { + async fn record_initial_history(&self, conversation_history: InitialHistory) { + let turn_context = self.new_turn(SessionSettingsUpdate::default()).await; match conversation_history { InitialHistory::New => { // Build and record initial items (user instructions + environment context) - let items = self.build_initial_context(turn_context); - self.record_conversation_items(&items).await; + let items = self.build_initial_context(&turn_context); + self.record_conversation_items(&turn_context, &items).await; + // Ensure initial items are visible to immediate readers (e.g., tests, forks). + self.flush_rollout().await; } InitialHistory::Resumed(_) | InitialHistory::Forked(_) => { let rollout_items = conversation_history.get_rollout_items(); let persist = matches!(conversation_history, InitialHistory::Forked(_)); + // If resuming, warn when the last recorded model differs from the current one. + if let InitialHistory::Resumed(_) = conversation_history + && let Some(prev) = rollout_items.iter().rev().find_map(|it| { + if let RolloutItem::TurnContext(ctx) = it { + Some(ctx.model.as_str()) + } else { + None + } + }) + { + let curr = turn_context.client.get_model(); + if prev != curr { + warn!( + "resuming session with different model: previous={prev}, current={curr}" + ); + self.send_event( + &turn_context, + EventMsg::Warning(WarningEvent { + message: format!( + "This session was recorded with model `{prev}` but is resuming with `{curr}`. \ + Consider switching back to `{prev}` as it may affect Codex performance." + ), + }), + ) + .await; + } + } + // Always add response items to conversation history let reconstructed_history = - self.reconstruct_history_from_rollout(turn_context, &rollout_items); + self.reconstruct_history_from_rollout(&turn_context, &rollout_items); if !reconstructed_history.is_empty() { self.record_into_history(&reconstructed_history).await; } @@ -565,12 +688,87 @@ impl Session { if persist && !rollout_items.is_empty() { self.persist_rollout_items(&rollout_items).await; } + // Flush after seeding history and any persisted rollout copy. + self.flush_rollout().await; } } } + pub(crate) async fn update_settings(&self, updates: SessionSettingsUpdate) { + let mut state = self.state.lock().await; + + state.session_configuration = state.session_configuration.apply(&updates); + } + + pub(crate) async fn new_turn(&self, updates: SessionSettingsUpdate) -> Arc { + let sub_id = self.next_internal_sub_id(); + self.new_turn_with_sub_id(sub_id, updates).await + } + + pub(crate) async fn new_turn_with_sub_id( + &self, + sub_id: String, + updates: SessionSettingsUpdate, + ) -> Arc { + let session_configuration = { + let mut state = self.state.lock().await; + let session_configuration = state.session_configuration.clone().apply(&updates); + state.session_configuration = session_configuration.clone(); + session_configuration + }; + + let mut turn_context: TurnContext = Self::make_turn_context( + Some(Arc::clone(&self.services.auth_manager)), + &self.services.otel_event_manager, + session_configuration.provider.clone(), + &session_configuration, + self.conversation_id, + sub_id, + ); + if let Some(final_schema) = updates.final_output_json_schema { + turn_context.final_output_json_schema = final_schema; + } + Arc::new(turn_context) + } + + fn build_environment_update_item( + &self, + previous: Option<&Arc>, + next: &TurnContext, + ) -> Option { + let prev = previous?; + + let prev_context = EnvironmentContext::from(prev.as_ref()); + let next_context = EnvironmentContext::from(next); + if prev_context.equals_except_shell(&next_context) { + return None; + } + Some(ResponseItem::from(EnvironmentContext::diff( + prev.as_ref(), + next, + ))) + } + /// Persist the event to rollout and send it to clients. - pub(crate) async fn send_event(&self, event: Event) { + pub(crate) async fn send_event(&self, turn_context: &TurnContext, msg: EventMsg) { + let legacy_source = msg.clone(); + let event = Event { + id: turn_context.sub_id.clone(), + msg, + }; + self.send_event_raw(event).await; + + let show_raw_agent_reasoning = self.show_raw_agent_reasoning(); + for legacy in legacy_source.as_legacy_events(show_raw_agent_reasoning) { + let legacy_event = Event { + id: turn_context.sub_id.clone(), + msg: legacy, + }; + self.send_event_raw(legacy_event).await; + } + } + + pub(crate) async fn send_event_raw(&self, event: Event) { // Persist the event into rollout (recorder filters as needed) let rollout_items = vec![RolloutItem::EventMsg(event.msg.clone())]; self.persist_rollout_items(&rollout_items).await; @@ -579,74 +777,149 @@ impl Session { } } + async fn emit_turn_item_started(&self, turn_context: &TurnContext, item: &TurnItem) { + self.send_event( + turn_context, + EventMsg::ItemStarted(ItemStartedEvent { + thread_id: self.conversation_id, + turn_id: turn_context.sub_id.clone(), + item: item.clone(), + }), + ) + .await; + } + + async fn emit_turn_item_completed(&self, turn_context: &TurnContext, item: TurnItem) { + self.send_event( + turn_context, + EventMsg::ItemCompleted(ItemCompletedEvent { + thread_id: self.conversation_id, + turn_id: turn_context.sub_id.clone(), + item, + }), + ) + .await; + } + + pub(crate) async fn assess_sandbox_command( + &self, + turn_context: &TurnContext, + call_id: &str, + command: &[String], + failure_message: Option<&str>, + ) -> Option { + let config = turn_context.client.config(); + let provider = turn_context.client.provider().clone(); + let auth_manager = Arc::clone(&self.services.auth_manager); + let otel = self.services.otel_event_manager.clone(); + crate::sandboxing::assessment::assess_command( + config, + provider, + auth_manager, + &otel, + self.conversation_id, + turn_context.client.get_session_source(), + call_id, + command, + &turn_context.sandbox_policy, + &turn_context.cwd, + failure_message, + ) + .await + } + + /// Emit an exec approval request event and await the user's decision. + /// + /// The request is keyed by `sub_id`/`call_id` so matching responses are delivered + /// to the correct in-flight turn. If the task is aborted, this returns the + /// default `ReviewDecision` (`Denied`). pub async fn request_command_approval( &self, - sub_id: String, + turn_context: &TurnContext, call_id: String, command: Vec, cwd: PathBuf, reason: Option, - ) -> oneshot::Receiver { + risk: Option, + ) -> ReviewDecision { + let sub_id = turn_context.sub_id.clone(); // Add the tx_approve callback to the map before sending the request. let (tx_approve, rx_approve) = oneshot::channel(); let event_id = sub_id.clone(); let prev_entry = { - let mut state = self.state.lock().await; - state.pending_approvals.insert(sub_id, tx_approve) + let mut active = self.active_turn.lock().await; + match active.as_mut() { + Some(at) => { + let mut ts = at.turn_state.lock().await; + ts.insert_pending_approval(sub_id, tx_approve) + } + None => None, + } }; if prev_entry.is_some() { warn!("Overwriting existing pending approval for sub_id: {event_id}"); } - let event = Event { - id: event_id, - msg: EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent { - call_id, - command, - cwd, - reason, - }), - }; - self.send_event(event).await; - rx_approve + let parsed_cmd = parse_command(&command); + let event = EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent { + call_id, + turn_id: turn_context.sub_id.clone(), + command, + cwd, + reason, + risk, + parsed_cmd, + }); + self.send_event(turn_context, event).await; + rx_approve.await.unwrap_or_default() } pub async fn request_patch_approval( &self, - sub_id: String, + turn_context: &TurnContext, call_id: String, - action: &ApplyPatchAction, + changes: HashMap, reason: Option, grant_root: Option, ) -> oneshot::Receiver { + let sub_id = turn_context.sub_id.clone(); // Add the tx_approve callback to the map before sending the request. let (tx_approve, rx_approve) = oneshot::channel(); let event_id = sub_id.clone(); let prev_entry = { - let mut state = self.state.lock().await; - state.pending_approvals.insert(sub_id, tx_approve) + let mut active = self.active_turn.lock().await; + match active.as_mut() { + Some(at) => { + let mut ts = at.turn_state.lock().await; + ts.insert_pending_approval(sub_id, tx_approve) + } + None => None, + } }; if prev_entry.is_some() { warn!("Overwriting existing pending approval for sub_id: {event_id}"); } - let event = Event { - id: event_id, - msg: EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent { - call_id, - changes: convert_apply_patch_to_protocol(action), - reason, - grant_root, - }), - }; - self.send_event(event).await; + let event = EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent { + call_id, + changes, + reason, + grant_root, + }); + self.send_event(turn_context, event).await; rx_approve } pub async fn notify_approval(&self, sub_id: &str, decision: ReviewDecision) { let entry = { - let mut state = self.state.lock().await; - state.pending_approvals.remove(sub_id) + let mut active = self.active_turn.lock().await; + match active.as_mut() { + Some(at) => { + let mut ts = at.turn_state.lock().await; + ts.remove_pending_approval(sub_id) + } + None => None, + } }; match entry { Some(tx_approve) => { @@ -658,16 +931,16 @@ impl Session { } } - pub async fn add_approved_command(&self, cmd: Vec) { - let mut state = self.state.lock().await; - state.approved_commands.insert(cmd); - } - /// Records input items: always append to conversation history and /// persist these response items to rollout. - async fn record_conversation_items(&self, items: &[ResponseItem]) { + pub(crate) async fn record_conversation_items( + &self, + turn_context: &TurnContext, + items: &[ResponseItem], + ) { self.record_into_history(items).await; self.persist_rollout_response_items(items).await; + self.send_raw_response_items(turn_context, items).await; } fn reconstruct_history_from_rollout( @@ -675,14 +948,14 @@ impl Session { turn_context: &TurnContext, rollout_items: &[RolloutItem], ) -> Vec { - let mut history = ConversationHistory::new(); + let mut history = ContextManager::new(); for item in rollout_items { match item { RolloutItem::ResponseItem(response_item) => { history.record_items(std::iter::once(response_item)); } RolloutItem::Compacted(compacted) => { - let snapshot = history.contents(); + let snapshot = history.get_history(); let user_messages = collect_user_messages(&snapshot); let rebuilt = build_compacted_history( self.build_initial_context(turn_context), @@ -694,13 +967,18 @@ impl Session { _ => {} } } - history.contents() + history.get_history() } /// Append ResponseItems to the in-memory conversation history only. - async fn record_into_history(&self, items: &[ResponseItem]) { + pub(crate) async fn record_into_history(&self, items: &[ResponseItem]) { let mut state = self.state.lock().await; - state.history.record_items(items.iter()); + state.record_items(items.iter()); + } + + pub(crate) async fn replace_history(&self, items: Vec) { + let mut state = self.state.lock().await; + state.replace_history(items); } async fn persist_rollout_response_items(&self, items: &[ResponseItem]) { @@ -712,23 +990,42 @@ impl Session { self.persist_rollout_items(&rollout_items).await; } + async fn send_raw_response_items(&self, turn_context: &TurnContext, items: &[ResponseItem]) { + for item in items { + self.send_event( + turn_context, + EventMsg::RawResponseItem(RawResponseItemEvent { item: item.clone() }), + ) + .await; + } + } + pub(crate) fn build_initial_context(&self, turn_context: &TurnContext) -> Vec { - let mut items = Vec::::with_capacity(2); + let mut items = Vec::::with_capacity(3); + if let Some(developer_instructions) = turn_context.developer_instructions.as_deref() { + items.push(DeveloperInstructions::new(developer_instructions.to_string()).into()); + } if let Some(user_instructions) = turn_context.user_instructions.as_deref() { - items.push(UserInstructions::new(user_instructions.to_string()).into()); + items.push( + UserInstructions { + text: user_instructions.to_string(), + directory: turn_context.cwd.to_string_lossy().into_owned(), + } + .into(), + ); } items.push(ResponseItem::from(EnvironmentContext::new( Some(turn_context.cwd.clone()), Some(turn_context.approval_policy), Some(turn_context.sandbox_policy.clone()), - Some(self.user_shell.clone()), + Some(self.user_shell().clone()), ))); items } - async fn persist_rollout_items(&self, items: &[RolloutItem]) { + pub(crate) async fn persist_rollout_items(&self, items: &[RolloutItem]) { let recorder = { - let guard = self.rollout.lock().await; + let guard = self.services.rollout.lock().await; guard.clone() }; if let Some(rec) = recorder @@ -738,467 +1035,289 @@ impl Session { } } - async fn update_token_usage_info( + pub(crate) async fn clone_history(&self) -> ContextManager { + let state = self.state.lock().await; + state.clone_history() + } + + pub(crate) async fn update_token_usage_info( &self, turn_context: &TurnContext, token_usage: Option<&TokenUsage>, ) { - let mut state = self.state.lock().await; - if let Some(token_usage) = token_usage { - let info = TokenUsageInfo::new_or_append( - &state.token_info, - &Some(token_usage.clone()), - turn_context.client.get_model_context_window(), - ); - state.token_info = info; + { + let mut state = self.state.lock().await; + if let Some(token_usage) = token_usage { + state.update_token_info_from_usage( + token_usage, + turn_context.client.get_model_context_window(), + ); + } } + self.send_token_count_event(turn_context).await; } - async fn update_rate_limits(&self, new_rate_limits: RateLimitSnapshotEvent) { - let mut state = self.state.lock().await; - state.latest_rate_limits = Some(new_rate_limits); + pub(crate) async fn override_last_token_usage_estimate( + &self, + turn_context: &TurnContext, + estimated_total_tokens: i64, + ) { + { + let mut state = self.state.lock().await; + let mut info = state.token_info().unwrap_or(TokenUsageInfo { + total_token_usage: TokenUsage::default(), + last_token_usage: TokenUsage::default(), + model_context_window: None, + }); + + info.last_token_usage = TokenUsage { + input_tokens: 0, + cached_input_tokens: 0, + output_tokens: 0, + reasoning_output_tokens: 0, + total_tokens: estimated_total_tokens.max(0), + }; + + if info.model_context_window.is_none() { + info.model_context_window = turn_context.client.get_model_context_window(); + } + + state.set_token_info(Some(info)); + } + self.send_token_count_event(turn_context).await; } - async fn get_token_count_event(&self) -> TokenCountEvent { - let state = self.state.lock().await; - TokenCountEvent { - info: state.token_info.clone(), - rate_limits: state.latest_rate_limits.clone(), + pub(crate) async fn update_rate_limits( + &self, + turn_context: &TurnContext, + new_rate_limits: RateLimitSnapshot, + ) { + { + let mut state = self.state.lock().await; + state.set_rate_limits(new_rate_limits); + } + self.send_token_count_event(turn_context).await; + } + + async fn send_token_count_event(&self, turn_context: &TurnContext) { + let (info, rate_limits) = { + let state = self.state.lock().await; + state.token_info_and_rate_limits() + }; + let event = EventMsg::TokenCount(TokenCountEvent { info, rate_limits }); + self.send_event(turn_context, event).await; + } + + 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; } } /// Record a user input item to conversation history and also persist a /// corresponding UserMessage EventMsg to rollout. - async fn record_input_and_rollout_usermsg(&self, response_input: &ResponseInputItem) { + async fn record_input_and_rollout_usermsg( + &self, + turn_context: &TurnContext, + response_input: &ResponseInputItem, + ) { let response_item: ResponseItem = response_input.clone().into(); // Add to conversation history and persist response item to rollout - self.record_conversation_items(std::slice::from_ref(&response_item)) + self.record_conversation_items(turn_context, std::slice::from_ref(&response_item)) .await; // Derive user message events and persist only UserMessage to rollout - let msgs = - map_response_item_to_event_messages(&response_item, self.show_raw_agent_reasoning); - let user_msgs: Vec = msgs - .into_iter() - .filter_map(|m| match m { - EventMsg::UserMessage(ev) => Some(RolloutItem::EventMsg(EventMsg::UserMessage(ev))), - _ => None, - }) - .collect(); - if !user_msgs.is_empty() { - self.persist_rollout_items(&user_msgs).await; + let turn_item = parse_turn_item(&response_item); + + if let Some(item @ TurnItem::UserMessage(_)) = turn_item { + self.emit_turn_item_started(turn_context, &item).await; + self.emit_turn_item_completed(turn_context, item).await; } } - async fn on_exec_command_begin( + pub(crate) async fn notify_background_event( &self, - turn_diff_tracker: &mut TurnDiffTracker, - exec_command_context: ExecCommandContext, + turn_context: &TurnContext, + message: impl Into, ) { - let ExecCommandContext { - sub_id, - call_id, - command_for_display, - cwd, - apply_patch, - } = exec_command_context; - let msg = match apply_patch { - Some(ApplyPatchCommandContext { - user_explicitly_approved_this_action, - changes, - }) => { - turn_diff_tracker.on_patch_begin(&changes); - - EventMsg::PatchApplyBegin(PatchApplyBeginEvent { - call_id, - auto_approved: !user_explicitly_approved_this_action, - changes, - }) - } - None => EventMsg::ExecCommandBegin(ExecCommandBeginEvent { - call_id, - command: command_for_display.clone(), - cwd, - parsed_cmd: parse_command(&command_for_display) - .into_iter() - .map(Into::into) - .collect(), - }), - }; - let event = Event { - id: sub_id.to_string(), - msg, - }; - self.send_event(event).await; + let event = EventMsg::BackgroundEvent(BackgroundEventEvent { + message: message.into(), + }); + self.send_event(turn_context, event).await; } - async fn on_exec_command_end( + pub(crate) async fn notify_stream_error( &self, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: &str, - call_id: &str, - output: &ExecToolCallOutput, - is_apply_patch: bool, + turn_context: &TurnContext, + message: impl Into, ) { - let ExecToolCallOutput { - stdout, - stderr, - aggregated_output, - duration, - exit_code, - timed_out: _, - } = output; - // Send full stdout/stderr to clients; do not truncate. - let stdout = stdout.text.clone(); - let stderr = stderr.text.clone(); - let formatted_output = format_exec_output_str(output); - let aggregated_output: String = aggregated_output.text.clone(); + let event = EventMsg::StreamError(StreamErrorEvent { + message: message.into(), + }); + self.send_event(turn_context, event).await; + } - let msg = if is_apply_patch { - EventMsg::PatchApplyEnd(PatchApplyEndEvent { - call_id: call_id.to_string(), - stdout, - stderr, - success: *exit_code == 0, - }) - } else { - EventMsg::ExecCommandEnd(ExecCommandEndEvent { - call_id: call_id.to_string(), - stdout, - stderr, - aggregated_output, - exit_code: *exit_code, - duration: *duration, - formatted_output, - }) - }; - - let event = Event { - id: sub_id.to_string(), - msg, - }; - self.send_event(event).await; - - // If this is an apply_patch, after we emit the end patch, emit a second event - // with the full turn diff if there is one. - if is_apply_patch { - let unified_diff = turn_diff_tracker.get_unified_diff(); - if let Ok(Some(unified_diff)) = unified_diff { - let msg = EventMsg::TurnDiff(TurnDiffEvent { unified_diff }); - let event = Event { - id: sub_id.into(), - msg, - }; - self.send_event(event).await; - } + async fn maybe_start_ghost_snapshot( + self: &Arc, + turn_context: Arc, + cancellation_token: CancellationToken, + ) { + if !self + .state + .lock() + .await + .session_configuration + .features + .enabled(Feature::GhostCommit) + { + return; } - } - /// Runs the exec tool call and emits events for the begin and end of the - /// command even on error. - /// - /// Returns the output of the exec tool call. - async fn run_exec_with_events<'a>( - &self, - turn_diff_tracker: &mut TurnDiffTracker, - begin_ctx: ExecCommandContext, - exec_args: ExecInvokeArgs<'a>, - ) -> crate::error::Result { - let is_apply_patch = begin_ctx.apply_patch.is_some(); - let sub_id = begin_ctx.sub_id.clone(); - let call_id = begin_ctx.call_id.clone(); + let token = match turn_context.tool_call_gate.subscribe().await { + Ok(token) => token, + Err(err) => { + warn!("failed to subscribe to ghost snapshot readiness: {err}"); + return; + } + }; - self.on_exec_command_begin(turn_diff_tracker, begin_ctx.clone()) + info!("spawning ghost snapshot task"); + let task = GhostSnapshotTask::new(token); + Arc::new(task) + .run( + Arc::new(SessionTaskContext::new(self.clone())), + turn_context.clone(), + Vec::new(), + cancellation_token, + ) .await; - - let result = process_exec_tool_call( - exec_args.params, - exec_args.sandbox_type, - exec_args.sandbox_policy, - exec_args.sandbox_cwd, - exec_args.codex_linux_sandbox_exe, - exec_args.stdout_stream, - ) - .await; - - let output_stderr; - let borrowed: &ExecToolCallOutput = match &result { - Ok(output) => output, - Err(CodexErr::Sandbox(SandboxErr::Timeout { output })) => output, - Err(e) => { - output_stderr = ExecToolCallOutput { - exit_code: -1, - stdout: StreamOutput::new(String::new()), - stderr: StreamOutput::new(get_error_message_ui(e)), - aggregated_output: StreamOutput::new(get_error_message_ui(e)), - duration: Duration::default(), - timed_out: false, - }; - &output_stderr - } - }; - self.on_exec_command_end( - turn_diff_tracker, - &sub_id, - &call_id, - borrowed, - is_apply_patch, - ) - .await; - - result - } - - /// Helper that emits a BackgroundEvent with the given message. This keeps - /// the call‑sites terse so adding more diagnostics does not clutter the - /// core agent logic. - async fn notify_background_event(&self, sub_id: &str, message: impl Into) { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::BackgroundEvent(BackgroundEventEvent { - message: message.into(), - }), - }; - self.send_event(event).await; - } - - async fn notify_stream_error(&self, sub_id: &str, message: impl Into) { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::StreamError(StreamErrorEvent { - message: message.into(), - }), - }; - self.send_event(event).await; - } - - /// Build the full turn input by concatenating the current conversation - /// history with additional items for this turn. - pub async fn turn_input_with_history(&self, extra: Vec) -> Vec { - let history = { - let state = self.state.lock().await; - state.history.contents() - }; - [history, extra].concat() } /// Returns the input if there was no task running to inject into - pub async fn inject_input(&self, input: Vec) -> Result<(), Vec> { - let mut state = self.state.lock().await; - if state.current_task.is_some() { - state.pending_input.push(input.into()); - Ok(()) - } else { - Err(input) + pub async fn inject_input(&self, input: Vec) -> Result<(), Vec> { + let mut active = self.active_turn.lock().await; + match active.as_mut() { + Some(at) => { + let mut ts = at.turn_state.lock().await; + ts.push_pending_input(input.into()); + Ok(()) + } + None => Err(input), } } pub async fn get_pending_input(&self) -> Vec { - let mut state = self.state.lock().await; - if state.pending_input.is_empty() { - Vec::with_capacity(0) - } else { - let mut ret = Vec::new(); - std::mem::swap(&mut ret, &mut state.pending_input); - ret + let mut active = self.active_turn.lock().await; + match active.as_mut() { + Some(at) => { + let mut ts = at.turn_state.lock().await; + ts.take_pending_input() + } + None => Vec::with_capacity(0), } } + pub async fn list_resources( + &self, + server: &str, + params: Option, + ) -> anyhow::Result { + self.services + .mcp_connection_manager + .read() + .await + .list_resources(server, params) + .await + } + + pub async fn list_resource_templates( + &self, + server: &str, + params: Option, + ) -> anyhow::Result { + self.services + .mcp_connection_manager + .read() + .await + .list_resource_templates(server, params) + .await + } + + pub async fn read_resource( + &self, + server: &str, + params: ReadResourceRequestParams, + ) -> anyhow::Result { + self.services + .mcp_connection_manager + .read() + .await + .read_resource(server, params) + .await + } + pub async fn call_tool( &self, server: &str, tool: &str, arguments: Option, - timeout: Option, ) -> anyhow::Result { - self.mcp_connection_manager - .call_tool(server, tool, arguments, timeout) + self.services + .mcp_connection_manager + .read() + .await + .call_tool(server, tool, arguments) .await } - pub async fn interrupt_task(&self) { + pub(crate) async fn parse_mcp_tool_name(&self, tool_name: &str) -> Option<(String, String)> { + self.services + .mcp_connection_manager + .read() + .await + .parse_tool_name(tool_name) + .await + } + + pub async fn interrupt_task(self: &Arc) { info!("interrupt received: abort current task, if any"); - let mut state = self.state.lock().await; - state.pending_approvals.clear(); - state.pending_input.clear(); - if let Some(task) = state.current_task.take() { - task.abort(TurnAbortReason::Interrupted); + let has_active_turn = { self.active_turn.lock().await.is_some() }; + if has_active_turn { + self.abort_all_tasks(TurnAbortReason::Interrupted).await; + } else { + self.cancel_mcp_startup().await; } } - fn interrupt_task_sync(&self) { - if let Ok(mut state) = self.state.try_lock() { - state.pending_approvals.clear(); - state.pending_input.clear(); - if let Some(task) = state.current_task.take() { - task.abort(TurnAbortReason::Interrupted); - } - } + pub(crate) fn notifier(&self) -> &UserNotifier { + &self.services.notifier } - /// Spawn the configured notifier (if any) with the given JSON payload as - /// the last argument. Failures are logged but otherwise ignored so that - /// notification issues do not interfere with the main workflow. - fn maybe_notify(&self, notification: UserNotification) { - let Some(notify_command) = &self.notify else { - return; - }; + pub(crate) fn user_shell(&self) -> &shell::Shell { + &self.services.user_shell + } - if notify_command.is_empty() { - return; - } + fn show_raw_agent_reasoning(&self) -> bool { + self.services.show_raw_agent_reasoning + } - let Ok(json) = serde_json::to_string(¬ification) else { - error!("failed to serialise notification payload"); - return; - }; - - let mut command = std::process::Command::new(¬ify_command[0]); - if notify_command.len() > 1 { - command.args(¬ify_command[1..]); - } - command.arg(json); - - // Fire-and-forget – we do not wait for completion. - if let Err(e) = command.spawn() { - warn!("failed to spawn notifier '{}': {e}", notify_command[0]); - } + async fn cancel_mcp_startup(&self) { + self.services.mcp_startup_cancellation_token.cancel(); } } -impl Drop for Session { - fn drop(&mut self) { - self.interrupt_task_sync(); - } -} - -#[derive(Clone, Debug)] -pub(crate) struct ExecCommandContext { - pub(crate) sub_id: String, - pub(crate) call_id: String, - pub(crate) command_for_display: Vec, - pub(crate) cwd: PathBuf, - pub(crate) apply_patch: Option, -} - -#[derive(Clone, Debug)] -pub(crate) struct ApplyPatchCommandContext { - pub(crate) user_explicitly_approved_this_action: bool, - pub(crate) changes: HashMap, -} - -#[derive(Clone, Debug, Eq, PartialEq)] -enum AgentTaskKind { - Regular, - Review, - Compact, -} - -/// A series of Turns in response to user input. -pub(crate) struct AgentTask { - sess: Arc, - sub_id: String, - handle: AbortHandle, - kind: AgentTaskKind, -} - -impl AgentTask { - fn spawn( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, - ) -> Self { - let handle = { - let sess = sess.clone(); - let sub_id = sub_id.clone(); - let tc = Arc::clone(&turn_context); - tokio::spawn(async move { run_task(sess, tc, sub_id, input).await }).abort_handle() - }; - Self { - sess, - sub_id, - handle, - kind: AgentTaskKind::Regular, - } - } - - fn review( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, - ) -> Self { - let handle = { - let sess = sess.clone(); - let sub_id = sub_id.clone(); - let tc = Arc::clone(&turn_context); - tokio::spawn(async move { run_task(sess, tc, sub_id, input).await }).abort_handle() - }; - Self { - sess, - sub_id, - handle, - kind: AgentTaskKind::Review, - } - } - - fn compact( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, - compact_instructions: String, - ) -> Self { - let handle = { - let sess = sess.clone(); - let sub_id = sub_id.clone(); - let tc = Arc::clone(&turn_context); - tokio::spawn(async move { - compact::run_compact_task(sess, tc, sub_id, input, compact_instructions).await - }) - .abort_handle() - }; - Self { - sess, - sub_id, - handle, - kind: AgentTaskKind::Compact, - } - } - - fn abort(self, reason: TurnAbortReason) { - // TOCTOU? - if !self.handle.is_finished() { - self.handle.abort(); - let event = Event { - id: self.sub_id.clone(), - msg: EventMsg::TurnAborted(TurnAbortedEvent { reason }), - }; - let sess = self.sess; - tokio::spawn(async move { - if self.kind == AgentTaskKind::Review { - exit_review_mode(sess.clone(), self.sub_id, None).await; - } - sess.send_event(event).await; - }); - } - } -} - -async fn submission_loop( - sess: Arc, - turn_context: TurnContext, - config: Arc, - rx_sub: Receiver, -) { - // Wrap once to avoid cloning TurnContext for each task. - let mut turn_context = Arc::new(turn_context); +async fn submission_loop(sess: Arc, config: Arc, rx_sub: Receiver) { + let mut previous_context: Option> = None; // To break out of this loop, send Op::Shutdown. while let Ok(sub) = rx_sub.recv().await { debug!(?sub, "Submission"); - match sub.op { + match sub.op.clone() { Op::Interrupt => { - sess.interrupt_task().await; + handlers::interrupt(&sess).await; } Op::OverrideTurnContext { cwd, @@ -1208,347 +1327,363 @@ async fn submission_loop( effort, summary, } => { - // Recalculate the persistent turn context with provided overrides. - let prev = Arc::clone(&turn_context); - let provider = prev.client.get_provider(); - - // Effective model + family - let (effective_model, effective_family) = if let Some(ref m) = model { - let fam = - find_family_for_model(m).unwrap_or_else(|| config.model_family.clone()); - (m.clone(), fam) - } else { - (prev.client.get_model(), prev.client.get_model_family()) - }; - - // Effective reasoning settings - let effective_effort = effort.unwrap_or(prev.client.get_reasoning_effort()); - let effective_summary = summary.unwrap_or(prev.client.get_reasoning_summary()); - - let auth_manager = prev.client.get_auth_manager(); - - // Build updated config for the client - let mut updated_config = (*config).clone(); - updated_config.model = effective_model.clone(); - updated_config.model_family = effective_family.clone(); - if let Some(model_info) = get_model_info(&effective_family) { - updated_config.model_context_window = Some(model_info.context_window); - } - - let client = ModelClient::new( - Arc::new(updated_config), - auth_manager, - provider, - effective_effort, - effective_summary, - sess.conversation_id, - ); - - let new_approval_policy = approval_policy.unwrap_or(prev.approval_policy); - let new_sandbox_policy = sandbox_policy - .clone() - .unwrap_or(prev.sandbox_policy.clone()); - let new_cwd = cwd.clone().unwrap_or_else(|| prev.cwd.clone()); - - let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &effective_family, - include_plan_tool: config.include_plan_tool, - include_apply_patch_tool: config.include_apply_patch_tool, - include_web_search_request: config.tools_web_search_request, - use_streamable_shell_tool: config.use_experimental_streamable_shell_tool, - include_view_image_tool: config.include_view_image_tool, - experimental_unified_exec_tool: config.use_experimental_unified_exec_tool, - }); - - let new_turn_context = TurnContext { - client, - tools_config, - user_instructions: prev.user_instructions.clone(), - base_instructions: prev.base_instructions.clone(), - approval_policy: new_approval_policy, - sandbox_policy: new_sandbox_policy.clone(), - shell_environment_policy: prev.shell_environment_policy.clone(), - cwd: new_cwd.clone(), - is_review_mode: false, - }; - - // Install the new persistent context for subsequent tasks/turns. - turn_context = Arc::new(new_turn_context); - - // Optionally persist changes to model / effort - if cwd.is_some() || approval_policy.is_some() || sandbox_policy.is_some() { - sess.record_conversation_items(&[ResponseItem::from(EnvironmentContext::new( + handlers::override_turn_context( + &sess, + SessionSettingsUpdate { cwd, approval_policy, sandbox_policy, - // Shell is not configurable from turn to turn - None, - ))]) + model, + reasoning_effort: effort, + reasoning_summary: summary, + ..Default::default() + }, + ) + .await; + } + Op::UserInput { .. } | Op::UserTurn { .. } => { + handlers::user_input_or_turn(&sess, sub.id.clone(), sub.op, &mut previous_context) .await; + } + Op::ExecApproval { id, decision } => { + handlers::exec_approval(&sess, id, decision).await; + } + Op::PatchApproval { id, decision } => { + handlers::patch_approval(&sess, id, decision).await; + } + Op::AddToHistory { text } => { + handlers::add_to_history(&sess, &config, text).await; + } + Op::GetHistoryEntryRequest { offset, log_id } => { + handlers::get_history_entry_request(&sess, &config, sub.id.clone(), offset, log_id) + .await; + } + Op::ListMcpTools => { + handlers::list_mcp_tools(&sess, &config, sub.id.clone()).await; + } + Op::ListCustomPrompts => { + handlers::list_custom_prompts(&sess, sub.id.clone()).await; + } + Op::Undo => { + handlers::undo(&sess, sub.id.clone()).await; + } + Op::Compact => { + handlers::compact(&sess, sub.id.clone()).await; + } + Op::RunUserShellCommand { command } => { + handlers::run_user_shell_command( + &sess, + sub.id.clone(), + command, + &mut previous_context, + ) + .await; + } + Op::Shutdown => { + if handlers::shutdown(&sess, sub.id.clone()).await { + break; } } - Op::UserInput { items } => { - // attempt to inject input into current task - if let Err(items) = sess.inject_input(items).await { - // no current task, spawn a new one - let task = - AgentTask::spawn(sess.clone(), Arc::clone(&turn_context), sub.id, items); - sess.set_task(task).await; - } + Op::Review { review_request } => { + handlers::review(&sess, &config, sub.id.clone(), review_request).await; } + _ => {} // Ignore unknown ops; enum is non_exhaustive to allow extensions. + } + } + debug!("Agent loop exited"); +} + +/// Operation handlers +mod handlers { + use crate::codex::Session; + use crate::codex::SessionSettingsUpdate; + use crate::codex::TurnContext; + + use crate::codex::spawn_review_thread; + use crate::config::Config; + use crate::mcp::auth::compute_auth_statuses; + use crate::tasks::CompactTask; + use crate::tasks::RegularTask; + use crate::tasks::UndoTask; + use crate::tasks::UserShellCommandTask; + use codex_protocol::custom_prompts::CustomPrompt; + use codex_protocol::protocol::ErrorEvent; + use codex_protocol::protocol::Event; + use codex_protocol::protocol::EventMsg; + use codex_protocol::protocol::ListCustomPromptsResponseEvent; + use codex_protocol::protocol::Op; + use codex_protocol::protocol::ReviewDecision; + use codex_protocol::protocol::ReviewRequest; + use codex_protocol::protocol::TurnAbortReason; + use codex_protocol::user_input::UserInput; + use std::sync::Arc; + use tracing::info; + use tracing::warn; + + pub async fn interrupt(sess: &Arc) { + sess.interrupt_task().await; + } + + pub async fn override_turn_context(sess: &Session, updates: SessionSettingsUpdate) { + sess.update_settings(updates).await; + } + + pub async fn user_input_or_turn( + sess: &Arc, + sub_id: String, + op: Op, + previous_context: &mut Option>, + ) { + let (items, updates) = match op { Op::UserTurn { - items, cwd, approval_policy, sandbox_policy, model, effort, summary, - } => { - // attempt to inject input into current task - if let Err(items) = sess.inject_input(items).await { - // Derive a fresh TurnContext for this turn using the provided overrides. - let provider = turn_context.client.get_provider(); - let auth_manager = turn_context.client.get_auth_manager(); + final_output_json_schema, + items, + } => ( + items, + SessionSettingsUpdate { + cwd: Some(cwd), + approval_policy: Some(approval_policy), + sandbox_policy: Some(sandbox_policy), + model: Some(model), + reasoning_effort: Some(effort), + reasoning_summary: Some(summary), + final_output_json_schema: Some(final_output_json_schema), + }, + ), + Op::UserInput { items } => (items, SessionSettingsUpdate::default()), + _ => unreachable!(), + }; - // Derive a model family for the requested model; fall back to the session's. - let model_family = find_family_for_model(&model) - .unwrap_or_else(|| config.model_family.clone()); + let current_context = sess.new_turn_with_sub_id(sub_id, updates).await; + current_context + .client + .get_otel_event_manager() + .user_prompt(&items); - // Create a per‑turn Config clone with the requested model/family. - let mut per_turn_config = (*config).clone(); - per_turn_config.model = model.clone(); - per_turn_config.model_family = model_family.clone(); - if let Some(model_info) = get_model_info(&model_family) { - per_turn_config.model_context_window = Some(model_info.context_window); - } - - // Build a new client with per‑turn reasoning settings. - // Reuse the same provider and session id; auth defaults to env/API key. - let client = ModelClient::new( - Arc::new(per_turn_config), - auth_manager, - provider, - effort, - summary, - sess.conversation_id, - ); - - let fresh_turn_context = TurnContext { - client, - tools_config: ToolsConfig::new(&ToolsConfigParams { - model_family: &model_family, - include_plan_tool: config.include_plan_tool, - include_apply_patch_tool: config.include_apply_patch_tool, - include_web_search_request: config.tools_web_search_request, - use_streamable_shell_tool: config - .use_experimental_streamable_shell_tool, - include_view_image_tool: config.include_view_image_tool, - experimental_unified_exec_tool: config - .use_experimental_unified_exec_tool, - }), - user_instructions: turn_context.user_instructions.clone(), - base_instructions: turn_context.base_instructions.clone(), - approval_policy, - sandbox_policy, - shell_environment_policy: turn_context.shell_environment_policy.clone(), - cwd, - is_review_mode: false, - }; - - // if the environment context has changed, record it in the conversation history - let previous_env_context = EnvironmentContext::from(turn_context.as_ref()); - let new_env_context = EnvironmentContext::from(&fresh_turn_context); - if !new_env_context.equals_except_shell(&previous_env_context) { - sess.record_conversation_items(&[ResponseItem::from(new_env_context)]) - .await; - } - - // Install the new persistent context for subsequent tasks/turns. - turn_context = Arc::new(fresh_turn_context); - - // no current task, spawn a new one with the per‑turn context - let task = - AgentTask::spawn(sess.clone(), Arc::clone(&turn_context), sub.id, items); - sess.set_task(task).await; - } - } - Op::ExecApproval { id, decision } => match decision { - ReviewDecision::Abort => { - sess.interrupt_task().await; - } - other => sess.notify_approval(&id, other).await, - }, - Op::PatchApproval { id, decision } => match decision { - ReviewDecision::Abort => { - sess.interrupt_task().await; - } - other => sess.notify_approval(&id, other).await, - }, - Op::AddToHistory { text } => { - let id = sess.conversation_id; - let config = config.clone(); - tokio::spawn(async move { - if let Err(e) = crate::message_history::append_entry(&text, &id, &config).await - { - warn!("failed to append to message history: {e}"); - } - }); - } - - Op::GetHistoryEntryRequest { offset, log_id } => { - let config = config.clone(); - let sess_clone = sess.clone(); - let sub_id = sub.id.clone(); - - tokio::spawn(async move { - // Run lookup in blocking thread because it does file IO + locking. - let entry_opt = tokio::task::spawn_blocking(move || { - crate::message_history::lookup(log_id, offset, &config) - }) - .await - .unwrap_or(None); - - let event = Event { - id: sub_id, - msg: EventMsg::GetHistoryEntryResponse( - crate::protocol::GetHistoryEntryResponseEvent { - offset, - log_id, - entry: entry_opt.map(|e| { - codex_protocol::message_history::HistoryEntry { - conversation_id: e.session_id, - ts: e.ts, - text: e.text, - } - }), - }, - ), - }; - - sess_clone.send_event(event).await; - }); - } - Op::ListMcpTools => { - let sub_id = sub.id.clone(); - - // This is a cheap lookup from the connection manager's cache. - let tools = sess.mcp_connection_manager.list_all_tools(); - let event = Event { - id: sub_id, - msg: EventMsg::McpListToolsResponse( - crate::protocol::McpListToolsResponseEvent { tools }, - ), - }; - sess.send_event(event).await; - } - Op::ListCustomPrompts => { - let sub_id = sub.id.clone(); - - let custom_prompts: Vec = - if let Some(dir) = crate::custom_prompts::default_prompts_dir() { - crate::custom_prompts::discover_prompts_in(&dir).await - } else { - Vec::new() - }; - - let event = Event { - id: sub_id, - msg: EventMsg::ListCustomPromptsResponse(ListCustomPromptsResponseEvent { - custom_prompts, - }), - }; - sess.send_event(event).await; - } - Op::Compact => { - // Attempt to inject input into current task - if let Err(items) = sess - .inject_input(vec![InputItem::Text { - text: compact::COMPACT_TRIGGER_TEXT.to_string(), - }]) - .await - { - compact::spawn_compact_task( - sess.clone(), - Arc::clone(&turn_context), - sub.id, - items, - ) + // Attempt to inject input into current task + if let Err(items) = sess.inject_input(items).await { + if let Some(env_item) = + sess.build_environment_update_item(previous_context.as_ref(), ¤t_context) + { + sess.record_conversation_items(¤t_context, std::slice::from_ref(&env_item)) .await; - } } - Op::Shutdown => { - info!("Shutting down Codex instance"); - // Gracefully flush and shutdown rollout recorder on session end so tests - // that inspect the rollout file do not race with the background writer. - let recorder_opt = { - let mut guard = sess.rollout.lock().await; - guard.take() - }; - if let Some(rec) = recorder_opt - && let Err(e) = rec.shutdown().await - { - warn!("failed to shutdown rollout recorder: {e}"); - let event = Event { - id: sub.id.clone(), - msg: EventMsg::Error(ErrorEvent { - message: "Failed to shutdown rollout recorder".to_string(), - }), - }; - sess.send_event(event).await; - } - - let event = Event { - id: sub.id.clone(), - msg: EventMsg::ShutdownComplete, - }; - sess.send_event(event).await; - break; - } - Op::GetPath => { - let sub_id = sub.id.clone(); - // Flush rollout writes before returning the path so readers observe a consistent file. - let (path, rec_opt) = { - let guard = sess.rollout.lock().await; - match guard.as_ref() { - Some(rec) => (rec.get_rollout_path(), Some(rec.clone())), - None => { - error!("rollout recorder not found"); - continue; - } - } - }; - if let Some(rec) = rec_opt - && let Err(e) = rec.flush().await - { - warn!("failed to flush rollout recorder before GetHistory: {e}"); - } - let event = Event { - id: sub_id.clone(), - msg: EventMsg::ConversationPath(ConversationPathResponseEvent { - conversation_id: sess.conversation_id, - path, - }), - }; - sess.send_event(event).await; - } - Op::Review { review_request } => { - spawn_review_thread( - sess.clone(), - config.clone(), - turn_context.clone(), - sub.id, - review_request, - ) + sess.spawn_task(Arc::clone(¤t_context), items, RegularTask) .await; - } - _ => { - // Ignore unknown ops; enum is non_exhaustive to allow extensions. - } + *previous_context = Some(current_context); } } - debug!("Agent loop exited"); + + pub async fn run_user_shell_command( + sess: &Arc, + sub_id: String, + command: String, + previous_context: &mut Option>, + ) { + let turn_context = sess + .new_turn_with_sub_id(sub_id, SessionSettingsUpdate::default()) + .await; + sess.spawn_task( + Arc::clone(&turn_context), + Vec::new(), + UserShellCommandTask::new(command), + ) + .await; + *previous_context = Some(turn_context); + } + + pub async fn exec_approval(sess: &Arc, id: String, decision: ReviewDecision) { + match decision { + ReviewDecision::Abort => { + sess.interrupt_task().await; + } + other => sess.notify_approval(&id, other).await, + } + } + + pub async fn patch_approval(sess: &Arc, id: String, decision: ReviewDecision) { + match decision { + ReviewDecision::Abort => { + sess.interrupt_task().await; + } + other => sess.notify_approval(&id, other).await, + } + } + + pub async fn add_to_history(sess: &Arc, config: &Arc, text: String) { + let id = sess.conversation_id; + let config = Arc::clone(config); + tokio::spawn(async move { + if let Err(e) = crate::message_history::append_entry(&text, &id, &config).await { + warn!("failed to append to message history: {e}"); + } + }); + } + + pub async fn get_history_entry_request( + sess: &Arc, + config: &Arc, + sub_id: String, + offset: usize, + log_id: u64, + ) { + let config = Arc::clone(config); + let sess_clone = Arc::clone(sess); + + tokio::spawn(async move { + // Run lookup in blocking thread because it does file IO + locking. + let entry_opt = tokio::task::spawn_blocking(move || { + crate::message_history::lookup(log_id, offset, &config) + }) + .await + .unwrap_or(None); + + let event = Event { + id: sub_id, + msg: EventMsg::GetHistoryEntryResponse( + crate::protocol::GetHistoryEntryResponseEvent { + offset, + log_id, + entry: entry_opt.map(|e| codex_protocol::message_history::HistoryEntry { + conversation_id: e.session_id, + ts: e.ts, + text: e.text, + }), + }, + ), + }; + + sess_clone.send_event_raw(event).await; + }); + } + + pub async fn list_mcp_tools(sess: &Session, config: &Arc, sub_id: String) { + let mcp_connection_manager = sess.services.mcp_connection_manager.read().await; + let (tools, auth_status_entries, resources, resource_templates) = tokio::join!( + mcp_connection_manager.list_all_tools(), + compute_auth_statuses( + config.mcp_servers.iter(), + config.mcp_oauth_credentials_store_mode, + ), + mcp_connection_manager.list_all_resources(), + mcp_connection_manager.list_all_resource_templates(), + ); + let auth_statuses = auth_status_entries + .iter() + .map(|(name, entry)| (name.clone(), entry.auth_status)) + .collect(); + let event = Event { + id: sub_id, + msg: EventMsg::McpListToolsResponse(crate::protocol::McpListToolsResponseEvent { + tools: tools + .into_iter() + .map(|(name, tool)| (name, tool.tool)) + .collect(), + resources, + resource_templates, + auth_statuses, + }), + }; + sess.send_event_raw(event).await; + } + + pub async fn list_custom_prompts(sess: &Session, sub_id: String) { + let custom_prompts: Vec = + if let Some(dir) = crate::custom_prompts::default_prompts_dir() { + crate::custom_prompts::discover_prompts_in(&dir).await + } else { + Vec::new() + }; + + let event = Event { + id: sub_id, + msg: EventMsg::ListCustomPromptsResponse(ListCustomPromptsResponseEvent { + custom_prompts, + }), + }; + sess.send_event_raw(event).await; + } + + pub async fn undo(sess: &Arc, sub_id: String) { + let turn_context = sess + .new_turn_with_sub_id(sub_id, SessionSettingsUpdate::default()) + .await; + sess.spawn_task(turn_context, Vec::new(), UndoTask::new()) + .await; + } + + pub async fn compact(sess: &Arc, sub_id: String) { + let turn_context = sess + .new_turn_with_sub_id(sub_id, SessionSettingsUpdate::default()) + .await; + // Attempt to inject input into current task + if let Err(items) = sess + .inject_input(vec![UserInput::Text { + text: turn_context.compact_prompt().to_string(), + }]) + .await + { + sess.spawn_task(Arc::clone(&turn_context), items, CompactTask) + .await; + } + } + + pub async fn shutdown(sess: &Arc, sub_id: String) -> bool { + sess.abort_all_tasks(TurnAbortReason::Interrupted).await; + info!("Shutting down Codex instance"); + + // Gracefully flush and shutdown rollout recorder on session end so tests + // that inspect the rollout file do not race with the background writer. + let recorder_opt = { + let mut guard = sess.services.rollout.lock().await; + guard.take() + }; + if let Some(rec) = recorder_opt + && let Err(e) = rec.shutdown().await + { + warn!("failed to shutdown rollout recorder: {e}"); + let event = Event { + id: sub_id.clone(), + msg: EventMsg::Error(ErrorEvent { + message: "Failed to shutdown rollout recorder".to_string(), + }), + }; + sess.send_event_raw(event).await; + } + + let event = Event { + id: sub_id, + msg: EventMsg::ShutdownComplete, + }; + sess.send_event_raw(event).await; + true + } + + pub async fn review( + sess: &Arc, + config: &Arc, + sub_id: String, + review_request: ReviewRequest, + ) { + let turn_context = sess + .new_turn_with_sub_id(sub_id.clone(), SessionSettingsUpdate::default()) + .await; + spawn_review_thread( + Arc::clone(sess), + Arc::clone(config), + turn_context.clone(), + sub_id, + review_request, + ) + .await; + } } /// Spawn a review thread using the given prompt. @@ -1562,14 +1697,14 @@ async fn spawn_review_thread( let model = config.review_model.clone(); let review_model_family = find_family_for_model(&model) .unwrap_or_else(|| parent_turn_context.client.get_model_family()); + // For reviews, disable web_search and view_image regardless of global settings. + let mut review_features = config.features.clone(); + review_features + .disable(crate::features::Feature::WebSearchRequest) + .disable(crate::features::Feature::ViewImageTool); let tools_config = ToolsConfig::new(&ToolsConfigParams { model_family: &review_model_family, - include_plan_tool: false, - include_apply_patch_tool: config.include_apply_patch_tool, - include_web_search_request: false, - use_streamable_shell_tool: false, - include_view_image_tool: false, - experimental_unified_exec_tool: config.use_experimental_unified_exec_tool, + features: &review_features, }); let base_instructions = REVIEW_PROMPT.to_string(); @@ -1588,45 +1723,53 @@ async fn spawn_review_thread( per_turn_config.model_context_window = Some(model_info.context_window); } + let otel_event_manager = parent_turn_context + .client + .get_otel_event_manager() + .with_model( + per_turn_config.model.as_str(), + per_turn_config.model_family.slug.as_str(), + ); + let per_turn_config = Arc::new(per_turn_config); let client = ModelClient::new( per_turn_config.clone(), auth_manager, + otel_event_manager, provider, per_turn_config.model_reasoning_effort, per_turn_config.model_reasoning_summary, sess.conversation_id, + parent_turn_context.client.get_session_source(), ); let review_turn_context = TurnContext { + sub_id: sub_id.to_string(), client, tools_config, + developer_instructions: None, user_instructions: None, base_instructions: Some(base_instructions.clone()), + compact_prompt: parent_turn_context.compact_prompt.clone(), approval_policy: parent_turn_context.approval_policy, sandbox_policy: parent_turn_context.sandbox_policy.clone(), shell_environment_policy: parent_turn_context.shell_environment_policy.clone(), cwd: parent_turn_context.cwd.clone(), - is_review_mode: true, + final_output_json_schema: None, + codex_linux_sandbox_exe: parent_turn_context.codex_linux_sandbox_exe.clone(), + tool_call_gate: Arc::new(ReadinessFlag::new()), }; // Seed the child task with the review prompt as the initial user message. - let input: Vec = vec![InputItem::Text { - text: format!("{base_instructions}\n\n---\n\nNow, here's your task: {review_prompt}"), + let input: Vec = vec![UserInput::Text { + text: review_prompt, }]; let tc = Arc::new(review_turn_context); - - // Clone sub_id for the upcoming announcement before moving it into the task. - let sub_id_for_event = sub_id.clone(); - let task = AgentTask::review(sess.clone(), tc.clone(), sub_id, input); - sess.set_task(task).await; + sess.spawn_task(tc.clone(), input, ReviewTask).await; // Announce entering review mode so UIs can switch modes. - sess.send_event(Event { - id: sub_id_for_event, - msg: EventMsg::EnteredReviewMode(review_request), - }) - .await; + sess.send_event(&tc, EventMsg::EnteredReviewMode(review_request)) + .await; } /// Takes a user message as input and runs a loop where, at each turn, the model @@ -1643,46 +1786,30 @@ async fn spawn_review_thread( /// - If the model sends only an assistant message, we record it in the /// conversation history and consider the task complete. /// -/// Review mode: when `turn_context.is_review_mode` is true, the turn runs in an -/// isolated in-memory thread without the parent session's prior history or -/// user_instructions. Emits ExitedReviewMode upon final review message. -async fn run_task( +pub(crate) async fn run_task( sess: Arc, turn_context: Arc, - sub_id: String, - input: Vec, -) { + input: Vec, + cancellation_token: CancellationToken, +) -> Option { if input.is_empty() { - return; + return None; } - let event = Event { - id: sub_id.clone(), - msg: EventMsg::TaskStarted(TaskStartedEvent { - model_context_window: turn_context.client.get_model_context_window(), - }), - }; - sess.send_event(event).await; + let event = EventMsg::TaskStarted(TaskStartedEvent { + model_context_window: turn_context.client.get_model_context_window(), + }); + sess.send_event(&turn_context, event).await; let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input); - // For review threads, keep an isolated in-memory history so the - // model sees a fresh conversation without the parent session's history. - // For normal turns, continue recording to the session history as before. - let is_review_mode = turn_context.is_review_mode; - let mut review_thread_history: Vec = Vec::new(); - if is_review_mode { - // Seed review threads with environment context so the model knows the working directory. - review_thread_history.extend(sess.build_initial_context(turn_context.as_ref())); - review_thread_history.push(initial_input_for_turn.into()); - } else { - sess.record_input_and_rollout_usermsg(&initial_input_for_turn) - .await; - } + sess.record_input_and_rollout_usermsg(turn_context.as_ref(), &initial_input_for_turn) + .await; + sess.maybe_start_ghost_snapshot(Arc::clone(&turn_context), cancellation_token.child_token()) + .await; let mut last_agent_message: Option = None; // Although from the perspective of codex.rs, TurnDiffTracker has the lifecycle of a Task which contains // many turns, from the perspective of the user, it is a single turn. - let mut turn_diff_tracker = TurnDiffTracker::new(); - let mut auto_compact_recently_attempted = false; + let turn_diff_tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); loop { // Note that pending_input would be something like a message the user @@ -1696,44 +1823,26 @@ async fn run_task( .collect::>(); // Construct the input that we will send to the model. - // - // - For review threads, use the isolated in-memory history so the - // model sees a fresh conversation (no parent history/user_instructions). - // - // - For normal turns, use the session's full history. When using the - // chat completions API (or ZDR clients), the model needs the full - // conversation history on each turn. The rollout file, however, should - // only record the new items that originated in this turn so that it - // represents an append-only log without duplicates. - let turn_input: Vec = if is_review_mode { - if !pending_input.is_empty() { - review_thread_history.extend(pending_input); - } - review_thread_history.clone() - } else { - sess.record_conversation_items(&pending_input).await; - sess.turn_input_with_history(pending_input).await + let turn_input: Vec = { + sess.record_conversation_items(&turn_context, &pending_input) + .await; + sess.clone_history().await.get_history_for_prompt() }; - let turn_input_messages: Vec = turn_input + let turn_input_messages = turn_input .iter() - .filter_map(|item| match item { - ResponseItem::Message { content, .. } => Some(content), + .filter_map(|item| match parse_turn_item(item) { + Some(TurnItem::UserMessage(user_message)) => Some(user_message), _ => None, }) - .flat_map(|content| { - content.iter().filter_map(|item| match item { - ContentItem::OutputText { text } => Some(text.clone()), - _ => None, - }) - }) - .collect(); + .map(|user_message| user_message.message()) + .collect::>(); match run_turn( - &sess, - turn_context.as_ref(), - &mut turn_diff_tracker, - sub_id.clone(), + Arc::clone(&sess), + Arc::clone(&turn_context), + Arc::clone(&turn_diff_tracker), turn_input, + cancellation_token.child_token(), ) .await { @@ -1748,242 +1857,132 @@ async fn run_task( .unwrap_or(i64::MAX); let total_usage_tokens = total_token_usage .as_ref() - .map(|usage| usage.tokens_in_context_window()); + .map(TokenUsage::tokens_in_context_window); let token_limit_reached = total_usage_tokens - .map(|tokens| (tokens as i64) >= limit) + .map(|tokens| tokens >= limit) .unwrap_or(false); - let mut items_to_record_in_conversation_history = Vec::::new(); - let mut responses = Vec::::new(); - for processed_response_item in processed_items { - let ProcessedResponseItem { item, response } = processed_response_item; - match (&item, &response) { - (ResponseItem::Message { role, .. }, None) if role == "assistant" => { - // If the model returned a message, we need to record it. - items_to_record_in_conversation_history.push(item); - } - ( - ResponseItem::LocalShellCall { .. }, - Some(ResponseInputItem::FunctionCallOutput { call_id, output }), - ) => { - items_to_record_in_conversation_history.push(item); - items_to_record_in_conversation_history.push( - ResponseItem::FunctionCallOutput { - call_id: call_id.clone(), - output: output.clone(), - }, - ); - } - ( - ResponseItem::FunctionCall { .. }, - Some(ResponseInputItem::FunctionCallOutput { call_id, output }), - ) => { - items_to_record_in_conversation_history.push(item); - items_to_record_in_conversation_history.push( - ResponseItem::FunctionCallOutput { - call_id: call_id.clone(), - output: output.clone(), - }, - ); - } - ( - ResponseItem::CustomToolCall { .. }, - Some(ResponseInputItem::CustomToolCallOutput { call_id, output }), - ) => { - items_to_record_in_conversation_history.push(item); - items_to_record_in_conversation_history.push( - ResponseItem::CustomToolCallOutput { - call_id: call_id.clone(), - output: output.clone(), - }, - ); - } - ( - ResponseItem::FunctionCall { .. }, - Some(ResponseInputItem::McpToolCallOutput { call_id, result }), - ) => { - items_to_record_in_conversation_history.push(item); - let output = match result { - Ok(call_tool_result) => { - convert_call_tool_result_to_function_call_output_payload( - call_tool_result, - ) - } - Err(err) => FunctionCallOutputPayload { - content: err.clone(), - success: Some(false), - }, - }; - items_to_record_in_conversation_history.push( - ResponseItem::FunctionCallOutput { - call_id: call_id.clone(), - output, - }, - ); - } - ( - ResponseItem::Reasoning { - id, - summary, - content, - encrypted_content, - }, - None, - ) => { - items_to_record_in_conversation_history.push(ResponseItem::Reasoning { - id: id.clone(), - summary: summary.clone(), - content: content.clone(), - encrypted_content: encrypted_content.clone(), - }); - } - _ => { - warn!("Unexpected response item: {item:?} with response: {response:?}"); - } - }; - if let Some(response) = response { - responses.push(response); - } - } - - // Only attempt to take the lock if there is something to record. - if !items_to_record_in_conversation_history.is_empty() { - if is_review_mode { - review_thread_history - .extend(items_to_record_in_conversation_history.clone()); - } else { - sess.record_conversation_items(&items_to_record_in_conversation_history) - .await; - } - } + let (responses, items_to_record_in_conversation_history) = + process_items(processed_items, &sess, &turn_context).await; + // as long as compaction works well in getting us way below the token limit, we shouldn't worry about being in an infinite loop. if token_limit_reached { - if auto_compact_recently_attempted { - let limit_str = limit.to_string(); - let current_tokens = total_usage_tokens - .map(|tokens| tokens.to_string()) - .unwrap_or_else(|| "unknown".to_string()); - let event = Event { - id: sub_id.clone(), - msg: EventMsg::Error(ErrorEvent { - message: format!( - "Conversation is still above the token limit after automatic summarization (limit {limit_str}, current {current_tokens}). Please start a new session or trim your input." - ), - }), - }; - sess.send_event(event).await; - break; - } - auto_compact_recently_attempted = true; compact::run_inline_auto_compact_task(sess.clone(), turn_context.clone()).await; continue; } - auto_compact_recently_attempted = false; - if responses.is_empty() { last_agent_message = get_last_assistant_message_from_turn( &items_to_record_in_conversation_history, ); - sess.maybe_notify(UserNotification::AgentTurnComplete { - turn_id: sub_id.clone(), - input_messages: turn_input_messages, - last_assistant_message: last_agent_message.clone(), - }); + sess.notifier() + .notify(&UserNotification::AgentTurnComplete { + thread_id: sess.conversation_id.to_string(), + turn_id: turn_context.sub_id.clone(), + cwd: turn_context.cwd.display().to_string(), + input_messages: turn_input_messages, + last_assistant_message: last_agent_message.clone(), + }); break; } continue; } + Err(CodexErr::TurnAborted { + dangling_artifacts: processed_items, + }) => { + let _ = process_items(processed_items, &sess, &turn_context).await; + // Aborted turn is reported via a different event. + break; + } Err(e) => { info!("Turn error: {e:#}"); - let event = Event { - id: sub_id.clone(), - msg: EventMsg::Error(ErrorEvent { - message: e.to_string(), - }), - }; - sess.send_event(event).await; + let event = EventMsg::Error(ErrorEvent { + message: e.to_string(), + }); + sess.send_event(&turn_context, event).await; // let the user continue the conversation break; } } } - // If this was a review thread and we have a final assistant message, - // try to parse it as a ReviewOutput. - // - // If parsing fails, construct a minimal ReviewOutputEvent using the plain - // text as the overall explanation. Else, just exit review mode with None. - // - // Emits an ExitedReviewMode event with the parsed review output. - if turn_context.is_review_mode { - exit_review_mode( - sess.clone(), - sub_id.clone(), - last_agent_message.as_deref().map(parse_review_output_event), - ) - .await; - } - - sess.remove_task(&sub_id).await; - let event = Event { - id: sub_id, - msg: EventMsg::TaskComplete(TaskCompleteEvent { last_agent_message }), - }; - sess.send_event(event).await; -} - -/// Parse the review output; when not valid JSON, build a structured -/// fallback that carries the plain text as the overall explanation. -/// -/// Returns: a ReviewOutputEvent parsed from JSON or a fallback populated from text. -fn parse_review_output_event(text: &str) -> ReviewOutputEvent { - // Try direct parse first - if let Ok(ev) = serde_json::from_str::(text) { - return ev; - } - // If wrapped in markdown fences or extra prose, attempt to extract the first JSON object - if let (Some(start), Some(end)) = (text.find('{'), text.rfind('}')) - && start < end - && let Some(slice) = text.get(start..=end) - && let Ok(ev) = serde_json::from_str::(slice) - { - return ev; - } - // Not JSON – return a structured ReviewOutputEvent that carries - // the plain text as the overall explanation. - ReviewOutputEvent { - overall_explanation: text.to_string(), - ..Default::default() - } + last_agent_message } async fn run_turn( - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: String, + sess: Arc, + turn_context: Arc, + turn_diff_tracker: SharedTurnDiffTracker, input: Vec, + cancellation_token: CancellationToken, ) -> CodexResult { - let tools = get_openai_tools( + let mcp_tools = sess + .services + .mcp_connection_manager + .read() + .await + .list_all_tools() + .or_cancel(&cancellation_token) + .await?; + let router = Arc::new(ToolRouter::from_config( &turn_context.tools_config, - Some(sess.mcp_connection_manager.list_all_tools()), - ); + Some( + mcp_tools + .into_iter() + .map(|(name, tool)| (name, tool.tool)) + .collect(), + ), + )); + let model_supports_parallel = turn_context + .client + .get_model_family() + .supports_parallel_tool_calls; + let parallel_tool_calls = model_supports_parallel; let prompt = Prompt { input, - tools, + tools: router.specs(), + parallel_tool_calls, base_instructions_override: turn_context.base_instructions.clone(), + output_schema: turn_context.final_output_json_schema.clone(), }; let mut retries = 0; loop { - match try_run_turn(sess, turn_context, turn_diff_tracker, &sub_id, &prompt).await { + match try_run_turn( + Arc::clone(&router), + Arc::clone(&sess), + Arc::clone(&turn_context), + Arc::clone(&turn_diff_tracker), + &prompt, + cancellation_token.child_token(), + ) + .await + { Ok(output) => return Ok(output), + Err(CodexErr::TurnAborted { + dangling_artifacts: processed_items, + }) => { + return Err(CodexErr::TurnAborted { + dangling_artifacts: processed_items, + }); + } Err(CodexErr::Interrupted) => return Err(CodexErr::Interrupted), Err(CodexErr::EnvVar(var)) => return Err(CodexErr::EnvVar(var)), - Err(e @ (CodexErr::UsageLimitReached(_) | CodexErr::UsageNotIncluded)) => { + Err(e @ CodexErr::Fatal(_)) => return Err(e), + Err(e @ CodexErr::ContextWindowExceeded) => { + sess.set_total_tokens_full(&turn_context).await; return Err(e); } + Err(CodexErr::UsageLimitReached(e)) => { + let rate_limits = e.rate_limits.clone(); + if let Some(rate_limits) = rate_limits { + sess.update_rate_limits(&turn_context, rate_limits).await; + } + return Err(CodexErr::UsageLimitReached(e)); + } + Err(CodexErr::UsageNotIncluded) => return Err(CodexErr::UsageNotIncluded), + Err(e @ CodexErr::QuotaExceeded) => return Err(e), + Err(e @ CodexErr::RefreshTokenFailed(_)) => return Err(e), Err(e) => { // Use the configured provider-specific stream retry budget. let max_retries = turn_context.client.get_provider().stream_max_retries(); @@ -2001,10 +2000,8 @@ async fn run_turn( // user understands what is happening instead of staring // at a seemingly frozen screen. sess.notify_stream_error( - &sub_id, - format!( - "stream error: {e}; retrying {retries}/{max_retries} in {delay:?}…" - ), + &turn_context, + format!("Reconnecting... {retries}/{max_retries}"), ) .await; @@ -2022,9 +2019,9 @@ async fn run_turn( /// "handled" such that it produces a `ResponseInputItem` that needs to be /// sent back to the model on the next turn. #[derive(Debug)] -struct ProcessedResponseItem { - item: ResponseItem, - response: Option, +pub struct ProcessedResponseItem { + pub item: ResponseItem, + pub response: Option, } #[derive(Debug)] @@ -2033,68 +2030,15 @@ struct TurnRunResult { total_token_usage: Option, } +#[allow(clippy::too_many_arguments)] async fn try_run_turn( - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: &str, + router: Arc, + sess: Arc, + turn_context: Arc, + turn_diff_tracker: SharedTurnDiffTracker, prompt: &Prompt, + cancellation_token: CancellationToken, ) -> CodexResult { - // call_ids that are part of this response. - let completed_call_ids = prompt - .input - .iter() - .filter_map(|ri| match ri { - ResponseItem::FunctionCallOutput { call_id, .. } => Some(call_id), - ResponseItem::LocalShellCall { - call_id: Some(call_id), - .. - } => Some(call_id), - ResponseItem::CustomToolCallOutput { call_id, .. } => Some(call_id), - _ => None, - }) - .collect::>(); - - // call_ids that were pending but are not part of this response. - // This usually happens because the user interrupted the model before we responded to one of its tool calls - // and then the user sent a follow-up message. - let missing_calls = { - prompt - .input - .iter() - .filter_map(|ri| match ri { - ResponseItem::FunctionCall { call_id, .. } => Some(call_id), - ResponseItem::LocalShellCall { - call_id: Some(call_id), - .. - } => Some(call_id), - ResponseItem::CustomToolCall { call_id, .. } => Some(call_id), - _ => None, - }) - .filter_map(|call_id| { - if completed_call_ids.contains(&call_id) { - None - } else { - Some(call_id.clone()) - } - }) - .map(|call_id| ResponseItem::CustomToolCallOutput { - call_id, - output: "aborted".to_string(), - }) - .collect::>() - }; - let prompt: Cow = if missing_calls.is_empty() { - Cow::Borrowed(prompt) - } else { - // Add the synthetic aborted missing calls to the beginning of the input to ensure all call ids have responses. - let input = [missing_calls, prompt.input.clone()].concat(); - Cow::Owned(Prompt { - input, - ..prompt.clone() - }) - }; - let rollout_item = RolloutItem::TurnContext(TurnContextItem { cwd: turn_context.cwd.clone(), approval_policy: turn_context.approval_policy, @@ -2103,87 +2047,161 @@ async fn try_run_turn( effort: turn_context.client.get_reasoning_effort(), summary: turn_context.client.get_reasoning_summary(), }); - sess.persist_rollout_items(&[rollout_item]).await; - let mut stream = turn_context.client.clone().stream(&prompt).await?; - let mut output = Vec::new(); + sess.persist_rollout_items(&[rollout_item]).await; + let mut stream = turn_context + .client + .clone() + .stream(prompt) + .or_cancel(&cancellation_token) + .await??; + + let tool_runtime = ToolCallRuntime::new( + Arc::clone(&router), + Arc::clone(&sess), + Arc::clone(&turn_context), + Arc::clone(&turn_diff_tracker), + ); + let mut output: FuturesOrdered>> = + FuturesOrdered::new(); + + let mut active_item: Option = None; loop { // Poll the next item from the model stream. We must inspect *both* Ok and Err // cases so that transient stream failures (e.g., dropped SSE connection before // `response.completed`) bubble up and trigger the caller's retry logic. - let event = stream.next().await; - let Some(event) = event else { - // Channel closed without yielding a final Completed event or explicit error. - // Treat as a disconnected stream so the caller can retry. - return Err(CodexErr::Stream( - "stream closed before response.completed".into(), - None, - )); + let event = match stream.next().or_cancel(&cancellation_token).await { + Ok(event) => event, + Err(codex_async_utils::CancelErr::Cancelled) => { + let processed_items = output.try_collect().await?; + return Err(CodexErr::TurnAborted { + dangling_artifacts: processed_items, + }); + } }; let event = match event { - Ok(ev) => ev, - Err(e) => { - // Propagate the underlying stream error to the caller (run_turn), which - // will apply the configured `stream_max_retries` policy. - return Err(e); + Some(res) => res?, + None => { + return Err(CodexErr::Stream( + "stream closed before response.completed".into(), + None, + )); } }; + let add_completed = &mut |response_item: ProcessedResponseItem| { + output.push_back(future::ready(Ok(response_item)).boxed()); + }; + match event { ResponseEvent::Created => {} ResponseEvent::OutputItemDone(item) => { - let response = handle_response_item( - sess, - turn_context, - turn_diff_tracker, - sub_id, - item.clone(), - ) - .await?; - output.push(ProcessedResponseItem { item, response }); + let previously_active_item = active_item.take(); + match ToolRouter::build_tool_call(sess.as_ref(), item.clone()).await { + Ok(Some(call)) => { + let payload_preview = call.payload.log_payload().into_owned(); + tracing::info!("ToolCall: {} {}", call.tool_name, payload_preview); + + let response = + tool_runtime.handle_tool_call(call, cancellation_token.child_token()); + + output.push_back( + async move { + Ok(ProcessedResponseItem { + item, + response: Some(response.await?), + }) + } + .boxed(), + ); + } + Ok(None) => { + if let Some(turn_item) = handle_non_tool_response_item(&item).await { + if previously_active_item.is_none() { + sess.emit_turn_item_started(&turn_context, &turn_item).await; + } + + sess.emit_turn_item_completed(&turn_context, turn_item) + .await; + } + + add_completed(ProcessedResponseItem { + item, + response: None, + }); + } + Err(FunctionCallError::MissingLocalShellCallId) => { + let msg = "LocalShellCall without call_id or id"; + turn_context + .client + .get_otel_event_manager() + .log_tool_failed("local_shell", msg); + error!(msg); + + let response = ResponseInputItem::FunctionCallOutput { + call_id: String::new(), + output: FunctionCallOutputPayload { + content: msg.to_string(), + ..Default::default() + }, + }; + add_completed(ProcessedResponseItem { + item, + response: Some(response), + }); + } + Err(FunctionCallError::RespondToModel(message)) + | Err(FunctionCallError::Denied(message)) => { + let response = ResponseInputItem::FunctionCallOutput { + call_id: String::new(), + output: FunctionCallOutputPayload { + content: message, + ..Default::default() + }, + }; + add_completed(ProcessedResponseItem { + item, + response: Some(response), + }); + } + Err(FunctionCallError::Fatal(message)) => { + return Err(CodexErr::Fatal(message)); + } + } } - ResponseEvent::WebSearchCallBegin { call_id } => { - let _ = sess - .tx_event - .send(Event { - id: sub_id.to_string(), - msg: EventMsg::WebSearchBegin(WebSearchBeginEvent { call_id }), - }) - .await; + ResponseEvent::OutputItemAdded(item) => { + if let Some(turn_item) = handle_non_tool_response_item(&item).await { + let tracked_item = turn_item.clone(); + sess.emit_turn_item_started(&turn_context, &turn_item).await; + + active_item = Some(tracked_item); + } } ResponseEvent::RateLimits(snapshot) => { // Update internal state with latest rate limits, but defer sending until // token usage is available to avoid duplicate TokenCount events. - sess.update_rate_limits(snapshot).await; + sess.update_rate_limits(&turn_context, snapshot).await; } ResponseEvent::Completed { response_id: _, token_usage, } => { - sess.update_token_usage_info(turn_context, token_usage.as_ref()) + sess.update_token_usage_info(&turn_context, token_usage.as_ref()) .await; - let token_event = sess.get_token_count_event().await; - let _ = sess - .send_event(Event { - id: sub_id.to_string(), - msg: EventMsg::TokenCount(token_event), - }) - .await; - - let unified_diff = turn_diff_tracker.get_unified_diff(); + let processed_items = output.try_collect().await?; + let unified_diff = { + let mut tracker = turn_diff_tracker.lock().await; + tracker.get_unified_diff() + }; if let Ok(Some(unified_diff)) = unified_diff { let msg = EventMsg::TurnDiff(TurnDiffEvent { unified_diff }); - let event = Event { - id: sub_id.to_string(), - msg, - }; - sess.send_event(event).await; + sess.send_event(&turn_context, msg).await; } let result = TurnRunResult { - processed_items: output, + processed_items, total_token_usage: token_usage.clone(), }; @@ -2192,1064 +2210,86 @@ async fn try_run_turn( ResponseEvent::OutputTextDelta(delta) => { // In review child threads, suppress assistant text deltas; the // UI will show a selection popup from the final ReviewOutput. - if !turn_context.is_review_mode { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }), + if let Some(active) = active_item.as_ref() { + let event = AgentMessageContentDeltaEvent { + thread_id: sess.conversation_id.to_string(), + turn_id: turn_context.sub_id.clone(), + item_id: active.id(), + delta: delta.clone(), }; - sess.send_event(event).await; + sess.send_event(&turn_context, EventMsg::AgentMessageContentDelta(event)) + .await; } else { - trace!("suppressing OutputTextDelta in review mode"); + error_or_panic("ReasoningSummaryDelta without active item".to_string()); } } - ResponseEvent::ReasoningSummaryDelta(delta) => { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::AgentReasoningDelta(AgentReasoningDeltaEvent { delta }), - }; - sess.send_event(event).await; - } - ResponseEvent::ReasoningSummaryPartAdded => { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::AgentReasoningSectionBreak(AgentReasoningSectionBreakEvent {}), - }; - sess.send_event(event).await; - } - ResponseEvent::ReasoningContentDelta(delta) => { - if sess.show_raw_agent_reasoning { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::AgentReasoningRawContentDelta( - AgentReasoningRawContentDeltaEvent { delta }, - ), + ResponseEvent::ReasoningSummaryDelta { + delta, + summary_index, + } => { + if let Some(active) = active_item.as_ref() { + let event = ReasoningContentDeltaEvent { + thread_id: sess.conversation_id.to_string(), + turn_id: turn_context.sub_id.clone(), + item_id: active.id(), + delta, + summary_index, }; - sess.send_event(event).await; + sess.send_event(&turn_context, EventMsg::ReasoningContentDelta(event)) + .await; + } else { + error_or_panic("ReasoningSummaryDelta without active item".to_string()); + } + } + ResponseEvent::ReasoningSummaryPartAdded { summary_index } => { + if let Some(active) = active_item.as_ref() { + let event = + EventMsg::AgentReasoningSectionBreak(AgentReasoningSectionBreakEvent { + item_id: active.id(), + summary_index, + }); + sess.send_event(&turn_context, event).await; + } else { + error_or_panic("ReasoningSummaryPartAdded without active item".to_string()); + } + } + ResponseEvent::ReasoningContentDelta { + delta, + content_index, + } => { + if let Some(active) = active_item.as_ref() { + let event = ReasoningRawContentDeltaEvent { + thread_id: sess.conversation_id.to_string(), + turn_id: turn_context.sub_id.clone(), + item_id: active.id(), + delta, + content_index, + }; + sess.send_event(&turn_context, EventMsg::ReasoningRawContentDelta(event)) + .await; + } else { + error_or_panic("ReasoningRawContentDelta without active item".to_string()); } } } } } -async fn handle_response_item( - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: &str, - item: ResponseItem, -) -> CodexResult> { +async fn handle_non_tool_response_item(item: &ResponseItem) -> Option { debug!(?item, "Output item"); - let output = match item { - ResponseItem::FunctionCall { - name, - arguments, - call_id, - .. - } => { - info!("FunctionCall: {name}({arguments})"); - Some( - handle_function_call( - sess, - turn_context, - turn_diff_tracker, - sub_id.to_string(), - name, - arguments, - call_id, - ) - .await, - ) - } - ResponseItem::LocalShellCall { - id, - call_id, - status: _, - action, - } => { - let LocalShellAction::Exec(action) = action; - tracing::info!("LocalShellCall: {action:?}"); - let params = ShellToolCallParams { - command: action.command, - workdir: action.working_directory, - timeout_ms: action.timeout_ms, - with_escalated_permissions: None, - justification: None, - }; - let effective_call_id = match (call_id, id) { - (Some(call_id), _) => call_id, - (None, Some(id)) => id, - (None, None) => { - error!("LocalShellCall without call_id or id"); - return Ok(Some(ResponseInputItem::FunctionCallOutput { - call_id: "".to_string(), - output: FunctionCallOutputPayload { - content: "LocalShellCall without call_id or id".to_string(), - success: None, - }, - })); - } - }; - let exec_params = to_exec_params(params, turn_context); - Some( - handle_container_exec_with_params( - exec_params, - sess, - turn_context, - turn_diff_tracker, - sub_id.to_string(), - effective_call_id, - ) - .await, - ) - } - ResponseItem::CustomToolCall { - id: _, - call_id, - name, - input, - status: _, - } => Some( - handle_custom_tool_call( - sess, - turn_context, - turn_diff_tracker, - sub_id.to_string(), - name, - input, - call_id, - ) - .await, - ), - ResponseItem::FunctionCallOutput { .. } => { - debug!("unexpected FunctionCallOutput from stream"); - None - } - ResponseItem::CustomToolCallOutput { .. } => { - debug!("unexpected CustomToolCallOutput from stream"); - None - } + match item { ResponseItem::Message { .. } | ResponseItem::Reasoning { .. } - | ResponseItem::WebSearchCall { .. } => { - // In review child threads, suppress assistant message events but - // keep reasoning/web search. - let msgs = match &item { - ResponseItem::Message { .. } if turn_context.is_review_mode => { - trace!("suppressing assistant Message in review mode"); - Vec::new() - } - _ => map_response_item_to_event_messages(&item, sess.show_raw_agent_reasoning), - }; - for msg in msgs { - let event = Event { - id: sub_id.to_string(), - msg, - }; - sess.send_event(event).await; - } + | ResponseItem::WebSearchCall { .. } => parse_turn_item(item), + ResponseItem::FunctionCallOutput { .. } | ResponseItem::CustomToolCallOutput { .. } => { + debug!("unexpected tool output from stream"); None } - ResponseItem::Other => None, - }; - Ok(output) -} - -async fn handle_unified_exec_tool_call( - sess: &Session, - call_id: String, - session_id: Option, - arguments: Vec, - timeout_ms: Option, -) -> ResponseInputItem { - let parsed_session_id = if let Some(session_id) = session_id { - match session_id.parse::() { - Ok(parsed) => Some(parsed), - Err(output) => { - return ResponseInputItem::FunctionCallOutput { - call_id: call_id.to_string(), - output: FunctionCallOutputPayload { - content: format!("invalid session_id: {session_id} due to error {output}"), - success: Some(false), - }, - }; - } - } - } else { - None - }; - - let request = crate::unified_exec::UnifiedExecRequest { - session_id: parsed_session_id, - input_chunks: &arguments, - timeout_ms, - }; - - let result = sess.unified_exec_manager.handle_request(request).await; - - let output_payload = match result { - Ok(value) => { - #[derive(Serialize)] - struct SerializedUnifiedExecResult<'a> { - session_id: Option, - output: &'a str, - } - - match serde_json::to_string(&SerializedUnifiedExecResult { - session_id: value.session_id.map(|id| id.to_string()), - output: &value.output, - }) { - Ok(serialized) => FunctionCallOutputPayload { - content: serialized, - success: Some(true), - }, - Err(err) => FunctionCallOutputPayload { - content: format!("failed to serialize unified exec output: {err}"), - success: Some(false), - }, - } - } - Err(err) => FunctionCallOutputPayload { - content: format!("unified exec failed: {err}"), - success: Some(false), - }, - }; - - ResponseInputItem::FunctionCallOutput { - call_id, - output: output_payload, + _ => None, } } -async fn handle_function_call( - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: String, - name: String, - arguments: String, - call_id: String, -) -> ResponseInputItem { - match name.as_str() { - "container.exec" | "shell" => { - let params = match parse_container_exec_arguments(arguments, turn_context, &call_id) { - Ok(params) => params, - Err(output) => { - return *output; - } - }; - handle_container_exec_with_params( - params, - sess, - turn_context, - turn_diff_tracker, - sub_id, - call_id, - ) - .await - } - "unified_exec" => { - #[derive(Deserialize)] - struct UnifiedExecArgs { - input: Vec, - #[serde(default)] - session_id: Option, - #[serde(default)] - timeout_ms: Option, - } - - let args = match serde_json::from_str::(&arguments) { - Ok(args) => args, - Err(err) => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {err}"), - success: Some(false), - }, - }; - } - }; - - handle_unified_exec_tool_call( - sess, - call_id, - args.session_id, - args.input, - args.timeout_ms, - ) - .await - } - "view_image" => { - #[derive(serde::Deserialize)] - struct SeeImageArgs { - path: String, - } - let args = match serde_json::from_str::(&arguments) { - Ok(a) => a, - Err(e) => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {e}"), - success: Some(false), - }, - }; - } - }; - let abs = turn_context.resolve_path(Some(args.path)); - let output = match sess - .inject_input(vec![InputItem::LocalImage { path: abs }]) - .await - { - Ok(()) => FunctionCallOutputPayload { - content: "attached local image path".to_string(), - success: Some(true), - }, - Err(_) => FunctionCallOutputPayload { - content: "unable to attach image (no active task)".to_string(), - success: Some(false), - }, - }; - ResponseInputItem::FunctionCallOutput { call_id, output } - } - "apply_patch" => { - let args = match serde_json::from_str::(&arguments) { - Ok(a) => a, - Err(e) => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {e}"), - success: None, - }, - }; - } - }; - let exec_params = ExecParams { - command: vec!["apply_patch".to_string(), args.input.clone()], - cwd: turn_context.cwd.clone(), - timeout_ms: None, - env: HashMap::new(), - with_escalated_permissions: None, - justification: None, - }; - handle_container_exec_with_params( - exec_params, - sess, - turn_context, - turn_diff_tracker, - sub_id, - call_id, - ) - .await - } - "update_plan" => handle_update_plan(sess, arguments, sub_id, call_id).await, - EXEC_COMMAND_TOOL_NAME => { - // TODO(mbolin): Sandbox check. - let exec_params = match serde_json::from_str::(&arguments) { - Ok(params) => params, - Err(e) => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {e}"), - success: Some(false), - }, - }; - } - }; - let result = sess - .session_manager - .handle_exec_command_request(exec_params) - .await; - let function_call_output = crate::exec_command::result_into_payload(result); - ResponseInputItem::FunctionCallOutput { - call_id, - output: function_call_output, - } - } - WRITE_STDIN_TOOL_NAME => { - let write_stdin_params = match serde_json::from_str::(&arguments) { - Ok(params) => params, - Err(e) => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {e}"), - success: Some(false), - }, - }; - } - }; - let result = sess - .session_manager - .handle_write_stdin_request(write_stdin_params) - .await; - let function_call_output: FunctionCallOutputPayload = - crate::exec_command::result_into_payload(result); - ResponseInputItem::FunctionCallOutput { - call_id, - output: function_call_output, - } - } - _ => { - match sess.mcp_connection_manager.parse_tool_name(&name) { - Some((server, tool_name)) => { - // TODO(mbolin): Determine appropriate timeout for tool call. - let timeout = None; - handle_mcp_tool_call( - sess, &sub_id, call_id, server, tool_name, arguments, timeout, - ) - .await - } - None => { - // Unknown function: reply with structured failure so the model can adapt. - ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("unsupported call: {name}"), - success: None, - }, - } - } - } - } - } -} - -async fn handle_custom_tool_call( - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: String, - name: String, - input: String, - call_id: String, -) -> ResponseInputItem { - info!("CustomToolCall: {name} {input}"); - match name.as_str() { - "apply_patch" => { - let exec_params = ExecParams { - command: vec!["apply_patch".to_string(), input.clone()], - cwd: turn_context.cwd.clone(), - timeout_ms: None, - env: HashMap::new(), - with_escalated_permissions: None, - justification: None, - }; - let resp = handle_container_exec_with_params( - exec_params, - sess, - turn_context, - turn_diff_tracker, - sub_id, - call_id, - ) - .await; - - // Convert function-call style output into a custom tool call output - match resp { - ResponseInputItem::FunctionCallOutput { call_id, output } => { - ResponseInputItem::CustomToolCallOutput { - call_id, - output: output.content, - } - } - // Pass through if already a custom tool output or other variant - other => other, - } - } - _ => { - debug!("unexpected CustomToolCall from stream"); - ResponseInputItem::CustomToolCallOutput { - call_id, - output: format!("unsupported custom tool call: {name}"), - } - } - } -} - -fn to_exec_params(params: ShellToolCallParams, turn_context: &TurnContext) -> ExecParams { - ExecParams { - command: params.command, - cwd: turn_context.resolve_path(params.workdir.clone()), - timeout_ms: params.timeout_ms, - env: create_env(&turn_context.shell_environment_policy), - with_escalated_permissions: params.with_escalated_permissions, - justification: params.justification, - } -} - -fn parse_container_exec_arguments( - arguments: String, - turn_context: &TurnContext, - call_id: &str, -) -> Result> { - // parse command - match serde_json::from_str::(&arguments) { - Ok(shell_tool_call_params) => Ok(to_exec_params(shell_tool_call_params, turn_context)), - Err(e) => { - // allow model to re-sample - let output = ResponseInputItem::FunctionCallOutput { - call_id: call_id.to_string(), - output: FunctionCallOutputPayload { - content: format!("failed to parse function arguments: {e}"), - success: None, - }, - }; - Err(Box::new(output)) - } - } -} - -pub struct ExecInvokeArgs<'a> { - pub params: ExecParams, - pub sandbox_type: SandboxType, - pub sandbox_policy: &'a SandboxPolicy, - pub sandbox_cwd: &'a Path, - pub codex_linux_sandbox_exe: &'a Option, - pub stdout_stream: Option, -} - -fn maybe_translate_shell_command( - params: ExecParams, - sess: &Session, - turn_context: &TurnContext, -) -> ExecParams { - let should_translate = matches!(sess.user_shell, crate::shell::Shell::PowerShell(_)) - || turn_context.shell_environment_policy.use_profile; - - if should_translate - && let Some(command) = sess - .user_shell - .format_default_shell_invocation(params.command.clone()) - { - return ExecParams { command, ..params }; - } - params -} - -async fn handle_container_exec_with_params( - params: ExecParams, - sess: &Session, - turn_context: &TurnContext, - turn_diff_tracker: &mut TurnDiffTracker, - sub_id: String, - call_id: String, -) -> ResponseInputItem { - if params.with_escalated_permissions.unwrap_or(false) - && !matches!(turn_context.approval_policy, AskForApproval::OnRequest) - { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!( - "approval policy is {policy:?}; reject command — you should not ask for escalated permissions if the approval policy is {policy:?}", - policy = turn_context.approval_policy - ), - success: None, - }, - }; - } - - // check if this was a patch, and apply it if so - let apply_patch_exec = match maybe_parse_apply_patch_verified(¶ms.command, ¶ms.cwd) { - MaybeApplyPatchVerified::Body(changes) => { - match apply_patch::apply_patch(sess, turn_context, &sub_id, &call_id, changes).await { - InternalApplyPatchInvocation::Output(item) => return item, - InternalApplyPatchInvocation::DelegateToExec(apply_patch_exec) => { - Some(apply_patch_exec) - } - } - } - MaybeApplyPatchVerified::CorrectnessError(parse_error) => { - // It looks like an invocation of `apply_patch`, but we - // could not resolve it into a patch that would apply - // cleanly. Return to model for resample. - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("error: {parse_error:#}"), - success: None, - }, - }; - } - MaybeApplyPatchVerified::ShellParseError(error) => { - trace!("Failed to parse shell command, {error:?}"); - None - } - MaybeApplyPatchVerified::NotApplyPatch => None, - }; - - let (params, safety, command_for_display) = match &apply_patch_exec { - Some(ApplyPatchExec { - action: ApplyPatchAction { patch, cwd, .. }, - user_explicitly_approved_this_action, - }) => { - let path_to_codex = std::env::current_exe() - .ok() - .map(|p| p.to_string_lossy().to_string()); - let Some(path_to_codex) = path_to_codex else { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: "failed to determine path to codex executable".to_string(), - success: None, - }, - }; - }; - - let params = ExecParams { - command: vec![ - path_to_codex, - CODEX_APPLY_PATCH_ARG1.to_string(), - patch.clone(), - ], - cwd: cwd.clone(), - timeout_ms: params.timeout_ms, - env: HashMap::new(), - with_escalated_permissions: params.with_escalated_permissions, - justification: params.justification.clone(), - }; - let safety = if *user_explicitly_approved_this_action { - SafetyCheck::AutoApprove { - sandbox_type: SandboxType::None, - } - } else { - assess_safety_for_untrusted_command( - turn_context.approval_policy, - &turn_context.sandbox_policy, - params.with_escalated_permissions.unwrap_or(false), - ) - }; - ( - params, - safety, - vec!["apply_patch".to_string(), patch.clone()], - ) - } - None => { - let safety = { - let state = sess.state.lock().await; - assess_command_safety( - ¶ms.command, - turn_context.approval_policy, - &turn_context.sandbox_policy, - &state.approved_commands, - params.with_escalated_permissions.unwrap_or(false), - ) - }; - let command_for_display = params.command.clone(); - (params, safety, command_for_display) - } - }; - - let sandbox_type = match safety { - SafetyCheck::AutoApprove { sandbox_type } => sandbox_type, - SafetyCheck::AskUser => { - let rx_approve = sess - .request_command_approval( - sub_id.clone(), - call_id.clone(), - params.command.clone(), - params.cwd.clone(), - params.justification.clone(), - ) - .await; - match rx_approve.await.unwrap_or_default() { - ReviewDecision::Approved => (), - ReviewDecision::ApprovedForSession => { - sess.add_approved_command(params.command.clone()).await; - } - ReviewDecision::Denied | ReviewDecision::Abort => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: "exec command rejected by user".to_string(), - success: None, - }, - }; - } - } - // No sandboxing is applied because the user has given - // explicit approval. Often, we end up in this case because - // the command cannot be run in a sandbox, such as - // installing a new dependency that requires network access. - SandboxType::None - } - SafetyCheck::Reject { reason } => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!("exec command rejected: {reason}"), - success: None, - }, - }; - } - }; - - let exec_command_context = ExecCommandContext { - sub_id: sub_id.clone(), - call_id: call_id.clone(), - command_for_display: command_for_display.clone(), - cwd: params.cwd.clone(), - apply_patch: apply_patch_exec.map( - |ApplyPatchExec { - action, - user_explicitly_approved_this_action, - }| ApplyPatchCommandContext { - user_explicitly_approved_this_action, - changes: convert_apply_patch_to_protocol(&action), - }, - ), - }; - - let params = maybe_translate_shell_command(params, sess, turn_context); - let output_result = sess - .run_exec_with_events( - turn_diff_tracker, - exec_command_context.clone(), - ExecInvokeArgs { - params: params.clone(), - sandbox_type, - sandbox_policy: &turn_context.sandbox_policy, - sandbox_cwd: &turn_context.cwd, - codex_linux_sandbox_exe: &sess.codex_linux_sandbox_exe, - stdout_stream: if exec_command_context.apply_patch.is_some() { - None - } else { - Some(StdoutStream { - sub_id: sub_id.clone(), - call_id: call_id.clone(), - tx_event: sess.tx_event.clone(), - }) - }, - }, - ) - .await; - - match output_result { - Ok(output) => { - let ExecToolCallOutput { exit_code, .. } = &output; - - let is_success = *exit_code == 0; - let content = format_exec_output(&output); - ResponseInputItem::FunctionCallOutput { - call_id: call_id.clone(), - output: FunctionCallOutputPayload { - content, - success: Some(is_success), - }, - } - } - Err(CodexErr::Sandbox(error)) => { - handle_sandbox_error( - turn_diff_tracker, - params, - exec_command_context, - error, - sandbox_type, - sess, - turn_context, - ) - .await - } - Err(e) => ResponseInputItem::FunctionCallOutput { - call_id: call_id.clone(), - output: FunctionCallOutputPayload { - content: format!("execution error: {e}"), - success: None, - }, - }, - } -} - -async fn handle_sandbox_error( - turn_diff_tracker: &mut TurnDiffTracker, - params: ExecParams, - exec_command_context: ExecCommandContext, - error: SandboxErr, - sandbox_type: SandboxType, - sess: &Session, - turn_context: &TurnContext, -) -> ResponseInputItem { - let call_id = exec_command_context.call_id.clone(); - let sub_id = exec_command_context.sub_id.clone(); - let cwd = exec_command_context.cwd.clone(); - - if let SandboxErr::Timeout { output } = &error { - let content = format_exec_output(output); - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content, - success: Some(false), - }, - }; - } - - // Early out if either the user never wants to be asked for approval, or - // we're letting the model manage escalation requests. Otherwise, continue - match turn_context.approval_policy { - AskForApproval::Never | AskForApproval::OnRequest => { - return ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: format!( - "failed in sandbox {sandbox_type:?} with execution error: {error}" - ), - success: Some(false), - }, - }; - } - AskForApproval::UnlessTrusted | AskForApproval::OnFailure => (), - } - - // Note that when `error` is `SandboxErr::Denied`, it could be a false - // positive. That is, it may have exited with a non-zero exit code, not - // because the sandbox denied it, but because that is its expected behavior, - // i.e., a grep command that did not match anything. Ideally we would - // include additional metadata on the command to indicate whether non-zero - // exit codes merit a retry. - - // For now, we categorically ask the user to retry without sandbox and - // emit the raw error as a background event. - sess.notify_background_event(&sub_id, format!("Execution failed: {error}")) - .await; - - let rx_approve = sess - .request_command_approval( - sub_id.clone(), - call_id.clone(), - params.command.clone(), - cwd.clone(), - Some("command failed; retry without sandbox?".to_string()), - ) - .await; - - match rx_approve.await.unwrap_or_default() { - ReviewDecision::Approved | ReviewDecision::ApprovedForSession => { - // Persist this command as pre‑approved for the - // remainder of the session so future - // executions skip the sandbox directly. - // TODO(ragona): Isn't this a bug? It always saves the command in an | fork? - sess.add_approved_command(params.command.clone()).await; - // Inform UI we are retrying without sandbox. - sess.notify_background_event(&sub_id, "retrying command without sandbox") - .await; - - // This is an escalated retry; the policy will not be - // examined and the sandbox has been set to `None`. - let retry_output_result = sess - .run_exec_with_events( - turn_diff_tracker, - exec_command_context.clone(), - ExecInvokeArgs { - params, - sandbox_type: SandboxType::None, - sandbox_policy: &turn_context.sandbox_policy, - sandbox_cwd: &turn_context.cwd, - codex_linux_sandbox_exe: &sess.codex_linux_sandbox_exe, - stdout_stream: if exec_command_context.apply_patch.is_some() { - None - } else { - Some(StdoutStream { - sub_id: sub_id.clone(), - call_id: call_id.clone(), - tx_event: sess.tx_event.clone(), - }) - }, - }, - ) - .await; - - match retry_output_result { - Ok(retry_output) => { - let ExecToolCallOutput { exit_code, .. } = &retry_output; - - let is_success = *exit_code == 0; - let content = format_exec_output(&retry_output); - - ResponseInputItem::FunctionCallOutput { - call_id: call_id.clone(), - output: FunctionCallOutputPayload { - content, - success: Some(is_success), - }, - } - } - Err(e) => ResponseInputItem::FunctionCallOutput { - call_id: call_id.clone(), - output: FunctionCallOutputPayload { - content: format!("retry failed: {e}"), - success: None, - }, - }, - } - } - ReviewDecision::Denied | ReviewDecision::Abort => { - // Fall through to original failure handling. - ResponseInputItem::FunctionCallOutput { - call_id, - output: FunctionCallOutputPayload { - content: "exec command rejected by user".to_string(), - success: None, - }, - } - } - } -} - -fn format_exec_output_str(exec_output: &ExecToolCallOutput) -> String { - let ExecToolCallOutput { - aggregated_output, .. - } = exec_output; - - // Head+tail truncation for the model: show the beginning and end with an elision. - // Clients still receive full streams; only this formatted summary is capped. - - let mut s = &aggregated_output.text; - let prefixed_str: String; - - if exec_output.timed_out { - prefixed_str = format!( - "command timed out after {} milliseconds\n", - exec_output.duration.as_millis() - ) + s; - s = &prefixed_str; - } - - let total_lines = s.lines().count(); - if s.len() <= MODEL_FORMAT_MAX_BYTES && total_lines <= MODEL_FORMAT_MAX_LINES { - return s.to_string(); - } - - let lines: Vec<&str> = s.lines().collect(); - let head_take = MODEL_FORMAT_HEAD_LINES.min(lines.len()); - let tail_take = MODEL_FORMAT_TAIL_LINES.min(lines.len().saturating_sub(head_take)); - let omitted = lines.len().saturating_sub(head_take + tail_take); - - // Join head and tail blocks (lines() strips newlines; reinsert them) - let head_block = lines - .iter() - .take(head_take) - .cloned() - .collect::>() - .join("\n"); - let tail_block = if tail_take > 0 { - lines[lines.len() - tail_take..].join("\n") - } else { - String::new() - }; - let marker = format!("\n[... omitted {omitted} of {total_lines} lines ...]\n\n"); - - // Byte budgets for head/tail around the marker - let mut head_budget = MODEL_FORMAT_HEAD_BYTES.min(MODEL_FORMAT_MAX_BYTES); - let tail_budget = MODEL_FORMAT_MAX_BYTES.saturating_sub(head_budget + marker.len()); - if tail_budget == 0 && marker.len() >= MODEL_FORMAT_MAX_BYTES { - // Degenerate case: marker alone exceeds budget; return a clipped marker - return take_bytes_at_char_boundary(&marker, MODEL_FORMAT_MAX_BYTES).to_string(); - } - if tail_budget == 0 { - // Make room for the marker by shrinking head - head_budget = MODEL_FORMAT_MAX_BYTES.saturating_sub(marker.len()); - } - - // Enforce line-count cap by trimming head/tail lines - let head_lines_text = head_block; - let tail_lines_text = tail_block; - // Build final string respecting byte budgets - let head_part = take_bytes_at_char_boundary(&head_lines_text, head_budget); - let mut result = String::with_capacity(MODEL_FORMAT_MAX_BYTES.min(s.len())); - - result.push_str(head_part); - result.push_str(&marker); - - let remaining = MODEL_FORMAT_MAX_BYTES.saturating_sub(result.len()); - let tail_budget_final = remaining; - let tail_part = take_last_bytes_at_char_boundary(&tail_lines_text, tail_budget_final); - result.push_str(tail_part); - - result -} - -// Truncate a &str to a byte budget at a char boundary (prefix) -#[inline] -fn take_bytes_at_char_boundary(s: &str, maxb: usize) -> &str { - if s.len() <= maxb { - return s; - } - let mut last_ok = 0; - for (i, ch) in s.char_indices() { - let nb = i + ch.len_utf8(); - if nb > maxb { - break; - } - last_ok = nb; - } - &s[..last_ok] -} - -// Take a suffix of a &str within a byte budget at a char boundary -#[inline] -fn take_last_bytes_at_char_boundary(s: &str, maxb: usize) -> &str { - if s.len() <= maxb { - return s; - } - let mut start = s.len(); - let mut used = 0usize; - for (i, ch) in s.char_indices().rev() { - let nb = ch.len_utf8(); - if used + nb > maxb { - break; - } - start = i; - used += nb; - if start == 0 { - break; - } - } - &s[start..] -} - -/// Exec output is a pre-serialized JSON payload -fn format_exec_output(exec_output: &ExecToolCallOutput) -> String { - let ExecToolCallOutput { - exit_code, - duration, - .. - } = exec_output; - - #[derive(Serialize)] - struct ExecMetadata { - exit_code: i32, - duration_seconds: f32, - } - - #[derive(Serialize)] - struct ExecOutput<'a> { - output: &'a str, - metadata: ExecMetadata, - } - - // round to 1 decimal place - let duration_seconds = ((duration.as_secs_f32()) * 10.0).round() / 10.0; - - let formatted_output = format_exec_output_str(exec_output); - - let payload = ExecOutput { - output: &formatted_output, - metadata: ExecMetadata { - exit_code: *exit_code, - duration_seconds, - }, - }; - - #[expect(clippy::expect_used)] - serde_json::to_string(&payload).expect("serialize ExecOutput") -} - pub(super) fn get_last_assistant_message_from_turn(responses: &[ResponseItem]) -> Option { responses.iter().rev().find_map(|item| { if let ResponseItem::Message { role, content, .. } = item { @@ -3269,96 +2309,8 @@ pub(super) fn get_last_assistant_message_from_turn(responses: &[ResponseItem]) - } }) } -fn convert_call_tool_result_to_function_call_output_payload( - call_tool_result: &CallToolResult, -) -> FunctionCallOutputPayload { - let CallToolResult { - content, - is_error, - structured_content, - } = call_tool_result; - - // In terms of what to send back to the model, we prefer structured_content, - // if available, and fallback to content, otherwise. - let mut is_success = is_error != &Some(true); - let content = if let Some(structured_content) = structured_content - && structured_content != &serde_json::Value::Null - && let Ok(serialized_structured_content) = serde_json::to_string(&structured_content) - { - serialized_structured_content - } else { - match serde_json::to_string(&content) { - Ok(serialized_content) => serialized_content, - Err(err) => { - // If we could not serialize either content or structured_content to - // JSON, flag this as an error. - is_success = false; - err.to_string() - } - } - }; - - FunctionCallOutputPayload { - content, - success: Some(is_success), - } -} - -/// Emits an ExitedReviewMode Event with optional ReviewOutput, -/// and records a developer message with the review output. -async fn exit_review_mode( - session: Arc, - task_sub_id: String, - review_output: Option, -) { - let event = Event { - id: task_sub_id, - msg: EventMsg::ExitedReviewMode(ExitedReviewModeEvent { - review_output: review_output.clone(), - }), - }; - session.send_event(event).await; - - let mut user_message = String::new(); - if let Some(out) = review_output { - let mut findings_str = String::new(); - let text = out.overall_explanation.trim(); - if !text.is_empty() { - findings_str.push_str(text); - } - if !out.findings.is_empty() { - let block = format_review_findings_block(&out.findings, None); - findings_str.push_str(&format!("\n{block}")); - } - user_message.push_str(&format!( - r#" - User initiated a review task. Here's the full review output from reviewer model. User may select one or more comments to resolve. - review - - {findings_str} - - -"#)); - } else { - user_message.push_str(r#" - User initiated a review task, but was interrupted. If user asks about this, tell them to re-initiate a review with `/review` and wait for it to complete. - review - - None. - - -"#); - } - - session - .record_conversation_items(&[ResponseItem::Message { - id: None, - role: "user".to_string(), - content: vec![ContentItem::InputText { text: user_message }], - }]) - .await; -} +use crate::features::Features; #[cfg(test)] pub(crate) use tests::make_session_and_context; @@ -3367,10 +2319,29 @@ mod tests { use super::*; use crate::config::ConfigOverrides; use crate::config::ConfigToml; + use crate::exec::ExecToolCallOutput; + use crate::tools::format_exec_output_str; + use crate::protocol::CompactedItem; use crate::protocol::InitialHistory; use crate::protocol::ResumedHistory; + use crate::state::TaskKind; + use crate::tasks::SessionTask; + use crate::tasks::SessionTaskContext; + use crate::tools::ToolRouter; + use crate::tools::context::ToolInvocation; + use crate::tools::context::ToolOutput; + use crate::tools::context::ToolPayload; + use crate::tools::handlers::ShellHandler; + use crate::tools::handlers::UnifiedExecHandler; + use crate::tools::registry::ToolHandler; + use crate::turn_diff_tracker::TurnDiffTracker; + use codex_app_server_protocol::AuthMode; use codex_protocol::models::ContentItem; + use codex_protocol::models::ResponseItem; + use std::time::Duration; + use tokio::time::sleep; + use mcp_types::ContentBlock; use mcp_types::TextContent; use pretty_assertions::assert_eq; @@ -3395,16 +2366,17 @@ mod tests { let (session, turn_context) = make_session_and_context(); let (rollout_items, expected) = sample_rollout(&session, &turn_context); - tokio_test::block_on(session.record_initial_history( - &turn_context, - InitialHistory::Resumed(ResumedHistory { + tokio_test::block_on(session.record_initial_history(InitialHistory::Resumed( + ResumedHistory { conversation_id: ConversationId::default(), history: rollout_items, rollout_path: PathBuf::from("/tmp/resume.jsonl"), - }), - )); + }, + ))); - let actual = tokio_test::block_on(async { session.state.lock().await.history.contents() }); + let actual = tokio_test::block_on(async { + session.state.lock().await.clone_history().get_history() + }); assert_eq!(expected, actual); } @@ -3413,11 +2385,11 @@ mod tests { let (session, turn_context) = make_session_and_context(); let (rollout_items, expected) = sample_rollout(&session, &turn_context); - tokio_test::block_on( - session.record_initial_history(&turn_context, InitialHistory::Forked(rollout_items)), - ); + tokio_test::block_on(session.record_initial_history(InitialHistory::Forked(rollout_items))); - let actual = tokio_test::block_on(async { session.state.lock().await.history.contents() }); + let actual = tokio_test::block_on(async { + session.state.lock().await.clone_history().get_history() + }); assert_eq!(expected, actual); } @@ -3433,7 +2405,7 @@ mod tests { })), }; - let got = convert_call_tool_result_to_function_call_output_payload(&ctr); + let got = FunctionCallOutputPayload::from(&ctr); let expected = FunctionCallOutputPayload { content: serde_json::to_string(&json!({ "ok": true, @@ -3441,89 +2413,12 @@ mod tests { })) .unwrap(), success: Some(true), + ..Default::default() }; assert_eq!(expected, got); } - #[test] - fn model_truncation_head_tail_by_lines() { - // Build 400 short lines so line-count limit, not byte budget, triggers truncation - let lines: Vec = (1..=400).map(|i| format!("line{i}")).collect(); - let full = lines.join("\n"); - - let exec = ExecToolCallOutput { - exit_code: 0, - stdout: StreamOutput::new(String::new()), - stderr: StreamOutput::new(String::new()), - aggregated_output: StreamOutput::new(full), - duration: StdDuration::from_secs(1), - timed_out: false, - }; - - let out = format_exec_output_str(&exec); - - // Expect elision marker with correct counts - let omitted = 400 - MODEL_FORMAT_MAX_LINES; // 144 - let marker = format!("\n[... omitted {omitted} of 400 lines ...]\n\n"); - assert!(out.contains(&marker), "missing marker: {out}"); - - // Validate head and tail - let parts: Vec<&str> = out.split(&marker).collect(); - assert_eq!(parts.len(), 2, "expected one marker split"); - let head = parts[0]; - let tail = parts[1]; - - let expected_head: String = (1..=MODEL_FORMAT_HEAD_LINES) - .map(|i| format!("line{i}")) - .collect::>() - .join("\n"); - assert!(head.starts_with(&expected_head), "head mismatch"); - - let expected_tail: String = ((400 - MODEL_FORMAT_TAIL_LINES + 1)..=400) - .map(|i| format!("line{i}")) - .collect::>() - .join("\n"); - assert!(tail.ends_with(&expected_tail), "tail mismatch"); - } - - #[test] - fn model_truncation_respects_byte_budget() { - // Construct a large output (about 100kB) so byte budget dominates - let big_line = "x".repeat(100); - let full = std::iter::repeat_n(big_line, 1000) - .collect::>() - .join("\n"); - - let exec = ExecToolCallOutput { - exit_code: 0, - stdout: StreamOutput::new(String::new()), - stderr: StreamOutput::new(String::new()), - aggregated_output: StreamOutput::new(full.clone()), - duration: StdDuration::from_secs(1), - timed_out: false, - }; - - let out = format_exec_output_str(&exec); - assert!(out.len() <= MODEL_FORMAT_MAX_BYTES, "exceeds byte budget"); - assert!(out.contains("omitted"), "should contain elision marker"); - - // Ensure head and tail are drawn from the original - assert!(full.starts_with(out.chars().take(8).collect::().as_str())); - assert!( - full.ends_with( - out.chars() - .rev() - .take(8) - .collect::() - .chars() - .rev() - .collect::() - .as_str() - ) - ); - } - #[test] fn includes_timed_out_message() { let exec = ExecToolCallOutput { @@ -3551,11 +2446,12 @@ mod tests { structured_content: Some(serde_json::Value::Null), }; - let got = convert_call_tool_result_to_function_call_output_payload(&ctr); + let got = FunctionCallOutputPayload::from(&ctr); let expected = FunctionCallOutputPayload { content: serde_json::to_string(&vec![text_block("hello"), text_block("world")]) .unwrap(), success: Some(true), + ..Default::default() }; assert_eq!(expected, got); @@ -3569,10 +2465,11 @@ mod tests { structured_content: Some(json!({ "message": "bad" })), }; - let got = convert_call_tool_result_to_function_call_output_payload(&ctr); + let got = FunctionCallOutputPayload::from(&ctr); let expected = FunctionCallOutputPayload { content: serde_json::to_string(&json!({ "message": "bad" })).unwrap(), success: Some(false), + ..Default::default() }; assert_eq!(expected, got); @@ -3586,10 +2483,11 @@ mod tests { structured_content: None, }; - let got = convert_call_tool_result_to_function_call_output_payload(&ctr); + let got = FunctionCallOutputPayload::from(&ctr); let expected = FunctionCallOutputPayload { content: serde_json::to_string(&vec![text_block("alpha")]).unwrap(), success: Some(true), + ..Default::default() }; assert_eq!(expected, got); @@ -3603,6 +2501,19 @@ mod tests { }) } + fn otel_event_manager(conversation_id: ConversationId, config: &Config) -> OtelEventManager { + OtelEventManager::new( + conversation_id, + config.model.as_str(), + config.model_family.slug.as_str(), + None, + Some("test@test.com".to_string()), + Some(AuthMode::ChatGPT), + false, + "test".to_string(), + ) + } + pub(crate) fn make_session_and_context() -> (Session, TurnContext) { let (tx_event, _rx_event) = async_channel::unbounded(); let codex_home = tempfile::tempdir().expect("create temp dir"); @@ -3614,60 +2525,347 @@ mod tests { .expect("load default test config"); let config = Arc::new(config); let conversation_id = ConversationId::default(); - let client = ModelClient::new( - config.clone(), - None, - config.model_provider.clone(), - config.model_reasoning_effort, - config.model_reasoning_summary, - conversation_id, + let otel_event_manager = otel_event_manager(conversation_id, config.as_ref()); + let auth_manager = AuthManager::shared( + config.cwd.clone(), + false, + config.cli_auth_credentials_store_mode, ); - let tools_config = ToolsConfig::new(&ToolsConfigParams { - model_family: &config.model_family, - include_plan_tool: config.include_plan_tool, - include_apply_patch_tool: config.include_apply_patch_tool, - include_web_search_request: config.tools_web_search_request, - use_streamable_shell_tool: config.use_experimental_streamable_shell_tool, - include_view_image_tool: config.include_view_image_tool, - experimental_unified_exec_tool: config.use_experimental_unified_exec_tool, - }); - let turn_context = TurnContext { - client, - cwd: config.cwd.clone(), - base_instructions: config.base_instructions.clone(), + + let session_configuration = SessionConfiguration { + provider: config.model_provider.clone(), + model: config.model.clone(), + model_reasoning_effort: config.model_reasoning_effort, + model_reasoning_summary: config.model_reasoning_summary, + developer_instructions: config.developer_instructions.clone(), user_instructions: config.user_instructions.clone(), + base_instructions: config.base_instructions.clone(), + compact_prompt: config.compact_prompt.clone(), approval_policy: config.approval_policy, sandbox_policy: config.sandbox_policy.clone(), - shell_environment_policy: config.shell_environment_policy.clone(), - tools_config, - is_review_mode: false, + cwd: config.cwd.clone(), + original_config_do_not_use: Arc::clone(&config), + features: Features::default(), + session_source: SessionSource::Exec, }; + + let state = SessionState::new(session_configuration.clone()); + + let services = SessionServices { + mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::default())), + mcp_startup_cancellation_token: CancellationToken::new(), + unified_exec_manager: UnifiedExecSessionManager::default(), + notifier: UserNotifier::new(None), + rollout: Mutex::new(None), + user_shell: shell::Shell::Unknown, + show_raw_agent_reasoning: config.show_raw_agent_reasoning, + auth_manager: Arc::clone(&auth_manager), + otel_event_manager: otel_event_manager.clone(), + tool_approvals: Mutex::new(ApprovalStore::default()), + }; + + let turn_context = Session::make_turn_context( + Some(Arc::clone(&auth_manager)), + &otel_event_manager, + session_configuration.provider.clone(), + &session_configuration, + conversation_id, + "turn_id".to_string(), + ); + let session = Session { conversation_id, tx_event, - mcp_connection_manager: McpConnectionManager::default(), - session_manager: ExecSessionManager::default(), - unified_exec_manager: UnifiedExecSessionManager::default(), - notify: None, - rollout: Mutex::new(None), - state: Mutex::new(State { - history: ConversationHistory::new(), - ..Default::default() - }), - codex_linux_sandbox_exe: None, - user_shell: shell::Shell::Unknown, - show_raw_agent_reasoning: config.show_raw_agent_reasoning, + state: Mutex::new(state), + active_turn: Mutex::new(None), + services, next_internal_sub_id: AtomicU64::new(0), }; + (session, turn_context) } + // Like make_session_and_context, but returns Arc and the event receiver + // so tests can assert on emitted events. + fn make_session_and_context_with_rx() -> ( + Arc, + Arc, + async_channel::Receiver, + ) { + let (tx_event, rx_event) = async_channel::unbounded(); + let codex_home = tempfile::tempdir().expect("create temp dir"); + let config = Config::load_from_base_config_with_overrides( + ConfigToml::default(), + ConfigOverrides::default(), + codex_home.path().to_path_buf(), + ) + .expect("load default test config"); + let config = Arc::new(config); + let conversation_id = ConversationId::default(); + let otel_event_manager = otel_event_manager(conversation_id, config.as_ref()); + let auth_manager = AuthManager::shared( + config.cwd.clone(), + false, + config.cli_auth_credentials_store_mode, + ); + + let session_configuration = SessionConfiguration { + provider: config.model_provider.clone(), + model: config.model.clone(), + model_reasoning_effort: config.model_reasoning_effort, + model_reasoning_summary: config.model_reasoning_summary, + developer_instructions: config.developer_instructions.clone(), + user_instructions: config.user_instructions.clone(), + base_instructions: config.base_instructions.clone(), + compact_prompt: config.compact_prompt.clone(), + approval_policy: config.approval_policy, + sandbox_policy: config.sandbox_policy.clone(), + cwd: config.cwd.clone(), + original_config_do_not_use: Arc::clone(&config), + features: Features::default(), + session_source: SessionSource::Exec, + }; + + let state = SessionState::new(session_configuration.clone()); + + let services = SessionServices { + mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::default())), + mcp_startup_cancellation_token: CancellationToken::new(), + unified_exec_manager: UnifiedExecSessionManager::default(), + notifier: UserNotifier::new(None), + rollout: Mutex::new(None), + user_shell: shell::Shell::Unknown, + show_raw_agent_reasoning: config.show_raw_agent_reasoning, + auth_manager: Arc::clone(&auth_manager), + otel_event_manager: otel_event_manager.clone(), + tool_approvals: Mutex::new(ApprovalStore::default()), + }; + + let turn_context = Arc::new(Session::make_turn_context( + Some(Arc::clone(&auth_manager)), + &otel_event_manager, + session_configuration.provider.clone(), + &session_configuration, + conversation_id, + "turn_id".to_string(), + )); + + let session = Arc::new(Session { + conversation_id, + tx_event, + state: Mutex::new(state), + active_turn: Mutex::new(None), + services, + next_internal_sub_id: AtomicU64::new(0), + }); + + (session, turn_context, rx_event) + } + + #[derive(Clone, Copy)] + struct NeverEndingTask { + kind: TaskKind, + listen_to_cancellation_token: bool, + } + + #[async_trait::async_trait] + impl SessionTask for NeverEndingTask { + fn kind(&self) -> TaskKind { + self.kind + } + + async fn run( + self: Arc, + _session: Arc, + _ctx: Arc, + _input: Vec, + cancellation_token: CancellationToken, + ) -> Option { + if self.listen_to_cancellation_token { + cancellation_token.cancelled().await; + return None; + } + loop { + sleep(Duration::from_secs(60)).await; + } + } + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + #[test_log::test] + async fn abort_regular_task_emits_turn_aborted_only() { + let (sess, tc, rx) = make_session_and_context_with_rx(); + let input = vec![UserInput::Text { + text: "hello".to_string(), + }]; + sess.spawn_task( + Arc::clone(&tc), + input, + NeverEndingTask { + kind: TaskKind::Regular, + listen_to_cancellation_token: false, + }, + ) + .await; + + sess.abort_all_tasks(TurnAbortReason::Interrupted).await; + + let evt = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("timeout waiting for event") + .expect("event"); + match evt.msg { + EventMsg::TurnAborted(e) => assert_eq!(TurnAbortReason::Interrupted, e.reason), + other => panic!("unexpected event: {other:?}"), + } + assert!(rx.try_recv().is_err()); + } + + #[tokio::test] + async fn abort_gracefuly_emits_turn_aborted_only() { + let (sess, tc, rx) = make_session_and_context_with_rx(); + let input = vec![UserInput::Text { + text: "hello".to_string(), + }]; + sess.spawn_task( + Arc::clone(&tc), + input, + NeverEndingTask { + kind: TaskKind::Regular, + listen_to_cancellation_token: true, + }, + ) + .await; + + sess.abort_all_tasks(TurnAbortReason::Interrupted).await; + + let evt = rx.recv().await.expect("event"); + match evt.msg { + EventMsg::TurnAborted(e) => assert_eq!(TurnAbortReason::Interrupted, e.reason), + other => panic!("unexpected event: {other:?}"), + } + assert!(rx.try_recv().is_err()); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn abort_review_task_emits_exited_then_aborted_and_records_history() { + let (sess, tc, rx) = make_session_and_context_with_rx(); + let input = vec![UserInput::Text { + text: "start review".to_string(), + }]; + sess.spawn_task(Arc::clone(&tc), input, ReviewTask).await; + + sess.abort_all_tasks(TurnAbortReason::Interrupted).await; + + // Drain events until we observe ExitedReviewMode; earlier + // RawResponseItem entries (e.g., environment context) may arrive first. + loop { + let evt = tokio::time::timeout(std::time::Duration::from_secs(1), rx.recv()) + .await + .expect("timeout waiting for first event") + .expect("first event"); + match evt.msg { + EventMsg::ExitedReviewMode(ev) => { + assert!(ev.review_output.is_none()); + break; + } + // Ignore any non-critical events before exit. + _ => continue, + } + } + loop { + let evt = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("timeout waiting for next event") + .expect("event"); + match evt.msg { + EventMsg::RawResponseItem(_) => continue, + EventMsg::TurnAborted(e) => { + assert_eq!(TurnAbortReason::Interrupted, e.reason); + break; + } + other => panic!("unexpected second event: {other:?}"), + } + } + + let history = sess.clone_history().await.get_history(); + let found = history.iter().any(|item| match item { + ResponseItem::Message { role, content, .. } if role == "user" => { + content.iter().any(|ci| match ci { + ContentItem::InputText { text } => { + text.contains("") + && text.contains("review") + && text.contains("interrupted") + } + _ => false, + }) + } + _ => false, + }); + assert!( + found, + "synthetic review interruption not recorded in history" + ); + } + + #[tokio::test] + async fn fatal_tool_error_stops_turn_and_reports_error() { + let (session, turn_context, _rx) = make_session_and_context_with_rx(); + let tools = { + session + .services + .mcp_connection_manager + .read() + .await + .list_all_tools() + .await + }; + let router = ToolRouter::from_config( + &turn_context.tools_config, + Some( + tools + .into_iter() + .map(|(name, tool)| (name, tool.tool)) + .collect(), + ), + ); + let item = ResponseItem::CustomToolCall { + id: None, + status: None, + call_id: "call-1".to_string(), + name: "shell".to_string(), + input: "{}".to_string(), + }; + + let call = ToolRouter::build_tool_call(session.as_ref(), item.clone()) + .await + .expect("build tool call") + .expect("tool call present"); + let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); + let err = router + .dispatch_tool_call( + Arc::clone(&session), + Arc::clone(&turn_context), + tracker, + call, + ) + .await + .expect_err("expected fatal error"); + + match err { + FunctionCallError::Fatal(message) => { + assert_eq!(message, "tool shell invoked with incompatible payload"); + } + other => panic!("expected FunctionCallError::Fatal, got {other:?}"), + } + } + fn sample_rollout( session: &Session, turn_context: &TurnContext, ) -> (Vec, Vec) { let mut rollout_items = Vec::new(); - let mut live_history = ConversationHistory::new(); + let mut live_history = ContextManager::new(); let initial_context = session.build_initial_context(turn_context); for item in &initial_context { @@ -3696,7 +2894,7 @@ mod tests { rollout_items.push(RolloutItem::ResponseItem(assistant1.clone())); let summary1 = "summary one"; - let snapshot1 = live_history.contents(); + let snapshot1 = live_history.get_history(); let user_messages1 = collect_user_messages(&snapshot1); let rebuilt1 = build_compacted_history( session.build_initial_context(turn_context), @@ -3729,7 +2927,7 @@ mod tests { rollout_items.push(RolloutItem::ResponseItem(assistant2.clone())); let summary2 = "summary two"; - let snapshot2 = live_history.contents(); + let snapshot2 = live_history.get_history(); let user_messages2 = collect_user_messages(&snapshot2); let rebuilt2 = build_compacted_history( session.build_initial_context(turn_context), @@ -3761,7 +2959,7 @@ mod tests { live_history.record_items(std::iter::once(&assistant3)); rollout_items.push(RolloutItem::ResponseItem(assistant3.clone())); - (rollout_items, live_history.contents()) + (rollout_items, live_history.get_history()) } #[tokio::test] @@ -3772,9 +2970,11 @@ mod tests { use crate::turn_diff_tracker::TurnDiffTracker; use std::collections::HashMap; - let (session, mut turn_context) = make_session_and_context(); + let (session, mut turn_context_raw) = make_session_and_context(); // Ensure policy is NOT OnRequest so the early rejection path triggers - turn_context.approval_policy = AskForApproval::OnFailure; + turn_context_raw.approval_policy = AskForApproval::OnFailure; + let session = Arc::new(session); + let mut turn_context = Arc::new(turn_context_raw); let params = ExecParams { command: if cfg!(windows) { @@ -3795,6 +2995,7 @@ mod tests { env: HashMap::new(), with_escalated_permissions: Some(true), justification: Some("test".to_string()), + arg0: None, }; let params2 = ExecParams { @@ -3802,23 +3003,34 @@ mod tests { ..params.clone() }; - let mut turn_diff_tracker = TurnDiffTracker::new(); + let turn_diff_tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); - let sub_id = "test-sub".to_string(); + let tool_name = "shell"; let call_id = "test-call".to_string(); - let resp = handle_container_exec_with_params( - params, - &session, - &turn_context, - &mut turn_diff_tracker, - sub_id, - call_id, - ) - .await; + let handler = ShellHandler; + let resp = handler + .handle(ToolInvocation { + session: Arc::clone(&session), + turn: Arc::clone(&turn_context), + tracker: Arc::clone(&turn_diff_tracker), + call_id, + tool_name: tool_name.to_string(), + payload: ToolPayload::Function { + arguments: serde_json::json!({ + "command": params.command.clone(), + "workdir": Some(turn_context.cwd.to_string_lossy().to_string()), + "timeout_ms": params.timeout_ms, + "with_escalated_permissions": params.with_escalated_permissions, + "justification": params.justification.clone(), + }) + .to_string(), + }, + }) + .await; - let ResponseInputItem::FunctionCallOutput { output, .. } = resp else { - panic!("expected FunctionCallOutput"); + let Err(FunctionCallError::RespondToModel(output)) = resp else { + panic!("expected error result"); }; let expected = format!( @@ -3826,24 +3038,37 @@ mod tests { policy = turn_context.approval_policy ); - pretty_assertions::assert_eq!(output.content, expected); + pretty_assertions::assert_eq!(output, expected); // Now retry the same command WITHOUT escalated permissions; should succeed. // Force DangerFullAccess to avoid platform sandbox dependencies in tests. - turn_context.sandbox_policy = SandboxPolicy::DangerFullAccess; + Arc::get_mut(&mut turn_context) + .expect("unique turn context Arc") + .sandbox_policy = SandboxPolicy::DangerFullAccess; - let resp2 = handle_container_exec_with_params( - params2, - &session, - &turn_context, - &mut turn_diff_tracker, - "test-sub".to_string(), - "test-call-2".to_string(), - ) - .await; + let resp2 = handler + .handle(ToolInvocation { + session: Arc::clone(&session), + turn: Arc::clone(&turn_context), + tracker: Arc::clone(&turn_diff_tracker), + call_id: "test-call-2".to_string(), + tool_name: tool_name.to_string(), + payload: ToolPayload::Function { + arguments: serde_json::json!({ + "command": params2.command.clone(), + "workdir": Some(turn_context.cwd.to_string_lossy().to_string()), + "timeout_ms": params2.timeout_ms, + "with_escalated_permissions": params2.with_escalated_permissions, + "justification": params2.justification.clone(), + }) + .to_string(), + }, + }) + .await; - let ResponseInputItem::FunctionCallOutput { output, .. } = resp2 else { - panic!("expected FunctionCallOutput on retry"); + let output = match resp2.expect("expected Ok result") { + ToolOutput::Function { content, .. } => content, + _ => panic!("unexpected tool output"), }; #[derive(Deserialize, PartialEq, Eq, Debug)] @@ -3858,10 +3083,50 @@ mod tests { } let exec_output: ResponseExecOutput = - serde_json::from_str(&output.content).expect("valid exec output json"); + serde_json::from_str(&output).expect("valid exec output json"); pretty_assertions::assert_eq!(exec_output.metadata, ResponseExecMetadata { exit_code: 0 }); assert!(exec_output.output.contains("hi")); - pretty_assertions::assert_eq!(output.success, Some(true)); + } + #[tokio::test] + async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request() { + use crate::protocol::AskForApproval; + use crate::turn_diff_tracker::TurnDiffTracker; + + let (session, mut turn_context_raw) = make_session_and_context(); + turn_context_raw.approval_policy = AskForApproval::OnFailure; + let session = Arc::new(session); + let turn_context = Arc::new(turn_context_raw); + let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); + + let handler = UnifiedExecHandler; + let resp = handler + .handle(ToolInvocation { + session: Arc::clone(&session), + turn: Arc::clone(&turn_context), + tracker: Arc::clone(&tracker), + call_id: "exec-call".to_string(), + tool_name: "exec_command".to_string(), + payload: ToolPayload::Function { + arguments: serde_json::json!({ + "cmd": "echo hi", + "with_escalated_permissions": true, + "justification": "need unsandboxed execution", + }) + .to_string(), + }, + }) + .await; + + let Err(FunctionCallError::RespondToModel(output)) = resp else { + panic!("expected error result"); + }; + + let expected = format!( + "approval policy is {policy:?}; reject command — you cannot ask for escalated permissions if the approval policy is {policy:?}", + policy = turn_context.approval_policy + ); + + pretty_assertions::assert_eq!(output, expected); } } diff --git a/codex-rs/core/src/codex/compact.rs b/codex-rs/core/src/codex/compact.rs deleted file mode 100644 index 05d57cd945..0000000000 --- a/codex-rs/core/src/codex/compact.rs +++ /dev/null @@ -1,399 +0,0 @@ -use std::sync::Arc; - -use super::AgentTask; -use super::Session; -use super::TurnContext; -use super::get_last_assistant_message_from_turn; -use crate::Prompt; -use crate::client_common::ResponseEvent; -use crate::error::CodexErr; -use crate::error::Result as CodexResult; -use crate::protocol::AgentMessageEvent; -use crate::protocol::CompactedItem; -use crate::protocol::ErrorEvent; -use crate::protocol::Event; -use crate::protocol::EventMsg; -use crate::protocol::InputItem; -use crate::protocol::InputMessageKind; -use crate::protocol::TaskCompleteEvent; -use crate::protocol::TaskStartedEvent; -use crate::protocol::TurnContextItem; -use crate::util::backoff; -use askama::Template; -use codex_protocol::models::ContentItem; -use codex_protocol::models::ResponseInputItem; -use codex_protocol::models::ResponseItem; -use codex_protocol::protocol::RolloutItem; -use futures::prelude::*; - -pub(super) const COMPACT_TRIGGER_TEXT: &str = "Start Summarization"; -const SUMMARIZATION_PROMPT: &str = include_str!("../../templates/compact/prompt.md"); - -#[derive(Template)] -#[template(path = "compact/history_bridge.md", escape = "none")] -struct HistoryBridgeTemplate<'a> { - user_messages_text: &'a str, - summary_text: &'a str, -} - -pub(super) async fn spawn_compact_task( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, -) { - let task = AgentTask::compact( - sess.clone(), - turn_context, - sub_id, - input, - SUMMARIZATION_PROMPT.to_string(), - ); - sess.set_task(task).await; -} - -pub(super) async fn run_inline_auto_compact_task( - sess: Arc, - turn_context: Arc, -) { - let sub_id = sess.next_internal_sub_id(); - let input = vec![InputItem::Text { - text: COMPACT_TRIGGER_TEXT.to_string(), - }]; - run_compact_task_inner( - sess, - turn_context, - sub_id, - input, - SUMMARIZATION_PROMPT.to_string(), - false, - ) - .await; -} - -pub(super) async fn run_compact_task( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, - compact_instructions: String, -) { - let start_event = Event { - id: sub_id.clone(), - msg: EventMsg::TaskStarted(TaskStartedEvent { - model_context_window: turn_context.client.get_model_context_window(), - }), - }; - sess.send_event(start_event).await; - run_compact_task_inner( - sess.clone(), - turn_context, - sub_id.clone(), - input, - compact_instructions, - true, - ) - .await; - let event = Event { - id: sub_id, - msg: EventMsg::TaskComplete(TaskCompleteEvent { - last_agent_message: None, - }), - }; - sess.send_event(event).await; -} - -async fn run_compact_task_inner( - sess: Arc, - turn_context: Arc, - sub_id: String, - input: Vec, - compact_instructions: String, - remove_task_on_completion: bool, -) { - let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input); - let instructions_override = compact_instructions; - let turn_input = sess - .turn_input_with_history(vec![initial_input_for_turn.clone().into()]) - .await; - - let prompt = Prompt { - input: turn_input, - tools: Vec::new(), - base_instructions_override: Some(instructions_override), - }; - - let max_retries = turn_context.client.get_provider().stream_max_retries(); - let mut retries = 0; - - let rollout_item = RolloutItem::TurnContext(TurnContextItem { - cwd: turn_context.cwd.clone(), - approval_policy: turn_context.approval_policy, - sandbox_policy: turn_context.sandbox_policy.clone(), - model: turn_context.client.get_model(), - effort: turn_context.client.get_reasoning_effort(), - summary: turn_context.client.get_reasoning_summary(), - }); - sess.persist_rollout_items(&[rollout_item]).await; - - loop { - let attempt_result = drain_to_completed(&sess, turn_context.as_ref(), &prompt).await; - - match attempt_result { - Ok(()) => { - break; - } - Err(CodexErr::Interrupted) => { - return; - } - Err(e) => { - if retries < max_retries { - retries += 1; - let delay = backoff(retries); - sess.notify_stream_error( - &sub_id, - format!( - "stream error: {e}; retrying {retries}/{max_retries} in {delay:?}…" - ), - ) - .await; - tokio::time::sleep(delay).await; - continue; - } else { - let event = Event { - id: sub_id.clone(), - msg: EventMsg::Error(ErrorEvent { - message: e.to_string(), - }), - }; - sess.send_event(event).await; - return; - } - } - } - } - - if remove_task_on_completion { - sess.remove_task(&sub_id).await; - } - let history_snapshot = { - let state = sess.state.lock().await; - state.history.contents() - }; - let summary_text = get_last_assistant_message_from_turn(&history_snapshot).unwrap_or_default(); - let user_messages = collect_user_messages(&history_snapshot); - let initial_context = sess.build_initial_context(turn_context.as_ref()); - let new_history = build_compacted_history(initial_context, &user_messages, &summary_text); - { - let mut state = sess.state.lock().await; - state.history.replace(new_history); - } - - let rollout_item = RolloutItem::Compacted(CompactedItem { - message: summary_text.clone(), - }); - sess.persist_rollout_items(&[rollout_item]).await; - - let event = Event { - id: sub_id.clone(), - msg: EventMsg::AgentMessage(AgentMessageEvent { - message: "Compact task completed".to_string(), - }), - }; - sess.send_event(event).await; -} - -pub fn content_items_to_text(content: &[ContentItem]) -> Option { - let mut pieces = Vec::new(); - for item in content { - match item { - ContentItem::InputText { text } | ContentItem::OutputText { text } => { - if !text.is_empty() { - pieces.push(text.as_str()); - } - } - ContentItem::InputImage { .. } => {} - } - } - if pieces.is_empty() { - None - } else { - Some(pieces.join("\n")) - } -} - -pub(crate) fn collect_user_messages(items: &[ResponseItem]) -> Vec { - items - .iter() - .filter_map(|item| match item { - ResponseItem::Message { role, content, .. } if role == "user" => { - content_items_to_text(content) - } - _ => None, - }) - .filter(|text| !is_session_prefix_message(text)) - .collect() -} - -pub fn is_session_prefix_message(text: &str) -> bool { - matches!( - InputMessageKind::from(("user", text)), - InputMessageKind::UserInstructions | InputMessageKind::EnvironmentContext - ) -} - -pub(crate) fn build_compacted_history( - initial_context: Vec, - user_messages: &[String], - summary_text: &str, -) -> Vec { - let mut history = initial_context; - let user_messages_text = if user_messages.is_empty() { - "(none)".to_string() - } else { - user_messages.join("\n\n") - }; - let summary_text = if summary_text.is_empty() { - "(no summary available)".to_string() - } else { - summary_text.to_string() - }; - let Ok(bridge) = HistoryBridgeTemplate { - user_messages_text: &user_messages_text, - summary_text: &summary_text, - } - .render() else { - return vec![]; - }; - history.push(ResponseItem::Message { - id: None, - role: "user".to_string(), - content: vec![ContentItem::InputText { text: bridge }], - }); - history -} - -async fn drain_to_completed( - sess: &Session, - turn_context: &TurnContext, - prompt: &Prompt, -) -> CodexResult<()> { - let mut stream = turn_context.client.clone().stream(prompt).await?; - loop { - let maybe_event = stream.next().await; - let Some(event) = maybe_event else { - return Err(CodexErr::Stream( - "stream closed before response.completed".into(), - None, - )); - }; - match event { - Ok(ResponseEvent::OutputItemDone(item)) => { - let mut state = sess.state.lock().await; - state.history.record_items(std::slice::from_ref(&item)); - } - Ok(ResponseEvent::Completed { .. }) => { - return Ok(()); - } - Ok(_) => continue, - Err(e) => return Err(e), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn content_items_to_text_joins_non_empty_segments() { - let items = vec![ - ContentItem::InputText { - text: "hello".to_string(), - }, - ContentItem::OutputText { - text: String::new(), - }, - ContentItem::OutputText { - text: "world".to_string(), - }, - ]; - - let joined = content_items_to_text(&items); - - assert_eq!(Some("hello\nworld".to_string()), joined); - } - - #[test] - fn content_items_to_text_ignores_image_only_content() { - let items = vec![ContentItem::InputImage { - image_url: "file://image.png".to_string(), - }]; - - let joined = content_items_to_text(&items); - - assert_eq!(None, joined); - } - - #[test] - fn collect_user_messages_extracts_user_text_only() { - let items = vec![ - ResponseItem::Message { - id: Some("assistant".to_string()), - role: "assistant".to_string(), - content: vec![ContentItem::OutputText { - text: "ignored".to_string(), - }], - }, - ResponseItem::Message { - id: Some("user".to_string()), - role: "user".to_string(), - content: vec![ - ContentItem::InputText { - text: "first".to_string(), - }, - ContentItem::OutputText { - text: "second".to_string(), - }, - ], - }, - ResponseItem::Other, - ]; - - let collected = collect_user_messages(&items); - - assert_eq!(vec!["first\nsecond".to_string()], collected); - } - - #[test] - fn collect_user_messages_filters_session_prefix_entries() { - let items = vec![ - ResponseItem::Message { - id: None, - role: "user".to_string(), - content: vec![ContentItem::InputText { - text: "do things".to_string(), - }], - }, - ResponseItem::Message { - id: None, - role: "user".to_string(), - content: vec![ContentItem::InputText { - text: "cwd=/tmp".to_string(), - }], - }, - ResponseItem::Message { - id: None, - role: "user".to_string(), - content: vec![ContentItem::InputText { - text: "real user message".to_string(), - }], - }, - ]; - - let collected = collect_user_messages(&items); - - assert_eq!(vec!["real user message".to_string()], collected); - } -} diff --git a/codex-rs/core/src/codex_conversation.rs b/codex-rs/core/src/codex_conversation.rs index d3b00046fc..5bb9c97c5b 100644 --- a/codex-rs/core/src/codex_conversation.rs +++ b/codex-rs/core/src/codex_conversation.rs @@ -3,16 +3,21 @@ use crate::error::Result as CodexResult; use crate::protocol::Event; use crate::protocol::Op; use crate::protocol::Submission; +use std::path::PathBuf; pub struct CodexConversation { codex: Codex, + rollout_path: PathBuf, } /// Conduit for the bidirectional stream of messages that compose a conversation /// in Codex. impl CodexConversation { - pub(crate) fn new(codex: Codex) -> Self { - Self { codex } + pub(crate) fn new(codex: Codex, rollout_path: PathBuf) -> Self { + Self { + codex, + rollout_path, + } } pub async fn submit(&self, op: Op) -> CodexResult { @@ -27,4 +32,8 @@ impl CodexConversation { pub async fn next_event(&self) -> CodexResult { self.codex.next_event().await } + + pub fn rollout_path(&self) -> PathBuf { + self.rollout_path.clone() + } } diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs new file mode 100644 index 0000000000..4cb4d4a06a --- /dev/null +++ b/codex-rs/core/src/codex_delegate.rs @@ -0,0 +1,300 @@ +use std::sync::Arc; +use std::sync::atomic::AtomicU64; + +use async_channel::Receiver; +use async_channel::Sender; +use codex_async_utils::OrCancelExt; +use codex_protocol::protocol::ApplyPatchApprovalRequestEvent; +use codex_protocol::protocol::Event; +use codex_protocol::protocol::EventMsg; +use codex_protocol::protocol::ExecApprovalRequestEvent; +use codex_protocol::protocol::Op; +use codex_protocol::protocol::SessionSource; +use codex_protocol::protocol::SubAgentSource; +use codex_protocol::protocol::Submission; +use codex_protocol::user_input::UserInput; +use tokio_util::sync::CancellationToken; + +use crate::AuthManager; +use crate::codex::Codex; +use crate::codex::CodexSpawnOk; +use crate::codex::SUBMISSION_CHANNEL_CAPACITY; +use crate::codex::Session; +use crate::codex::TurnContext; +use crate::config::Config; +use crate::error::CodexErr; +use codex_protocol::protocol::InitialHistory; + +/// Start an interactive sub-Codex conversation and return IO channels. +/// +/// The returned `events_rx` yields non-approval events emitted by the sub-agent. +/// Approval requests are handled via `parent_session` and are not surfaced. +/// The returned `ops_tx` allows the caller to submit additional `Op`s to the sub-agent. +pub(crate) async fn run_codex_conversation_interactive( + config: Config, + auth_manager: Arc, + parent_session: Arc, + parent_ctx: Arc, + cancel_token: CancellationToken, + initial_history: Option, +) -> Result { + let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); + let (tx_ops, rx_ops) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); + + let CodexSpawnOk { codex, .. } = Codex::spawn( + config, + auth_manager, + initial_history.unwrap_or(InitialHistory::New), + SessionSource::SubAgent(SubAgentSource::Review), + ) + .await?; + let codex = Arc::new(codex); + + // Use a child token so parent cancel cascades but we can scope it to this task + let cancel_token_events = cancel_token.child_token(); + let cancel_token_ops = cancel_token.child_token(); + + // Forward events from the sub-agent to the consumer, filtering approvals and + // routing them to the parent session for decisions. + let parent_session_clone = Arc::clone(&parent_session); + let parent_ctx_clone = Arc::clone(&parent_ctx); + let codex_for_events = Arc::clone(&codex); + tokio::spawn(async move { + let _ = forward_events( + codex_for_events, + tx_sub, + parent_session_clone, + parent_ctx_clone, + cancel_token_events.clone(), + ) + .or_cancel(&cancel_token_events) + .await; + }); + + // Forward ops from the caller to the sub-agent. + let codex_for_ops = Arc::clone(&codex); + tokio::spawn(async move { + forward_ops(codex_for_ops, rx_ops, cancel_token_ops).await; + }); + + Ok(Codex { + next_id: AtomicU64::new(0), + tx_sub: tx_ops, + rx_event: rx_sub, + }) +} + +/// Convenience wrapper for one-time use with an initial prompt. +/// +/// Internally calls the interactive variant, then immediately submits the provided input. +pub(crate) async fn run_codex_conversation_one_shot( + config: Config, + auth_manager: Arc, + input: Vec, + parent_session: Arc, + parent_ctx: Arc, + cancel_token: CancellationToken, + initial_history: Option, +) -> Result { + // Use a child token so we can stop the delegate after completion without + // requiring the caller to cancel the parent token. + let child_cancel = cancel_token.child_token(); + let io = run_codex_conversation_interactive( + config, + auth_manager, + parent_session, + parent_ctx, + child_cancel.clone(), + initial_history, + ) + .await?; + + // Send the initial input to kick off the one-shot turn. + io.submit(Op::UserInput { items: input }).await?; + + // Bridge events so we can observe completion and shut down automatically. + let (tx_bridge, rx_bridge) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); + let ops_tx = io.tx_sub.clone(); + let io_for_bridge = io; + tokio::spawn(async move { + while let Ok(event) = io_for_bridge.next_event().await { + let should_shutdown = matches!( + event.msg, + EventMsg::TaskComplete(_) | EventMsg::TurnAborted(_) + ); + let _ = tx_bridge.send(event).await; + if should_shutdown { + let _ = ops_tx + .send(Submission { + id: "shutdown".to_string(), + op: Op::Shutdown {}, + }) + .await; + child_cancel.cancel(); + break; + } + } + }); + + // For one-shot usage, return a closed `tx_sub` so callers cannot submit + // additional ops after the initial request. Create a channel and drop the + // receiver to close it immediately. + let (tx_closed, rx_closed) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); + drop(rx_closed); + + Ok(Codex { + next_id: AtomicU64::new(0), + rx_event: rx_bridge, + tx_sub: tx_closed, + }) +} + +async fn forward_events( + codex: Arc, + tx_sub: Sender, + parent_session: Arc, + parent_ctx: Arc, + cancel_token: CancellationToken, +) { + while let Ok(event) = codex.next_event().await { + match event { + // ignore all legacy delta events + Event { + id: _, + msg: EventMsg::AgentMessageDelta(_) | EventMsg::AgentReasoningDelta(_), + } => continue, + Event { + id: _, + msg: EventMsg::SessionConfigured(_), + } => continue, + Event { + id, + msg: EventMsg::ExecApprovalRequest(event), + } => { + // Initiate approval via parent session; do not surface to consumer. + handle_exec_approval( + &codex, + id, + &parent_session, + &parent_ctx, + event, + &cancel_token, + ) + .await; + } + Event { + id, + msg: EventMsg::ApplyPatchApprovalRequest(event), + } => { + handle_patch_approval( + &codex, + id, + &parent_session, + &parent_ctx, + event, + &cancel_token, + ) + .await; + } + other => { + let _ = tx_sub.send(other).await; + } + } + } +} + +/// Forward ops from a caller to a sub-agent, respecting cancellation. +async fn forward_ops( + codex: Arc, + rx_ops: Receiver, + cancel_token_ops: CancellationToken, +) { + loop { + let op: Op = match rx_ops.recv().or_cancel(&cancel_token_ops).await { + Ok(Ok(Submission { id: _, op })) => op, + Ok(Err(_)) | Err(_) => break, + }; + let _ = codex.submit(op).await; + } +} + +/// Handle an ExecApprovalRequest by consulting the parent session and replying. +async fn handle_exec_approval( + codex: &Codex, + id: String, + parent_session: &Session, + parent_ctx: &TurnContext, + event: ExecApprovalRequestEvent, + cancel_token: &CancellationToken, +) { + // Race approval with cancellation and timeout to avoid hangs. + let approval_fut = parent_session.request_command_approval( + parent_ctx, + parent_ctx.sub_id.clone(), + event.command, + event.cwd, + event.reason, + event.risk, + ); + let decision = await_approval_with_cancel( + approval_fut, + parent_session, + &parent_ctx.sub_id, + cancel_token, + ) + .await; + + let _ = codex.submit(Op::ExecApproval { id, decision }).await; +} + +/// Handle an ApplyPatchApprovalRequest by consulting the parent session and replying. +async fn handle_patch_approval( + codex: &Codex, + id: String, + parent_session: &Session, + parent_ctx: &TurnContext, + event: ApplyPatchApprovalRequestEvent, + cancel_token: &CancellationToken, +) { + let decision_rx = parent_session + .request_patch_approval( + parent_ctx, + parent_ctx.sub_id.clone(), + event.changes, + event.reason, + event.grant_root, + ) + .await; + let decision = await_approval_with_cancel( + async move { decision_rx.await.unwrap_or_default() }, + parent_session, + &parent_ctx.sub_id, + cancel_token, + ) + .await; + let _ = codex.submit(Op::PatchApproval { id, decision }).await; +} + +/// Await an approval decision, aborting on cancellation. +async fn await_approval_with_cancel( + fut: F, + parent_session: &Session, + sub_id: &str, + cancel_token: &CancellationToken, +) -> codex_protocol::protocol::ReviewDecision +where + F: core::future::Future, +{ + tokio::select! { + biased; + _ = cancel_token.cancelled() => { + parent_session + .notify_approval(sub_id, codex_protocol::protocol::ReviewDecision::Abort) + .await; + codex_protocol::protocol::ReviewDecision::Abort + } + decision = fut => { + decision + } + } +} diff --git a/codex-rs/core/src/command_safety/is_dangerous_command.rs b/codex-rs/core/src/command_safety/is_dangerous_command.rs new file mode 100644 index 0000000000..09594bb1c5 --- /dev/null +++ b/codex-rs/core/src/command_safety/is_dangerous_command.rs @@ -0,0 +1,142 @@ +use codex_protocol::protocol::AskForApproval; +use codex_protocol::protocol::SandboxPolicy; + +use crate::bash::parse_shell_lc_plain_commands; +use crate::is_safe_command::is_known_safe_command; + +pub fn requires_initial_appoval( + policy: AskForApproval, + sandbox_policy: &SandboxPolicy, + command: &[String], + with_escalated_permissions: bool, +) -> bool { + if is_known_safe_command(command) { + return false; + } + match policy { + AskForApproval::Never | AskForApproval::OnFailure => false, + AskForApproval::OnRequest => { + // In DangerFullAccess, only prompt if the command looks dangerous. + if matches!(sandbox_policy, SandboxPolicy::DangerFullAccess) { + return command_might_be_dangerous(command); + } + + // In restricted sandboxes (ReadOnly/WorkspaceWrite), do not prompt for + // non‑escalated, non‑dangerous commands — let the sandbox enforce + // restrictions (e.g., block network/write) without a user prompt. + let wants_escalation: bool = with_escalated_permissions; + if wants_escalation { + return true; + } + command_might_be_dangerous(command) + } + AskForApproval::UnlessTrusted => !is_known_safe_command(command), + } +} + +pub fn command_might_be_dangerous(command: &[String]) -> bool { + if is_dangerous_to_call_with_exec(command) { + return true; + } + + // Support `bash -lc "