diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a551f7247..b337ea424 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,6 +28,14 @@ on: description: Run the oxa build (disabled only for draft PR checks) type: boolean default: true + engine: + description: The engine component changed (false only for a merge group that leaves engine/ alone) + type: boolean + default: true + oxa: + description: The oxa component changed (false only for a merge group that leaves oxa/ alone) + type: boolean + default: true permissions: contents: read @@ -41,14 +49,15 @@ defaults: run: shell: bash # bash -eo pipefail -# Runner choice for the x86_64 jobs (the expression in each `runs-on`): the project's own runner (label `oxci`, a rented +# Runner choice for the x86_64 jobs (the expression in each `runs-on`): the project's own runners (label `oxci` for the merge queue, +# main and manual runs; label `oxci-pr`, a separate low-priority pool, for pull requests; a rented # throwaway server with no secrets) when the repository variable OXCI is 'on'. Pull-request runs use it only when # OXCI_PR is also 'on' AND the pull request comes from a branch of this repository; a pull request from a fork always # runs on the hosted image, so outside code never reaches the runner. Any other value of OXCI sends every job back # to the hosted images (kill switch). The ARM jobs are always hosted. jobs: engine: - if: inputs.heavy + if: inputs.heavy && inputs.engine name: oxe-server (MoE) + oxi runs-on: ubuntu-24.04-arm container: @@ -69,7 +78,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant fedora44-moe engine - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -102,6 +111,15 @@ jobs: run: | ulimit -c 0 # no core dumps from the deliberate-abort tests (tests::panic_child) cargo test --release --locked --no-fail-fast --workspace --exclude qwen4exp + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + engine/target + key: ${{ steps.rust-key.outputs.prefix }}main - name: Package if: inputs.package env: @@ -119,7 +137,7 @@ jobs: retention-days: 7 flash: - if: inputs.heavy + if: inputs.heavy && inputs.engine name: oxe-server --features flash runs-on: ubuntu-24.04-arm container: @@ -143,7 +161,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant fedora44-flash engine - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -178,6 +196,15 @@ jobs: cargo test --release --locked --no-fail-fast -p qwen4exp ./target/release/flash-m3 selftest | tee selftest.txt ! grep -q FAIL selftest.txt + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + engine/target + key: ${{ steps.rust-key.outputs.prefix }}main - name: Package if: inputs.package env: @@ -193,9 +220,9 @@ jobs: retention-days: 7 oxa-test-build: - if: inputs.heavy + if: inputs.heavy && inputs.oxa name: oxa test archive (x86_64) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-24.04' }} + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 30 env: RUSTUP_TOOLCHAIN: "1.99.1" @@ -209,7 +236,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant ubuntu24-x86-oxa-ci oxa - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -249,6 +276,15 @@ jobs: --archive-file "$RUNNER_TEMP/oxa-tests.tar.zst" cargo nextest list --archive-file "$RUNNER_TEMP/oxa-tests.tar.zst" \ --message-format json > "$RUNNER_TEMP/oxa-test-list.json" + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + oxa/target + key: ${{ steps.rust-key.outputs.prefix }}main - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: oxa-test-archive-x86 @@ -264,10 +300,10 @@ jobs: retention-days: 1 oxa-test-shards: - if: inputs.heavy + if: inputs.heavy && inputs.oxa needs: oxa-test-build name: oxa tests shard ${{ matrix.shard }} (x86_64) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-24.04' }} + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 20 strategy: fail-fast: false @@ -322,10 +358,10 @@ jobs: oxa-tests: # Preserve the required check name. A missing/failed build or shard cannot turn green. - if: always() && inputs.heavy + if: always() && inputs.heavy && inputs.oxa needs: [oxa-test-build, oxa-test-shards] name: oxa tests (bwrap sandbox required) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-24.04' }} + runs-on: ubuntu-24.04-arm timeout-minutes: 5 steps: - name: Every archive and partition must pass @@ -350,7 +386,7 @@ jobs: "$RUNNER_TEMP/oxa-reports/junit-1.xml" "$RUNNER_TEMP/oxa-reports/junit-2.xml" oxa: - if: inputs.build_oxa + if: inputs.build_oxa && inputs.oxa # oxa (the coding agent) is its own cargo workspace at oxa/. No model, no engine build: oxa # talks to the engine over HTTP only. Generic aarch64 code (no target-cpu flag). name: oxa @@ -368,7 +404,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant fedora44-oxa-release oxa - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -388,6 +424,15 @@ jobs: - name: Build oxa working-directory: oxa run: cargo build --release --locked --bin oxa + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + oxa/target + key: ${{ steps.rust-key.outputs.prefix }}main - name: Package if: inputs.package working-directory: oxa diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index a8029da85..1c83ab8b1 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -1,6 +1,25 @@ name: checks # Hosted CI: cheap drafts, fast PR checks, full merge-queue/main/manual/nightly gate. # Security scans always run, including docs-only changes. No secrets. +# +# Path-aware PRs and merge queue: tools/ci/paths.py classifies the files a ready PR changes +# (base...head) or a queued group changes (merge_group.base_sha..head_sha); ready PRs and the queue +# run the same set. Outputs: engine/ and fuzz/oxe/ -> `engine` (clippy, x86_64 stub server tests, +# ARM engine and Flash builds, oxe fuzz smoke); oxa/ and fuzz/oxa/ -> `oxa` (clippy, the oxa test +# archive and shards, the ARM oxa build, oxa fuzz smoke); oxc/ -> `lab`, `cli` and `gateway`; +# oxc-rs/ and conformance/ -> `cli`; oxc-gateway/ -> `gateway`. Anything else (.github/, tools/, +# packaging/, root files, a new directory) and an empty group diff count as "everything changed". +# A push on main, the nightly run and manual runs set every output, so main runs everything after +# each merge (one run at a time; a newer push replaces a waiting one) and catches cross-component +# breaks that a path-gated PR or queue entry skipped. Drafts run only the classifier, the scans +# and rustfmt. The scans run on every event and every change, unconditionally. +# The `gate` job aggregates every job below; it fails when any job failed or was cancelled, when +# the classifier or the scans did not succeed, or when a classifier output is not literally +# 'true'/'false', so a skip caused by a failed or missing classifier cannot pass. +# +# Caches: jobs only restore; only main (push, nightly, manual) saves, once per dependency-level key +# prefix (compiler, flags, Cargo.toml, Cargo.lock) as `main`. Caches saved on PR or queue +# refs are visible only there, so saving them would only evict main's shared caches. on: push: branches: ["main"] @@ -26,7 +45,8 @@ concurrency: env: RUST_VERSION: "1.99.1" -# Runner choice for the x86_64 jobs (the expression in each `runs-on`): the project's own runner (label `oxci`, a rented +# Runner choice for the x86_64 jobs (the expression in each `runs-on`): the project's own runners (label `oxci` for the merge queue, +# main and manual runs; label `oxci-pr`, a separate low-priority pool, for pull requests; a rented # throwaway server with no secrets) when the repository variable OXCI is 'on'. Pull-request runs use it only when # OXCI_PR is also 'on' AND the pull request comes from a branch of this repository; a pull request from a fork always # runs on the hosted image, so outside code never reaches the runner. Any other value of OXCI sends every job back @@ -34,7 +54,7 @@ env: jobs: changes: name: classify changed paths - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + runs-on: ubuntu-24.04-arm timeout-minutes: 5 outputs: full: ${{ steps.paths.outputs.full }} @@ -42,6 +62,9 @@ jobs: engine: ${{ steps.paths.outputs.engine }} oxa: ${{ steps.paths.outputs.oxa }} lab: ${{ steps.paths.outputs.lab }} + cli: ${{ steps.paths.outputs.cli }} + gateway: ${{ steps.paths.outputs.gateway }} + md: ${{ steps.paths.outputs.md }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -55,8 +78,8 @@ jobs: links: name: documentation links needs: changes - if: (needs.changes.outputs.full == 'true' || needs.changes.outputs.docs == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + if: (needs.changes.outputs.full == 'true' || needs.changes.outputs.docs == 'true' || needs.changes.outputs.md == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) + runs-on: ubuntu-24.04-arm timeout-minutes: 5 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -68,12 +91,17 @@ jobs: build: # Always call the reusable workflow so each required child check reports, even when skipped. # oxe-server, oxi, the Flash variant and oxa: build and model-free tests on ARM64 (build.yml) + # Ready PRs and the queue run the engine and oxa jobs only for the components they changed; + # the classifier sets every component on main, nightly and manual runs. + needs: changes uses: ./.github/workflows/build.yml with: version: dev-${{ github.sha }} package: false - heavy: ${{ github.event_name != 'pull_request' && github.event_name != 'push' }} + heavy: ${{ github.event_name != 'pull_request' || github.event.pull_request.draft == false }} build_oxa: ${{ github.event_name != 'pull_request' || github.event.pull_request.draft == false }} + engine: ${{ needs.changes.outputs.engine == 'true' }} + oxa: ${{ needs.changes.outputs.oxa == 'true' }} oxa-nightly-arm: # Extra primary-platform coverage at night; manual opt-in validates this job before merge. @@ -99,7 +127,7 @@ jobs: - name: Compiler and source cache key id: rust-key run: python3 -I tools/ci/rust_cache_key.py --variant ubuntu24-arm-oxa-ci oxa - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -114,6 +142,15 @@ jobs: run: tools/ci/install_nextest.sh "$RUNNER_TEMP/nextest" - name: Whole ARM64 workspace, isolated HOME and mandatory bwrap run: tools/ci/run_oxa_nightly.sh "$RUNNER_TEMP/oxa-arm-nightly" + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + oxa/target + key: ${{ steps.rust-key.outputs.prefix }}main - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 if: always() with: @@ -126,7 +163,7 @@ jobs: rustfmt: name: rustfmt (oxa) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + runs-on: ubuntu-24.04-arm timeout-minutes: 15 env: RUSTUP_TOOLCHAIN: "1.99.1" @@ -148,10 +185,10 @@ jobs: native-controller-cli: needs: changes - if: github.event_name != 'pull_request' || (github.event.pull_request.draft == false && (needs.changes.outputs.full == 'true' || needs.changes.outputs.lab == 'true')) + if: needs.changes.outputs.cli == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) name: experimental oxc CLI (x86_64) - # stays on the hosted image for now: its conformance step failed on the project's runner (to be looked at) - runs-on: ubuntu-24.04 + # the project's runner image uses the same tool-cache path as the hosted image, so setup-python works there too + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 10 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -179,7 +216,7 @@ jobs: clippy: needs: changes - if: github.event_name != 'pull_request' || (github.event.pull_request.draft == false && (needs.changes.outputs.full == 'true' || needs.changes.outputs.engine == 'true' || needs.changes.outputs.oxa == 'true')) + if: (needs.changes.outputs.engine == 'true' || needs.changes.outputs.oxa == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) # Compile errors fail the job; lint warnings are reported but do not fail it yet. Add # "-- -D warnings" once the warning count is known to be zero. name: clippy (engine, oxa; aarch64) @@ -197,7 +234,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant fedora44-clippy engine oxa - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -216,23 +253,34 @@ jobs: - name: Rust cache result run: echo "Rust cache exact hit = ${{ steps.rust-cache.outputs.cache-hit }}" >> "$GITHUB_STEP_SUMMARY" - name: engine (model-free workspace, all targets) - if: github.event_name != 'pull_request' || needs.changes.outputs.full == 'true' || needs.changes.outputs.engine == 'true' + if: needs.changes.outputs.engine == 'true' working-directory: engine run: cargo clippy --locked --workspace --exclude qwen4exp --all-targets - name: oxa (all targets) - if: github.event_name != 'pull_request' || needs.changes.outputs.full == 'true' || needs.changes.outputs.oxa == 'true' + if: needs.changes.outputs.oxa == 'true' working-directory: oxa run: cargo clippy --locked --workspace --all-targets + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + engine/target + oxa/target + key: ${{ steps.rust-key.outputs.prefix }}main x86-64-stub: needs: changes - if: github.event_name != 'pull_request' || github.event.pull_request.draft == false + # engine changes run the server tests; controller changes need the stub build for the contract + if: (needs.changes.outputs.engine == 'true' || needs.changes.outputs.lab == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) # oxe-server builds on x86_64 with the stub engine only (kernels-sys compiles its kernels on # aarch64 only; the model engine refuses to load elsewhere). Its server tests that do not need # the model engine run here. Uses the hosted image's rustup (setup_toolchain.sh installs the # aarch64 rustup binary only). name: oxe-server --engine stub on x86_64 (build + server tests) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 60 env: RUSTUP_TOOLCHAIN: "1.99.1" @@ -247,7 +295,7 @@ jobs: id: rust-key working-directory: ${{ github.workspace }} run: python3 -I tools/ci/rust_cache_key.py --variant ubuntu24-x86-stub engine - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 id: rust-cache with: path: | @@ -264,7 +312,8 @@ jobs: run: find engine oxa -path '*/target' -prune -o -type f -exec touch -c {} + - name: Rust cache result run: echo "Rust cache exact hit = ${{ steps.rust-cache.outputs.cache-hit }}" >> "$GITHUB_STEP_SUMMARY" - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 + id: pip-cache with: path: ~/.cache/pip key: ${{ runner.os }}-${{ runner.arch }}-pip-${{ hashFiles('oxc/pyproject.toml', 'tools/openai-contract/requirements.txt') }} @@ -275,7 +324,7 @@ jobs: working-directory: engine run: ./target/release/oxe-server --help > help.txt && head -n 3 help.txt - name: Server tests (stub engine; model-engine tests are aarch64-only) - if: github.event_name != 'pull_request' || needs.changes.outputs.full == 'true' || needs.changes.outputs.engine == 'true' + if: needs.changes.outputs.engine == 'true' working-directory: engine timeout-minutes: 30 run: | @@ -289,11 +338,28 @@ jobs: mkdir -p "$root/home" env -i PATH="$PATH" HOME="$root/home" PIP_CACHE_DIR="$HOME/.cache/pip" \ tools/openai-contract/run.sh --mode both --server "$PWD/engine/target/release/oxe-server" --work "$root/work" + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.rust-cache.outputs.cache-matched-key != format('{0}main', steps.rust-key.outputs.prefix) + with: + path: | + ~/.cargo/registry + ~/.cargo/git + engine/target + key: ${{ steps.rust-key.outputs.prefix }}main + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.pip-cache.outputs.cache-hit != 'true' + with: + path: ~/.cache/pip + key: ${{ runner.os }}-${{ runner.arch }}-pip-${{ hashFiles('oxc/pyproject.toml', 'tools/openai-contract/requirements.txt') }} fuzz-oxa: # Task 61: the stable smoke of the oxa cargo-fuzz targets (seconds; no nightly/sanitizer). name: oxa fuzz smoke (x86_64) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + needs: changes + if: needs.changes.outputs.oxa == 'true' + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 30 env: RUSTUP_TOOLCHAIN: "1.99.1" @@ -310,7 +376,9 @@ jobs: fuzz-oxe: # Task 61: the stable smoke of the oxe cargo-fuzz targets (seconds; no model weights). name: oxe fuzz smoke (x86_64) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + needs: changes + if: needs.changes.outputs.engine == 'true' + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 30 env: RUSTUP_TOOLCHAIN: "1.99.1" @@ -325,9 +393,10 @@ jobs: run: cargo test --locked --manifest-path fuzz/oxe/Cargo.toml --test smoke lab: - if: github.event_name != 'pull_request' && github.event_name != 'push' + needs: changes + if: needs.changes.outputs.lab == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) name: lab tests, CLI, receipt scrub (x86_64) - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} timeout-minutes: 30 defaults: run: @@ -337,7 +406,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5 + - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 + id: pip-cache with: path: ~/.cache/pip key: ${{ runner.os }}-${{ runner.arch }}-pip-${{ hashFiles('oxc/pyproject.toml', 'tools/openai-contract/requirements.txt') }} @@ -379,10 +449,17 @@ jobs: root=$(mktemp -d); trap 'rm -rf "$root"' EXIT HOME="$root" XDG_CONFIG_HOME="$root/c" XDG_STATE_HOME="$root/s" XDG_RUNTIME_DIR="$root/r" \ PYTHONPATH=src python3 -m oxc --help > /dev/null + - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 + # Only main seeds caches: queue and PR refs cannot share theirs (see the header). + if: github.ref == 'refs/heads/main' && github.event_name != 'merge_group' && steps.pip-cache.outputs.cache-hit != 'true' + with: + path: ~/.cache/pip + key: ${{ runner.os }}-${{ runner.arch }}-pip-${{ hashFiles('oxc/pyproject.toml', 'tools/openai-contract/requirements.txt') }} scans: name: secret scan, SDK-material check, product isolation - runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository)) && 'oxci' || 'ubuntu-latest' }} + # Never path-gated: runs on every event and every change. + runs-on: ubuntu-24.04-arm timeout-minutes: 15 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -443,3 +520,57 @@ jobs: # when receipts are scrubbed locally). Here that is the hosted runner's account, `runner`, # which only matches the English word "runner" in receipts. The other rules still run. run: git ls-files -z benchmarks experiments | xargs -0 -r timeout 300 python3 -I tools/scrub_receipts.py --check --user '' + + gateway: + # Moved from cloud-gateway.yml (fmt + conformance, same steps and matrix) so the gate covers it. + needs: changes + if: needs.changes.outputs.gateway == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) + name: cloud gateway (${{ matrix.runner }}) + strategy: + matrix: + runner: [ubuntu-latest, ubuntu-24.04-arm] + # x86_64 uses the shared runner expression; ARM stays hosted. + runs-on: ${{ matrix.runner == 'ubuntu-latest' && (vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04') || matrix.runner }} + timeout-minutes: 15 + env: + RUSTUP_TOOLCHAIN: "1.99.1" + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - run: | + sudo apt-get update -q + sudo apt-get install -y -q --no-install-recommends bubblewrap + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 + - run: rustup toolchain install "$RUSTUP_TOOLCHAIN" --profile minimal --component clippy --component rustfmt --no-self-update + - run: cargo fmt --manifest-path oxc-gateway/Cargo.toml --all -- --check + - run: cargo check --locked --manifest-path oxc-gateway/Cargo.toml --no-default-features + - run: cargo clippy --locked --manifest-path oxc-gateway/Cargo.toml --all-targets --all-features -- -D warnings + - run: cargo build --locked --manifest-path oxc-gateway/Cargo.toml --all-features --examples + - run: /usr/bin/python3 -m unittest discover -s oxc-gateway/conformance -v + - run: OXC_POLICY_CONTRACT="$PWD/oxc-gateway/target/debug/examples/policy_contract" OXC_SECRETS_CONTRACT="$PWD/oxc-gateway/target/debug/examples/secrets_contract" /usr/bin/python3 -m unittest discover -s oxc-gateway/conformance -v + + gate: + # The one aggregate check: every job in `needs` must succeed or be skipped; the classifier and + # the scans must succeed; every classifier output must be literally 'true' or 'false'. + name: gate + if: always() + needs: [changes, scans, links, rustfmt, clippy, x86-64-stub, native-controller-cli, fuzz-oxa, fuzz-oxe, lab, build, gateway] + runs-on: ubuntu-24.04-arm + timeout-minutes: 5 + steps: + - env: + NEEDS: ${{ toJSON(needs) }} + run: | + python3 -I - <<'PY' + import json, os, sys + needs = json.loads(os.environ['NEEDS']) + bad = {k: v['result'] for k, v in needs.items() if v['result'] not in ('success', 'skipped')} + for k in ('changes', 'scans'): # these two may never be skipped + if needs[k]['result'] != 'success': bad[k] = needs[k]['result'] + flags = needs['changes'].get('outputs', {}) + for k in ('engine', 'oxa', 'lab', 'cli', 'gateway', 'docs', 'md', 'full'): + if flags.get(k) not in ('true', 'false'): bad['changes.' + k] = repr(flags.get(k)) + print(json.dumps({k: v['result'] for k, v in needs.items()}, indent=1)); print('bad:', bad) + sys.exit(1 if bad else 0) + PY diff --git a/.github/workflows/cloud-gateway.yml b/.github/workflows/cloud-gateway.yml index 3b0c4e610..dc82ccd32 100644 --- a/.github/workflows/cloud-gateway.yml +++ b/.github/workflows/cloud-gateway.yml @@ -1,9 +1,7 @@ name: cloud gateway +# Manual runs only. PRs, the merge queue and main run the same steps as job `gateway` in +# checks.yml, path-gated on the classifier's `gateway` output and covered by the `gate` check. on: - pull_request: - types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] - paths: ['oxc-gateway/**', 'oxc/**', '.github/workflows/cloud-gateway.yml'] - merge_group: workflow_dispatch: permissions: contents: read @@ -14,7 +12,7 @@ env: RUSTUP_TOOLCHAIN: '1.99.1' jobs: fmt: - runs-on: ubuntu-latest + runs-on: ${{ vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04' }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -26,7 +24,9 @@ jobs: strategy: matrix: runner: [ubuntu-latest, ubuntu-24.04-arm] - runs-on: ${{ matrix.runner }} + # x86_64 uses the shared runner expression (see checks.yml); ARM stays hosted. The matrix + # value is unchanged, so the check names are too. + runs-on: ${{ matrix.runner == 'ubuntu-latest' && (vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04') || matrix.runner }} timeout-minutes: 15 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml index 5db01d409..1d47d9835 100644 --- a/.github/workflows/release-build.yml +++ b/.github/workflows/release-build.yml @@ -1,6 +1,12 @@ name: release-build # Artifact-only qualification. No tags, schedules, credentials, or publication jobs. on: + workflow_call: + inputs: + version: + description: release tag, used in the archive names + type: string + default: v0.2.0 workflow_dispatch: pull_request: types: [opened, synchronize, reopened, ready_for_review] @@ -14,13 +20,14 @@ permissions: concurrency: group: release-build-${{ github.ref }} - cancel-in-progress: true + cancel-in-progress: ${{ github.event_name == 'pull_request' }} defaults: run: shell: bash env: + VERSION: ${{ inputs.version || 'v0.2.0' }} CARGO_HOME: /opt/cargo RUSTUP_HOME: /opt/rustup CARGO_BUILD_JOBS: '2' @@ -76,7 +83,7 @@ jobs: done cmp /build/first.tar.gz /build/second.tar.gz arch=$(uname -m) - cp /build/first.tar.gz "$GITHUB_WORKSPACE/dist/ox-llm-0.2.0-linux-$arch.tar.gz" + cp /build/first.tar.gz "$GITHUB_WORKSPACE/dist/ox-llm-$VERSION-linux-$arch.tar.gz" echo 'PASS: two clean builds in different source/target directories produced identical payload tarballs.' \ > "$GITHUB_WORKSPACE/dist/REPRODUCIBILITY.txt" { rustc --version; cargo --version; cc --version; c++ --version; ld --version; rpm -q glibc libstdc++; } \ @@ -88,8 +95,9 @@ jobs: run: | cd /build/first mkdir -p /build/payload - tar -xzf "$GITHUB_WORKSPACE/dist/ox-llm-0.2.0-linux-$(uname -m).tar.gz" -C /build/payload + tar -xzf "$GITHUB_WORKSPACE/dist/ox-llm-$VERSION-linux-$(uname -m).tar.gz" -C /build/payload python3 -B packaging/release/sbom.py /build/payload "$GITHUB_WORKSPACE/dist/SBOM.cdx.json" + cp "$GITHUB_WORKSPACE/dist/SBOM.cdx.json" "$GITHUB_WORKSPACE/dist/ox-llm-$VERSION-linux-$(uname -m).sbom.cdx.json" python3 -B - <<'PY' import os, pathlib, re payload = pathlib.Path('/build/payload') diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1e965ba3b..74836bd93 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,8 +41,15 @@ jobs: version: ${{ needs.version.outputs.version }} package: true + combined: + # the oxc + oxa + oxe archives for aarch64 and x86_64 (cloud-only setup needs no engine weights) + needs: version + uses: ./.github/workflows/release-build.yml + with: + version: ${{ needs.version.outputs.version }} + release: - needs: [version, build] + needs: [version, build, combined] if: github.ref_type == 'tag' runs-on: ubuntu-latest timeout-minutes: 20 @@ -67,6 +74,19 @@ jobs: pattern: dist-* merge-multiple: true path: dist + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: release-build-* + path: combined + - name: Combined archives and SBOMs + run: | + set -euo pipefail + for a in aarch64 x86_64; do + for f in "ox-llm-$TAG-linux-$a.tar.gz" "ox-llm-$TAG-linux-$a.sbom.cdx.json"; do + cp "combined/release-build-$a/$f" "dist/$f" + done + sha256sum "dist/ox-llm-$TAG-linux-$a.tar.gz" | sed 's# dist/# #' > "dist/ox-llm-$TAG-linux-$a.tar.gz.sha256" + done - name: SHA256SUMS working-directory: dist run: | @@ -78,14 +98,17 @@ jobs: test -f "./$p-$TAG-linux-aarch64.tar.gz" || { echo "missing tarball: $p"; exit 1; } done cat ./*.tar.gz.sha256 > SHA256SUMS + sha256sum ./*.sbom.cdx.json | sed 's# \./# #' >> SHA256SUMS rm ./*.tar.gz.sha256 sha256sum -c SHA256SUMS - test "$(wc -l < SHA256SUMS)" -eq "$n" + test "$(wc -l < SHA256SUMS)" -eq "$((n + 2))" cat SHA256SUMS - name: Build provenance uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 with: - subject-path: dist/*.tar.gz + subject-path: | + dist/*.tar.gz + dist/*.sbom.cdx.json - name: Create the GitHub Release env: GH_TOKEN: ${{ github.token }} @@ -106,6 +129,7 @@ jobs: echo "| \`oxe-server-flash-$TAG-linux-aarch64.tar.gz\` | \`oxe-server\` with the Flash engine (\`--engine flash\`) + \`flash-m0\` (expert sidecar) and \`flash-m3\` (selftest); ggml built in the same run from the pinned public sources |" echo "| \`oxi-$TAG-linux-aarch64.tar.gz\` | \`oxi\`: FLUX.2 klein image engine (experimental) |" echo "| \`oxa-$TAG-linux-aarch64.tar.gz\` | \`oxa\`: the coding agent (talks to the engine over its OpenAI-compatible API) |" + echo "| \`ox-llm-$TAG-linux-aarch64.tar.gz\` / \`ox-llm-$TAG-linux-x86_64.tar.gz\` | combined: oxc (controller payload and wrappers), oxa, oxe, oxe-server, licences; with \`.sbom.cdx.json\`. Cloud-only setup: oxc/docs/CLOUD-ONLY-QUICKSTART.md |" echo echo "Verify:" echo @@ -119,5 +143,5 @@ jobs: } > notes.md opts=() case $TAG in *-*) opts+=(--prerelease);; esac - gh release create "$TAG" dist/*.tar.gz dist/SHA256SUMS --repo "$GITHUB_REPOSITORY" \ + gh release create "$TAG" dist/*.tar.gz dist/*.sbom.cdx.json dist/SHA256SUMS --repo "$GITHUB_REPOSITORY" \ --verify-tag --title "ox-llm $TAG" --notes-file notes.md "${opts[@]}" diff --git a/README.md b/README.md index 1f6705015..4643bc979 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ history lives in the client. oxa is the client that is tuned for this engine. Se - **Start here:** [oxc/README.md](oxc/README.md#quick-start) covers engine downloads and verification. The controller bootstrap installs source into an explicit sandbox HOME; see its [installation status and limits](oxc/docs/BOOTSTRAP-INSTALL.md). +- **Prebuilt, any Linux:** each release attaches `ox-llm--linux-aarch64.tar.gz` and + `ox-llm--linux-x86_64.tar.gz` (oxc, oxa, oxe, licences) with SBOMs, `SHA256SUMS` and build provenance. - **Cloud only, no local engine:** [oxc/docs/CLOUD-ONLY-QUICKSTART.md](oxc/docs/CLOUD-ONLY-QUICKSTART.md) (any Linux, x86_64 or arm64; cloud models through oxc, plus oxa). - **Code (installed runtime):** run `oxc code` in a project directory, or press the code button in the widget. Both diff --git a/engine/docs/PORTABILITY-PROBE-ARM2.md b/engine/docs/PORTABILITY-PROBE-ARM2.md new file mode 100644 index 000000000..2a07d952c --- /dev/null +++ b/engine/docs/PORTABILITY-PROBE-ARM2.md @@ -0,0 +1,51 @@ +# Portability probe, step 1: second ARM machine (Apple M2, Asahi-style Linux) + +Issue #293. No product code was changed. Build output stayed in `engine/target` (`CARGO_TARGET_DIR=$PWD/target`). + +## 1. The CPU +- `lscpu`: aarch64, Apple M2 (big.LITTLE): 4 x Blizzard-M2 (cpu0-3, max 2424 MHz) + 4 x Avalanche-M2 (cpu4-7, max 3504 MHz), 1 thread/core, 8 CPUs. L2 20 MiB (2 instances). Kernel 7.2.11 Arch. +- Memory: 15 GiB (MemTotal), about 10 GiB available during the run. Rust 1.99.1, GCC 16.1.1 (README asks for GCC 16). +- Flags present: asimd, asimdhp/fphp (fp16), asimddp (dotprod), asimdfhm (fhm), **i8mm, bf16**, asimdrdm, sha3, sha512, aes, atomics, lrcpc, fcma, jscvt, frint, bti, paca/pacg, ecv. +- Flags absent: **sve, sve2, sme** (the engine builds with `+nosve+nosme`, so it needs none). No hardware RNG flag (`rng`) visible. +- `rustc --print target-cpus` knows `apple-m4/m5` but only reports `native = apple-m2`; there is no `apple-m2` name in this rustc. + +## 2. Build of the real engine (not stub) +Command (as in engine/README "Build from source"): `cd engine; CARGO_BUILD_JOBS=4 cargo build --release -p server -p oxe-cli` -> finished in 44 s (no Flash build: needs the ggml libs from the lab repo; not tried). +- It built **unchanged**, with the repo's `.cargo/config.toml` `target-cpu=oryon-1` (not the CPU of this machine). `kernels-sys/build.rs` uses `-march=armv8.6-a+dotprod+i8mm+fp16+bf16+nosve+nosme`, which this core implements, so nothing refused to build. +- Runtime guard: `crates/server/src/startup_check.rs` (`REQUIRED_CPU_FEATURES`, `cpu_has`) checks i8mm, bf16, dotprod, fp16 at start and exits with a message when one is missing. `crates/model/src/engine.rs:78-83` reports dotprod/i8mm/fp16/bf16/sve. All four are present here, so the guard passes. Fallback when a feature is missing: none; the build is compile-time ARMv8.6 (the README says other cores stop with "Illegal instruction"; the guard now turns that into a message). Not testable here: this CPU has all four. +- Risk: `target-cpu=oryon-1` lets rustc emit instructions of Oryon's feature set (e.g. `rng`, `crc`; Oryon is ARMv8.7-ish). Apple M2 lacks some Oryon-only features, and nothing hit one in the tests below, but this is luck of codegen and not guaranteed. `qwen4exp/build.rs:75` (Flash) hardcodes `-mcpu=oryon-1+rng+crc+nopauth+...`; the `rng` part is not in this CPU's flags, so the Flash build on this machine is a likely failure or SIGILL; it was not built. +- `oxe-server --engine oxe --model /nonexistent.gguf` starts and stops with `tokenizer: No such file or directory`, as expected without a model. + +## 3. Model-free tests and probe +`cd engine; cargo test --release -p kernels-sys -p model -p sched -p tokenizer -p chat -p server` (8 min 19 s, RUST_TEST_THREADS=1 from config): +- chat 29, kernels-sys 25 (6 ignored), model 92 (2 ignored) plus fattn 4, pfattn_kernel 10, pfg_kernel 7, sched 19, tokenizer 6, server 233 (41 ignored, they need the Ornith GGUF) plus startup_check 11, flash_template 5. **0 failed.** The long-context and prefill-attention accuracy tests (vs f64) also pass; their comments already name this Apple M2 as a second target ("lab-arm64 helper"). + +ISA roofline probe: `./target/release/isa-roof --quick --out target/isa-roof.json` (5.9 s; receipt says "NOT Oryon numbers; functional test only"; all 5 GEMM tile self-tests ok). Per core, cpu4 = Avalanche (P core, ~3.2 GHz, all-8-cores figure in parentheses): + +| kernel | cpu0 (E) | cpu4 (P) | all 8 | +|---|---|---|---| +| f32 FMLA.4s GFMA/s | 19.2 | 50.7 | 256 | +| f16 FMLA.8h GFMA/s | 35.9 | 101.2 | 511 | +| f16 FMLA.8h by element | 38.3 | 101.3 | 513 | +| bf16 BFMMLA GFMA/s | 9.6 | 25.3 | 128 | +| int8 SDOT GMAC/s | 76.3 | 202.3 | 1019 | +| int8 SMMLA GMAC/s | 76.5 | 172.3 | 928 | +| tile f16 8x16 elem nofold L1 | 23.7 | 65.8 | 328 | +| tile f16 8x16 LD1R fold32 L1 | 22.5 | 62.6 | 310 | +| tile bf16 8x8 BFMMLA L1 | 9.4 | 24.6 | 125 | +| today fa2 unit L2 | 17.1 | 42.8 | 219 | +| today fa2x unit L2 | 16.1 | 41.8 | 211 | + +PMU cycle counts work: FMLA.8h = 4.0 inst/cycle, BFMMLA 0.5 inst/cycle (the probe's "path A: pfg-f16" hint: f16 FMLA, not BFMMLA, is the fast path here). The probe's Oryon verdict table does not apply. For the Snapdragon target the code comments quote 0.770 TFMA/s for the L1 f16 tile on 12 Oryon cores; this machine's all-core 0.33 TFMA/s on 8 mixed cores is about 0.43 of that. Note: "all 8" includes 4 slow E cores, started together, and the 5 x 10 ms `--quick` samples are short (no sustained/thermal data; fanless MacBook Air). Memory bandwidth was not measured (no model-free bandwidth probe was run; `barrier-bench` was not run either). + +## 4. Model +No weights fetched. The supported models are 35B MoE GGUFs (Ornith 17.4 GB, Qwen3.6 22 GB; `oxc/dependencies/model-artifacts.json`); nothing in that file is under 2 GB except a 10.9 MB item. The 15 GiB RAM cannot hold Ornith 17.4 GB resident (mmap would stream from SSD; only the Flash engine is built for that). No sub-2 GB model that the engine supports is named in the docs. Next step: either use the Flash engine's SSD streaming (needs the Flash build, see above) or have the maintainer name a small qwen35moe-architecture GGUF for a smoke test. Model paths in the tests expect `~/.local/share/ai-stack/models/llm`. + +## 5. Comparison with the Snapdragon target +- **Ran unchanged:** full `oxe-server`/`oxe`/`isa-roof` release build with the repo's own flags; all model-free tests; startup CPU-feature guard; isa-roof with PMU. +- **Had to be switched off:** nothing, for the non-Flash build. Not tried: Flash build (needs external ggml libs; hardcoded `-mcpu=oryon-1+rng...`), NPU/oxn paths (Qualcomm-only), real-model runs. +- **Slower or missing:** about 0.4x of the Oryon L1-tile roof; heterogeneous cores (4 E + 4 P) where the engine's static plan with thread barriers (170-210 per token) and per-core placement assumes 12 equal cores, so E cores will likely stall every barrier; default `--oxe-cpus` should be P cores 4-7 only. 15 GiB vs the laptop's memory. No SVE/SME (not used). +- **Estimate to make it a supported target (about 1-2 weeks, mostly with a model present):** 1 day, make `target-cpu` per-machine (`native` or an `apple-m2` flag set) instead of oryon-1, in `.cargo/config.toml` and `qwen4exp/build.rs`, plus a CI/run check for SIGILL; 2-3 days, thread placement and plan for 4P+4E (P-core-only default, tune barriers/partition); 1-2 days, a small-model smoke/exactness test (needs a small GGUF); 2-4 days, a bandwidth probe, tuning tile choice (fp16 by-element path wins here), and receipts; Flash on this machine, +2-3 days if wanted. Numerics: the byte-exact gates were defined on the Snapdragon build and need re-baselining. + +## 6. Where is the win? (issue comment) +On this CPU, the existing kernels already run at their hardware roof (f16 FMLA 4/cycle, SDOT/SMMLA near peak), so hand tuning is not the open item. The open item is the GPU/accelerator, which this probe cannot measure: the Apple GPU is not reachable through the CPU kernels, and the omarchy-mlx approach was not evaluated here. On this machine the likely wins are the serving layer (oxc: API, cache/echo, cloud routing) over an existing backend, then a GPU backend for decode; CPU kernels need only retuning for placement. x86 GPU was out of scope for this probe. diff --git a/oxa/docs/pages/cli.md b/oxa/docs/pages/cli.md index e6b877048..d17c429da 100644 --- a/oxa/docs/pages/cli.md +++ b/oxa/docs/pages/cli.md @@ -155,7 +155,7 @@ Variables that oxa reads. Variables marked *set* are set by oxa for the bash too | `BRAVE_SEARCH_API_KEY` | Brave Search key for `web_search`; wins over `brave-search.key` ([tools.md](tools.md#web_search)) | | `OXA_CODEMODE_MAX_WORKERS` | script workers alive at once in the process. Default 2; overrides `codemode.maxWorkers` | | `OXA_CLEAR_ON_SHRINK` | `1` clears empty rows when the rendered output shrinks | -| `OXA_SKIN` | `pi` or `oxa`: the look of the interactive UI for this run, over the `ui.skin` setting | +| `OXA_SKIN` | `pi`, `oxa` or `ascii` (ASCII glyphs only): the look of the interactive UI for this run, over the `ui.skin` setting | | `OXA_REDUCED_MOTION` | `1` switches the animations of the `oxa` skin off, as the `ui.reducedMotion` setting does | | `NO_COLOR` | with the `oxa` skin: no colour and no animation | | `OXA_CLIPBOARD_OSC52` | `1` also copies through OSC 52, the escape sequence that terminals relay to the clipboard | diff --git a/oxa/docs/pages/settings.md b/oxa/docs/pages/settings.md index 0ec0f10d9..d6607e3ac 100644 --- a/oxa/docs/pages/settings.md +++ b/oxa/docs/pages/settings.md @@ -46,7 +46,7 @@ Types: `bool`, `number` (whole, not negative), `string`, `list` (of strings), `o | `ui.animationGraphics` | `auto` / `off` | `auto` | `auto` draws the scene as pixel art when the terminal answers the kitty graphics query at start and is kitty or Ghostty (every frame is sent once and deleted again on exit); `off` keeps the scene in text everywhere | | `ui.animationScene` | `graphics` / `stack` / `stars` / `charge` | `null` | the scene of `ui.animation = "arcade"`. Unset: the pixel-art scene where the terminal can draw it, else `stack` (stacking columns). `stack`, `stars` (a ship in a starfield) and `charge` (the ox, in block characters) are the text scenes; `graphics` asks for the pixel-art scene on any terminal that answers the graphics query | | `ui.reducedMotion` | bool | `false` | no animation in the `oxa` skin: the working sprite and the YOLO flames are replaced by the static text badge. The environment variable `OXA_REDUCED_MOTION=1` does the same for one run; `NO_COLOR` and `TERM=dumb` imply it | -| `ui.skin` | `auto` / `pi` / `oxa` | `auto` | the look of the interactive UI: `oxa` is OXA's own (frames, a status line with meters, a key hint bar), `pi` is the plain look, and `auto` is `oxa`. With the `oxa` skin, `NO_COLOR` turns all colour off | +| `ui.skin` | `auto` / `pi` / `oxa` / `ascii` | `auto` | the look of the interactive UI: `oxa` is OXA's own (frames, a status line with meters, a key hint bar), `pi` is the plain look, `ascii` is `oxa` with ASCII glyphs only, and `auto` is `oxa` (ASCII when the locale is not UTF-8 or `TERM` is `linux`/`dumb`). With the `oxa` skin, `NO_COLOR` turns all colour off | | `themes` | list | `[]` | extra theme files or directories | | `tuiMode` | `fullscreen` / `regular` | `fullscreen` | start-up layout | | `quietStartup` | `bool` or `"header"` | `false` | shorter start-up listing; `"header"` shows only the header | diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 7983dd97d..d6089db74 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -1,6 +1,7 @@ # Unattended work `oxa work tasks.json` opts into sequential tasks in separate git worktrees. +Completed tasks are skipped on restart; add `--rerun` to deliberately run them again. Run it inside the project repository. The JSON file is an array: ```json @@ -16,8 +17,8 @@ authenticated `gh` CLI to fetch the issue title and body. Issue lookup is refuse when the environment or user settings enable offline mode. Each task runs a child `oxa -p` with its model. The installed offline policy, -including a parent `--offline` flag, is passed to every child. Normal agent permission, trust and -sandbox rules apply. Configure project trust before unattended use. The runner +including a parent `--offline` flag, is passed to every child. Normal agent +permission, trust and sandbox rules apply. Configure project trust before unattended use. The runner executes the base version of `.oxa/settings.json`'s `done` commands outside the agent after it exits, even without edits. These are trusted project shell commands: invoking `work` authorizes their execution as your user. `in subdirectory: command` @@ -34,20 +35,44 @@ and 2 for invalid input or output errors. A task setup failure produces a blocke JSON line and does not skip later tasks. If a task cannot be locked or already has a receipt, its existing state is preserved; a pre-worktree failure has an empty `worktree` in the result. Locks use kernel ownership, so an abandoned lock file -after process death does not permanently block a task. +after process death does not permanently block a task. A surviving child keeps the +lock until it exits, preventing overlap with a restarted runner. Worktrees and uncommitted changes remain in `.oxa-worktrees//` beside the repository (or the separate git directory). Result receipts live inside the common git directory at `oxa-work/.json`. Worktrees stay outside that git -directory so the normal sandbox can bind git objects and refs for commits. No automatic -cleanup, commit, push or PR creation is implemented yet. +directory so the normal sandbox can bind git objects and refs for commits. +Uncommitted task work, including red work, is saved as a commit with the repository's +configured identity. Commit hooks run normally. A missing identity or rejected commit +blocks the task and retains every file. Unresolved conflicts remain in progress for +the agent; they are never staged as a fabricated resolution. There is no automatic +cleanup, push or PR creation yet. -To continue a task, run the same file again. The retained receipt must match its -ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere -is blocked. Uncommitted changes are retained and blocked until you commit them. +To continue an interrupted, red or blocked task, run the same file again. Use +`--rerun` to continue a completed task after its base changes. The retained receipt +must match its ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere +is blocked. Existing dirty work is saved when possible before a later retry. Merge conflicts are passed to the agent with the merge still in progress, the conflicted file list and merge output. An unfinished merge can be continued on a later run. The agent must finish the merge commit and keep the task branch selected; unresolved or aborted base merges remain blocked even when checks would pass. Inspect retained work before removing it with `git worktree remove`. Review task prompts and bases before running them. + +`oxa work --status` prints one line per recorded task, sorted by ID: + +```text +docs-fix done 1800000000 finished +``` + +Columns are task ID, state, last step-progress time (Unix seconds), and current step. +Running tasks report steps such as `agent`, `acceptance`, `repair` or `saving`. +The state file is replaced atomically with owner-only permissions at each transition. +It records the task specification, captured base/checks, repair output and current +step. Reusing an ID with a different specification is refused; use a new ID. +Restart continues the saved step. An agent or check interrupted within its step +may run again; tasks and checks should tolerate that. Checkpoints after an agent +step avoid replaying it when only acceptance or saving remains. + +Status probes the task lock. An interrupted phase with no surviving worker is shown +as blocked; the checkpoint stays intact for the next task-file invocation. diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index 0ca62ae99..a44e2008b 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -177,7 +177,7 @@ const COMMANDS: &[(&str, &str)] = &[ "Draft a scrubbed ox-llm issue; submit posts it only after you type yes", ), ( - "oxa work | --issue N --model MODEL", + "oxa work [--rerun] | --issue N --model MODEL | --status", "Run tasks in isolated worktrees with external acceptance checks", ), ( diff --git a/oxa/oxa-cli/src/sandbox_layout.rs b/oxa/oxa-cli/src/sandbox_layout.rs index 0fa65de0f..7bc14af1c 100644 --- a/oxa/oxa-cli/src/sandbox_layout.rs +++ b/oxa/oxa-cli/src/sandbox_layout.rs @@ -41,6 +41,38 @@ pub(crate) fn home_confirm_effect() -> String { /// notice when they apply. `confirmed` = the interactive start in `$HOME` itself was confirmed /// (`home_cwd::check`); the user setting [`SETTING_ALLOW_HOME_WRITE`] does the same in every /// mode. Neither ever makes `/` or a parent of `$HOME` writable. +/// True when the binary lives under a writable root (workspace or a writable extra path): +/// a read-only bind there would make `cargo build`/`clean` fail with EBUSY. +fn exe_under_writable(exe: &Path, roots: &[&Path]) -> bool { + let canon = exe.canonicalize().ok(); + roots.iter().any(|r| { + let rc = r.canonicalize().ok(); + exe.starts_with(r) + || canon.as_deref().is_some_and(|k| k.starts_with(r)) + || rc + .as_deref() + .is_some_and(|rc| exe.starts_with(rc) || canon.as_deref().is_some_and(|k| k.starts_with(rc))) + }) +} + +#[cfg(test)] +mod exe_tests { + use super::*; + + #[test] + fn exe_under_cwd_or_writable_extra_is_detected() { + let d = std::env::temp_dir().join(format!("oxa-exe-{}", std::process::id())); + let (w, other) = (d.join("w"), d.join("o")); + std::fs::create_dir_all(w.join("target")).unwrap(); + std::fs::create_dir_all(&other).unwrap(); + let exe = w.join("target/oxa"); + std::fs::write(&exe, b"").unwrap(); + assert!(exe_under_writable(&exe, &[other.as_path(), w.as_path()])); + assert!(!exe_under_writable(&exe, &[other.as_path()])); + std::fs::remove_dir_all(&d).ok(); + } +} + pub(crate) fn layout( env: &Env, dirs: &Dirs, @@ -68,14 +100,22 @@ pub(crate) fn layout( notices.push((Level::Warning, home_root_warning(cwd, home_itself))); } let read_extra = paths(SETTING_READ_PATHS); + let exe = std::env::current_exe().ok(); + let write_extra = paths(SETTING_WRITE_PATHS); + let mut roots: Vec<&Path> = vec![cwd]; + roots.extend(resolved.as_deref()); + roots.extend(write_extra.iter().map(|p| p.as_path())); + let exe_in_workspace = exe.as_deref().is_some_and(|e| exe_under_writable(e, &roots)); let mut built = Layout::build(&LayoutInputs { home: dirs.home.clone(), agent_dir: Some(dirs.agent_dir.clone()), path_var: env.get("PATH").unwrap_or("").to_string(), env: env.pairs().to_vec(), read_extra: read_extra.clone(), - write_extra: paths(SETTING_WRITE_PATHS), + write_extra, allow_home_write, + exe, + exe_in_workspace, }); // #196: extra read-only paths are never silent; say which are mounted and which are not. // A bind that contains the cwd is never mounted (the cwd bind covers it): its siblings diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 4afd4b9d1..6978aa9f6 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -3,6 +3,24 @@ use std::path::{Path, PathBuf}; pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dyn Write) -> i32 { let result = (|| -> Result { + if args == ["--status"] { + for record in oxa_work::status(cwd)? { + let state = serde_json::to_value(record.outcome).map_err(|e| e.to_string())?; + let step = serde_json::to_value(record.step).map_err(|e| e.to_string())?; + writeln!( + out, + "{} {} {} {}", + record.id, + state.as_str().unwrap_or("unknown"), + record.progress_at, + step.as_str().unwrap_or("unknown") + ) + .map_err(|e| e.to_string())?; + } + return Ok(0); + } + let rerun = args.last().is_some_and(|s| s == "--rerun"); + let args = if rerun { &args[..args.len() - 1] } else { args }; let tasks = match args { [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, [flag, number, model_flag, model] if flag == "--issue" && model_flag == "--model" => { @@ -15,12 +33,17 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy model.clone(), )?] } - _ => return Err("usage: oxa work | --issue N --model MODEL".into()), + _ => { + return Err( + "usage: oxa work [--rerun] | --issue N --model MODEL [--rerun] | --status" + .into(), + ) + } }; let binary: PathBuf = std::env::current_exe().map_err(|e| e.to_string())?; let mut code = 0; for task in tasks { - let record = oxa_work::run_task_with_policy(cwd, &binary, &task, oxa_proto::offline()) + let record = oxa_work::run_task_with_options(cwd, &binary, &task, rerun, oxa_proto::offline()) .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); if matches!( record.outcome, diff --git a/oxa/oxa-cli/tests/work_continue.rs b/oxa/oxa-cli/tests/work_continue.rs index bc0c252d4..6e437bdf5 100644 --- a/oxa/oxa-cli/tests/work_continue.rs +++ b/oxa/oxa-cli/tests/work_continue.rs @@ -148,7 +148,7 @@ fn unresolved_merge_can_be_continued_without_resetting_it() { assert!(!merge_head(&env, tree(&second)).exists()); } #[test] -fn dirty_retained_worktree_is_blocked_without_dropping_edits() { +fn completed_work_is_saved_and_not_replayed() { let env = TestEnv::new(); setup(&env, "[]"); trust(&env); @@ -161,8 +161,9 @@ fn dirty_retained_worktree_is_blocked_without_dropping_edits() { ); assert_eq!(first["outcome"], "done", "{first}"); let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); - assert_eq!(record["outcome"], "blocked", "{record}"); + assert_eq!(record["outcome"], "done", "{record}"); assert!(requests.is_empty()); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); assert_eq!( std::fs::read_to_string(tree(&record).join("notes.txt")).unwrap(), "keep me" diff --git a/oxa/oxa-cli/tests/work_state.rs b/oxa/oxa-cli/tests/work_state.rs new file mode 100644 index 000000000..8101df4f5 --- /dev/null +++ b/oxa/oxa-cli/tests/work_state.rs @@ -0,0 +1,290 @@ +//! Durable outcomes through the real runner, fake engine and local bare remote. +#[path = "common/work.rs"] +mod work; +use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +use std::{ + fs, + path::{Path, PathBuf}, + process::{Command, Stdio}, + time::{Duration, Instant}, +}; +use work::{git, run, run_args, run_observed, setup}; +fn project(env: &TestEnv) -> PathBuf { + env.work().join("project") +} +fn receipt(env: &TestEnv) -> PathBuf { + project(env).join(".git/oxa-work/test.json") +} +fn tree(record: &serde_json::Value) -> &Path { + Path::new(record["worktree"].as_str().unwrap()) +} +fn trust(env: &TestEnv) { + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always"}"#, + ); +} +fn write_turn() -> ScriptTurn { + ScriptTurn::tool("write", "write", r#"{"path":"answer.txt","content":"retained"}"#) +} +fn load(env: &TestEnv) -> serde_json::Value { + serde_json::from_slice(&fs::read(receipt(env)).unwrap()).unwrap() +} +fn replace_state(env: &TestEnv, value: &serde_json::Value) { + fs::write(receipt(env), serde_json::to_vec(value).unwrap()).unwrap(); +} +#[test] +fn uncommitted_success_is_saved_as_a_commit() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let record = run(&env, vec![write_turn(), ScriptTurn::text("done")]); + assert_eq!(record["outcome"], "done", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:answer.txt"]), "retained"); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); + use std::os::unix::fs::PermissionsExt; + assert_eq!( + fs::metadata(receipt(&env)).unwrap().permissions().mode() & 0o077, + 0 + ); + assert_eq!(record["step"], "finished"); + assert!(record["progress_at"].as_u64().unwrap() > 0); +} +#[test] +fn red_work_is_saved_too() { + let env = TestEnv::new(); + setup(&env, r#"["printf failed > failure.txt; exit 1"]"#); + let record = run( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("cannot repair")], + ); + assert_eq!(record["outcome"], "red", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:failure.txt"]), "failed"); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); +} +#[test] +fn completed_task_is_skipped_unless_rerun_is_explicit() { + let env = TestEnv::new(); + setup(&env, "[]"); + let first = run(&env, vec![ScriptTurn::text("done")]); + let (again, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(first, again); + assert!(requests.is_empty()); + let repeated = run_args( + &env, + vec![ScriptTurn::text("repeated")], + &["work", "../tasks.json", "--rerun"], + None, + ); + assert_eq!(repeated["outcome"], "nothing_to_do", "{repeated}"); +} +#[test] +fn restart_at_saving_preserves_outcome_without_replaying_agent() { + let env = TestEnv::new(); + setup(&env, "[]"); + let first = run(&env, vec![ScriptTurn::text("done")]); + fs::write(tree(&first).join("recovered.txt"), "recovered").unwrap(); + let mut state = load(&env); + state["step"] = "saving".into(); + state["outcome"] = "red".into(); + state["detail"] = "previous acceptance failed".into(); + replace_state(&env, &state); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "red"); + assert!(requests.is_empty()); + assert_eq!( + git(&env, tree(&record), &["show", "HEAD:recovered.txt"]), + "recovered" + ); +} +#[test] +fn restart_at_acceptance_keeps_captured_checks_and_base() { + let env = TestEnv::new(); + setup(&env, r#"["printf ran >> attempts"]"#); + let first = run(&env, vec![ScriptTurn::text("done")]); + env.write("work/project/.oxa/settings.json", r#"{"done":["exit 7"]}"#); + git(&env, &project(&env), &["add", ".oxa/settings.json"]); + git(&env, &project(&env), &["commit", "-m", "advance base checks"]); + let mut state = load(&env); + state["step"] = "acceptance".into(); + state["outcome"] = "running".into(); + replace_state(&env, &state); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "done", "{record}"); + assert!(requests.is_empty()); + assert_eq!( + fs::read_to_string(tree(&first).join("attempts")).unwrap(), + "ranran" + ); +} +#[test] +fn status_reports_state_progress_time_and_current_step() { + let env = TestEnv::new(); + setup(&env, "[]"); + let record = run(&env, vec![ScriptTurn::text("done")]); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "--status"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + assert!(out.status.success()); + assert_eq!( + String::from_utf8_lossy(&out.stdout).trim(), + format!("test nothing_to_do {} finished", record["progress_at"]) + ); +} +#[test] +fn failed_auto_commit_retains_every_file_and_blocks() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + // A user hook refuses the automatic commit. The runner must not bypass it. + use std::os::unix::fs::PermissionsExt; + let hook = env.write("work/project/.git/hooks/pre-commit", "#!/bin/sh\nexit 1\n"); + fs::set_permissions(hook, fs::Permissions::from_mode(0o755)).unwrap(); + let record = run(&env, vec![write_turn(), ScriptTurn::text("done")]); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert_eq!( + fs::read_to_string(tree(&record).join("answer.txt")).unwrap(), + "retained" + ); + assert!(!git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); +} +fn until(mut predicate: impl FnMut() -> bool) { + let deadline = Instant::now() + Duration::from_secs(15); + while !predicate() { + assert!(Instant::now() < deadline, "timed out"); + std::thread::sleep(Duration::from_millis(10)); + } +} +#[test] +fn child_keeps_lock_after_runner_is_killed_then_task_restarts() { + let env = TestEnv::new(); + setup(&env, "[]"); + let server = FakeServer::start( + vec![ScriptTurn::text("first")], + FakeOpts { + hold_headers_ms: 1500, + ..FakeOpts::default() + }, + ) + .unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs(&server.url())) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + until(|| !server.requests().is_empty()); + assert_eq!(load(&env)["step"], "agent"); + child.kill().unwrap(); + child.wait().unwrap(); + let blocked = run(&env, vec![]); + assert_eq!(blocked["outcome"], "blocked", "{blocked}"); + assert!( + blocked["detail"].as_str().unwrap().contains("already running"), + "{blocked}" + ); + assert_eq!( + load(&env)["step"], + "agent", + "busy retry must not clobber the checkpoint" + ); + let lock_path = project(&env).join(".git/oxa-work/test.lock"); + until(|| { + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .open(&lock_path) + .unwrap(); + file.try_lock().is_ok() + }); + let restarted = run(&env, vec![ScriptTurn::text("resumed")]); + assert_eq!(restarted["outcome"], "nothing_to_do", "{restarted}"); + assert_eq!(restarted["step"], "finished"); +} + +#[test] +fn saving_git_keeps_lock_after_runner_dies_during_commit_hook() { + use std::os::unix::fs::PermissionsExt; + struct Release(PathBuf); + impl Drop for Release { + fn drop(&mut self) { + let _ = fs::write(&self.0, "release"); + } + } + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let release = Release(env.home().join("release-hook")); + let hook = env.write( + "work/project/.git/hooks/pre-commit", + r#"#!/bin/sh +touch "$HOME/hook-started" +attempt=0 +while ! test -f "$HOME/release-hook"; do + attempt=$((attempt + 1)) + if test "$attempt" -ge 500; then exit 9; fi + sleep 0.01 +done +"#, + ); + fs::set_permissions(hook, fs::Permissions::from_mode(0o755)).unwrap(); + let server = + FakeServer::start(vec![write_turn(), ScriptTurn::text("done")], FakeOpts::default()).unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs(&server.url())) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + until(|| env.home().join("hook-started").exists()); + child.kill().unwrap(); + child.wait().unwrap(); + let blocked = run(&env, vec![]); + assert!( + blocked["detail"].as_str().unwrap().contains("already running"), + "{blocked}" + ); + assert_eq!(load(&env)["step"], "saving"); + fs::write(&release.0, "release").unwrap(); + let path = project(&env).join(".git/oxa-work/test.lock"); + until(|| { + let file = fs::OpenOptions::new().read(true).write(true).open(&path).unwrap(); + file.try_lock().is_ok() + }); + let record = run(&env, vec![]); + assert_eq!(record["outcome"], "done", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:answer.txt"]), "retained"); +} +#[test] +fn status_reports_an_unlocked_interrupted_phase_as_blocked_without_mutating_state() { + let env = TestEnv::new(); + setup(&env, "[]"); + run(&env, vec![ScriptTurn::text("done")]); + let mut state = load(&env); + state["step"] = "agent".into(); + state["outcome"] = "running".into(); + replace_state(&env, &state); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "--status"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + let text = String::from_utf8_lossy(&out.stdout); + assert!( + text.starts_with("test blocked ") && text.trim().ends_with("agent"), + "{text}" + ); + assert_eq!(load(&env), state); +} diff --git a/oxa/oxa-config/src/env.rs b/oxa/oxa-config/src/env.rs index 125ca6943..09a0e83cc 100644 --- a/oxa/oxa-config/src/env.rs +++ b/oxa/oxa-config/src/env.rs @@ -188,7 +188,7 @@ pub const ENV_TABLE: &[EnvVar] = &[ EnvVar { name: "OXA_SKIN", pi_name: None, - description: "pi or oxa: the look of the interactive UI for this run (over the ui.skin setting)", + description: "pi, oxa or ascii (ASCII glyphs only): the look of the interactive UI for this run (over the ui.skin setting)", supported: true, }, EnvVar { diff --git a/oxa/oxa-config/src/models.rs b/oxa/oxa-config/src/models.rs index 90d9b2b20..fbe596c21 100644 --- a/oxa/oxa-config/src/models.rs +++ b/oxa/oxa-config/src/models.rs @@ -547,6 +547,48 @@ fn list_hint(available: &[ModelRef]) -> String { } } +fn edit_distance(a: &str, b: &str) -> usize { + let b: Vec = b.chars().collect(); + let mut row: Vec = (0..=b.len()).collect(); + for (i, ca) in a.chars().enumerate() { + let mut prev = row[0]; + row[0] = i + 1; + for (j, cb) in b.iter().enumerate() { + let cur = row[j + 1]; + row[j + 1] = (prev + usize::from(ca != *cb)).min(row[j] + 1).min(cur + 1); + prev = cur; + } + } + row[b.len()] +} + +/// The available id closest to a mistyped pattern: a substring match, or a small edit distance. +fn suggest(pattern: &str, available: &[ModelRef]) -> Option { + let p = pattern.to_lowercase(); + let single = available.windows(2).all(|w| w[0].endpoint == w[1].endpoint); + let name = |m: &ModelRef| if single { m.model.clone() } else { qualified(m) }; + if p.len() >= 3 { + if let Some(m) = available.iter().find(|m| m.model.to_lowercase().contains(&p)) { + return Some(name(m)); + } + } + available + .iter() + .map(|m| { + let id = m.model.to_lowercase(); + let tail = id.rsplit('/').next().unwrap_or(&id); + (edit_distance(&p, &id).min(edit_distance(&p, tail)), m) + }) + .filter(|(d, _)| *d <= 3.max(p.len() / 4)) + .min_by_key(|(d, _)| *d) + .map(|(_, m)| name(m)) +} + fn not_found(pattern: &str, available: &[ModelRef]) -> OxaError { - OxaError::new(codes::NOT_FOUND, format!("model {pattern:?} not found")).with_hint(list_hint(available)) + OxaError::new(codes::NOT_FOUND, format!("model {pattern:?} not found")).with_hint( + match suggest(pattern, available) { + Some(s) => format!("did you mean {s}?\n{}", list_hint(available)), + None => list_hint(available), + }, + ) } diff --git a/oxa/oxa-config/src/schema.rs b/oxa/oxa-config/src/schema.rs index a642cc5cc..50da637d4 100644 --- a/oxa/oxa-config/src/schema.rs +++ b/oxa/oxa-config/src/schema.rs @@ -267,7 +267,7 @@ const SCHEMA: &[KeyDef] = &[ "null", ), key("ui.reducedMotion", Kind::Bool, "false"), - key("ui.skin", Kind::Enum(&["auto", "pi", "oxa"]), "\"auto\""), + key("ui.skin", Kind::Enum(&["auto", "pi", "oxa", "ascii"]), "\"auto\""), verdict_o("warnings.anthropicExtraUsage", Kind::Bool, "true"), verdict_o("websocketConnectTimeoutMs", Kind::Any, "15000"), ]; diff --git a/oxa/oxa-config/tests/b6_model_resolution_env_table_dirs.rs b/oxa/oxa-config/tests/b6_model_resolution_env_table_dirs.rs index a85b00958..dab630368 100644 --- a/oxa/oxa-config/tests/b6_model_resolution_env_table_dirs.rs +++ b/oxa/oxa-config/tests/b6_model_resolution_env_table_dirs.rs @@ -380,3 +380,24 @@ fn not_found_hint_lists_bare_ids_for_a_single_endpoint() { "available: anthropic/claude-haiku-5-5, flash-20b" ); } + +/// #381: a near miss gets a "did you mean" with the id the user can paste back. +#[test] +fn not_found_hint_suggests_the_closest_model() { + let models = vec![ + mr("oxe", "local-default"), + mr("oxe", "anthropic/claude-haiku-5-5"), + ]; + for typo in ["claude-haiku-5-6", "local-defualt"] { + let hint = resolve_model(typo, None, &models) + .unwrap_err() + .hint + .expect("a hint"); + assert!(hint.contains("did you mean"), "{typo}: {hint}"); + } + let hint = resolve_model("local-defualt", None, &models) + .unwrap_err() + .hint + .unwrap(); + assert!(hint.contains("did you mean local-default?"), "{hint}"); +} diff --git a/oxa/oxa-core/src/commands.rs b/oxa/oxa-core/src/commands.rs index e6f0b4bcc..f17163e00 100644 --- a/oxa/oxa-core/src/commands.rs +++ b/oxa/oxa-core/src/commands.rs @@ -67,6 +67,8 @@ const BUILTINS: &[(&str, &str, Option<&str>)] = &[ "Reload keybindings, extensions, skills, prompts, themes, and context files", None, ), + ("help", "List the commands", None), + ("usage", "Show token usage of this session", None), ("quit", "Quit oxa", None), ]; diff --git a/oxa/oxa-core/tests/c5_slash_command_registry.rs b/oxa/oxa-core/tests/c5_slash_command_registry.rs index 42b06e7fc..eb75f2392 100644 --- a/oxa/oxa-core/tests/c5_slash_command_registry.rs +++ b/oxa/oxa-core/tests/c5_slash_command_registry.rs @@ -194,6 +194,8 @@ fn builtins_list() { "compact", "resume", "reload", + "help", + "usage", "quit", ] .iter() diff --git a/oxa/oxa-core/tests/embedded_gateway_loop.rs b/oxa/oxa-core/tests/embedded_gateway_loop.rs new file mode 100644 index 000000000..860c602a7 --- /dev/null +++ b/oxa/oxa-core/tests/embedded_gateway_loop.rs @@ -0,0 +1,244 @@ +//! #289: the real core and read tool using the socket-free Engine adapter. +//! The injected peer replaces provider I/O; it never reads a key or opens a port. +use oxa_config::{Dirs, Settings}; +use oxa_core::{Deps, Open, Session, ToolSelection}; +use oxa_engine::{EmbeddedEngine, EmbeddedGateway, Flow, GatewayContext, GatewayDiscovery, SseEvent}; +use oxa_fake::TestEnv; +use oxa_perm::ModePolicy; +use oxa_proto::{codes, AgentEvent, CancelToken, EntryBody, HeadlessUi, Mode, OxaError, Role, VecSink}; +use oxa_resources::Resources; +use oxa_sandbox::NoSandbox; +use oxa_session::MemStore; +use oxa_tools::ReadTool; +use serde_json::{json, Value}; +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, +}; + +enum Reply { + Tool, + Text(&'static str), + Budget, + FailedSettlement, +} +#[derive(Default)] +struct Calls { + bodies: Vec, + contexts: Vec<(String, String, String)>, +} +struct Gateway { + calls: Arc>, + replies: Mutex>, +} +impl EmbeddedGateway for Gateway { + fn discovery(&self) -> Result { + Ok(GatewayDiscovery { + models: json!({"data":[{"id":"cloud-model", "remote":true, + "context_length":64000, "max_output_tokens":4096}]}), + status: json!({"engine_mode":"none", "model_limits":{"cloud-model":{ + "cap":64000, "max_output":4096, "capabilities":{ + "remote":true,"remote_provider":"openrouter","context_window":64000, + "cancel":true,"echo":false,"cache_hints":false,"prefill":false, + "thinking":false,"vision":false + } + }}}), + }) + } + fn chat( + &self, + body: &[u8], + context: GatewayContext<'_>, + cancel: &CancelToken, + on: &mut dyn FnMut(SseEvent) -> Flow, + ) -> Result<(), OxaError> { + { + let mut calls = self.calls.lock().unwrap(); + calls.bodies.push(serde_json::from_slice(body).unwrap()); + calls.contexts.push(( + context.origin.into(), + context.session.into(), + context.idempotency_key.into(), + )); + } + let reply = self + .replies + .lock() + .unwrap() + .pop_front() + .expect("unexpected gateway replay"); + if matches!(reply, Reply::Budget) { + return Err(OxaError::new(codes::BUDGET_EXHAUSTED, "synthetic daily limit")); + } + let mut events = match reply { + Reply::Tool => vec![ + json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call-read", + "function":{"name":"read","arguments":"{ \"path\": "}}]}}]}), + json!({"choices":[{"delta":{"tool_calls":[{"index":0, + "function":{"arguments":"\"sample.txt\" }"}}]}}]}), + ], + Reply::Text(text) => vec![json!({"choices":[{"delta":{"content":text}}]})], + Reply::FailedSettlement => vec![json!({"choices":[{"delta":{"content":"unsettled answer"}}]})], + Reply::Budget => unreachable!(), + }; + events.push(json!({"choices":[{"delta":{},"finish_reason": + if matches!(reply, Reply::Tool) {"tool_calls"} else {"stop"}}], + "usage":{"prompt_tokens":10,"completion_tokens":4,"total_tokens":14}})); + for event in events + .into_iter() + .map(|v| SseEvent::Data(v.to_string())) + .chain([SseEvent::Done]) + { + if cancel.is_cancelled() || on(event) == Flow::Stop { + return Err(OxaError::new(codes::ABORTED, "request cancelled")); + } + } + if matches!(reply, Reply::FailedSettlement) { + return Err(OxaError::new( + "cloud_ledger_unavailable", + "synthetic settlement failure", + )); + } + Ok(()) + } +} + +fn open(env: &TestEnv, replies: Vec) -> (Session, Arc>) { + let calls = Arc::new(Mutex::new(Calls::default())); + let gateway = Arc::new(Gateway { + calls: calls.clone(), + replies: Mutex::new(replies.into()), + }); + let deps = Deps { + engine: Arc::new(EmbeddedEngine::new(gateway)), + store: Arc::new(MemStore::new()), + tools: vec![Arc::new(ReadTool::new())], + hooks: vec![], + policy: Box::new(ModePolicy::new()), + sandbox: Arc::new(NoSandbox), + settings: Settings::from_values(json!({"oxa":{"envNote":false}}), json!({})), + dirs: Dirs::under(env.root()), + cwd: env.work(), + selection: ToolSelection::default(), + resources: Resources::default(), + model: Some("cloud-model".into()), + thinking: None, + mode: Some(Mode::Workspace), + session_id: None, + jobs: oxa_core::fresh_jobs(), + }; + (Session::open(deps, Open::New).unwrap(), calls) +} + +#[test] +fn a_real_read_tool_loop_replays_raw_calls_and_remote_metadata_in_process() { + let env = TestEnv::new(); + env.write("work/sample.txt", "synthetic file contents\n"); + let (mut session, calls) = open(&env, vec![Reply::Tool, Reply::Text("Done.\n")]); + let (mut sink, mut ui) = (VecSink::default(), HeadlessUi::default()); + session + .prompt("read sample.txt".into(), vec![], &mut sink, &mut ui) + .unwrap(); + let wire = session.inner().transcript.wire(); + assert_eq!( + wire.iter().map(|m| m.role).collect::>(), + vec![Role::User, Role::Assistant, Role::Tool, Role::Assistant] + ); + assert_eq!(wire[1].tool_calls[0].args_raw, "{ \"path\": \"sample.txt\" }"); + assert!(wire[2] + .content + .as_deref() + .unwrap() + .contains("synthetic file contents")); + assert_eq!(wire[3].content.as_deref(), Some("Done.\n")); + assert!(session.state().model_remote); + assert_eq!(session.state().model_provider, Some("openrouter")); + assert!(sink + .0 + .iter() + .any(|e| matches!(e, AgentEvent::ToolExecutionEnd { .. }))); + let calls = calls.lock().unwrap(); + assert_eq!(calls.bodies.len(), 2, "no local prefill or extra engine request"); + let replay = calls.bodies[1]["messages"].as_array().unwrap(); + assert!(replay + .iter() + .any(|m| m["role"] == "tool" && m["tool_call_id"] == "call-read")); + for body in &calls.bodies { + assert!(body.get("cache_prompt").is_none()); + assert!(body.get("chat_template_kwargs").is_none()); + assert_eq!(body["model"], "cloud-model"); + } + assert!(calls + .contexts + .iter() + .all(|(origin, session, key)| origin == "oxa" && !session.is_empty() && !key.is_empty())); + assert_eq!(calls.contexts[0].1, calls.contexts[1].1); + assert_ne!(calls.contexts[0].2, calls.contexts[1].2); +} + +#[test] +fn a_budget_refusal_does_not_retry_or_commit_an_assistant() { + let env = TestEnv::new(); + let (mut session, calls) = open(&env, vec![Reply::Budget]); + let (mut sink, mut ui) = (VecSink::default(), HeadlessUi::default()); + session + .prompt("hello".into(), vec![], &mut sink, &mut ui) + .unwrap(); + assert_eq!(calls.lock().unwrap().bodies.len(), 1); + assert!(session + .inner() + .transcript + .wire() + .iter() + .all(|m| m.role != Role::Assistant)); + assert!(session.inner().tree.entries().iter().any(|entry| matches!( + &entry.body, EntryBody::Shown {text,..} if text.contains("synthetic daily limit") + ))); + assert!(!sink + .0 + .iter() + .any(|event| matches!(event, AgentEvent::AutoRetryStart { .. }))); +} + +#[test] +fn unsettled_stream_text_stays_out_of_history_and_the_next_turn_is_healthy() { + let env = TestEnv::new(); + let (mut session, calls) = open(&env, vec![Reply::FailedSettlement, Reply::Text("healthy answer")]); + let (mut sink, mut ui) = (VecSink::default(), HeadlessUi::default()); + session + .prompt("first".into(), vec![], &mut sink, &mut ui) + .unwrap(); + assert_eq!( + calls.lock().unwrap().bodies.len(), + 1, + "a visible failed stream cannot retry" + ); + assert!(session + .inner() + .transcript + .wire() + .iter() + .all(|m| m.role != Role::Assistant)); + assert!(session.inner().tree.entries().iter().any(|entry| matches!( + &entry.body, EntryBody::Shown {text,..} if text == "unsettled answer" + ))); + session + .prompt("again".into(), vec![], &mut sink, &mut ui) + .unwrap(); + let calls = calls.lock().unwrap(); + assert_eq!(calls.bodies.len(), 2); + assert!(!calls.bodies[1]["messages"] + .to_string() + .contains("unsettled answer")); + assert_eq!( + session + .inner() + .transcript + .wire() + .last() + .unwrap() + .content + .as_deref(), + Some("healthy answer") + ); +} diff --git a/oxa/oxa-engine/README.md b/oxa/oxa-engine/README.md index 83e05ec8d..dbe44f121 100644 --- a/oxa/oxa-engine/README.md +++ b/oxa/oxa-engine/README.md @@ -1,12 +1,13 @@ # oxa-engine -HTTP + SSE client for the oxe engine and the pure request builder. Depends on `oxa-proto`. Used by `oxa-core` and `oxa-cli`; HTTP is the only coupling between oxa and the engine (O-135). +HTTP + SSE client for the oxe engine and the pure request builder. Depends on `oxa-proto`. Used by `oxa-core` and `oxa-cli`; HTTP couples oxa to the local engine (O-135). | Module | Task | Provides | |---|---|---| | `lib.rs` | architect (frozen) | re-exports; `Engine`, `ChatRequest`, `Hints`, `EngineInfo` come from `oxa-proto` | | `http.rs`, `sse.rs` | A1 | `Url`, `http_request`, `SseParser`, `post_sse` | | `chat.rs`, `info.rs`, `hints.rs` | A2 | `build_body`, `ReplyAssembler`, `HttpEngine`, `build_headers`, `new_idem_key`, `fetch_info`, `count_tokens` | +| `embedded.rs` | #289 | `EmbeddedEngine`, the injected `EmbeddedGateway` interface | Contracts - `build_body` / `build_headers` are pure and are the only request builders. Body layout is fixed in the doc comment of `build_body`; W6 fields are always present. @@ -18,3 +19,17 @@ Contracts - Info (F4, F14): capabilities from `/_local/status` `model_limits[limits_for].capabilities` (before the model loads), else `/props` `oxe_hints`; a `/props` 503 is no error; a controller in `state: loading` without a context is `engine_busy`. - `HttpEngine::clear_hint` (EG5): `POST /_local/cache/clear {"scope":"hint","hint":h}`, 404 -> `POST /state-cache/clear`; 409 -> `busy`; other non-2xx -> `engine_http` / `engine_busy`. - Contract tests against the fake: `tests/fit_engine_contract.rs`. + +`EmbeddedEngine::new(Arc)` explicitly opts into in-process +cloud chat. It shares the request builder, discovery parser and reply assembler +with HTTP. The binary supplies a gateway implementation that uses the same +admission, key, budget and ledger pipeline as the daemon. Neither the adapter +nor its caller owns provider credentials or implements those policies again. +No default engine selection changes in this slice. + +Gateway completion includes ledger settlement; a Done event alone cannot make +an assistant turn succeed. Offline and pre-cancelled requests stop before any +gateway call. Mid-stream failures cannot produce a committed turn, and the +adapter never retries. Exact cloud tokenization is unsupported and prompt counts +remain client estimates. `tests/embedded_gateway.rs` exercises the socket-free +interface; wiring the real shared gateway and CLI selection is the next slice. diff --git a/oxa/oxa-engine/src/embedded.rs b/oxa/oxa-engine/src/embedded.rs new file mode 100644 index 000000000..5686f9ee8 --- /dev/null +++ b/oxa/oxa-engine/src/embedded.rs @@ -0,0 +1,200 @@ +//! In-process access to the controller's normalized cloud pipeline (#289). +//! +//! The gateway implementation owns admission, credentials, provider I/O, budget +//! reservations and ledger settlement. This adapter reuses the HTTP engine's +//! request builder and reply assembler; it opens no socket and reads no key. +use crate::{build_body_for, new_idem_key, parse_info_for, Flow, ReplyAssembler, SseEvent}; +use oxa_proto::{ + codes, AssistantTurn, CancelToken, ChatRequest, Engine, EngineInfo, OxaError, StreamEvent, ThinkingLevel, + Value, +}; +use std::{sync::Arc, time::Instant}; + +/// The same public discovery documents served by the daemon. +pub struct GatewayDiscovery { + pub models: Value, + pub status: Value, +} + +/// Caller metadata shared with the daemon's normalized chat entry point. +/// These values are not provider headers or local engine cache hints. +pub struct GatewayContext<'a> { + pub origin: &'static str, + pub session: &'a str, + pub idempotency_key: &'a str, +} + +/// Transport-independent access to one gateway instance. +/// +/// Implementations must use the daemon's admission, key, budget and ledger code, +/// honor `cancel` during blocking work and stop delivery when `on` returns Stop. +/// `Ok(())` means processing and ledger settlement have completed successfully; +/// delivering Done alone must not release a reservation or commit success. +/// Normalized refusal errors retain their oxa classification, including budget +/// exhaustion. No provider-specific translation belongs in the caller. +pub trait EmbeddedGateway: Send + Sync { + fn discovery(&self) -> Result; + fn chat( + &self, + body: &[u8], + context: GatewayContext<'_>, + cancel: &CancelToken, + on: &mut dyn FnMut(SseEvent) -> Flow, + ) -> Result<(), OxaError>; +} + +/// An explicitly constructed cloud-only Engine; the default remains HttpEngine. +/// The shared gateway implementation is injected by the binary's composition. +pub struct EmbeddedEngine { + gateway: Arc, +} + +impl EmbeddedEngine { + pub fn new(gateway: Arc) -> Self { + Self { gateway } + } +} + +fn online() -> Result<(), OxaError> { + if oxa_proto::offline() { + return Err(OxaError::new( + codes::OFFLINE, + "cloud models are unavailable in offline mode", + )); + } + Ok(()) +} +fn aborted() -> OxaError { + OxaError::new(codes::ABORTED, "request cancelled") +} +fn broken(message: impl Into) -> OxaError { + OxaError::new(codes::STREAM_BROKEN, message) +} + +impl Engine for EmbeddedEngine { + fn info(&self, target: Option<&str>) -> Result { + online()?; + let discovery = self.gateway.discovery()?; + let info = parse_info_for("in-process", &discovery.models, &discovery.status, target)?; + if !info.caps.remote { + return Err(OxaError::new( + codes::UNSUPPORTED_IN_OXA, + "the in-process gateway requires a cloud model", + )); + } + Ok(info) + } + + fn count_tokens(&self, _text: &str) -> Result { + online()?; + Err(OxaError::new( + codes::UNSUPPORTED_IN_OXA, + "exact tokenization is unavailable for cloud models", + )) + } + + fn chat( + &self, + request: &ChatRequest, + cancel: &CancelToken, + on: &mut dyn FnMut(StreamEvent), + ) -> Result { + online()?; + if cancel.is_cancelled() { + return Err(aborted()); + } + let mut body = Vec::new(); + build_body_for(request, true, &mut body); + let key = if request.hints.idem_key.is_empty() { + new_idem_key() + } else { + request.hints.idem_key.to_string() + }; + let context = GatewayContext { + origin: "oxa", + session: request.hints.session, + idempotency_key: &key, + }; + let mut assembler = ReplyAssembler::new(); + let started = Instant::now(); + let mut done = false; + let mut emitted = false; + let mut failure = None; + let outcome = self.gateway.chat(&body, context, cancel, &mut |event| { + if failure.is_some() { + return Flow::Stop; + } + if cancel.is_cancelled() { + failure = Some(aborted()); + return Flow::Stop; + } + if done { + failure = Some(broken("the gateway delivered an event after [DONE]")); + return Flow::Stop; + } + match event { + SseEvent::Data(data) => match assembler.chunk(&data) { + Ok(events) => { + for event in events { + if cancel.is_cancelled() { + failure = Some(aborted()); + return Flow::Stop; + } + emitted = true; + on(event); + } + } + Err(error) => { + failure = Some(error); + return Flow::Stop; + } + }, + SseEvent::Comment(comment) => { + let seconds = started.elapsed().as_secs().min(u64::from(u32::MAX)) as u32; + if let Some(event) = assembler.comment(&comment, seconds) { + on(event); + } + } + SseEvent::Done => done = true, + } + Flow::Continue + }); + if cancel.is_cancelled() { + return Err(aborted()); + } + if let Some(error) = failure.or_else(|| outcome.err()) { + // As with HTTP, a refusal after visible reply data is a cut stream; + // it must never invite replay of an already visible tool or answer. + return Err(if emitted && error.code != codes::STREAM_BROKEN { + broken(format!("the reply was cut: {}", error.message)) + } else { + error + }); + } + if !done { + return Err(broken("the gateway closed the stream before [DONE]")); + } + for event in assembler.flush() { + if cancel.is_cancelled() { + return Err(aborted()); + } + on(event); + } + if cancel.is_cancelled() { + return Err(aborted()); + } + let level = if request.thinking { + ThinkingLevel::Medium + } else { + ThinkingLevel::Off + }; + let mut turn = assembler.finish(level)?; + if turn.model.is_empty() { + turn.model = request.model.to_string(); + } + if cancel.is_cancelled() { + return Err(aborted()); + } + Ok(turn) + } +} diff --git a/oxa/oxa-engine/src/lib.rs b/oxa/oxa-engine/src/lib.rs index 68ce2af19..8fde716c8 100644 --- a/oxa/oxa-engine/src/lib.rs +++ b/oxa/oxa-engine/src/lib.rs @@ -18,6 +18,7 @@ #![forbid(unsafe_code)] mod chat; +mod embedded; mod handshake; mod hints; mod http; @@ -25,6 +26,7 @@ mod info; mod sse; pub use chat::{build_body, build_body_for, HttpEngine, ReplyAssembler}; +pub use embedded::{EmbeddedEngine, EmbeddedGateway, GatewayContext, GatewayDiscovery}; pub use handshake::{handshake_warning, NEEDED_CAPABILITIES, PROTOCOL_UNDERSTOOD}; pub use hints::{build_headers, new_idem_key}; pub use http::{http_request, HttpRequest, HttpResponse, Url}; diff --git a/oxa/oxa-engine/tests/embedded_gateway.rs b/oxa/oxa-engine/tests/embedded_gateway.rs new file mode 100644 index 000000000..8da836a14 --- /dev/null +++ b/oxa/oxa-engine/tests/embedded_gateway.rs @@ -0,0 +1,353 @@ +//! Socket-free Engine contracts for #289's in-process adapter. +use oxa_engine::{ + build_body_for, EmbeddedEngine, EmbeddedGateway, Flow, GatewayContext, GatewayDiscovery, SseEvent, +}; +use oxa_proto::{ + codes, CancelToken, ChatRequest, Engine, Finish, FrozenPrefix, Hints, OxaError, StreamEvent, + ThinkingLevel, TurnKind, WireMsg, +}; +use serde_json::{json, Value}; +use std::sync::{Arc, Mutex}; + +// The process-wide network policy is shared only within this test binary. +static POLICY: Mutex<()> = Mutex::new(()); + +#[derive(Default)] +struct Capture { + discovery: usize, + bodies: Vec>, + contexts: Vec<(String, String, String)>, + stopped: bool, +} +struct Script { + capture: Arc>, + events: Vec, + error: Option<&'static str>, +} +impl EmbeddedGateway for Script { + fn discovery(&self) -> Result { + self.capture.lock().unwrap().discovery += 1; + Ok(GatewayDiscovery { + models: json!({"data":[{ + "id":"cloud-model", "remote":true, "context_length":64000, + "max_output_tokens":4096, "remote_provider":"openrouter" + }]}), + status: json!({"engine_mode":"none", "model":"local-model"}), + }) + } + fn chat( + &self, + body: &[u8], + context: GatewayContext<'_>, + _cancel: &CancelToken, + on: &mut dyn FnMut(SseEvent) -> Flow, + ) -> Result<(), OxaError> { + { + let mut capture = self.capture.lock().unwrap(); + capture.bodies.push(body.to_vec()); + capture.contexts.push(( + context.origin.to_string(), + context.session.to_string(), + context.idempotency_key.to_string(), + )); + } + for event in &self.events { + if on(event.clone()) == Flow::Stop { + self.capture.lock().unwrap().stopped = true; + return Ok(()); + } + } + self.error.map_or(Ok(()), |code| { + Err(OxaError::new(code, "synthetic gateway refusal")) + }) + } +} +fn engine(events: Vec, error: Option<&'static str>) -> (EmbeddedEngine, Arc>) { + let capture = Arc::new(Mutex::new(Capture::default())); + let gateway = Script { + capture: capture.clone(), + events, + error, + }; + (EmbeddedEngine::new(Arc::new(gateway)), capture) +} +fn data(value: Value) -> SseEvent { + SseEvent::Data(value.to_string()) +} +fn complete() -> Vec { + vec![ + data(json!({"choices":[{"delta":{"content":"answer"}}]})), + data(json!({"choices":[{"finish_reason":"stop","delta":{}}], + "usage":{"prompt_tokens":7,"completion_tokens":3,"total_tokens":10}})), + SseEvent::Done, + ] +} +fn prefix() -> FrozenPrefix { + FrozenPrefix { + sections: vec![("preamble".into(), "SYS".into())], + tools_json: "[]".into(), + sha256: [0; 32], + } +} +fn request<'a>(prefix: &'a FrozenPrefix, messages: &'a [WireMsg]) -> ChatRequest<'a> { + ChatRequest { + prefix, + messages, + model: "cloud-model", + max_tokens: 64, + thinking: true, + preserve_thinking: true, + sampling: None, + hints: Hints { + session: "synthetic-session", + idem_key: "synthetic-request-id", + kind: TurnKind::Chat, + background: false, + snapshot_keep: true, + }, + } +} + +#[test] +fn discovery_and_chat_reuse_the_existing_cloud_wire_contract() { + let _policy = POLICY.lock().unwrap(); + let (engine, capture) = engine(complete(), None); + let info = engine.info(None).unwrap(); + assert_eq!(info.base_url, "in-process"); + assert_eq!(info.model, "cloud-model"); + assert_eq!(info.n_ctx, 64000); + assert_eq!(info.max_output, 4096); + assert!(info.caps.remote); + assert!(!info.caps.echo); + assert!(!info.caps.cache_hints); + let prefix = prefix(); + let messages = [WireMsg::user("hello")]; + let request = request(&prefix, &messages); + let mut expected = Vec::new(); + build_body_for(&request, true, &mut expected); + let mut events = Vec::new(); + let turn = engine + .chat(&request, &CancelToken::new(), &mut |e| events.push(e)) + .unwrap(); + assert_eq!(turn.model, "cloud-model", "model defaults to the requested alias"); + assert_eq!(turn.msg.content.as_deref(), Some("answer")); + assert_eq!(turn.usage.prompt, 7); + assert_eq!(turn.usage.completion, 3); + assert_eq!(turn.level, ThinkingLevel::Medium); + assert_eq!(turn.finish, Finish::Stop); + assert_eq!(events, vec![StreamEvent::Text("answer".into())]); + let capture = capture.lock().unwrap(); + assert_eq!(capture.bodies, vec![expected]); + assert_eq!( + capture.contexts, + vec![( + "oxa".into(), + "synthetic-session".into(), + "synthetic-request-id".into() + )] + ); +} + +#[test] +fn tool_calls_keep_raw_arguments_and_replay_with_the_tool_result() { + let _policy = POLICY.lock().unwrap(); + let events = vec![ + data( + json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call-1", + "function":{"name":"lookup","arguments":"{ \"name\": "}}]}}]}), + ), + data(json!({"choices":[{"delta":{"tool_calls":[{"index":0, + "function":{"arguments":"\"sample\" }"}}]},"finish_reason":"tool_calls"}], + "usage":{"prompt_tokens":9,"completion_tokens":4,"total_tokens":13}})), + SseEvent::Done, + ]; + let (engine, capture) = engine(events, None); + let prefix = prefix(); + let mut messages = vec![WireMsg::user("lookup sample")]; + let turn = engine + .chat(&request(&prefix, &messages), &CancelToken::new(), &mut |_| {}) + .unwrap(); + assert_eq!(turn.finish, Finish::ToolCalls); + assert_eq!(turn.msg.tool_calls[0].args_raw, "{ \"name\": \"sample\" }"); + messages.push(turn.msg); + messages.push(WireMsg::tool_result("call-1", "found")); + engine + .chat(&request(&prefix, &messages), &CancelToken::new(), &mut |_| {}) + .unwrap(); + let capture = capture.lock().unwrap(); + let replay: Value = serde_json::from_slice(&capture.bodies[1]).unwrap(); + assert_eq!( + replay["messages"][2]["tool_calls"][0]["function"]["arguments"], + "{ \"name\": \"sample\" }" + ); + assert_eq!(replay["messages"][3]["tool_call_id"], "call-1"); + assert_eq!(replay["messages"][3]["content"], "found"); +} + +#[test] +fn incomplete_or_malformed_streams_never_return_an_assistant_turn() { + let _policy = POLICY.lock().unwrap(); + let prefix = prefix(); + for events in [ + vec![], + complete()[..2].to_vec(), + vec![SseEvent::Done], + vec![SseEvent::Data("invalid JSON".into()), SseEvent::Done], + vec![ + data(json!({"choices":[{"finish_reason":"stop"}]})), + SseEvent::Done, + ], + vec![ + data(json!({"usage":{"prompt_tokens":1,"completion_tokens":1}})), + SseEvent::Done, + ], + [complete(), vec![SseEvent::Done]].concat(), + ] { + let (engine, _) = engine(events, None); + let error = engine + .chat(&request(&prefix, &[]), &CancelToken::new(), &mut |_| {}) + .unwrap_err(); + assert_eq!(error.code, codes::STREAM_BROKEN); + } +} + +#[test] +fn gateway_refusals_are_preserved_before_data_and_cut_after_data() { + let _policy = POLICY.lock().unwrap(); + let prefix = prefix(); + let request = request(&prefix, &[]); + for code in [codes::BUDGET_EXHAUSTED, codes::DENIED, codes::ENGINE_DOWN] { + let (engine, capture) = engine(vec![], Some(code)); + assert_eq!( + engine + .chat(&request, &CancelToken::new(), &mut |_| {}) + .unwrap_err() + .code, + code + ); + assert_eq!( + capture.lock().unwrap().bodies.len(), + 1, + "the adapter never retries" + ); + let (engine, _) = self::engine(complete(), Some(code)); + assert_eq!( + engine + .chat(&request, &CancelToken::new(), &mut |_| {}) + .unwrap_err() + .code, + codes::STREAM_BROKEN, + "even Done does not commit success before ledger settlement" + ); + } + // In-stream JSON errors keep ReplyAssembler's existing F3 classification. + // Before-stream budget refusals use the typed gateway error tested above. + let (engine, capture) = engine( + vec![ + data(json!({"error":{ + "code":"budget_exceeded","message":"synthetic daily limit"}})), + SseEvent::Done, + ], + None, + ); + assert_eq!( + engine + .chat(&request, &CancelToken::new(), &mut |_| {}) + .unwrap_err() + .code, + codes::STREAM_BROKEN + ); + assert!(capture.lock().unwrap().stopped); +} + +#[test] +fn cancellation_stops_delivery_and_cannot_commit_a_partial_reply() { + let _policy = POLICY.lock().unwrap(); + let prefix = prefix(); + let request = request(&prefix, &[]); + let (engine, capture) = engine(complete(), None); + let cancel = CancelToken::new(); + cancel.cancel(); + assert_eq!( + engine.chat(&request, &cancel, &mut |_| {}).unwrap_err().code, + codes::ABORTED + ); + assert!(capture.lock().unwrap().bodies.is_empty()); + let cancel = CancelToken::new(); + let mut events = Vec::new(); + let error = engine + .chat(&request, &cancel, &mut |event| { + events.push(event); + cancel.cancel(); + }) + .unwrap_err(); + assert_eq!(error.code, codes::ABORTED); + assert_eq!(events.len(), 1); + assert!(capture.lock().unwrap().stopped); +} + +#[test] +fn cancellation_during_the_final_buffer_flush_never_returns_a_turn() { + let _policy = POLICY.lock().unwrap(); + let prefix = prefix(); + let events = vec![ + data(json!({"choices":[{"delta":{"content":" crate::Skin { let from_env = opts .env .iter() - .find(|(k, v)| k == "OXA_SKIN" && matches!(v.trim(), "pi" | "oxa")) + .find(|(k, v)| k == "OXA_SKIN" && matches!(v.trim(), "pi" | "oxa" | "ascii")) .map(|(_, v)| v.trim().to_string()); crate::skin::resolve_skin( from_env.or_else(|| opts.settings.string("ui.skin")).as_deref(), diff --git a/oxa/oxa-interactive/src/skin/chrome.rs b/oxa/oxa-interactive/src/skin/chrome.rs index d33d6ecbb..0a9693f0c 100644 --- a/oxa/oxa-interactive/src/skin/chrome.rs +++ b/oxa/oxa-interactive/src/skin/chrome.rs @@ -128,12 +128,26 @@ pub fn oxa_hud(st: &StatusModel, width: u16, theme: &Theme, skin: Skin) -> Strin let g = glyphs(skin); let w = width as usize; let parts = HudParts::new(st, g); - for drops in 0..=HUD_DROPS { + for drops in 0..10 { let line = hud_build(&parts, drops, &parts.model, g, theme); if visible_width(&line) <= w { return line; } } + // Still too wide: shorten the model name (down to a readable stub) before the mode and the + // location go, since those say what the session may do and where it runs. + const MIN_MODEL: usize = 10; + for drops in 10..=HUD_DROPS { + let base = visible_width(&hud_build(&parts, drops, "", g, theme)); + let room = w.saturating_sub(base); + if room >= MIN_MODEL.min(visible_width(&parts.model)) { + let short = truncate_to_width(&parts.model, room, "..."); + let line = hud_build(&parts, drops, &short, g, theme); + if visible_width(&line) <= w { + return line; + } + } + } // Everything is dropped and it is still too wide: cut the model name, then the line. let base = visible_width(&hud_build(&parts, HUD_DROPS, "", g, theme)); let room = w.saturating_sub(base); @@ -142,7 +156,7 @@ pub fn oxa_hud(st: &StatusModel, width: u16, theme: &Theme, skin: Skin) -> Strin } /// Number of HUD segments that can be dropped (sec. 4.2 drop order, 1st .. 10th). -const HUD_DROPS: usize = 10; +const HUD_DROPS: usize = 12; /// The HUD's data, cleaned and ready to lay out. Built once per call. struct HudParts { @@ -257,12 +271,21 @@ fn hud_build(p: &HudParts, drops: usize, model: &str, g: &Glyphs, theme: &Theme) Some((pct, fraction, detail)) => { let token = ctx_token(*pct); let cells = if shown(6) { 10 } else { 5 }; - let mut s = format!( - "{} {} {}", - theme.fg("muted", "CTX"), - theme.fg(token, &meter(*fraction, cells, g)), - theme.fg(token, &format!("{pct}%")) - ); + let mut s = if shown(10) { + format!( + "{} {} {}", + theme.fg("muted", "CTX"), + theme.fg(token, &meter(*fraction, cells, g)), + theme.fg(token, &format!("{pct}%")) + ) + } else { + // Narrow: the bar goes before the mode and the location do. + format!( + "{} {}", + theme.fg("muted", "CTX"), + theme.fg(token, &format!("{pct}%")) + ) + }; if shown(4) { s.push_str(&format!(" {}", theme.fg("muted", detail))); } @@ -307,14 +330,14 @@ fn hud_build(p: &HudParts, drops: usize, model: &str, g: &Glyphs, theme: &Theme) } // 8: permission mode. - if shown(10) { + if shown(11) { if let Some(mode) = p.mode { segs.push(theme.fg(mode_token(mode), mode.as_str())); } } // 9: where the model runs (D4b); goes with the mode. - if shown(10) { + if shown(12) { segs.push(if p.is_remote { theme.fg( "accent", diff --git a/oxa/oxa-interactive/src/skin/mod.rs b/oxa/oxa-interactive/src/skin/mod.rs index 62eacaf59..0abcf9666 100644 --- a/oxa/oxa-interactive/src/skin/mod.rs +++ b/oxa/oxa-interactive/src/skin/mod.rs @@ -137,6 +137,9 @@ pub fn glyphs(skin: Skin) -> &'static Glyphs { /// where `auto` is OXA's look. `utf8 == false` (a locale without UTF-8) turns `Oxa` into /// `OxaAscii`. pub fn resolve_skin(setting: Option<&str>, utf8: bool) -> Skin { + if setting.map(str::trim) == Some("ascii") { + return Skin::OxaAscii; + } let oxa = !matches!(setting.map(str::trim), Some("pi")); match (oxa, utf8) { (false, _) => Skin::Pi, @@ -146,17 +149,19 @@ pub fn resolve_skin(setting: Option<&str>, utf8: bool) -> Skin { } /// `true` when the environment's locale is UTF-8: the first set of `LC_ALL`, `LC_CTYPE`, -/// `LANG` names `UTF-8` / `utf8` (any case). No locale at all counts as UTF-8 unless -/// `TERM` is `linux` or `dumb` (pure: `env` is the complete input). +/// `LANG` names `UTF-8` / `utf8` (any case). `TERM` `linux` or `dumb` is never UTF-8 for this purpose; no locale counts as UTF-8 (pure: `env` is the complete input). pub fn env_is_utf8(env: &[(String, String)]) -> bool { let get = |k: &str| { env.iter() .find(|(n, v)| n == k && !v.is_empty()) .map(|(_, v)| v.to_ascii_lowercase()) }; + if matches!(get("TERM").as_deref(), Some("linux") | Some("dumb")) { + return false; + } match get("LC_ALL").or_else(|| get("LC_CTYPE")).or_else(|| get("LANG")) { Some(v) => v.contains("utf-8") || v.contains("utf8"), - None => !matches!(get("TERM").as_deref(), Some("linux") | Some("dumb")), + None => true, } } @@ -297,6 +302,7 @@ mod tests { assert_eq!(resolve_skin(Some("auto"), false), Skin::OxaAscii); assert_eq!(resolve_skin(Some("pi"), true), Skin::Pi); assert_eq!(resolve_skin(Some("oxa"), false), Skin::OxaAscii); + assert_eq!(resolve_skin(Some("ascii"), true), Skin::OxaAscii); } #[test] @@ -390,6 +396,7 @@ mod tests { assert!(env_is_utf8(&e(&[("LANG", "C"), ("LC_ALL", "C.utf8")]))); assert!(env_is_utf8(&e(&[("TERM", "xterm-256color")]))); assert!(!env_is_utf8(&e(&[("TERM", "linux")]))); + assert!(!env_is_utf8(&e(&[("TERM", "dumb"), ("LANG", "en_US.UTF-8")]))); assert!(env_no_color(&e(&[("NO_COLOR", "1")]))); assert!(!env_no_color(&e(&[("NO_COLOR", "")]))); assert!(env_no_color(&e(&[("TERM", "dumb")]))); diff --git a/oxa/oxa-interactive/src/slash.rs b/oxa/oxa-interactive/src/slash.rs index 029816d36..7ceee158c 100644 --- a/oxa/oxa-interactive/src/slash.rs +++ b/oxa/oxa-interactive/src/slash.rs @@ -36,7 +36,7 @@ pub struct SlashCx<'a> { /// Kept in oxa: every command of slash-commands.md that plan sec. 1 keeps, plus `engine`, /// `mode` and `mcp`. Omitted pi commands (`login`, `logout`, `share`) are handled too: they /// answer the fixed `unsupported_in_oxa` notice. -pub const INTERACTIVE_COMMANDS: [&str; 29] = [ +pub const INTERACTIVE_COMMANDS: [&str; 31] = [ "settings", "model", "scoped-models", @@ -66,12 +66,17 @@ pub const INTERACTIVE_COMMANDS: [&str; 29] = [ "login", "logout", "share", + "help", + "usage", ]; /// pi commands oxa omits (verdict O). A hook command, template or skill with the same name /// still runs first, as in `oxa_core::expand_input`. const OMITTED: [&str; 3] = ["login", "logout", "share"]; +/// Names added after pi: a template, skill or extension command with the same name wins. +const OVERRIDABLE: [&str; 2] = ["help", "usage"]; + /// Dispatch one submitted editor line. /// /// * Not starting with `/`, or a `/name` that is not in [`INTERACTIVE_COMMANDS`]: returns @@ -94,7 +99,33 @@ pub fn dispatch_slash(line: &str, cx: &SlashCx) -> Option { let name_len = body.find(char::is_whitespace).unwrap_or(body.len()); let name = &body[..name_len]; let args = body[name_len..].trim(); - if name.is_empty() || !INTERACTIVE_COMMANDS.contains(&name) { + if name.is_empty() { + return None; + } + if !INTERACTIVE_COMMANDS.contains(&name) { + // A template, skill or extension command goes to the core; a path (`/tmp/x`) is text. + // A bare unknown name (`/bogus`) is a typo and is rejected here, without a model call. + // A name followed by text (`/src explain this`) is a prompt and reaches the model, + // so the outcome never depends on the host filesystem. + if name.contains('/') + || name.contains('.') + || !args.is_empty() + || cx.commands.iter().any(|c| c.name == name) + { + return None; + } + return Some(notify( + Level::Error, + format!("Unknown command /{name}. Type /help for the list of commands; add text after a name to send the line as a prompt."), + )); + } + + if OVERRIDABLE.contains(&name) + && cx + .commands + .iter() + .any(|c| c.name == name && c.source != Source::Builtin) + { return None; } @@ -171,7 +202,8 @@ pub fn dispatch_slash(line: &str, cx: &SlashCx) -> Option { AppAction::Session(SessionCmd::SetName(args.to_string())) } } - "session" => AppAction::ShowLines(session_info_lines(cx)), + "session" | "usage" => AppAction::ShowLines(session_info_lines(cx)), + "help" => AppAction::ShowLines(help_lines(cx)), "hotkeys" => AppAction::ShowLines(hotkeys_lines(cx.keymap)), "fork" => AppAction::Open(SelectorKind::ForkPoint), // O-182: `/yolo` toggles, `/yolo on` / `/yolo off` set. @@ -216,6 +248,35 @@ pub fn dispatch_slash(line: &str, cx: &SlashCx) -> Option { Some(action) } +/// The `/help` listing: every command of the session with its description, then the keys hint. +pub fn help_lines(cx: &SlashCx) -> Vec { + let mut out = vec!["Commands:".to_string()]; + let mut names: Vec<(String, String)> = INTERACTIVE_COMMANDS + .iter() + .filter(|n| !OMITTED.contains(n) && **n != "exit") + .map(|n| { + let d = cx.commands.iter().find(|c| c.name == *n); + ( + n.to_string(), + d.map(|c| c.description.clone()).unwrap_or_default(), + ) + }) + .collect(); + for c in cx + .commands + .iter() + .filter(|c| !INTERACTIVE_COMMANDS.contains(&c.name.as_str())) + { + names.push((c.name.clone(), c.description.clone())); + } + for (n, d) in names { + out.push(format!(" /{n:<18} {d}")); + } + out.push(String::new()); + out.push("Keys: /hotkeys lists every key binding.".to_string()); + out +} + /// The `/hotkeys` listing built from the live keymap (user overrides included): grouped as in /// keybindings.md, one `keys description` line per action; unbound actions show `(unbound)`. pub fn hotkeys_lines(keymap: &Keymap) -> Vec { diff --git a/oxa/oxa-interactive/src/views/tools.rs b/oxa/oxa-interactive/src/views/tools.rs index a8621f833..ce507c25d 100644 --- a/oxa/oxa-interactive/src/views/tools.rs +++ b/oxa/oxa-interactive/src/views/tools.rs @@ -283,7 +283,18 @@ pub fn tool_result_lines( "read" => highlight_code(text, language_from_path(path), theme), "write" => { let content = args.get("content").and_then(Value::as_str).unwrap_or(text); - highlight_code(content, language_from_path(path), theme) + // A summary line, then the content as added (`+`) lines, so a write does not + // look like the output of `cat`. + let n = content.lines().count(); + let summary = format!( + "{n} line{}, {} bytes", + if n == 1 { "" } else { "s" }, + content.len() + ); + let diff: String = content.lines().map(|l| format!("+{l}\n")).collect(); + let mut out = vec![theme.dim(&summary)]; + out.extend(render_diff(&diff, cx.width, cx)); + out } "edit" => match ui.get("diff").and_then(Value::as_str) { Some(diff) => render_diff(diff, cx.width, cx), diff --git a/oxa/oxa-interactive/tests/d11_slash_command_handlers.rs b/oxa/oxa-interactive/tests/d11_slash_command_handlers.rs index ae5e8ace3..2677cf655 100644 --- a/oxa/oxa-interactive/tests/d11_slash_command_handlers.rs +++ b/oxa/oxa-interactive/tests/d11_slash_command_handlers.rs @@ -109,7 +109,69 @@ fn every_kept_command_has_handler() { } // Plain text and unknown names are not commands. assert!(fx.run("hello /model").is_none()); - assert!(fx.run("/no-such-command").is_none()); + // A path is not a command. + assert!(fx.run("/tmp/file.txt what is this").is_none()); + // Single-component absolute paths too (existing root entry, or a file name with a dot). + assert!(fx.run("/etc explain this directory").is_none()); + assert!(fx.run("/README.md summarize this file").is_none()); +} + +/// #341: an unknown `/x` is rejected locally; `/help` and `/usage` answer locally. +#[test] +fn unknown_slash_is_local_error_and_help_usage_exist() { + let fx = Fx::new(); + let (level, text) = notice(expect_some("/bogus", fx.run("/bogus"))); + assert_eq!(level, Level::Error); + assert!(text.contains("/bogus") && text.contains("/help"), "{text}"); + match expect_some("/help", fx.run("/help")) { + AppAction::ShowLines(l) => { + assert!(l.iter().any(|x| x.contains("/model"))); + assert!(l.iter().any(|x| x.contains("/hotkeys"))); + } + _ => panic!("/help shows lines"), + } + assert!(matches!( + expect_some("/usage", fx.run("/usage")), + AppAction::ShowLines(_) + )); + // `/cost` shows no cost, so it is not a built-in: it is an unknown command. + let (level, _) = notice(expect_some("/cost", fx.run("/cost"))); + assert_eq!(level, Level::Error); + // A bare dotless unknown name is a typo: rejected locally. + let (level, _) = notice(expect_some("/zz-nope-qq", fx.run("/zz-nope-qq"))); + assert_eq!(level, Level::Error); + // With text after it, it is a prompt (host independent): it reaches the model. + assert!(fx.run("/zz-nope-qq go").is_none()); + assert!(fx.run("/src explain this module").is_none()); + // A template or skill command of the session still goes to the core. + let mut fx = Fx::new(); + fx.commands.push(CommandSpec { + name: "skill:demo".to_string(), + description: "demo".to_string(), + argument_hint: None, + source: Source::Extension("x".to_string()), + }); + assert!(fx.run("/skill:demo").is_none()); + // An extension command named like a name that is not built in reaches the core. + fx.commands.push(CommandSpec { + name: "deploy".to_string(), + description: "d".to_string(), + argument_hint: None, + source: Source::Extension("x".to_string()), + }); + assert!(fx.run("/deploy").is_none()); + // A template or extension named `help` / `usage` overrides the built-in handler. + for n in ["help", "usage"] { + let mut fx = Fx::new(); + fx.commands.retain(|c| c.name != n); + fx.commands.push(CommandSpec { + name: n.to_string(), + description: "mine".to_string(), + argument_hint: None, + source: Source::Extension("x".to_string()), + }); + assert!(fx.run(&format!("/{n} x")).is_none(), "/{n} override"); + } } /// /engine status and load diff --git a/oxa/oxa-interactive/tests/d5_tool_views.rs b/oxa/oxa-interactive/tests/d5_tool_views.rs index 0d26803d8..fe562998f 100644 --- a/oxa/oxa-interactive/tests/d5_tool_views.rs +++ b/oxa/oxa-interactive/tests/d5_tool_views.rs @@ -364,3 +364,20 @@ fn unknown_tool_generic_view() { ] ); } + +#[test] +fn write_result_shows_summary_and_added_lines() { + let theme = Theme::plain(); + let settings = ViewSettings::default(); + let hooks: Vec> = Vec::new(); + let cx = cx(&theme, &settings, &hooks); + let lines = tool_result_lines( + "write", + &json!({"path": "a.txt", "content": "hello\n"}), + "Successfully wrote to a.txt", + false, + &json!({}), + &cx, + ); + assert_eq!(lines, vec!["1 line, 6 bytes", "+hello"]); +} diff --git a/oxa/oxa-interactive/tests/u2_oxa_chrome.rs b/oxa/oxa-interactive/tests/u2_oxa_chrome.rs index a9650ffdf..06119745c 100644 --- a/oxa/oxa-interactive/tests/u2_oxa_chrome.rs +++ b/oxa/oxa-interactive/tests/u2_oxa_chrome.rs @@ -1143,3 +1143,25 @@ fn hud_thinking_label_claude_55_effective_effort() { let line = hud_for("qwen-local", ThinkingLevel::Off); assert!(line.contains("off") && !line.contains("adaptive"), "{line}"); } + +/// #343: at 60 columns a cloud model still shows where it runs and the permission mode. +#[test] +fn hud_keeps_location_at_60_columns() { + let st = StatusModel { + model: "anthropic/claude-haiku-5-5".to_string(), + model_remote: true, + model_provider: Some("anthropic"), + ..full_status() + }; + for width in 60..=70u16 { + let line = plain_hud(&st, width); + assert!( + line.contains("cloud · anthropic"), + "location at {width}: {line:?}" + ); + if width >= 70 { + assert!(line.contains("workspace"), "mode at {width}: {line:?}"); + } + assert!(line.contains("READY"), "state at {width}: {line:?}"); + } +} diff --git a/oxa/oxa-sandbox/src/bwrap.rs b/oxa/oxa-sandbox/src/bwrap.rs index d474ad2bb..522f51da1 100644 --- a/oxa/oxa-sandbox/src/bwrap.rs +++ b/oxa/oxa-sandbox/src/bwrap.rs @@ -196,13 +196,24 @@ pub(crate) fn bwrap_args_homes( // sbx-gitsym: checked again for every command (the session may run long): a git directory // whose protected entries are not plain (`gitdirs::unsafe_reason`) is never read-write, and // then none of them is (no masks either: nothing can be created in a read-only bind). - let git_writable = writable && layout.git.rw.iter().all(|d| unsafe_reason(d).is_none()); + // #322: a read-write git dir that vanished (pruned worktree) is not bound, and then its + // surviving ancestors are read-only too: it could otherwise be recreated with its own + // `commondir`, config or hooks. + let git_writable = writable + && layout + .git + .rw + .iter() + .all(|d| unsafe_reason(d).is_none() && std::fs::metadata(d).is_ok()); let git_rw = if git_writable { "--bind" } else { "--ro-bind" }; for p in layout .git .rw .iter() .filter(|p| !p.starts_with(cwd_path) && !cwd_path.starts_with(p)) + // #322: a git dir removed since the session started (pruned worktree) would make + // bwrap fail for every command. + .filter(|p| std::fs::metadata(p).is_ok()) { let p = p.to_string_lossy(); push(&mut a, &[git_rw, &p, &p]); @@ -565,7 +576,11 @@ pub(crate) fn create_missing_git_files_homes( return; } // sbx-gitsym: nothing is created for git directories that are bound read-only. - let git_writable = layout.git.rw.iter().all(|d| unsafe_reason(d).is_none()); + let git_writable = layout + .git + .rw + .iter() + .all(|d| unsafe_reason(d).is_none() && std::fs::metadata(d).is_ok()); let mut files: Vec = layout .git .protect diff --git a/oxa/oxa-sandbox/src/layout.rs b/oxa/oxa-sandbox/src/layout.rs index 1a4f0ff88..e52dd1447 100644 --- a/oxa/oxa-sandbox/src/layout.rs +++ b/oxa/oxa-sandbox/src/layout.rs @@ -187,6 +187,12 @@ pub struct LayoutInputs { pub read_extra: Vec, pub write_extra: Vec, pub allow_home_write: bool, + /// The running oxa binary: exposed read-only (when under `$HOME`, which is hidden) and + /// named by `OXA_RUNNING_BIN`, so an agent can start the TUI in tmux from inside the sandbox. + pub exe: Option, + /// The binary already lies inside the writable workspace: name it, but do not bind it + /// (a bind makes the file a mount point, so a rebuild could not replace it). + pub exe_in_workspace: bool, } /// `~` / `~/x` -> `$HOME` / `$HOME/x`; other paths unchanged. @@ -239,6 +245,9 @@ impl Layout { } read.extend(TOOLCHAIN_READ.iter().map(|r| i.home.join(r))); read.extend(i.read_extra.iter().cloned()); + if !i.exe_in_workspace { + read.extend(i.exe.iter().cloned()); + } let mut write: Vec = CACHE_WRITE.iter().map(|w| i.home.join(w)).collect(); write.extend(i.write_extra.iter().cloned()); let keep = |p: &PathBuf| Layout::keeps_extra(&i.home, i.agent_dir.as_deref(), p); @@ -295,6 +304,15 @@ impl Layout { env.push((k.to_string(), v.to_string())); } } + if let Some(exe) = i + .exe + .as_ref() + .filter(|e| e.is_absolute() && e.is_file() && !denied(e, &deny)) + { + if !env.iter().any(|(n, _)| n == "OXA_RUNNING_BIN") { + env.push(("OXA_RUNNING_BIN".to_string(), exe.display().to_string())); + } + } env.sort(); env.dedup_by(|a, b| a.0 == b.0); Layout { @@ -323,6 +341,42 @@ impl Layout { mod tests { use super::*; + #[test] + fn the_running_binary_is_bound_and_named() { + let d = std::env::temp_dir().join(format!("oxa-exe-{}", std::process::id())); + let bin = d.join("home/.oxa/bin/oxa"); + std::fs::create_dir_all(bin.parent().unwrap()).unwrap(); + std::fs::write(&bin, "x").unwrap(); + let l = Layout::build(&LayoutInputs { + home: d.join("home"), + exe: Some(bin.clone()), + ..Default::default() + }); + let _ = std::fs::remove_dir_all(&d); + assert!(l.read.contains(&bin), "{:?}", l.read); + assert!(l + .env + .contains(&("OXA_RUNNING_BIN".into(), bin.display().to_string()))); + } + + #[test] + fn a_binary_in_the_workspace_is_named_not_bound() { + let d = std::env::temp_dir().join(format!("oxa-exew-{}", std::process::id())); + let bin = d.join("home/ws/target/release/oxa"); + std::fs::create_dir_all(bin.parent().unwrap()).unwrap(); + std::fs::write(&bin, "x").unwrap(); + let l = Layout::build(&LayoutInputs { + home: d.join("home"), + exe: Some(bin.clone()), + exe_in_workspace: true, + ..Default::default() + }); + let _ = std::fs::remove_dir_all(&d); + assert!(!l.read.contains(&bin), "{:?}", l.read); + assert!(l.env.iter().any(|(k, _)| k == "OXA_RUNNING_BIN")); + assert!(!l.env.iter().any(|(k, _)| k == "OXA_BIN")); + } + #[test] fn secret_names_are_recognised() { for k in [ diff --git a/oxa/oxa-sandbox/tests/git_worktree_binds.rs b/oxa/oxa-sandbox/tests/git_worktree_binds.rs index 7a5e86bcd..cbd7a25d5 100644 --- a/oxa/oxa-sandbox/tests/git_worktree_binds.rs +++ b/oxa/oxa-sandbox/tests/git_worktree_binds.rs @@ -891,3 +891,48 @@ fn issue_103_plain_clone_keeps_hooks_and_config_read_only() { } assert!(!plain.join(".git/hooks/x").exists()); } + +/// #322: a git directory that vanished after the session started (worktree pruned or removed) +/// must not make bwrap fail for every later command. +#[test] +fn vanished_git_dir_is_not_bound() { + let env = TestEnv::new(); + let w = build_worktree(&env); + let b = bwrap_value(&env).with_git_dirs(&w.wt); + let g = w.gitdir.to_string_lossy().into_owned(); + assert!(sandbox_args(&b, &w.wt, true).contains(&g), "bound while present"); + std::fs::remove_dir_all(&w.gitdir).expect("remove gitdir"); + let args = sandbox_args(&b, &w.wt, true); + assert!(!args.contains(&g), "a missing source must not be bound: {args:?}"); +} + +/// #322 review: with the gitdir gone, nothing under the surviving common dir may be writable +/// (the vanished gitdir could be recreated with its own `commondir`/hooks), and a dangling +/// symlink in its place is not bound either. +#[test] +fn vanished_git_dir_leaves_no_writable_bind_to_recreate_it() { + let env = TestEnv::new(); + let w = build_worktree(&env); + let b = bwrap_value(&env).with_git_dirs(&w.wt); + std::fs::remove_dir_all(&w.gitdir).expect("remove gitdir"); + let args = sandbox_args(&b, &w.wt, true); + let common = w.main_git.to_string_lossy().into_owned(); + for (i, x) in args.iter().enumerate() { + if x == "--bind" || x == "--bind-try" { + let src = &args[i + 1]; + assert!( + src != &common && !common.starts_with(&format!("{src}/")) || src == "/", + "writable bind over the common dir: {src}" + ); + } + } + assert!( + args.windows(3) + .any(|t| t[0] == "--ro-bind" && t[1] == common && t[2] == common), + "common dir read-only: {args:?}" + ); + // A dangling symlink in place of the gitdir is not bound. + std::os::unix::fs::symlink(env.root().join("nowhere"), &w.gitdir).expect("symlink"); + let g = w.gitdir.to_string_lossy().into_owned(); + assert!(!sandbox_args(&b, &w.wt, true).contains(&g)); +} diff --git a/oxa/oxa-sandbox/tests/h_sandbox_layout.rs b/oxa/oxa-sandbox/tests/h_sandbox_layout.rs index e63c3b0c3..db542990b 100644 --- a/oxa/oxa-sandbox/tests/h_sandbox_layout.rs +++ b/oxa/oxa-sandbox/tests/h_sandbox_layout.rs @@ -68,6 +68,8 @@ fn machine(env: &TestEnv) -> Layout { read_extra: Vec::new(), write_extra: Vec::new(), allow_home_write: false, + exe: None, + exe_in_workspace: false, }) } @@ -896,6 +898,8 @@ fn mise_cache_and_state_are_writable() { read_extra: Vec::new(), write_extra: Vec::new(), allow_home_write: false, + exe: None, + exe_in_workspace: false, }); assert!(l.write.contains(&home.join(".cache/mise")), "{:?}", l.write); assert!( diff --git a/oxa/oxa-work/src/execute.rs b/oxa/oxa-work/src/execute.rs new file mode 100644 index 000000000..ec272d23a --- /dev/null +++ b/oxa/oxa-work/src/execute.rs @@ -0,0 +1,129 @@ +use super::{acceptance, agent, checked, git, state, tree, Outcome, Result, Task}; +use state::{Checkpoint, State, Step}; +use std::fs::File; +use std::path::Path; + +fn complete(tree: &Path, state: &mut State) -> Result<()> { + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + if let Some(error) = &point.agent_error { + return Err(error.clone()); + } + let head = checked(git(tree, &["rev-parse", "HEAD"]))?; + let dirty = checked(git(tree, &["status", "--porcelain"]))?; + state.record.outcome = if head == point.initial && dirty.is_empty() { + Outcome::NothingToDo + } else { + Outcome::Done + }; + Ok(()) +} +pub(crate) fn execute( + repo: &Path, + binary: &Path, + task: &Task, + path: &Path, + state: &mut State, + lock: &File, +) -> Result<()> { + let tree = state.record.worktree.clone(); + loop { + match state.record.step { + Step::Preparing => { + let prepared = tree::prepare(repo, &tree, task)?; + let commands = tree::done_from_base(&tree, &prepared.base)?; + state.checkpoint = Some(Checkpoint { + initial: prepared.initial, + base: prepared.base, + prompt: prepared.prompt, + commands, + agent_error: None, + failures: String::new(), + }); + state::save(path, state, Step::Agent)?; + } + Step::Agent | Step::Repair => { + tree::validate(repo, &tree, task)?; + let step = state.record.step; + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + let prompt = if step == Step::Repair { + format!( + "{}\n\nExternal acceptance checks failed. Repair once:\n{}", + point.prompt, point.failures + ) + } else { + point.prompt.clone() + }; + let result = agent(binary, &tree, task, &prompt, lock, state.offline); + tree::ensure_merged(&tree, &point.base, &task.branch)?; + state + .checkpoint + .as_mut() + .ok_or("missing task checkpoint")? + .agent_error = result.err(); + state::save( + path, + state, + if step == Step::Agent { + Step::Acceptance + } else { + Step::Recheck + }, + )?; + } + Step::Acceptance | Step::Recheck => { + tree::validate(repo, &tree, task)?; + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + tree::ensure_merged(&tree, &point.base, &task.branch)?; + let failures = acceptance( + &tree.canonicalize().map_err(|e| e.to_string())?, + &point.commands, + lock, + state.offline, + )?; + if failures.is_empty() { + complete(&tree, state)?; + state::save(path, state, Step::Saving)?; + } else if state.record.step == Step::Acceptance { + state + .checkpoint + .as_mut() + .ok_or("missing task checkpoint")? + .failures = failures; + state::save(path, state, Step::Repair)?; + } else { + if let Some(error) = &point.agent_error { + return Err(format!("{error}\nAcceptance checks:\n{failures}")); + } + state.record.outcome = Outcome::Red; + state.record.detail = failures; + state::save(path, state, Step::Saving)?; + } + } + Step::Saving | Step::Finished => return Ok(()), + } + } +} +/// Save all staged, unstaged and untracked files. Conflicts are kept in progress for the agent. +pub(crate) fn save_work(repo: &Path, tree: &Path, task: &Task, lock: &File) -> Result<()> { + if !tree.exists() { + return Ok(()); + } + tree::validate(repo, tree, task)?; + let run = |args: &[&str]| -> Result { + let mut command = git(tree, args); + command.stdin(lock.try_clone().map_err(|e| e.to_string())?); + super::checked_output(command.output().map_err(|e| e.to_string())?) + }; + if tree::merging(tree)? || !run(&["diff", "--name-only", "--diff-filter=U"])?.is_empty() { + return Err("unresolved merge retained in progress; files were not discarded".into()); + } + if run(&["status", "--porcelain"])?.is_empty() { + return Ok(()); + } + run(&["add", "--all"])?; + run(&["commit", "-m", &format!("oxa work: save task {}", task.id)])?; + if !run(&["status", "--porcelain"])?.is_empty() { + return Err("uncommitted changes remain after saving; retained for inspection".into()); + } + Ok(()) +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 96e6f760d..1883032d2 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -1,13 +1,16 @@ //! Opt-in, sequential unattended task execution. Worktrees are retained on every outcome. +mod execute; +mod state; mod tree; use serde::{Deserialize, Serialize}; +pub use state::Step; use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; pub type Result = std::result::Result; -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct Task { pub id: String, @@ -24,6 +27,7 @@ fn default_base() -> String { #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Outcome { + Running, Done, NothingToDo, Red, @@ -37,13 +41,17 @@ pub struct Record { pub worktree: PathBuf, pub outcome: Outcome, pub detail: String, + pub step: Step, + pub progress_at: u64, } fn output(mut command: Command) -> Result { command.stdin(Stdio::null()).output().map_err(|e| e.to_string()) } fn checked(command: Command) -> Result { - let out = output(command)?; + checked_output(output(command)?) +} +fn checked_output(out: Output) -> Result { if !out.status.success() { return Err(String::from_utf8_lossy(&out.stderr).into_owned()); } @@ -112,7 +120,7 @@ pub fn issue_task(repo: &Path, number: u64, model: String) -> Result { }) } -fn acceptance(root: &Path, commands: &[String]) -> Result { +fn acceptance(root: &Path, commands: &[String], lock: &fs::File, offline: bool) -> Result { let mut failures = String::new(); for entry in commands { let (dir, shell) = match entry.strip_prefix("in ").and_then(|s| s.split_once(':')) { @@ -124,8 +132,13 @@ fn acceptance(root: &Path, commands: &[String]) -> Result { return Err("done directory escapes worktree".into()); } let mut c = Command::new("sh"); - c.current_dir(dir).args(["-c", shell]); - let out = output(c)?; + c.current_dir(dir) + .args(["-c", shell]) + .stdin(lock.try_clone().map_err(|e| e.to_string())?); + if offline { + c.env("OXA_OFFLINE", "1"); + } + let out = c.output().map_err(|e| e.to_string())?; if !out.status.success() { failures.push_str(&format!( "{entry}: {}\n{}\n{}\n", @@ -137,15 +150,25 @@ fn acceptance(root: &Path, commands: &[String]) -> Result { } Ok(failures) } -fn agent(binary: &Path, root: &Path, task: &Task, prompt: &str, offline: bool) -> Result<()> { +fn agent( + binary: &Path, + root: &Path, + task: &Task, + prompt: &str, + lock: &fs::File, + offline: bool, +) -> Result<()> { let mut c = Command::new(binary); c.current_dir(root) .args(["-p", "--model", &task.model, "--no-session"]); if offline { c.arg("--offline"); } - c.args(["--", prompt]); - checked(c).map(|_| ()) + // Stdin was null for unattended runs. An empty locked file still supplies EOF, + // and fd 0 keeps the shared kernel lock alive if the runner dies before its child. + c.args(["--", prompt]) + .stdin(lock.try_clone().map_err(|e| e.to_string())?); + checked_output(c.output().map_err(|e| e.to_string())?).map(|_| ()) } pub fn blocked_record(task: &Task, detail: String) -> Record { @@ -155,12 +178,23 @@ pub fn blocked_record(task: &Task, detail: String) -> Record { worktree: PathBuf::new(), outcome: Outcome::Blocked, detail, + step: Step::Finished, + progress_at: state::now(), } } +pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { + run_task_with_options(repo, binary, task, false, offline) +} pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { - run_task_with_policy(repo, binary, task, false) + run_task_with_options(repo, binary, task, false, false) } -pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { +pub fn run_task_with_options( + repo: &Path, + binary: &Path, + task: &Task, + rerun: bool, + offline: bool, +) -> Result { validate(task)?; let root = checked(git(repo, &["rev-parse", "--show-toplevel"]))?; let root = PathBuf::from(root); @@ -173,17 +207,7 @@ pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bo let storage = common.join("oxa-work"); fs::create_dir_all(&storage).map_err(|e| e.to_string())?; let lock_path = storage.join(format!("{}.lock", task.id)); - let lock_file = fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(false) - .open(&lock_path) - .map_err(|e| format!("cannot lock task {}: {e}", task.id))?; - lock_file - .try_lock() - .map_err(|e| format!("task {} is already running: {e}", task.id))?; - // Keep the inode so a crashed runner leaves no stale ownership and concurrent - // runners cannot acquire locks on two different files with the same name. + let lock = state::lock(&lock_path)?; let receipt = storage.join(format!("{}.json", task.id)); // The common git directory must not be an ancestor of the child cwd: the sandbox // deliberately refuses writable git binds with that layout. @@ -199,62 +223,100 @@ pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bo let trees = parent.join(".oxa-worktrees").join(name); fs::create_dir_all(&trees).map_err(|e| e.to_string())?; let tree = trees.join(&task.id); - tree::check_receipt(&receipt, &tree, task)?; - let mut record = Record { - id: task.id.clone(), - branch: task.branch.clone(), - worktree: tree.clone(), - outcome: Outcome::Blocked, - detail: String::new(), + let mut state = if receipt.exists() { + let old = state::read(&receipt)?; + if old.task != *task + || old.record.worktree != tree + || old.record.id != task.id + || old.record.branch != task.branch + { + return Err("task specification changed; use a new task id".into()); + } + if old.record.step != Step::Finished { + old + } else if !rerun && matches!(old.record.outcome, Outcome::Done | Outcome::NothingToDo) { + return Ok(old.record); + } else { + new_state(task, tree, offline || old.offline) + } + } else { + new_state(task, tree, offline) }; - let result = (|| -> Result { - let prepared = tree::prepare(&root, &tree, task)?; - let tree = tree.canonicalize().map_err(|e| e.to_string())?; - let commands = tree::done_from_base(&tree, &prepared.base)?; - let first_agent = agent(binary, &tree, task, &prepared.prompt, offline); - tree::ensure_merged(&tree, &prepared.base, &task.branch)?; - let failures = acceptance(&tree, &commands)?; - if !failures.is_empty() { - let repair_agent = agent( - binary, - &tree, - task, - &format!( - "{}\n\nExternal acceptance checks failed. Repair once:\n{failures}", - prepared.prompt - ), - offline, - ); - tree::ensure_merged(&tree, &prepared.base, &task.branch)?; - let failures = acceptance(&tree, &commands)?; - if !failures.is_empty() { - if let Err(error) = repair_agent { - return Err(format!("{error}\nAcceptance checks:\n{failures}")); + state.offline |= offline; + let step = state.record.step; + state::save(&receipt, &mut state, step)?; + if let Err(error) = execute::execute(&root, binary, task, &receipt, &mut state, &lock) { + state.record.outcome = Outcome::Blocked; + state.record.detail = error; + } + state::save(&receipt, &mut state, Step::Saving)?; + if let Err(error) = execute::save_work(&root, &state.record.worktree, task, &lock) { + state.record.outcome = Outcome::Blocked; + if !state.record.detail.is_empty() { + state.record.detail.push('\n'); + } + state.record.detail.push_str(&format!("save: {error}")); + } + state::save(&receipt, &mut state, Step::Finished)?; + Ok(state.record) +} +fn new_state(task: &Task, worktree: PathBuf, offline: bool) -> state::State { + state::State { + record: Record { + id: task.id.clone(), + branch: task.branch.clone(), + worktree, + outcome: Outcome::Running, + detail: String::new(), + step: Step::Preparing, + progress_at: state::now(), + }, + task: task.clone(), + checkpoint: None, + offline, + } +} +pub fn status(repo: &Path) -> Result> { + let common = checked(git( + repo, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?; + let directory = PathBuf::from(common).join("oxa-work"); + if !directory.exists() { + return Ok(vec![]); + } + let mut records = vec![]; + for entry in fs::read_dir(directory).map_err(|e| e.to_string())? { + let path = entry.map_err(|e| e.to_string())?.path(); + if path.extension().is_some_and(|s| s == "json") { + let mut value = state::read(&path)?; + if value.record.step != Step::Finished { + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .open(path.with_extension("lock")); + let busy = match file { + Ok(file) => match file.try_lock() { + Ok(()) => { + value = state::read(&path)?; + false + } + Err(std::fs::TryLockError::WouldBlock) => true, + Err(error) => return Err(error.to_string()), + }, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => false, + Err(error) => return Err(error.to_string()), + }; + if value.record.step != Step::Finished { + value.record.outcome = if busy { Outcome::Running } else { Outcome::Blocked }; + if !busy { + value.record.detail = "task was interrupted; rerun its task file to resume".into(); + } } - record.detail = failures; - return Ok(Outcome::Red); } - repair_agent?; - } else { - first_agent?; + records.push(value.record); } - let head = checked(git(&tree, &["rev-parse", "HEAD"]))?; - let dirty = checked(git(&tree, &["status", "--porcelain"]))?; - Ok(if head == prepared.initial && dirty.is_empty() { - Outcome::NothingToDo - } else { - Outcome::Done - }) - })(); - match result { - Ok(outcome) => record.outcome = outcome, - Err(e) => record.detail = e, } - // Separate receipts avoid overwriting another task's result. Never remove its worktree. - fs::write( - receipt, - serde_json::to_vec_pretty(&record).map_err(|e| e.to_string())?, - ) - .map_err(|e| e.to_string())?; - Ok(record) + records.sort_by(|a, b| a.id.cmp(&b.id)); + Ok(records) } diff --git a/oxa/oxa-work/src/state.rs b/oxa/oxa-work/src/state.rs new file mode 100644 index 000000000..ff0b433e9 --- /dev/null +++ b/oxa/oxa-work/src/state.rs @@ -0,0 +1,94 @@ +//! Durable step checkpoints and a kernel lock that recovers after process death. +use super::{Record, Result, Task}; +use serde::{Deserialize, Serialize}; +use std::fs::{self, File, OpenOptions}; +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::Path; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum Step { + Preparing, + Agent, + Acceptance, + Repair, + Recheck, + Saving, + Finished, +} + +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct Checkpoint { + pub initial: String, + pub base: String, + pub prompt: String, + pub commands: Vec, + pub agent_error: Option, + pub failures: String, +} +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct State { + #[serde(flatten)] + pub record: Record, + pub task: Task, + pub checkpoint: Option, + pub offline: bool, +} +pub(crate) fn now() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +} +pub(crate) fn lock(path: &Path) -> Result { + let file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .mode(0o600) + .open(path) + .map_err(|e| e.to_string())?; + file.try_lock() + .map_err(|e| format!("task is already running: {e}"))?; + file.set_len(0).map_err(|e| e.to_string())?; + // Keep the inode: removing a lock file permits two processes to lock different inodes. + Ok(file) +} +pub(crate) fn read(path: &Path) -> Result { + serde_json::from_slice(&fs::read(path).map_err(|e| e.to_string())?) + .map_err(|e| format!("task state: {e}")) +} +pub(crate) fn save(path: &Path, state: &mut State, step: Step) -> Result<()> { + state.record.step = step; + state.record.progress_at = now(); + let temporary = path.with_extension(format!( + "tmp-{}-{}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() + )); + let result = (|| -> Result<()> { + let mut file = OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(&temporary) + .map_err(|e| e.to_string())?; + file.write_all(&serde_json::to_vec_pretty(state).map_err(|e| e.to_string())?) + .map_err(|e| e.to_string())?; + file.sync_all().map_err(|e| e.to_string())?; + fs::rename(&temporary, path).map_err(|e| e.to_string())?; + File::open(path.parent().ok_or("state has no parent")?) + .and_then(|p| p.sync_all()) + .map_err(|e| e.to_string()) + })(); + if result.is_err() { + let _ = fs::remove_file(temporary); + } + result +} diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs index 122fe61a7..341282286 100644 --- a/oxa/oxa-work/src/tree.rs +++ b/oxa/oxa-work/src/tree.rs @@ -1,6 +1,5 @@ //! Worktree continuation uses ordinary merges; no reset, stash or force checkout. use super::{checked, git, output, Result, Task}; -use std::fs; use std::path::{Path, PathBuf}; pub(crate) struct Prepared { @@ -15,7 +14,7 @@ fn git_path(tree: &Path, name: &str) -> Result { )) .map(PathBuf::from) } -fn merging(tree: &Path) -> Result { +pub(crate) fn merging(tree: &Path) -> Result { Ok(git_path(tree, "MERGE_HEAD")?.exists()) } fn merge_prompt(tree: &Path, task: &Task, base: &str, output: &str) -> Result { @@ -45,23 +44,7 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result _ => return Err(String::from_utf8_lossy(&exists.stderr).into()), } } - let canonical = tree.canonicalize().map_err(|e| e.to_string())?; - let actual = PathBuf::from(checked(git(tree, &["rev-parse", "--show-toplevel"]))?); - let common = |p: &Path| -> Result { - PathBuf::from(checked(git( - p, - &["rev-parse", "--path-format=absolute", "--git-common-dir"], - ))?) - .canonicalize() - .map_err(|e| e.to_string()) - }; - if actual.canonicalize().map_err(|e| e.to_string())? != canonical || common(tree)? != common(repo)? { - return Err("retained directory is not this repository's task worktree".into()); - } - let branch = checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))?; - if branch != format!("refs/heads/{}", task.branch) { - return Err("retained worktree is on a different branch".into()); - } + validate(repo, tree, task)?; for operation in ["rebase-merge", "rebase-apply", "CHERRY_PICK_HEAD", "REVERT_HEAD"] { if git_path(tree, operation)?.exists() { return Err(format!("retained worktree has {operation} in progress")); @@ -109,6 +92,26 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result prompt: task.prompt.clone(), }) } +pub(crate) fn validate(repo: &Path, tree: &Path, task: &Task) -> Result<()> { + let canonical = tree.canonicalize().map_err(|e| e.to_string())?; + let actual = PathBuf::from(checked(git(tree, &["rev-parse", "--show-toplevel"]))?); + let common = |p: &Path| -> Result { + PathBuf::from(checked(git( + p, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?) + .canonicalize() + .map_err(|e| e.to_string()) + }; + if actual.canonicalize().map_err(|e| e.to_string())? != canonical || common(tree)? != common(repo)? { + return Err("retained directory is not this repository's task worktree".into()); + } + let branch = checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))?; + if branch != format!("refs/heads/{}", task.branch) { + return Err("retained worktree is on a different branch".into()); + } + Ok(()) +} pub(crate) fn ensure_merged(tree: &Path, base: &str, branch: &str) -> Result<()> { if checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))? != format!("refs/heads/{branch}") { return Err("agent left the worktree on a different branch".into()); @@ -137,13 +140,3 @@ pub(crate) fn done_from_base(repo: &Path, base: &str) -> Result> { Some(value) => serde_json::from_value(value.clone()).map_err(|e| format!("done commands: {e}")), } } -pub(crate) fn check_receipt(receipt: &Path, tree: &Path, task: &Task) -> Result<()> { - if receipt.exists() { - let record: super::Record = serde_json::from_slice(&fs::read(receipt).map_err(|e| e.to_string())?) - .map_err(|e| format!("retained receipt: {e}"))?; - if record.id != task.id || record.branch != task.branch || record.worktree != tree { - return Err("retained receipt belongs to a different task branch or worktree".into()); - } - } - Ok(()) -} diff --git a/oxc-rs/ANTHROPIC_STREAM.md b/oxc-rs/ANTHROPIC_STREAM.md new file mode 100644 index 000000000..8db813ee4 --- /dev/null +++ b/oxc-rs/ANTHROPIC_STREAM.md @@ -0,0 +1,36 @@ +# Anthropic event translation (#334, slice 2) + +Enable `upstream-anthropic-stream` to use `anthropic_stream::Stream`. It accepts +decoded Messages events and returns OpenAI chunk JSON values in order. The caller +supplies the timestamp. SSE framing, transport and accounting remain the gateway's +responsibility; no network, key or clock access occurs here. Production callers use +`Stream::with_redactor` with the gateway's `Redactor` adapter. Its feed/finish hooks +keep independent hold-back state for text, thinking and each tool's arguments; +atomic tool names and IDs use `whole`, never a hold-back buffer. Held tails flush +before the finish chunk. Completed.assistant contains the same safe text as the +chunks, while native signed blocks stay unmodified for private replay. `Stream::new` +uses Identity and is only for callers without sensitive data and unredacted tests. + +At EOF, call `finish()`. Missing message_stop, provider errors and malformed event +sequences fail with fixed errors; they never yield a trusted usage record. Only a +valid output_tokens count in message_delta itself followed by message_stop produces +final usage; empty/cache-only terminal usage never reuses an initial estimate. Unlike +the Python wire, an initial-only usage estimate is never emitted as final usage. +Input, cache-read and cache-write tokens remain separate for correct pricing (#283). +Completed native blocks include thinking signatures and redacted data for the +caller's exact-prefix replay. Never log/persist opaque blocks as diagnostics. + +Synthetic Python fixtures feed mock events directly into `_Stream`, with sockets +forbidden, a cleared environment and private HOME. Rust compares canonical JSON +bytes for chunk arrays, and exact native block/usage values. All stop reasons, Unicode, +thinking, tools, partial JSON, invalid usage and usage opt-out are covered. Repeated +signature deltas replace the prior signature exactly as Python does. Synthetic +marker splits and held tails are compared against Python's stateful redactor too. + +```sh +python3 -I oxc-rs/tests/fixtures/record_anthropic_streams.py --check +cargo test --locked --manifest-path oxc-rs/Cargo.toml --features upstream-anthropic-stream +cargo check --locked --manifest-path oxc-rs/Cargo.toml --no-default-features +cargo clippy --locked --manifest-path oxc-rs/Cargo.toml --all-features --all-targets -- -D warnings +cargo fmt --manifest-path oxc-rs/Cargo.toml --all -- --check +``` diff --git a/oxc-rs/Cargo.toml b/oxc-rs/Cargo.toml index 597155928..92bf5a2a9 100644 --- a/oxc-rs/Cargo.toml +++ b/oxc-rs/Cargo.toml @@ -9,6 +9,7 @@ autobins = false [features] default = [] native-controller = [] +upstream-anthropic-stream = ["dep:serde_json"] upstream-anthropic-request = ["dep:serde_json"] [dependencies] diff --git a/oxc-rs/src/anthropic_stream.rs b/oxc-rs/src/anthropic_stream.rs new file mode 100644 index 000000000..3aa51a921 --- /dev/null +++ b/oxc-rs/src/anthropic_stream.rs @@ -0,0 +1,454 @@ +//! Parsed Anthropic Messages events -> OpenAI chunks and a terminal usage record. +//! SSE framing, transport, redactor implementation and accounting belong to the gateway. +use serde_json::{json, Map, Value}; +use std::collections::BTreeMap; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct StreamError; + +impl std::fmt::Display for StreamError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("Invalid or incomplete Anthropic stream") + } +} + +impl std::error::Error for StreamError {} + +/// Raw billing categories stay separate. Cache writes must never be priced as reads. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Usage { + pub input_tokens: u64, + pub output_tokens: u64, + pub cache_read_input_tokens: u64, + pub cache_creation_input_tokens: u64, +} + +impl Usage { + pub fn from_value(value: &Value) -> Option { + let usage = Self { + input_tokens: value["input_tokens"].as_u64()?, + output_tokens: value["output_tokens"].as_u64()?, + cache_read_input_tokens: value + .get("cache_read_input_tokens") + .map_or(Some(0), Value::as_u64)?, + cache_creation_input_tokens: value + .get("cache_creation_input_tokens") + .map_or(Some(0), Value::as_u64)?, + }; + usage + .input_tokens + .checked_add(usage.cache_read_input_tokens)? + .checked_add(usage.cache_creation_input_tokens)? + .checked_add(usage.output_tokens)?; + Some(usage) + } + + pub fn openai(self) -> Value { + let prompt = + self.input_tokens + self.cache_read_input_tokens + self.cache_creation_input_tokens; + json!({"prompt_tokens":prompt, "completion_tokens":self.output_tokens, + "total_tokens":prompt + self.output_tokens, + "prompt_tokens_details":{"cached_tokens":self.cache_read_input_tokens, + "cache_write_tokens":self.cache_creation_input_tokens}, + "completion_tokens_details":{"reasoning_tokens":0}}) + } +} + +pub fn finish_reason(reason: &str) -> &'static str { + match reason { + "max_tokens" | "model_context_window_exceeded" => "length", + "tool_use" => "tool_calls", + "refusal" => "content_filter", + _ => "stop", + } +} + +/// Available only after a complete, non-error stream. Native blocks can be kept +/// by the caller for exact-prefix thinking replay; they must not be logged. +pub struct Completed { + pub assistant: Value, + pub native_blocks: Vec, + pub usage: Option, + pub finish_reason: &'static str, +} + +struct Block { + native: Value, + slot: Option, + arguments: String, + safe_arguments: String, + safe_id: String, + safe_name: String, + closed: bool, +} + +/// Each visible field has independent hold-back state; tool argument streams are +/// keyed by their stable OpenAI slot, never mixed with names or another call. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Field { + Content, + Thinking, + ToolArguments(usize), +} + +/// Inject the gateway's streaming redactor without passing credentials here. +/// `feed` may withhold partial matches; `finish` releases a field's remaining tail. +/// `whole` redacts atomic tool names/IDs in place, never with hold-back buffering. +pub trait Redactor { + fn feed(&mut self, field: Field, fragment: &str) -> String; + fn finish(&mut self, field: Field) -> String; + fn whole(&mut self, value: &str) -> String; +} + +/// Explicit identity transform for callers without sensitive data and parity tests. +pub struct Identity; +impl Redactor for Identity { + fn feed(&mut self, _: Field, fragment: &str) -> String { + fragment.to_owned() + } + fn finish(&mut self, _: Field) -> String { + String::new() + } + fn whole(&mut self, value: &str) -> String { + value.to_owned() + } +} + +pub struct Stream { + redactor: R, + model: String, + created: u64, + include_usage: bool, + started: bool, + stopped: bool, + failed: bool, + finish: Option<&'static str>, + raw_usage: Map, + usage_final: bool, + blocks: BTreeMap, + next_slot: usize, + text: String, + reasoning: String, +} + +impl Stream { + /// The caller supplies one timestamp so the translator needs no clock or IO. + pub fn new(model: impl Into, created: u64, include_usage: bool) -> Self { + Self::with_redactor(model, created, include_usage, Identity) + } +} + +impl Stream { + /// Production gateways supply their stateful redactor here. Completed.assistant + /// is assembled from the same safe fragments as emitted chunks. Native blocks + /// stay opaque/unmodified and must remain inside the exact-prefix private store. + pub fn with_redactor( + model: impl Into, + created: u64, + include_usage: bool, + redactor: R, + ) -> Self { + Self { + redactor, + model: model.into(), + created, + include_usage, + started: false, + stopped: false, + failed: false, + finish: None, + raw_usage: Map::new(), + usage_final: false, + blocks: BTreeMap::new(), + next_slot: 0, + text: String::new(), + reasoning: String::new(), + } + } + + fn chunk(&self, delta: Value, finish: Option<&str>) -> Value { + json!({"id":"chatcmpl_anthropic", "object":"chat.completion.chunk", "created":self.created, + "model":self.model, "choices":[{"index":0, "delta":delta, "finish_reason":finish}]}) + } + + fn usage(&self) -> Option { + self.usage_final + .then(|| Usage::from_value(&Value::Object(self.raw_usage.clone()))) + .flatten() + } + + fn merge_usage(&mut self, value: &Value) -> Result<(), StreamError> { + self.raw_usage + .extend(value.as_object().ok_or(StreamError)?.clone()); + Ok(()) + } + + /// Translate one decoded event. Once rejected, the stream stays failed. + pub fn event(&mut self, value: &Value) -> Result, StreamError> { + if self.failed { + return Err(StreamError); + } + if self.stopped { + return Ok(Vec::new()); + } + let result = self.translate(value); + if result.is_err() { + self.failed = true; + } + result + } + + fn translate(&mut self, value: &Value) -> Result, StreamError> { + let kind = value["type"].as_str().ok_or(StreamError)?; + if kind == "error" { + return Err(StreamError); + } + if kind == "ping" { + return Ok(Vec::new()); + } + if kind == "message_start" { + if self.started { + return Err(StreamError); + } + self.started = true; + if let Some(usage) = value["message"].get("usage") { + self.merge_usage(usage)?; + } + return Ok(vec![ + self.chunk(json!({"role":"assistant", "content":null}), None) + ]); + } + if !self.started { + return Err(StreamError); + } + let mut out = Vec::new(); + match kind { + "content_block_start" => { + let index = value["index"].as_u64().ok_or(StreamError)?; + if self.blocks.contains_key(&index) { + return Err(StreamError); + } + let mut native = value["content_block"] + .as_object() + .ok_or(StreamError)? + .clone(); + let (mut safe_id, mut safe_name) = (String::new(), String::new()); + let slot = match native.get("type").and_then(Value::as_str) { + Some("tool_use") => { + let slot = self.next_slot; + self.next_slot += 1; + let id = native.entry("id").or_insert(json!("toolu_unknown")); + if !id.is_string() { + return Err(StreamError); + } + let name = native.entry("name").or_insert(json!("tool")); + if !name.is_string() { + return Err(StreamError); + } + native.entry("input").or_insert(json!({})); + safe_id = self + .redactor + .whole(native["id"].as_str().ok_or(StreamError)?); + safe_name = self + .redactor + .whole(native["name"].as_str().ok_or(StreamError)?); + out.push(self.chunk( + json!({"tool_calls":[{"index":slot, "id":safe_id, + "type":"function", "function":{"name":safe_name, "arguments":""}}]}), + None, + )); + Some(slot) + } + Some("thinking") => { + native.entry("thinking").or_insert(json!("")); + native.entry("signature").or_insert(json!("")); + None + } + Some("text") => { + native.entry("text").or_insert(json!("")); + None + } + Some(_) => None, + None => return Err(StreamError), + }; + self.blocks.insert( + index, + Block { + native: Value::Object(native), + slot, + arguments: String::new(), + safe_arguments: String::new(), + safe_id, + safe_name, + closed: false, + }, + ); + } + "content_block_delta" => { + let index = value["index"].as_u64().ok_or(StreamError)?; + let block = self + .blocks + .get_mut(&index) + .filter(|b| !b.closed) + .ok_or(StreamError)?; + let delta = &value["delta"]; + let field = match delta["type"].as_str() { + Some("text_delta") => Some(("text", "content")), + Some("thinking_delta") => Some(("thinking", "reasoning_content")), + Some("signature_delta") => Some(("signature", "")), + Some("input_json_delta") => Some(("partial_json", "tool_calls")), + _ => None, + }; + if let Some((field, wire)) = field { + let text = delta[field].as_str().ok_or(StreamError)?; + if field == "partial_json" { + let slot = block.slot.ok_or(StreamError)?; + block.arguments.push_str(text); + let safe = self.redactor.feed(Field::ToolArguments(slot), text); + block.safe_arguments.push_str(&safe); + if !safe.is_empty() { + out.push(self.chunk( + json!({"tool_calls":[{"index":slot, + "function":{"arguments":safe}}]}), + None, + )); + } + } else { + if block.native["type"] != if field == "text" { "text" } else { "thinking" } + { + return Err(StreamError); + } + if field == "signature" { + // Match the reference's opaque replacement semantics. + block.native[field] = json!(text); + return Ok(out); + } + let existing = block.native[field].as_str().ok_or(StreamError)?; + block.native[field] = json!(format!("{existing}{text}")); + let safe = self.redactor.feed( + if field == "text" { + Field::Content + } else { + Field::Thinking + }, + text, + ); + if field == "text" { + self.text.push_str(&safe); + } + if field == "thinking" { + self.reasoning.push_str(&safe); + } + if !safe.is_empty() { + out.push(self.chunk(json!({wire:safe}), None)); + } + } + } + } + "content_block_stop" => { + let index = value["index"].as_u64().ok_or(StreamError)?; + let block = self + .blocks + .get_mut(&index) + .filter(|b| !b.closed) + .ok_or(StreamError)?; + block.closed = true; + if block.slot.is_some() { + block.native["input"] = if block.arguments.is_empty() { + json!({}) + } else { + serde_json::from_str(&block.arguments) + .unwrap_or_else(|_| json!({"input":block.arguments})) + }; + } + } + "message_delta" => { + self.usage_final = false; + if let Some(reason) = value["delta"]["stop_reason"].as_str() { + self.finish = Some(finish_reason(reason)); + } + if let Some(usage) = value.get("usage") { + self.merge_usage(usage)?; + self.usage_final = usage["output_tokens"].as_u64().is_some() + && Usage::from_value(&Value::Object(self.raw_usage.clone())).is_some(); + } + } + "message_stop" => { + if self.blocks.values().any(|b| !b.closed) { + return Err(StreamError); + } + self.stopped = true; + let tail = self.redactor.finish(Field::Content); + self.text.push_str(&tail); + if !tail.is_empty() { + out.push(self.chunk(json!({"content":tail}), None)); + } + let tail = self.redactor.finish(Field::Thinking); + self.reasoning.push_str(&tail); + if !tail.is_empty() { + out.push(self.chunk(json!({"reasoning_content":tail}), None)); + } + let mut tails = Vec::new(); + for block in self.blocks.values_mut() { + if let Some(slot) = block.slot { + let tail = self.redactor.finish(Field::ToolArguments(slot)); + block.safe_arguments.push_str(&tail); + if !tail.is_empty() { + tails.push((slot, tail)); + } + } + } + for (slot, tail) in tails { + out.push(self.chunk( + json!({"tool_calls":[{"index":slot,"function":{"arguments":tail}}]}), + None, + )); + } + out.push(self.chunk(json!({}), Some(self.finish.unwrap_or("stop")))); + if self.include_usage { + if let Some(usage) = self.usage() { + let mut chunk = self.chunk(json!({}), None); + chunk["choices"] = json!([]); + chunk["usage"] = usage.openai(); + out.push(chunk); + } + } + } + _ => {} + } + Ok(out) + } + + /// EOF without message_stop (or any prior error) cannot produce trusted usage. + pub fn finish(self) -> Result { + if !self.stopped || self.failed { + return Err(StreamError); + } + let usage = self.usage(); + let mut assistant = json!({"role":"assistant", "content":if self.text.is_empty() { Value::Null } else { json!(self.text) }}); + if !self.reasoning.is_empty() { + assistant["reasoning_content"] = json!(self.reasoning); + } + let mut calls: Vec<_> = self + .blocks + .values() + .filter_map(|b| b.slot.map(|s| (s, b))) + .collect(); + calls.sort_by_key(|(slot, _)| *slot); + if !calls.is_empty() { + assistant["tool_calls"] = calls + .iter() + .map(|(_, b)| { + json!({"id":b.safe_id, + "type":"function", "function":{"name":b.safe_name, "arguments":b.safe_arguments}}) + }) + .collect(); + } + Ok(Completed { + assistant, + native_blocks: self.blocks.into_values().map(|b| b.native).collect(), + usage, + finish_reason: self.finish.unwrap_or("stop"), + }) + } +} diff --git a/oxc-rs/src/lib.rs b/oxc-rs/src/lib.rs index 06034f236..3c7c0e9a9 100644 --- a/oxc-rs/src/lib.rs +++ b/oxc-rs/src/lib.rs @@ -2,3 +2,6 @@ #[cfg(feature = "upstream-anthropic-request")] pub mod anthropic_request; + +#[cfg(feature = "upstream-anthropic-stream")] +pub mod anthropic_stream; diff --git a/oxc-rs/tests/anthropic_stream.rs b/oxc-rs/tests/anthropic_stream.rs new file mode 100644 index 000000000..5a4ce6eb0 --- /dev/null +++ b/oxc-rs/tests/anthropic_stream.rs @@ -0,0 +1,208 @@ +#![cfg(feature = "upstream-anthropic-stream")] +use oxc::anthropic_stream::{Field, Redactor, Stream, Usage}; +use serde_json::{json, Value}; +use std::collections::BTreeMap; + +fn canonical(mut value: Value) -> Vec { + value.sort_all_objects(); + serde_json::to_vec(&value).unwrap() +} + +struct FixtureRedactor { + marker: Option, + held: BTreeMap, +} +impl Redactor for FixtureRedactor { + fn feed(&mut self, field: Field, text: &str) -> String { + let Some(marker) = self.marker.as_ref() else { + return text.to_owned(); + }; + let data = format!("{}{text}", self.held.remove(&field).unwrap_or_default()) + .replace(marker, "[redacted]"); + let hold = (1..marker.len()) + .rev() + .find(|&size| marker.is_char_boundary(size) && data.ends_with(&marker[..size])) + .unwrap_or(0); + let (safe, tail) = data.split_at(data.len() - hold); + self.held.insert(field, tail.to_owned()); + safe.to_owned() + } + fn finish(&mut self, field: Field) -> String { + self.held.remove(&field).unwrap_or_default() + } + fn whole(&mut self, value: &str) -> String { + self.marker + .as_ref() + .map_or_else(|| value.to_owned(), |m| value.replace(m, "[redacted]")) + } +} + +#[test] +fn python_stream_contracts() { + for row in include_str!("fixtures/anthropic_streams.jsonl").lines() { + let row: Value = serde_json::from_str(row).unwrap(); + let mut stream = Stream::with_redactor( + "synthetic-model", + 1700000000, + row["include_usage"] == true, + FixtureRedactor { + marker: row["redact_marker"].as_str().map(str::to_owned), + held: BTreeMap::new(), + }, + ); + let chunks: Vec<_> = row["events"] + .as_array() + .unwrap() + .iter() + .flat_map(|event| stream.event(event).unwrap()) + .collect(); + assert_eq!( + canonical(json!(chunks)), + canonical(row["expected"].clone()), + "{}", + row["name"] + ); + let result = stream.finish().unwrap(); + if row["redact_marker"].is_string() { + let assistant = serde_json::to_string(&result.assistant).unwrap(); + assert!(!assistant.contains("synthetic-redact-marker")); + assert_eq!( + result.assistant["content"], + "Synthetic λ [redacted] synthetic-" + ); + assert_eq!( + result.assistant["reasoning_content"], + "[redacted] synthetic-" + ); + assert_eq!( + result.assistant["tool_calls"][0]["function"]["name"], + "read_[redacted]" + ); + assert_eq!( + result.assistant["tool_calls"][0]["function"]["arguments"], + "{\"note\":\"[redacted]\"}" + ); + } + assert_eq!( + json!(result.native_blocks), + row["native_blocks"], + "{}", + row["name"] + ); + assert_eq!( + result.usage.map(Usage::openai).unwrap_or(Value::Null), + row["final_usage"], + "{}", + row["name"] + ); + } +} + +#[test] +fn only_complete_streams_provide_final_usage() { + let start = + json!({"type":"message_start", "message":{"usage":{"input_tokens":9, "output_tokens":0}}}); + let mut stream = Stream::new("synthetic", 0, true); + stream.event(&start).unwrap(); + assert!(stream.finish().is_err()); + let mut stream = Stream::new("synthetic", 0, true); + stream.event(&start).unwrap(); + let chunks = stream.event(&json!({"type":"message_stop"})).unwrap(); + assert_eq!(chunks.len(), 1); + assert!(stream.finish().unwrap().usage.is_none()); + let mut stream = Stream::new("synthetic", 0, true); + stream.event(&start).unwrap(); + assert!(stream + .event(&json!({"type":"error", "error":{"message":"synthetic"}})) + .is_err()); + assert!(stream.event(&json!({"type":"message_stop"})).is_err()); + assert!(stream.finish().is_err()); +} + +#[test] +fn malformed_and_overflowed_usage_is_untrusted() { + for usage in [ + json!({}), + json!({"input_tokens":1}), + json!({"input_tokens":true, "output_tokens":0}), + json!({"input_tokens":1.5, "output_tokens":0}), + json!({"input_tokens":1, "output_tokens":-1}), + json!({"input_tokens":1, "output_tokens":0, "cache_read_input_tokens":null}), + json!({"input_tokens":u64::MAX, "output_tokens":1}), + ] { + assert!(Usage::from_value(&usage).is_none()); + } + let usage = Usage::from_value(&json!({"input_tokens":10, "output_tokens":7, + "cache_read_input_tokens":3, "cache_creation_input_tokens":2})) + .unwrap(); + assert_eq!( + ( + usage.input_tokens, + usage.cache_read_input_tokens, + usage.cache_creation_input_tokens + ), + (10, 3, 2) + ); + assert_eq!(usage.openai()["prompt_tokens"], 15); +} + +#[test] +fn malformed_event_order_fails_without_panicking() { + for event in [ + json!(null), + json!({"type":"content_block_delta"}), + json!({"type":"message_stop"}), + json!({"type":"message_delta"}), + ] { + assert!(Stream::new("synthetic", 0, false).event(&event).is_err()); + } + for event in [ + json!({"type":"content_block_delta", "index":0, "delta":{"type":"input_json_delta", "partial_json":"{}"}}), + json!({"type":"content_block_start", "index":true, "content_block":{"type":"text"}}), + json!({"type":"content_block_start", "index":0, "content_block":3}), + ] { + let mut stream = Stream::new("synthetic", 0, false); + stream + .event(&json!({"type":"message_start", "message":{}})) + .unwrap(); + assert!(stream.event(&event).is_err()); + assert!(stream.finish().is_err()); + } +} + +#[test] +fn repeated_signatures_replace_and_late_events_do_not_duplicate_output() { + let mut stream = Stream::new("synthetic", 0, false); + for event in [ + json!({"type":"message_start", "message":{}}), + json!({"type":"content_block_start", "index":0, "content_block":{"type":"thinking"}}), + json!({"type":"content_block_delta", "index":0, "delta":{"type":"signature_delta", "signature":"opaque-"}}), + json!({"type":"content_block_delta", "index":0, "delta":{"type":"signature_delta", "signature":"suffix"}}), + json!({"type":"content_block_stop", "index":0}), + json!({"type":"message_stop"}), + ] { + stream.event(&event).unwrap(); + } + assert!(stream + .event(&json!({"type":"message_stop"})) + .unwrap() + .is_empty()); + let completed = stream.finish().unwrap(); + assert_eq!(completed.native_blocks[0]["signature"], "suffix"); +} + +#[test] +fn terminal_usage_must_supply_its_own_output_count() { + for terminal in [ + json!({}), + json!({"cache_read_input_tokens":3}), + json!({"output_tokens":true}), + ] { + let mut stream = Stream::new("synthetic", 0, true); + stream.event(&json!({"type":"message_start","message":{"usage":{"input_tokens":10,"output_tokens":0}}})).unwrap(); + stream.event(&json!({"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":terminal})).unwrap(); + let chunks = stream.event(&json!({"type":"message_stop"})).unwrap(); + assert_eq!(chunks.len(), 1, "provisional usage must not be emitted"); + assert!(stream.finish().unwrap().usage.is_none()); + } +} diff --git a/oxc-rs/tests/fixtures/anthropic_streams.jsonl b/oxc-rs/tests/fixtures/anthropic_streams.jsonl new file mode 100644 index 000000000..dbe609a54 --- /dev/null +++ b/oxc-rs/tests/fixtures/anthropic_streams.jsonl @@ -0,0 +1,15 @@ +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"end_turn"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"end_turn","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"stop_sequence"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"stop_sequence","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"max_tokens"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"length","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"max_tokens","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"tool_use"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"tool_calls","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"tool_use","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"refusal"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"content_filter","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"refusal","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"pause_turn"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"pause_turn","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"model_context_window_exceeded"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"length","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"model_context_window_exceeded","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"end_turn"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":false,"name":"no_usage_requested","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"type":"ping"},{"content_block":{"type":"thinking"},"index":0,"type":"content_block_start"},{"delta":{"thinking":"Synthetic thought","type":"thinking_delta"},"index":0,"type":"content_block_delta"},{"delta":{"signature":"synthetic-signature","type":"signature_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"content_block":{"data":"synthetic-opaque-state","type":"redacted_thinking"},"index":1,"type":"content_block_start"},{"index":1,"type":"content_block_stop"},{"content_block":{"id":"call_1","input":{},"name":"read","type":"tool_use"},"index":2,"type":"content_block_start"},{"delta":{"partial_json":"{\"path\":","type":"input_json_delta"},"index":2,"type":"content_block_delta"},{"delta":{"partial_json":"\"λ\"}","type":"input_json_delta"},"index":2,"type":"content_block_delta"},{"index":2,"type":"content_block_stop"},{"content_block":{"id":"call_2","input":{},"name":"other","type":"tool_use"},"index":3,"type":"content_block_start"},{"delta":{"partial_json":"{}","type":"input_json_delta"},"index":3,"type":"content_block_delta"},{"index":3,"type":"content_block_stop"},{"delta":{"stop_reason":"tool_use"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"reasoning_content":"Synthetic thought"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"","name":"read"},"id":"call_1","index":0,"type":"function"}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"{\"path\":"},"index":0}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"\"λ\"}"},"index":0}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"","name":"other"},"id":"call_2","index":1,"type":"function"}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"{}"},"index":1}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"tool_calls","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"thinking_tools","native_blocks":[{"signature":"synthetic-signature","thinking":"Synthetic thought","type":"thinking"},{"data":"synthetic-opaque-state","type":"redacted_thinking"},{"id":"call_1","input":{"path":"λ"},"name":"read","type":"tool_use"},{"id":"call_2","input":{},"name":"other","type":"tool_use"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"id":"call_1","input":{},"name":"read","type":"tool_use"},"index":0,"type":"content_block_start"},{"delta":{"partial_json":"invalid","type":"input_json_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"tool_use"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"","name":"read"},"id":"call_1","index":0,"type":"function"}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"invalid"},"index":0}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"tool_calls","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"invalid_json_fallback","native_blocks":[{"id":"call_1","input":{"input":"invalid"},"name":"read","type":"tool_use"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"end_turn"},"type":"message_delta","usage":{"output_tokens":true}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"}],"final_usage":null,"include_usage":true,"name":"invalid_usage","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"future_stop_reason"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"unknown_stop","native_blocks":[{"text":"Synthetic λ","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"end_turn"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"empty_text","native_blocks":[{"text":"","type":"text"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"type":"thinking"},"index":0,"type":"content_block_start"},{"delta":{"signature":"opaque-first","type":"signature_delta"},"index":0,"type":"content_block_delta"},{"delta":{"signature":"opaque-replacement","type":"signature_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"delta":{"stop_reason":"end_turn"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"stop","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"repeated_signature","native_blocks":[{"signature":"opaque-replacement","thinking":"","type":"thinking"}],"redact_marker":null} +{"events":[{"message":{"usage":{"cache_creation_input_tokens":2,"cache_read_input_tokens":3,"input_tokens":10,"output_tokens":0}},"type":"message_start"},{"content_block":{"text":"","type":"text"},"index":0,"type":"content_block_start"},{"delta":{"text":"Synthetic λ synthetic-redact-","type":"text_delta"},"index":0,"type":"content_block_delta"},{"delta":{"text":"marker synthetic-","type":"text_delta"},"index":0,"type":"content_block_delta"},{"index":0,"type":"content_block_stop"},{"content_block":{"type":"thinking"},"index":1,"type":"content_block_start"},{"delta":{"thinking":"synthetic-redact-","type":"thinking_delta"},"index":1,"type":"content_block_delta"},{"delta":{"thinking":"marker synthetic-","type":"thinking_delta"},"index":1,"type":"content_block_delta"},{"delta":{"signature":"opaque-signature","type":"signature_delta"},"index":1,"type":"content_block_delta"},{"index":1,"type":"content_block_stop"},{"content_block":{"id":"call_1","input":{},"name":"read_synthetic-redact-marker","type":"tool_use"},"index":2,"type":"content_block_start"},{"delta":{"partial_json":"{\"note\":\"synthetic-redact-","type":"input_json_delta"},"index":2,"type":"content_block_delta"},{"delta":{"partial_json":"marker\"}","type":"input_json_delta"},"index":2,"type":"content_block_delta"},{"index":2,"type":"content_block_stop"},{"delta":{"stop_reason":"tool_use"},"type":"message_delta","usage":{"output_tokens":7}},{"type":"message_stop"}],"expected":[{"choices":[{"delta":{"content":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"Synthetic λ "},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"[redacted] "},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"reasoning_content":"[redacted] "},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"","name":"read_[redacted]"},"id":"call_1","index":0,"type":"function"}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"{\"note\":\""},"index":0}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"[redacted]\"}"},"index":0}]},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"content":"synthetic-"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{"reasoning_content":"synthetic-"},"finish_reason":null,"index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[{"delta":{},"finish_reason":"tool_calls","index":0}],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk"},{"choices":[],"created":1700000000,"id":"chatcmpl_anthropic","model":"synthetic-model","object":"chat.completion.chunk","usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22}}],"final_usage":{"completion_tokens":7,"completion_tokens_details":{"reasoning_tokens":0},"prompt_tokens":15,"prompt_tokens_details":{"cache_write_tokens":2,"cached_tokens":3},"total_tokens":22},"include_usage":true,"name":"redaction","native_blocks":[{"text":"Synthetic λ synthetic-redact-marker synthetic-","type":"text"},{"signature":"opaque-signature","thinking":"synthetic-redact-marker synthetic-","type":"thinking"},{"id":"call_1","input":{"note":"synthetic-redact-marker"},"name":"read_synthetic-redact-marker","type":"tool_use"}],"redact_marker":"synthetic-redact-marker"} diff --git a/oxc-rs/tests/fixtures/record_anthropic_streams.py b/oxc-rs/tests/fixtures/record_anthropic_streams.py new file mode 100644 index 000000000..8da154789 --- /dev/null +++ b/oxc-rs/tests/fixtures/record_anthropic_streams.py @@ -0,0 +1,98 @@ +"""Record synthetic events through the Python stream translator (no transport).""" +import argparse +import json +import os +from pathlib import Path +import socket +import sys +import tempfile +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[3] +sys.path.insert(0, str(ROOT / 'oxc' / 'src')) +CREATED = 1700000000 + + +def record(): + from oxc.runtime import cloud_anthropic as source + + def start(usage=None): + return {'type': 'message_start', 'message': {'usage': usage or { + 'input_tokens': 10, 'output_tokens': 0, 'cache_read_input_tokens': 3, + 'cache_creation_input_tokens': 2}}} + + def block(index, kind, **fields): + return {'type': 'content_block_start', 'index': index, + 'content_block': {'type': kind, **fields}} + + def delta(index, kind, **fields): + return {'type': 'content_block_delta', 'index': index, 'delta': {'type': kind, **fields}} + + def stop(index): + return {'type': 'content_block_stop', 'index': index} + + def end(reason, usage=None): + return [{'type': 'message_delta', 'delta': {'stop_reason': reason}, + 'usage': {'output_tokens': 7} if usage is None else usage}, {'type': 'message_stop'}] + + text = [start(), block(0, 'text', text=''), delta(0, 'text_delta', text='Synthetic λ'), stop(0)] + cases = [(reason, text + end(reason), True) for reason in source.STOP_REASONS] + cases += [ + ('no_usage_requested', text + end('end_turn'), False), + ('thinking_tools', [start(), {'type': 'ping'}, block(0, 'thinking'), + delta(0, 'thinking_delta', thinking='Synthetic thought'), + delta(0, 'signature_delta', signature='synthetic-signature'), stop(0), + block(1, 'redacted_thinking', data='synthetic-opaque-state'), stop(1), + block(2, 'tool_use', id='call_1', name='read', input={}), + delta(2, 'input_json_delta', partial_json='{"path":'), + delta(2, 'input_json_delta', partial_json='"λ"}'), stop(2), + block(3, 'tool_use', id='call_2', name='other', input={}), + delta(3, 'input_json_delta', partial_json='{}'), stop(3)] + end('tool_use'), True), + ('invalid_json_fallback', [start(), block(0, 'tool_use', id='call_1', name='read', input={}), + delta(0, 'input_json_delta', partial_json='invalid'), stop(0)] + end('tool_use'), True), + ('invalid_usage', text + end('end_turn', {'output_tokens': True}), True), + ('unknown_stop', text + end('future_stop_reason'), True), + ('empty_text', [start(), block(0, 'text', text=''), delta(0, 'text_delta', text=''), stop(0)] + + end('end_turn'), True), + ('repeated_signature', [start(), block(0, 'thinking'), + delta(0, 'signature_delta', signature='opaque-first'), + delta(0, 'signature_delta', signature='opaque-replacement'), stop(0)] + end('end_turn'), True), + ('redaction', [start(), block(0, 'text', text=''), + delta(0, 'text_delta', text='Synthetic λ synthetic-redact-'), + delta(0, 'text_delta', text='marker synthetic-'), stop(0), block(1, 'thinking'), + delta(1, 'thinking_delta', thinking='synthetic-redact-'), + delta(1, 'thinking_delta', thinking='marker synthetic-'), + delta(1, 'signature_delta', signature='opaque-signature'), stop(1), + block(2, 'tool_use', id='call_1', name='read_synthetic-redact-marker', input={}), + delta(2, 'input_json_delta', partial_json='{"note":"synthetic-redact-'), + delta(2, 'input_json_delta', partial_json='marker"}'), stop(2)] + end('tool_use'), True), + ] + rows = [] + for name, events, include_usage in cases: + marker = 'synthetic-redact-marker' if name == 'redaction' else None + stream = source._Stream({'id': 'synthetic-model', 'upstream_model': 'synthetic'}, include_usage, key=marker) + chunks = [] + with patch.object(source.time, 'time', return_value=CREATED): + for event in events: + chunks.extend(source.cloud.redact_value(chunk, marker) for chunk in stream.event(event)) + rows.append({'name': name, 'events': events, 'include_usage': include_usage, + 'expected': chunks, 'native_blocks': stream.blocks, 'redact_marker': marker, + 'final_usage': stream.usage if stream.usage_final else None}) + return ''.join(json.dumps(row, ensure_ascii=False, sort_keys=True, separators=(',', ':')) + '\n' + for row in rows) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--check', action='store_true') + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix='ox-upstream-stream-') as home: + os.environ.clear() + os.environ.update(HOME=home, XDG_CONFIG_HOME=home, XDG_DATA_HOME=home, XDG_CACHE_HOME=home) + with patch.object(socket, 'socket', side_effect=AssertionError('network forbidden')): + data = record() + target = Path(__file__).with_name('anthropic_streams.jsonl') + if args.check: + assert target.read_bytes() == data.encode(), 'Python fixture drift' + else: + target.write_bytes(data.encode()) diff --git a/oxc/tools/session_replay.py b/oxc/tools/session_replay.py index 327c1cde4..ef3f3adfb 100755 --- a/oxc/tools/session_replay.py +++ b/oxc/tools/session_replay.py @@ -660,7 +660,10 @@ def reject_nonfinite(_value): continue if validate_schema: import jsonschema - from referencing.exceptions import Unresolvable + try: + from referencing.exceptions import Unresolvable + except ImportError: # jsonschema older than 4.18 has no `referencing` package + Unresolvable = jsonschema.exceptions.RefResolutionError schema = tools[call['name']] def external_ref(node): if isinstance(node, dict): diff --git a/packaging/release/arch-test.sh b/packaging/release/arch-test.sh index abbe20e97..6557bfa9e 100755 --- a/packaging/release/arch-test.sh +++ b/packaging/release/arch-test.sh @@ -11,6 +11,8 @@ sha256sum -c SHA256SUMS export SOURCE_DATE_EPOCH export OX_SOURCE_COMMIT export OX_SOURCE_SHA256=$(sha256sum "ox-llm-$OX_SOURCE_COMMIT.tar.gz" | cut -d' ' -f1) +# release-build names the archive with the tag (ox-llm-v0.2.0-...); the PKGBUILD uses pkgver. +cp ox-llm-v*-linux-$(uname -m).tar.gz "ox-llm-0.2.0-linux-$(uname -m).tar.gz" export OX_BINARY_SHA256=$(sha256sum "ox-llm-0.2.0-linux-$(uname -m).tar.gz" | cut -d' ' -f1) # Verify the binary package and then a real source rebuild (including check()). for variant in ox-llm-bin ox-llm; do diff --git a/tools/ci/AUDIT.md b/tools/ci/AUDIT.md index c2685f23d..8067e8b35 100644 --- a/tools/ci/AUDIT.md +++ b/tools/ci/AUDIT.md @@ -64,11 +64,12 @@ keep separate builds because their features and linked libraries differ; clippy separate analysis rather than generating reusable runtime objects. Production ARM64 binaries cannot replace x86_64 CI test executables. The artifact reuse is between identical test builds. -Drafts retain formatting and scans only. Ready PRs check changed Rust components with clippy -and run the stub library suite for engine changes; all retain oxa/stub build-smoke and loopback -contract checks. Unknown paths conservatively select all components. Queue/manual/nightly -runs retain every gate, main retains the existing fast post-queue policy, and skipped required -jobs still report. No workflow paths filters or additional cancellation are introduced. +Drafts retain the classifier, scans and formatting only. Ready PRs and the merge queue run +the same path-selected set (`tools/ci/paths.py`: `engine`, `oxa`, `lab`, `cli`, `gateway`, `md`); +unknown paths select every component, and the scans run on every change. A push on main, the +nightly run and manual runs select every component, so main runs the full set after each merge. +Caches are restored everywhere and saved only on main, once per dependency-level key prefix. The +`gate` job aggregates every job and fails on a failed, cancelled or flag-less classifier. ## Redundant-test proposal diff --git a/tools/ci/paths.py b/tools/ci/paths.py index 77d20f64d..40a0b6677 100644 --- a/tools/ci/paths.py +++ b/tools/ci/paths.py @@ -20,27 +20,42 @@ def documentation(path): 'README.md', 'CONTRIBUTING.md', 'CHANGELOG.md', 'AGENTS.md', 'SECURITY.md') +# Top-level directories and the outputs (job groups) they select. `lab` is the Python controller +# suite, `cli` the oxc CLI conformance (Python and oxc-rs), `gateway` the oxc-gateway jobs. Nothing +# under oxc/ reads oxc-rs/, oxc-gateway/ or conformance/, so those paths skip `lab`. +COMPONENTS = {'engine': {'engine'}, 'oxa': {'oxa'}, 'oxc': {'lab', 'cli', 'gateway'}, + 'oxc-rs': {'cli'}, 'conformance': {'cli'}, 'oxc-gateway': {'gateway'}} +FUZZ = {'fuzz/oxa': {'oxa'}, 'fuzz/oxe': {'engine'}} +OUTPUTS = ('engine', 'oxa', 'lab', 'cli', 'gateway') +# Anything else (.github/, tools/, packaging/, root files, new directories) marks every component. + + def classify(paths, full_event=False): paths = list(paths) docs = bool(paths) and all(documentation(path) for path in paths) - changed = {'oxa': False, 'engine': False, 'lab': False} + changed = dict.fromkeys(OUTPUTS, False) for path in paths: if documentation(path): continue - component = {'oxc': 'lab'}.get(path.split('/', 1)[0], path.split('/', 1)[0]) # dir oxc/ is the lab component (job id kept) - if component in changed: + components = COMPONENTS.get(path.split('/', 1)[0]) or FUZZ.get('/'.join(path.split('/', 2)[:2])) + for component in components or OUTPUTS: changed[component] = True - else: - changed = dict.fromkeys(changed, True) - return {**changed, 'docs': docs, 'full': full_event} + md = any(Path(path).suffix.lower() in ('.md', '.rst') for path in paths) + return {**changed, 'docs': docs, 'md': md, 'full': full_event} def changed_paths(event, name): if event.get('deleted'): return [] - if name in ('schedule', 'workflow_dispatch', 'merge_group'): + if name in ('schedule', 'workflow_dispatch'): return subprocess.check_output(['git', 'ls-files', '-z']).decode().split('\0')[:-1] - if name == 'pull_request': + if name == 'merge_group': + # Only what the group changes on top of its base; an empty diff cannot be classified, + # so it conservatively counts as every file. + group = event.get('merge_group') or {} + base, head = group.get('base_sha'), group.get('head_sha') + triple = False + elif name == 'pull_request': base, head = event['pull_request']['base']['sha'], event['pull_request']['head']['sha'] triple = True else: @@ -53,7 +68,10 @@ def changed_paths(event, name): if base == head: return subprocess.check_output(['git', 'ls-files', '-z']).decode().split('\0')[:-1] span = base + ('...' if triple else '..') + head - return subprocess.check_output(['git', 'diff', '--name-only', '-z', span, '--']).decode().split('\0')[:-1] + changed = subprocess.check_output(['git', 'diff', '--name-only', '-z', span, '--']).decode().split('\0')[:-1] + if name == 'merge_group' and not changed: + return subprocess.check_output(['git', 'ls-files', '-z']).decode().split('\0')[:-1] + return changed def full_gate(name, ref): @@ -66,6 +84,10 @@ def main(): name = os.environ['GITHUB_EVENT_NAME'] full = full_gate(name, os.environ.get('GITHUB_REF', '')) result = classify(changed_paths(event, name), full) + if full and name != 'merge_group': + # main after a merge, nightly and manual runs test every component; the queue and pull + # requests keep the diff result, so the workflow needs no event tests of its own. + result.update(dict.fromkeys(OUTPUTS, True)) with open(os.environ['GITHUB_OUTPUT'], 'a') as stream: for key, value in result.items(): stream.write(f'{key}={str(value).lower()}\n') diff --git a/tools/ci/tests/test_gate.py b/tools/ci/tests/test_gate.py new file mode 100644 index 000000000..762536446 --- /dev/null +++ b/tools/ci/tests/test_gate.py @@ -0,0 +1,63 @@ +"""The `gate` job in checks.yml covers every job and every classifier output; caches save only on main. + +Parsed line by line (no YAML library) so it runs with a bare `python3 -I`. +""" +import importlib.util +from pathlib import Path +import re +import unittest + +WORKFLOWS = Path(__file__).resolve().parents[3] / '.github' / 'workflows' +SPEC = importlib.util.spec_from_file_location('paths', Path(__file__).resolve().parents[1] / 'paths.py') +PATHS = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(PATHS) + +# Jobs outside the gate, with the reason. +UNGATED = {'gate': 'the gate itself', 'oxa-nightly-arm': 'nightly and manual opt-in only; never on PRs or the queue'} + + +def jobs(text): + return re.findall(r'^ ([A-Za-z0-9_-]+):\s*$', text.split('\njobs:\n', 1)[1], re.M) + + +def job_block(text, name): + body = text.split('\njobs:\n', 1)[1] + return re.search(rf'^ {re.escape(name)}:\s*\n((?:(?: .*|\s*)\n)*)', body + '\n', re.M).group(1) + + +class Gate(unittest.TestCase): + def setUp(self): + self.checks = (WORKFLOWS / 'checks.yml').read_text() + + def test_gate_needs_every_job(self): + block = job_block(self.checks, 'gate') + needs = set(re.search(r'^ needs: \[(.*)\]', block, re.M).group(1).replace(' ', '').split(',')) + self.assertIn('if: always()', block) + self.assertEqual(needs, set(jobs(self.checks)) - set(UNGATED)) + + def test_gate_checks_every_classifier_output(self): + outputs = set(re.findall(r'^ ([a-z]+): \$\{\{ steps\.paths\.outputs\.\1 \}\}', + job_block(self.checks, 'changes'), re.M)) + produced = set(PATHS.classify(['oxa/src/a.rs'])) + self.assertEqual(outputs, produced) + checked = set(re.search(r"for k in \(([^)]*)\):\n\s*if flags", job_block(self.checks, 'gate')) + .group(1).replace("'", '').replace(' ', '').split(',')) + self.assertEqual(checked, produced) + + def test_scans_and_classifier_are_never_conditional(self): + for name in ('changes', 'scans'): + self.assertNotRegex(job_block(self.checks, name), r'(?m)^ if:', name) + + def test_caches_restore_only_and_save_only_on_main(self): + for path in sorted(WORKFLOWS.glob('*.y*ml')): + text = path.read_text() + self.assertNotRegex(text, r'actions/cache@', path.name) + for match in re.finditer(r'actions/cache/save@.*\n(?:\s+#.*\n)?\s+if: (.*)', text): + self.assertIn("github.ref == 'refs/heads/main'", match.group(1), path.name) + self.assertIn("github.event_name != 'merge_group'", match.group(1), path.name) + self.assertEqual(len(re.findall(r'actions/cache/save@', text)), + len(re.findall(r'actions/cache/restore@', text)), path.name) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/ci/tests/test_paths.py b/tools/ci/tests/test_paths.py index 9095a6c39..6409fffab 100644 --- a/tools/ci/tests/test_paths.py +++ b/tools/ci/tests/test_paths.py @@ -9,22 +9,34 @@ spec = importlib.util.spec_from_file_location('paths', Path(__file__).resolve().parents[1] / 'paths.py') paths = importlib.util.module_from_spec(spec) spec.loader.exec_module(paths) +KEYS = ('engine', 'oxa', 'lab', 'cli', 'gateway') class Paths(unittest.TestCase): def test_component_source_and_test_changes(self): - for component, directory in (('engine', 'engine'), ('oxa', 'oxa'), ('lab', 'oxc')): + for directory, components in (('engine', {'engine'}), ('oxa', {'oxa'}), + ('oxc', {'lab', 'cli', 'gateway'}), ('oxc-rs', {'cli'}), + ('conformance', {'cli'}), ('oxc-gateway', {'gateway'}), + ('fuzz/oxa', {'oxa'}), ('fuzz/oxe', {'engine'})): for suffix in ('src/code.rs', 'tests/test_unit.py', 'Cargo.lock'): - with self.subTest(component=component, suffix=suffix): + with self.subTest(directory=directory, suffix=suffix): got = paths.classify([directory + '/' + suffix]) - self.assertEqual({key for key in ('engine', 'oxa', 'lab') if got[key]}, {component}) + self.assertEqual({key for key in KEYS if got[key]}, components) self.assertFalse(got['full']) + self.assertFalse(got['md']) + + def test_md_flags_any_markdown_or_rst_change(self): + self.assertTrue(paths.classify(['oxa/src/a.rs', 'fuzz/oxa/FINDINGS.md'])['md']) + self.assertTrue(paths.classify(['docs/page.rst'])['md']) + self.assertTrue(paths.classify(['oxa/oxa-resources/builtin-skills/ox-llm/SKILL.md'])['md']) + self.assertFalse(paths.classify(['oxa/src/a.rs', 'tools/ci/paths.py'])['md']) + self.assertFalse(paths.classify([], True)['md']) def test_docs_only_and_mixed_changes(self): got = paths.classify(['README.md', 'oxc/docs/page.md', 'oxa/docs/page.rst'], True) self.assertTrue(got['docs']) self.assertTrue(got['full']) - self.assertFalse(any(got[key] for key in ('engine', 'oxa', 'lab'))) + self.assertFalse(any(got[key] for key in KEYS)) got = paths.classify(['oxc/docs/page.md', 'oxa/src/code.rs']) self.assertTrue(got['oxa']) self.assertFalse(got['lab']) @@ -48,7 +60,7 @@ def test_unknown_workflow_and_tooling_changes_test_every_product(self): for path in ('.github/workflows/checks.yml', 'tools/ci/paths.py', 'new-product/code', 'engine-other/src/code.rs', 'lab-notes.txt'): got = paths.classify([path]) - self.assertTrue(all(got[key] for key in ('engine', 'oxa', 'lab')), path) + self.assertTrue(all(got[key] for key in KEYS), path) def test_full_event_source_runs_and_empty_diffs_are_full(self): self.assertTrue(paths.classify(['oxc/src/code.py'], True)['full']) @@ -64,14 +76,15 @@ def test_full_gate_is_independent_of_changed_paths(self): self.assertTrue(paths.classify(['README.md'], True)['full']) def test_event_entrypoint_preserves_full_gate_for_docs_and_merge_queue(self): - cases = [('merge_group', 'refs/heads/queue', {}, True), - ('workflow_dispatch', 'refs/heads/feature', {}, True), - ('schedule', 'refs/heads/main', {}, True), - ('push', 'refs/heads/main', {'before': 'a' * 40, 'after': 'b' * 40}, True)] + # (event, ref, payload, full, every component forced on) + cases = [('merge_group', 'refs/heads/queue', {'merge_group': {'base_sha': 'a' * 40, 'head_sha': 'b' * 40}}, True, False), + ('workflow_dispatch', 'refs/heads/feature', {}, True, True), + ('schedule', 'refs/heads/main', {}, True, True), + ('push', 'refs/heads/main', {'before': 'a' * 40, 'after': 'b' * 40}, True, True)] for draft in (False, True): cases.append(('pull_request', 'refs/pull/1/merge', {'pull_request': { - 'draft': draft, 'base': {'sha': 'a' * 40}, 'head': {'sha': 'b' * 40}}}, False)) - for event, ref, payload, full in cases: + 'draft': draft, 'base': {'sha': 'a' * 40}, 'head': {'sha': 'b' * 40}}}, False, False)) + for event, ref, payload, full, everything in cases: with self.subTest(event=event, ref=ref, draft=payload.get('pull_request', {}).get('draft')): with tempfile.TemporaryDirectory() as directory: source, output = Path(directory) / 'event.json', Path(directory) / 'output' @@ -84,6 +97,9 @@ def test_event_entrypoint_preserves_full_gate_for_docs_and_merge_queue(self): values = dict(line.split('=', 1) for line in output.read_text().splitlines()) self.assertEqual(values['full'], str(full).lower()) self.assertEqual(values['docs'], 'true') + self.assertEqual(values['md'], 'true') + for key in KEYS: + self.assertEqual(values[key], str(everything).lower(), key) class GitPaths(unittest.TestCase): @@ -93,10 +109,39 @@ def test_pr_uses_merge_base_and_nul_paths(self): self.assertEqual(paths.changed_paths(event, 'pull_request'), ['oxa/src/a.rs', 'oxc/docs/a.md']) self.assertIn('a' * 40 + '...' + 'b' * 40, run.call_args.args[0]) - def test_merge_queue_enumerates_sources_without_push_payload_fields(self): - with patch.object(paths.subprocess, 'check_output', return_value=b'oxa/src/a.rs\0') as run: - self.assertEqual(paths.changed_paths({'merge_group': {}}, 'merge_group'), ['oxa/src/a.rs']) - self.assertEqual(run.call_args.args[0], ['git', 'ls-files', '-z']) + def group(self, diff, listing=b'engine/a.rs\0oxa/a.rs\0oxc/a.py\0'): + event = {'merge_group': {'base_sha': 'a' * 40, 'head_sha': 'b' * 40}} + with patch.object(paths.subprocess, 'check_output', side_effect=[diff, listing]) as run: + got = paths.classify(paths.changed_paths(event, 'merge_group'), True) + self.assertEqual(run.call_args_list[0].args[0], + ['git', 'diff', '--name-only', '-z', 'a' * 40 + '..' + 'b' * 40, '--']) + return {key for key in KEYS if got[key]} + + def test_merge_group_classifies_only_the_group_diff(self): + self.assertEqual(self.group(b'oxa/oxa-core/src/a.rs\0fuzz/oxa/smoke.rs\0'), {'oxa'}) + self.assertEqual(self.group(b'oxc-gateway/src/a.rs\0oxc-rs/src/a.rs\0conformance/t.py\0'), {'cli', 'gateway'}) + self.assertEqual(self.group(b'engine/crates/server/src/a.rs\0fuzz/oxe/smoke.rs\0'), {'engine'}) + self.assertEqual(self.group(b'oxa/src/a.rs\0engine/src/a.rs\0'), {'oxa', 'engine'}) + self.assertEqual(self.group(b'docs/page.md\0'), set()) + for path in (b'.github/workflows/build.yml', b'tools/ci/paths.py', b'packaging/x', b'NOTICE', + b'fuzz/other/x.rs'): + self.assertEqual(self.group(path + b'\0'), set(KEYS), path) + + def test_empty_merge_group_diff_tests_everything(self): + self.assertEqual(self.group(b''), set(KEYS)) + + def test_merge_group_rejects_untrusted_shas(self): + with patch.object(paths.subprocess, 'check_output') as run: + for event in ({'merge_group': {'base_sha': '--x', 'head_sha': 'b' * 40}}, {'merge_group': {}}): + with self.assertRaises(ValueError): + paths.changed_paths(event, 'merge_group') + run.assert_not_called() + + def test_dispatch_and_schedule_enumerate_every_file(self): + for name in ('workflow_dispatch', 'schedule'): + with patch.object(paths.subprocess, 'check_output', return_value=b'oxa/src/a.rs\0') as run: + self.assertEqual(paths.changed_paths({}, name), ['oxa/src/a.rs']) + self.assertEqual(run.call_args.args[0], ['git', 'ls-files', '-z']) def test_new_branch_compares_to_main(self): event = {'before': '0' * 40, 'after': 'b' * 40} diff --git a/tools/ci/tests/test_release_assets.py b/tools/ci/tests/test_release_assets.py new file mode 100644 index 000000000..5245f4f04 --- /dev/null +++ b/tools/ci/tests/test_release_assets.py @@ -0,0 +1,37 @@ +import re +import unittest +from pathlib import Path + +W = Path(__file__).resolve().parents[3] / '.github' / 'workflows' + + +class ReleaseAssets(unittest.TestCase): + def test_release_attaches_combined_archives(self): + rel = (W / 'release.yml').read_text() + self.assertIn('release-build.yml', rel) + self.assertIn('for a in aarch64 x86_64', rel) + self.assertIn('ox-llm-$TAG-linux-$a.tar.gz', rel) + self.assertIn('ox-llm-$TAG-linux-$a.sbom.cdx.json', rel) + + self.assertIn('dist/*.sbom.cdx.json', rel) + + def test_release_build_is_reusable_and_versioned(self): + rb = (W / 'release-build.yml').read_text() + self.assertIn('workflow_call:', rb) + self.assertIsNone(re.search(r'ox-llm-0\.2\.0-linux', rb)) + + def test_release_build_does_not_cancel_called_runs(self): + rb = (W / 'release-build.yml').read_text() + self.assertIsNone(re.search(r'cancel-in-progress:\s*true', rb)) + self.assertIn("cancel-in-progress: ${{ github.event_name == 'pull_request' }}", rb) + + def test_arch_consumers_accept_the_versioned_archive(self): + # release-build names the archive ox-llm-v-linux-; the PKGBUILD wants the + # unprefixed pkgver name, so arch-test.sh must map one to the other. + t = (W.parents[1] / 'packaging/release/arch-test.sh').read_text() + self.assertIn('ox-llm-v*-linux-$(uname -m).tar.gz', t) + self.assertIn('ox-llm-0.2.0-linux-$(uname -m).tar.gz', t) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/ci/tests/test_workflow_runners.py b/tools/ci/tests/test_workflow_runners.py new file mode 100644 index 000000000..d8baa9b5b --- /dev/null +++ b/tools/ci/tests/test_workflow_runners.py @@ -0,0 +1,123 @@ +"""Every workflow job runs on a hosted ARM image or on the project's runner expression. + +A job whose `runs-on` names a hosted x86_64 image directly (ubuntu-latest, ubuntu-24.04, ...) +bypasses the project's runners for good; this check keeps the x86_64/ARM split automatic. +Parsed line by line (no YAML library) so it runs with a bare `python3 -I`. +""" +from pathlib import Path +import re +import unittest + +WORKFLOWS = Path(__file__).resolve().parents[3] / '.github' / 'workflows' + +ARM_IMAGE = 'ubuntu-24.04-arm' +# The runner expression from checks.yml: the project's runners when the switch is on, otherwise +# (or for fork pull requests) the hosted x86_64 image. +RUNNER_CONDITION = ( + "vars.OXCI == 'on' && (github.event_name != 'pull_request' && 'oxci' || (vars.OXCI_PR == 'on' && " + "github.event.pull_request.head.repo.full_name == github.repository) && 'oxci-pr') || 'ubuntu-24.04'" +) +RUNNER_EXPRESSION = '${{ ' + RUNNER_CONDITION + ' }}' + +# Workflows allowed to name hosted images directly, with the reason. +EXCEPTIONS = { + 'release.yml': 'publishes releases; published binaries are never built on the project\'s own runners', + 'release-build.yml': 'builds the published binaries; they are never built on the project\'s own runners', +} + +FIX = ( + "Run the job on the hosted ARM image ({arm}) or copy the runner expression from " + ".github/workflows/checks.yml:\n runs-on: {expr}" +) + + +def jobs_runs_on(text): + """Yield (job, runs-on value, matrix values by key) for every job in a workflow.""" + jobs, in_jobs, job = {}, False, None + for line in text.splitlines(): + if re.match(r'^jobs:\s*$', line): + in_jobs = True + continue + if in_jobs and re.match(r'^\S', line) and not line.startswith('#'): + in_jobs = False + if not in_jobs: + continue + m = re.match(r'^ ([A-Za-z0-9_-]+):\s*$', line) + if m: + job = m.group(1) + jobs[job] = {'runs_on': None, 'matrix': {}} + continue + if job is None: + continue + m = re.match(r'^ runs-on:\s*(.+?)\s*$', line) + if m: + jobs[job]['runs_on'] = m.group(1).strip('"\'') if not m.group(1).startswith('${{') else m.group(1) + continue + m = re.match(r'^\s+([A-Za-z0-9_-]+):\s*\[(.*)\]\s*$', line) + if m: + values = [v.strip().strip('"\'') for v in m.group(2).split(',') if v.strip()] + jobs[job]['matrix'][m.group(1)] = values + return jobs + + +def problems(name, text): + found = [] + for job, info in jobs_runs_on(text).items(): + value = info['runs_on'] + if value is None: + continue # reusable-workflow call: no runner of its own + if value == ARM_IMAGE or value == RUNNER_EXPRESSION: + continue + if value.startswith('${{'): + refs = re.findall(r'matrix\.([A-Za-z0-9_-]+)', value) + ok = bool(refs) + for key in set(refs): + for v in info['matrix'].get(key, [None]): + if v == ARM_IMAGE: + continue + # an x86 matrix entry must be routed through the runner expression + guard = f"matrix.{key} == '{v}' && ({RUNNER_CONDITION})" + if v is None or guard not in value: + ok = False + if ok: + continue + found.append(f'{name}: job {job!r} has runs-on: {value}') + return found + + +class WorkflowRunners(unittest.TestCase): + def test_no_job_names_a_hosted_x86_image(self): + checks = (WORKFLOWS / 'checks.yml').read_text() + self.assertIn(RUNNER_EXPRESSION, checks, 'the runner expression in this test must match checks.yml') + found = [] + files = sorted(WORKFLOWS.glob('*.y*ml')) + self.assertTrue(files) + for path in files: + if path.name in EXCEPTIONS: + continue + found += problems(path.name, path.read_text()) + self.assertFalse(found, '\n'.join(found) + '\n' + FIX.format(arm=ARM_IMAGE, expr=RUNNER_EXPRESSION)) + + def test_exceptions_exist(self): + for name in EXCEPTIONS: + self.assertTrue((WORKFLOWS / name).is_file(), name) + + def test_detects_hosted_images(self): + for image in ('ubuntu-latest', 'ubuntu-24.04', 'ubuntu-22.04', '"ubuntu-latest"'): + text = f'on: push\njobs:\n a:\n runs-on: {image}\n' + self.assertTrue(problems('x.yml', text), image) + text = 'jobs:\n a:\n strategy:\n matrix:\n r: [ubuntu-latest]\n runs-on: ${{ matrix.r }}\n' + self.assertTrue(problems('x.yml', text)) + expr = RUNNER_EXPRESSION.replace("|| 'ubuntu-24.04' }}", "|| 'ubuntu-22.04' }}") + self.assertTrue(problems('x.yml', f'jobs:\n a:\n runs-on: {expr}\n')) + + def test_accepts_arm_and_expression(self): + for value in (ARM_IMAGE, RUNNER_EXPRESSION): + self.assertFalse(problems('x.yml', f'jobs:\n a:\n runs-on: {value}\n')) + guarded = ("${{ matrix.r == 'ubuntu-latest' && (" + RUNNER_CONDITION + ") || matrix.r }}") + text = f'jobs:\n a:\n strategy:\n matrix:\n r: [ubuntu-latest, {ARM_IMAGE}]\n runs-on: {guarded}\n' + self.assertFalse(problems('x.yml', text)) + + +if __name__ == '__main__': + unittest.main()