diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 16c528cc..c2dbff6f 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,3 @@ -github: instructkr +github: + - ultraworkers + - Yeachan-Heo diff --git a/.github/scripts/check_doc_source_of_truth.py b/.github/scripts/check_doc_source_of_truth.py new file mode 100755 index 00000000..c831eb77 --- /dev/null +++ b/.github/scripts/check_doc_source_of_truth.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +from pathlib import Path +import re +import sys + +ROOT = Path(__file__).resolve().parents[2] +FILES = [ + ROOT / 'README.md', + ROOT / 'USAGE.md', + ROOT / 'PARITY.md', + ROOT / 'PHILOSOPHY.md', + ROOT / 'ROADMAP.md', + ROOT / '.github' / 'FUNDING.yml', +] +FILES.extend(sorted((ROOT / 'docs').rglob('*.md')) if (ROOT / 'docs').exists() else []) + +FORBIDDEN = { + r'github\.com/Yeachan-Heo/claw-code(?!-parity)': 'replace old claw-code GitHub links with ultraworkers/claw-code', + r'github\.com/code-yeongyu/claw-code': 'replace stale alternate claw-code GitHub links with ultraworkers/claw-code', + r'discord\.gg/6ztZB9jvWq': 'replace the stale UltraWorkers Discord invite with the current invite', + r'api\.star-history\.com/svg\?repos=Yeachan-Heo/claw-code': 'update star-history embeds to ultraworkers/claw-code', + r'star-history\.com/#Yeachan-Heo/claw-code': 'update star-history links to ultraworkers/claw-code', + r'assets/clawd-hero\.jpeg': 'rename stale hero asset references to assets/claw-hero.jpeg', + r'assets/instructkr\.png': 'remove stale instructkr image references', +} + +errors: list[str] = [] +for path in FILES: + if not path.exists(): + continue + text = path.read_text(encoding='utf-8') + for pattern, message in FORBIDDEN.items(): + for match in re.finditer(pattern, text): + line = text.count('\n', 0, match.start()) + 1 + errors.append(f'{path.relative_to(ROOT)}:{line}: {message}') + +if errors: + print('doc source-of-truth check failed:', file=sys.stderr) + for error in errors: + print(f' - {error}', file=sys.stderr) + sys.exit(1) + +print('doc source-of-truth check passed') diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..682a983c --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,68 @@ +name: Release binaries + +on: + push: + tags: + - 'v*' + workflow_dispatch: + +permissions: + contents: write + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + name: build-${{ matrix.name }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - name: linux-x64 + os: ubuntu-latest + bin: claw + artifact_name: claw-linux-x64 + - name: macos-arm64 + os: macos-14 + bin: claw + artifact_name: claw-macos-arm64 + defaults: + run: + working-directory: rust + steps: + - uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@stable + + - uses: Swatinem/rust-cache@v2 + with: + workspaces: rust -> target + + - name: Build release binary + run: cargo build --release -p rusty-claude-cli + + - name: Package artifact + shell: bash + run: | + mkdir -p dist + cp "target/release/${{ matrix.bin }}" "dist/${{ matrix.artifact_name }}" + chmod +x "dist/${{ matrix.artifact_name }}" + + - name: Upload workflow artifact + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.artifact_name }} + path: rust/dist/${{ matrix.artifact_name }} + + - name: Upload release asset + if: startsWith(github.ref, 'refs/tags/') + uses: softprops/action-gh-release@v2 + with: + files: rust/dist/${{ matrix.artifact_name }} + fail_on_unmatched_files: true diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index 80776557..97760472 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -8,12 +8,28 @@ on: - 'omx-issue-*' paths: - .github/workflows/rust-ci.yml + - .github/scripts/check_doc_source_of_truth.py + - .github/FUNDING.yml + - README.md + - USAGE.md + - PARITY.md + - PHILOSOPHY.md + - ROADMAP.md + - docs/** - rust/** pull_request: branches: - main paths: - .github/workflows/rust-ci.yml + - .github/scripts/check_doc_source_of_truth.py + - .github/FUNDING.yml + - README.md + - USAGE.md + - PARITY.md + - PHILOSOPHY.md + - ROADMAP.md + - docs/** - rust/** workflow_dispatch: @@ -29,6 +45,20 @@ env: CARGO_TERM_COLOR: always jobs: + doc-source-of-truth: + name: docs source-of-truth + runs-on: ubuntu-latest + defaults: + run: + working-directory: . + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Check docs and metadata for stale branding + run: python .github/scripts/check_doc_source_of_truth.py + fmt: name: cargo fmt runs-on: ubuntu-latest @@ -43,8 +73,8 @@ jobs: - name: Check formatting run: cargo fmt --all --check - test-rusty-claude-cli: - name: cargo test -p rusty-claude-cli + test-workspace: + name: cargo test --workspace runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -52,5 +82,19 @@ jobs: - uses: Swatinem/rust-cache@v2 with: workspaces: rust -> target - - name: Run crate tests - run: cargo test -p rusty-claude-cli + - name: Run workspace tests + run: cargo test --workspace + + clippy-workspace: + name: cargo clippy --workspace + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy + - uses: Swatinem/rust-cache@v2 + with: + workspaces: rust -> target + - name: Run workspace clippy + run: cargo clippy --workspace diff --git a/Containerfile b/Containerfile new file mode 100644 index 00000000..eec07d3f --- /dev/null +++ b/Containerfile @@ -0,0 +1,13 @@ +FROM rust:bookworm + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + ca-certificates \ + git \ + libssl-dev \ + pkg-config \ + && rm -rf /var/lib/apt/lists/* + +ENV CARGO_TERM_COLOR=always +WORKDIR /workspace +CMD ["bash"] diff --git a/README.md b/README.md index caae91a0..adbbe111 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,17 @@ -# Rewriting Project Claw Code +# Claw Code

- ⭐ The fastest repo in history to surpass 50K stars, reaching the milestone in just 2 hours after publication ⭐ + ultraworkers/claw-code + · + Usage + · + Rust workspace + · + Parity + · + Roadmap + · + UltraWorkers Discord

@@ -9,188 +19,75 @@ - Star History Chart + Star history for ultraworkers/claw-code

- Claw + Claw Code

-

- Autonomously maintained by lobsters/claws — not by human hands -

- -

- clawhip · - oh-my-openagent · - oh-my-claudecode · - oh-my-codex · - UltraWorkers Discord -

+Claw Code is the public Rust implementation of the `claw` CLI agent harness. +The canonical implementation lives in [`rust/`](./rust), and the current source of truth for this repository is **ultraworkers/claw-code**. > [!IMPORTANT] -> The active Rust workspace now lives in [`rust/`](./rust). Start with [`USAGE.md`](./USAGE.md) for build, auth, CLI, session, and parity-harness workflows, then use [`rust/README.md`](./rust/README.md) for crate-level details. +> Start with [`USAGE.md`](./USAGE.md) for build, auth, CLI, session, and parity-harness workflows. Make `claw doctor` your first health check after building, use [`rust/README.md`](./rust/README.md) for crate-level details, read [`PARITY.md`](./PARITY.md) for the current Rust-port checkpoint, and see [`docs/container.md`](./docs/container.md) for the container-first workflow. -> Want the bigger idea behind this repo? Read [`PHILOSOPHY.md`](./PHILOSOPHY.md) and Sigrid Jin's public explanation: https://x.com/realsigridjin/status/2039472968624185713 +## Current repository shape -> Shout-out to the UltraWorkers ecosystem powering this repo: [clawhip](https://github.com/Yeachan-Heo/clawhip), [oh-my-openagent](https://github.com/code-yeongyu/oh-my-openagent), [oh-my-claudecode](https://github.com/Yeachan-Heo/oh-my-claudecode), [oh-my-codex](https://github.com/Yeachan-Heo/oh-my-codex), and the [UltraWorkers Discord](https://discord.gg/6ztZB9jvWq). +- **`rust/`** — canonical Rust workspace and the `claw` CLI binary +- **`USAGE.md`** — task-oriented usage guide for the current product surface +- **`PARITY.md`** — Rust-port parity status and migration notes +- **`ROADMAP.md`** — active roadmap and cleanup backlog +- **`PHILOSOPHY.md`** — project intent and system-design framing +- **`src/` + `tests/`** — companion Python/reference workspace and audit helpers; not the primary runtime surface ---- - -## Backstory - -This repo is maintained by **lobsters/claws**, not by a conventional human-only dev team. - -The people behind the system are [Bellman / Yeachan Heo](https://github.com/Yeachan-Heo) and friends like [Yeongyu](https://github.com/code-yeongyu), but the repo itself is being pushed forward by autonomous claw workflows: parallel coding sessions, event-driven orchestration, recovery loops, and machine-readable lane state. - -In practice, that means this project is not just *about* coding agents — it is being **actively built by them**. Features, tests, telemetry, docs, and workflow hardening are landed through claw-driven loops using [clawhip](https://github.com/Yeachan-Heo/clawhip), [oh-my-openagent](https://github.com/code-yeongyu/oh-my-openagent), [oh-my-claudecode](https://github.com/Yeachan-Heo/oh-my-claudecode), and [oh-my-codex](https://github.com/Yeachan-Heo/oh-my-codex). - -This repository exists to prove that an open coding harness can be built **autonomously, in public, and at high velocity** — with humans setting direction and claws doing the grinding. - -See the public build story here: - -https://x.com/realsigridjin/status/2039472968624185713 - -![Tweet screenshot](assets/tweet-screenshot.png) - ---- - -## Porting Status - -The main source tree is now Python-first. - -- `src/` contains the active Python porting workspace -- `tests/` verifies the current Python workspace -- the exposed snapshot is no longer part of the tracked repository state - -The current Python workspace is not yet a complete one-to-one replacement for the original system, but the primary implementation surface is now Python. - -## Why this rewrite exists - -I originally studied the exposed codebase to understand its harness, tool wiring, and agent workflow. After spending more time with the legal and ethical questions—and after reading the essay linked below—I did not want the exposed snapshot itself to remain the main tracked source tree. - -This repository now focuses on Python porting work instead. - -## Repository Layout - -```text -. -├── src/ # Python porting workspace -│ ├── __init__.py -│ ├── commands.py -│ ├── main.py -│ ├── models.py -│ ├── port_manifest.py -│ ├── query_engine.py -│ ├── task.py -│ └── tools.py -├── tests/ # Python verification -├── assets/omx/ # OmX workflow screenshots -├── 2026-03-09-is-legal-the-same-as-legitimate-ai-reimplementation-and-the-erosion-of-copyleft.md -└── README.md -``` - -## Python Workspace Overview - -The new Python `src/` tree currently provides: - -- **`port_manifest.py`** — summarizes the current Python workspace structure -- **`models.py`** — dataclasses for subsystems, modules, and backlog state -- **`commands.py`** — Python-side command port metadata -- **`tools.py`** — Python-side tool port metadata -- **`query_engine.py`** — renders a Python porting summary from the active workspace -- **`main.py`** — a CLI entrypoint for manifest and summary output - -## Quickstart - -Render the Python porting summary: +## Quick start ```bash -python3 -m src.main summary +cd rust +cargo build --workspace +./target/debug/claw --help +./target/debug/claw prompt "summarize this repository" ``` -Print the current Python workspace manifest: +Authenticate with either an API key or the built-in OAuth flow: ```bash -python3 -m src.main manifest +export ANTHROPIC_API_KEY="sk-ant-..." +# or +cd rust +./target/debug/claw login ``` -List the current Python modules: +Run the workspace test suite: ```bash -python3 -m src.main subsystems --limit 16 +cd rust +cargo test --workspace ``` -Run verification: +## Documentation map -```bash -python3 -m unittest discover -s tests -v -``` +- [`USAGE.md`](./USAGE.md) — quick commands, auth, sessions, config, parity harness +- [`rust/README.md`](./rust/README.md) — crate map, CLI surface, features, workspace layout +- [`PARITY.md`](./PARITY.md) — parity status for the Rust port +- [`rust/MOCK_PARITY_HARNESS.md`](./rust/MOCK_PARITY_HARNESS.md) — deterministic mock-service harness details +- [`ROADMAP.md`](./ROADMAP.md) — active roadmap and open cleanup work +- [`PHILOSOPHY.md`](./PHILOSOPHY.md) — why the project exists and how it is operated -Run the parity audit against the local ignored archive (when present): +## Ecosystem -```bash -python3 -m src.main parity-audit -``` +Claw Code is built in the open alongside the broader UltraWorkers toolchain: -Inspect mirrored command/tool inventories: +- [clawhip](https://github.com/Yeachan-Heo/clawhip) +- [oh-my-openagent](https://github.com/code-yeongyu/oh-my-openagent) +- [oh-my-claudecode](https://github.com/Yeachan-Heo/oh-my-claudecode) +- [oh-my-codex](https://github.com/Yeachan-Heo/oh-my-codex) +- [UltraWorkers Discord](https://discord.gg/5TUQKqFWd) -```bash -python3 -m src.main commands --limit 10 -python3 -m src.main tools --limit 10 -``` - -Run a local Ollama-backed chat prompt: - -```bash -ollama run qwen2.5-coder:7b -python3 -m src.main chat "write a hello world in python" -``` - -The local chat path uses Ollama at `http://localhost:11434` by default and sends a single non-streaming `/api/chat` request to `qwen2.5-coder:7b`. - -For the current local agent/tool integration, request features, and command examples, see [`OLLAMA_INTEGRATION.md`](./OLLAMA_INTEGRATION.md). - -## Current Parity Checkpoint - -The port now mirrors the archived root-entry file surface, top-level subsystem names, and command/tool inventories much more closely than before. However, it is **not yet** a full runtime-equivalent replacement for the original TypeScript system; the Python tree still contains fewer executable runtime slices than the archived source. - - -## Built with `oh-my-codex` - -The restructuring and documentation work on this repository was AI-assisted and orchestrated with Yeachan Heo's [oh-my-codex (OmX)](https://github.com/Yeachan-Heo/oh-my-codex), layered on top of Codex. - -- **`$team` mode:** used for coordinated parallel review and architectural feedback -- **`$ralph` mode:** used for persistent execution, verification, and completion discipline -- **Codex-driven workflow:** used to turn the main `src/` tree into a Python-first porting workspace - -### OmX workflow screenshots - -![OmX workflow screenshot 1](assets/omx/omx-readme-review-1.png) - -*Ralph/team orchestration view while the README and essay context were being reviewed in terminal panes.* - -![OmX workflow screenshot 2](assets/omx/omx-readme-review-2.png) - -*Split-pane review and verification flow during the final README wording pass.* - -## Community - -

- UltraWorkers Discord -

- -Join the [**UltraWorkers Discord**](https://discord.gg/6ztZB9jvWq) — the community around clawhip, oh-my-openagent, oh-my-claudecode, oh-my-codex, and claw-code. Come chat about LLMs, harness engineering, agent workflows, and autonomous software development. - -[![Discord](https://img.shields.io/badge/Join%20Discord-UltraWorkers-5865F2?logo=discord&style=for-the-badge)](https://discord.gg/6ztZB9jvWq) - -## Star History - -See the chart at the top of this README. - -## Ownership / Affiliation Disclaimer +## Ownership / affiliation disclaimer - This repository does **not** claim ownership of the original Claude Code source material. - This repository is **not affiliated with, endorsed by, or maintained by Anthropic**. diff --git a/ROADMAP.md b/ROADMAP.md index 38cd548f..5bf89472 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -271,15 +271,18 @@ Acceptance: Priority order: P0 = blocks CI/green state, P1 = blocks integration wiring, P2 = clawability hardening, P3 = swarm-efficiency improvements. **P0 — Fix first (CI reliability)** -1. Isolate `render_diff_report` tests into tmpdir — flaky under `cargo test --workspace`; reads real working-tree state; breaks CI during active worktree ops -2. Expand GitHub CI from single-crate coverage to workspace-grade verification — current `rust-ci.yml` runs `cargo fmt` and `cargo test -p rusty-claude-cli`, but misses broader `cargo test --workspace` coverage that already passes locally -3. Add release-grade binary workflow — repo has a Rust CLI and release intent, but no GitHub Actions path that builds tagged artifacts / checks release packaging before a publish step -4. Add container-first test/run docs — runtime detects Docker/Podman/container state, but docs do not show a canonical container workflow for `cargo test --workspace`, binary execution, or bind-mounted repo usage -5. Surface `doctor` / preflight diagnostics in onboarding docs and help — the CLI already has setup-diagnosis commands and branch preflight machinery, but they are not prominent enough in README/USAGE, so new users still ask manual setup questions instead of running a built-in health check first -6. Add branding/source-of-truth residue checks for docs — after repo migration, old org names can survive in badges, star-history URLs, and copied snippets; docs need a consistency pass or CI lint to catch stale branding automatically -7. Reconcile README product narrative with current repo reality — top-level docs now say the active workspace is Rust, but later sections still describe the repo as Python-first; users should not have to infer which implementation is canonical -8. Eliminate warning spam from first-run help/build path — `cargo run -p rusty-claude-cli -- --help` currently prints a wall of compile warnings before the actual help text, which pollutes the first-touch UX and hides the product surface behind unrelated noise -9. Promote `doctor` from slash-only to top-level CLI entrypoint — users naturally try `claw doctor`, but today it errors and tells them to enter a REPL or resume path first; healthcheck flows should be callable directly from the shell +1. Isolate `render_diff_report` tests into tmpdir — **done**: `render_diff_report_for()` tests run in temp git repos instead of the live working tree, and targeted `cargo test -p rusty-claude-cli render_diff_report -- --nocapture` now stays green during branch/worktree activity +2. Expand GitHub CI from single-crate coverage to workspace-grade verification — **done**: `.github/workflows/rust-ci.yml` now runs `cargo test --workspace` plus fmt/clippy at the workspace level +3. Add release-grade binary workflow — **done**: `.github/workflows/release.yml` now builds tagged Rust release artifacts for the CLI +4. Add container-first test/run docs — **done**: `Containerfile` + `docs/container.md` document the canonical Docker/Podman workflow for build, bind-mount, and `cargo test --workspace` usage +5. Surface `doctor` / preflight diagnostics in onboarding docs and help — **done**: README + USAGE now put `claw doctor` / `/doctor` in the first-run path and point at the built-in preflight report +6. Automate branding/source-of-truth residue checks in CI — **done**: `.github/scripts/check_doc_source_of_truth.py` and the `doc-source-of-truth` CI job now block stale repo/org/invite residue in tracked docs and metadata +7. Eliminate warning spam from first-run help/build path — **done**: current `cargo run -q -p rusty-claude-cli -- --help` renders clean help output without a warning wall before the product surface +8. Promote `doctor` from slash-only to top-level CLI entrypoint — **done**: `claw doctor` is now a local shell entrypoint with regression coverage for direct help and health-report output +9. Make machine-readable status commands actually machine-readable — **done**: `claw --output-format json status` and `claw --output-format json sandbox` now emit structured JSON snapshots instead of prose tables +10. Unify legacy config/skill namespaces in user-facing output — **done**: skills/help JSON/text output now present `.claw` as the canonical namespace and collapse legacy roots behind `.claw`-shaped source ids/labels +11. Honor JSON output on inventory commands like `skills` and `mcp` — **done**: direct CLI inventory commands now honor `--output-format json` with structured payloads for both skills and MCP inventory +12. Audit `--output-format` contract across the whole CLI surface — **done**: direct CLI commands now honor deterministic JSON/text handling across help/version/status/sandbox/agents/mcp/skills/bootstrap-plan/system-prompt/init/doctor, with regression coverage in `output_format_contract.rs` and resumed `/status` JSON coverage **P1 — Next (integration wiring, unblocks verification)** 2. Add cross-module integration tests — **done**: 12 integration tests covering worker→recovery→policy, stale_branch→policy, green_contract→policy, reconciliation flows @@ -297,11 +300,38 @@ Priority order: P0 = blocks CI/green state, P1 = blocks integration wiring, P2 = 12. Lane board / machine-readable status API — **done**: Lane completion hardening + `LaneContext::completed` auto-detection + MCP degraded reporting surface machine-readable state 13. **Session completion failure classification** — **done**: `WorkerFailureKind::Provider` + `observe_completion()` + recovery recipe bridge landed 14. **Config merge validation gap** — **done**: `config.rs` hook validation before deep-merge (+56 lines), malformed entries fail with source-path context instead of merged parse errors -15. **MCP manager discovery flaky test** — `manager_discovery_report_keeps_healthy_servers_when_one_server_fails` has intermittent timing issues in CI; temporarily ignored, needs root cause fix +15. **MCP manager discovery flaky test** — **done**: `manager_discovery_report_keeps_healthy_servers_when_one_server_fails` now runs as a normal workspace test again after repeated stable passes, so degraded-startup coverage is no longer hidden behind `#[ignore]` + +16. **Commit provenance / worktree-aware push events** — **done**: `LaneCommitProvenance` now carries branch/worktree/canonical-commit/supersession metadata in lane events, and `dedupe_superseded_commit_events()` is applied before agent manifests are written so superseded commit events collapse to the latest canonical lineage +17. **Orphaned module integration audit** — **done**: `runtime` now keeps `session_control` and `trust_resolver` behind `#[cfg(test)]` until they are wired into a real non-test execution path, so normal builds no longer advertise dead clawability surface area. +18. **Context-window preflight gap** — **done**: provider request sizing now emits `context_window_blocked` before oversized requests leave the process, using a model-context registry instead of the old naive max-token heuristic. +19. **Subcommand help falls through into runtime/API path** — **done**: `claw doctor --help`, `claw status --help`, `claw sandbox --help`, and nested `mcp`/`skills` help are now intercepted locally without runtime/provider startup, with regression tests covering the direct CLI paths. +20. **Session state classification gap (working vs blocked vs finished vs truly stale)** — **done**: agent manifests now derive machine states such as `working`, `blocked_background_job`, `blocked_merge_conflict`, `degraded_mcp`, `interrupted_transport`, `finished_pending_report`, and `finished_cleanable`, and terminal-state persistence records commit provenance plus derived state so downstream monitoring can distinguish quiet progress from truly idle sessions. +21. **Resumed `/status` JSON parity gap** — dogfooding shows fresh `claw status --output-format json` now emits structured JSON, but resumed slash-command status still leaks through a text-shaped path in at least one dispatch path. Local CI-equivalent repro fails `rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs::resumed_status_command_emits_structured_json_when_requested` with `expected value at line 1 column 1`, so resumed automation can receive text where JSON was explicitly requested. **Action:** unify fresh vs resumed `/status` rendering through one output-format contract and add regression coverage so resumed JSON output is guaranteed valid. +22. **Opaque failure surface for session/runtime crashes** — repeated dogfood-facing failures can currently collapse to generic wrappers like `Something went wrong while processing your request. Please try again, or use /new to start a fresh session.` without exposing whether the fault was provider auth, session corruption, slash-command dispatch, render failure, or transport/runtime panic. This blocks fast self-recovery and turns actionable clawability bugs into blind retries. **Action:** preserve a short user-safe failure class (`provider_auth`, `session_load`, `command_dispatch`, `render`, `runtime_panic`, etc.), attach a local trace/session id, and ensure operators can jump from the chat-visible error to the exact failure log quickly. +23. **`doctor --output-format json` check-level structure gap** — **done**: `claw doctor --output-format json` now keeps the human-readable `message`/`report` while also emitting structured per-check diagnostics (`name`, `status`, `summary`, `details`, plus typed fields like workspace paths and sandbox fallback data), with regression coverage in `output_format_contract.rs`. +24. **Plugin lifecycle init/shutdown test flakes under workspace-parallel execution** — dogfooding surfaced that `build_runtime_runs_plugin_lifecycle_init_and_shutdown` can fail under `cargo test --workspace` while passing in isolation because sibling tests race on tempdir-backed shell init script paths. This is test brittleness rather than a code-path regression, but it still destabilizes CI confidence and wastes diagnosis cycles. **Action:** isolate temp resources per test robustly (unique dirs + no shared cwd assumptions), audit cleanup timing, and add a regression guard so the plugin lifecycle test remains stable under parallel workspace execution. +26. **Resumed local-command JSON parity gap** — **done**: direct `claw --output-format json` already had structured renderers for `sandbox`, `mcp`, `skills`, `version`, and `init`, but resumed `claw --output-format json --resume /…` paths still fell back to prose because resumed slash dispatch only emitted JSON for `/status`. Resumed `/sandbox`, `/mcp`, `/skills`, `/version`, and `/init` now reuse the same JSON envelopes as their direct CLI counterparts, with regression coverage in `rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs` and `rust/crates/rusty-claude-cli/tests/output_format_contract.rs`. + +41. **Phantom completions root cause: global session store has no per-worktree isolation** — + + **Root cause.** The session store under `~/.local/share/opencode` is global to the host. Every `opencode serve` instance — including the parallel lane workers spawned per worktree — reads and writes the same on-disk session directory. Sessions are keyed only by id and timestamp, not by the workspace they were created in, so there is no structural barrier between a session created in worktree `/tmp/b4-phantom-diag` and one created in `/tmp/b4-omc-flat`. Whichever serve instance picks up a given session id can drive it from whatever CWD that serve happens to be running in. + + **Impact.** Parallel lanes silently cross wires. A lane reports a clean run — file edits, builds, tests — and the orchestrator marks the lane green, but the writes were applied against another worktree's CWD because a sibling `opencode serve` won the session race. The originating worktree shows no diff, the *other* worktree gains unexplained edits, and downstream consumers (clawhip lane events, PR pushes, merge gates) treat the empty originator as a successful no-op. These are the "phantom completions" we keep chasing: success messaging without any landed changes in the lane that claimed them, plus stray edits in unrelated lanes whose own runs never touched those files. Because the report path is happy, retries and recovery recipes never fire, so the lane silently wedges until a human notices the diff is empty. + + **Proposed fix.** Bind every session to its workspace root + branch at creation time and refuse to drive it from any other CWD. + + - At session creation, capture the canonical workspace root (resolved git worktree path) and the active branch and persist them on the session record. + - On every load (`opencode serve`, slash-command resume, lane recovery), validate that the current process CWD matches the persisted workspace root before any tool with side effects (file_ops, bash, git) is allowed to run. Mismatches surface as a typed `WorkspaceMismatch` failure class instead of silently writing to the wrong tree. + - Namespace the on-disk session path under the workspace fingerprint (e.g. `//`) so two parallel `opencode serve` instances physically cannot collide on the same session id. + - Forks inherit the parent's workspace root by default; an explicit re-bind is required to move a session to a new worktree, and that re-bind is itself recorded as a structured event so the orchestrator can audit cross-worktree handoffs. + - Surface a `branch.workspace_mismatch` lane event so clawhip stops counting wrong-CWD writes as lane completions. + + **Status.** A `workspace_root` field has been added to `Session` in `rust/crates/runtime/src/session.rs` (with builder, accessor, JSON + JSONL round-trip, fork inheritance, and given/when/then test coverage in `persists_workspace_root_round_trip_and_forks_inherit_it`). The CWD validation, the namespaced on-disk path, and the `branch.workspace_mismatch` lane event are still outstanding and tracked under this item. **P3 — Swarm efficiency** -13. Swarm branch-lock protocol — detect same-module/same-branch collision before parallel workers drift into duplicate implementation -14. Commit provenance / worktree-aware push events — emit branch, worktree, superseded-by, and canonical commit lineage so parallel sessions stop producing duplicate-looking push summaries +13. Swarm branch-lock protocol — **done**: `branch_lock::detect_branch_lock_collisions()` now detects same-branch/same-scope and nested-module collisions before parallel lanes drift into duplicate implementation +14. Commit provenance / worktree-aware push events — **done**: lane event provenance now includes branch/worktree/superseded/canonical lineage metadata, and manifest persistence de-dupes superseded commit events before downstream consumers render them ## Suggested Session Split diff --git a/USAGE.md b/USAGE.md index f8232d6b..f0220758 100644 --- a/USAGE.md +++ b/USAGE.md @@ -1,6 +1,20 @@ # Claw Code Usage -This guide covers the current Rust workspace under `rust/` and the `claw` CLI binary. +This guide covers the current Rust workspace under `rust/` and the `claw` CLI binary. If you are brand new, make the doctor health check your first run: start `claw`, then run `/doctor`. + +## Quick-start health check + +Run this before prompts, sessions, or automation: + +```bash +cd rust +cargo build --workspace +./target/debug/claw +# first command inside the REPL +/doctor +``` + +`/doctor` is the built-in setup and preflight diagnostic. Once you have a saved session, you can rerun it with `./target/debug/claw --resume latest /doctor`. ## Prerequisites @@ -10,17 +24,25 @@ This guide covers the current Rust workspace under `rust/` and the `claw` CLI bi - `claw login` for OAuth-based auth - Optional: `ANTHROPIC_BASE_URL` when targeting a proxy or local service -## Build the workspace +## Install / build the workspace ```bash cd rust cargo build --workspace ``` -The CLI binary is available at `rust/target/debug/claw` after a debug build. +The CLI binary is available at `rust/target/debug/claw` after a debug build. Make the doctor check above your first post-build step. ## Quick start +### First-run doctor check + +```bash +cd rust +./target/debug/claw +/doctor +``` + ### Interactive REPL ```bash @@ -87,6 +109,157 @@ cd rust ./target/debug/claw logout ``` +## Local Models + +`claw` can talk to local servers and provider gateways through either Anthropic-compatible or OpenAI-compatible endpoints. Use `ANTHROPIC_BASE_URL` with `ANTHROPIC_AUTH_TOKEN` for Anthropic-compatible services, or `OPENAI_BASE_URL` with `OPENAI_API_KEY` for OpenAI-compatible services. OAuth is Anthropic-only, so when `OPENAI_BASE_URL` is set you should use API-key style auth instead of `claw login`. + +### Anthropic-compatible endpoint + +```bash +export ANTHROPIC_BASE_URL="http://127.0.0.1:8080" +export ANTHROPIC_AUTH_TOKEN="local-dev-token" + +cd rust +./target/debug/claw --model "claude-sonnet-4-6" prompt "reply with the word ready" +``` + +### OpenAI-compatible endpoint + +```bash +export OPENAI_BASE_URL="http://127.0.0.1:8000/v1" +export OPENAI_API_KEY="local-dev-token" + +cd rust +./target/debug/claw --model "qwen2.5-coder" prompt "reply with the word ready" +``` + +### Ollama + +```bash +export OPENAI_BASE_URL="http://127.0.0.1:11434/v1" +unset OPENAI_API_KEY + +cd rust +./target/debug/claw --model "llama3.2" prompt "summarize this repository in one sentence" +``` + +### OpenRouter + +```bash +export OPENAI_BASE_URL="https://openrouter.ai/api/v1" +export OPENAI_API_KEY="sk-or-v1-..." + +cd rust +./target/debug/claw --model "openai/gpt-4.1-mini" prompt "summarize this repository in one sentence" +``` + +## Supported Providers & Models + +`claw` has three built-in provider backends. The provider is selected automatically based on the model name, falling back to whichever credential is present in the environment. + +### Provider matrix + +| Provider | Protocol | Auth env var(s) | Base URL env var | Default base URL | +|---|---|---|---|---| +| **Anthropic** (direct) | Anthropic Messages API | `ANTHROPIC_API_KEY` or `ANTHROPIC_AUTH_TOKEN` or OAuth (`claw login`) | `ANTHROPIC_BASE_URL` | `https://api.anthropic.com` | +| **xAI** | OpenAI-compatible | `XAI_API_KEY` | `XAI_BASE_URL` | `https://api.x.ai/v1` | +| **OpenAI-compatible** | OpenAI Chat Completions | `OPENAI_API_KEY` | `OPENAI_BASE_URL` | `https://api.openai.com/v1` | + +The OpenAI-compatible backend also serves as the gateway for **OpenRouter**, **Ollama**, and any other service that speaks the OpenAI `/v1/chat/completions` wire format — just point `OPENAI_BASE_URL` at the service. + +### Tested models and aliases + +These are the models registered in the built-in alias table with known token limits: + +| Alias | Resolved model name | Provider | Max output tokens | Context window | +|---|---|---|---|---| +| `opus` | `claude-opus-4-6` | Anthropic | 32 000 | 200 000 | +| `sonnet` | `claude-sonnet-4-6` | Anthropic | 64 000 | 200 000 | +| `haiku` | `claude-haiku-4-5-20251213` | Anthropic | 64 000 | 200 000 | +| `grok` / `grok-3` | `grok-3` | xAI | 64 000 | 131 072 | +| `grok-mini` / `grok-3-mini` | `grok-3-mini` | xAI | 64 000 | 131 072 | +| `grok-2` | `grok-2` | xAI | — | — | + +Any model name that does not match an alias is passed through verbatim. This is how you use OpenRouter model slugs (`openai/gpt-4.1-mini`), Ollama tags (`llama3.2`), or full Anthropic model IDs (`claude-sonnet-4-20250514`). + +### User-defined aliases + +You can add custom aliases in any settings file (`~/.claw/settings.json`, `.claw/settings.json`, or `.claw/settings.local.json`): + +```json +{ + "aliases": { + "fast": "claude-haiku-4-5-20251213", + "smart": "claude-opus-4-6", + "cheap": "grok-3-mini" + } +} +``` + +Local project settings override user-level settings. Aliases resolve through the built-in table, so `"fast": "haiku"` also works. + +### How provider detection works + +1. If the resolved model name starts with `claude` → Anthropic. +2. If it starts with `grok` → xAI. +3. Otherwise, `claw` checks which credential is set: `ANTHROPIC_API_KEY`/`ANTHROPIC_AUTH_TOKEN` first, then `OPENAI_API_KEY`, then `XAI_API_KEY`. +4. If nothing matches, it defaults to Anthropic. + +## FAQ + +### What about Codex? + +The name "codex" appears in the Claw Code ecosystem but it does **not** refer to OpenAI Codex (the code-generation model). Here is what it means in this project: + +- **`oh-my-codex` (OmX)** is the workflow and plugin layer that sits on top of `claw`. It provides planning modes, parallel multi-agent execution, notification routing, and other automation features. See [PHILOSOPHY.md](./PHILOSOPHY.md) and the [oh-my-codex repo](https://github.com/Yeachan-Heo/oh-my-codex). +- **`.codex/` directories** (e.g. `.codex/skills`, `.codex/agents`, `.codex/commands`) are legacy lookup paths that `claw` still scans alongside the primary `.claw/` directories. +- **`CODEX_HOME`** is an optional environment variable that points to a custom root for user-level skill and command lookups. + +`claw` does **not** support OpenAI Codex sessions, the Codex CLI, or Codex session import/export. If you need to use OpenAI models (like GPT-4.1), configure the OpenAI-compatible provider as shown above in the [OpenAI-compatible endpoint](#openai-compatible-endpoint) and [OpenRouter](#openrouter) sections. + +## HTTP proxy support + +`claw` honours the standard `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables (both upper- and lower-case spellings are accepted) when issuing outbound requests to Anthropic, OpenAI-, and xAI-compatible endpoints. Set them before launching the CLI and the underlying `reqwest` client will be configured automatically. + +### Environment variables + +```bash +export HTTPS_PROXY="http://proxy.corp.example:3128" +export HTTP_PROXY="http://proxy.corp.example:3128" +export NO_PROXY="localhost,127.0.0.1,.corp.example" + +cd rust +./target/debug/claw prompt "hello via the corporate proxy" +``` + +### Programmatic `proxy_url` config option + +As an alternative to per-scheme environment variables, the `ProxyConfig` type exposes a `proxy_url` field that acts as a single catch-all proxy for both HTTP and HTTPS traffic. When `proxy_url` is set it takes precedence over the separate `http_proxy` and `https_proxy` fields. + +```rust +use api::{build_http_client_with, ProxyConfig}; + +// From a single unified URL (config file, CLI flag, etc.) +let config = ProxyConfig::from_proxy_url("http://proxy.corp.example:3128"); +let client = build_http_client_with(&config).expect("proxy client"); + +// Or set the field directly alongside NO_PROXY +let config = ProxyConfig { + proxy_url: Some("http://proxy.corp.example:3128".to_string()), + no_proxy: Some("localhost,127.0.0.1".to_string()), + ..ProxyConfig::default() +}; +let client = build_http_client_with(&config).expect("proxy client"); +``` + +### Notes + +- When both `HTTPS_PROXY` and `HTTP_PROXY` are set, the secure proxy applies to `https://` URLs and the plain proxy applies to `http://` URLs. +- `proxy_url` is a unified alternative: when set, it applies to both `http://` and `https://` destinations, overriding the per-scheme fields. +- `NO_PROXY` accepts a comma-separated list of host suffixes (for example `.corp.example`) and IP literals. +- Empty values are treated as unset, so leaving `HTTPS_PROXY=""` in your shell will not enable a proxy. +- If a proxy URL cannot be parsed, `claw` falls back to a direct (no-proxy) client so existing workflows keep working; double-check the URL if you expected the request to be tunnelled. + ## Common operational commands ```bash diff --git a/assets/clawd-hero.jpeg b/assets/claw-hero.jpeg similarity index 100% rename from assets/clawd-hero.jpeg rename to assets/claw-hero.jpeg diff --git a/assets/instructkr.png b/assets/instructkr.png deleted file mode 100644 index 08d300fb..00000000 Binary files a/assets/instructkr.png and /dev/null differ diff --git a/assets/sigrid-photo.png b/assets/sigrid-photo.png new file mode 100644 index 00000000..a70aa842 Binary files /dev/null and b/assets/sigrid-photo.png differ diff --git a/docs/container.md b/docs/container.md new file mode 100644 index 00000000..aeaa45e3 --- /dev/null +++ b/docs/container.md @@ -0,0 +1,132 @@ +# Container-first claw-code workflows + +This repo already had **container detection** in the Rust runtime before this document was added: + +- `rust/crates/runtime/src/sandbox.rs` detects Docker/Podman/container markers such as `/.dockerenv`, `/run/.containerenv`, matching env vars, and `/proc/1/cgroup` hints. +- `rust/crates/rusty-claude-cli/src/main.rs` exposes that state through the `claw sandbox` / `cargo run -p rusty-claude-cli -- sandbox` report. +- `.github/workflows/rust-ci.yml` runs on `ubuntu-latest`, but it does **not** define a Docker or Podman container job. +- Before this change, the repo did **not** have a checked-in `Dockerfile`, `Containerfile`, or `.devcontainer/` config. + +This document adds a small checked-in `Containerfile` so Docker and Podman users have one canonical container workflow. + +## What the checked-in container image is for + +The root [`../Containerfile`](../Containerfile) gives you a reusable Rust build/test shell with the extra packages this workspace commonly needs (`git`, `pkg-config`, `libssl-dev`, certificates). + +It does **not** copy the repository into the image. Instead, the recommended flow is to bind-mount your checkout into `/workspace` so edits stay on the host. + +## Build the image + +From the repository root: + +### Docker + +```bash +docker build -t claw-code-dev -f Containerfile . +``` + +### Podman + +```bash +podman build -t claw-code-dev -f Containerfile . +``` + +## Run `cargo test --workspace` in the container + +These commands mount the repo, keep Cargo build artifacts out of the working tree, and run from the Rust workspace at `rust/`. + +### Docker + +```bash +docker run --rm -it \ + -v "$PWD":/workspace \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev \ + cargo test --workspace +``` + +### Podman + +```bash +podman run --rm -it \ + -v "$PWD":/workspace:Z \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev \ + cargo test --workspace +``` + +If you want a fully clean rebuild, add `cargo clean &&` before `cargo test --workspace`. + +## Open a shell in the container + +### Docker + +```bash +docker run --rm -it \ + -v "$PWD":/workspace \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev +``` + +### Podman + +```bash +podman run --rm -it \ + -v "$PWD":/workspace:Z \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev +``` + +Inside the shell: + +```bash +cargo build --workspace +cargo test --workspace +cargo run -p rusty-claude-cli -- --help +cargo run -p rusty-claude-cli -- sandbox +``` + +The `sandbox` command is a useful sanity check: inside Docker or Podman it should report `In container true` and list the markers the runtime detected. + +## Bind-mount this repo and another repo at the same time + +If you want to run `claw` against a second checkout while keeping `claw-code` itself mounted read-write: + +### Docker + +```bash +docker run --rm -it \ + -v "$PWD":/workspace \ + -v "$HOME/src/other-repo":/repo \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev +``` + +### Podman + +```bash +podman run --rm -it \ + -v "$PWD":/workspace:Z \ + -v "$HOME/src/other-repo":/repo:Z \ + -e CARGO_TARGET_DIR=/tmp/claw-target \ + -w /workspace/rust \ + claw-code-dev +``` + +Then, for example: + +```bash +cargo run -p rusty-claude-cli -- prompt "summarize /repo" +``` + +## Notes + +- Docker and Podman use the same checked-in `Containerfile`. +- The `:Z` suffix in the Podman examples is for SELinux relabeling; keep it on Fedora/RHEL-class hosts. +- Running with `CARGO_TARGET_DIR=/tmp/claw-target` avoids leaving container-owned `target/` artifacts in your bind-mounted checkout. +- For non-container local development, keep using [`../USAGE.md`](../USAGE.md) and [`../rust/README.md`](../rust/README.md). diff --git a/install.sh b/install.sh new file mode 100755 index 00000000..c95cddaf --- /dev/null +++ b/install.sh @@ -0,0 +1,394 @@ +#!/usr/bin/env bash +# Claw Code installer +# +# Detects the host OS, verifies the Rust toolchain (rustc + cargo), +# builds the `claw` binary from the `rust/` workspace, and runs a +# post-install verification step. Supports Linux, macOS, and WSL. +# +# Usage: +# ./install.sh # debug build (fast, default) +# ./install.sh --release # optimized release build +# ./install.sh --no-verify # skip post-install verification +# ./install.sh --help # print usage +# +# Environment overrides: +# CLAW_BUILD_PROFILE=debug|release same as --release toggle +# CLAW_SKIP_VERIFY=1 same as --no-verify + +set -euo pipefail + +# --------------------------------------------------------------------------- +# Pretty printing +# --------------------------------------------------------------------------- + +if [ -t 1 ] && command -v tput >/dev/null 2>&1 && [ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]; then + COLOR_RESET="$(tput sgr0)" + COLOR_BOLD="$(tput bold)" + COLOR_DIM="$(tput dim)" + COLOR_RED="$(tput setaf 1)" + COLOR_GREEN="$(tput setaf 2)" + COLOR_YELLOW="$(tput setaf 3)" + COLOR_BLUE="$(tput setaf 4)" + COLOR_CYAN="$(tput setaf 6)" +else + COLOR_RESET="" + COLOR_BOLD="" + COLOR_DIM="" + COLOR_RED="" + COLOR_GREEN="" + COLOR_YELLOW="" + COLOR_BLUE="" + COLOR_CYAN="" +fi + +CURRENT_STEP=0 +TOTAL_STEPS=6 + +step() { + CURRENT_STEP=$((CURRENT_STEP + 1)) + printf '\n%s[%d/%d]%s %s%s%s\n' \ + "${COLOR_BLUE}" "${CURRENT_STEP}" "${TOTAL_STEPS}" "${COLOR_RESET}" \ + "${COLOR_BOLD}" "$1" "${COLOR_RESET}" +} + +info() { printf '%s ->%s %s\n' "${COLOR_CYAN}" "${COLOR_RESET}" "$1"; } +ok() { printf '%s ok%s %s\n' "${COLOR_GREEN}" "${COLOR_RESET}" "$1"; } +warn() { printf '%s warn%s %s\n' "${COLOR_YELLOW}" "${COLOR_RESET}" "$1"; } +error() { printf '%s error%s %s\n' "${COLOR_RED}" "${COLOR_RESET}" "$1" 1>&2; } + +print_banner() { + printf '%s' "${COLOR_BOLD}" + cat <<'EOF' + ____ _ ____ _ + / ___|| | __ _ __ __ / ___|___ __| | ___ + | | | | / _` |\ \ /\ / /| | / _ \ / _` |/ _ \ + | |___ | || (_| | \ V V / | |__| (_) | (_| | __/ + \____||_| \__,_| \_/\_/ \____\___/ \__,_|\___| +EOF + printf '%s\n' "${COLOR_RESET}" + printf '%sClaw Code installer%s\n' "${COLOR_DIM}" "${COLOR_RESET}" +} + +print_usage() { + cat <<'EOF' +Usage: ./install.sh [options] + +Options: + --release Build the optimized release profile (slower, smaller binary). + --debug Build the debug profile (default, faster compile). + --no-verify Skip the post-install verification step. + -h, --help Show this help text and exit. + +Environment overrides: + CLAW_BUILD_PROFILE debug | release + CLAW_SKIP_VERIFY set to 1 to skip verification +EOF +} + +# --------------------------------------------------------------------------- +# Argument parsing +# --------------------------------------------------------------------------- + +BUILD_PROFILE="${CLAW_BUILD_PROFILE:-debug}" +SKIP_VERIFY="${CLAW_SKIP_VERIFY:-0}" + +while [ "$#" -gt 0 ]; do + case "$1" in + --release) + BUILD_PROFILE="release" + ;; + --debug) + BUILD_PROFILE="debug" + ;; + --no-verify) + SKIP_VERIFY="1" + ;; + -h|--help) + print_usage + exit 0 + ;; + *) + error "unknown argument: $1" + print_usage + exit 2 + ;; + esac + shift +done + +case "${BUILD_PROFILE}" in + debug|release) ;; + *) + error "invalid build profile: ${BUILD_PROFILE} (expected debug or release)" + exit 2 + ;; +esac + +# --------------------------------------------------------------------------- +# Troubleshooting hints +# --------------------------------------------------------------------------- + +print_troubleshooting() { + cat </dev/null 2>&1 +} + +# --------------------------------------------------------------------------- +# Step 1: detect OS / arch / WSL +# --------------------------------------------------------------------------- + +print_banner +step "Detecting host environment" + +UNAME_S="$(uname -s 2>/dev/null || echo unknown)" +UNAME_M="$(uname -m 2>/dev/null || echo unknown)" +OS_FAMILY="unknown" +IS_WSL="0" + +case "${UNAME_S}" in + Linux*) + OS_FAMILY="linux" + if grep -qiE 'microsoft|wsl' /proc/version 2>/dev/null; then + IS_WSL="1" + fi + ;; + Darwin*) + OS_FAMILY="macos" + ;; + MINGW*|MSYS*|CYGWIN*) + OS_FAMILY="windows-shell" + ;; +esac + +info "uname: ${UNAME_S} ${UNAME_M}" +info "os family: ${OS_FAMILY}" +if [ "${IS_WSL}" = "1" ]; then + info "wsl: yes" +fi + +case "${OS_FAMILY}" in + linux|macos) + ok "supported platform detected" + ;; + windows-shell) + error "Detected a native Windows shell (MSYS/Cygwin/MinGW)." + error "Please re-run this script from inside a WSL distribution." + exit 1 + ;; + *) + error "Unsupported or unknown OS: ${UNAME_S}" + error "Supported: Linux, macOS, and Windows via WSL." + exit 1 + ;; +esac + +# --------------------------------------------------------------------------- +# Step 2: locate the Rust workspace +# --------------------------------------------------------------------------- + +step "Locating the Rust workspace" + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +RUST_DIR="${SCRIPT_DIR}/rust" + +if [ ! -d "${RUST_DIR}" ]; then + error "Could not find rust/ workspace next to install.sh" + error "Expected: ${RUST_DIR}" + exit 1 +fi + +if [ ! -f "${RUST_DIR}/Cargo.toml" ]; then + error "Missing ${RUST_DIR}/Cargo.toml — repository layout looks unexpected." + exit 1 +fi + +ok "workspace at ${RUST_DIR}" + +# --------------------------------------------------------------------------- +# Step 3: prerequisite checks +# --------------------------------------------------------------------------- + +step "Checking prerequisites" + +MISSING_PREREQS=0 + +if require_cmd rustc; then + RUSTC_VERSION="$(rustc --version 2>/dev/null || echo 'unknown')" + ok "rustc found: ${RUSTC_VERSION}" +else + error "rustc not found in PATH" + MISSING_PREREQS=1 +fi + +if require_cmd cargo; then + CARGO_VERSION="$(cargo --version 2>/dev/null || echo 'unknown')" + ok "cargo found: ${CARGO_VERSION}" +else + error "cargo not found in PATH" + MISSING_PREREQS=1 +fi + +if require_cmd git; then + ok "git found: $(git --version 2>/dev/null || echo 'unknown')" +else + warn "git not found — some workflows (login, session export) may degrade" +fi + +if [ "${OS_FAMILY}" = "linux" ]; then + if require_cmd pkg-config; then + ok "pkg-config found" + else + warn "pkg-config not found — may be required for OpenSSL-linked crates" + fi +fi + +if [ "${OS_FAMILY}" = "macos" ]; then + if ! require_cmd cc && ! xcode-select -p >/dev/null 2>&1; then + warn "Xcode command line tools not detected — run: xcode-select --install" + fi +fi + +if [ "${MISSING_PREREQS}" -ne 0 ]; then + error "Missing required tools. See troubleshooting below." + exit 1 +fi + +# --------------------------------------------------------------------------- +# Step 4: build the workspace +# --------------------------------------------------------------------------- + +step "Building the claw workspace (${BUILD_PROFILE})" + +CARGO_FLAGS=("build" "--workspace") +if [ "${BUILD_PROFILE}" = "release" ]; then + CARGO_FLAGS+=("--release") +fi + +info "running: cargo ${CARGO_FLAGS[*]}" +info "this may take a few minutes on the first build" + +( + cd "${RUST_DIR}" + CARGO_TERM_COLOR="${CARGO_TERM_COLOR:-always}" cargo "${CARGO_FLAGS[@]}" +) + +CLAW_BIN="${RUST_DIR}/target/${BUILD_PROFILE}/claw" + +if [ ! -x "${CLAW_BIN}" ]; then + error "Expected binary not found at ${CLAW_BIN}" + error "The build reported success but the binary is missing — check cargo output above." + exit 1 +fi + +ok "built ${CLAW_BIN}" + +# --------------------------------------------------------------------------- +# Step 5: post-install verification +# --------------------------------------------------------------------------- + +step "Verifying the installed binary" + +if [ "${SKIP_VERIFY}" = "1" ]; then + warn "verification skipped (--no-verify or CLAW_SKIP_VERIFY=1)" +else + info "running: claw --version" + if VERSION_OUT="$("${CLAW_BIN}" --version 2>&1)"; then + ok "claw --version -> ${VERSION_OUT}" + else + error "claw --version failed:" + printf '%s\n' "${VERSION_OUT}" 1>&2 + exit 1 + fi + + info "running: claw --help (smoke test)" + if "${CLAW_BIN}" --help >/dev/null 2>&1; then + ok "claw --help responded" + else + error "claw --help failed" + exit 1 + fi +fi + +# --------------------------------------------------------------------------- +# Step 6: next steps +# --------------------------------------------------------------------------- + +step "Next steps" + +cat < One-shot prompt (non-interactive) - login Authenticate via OAuth - logout Clear stored credentials - init Initialize project config - status Show the current workspace status snapshot - sandbox Show the current sandbox isolation snapshot - agents Inspect agent definitions - mcp Inspect configured MCP servers - skills Inspect installed skills - system-prompt Render the assembled system prompt +Top-level commands: + prompt + help + version + status + sandbox + dump-manifests + bootstrap-plan + agents + mcp + skills + system-prompt + login + logout + init ``` -For the current canonical help text, run `cargo run -p rusty-claude-cli -- --help`. +The command surface is moving quickly. For the canonical live help text, run: + +```bash +cargo run -p rusty-claude-cli -- --help +``` ## Slash Commands (REPL) Tab completion expands slash commands, model aliases, permission modes, and recent session IDs. -| Command | Description | -|---------|-------------| -| `/help` | Show help | -| `/status` | Show session status (model, tokens, cost) | -| `/cost` | Show cost breakdown | -| `/compact` | Compact conversation history | -| `/clear` | Clear conversation | -| `/model [name]` | Show or switch model | -| `/permissions` | Show or switch permission mode | -| `/config [section]` | Show config (env, hooks, model) | -| `/memory` | Show CLAUDE.md contents | -| `/diff` | Show git diff | -| `/export [path]` | Export conversation | -| `/resume [id]` | Resume a saved conversation | -| `/session [id]` | Resume a previous session | -| `/version` | Show version | +The REPL now exposes a much broader surface than the original minimal shell: -See [`../USAGE.md`](../USAGE.md) for examples covering interactive use, JSON automation, sessions, permissions, and the mock parity harness. +- session / visibility: `/help`, `/status`, `/sandbox`, `/cost`, `/resume`, `/session`, `/version`, `/usage`, `/stats` +- workspace / git: `/compact`, `/clear`, `/config`, `/memory`, `/init`, `/diff`, `/commit`, `/pr`, `/issue`, `/export`, `/hooks`, `/files`, `/branch`, `/release-notes`, `/add-dir` +- discovery / debugging: `/mcp`, `/agents`, `/skills`, `/doctor`, `/tasks`, `/context`, `/desktop`, `/ide` +- automation / analysis: `/review`, `/advisor`, `/insights`, `/security-review`, `/subagent`, `/team`, `/telemetry`, `/providers`, `/cron`, and more +- plugin management: `/plugin` (with aliases `/plugins`, `/marketplace`) + +Notable claw-first surfaces now available directly in slash form: +- `/skills [list|install |help]` +- `/agents [list|help]` +- `/mcp [list|show |help]` +- `/doctor` +- `/plugin [list|install |enable |disable |uninstall |update ]` +- `/subagent [list|steer |kill ]` + +See [`../USAGE.md`](../USAGE.md) for usage examples and run `cargo run -p rusty-claude-cli -- --help` for the live canonical command list. ## Workspace Layout -``` +```text rust/ ├── Cargo.toml # Workspace root ├── Cargo.lock └── crates/ - ├── api/ # Anthropic API client + SSE streaming - ├── commands/ # Shared slash-command registry + ├── api/ # Provider clients + streaming + request preflight + ├── commands/ # Shared slash-command registry + help rendering ├── compat-harness/ # TS manifest extraction harness ├── mock-anthropic-service/ # Deterministic local Anthropic-compatible mock - ├── plugins/ # Plugin registry and hook wiring primitives - ├── runtime/ # Session, config, permissions, MCP, prompts + ├── plugins/ # Plugin metadata, manager, install/enable/disable surfaces + ├── runtime/ # Session, config, permissions, MCP, prompts, auth/runtime loop ├── rusty-claude-cli/ # Main CLI binary (`claw`) ├── telemetry/ # Session tracing and usage telemetry types - └── tools/ # Built-in tool implementations + └── tools/ # Built-in tools, skill resolution, tool search, agent runtime surfaces ``` ### Crate Responsibilities -- **api** — HTTP client, SSE stream parser, request/response types, auth (API key + OAuth bearer) -- **commands** — Slash command definitions and help text generation -- **compat-harness** — Extracts tool/prompt manifests from upstream TS source -- **mock-anthropic-service** — Deterministic `/v1/messages` mock for CLI parity tests and local harness runs -- **plugins** — Plugin metadata, registries, and hook integration surfaces -- **runtime** — `ConversationRuntime` agentic loop, `ConfigLoader` hierarchy, `Session` persistence, permission policy, MCP client, system prompt assembly, usage tracking -- **rusty-claude-cli** — REPL, one-shot prompt, streaming display, tool call rendering, CLI argument parsing -- **telemetry** — Session trace events and supporting telemetry payloads -- **tools** — Tool specs + execution: Bash, ReadFile, WriteFile, EditFile, GlobSearch, GrepSearch, WebSearch, WebFetch, Agent, TodoWrite, NotebookEdit, Skill, ToolSearch, REPL runtimes +- **api** — provider clients, SSE streaming, request/response types, auth (API key + OAuth bearer), request-size/context-window preflight +- **commands** — slash command definitions, parsing, help text generation, JSON/text command rendering +- **compat-harness** — extracts tool/prompt manifests from upstream TS source +- **mock-anthropic-service** — deterministic `/v1/messages` mock for CLI parity tests and local harness runs +- **plugins** — plugin metadata, install/enable/disable/update flows, plugin tool definitions, hook integration surfaces +- **runtime** — `ConversationRuntime`, config loading, session persistence, permission policy, MCP client lifecycle, system prompt assembly, usage tracking +- **rusty-claude-cli** — REPL, one-shot prompt, direct CLI subcommands, streaming display, tool call rendering, CLI argument parsing +- **telemetry** — session trace events and supporting telemetry payloads +- **tools** — tool specs + execution: Bash, ReadFile, WriteFile, EditFile, GlobSearch, GrepSearch, WebSearch, WebFetch, Agent, TodoWrite, NotebookEdit, Skill, ToolSearch, and runtime-facing tool discovery ## Stats diff --git a/rust/TUI-ENHANCEMENT-PLAN.md b/rust/TUI-ENHANCEMENT-PLAN.md index d2a06574..a9de6c35 100644 --- a/rust/TUI-ENHANCEMENT-PLAN.md +++ b/rust/TUI-ENHANCEMENT-PLAN.md @@ -20,12 +20,14 @@ This plan covers a comprehensive analysis of the current terminal user interface ### Current TUI Components +> Note: The legacy prototype files `app.rs` and `args.rs` were removed on 2026-04-05. +> References below describe future extraction targets, not current tracked source files. + | Component | File | What It Does Today | Quality | |---|---|---|---| | **Input** | `input.rs` (269 lines) | `rustyline`-based line editor with slash-command tab completion, Shift+Enter newline, history | ✅ Solid | | **Rendering** | `render.rs` (641 lines) | Markdown→terminal rendering (headings, lists, tables, code blocks with syntect highlighting, blockquotes), spinner widget | ✅ Good | | **App/REPL loop** | `main.rs` (3,159 lines) | The monolithic `LiveCli` struct: REPL loop, all slash command handlers, streaming output, tool call display, permission prompting, session management | ⚠️ Monolithic | -| **Alt App** | `app.rs` (398 lines) | An earlier `CliApp` prototype with `ConversationClient`, stream event handling, `TerminalRenderer`, output format support | ⚠️ Appears unused/legacy | ### Key Dependencies @@ -56,7 +58,7 @@ This plan covers a comprehensive analysis of the current terminal user interface 8. **Streaming is char-by-char with artificial delay** — `stream_markdown` sleeps 8ms per whitespace-delimited chunk 9. **No color theme customization** — hardcoded `ColorTheme::default()` 10. **No resize handling** — no terminal size awareness for wrapping, truncation, or layout -11. **Dual app structs** — `app.rs` has a separate `CliApp` that duplicates `LiveCli` from `main.rs` +11. **Historical dual app split** — the repo previously carried a separate `CliApp` prototype alongside `LiveCli`; the prototype is gone, but the monolithic `main.rs` still needs extraction 12. **No pager for long outputs** — `/status`, `/config`, `/memory` can overflow the viewport 13. **Tool results not collapsible** — large bash outputs flood the screen 14. **No thinking/reasoning indicator** — when the model is in "thinking" mode, no visual distinction @@ -73,8 +75,8 @@ This plan covers a comprehensive analysis of the current terminal user interface | Task | Description | Effort | |---|---|---| | 0.1 | **Extract `LiveCli` into `app.rs`** — Move the entire `LiveCli` struct, its impl, and helpers (`format_*`, `render_*`, session management) out of `main.rs` into focused modules: `app.rs` (core), `format.rs` (report formatting), `session_manager.rs` (session CRUD) | M | -| 0.2 | **Remove or merge the legacy `CliApp`** — The existing `app.rs` has an unused `CliApp` with its own `ConversationClient`-based rendering. Either delete it or merge its unique features (stream event handler pattern) into the active `LiveCli` | S | -| 0.3 | **Extract `main.rs` arg parsing** — The current `parse_args()` is a hand-rolled parser that duplicates the clap-based `args.rs`. Consolidate on the hand-rolled parser (it's more feature-complete) and move it to `args.rs`, or adopt clap fully | S | +| 0.2 | **Keep the legacy `CliApp` removed** — The old `CliApp` prototype has already been deleted; if any unique ideas remain valuable (for example stream event handler patterns), reintroduce them intentionally inside the active `LiveCli` extraction rather than restoring the old file wholesale | S | +| 0.3 | **Extract `main.rs` arg parsing** — The current `parse_args()` is still a hand-rolled parser in `main.rs`. If parsing is extracted later, do it into a newly-introduced module intentionally rather than reviving the removed prototype `args.rs` by accident | S | | 0.4 | **Create a `tui/` module** — Introduce `crates/rusty-claude-cli/src/tui/mod.rs` as the namespace for all new TUI components: `status_bar.rs`, `layout.rs`, `tool_panel.rs`, etc. | S | ### Phase 1: Status Bar & Live HUD @@ -214,7 +216,7 @@ crates/rusty-claude-cli/src/ | Terminal compatibility issues (tmux, SSH, Windows) | Rely on crossterm's abstraction; test in degraded environments | | Performance regression with rich rendering | Profile before/after; keep the fast path (raw streaming) always available | | Scope creep into Phase 6 | Ship Phases 0–3 as a coherent release before starting Phase 6 | -| `app.rs` vs `main.rs` confusion | Phase 0.2 explicitly resolves this by removing the legacy `CliApp` | +| Historical `app.rs` vs `main.rs` confusion | Keep the legacy prototype removed and avoid reintroducing a second app surface accidentally during extraction | --- diff --git a/rust/crates/api/src/error.rs b/rust/crates/api/src/error.rs index 76498891..2aa84d94 100644 --- a/rust/crates/api/src/error.rs +++ b/rust/crates/api/src/error.rs @@ -2,22 +2,50 @@ use std::env::VarError; use std::fmt::{Display, Formatter}; use std::time::Duration; +const GENERIC_FATAL_WRAPPER_MARKERS: &[&str] = &[ + "something went wrong while processing your request", + "please try again, or use /new to start a fresh session", +]; + +const CONTEXT_WINDOW_ERROR_MARKERS: &[&str] = &[ + "maximum context length", + "context window", + "context length", + "too many tokens", + "prompt is too long", + "input is too long", + "request is too large", +]; + #[derive(Debug)] pub enum ApiError { MissingCredentials { provider: &'static str, env_vars: &'static [&'static str], }, + ContextWindowExceeded { + model: String, + estimated_input_tokens: u32, + requested_output_tokens: u32, + estimated_total_tokens: u32, + context_window_tokens: u32, + }, ExpiredOAuthToken, Auth(String), InvalidApiKeyEnv(VarError), Http(reqwest::Error), Io(std::io::Error), - Json(serde_json::Error), + Json { + provider: String, + model: String, + body_snippet: String, + source: serde_json::Error, + }, Api { status: reqwest::StatusCode, error_type: Option, message: Option, + request_id: Option, body: String, retryable: bool, }, @@ -41,6 +69,25 @@ impl ApiError { Self::MissingCredentials { provider, env_vars } } + /// Build a `Self::Json` enriched with the provider name, the model that + /// was requested, and the first 200 characters of the raw response body so + /// that callers can diagnose deserialization failures without re-running + /// the request. + #[must_use] + pub fn json_deserialize( + provider: impl Into, + model: impl Into, + body: &str, + source: serde_json::Error, + ) -> Self { + Self::Json { + provider: provider.into(), + model: model.into(), + body_snippet: truncate_body_snippet(body, 200), + source, + } + } + #[must_use] pub fn is_retryable(&self) -> bool { match self { @@ -48,11 +95,106 @@ impl ApiError { Self::Api { retryable, .. } => *retryable, Self::RetriesExhausted { last_error, .. } => last_error.is_retryable(), Self::MissingCredentials { .. } + | Self::ContextWindowExceeded { .. } | Self::ExpiredOAuthToken | Self::Auth(_) | Self::InvalidApiKeyEnv(_) | Self::Io(_) - | Self::Json(_) + | Self::Json { .. } + | Self::InvalidSseFrame(_) + | Self::BackoffOverflow { .. } => false, + } + } + + #[must_use] + pub fn request_id(&self) -> Option<&str> { + match self { + Self::Api { request_id, .. } => request_id.as_deref(), + Self::RetriesExhausted { last_error, .. } => last_error.request_id(), + Self::MissingCredentials { .. } + | Self::ContextWindowExceeded { .. } + | Self::ExpiredOAuthToken + | Self::Auth(_) + | Self::InvalidApiKeyEnv(_) + | Self::Http(_) + | Self::Io(_) + | Self::Json { .. } + | Self::InvalidSseFrame(_) + | Self::BackoffOverflow { .. } => None, + } + } + + #[must_use] + pub fn safe_failure_class(&self) -> &'static str { + match self { + Self::RetriesExhausted { .. } if self.is_context_window_failure() => "context_window", + Self::RetriesExhausted { .. } if self.is_generic_fatal_wrapper() => { + "provider_retry_exhausted" + } + Self::RetriesExhausted { last_error, .. } => last_error.safe_failure_class(), + Self::MissingCredentials { .. } | Self::ExpiredOAuthToken | Self::Auth(_) => { + "provider_auth" + } + Self::Api { status, .. } if matches!(status.as_u16(), 401 | 403) => "provider_auth", + Self::ContextWindowExceeded { .. } => "context_window", + Self::Api { .. } if self.is_context_window_failure() => "context_window", + Self::Api { status, .. } if status.as_u16() == 429 => "provider_rate_limit", + Self::Api { .. } if self.is_generic_fatal_wrapper() => "provider_internal", + Self::Api { .. } => "provider_error", + Self::Http(_) | Self::InvalidSseFrame(_) | Self::BackoffOverflow { .. } => { + "provider_transport" + } + Self::InvalidApiKeyEnv(_) | Self::Io(_) | Self::Json { .. } => "runtime_io", + } + } + + #[must_use] + pub fn is_generic_fatal_wrapper(&self) -> bool { + match self { + Self::Api { message, body, .. } => { + message + .as_deref() + .is_some_and(looks_like_generic_fatal_wrapper) + || looks_like_generic_fatal_wrapper(body) + } + Self::RetriesExhausted { last_error, .. } => last_error.is_generic_fatal_wrapper(), + Self::MissingCredentials { .. } + | Self::ContextWindowExceeded { .. } + | Self::ExpiredOAuthToken + | Self::Auth(_) + | Self::InvalidApiKeyEnv(_) + | Self::Http(_) + | Self::Io(_) + | Self::Json { .. } + | Self::InvalidSseFrame(_) + | Self::BackoffOverflow { .. } => false, + } + } + + #[must_use] + pub fn is_context_window_failure(&self) -> bool { + match self { + Self::ContextWindowExceeded { .. } => true, + Self::Api { + status, + message, + body, + .. + } => { + matches!(status.as_u16(), 400 | 413 | 422) + && (message + .as_deref() + .is_some_and(looks_like_context_window_error) + || looks_like_context_window_error(body)) + } + Self::RetriesExhausted { last_error, .. } => last_error.is_context_window_failure(), + Self::MissingCredentials { .. } + | Self::ExpiredOAuthToken + | Self::Auth(_) + | Self::InvalidApiKeyEnv(_) + | Self::Http(_) + | Self::Io(_) + | Self::Json { .. } | Self::InvalidSseFrame(_) | Self::BackoffOverflow { .. } => false, } @@ -62,10 +204,36 @@ impl ApiError { impl Display for ApiError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - Self::MissingCredentials { provider, env_vars } => write!( + Self::MissingCredentials { provider, env_vars } => { + write!( + f, + "missing {provider} credentials; export {} before calling the {provider} API", + env_vars.join(" or ") + )?; + if cfg!(target_os = "windows") { + if let Some(primary) = env_vars.first() { + write!( + f, + " (on Windows, environment variables set in PowerShell only persist for the current session; use `setx {primary} ` to make it permanent, then open a new terminal, or place a `.env` file containing `{primary}=` in the current working directory)" + )?; + } else { + write!( + f, + " (on Windows, environment variables set in PowerShell only persist for the current session; use `setx` to make them permanent, then open a new terminal, or place a `.env` file in the current working directory)" + )?; + } + } + Ok(()) + } + Self::ContextWindowExceeded { + model, + estimated_input_tokens, + requested_output_tokens, + estimated_total_tokens, + context_window_tokens, + } => write!( f, - "missing {provider} credentials; export {} before calling the {provider} API", - env_vars.join(" or ") + "context_window_blocked for {model}: estimated input {estimated_input_tokens} + requested output {requested_output_tokens} = {estimated_total_tokens} tokens exceeds the {context_window_tokens}-token context window; compact the session or reduce request size before retrying" ), Self::ExpiredOAuthToken => { write!( @@ -79,19 +247,37 @@ impl Display for ApiError { } Self::Http(error) => write!(f, "http error: {error}"), Self::Io(error) => write!(f, "io error: {error}"), - Self::Json(error) => write!(f, "json error: {error}"), + Self::Json { + provider, + model, + body_snippet, + source, + } => write!( + f, + "failed to parse {provider} response for model {model}: {source}; first 200 chars of body: {body_snippet}" + ), Self::Api { status, error_type, message, + request_id, body, .. - } => match (error_type, message) { - (Some(error_type), Some(message)) => { - write!(f, "api returned {status} ({error_type}): {message}") + } => { + if let (Some(error_type), Some(message)) = (error_type, message) { + write!(f, "api returned {status} ({error_type})")?; + if let Some(request_id) = request_id { + write!(f, " [trace {request_id}]")?; + } + write!(f, ": {message}") + } else { + write!(f, "api returned {status}")?; + if let Some(request_id) = request_id { + write!(f, " [trace {request_id}]")?; + } + write!(f, ": {body}") } - _ => write!(f, "api returned {status}: {body}"), - }, + } Self::RetriesExhausted { attempts, last_error, @@ -124,7 +310,12 @@ impl From for ApiError { impl From for ApiError { fn from(value: serde_json::Error) -> Self { - Self::Json(value) + Self::Json { + provider: "unknown".to_string(), + model: "unknown".to_string(), + body_snippet: String::new(), + source: value, + } } } @@ -133,3 +324,163 @@ impl From for ApiError { Self::InvalidApiKeyEnv(value) } } + +fn looks_like_generic_fatal_wrapper(text: &str) -> bool { + let lowered = text.to_ascii_lowercase(); + GENERIC_FATAL_WRAPPER_MARKERS + .iter() + .any(|marker| lowered.contains(marker)) +} + +fn looks_like_context_window_error(text: &str) -> bool { + let lowered = text.to_ascii_lowercase(); + CONTEXT_WINDOW_ERROR_MARKERS + .iter() + .any(|marker| lowered.contains(marker)) +} + +/// Truncate `body` so the resulting snippet contains at most `max_chars` +/// characters (counted by Unicode scalar values, not bytes), preserving the +/// leading slice of the body that the caller most often needs to inspect. +fn truncate_body_snippet(body: &str, max_chars: usize) -> String { + let mut taken_chars = 0; + let mut byte_end = 0; + for (offset, character) in body.char_indices() { + if taken_chars >= max_chars { + break; + } + taken_chars += 1; + byte_end = offset + character.len_utf8(); + } + if taken_chars >= max_chars && byte_end < body.len() { + format!("{}…", &body[..byte_end]) + } else { + body[..byte_end].to_string() + } +} + +#[cfg(test)] +mod tests { + use super::{truncate_body_snippet, ApiError}; + + #[test] + fn json_deserialize_error_includes_provider_model_and_truncated_body_snippet() { + let raw_body = format!("{}{}", "x".repeat(190), "_TAIL_PAST_200_CHARS_MARKER_"); + let source = serde_json::from_str::("{not json") + .expect_err("invalid json should fail to parse"); + + let error = ApiError::json_deserialize("Anthropic", "claude-opus-4-6", &raw_body, source); + let rendered = error.to_string(); + + assert!( + rendered.starts_with("failed to parse Anthropic response for model claude-opus-4-6: "), + "rendered error should lead with provider and model: {rendered}" + ); + assert!( + rendered.contains("first 200 chars of body: "), + "rendered error should label the body snippet: {rendered}" + ); + let snippet = rendered + .split("first 200 chars of body: ") + .nth(1) + .expect("snippet section should be present"); + assert!( + snippet.starts_with(&"x".repeat(190)), + "snippet should preserve the leading characters of the body: {snippet}" + ); + assert!( + snippet.ends_with('…'), + "snippet should signal truncation with an ellipsis: {snippet}" + ); + assert!( + !snippet.contains("_TAIL_PAST_200_CHARS_MARKER_"), + "snippet should drop characters past the 200-char cap: {snippet}" + ); + assert_eq!(error.safe_failure_class(), "runtime_io"); + assert_eq!(error.request_id(), None); + assert!(!error.is_retryable()); + } + + #[test] + fn truncate_body_snippet_keeps_short_bodies_intact() { + assert_eq!(truncate_body_snippet("hello", 200), "hello"); + assert_eq!(truncate_body_snippet("", 200), ""); + } + + #[test] + fn truncate_body_snippet_caps_long_bodies_at_max_chars() { + let body = "a".repeat(250); + let snippet = truncate_body_snippet(&body, 200); + assert_eq!(snippet.chars().count(), 201, "200 chars + ellipsis"); + assert!(snippet.ends_with('…')); + assert!(snippet.starts_with(&"a".repeat(200))); + } + + #[test] + fn truncate_body_snippet_does_not_split_multibyte_characters() { + let body = "한글한글한글한글한글한글"; + let snippet = truncate_body_snippet(body, 4); + assert_eq!(snippet, "한글한글…"); + } + + #[test] + fn detects_generic_fatal_wrapper_and_classifies_it_as_provider_internal() { + let error = ApiError::Api { + status: reqwest::StatusCode::INTERNAL_SERVER_ERROR, + error_type: Some("api_error".to_string()), + message: Some( + "Something went wrong while processing your request. Please try again, or use /new to start a fresh session." + .to_string(), + ), + request_id: Some("req_jobdori_123".to_string()), + body: String::new(), + retryable: true, + }; + + assert!(error.is_generic_fatal_wrapper()); + assert_eq!(error.safe_failure_class(), "provider_internal"); + assert_eq!(error.request_id(), Some("req_jobdori_123")); + assert!(error.to_string().contains("[trace req_jobdori_123]")); + } + + #[test] + fn retries_exhausted_preserves_nested_request_id_and_failure_class() { + let error = ApiError::RetriesExhausted { + attempts: 3, + last_error: Box::new(ApiError::Api { + status: reqwest::StatusCode::BAD_GATEWAY, + error_type: Some("api_error".to_string()), + message: Some( + "Something went wrong while processing your request. Please try again, or use /new to start a fresh session." + .to_string(), + ), + request_id: Some("req_nested_456".to_string()), + body: String::new(), + retryable: true, + }), + }; + + assert!(error.is_generic_fatal_wrapper()); + assert_eq!(error.safe_failure_class(), "provider_retry_exhausted"); + assert_eq!(error.request_id(), Some("req_nested_456")); + } + + #[test] + fn classifies_provider_context_window_errors() { + let error = ApiError::Api { + status: reqwest::StatusCode::BAD_REQUEST, + error_type: Some("invalid_request_error".to_string()), + message: Some( + "This model's maximum context length is 200000 tokens, but your request used 230000 tokens." + .to_string(), + ), + request_id: Some("req_ctx_123".to_string()), + body: String::new(), + retryable: false, + }; + + assert!(error.is_context_window_failure()); + assert_eq!(error.safe_failure_class(), "context_window"); + assert_eq!(error.request_id(), Some("req_ctx_123")); + } +} diff --git a/rust/crates/api/src/http_client.rs b/rust/crates/api/src/http_client.rs new file mode 100644 index 00000000..508a5775 --- /dev/null +++ b/rust/crates/api/src/http_client.rs @@ -0,0 +1,344 @@ +use crate::error::ApiError; + +const HTTP_PROXY_KEYS: [&str; 2] = ["HTTP_PROXY", "http_proxy"]; +const HTTPS_PROXY_KEYS: [&str; 2] = ["HTTPS_PROXY", "https_proxy"]; +const NO_PROXY_KEYS: [&str; 2] = ["NO_PROXY", "no_proxy"]; + +/// Snapshot of the proxy-related environment variables that influence the +/// outbound HTTP client. Captured up front so callers can inspect, log, and +/// test the resolved configuration without re-reading the process environment. +/// +/// When `proxy_url` is set it acts as a single catch-all proxy for both +/// HTTP and HTTPS traffic, taking precedence over the per-scheme fields. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct ProxyConfig { + pub http_proxy: Option, + pub https_proxy: Option, + pub no_proxy: Option, + /// Optional unified proxy URL that applies to both HTTP and HTTPS. + /// When set, this takes precedence over `http_proxy` and `https_proxy`. + pub proxy_url: Option, +} + +impl ProxyConfig { + /// Read proxy settings from the live process environment, honouring both + /// the upper- and lower-case spellings used by curl, git, and friends. + #[must_use] + pub fn from_env() -> Self { + Self::from_lookup(|key| std::env::var(key).ok()) + } + + /// Create a proxy configuration from a single URL that applies to both + /// HTTP and HTTPS traffic. This is the config-file alternative to setting + /// `HTTP_PROXY` and `HTTPS_PROXY` environment variables separately. + #[must_use] + pub fn from_proxy_url(url: impl Into) -> Self { + Self { + proxy_url: Some(url.into()), + ..Self::default() + } + } + + fn from_lookup(mut lookup: F) -> Self + where + F: FnMut(&str) -> Option, + { + Self { + http_proxy: first_non_empty(&HTTP_PROXY_KEYS, &mut lookup), + https_proxy: first_non_empty(&HTTPS_PROXY_KEYS, &mut lookup), + no_proxy: first_non_empty(&NO_PROXY_KEYS, &mut lookup), + proxy_url: None, + } + } + + #[must_use] + pub fn is_empty(&self) -> bool { + self.proxy_url.is_none() && self.http_proxy.is_none() && self.https_proxy.is_none() + } +} + +/// Build a `reqwest::Client` that honours the standard `HTTP_PROXY`, +/// `HTTPS_PROXY`, and `NO_PROXY` environment variables. When no proxy is +/// configured the client behaves identically to `reqwest::Client::new()`. +pub fn build_http_client() -> Result { + build_http_client_with(&ProxyConfig::from_env()) +} + +/// Infallible counterpart to [`build_http_client`] for constructors that +/// historically returned `Self` rather than `Result`. When the proxy +/// configuration is malformed we fall back to a default client so that +/// callers retain the previous behaviour and the failure surfaces on the +/// first outbound request instead of at construction time. +#[must_use] +pub fn build_http_client_or_default() -> reqwest::Client { + build_http_client().unwrap_or_else(|_| reqwest::Client::new()) +} + +/// Build a `reqwest::Client` from an explicit [`ProxyConfig`]. Used by tests +/// and by callers that want to override process-level environment lookups. +/// +/// When `config.proxy_url` is set it overrides the per-scheme `http_proxy` +/// and `https_proxy` fields and is registered as both an HTTP and HTTPS +/// proxy so a single value can route every outbound request. +pub fn build_http_client_with(config: &ProxyConfig) -> Result { + let mut builder = reqwest::Client::builder().no_proxy(); + + let no_proxy = config + .no_proxy + .as_deref() + .and_then(reqwest::NoProxy::from_string); + + let (http_proxy_url, https_proxy_url) = match config.proxy_url.as_deref() { + Some(unified) => (Some(unified), Some(unified)), + None => (config.http_proxy.as_deref(), config.https_proxy.as_deref()), + }; + + if let Some(url) = https_proxy_url { + let mut proxy = reqwest::Proxy::https(url)?; + if let Some(filter) = no_proxy.clone() { + proxy = proxy.no_proxy(Some(filter)); + } + builder = builder.proxy(proxy); + } + + if let Some(url) = http_proxy_url { + let mut proxy = reqwest::Proxy::http(url)?; + if let Some(filter) = no_proxy.clone() { + proxy = proxy.no_proxy(Some(filter)); + } + builder = builder.proxy(proxy); + } + + Ok(builder.build()?) +} + +fn first_non_empty(keys: &[&str], lookup: &mut F) -> Option +where + F: FnMut(&str) -> Option, +{ + keys.iter() + .find_map(|key| lookup(key).filter(|value| !value.is_empty())) +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use super::{build_http_client_with, ProxyConfig}; + + fn config_from_map(pairs: &[(&str, &str)]) -> ProxyConfig { + let map: HashMap = pairs + .iter() + .map(|(key, value)| ((*key).to_string(), (*value).to_string())) + .collect(); + ProxyConfig::from_lookup(|key| map.get(key).cloned()) + } + + #[test] + fn proxy_config_is_empty_when_no_env_vars_are_set() { + // given + let config = config_from_map(&[]); + + // when + let empty = config.is_empty(); + + // then + assert!(empty); + assert_eq!(config, ProxyConfig::default()); + } + + #[test] + fn proxy_config_reads_uppercase_http_https_and_no_proxy() { + // given + let pairs = [ + ("HTTP_PROXY", "http://proxy.internal:3128"), + ("HTTPS_PROXY", "http://secure.internal:3129"), + ("NO_PROXY", "localhost,127.0.0.1,.corp"), + ]; + + // when + let config = config_from_map(&pairs); + + // then + assert_eq!( + config.http_proxy.as_deref(), + Some("http://proxy.internal:3128") + ); + assert_eq!( + config.https_proxy.as_deref(), + Some("http://secure.internal:3129") + ); + assert_eq!( + config.no_proxy.as_deref(), + Some("localhost,127.0.0.1,.corp") + ); + assert!(!config.is_empty()); + } + + #[test] + fn proxy_config_falls_back_to_lowercase_keys() { + // given + let pairs = [ + ("http_proxy", "http://lower.internal:3128"), + ("https_proxy", "http://lower-secure.internal:3129"), + ("no_proxy", ".lower"), + ]; + + // when + let config = config_from_map(&pairs); + + // then + assert_eq!( + config.http_proxy.as_deref(), + Some("http://lower.internal:3128") + ); + assert_eq!( + config.https_proxy.as_deref(), + Some("http://lower-secure.internal:3129") + ); + assert_eq!(config.no_proxy.as_deref(), Some(".lower")); + } + + #[test] + fn proxy_config_prefers_uppercase_over_lowercase_when_both_set() { + // given + let pairs = [ + ("HTTP_PROXY", "http://upper.internal:3128"), + ("http_proxy", "http://lower.internal:3128"), + ]; + + // when + let config = config_from_map(&pairs); + + // then + assert_eq!( + config.http_proxy.as_deref(), + Some("http://upper.internal:3128") + ); + } + + #[test] + fn proxy_config_treats_empty_strings_as_unset() { + // given + let pairs = [("HTTP_PROXY", ""), ("http_proxy", "")]; + + // when + let config = config_from_map(&pairs); + + // then + assert!(config.http_proxy.is_none()); + } + + #[test] + fn build_http_client_succeeds_when_no_proxy_is_configured() { + // given + let config = ProxyConfig::default(); + + // when + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); + } + + #[test] + fn build_http_client_succeeds_with_valid_http_and_https_proxies() { + // given + let config = ProxyConfig { + http_proxy: Some("http://proxy.internal:3128".to_string()), + https_proxy: Some("http://secure.internal:3129".to_string()), + no_proxy: Some("localhost,127.0.0.1".to_string()), + proxy_url: None, + }; + + // when + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); + } + + #[test] + fn build_http_client_returns_http_error_for_invalid_proxy_url() { + // given + let config = ProxyConfig { + http_proxy: None, + https_proxy: Some("not a url".to_string()), + no_proxy: None, + proxy_url: None, + }; + + // when + let result = build_http_client_with(&config); + + // then + let error = result.expect_err("invalid proxy URL must be reported as a build failure"); + assert!( + matches!(error, crate::error::ApiError::Http(_)), + "expected ApiError::Http for invalid proxy URL, got: {error:?}" + ); + } + + #[test] + fn from_proxy_url_sets_unified_field_and_leaves_per_scheme_empty() { + // given / when + let config = ProxyConfig::from_proxy_url("http://unified.internal:3128"); + + // then + assert_eq!( + config.proxy_url.as_deref(), + Some("http://unified.internal:3128") + ); + assert!(config.http_proxy.is_none()); + assert!(config.https_proxy.is_none()); + assert!(!config.is_empty()); + } + + #[test] + fn build_http_client_succeeds_with_unified_proxy_url() { + // given + let config = ProxyConfig { + proxy_url: Some("http://unified.internal:3128".to_string()), + no_proxy: Some("localhost".to_string()), + ..ProxyConfig::default() + }; + + // when + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); + } + + #[test] + fn proxy_url_takes_precedence_over_per_scheme_fields() { + // given – both per-scheme and unified are set + let config = ProxyConfig { + http_proxy: Some("http://per-scheme.internal:1111".to_string()), + https_proxy: Some("http://per-scheme.internal:2222".to_string()), + no_proxy: None, + proxy_url: Some("http://unified.internal:3128".to_string()), + }; + + // when – building succeeds (the unified URL is valid) + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); + } + + #[test] + fn build_http_client_returns_error_for_invalid_unified_proxy_url() { + // given + let config = ProxyConfig::from_proxy_url("not a url"); + + // when + let result = build_http_client_with(&config); + + // then + assert!( + matches!(result, Err(crate::error::ApiError::Http(_))), + "invalid unified proxy URL should fail: {result:?}" + ); + } +} diff --git a/rust/crates/api/src/lib.rs b/rust/crates/api/src/lib.rs index cac2f539..bcf3e1b2 100644 --- a/rust/crates/api/src/lib.rs +++ b/rust/crates/api/src/lib.rs @@ -1,5 +1,6 @@ mod client; mod error; +mod http_client; mod prompt_cache; mod providers; mod sse; @@ -10,6 +11,9 @@ pub use client::{ resolve_startup_auth_source, MessageStream, OAuthTokenSet, ProviderClient, }; pub use error::ApiError; +pub use http_client::{ + build_http_client, build_http_client_or_default, build_http_client_with, ProxyConfig, +}; pub use prompt_cache::{ CacheBreakEvent, PromptCache, PromptCacheConfig, PromptCachePaths, PromptCacheRecord, PromptCacheStats, @@ -17,7 +21,8 @@ pub use prompt_cache::{ pub use providers::anthropic::{AnthropicClient, AnthropicClient as ApiClient, AuthSource}; pub use providers::openai_compat::{OpenAiCompatClient, OpenAiCompatConfig}; pub use providers::{ - detect_provider_kind, max_tokens_for_model, resolve_model_alias, ProviderKind, + detect_provider_kind, max_tokens_for_model, max_tokens_for_model_with_override, + resolve_model_alias, ProviderKind, }; pub use sse::{parse_frame, SseParser}; pub use types::{ diff --git a/rust/crates/api/src/providers/anthropic.rs b/rust/crates/api/src/providers/anthropic.rs index f8b41ac0..31801690 100644 --- a/rust/crates/api/src/providers/anthropic.rs +++ b/rust/crates/api/src/providers/anthropic.rs @@ -1,4 +1,5 @@ use std::collections::VecDeque; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -12,18 +13,19 @@ use serde_json::{Map, Value}; use telemetry::{AnalyticsEvent, AnthropicRequestProfile, ClientIdentity, SessionTracer}; use crate::error::ApiError; +use crate::http_client::build_http_client_or_default; use crate::prompt_cache::{PromptCache, PromptCacheRecord, PromptCacheStats}; -use super::{Provider, ProviderFuture}; +use super::{model_token_limit, resolve_model_alias, Provider, ProviderFuture}; use crate::sse::SseParser; use crate::types::{MessageDeltaEvent, MessageRequest, MessageResponse, StreamEvent, Usage}; pub const DEFAULT_BASE_URL: &str = "https://api.anthropic.com"; const REQUEST_ID_HEADER: &str = "request-id"; const ALT_REQUEST_ID_HEADER: &str = "x-request-id"; -const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_millis(200); -const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(2); -const DEFAULT_MAX_RETRIES: u32 = 2; +const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_secs(1); +const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(128); +const DEFAULT_MAX_RETRIES: u32 = 8; #[derive(Debug, Clone, PartialEq, Eq)] pub enum AuthSource { @@ -127,7 +129,7 @@ impl AnthropicClient { #[must_use] pub fn new(api_key: impl Into) -> Self { Self { - http: reqwest::Client::new(), + http: build_http_client_or_default(), auth: AuthSource::ApiKey(api_key.into()), base_url: DEFAULT_BASE_URL.to_string(), max_retries: DEFAULT_MAX_RETRIES, @@ -143,7 +145,7 @@ impl AnthropicClient { #[must_use] pub fn from_auth(auth: AuthSource) -> Self { Self { - http: reqwest::Client::new(), + http: build_http_client_or_default(), auth, base_url: DEFAULT_BASE_URL.to_string(), max_retries: DEFAULT_MAX_RETRIES, @@ -294,12 +296,14 @@ impl AnthropicClient { } } - let response = self.send_with_retry(&request).await?; - let request_id = request_id_from_headers(response.headers()); - let mut response = response - .json::() - .await - .map_err(ApiError::from)?; + self.preflight_message_request(&request).await?; + + let http_response = self.send_with_retry(&request).await?; + let request_id = request_id_from_headers(http_response.headers()); + let body = http_response.text().await.map_err(ApiError::from)?; + let mut response = serde_json::from_str::(&body).map_err(|error| { + ApiError::json_deserialize("Anthropic", &request.model, &body, error) + })?; if response.request_id.is_none() { response.request_id = request_id; } @@ -337,13 +341,14 @@ impl AnthropicClient { &self, request: &MessageRequest, ) -> Result { + self.preflight_message_request(request).await?; let response = self .send_with_retry(&request.clone().with_streaming()) .await?; Ok(MessageStream { request_id: request_id_from_headers(response.headers()), response, - parser: SseParser::new(), + parser: SseParser::new().with_context("Anthropic", request.model.clone()), pending: VecDeque::new(), done: false, request: request.clone(), @@ -368,10 +373,10 @@ impl AnthropicClient { .await .map_err(ApiError::from)?; let response = expect_success(response).await?; - response - .json::() - .await - .map_err(ApiError::from) + let body = response.text().await.map_err(ApiError::from)?; + serde_json::from_str::(&body).map_err(|error| { + ApiError::json_deserialize("Anthropic OAuth (exchange)", "n/a", &body, error) + }) } pub async fn refresh_oauth_token( @@ -388,10 +393,10 @@ impl AnthropicClient { .await .map_err(ApiError::from)?; let response = expect_success(response).await?; - response - .json::() - .await - .map_err(ApiError::from) + let body = response.text().await.map_err(ApiError::from)?; + serde_json::from_str::(&body).map_err(|error| { + ApiError::json_deserialize("Anthropic OAuth (refresh)", "n/a", &body, error) + }) } async fn send_with_retry( @@ -449,7 +454,7 @@ impl AnthropicClient { break; } - tokio::time::sleep(self.backoff_for_attempt(attempts)?).await; + tokio::time::sleep(self.jittered_backoff_for_attempt(attempts)?).await; } Err(ApiError::RetriesExhausted { @@ -463,18 +468,74 @@ impl AnthropicClient { request: &MessageRequest, ) -> Result { let request_url = format!("{}/v1/messages", self.base_url.trim_end_matches('/')); + let mut request_body = self.request_profile.render_json_body(request)?; + strip_unsupported_beta_body_fields(&mut request_body); + let request_builder = self.build_request(&request_url).json(&request_body); + request_builder.send().await.map_err(ApiError::from) + } + + fn build_request(&self, request_url: &str) -> reqwest::RequestBuilder { let request_builder = self .http - .post(&request_url) + .post(request_url) .header("content-type", "application/json"); let mut request_builder = self.auth.apply(request_builder); for (header_name, header_value) in self.request_profile.header_pairs() { request_builder = request_builder.header(header_name, header_value); } + request_builder + } - let request_body = self.request_profile.render_json_body(request)?; - request_builder = request_builder.json(&request_body); - request_builder.send().await.map_err(ApiError::from) + async fn preflight_message_request(&self, request: &MessageRequest) -> Result<(), ApiError> { + let Some(limit) = model_token_limit(&request.model) else { + return Ok(()); + }; + + let counted_input_tokens = match self.count_tokens(request).await { + Ok(count) => count, + Err(_) => return Ok(()), + }; + let estimated_total_tokens = counted_input_tokens.saturating_add(request.max_tokens); + if estimated_total_tokens > limit.context_window_tokens { + return Err(ApiError::ContextWindowExceeded { + model: resolve_model_alias(&request.model), + estimated_input_tokens: counted_input_tokens, + requested_output_tokens: request.max_tokens, + estimated_total_tokens, + context_window_tokens: limit.context_window_tokens, + }); + } + + Ok(()) + } + + async fn count_tokens(&self, request: &MessageRequest) -> Result { + #[derive(serde::Deserialize)] + struct CountTokensResponse { + input_tokens: u32, + } + + let request_url = format!("{}/v1/messages/count_tokens", self.base_url.trim_end_matches('/')); + let mut request_body = self.request_profile.render_json_body(request)?; + strip_unsupported_beta_body_fields(&mut request_body); + let response = self + .build_request(&request_url) + .json(&request_body) + .send() + .await + .map_err(ApiError::from)?; + + let response = expect_success(response).await?; + let body = response.text().await.map_err(ApiError::from)?; + let parsed = serde_json::from_str::(&body).map_err(|error| { + ApiError::json_deserialize( + "Anthropic count_tokens", + &request.model, + &body, + error, + ) + })?; + Ok(parsed.input_tokens) } fn record_request_failure(&self, attempt: u32, error: &ApiError) { @@ -509,6 +570,40 @@ impl AnthropicClient { .checked_mul(multiplier) .map_or(self.max_backoff, |delay| delay.min(self.max_backoff))) } + + fn jittered_backoff_for_attempt(&self, attempt: u32) -> Result { + let base = self.backoff_for_attempt(attempt)?; + Ok(base + jitter_for_base(base)) + } +} + +/// Process-wide counter that guarantees distinct jitter samples even when +/// the system clock resolution is coarser than consecutive retry sleeps. +static JITTER_COUNTER: AtomicU64 = AtomicU64::new(0); + +/// Returns a random additive jitter in `[0, base]` to decorrelate retries +/// from multiple concurrent clients. Entropy is drawn from the nanosecond +/// wall clock mixed with a monotonic counter and run through a splitmix64 +/// finalizer; adequate for retry jitter (no cryptographic requirement). +fn jitter_for_base(base: Duration) -> Duration { + let base_nanos = u64::try_from(base.as_nanos()).unwrap_or(u64::MAX); + if base_nanos == 0 { + return Duration::ZERO; + } + let raw_nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|elapsed| u64::try_from(elapsed.as_nanos()).unwrap_or(u64::MAX)) + .unwrap_or(0); + let tick = JITTER_COUNTER.fetch_add(1, Ordering::Relaxed); + // splitmix64 finalizer — mixes the low bits so large bases still see + // jitter across their full range instead of being clamped to subsec nanos. + let mut mixed = raw_nanos.wrapping_add(tick).wrapping_add(0x9E37_79B9_7F4A_7C15); + mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + mixed ^= mixed >> 31; + // Inclusive upper bound: jitter may equal `base`, matching "up to base". + let jitter_nanos = mixed % base_nanos.saturating_add(1); + Duration::from_nanos(jitter_nanos) } impl AuthSource { @@ -673,7 +768,7 @@ fn now_unix_timestamp() -> u64 { fn read_env_non_empty(key: &str) -> Result, ApiError> { match std::env::var(key) { Ok(value) if !value.is_empty() => Ok(Some(value)), - Ok(_) | Err(std::env::VarError::NotPresent) => Ok(None), + Ok(_) | Err(std::env::VarError::NotPresent) => Ok(super::dotenv_value(key)), Err(error) => Err(ApiError::from(error)), } } @@ -805,6 +900,7 @@ async fn expect_success(response: reqwest::Response) -> Result(&body).ok(); let retryable = is_retryable_status(status); @@ -817,6 +913,7 @@ async fn expect_success(response: reqwest::Response) -> Result bool { matches!(status.as_u16(), 408 | 409 | 429 | 500 | 502 | 503 | 504) } +/// Remove beta-only body fields that the standard `/v1/messages` and +/// `/v1/messages/count_tokens` endpoints reject as `Extra inputs are not +/// permitted`. The `betas` opt-in is communicated via the `anthropic-beta` +/// HTTP header on these endpoints, never as a JSON body field. +fn strip_unsupported_beta_body_fields(body: &mut Value) { + if let Some(object) = body.as_object_mut() { + object.remove("betas"); + } +} + #[derive(Debug, Deserialize)] struct AnthropicErrorEnvelope { error: AnthropicErrorBody, @@ -1178,6 +1285,58 @@ mod tests { ); } + #[test] + fn jittered_backoff_stays_within_additive_bounds_and_varies() { + let client = AnthropicClient::new("test-key").with_retry_policy( + 8, + Duration::from_secs(1), + Duration::from_secs(128), + ); + let mut samples = Vec::with_capacity(64); + for _ in 0..64 { + let base = client.backoff_for_attempt(3).expect("base attempt 3"); + let jittered = client + .jittered_backoff_for_attempt(3) + .expect("jittered attempt 3"); + assert!( + jittered >= base, + "jittered delay {jittered:?} must be at least the base {base:?}" + ); + assert!( + jittered <= base * 2, + "jittered delay {jittered:?} must not exceed base*2 {:?}", + base * 2 + ); + samples.push(jittered); + } + let distinct: std::collections::HashSet<_> = samples.iter().collect(); + assert!( + distinct.len() > 1, + "jitter should produce varied delays across samples, got {samples:?}" + ); + } + + #[test] + fn default_retry_policy_matches_exponential_schedule() { + let client = AnthropicClient::new("test-key"); + assert_eq!( + client.backoff_for_attempt(1).expect("attempt 1"), + Duration::from_secs(1) + ); + assert_eq!( + client.backoff_for_attempt(2).expect("attempt 2"), + Duration::from_secs(2) + ); + assert_eq!( + client.backoff_for_attempt(3).expect("attempt 3"), + Duration::from_secs(4) + ); + assert_eq!( + client.backoff_for_attempt(8).expect("attempt 8"), + Duration::from_secs(128) + ); + } + #[test] fn retryable_statuses_are_detected() { assert!(super::is_retryable_status( @@ -1242,4 +1401,73 @@ mod tests { Some("Bearer proxy-token") ); } + + #[test] + fn strip_unsupported_beta_body_fields_removes_betas_array() { + let mut body = serde_json::json!({ + "model": "claude-sonnet-4-6", + "max_tokens": 1024, + "betas": ["claude-code-20250219", "prompt-caching-scope-2026-01-05"], + "metadata": {"source": "test"}, + }); + + super::strip_unsupported_beta_body_fields(&mut body); + + assert!( + body.get("betas").is_none(), + "betas body field must be stripped before sending to /v1/messages" + ); + assert_eq!( + body.get("model").and_then(serde_json::Value::as_str), + Some("claude-sonnet-4-6") + ); + assert_eq!(body["max_tokens"], serde_json::json!(1024)); + assert_eq!(body["metadata"]["source"], serde_json::json!("test")); + } + + #[test] + fn strip_unsupported_beta_body_fields_is_a_noop_when_betas_absent() { + let mut body = serde_json::json!({ + "model": "claude-sonnet-4-6", + "max_tokens": 1024, + }); + let original = body.clone(); + + super::strip_unsupported_beta_body_fields(&mut body); + + assert_eq!(body, original); + } + + #[test] + fn rendered_request_body_strips_betas_for_standard_messages_endpoint() { + let client = AnthropicClient::new("test-key").with_beta("tools-2026-04-01"); + let request = MessageRequest { + model: "claude-sonnet-4-6".to_string(), + max_tokens: 64, + messages: vec![], + system: None, + tools: None, + tool_choice: None, + stream: false, + }; + + let mut rendered = client + .request_profile() + .render_json_body(&request) + .expect("body should render"); + assert!( + rendered.get("betas").is_some(), + "render_json_body still emits betas; the strip helper guards the wire format", + ); + super::strip_unsupported_beta_body_fields(&mut rendered); + + assert!( + rendered.get("betas").is_none(), + "betas must not appear in /v1/messages request bodies" + ); + assert_eq!( + rendered.get("model").and_then(serde_json::Value::as_str), + Some("claude-sonnet-4-6") + ); + } } diff --git a/rust/crates/api/src/providers/mod.rs b/rust/crates/api/src/providers/mod.rs index d28febd3..36c01fd5 100644 --- a/rust/crates/api/src/providers/mod.rs +++ b/rust/crates/api/src/providers/mod.rs @@ -1,14 +1,19 @@ +#![allow(clippy::cast_possible_truncation)] use std::future::Future; use std::pin::Pin; +use serde::Serialize; + use crate::error::ApiError; use crate::types::{MessageRequest, MessageResponse}; pub mod anthropic; pub mod openai_compat; +#[allow(dead_code)] pub type ProviderFuture<'a, T> = Pin> + Send + 'a>>; +#[allow(dead_code)] pub trait Provider { type Stream; @@ -38,6 +43,12 @@ pub struct ProviderMetadata { pub default_base_url: &'static str, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ModelTokenLimit { + pub max_output_tokens: u32, + pub context_window_tokens: u32, +} + const MODEL_REGISTRY: &[(&str, ProviderMetadata)] = &[ ( "opus", @@ -180,17 +191,150 @@ pub fn detect_provider_kind(model: &str) -> ProviderKind { #[must_use] pub fn max_tokens_for_model(model: &str) -> u32 { + model_token_limit(model).map_or_else( + || { + let canonical = resolve_model_alias(model); + if canonical.contains("opus") { + 32_000 + } else { + 64_000 + } + }, + |limit| limit.max_output_tokens, + ) +} + +/// Returns the effective max output tokens for a model, preferring a plugin +/// override when present. Falls back to [`max_tokens_for_model`] when the +/// override is `None`. +#[must_use] +pub fn max_tokens_for_model_with_override(model: &str, plugin_override: Option) -> u32 { + plugin_override.unwrap_or_else(|| max_tokens_for_model(model)) +} + +#[must_use] +pub fn model_token_limit(model: &str) -> Option { let canonical = resolve_model_alias(model); - if canonical.contains("opus") { - 32_000 - } else { - 64_000 + match canonical.as_str() { + "claude-opus-4-6" => Some(ModelTokenLimit { + max_output_tokens: 32_000, + context_window_tokens: 200_000, + }), + "claude-sonnet-4-6" | "claude-haiku-4-5-20251213" => Some(ModelTokenLimit { + max_output_tokens: 64_000, + context_window_tokens: 200_000, + }), + "grok-3" | "grok-3-mini" => Some(ModelTokenLimit { + max_output_tokens: 64_000, + context_window_tokens: 131_072, + }), + _ => None, } } +pub fn preflight_message_request(request: &MessageRequest) -> Result<(), ApiError> { + let Some(limit) = model_token_limit(&request.model) else { + return Ok(()); + }; + + let estimated_input_tokens = estimate_message_request_input_tokens(request); + let estimated_total_tokens = estimated_input_tokens.saturating_add(request.max_tokens); + if estimated_total_tokens > limit.context_window_tokens { + return Err(ApiError::ContextWindowExceeded { + model: resolve_model_alias(&request.model), + estimated_input_tokens, + requested_output_tokens: request.max_tokens, + estimated_total_tokens, + context_window_tokens: limit.context_window_tokens, + }); + } + + Ok(()) +} + +fn estimate_message_request_input_tokens(request: &MessageRequest) -> u32 { + let mut estimate = estimate_serialized_tokens(&request.messages); + estimate = estimate.saturating_add(estimate_serialized_tokens(&request.system)); + estimate = estimate.saturating_add(estimate_serialized_tokens(&request.tools)); + estimate = estimate.saturating_add(estimate_serialized_tokens(&request.tool_choice)); + estimate +} + +fn estimate_serialized_tokens(value: &T) -> u32 { + serde_json::to_vec(value) + .ok() + .map_or(0, |bytes| (bytes.len() / 4 + 1) as u32) +} + +/// Parse a `.env` file body into key/value pairs using a minimal `KEY=VALUE` +/// grammar. Lines that are blank, start with `#`, or do not contain `=` are +/// ignored. Surrounding double or single quotes are stripped from the value. +/// An optional leading `export ` prefix on the key is also stripped so files +/// shared with shell `source` workflows still parse cleanly. +pub(crate) fn parse_dotenv(content: &str) -> std::collections::HashMap { + let mut values = std::collections::HashMap::new(); + for raw_line in content.lines() { + let line = raw_line.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + let Some((raw_key, raw_value)) = line.split_once('=') else { + continue; + }; + let trimmed_key = raw_key.trim(); + let key = trimmed_key + .strip_prefix("export ") + .map_or(trimmed_key, str::trim) + .to_string(); + if key.is_empty() { + continue; + } + let trimmed_value = raw_value.trim(); + let unquoted = if (trimmed_value.starts_with('"') && trimmed_value.ends_with('"') + || trimmed_value.starts_with('\'') && trimmed_value.ends_with('\'')) + && trimmed_value.len() >= 2 + { + &trimmed_value[1..trimmed_value.len() - 1] + } else { + trimmed_value + }; + values.insert(key, unquoted.to_string()); + } + values +} + +/// Load and parse a `.env` file from the given path. Missing files yield +/// `None` instead of an error so callers can use this as a soft fallback. +pub(crate) fn load_dotenv_file( + path: &std::path::Path, +) -> Option> { + let content = std::fs::read_to_string(path).ok()?; + Some(parse_dotenv(&content)) +} + +/// Look up `key` in a `.env` file located in the current working directory. +/// Returns `None` when the file is missing, the key is absent, or the value +/// is empty. +pub(crate) fn dotenv_value(key: &str) -> Option { + let cwd = std::env::current_dir().ok()?; + let values = load_dotenv_file(&cwd.join(".env"))?; + values.get(key).filter(|value| !value.is_empty()).cloned() +} + #[cfg(test)] mod tests { - use super::{detect_provider_kind, max_tokens_for_model, resolve_model_alias, ProviderKind}; + use serde_json::json; + + use crate::error::ApiError; + use crate::types::{ + InputContentBlock, InputMessage, MessageRequest, ToolChoice, ToolDefinition, + }; + + use super::{ + detect_provider_kind, load_dotenv_file, max_tokens_for_model, + max_tokens_for_model_with_override, model_token_limit, parse_dotenv, + preflight_message_request, resolve_model_alias, ProviderKind, + }; #[test] fn resolves_grok_aliases() { @@ -213,4 +357,217 @@ mod tests { assert_eq!(max_tokens_for_model("opus"), 32_000); assert_eq!(max_tokens_for_model("grok-3"), 64_000); } + + #[test] + fn plugin_config_max_output_tokens_overrides_model_default() { + // given + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("time should be after epoch") + .as_nanos(); + let root = std::env::temp_dir().join(format!("api-plugin-max-tokens-{nanos}")); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + std::fs::create_dir_all(cwd.join(".claw")).expect("project config dir"); + std::fs::create_dir_all(&home).expect("home config dir"); + std::fs::write( + home.join("settings.json"), + r#"{ + "plugins": { + "maxOutputTokens": 12345 + } + }"#, + ) + .expect("write plugin settings"); + + // when + let loaded = runtime::ConfigLoader::new(&cwd, &home) + .load() + .expect("config should load"); + let plugin_override = loaded.plugins().max_output_tokens(); + let effective = max_tokens_for_model_with_override("claude-opus-4-6", plugin_override); + + // then + assert_eq!(plugin_override, Some(12345)); + assert_eq!(effective, 12345); + assert_ne!(effective, max_tokens_for_model("claude-opus-4-6")); + + std::fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn max_tokens_for_model_with_override_falls_back_when_plugin_unset() { + // given + let plugin_override: Option = None; + + // when + let effective = max_tokens_for_model_with_override("claude-opus-4-6", plugin_override); + + // then + assert_eq!(effective, max_tokens_for_model("claude-opus-4-6")); + assert_eq!(effective, 32_000); + } + + #[test] + fn returns_context_window_metadata_for_supported_models() { + assert_eq!( + model_token_limit("claude-sonnet-4-6") + .expect("claude-sonnet-4-6 should be registered") + .context_window_tokens, + 200_000 + ); + assert_eq!( + model_token_limit("grok-mini") + .expect("grok-mini should resolve to a registered model") + .context_window_tokens, + 131_072 + ); + } + + #[test] + fn preflight_blocks_requests_that_exceed_the_model_context_window() { + let request = MessageRequest { + model: "claude-sonnet-4-6".to_string(), + max_tokens: 64_000, + messages: vec![InputMessage { + role: "user".to_string(), + content: vec![InputContentBlock::Text { + text: "x".repeat(600_000), + }], + }], + system: Some("Keep the answer short.".to_string()), + tools: Some(vec![ToolDefinition { + name: "weather".to_string(), + description: Some("Fetches weather".to_string()), + input_schema: json!({ + "type": "object", + "properties": { "city": { "type": "string" } }, + }), + }]), + tool_choice: Some(ToolChoice::Auto), + stream: true, + }; + + let error = preflight_message_request(&request) + .expect_err("oversized request should be rejected before the provider call"); + + match error { + ApiError::ContextWindowExceeded { + model, + estimated_input_tokens, + requested_output_tokens, + estimated_total_tokens, + context_window_tokens, + } => { + assert_eq!(model, "claude-sonnet-4-6"); + assert!(estimated_input_tokens > 136_000); + assert_eq!(requested_output_tokens, 64_000); + assert!(estimated_total_tokens > context_window_tokens); + assert_eq!(context_window_tokens, 200_000); + } + other => panic!("expected context-window preflight failure, got {other:?}"), + } + } + + #[test] + fn preflight_skips_unknown_models() { + let request = MessageRequest { + model: "unknown-model".to_string(), + max_tokens: 64_000, + messages: vec![InputMessage { + role: "user".to_string(), + content: vec![InputContentBlock::Text { + text: "x".repeat(600_000), + }], + }], + system: None, + tools: None, + tool_choice: None, + stream: false, + }; + + preflight_message_request(&request) + .expect("models without context metadata should skip the guarded preflight"); + } + + #[test] + fn parse_dotenv_extracts_keys_handles_comments_quotes_and_export_prefix() { + // given + let body = "\ +# this is a comment + +ANTHROPIC_API_KEY=plain-value +XAI_API_KEY=\"quoted-value\" +OPENAI_API_KEY='single-quoted' +export GROK_API_KEY=exported-value + PADDED_KEY = padded-value +EMPTY_VALUE= +NO_EQUALS_LINE +"; + + // when + let values = parse_dotenv(body); + + // then + assert_eq!( + values.get("ANTHROPIC_API_KEY").map(String::as_str), + Some("plain-value") + ); + assert_eq!( + values.get("XAI_API_KEY").map(String::as_str), + Some("quoted-value") + ); + assert_eq!( + values.get("OPENAI_API_KEY").map(String::as_str), + Some("single-quoted") + ); + assert_eq!( + values.get("GROK_API_KEY").map(String::as_str), + Some("exported-value") + ); + assert_eq!( + values.get("PADDED_KEY").map(String::as_str), + Some("padded-value") + ); + assert_eq!(values.get("EMPTY_VALUE").map(String::as_str), Some("")); + assert!(!values.contains_key("NO_EQUALS_LINE")); + assert!(!values.contains_key("# this is a comment")); + } + + #[test] + fn load_dotenv_file_reads_keys_from_disk_and_returns_none_when_missing() { + // given + let temp_root = std::env::temp_dir().join(format!( + "api-dotenv-test-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |duration| duration.as_nanos()) + )); + std::fs::create_dir_all(&temp_root).expect("create temp dir"); + let env_path = temp_root.join(".env"); + std::fs::write( + &env_path, + "ANTHROPIC_API_KEY=secret-from-file\n# comment\nXAI_API_KEY=\"xai-secret\"\n", + ) + .expect("write .env"); + let missing_path = temp_root.join("does-not-exist.env"); + + // when + let loaded = load_dotenv_file(&env_path).expect("file should load"); + let missing = load_dotenv_file(&missing_path); + + // then + assert_eq!( + loaded.get("ANTHROPIC_API_KEY").map(String::as_str), + Some("secret-from-file") + ); + assert_eq!( + loaded.get("XAI_API_KEY").map(String::as_str), + Some("xai-secret") + ); + assert!(missing.is_none()); + + let _ = std::fs::remove_dir_all(&temp_root); + } } diff --git a/rust/crates/api/src/providers/openai_compat.rs b/rust/crates/api/src/providers/openai_compat.rs index 48eec301..57d9ef22 100644 --- a/rust/crates/api/src/providers/openai_compat.rs +++ b/rust/crates/api/src/providers/openai_compat.rs @@ -1,10 +1,12 @@ use std::collections::{BTreeMap, VecDeque}; -use std::time::Duration; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use serde::Deserialize; use serde_json::{json, Value}; use crate::error::ApiError; +use crate::http_client::build_http_client_or_default; use crate::types::{ ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent, InputContentBlock, InputMessage, MessageDelta, MessageDeltaEvent, MessageRequest, @@ -12,15 +14,15 @@ use crate::types::{ ToolChoice, ToolDefinition, ToolResultContentBlock, Usage, }; -use super::{Provider, ProviderFuture}; +use super::{preflight_message_request, Provider, ProviderFuture}; pub const DEFAULT_XAI_BASE_URL: &str = "https://api.x.ai/v1"; pub const DEFAULT_OPENAI_BASE_URL: &str = "https://api.openai.com/v1"; const REQUEST_ID_HEADER: &str = "request-id"; const ALT_REQUEST_ID_HEADER: &str = "x-request-id"; -const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_millis(200); -const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(2); -const DEFAULT_MAX_RETRIES: u32 = 2; +const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_secs(1); +const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(128); +const DEFAULT_MAX_RETRIES: u32 = 8; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct OpenAiCompatConfig { @@ -81,7 +83,7 @@ impl OpenAiCompatClient { #[must_use] pub fn new(api_key: impl Into, config: OpenAiCompatConfig) -> Self { Self { - http: reqwest::Client::new(), + http: build_http_client_or_default(), api_key: api_key.into(), config, base_url: read_base_url(config), @@ -128,9 +130,18 @@ impl OpenAiCompatClient { stream: false, ..request.clone() }; + preflight_message_request(&request)?; let response = self.send_with_retry(&request).await?; let request_id = request_id_from_headers(response.headers()); - let payload = response.json::().await?; + let body = response.text().await.map_err(ApiError::from)?; + let payload = serde_json::from_str::(&body).map_err(|error| { + ApiError::json_deserialize( + self.config.provider_name, + &request.model, + &body, + error, + ) + })?; let mut normalized = normalize_response(&request.model, payload)?; if normalized.request_id.is_none() { normalized.request_id = request_id; @@ -142,13 +153,17 @@ impl OpenAiCompatClient { &self, request: &MessageRequest, ) -> Result { + preflight_message_request(request)?; let response = self .send_with_retry(&request.clone().with_streaming()) .await?; Ok(MessageStream { request_id: request_id_from_headers(response.headers()), response, - parser: OpenAiSseParser::new(), + parser: OpenAiSseParser::with_context( + self.config.provider_name, + request.model.clone(), + ), pending: VecDeque::new(), done: false, state: StreamState::new(request.model.clone()), @@ -177,7 +192,7 @@ impl OpenAiCompatClient { break retryable_error; } - tokio::time::sleep(self.backoff_for_attempt(attempts)?).await; + tokio::time::sleep(self.jittered_backoff_for_attempt(attempts)?).await; }; Err(ApiError::RetriesExhausted { @@ -213,6 +228,37 @@ impl OpenAiCompatClient { .checked_mul(multiplier) .map_or(self.max_backoff, |delay| delay.min(self.max_backoff))) } + + fn jittered_backoff_for_attempt(&self, attempt: u32) -> Result { + let base = self.backoff_for_attempt(attempt)?; + Ok(base + jitter_for_base(base)) + } +} + +/// Process-wide counter that guarantees distinct jitter samples even when +/// the system clock resolution is coarser than consecutive retry sleeps. +static JITTER_COUNTER: AtomicU64 = AtomicU64::new(0); + +/// Returns a random additive jitter in `[0, base]` to decorrelate retries +/// from multiple concurrent clients. Entropy is drawn from the nanosecond +/// wall clock mixed with a monotonic counter and run through a splitmix64 +/// finalizer; adequate for retry jitter (no cryptographic requirement). +fn jitter_for_base(base: Duration) -> Duration { + let base_nanos = u64::try_from(base.as_nanos()).unwrap_or(u64::MAX); + if base_nanos == 0 { + return Duration::ZERO; + } + let raw_nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|elapsed| u64::try_from(elapsed.as_nanos()).unwrap_or(u64::MAX)) + .unwrap_or(0); + let tick = JITTER_COUNTER.fetch_add(1, Ordering::Relaxed); + let mut mixed = raw_nanos.wrapping_add(tick).wrapping_add(0x9E37_79B9_7F4A_7C15); + mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + mixed ^= mixed >> 31; + let jitter_nanos = mixed % base_nanos.saturating_add(1); + Duration::from_nanos(jitter_nanos) } impl Provider for OpenAiCompatClient { @@ -280,11 +326,17 @@ impl MessageStream { #[derive(Debug, Default)] struct OpenAiSseParser { buffer: Vec, + provider: String, + model: String, } impl OpenAiSseParser { - fn new() -> Self { - Self::default() + fn with_context(provider: impl Into, model: impl Into) -> Self { + Self { + buffer: Vec::new(), + provider: provider.into(), + model: model.into(), + } } fn push(&mut self, chunk: &[u8]) -> Result, ApiError> { @@ -292,7 +344,7 @@ impl OpenAiSseParser { let mut events = Vec::new(); while let Some(frame) = next_sse_frame(&mut self.buffer) { - if let Some(event) = parse_sse_frame(&frame)? { + if let Some(event) = parse_sse_frame(&frame, &self.provider, &self.model)? { events.push(event); } } @@ -833,7 +885,11 @@ fn next_sse_frame(buffer: &mut Vec) -> Option { Some(String::from_utf8_lossy(&frame[..frame_len]).into_owned()) } -fn parse_sse_frame(frame: &str) -> Result, ApiError> { +fn parse_sse_frame( + frame: &str, + provider: &str, + model: &str, +) -> Result, ApiError> { let trimmed = frame.trim(); if trimmed.is_empty() { return Ok(None); @@ -855,15 +911,15 @@ fn parse_sse_frame(frame: &str) -> Result, ApiError> if payload == "[DONE]" { return Ok(None); } - serde_json::from_str(&payload) + serde_json::from_str::(&payload) .map(Some) - .map_err(ApiError::from) + .map_err(|error| ApiError::json_deserialize(provider, model, &payload, error)) } fn read_env_non_empty(key: &str) -> Result, ApiError> { match std::env::var(key) { Ok(value) if !value.is_empty() => Ok(Some(value)), - Ok(_) | Err(std::env::VarError::NotPresent) => Ok(None), + Ok(_) | Err(std::env::VarError::NotPresent) => Ok(super::dotenv_value(key)), Err(error) => Err(ApiError::from(error)), } } @@ -904,6 +960,7 @@ async fn expect_success(response: reqwest::Response) -> Result(&body).ok(); let retryable = is_retryable_status(status); @@ -916,6 +973,7 @@ async fn expect_success(response: reqwest::Response) -> Result, + provider: Option, + model: Option, } impl SseParser { @@ -12,12 +14,23 @@ impl SseParser { Self::default() } + /// Attach the provider name and model to this parser so that JSON + /// deserialization failures within streamed frames carry enough context + /// for callers to understand which upstream produced the unparseable + /// payload. + #[must_use] + pub fn with_context(mut self, provider: impl Into, model: impl Into) -> Self { + self.provider = Some(provider.into()); + self.model = Some(model.into()); + self + } + pub fn push(&mut self, chunk: &[u8]) -> Result, ApiError> { self.buffer.extend_from_slice(chunk); let mut events = Vec::new(); while let Some(frame) = self.next_frame() { - if let Some(event) = parse_frame(&frame)? { + if let Some(event) = self.parse_frame_with_context(&frame)? { events.push(event); } } @@ -31,12 +44,18 @@ impl SseParser { } let trailing = std::mem::take(&mut self.buffer); - match parse_frame(&String::from_utf8_lossy(&trailing))? { + match self.parse_frame_with_context(&String::from_utf8_lossy(&trailing))? { Some(event) => Ok(vec![event]), None => Ok(Vec::new()), } } + fn parse_frame_with_context(&self, frame: &str) -> Result, ApiError> { + let provider = self.provider.as_deref().unwrap_or("unknown"); + let model = self.model.as_deref().unwrap_or("unknown"); + parse_frame_with_provider(frame, provider, model) + } + fn next_frame(&mut self) -> Option { let separator = self .buffer @@ -61,6 +80,14 @@ impl SseParser { } pub fn parse_frame(frame: &str) -> Result, ApiError> { + parse_frame_with_provider(frame, "unknown", "unknown") +} + +pub(crate) fn parse_frame_with_provider( + frame: &str, + provider: &str, + model: &str, +) -> Result, ApiError> { let trimmed = frame.trim(); if trimmed.is_empty() { return Ok(None); @@ -97,7 +124,7 @@ pub fn parse_frame(frame: &str) -> Result, ApiError> { serde_json::from_str::(&payload) .map(Some) - .map_err(ApiError::from) + .map_err(|error| ApiError::json_deserialize(provider, model, &payload, error)) } #[cfg(test)] @@ -276,4 +303,28 @@ mod tests { )) ); } + + #[test] + fn given_message_delta_frame_with_empty_usage_when_parsed_then_usage_defaults_to_zero() { + // given + let frame = concat!( + "event: message_delta\n", + "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null},\"usage\":{}}\n\n" + ); + + // when + let event = parse_frame(frame).expect("frame should parse"); + + // then + assert_eq!( + event, + Some(StreamEvent::MessageDelta(crate::types::MessageDeltaEvent { + delta: MessageDelta { + stop_reason: Some("end_turn".to_string()), + stop_sequence: None, + }, + usage: Usage::default(), + })) + ); + } } diff --git a/rust/crates/api/src/types.rs b/rust/crates/api/src/types.rs index ec70d404..f2e33f61 100644 --- a/rust/crates/api/src/types.rs +++ b/rust/crates/api/src/types.rs @@ -113,6 +113,7 @@ pub struct MessageResponse { pub stop_reason: Option, #[serde(default)] pub stop_sequence: Option, + #[serde(default)] pub usage: Usage, #[serde(default)] pub request_id: Option, @@ -147,13 +148,15 @@ pub enum OutputContentBlock { }, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Usage { + #[serde(default)] pub input_tokens: u32, #[serde(default)] pub cache_creation_input_tokens: u32, #[serde(default)] pub cache_read_input_tokens: u32, + #[serde(default)] pub output_tokens: u32, } @@ -194,6 +197,7 @@ pub struct MessageStartEvent { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct MessageDeltaEvent { pub delta: MessageDelta, + #[serde(default)] pub usage: Usage, } diff --git a/rust/crates/api/tests/client_integration.rs b/rust/crates/api/tests/client_integration.rs index 01addefe..9d45cc78 100644 --- a/rust/crates/api/tests/client_integration.rs +++ b/rust/crates/api/tests/client_integration.rs @@ -97,9 +97,44 @@ async fn send_message_posts_json_and_parses_response() { assert!(body.get("stream").is_none()); assert_eq!(body["tools"][0]["name"], json!("get_weather")); assert_eq!(body["tool_choice"]["type"], json!("auto")); - assert_eq!( - body["betas"], - json!(["claude-code-20250219", "prompt-caching-scope-2026-01-05"]) + assert!( + body.get("betas").is_none(), + "betas must travel via the anthropic-beta header, not the request body" + ); +} + +#[tokio::test] +async fn send_message_blocks_oversized_requests_before_the_http_call() { + let state = Arc::new(Mutex::new(Vec::::new())); + let server = spawn_server( + state.clone(), + vec![http_response("200 OK", "application/json", "{}")], + ) + .await; + + let client = AnthropicClient::new("test-key").with_base_url(server.base_url()); + let error = client + .send_message(&MessageRequest { + model: "claude-sonnet-4-6".to_string(), + max_tokens: 64_000, + messages: vec![InputMessage { + role: "user".to_string(), + content: vec![InputContentBlock::Text { + text: "x".repeat(600_000), + }], + }], + system: Some("Keep the answer short.".to_string()), + tools: None, + tool_choice: None, + stream: false, + }) + .await + .expect_err("oversized request should fail local context-window preflight"); + + assert!(matches!(error, ApiError::ContextWindowExceeded { .. })); + assert!( + state.lock().await.is_empty(), + "preflight failure should avoid any upstream HTTP request" ); } @@ -156,13 +191,9 @@ async fn send_message_applies_request_profile_and_records_telemetry() { let body: serde_json::Value = serde_json::from_str(&request.body).expect("request body should be json"); assert_eq!(body["metadata"]["source"], json!("clawd-code")); - assert_eq!( - body["betas"], - json!([ - "claude-code-20250219", - "prompt-caching-scope-2026-01-05", - "tools-2026-04-01" - ]) + assert!( + body.get("betas").is_none(), + "betas must travel via the anthropic-beta header, not the request body" ); let events = sink.events(); @@ -241,6 +272,44 @@ async fn send_message_parses_prompt_cache_token_usage_from_response() { assert_eq!(response.usage.output_tokens, 4); } +#[tokio::test] +async fn given_empty_usage_object_when_send_message_parses_response_then_usage_defaults_to_zero() { + // given + let state = Arc::new(Mutex::new(Vec::::new())); + let body = concat!( + "{", + "\"id\":\"msg_empty_usage\",", + "\"type\":\"message\",", + "\"role\":\"assistant\",", + "\"content\":[{\"type\":\"text\",\"text\":\"Hello from Claude\"}],", + "\"model\":\"claude-3-7-sonnet-latest\",", + "\"stop_reason\":\"end_turn\",", + "\"stop_sequence\":null,", + "\"usage\":{}", + "}" + ); + let server = spawn_server( + state, + vec![http_response("200 OK", "application/json", body)], + ) + .await; + let client = AnthropicClient::new("test-key").with_base_url(server.base_url()); + + // when + let response = client + .send_message(&sample_request(false)) + .await + .expect("response with empty usage object should still parse"); + + // then + assert_eq!(response.id, "msg_empty_usage"); + assert_eq!(response.total_tokens(), 0); + assert_eq!(response.usage.input_tokens, 0); + assert_eq!(response.usage.cache_creation_input_tokens, 0); + assert_eq!(response.usage.cache_read_input_tokens, 0); + assert_eq!(response.usage.output_tokens, 0); +} + #[tokio::test] #[allow(clippy::await_holding_lock)] async fn stream_message_parses_sse_events_with_tool_use() { @@ -476,6 +545,71 @@ async fn surfaces_retry_exhaustion_for_persistent_retryable_errors() { } } +#[tokio::test] +async fn retries_multiple_retryable_failures_with_exponential_backoff_and_jitter() { + let state = Arc::new(Mutex::new(Vec::::new())); + let server = spawn_server( + state.clone(), + vec![ + http_response( + "429 Too Many Requests", + "application/json", + "{\"type\":\"error\",\"error\":{\"type\":\"rate_limit_error\",\"message\":\"slow down\"}}", + ), + http_response( + "500 Internal Server Error", + "application/json", + "{\"type\":\"error\",\"error\":{\"type\":\"api_error\",\"message\":\"boom\"}}", + ), + http_response( + "503 Service Unavailable", + "application/json", + "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}", + ), + http_response( + "429 Too Many Requests", + "application/json", + "{\"type\":\"error\",\"error\":{\"type\":\"rate_limit_error\",\"message\":\"slow down again\"}}", + ), + http_response( + "503 Service Unavailable", + "application/json", + "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"still busy\"}}", + ), + http_response( + "200 OK", + "application/json", + "{\"id\":\"msg_exp_retry\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Recovered after 5\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}", + ), + ], + ) + .await; + + let client = ApiClient::new("test-key") + .with_base_url(server.base_url()) + .with_retry_policy(8, Duration::from_millis(1), Duration::from_millis(4)); + let started_at = std::time::Instant::now(); + + let response = client + .send_message(&sample_request(false)) + .await + .expect("8-retry policy should absorb 5 retryable failures"); + + let elapsed = started_at.elapsed(); + assert_eq!(response.total_tokens(), 5); + assert_eq!( + state.lock().await.len(), + 6, + "client should issue 1 original + 5 retry requests before the 200" + ); + // Jittered sleeps are bounded by 2 * max_backoff per retry (base + jitter), + // so 5 sleeps fit comfortably below this upper bound with generous slack. + assert!( + elapsed < Duration::from_secs(5), + "retries should complete promptly, took {elapsed:?}" + ); +} + #[tokio::test] #[allow(clippy::await_holding_lock)] async fn send_message_reuses_recent_completion_cache_entries() { diff --git a/rust/crates/api/tests/openai_compat_integration.rs b/rust/crates/api/tests/openai_compat_integration.rs index e12e6733..2dd90a2c 100644 --- a/rust/crates/api/tests/openai_compat_integration.rs +++ b/rust/crates/api/tests/openai_compat_integration.rs @@ -4,10 +4,10 @@ use std::sync::Arc; use std::sync::{Mutex as StdMutex, OnceLock}; use api::{ - ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent, - InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, OpenAiCompatClient, - OpenAiCompatConfig, OutputContentBlock, ProviderClient, StreamEvent, ToolChoice, - ToolDefinition, + ApiError, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, + ContentBlockStopEvent, InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, + OpenAiCompatClient, OpenAiCompatConfig, OutputContentBlock, ProviderClient, StreamEvent, + ToolChoice, ToolDefinition, }; use serde_json::json; use tokio::io::{AsyncReadExt, AsyncWriteExt}; @@ -63,6 +63,42 @@ async fn send_message_uses_openai_compatible_endpoint_and_auth() { assert_eq!(body["tools"][0]["type"], json!("function")); } +#[tokio::test] +async fn send_message_blocks_oversized_xai_requests_before_the_http_call() { + let state = Arc::new(Mutex::new(Vec::::new())); + let server = spawn_server( + state.clone(), + vec![http_response("200 OK", "application/json", "{}")], + ) + .await; + + let client = OpenAiCompatClient::new("xai-test-key", OpenAiCompatConfig::xai()) + .with_base_url(server.base_url()); + let error = client + .send_message(&MessageRequest { + model: "grok-3".to_string(), + max_tokens: 64_000, + messages: vec![InputMessage { + role: "user".to_string(), + content: vec![InputContentBlock::Text { + text: "x".repeat(300_000), + }], + }], + system: Some("Keep the answer short.".to_string()), + tools: None, + tool_choice: None, + stream: false, + }) + .await + .expect_err("oversized request should fail local context-window preflight"); + + assert!(matches!(error, ApiError::ContextWindowExceeded { .. })); + assert!( + state.lock().await.is_empty(), + "preflight failure should avoid any upstream HTTP request" + ); +} + #[tokio::test] async fn send_message_accepts_full_chat_completions_endpoint_override() { let state = Arc::new(Mutex::new(Vec::::new())); diff --git a/rust/crates/api/tests/proxy_integration.rs b/rust/crates/api/tests/proxy_integration.rs new file mode 100644 index 00000000..7e390698 --- /dev/null +++ b/rust/crates/api/tests/proxy_integration.rs @@ -0,0 +1,173 @@ +use std::ffi::OsString; +use std::sync::{Mutex, OnceLock}; + +use api::{build_http_client_with, ProxyConfig}; + +fn env_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) +} + +struct EnvVarGuard { + key: &'static str, + original: Option, +} + +impl EnvVarGuard { + fn set(key: &'static str, value: Option<&str>) -> Self { + let original = std::env::var_os(key); + match value { + Some(value) => std::env::set_var(key, value), + None => std::env::remove_var(key), + } + Self { key, original } + } +} + +impl Drop for EnvVarGuard { + fn drop(&mut self) { + match &self.original { + Some(value) => std::env::set_var(self.key, value), + None => std::env::remove_var(self.key), + } + } +} + +#[test] +fn proxy_config_from_env_reads_uppercase_proxy_vars() { + // given + let _lock = env_lock(); + let _http = EnvVarGuard::set("HTTP_PROXY", Some("http://proxy.corp:3128")); + let _https = EnvVarGuard::set("HTTPS_PROXY", Some("http://secure.corp:3129")); + let _no = EnvVarGuard::set("NO_PROXY", Some("localhost,127.0.0.1")); + let _http_lower = EnvVarGuard::set("http_proxy", None); + let _https_lower = EnvVarGuard::set("https_proxy", None); + let _no_lower = EnvVarGuard::set("no_proxy", None); + + // when + let config = ProxyConfig::from_env(); + + // then + assert_eq!(config.http_proxy.as_deref(), Some("http://proxy.corp:3128")); + assert_eq!( + config.https_proxy.as_deref(), + Some("http://secure.corp:3129") + ); + assert_eq!(config.no_proxy.as_deref(), Some("localhost,127.0.0.1")); + assert!(config.proxy_url.is_none()); + assert!(!config.is_empty()); +} + +#[test] +fn proxy_config_from_env_reads_lowercase_proxy_vars() { + // given + let _lock = env_lock(); + let _http = EnvVarGuard::set("HTTP_PROXY", None); + let _https = EnvVarGuard::set("HTTPS_PROXY", None); + let _no = EnvVarGuard::set("NO_PROXY", None); + let _http_lower = EnvVarGuard::set("http_proxy", Some("http://lower.corp:3128")); + let _https_lower = EnvVarGuard::set("https_proxy", Some("http://lower-secure.corp:3129")); + let _no_lower = EnvVarGuard::set("no_proxy", Some(".internal")); + + // when + let config = ProxyConfig::from_env(); + + // then + assert_eq!(config.http_proxy.as_deref(), Some("http://lower.corp:3128")); + assert_eq!( + config.https_proxy.as_deref(), + Some("http://lower-secure.corp:3129") + ); + assert_eq!(config.no_proxy.as_deref(), Some(".internal")); + assert!(!config.is_empty()); +} + +#[test] +fn proxy_config_from_env_is_empty_when_no_vars_set() { + // given + let _lock = env_lock(); + let _http = EnvVarGuard::set("HTTP_PROXY", None); + let _https = EnvVarGuard::set("HTTPS_PROXY", None); + let _no = EnvVarGuard::set("NO_PROXY", None); + let _http_lower = EnvVarGuard::set("http_proxy", None); + let _https_lower = EnvVarGuard::set("https_proxy", None); + let _no_lower = EnvVarGuard::set("no_proxy", None); + + // when + let config = ProxyConfig::from_env(); + + // then + assert!(config.is_empty()); + assert!(config.http_proxy.is_none()); + assert!(config.https_proxy.is_none()); + assert!(config.no_proxy.is_none()); +} + +#[test] +fn proxy_config_from_env_treats_empty_values_as_unset() { + // given + let _lock = env_lock(); + let _http = EnvVarGuard::set("HTTP_PROXY", Some("")); + let _https = EnvVarGuard::set("HTTPS_PROXY", Some("")); + let _http_lower = EnvVarGuard::set("http_proxy", Some("")); + let _https_lower = EnvVarGuard::set("https_proxy", Some("")); + let _no = EnvVarGuard::set("NO_PROXY", Some("")); + let _no_lower = EnvVarGuard::set("no_proxy", Some("")); + + // when + let config = ProxyConfig::from_env(); + + // then + assert!(config.is_empty()); +} + +#[test] +fn build_client_with_env_proxy_config_succeeds() { + // given + let _lock = env_lock(); + let _http = EnvVarGuard::set("HTTP_PROXY", Some("http://proxy.corp:3128")); + let _https = EnvVarGuard::set("HTTPS_PROXY", Some("http://secure.corp:3129")); + let _no = EnvVarGuard::set("NO_PROXY", Some("localhost")); + let _http_lower = EnvVarGuard::set("http_proxy", None); + let _https_lower = EnvVarGuard::set("https_proxy", None); + let _no_lower = EnvVarGuard::set("no_proxy", None); + let config = ProxyConfig::from_env(); + + // when + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); +} + +#[test] +fn build_client_with_proxy_url_config_succeeds() { + // given + let config = ProxyConfig::from_proxy_url("http://unified.corp:3128"); + + // when + let result = build_http_client_with(&config); + + // then + assert!(result.is_ok()); +} + +#[test] +fn proxy_config_from_env_prefers_uppercase_over_lowercase() { + // given + let _lock = env_lock(); + let _http_upper = EnvVarGuard::set("HTTP_PROXY", Some("http://upper.corp:3128")); + let _http_lower = EnvVarGuard::set("http_proxy", Some("http://lower.corp:3128")); + let _https = EnvVarGuard::set("HTTPS_PROXY", None); + let _https_lower = EnvVarGuard::set("https_proxy", None); + let _no = EnvVarGuard::set("NO_PROXY", None); + let _no_lower = EnvVarGuard::set("no_proxy", None); + + // when + let config = ProxyConfig::from_env(); + + // then + assert_eq!(config.http_proxy.as_deref(), Some("http://upper.corp:3128")); +} diff --git a/rust/crates/commands/src/lib.rs b/rust/crates/commands/src/lib.rs index 7e6191d9..dc329d10 100644 --- a/rust/crates/commands/src/lib.rs +++ b/rust/crates/commands/src/lib.rs @@ -9,6 +9,7 @@ use runtime::{ compact_session, CompactionConfig, ConfigLoader, ConfigSource, McpOAuthConfig, McpServerConfig, ScopedMcpServerConfig, Session, }; +use serde_json::{json, Value}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct CommandManifestEntry { @@ -49,6 +50,12 @@ pub struct SlashCommandSpec { pub resume_supported: bool, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SkillSlashDispatch { + Local, + Invoke(String), +} + const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[ SlashCommandSpec { name: "help", @@ -214,8 +221,10 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[ SlashCommandSpec { name: "session", aliases: &[], - summary: "List, switch, or fork managed local sessions", - argument_hint: Some("[list|switch |fork [branch-name]]"), + summary: "List, switch, fork, or delete managed local sessions", + argument_hint: Some( + "[list|switch |fork [branch-name]|delete [--force]]", + ), resume_supported: false, }, SlashCommandSpec { @@ -236,9 +245,9 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[ }, SlashCommandSpec { name: "skills", - aliases: &[], - summary: "List or install available skills", - argument_hint: Some("[list|install |help]"), + aliases: &["skill"], + summary: "List, install, or invoke available skills", + argument_hint: Some("[list|install |help| [args]]"), resume_supported: true, }, SlashCommandSpec { @@ -1181,6 +1190,9 @@ pub enum SlashCommand { AddDir { path: Option, }, + History { + count: Option, + }, Unknown(String), } @@ -1305,7 +1317,7 @@ pub fn validate_slash_command_input( "agents" => SlashCommand::Agents { args: parse_list_or_help_args(command, remainder)?, }, - "skills" => SlashCommand::Skills { + "skills" | "skill" => SlashCommand::Skills { args: parse_skills_args(remainder.as_deref())?, }, "doctor" => { @@ -1414,6 +1426,9 @@ pub fn validate_slash_command_input( "tag" => SlashCommand::Tag { label: remainder }, "output-style" => SlashCommand::OutputStyle { style: remainder }, "add-dir" => SlashCommand::AddDir { path: remainder }, + "history" => SlashCommand::History { + count: optional_single_arg(command, &args, "[count]")?, + }, other => SlashCommand::Unknown(other.to_string()), })) } @@ -1513,7 +1528,7 @@ fn parse_session_command(args: &[&str]) -> Result Err(usage_error("session", "[list|switch |fork [branch-name]]")), + ["list", ..] => Err(usage_error("session", "[list|switch |fork [branch-name]|delete [--force]]")), ["switch"] => Err(usage_error("session switch", "")), ["switch", target] => Ok(SlashCommand::Session { action: Some("switch".to_string()), @@ -1537,12 +1552,33 @@ fn parse_session_command(args: &[&str]) -> Result Err(command_error( + ["delete"] => Err(usage_error("session delete", " [--force]")), + ["delete", target] => Ok(SlashCommand::Session { + action: Some("delete".to_string()), + target: Some((*target).to_string()), + }), + ["delete", target, "--force"] => Ok(SlashCommand::Session { + action: Some("delete-force".to_string()), + target: Some((*target).to_string()), + }), + ["delete", _target, unexpected] => Err(command_error( &format!( - "Unknown /session action '{action}'. Use list, switch , or fork [branch-name]." + "Unsupported /session delete flag '{unexpected}'. Use --force to skip confirmation." ), "session", - "/session [list|switch |fork [branch-name]]", + "/session delete [--force]", + )), + ["delete", ..] => Err(command_error( + "Unexpected arguments for /session delete.", + "session", + "/session delete [--force]", + )), + [action, ..] => Err(command_error( + &format!( + "Unknown /session action '{action}'. Use list, switch , fork [branch-name], or delete [--force]." + ), + "session", + "/session [list|switch |fork [branch-name]|delete [--force]]", )), } } @@ -1685,13 +1721,7 @@ fn parse_skills_args(args: Option<&str>) -> Result, SlashCommandP } } - Err(command_error( - &format!( - "Unexpected arguments for /skills: {args}. Use /skills, /skills list, /skills install , or /skills help." - ), - "skills", - "/skills [list|install |help]", - )) + Ok(Some(args.to_string())) } fn usage_error(command: &str, argument_hint: &str) -> SlashCommandParseError { @@ -1785,24 +1815,29 @@ pub fn resume_supported_slash_commands() -> Vec<&'static SlashCommandSpec> { fn slash_command_category(name: &str) -> &'static str { match name { - "help" | "status" | "sandbox" | "model" | "permissions" | "cost" | "resume" | "session" - | "version" | "login" | "logout" | "usage" | "stats" | "rename" | "privacy-settings" => { - "Session & visibility" - } - "compact" | "clear" | "config" | "memory" | "init" | "diff" | "commit" | "pr" | "issue" - | "export" | "plugin" | "branch" | "add-dir" | "files" | "hooks" | "release-notes" => { - "Workspace & git" - } - "agents" | "skills" | "teleport" | "debug-tool-call" | "mcp" | "context" | "tasks" - | "doctor" | "ide" | "desktop" => "Discovery & debugging", - "bughunter" | "ultraplan" | "review" | "security-review" | "advisor" | "insights" => { - "Analysis & automation" - } - "theme" | "vim" | "voice" | "color" | "effort" | "fast" | "brief" | "output-style" - | "keybindings" | "stickers" => "Appearance & input", - "copy" | "share" | "feedback" | "summary" | "tag" | "thinkback" | "plan" | "exit" - | "upgrade" | "rewind" => "Communication & control", - _ => "Other", + "help" | "status" | "cost" | "resume" | "session" | "version" | "login" | "logout" + | "usage" | "stats" | "rename" | "clear" | "compact" | "history" | "tokens" | "cache" + | "exit" | "summary" | "tag" | "thinkback" | "copy" | "share" | "feedback" | "rewind" + | "pin" | "unpin" | "bookmarks" | "context" | "files" | "focus" | "unfocus" | "retry" + | "stop" | "undo" => "Session", + "diff" | "commit" | "pr" | "issue" | "branch" | "blame" | "log" | "git" | "stash" + | "init" | "export" | "plan" | "review" | "security-review" | "bughunter" | "ultraplan" + | "teleport" | "refactor" | "fix" | "autofix" | "explain" | "docs" | "perf" | "search" + | "references" | "definition" | "hover" | "symbols" | "map" | "web" | "image" + | "screenshot" | "paste" | "listen" | "speak" | "test" | "lint" | "build" | "run" + | "format" | "parallel" | "multi" | "macro" | "alias" | "templates" | "migrate" + | "benchmark" | "cron" | "agent" | "subagent" | "agents" | "skills" | "team" | "plugin" + | "mcp" | "hooks" | "tasks" | "advisor" | "insights" | "release-notes" | "chat" + | "approve" | "deny" | "allowed-tools" | "add-dir" => "Tools", + "model" | "permissions" | "config" | "memory" | "theme" | "vim" | "voice" | "color" + | "effort" | "fast" | "brief" | "output-style" | "keybindings" | "privacy-settings" + | "stickers" | "language" | "profile" | "max-tokens" | "temperature" | "system-prompt" + | "api-key" | "terminal-setup" | "notifications" | "telemetry" | "providers" | "env" + | "project" | "reasoning" | "budget" | "rate-limit" | "workspace" | "reset" | "ide" + | "desktop" | "upgrade" => "Config", + "debug-tool-call" | "doctor" | "sandbox" | "diagnostics" | "tool-details" | "changelog" + | "metrics" => "Debug", + _ => "Tools", } } @@ -1911,12 +1946,7 @@ pub fn render_slash_command_help() -> String { String::new(), ]; - let categories = [ - "Session & visibility", - "Workspace & git", - "Discovery & debugging", - "Analysis & automation", - ]; + let categories = ["Session", "Tools", "Config", "Debug"]; for category in categories { lines.push(category.to_string()); @@ -1929,6 +1959,12 @@ pub fn render_slash_command_help() -> String { lines.push(String::new()); } + lines.push("Keyboard shortcuts".to_string()); + lines.push(" Up/Down Navigate prompt history".to_string()); + lines.push(" Tab Complete commands, modes, and recent sessions".to_string()); + lines.push(" Ctrl-C Clear input (or exit on empty prompt)".to_string()); + lines.push(" Shift+Enter/Ctrl+J Insert a newline".to_string()); + lines .into_iter() .rev() @@ -1954,25 +1990,49 @@ pub struct PluginsCommandResult { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] enum DefinitionSource { + ProjectClaw, ProjectCodex, ProjectClaude, + UserClawConfigHome, UserCodexHome, + UserClaw, UserCodex, UserClaude, } -impl DefinitionSource { +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum DefinitionScope { + Project, + UserConfigHome, + UserHome, +} + +impl DefinitionScope { fn label(self) -> &'static str { match self { - Self::ProjectCodex => "Project (.codex)", - Self::ProjectClaude => "Project (.claude)", - Self::UserCodexHome => "User ($CODEX_HOME)", - Self::UserCodex => "User (~/.codex)", - Self::UserClaude => "User (~/.claude)", + Self::Project => "Project roots", + Self::UserConfigHome => "User config roots", + Self::UserHome => "User home roots", } } } +impl DefinitionSource { + fn report_scope(self) -> DefinitionScope { + match self { + Self::ProjectClaw | Self::ProjectCodex | Self::ProjectClaude => { + DefinitionScope::Project + } + Self::UserClawConfigHome | Self::UserCodexHome => DefinitionScope::UserConfigHome, + Self::UserClaw | Self::UserCodex | Self::UserClaude => DefinitionScope::UserHome, + } + } + + fn label(self) -> &'static str { + self.report_scope().label() + } +} + #[derive(Debug, Clone, PartialEq, Eq)] struct AgentSummary { name: String, @@ -2142,17 +2202,47 @@ pub fn handle_plugins_slash_command( } pub fn handle_agents_slash_command(args: Option<&str>, cwd: &Path) -> std::io::Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_agents_usage(None), + _ => render_agents_usage(Some(&help_path.join(" "))), + }); + } + } + match normalize_optional_args(args) { None | Some("list") => { let roots = discover_definition_roots(cwd, "agents"); let agents = load_agents_from_roots(&roots)?; Ok(render_agents_report(&agents)) } - Some("-h" | "--help" | "help") => Ok(render_agents_usage(None)), + Some(args) if is_help_arg(args) => Ok(render_agents_usage(None)), Some(args) => Ok(render_agents_usage(Some(args))), } } +pub fn handle_agents_slash_command_json(args: Option<&str>, cwd: &Path) -> std::io::Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_agents_usage_json(None), + _ => render_agents_usage_json(Some(&help_path.join(" "))), + }); + } + } + + match normalize_optional_args(args) { + None | Some("list") => { + let roots = discover_definition_roots(cwd, "agents"); + let agents = load_agents_from_roots(&roots)?; + Ok(render_agents_report_json(cwd, &agents)) + } + Some(args) if is_help_arg(args) => Ok(render_agents_usage_json(None)), + Some(args) => Ok(render_agents_usage_json(Some(args))), + } +} + pub fn handle_mcp_slash_command( args: Option<&str>, cwd: &Path, @@ -2161,7 +2251,25 @@ pub fn handle_mcp_slash_command( render_mcp_report_for(&loader, cwd, args) } +pub fn handle_mcp_slash_command_json( + args: Option<&str>, + cwd: &Path, +) -> Result { + let loader = ConfigLoader::default_for(cwd); + render_mcp_report_json_for(&loader, cwd, args) +} + pub fn handle_skills_slash_command(args: Option<&str>, cwd: &Path) -> std::io::Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_skills_usage(None), + ["install", ..] => render_skills_usage(Some("install")), + _ => render_skills_usage(Some(&help_path.join(" "))), + }); + } + } + match normalize_optional_args(args) { None | Some("list") => { let roots = discover_skill_roots(cwd); @@ -2177,16 +2285,177 @@ pub fn handle_skills_slash_command(args: Option<&str>, cwd: &Path) -> std::io::R let install = install_skill(target, cwd)?; Ok(render_skill_install_report(&install)) } - Some("-h" | "--help" | "help") => Ok(render_skills_usage(None)), + Some(args) if is_help_arg(args) => Ok(render_skills_usage(None)), Some(args) => Ok(render_skills_usage(Some(args))), } } +pub fn handle_skills_slash_command_json(args: Option<&str>, cwd: &Path) -> std::io::Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_skills_usage_json(None), + ["install", ..] => render_skills_usage_json(Some("install")), + _ => render_skills_usage_json(Some(&help_path.join(" "))), + }); + } + } + + match normalize_optional_args(args) { + None | Some("list") => { + let roots = discover_skill_roots(cwd); + let skills = load_skills_from_roots(&roots)?; + Ok(render_skills_report_json(&skills)) + } + Some("install") => Ok(render_skills_usage_json(Some("install"))), + Some(args) if args.starts_with("install ") => { + let target = args["install ".len()..].trim(); + if target.is_empty() { + return Ok(render_skills_usage_json(Some("install"))); + } + let install = install_skill(target, cwd)?; + Ok(render_skill_install_report_json(&install)) + } + Some(args) if is_help_arg(args) => Ok(render_skills_usage_json(None)), + Some(args) => Ok(render_skills_usage_json(Some(args))), + } +} + +#[must_use] +pub fn classify_skills_slash_command(args: Option<&str>) -> SkillSlashDispatch { + match normalize_optional_args(args) { + None | Some("list" | "help" | "-h" | "--help") => SkillSlashDispatch::Local, + Some(args) if args == "install" || args.starts_with("install ") => { + SkillSlashDispatch::Local + } + Some(args) => SkillSlashDispatch::Invoke(format!("${}", args.trim_start_matches('/'))), + } +} + +/// Resolve a skill invocation by validating the skill exists on disk before +/// returning the dispatch. When the skill is not found, returns `Err` with a +/// human-readable message that lists nearby skill names. +pub fn resolve_skill_invocation( + cwd: &Path, + args: Option<&str>, +) -> Result { + let dispatch = classify_skills_slash_command(args); + if let SkillSlashDispatch::Invoke(ref prompt) = dispatch { + // Extract the skill name from the "$skill [args]" prompt. + let skill_token = prompt + .trim_start_matches('$') + .split_whitespace() + .next() + .unwrap_or_default(); + if !skill_token.is_empty() { + if let Err(error) = resolve_skill_path(cwd, skill_token) { + let mut message = format!("Unknown skill: {skill_token} ({error})"); + let roots = discover_skill_roots(cwd); + if let Ok(available) = load_skills_from_roots(&roots) { + let names: Vec = available + .iter() + .filter(|s| s.shadowed_by.is_none()) + .map(|s| s.name.clone()) + .collect(); + if !names.is_empty() { + message.push_str(&format!("\n Available skills: {}", names.join(", "))); + } + } + message.push_str("\n Usage: /skills [list|install |help| [args]]"); + return Err(message); + } + } + } + Ok(dispatch) +} + +pub fn resolve_skill_path(cwd: &Path, skill: &str) -> std::io::Result { + let requested = skill.trim().trim_start_matches('/').trim_start_matches('$'); + if requested.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "skill must not be empty", + )); + } + + let roots = discover_skill_roots(cwd); + for root in &roots { + let mut entries = Vec::new(); + for entry in fs::read_dir(&root.path)? { + let entry = entry?; + match root.origin { + SkillOrigin::SkillsDir => { + if !entry.path().is_dir() { + continue; + } + let skill_path = entry.path().join("SKILL.md"); + if !skill_path.is_file() { + continue; + } + let contents = fs::read_to_string(&skill_path)?; + let (name, _) = parse_skill_frontmatter(&contents); + entries.push(( + name.unwrap_or_else(|| entry.file_name().to_string_lossy().to_string()), + skill_path, + )); + } + SkillOrigin::LegacyCommandsDir => { + let path = entry.path(); + let markdown_path = if path.is_dir() { + let skill_path = path.join("SKILL.md"); + if !skill_path.is_file() { + continue; + } + skill_path + } else if path + .extension() + .is_some_and(|ext| ext.to_string_lossy().eq_ignore_ascii_case("md")) + { + path + } else { + continue; + }; + + let contents = fs::read_to_string(&markdown_path)?; + let fallback_name = markdown_path.file_stem().map_or_else( + || entry.file_name().to_string_lossy().to_string(), + |stem| stem.to_string_lossy().to_string(), + ); + let (name, _) = parse_skill_frontmatter(&contents); + entries.push((name.unwrap_or(fallback_name), markdown_path)); + } + } + } + entries.sort_by(|left, right| left.0.cmp(&right.0)); + if let Some((_, path)) = entries + .into_iter() + .find(|(name, _)| name.eq_ignore_ascii_case(requested)) + { + return Ok(path); + } + } + + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + format!("unknown skill: {requested}"), + )) +} + fn render_mcp_report_for( loader: &ConfigLoader, cwd: &Path, args: Option<&str>, ) -> Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_mcp_usage(None), + ["show", ..] => render_mcp_usage(Some("show")), + _ => render_mcp_usage(Some(&help_path.join(" "))), + }); + } + } + match normalize_optional_args(args) { None | Some("list") => { let runtime_config = loader.load()?; @@ -2195,7 +2464,7 @@ fn render_mcp_report_for( runtime_config.mcp().servers(), )) } - Some("-h" | "--help" | "help") => Ok(render_mcp_usage(None)), + Some(args) if is_help_arg(args) => Ok(render_mcp_usage(None)), Some("show") => Ok(render_mcp_usage(Some("show"))), Some(args) if args.split_whitespace().next() == Some("show") => { let mut parts = args.split_whitespace(); @@ -2217,6 +2486,51 @@ fn render_mcp_report_for( } } +fn render_mcp_report_json_for( + loader: &ConfigLoader, + cwd: &Path, + args: Option<&str>, +) -> Result { + if let Some(args) = normalize_optional_args(args) { + if let Some(help_path) = help_path_from_args(args) { + return Ok(match help_path.as_slice() { + [] => render_mcp_usage_json(None), + ["show", ..] => render_mcp_usage_json(Some("show")), + _ => render_mcp_usage_json(Some(&help_path.join(" "))), + }); + } + } + + match normalize_optional_args(args) { + None | Some("list") => { + let runtime_config = loader.load()?; + Ok(render_mcp_summary_report_json( + cwd, + runtime_config.mcp().servers(), + )) + } + Some(args) if is_help_arg(args) => Ok(render_mcp_usage_json(None)), + Some("show") => Ok(render_mcp_usage_json(Some("show"))), + Some(args) if args.split_whitespace().next() == Some("show") => { + let mut parts = args.split_whitespace(); + let _ = parts.next(); + let Some(server_name) = parts.next() else { + return Ok(render_mcp_usage_json(Some("show"))); + }; + if parts.next().is_some() { + return Ok(render_mcp_usage_json(Some(args))); + } + let runtime_config = loader.load()?; + Ok(render_mcp_server_report_json( + cwd, + server_name, + runtime_config.mcp().get(server_name), + )) + } + Some(args) => Ok(render_mcp_usage_json(Some(args))), + } +} + #[must_use] pub fn render_plugins_report(plugins: &[PluginSummary]) -> String { let mut lines = vec!["Plugins".to_string()]; @@ -2273,6 +2587,11 @@ fn discover_definition_roots(cwd: &Path, leaf: &str) -> Vec<(DefinitionSource, P let mut roots = Vec::new(); for ancestor in cwd.ancestors() { + push_unique_root( + &mut roots, + DefinitionSource::ProjectClaw, + ancestor.join(".claw").join(leaf), + ); push_unique_root( &mut roots, DefinitionSource::ProjectCodex, @@ -2285,6 +2604,14 @@ fn discover_definition_roots(cwd: &Path, leaf: &str) -> Vec<(DefinitionSource, P ); } + if let Ok(claw_config_home) = env::var("CLAW_CONFIG_HOME") { + push_unique_root( + &mut roots, + DefinitionSource::UserClawConfigHome, + PathBuf::from(claw_config_home).join(leaf), + ); + } + if let Ok(codex_home) = env::var("CODEX_HOME") { push_unique_root( &mut roots, @@ -2293,8 +2620,21 @@ fn discover_definition_roots(cwd: &Path, leaf: &str) -> Vec<(DefinitionSource, P ); } + if let Ok(claude_config_dir) = env::var("CLAUDE_CONFIG_DIR") { + push_unique_root( + &mut roots, + DefinitionSource::UserClaude, + PathBuf::from(claude_config_dir).join(leaf), + ); + } + if let Some(home) = env::var_os("HOME") { let home = PathBuf::from(home); + push_unique_root( + &mut roots, + DefinitionSource::UserClaw, + home.join(".claw").join(leaf), + ); push_unique_root( &mut roots, DefinitionSource::UserCodex, @@ -2310,10 +2650,29 @@ fn discover_definition_roots(cwd: &Path, leaf: &str) -> Vec<(DefinitionSource, P roots } +#[allow(clippy::too_many_lines)] fn discover_skill_roots(cwd: &Path) -> Vec { let mut roots = Vec::new(); for ancestor in cwd.ancestors() { + push_unique_skill_root( + &mut roots, + DefinitionSource::ProjectClaw, + ancestor.join(".claw").join("skills"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::ProjectClaw, + ancestor.join(".omc").join("skills"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::ProjectClaw, + ancestor.join(".agents").join("skills"), + SkillOrigin::SkillsDir, + ); push_unique_skill_root( &mut roots, DefinitionSource::ProjectCodex, @@ -2326,6 +2685,12 @@ fn discover_skill_roots(cwd: &Path) -> Vec { ancestor.join(".claude").join("skills"), SkillOrigin::SkillsDir, ); + push_unique_skill_root( + &mut roots, + DefinitionSource::ProjectClaw, + ancestor.join(".claw").join("commands"), + SkillOrigin::LegacyCommandsDir, + ); push_unique_skill_root( &mut roots, DefinitionSource::ProjectCodex, @@ -2340,6 +2705,22 @@ fn discover_skill_roots(cwd: &Path) -> Vec { ); } + if let Ok(claw_config_home) = env::var("CLAW_CONFIG_HOME") { + let claw_config_home = PathBuf::from(claw_config_home); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClawConfigHome, + claw_config_home.join("skills"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClawConfigHome, + claw_config_home.join("commands"), + SkillOrigin::LegacyCommandsDir, + ); + } + if let Ok(codex_home) = env::var("CODEX_HOME") { let codex_home = PathBuf::from(codex_home); push_unique_skill_root( @@ -2358,6 +2739,24 @@ fn discover_skill_roots(cwd: &Path) -> Vec { if let Some(home) = env::var_os("HOME") { let home = PathBuf::from(home); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaw, + home.join(".claw").join("skills"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaw, + home.join(".omc").join("skills"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaw, + home.join(".claw").join("commands"), + SkillOrigin::LegacyCommandsDir, + ); push_unique_skill_root( &mut roots, DefinitionSource::UserCodex, @@ -2376,6 +2775,12 @@ fn discover_skill_roots(cwd: &Path) -> Vec { home.join(".claude").join("skills"), SkillOrigin::SkillsDir, ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaude, + home.join(".claude").join("skills").join("omc-learned"), + SkillOrigin::SkillsDir, + ); push_unique_skill_root( &mut roots, DefinitionSource::UserClaude, @@ -2384,6 +2789,29 @@ fn discover_skill_roots(cwd: &Path) -> Vec { ); } + if let Ok(claude_config_dir) = env::var("CLAUDE_CONFIG_DIR") { + let claude_config_dir = PathBuf::from(claude_config_dir); + let skills_dir = claude_config_dir.join("skills"); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaude, + skills_dir.clone(), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaude, + skills_dir.join("omc-learned"), + SkillOrigin::SkillsDir, + ); + push_unique_skill_root( + &mut roots, + DefinitionSource::UserClaude, + claude_config_dir.join("commands"), + SkillOrigin::LegacyCommandsDir, + ); + } + roots } @@ -2438,15 +2866,18 @@ fn install_skill_into( } fn default_skill_install_root() -> std::io::Result { + if let Ok(claw_config_home) = env::var("CLAW_CONFIG_HOME") { + return Ok(PathBuf::from(claw_config_home).join("skills")); + } if let Ok(codex_home) = env::var("CODEX_HOME") { return Ok(PathBuf::from(codex_home).join("skills")); } if let Some(home) = env::var_os("HOME") { - return Ok(PathBuf::from(home).join(".codex").join("skills")); + return Ok(PathBuf::from(home).join(".claw").join("skills")); } Err(std::io::Error::new( std::io::ErrorKind::NotFound, - "unable to resolve a skills install root; set CODEX_HOME or HOME", + "unable to resolve a skills install root; set CLAW_CONFIG_HOME or HOME", )) } @@ -2812,22 +3243,20 @@ fn render_agents_report(agents: &[AgentSummary]) -> String { String::new(), ]; - for source in [ - DefinitionSource::ProjectCodex, - DefinitionSource::ProjectClaude, - DefinitionSource::UserCodexHome, - DefinitionSource::UserCodex, - DefinitionSource::UserClaude, + for scope in [ + DefinitionScope::Project, + DefinitionScope::UserConfigHome, + DefinitionScope::UserHome, ] { let group = agents .iter() - .filter(|agent| agent.source == source) + .filter(|agent| agent.source.report_scope() == scope) .collect::>(); if group.is_empty() { continue; } - lines.push(format!("{}:", source.label())); + lines.push(format!("{}:", scope.label())); for agent in group { let detail = agent_detail(agent); match agent.shadowed_by { @@ -2841,6 +3270,25 @@ fn render_agents_report(agents: &[AgentSummary]) -> String { lines.join("\n").trim_end().to_string() } +fn render_agents_report_json(cwd: &Path, agents: &[AgentSummary]) -> Value { + let active = agents + .iter() + .filter(|agent| agent.shadowed_by.is_none()) + .count(); + json!({ + "kind": "agents", + "action": "list", + "working_directory": cwd.display().to_string(), + "count": agents.len(), + "summary": { + "total": agents.len(), + "active": active, + "shadowed": agents.len().saturating_sub(active), + }, + "agents": agents.iter().map(agent_summary_json).collect::>(), + }) +} + fn agent_detail(agent: &AgentSummary) -> String { let mut parts = vec![agent.name.clone()]; if let Some(description) = &agent.description { @@ -2870,22 +3318,20 @@ fn render_skills_report(skills: &[SkillSummary]) -> String { String::new(), ]; - for source in [ - DefinitionSource::ProjectCodex, - DefinitionSource::ProjectClaude, - DefinitionSource::UserCodexHome, - DefinitionSource::UserCodex, - DefinitionSource::UserClaude, + for scope in [ + DefinitionScope::Project, + DefinitionScope::UserConfigHome, + DefinitionScope::UserHome, ] { let group = skills .iter() - .filter(|skill| skill.source == source) + .filter(|skill| skill.source.report_scope() == scope) .collect::>(); if group.is_empty() { continue; } - lines.push(format!("{}:", source.label())); + lines.push(format!("{}:", scope.label())); for skill in group { let mut parts = vec![skill.name.clone()]; if let Some(description) = &skill.description { @@ -2906,6 +3352,23 @@ fn render_skills_report(skills: &[SkillSummary]) -> String { lines.join("\n").trim_end().to_string() } +fn render_skills_report_json(skills: &[SkillSummary]) -> Value { + let active = skills + .iter() + .filter(|skill| skill.shadowed_by.is_none()) + .count(); + json!({ + "kind": "skills", + "action": "list", + "summary": { + "total": skills.len(), + "active": active, + "shadowed": skills.len().saturating_sub(active), + }, + "skills": skills.iter().map(skill_summary_json).collect::>(), + }) +} + fn render_skill_install_report(skill: &InstalledSkill) -> String { let mut lines = vec![ "Skills".to_string(), @@ -2927,6 +3390,20 @@ fn render_skill_install_report(skill: &InstalledSkill) -> String { lines.join("\n") } +fn render_skill_install_report_json(skill: &InstalledSkill) -> Value { + json!({ + "kind": "skills", + "action": "install", + "result": "installed", + "invocation_name": &skill.invocation_name, + "invoke_as": format!("${}", skill.invocation_name), + "display_name": &skill.display_name, + "source": skill.source.display().to_string(), + "registry_root": skill.registry_root.display().to_string(), + "installed_path": skill.installed_path.display().to_string(), + }) +} + fn render_mcp_summary_report( cwd: &Path, servers: &BTreeMap, @@ -2954,6 +3431,22 @@ fn render_mcp_summary_report( lines.join("\n") } +fn render_mcp_summary_report_json( + cwd: &Path, + servers: &BTreeMap, +) -> Value { + json!({ + "kind": "mcp", + "action": "list", + "working_directory": cwd.display().to_string(), + "configured_servers": servers.len(), + "servers": servers + .iter() + .map(|(name, server)| mcp_server_json(name, server)) + .collect::>(), + }) +} + fn render_mcp_server_report( cwd: &Path, server_name: &str, @@ -3032,16 +3525,51 @@ fn render_mcp_server_report( lines.join("\n") } + +fn render_mcp_server_report_json( + cwd: &Path, + server_name: &str, + server: Option<&ScopedMcpServerConfig>, +) -> Value { + match server { + Some(server) => json!({ + "kind": "mcp", + "action": "show", + "working_directory": cwd.display().to_string(), + "found": true, + "server": mcp_server_json(server_name, server), + }), + None => json!({ + "kind": "mcp", + "action": "show", + "working_directory": cwd.display().to_string(), + "found": false, + "server_name": server_name, + "message": format!("server `{server_name}` is not configured"), + }), + } +} + fn normalize_optional_args(args: Option<&str>) -> Option<&str> { args.map(str::trim).filter(|value| !value.is_empty()) } +fn is_help_arg(arg: &str) -> bool { + matches!(arg, "help" | "-h" | "--help") +} + +fn help_path_from_args(args: &str) -> Option> { + let parts = args.split_whitespace().collect::>(); + let help_index = parts.iter().position(|part| is_help_arg(part))?; + Some(parts[..help_index].to_vec()) +} + fn render_agents_usage(unexpected: Option<&str>) -> String { let mut lines = vec![ "Agents".to_string(), " Usage /agents [list|help]".to_string(), " Direct CLI claw agents".to_string(), - " Sources .codex/agents, .claude/agents, $CODEX_HOME/agents".to_string(), + " Sources .claw/agents, ~/.claw/agents, $CLAW_CONFIG_HOME/agents".to_string(), ]; if let Some(args) = unexpected { lines.push(format!(" Unexpected {args}")); @@ -3049,13 +3577,28 @@ fn render_agents_usage(unexpected: Option<&str>) -> String { lines.join("\n") } +fn render_agents_usage_json(unexpected: Option<&str>) -> Value { + json!({ + "kind": "agents", + "action": "help", + "usage": { + "slash_command": "/agents [list|help]", + "direct_cli": "claw agents [list|help]", + "sources": [".claw/agents", "~/.claw/agents", "$CLAW_CONFIG_HOME/agents"], + }, + "unexpected": unexpected, + }) +} + fn render_skills_usage(unexpected: Option<&str>) -> String { let mut lines = vec![ "Skills".to_string(), - " Usage /skills [list|install |help]".to_string(), - " Direct CLI claw skills [list|install |help]".to_string(), - " Install root $CODEX_HOME/skills or ~/.codex/skills".to_string(), - " Sources .codex/skills, .claude/skills, legacy /commands".to_string(), + " Usage /skills [list|install |help| [args]]".to_string(), + " Alias /skill".to_string(), + " Direct CLI claw skills [list|install |help| [args]]".to_string(), + " Invoke /skills help overview -> $help overview".to_string(), + " Install root $CLAW_CONFIG_HOME/skills or ~/.claw/skills".to_string(), + " Sources .claw/skills, .omc/skills, .agents/skills, .codex/skills, .claude/skills, ~/.claw/skills, ~/.omc/skills, ~/.claude/skills/omc-learned, ~/.codex/skills, ~/.claude/skills, legacy /commands".to_string(), ]; if let Some(args) = unexpected { lines.push(format!(" Unexpected {args}")); @@ -3063,6 +3606,35 @@ fn render_skills_usage(unexpected: Option<&str>) -> String { lines.join("\n") } +fn render_skills_usage_json(unexpected: Option<&str>) -> Value { + json!({ + "kind": "skills", + "action": "help", + "usage": { + "slash_command": "/skills [list|install |help| [args]]", + "aliases": ["/skill"], + "direct_cli": "claw skills [list|install |help| [args]]", + "invoke": "/skills help overview -> $help overview", + "install_root": "$CLAW_CONFIG_HOME/skills or ~/.claw/skills", + "sources": [ + ".claw/skills", + ".omc/skills", + ".agents/skills", + ".codex/skills", + ".claude/skills", + "~/.claw/skills", + "~/.omc/skills", + "~/.claude/skills/omc-learned", + "~/.codex/skills", + "~/.claude/skills", + "legacy /commands", + "legacy fallback dirs still load automatically" + ], + }, + "unexpected": unexpected, + }) +} + fn render_mcp_usage(unexpected: Option<&str>) -> String { let mut lines = vec![ "MCP".to_string(), @@ -3076,6 +3648,19 @@ fn render_mcp_usage(unexpected: Option<&str>) -> String { lines.join("\n") } +fn render_mcp_usage_json(unexpected: Option<&str>) -> Value { + json!({ + "kind": "mcp", + "action": "help", + "usage": { + "slash_command": "/mcp [list|show |help]", + "direct_cli": "claw mcp [list|show |help]", + "sources": [".claw/settings.json", ".claw/settings.local.json"], + }, + "unexpected": unexpected, + }) +} + fn config_source_label(source: ConfigSource) -> &'static str { match source { ConfigSource::User => "user", @@ -3152,6 +3737,138 @@ fn format_mcp_oauth(oauth: Option<&McpOAuthConfig>) -> String { } } +fn definition_source_id(source: DefinitionSource) -> &'static str { + match source { + DefinitionSource::ProjectClaw + | DefinitionSource::ProjectCodex + | DefinitionSource::ProjectClaude => "project_claw", + DefinitionSource::UserClawConfigHome | DefinitionSource::UserCodexHome => { + "user_claw_config_home" + } + DefinitionSource::UserClaw | DefinitionSource::UserCodex | DefinitionSource::UserClaude => { + "user_claw" + } + } +} + +fn definition_source_json(source: DefinitionSource) -> Value { + json!({ + "id": definition_source_id(source), + "label": source.label(), + }) +} + +fn agent_summary_json(agent: &AgentSummary) -> Value { + json!({ + "name": &agent.name, + "description": &agent.description, + "model": &agent.model, + "reasoning_effort": &agent.reasoning_effort, + "source": definition_source_json(agent.source), + "active": agent.shadowed_by.is_none(), + "shadowed_by": agent.shadowed_by.map(definition_source_json), + }) +} + +fn skill_origin_id(origin: SkillOrigin) -> &'static str { + match origin { + SkillOrigin::SkillsDir => "skills_dir", + SkillOrigin::LegacyCommandsDir => "legacy_commands_dir", + } +} + +fn skill_origin_json(origin: SkillOrigin) -> Value { + json!({ + "id": skill_origin_id(origin), + "detail_label": origin.detail_label(), + }) +} + +fn skill_summary_json(skill: &SkillSummary) -> Value { + json!({ + "name": &skill.name, + "description": &skill.description, + "source": definition_source_json(skill.source), + "origin": skill_origin_json(skill.origin), + "active": skill.shadowed_by.is_none(), + "shadowed_by": skill.shadowed_by.map(definition_source_json), + }) +} + +fn config_source_id(source: ConfigSource) -> &'static str { + match source { + ConfigSource::User => "user", + ConfigSource::Project => "project", + ConfigSource::Local => "local", + } +} + +fn config_source_json(source: ConfigSource) -> Value { + json!({ + "id": config_source_id(source), + "label": config_source_label(source), + }) +} + +fn mcp_transport_json(config: &McpServerConfig) -> Value { + let label = mcp_transport_label(config); + json!({ + "id": label, + "label": label, + }) +} + +fn mcp_oauth_json(oauth: Option<&McpOAuthConfig>) -> Value { + let Some(oauth) = oauth else { + return Value::Null; + }; + json!({ + "client_id": &oauth.client_id, + "callback_port": oauth.callback_port, + "auth_server_metadata_url": &oauth.auth_server_metadata_url, + "xaa": oauth.xaa, + }) +} + +fn mcp_server_details_json(config: &McpServerConfig) -> Value { + match config { + McpServerConfig::Stdio(config) => json!({ + "command": &config.command, + "args": &config.args, + "env_keys": config.env.keys().cloned().collect::>(), + "tool_call_timeout_ms": config.tool_call_timeout_ms, + }), + McpServerConfig::Sse(config) | McpServerConfig::Http(config) => json!({ + "url": &config.url, + "header_keys": config.headers.keys().cloned().collect::>(), + "headers_helper": &config.headers_helper, + "oauth": mcp_oauth_json(config.oauth.as_ref()), + }), + McpServerConfig::Ws(config) => json!({ + "url": &config.url, + "header_keys": config.headers.keys().cloned().collect::>(), + "headers_helper": &config.headers_helper, + }), + McpServerConfig::Sdk(config) => json!({ + "name": &config.name, + }), + McpServerConfig::ManagedProxy(config) => json!({ + "url": &config.url, + "id": &config.id, + }), + } +} + +fn mcp_server_json(name: &str, server: &ScopedMcpServerConfig) -> Value { + json!({ + "name": name, + "scope": config_source_json(server.scope), + "transport": mcp_transport_json(&server.config), + "summary": mcp_server_summary(&server.config), + "details": mcp_server_details_json(&server.config), + }) +} + #[must_use] pub fn handle_slash_command( input: &str, @@ -3254,6 +3971,7 @@ pub fn handle_slash_command( | SlashCommand::Tag { .. } | SlashCommand::OutputStyle { .. } | SlashCommand::AddDir { .. } + | SlashCommand::History { .. } | SlashCommand::Unknown(_) => None, } } @@ -3261,18 +3979,23 @@ pub fn handle_slash_command( #[cfg(test)] mod tests { use super::{ - handle_plugins_slash_command, handle_slash_command, load_agents_from_roots, - load_skills_from_roots, render_agents_report, render_plugins_report, render_skills_report, - render_slash_command_help, render_slash_command_help_detail, - resume_supported_slash_commands, slash_command_specs, suggest_slash_commands, - validate_slash_command_input, DefinitionSource, SkillOrigin, SkillRoot, SlashCommand, + classify_skills_slash_command, handle_agents_slash_command_json, + handle_plugins_slash_command, handle_skills_slash_command_json, handle_slash_command, + load_agents_from_roots, load_skills_from_roots, render_agents_report, + render_agents_report_json, render_mcp_report_json_for, render_plugins_report, + render_skills_report, render_slash_command_help, render_slash_command_help_detail, + resolve_skill_path, resume_supported_slash_commands, slash_command_specs, + suggest_slash_commands, validate_slash_command_input, DefinitionSource, SkillOrigin, + SkillRoot, SkillSlashDispatch, SlashCommand, }; use plugins::{PluginKind, PluginManager, PluginManagerConfig, PluginMetadata, PluginSummary}; use runtime::{ CompactionConfig, ConfigLoader, ContentBlock, ConversationMessage, MessageRole, Session, }; + use std::ffi::OsString; use std::fs; use std::path::{Path, PathBuf}; + use std::sync::{Mutex, OnceLock}; use std::time::{SystemTime, UNIX_EPOCH}; fn temp_dir(label: &str) -> PathBuf { @@ -3283,6 +4006,18 @@ mod tests { std::env::temp_dir().join(format!("commands-plugin-{label}-{nanos}")) } + fn env_lock() -> &'static Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) + } + + fn restore_env_var(key: &str, original: Option) { + match original { + Some(value) => std::env::set_var(key, value), + None => std::env::remove_var(key), + } + } + fn write_external_plugin(root: &Path, name: &str, version: &str) { fs::create_dir_all(root.join(".claude-plugin")).expect("manifest dir"); fs::write( @@ -3551,6 +4286,47 @@ mod tests { ); } + #[test] + fn parses_history_command_without_count() { + // given + let input = "/history"; + + // when + let parsed = SlashCommand::parse(input); + + // then + assert_eq!(parsed, Ok(Some(SlashCommand::History { count: None }))); + } + + #[test] + fn parses_history_command_with_numeric_count() { + // given + let input = "/history 25"; + + // when + let parsed = SlashCommand::parse(input); + + // then + assert_eq!( + parsed, + Ok(Some(SlashCommand::History { + count: Some("25".to_string()) + })) + ); + } + + #[test] + fn rejects_history_with_extra_arguments() { + // given + let input = "/history 25 extra"; + + // when + let error = parse_error_message(input); + + // then + assert!(error.contains("Usage: /history [count]")); + } + #[test] fn rejects_unexpected_arguments_for_no_arg_commands() { // given @@ -3592,7 +4368,7 @@ mod tests { // then assert!(error.contains("Usage: /teleport ")); - assert!(error.contains(" Category Discovery & debugging")); + assert!(error.contains(" Category Tools")); } #[test] @@ -3613,24 +4389,40 @@ mod tests { } #[test] - fn rejects_invalid_agents_and_skills_arguments() { + fn rejects_invalid_agents_arguments() { // given let agents_input = "/agents show planner"; - let skills_input = "/skills show help"; // when let agents_error = parse_error_message(agents_input); - let skills_error = parse_error_message(skills_input); // then assert!(agents_error.contains( "Unexpected arguments for /agents: show planner. Use /agents, /agents list, or /agents help." )); assert!(agents_error.contains(" Usage /agents [list|help]")); - assert!(skills_error.contains( - "Unexpected arguments for /skills: show help. Use /skills, /skills list, /skills install , or /skills help." - )); - assert!(skills_error.contains(" Usage /skills [list|install |help]")); + } + + #[test] + fn accepts_skills_invocation_arguments_for_prompt_dispatch() { + assert_eq!( + SlashCommand::parse("/skills help overview"), + Ok(Some(SlashCommand::Skills { + args: Some("help overview".to_string()), + })) + ); + assert_eq!( + classify_skills_slash_command(Some("help overview")), + SkillSlashDispatch::Invoke("$help overview".to_string()) + ); + assert_eq!( + classify_skills_slash_command(Some("/test")), + SkillSlashDispatch::Invoke("$test".to_string()) + ); + assert_eq!( + classify_skills_slash_command(Some("install ./skill-pack")), + SkillSlashDispatch::Local + ); } #[test] @@ -3650,10 +4442,10 @@ mod tests { let help = render_slash_command_help(); assert!(help.contains("Start here /status, /diff, /agents, /skills, /commit")); assert!(help.contains("[resume] also works with --resume SESSION.jsonl")); - assert!(help.contains("Session & visibility")); - assert!(help.contains("Workspace & git")); - assert!(help.contains("Discovery & debugging")); - assert!(help.contains("Analysis & automation")); + assert!(help.contains("Session")); + assert!(help.contains("Tools")); + assert!(help.contains("Config")); + assert!(help.contains("Debug")); assert!(help.contains("/help")); assert!(help.contains("/status")); assert!(help.contains("/sandbox")); @@ -3684,11 +4476,59 @@ mod tests { )); assert!(help.contains("aliases: /plugins, /marketplace")); assert!(help.contains("/agents [list|help]")); - assert!(help.contains("/skills [list|install |help]")); + assert!(help.contains("/skills [list|install |help| [args]]")); + assert!(help.contains("aliases: /skill")); assert_eq!(slash_command_specs().len(), 141); assert!(resume_supported_slash_commands().len() >= 39); } + #[test] + fn renders_help_with_grouped_categories_and_keyboard_shortcuts() { + // given + let categories = ["Session", "Tools", "Config", "Debug"]; + + // when + let help = render_slash_command_help(); + + // then + for category in categories { + assert!( + help.contains(category), + "expected help to contain category {category}" + ); + } + let session_index = help.find("Session").expect("Session header should exist"); + let tools_index = help.find("Tools").expect("Tools header should exist"); + let config_index = help.find("Config").expect("Config header should exist"); + let debug_index = help.find("Debug").expect("Debug header should exist"); + assert!(session_index < tools_index); + assert!(tools_index < config_index); + assert!(config_index < debug_index); + + assert!(help.contains("Keyboard shortcuts")); + assert!(help.contains("Up/Down Navigate prompt history")); + assert!(help.contains("Tab Complete commands, modes, and recent sessions")); + assert!(help.contains("Ctrl-C Clear input (or exit on empty prompt)")); + assert!(help.contains("Shift+Enter/Ctrl+J Insert a newline")); + + // every command should still render with a summary line + for spec in slash_command_specs() { + let usage = match spec.argument_hint { + Some(hint) => format!("/{} {hint}", spec.name), + None => format!("/{}", spec.name), + }; + assert!( + help.contains(&usage), + "expected help to contain command {usage}" + ); + assert!( + help.contains(spec.summary), + "expected help to contain summary for /{}", + spec.name + ); + } + } + #[test] fn renders_per_command_help_detail() { // given @@ -3701,7 +4541,7 @@ mod tests { assert!(help.contains("/plugin")); assert!(help.contains("Summary Manage Claw Code plugins")); assert!(help.contains("Aliases /plugins, /marketplace")); - assert!(help.contains("Category Workspace & git")); + assert!(help.contains("Category Tools")); } #[test] @@ -3709,7 +4549,7 @@ mod tests { let help = render_slash_command_help_detail("mcp").expect("detail help should exist"); assert!(help.contains("/mcp")); assert!(help.contains("Summary Inspect configured MCP servers")); - assert!(help.contains("Category Discovery & debugging")); + assert!(help.contains("Category Tools")); assert!(help.contains("Resume Supported with --resume SESSION.jsonl")); } @@ -3894,7 +4734,7 @@ mod tests { let workspace = temp_dir("agents-workspace"); let project_agents = workspace.join(".codex").join("agents"); let user_home = temp_dir("agents-home"); - let user_agents = user_home.join(".codex").join("agents"); + let user_agents = user_home.join(".claude").join("agents"); write_agent( &project_agents, @@ -3927,16 +4767,82 @@ mod tests { assert!(report.contains("Agents")); assert!(report.contains("2 active agents")); - assert!(report.contains("Project (.codex):")); + assert!(report.contains("Project roots:")); assert!(report.contains("planner · Project planner · gpt-5.4 · medium")); - assert!(report.contains("User (~/.codex):")); - assert!(report.contains("(shadowed by Project (.codex)) planner · User planner")); + assert!(report.contains("User home roots:")); + assert!(report.contains("(shadowed by Project roots) planner · User planner")); assert!(report.contains("verifier · Verification agent · gpt-5.4-mini · high")); let _ = fs::remove_dir_all(workspace); let _ = fs::remove_dir_all(user_home); } + #[test] + fn renders_agents_reports_as_json() { + let workspace = temp_dir("agents-json-workspace"); + let project_agents = workspace.join(".codex").join("agents"); + let user_home = temp_dir("agents-json-home"); + let user_agents = user_home.join(".codex").join("agents"); + + write_agent( + &project_agents, + "planner", + "Project planner", + "gpt-5.4", + "medium", + ); + write_agent( + &project_agents, + "verifier", + "Verification agent", + "gpt-5.4-mini", + "high", + ); + write_agent( + &user_agents, + "planner", + "User planner", + "gpt-5.4-mini", + "high", + ); + + let roots = vec![ + (DefinitionSource::ProjectCodex, project_agents), + (DefinitionSource::UserCodex, user_agents), + ]; + let report = render_agents_report_json( + &workspace, + &load_agents_from_roots(&roots).expect("agent roots should load"), + ); + + assert_eq!(report["kind"], "agents"); + assert_eq!(report["action"], "list"); + assert_eq!(report["working_directory"], workspace.display().to_string()); + assert_eq!(report["count"], 3); + assert_eq!(report["summary"]["active"], 2); + assert_eq!(report["summary"]["shadowed"], 1); + assert_eq!(report["agents"][0]["name"], "planner"); + assert_eq!(report["agents"][0]["model"], "gpt-5.4"); + assert_eq!(report["agents"][0]["active"], true); + assert_eq!(report["agents"][1]["name"], "verifier"); + assert_eq!(report["agents"][2]["name"], "planner"); + assert_eq!(report["agents"][2]["active"], false); + assert_eq!(report["agents"][2]["shadowed_by"]["id"], "project_claw"); + + let help = handle_agents_slash_command_json(Some("help"), &workspace).expect("agents help"); + assert_eq!(help["kind"], "agents"); + assert_eq!(help["action"], "help"); + assert_eq!(help["usage"]["direct_cli"], "claw agents [list|help]"); + + let unexpected = handle_agents_slash_command_json(Some("show planner"), &workspace) + .expect("agents usage"); + assert_eq!(unexpected["action"], "help"); + assert_eq!(unexpected["unexpected"], "show planner"); + + let _ = fs::remove_dir_all(workspace); + let _ = fs::remove_dir_all(user_home); + } + #[test] fn lists_skills_from_project_and_user_roots() { let workspace = temp_dir("skills-workspace"); @@ -3972,18 +4878,92 @@ mod tests { assert!(report.contains("Skills")); assert!(report.contains("3 available skills")); - assert!(report.contains("Project (.codex):")); + assert!(report.contains("Project roots:")); assert!(report.contains("plan · Project planning guidance")); - assert!(report.contains("Project (.claude):")); assert!(report.contains("deploy · Legacy deployment guidance · legacy /commands")); - assert!(report.contains("User (~/.codex):")); - assert!(report.contains("(shadowed by Project (.codex)) plan · User planning guidance")); + assert!(report.contains("User home roots:")); + assert!(report.contains("(shadowed by Project roots) plan · User planning guidance")); assert!(report.contains("help · Help guidance")); let _ = fs::remove_dir_all(workspace); let _ = fs::remove_dir_all(user_home); } + #[test] + fn resolves_project_skills_and_legacy_commands_from_shared_registry() { + let workspace = temp_dir("resolve-project-skills"); + let project_skills = workspace.join(".claw").join("skills"); + let legacy_commands = workspace.join(".claw").join("commands"); + + write_skill(&project_skills, "plan", "Project planning guidance"); + write_legacy_command(&legacy_commands, "handoff", "Legacy handoff guidance"); + + assert_eq!( + resolve_skill_path(&workspace, "$plan").expect("project skill should resolve"), + project_skills.join("plan").join("SKILL.md") + ); + assert_eq!( + resolve_skill_path(&workspace, "/handoff").expect("legacy command should resolve"), + legacy_commands.join("handoff.md") + ); + } + + #[test] + fn renders_skills_reports_as_json() { + let workspace = temp_dir("skills-json-workspace"); + let project_skills = workspace.join(".codex").join("skills"); + let project_commands = workspace.join(".claude").join("commands"); + let user_home = temp_dir("skills-json-home"); + let user_skills = user_home.join(".codex").join("skills"); + + write_skill(&project_skills, "plan", "Project planning guidance"); + write_legacy_command(&project_commands, "deploy", "Legacy deployment guidance"); + write_skill(&user_skills, "plan", "User planning guidance"); + write_skill(&user_skills, "help", "Help guidance"); + + let roots = vec![ + SkillRoot { + source: DefinitionSource::ProjectCodex, + path: project_skills, + origin: SkillOrigin::SkillsDir, + }, + SkillRoot { + source: DefinitionSource::ProjectClaude, + path: project_commands, + origin: SkillOrigin::LegacyCommandsDir, + }, + SkillRoot { + source: DefinitionSource::UserCodex, + path: user_skills, + origin: SkillOrigin::SkillsDir, + }, + ]; + let report = super::render_skills_report_json( + &load_skills_from_roots(&roots).expect("skills should load"), + ); + assert_eq!(report["kind"], "skills"); + assert_eq!(report["action"], "list"); + assert_eq!(report["summary"]["active"], 3); + assert_eq!(report["summary"]["shadowed"], 1); + assert_eq!(report["skills"][0]["name"], "plan"); + assert_eq!(report["skills"][0]["source"]["id"], "project_claw"); + assert_eq!(report["skills"][1]["name"], "deploy"); + assert_eq!(report["skills"][1]["origin"]["id"], "legacy_commands_dir"); + assert_eq!(report["skills"][3]["shadowed_by"]["id"], "project_claw"); + + let help = handle_skills_slash_command_json(Some("help"), &workspace).expect("skills help"); + assert_eq!(help["kind"], "skills"); + assert_eq!(help["action"], "help"); + assert_eq!(help["usage"]["aliases"][0], "/skill"); + assert_eq!( + help["usage"]["direct_cli"], + "claw skills [list|install |help| [args]]" + ); + + let _ = fs::remove_dir_all(workspace); + let _ = fs::remove_dir_all(user_home); + } + #[test] fn agents_and_skills_usage_support_help_and_unexpected_args() { let cwd = temp_dir("slash-usage"); @@ -3992,6 +4972,8 @@ mod tests { super::handle_agents_slash_command(Some("help"), &cwd).expect("agents help"); assert!(agents_help.contains("Usage /agents [list|help]")); assert!(agents_help.contains("Direct CLI claw agents")); + assert!(agents_help + .contains("Sources .claw/agents, ~/.claw/agents, $CLAW_CONFIG_HOME/agents")); let agents_unexpected = super::handle_agents_slash_command(Some("show planner"), &cwd).expect("agents usage"); @@ -3999,17 +4981,114 @@ mod tests { let skills_help = super::handle_skills_slash_command(Some("--help"), &cwd).expect("skills help"); - assert!(skills_help.contains("Usage /skills [list|install |help]")); - assert!(skills_help.contains("Install root $CODEX_HOME/skills or ~/.codex/skills")); + assert!(skills_help + .contains("Usage /skills [list|install |help| [args]]")); + assert!(skills_help.contains("Alias /skill")); + assert!(skills_help.contains("Invoke /skills help overview -> $help overview")); + assert!(skills_help.contains("Install root $CLAW_CONFIG_HOME/skills or ~/.claw/skills")); + assert!(skills_help.contains(".omc/skills")); + assert!(skills_help.contains(".agents/skills")); + assert!(skills_help.contains("~/.claude/skills/omc-learned")); assert!(skills_help.contains("legacy /commands")); let skills_unexpected = super::handle_skills_slash_command(Some("show help"), &cwd).expect("skills usage"); - assert!(skills_unexpected.contains("Unexpected show help")); + assert!(skills_unexpected.contains("Unexpected show")); + + let skills_install_help = super::handle_skills_slash_command(Some("install --help"), &cwd) + .expect("nested skills help"); + assert!(skills_install_help + .contains("Usage /skills [list|install |help| [args]]")); + assert!(skills_install_help.contains("Alias /skill")); + assert!(skills_install_help.contains("Unexpected install")); + + let skills_unknown_help = + super::handle_skills_slash_command(Some("show --help"), &cwd).expect("skills help"); + assert!(skills_unknown_help + .contains("Usage /skills [list|install |help| [args]]")); + assert!(skills_unknown_help.contains("Unexpected show")); + + let skills_help_json = + super::handle_skills_slash_command_json(Some("help"), &cwd).expect("skills help json"); + let sources = skills_help_json["usage"]["sources"] + .as_array() + .expect("skills help sources"); + assert_eq!(skills_help_json["usage"]["aliases"][0], "/skill"); + assert!(sources.iter().any(|value| value == ".omc/skills")); + assert!(sources.iter().any(|value| value == ".agents/skills")); + assert!(sources.iter().any(|value| value == "~/.omc/skills")); + assert!(sources + .iter() + .any(|value| value == "~/.claude/skills/omc-learned")); let _ = fs::remove_dir_all(cwd); } + #[test] + fn discovers_omc_skills_from_project_and_user_compatibility_roots() { + let _guard = env_lock().lock().expect("env lock"); + let workspace = temp_dir("skills-omc-workspace"); + let user_home = temp_dir("skills-omc-home"); + let claude_config_dir = temp_dir("skills-omc-claude-config"); + let project_omc_skills = workspace.join(".omc").join("skills"); + let project_agents_skills = workspace.join(".agents").join("skills"); + let user_omc_skills = user_home.join(".omc").join("skills"); + let claude_config_skills = claude_config_dir.join("skills"); + let claude_config_commands = claude_config_dir.join("commands"); + let learned_skills = claude_config_dir.join("skills").join("omc-learned"); + let original_home = std::env::var_os("HOME"); + let original_claude_config_dir = std::env::var_os("CLAUDE_CONFIG_DIR"); + + write_skill(&project_omc_skills, "hud", "OMC HUD guidance"); + write_skill( + &project_agents_skills, + "trace", + "Compatibility skill guidance", + ); + write_skill(&user_omc_skills, "cancel", "OMC cancel guidance"); + write_skill( + &claude_config_skills, + "statusline", + "Claude config skill guidance", + ); + write_legacy_command( + &claude_config_commands, + "doctor-check", + "Claude config command guidance", + ); + write_skill(&learned_skills, "learned", "Learned skill guidance"); + std::env::set_var("HOME", &user_home); + std::env::set_var("CLAUDE_CONFIG_DIR", &claude_config_dir); + + let report = super::handle_skills_slash_command(None, &workspace).expect("skills list"); + assert!(report.contains("available skills")); + assert!(report.contains("hud · OMC HUD guidance")); + assert!(report.contains("trace · Compatibility skill guidance")); + assert!(report.contains("cancel · OMC cancel guidance")); + assert!(report.contains("statusline · Claude config skill guidance")); + assert!(report.contains("doctor-check · Claude config command guidance · legacy /commands")); + assert!(report.contains("learned · Learned skill guidance")); + + let help = + super::handle_skills_slash_command_json(Some("help"), &workspace).expect("skills help"); + let sources = help["usage"]["sources"] + .as_array() + .expect("skills help sources"); + assert_eq!(help["usage"]["aliases"][0], "/skill"); + assert!(sources.iter().any(|value| value == ".omc/skills")); + assert!(sources.iter().any(|value| value == ".agents/skills")); + assert!(sources.iter().any(|value| value == "~/.omc/skills")); + assert!(sources + .iter() + .any(|value| value == "~/.claude/skills/omc-learned")); + + restore_env_var("HOME", original_home); + restore_env_var("CLAUDE_CONFIG_DIR", original_claude_config_dir); + let _ = fs::remove_dir_all(workspace); + let _ = fs::remove_dir_all(user_home); + let _ = fs::remove_dir_all(claude_config_dir); + } + #[test] fn mcp_usage_supports_help_and_unexpected_args() { let cwd = temp_dir("mcp-usage"); @@ -4022,6 +5101,16 @@ mod tests { super::handle_mcp_slash_command(Some("show alpha beta"), &cwd).expect("mcp usage"); assert!(unexpected.contains("Unexpected show alpha beta")); + let nested_help = + super::handle_mcp_slash_command(Some("show --help"), &cwd).expect("mcp help"); + assert!(nested_help.contains("Usage /mcp [list|show |help]")); + assert!(nested_help.contains("Unexpected show")); + + let unknown_help = + super::handle_mcp_slash_command(Some("inspect --help"), &cwd).expect("mcp usage"); + assert!(unknown_help.contains("Usage /mcp [list|show |help]")); + assert!(unknown_help.contains("Unexpected inspect")); + let _ = fs::remove_dir_all(cwd); } @@ -4102,6 +5191,88 @@ mod tests { let _ = fs::remove_dir_all(config_home); } + #[test] + fn renders_mcp_reports_as_json() { + let workspace = temp_dir("mcp-json-workspace"); + let config_home = temp_dir("mcp-json-home"); + fs::create_dir_all(workspace.join(".claw")).expect("workspace config dir"); + fs::create_dir_all(&config_home).expect("config home"); + fs::write( + workspace.join(".claw").join("settings.json"), + r#"{ + "mcpServers": { + "alpha": { + "command": "uvx", + "args": ["alpha-server"], + "env": {"ALPHA_TOKEN": "secret"}, + "toolCallTimeoutMs": 1200 + }, + "remote": { + "type": "http", + "url": "https://remote.example/mcp", + "headers": {"Authorization": "Bearer secret"}, + "headersHelper": "./bin/headers", + "oauth": { + "clientId": "remote-client", + "callbackPort": 7878 + } + } + } + }"#, + ) + .expect("write settings"); + fs::write( + workspace.join(".claw").join("settings.local.json"), + r#"{ + "mcpServers": { + "remote": { + "type": "ws", + "url": "wss://remote.example/mcp" + } + } + }"#, + ) + .expect("write local settings"); + + let loader = ConfigLoader::new(&workspace, &config_home); + let list = + render_mcp_report_json_for(&loader, &workspace, None).expect("mcp list json render"); + assert_eq!(list["kind"], "mcp"); + assert_eq!(list["action"], "list"); + assert_eq!(list["configured_servers"], 2); + assert_eq!(list["servers"][0]["name"], "alpha"); + assert_eq!(list["servers"][0]["transport"]["id"], "stdio"); + assert_eq!(list["servers"][0]["details"]["command"], "uvx"); + assert_eq!(list["servers"][1]["name"], "remote"); + assert_eq!(list["servers"][1]["scope"]["id"], "local"); + assert_eq!(list["servers"][1]["transport"]["id"], "ws"); + assert_eq!( + list["servers"][1]["details"]["url"], + "wss://remote.example/mcp" + ); + + let show = render_mcp_report_json_for(&loader, &workspace, Some("show alpha")) + .expect("mcp show json render"); + assert_eq!(show["action"], "show"); + assert_eq!(show["found"], true); + assert_eq!(show["server"]["name"], "alpha"); + assert_eq!(show["server"]["details"]["env_keys"][0], "ALPHA_TOKEN"); + assert_eq!(show["server"]["details"]["tool_call_timeout_ms"], 1200); + + let missing = render_mcp_report_json_for(&loader, &workspace, Some("show missing")) + .expect("mcp missing json render"); + assert_eq!(missing["found"], false); + assert_eq!(missing["server_name"], "missing"); + + let help = + render_mcp_report_json_for(&loader, &workspace, Some("help")).expect("mcp help json"); + assert_eq!(help["action"], "help"); + assert_eq!(help["usage"]["sources"][0], ".claw/settings.json"); + + let _ = fs::remove_dir_all(workspace); + let _ = fs::remove_dir_all(config_home); + } + #[test] fn parses_quoted_skill_frontmatter_values() { let contents = "---\nname: \"hud\"\ndescription: 'Quoted description'\n---\n"; @@ -4154,7 +5325,7 @@ mod tests { let listed = render_skills_report( &load_skills_from_roots(&roots).expect("installed skills should load"), ); - assert!(listed.contains("User ($CODEX_HOME):")); + assert!(listed.contains("User config roots:")); assert!(listed.contains("help · Helpful skill")); let _ = fs::remove_dir_all(workspace); diff --git a/rust/crates/plugins/src/lib.rs b/rust/crates/plugins/src/lib.rs index 3070d177..052d1bab 100644 --- a/rust/crates/plugins/src/lib.rs +++ b/rust/crates/plugins/src/lib.rs @@ -920,6 +920,9 @@ pub enum PluginManifestValidationError { tool_name: String, permission: String, }, + UnsupportedManifestContract { + detail: String, + }, } impl Display for PluginManifestValidationError { @@ -965,6 +968,7 @@ impl Display for PluginManifestValidationError { f, "plugin tool `{tool_name}` requiredPermission `{permission}` must be read-only, workspace-write, or danger-full-access" ), + Self::UnsupportedManifestContract { detail } => f.write_str(detail), } } } @@ -1594,10 +1598,73 @@ fn load_manifest_from_path( manifest_path.display() )) })?; - let raw_manifest: RawPluginManifest = serde_json::from_str(&contents)?; + let raw_json: Value = serde_json::from_str(&contents)?; + let compatibility_errors = detect_claude_code_manifest_contract_gaps(&raw_json); + if !compatibility_errors.is_empty() { + return Err(PluginError::ManifestValidation(compatibility_errors)); + } + let raw_manifest: RawPluginManifest = serde_json::from_value(raw_json)?; build_plugin_manifest(root, raw_manifest) } +fn detect_claude_code_manifest_contract_gaps( + raw_manifest: &Value, +) -> Vec { + let Some(root) = raw_manifest.as_object() else { + return Vec::new(); + }; + + let mut errors = Vec::new(); + + for (field, detail) in [ + ( + "skills", + "plugin manifest field `skills` uses the Claude Code plugin contract; `claw` does not load plugin-managed skills and instead discovers skills from local roots such as `.claw/skills`, `.omc/skills`, `.agents/skills`, `~/.omc/skills`, and `~/.claude/skills/omc-learned`.", + ), + ( + "mcpServers", + "plugin manifest field `mcpServers` uses the Claude Code plugin contract; `claw` does not import MCP servers from plugin manifests.", + ), + ( + "agents", + "plugin manifest field `agents` uses the Claude Code plugin contract; `claw` does not load plugin-managed agent markdown catalogs from plugin manifests.", + ), + ] { + if root.contains_key(field) { + errors.push(PluginManifestValidationError::UnsupportedManifestContract { + detail: detail.to_string(), + }); + } + } + + if root + .get("commands") + .and_then(Value::as_array) + .is_some_and(|commands| commands.iter().any(Value::is_string)) + { + errors.push(PluginManifestValidationError::UnsupportedManifestContract { + detail: "plugin manifest field `commands` uses Claude Code-style directory globs; `claw` slash dispatch is still built-in and does not load plugin slash command markdown files.".to_string(), + }); + } + + if let Some(hooks) = root.get("hooks").and_then(Value::as_object) { + for hook_name in hooks.keys() { + if !matches!( + hook_name.as_str(), + "PreToolUse" | "PostToolUse" | "PostToolUseFailure" + ) { + errors.push(PluginManifestValidationError::UnsupportedManifestContract { + detail: format!( + "plugin hook `{hook_name}` uses the Claude Code lifecycle contract; `claw` plugins currently support only PreToolUse, PostToolUse, and PostToolUseFailure." + ), + }); + } + } + } + + errors +} + fn plugin_manifest_path(root: &Path) -> Result { let direct_path = root.join(MANIFEST_FILE_NAME); if direct_path.exists() { @@ -2517,6 +2584,37 @@ mod tests { let _ = fs::remove_dir_all(root); } + #[test] + fn load_plugin_from_directory_rejects_claude_code_manifest_contracts_with_guidance() { + let root = temp_dir("manifest-claude-code-contract"); + write_file( + root.join(MANIFEST_FILE_NAME).as_path(), + r#"{ + "name": "oh-my-claudecode", + "version": "4.10.2", + "description": "Claude Code plugin manifest", + "hooks": { + "SessionStart": ["scripts/session-start.mjs"] + }, + "agents": ["agents/*.md"], + "commands": ["commands/**/*.md"], + "skills": "./skills/", + "mcpServers": "./.mcp.json" +}"#, + ); + + let error = load_plugin_from_directory(&root) + .expect_err("Claude Code plugin manifest should fail with guidance"); + let rendered = error.to_string(); + assert!(rendered.contains("field `skills` uses the Claude Code plugin contract")); + assert!(rendered.contains("field `mcpServers` uses the Claude Code plugin contract")); + assert!(rendered.contains("field `agents` uses the Claude Code plugin contract")); + assert!(rendered.contains("field `commands` uses Claude Code-style directory globs")); + assert!(rendered.contains("hook `SessionStart` uses the Claude Code lifecycle contract")); + + let _ = fs::remove_dir_all(root); + } + #[test] fn load_plugin_from_directory_rejects_missing_tool_or_command_paths() { let root = temp_dir("manifest-paths"); diff --git a/rust/crates/runtime/Cargo.toml b/rust/crates/runtime/Cargo.toml index 8b692176..b1bd04f3 100644 --- a/rust/crates/runtime/Cargo.toml +++ b/rust/crates/runtime/Cargo.toml @@ -13,7 +13,7 @@ regex = "1" serde = { version = "1", features = ["derive"] } serde_json.workspace = true telemetry = { path = "../telemetry" } -tokio = { version = "1", features = ["io-util", "macros", "process", "rt", "rt-multi-thread", "time"] } +tokio = { version = "1", features = ["io-std", "io-util", "macros", "process", "rt", "rt-multi-thread", "time"] } walkdir = "2" [lints] diff --git a/rust/crates/runtime/src/branch_lock.rs b/rust/crates/runtime/src/branch_lock.rs new file mode 100644 index 00000000..6fbf0d09 --- /dev/null +++ b/rust/crates/runtime/src/branch_lock.rs @@ -0,0 +1,144 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct BranchLockIntent { + #[serde(rename = "laneId")] + pub lane_id: String, + pub branch: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub worktree: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub modules: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct BranchLockCollision { + pub branch: String, + pub module: String, + #[serde(rename = "laneIds")] + pub lane_ids: Vec, +} + +#[must_use] +pub fn detect_branch_lock_collisions(intents: &[BranchLockIntent]) -> Vec { + let mut collisions = Vec::new(); + + for (index, left) in intents.iter().enumerate() { + for right in &intents[index + 1..] { + if left.branch != right.branch { + continue; + } + for module in overlapping_modules(&left.modules, &right.modules) { + collisions.push(BranchLockCollision { + branch: left.branch.clone(), + module, + lane_ids: vec![left.lane_id.clone(), right.lane_id.clone()], + }); + } + } + } + + collisions.sort_by(|a, b| { + a.branch + .cmp(&b.branch) + .then(a.module.cmp(&b.module)) + .then(a.lane_ids.cmp(&b.lane_ids)) + }); + collisions.dedup(); + collisions +} + +fn overlapping_modules(left: &[String], right: &[String]) -> Vec { + let mut overlaps = Vec::new(); + for left_module in left { + for right_module in right { + if modules_overlap(left_module, right_module) { + overlaps.push(shared_scope(left_module, right_module)); + } + } + } + overlaps.sort(); + overlaps.dedup(); + overlaps +} + +fn modules_overlap(left: &str, right: &str) -> bool { + left == right + || left.starts_with(&format!("{right}/")) + || right.starts_with(&format!("{left}/")) +} + +fn shared_scope(left: &str, right: &str) -> String { + if left.starts_with(&format!("{right}/")) || left == right { + right.to_string() + } else { + left.to_string() + } +} + +#[cfg(test)] +mod tests { + use super::{detect_branch_lock_collisions, BranchLockIntent}; + + #[test] + fn detects_same_branch_same_module_collisions() { + let collisions = detect_branch_lock_collisions(&[ + BranchLockIntent { + lane_id: "lane-a".to_string(), + branch: "feature/lock".to_string(), + worktree: Some("wt-a".to_string()), + modules: vec!["runtime/mcp".to_string()], + }, + BranchLockIntent { + lane_id: "lane-b".to_string(), + branch: "feature/lock".to_string(), + worktree: Some("wt-b".to_string()), + modules: vec!["runtime/mcp".to_string()], + }, + ]); + + assert_eq!(collisions.len(), 1); + assert_eq!(collisions[0].branch, "feature/lock"); + assert_eq!(collisions[0].module, "runtime/mcp"); + } + + #[test] + fn detects_nested_module_scope_collisions() { + let collisions = detect_branch_lock_collisions(&[ + BranchLockIntent { + lane_id: "lane-a".to_string(), + branch: "feature/lock".to_string(), + worktree: None, + modules: vec!["runtime".to_string()], + }, + BranchLockIntent { + lane_id: "lane-b".to_string(), + branch: "feature/lock".to_string(), + worktree: None, + modules: vec!["runtime/mcp".to_string()], + }, + ]); + + assert_eq!(collisions[0].module, "runtime"); + } + + #[test] + fn ignores_different_branches() { + let collisions = detect_branch_lock_collisions(&[ + BranchLockIntent { + lane_id: "lane-a".to_string(), + branch: "feature/a".to_string(), + worktree: None, + modules: vec!["runtime/mcp".to_string()], + }, + BranchLockIntent { + lane_id: "lane-b".to_string(), + branch: "feature/b".to_string(), + worktree: None, + modules: vec!["runtime/mcp".to_string()], + }, + ]); + + assert!(collisions.is_empty()); + } +} diff --git a/rust/crates/runtime/src/config.rs b/rust/crates/runtime/src/config.rs index 3120ac55..70d69b31 100644 --- a/rust/crates/runtime/src/config.rs +++ b/rust/crates/runtime/src/config.rs @@ -9,6 +9,27 @@ use crate::sandbox::{FilesystemIsolationMode, SandboxConfig}; /// Schema name advertised by generated settings files. pub const CLAW_SETTINGS_SCHEMA_NAME: &str = "SettingsSchema"; +/// Top-level settings keys recognized by the runtime configuration loader. +const KNOWN_TOP_LEVEL_KEYS: &[&str] = &[ + "$schema", + "enabledPlugins", + "env", + "hooks", + "mcpServers", + "model", + "oauth", + "permissionMode", + "permissions", + "plugins", + "sandbox", +]; + +/// Deprecated top-level keys mapped to their replacement guidance. +const DEPRECATED_TOP_LEVEL_KEYS: &[(&str, &str)] = &[ + ("allowedTools", "permissions.allow"), + ("ignorePatterns", "permissions.deny"), +]; + /// Origin of a loaded settings file in the configuration precedence chain. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum ConfigSource { @@ -48,6 +69,7 @@ pub struct RuntimePluginConfig { install_root: Option, registry_path: Option, bundled_root: Option, + max_output_tokens: Option, } /// Structured feature configuration consumed by runtime subsystems. @@ -58,9 +80,20 @@ pub struct RuntimeFeatureConfig { mcp: McpConfigCollection, oauth: Option, model: Option, + aliases: BTreeMap, permission_mode: Option, permission_rules: RuntimePermissionRuleConfig, sandbox: SandboxConfig, + provider_fallbacks: ProviderFallbackConfig, +} + +/// Ordered chain of fallback model identifiers used when the primary +/// provider returns a retryable failure (429/500/503/etc.). The chain is +/// strict: each entry is tried in order until one succeeds. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct ProviderFallbackConfig { + primary: Option, + fallbacks: Vec, } /// Hook command lists grouped by lifecycle stage. @@ -259,17 +292,33 @@ impl ConfigLoader { let mut merged = BTreeMap::new(); let mut loaded_entries = Vec::new(); let mut mcp_servers = BTreeMap::new(); + let mut all_warnings = Vec::new(); for entry in self.discover() { - let Some(value) = read_optional_json_object(&entry.path)? else { + crate::config_validate::check_unsupported_format(&entry.path)?; + let Some(parsed) = read_optional_json_object(&entry.path)? else { continue; }; - validate_optional_hooks_config(&value, &entry.path)?; - merge_mcp_servers(&mut mcp_servers, entry.source, &value, &entry.path)?; - deep_merge_objects(&mut merged, &value); + let validation = crate::config_validate::validate_config_file( + &parsed.object, + &parsed.source, + &entry.path, + ); + if !validation.is_ok() { + let first_error = &validation.errors[0]; + return Err(ConfigError::Parse(first_error.to_string())); + } + all_warnings.extend(validation.warnings); + validate_optional_hooks_config(&parsed.object, &entry.path)?; + merge_mcp_servers(&mut mcp_servers, entry.source, &parsed.object, &entry.path)?; + deep_merge_objects(&mut merged, &parsed.object); loaded_entries.push(entry); } + for warning in &all_warnings { + eprintln!("warning: {warning}"); + } + let merged_value = JsonValue::Object(merged.clone()); let feature_config = RuntimeFeatureConfig { @@ -280,9 +329,11 @@ impl ConfigLoader { }, oauth: parse_optional_oauth_config(&merged_value, "merged settings.oauth")?, model: parse_optional_model(&merged_value), + aliases: parse_optional_aliases(&merged_value)?, permission_mode: parse_optional_permission_mode(&merged_value)?, permission_rules: parse_optional_permission_rules(&merged_value)?, sandbox: parse_optional_sandbox_config(&merged_value)?, + provider_fallbacks: parse_optional_provider_fallbacks(&merged_value)?, }; Ok(RuntimeConfig { @@ -353,6 +404,11 @@ impl RuntimeConfig { self.feature_config.model.as_deref() } + #[must_use] + pub fn aliases(&self) -> &BTreeMap { + &self.feature_config.aliases + } + #[must_use] pub fn permission_mode(&self) -> Option { self.feature_config.permission_mode @@ -367,6 +423,11 @@ impl RuntimeConfig { pub fn sandbox(&self) -> &SandboxConfig { &self.feature_config.sandbox } + + #[must_use] + pub fn provider_fallbacks(&self) -> &ProviderFallbackConfig { + &self.feature_config.provider_fallbacks + } } impl RuntimeFeatureConfig { @@ -407,6 +468,11 @@ impl RuntimeFeatureConfig { self.model.as_deref() } + #[must_use] + pub fn aliases(&self) -> &BTreeMap { + &self.aliases + } + #[must_use] pub fn permission_mode(&self) -> Option { self.permission_mode @@ -421,6 +487,33 @@ impl RuntimeFeatureConfig { pub fn sandbox(&self) -> &SandboxConfig { &self.sandbox } + + #[must_use] + pub fn provider_fallbacks(&self) -> &ProviderFallbackConfig { + &self.provider_fallbacks + } +} + +impl ProviderFallbackConfig { + #[must_use] + pub fn new(primary: Option, fallbacks: Vec) -> Self { + Self { primary, fallbacks } + } + + #[must_use] + pub fn primary(&self) -> Option<&str> { + self.primary.as_deref() + } + + #[must_use] + pub fn fallbacks(&self) -> &[String] { + &self.fallbacks + } + + #[must_use] + pub fn is_empty(&self) -> bool { + self.fallbacks.is_empty() + } } impl RuntimePluginConfig { @@ -449,6 +542,15 @@ impl RuntimePluginConfig { self.bundled_root.as_deref() } + #[must_use] + pub fn max_output_tokens(&self) -> Option { + self.max_output_tokens + } + + pub fn set_max_output_tokens(&mut self, max_output_tokens: Option) { + self.max_output_tokens = max_output_tokens; + } + pub fn set_plugin_state(&mut self, plugin_id: String, enabled: bool) { self.enabled_plugins.insert(plugin_id, enabled); } @@ -572,9 +674,13 @@ impl McpServerConfig { } } -fn read_optional_json_object( - path: &Path, -) -> Result>, ConfigError> { +/// Parsed JSON object paired with its raw source text for validation. +struct ParsedConfigFile { + object: BTreeMap, + source: String, +} + +fn read_optional_json_object(path: &Path) -> Result, ConfigError> { let is_legacy_config = path.file_name().and_then(|name| name.to_str()) == Some(".claw.json"); let contents = match fs::read_to_string(path) { Ok(contents) => contents, @@ -583,7 +689,10 @@ fn read_optional_json_object( }; if contents.trim().is_empty() { - return Ok(Some(BTreeMap::new())); + return Ok(Some(ParsedConfigFile { + object: BTreeMap::new(), + source: contents, + })); } let parsed = match JsonValue::parse(&contents) { @@ -600,7 +709,10 @@ fn read_optional_json_object( path.display() ))); }; - Ok(Some(object.clone())) + Ok(Some(ParsedConfigFile { + object: object.clone(), + source: contents, + })) } fn merge_mcp_servers( @@ -637,6 +749,13 @@ fn parse_optional_model(root: &JsonValue) -> Option { .map(ToOwned::to_owned) } +fn parse_optional_aliases(root: &JsonValue) -> Result, ConfigError> { + let Some(object) = root.as_object() else { + return Ok(BTreeMap::new()); + }; + Ok(optional_string_map(object, "aliases", "merged settings")?.unwrap_or_default()) +} + fn parse_optional_hooks_config(root: &JsonValue) -> Result { let Some(object) = root.as_object() else { return Ok(RuntimeHookConfig::default()); @@ -714,6 +833,7 @@ fn parse_optional_plugin_config(root: &JsonValue) -> Result Result Result { + let Some(object) = root.as_object() else { + return Ok(ProviderFallbackConfig::default()); + }; + let Some(value) = object.get("providerFallbacks") else { + return Ok(ProviderFallbackConfig::default()); + }; + let entry = expect_object(value, "merged settings.providerFallbacks")?; + let primary = + optional_string(entry, "primary", "merged settings.providerFallbacks")?.map(str::to_string); + let fallbacks = optional_string_array(entry, "fallbacks", "merged settings.providerFallbacks")? + .unwrap_or_default(); + Ok(ProviderFallbackConfig { primary, fallbacks }) +} + fn parse_filesystem_mode_label(value: &str) -> Result { match value { "off" => Ok(FilesystemIsolationMode::Off), @@ -957,6 +1094,27 @@ fn optional_u16( } } +fn optional_u32( + object: &BTreeMap, + key: &str, + context: &str, +) -> Result, ConfigError> { + match object.get(key) { + Some(value) => { + let Some(number) = value.as_i64() else { + return Err(ConfigError::Parse(format!( + "{context}: field {key} must be a non-negative integer" + ))); + }; + let number = u32::try_from(number).map_err(|_| { + ConfigError::Parse(format!("{context}: field {key} is out of range")) + })?; + Ok(Some(number)) + } + None => Ok(None), + } +} + fn optional_u64( object: &BTreeMap, key: &str, @@ -1247,6 +1405,66 @@ mod tests { fs::remove_dir_all(root).expect("cleanup temp dir"); } + #[test] + fn parses_provider_fallbacks_chain_with_primary_and_ordered_fallbacks() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + fs::create_dir_all(cwd.join(".claw")).expect("project config dir"); + fs::create_dir_all(&home).expect("home config dir"); + fs::write( + home.join("settings.json"), + r#"{ + "providerFallbacks": { + "primary": "claude-opus-4-6", + "fallbacks": ["grok-3", "grok-3-mini"] + } + }"#, + ) + .expect("write provider fallback settings"); + + // when + let loaded = ConfigLoader::new(&cwd, &home) + .load() + .expect("config should load"); + + // then + let chain = loaded.provider_fallbacks(); + assert_eq!(chain.primary(), Some("claude-opus-4-6")); + assert_eq!( + chain.fallbacks(), + &["grok-3".to_string(), "grok-3-mini".to_string()] + ); + assert!(!chain.is_empty()); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn provider_fallbacks_default_is_empty_when_unset() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + fs::create_dir_all(&home).expect("home config dir"); + fs::create_dir_all(&cwd).expect("project dir"); + fs::write(home.join("settings.json"), "{}").expect("write empty settings"); + + // when + let loaded = ConfigLoader::new(&cwd, &home) + .load() + .expect("config should load"); + + // then + let chain = loaded.provider_fallbacks(); + assert_eq!(chain.primary(), None); + assert!(chain.fallbacks().is_empty()); + assert!(chain.is_empty()); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + #[test] fn parses_typed_mcp_and_oauth_config() { let root = temp_dir(); @@ -1493,6 +1711,49 @@ mod tests { fs::remove_dir_all(root).expect("cleanup temp dir"); } + #[test] + fn parses_user_defined_model_aliases_from_settings() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + fs::create_dir_all(cwd.join(".claw")).expect("project config dir"); + fs::create_dir_all(&home).expect("home config dir"); + + fs::write( + home.join("settings.json"), + r#"{"aliases":{"fast":"claude-haiku-4-5-20251213","smart":"claude-opus-4-6"}}"#, + ) + .expect("write user settings"); + fs::write( + cwd.join(".claw").join("settings.local.json"), + r#"{"aliases":{"smart":"claude-sonnet-4-6","cheap":"grok-3-mini"}}"#, + ) + .expect("write local settings"); + + // when + let loaded = ConfigLoader::new(&cwd, &home) + .load() + .expect("config should load"); + + // then + let aliases = loaded.aliases(); + assert_eq!( + aliases.get("fast").map(String::as_str), + Some("claude-haiku-4-5-20251213") + ); + assert_eq!( + aliases.get("smart").map(String::as_str), + Some("claude-sonnet-4-6") + ); + assert_eq!( + aliases.get("cheap").map(String::as_str), + Some("grok-3-mini") + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + #[test] fn empty_settings_file_loads_defaults() { // given @@ -1574,12 +1835,13 @@ mod tests { .load() .expect_err("config should fail"); - // then + // then — config validation now catches the mixed array before the hooks parser let rendered = error.to_string(); - assert!(rendered.contains(&format!( - "{}: hooks: field PreToolUse must contain only strings", - project_settings.display() - ))); + assert!( + rendered.contains("hooks.PreToolUse") + && rendered.contains("must be an array of strings"), + "expected validation error for hooks.PreToolUse, got: {rendered}" + ); assert!(!rendered.contains("merged settings.hooks")); fs::remove_dir_all(root).expect("cleanup temp dir"); @@ -1645,4 +1907,140 @@ mod tests { assert!(config.state_for("missing", true)); assert!(!config.state_for("missing", false)); } + + #[test] + fn validates_unknown_top_level_keys_with_line_and_field_name() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + let user_settings = home.join("settings.json"); + fs::create_dir_all(&home).expect("home config dir"); + fs::create_dir_all(&cwd).expect("project dir"); + fs::write( + &user_settings, + "{\n \"model\": \"opus\",\n \"telemetry\": true\n}\n", + ) + .expect("write user settings"); + + // when + let error = ConfigLoader::new(&cwd, &home) + .load() + .expect_err("config should fail"); + + // then + let rendered = error.to_string(); + assert!( + rendered.contains(&format!("{}:3:", user_settings.display())), + "error should include file path and line number, got: {rendered}" + ); + assert!( + rendered.contains("unknown field telemetry"), + "error should name the offending field, got: {rendered}" + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn validates_deprecated_top_level_keys_with_replacement_guidance() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + let user_settings = home.join("settings.json"); + fs::create_dir_all(&home).expect("home config dir"); + fs::create_dir_all(&cwd).expect("project dir"); + fs::write( + &user_settings, + "{\n \"model\": \"opus\",\n \"allowedTools\": [\"Read\"]\n}\n", + ) + .expect("write user settings"); + + // when + let error = ConfigLoader::new(&cwd, &home) + .load() + .expect_err("config should fail"); + + // then + let rendered = error.to_string(); + assert!( + rendered.contains(&format!("{}:3:", user_settings.display())), + "error should include file path and line number, got: {rendered}" + ); + assert!( + rendered.contains("deprecated field allowedTools"), + "error should call out the deprecated field, got: {rendered}" + ); + assert!( + rendered.contains("permissions.allow"), + "error should mention the replacement field, got: {rendered}" + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn validates_wrong_type_for_known_field_with_field_path() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + let user_settings = home.join("settings.json"); + fs::create_dir_all(&home).expect("home config dir"); + fs::create_dir_all(&cwd).expect("project dir"); + fs::write( + &user_settings, + "{\n \"hooks\": {\n \"PreToolUse\": \"not-an-array\"\n }\n}\n", + ) + .expect("write user settings"); + + // when + let error = ConfigLoader::new(&cwd, &home) + .load() + .expect_err("config should fail"); + + // then + let rendered = error.to_string(); + assert!( + rendered.contains(&format!("{}: hooks", user_settings.display())), + "error should include file path and field path, got: {rendered}" + ); + assert!( + rendered.contains("PreToolUse must be an array"), + "error should describe the type mismatch, got: {rendered}" + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn unknown_top_level_key_suggests_closest_match() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + let user_settings = home.join("settings.json"); + fs::create_dir_all(&home).expect("home config dir"); + fs::create_dir_all(&cwd).expect("project dir"); + fs::write(&user_settings, "{\n \"modle\": \"opus\"\n}\n").expect("write user settings"); + + // when + let error = ConfigLoader::new(&cwd, &home) + .load() + .expect_err("config should fail"); + + // then + let rendered = error.to_string(); + assert!( + rendered.contains("unknown field modle"), + "error should name the offending field, got: {rendered}" + ); + assert!( + rendered.contains("did you mean model?"), + "error should suggest the closest known key, got: {rendered}" + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } } diff --git a/rust/crates/runtime/src/config_validate.rs b/rust/crates/runtime/src/config_validate.rs new file mode 100644 index 00000000..31ccde1c --- /dev/null +++ b/rust/crates/runtime/src/config_validate.rs @@ -0,0 +1,887 @@ +use std::collections::BTreeMap; +use std::path::Path; + +use crate::config::ConfigError; +use crate::json::JsonValue; + +/// Diagnostic emitted when a config file contains a suspect field. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ConfigDiagnostic { + pub path: String, + pub field: String, + pub line: Option, + pub kind: DiagnosticKind, +} + +/// Classification of the diagnostic. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum DiagnosticKind { + UnknownKey { + suggestion: Option, + }, + WrongType { + expected: &'static str, + got: &'static str, + }, + Deprecated { + replacement: &'static str, + }, +} + +impl std::fmt::Display for ConfigDiagnostic { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let location = self + .line + .map_or_else(String::new, |line| format!(" (line {line})")); + match &self.kind { + DiagnosticKind::UnknownKey { suggestion: None } => { + write!(f, "{}: unknown key \"{}\"{location}", self.path, self.field) + } + DiagnosticKind::UnknownKey { + suggestion: Some(hint), + } => { + write!( + f, + "{}: unknown key \"{}\"{location}. Did you mean \"{}\"?", + self.path, self.field, hint + ) + } + DiagnosticKind::WrongType { expected, got } => { + write!( + f, + "{}: field \"{}\" must be {expected}, got {got}{location}", + self.path, self.field + ) + } + DiagnosticKind::Deprecated { replacement } => { + write!( + f, + "{}: field \"{}\" is deprecated{location}. Use \"{replacement}\" instead", + self.path, self.field + ) + } + } + } +} + +/// Result of validating a single config file. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ValidationResult { + pub errors: Vec, + pub warnings: Vec, +} + +impl ValidationResult { + #[must_use] + pub fn is_ok(&self) -> bool { + self.errors.is_empty() + } + + fn merge(&mut self, other: Self) { + self.errors.extend(other.errors); + self.warnings.extend(other.warnings); + } +} + +// ---- known-key schema ---- + +/// Expected type for a config field. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum FieldType { + String, + Bool, + Object, + StringArray, + Number, +} + +impl FieldType { + fn label(self) -> &'static str { + match self { + Self::String => "a string", + Self::Bool => "a boolean", + Self::Object => "an object", + Self::StringArray => "an array of strings", + Self::Number => "a number", + } + } + + fn matches(self, value: &JsonValue) -> bool { + match self { + Self::String => value.as_str().is_some(), + Self::Bool => value.as_bool().is_some(), + Self::Object => value.as_object().is_some(), + Self::StringArray => value + .as_array() + .is_some_and(|arr| arr.iter().all(|v| v.as_str().is_some())), + Self::Number => value.as_i64().is_some(), + } + } +} + +fn json_type_label(value: &JsonValue) -> &'static str { + match value { + JsonValue::Null => "null", + JsonValue::Bool(_) => "a boolean", + JsonValue::Number(_) => "a number", + JsonValue::String(_) => "a string", + JsonValue::Array(_) => "an array", + JsonValue::Object(_) => "an object", + } +} + +struct FieldSpec { + name: &'static str, + expected: FieldType, +} + +struct DeprecatedField { + name: &'static str, + replacement: &'static str, +} + +const TOP_LEVEL_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "$schema", + expected: FieldType::String, + }, + FieldSpec { + name: "model", + expected: FieldType::String, + }, + FieldSpec { + name: "hooks", + expected: FieldType::Object, + }, + FieldSpec { + name: "permissions", + expected: FieldType::Object, + }, + FieldSpec { + name: "permissionMode", + expected: FieldType::String, + }, + FieldSpec { + name: "mcpServers", + expected: FieldType::Object, + }, + FieldSpec { + name: "oauth", + expected: FieldType::Object, + }, + FieldSpec { + name: "enabledPlugins", + expected: FieldType::Object, + }, + FieldSpec { + name: "plugins", + expected: FieldType::Object, + }, + FieldSpec { + name: "sandbox", + expected: FieldType::Object, + }, + FieldSpec { + name: "env", + expected: FieldType::Object, + }, +]; + +const HOOKS_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "PreToolUse", + expected: FieldType::StringArray, + }, + FieldSpec { + name: "PostToolUse", + expected: FieldType::StringArray, + }, + FieldSpec { + name: "PostToolUseFailure", + expected: FieldType::StringArray, + }, +]; + +const PERMISSIONS_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "defaultMode", + expected: FieldType::String, + }, + FieldSpec { + name: "allow", + expected: FieldType::StringArray, + }, + FieldSpec { + name: "deny", + expected: FieldType::StringArray, + }, + FieldSpec { + name: "ask", + expected: FieldType::StringArray, + }, +]; + +const PLUGINS_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "enabled", + expected: FieldType::Object, + }, + FieldSpec { + name: "externalDirectories", + expected: FieldType::StringArray, + }, + FieldSpec { + name: "installRoot", + expected: FieldType::String, + }, + FieldSpec { + name: "registryPath", + expected: FieldType::String, + }, + FieldSpec { + name: "bundledRoot", + expected: FieldType::String, + }, + FieldSpec { + name: "maxOutputTokens", + expected: FieldType::Number, + }, +]; + +const SANDBOX_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "enabled", + expected: FieldType::Bool, + }, + FieldSpec { + name: "namespaceRestrictions", + expected: FieldType::Bool, + }, + FieldSpec { + name: "networkIsolation", + expected: FieldType::Bool, + }, + FieldSpec { + name: "filesystemMode", + expected: FieldType::String, + }, + FieldSpec { + name: "allowedMounts", + expected: FieldType::StringArray, + }, +]; + +const OAUTH_FIELDS: &[FieldSpec] = &[ + FieldSpec { + name: "clientId", + expected: FieldType::String, + }, + FieldSpec { + name: "authorizeUrl", + expected: FieldType::String, + }, + FieldSpec { + name: "tokenUrl", + expected: FieldType::String, + }, + FieldSpec { + name: "callbackPort", + expected: FieldType::Number, + }, + FieldSpec { + name: "manualRedirectUrl", + expected: FieldType::String, + }, + FieldSpec { + name: "scopes", + expected: FieldType::StringArray, + }, +]; + +const DEPRECATED_FIELDS: &[DeprecatedField] = &[ + DeprecatedField { + name: "permissionMode", + replacement: "permissions.defaultMode", + }, + DeprecatedField { + name: "enabledPlugins", + replacement: "plugins.enabled", + }, +]; + +// ---- line-number resolution ---- + +/// Find the 1-based line number where a JSON key first appears in the raw source. +fn find_key_line(source: &str, key: &str) -> Option { + // Search for `"key"` followed by optional whitespace and a colon. + let needle = format!("\"{key}\""); + let mut search_start = 0; + while let Some(offset) = source[search_start..].find(&needle) { + let absolute = search_start + offset; + let after = absolute + needle.len(); + // Verify the next non-whitespace char is `:` to confirm this is a key, not a value. + if source[after..].chars().find(|ch| !ch.is_ascii_whitespace()) == Some(':') { + return Some(source[..absolute].chars().filter(|&ch| ch == '\n').count() + 1); + } + search_start = after; + } + None +} + +// ---- core validation ---- + +fn validate_object_keys( + object: &BTreeMap, + known_fields: &[FieldSpec], + prefix: &str, + source: &str, + path_display: &str, +) -> ValidationResult { + let mut result = ValidationResult { + errors: Vec::new(), + warnings: Vec::new(), + }; + + let known_names: Vec<&str> = known_fields.iter().map(|f| f.name).collect(); + + for (key, value) in object { + let field_path = if prefix.is_empty() { + key.clone() + } else { + format!("{prefix}.{key}") + }; + + if let Some(spec) = known_fields.iter().find(|f| f.name == key) { + // Type check. + if !spec.expected.matches(value) { + result.errors.push(ConfigDiagnostic { + path: path_display.to_string(), + field: field_path, + line: find_key_line(source, key), + kind: DiagnosticKind::WrongType { + expected: spec.expected.label(), + got: json_type_label(value), + }, + }); + } + } else { + // Unknown key. + let suggestion = suggest_field(key, &known_names); + result.errors.push(ConfigDiagnostic { + path: path_display.to_string(), + field: field_path, + line: find_key_line(source, key), + kind: DiagnosticKind::UnknownKey { suggestion }, + }); + } + } + + result +} + +fn suggest_field(input: &str, candidates: &[&str]) -> Option { + let input_lower = input.to_ascii_lowercase(); + candidates + .iter() + .filter_map(|candidate| { + let distance = simple_edit_distance(&input_lower, &candidate.to_ascii_lowercase()); + (distance <= 3).then_some((distance, *candidate)) + }) + .min_by_key(|(distance, _)| *distance) + .map(|(_, name)| name.to_string()) +} + +fn simple_edit_distance(left: &str, right: &str) -> usize { + if left.is_empty() { + return right.len(); + } + if right.is_empty() { + return left.len(); + } + let right_chars: Vec = right.chars().collect(); + let mut previous: Vec = (0..=right_chars.len()).collect(); + let mut current = vec![0; right_chars.len() + 1]; + + for (left_index, left_char) in left.chars().enumerate() { + current[0] = left_index + 1; + for (right_index, right_char) in right_chars.iter().enumerate() { + let cost = usize::from(left_char != *right_char); + current[right_index + 1] = (previous[right_index + 1] + 1) + .min(current[right_index] + 1) + .min(previous[right_index] + cost); + } + previous.clone_from(¤t); + } + + previous[right_chars.len()] +} + +/// Validate a parsed config file's keys and types against the known schema. +/// +/// Returns diagnostics (errors and deprecation warnings) without blocking the load. +pub fn validate_config_file( + object: &BTreeMap, + source: &str, + file_path: &Path, +) -> ValidationResult { + let path_display = file_path.display().to_string(); + let mut result = validate_object_keys(object, TOP_LEVEL_FIELDS, "", source, &path_display); + + // Check deprecated fields. + for deprecated in DEPRECATED_FIELDS { + if object.contains_key(deprecated.name) { + result.warnings.push(ConfigDiagnostic { + path: path_display.clone(), + field: deprecated.name.to_string(), + line: find_key_line(source, deprecated.name), + kind: DiagnosticKind::Deprecated { + replacement: deprecated.replacement, + }, + }); + } + } + + // Validate known nested objects. + if let Some(hooks) = object.get("hooks").and_then(JsonValue::as_object) { + result.merge(validate_object_keys( + hooks, + HOOKS_FIELDS, + "hooks", + source, + &path_display, + )); + } + if let Some(permissions) = object.get("permissions").and_then(JsonValue::as_object) { + result.merge(validate_object_keys( + permissions, + PERMISSIONS_FIELDS, + "permissions", + source, + &path_display, + )); + } + if let Some(plugins) = object.get("plugins").and_then(JsonValue::as_object) { + result.merge(validate_object_keys( + plugins, + PLUGINS_FIELDS, + "plugins", + source, + &path_display, + )); + } + if let Some(sandbox) = object.get("sandbox").and_then(JsonValue::as_object) { + result.merge(validate_object_keys( + sandbox, + SANDBOX_FIELDS, + "sandbox", + source, + &path_display, + )); + } + if let Some(oauth) = object.get("oauth").and_then(JsonValue::as_object) { + result.merge(validate_object_keys( + oauth, + OAUTH_FIELDS, + "oauth", + source, + &path_display, + )); + } + + result +} + +/// Check whether a file path uses an unsupported config format (e.g. TOML). +pub fn check_unsupported_format(file_path: &Path) -> Result<(), ConfigError> { + if let Some(ext) = file_path.extension().and_then(|e| e.to_str()) { + if ext.eq_ignore_ascii_case("toml") { + return Err(ConfigError::Parse(format!( + "{}: TOML config files are not supported. Use JSON (settings.json) instead", + file_path.display() + ))); + } + } + Ok(()) +} + +/// Format all diagnostics into a human-readable report. +#[must_use] +pub fn format_diagnostics(result: &ValidationResult) -> String { + let mut lines = Vec::new(); + for warning in &result.warnings { + lines.push(format!("warning: {warning}")); + } + for error in &result.errors { + lines.push(format!("error: {error}")); + } + lines.join("\n") +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + fn test_path() -> PathBuf { + PathBuf::from("/test/settings.json") + } + + #[test] + fn detects_unknown_top_level_key() { + // given + let source = r#"{"model": "opus", "unknownField": true}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "unknownField"); + assert!(matches!( + result.errors[0].kind, + DiagnosticKind::UnknownKey { .. } + )); + } + + #[test] + fn detects_wrong_type_for_model() { + // given + let source = r#"{"model": 123}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "model"); + assert!(matches!( + result.errors[0].kind, + DiagnosticKind::WrongType { + expected: "a string", + got: "a number" + } + )); + } + + #[test] + fn detects_deprecated_permission_mode() { + // given + let source = r#"{"permissionMode": "plan"}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.warnings.len(), 1); + assert_eq!(result.warnings[0].field, "permissionMode"); + assert!(matches!( + result.warnings[0].kind, + DiagnosticKind::Deprecated { + replacement: "permissions.defaultMode" + } + )); + } + + #[test] + fn detects_deprecated_enabled_plugins() { + // given + let source = r#"{"enabledPlugins": {"tool-guard@builtin": true}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.warnings.len(), 1); + assert_eq!(result.warnings[0].field, "enabledPlugins"); + assert!(matches!( + result.warnings[0].kind, + DiagnosticKind::Deprecated { + replacement: "plugins.enabled" + } + )); + } + + #[test] + fn reports_line_number_for_unknown_key() { + // given + let source = "{\n \"model\": \"opus\",\n \"badKey\": true\n}"; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].line, Some(3)); + assert_eq!(result.errors[0].field, "badKey"); + } + + #[test] + fn reports_line_number_for_wrong_type() { + // given + let source = "{\n \"model\": 42\n}"; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].line, Some(2)); + } + + #[test] + fn validates_nested_hooks_keys() { + // given + let source = r#"{"hooks": {"PreToolUse": ["cmd"], "BadHook": ["x"]}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "hooks.BadHook"); + } + + #[test] + fn validates_nested_permissions_keys() { + // given + let source = r#"{"permissions": {"allow": ["Read"], "denyAll": true}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "permissions.denyAll"); + } + + #[test] + fn validates_nested_sandbox_keys() { + // given + let source = r#"{"sandbox": {"enabled": true, "containerMode": "strict"}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "sandbox.containerMode"); + } + + #[test] + fn validates_nested_plugins_keys() { + // given + let source = r#"{"plugins": {"installRoot": "/tmp", "autoUpdate": true}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "plugins.autoUpdate"); + } + + #[test] + fn validates_nested_oauth_keys() { + // given + let source = r#"{"oauth": {"clientId": "abc", "secret": "hidden"}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "oauth.secret"); + } + + #[test] + fn valid_config_produces_no_diagnostics() { + // given + let source = r#"{ + "model": "opus", + "hooks": {"PreToolUse": ["guard"]}, + "permissions": {"defaultMode": "plan", "allow": ["Read"]}, + "mcpServers": {}, + "sandbox": {"enabled": false} +}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert!(result.is_ok()); + assert!(result.warnings.is_empty()); + } + + #[test] + fn suggests_close_field_name() { + // given + let source = r#"{"modle": "opus"}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + match &result.errors[0].kind { + DiagnosticKind::UnknownKey { + suggestion: Some(s), + } => assert_eq!(s, "model"), + other => panic!("expected suggestion, got {other:?}"), + } + } + + #[test] + fn format_diagnostics_includes_all_entries() { + // given + let source = r#"{"permissionMode": "plan", "badKey": 1}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + let result = validate_config_file(object, source, &test_path()); + + // when + let output = format_diagnostics(&result); + + // then + assert!(output.contains("warning:")); + assert!(output.contains("error:")); + assert!(output.contains("badKey")); + assert!(output.contains("permissionMode")); + } + + #[test] + fn check_unsupported_format_rejects_toml() { + // given + let path = PathBuf::from("/home/.claw/settings.toml"); + + // when + let result = check_unsupported_format(&path); + + // then + assert!(result.is_err()); + let message = result.unwrap_err().to_string(); + assert!(message.contains("TOML")); + assert!(message.contains("settings.toml")); + } + + #[test] + fn check_unsupported_format_allows_json() { + // given + let path = PathBuf::from("/home/.claw/settings.json"); + + // when / then + assert!(check_unsupported_format(&path).is_ok()); + } + + #[test] + fn wrong_type_in_nested_sandbox_field() { + // given + let source = r#"{"sandbox": {"enabled": "yes"}}"#; + let parsed = JsonValue::parse(source).expect("valid json"); + let object = parsed.as_object().expect("object"); + + // when + let result = validate_config_file(object, source, &test_path()); + + // then + assert_eq!(result.errors.len(), 1); + assert_eq!(result.errors[0].field, "sandbox.enabled"); + assert!(matches!( + result.errors[0].kind, + DiagnosticKind::WrongType { + expected: "a boolean", + got: "a string" + } + )); + } + + #[test] + fn display_format_unknown_key_with_line() { + // given + let diag = ConfigDiagnostic { + path: "/test/settings.json".to_string(), + field: "badKey".to_string(), + line: Some(5), + kind: DiagnosticKind::UnknownKey { suggestion: None }, + }; + + // when + let output = diag.to_string(); + + // then + assert_eq!( + output, + r#"/test/settings.json: unknown key "badKey" (line 5)"# + ); + } + + #[test] + fn display_format_wrong_type_with_line() { + // given + let diag = ConfigDiagnostic { + path: "/test/settings.json".to_string(), + field: "model".to_string(), + line: Some(2), + kind: DiagnosticKind::WrongType { + expected: "a string", + got: "a number", + }, + }; + + // when + let output = diag.to_string(); + + // then + assert_eq!( + output, + r#"/test/settings.json: field "model" must be a string, got a number (line 2)"# + ); + } + + #[test] + fn display_format_deprecated_with_line() { + // given + let diag = ConfigDiagnostic { + path: "/test/settings.json".to_string(), + field: "permissionMode".to_string(), + line: Some(3), + kind: DiagnosticKind::Deprecated { + replacement: "permissions.defaultMode", + }, + }; + + // when + let output = diag.to_string(); + + // then + assert_eq!( + output, + r#"/test/settings.json: field "permissionMode" is deprecated (line 3). Use "permissions.defaultMode" instead"# + ); + } +} diff --git a/rust/crates/runtime/src/conversation.rs b/rust/crates/runtime/src/conversation.rs index 1010bd3d..99cce827 100644 --- a/rust/crates/runtime/src/conversation.rs +++ b/rust/crates/runtime/src/conversation.rs @@ -504,6 +504,10 @@ where &self.session } + pub fn session_mut(&mut self) -> &mut Session { + &mut self.session + } + #[must_use] pub fn fork_session(&self, branch_name: Option) -> Session { self.session.fork(branch_name) @@ -890,6 +894,7 @@ mod tests { current_date: "2026-03-31".to_string(), git_status: None, git_diff: None, + git_context: None, instruction_files: Vec::new(), }) .with_os("linux", "6.8") diff --git a/rust/crates/runtime/src/file_ops.rs b/rust/crates/runtime/src/file_ops.rs index fde889b7..546897b3 100644 --- a/rust/crates/runtime/src/file_ops.rs +++ b/rust/crates/runtime/src/file_ops.rs @@ -28,6 +28,7 @@ fn is_binary_file(path: &Path) -> io::Result { /// Validate that a resolved path stays within the given workspace root. /// Returns the canonical path on success, or an error if the path escapes /// the workspace boundary (e.g. via `../` traversal or symlink). +#[allow(dead_code)] fn validate_workspace_boundary(resolved: &Path, workspace_root: &Path) -> io::Result<()> { if !resolved.starts_with(workspace_root) { return Err(io::Error::new( @@ -557,6 +558,7 @@ fn normalize_path_allow_missing(path: &str) -> io::Result { } /// Read a file with workspace boundary enforcement. +#[allow(dead_code)] pub fn read_file_in_workspace( path: &str, offset: Option, @@ -572,6 +574,7 @@ pub fn read_file_in_workspace( } /// Write a file with workspace boundary enforcement. +#[allow(dead_code)] pub fn write_file_in_workspace( path: &str, content: &str, @@ -586,6 +589,7 @@ pub fn write_file_in_workspace( } /// Edit a file with workspace boundary enforcement. +#[allow(dead_code)] pub fn edit_file_in_workspace( path: &str, old_string: &str, @@ -602,6 +606,7 @@ pub fn edit_file_in_workspace( } /// Check whether a path is a symlink that resolves outside the workspace. +#[allow(dead_code)] pub fn is_symlink_escape(path: &Path, workspace_root: &Path) -> io::Result { let metadata = fs::symlink_metadata(path)?; if !metadata.is_symlink() { diff --git a/rust/crates/runtime/src/git_context.rs b/rust/crates/runtime/src/git_context.rs new file mode 100644 index 00000000..5703ebe8 --- /dev/null +++ b/rust/crates/runtime/src/git_context.rs @@ -0,0 +1,324 @@ +use std::path::Path; +use std::process::Command; + +/// A single git commit entry from the log. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GitCommitEntry { + pub hash: String, + pub subject: String, +} + +/// Git-aware context gathered at startup for injection into the system prompt. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GitContext { + pub branch: Option, + pub recent_commits: Vec, + pub staged_files: Vec, +} + +const MAX_RECENT_COMMITS: usize = 5; + +impl GitContext { + /// Detect the git context from the given working directory. + /// + /// Returns `None` when the directory is not inside a git repository. + #[must_use] + pub fn detect(cwd: &Path) -> Option { + // Quick gate: is this a git repo at all? + let rev_parse = Command::new("git") + .args(["rev-parse", "--is-inside-work-tree"]) + .current_dir(cwd) + .output() + .ok()?; + if !rev_parse.status.success() { + return None; + } + + Some(Self { + branch: read_branch(cwd), + recent_commits: read_recent_commits(cwd), + staged_files: read_staged_files(cwd), + }) + } + + /// Render a human-readable summary suitable for system-prompt injection. + #[must_use] + pub fn render(&self) -> String { + let mut lines = Vec::new(); + + if let Some(branch) = &self.branch { + lines.push(format!("Git branch: {branch}")); + } + + if !self.recent_commits.is_empty() { + lines.push(String::new()); + lines.push("Recent commits:".to_string()); + for entry in &self.recent_commits { + lines.push(format!(" {} {}", entry.hash, entry.subject)); + } + } + + if !self.staged_files.is_empty() { + lines.push(String::new()); + lines.push("Staged files:".to_string()); + for file in &self.staged_files { + lines.push(format!(" {file}")); + } + } + + lines.join("\n") + } +} + +fn read_branch(cwd: &Path) -> Option { + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .current_dir(cwd) + .output() + .ok()?; + if !output.status.success() { + return None; + } + let branch = String::from_utf8(output.stdout).ok()?; + let trimmed = branch.trim(); + if trimmed.is_empty() || trimmed == "HEAD" { + None + } else { + Some(trimmed.to_string()) + } +} + +fn read_recent_commits(cwd: &Path) -> Vec { + let output = Command::new("git") + .args([ + "--no-optional-locks", + "log", + "--oneline", + "-n", + &MAX_RECENT_COMMITS.to_string(), + "--no-decorate", + ]) + .current_dir(cwd) + .output() + .ok(); + let Some(output) = output else { + return Vec::new(); + }; + if !output.status.success() { + return Vec::new(); + } + let stdout = String::from_utf8(output.stdout).unwrap_or_default(); + stdout + .lines() + .filter_map(|line| { + let line = line.trim(); + if line.is_empty() { + return None; + } + let (hash, subject) = line.split_once(' ')?; + Some(GitCommitEntry { + hash: hash.to_string(), + subject: subject.to_string(), + }) + }) + .collect() +} + +fn read_staged_files(cwd: &Path) -> Vec { + let output = Command::new("git") + .args(["--no-optional-locks", "diff", "--cached", "--name-only"]) + .current_dir(cwd) + .output() + .ok(); + let Some(output) = output else { + return Vec::new(); + }; + if !output.status.success() { + return Vec::new(); + } + let stdout = String::from_utf8(output.stdout).unwrap_or_default(); + stdout + .lines() + .filter(|line| !line.trim().is_empty()) + .map(|line| line.trim().to_string()) + .collect() +} + +#[cfg(test)] +mod tests { + use super::{GitCommitEntry, GitContext}; + use std::fs; + use std::process::Command; + use std::time::{SystemTime, UNIX_EPOCH}; + + fn temp_dir(label: &str) -> std::path::PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time should be after epoch") + .as_nanos(); + std::env::temp_dir().join(format!("runtime-git-context-{label}-{nanos}")) + } + + fn env_lock() -> std::sync::MutexGuard<'static, ()> { + crate::test_env_lock() + } + + fn ensure_valid_cwd() { + if std::env::current_dir().is_err() { + std::env::set_current_dir(env!("CARGO_MANIFEST_DIR")) + .expect("test cwd should be recoverable"); + } + } + + #[test] + fn returns_none_for_non_git_directory() { + // given + let _guard = env_lock(); + ensure_valid_cwd(); + let root = temp_dir("non-git"); + fs::create_dir_all(&root).expect("create dir"); + + // when + let context = GitContext::detect(&root); + + // then + assert!(context.is_none()); + fs::remove_dir_all(root).expect("cleanup"); + } + + #[test] + fn detects_branch_name_and_commits() { + // given + let _guard = env_lock(); + ensure_valid_cwd(); + let root = temp_dir("branch-commits"); + fs::create_dir_all(&root).expect("create dir"); + git(&root, &["init", "--quiet", "--initial-branch=main"]); + git(&root, &["config", "user.email", "tests@example.com"]); + git(&root, &["config", "user.name", "Git Context Tests"]); + fs::write(root.join("a.txt"), "a\n").expect("write a"); + git(&root, &["add", "a.txt"]); + git(&root, &["commit", "-m", "first commit", "--quiet"]); + fs::write(root.join("b.txt"), "b\n").expect("write b"); + git(&root, &["add", "b.txt"]); + git(&root, &["commit", "-m", "second commit", "--quiet"]); + + // when + let context = GitContext::detect(&root).expect("should detect git repo"); + + // then + assert_eq!(context.branch.as_deref(), Some("main")); + assert_eq!(context.recent_commits.len(), 2); + assert_eq!(context.recent_commits[0].subject, "second commit"); + assert_eq!(context.recent_commits[1].subject, "first commit"); + assert!(context.staged_files.is_empty()); + fs::remove_dir_all(root).expect("cleanup"); + } + + #[test] + fn detects_staged_files() { + // given + let _guard = env_lock(); + ensure_valid_cwd(); + let root = temp_dir("staged"); + fs::create_dir_all(&root).expect("create dir"); + git(&root, &["init", "--quiet", "--initial-branch=main"]); + git(&root, &["config", "user.email", "tests@example.com"]); + git(&root, &["config", "user.name", "Git Context Tests"]); + fs::write(root.join("init.txt"), "init\n").expect("write init"); + git(&root, &["add", "init.txt"]); + git(&root, &["commit", "-m", "initial", "--quiet"]); + fs::write(root.join("staged.txt"), "staged\n").expect("write staged"); + git(&root, &["add", "staged.txt"]); + + // when + let context = GitContext::detect(&root).expect("should detect git repo"); + + // then + assert_eq!(context.staged_files, vec!["staged.txt"]); + fs::remove_dir_all(root).expect("cleanup"); + } + + #[test] + fn render_formats_all_sections() { + // given + let context = GitContext { + branch: Some("feat/test".to_string()), + recent_commits: vec![ + GitCommitEntry { + hash: "abc1234".to_string(), + subject: "add feature".to_string(), + }, + GitCommitEntry { + hash: "def5678".to_string(), + subject: "fix bug".to_string(), + }, + ], + staged_files: vec!["src/main.rs".to_string()], + }; + + // when + let rendered = context.render(); + + // then + assert!(rendered.contains("Git branch: feat/test")); + assert!(rendered.contains("abc1234 add feature")); + assert!(rendered.contains("def5678 fix bug")); + assert!(rendered.contains("src/main.rs")); + } + + #[test] + fn render_omits_empty_sections() { + // given + let context = GitContext { + branch: Some("main".to_string()), + recent_commits: Vec::new(), + staged_files: Vec::new(), + }; + + // when + let rendered = context.render(); + + // then + assert!(rendered.contains("Git branch: main")); + assert!(!rendered.contains("Recent commits:")); + assert!(!rendered.contains("Staged files:")); + } + + #[test] + fn limits_to_five_recent_commits() { + // given + let _guard = env_lock(); + ensure_valid_cwd(); + let root = temp_dir("five-commits"); + fs::create_dir_all(&root).expect("create dir"); + git(&root, &["init", "--quiet", "--initial-branch=main"]); + git(&root, &["config", "user.email", "tests@example.com"]); + git(&root, &["config", "user.name", "Git Context Tests"]); + for i in 1..=8 { + let name = format!("file{i}.txt"); + fs::write(root.join(&name), format!("{i}\n")).expect("write file"); + git(&root, &["add", &name]); + git(&root, &["commit", "-m", &format!("commit {i}"), "--quiet"]); + } + + // when + let context = GitContext::detect(&root).expect("should detect git repo"); + + // then + assert_eq!(context.recent_commits.len(), 5); + assert_eq!(context.recent_commits[0].subject, "commit 8"); + assert_eq!(context.recent_commits[4].subject, "commit 4"); + fs::remove_dir_all(root).expect("cleanup"); + } + + fn git(cwd: &std::path::Path, args: &[&str]) { + let status = Command::new("git") + .args(args) + .current_dir(cwd) + .output() + .unwrap_or_else(|_| panic!("git {args:?} should run")) + .status; + assert!(status.success(), "git {args:?} failed"); + } +} diff --git a/rust/crates/runtime/src/lane_events.rs b/rust/crates/runtime/src/lane_events.rs index c96f829e..96a9ac8a 100644 --- a/rust/crates/runtime/src/lane_events.rs +++ b/rust/crates/runtime/src/lane_events.rs @@ -1,3 +1,4 @@ +#![allow(clippy::similar_names)] use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -76,6 +77,20 @@ pub struct LaneEventBlocker { pub detail: String, } +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct LaneCommitProvenance { + pub commit: String, + pub branch: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub worktree: Option, + #[serde(rename = "canonicalCommit", skip_serializing_if = "Option::is_none")] + pub canonical_commit: Option, + #[serde(rename = "supersededBy", skip_serializing_if = "Option::is_none")] + pub superseded_by: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub lineage: Vec, +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct LaneEvent { pub event: LaneEventName, @@ -114,8 +129,42 @@ impl LaneEvent { #[must_use] pub fn finished(emitted_at: impl Into, detail: Option) -> Self { - Self::new(LaneEventName::Finished, LaneEventStatus::Completed, emitted_at) - .with_optional_detail(detail) + Self::new( + LaneEventName::Finished, + LaneEventStatus::Completed, + emitted_at, + ) + .with_optional_detail(detail) + } + + #[must_use] + pub fn commit_created( + emitted_at: impl Into, + detail: Option, + provenance: LaneCommitProvenance, + ) -> Self { + Self::new( + LaneEventName::CommitCreated, + LaneEventStatus::Completed, + emitted_at, + ) + .with_optional_detail(detail) + .with_data(serde_json::to_value(provenance).expect("commit provenance should serialize")) + } + + #[must_use] + pub fn superseded( + emitted_at: impl Into, + detail: Option, + provenance: LaneCommitProvenance, + ) -> Self { + Self::new( + LaneEventName::Superseded, + LaneEventStatus::Superseded, + emitted_at, + ) + .with_optional_detail(detail) + .with_data(serde_json::to_value(provenance).expect("commit provenance should serialize")) } #[must_use] @@ -157,12 +206,53 @@ impl LaneEvent { } } +#[must_use] +pub fn dedupe_superseded_commit_events(events: &[LaneEvent]) -> Vec { + let mut keep = vec![true; events.len()]; + let mut latest_by_key = std::collections::BTreeMap::::new(); + + for (index, event) in events.iter().enumerate() { + if event.event != LaneEventName::CommitCreated { + continue; + } + let Some(data) = event.data.as_ref() else { + continue; + }; + let key = data + .get("canonicalCommit") + .or_else(|| data.get("commit")) + .and_then(serde_json::Value::as_str) + .map(str::to_string); + let superseded = data + .get("supersededBy") + .and_then(serde_json::Value::as_str) + .is_some(); + if superseded { + keep[index] = false; + continue; + } + if let Some(key) = key { + if let Some(previous) = latest_by_key.insert(key, index) { + keep[previous] = false; + } + } + } + + events + .iter() + .cloned() + .zip(keep) + .filter_map(|(event, retain)| retain.then_some(event)) + .collect() +} + #[cfg(test)] mod tests { use serde_json::json; use super::{ - LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass, + dedupe_superseded_commit_events, LaneCommitProvenance, LaneEvent, LaneEventBlocker, + LaneEventName, LaneEventStatus, LaneFailureClass, }; #[test] @@ -170,10 +260,7 @@ mod tests { let cases = [ (LaneEventName::Started, "lane.started"), (LaneEventName::Ready, "lane.ready"), - ( - LaneEventName::PromptMisdelivery, - "lane.prompt_misdelivery", - ), + (LaneEventName::PromptMisdelivery, "lane.prompt_misdelivery"), (LaneEventName::Blocked, "lane.blocked"), (LaneEventName::Red, "lane.red"), (LaneEventName::Green, "lane.green"), @@ -193,7 +280,10 @@ mod tests { ]; for (event, expected) in cases { - assert_eq!(serde_json::to_value(event).expect("serialize event"), json!(expected)); + assert_eq!( + serde_json::to_value(event).expect("serialize event"), + json!(expected) + ); } } @@ -238,4 +328,56 @@ mod tests { assert_eq!(failed.status, LaneEventStatus::Failed); assert_eq!(failed.detail.as_deref(), Some("broken server")); } + + #[test] + fn commit_events_can_carry_worktree_and_supersession_metadata() { + let event = LaneEvent::commit_created( + "2026-04-04T00:00:00Z", + Some("commit created".to_string()), + LaneCommitProvenance { + commit: "abc123".to_string(), + branch: "feature/provenance".to_string(), + worktree: Some("wt-a".to_string()), + canonical_commit: Some("abc123".to_string()), + superseded_by: None, + lineage: vec!["abc123".to_string()], + }, + ); + let event_json = serde_json::to_value(&event).expect("lane event should serialize"); + assert_eq!(event_json["event"], "lane.commit.created"); + assert_eq!(event_json["data"]["branch"], "feature/provenance"); + assert_eq!(event_json["data"]["worktree"], "wt-a"); + } + + #[test] + fn dedupes_superseded_commit_events_by_canonical_commit() { + let retained = dedupe_superseded_commit_events(&[ + LaneEvent::commit_created( + "2026-04-04T00:00:00Z", + Some("old".to_string()), + LaneCommitProvenance { + commit: "old123".to_string(), + branch: "feature/provenance".to_string(), + worktree: Some("wt-a".to_string()), + canonical_commit: Some("canon123".to_string()), + superseded_by: Some("new123".to_string()), + lineage: vec!["old123".to_string(), "new123".to_string()], + }, + ), + LaneEvent::commit_created( + "2026-04-04T00:00:01Z", + Some("new".to_string()), + LaneCommitProvenance { + commit: "new123".to_string(), + branch: "feature/provenance".to_string(), + worktree: Some("wt-b".to_string()), + canonical_commit: Some("canon123".to_string()), + superseded_by: None, + lineage: vec!["old123".to_string(), "new123".to_string()], + }, + ), + ]); + assert_eq!(retained.len(), 1); + assert_eq!(retained[0].detail.as_deref(), Some("new")); + } } diff --git a/rust/crates/runtime/src/lib.rs b/rust/crates/runtime/src/lib.rs index 7550c493..e87dfd75 100644 --- a/rust/crates/runtime/src/lib.rs +++ b/rust/crates/runtime/src/lib.rs @@ -7,10 +7,13 @@ mod bash; pub mod bash_validation; mod bootstrap; +pub mod branch_lock; mod compact; mod config; +pub mod config_validate; mod conversation; mod file_ops; +mod git_context; pub mod green_contract; mod hooks; mod json; @@ -19,6 +22,7 @@ pub mod lsp_client; mod mcp; mod mcp_client; pub mod mcp_lifecycle_hardened; +pub mod mcp_server; mod mcp_stdio; pub mod mcp_tool_bridge; mod oauth; @@ -32,28 +36,36 @@ mod remote; pub mod sandbox; mod session; pub mod session_control; +pub use session_control::SessionStore; mod sse; +pub mod stale_base; pub mod stale_branch; pub mod summary_compression; pub mod task_packet; pub mod task_registry; pub mod team_cron_registry; -pub mod trust_resolver; +#[cfg(test)] +mod trust_resolver; mod usage; pub mod worker_boot; pub use bash::{execute_bash, BashCommandInput, BashCommandOutput}; pub use bootstrap::{BootstrapPhase, BootstrapPlan}; +pub use branch_lock::{detect_branch_lock_collisions, BranchLockCollision, BranchLockIntent}; pub use compact::{ compact_session, estimate_session_tokens, format_compact_summary, get_compact_continuation_message, should_compact, CompactionConfig, CompactionResult, }; +pub use config_validate::{ + check_unsupported_format, format_diagnostics, validate_config_file, ConfigDiagnostic, + DiagnosticKind, ValidationResult, +}; pub use config::{ ConfigEntry, ConfigError, ConfigLoader, ConfigSource, McpConfigCollection, McpManagedProxyServerConfig, McpOAuthConfig, McpRemoteServerConfig, McpSdkServerConfig, McpServerConfig, McpStdioServerConfig, McpTransport, McpWebSocketServerConfig, OAuthConfig, - ResolvedPermissionMode, RuntimeConfig, RuntimeFeatureConfig, RuntimeHookConfig, - RuntimePermissionRuleConfig, RuntimePluginConfig, ScopedMcpServerConfig, + ProviderFallbackConfig, ResolvedPermissionMode, RuntimeConfig, RuntimeFeatureConfig, + RuntimeHookConfig, RuntimePermissionRuleConfig, RuntimePluginConfig, ScopedMcpServerConfig, CLAW_SETTINGS_SCHEMA_NAME, }; pub use conversation::{ @@ -61,6 +73,7 @@ pub use conversation::{ ConversationRuntime, PromptCacheEvent, RuntimeError, StaticToolExecutor, ToolError, ToolExecutor, TurnSummary, }; +pub use git_context::{GitCommitEntry, GitContext}; pub use file_ops::{ edit_file, glob_search, grep_search, read_file, write_file, EditFileOutput, GlobSearchOutput, GrepSearchInput, GrepSearchOutput, ReadFileOutput, StructuredPatchHunk, TextFilePayload, @@ -70,7 +83,8 @@ pub use hooks::{ HookAbortSignal, HookEvent, HookProgressEvent, HookProgressReporter, HookRunResult, HookRunner, }; pub use lane_events::{ - LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass, + dedupe_superseded_commit_events, LaneCommitProvenance, LaneEvent, LaneEventBlocker, + LaneEventName, LaneEventStatus, LaneFailureClass, }; pub use mcp::{ mcp_server_signature, mcp_tool_name, mcp_tool_prefix, normalize_name_for_mcp, @@ -84,6 +98,7 @@ pub use mcp_lifecycle_hardened::{ McpDegradedReport, McpErrorSurface, McpFailedServer, McpLifecyclePhase, McpLifecycleState, McpLifecycleValidator, McpPhaseResult, }; +pub use mcp_server::{McpServer, McpServerSpec, ToolCallHandler, MCP_SERVER_PROTOCOL_VERSION}; pub use mcp_stdio::{ spawn_mcp_stdio_process, JsonRpcError, JsonRpcId, JsonRpcRequest, JsonRpcResponse, ManagedMcpTool, McpDiscoveryFailure, McpInitializeClientInfo, McpInitializeParams, @@ -133,14 +148,19 @@ pub use sandbox::{ }; pub use session::{ ContentBlock, ConversationMessage, MessageRole, Session, SessionCompaction, SessionError, - SessionFork, + SessionFork, SessionPromptEntry, }; pub use sse::{IncrementalSseParser, SseEvent}; +pub use stale_base::{ + check_base_commit, format_stale_base_warning, read_claw_base_file, resolve_expected_base, + BaseCommitSource, BaseCommitState, +}; pub use stale_branch::{ apply_policy, check_freshness, BranchFreshness, StaleBranchAction, StaleBranchEvent, StaleBranchPolicy, }; pub use task_packet::{validate_packet, TaskPacket, TaskPacketValidationError, ValidatedPacket}; +#[cfg(test)] pub use trust_resolver::{TrustConfig, TrustDecision, TrustEvent, TrustPolicy, TrustResolver}; pub use usage::{ format_usd, pricing_for_model, ModelPricing, TokenUsage, UsageCostEstimate, UsageTracker, diff --git a/rust/crates/runtime/src/lsp_client.rs b/rust/crates/runtime/src/lsp_client.rs index 92c0ed1a..63027139 100644 --- a/rust/crates/runtime/src/lsp_client.rs +++ b/rust/crates/runtime/src/lsp_client.rs @@ -1,3 +1,4 @@ +#![allow(clippy::should_implement_trait, clippy::must_use_candidate)] //! LSP (Language Server Protocol) client registry for tool dispatch. use std::collections::HashMap; diff --git a/rust/crates/runtime/src/mcp_lifecycle_hardened.rs b/rust/crates/runtime/src/mcp_lifecycle_hardened.rs index 969eff4f..330ff63c 100644 --- a/rust/crates/runtime/src/mcp_lifecycle_hardened.rs +++ b/rust/crates/runtime/src/mcp_lifecycle_hardened.rs @@ -1,3 +1,4 @@ +#![allow(clippy::unnested_or_patterns, clippy::map_unwrap_or)] use std::collections::{BTreeMap, BTreeSet}; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; @@ -599,7 +600,10 @@ mod tests { )); match result { - McpPhaseResult::Failure { phase: failed_phase, error } => { + McpPhaseResult::Failure { + phase: failed_phase, + error, + } => { assert_eq!(failed_phase, phase); assert_eq!(error.phase, phase); assert_eq!( diff --git a/rust/crates/runtime/src/mcp_server.rs b/rust/crates/runtime/src/mcp_server.rs new file mode 100644 index 00000000..23b9bc3b --- /dev/null +++ b/rust/crates/runtime/src/mcp_server.rs @@ -0,0 +1,442 @@ +//! Minimal Model Context Protocol (MCP) server. +//! +//! Implements a newline-safe, LSP-framed JSON-RPC server over stdio that +//! answers `initialize`, `tools/list`, and `tools/call` requests. The framing +//! matches the client transport implemented in [`crate::mcp_stdio`] so this +//! server can be driven by either an external MCP client (e.g. Claude +//! Desktop) or `claw`'s own [`McpServerManager`](crate::McpServerManager). +//! +//! The server is intentionally small: it exposes a list of pre-built +//! [`McpTool`] descriptors and delegates `tools/call` to a caller-supplied +//! handler. Tool execution itself lives in the `tools` crate; this module is +//! purely the transport + dispatch loop. +//! +//! [`McpTool`]: crate::mcp_stdio::McpTool + +use std::io; + +use serde_json::{json, Value as JsonValue}; +use tokio::io::{ + stdin, stdout, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, Stdin, Stdout, +}; + +use crate::mcp_stdio::{ + JsonRpcError, JsonRpcId, JsonRpcRequest, JsonRpcResponse, McpInitializeResult, + McpInitializeServerInfo, McpListToolsResult, McpTool, McpToolCallContent, McpToolCallParams, + McpToolCallResult, +}; + +/// Protocol version the server advertises during `initialize`. +/// +/// Matches the version used by the built-in client in +/// [`crate::mcp_stdio`], so the two stay in lockstep. +pub const MCP_SERVER_PROTOCOL_VERSION: &str = "2025-03-26"; + +/// Synchronous handler invoked for every `tools/call` request. +/// +/// Returning `Ok(text)` yields a single `text` content block and +/// `isError: false`. Returning `Err(message)` yields a `text` block with the +/// error and `isError: true`, mirroring the error-surfacing convention used +/// elsewhere in claw. +pub type ToolCallHandler = + Box Result + Send + Sync + 'static>; + +/// Configuration for an [`McpServer`] instance. +/// +/// Named `McpServerSpec` rather than `McpServerConfig` to avoid colliding +/// with the existing client-side [`crate::config::McpServerConfig`] that +/// describes *remote* MCP servers the runtime connects to. +pub struct McpServerSpec { + /// Name advertised in the `serverInfo` field of the `initialize` response. + pub server_name: String, + /// Version advertised in the `serverInfo` field of the `initialize` + /// response. + pub server_version: String, + /// Tool descriptors returned for `tools/list`. + pub tools: Vec, + /// Handler invoked for `tools/call`. + pub tool_handler: ToolCallHandler, +} + +/// Minimal MCP stdio server. +/// +/// The server runs a blocking read/dispatch/write loop over the current +/// process's stdin/stdout, terminating cleanly when the peer closes the +/// stream. +pub struct McpServer { + spec: McpServerSpec, + stdin: BufReader, + stdout: Stdout, +} + +impl McpServer { + #[must_use] + pub fn new(spec: McpServerSpec) -> Self { + Self { + spec, + stdin: BufReader::new(stdin()), + stdout: stdout(), + } + } + + /// Runs the server until the client closes stdin. + /// + /// Returns `Ok(())` on clean EOF; any other I/O error is propagated so + /// callers can log and exit non-zero. + pub async fn run(&mut self) -> io::Result<()> { + loop { + let Some(payload) = read_frame(&mut self.stdin).await? else { + return Ok(()); + }; + + // Requests and notifications share a wire format; the absence of + // `id` distinguishes notifications, which must never receive a + // response. + let message: JsonValue = match serde_json::from_slice(&payload) { + Ok(value) => value, + Err(error) => { + // Parse error with null id per JSON-RPC 2.0 §4.2. + let response = JsonRpcResponse:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Null, + result: None, + error: Some(JsonRpcError { + code: -32700, + message: format!("parse error: {error}"), + data: None, + }), + }; + write_response(&mut self.stdout, &response).await?; + continue; + } + }; + + if message.get("id").is_none() { + // Notification: dispatch for side effects only (e.g. log), + // but send no reply. + continue; + } + + let request: JsonRpcRequest = match serde_json::from_value(message) { + Ok(request) => request, + Err(error) => { + let response = JsonRpcResponse:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Null, + result: None, + error: Some(JsonRpcError { + code: -32600, + message: format!("invalid request: {error}"), + data: None, + }), + }; + write_response(&mut self.stdout, &response).await?; + continue; + } + }; + + let response = self.dispatch(request); + write_response(&mut self.stdout, &response).await?; + } + } + + fn dispatch(&self, request: JsonRpcRequest) -> JsonRpcResponse { + let id = request.id.clone(); + match request.method.as_str() { + "initialize" => self.handle_initialize(id), + "tools/list" => self.handle_tools_list(id), + "tools/call" => self.handle_tools_call(id, request.params), + other => JsonRpcResponse { + jsonrpc: "2.0".to_string(), + id, + result: None, + error: Some(JsonRpcError { + code: -32601, + message: format!("method not found: {other}"), + data: None, + }), + }, + } + } + + fn handle_initialize(&self, id: JsonRpcId) -> JsonRpcResponse { + let result = McpInitializeResult { + protocol_version: MCP_SERVER_PROTOCOL_VERSION.to_string(), + capabilities: json!({ "tools": {} }), + server_info: McpInitializeServerInfo { + name: self.spec.server_name.clone(), + version: self.spec.server_version.clone(), + }, + }; + JsonRpcResponse { + jsonrpc: "2.0".to_string(), + id, + result: serde_json::to_value(result).ok(), + error: None, + } + } + + fn handle_tools_list(&self, id: JsonRpcId) -> JsonRpcResponse { + let result = McpListToolsResult { + tools: self.spec.tools.clone(), + next_cursor: None, + }; + JsonRpcResponse { + jsonrpc: "2.0".to_string(), + id, + result: serde_json::to_value(result).ok(), + error: None, + } + } + + fn handle_tools_call( + &self, + id: JsonRpcId, + params: Option, + ) -> JsonRpcResponse { + let Some(params) = params else { + return invalid_params_response(id, "missing params for tools/call"); + }; + let call: McpToolCallParams = match serde_json::from_value(params) { + Ok(value) => value, + Err(error) => { + return invalid_params_response(id, &format!("invalid tools/call params: {error}")); + } + }; + let arguments = call.arguments.unwrap_or_else(|| json!({})); + let tool_result = (self.spec.tool_handler)(&call.name, &arguments); + let (text, is_error) = match tool_result { + Ok(text) => (text, false), + Err(message) => (message, true), + }; + let mut data = std::collections::BTreeMap::new(); + data.insert("text".to_string(), JsonValue::String(text)); + let call_result = McpToolCallResult { + content: vec![McpToolCallContent { + kind: "text".to_string(), + data, + }], + structured_content: None, + is_error: Some(is_error), + meta: None, + }; + JsonRpcResponse { + jsonrpc: "2.0".to_string(), + id, + result: serde_json::to_value(call_result).ok(), + error: None, + } + } +} + +fn invalid_params_response(id: JsonRpcId, message: &str) -> JsonRpcResponse { + JsonRpcResponse { + jsonrpc: "2.0".to_string(), + id, + result: None, + error: Some(JsonRpcError { + code: -32602, + message: message.to_string(), + data: None, + }), + } +} + +/// Reads a single LSP-framed JSON-RPC payload from `reader`. +/// +/// Returns `Ok(None)` on clean EOF before any header bytes have been read, +/// matching how [`crate::mcp_stdio::McpStdioProcess`] treats stream closure. +async fn read_frame(reader: &mut BufReader) -> io::Result>> { + let mut content_length: Option = None; + let mut first_header = true; + loop { + let mut line = String::new(); + let bytes_read = reader.read_line(&mut line).await?; + if bytes_read == 0 { + if first_header { + return Ok(None); + } + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + "MCP stdio stream closed while reading headers", + )); + } + first_header = false; + if line == "\r\n" || line == "\n" { + break; + } + let header = line.trim_end_matches(['\r', '\n']); + if let Some((name, value)) = header.split_once(':') { + if name.trim().eq_ignore_ascii_case("Content-Length") { + let parsed = value + .trim() + .parse::() + .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?; + content_length = Some(parsed); + } + } + } + + let content_length = content_length.ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidData, "missing Content-Length header") + })?; + let mut payload = vec![0_u8; content_length]; + reader.read_exact(&mut payload).await?; + Ok(Some(payload)) +} + +async fn write_response( + stdout: &mut Stdout, + response: &JsonRpcResponse, +) -> io::Result<()> { + let body = serde_json::to_vec(response) + .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?; + let header = format!("Content-Length: {}\r\n\r\n", body.len()); + stdout.write_all(header.as_bytes()).await?; + stdout.write_all(&body).await?; + stdout.flush().await +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dispatch_initialize_returns_server_info() { + let server = McpServer { + spec: McpServerSpec { + server_name: "test".to_string(), + server_version: "9.9.9".to_string(), + tools: Vec::new(), + tool_handler: Box::new(|_, _| Ok(String::new())), + }, + stdin: BufReader::new(stdin()), + stdout: stdout(), + }; + let request = JsonRpcRequest:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Number(1), + method: "initialize".to_string(), + params: None, + }; + let response = server.dispatch(request); + assert_eq!(response.id, JsonRpcId::Number(1)); + assert!(response.error.is_none()); + let result = response.result.expect("initialize result"); + assert_eq!(result["protocolVersion"], MCP_SERVER_PROTOCOL_VERSION); + assert_eq!(result["serverInfo"]["name"], "test"); + assert_eq!(result["serverInfo"]["version"], "9.9.9"); + } + + #[test] + fn dispatch_tools_list_returns_registered_tools() { + let tool = McpTool { + name: "echo".to_string(), + description: Some("Echo".to_string()), + input_schema: Some(json!({"type": "object"})), + annotations: None, + meta: None, + }; + let server = McpServer { + spec: McpServerSpec { + server_name: "test".to_string(), + server_version: "0.0.0".to_string(), + tools: vec![tool.clone()], + tool_handler: Box::new(|_, _| Ok(String::new())), + }, + stdin: BufReader::new(stdin()), + stdout: stdout(), + }; + let request = JsonRpcRequest:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Number(2), + method: "tools/list".to_string(), + params: None, + }; + let response = server.dispatch(request); + assert!(response.error.is_none()); + let result = response.result.expect("tools/list result"); + assert_eq!(result["tools"][0]["name"], "echo"); + } + + #[test] + fn dispatch_tools_call_wraps_handler_output() { + let server = McpServer { + spec: McpServerSpec { + server_name: "test".to_string(), + server_version: "0.0.0".to_string(), + tools: Vec::new(), + tool_handler: Box::new(|name, args| { + Ok(format!("called {name} with {args}")) + }), + }, + stdin: BufReader::new(stdin()), + stdout: stdout(), + }; + let request = JsonRpcRequest:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Number(3), + method: "tools/call".to_string(), + params: Some(json!({ + "name": "echo", + "arguments": {"text": "hi"} + })), + }; + let response = server.dispatch(request); + assert!(response.error.is_none()); + let result = response.result.expect("tools/call result"); + assert_eq!(result["isError"], false); + assert_eq!(result["content"][0]["type"], "text"); + assert!(result["content"][0]["text"] + .as_str() + .unwrap() + .starts_with("called echo")); + } + + #[test] + fn dispatch_tools_call_surfaces_handler_error() { + let server = McpServer { + spec: McpServerSpec { + server_name: "test".to_string(), + server_version: "0.0.0".to_string(), + tools: Vec::new(), + tool_handler: Box::new(|_, _| Err("boom".to_string())), + }, + stdin: BufReader::new(stdin()), + stdout: stdout(), + }; + let request = JsonRpcRequest:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Number(4), + method: "tools/call".to_string(), + params: Some(json!({"name": "broken"})), + }; + let response = server.dispatch(request); + let result = response.result.expect("tools/call result"); + assert_eq!(result["isError"], true); + assert_eq!(result["content"][0]["text"], "boom"); + } + + #[test] + fn dispatch_unknown_method_returns_method_not_found() { + let server = McpServer { + spec: McpServerSpec { + server_name: "test".to_string(), + server_version: "0.0.0".to_string(), + tools: Vec::new(), + tool_handler: Box::new(|_, _| Ok(String::new())), + }, + stdin: BufReader::new(stdin()), + stdout: stdout(), + }; + let request = JsonRpcRequest:: { + jsonrpc: "2.0".to_string(), + id: JsonRpcId::Number(5), + method: "nonsense".to_string(), + params: None, + }; + let response = server.dispatch(request); + let error = response.error.expect("error payload"); + assert_eq!(error.code, -32601); + } +} diff --git a/rust/crates/runtime/src/mcp_stdio.rs b/rust/crates/runtime/src/mcp_stdio.rs index 4ebdf620..5fbc31ba 100644 --- a/rust/crates/runtime/src/mcp_stdio.rs +++ b/rust/crates/runtime/src/mcp_stdio.rs @@ -360,8 +360,10 @@ impl McpServerManagerError { } fn recoverable(&self) -> bool { - !matches!(self.lifecycle_phase(), McpLifecyclePhase::InitializeHandshake) - && matches!(self, Self::Transport { .. } | Self::Timeout { .. }) + !matches!( + self.lifecycle_phase(), + McpLifecyclePhase::InitializeHandshake + ) && matches!(self, Self::Transport { .. } | Self::Timeout { .. }) } fn discovery_failure(&self, server_name: &str) -> McpDiscoveryFailure { @@ -417,10 +419,9 @@ impl McpServerManagerError { ("method".to_string(), (*method).to_string()), ("timeout_ms".to_string(), timeout_ms.to_string()), ]), - Self::UnknownTool { qualified_name } => BTreeMap::from([( - "qualified_tool".to_string(), - qualified_name.clone(), - )]), + Self::UnknownTool { qualified_name } => { + BTreeMap::from([("qualified_tool".to_string(), qualified_name.clone())]) + } Self::UnknownServer { server_name } => { BTreeMap::from([("server".to_string(), server_name.clone())]) } @@ -1425,11 +1426,10 @@ mod tests { use crate::mcp_client::McpClientBootstrap; use super::{ - spawn_mcp_stdio_process, JsonRpcId, JsonRpcRequest, JsonRpcResponse, - McpInitializeClientInfo, McpInitializeParams, McpInitializeResult, McpInitializeServerInfo, - McpListToolsResult, McpReadResourceParams, McpReadResourceResult, McpServerManager, - McpServerManagerError, McpStdioProcess, McpTool, McpToolCallParams, - unsupported_server_failed_server, + spawn_mcp_stdio_process, unsupported_server_failed_server, JsonRpcId, JsonRpcRequest, + JsonRpcResponse, McpInitializeClientInfo, McpInitializeParams, McpInitializeResult, + McpInitializeServerInfo, McpListToolsResult, McpReadResourceParams, McpReadResourceResult, + McpServerManager, McpServerManagerError, McpStdioProcess, McpTool, McpToolCallParams, }; use crate::McpLifecyclePhase; @@ -2652,8 +2652,37 @@ mod tests { }); } + fn write_initialize_disconnect_script() -> PathBuf { + let root = temp_dir(); + fs::create_dir_all(&root).expect("temp dir"); + let script_path = root.join("initialize-disconnect.py"); + let script = [ + "#!/usr/bin/env python3", + "import sys", + "header = b''", + r"while not header.endswith(b'\r\n\r\n'):", + " chunk = sys.stdin.buffer.read(1)", + " if not chunk:", + " raise SystemExit(1)", + " header += chunk", + "length = 0", + r"for line in header.decode().split('\r\n'):", + r" if line.lower().startswith('content-length:'):", + r" length = int(line.split(':', 1)[1].strip())", + "if length:", + " sys.stdin.buffer.read(length)", + "raise SystemExit(0)", + "", + ] + .join("\n"); + fs::write(&script_path, script).expect("write script"); + let mut permissions = fs::metadata(&script_path).expect("metadata").permissions(); + permissions.set_mode(0o755); + fs::set_permissions(&script_path, permissions).expect("chmod"); + script_path + } + #[test] - #[ignore = "flaky: intermittent timing issues in CI, see ROADMAP P2.15"] fn manager_discovery_report_keeps_healthy_servers_when_one_server_fails() { let runtime = Builder::new_current_thread() .enable_all() @@ -2663,6 +2692,7 @@ mod tests { let script_path = write_manager_mcp_server_script(); let root = script_path.parent().expect("script parent"); let alpha_log = root.join("alpha.log"); + let broken_script_path = write_initialize_disconnect_script(); let servers = BTreeMap::from([ ( "alpha".to_string(), @@ -2673,8 +2703,8 @@ mod tests { ScopedMcpServerConfig { scope: ConfigSource::Local, config: McpServerConfig::Stdio(McpStdioServerConfig { - command: "python3".to_string(), - args: vec!["-c".to_string(), "import sys; sys.exit(0)".to_string()], + command: broken_script_path.display().to_string(), + args: Vec::new(), env: BTreeMap::new(), tool_call_timeout_ms: None, }), @@ -2698,7 +2728,10 @@ mod tests { ); assert!(!report.failed_servers[0].recoverable); assert_eq!( - report.failed_servers[0].context.get("method").map(String::as_str), + report.failed_servers[0] + .context + .get("method") + .map(String::as_str), Some("initialize") ); assert!(report.failed_servers[0].error.contains("initialize")); @@ -2734,6 +2767,7 @@ mod tests { manager.shutdown().await.expect("shutdown"); cleanup_script(&script_path); + cleanup_script(&broken_script_path); }); } diff --git a/rust/crates/runtime/src/mcp_tool_bridge.rs b/rust/crates/runtime/src/mcp_tool_bridge.rs index fa7cbb66..af637a98 100644 --- a/rust/crates/runtime/src/mcp_tool_bridge.rs +++ b/rust/crates/runtime/src/mcp_tool_bridge.rs @@ -1,3 +1,11 @@ +#![allow( + clippy::await_holding_lock, + clippy::doc_markdown, + clippy::match_same_arms, + clippy::must_use_candidate, + clippy::uninlined_format_args, + clippy::unnested_or_patterns +)] //! Bridge between MCP tool surface (ListMcpResources, ReadMcpResource, McpAuth, MCP) //! and the existing McpServerManager runtime. //! diff --git a/rust/crates/runtime/src/permission_enforcer.rs b/rust/crates/runtime/src/permission_enforcer.rs index 31647427..3a45dc82 100644 --- a/rust/crates/runtime/src/permission_enforcer.rs +++ b/rust/crates/runtime/src/permission_enforcer.rs @@ -1,3 +1,8 @@ +#![allow( + clippy::match_wildcard_for_single_variants, + clippy::must_use_candidate, + clippy::uninlined_format_args +)] //! Permission enforcement layer that gates tool execution based on the //! active `PermissionPolicy`. diff --git a/rust/crates/runtime/src/plugin_lifecycle.rs b/rust/crates/runtime/src/plugin_lifecycle.rs index b67837a9..bd123219 100644 --- a/rust/crates/runtime/src/plugin_lifecycle.rs +++ b/rust/crates/runtime/src/plugin_lifecycle.rs @@ -1,3 +1,4 @@ +#![allow(clippy::redundant_closure_for_method_calls)] use std::time::{SystemTime, UNIX_EPOCH}; use serde::{Deserialize, Serialize}; diff --git a/rust/crates/runtime/src/prompt.rs b/rust/crates/runtime/src/prompt.rs index 307a64b7..060ca441 100644 --- a/rust/crates/runtime/src/prompt.rs +++ b/rust/crates/runtime/src/prompt.rs @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf}; use std::process::Command; use crate::config::{ConfigError, ConfigLoader, RuntimeConfig}; +use crate::git_context::GitContext; /// Errors raised while assembling the final system prompt. #[derive(Debug)] @@ -56,6 +57,7 @@ pub struct ProjectContext { pub current_date: String, pub git_status: Option, pub git_diff: Option, + pub git_context: Option, pub instruction_files: Vec, } @@ -71,6 +73,7 @@ impl ProjectContext { current_date: current_date.into(), git_status: None, git_diff: None, + git_context: None, instruction_files, }) } @@ -82,6 +85,7 @@ impl ProjectContext { let mut context = Self::discover(cwd, current_date)?; context.git_status = read_git_status(&context.cwd); context.git_diff = read_git_diff(&context.cwd); + context.git_context = GitContext::detect(&context.cwd); Ok(context) } } @@ -249,6 +253,31 @@ fn read_git_status(cwd: &Path) -> Option { } } +fn read_git_recent_commits(cwd: &Path) -> Option { + let output = Command::new("git") + .args([ + "--no-optional-locks", + "log", + "--oneline", + "--no-decorate", + "-n", + "5", + ]) + .current_dir(cwd) + .output() + .ok()?; + if !output.status.success() { + return None; + } + let stdout = String::from_utf8(output.stdout).ok()?; + let trimmed = stdout.trim(); + if trimmed.is_empty() { + None + } else { + Some(trimmed.to_string()) + } +} + fn read_git_diff(cwd: &Path) -> Option { let mut sections = Vec::new(); @@ -299,11 +328,27 @@ fn render_project_context(project_context: &ProjectContext) -> String { lines.push("Git status snapshot:".to_string()); lines.push(status.clone()); } + if let Some(ref gc) = project_context.git_context { + if !gc.recent_commits.is_empty() { + lines.push(String::new()); + lines.push("Recent commits (last 5):".to_string()); + for c in &gc.recent_commits { + lines.push(format!(" {} {}", c.hash, c.subject)); + } + } + } if let Some(diff) = &project_context.git_diff { lines.push(String::new()); lines.push("Git diff snapshot:".to_string()); lines.push(diff.clone()); } + if let Some(git_context) = &project_context.git_context { + let rendered = git_context.render(); + if !rendered.is_empty() { + lines.push(String::new()); + lines.push(rendered); + } + } lines.join("\n") } @@ -639,6 +684,80 @@ mod tests { fs::remove_dir_all(root).expect("cleanup temp dir"); } + #[test] + fn discover_with_git_includes_recent_commits_and_renders_them() { + // given: a git repo with three commits and a current branch + let _guard = env_lock(); + ensure_valid_cwd(); + let root = temp_dir(); + fs::create_dir_all(&root).expect("root dir"); + std::process::Command::new("git") + .args(["init", "--quiet", "-b", "main"]) + .current_dir(&root) + .status() + .expect("git init should run"); + std::process::Command::new("git") + .args(["config", "user.email", "tests@example.com"]) + .current_dir(&root) + .status() + .expect("git config email should run"); + std::process::Command::new("git") + .args(["config", "user.name", "Runtime Prompt Tests"]) + .current_dir(&root) + .status() + .expect("git config name should run"); + for (file, message) in [ + ("a.txt", "first commit"), + ("b.txt", "second commit"), + ("c.txt", "third commit"), + ] { + fs::write(root.join(file), "x\n").expect("write commit file"); + std::process::Command::new("git") + .args(["add", file]) + .current_dir(&root) + .status() + .expect("git add should run"); + std::process::Command::new("git") + .args(["commit", "-m", message, "--quiet"]) + .current_dir(&root) + .status() + .expect("git commit should run"); + } + fs::write(root.join("d.txt"), "staged\n").expect("write staged file"); + std::process::Command::new("git") + .args(["add", "d.txt"]) + .current_dir(&root) + .status() + .expect("git add staged should run"); + + // when: discovering project context with git auto-include + let context = + ProjectContext::discover_with_git(&root, "2026-03-31").expect("context should load"); + let rendered = SystemPromptBuilder::new() + .with_os("linux", "6.8") + .with_project_context(context.clone()) + .render(); + + // then: branch, recent commits and staged files are present in context + let gc = context.git_context.as_ref().expect("git context should be present"); + let commits: String = gc.recent_commits.iter().map(|c| c.subject.clone()).collect::>().join("\n"); + assert!(commits.contains("first commit")); + assert!(commits.contains("second commit")); + assert!(commits.contains("third commit")); + assert_eq!(gc.recent_commits.len(), 3); + + let status = context.git_status.as_deref().expect("status snapshot"); + assert!(status.contains("## main")); + assert!(status.contains("A d.txt")); + + assert!(rendered.contains("Recent commits (last 5):")); + assert!(rendered.contains("first commit")); + assert!(rendered.contains("Git status snapshot:")); + assert!(rendered.contains("## main")); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + #[test] fn discover_with_git_includes_diff_snapshot_for_tracked_changes() { let _guard = env_lock(); diff --git a/rust/crates/runtime/src/recovery_recipes.rs b/rust/crates/runtime/src/recovery_recipes.rs index 63fe92c9..5a916b88 100644 --- a/rust/crates/runtime/src/recovery_recipes.rs +++ b/rust/crates/runtime/src/recovery_recipes.rs @@ -1,3 +1,4 @@ +#![allow(clippy::cast_possible_truncation, clippy::uninlined_format_args)] //! Recovery recipes for common failure scenarios. //! //! Encodes known automatic recoveries for the six failure scenarios diff --git a/rust/crates/runtime/src/session.rs b/rust/crates/runtime/src/session.rs index 202db6c4..d7f4a549 100644 --- a/rust/crates/runtime/src/session.rs +++ b/rust/crates/runtime/src/session.rs @@ -65,12 +65,26 @@ pub struct SessionFork { pub branch_name: Option, } +/// A single user prompt recorded with a timestamp for history tracking. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SessionPromptEntry { + pub timestamp_ms: u64, + pub text: String, +} + #[derive(Debug, Clone, PartialEq, Eq)] struct SessionPersistence { path: PathBuf, } /// Persisted conversational state for the runtime and CLI session manager. +/// +/// `workspace_root` binds the session to the worktree it was created in. The +/// global session store under `~/.local/share/opencode` is shared across every +/// `opencode serve` instance, so without an explicit workspace root parallel +/// lanes can race and report success while writes land in the wrong CWD. See +/// ROADMAP.md item 41 (Phantom completions root cause) for the full +/// background. #[derive(Debug, Clone)] pub struct Session { pub version: u32, @@ -80,6 +94,8 @@ pub struct Session { pub messages: Vec, pub compaction: Option, pub fork: Option, + pub workspace_root: Option, + pub prompt_history: Vec, persistence: Option, } @@ -92,6 +108,8 @@ impl PartialEq for Session { && self.messages == other.messages && self.compaction == other.compaction && self.fork == other.fork + && self.workspace_root == other.workspace_root + && self.prompt_history == other.prompt_history } } @@ -141,6 +159,8 @@ impl Session { messages: Vec::new(), compaction: None, fork: None, + workspace_root: None, + prompt_history: Vec::new(), persistence: None, } } @@ -151,6 +171,22 @@ impl Session { self } + /// Bind this session to the workspace root it was created in. + /// + /// This is the per-worktree counterpart to the global session store and + /// lets downstream tooling reject writes that drift to the wrong CWD when + /// multiple `opencode serve` instances share `~/.local/share/opencode`. + #[must_use] + pub fn with_workspace_root(mut self, workspace_root: impl Into) -> Self { + self.workspace_root = Some(workspace_root.into()); + self + } + + #[must_use] + pub fn workspace_root(&self) -> Option<&Path> { + self.workspace_root.as_deref() + } + #[must_use] pub fn persistence_path(&self) -> Option<&Path> { self.persistence.as_ref().map(|value| value.path.as_path()) @@ -225,6 +261,8 @@ impl Session { parent_session_id: self.session_id.clone(), branch_name: normalize_optional_string(branch_name), }), + workspace_root: self.workspace_root.clone(), + prompt_history: self.prompt_history.clone(), persistence: None, } } @@ -262,6 +300,23 @@ impl Session { if let Some(fork) = &self.fork { object.insert("fork".to_string(), fork.to_json()); } + if let Some(workspace_root) = &self.workspace_root { + object.insert( + "workspace_root".to_string(), + JsonValue::String(workspace_root_to_string(workspace_root)?), + ); + } + if !self.prompt_history.is_empty() { + object.insert( + "prompt_history".to_string(), + JsonValue::Array( + self.prompt_history + .iter() + .map(SessionPromptEntry::to_jsonl_record) + .collect(), + ), + ); + } Ok(JsonValue::Object(object)) } @@ -302,6 +357,20 @@ impl Session { .map(SessionCompaction::from_json) .transpose()?; let fork = object.get("fork").map(SessionFork::from_json).transpose()?; + let workspace_root = object + .get("workspace_root") + .and_then(JsonValue::as_str) + .map(PathBuf::from); + let prompt_history = object + .get("prompt_history") + .and_then(JsonValue::as_array) + .map(|entries| { + entries + .iter() + .filter_map(SessionPromptEntry::from_json_opt) + .collect() + }) + .unwrap_or_default(); Ok(Self { version, session_id, @@ -310,6 +379,8 @@ impl Session { messages, compaction, fork, + workspace_root, + prompt_history, persistence: None, }) } @@ -322,6 +393,8 @@ impl Session { let mut messages = Vec::new(); let mut compaction = None; let mut fork = None; + let mut workspace_root = None; + let mut prompt_history = Vec::new(); for (line_number, raw_line) in contents.lines().enumerate() { let line = raw_line.trim(); @@ -356,6 +429,10 @@ impl Session { created_at_ms = Some(required_u64(object, "created_at_ms")?); updated_at_ms = Some(required_u64(object, "updated_at_ms")?); fork = object.get("fork").map(SessionFork::from_json).transpose()?; + workspace_root = object + .get("workspace_root") + .and_then(JsonValue::as_str) + .map(PathBuf::from); } "message" => { let message_value = object.get("message").ok_or_else(|| { @@ -371,6 +448,13 @@ impl Session { object.clone(), ))?); } + "prompt_history" => { + if let Some(entry) = + SessionPromptEntry::from_json_opt(&JsonValue::Object(object.clone())) + { + prompt_history.push(entry); + } + } other => { return Err(SessionError::Format(format!( "unsupported JSONL record type at line {}: {other}", @@ -389,15 +473,37 @@ impl Session { messages, compaction, fork, + workspace_root, + prompt_history, persistence: None, }) } + /// Record a user prompt with the current wall-clock timestamp. + /// + /// The entry is appended to the in-memory history and, when a persistence + /// path is configured, incrementally written to the JSONL session file. + pub fn push_prompt_entry(&mut self, text: impl Into) -> Result<(), SessionError> { + let timestamp_ms = current_time_millis(); + let entry = SessionPromptEntry { + timestamp_ms, + text: text.into(), + }; + self.prompt_history.push(entry); + let entry_ref = self.prompt_history.last().expect("entry was just pushed"); + self.append_persisted_prompt_entry(entry_ref) + } + fn render_jsonl_snapshot(&self) -> Result { let mut lines = vec![self.meta_record()?.render()]; if let Some(compaction) = &self.compaction { lines.push(compaction.to_jsonl_record()?.render()); } + lines.extend( + self.prompt_history + .iter() + .map(|entry| entry.to_jsonl_record().render()), + ); lines.extend( self.messages .iter() @@ -424,6 +530,25 @@ impl Session { Ok(()) } + fn append_persisted_prompt_entry( + &self, + entry: &SessionPromptEntry, + ) -> Result<(), SessionError> { + let Some(path) = self.persistence_path() else { + return Ok(()); + }; + + let needs_bootstrap = !path.exists() || fs::metadata(path)?.len() == 0; + if needs_bootstrap { + self.save_to_path(path)?; + return Ok(()); + } + + let mut file = OpenOptions::new().append(true).open(path)?; + writeln!(file, "{}", entry.to_jsonl_record().render())?; + Ok(()) + } + fn meta_record(&self) -> Result { let mut object = BTreeMap::new(); object.insert( @@ -449,6 +574,12 @@ impl Session { if let Some(fork) = &self.fork { object.insert("fork".to_string(), fork.to_json()); } + if let Some(workspace_root) = &self.workspace_root { + object.insert( + "workspace_root".to_string(), + JsonValue::String(workspace_root_to_string(workspace_root)?), + ); + } Ok(JsonValue::Object(object)) } @@ -734,6 +865,33 @@ impl SessionFork { } } +impl SessionPromptEntry { + #[must_use] + pub fn to_jsonl_record(&self) -> JsonValue { + let mut object = BTreeMap::new(); + object.insert( + "type".to_string(), + JsonValue::String("prompt_history".to_string()), + ); + object.insert( + "timestamp_ms".to_string(), + JsonValue::Number(i64::try_from(self.timestamp_ms).unwrap_or(i64::MAX)), + ); + object.insert("text".to_string(), JsonValue::String(self.text.clone())); + JsonValue::Object(object) + } + + fn from_json_opt(value: &JsonValue) -> Option { + let object = value.as_object()?; + let timestamp_ms = object + .get("timestamp_ms") + .and_then(JsonValue::as_i64) + .and_then(|value| u64::try_from(value).ok())?; + let text = object.get("text").and_then(JsonValue::as_str)?.to_string(); + Some(Self { timestamp_ms, text }) + } +} + fn message_record(message: &ConversationMessage) -> JsonValue { let mut object = BTreeMap::new(); object.insert("type".to_string(), JsonValue::String("message".to_string())); @@ -825,6 +983,15 @@ fn i64_from_usize(value: usize, key: &str) -> Result { .map_err(|_| SessionError::Format(format!("{key} out of range for JSON number"))) } +fn workspace_root_to_string(path: &Path) -> Result { + path.to_str().map(ToOwned::to_owned).ok_or_else(|| { + SessionError::Format(format!( + "workspace_root is not valid UTF-8: {}", + path.display() + )) + }) +} + fn normalize_optional_string(value: Option) -> Option { value.and_then(|value| { let trimmed = value.trim(); @@ -1206,6 +1373,29 @@ mod tests { assert!(error.to_string().contains("unsupported block type")); } + #[test] + fn persists_workspace_root_round_trip_and_forks_inherit_it() { + // given + let path = temp_session_path("workspace-root"); + let workspace_root = PathBuf::from("/tmp/b4-phantom-diag"); + let mut session = Session::new().with_workspace_root(workspace_root.clone()); + session + .push_user_text("write to the right cwd") + .expect("user message should append"); + + // when + session + .save_to_path(&path) + .expect("workspace-bound session should save"); + let restored = Session::load_from_path(&path).expect("session should load"); + let forked = restored.fork(Some("phantom-diag".to_string())); + fs::remove_file(&path).expect("temp file should be removable"); + + // then + assert_eq!(restored.workspace_root(), Some(workspace_root.as_path())); + assert_eq!(forked.workspace_root(), Some(workspace_root.as_path())); + } + fn temp_session_path(label: &str) -> PathBuf { let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) @@ -1244,3 +1434,12 @@ mod tests { .collect() } } + +/// Per-worktree session isolation: returns a session directory namespaced +/// by the workspace fingerprint of the given working directory. +/// This prevents parallel `opencode serve` instances from colliding. +pub fn workspace_sessions_dir(cwd: &std::path::Path) -> Result { + let store = crate::session_control::SessionStore::from_cwd(cwd) + .map_err(|e| SessionError::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?; + Ok(store.sessions_dir().to_path_buf()) +} diff --git a/rust/crates/runtime/src/session_control.rs b/rust/crates/runtime/src/session_control.rs index 2192156e..05245192 100644 --- a/rust/crates/runtime/src/session_control.rs +++ b/rust/crates/runtime/src/session_control.rs @@ -1,13 +1,257 @@ +#![allow(dead_code)] use std::env; use std::fmt::{Display, Formatter}; use std::fs; use std::path::{Path, PathBuf}; use std::time::UNIX_EPOCH; -use serde::{Deserialize, Serialize}; - use crate::session::{Session, SessionError}; -use crate::worker_boot::{Worker, WorkerReadySnapshot, WorkerRegistry, WorkerStatus}; + +/// Per-worktree session store that namespaces on-disk session files by +/// workspace fingerprint so that parallel `opencode serve` instances never +/// collide. +/// +/// Create via [`SessionStore::from_cwd`] (derives the store path from the +/// server's working directory) or [`SessionStore::from_data_dir`] (honours an +/// explicit `--data-dir` flag). Both constructors produce a directory layout +/// of `/sessions//` where `` is a +/// stable hex digest of the canonical workspace root. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SessionStore { + /// Resolved root of the session namespace, e.g. + /// `/home/user/project/.claw/sessions/a1b2c3d4e5f60718/`. + sessions_root: PathBuf, + /// The canonical workspace path that was fingerprinted. + workspace_root: PathBuf, +} + +impl SessionStore { + /// Build a store from the server's current working directory. + /// + /// The on-disk layout becomes `/.claw/sessions//`. + pub fn from_cwd(cwd: impl AsRef) -> Result { + let cwd = cwd.as_ref(); + let sessions_root = cwd + .join(".claw") + .join("sessions") + .join(workspace_fingerprint(cwd)); + fs::create_dir_all(&sessions_root)?; + Ok(Self { + sessions_root, + workspace_root: cwd.to_path_buf(), + }) + } + + /// Build a store from an explicit `--data-dir` flag. + /// + /// The on-disk layout becomes `/sessions//` + /// where `` is derived from `workspace_root`. + pub fn from_data_dir( + data_dir: impl AsRef, + workspace_root: impl AsRef, + ) -> Result { + let workspace_root = workspace_root.as_ref(); + let sessions_root = data_dir + .as_ref() + .join("sessions") + .join(workspace_fingerprint(workspace_root)); + fs::create_dir_all(&sessions_root)?; + Ok(Self { + sessions_root, + workspace_root: workspace_root.to_path_buf(), + }) + } + + /// The fully resolved sessions directory for this namespace. + #[must_use] + pub fn sessions_dir(&self) -> &Path { + &self.sessions_root + } + + /// The workspace root this store is bound to. + #[must_use] + pub fn workspace_root(&self) -> &Path { + &self.workspace_root + } + + pub fn create_handle(&self, session_id: &str) -> SessionHandle { + let id = session_id.to_string(); + let path = self + .sessions_root + .join(format!("{id}.{PRIMARY_SESSION_EXTENSION}")); + SessionHandle { id, path } + } + + pub fn resolve_reference(&self, reference: &str) -> Result { + if is_session_reference_alias(reference) { + let latest = self.latest_session()?; + return Ok(SessionHandle { + id: latest.id, + path: latest.path, + }); + } + + let direct = PathBuf::from(reference); + let candidate = if direct.is_absolute() { + direct.clone() + } else { + self.workspace_root.join(&direct) + }; + let looks_like_path = direct.extension().is_some() || direct.components().count() > 1; + let path = if candidate.exists() { + candidate + } else if looks_like_path { + return Err(SessionControlError::Format( + format_missing_session_reference(reference), + )); + } else { + self.resolve_managed_path(reference)? + }; + + Ok(SessionHandle { + id: session_id_from_path(&path).unwrap_or_else(|| reference.to_string()), + path, + }) + } + + pub fn resolve_managed_path(&self, session_id: &str) -> Result { + for extension in [PRIMARY_SESSION_EXTENSION, LEGACY_SESSION_EXTENSION] { + let path = self.sessions_root.join(format!("{session_id}.{extension}")); + if path.exists() { + return Ok(path); + } + } + Err(SessionControlError::Format( + format_missing_session_reference(session_id), + )) + } + + pub fn list_sessions(&self) -> Result, SessionControlError> { + let mut sessions = Vec::new(); + let read_result = fs::read_dir(&self.sessions_root); + let entries = match read_result { + Ok(entries) => entries, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(sessions), + Err(err) => return Err(err.into()), + }; + for entry in entries { + let entry = entry?; + let path = entry.path(); + if !is_managed_session_file(&path) { + continue; + } + let metadata = entry.metadata()?; + let modified_epoch_millis = metadata + .modified() + .ok() + .and_then(|time| time.duration_since(UNIX_EPOCH).ok()) + .map(|duration| duration.as_millis()) + .unwrap_or_default(); + let (id, message_count, parent_session_id, branch_name) = + match Session::load_from_path(&path) { + Ok(session) => { + let parent_session_id = session + .fork + .as_ref() + .map(|fork| fork.parent_session_id.clone()); + let branch_name = session + .fork + .as_ref() + .and_then(|fork| fork.branch_name.clone()); + ( + session.session_id, + session.messages.len(), + parent_session_id, + branch_name, + ) + } + Err(_) => ( + path.file_stem() + .and_then(|value| value.to_str()) + .unwrap_or("unknown") + .to_string(), + 0, + None, + None, + ), + }; + sessions.push(ManagedSessionSummary { + id, + path, + modified_epoch_millis, + message_count, + parent_session_id, + branch_name, + }); + } + sessions.sort_by(|left, right| { + right + .modified_epoch_millis + .cmp(&left.modified_epoch_millis) + .then_with(|| right.id.cmp(&left.id)) + }); + Ok(sessions) + } + + pub fn latest_session(&self) -> Result { + self.list_sessions()? + .into_iter() + .next() + .ok_or_else(|| SessionControlError::Format(format_no_managed_sessions())) + } + + pub fn load_session( + &self, + reference: &str, + ) -> Result { + let handle = self.resolve_reference(reference)?; + let session = Session::load_from_path(&handle.path)?; + Ok(LoadedManagedSession { + handle: SessionHandle { + id: session.session_id.clone(), + path: handle.path, + }, + session, + }) + } + + pub fn fork_session( + &self, + session: &Session, + branch_name: Option, + ) -> Result { + let parent_session_id = session.session_id.clone(); + let forked = session.fork(branch_name); + let handle = self.create_handle(&forked.session_id); + let branch_name = forked + .fork + .as_ref() + .and_then(|fork| fork.branch_name.clone()); + let forked = forked.with_persistence_path(handle.path.clone()); + forked.save_to_path(&handle.path)?; + Ok(ForkedManagedSession { + parent_session_id, + handle, + session: forked, + branch_name, + }) + } +} + +/// Stable hex fingerprint of a workspace path. +/// +/// Uses FNV-1a (64-bit) to produce a 16-char hex string that partitions the +/// on-disk session directory per workspace root. +#[must_use] +pub fn workspace_fingerprint(workspace_root: &Path) -> String { + let input = workspace_root.to_string_lossy(); + let mut hash = 0xcbf2_9ce4_8422_2325_u64; + for byte in input.as_bytes() { + hash ^= u64::from(*byte); + hash = hash.wrapping_mul(0x0100_0000_01b3); + } + format!("{hash:016x}") +} pub const PRIMARY_SESSION_EXTENSION: &str = "jsonl"; pub const LEGACY_SESSION_EXTENSION: &str = "json"; @@ -335,7 +579,7 @@ mod tests { use super::{ create_managed_session_handle_for, fork_managed_session_for, is_session_reference_alias, list_managed_sessions_for, load_managed_session_for, resolve_session_reference_for, - ManagedSessionSummary, LATEST_SESSION_REFERENCE, + workspace_fingerprint, ManagedSessionSummary, SessionStore, LATEST_SESSION_REFERENCE, }; use crate::session::Session; use std::fs; @@ -458,4 +702,172 @@ mod tests { ); fs::remove_dir_all(root).expect("temp dir should clean up"); } + + // ------------------------------------------------------------------ + // Per-worktree session isolation (SessionStore) tests + // ------------------------------------------------------------------ + + fn persist_session_via_store(store: &SessionStore, text: &str) -> Session { + let mut session = Session::new(); + session + .push_user_text(text) + .expect("session message should save"); + let handle = store.create_handle(&session.session_id); + let session = session.with_persistence_path(handle.path.clone()); + session + .save_to_path(&handle.path) + .expect("session should persist"); + session + } + + #[test] + fn workspace_fingerprint_is_deterministic_and_differs_per_path() { + // given + let path_a = Path::new("/tmp/worktree-alpha"); + let path_b = Path::new("/tmp/worktree-beta"); + + // when + let fp_a1 = workspace_fingerprint(path_a); + let fp_a2 = workspace_fingerprint(path_a); + let fp_b = workspace_fingerprint(path_b); + + // then + assert_eq!(fp_a1, fp_a2, "same path must produce the same fingerprint"); + assert_ne!( + fp_a1, fp_b, + "different paths must produce different fingerprints" + ); + assert_eq!(fp_a1.len(), 16, "fingerprint must be a 16-char hex string"); + } + + #[test] + fn session_store_from_cwd_isolates_sessions_by_workspace() { + // given + let base = temp_dir(); + let workspace_a = base.join("repo-alpha"); + let workspace_b = base.join("repo-beta"); + fs::create_dir_all(&workspace_a).expect("workspace a should exist"); + fs::create_dir_all(&workspace_b).expect("workspace b should exist"); + + let store_a = SessionStore::from_cwd(&workspace_a).expect("store a should build"); + let store_b = SessionStore::from_cwd(&workspace_b).expect("store b should build"); + + // when + let session_a = persist_session_via_store(&store_a, "alpha work"); + let _session_b = persist_session_via_store(&store_b, "beta work"); + + // then — each store only sees its own sessions + let list_a = store_a.list_sessions().expect("list a"); + let list_b = store_b.list_sessions().expect("list b"); + assert_eq!(list_a.len(), 1, "store a should see exactly one session"); + assert_eq!(list_b.len(), 1, "store b should see exactly one session"); + assert_eq!(list_a[0].id, session_a.session_id); + assert_ne!( + store_a.sessions_dir(), + store_b.sessions_dir(), + "session directories must differ across workspaces" + ); + fs::remove_dir_all(base).expect("temp dir should clean up"); + } + + #[test] + fn session_store_from_data_dir_namespaces_by_workspace() { + // given + let base = temp_dir(); + let data_dir = base.join("global-data"); + let workspace_a = PathBuf::from("/tmp/project-one"); + let workspace_b = PathBuf::from("/tmp/project-two"); + fs::create_dir_all(&data_dir).expect("data dir should exist"); + + let store_a = + SessionStore::from_data_dir(&data_dir, &workspace_a).expect("store a should build"); + let store_b = + SessionStore::from_data_dir(&data_dir, &workspace_b).expect("store b should build"); + + // when + persist_session_via_store(&store_a, "work in project-one"); + persist_session_via_store(&store_b, "work in project-two"); + + // then + assert_ne!( + store_a.sessions_dir(), + store_b.sessions_dir(), + "data-dir stores must namespace by workspace" + ); + assert_eq!(store_a.list_sessions().expect("list a").len(), 1); + assert_eq!(store_b.list_sessions().expect("list b").len(), 1); + assert_eq!(store_a.workspace_root(), workspace_a.as_path()); + assert_eq!(store_b.workspace_root(), workspace_b.as_path()); + fs::remove_dir_all(base).expect("temp dir should clean up"); + } + + #[test] + fn session_store_create_and_load_round_trip() { + // given + let base = temp_dir(); + fs::create_dir_all(&base).expect("base dir should exist"); + let store = SessionStore::from_cwd(&base).expect("store should build"); + let session = persist_session_via_store(&store, "round-trip message"); + + // when + let loaded = store + .load_session(&session.session_id) + .expect("session should load via store"); + + // then + assert_eq!(loaded.handle.id, session.session_id); + assert_eq!(loaded.session.messages.len(), 1); + fs::remove_dir_all(base).expect("temp dir should clean up"); + } + + #[test] + fn session_store_latest_and_resolve_reference() { + // given + let base = temp_dir(); + fs::create_dir_all(&base).expect("base dir should exist"); + let store = SessionStore::from_cwd(&base).expect("store should build"); + let _older = persist_session_via_store(&store, "older"); + wait_for_next_millisecond(); + let newer = persist_session_via_store(&store, "newer"); + + // when + let latest = store.latest_session().expect("latest should resolve"); + let handle = store + .resolve_reference("latest") + .expect("latest alias should resolve"); + + // then + assert_eq!(latest.id, newer.session_id); + assert_eq!(handle.id, newer.session_id); + fs::remove_dir_all(base).expect("temp dir should clean up"); + } + + #[test] + fn session_store_fork_stays_in_same_namespace() { + // given + let base = temp_dir(); + fs::create_dir_all(&base).expect("base dir should exist"); + let store = SessionStore::from_cwd(&base).expect("store should build"); + let source = persist_session_via_store(&store, "parent work"); + + // when + let forked = store + .fork_session(&source, Some("bugfix".to_string())) + .expect("fork should succeed"); + let sessions = store.list_sessions().expect("list sessions"); + + // then + assert_eq!( + sessions.len(), + 2, + "forked session must land in the same namespace" + ); + assert_eq!(forked.parent_session_id, source.session_id); + assert_eq!(forked.branch_name.as_deref(), Some("bugfix")); + assert!( + forked.handle.path.starts_with(store.sessions_dir()), + "forked session path must be inside the store namespace" + ); + fs::remove_dir_all(base).expect("temp dir should clean up"); + } } diff --git a/rust/crates/runtime/src/stale_base.rs b/rust/crates/runtime/src/stale_base.rs new file mode 100644 index 00000000..b432d307 --- /dev/null +++ b/rust/crates/runtime/src/stale_base.rs @@ -0,0 +1,429 @@ +#![allow(clippy::must_use_candidate)] +use std::path::Path; +use std::process::Command; + +/// Outcome of comparing the worktree HEAD against the expected base commit. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum BaseCommitState { + /// HEAD matches the expected base commit. + Matches, + /// HEAD has diverged from the expected base. + Diverged { expected: String, actual: String }, + /// No expected base was supplied (neither flag nor file). + NoExpectedBase, + /// The working directory is not inside a git repository. + NotAGitRepo, +} + +/// Where the expected base commit originated from. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum BaseCommitSource { + Flag(String), + File(String), +} + +/// Read the `.claw-base` file from the given directory and return the trimmed +/// commit hash, or `None` when the file is absent or empty. +pub fn read_claw_base_file(cwd: &Path) -> Option { + let path = cwd.join(".claw-base"); + let content = std::fs::read_to_string(path).ok()?; + let trimmed = content.trim(); + if trimmed.is_empty() { + None + } else { + Some(trimmed.to_string()) + } +} + +/// Resolve the expected base commit: prefer the `--base-commit` flag value, +/// fall back to reading `.claw-base` from `cwd`. +pub fn resolve_expected_base(flag_value: Option<&str>, cwd: &Path) -> Option { + if let Some(value) = flag_value { + let trimmed = value.trim(); + if !trimmed.is_empty() { + return Some(BaseCommitSource::Flag(trimmed.to_string())); + } + } + read_claw_base_file(cwd).map(BaseCommitSource::File) +} + +/// Verify that the worktree HEAD matches `expected_base`. +/// +/// Returns [`BaseCommitState::NoExpectedBase`] when no expected commit is +/// provided (the check is effectively a no-op in that case). +pub fn check_base_commit(cwd: &Path, expected_base: Option<&BaseCommitSource>) -> BaseCommitState { + let Some(source) = expected_base else { + return BaseCommitState::NoExpectedBase; + }; + let expected_raw = match source { + BaseCommitSource::Flag(value) | BaseCommitSource::File(value) => value.as_str(), + }; + + let Some(head_sha) = resolve_head_sha(cwd) else { + return BaseCommitState::NotAGitRepo; + }; + + let Some(expected_sha) = resolve_rev(cwd, expected_raw) else { + // If the expected ref cannot be resolved, compare raw strings as a + // best-effort fallback (e.g. partial SHA provided by the caller). + return if head_sha.starts_with(expected_raw) || expected_raw.starts_with(&head_sha) { + BaseCommitState::Matches + } else { + BaseCommitState::Diverged { + expected: expected_raw.to_string(), + actual: head_sha, + } + }; + }; + + if head_sha == expected_sha { + BaseCommitState::Matches + } else { + BaseCommitState::Diverged { + expected: expected_sha, + actual: head_sha, + } + } +} + +/// Format a human-readable warning when the base commit has diverged. +/// +/// Returns `None` for non-warning states (`Matches`, `NoExpectedBase`). +pub fn format_stale_base_warning(state: &BaseCommitState) -> Option { + match state { + BaseCommitState::Diverged { expected, actual } => Some(format!( + "warning: worktree HEAD ({actual}) does not match expected base commit ({expected}). \ + Session may run against a stale codebase." + )), + BaseCommitState::NotAGitRepo => { + Some("warning: stale-base check skipped — not inside a git repository.".to_string()) + } + BaseCommitState::Matches | BaseCommitState::NoExpectedBase => None, + } +} + +fn resolve_head_sha(cwd: &Path) -> Option { + resolve_rev(cwd, "HEAD") +} + +fn resolve_rev(cwd: &Path, rev: &str) -> Option { + let output = Command::new("git") + .args(["rev-parse", rev]) + .current_dir(cwd) + .output() + .ok()?; + if !output.status.success() { + return None; + } + let sha = String::from_utf8(output.stdout).ok()?; + let trimmed = sha.trim(); + if trimmed.is_empty() { + None + } else { + Some(trimmed.to_string()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use std::process::Command; + use std::time::{SystemTime, UNIX_EPOCH}; + + fn temp_dir() -> std::path::PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time should be after epoch") + .as_nanos(); + std::env::temp_dir().join(format!("runtime-stale-base-{nanos}")) + } + + fn init_repo(path: &std::path::Path) { + fs::create_dir_all(path).expect("create repo dir"); + run(path, &["init", "--quiet", "-b", "main"]); + run(path, &["config", "user.email", "tests@example.com"]); + run(path, &["config", "user.name", "Stale Base Tests"]); + fs::write(path.join("init.txt"), "initial\n").expect("write init file"); + run(path, &["add", "."]); + run(path, &["commit", "-m", "initial commit", "--quiet"]); + } + + fn run(cwd: &std::path::Path, args: &[&str]) { + let status = Command::new("git") + .args(args) + .current_dir(cwd) + .status() + .unwrap_or_else(|e| panic!("git {} failed to execute: {e}", args.join(" "))); + assert!( + status.success(), + "git {} exited with {status}", + args.join(" ") + ); + } + + fn commit_file(repo: &std::path::Path, name: &str, msg: &str) { + fs::write(repo.join(name), format!("{msg}\n")).expect("write file"); + run(repo, &["add", name]); + run(repo, &["commit", "-m", msg, "--quiet"]); + } + + fn head_sha(repo: &std::path::Path) -> String { + let output = Command::new("git") + .args(["rev-parse", "HEAD"]) + .current_dir(repo) + .output() + .expect("git rev-parse HEAD"); + String::from_utf8(output.stdout) + .expect("valid utf8") + .trim() + .to_string() + } + + #[test] + fn matches_when_head_equals_expected_base() { + // given + let root = temp_dir(); + init_repo(&root); + let sha = head_sha(&root); + let source = BaseCommitSource::Flag(sha); + + // when + let state = check_base_commit(&root, Some(&source)); + + // then + assert_eq!(state, BaseCommitState::Matches); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn diverged_when_head_moved_past_expected_base() { + // given + let root = temp_dir(); + init_repo(&root); + let old_sha = head_sha(&root); + commit_file(&root, "extra.txt", "move head forward"); + let new_sha = head_sha(&root); + let source = BaseCommitSource::Flag(old_sha.clone()); + + // when + let state = check_base_commit(&root, Some(&source)); + + // then + assert_eq!( + state, + BaseCommitState::Diverged { + expected: old_sha, + actual: new_sha, + } + ); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn no_expected_base_when_source_is_none() { + // given + let root = temp_dir(); + init_repo(&root); + + // when + let state = check_base_commit(&root, None); + + // then + assert_eq!(state, BaseCommitState::NoExpectedBase); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn not_a_git_repo_when_outside_repo() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + let source = BaseCommitSource::Flag("abc1234".to_string()); + + // when + let state = check_base_commit(&root, Some(&source)); + + // then + assert_eq!(state, BaseCommitState::NotAGitRepo); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn reads_claw_base_file() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + fs::write(root.join(".claw-base"), "abc1234def5678\n").expect("write .claw-base"); + + // when + let value = read_claw_base_file(&root); + + // then + assert_eq!(value, Some("abc1234def5678".to_string())); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn returns_none_for_missing_claw_base_file() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + + // when + let value = read_claw_base_file(&root); + + // then + assert!(value.is_none()); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn returns_none_for_empty_claw_base_file() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + fs::write(root.join(".claw-base"), " \n").expect("write empty .claw-base"); + + // when + let value = read_claw_base_file(&root); + + // then + assert!(value.is_none()); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn resolve_expected_base_prefers_flag_over_file() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + fs::write(root.join(".claw-base"), "from_file\n").expect("write .claw-base"); + + // when + let source = resolve_expected_base(Some("from_flag"), &root); + + // then + assert_eq!( + source, + Some(BaseCommitSource::Flag("from_flag".to_string())) + ); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn resolve_expected_base_falls_back_to_file() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + fs::write(root.join(".claw-base"), "from_file\n").expect("write .claw-base"); + + // when + let source = resolve_expected_base(None, &root); + + // then + assert_eq!( + source, + Some(BaseCommitSource::File("from_file".to_string())) + ); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn resolve_expected_base_returns_none_when_nothing_available() { + // given + let root = temp_dir(); + fs::create_dir_all(&root).expect("create dir"); + + // when + let source = resolve_expected_base(None, &root); + + // then + assert!(source.is_none()); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn format_warning_returns_message_for_diverged() { + // given + let state = BaseCommitState::Diverged { + expected: "abc1234".to_string(), + actual: "def5678".to_string(), + }; + + // when + let warning = format_stale_base_warning(&state); + + // then + let message = warning.expect("should produce warning"); + assert!(message.contains("abc1234")); + assert!(message.contains("def5678")); + assert!(message.contains("stale codebase")); + } + + #[test] + fn format_warning_returns_none_for_matches() { + // given + let state = BaseCommitState::Matches; + + // when + let warning = format_stale_base_warning(&state); + + // then + assert!(warning.is_none()); + } + + #[test] + fn format_warning_returns_none_for_no_expected_base() { + // given + let state = BaseCommitState::NoExpectedBase; + + // when + let warning = format_stale_base_warning(&state); + + // then + assert!(warning.is_none()); + } + + #[test] + fn matches_with_claw_base_file_in_real_repo() { + // given + let root = temp_dir(); + init_repo(&root); + let sha = head_sha(&root); + fs::write(root.join(".claw-base"), format!("{sha}\n")).expect("write .claw-base"); + let source = resolve_expected_base(None, &root); + + // when + let state = check_base_commit(&root, source.as_ref()); + + // then + assert_eq!(state, BaseCommitState::Matches); + fs::remove_dir_all(&root).expect("cleanup"); + } + + #[test] + fn diverged_with_claw_base_file_after_new_commit() { + // given + let root = temp_dir(); + init_repo(&root); + let old_sha = head_sha(&root); + fs::write(root.join(".claw-base"), format!("{old_sha}\n")).expect("write .claw-base"); + commit_file(&root, "new.txt", "advance head"); + let new_sha = head_sha(&root); + let source = resolve_expected_base(None, &root); + + // when + let state = check_base_commit(&root, source.as_ref()); + + // then + assert_eq!( + state, + BaseCommitState::Diverged { + expected: old_sha, + actual: new_sha, + } + ); + fs::remove_dir_all(&root).expect("cleanup"); + } +} diff --git a/rust/crates/runtime/src/stale_branch.rs b/rust/crates/runtime/src/stale_branch.rs index 6f69dc0a..ccdd3f53 100644 --- a/rust/crates/runtime/src/stale_branch.rs +++ b/rust/crates/runtime/src/stale_branch.rs @@ -1,3 +1,4 @@ +#![allow(clippy::must_use_candidate)] use std::path::Path; use std::process::Command; diff --git a/rust/crates/runtime/src/task_packet.rs b/rust/crates/runtime/src/task_packet.rs index 2170f22a..86d1c6c5 100644 --- a/rust/crates/runtime/src/task_packet.rs +++ b/rust/crates/runtime/src/task_packet.rs @@ -66,11 +66,7 @@ pub fn validate_packet(packet: TaskPacket) -> Result) -> Task { - self.create_task( - prompt.to_owned(), - description.map(str::to_owned), - None, - ) + self.create_task(prompt.to_owned(), description.map(str::to_owned), None) } pub fn create_from_packet( diff --git a/rust/crates/runtime/src/team_cron_registry.rs b/rust/crates/runtime/src/team_cron_registry.rs index be23dfe6..1e1a65f0 100644 --- a/rust/crates/runtime/src/team_cron_registry.rs +++ b/rust/crates/runtime/src/team_cron_registry.rs @@ -1,3 +1,4 @@ +#![allow(clippy::must_use_candidate)] //! In-memory registries for Team and Cron lifecycle management. //! //! Provides TeamCreate/Delete and CronCreate/Delete/List runtime backing diff --git a/rust/crates/runtime/src/worker_boot.rs b/rust/crates/runtime/src/worker_boot.rs index 4854e2a9..c8ed3d00 100644 --- a/rust/crates/runtime/src/worker_boot.rs +++ b/rust/crates/runtime/src/worker_boot.rs @@ -1,3 +1,10 @@ +#![allow( + clippy::struct_excessive_bools, + clippy::too_many_lines, + clippy::question_mark, + clippy::redundant_closure, + clippy::map_unwrap_or +)] //! In-memory worker-boot state machine and control registry. //! //! This provides a foundational control plane for reliable worker startup: @@ -257,7 +264,9 @@ impl WorkerRegistry { let prompt_preview = prompt_preview(worker.last_prompt.as_deref().unwrap_or_default()); let message = match observation.target { WorkerPromptTarget::Shell => { - format!("worker prompt landed in shell instead of coding agent: {prompt_preview}") + format!( + "worker prompt landed in shell instead of coding agent: {prompt_preview}" + ) } WorkerPromptTarget::WrongTarget => format!( "worker prompt landed in the wrong target instead of {}: {}", @@ -312,7 +321,9 @@ impl WorkerRegistry { worker.last_error = None; } - if detect_ready_for_prompt(screen_text, &lowered) && worker.status != WorkerStatus::ReadyForPrompt { + if detect_ready_for_prompt(screen_text, &lowered) + && worker.status != WorkerStatus::ReadyForPrompt + { worker.status = WorkerStatus::ReadyForPrompt; worker.prompt_in_flight = false; if matches!( @@ -412,7 +423,10 @@ impl WorkerRegistry { worker_id: worker.worker_id.clone(), status: worker.status, ready: worker.status == WorkerStatus::ReadyForPrompt, - blocked: matches!(worker.status, WorkerStatus::TrustRequired | WorkerStatus::Failed), + blocked: matches!( + worker.status, + WorkerStatus::TrustRequired | WorkerStatus::Failed + ), replay_prompt_ready: worker.replay_prompt.is_some(), last_error: worker.last_error.clone(), }) diff --git a/rust/crates/runtime/tests/integration_tests.rs b/rust/crates/runtime/tests/integration_tests.rs index 06a37726..49c66360 100644 --- a/rust/crates/runtime/tests/integration_tests.rs +++ b/rust/crates/runtime/tests/integration_tests.rs @@ -1,3 +1,4 @@ +#![allow(clippy::doc_markdown, clippy::uninlined_format_args, unused_imports)] //! Integration tests for cross-module wiring. //! //! These tests verify that adjacent modules in the runtime crate actually diff --git a/rust/crates/rusty-claude-cli/Cargo.toml b/rust/crates/rusty-claude-cli/Cargo.toml index 4e2e8e7e..635fdb32 100644 --- a/rust/crates/rusty-claude-cli/Cargo.toml +++ b/rust/crates/rusty-claude-cli/Cargo.toml @@ -31,3 +31,4 @@ workspace = true mock-anthropic-service = { path = "../mock-anthropic-service" } serde_json.workspace = true tokio = { version = "1", features = ["rt-multi-thread"] } + diff --git a/rust/crates/rusty-claude-cli/src/app.rs b/rust/crates/rusty-claude-cli/src/app.rs deleted file mode 100644 index 8a97c057..00000000 --- a/rust/crates/rusty-claude-cli/src/app.rs +++ /dev/null @@ -1,567 +0,0 @@ -use std::io::{self, Write}; -use std::path::PathBuf; - -use crate::args::{OutputFormat, PermissionMode}; -use crate::input::{LineEditor, ReadOutcome}; -use crate::render::{Spinner, TerminalRenderer}; -use runtime::{ConversationClient, ConversationMessage, RuntimeError, StreamEvent, UsageSummary}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SessionConfig { - pub model: String, - pub permission_mode: PermissionMode, - pub config: Option, - pub output_format: OutputFormat, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SessionState { - pub turns: usize, - pub compacted_messages: usize, - pub last_model: String, - pub last_usage: UsageSummary, -} - -impl SessionState { - #[must_use] - pub fn new(model: impl Into) -> Self { - Self { - turns: 0, - compacted_messages: 0, - last_model: model.into(), - last_usage: UsageSummary::default(), - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum CommandResult { - Continue, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum SlashCommand { - Help, - Status, - Compact, - Model { model: Option }, - Permissions { mode: Option }, - Config { section: Option }, - Memory, - Clear { confirm: bool }, - Unknown(String), -} - -impl SlashCommand { - #[must_use] - pub fn parse(input: &str) -> Option { - let trimmed = input.trim(); - if !trimmed.starts_with('/') { - return None; - } - - let mut parts = trimmed.trim_start_matches('/').split_whitespace(); - let command = parts.next().unwrap_or_default(); - Some(match command { - "help" => Self::Help, - "status" => Self::Status, - "compact" => Self::Compact, - "model" => Self::Model { - model: parts.next().map(ToOwned::to_owned), - }, - "permissions" => Self::Permissions { - mode: parts.next().map(ToOwned::to_owned), - }, - "config" => Self::Config { - section: parts.next().map(ToOwned::to_owned), - }, - "memory" => Self::Memory, - "clear" => Self::Clear { - confirm: parts.next() == Some("--confirm"), - }, - other => Self::Unknown(other.to_string()), - }) - } -} - -struct SlashCommandHandler { - command: SlashCommand, - summary: &'static str, -} - -const SLASH_COMMAND_HANDLERS: &[SlashCommandHandler] = &[ - SlashCommandHandler { - command: SlashCommand::Help, - summary: "Show command help", - }, - SlashCommandHandler { - command: SlashCommand::Status, - summary: "Show current session status", - }, - SlashCommandHandler { - command: SlashCommand::Compact, - summary: "Compact local session history", - }, - SlashCommandHandler { - command: SlashCommand::Model { model: None }, - summary: "Show or switch the active model", - }, - SlashCommandHandler { - command: SlashCommand::Permissions { mode: None }, - summary: "Show or switch the active permission mode", - }, - SlashCommandHandler { - command: SlashCommand::Config { section: None }, - summary: "Inspect current config path or section", - }, - SlashCommandHandler { - command: SlashCommand::Memory, - summary: "Inspect loaded memory/instruction files", - }, - SlashCommandHandler { - command: SlashCommand::Clear { confirm: false }, - summary: "Start a fresh local session", - }, -]; - -pub struct CliApp { - config: SessionConfig, - renderer: TerminalRenderer, - state: SessionState, - conversation_client: ConversationClient, - conversation_history: Vec, -} - -impl CliApp { - pub fn new(config: SessionConfig) -> Result { - let state = SessionState::new(config.model.clone()); - let conversation_client = ConversationClient::from_env(config.model.clone())?; - Ok(Self { - config, - renderer: TerminalRenderer::new(), - state, - conversation_client, - conversation_history: Vec::new(), - }) - } - - pub fn run_repl(&mut self) -> io::Result<()> { - let mut editor = LineEditor::new("› ", Vec::new()); - println!("Rusty Claude CLI interactive mode"); - println!("Type /help for commands. Shift+Enter or Ctrl+J inserts a newline."); - - loop { - match editor.read_line()? { - ReadOutcome::Submit(input) => { - if input.trim().is_empty() { - continue; - } - self.handle_submission(&input, &mut io::stdout())?; - } - ReadOutcome::Cancel => continue, - ReadOutcome::Exit => break, - } - } - - Ok(()) - } - - pub fn run_prompt(&mut self, prompt: &str, out: &mut impl Write) -> io::Result<()> { - self.render_response(prompt, out) - } - - pub fn handle_submission( - &mut self, - input: &str, - out: &mut impl Write, - ) -> io::Result { - if let Some(command) = SlashCommand::parse(input) { - return self.dispatch_slash_command(command, out); - } - - self.state.turns += 1; - self.render_response(input, out)?; - Ok(CommandResult::Continue) - } - - fn dispatch_slash_command( - &mut self, - command: SlashCommand, - out: &mut impl Write, - ) -> io::Result { - match command { - SlashCommand::Help => Self::handle_help(out), - SlashCommand::Status => self.handle_status(out), - SlashCommand::Compact => self.handle_compact(out), - SlashCommand::Model { model } => self.handle_model(model.as_deref(), out), - SlashCommand::Permissions { mode } => self.handle_permissions(mode.as_deref(), out), - SlashCommand::Config { section } => self.handle_config(section.as_deref(), out), - SlashCommand::Memory => self.handle_memory(out), - SlashCommand::Clear { confirm } => self.handle_clear(confirm, out), - SlashCommand::Unknown(name) => { - writeln!(out, "Unknown slash command: /{name}")?; - Ok(CommandResult::Continue) - } - } - } - - fn handle_help(out: &mut impl Write) -> io::Result { - writeln!(out, "Available commands:")?; - for handler in SLASH_COMMAND_HANDLERS { - let name = match handler.command { - SlashCommand::Help => "/help", - SlashCommand::Status => "/status", - SlashCommand::Compact => "/compact", - SlashCommand::Model { .. } => "/model [model]", - SlashCommand::Permissions { .. } => "/permissions [mode]", - SlashCommand::Config { .. } => "/config [section]", - SlashCommand::Memory => "/memory", - SlashCommand::Clear { .. } => "/clear [--confirm]", - SlashCommand::Unknown(_) => continue, - }; - writeln!(out, " {name:<9} {}", handler.summary)?; - } - Ok(CommandResult::Continue) - } - - fn handle_status(&mut self, out: &mut impl Write) -> io::Result { - writeln!( - out, - "status: turns={} model={} permission-mode={:?} output-format={:?} last-usage={} in/{} out config={}", - self.state.turns, - self.state.last_model, - self.config.permission_mode, - self.config.output_format, - self.state.last_usage.input_tokens, - self.state.last_usage.output_tokens, - self.config - .config - .as_ref() - .map_or_else(|| String::from(""), |path| path.display().to_string()) - )?; - Ok(CommandResult::Continue) - } - - fn handle_compact(&mut self, out: &mut impl Write) -> io::Result { - self.state.compacted_messages += self.state.turns; - self.state.turns = 0; - self.conversation_history.clear(); - writeln!( - out, - "Compacted session history into a local summary ({} messages total compacted).", - self.state.compacted_messages - )?; - Ok(CommandResult::Continue) - } - - fn handle_model( - &mut self, - model: Option<&str>, - out: &mut impl Write, - ) -> io::Result { - match model { - Some(model) => { - self.config.model = model.to_string(); - self.state.last_model = model.to_string(); - writeln!(out, "Active model set to {model}")?; - } - None => { - writeln!(out, "Active model: {}", self.config.model)?; - } - } - Ok(CommandResult::Continue) - } - - fn handle_permissions( - &mut self, - mode: Option<&str>, - out: &mut impl Write, - ) -> io::Result { - match mode { - None => writeln!(out, "Permission mode: {:?}", self.config.permission_mode)?, - Some("read-only") => { - self.config.permission_mode = PermissionMode::ReadOnly; - writeln!(out, "Permission mode set to read-only")?; - } - Some("workspace-write") => { - self.config.permission_mode = PermissionMode::WorkspaceWrite; - writeln!(out, "Permission mode set to workspace-write")?; - } - Some("danger-full-access") => { - self.config.permission_mode = PermissionMode::DangerFullAccess; - writeln!(out, "Permission mode set to danger-full-access")?; - } - Some(other) => { - writeln!(out, "Unknown permission mode: {other}")?; - } - } - Ok(CommandResult::Continue) - } - - fn handle_config( - &mut self, - section: Option<&str>, - out: &mut impl Write, - ) -> io::Result { - match section { - None => writeln!( - out, - "Config path: {}", - self.config - .config - .as_ref() - .map_or_else(|| String::from(""), |path| path.display().to_string()) - )?, - Some(section) => writeln!( - out, - "Config section `{section}` is not fully implemented yet; current config path is {}", - self.config - .config - .as_ref() - .map_or_else(|| String::from(""), |path| path.display().to_string()) - )?, - } - Ok(CommandResult::Continue) - } - - fn handle_memory(&mut self, out: &mut impl Write) -> io::Result { - writeln!( - out, - "Loaded memory/config file: {}", - self.config - .config - .as_ref() - .map_or_else(|| String::from(""), |path| path.display().to_string()) - )?; - Ok(CommandResult::Continue) - } - - fn handle_clear(&mut self, confirm: bool, out: &mut impl Write) -> io::Result { - if !confirm { - writeln!(out, "Refusing to clear without confirmation. Re-run as /clear --confirm")?; - return Ok(CommandResult::Continue); - } - - self.state.turns = 0; - self.state.compacted_messages = 0; - self.state.last_usage = UsageSummary::default(); - self.conversation_history.clear(); - writeln!(out, "Started a fresh local session.")?; - Ok(CommandResult::Continue) - } - - fn handle_stream_event( - renderer: &TerminalRenderer, - event: StreamEvent, - stream_spinner: &mut Spinner, - tool_spinner: &mut Spinner, - saw_text: &mut bool, - turn_usage: &mut UsageSummary, - out: &mut impl Write, - ) { - match event { - StreamEvent::TextDelta(delta) => { - if !*saw_text { - let _ = - stream_spinner.finish("Streaming response", renderer.color_theme(), out); - *saw_text = true; - } - let _ = write!(out, "{delta}"); - let _ = out.flush(); - } - StreamEvent::ToolCallStart { name, input } => { - if *saw_text { - let _ = writeln!(out); - } - let _ = tool_spinner.tick( - &format!("Running tool `{name}` with {input}"), - renderer.color_theme(), - out, - ); - } - StreamEvent::ToolCallResult { - name, - output, - is_error, - } => { - let label = if is_error { - format!("Tool `{name}` failed") - } else { - format!("Tool `{name}` completed") - }; - let _ = tool_spinner.finish(&label, renderer.color_theme(), out); - let rendered_output = format!("### Tool `{name}`\n\n```text\n{output}\n```\n"); - let _ = renderer.stream_markdown(&rendered_output, out); - } - StreamEvent::Usage(usage) => { - *turn_usage = usage; - } - } - } - - fn write_turn_output( - &self, - summary: &runtime::TurnSummary, - out: &mut impl Write, - ) -> io::Result<()> { - match self.config.output_format { - OutputFormat::Text => { - writeln!( - out, - "\nToken usage: {} input / {} output", - self.state.last_usage.input_tokens, self.state.last_usage.output_tokens - )?; - } - OutputFormat::Json => { - writeln!( - out, - "{}", - serde_json::json!({ - "message": summary.assistant_text, - "usage": { - "input_tokens": self.state.last_usage.input_tokens, - "output_tokens": self.state.last_usage.output_tokens, - } - }) - )?; - } - OutputFormat::Ndjson => { - writeln!( - out, - "{}", - serde_json::json!({ - "type": "message", - "text": summary.assistant_text, - "usage": { - "input_tokens": self.state.last_usage.input_tokens, - "output_tokens": self.state.last_usage.output_tokens, - } - }) - )?; - } - } - Ok(()) - } - - fn render_response(&mut self, input: &str, out: &mut impl Write) -> io::Result<()> { - let mut stream_spinner = Spinner::new(); - stream_spinner.tick( - "Opening conversation stream", - self.renderer.color_theme(), - out, - )?; - - let mut turn_usage = UsageSummary::default(); - let mut tool_spinner = Spinner::new(); - let mut saw_text = false; - let renderer = &self.renderer; - - let result = - self.conversation_client - .run_turn(&mut self.conversation_history, input, |event| { - Self::handle_stream_event( - renderer, - event, - &mut stream_spinner, - &mut tool_spinner, - &mut saw_text, - &mut turn_usage, - out, - ); - }); - - let summary = match result { - Ok(summary) => summary, - Err(error) => { - stream_spinner.fail( - "Streaming response failed", - self.renderer.color_theme(), - out, - )?; - return Err(io::Error::other(error)); - } - }; - self.state.last_usage = summary.usage.clone(); - if saw_text { - writeln!(out)?; - } else { - stream_spinner.finish("Streaming response", self.renderer.color_theme(), out)?; - } - - self.write_turn_output(&summary, out)?; - let _ = turn_usage; - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use std::path::PathBuf; - - use crate::args::{OutputFormat, PermissionMode}; - - use super::{CommandResult, SessionConfig, SlashCommand}; - - #[test] - fn parses_required_slash_commands() { - assert_eq!(SlashCommand::parse("/help"), Some(SlashCommand::Help)); - assert_eq!(SlashCommand::parse(" /status "), Some(SlashCommand::Status)); - assert_eq!( - SlashCommand::parse("/compact now"), - Some(SlashCommand::Compact) - ); - assert_eq!( - SlashCommand::parse("/model claude-sonnet"), - Some(SlashCommand::Model { - model: Some("claude-sonnet".into()), - }) - ); - assert_eq!( - SlashCommand::parse("/permissions workspace-write"), - Some(SlashCommand::Permissions { - mode: Some("workspace-write".into()), - }) - ); - assert_eq!( - SlashCommand::parse("/config hooks"), - Some(SlashCommand::Config { - section: Some("hooks".into()), - }) - ); - assert_eq!(SlashCommand::parse("/memory"), Some(SlashCommand::Memory)); - assert_eq!( - SlashCommand::parse("/clear --confirm"), - Some(SlashCommand::Clear { confirm: true }) - ); - } - - #[test] - fn help_output_lists_commands() { - let mut out = Vec::new(); - let result = super::CliApp::handle_help(&mut out).expect("help succeeds"); - assert_eq!(result, CommandResult::Continue); - let output = String::from_utf8_lossy(&out); - assert!(output.contains("/help")); - assert!(output.contains("/status")); - assert!(output.contains("/compact")); - assert!(output.contains("/model [model]")); - assert!(output.contains("/permissions [mode]")); - assert!(output.contains("/config [section]")); - assert!(output.contains("/memory")); - assert!(output.contains("/clear [--confirm]")); - } - - #[test] - fn session_state_tracks_config_values() { - let config = SessionConfig { - model: "claude".into(), - permission_mode: PermissionMode::DangerFullAccess, - config: Some(PathBuf::from("settings.toml")), - output_format: OutputFormat::Text, - }; - - assert_eq!(config.model, "claude"); - assert_eq!(config.permission_mode, PermissionMode::DangerFullAccess); - assert_eq!(config.config, Some(PathBuf::from("settings.toml"))); - } -} diff --git a/rust/crates/rusty-claude-cli/src/args.rs b/rust/crates/rusty-claude-cli/src/args.rs deleted file mode 100644 index e36934a8..00000000 --- a/rust/crates/rusty-claude-cli/src/args.rs +++ /dev/null @@ -1,108 +0,0 @@ -use std::path::PathBuf; - -use clap::{Parser, Subcommand, ValueEnum}; - -#[derive(Debug, Clone, Parser, PartialEq, Eq)] -#[command( - name = "rusty-claude-cli", - version, - about = "Rust Claude CLI prototype" -)] -pub struct Cli { - #[arg(long, default_value = "claude-opus-4-6")] - pub model: String, - - #[arg(long, value_enum, default_value_t = PermissionMode::DangerFullAccess)] - pub permission_mode: PermissionMode, - - #[arg(long)] - pub config: Option, - - #[arg(long, value_enum, default_value_t = OutputFormat::Text)] - pub output_format: OutputFormat, - - #[command(subcommand)] - pub command: Option, -} - -#[derive(Debug, Clone, Subcommand, PartialEq, Eq)] -pub enum Command { - /// Read upstream TS sources and print extracted counts - DumpManifests, - /// Print the current bootstrap phase skeleton - BootstrapPlan, - /// Start the OAuth login flow - Login, - /// Clear saved OAuth credentials - Logout, - /// Run a non-interactive prompt and exit - Prompt { prompt: Vec }, -} - -#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)] -pub enum PermissionMode { - ReadOnly, - WorkspaceWrite, - DangerFullAccess, -} - -#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)] -pub enum OutputFormat { - Text, - Json, - Ndjson, -} - -#[cfg(test)] -mod tests { - use clap::Parser; - - use super::{Cli, Command, OutputFormat, PermissionMode}; - - #[test] - fn parses_requested_flags() { - let cli = Cli::parse_from([ - "rusty-claude-cli", - "--model", - "claude-3-5-haiku", - "--permission-mode", - "read-only", - "--config", - "/tmp/config.toml", - "--output-format", - "ndjson", - "prompt", - "hello", - "world", - ]); - - assert_eq!(cli.model, "claude-3-5-haiku"); - assert_eq!(cli.permission_mode, PermissionMode::ReadOnly); - assert_eq!( - cli.config.as_deref(), - Some(std::path::Path::new("/tmp/config.toml")) - ); - assert_eq!(cli.output_format, OutputFormat::Ndjson); - assert_eq!( - cli.command, - Some(Command::Prompt { - prompt: vec!["hello".into(), "world".into()] - }) - ); - } - - #[test] - fn parses_login_and_logout_commands() { - let login = Cli::parse_from(["rusty-claude-cli", "login"]); - assert_eq!(login.command, Some(Command::Login)); - - let logout = Cli::parse_from(["rusty-claude-cli", "logout"]); - assert_eq!(logout.command, Some(Command::Logout)); - } - - #[test] - fn defaults_to_danger_full_access_permission_mode() { - let cli = Cli::parse_from(["rusty-claude-cli"]); - assert_eq!(cli.permission_mode, PermissionMode::DangerFullAccess); - } -} diff --git a/rust/crates/rusty-claude-cli/src/init.rs b/rust/crates/rusty-claude-cli/src/init.rs index 67d11875..a5f0ff7c 100644 --- a/rust/crates/rusty-claude-cli/src/init.rs +++ b/rust/crates/rusty-claude-cli/src/init.rs @@ -1,7 +1,7 @@ use std::fs; use std::path::{Path, PathBuf}; -const STARTER_CLAUDE_JSON: &str = concat!( +const STARTER_CLAW_JSON: &str = concat!( "{\n", " \"permissions\": {\n", " \"defaultMode\": \"dontAsk\"\n", @@ -9,7 +9,7 @@ const STARTER_CLAUDE_JSON: &str = concat!( "}\n", ); const GITIGNORE_COMMENT: &str = "# Claw Code local artifacts"; -const GITIGNORE_ENTRIES: [&str; 2] = [".claude/settings.local.json", ".claude/sessions/"]; +const GITIGNORE_ENTRIES: [&str; 2] = [".claw/settings.local.json", ".claw/sessions/"]; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum InitStatus { @@ -80,16 +80,16 @@ struct RepoDetection { pub(crate) fn initialize_repo(cwd: &Path) -> Result> { let mut artifacts = Vec::new(); - let claude_dir = cwd.join(".claude"); + let claw_dir = cwd.join(".claw"); artifacts.push(InitArtifact { - name: ".claude/", - status: ensure_dir(&claude_dir)?, + name: ".claw/", + status: ensure_dir(&claw_dir)?, }); - let claude_json = cwd.join(".claude.json"); + let claw_json = cwd.join(".claw.json"); artifacts.push(InitArtifact { - name: ".claude.json", - status: write_file_if_missing(&claude_json, STARTER_CLAUDE_JSON)?, + name: ".claw.json", + status: write_file_if_missing(&claw_json, STARTER_CLAW_JSON)?, }); let gitignore = cwd.join(".gitignore"); @@ -209,7 +209,7 @@ pub(crate) fn render_init_claude_md(cwd: &Path) -> String { lines.push("## Working agreement".to_string()); lines.push("- Prefer small, reviewable changes and keep generated bootstrap files aligned with actual repo workflows.".to_string()); - lines.push("- Keep shared defaults in `.claude.json`; reserve `.claude/settings.local.json` for machine-local overrides.".to_string()); + lines.push("- Keep shared defaults in `.claw.json`; reserve `.claw/settings.local.json` for machine-local overrides.".to_string()); lines.push("- Do not overwrite existing `CLAUDE.md` content automatically; update it intentionally when repo workflows change.".to_string()); lines.push(String::new()); @@ -354,15 +354,16 @@ mod tests { let report = initialize_repo(&root).expect("init should succeed"); let rendered = report.render(); - assert!(rendered.contains(".claude/ created")); - assert!(rendered.contains(".claude.json created")); + assert!(rendered.contains(".claw/")); + assert!(rendered.contains(".claw.json")); + assert!(rendered.contains("created")); assert!(rendered.contains(".gitignore created")); assert!(rendered.contains("CLAUDE.md created")); - assert!(root.join(".claude").is_dir()); - assert!(root.join(".claude.json").is_file()); + assert!(root.join(".claw").is_dir()); + assert!(root.join(".claw.json").is_file()); assert!(root.join("CLAUDE.md").is_file()); assert_eq!( - fs::read_to_string(root.join(".claude.json")).expect("read claude json"), + fs::read_to_string(root.join(".claw.json")).expect("read claw json"), concat!( "{\n", " \"permissions\": {\n", @@ -372,8 +373,8 @@ mod tests { ) ); let gitignore = fs::read_to_string(root.join(".gitignore")).expect("read gitignore"); - assert!(gitignore.contains(".claude/settings.local.json")); - assert!(gitignore.contains(".claude/sessions/")); + assert!(gitignore.contains(".claw/settings.local.json")); + assert!(gitignore.contains(".claw/sessions/")); let claude_md = fs::read_to_string(root.join("CLAUDE.md")).expect("read claude md"); assert!(claude_md.contains("Languages: Rust.")); assert!(claude_md.contains("cargo clippy --workspace --all-targets -- -D warnings")); @@ -386,8 +387,7 @@ mod tests { let root = temp_dir(); fs::create_dir_all(&root).expect("create root"); fs::write(root.join("CLAUDE.md"), "custom guidance\n").expect("write existing claude md"); - fs::write(root.join(".gitignore"), ".claude/settings.local.json\n") - .expect("write gitignore"); + fs::write(root.join(".gitignore"), ".claw/settings.local.json\n").expect("write gitignore"); let first = initialize_repo(&root).expect("first init should succeed"); assert!(first @@ -395,8 +395,9 @@ mod tests { .contains("CLAUDE.md skipped (already exists)")); let second = initialize_repo(&root).expect("second init should succeed"); let second_rendered = second.render(); - assert!(second_rendered.contains(".claude/ skipped (already exists)")); - assert!(second_rendered.contains(".claude.json skipped (already exists)")); + assert!(second_rendered.contains(".claw/")); + assert!(second_rendered.contains(".claw.json")); + assert!(second_rendered.contains("skipped (already exists)")); assert!(second_rendered.contains(".gitignore skipped (already exists)")); assert!(second_rendered.contains("CLAUDE.md skipped (already exists)")); assert_eq!( @@ -404,8 +405,8 @@ mod tests { "custom guidance\n" ); let gitignore = fs::read_to_string(root.join(".gitignore")).expect("read gitignore"); - assert_eq!(gitignore.matches(".claude/settings.local.json").count(), 1); - assert_eq!(gitignore.matches(".claude/sessions/").count(), 1); + assert_eq!(gitignore.matches(".claw/settings.local.json").count(), 1); + assert_eq!(gitignore.matches(".claw/sessions/").count(), 1); fs::remove_dir_all(root).expect("cleanup temp dir"); } diff --git a/rust/crates/rusty-claude-cli/src/main.rs b/rust/crates/rusty-claude-cli/src/main.rs index 81700722..847b0faa 100644 --- a/rust/crates/rusty-claude-cli/src/main.rs +++ b/rust/crates/rusty-claude-cli/src/main.rs @@ -13,7 +13,7 @@ mod render; use std::collections::BTreeSet; use std::env; use std::fs; -use std::io::{self, Read, Write}; +use std::io::{self, IsTerminal, Read, Write}; use std::net::TcpListener; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; @@ -24,33 +24,37 @@ use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant, UNIX_EPOCH}; use api::{ - resolve_startup_auth_source, AnthropicClient, AuthSource, ContentBlockDelta, InputContentBlock, - InputMessage, MessageRequest, MessageResponse, OutputContentBlock, PromptCache, - StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock, + detect_provider_kind, oauth_token_is_expired, resolve_startup_auth_source, AnthropicClient, + AuthSource, ContentBlockDelta, InputContentBlock, InputMessage, MessageRequest, + MessageResponse, OutputContentBlock, PromptCache, ProviderKind, StreamEvent as ApiStreamEvent, + ToolChoice, ToolDefinition, ToolResultContentBlock, }; use commands::{ - handle_agents_slash_command, handle_mcp_slash_command, handle_plugins_slash_command, - handle_skills_slash_command, render_slash_command_help, resume_supported_slash_commands, - slash_command_specs, validate_slash_command_input, SlashCommand, + classify_skills_slash_command, handle_agents_slash_command, handle_agents_slash_command_json, + handle_mcp_slash_command, handle_mcp_slash_command_json, handle_plugins_slash_command, + handle_skills_slash_command, handle_skills_slash_command_json, render_slash_command_help, + resume_supported_slash_commands, slash_command_specs, validate_slash_command_input, + SkillSlashDispatch, SlashCommand, }; use compat_harness::{extract_manifest, UpstreamPaths}; use init::initialize_repo; use plugins::{PluginHooks, PluginManager, PluginManagerConfig, PluginRegistry}; use render::{MarkdownStreamState, Spinner, TerminalRenderer}; use runtime::{ - clear_oauth_credentials, format_usd, generate_pkce_pair, generate_state, load_system_prompt, - parse_oauth_callback_request_target, pricing_for_model, resolve_sandbox_status, - save_oauth_credentials, ApiClient, ApiRequest, AssistantEvent, CompactionConfig, ConfigLoader, - ConfigSource, ContentBlock, ConversationMessage, ConversationRuntime, McpServerManager, - McpTool, MessageRole, ModelPricing, OAuthAuthorizationRequest, OAuthConfig, - OAuthTokenExchangeRequest, PermissionMode, PermissionPolicy, ProjectContext, PromptCacheEvent, - ResolvedPermissionMode, RuntimeError, Session, TokenUsage, ToolError, ToolExecutor, - UsageTracker, + check_base_commit, clear_oauth_credentials, format_stale_base_warning, format_usd, + generate_pkce_pair, generate_state, load_oauth_credentials, load_system_prompt, + parse_oauth_callback_request_target, pricing_for_model, resolve_expected_base, + resolve_sandbox_status, save_oauth_credentials, ApiClient, ApiRequest, AssistantEvent, + CompactionConfig, ConfigLoader, ConfigSource, ContentBlock, ConversationMessage, + ConversationRuntime, McpServer, McpServerManager, McpServerSpec, McpTool, MessageRole, + ModelPricing, OAuthAuthorizationRequest, OAuthConfig, OAuthTokenExchangeRequest, + PermissionMode, PermissionPolicy, ProjectContext, PromptCacheEvent, ResolvedPermissionMode, + RuntimeError, Session, TokenUsage, ToolError, ToolExecutor, UsageTracker, }; use serde::Deserialize; -use serde_json::json; -use tools::{GlobalToolRegistry, RuntimeToolDefinition, ToolSearchOutput}; +use serde_json::{json, Map, Value}; +use tools::{execute_tool, mvp_tool_specs, GlobalToolRegistry, RuntimeToolDefinition, ToolSearchOutput}; const DEFAULT_MODEL: &str = "claude-opus-4-6"; fn max_tokens_for_model(model: &str) -> u32 { @@ -66,6 +70,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); const BUILD_TARGET: Option<&str> = option_env!("TARGET"); const GIT_SHA: Option<&str> = option_env!("GIT_SHA"); const INTERNAL_PROGRESS_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(3); +const POST_TOOL_STALL_TIMEOUT: Duration = Duration::from_secs(10); const PRIMARY_SESSION_EXTENSION: &str = "jsonl"; const LEGACY_SESSION_EXTENSION: &str = "json"; const LATEST_SESSION_REFERENCE: &str = "latest"; @@ -83,10 +88,16 @@ const CLI_OPTION_SUGGESTIONS: &[&str] = &[ "--allowed-tools", "--resume", "--print", + "--compact", + "--base-commit", "-p", ]; type AllowedToolSet = BTreeSet; +type RuntimePluginStateBuildOutput = ( + Option>>, + Vec, +); fn main() { if let Err(error) = run() { @@ -104,90 +115,210 @@ Run `claw --help` for usage." } } +/// Read piped stdin content when stdin is not a terminal. +/// +/// Returns `None` when stdin is attached to a terminal (interactive REPL use), +/// when reading fails, or when the piped content is empty after trimming. +/// Returns `Some(raw_content)` when a pipe delivered non-empty content. +fn read_piped_stdin() -> Option { + if io::stdin().is_terminal() { + return None; + } + let mut buffer = String::new(); + if io::stdin().read_to_string(&mut buffer).is_err() { + return None; + } + if buffer.trim().is_empty() { + return None; + } + Some(buffer) +} + +/// Merge a piped stdin payload into a prompt argument. +/// +/// When `stdin_content` is `None` or empty after trimming, the prompt is +/// returned unchanged. Otherwise the trimmed stdin content is appended to the +/// prompt separated by a blank line so the model sees the prompt first and the +/// piped context immediately after it. +fn merge_prompt_with_stdin(prompt: &str, stdin_content: Option<&str>) -> String { + let Some(raw) = stdin_content else { + return prompt.to_string(); + }; + let trimmed = raw.trim(); + if trimmed.is_empty() { + return prompt.to_string(); + } + if prompt.is_empty() { + return trimmed.to_string(); + } + format!("{prompt}\n\n{trimmed}") +} + fn run() -> Result<(), Box> { let args: Vec = env::args().skip(1).collect(); match parse_args(&args)? { - CliAction::DumpManifests => dump_manifests(), - CliAction::BootstrapPlan => print_bootstrap_plan(), - CliAction::Agents { args } => LiveCli::print_agents(args.as_deref())?, - CliAction::Mcp { args } => LiveCli::print_mcp(args.as_deref())?, - CliAction::Skills { args } => LiveCli::print_skills(args.as_deref())?, - CliAction::PrintSystemPrompt { cwd, date } => print_system_prompt(cwd, date), - CliAction::Version => print_version(), + CliAction::DumpManifests { output_format } => dump_manifests(output_format)?, + CliAction::BootstrapPlan { output_format } => print_bootstrap_plan(output_format)?, + CliAction::Agents { + args, + output_format, + } => LiveCli::print_agents(args.as_deref(), output_format)?, + CliAction::Mcp { + args, + output_format, + } => LiveCli::print_mcp(args.as_deref(), output_format)?, + CliAction::Skills { + args, + output_format, + } => LiveCli::print_skills(args.as_deref(), output_format)?, + CliAction::Plugins { + action, + target, + output_format, + } => LiveCli::print_plugins(action.as_deref(), target.as_deref(), output_format)?, + CliAction::PrintSystemPrompt { + cwd, + date, + output_format, + } => print_system_prompt(cwd, date, output_format)?, + CliAction::Version { output_format } => print_version(output_format)?, CliAction::ResumeSession { session_path, commands, - } => resume_session(&session_path, &commands), + output_format, + } => resume_session(&session_path, &commands, output_format), CliAction::Status { model, permission_mode, - } => print_status_snapshot(&model, permission_mode)?, - CliAction::Sandbox => print_sandbox_status_snapshot()?, + output_format, + } => print_status_snapshot(&model, permission_mode, output_format)?, + CliAction::Sandbox { output_format } => print_sandbox_status_snapshot(output_format)?, CliAction::Prompt { prompt, model, output_format, allowed_tools, permission_mode, - } => LiveCli::new(model, true, allowed_tools, permission_mode)? - .run_turn_with_output(&prompt, output_format)?, - CliAction::Login => run_login()?, - CliAction::Logout => run_logout()?, - CliAction::Init => run_init()?, + compact: _, + base_commit, + } => { + run_stale_base_preflight(base_commit.as_deref()); + let stdin_context = read_piped_stdin(); + let effective_prompt = merge_prompt_with_stdin(&prompt, stdin_context.as_deref()); + LiveCli::new(model, true, allowed_tools, permission_mode)? + .run_turn_with_output(&effective_prompt, output_format, false)?; + } + CliAction::Login { output_format } => run_login(output_format)?, + CliAction::Logout { output_format } => run_logout(output_format)?, + CliAction::Doctor { output_format } => run_doctor(output_format)?, + CliAction::Init { output_format } => run_init(output_format)?, + CliAction::Export { + session_reference, + output_path, + output_format, + } => run_export(&session_reference, output_path.as_deref(), output_format)?, CliAction::Repl { model, allowed_tools, permission_mode, - } => run_repl(model, allowed_tools, permission_mode)?, - CliAction::Help => print_help(), + base_commit, + } => run_repl(model, allowed_tools, permission_mode, base_commit)?, + CliAction::HelpTopic(topic) => print_help_topic(topic), + CliAction::Help { output_format } => print_help(output_format)?, } Ok(()) } #[derive(Debug, Clone, PartialEq, Eq)] enum CliAction { - DumpManifests, - BootstrapPlan, + DumpManifests { + output_format: CliOutputFormat, + }, + BootstrapPlan { + output_format: CliOutputFormat, + }, Agents { args: Option, + output_format: CliOutputFormat, }, Mcp { args: Option, + output_format: CliOutputFormat, }, Skills { args: Option, + output_format: CliOutputFormat, + }, + Plugins { + action: Option, + target: Option, + output_format: CliOutputFormat, }, PrintSystemPrompt { cwd: PathBuf, date: String, + output_format: CliOutputFormat, + }, + Version { + output_format: CliOutputFormat, }, - Version, ResumeSession { session_path: PathBuf, commands: Vec, + output_format: CliOutputFormat, }, Status { model: String, permission_mode: PermissionMode, + output_format: CliOutputFormat, + }, + Sandbox { + output_format: CliOutputFormat, }, - Sandbox, Prompt { prompt: String, model: String, output_format: CliOutputFormat, allowed_tools: Option, permission_mode: PermissionMode, + compact: bool, + base_commit: Option, + }, + Login { + output_format: CliOutputFormat, + }, + Logout { + output_format: CliOutputFormat, + }, + Doctor { + output_format: CliOutputFormat, + }, + Init { + output_format: CliOutputFormat, + }, + Export { + session_reference: String, + output_path: Option, + output_format: CliOutputFormat, }, - Login, - Logout, - Init, Repl { model: String, allowed_tools: Option, permission_mode: PermissionMode, + base_commit: Option, }, + HelpTopic(LocalHelpTopic), // prompt-mode formatting is only supported for non-interactive runs - Help, + Help { + output_format: CliOutputFormat, + }, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum LocalHelpTopic { + Status, + Sandbox, + Doctor, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -216,6 +347,8 @@ fn parse_args(args: &[String]) -> Result { let mut wants_help = false; let mut wants_version = false; let mut allowed_tool_values = Vec::new(); + let mut compact = false; + let mut base_commit: Option = None; let mut rest = Vec::new(); let mut index = 0; @@ -233,11 +366,11 @@ fn parse_args(args: &[String]) -> Result { let value = args .get(index + 1) .ok_or_else(|| "missing value for --model".to_string())?; - model = resolve_model_alias(value).to_string(); + model = resolve_model_alias_with_config(value); index += 2; } flag if flag.starts_with("--model=") => { - model = resolve_model_alias(&flag[8..]).to_string(); + model = resolve_model_alias_with_config(&flag[8..]); index += 1; } "--output-format" => { @@ -266,6 +399,21 @@ fn parse_args(args: &[String]) -> Result { permission_mode_override = Some(PermissionMode::DangerFullAccess); index += 1; } + "--compact" => { + compact = true; + index += 1; + } + "--base-commit" => { + let value = args + .get(index + 1) + .ok_or_else(|| "missing value for --base-commit".to_string())?; + base_commit = Some(value.clone()); + index += 2; + } + flag if flag.starts_with("--base-commit=") => { + base_commit = Some(flag[14..].to_string()); + index += 1; + } "-p" => { // Claw Code compat: -p "prompt" = one-shot prompt let prompt = args[index + 1..].join(" "); @@ -274,11 +422,13 @@ fn parse_args(args: &[String]) -> Result { } return Ok(CliAction::Prompt { prompt, - model: resolve_model_alias(&model).to_string(), + model: resolve_model_alias_with_config(&model), output_format, allowed_tools: normalize_allowed_tools(&allowed_tool_values)?, permission_mode: permission_mode_override .unwrap_or_else(default_permission_mode), + compact, + base_commit: base_commit.clone(), }); } "--print" => { @@ -321,11 +471,11 @@ fn parse_args(args: &[String]) -> Result { } if wants_help { - return Ok(CliAction::Help); + return Ok(CliAction::Help { output_format }); } if wants_version { - return Ok(CliAction::Version); + return Ok(CliAction::Version { output_format }); } let allowed_tools = normalize_allowed_tools(&allowed_tool_values)?; @@ -336,12 +486,17 @@ fn parse_args(args: &[String]) -> Result { model, allowed_tools, permission_mode, + base_commit, }); } if rest.first().map(String::as_str) == Some("--resume") { - return parse_resume_args(&rest[1..]); + return parse_resume_args(&rest[1..], output_format); } - if let Some(action) = parse_single_word_command_alias(&rest, &model, permission_mode_override) + if let Some(action) = parse_local_help_action(&rest) { + return action; + } + if let Some(action) = + parse_single_word_command_alias(&rest, &model, permission_mode_override, output_format) { return action; } @@ -349,21 +504,39 @@ fn parse_args(args: &[String]) -> Result { let permission_mode = permission_mode_override.unwrap_or_else(default_permission_mode); match rest[0].as_str() { - "dump-manifests" => Ok(CliAction::DumpManifests), - "bootstrap-plan" => Ok(CliAction::BootstrapPlan), + "dump-manifests" => Ok(CliAction::DumpManifests { output_format }), + "bootstrap-plan" => Ok(CliAction::BootstrapPlan { output_format }), "agents" => Ok(CliAction::Agents { args: join_optional_args(&rest[1..]), + output_format, }), "mcp" => Ok(CliAction::Mcp { args: join_optional_args(&rest[1..]), + output_format, }), - "skills" => Ok(CliAction::Skills { - args: join_optional_args(&rest[1..]), - }), - "system-prompt" => parse_system_prompt_args(&rest[1..]), - "login" => Ok(CliAction::Login), - "logout" => Ok(CliAction::Logout), - "init" => Ok(CliAction::Init), + "skills" => { + let args = join_optional_args(&rest[1..]); + match classify_skills_slash_command(args.as_deref()) { + SkillSlashDispatch::Invoke(prompt) => Ok(CliAction::Prompt { + prompt, + model, + output_format, + allowed_tools, + permission_mode, + compact, + base_commit, + }), + SkillSlashDispatch::Local => Ok(CliAction::Skills { + args, + output_format, + }), + } + } + "system-prompt" => parse_system_prompt_args(&rest[1..], output_format), + "login" => Ok(CliAction::Login { output_format }), + "logout" => Ok(CliAction::Logout { output_format }), + "init" => Ok(CliAction::Init { output_format }), + "export" => parse_export_args(&rest[1..], output_format), "prompt" => { let prompt = rest[1..].join(" "); if prompt.trim().is_empty() { @@ -375,36 +548,69 @@ fn parse_args(args: &[String]) -> Result { output_format, allowed_tools, permission_mode, + compact, + base_commit: base_commit.clone(), }) } - other if other.starts_with('/') => parse_direct_slash_cli_action(&rest), + other if other.starts_with('/') => parse_direct_slash_cli_action( + &rest, + model, + output_format, + allowed_tools, + permission_mode, + compact, + base_commit, + ), _other => Ok(CliAction::Prompt { prompt: rest.join(" "), model, output_format, allowed_tools, permission_mode, + compact, + base_commit, }), } } +fn parse_local_help_action(rest: &[String]) -> Option> { + if rest.len() != 2 || !is_help_flag(&rest[1]) { + return None; + } + + let topic = match rest[0].as_str() { + "status" => LocalHelpTopic::Status, + "sandbox" => LocalHelpTopic::Sandbox, + "doctor" => LocalHelpTopic::Doctor, + _ => return None, + }; + Some(Ok(CliAction::HelpTopic(topic))) +} + +fn is_help_flag(value: &str) -> bool { + matches!(value, "--help" | "-h") +} + fn parse_single_word_command_alias( rest: &[String], model: &str, permission_mode_override: Option, + output_format: CliOutputFormat, ) -> Option> { if rest.len() != 1 { return None; } match rest[0].as_str() { - "help" => Some(Ok(CliAction::Help)), - "version" => Some(Ok(CliAction::Version)), + "help" => Some(Ok(CliAction::Help { output_format })), + "version" => Some(Ok(CliAction::Version { output_format })), "status" => Some(Ok(CliAction::Status { model: model.to_string(), permission_mode: permission_mode_override.unwrap_or_else(default_permission_mode), + output_format, })), - "sandbox" => Some(Ok(CliAction::Sandbox)), + "sandbox" => Some(Ok(CliAction::Sandbox { output_format })), + "doctor" => Some(Ok(CliAction::Doctor { output_format })), other => bare_slash_command_guidance(other).map(Err), } } @@ -422,6 +628,7 @@ fn bare_slash_command_guidance(command_name: &str) -> Option { | "logout" | "init" | "prompt" + | "export" ) { return None; } @@ -446,11 +653,22 @@ fn join_optional_args(args: &[String]) -> Option { (!trimmed.is_empty()).then(|| trimmed.to_string()) } -fn parse_direct_slash_cli_action(rest: &[String]) -> Result { +fn parse_direct_slash_cli_action( + rest: &[String], + model: String, + output_format: CliOutputFormat, + allowed_tools: Option, + permission_mode: PermissionMode, + compact: bool, + base_commit: Option, +) -> Result { let raw = rest.join(" "); match SlashCommand::parse(&raw) { - Ok(Some(SlashCommand::Help)) => Ok(CliAction::Help), - Ok(Some(SlashCommand::Agents { args })) => Ok(CliAction::Agents { args }), + Ok(Some(SlashCommand::Help)) => Ok(CliAction::Help { output_format }), + Ok(Some(SlashCommand::Agents { args })) => Ok(CliAction::Agents { + args, + output_format, + }), Ok(Some(SlashCommand::Mcp { action, target })) => Ok(CliAction::Mcp { args: match (action, target) { (None, None) => None, @@ -458,8 +676,25 @@ fn parse_direct_slash_cli_action(rest: &[String]) -> Result { (Some(action), Some(target)) => Some(format!("{action} {target}")), (None, Some(target)) => Some(target), }, + output_format, }), - Ok(Some(SlashCommand::Skills { args })) => Ok(CliAction::Skills { args }), + Ok(Some(SlashCommand::Skills { args })) => { + match classify_skills_slash_command(args.as_deref()) { + SkillSlashDispatch::Invoke(prompt) => Ok(CliAction::Prompt { + prompt, + model, + output_format, + allowed_tools, + permission_mode, + compact, + base_commit, + }), + SkillSlashDispatch::Local => Ok(CliAction::Skills { + args, + output_format, + }), + } + } Ok(Some(SlashCommand::Unknown(name))) => Err(format_unknown_direct_slash_command(&name)), Ok(Some(command)) => Err({ let _ = command; @@ -492,6 +727,10 @@ fn format_unknown_direct_slash_command(name: &str) -> String { message.push('\n'); message.push_str(&suggestions); } + if let Some(note) = omc_compatibility_note_for_unknown_slash_command(name) { + message.push('\n'); + message.push_str(note); + } message.push_str("\nRun `claw --help` for CLI usage, or start `claw` and use /help."); message } @@ -503,10 +742,21 @@ fn format_unknown_slash_command(name: &str) -> String { message.push('\n'); message.push_str(&suggestions); } + if let Some(note) = omc_compatibility_note_for_unknown_slash_command(name) { + message.push('\n'); + message.push_str(note); + } message.push_str("\n Help /help lists available slash commands"); message } +fn omc_compatibility_note_for_unknown_slash_command(name: &str) -> Option<&'static str> { + name.starts_with("oh-my-claudecode:") + .then_some( + "Compatibility note: `/oh-my-claudecode:*` is a Claude Code/OMC plugin command. `claw` does not yet load plugin slash commands, Claude statusline stdin, or OMC session hooks.", + ) +} + fn render_suggestion_line(label: &str, suggestions: &[String]) -> Option { (!suggestions.is_empty()).then(|| format!(" {label:<16} {}", suggestions.join(", "),)) } @@ -592,6 +842,27 @@ fn resolve_model_alias(model: &str) -> &str { } } +/// Resolve a model name through user-defined config aliases first, then fall +/// back to the built-in alias table. This is the entry point used wherever a +/// user-supplied model string is about to be dispatched to a provider. +fn resolve_model_alias_with_config(model: &str) -> String { + let trimmed = model.trim(); + if let Some(resolved) = config_alias_for_current_dir(trimmed) { + return resolve_model_alias(&resolved).to_string(); + } + resolve_model_alias(trimmed).to_string() +} + +fn config_alias_for_current_dir(alias: &str) -> Option { + if alias.is_empty() { + return None; + } + let cwd = env::current_dir().ok()?; + let loader = ConfigLoader::default_for(&cwd); + let config = loader.load().ok()?; + config.aliases().get(alias).cloned() +} + fn normalize_allowed_tools(values: &[String]) -> Result, String> { if values.is_empty() { return Ok(None); @@ -663,6 +934,46 @@ fn config_permission_mode_for_current_dir() -> Option { .map(permission_mode_from_resolved) } +fn config_model_for_current_dir() -> Option { + let cwd = env::current_dir().ok()?; + let loader = ConfigLoader::default_for(&cwd); + loader + .load() + .ok()? + .model() + .map(ToOwned::to_owned) +} + +fn resolve_repl_model(cli_model: String) -> String { + if cli_model != DEFAULT_MODEL { + return cli_model; + } + if let Some(env_model) = env::var("ANTHROPIC_MODEL") + .ok() + .map(|value| value.trim().to_string()) + .filter(|value| !value.is_empty()) + { + return resolve_model_alias_with_config(&env_model); + } + if let Some(config_model) = config_model_for_current_dir() { + return resolve_model_alias_with_config(&config_model); + } + cli_model +} + +fn provider_label(kind: ProviderKind) -> &'static str { + match kind { + ProviderKind::Anthropic => "anthropic", + ProviderKind::Xai => "xai", + ProviderKind::OpenAi => "openai", + } +} + +fn format_connected_line(model: &str) -> String { + let provider = provider_label(detect_provider_kind(model)); + format!("Connected: {model} via {provider}") +} + fn filter_tool_specs( tool_registry: &GlobalToolRegistry, allowed_tools: Option<&AllowedToolSet>, @@ -670,7 +981,10 @@ fn filter_tool_specs( tool_registry.definitions(allowed_tools) } -fn parse_system_prompt_args(args: &[String]) -> Result { +fn parse_system_prompt_args( + args: &[String], + output_format: CliOutputFormat, +) -> Result { let mut cwd = env::current_dir().map_err(|error| error.to_string())?; let mut date = DEFAULT_DATE.to_string(); let mut index = 0; @@ -695,10 +1009,66 @@ fn parse_system_prompt_args(args: &[String]) -> Result { } } - Ok(CliAction::PrintSystemPrompt { cwd, date }) + Ok(CliAction::PrintSystemPrompt { + cwd, + date, + output_format, + }) } -fn parse_resume_args(args: &[String]) -> Result { +fn parse_export_args( + args: &[String], + output_format: CliOutputFormat, +) -> Result { + let mut session_reference = LATEST_SESSION_REFERENCE.to_string(); + let mut output_path: Option = None; + let mut index = 0; + + while index < args.len() { + match args[index].as_str() { + "--session" => { + let value = args + .get(index + 1) + .ok_or_else(|| "missing value for --session".to_string())?; + session_reference = value.clone(); + index += 2; + } + flag if flag.starts_with("--session=") => { + session_reference = flag[10..].to_string(); + index += 1; + } + "--output" | "-o" => { + let value = args + .get(index + 1) + .ok_or_else(|| format!("missing value for {}", args[index]))?; + output_path = Some(PathBuf::from(value)); + index += 2; + } + flag if flag.starts_with("--output=") => { + output_path = Some(PathBuf::from(&flag[9..])); + index += 1; + } + other if other.starts_with('-') => { + return Err(format!("unknown export option: {other}")); + } + other if output_path.is_none() => { + output_path = Some(PathBuf::from(other)); + index += 1; + } + other => { + return Err(format!("unexpected export argument: {other}")); + } + } + } + + Ok(CliAction::Export { + session_reference, + output_path, + output_format, + }) +} + +fn parse_resume_args(args: &[String], output_format: CliOutputFormat) -> Result { let (session_path, command_tokens): (PathBuf, &[String]) = match args.first() { None => (PathBuf::from(LATEST_SESSION_REFERENCE), &[]), Some(first) if looks_like_slash_command_token(first) => { @@ -738,9 +1108,636 @@ fn parse_resume_args(args: &[String]) -> Result { Ok(CliAction::ResumeSession { session_path, commands, + output_format, }) } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum DiagnosticLevel { + Ok, + Warn, + Fail, +} + +impl DiagnosticLevel { + fn label(self) -> &'static str { + match self { + Self::Ok => "ok", + Self::Warn => "warn", + Self::Fail => "fail", + } + } + + fn is_failure(self) -> bool { + matches!(self, Self::Fail) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct DiagnosticCheck { + name: &'static str, + level: DiagnosticLevel, + summary: String, + details: Vec, + data: Map, +} + +impl DiagnosticCheck { + fn new(name: &'static str, level: DiagnosticLevel, summary: impl Into) -> Self { + Self { + name, + level, + summary: summary.into(), + details: Vec::new(), + data: Map::new(), + } + } + + fn with_details(mut self, details: Vec) -> Self { + self.details = details; + self + } + + fn with_data(mut self, data: Map) -> Self { + self.data = data; + self + } + + fn json_value(&self) -> Value { + let mut value = Map::from_iter([ + ( + "name".to_string(), + Value::String(self.name.to_ascii_lowercase()), + ), + ( + "status".to_string(), + Value::String(self.level.label().to_string()), + ), + ("summary".to_string(), Value::String(self.summary.clone())), + ( + "details".to_string(), + Value::Array( + self.details + .iter() + .cloned() + .map(Value::String) + .collect::>(), + ), + ), + ]); + value.extend(self.data.clone()); + Value::Object(value) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct DoctorReport { + checks: Vec, +} + +impl DoctorReport { + fn counts(&self) -> (usize, usize, usize) { + ( + self.checks + .iter() + .filter(|check| check.level == DiagnosticLevel::Ok) + .count(), + self.checks + .iter() + .filter(|check| check.level == DiagnosticLevel::Warn) + .count(), + self.checks + .iter() + .filter(|check| check.level == DiagnosticLevel::Fail) + .count(), + ) + } + + fn has_failures(&self) -> bool { + self.checks.iter().any(|check| check.level.is_failure()) + } + + fn render(&self) -> String { + let (ok_count, warn_count, fail_count) = self.counts(); + let mut lines = vec![ + "Doctor".to_string(), + format!( + "Summary\n OK {ok_count}\n Warnings {warn_count}\n Failures {fail_count}" + ), + ]; + lines.extend(self.checks.iter().map(render_diagnostic_check)); + lines.join("\n\n") + } + + fn json_value(&self) -> Value { + let report = self.render(); + let (ok_count, warn_count, fail_count) = self.counts(); + json!({ + "kind": "doctor", + "message": report, + "report": report, + "has_failures": self.has_failures(), + "summary": { + "total": self.checks.len(), + "ok": ok_count, + "warnings": warn_count, + "failures": fail_count, + }, + "checks": self + .checks + .iter() + .map(DiagnosticCheck::json_value) + .collect::>(), + }) + } +} + +fn render_diagnostic_check(check: &DiagnosticCheck) -> String { + let mut lines = vec![format!( + "{}\n Status {}\n Summary {}", + check.name, + check.level.label(), + check.summary + )]; + if !check.details.is_empty() { + lines.push(" Details".to_string()); + lines.extend(check.details.iter().map(|detail| format!(" - {detail}"))); + } + lines.join("\n") +} + +fn render_doctor_report() -> Result> { + let cwd = env::current_dir()?; + let config_loader = ConfigLoader::default_for(&cwd); + let config = config_loader.load(); + let discovered_config = config_loader.discover(); + let project_context = ProjectContext::discover_with_git(&cwd, DEFAULT_DATE)?; + let (project_root, git_branch) = + parse_git_status_metadata(project_context.git_status.as_deref()); + let git_summary = parse_git_workspace_summary(project_context.git_status.as_deref()); + let empty_config = runtime::RuntimeConfig::empty(); + let sandbox_config = config.as_ref().ok().unwrap_or(&empty_config); + let context = StatusContext { + cwd: cwd.clone(), + session_path: None, + loaded_config_files: config + .as_ref() + .ok() + .map_or(0, |runtime_config| runtime_config.loaded_entries().len()), + discovered_config_files: discovered_config.len(), + memory_file_count: project_context.instruction_files.len(), + project_root, + git_branch, + git_summary, + sandbox_status: resolve_sandbox_status(sandbox_config.sandbox(), &cwd), + }; + Ok(DoctorReport { + checks: vec![ + check_auth_health(), + check_config_health(&config_loader, config.as_ref()), + check_workspace_health(&context), + check_sandbox_health(&context.sandbox_status), + check_system_health(&cwd, config.as_ref().ok()), + ], + }) +} + +fn run_doctor(output_format: CliOutputFormat) -> Result<(), Box> { + let report = render_doctor_report()?; + let message = report.render(); + match output_format { + CliOutputFormat::Text => println!("{message}"), + CliOutputFormat::Json => { + println!("{}", serde_json::to_string_pretty(&report.json_value())?); + } + } + if report.has_failures() { + return Err("doctor found failing checks".into()); + } + Ok(()) +} + +/// Starts a minimal Model Context Protocol server that exposes claw's +/// built-in tools over stdio. +/// +/// Tool descriptors come from [`tools::mvp_tool_specs`] and calls are +/// dispatched through [`tools::execute_tool`], so this server exposes exactly +/// the same surface the in-process agent loop uses. +fn run_mcp_serve() -> Result<(), Box> { + let tools = mvp_tool_specs() + .into_iter() + .map(|spec| McpTool { + name: spec.name.to_string(), + description: Some(spec.description.to_string()), + input_schema: Some(spec.input_schema), + annotations: None, + meta: None, + }) + .collect(); + + let spec = McpServerSpec { + server_name: "claw".to_string(), + server_version: VERSION.to_string(), + tools, + tool_handler: Box::new(execute_tool), + }; + + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()?; + runtime.block_on(async move { + let mut server = McpServer::new(spec); + server.run().await + })?; + Ok(()) +} + +#[allow(clippy::too_many_lines)] +fn check_auth_health() -> DiagnosticCheck { + let api_key_present = env::var("ANTHROPIC_API_KEY") + .ok() + .is_some_and(|value| !value.trim().is_empty()); + let auth_token_present = env::var("ANTHROPIC_AUTH_TOKEN") + .ok() + .is_some_and(|value| !value.trim().is_empty()); + + match load_oauth_credentials() { + Ok(Some(token_set)) => { + let expired = oauth_token_is_expired(&api::OAuthTokenSet { + access_token: token_set.access_token.clone(), + refresh_token: token_set.refresh_token.clone(), + expires_at: token_set.expires_at, + scopes: token_set.scopes.clone(), + }); + let mut details = vec![ + format!( + "Environment api_key={} auth_token={}", + if api_key_present { "present" } else { "absent" }, + if auth_token_present { + "present" + } else { + "absent" + } + ), + format!( + "Saved OAuth expires_at={} refresh_token={} scopes={}", + token_set + .expires_at + .map_or_else(|| "".to_string(), |value| value.to_string()), + if token_set.refresh_token.is_some() { + "present" + } else { + "absent" + }, + if token_set.scopes.is_empty() { + "".to_string() + } else { + token_set.scopes.join(",") + } + ), + ]; + if expired { + details.push( + "Suggested action claw login to refresh local OAuth credentials".to_string(), + ); + } + DiagnosticCheck::new( + "Auth", + if expired { + DiagnosticLevel::Warn + } else { + DiagnosticLevel::Ok + }, + if expired { + "saved OAuth credentials are present but expired" + } else if api_key_present || auth_token_present { + "environment and saved credentials are available" + } else { + "saved OAuth credentials are available" + }, + ) + .with_details(details) + .with_data(Map::from_iter([ + ("api_key_present".to_string(), json!(api_key_present)), + ("auth_token_present".to_string(), json!(auth_token_present)), + ("saved_oauth_present".to_string(), json!(true)), + ("saved_oauth_expired".to_string(), json!(expired)), + ( + "saved_oauth_expires_at".to_string(), + json!(token_set.expires_at), + ), + ( + "refresh_token_present".to_string(), + json!(token_set.refresh_token.is_some()), + ), + ("scopes".to_string(), json!(token_set.scopes)), + ])) + } + Ok(None) => DiagnosticCheck::new( + "Auth", + if api_key_present || auth_token_present { + DiagnosticLevel::Ok + } else { + DiagnosticLevel::Warn + }, + if api_key_present || auth_token_present { + "environment credentials are configured" + } else { + "no API key or saved OAuth credentials were found" + }, + ) + .with_details(vec![format!( + "Environment api_key={} auth_token={}", + if api_key_present { "present" } else { "absent" }, + if auth_token_present { + "present" + } else { + "absent" + } + )]) + .with_data(Map::from_iter([ + ("api_key_present".to_string(), json!(api_key_present)), + ("auth_token_present".to_string(), json!(auth_token_present)), + ("saved_oauth_present".to_string(), json!(false)), + ("saved_oauth_expired".to_string(), json!(false)), + ("saved_oauth_expires_at".to_string(), Value::Null), + ("refresh_token_present".to_string(), json!(false)), + ("scopes".to_string(), json!(Vec::::new())), + ])), + Err(error) => DiagnosticCheck::new( + "Auth", + DiagnosticLevel::Fail, + format!("failed to inspect saved credentials: {error}"), + ) + .with_data(Map::from_iter([ + ("api_key_present".to_string(), json!(api_key_present)), + ("auth_token_present".to_string(), json!(auth_token_present)), + ("saved_oauth_present".to_string(), Value::Null), + ("saved_oauth_expired".to_string(), Value::Null), + ("saved_oauth_expires_at".to_string(), Value::Null), + ("refresh_token_present".to_string(), Value::Null), + ("scopes".to_string(), Value::Null), + ("saved_oauth_error".to_string(), json!(error.to_string())), + ])), + } +} + +fn check_config_health( + config_loader: &ConfigLoader, + config: Result<&runtime::RuntimeConfig, &runtime::ConfigError>, +) -> DiagnosticCheck { + let discovered = config_loader.discover(); + let discovered_count = discovered.len(); + let discovered_paths = discovered + .iter() + .map(|entry| entry.path.display().to_string()) + .collect::>(); + match config { + Ok(runtime_config) => { + let loaded_entries = runtime_config.loaded_entries(); + let mut details = vec![format!( + "Config files loaded {}/{}", + loaded_entries.len(), + discovered_count + )]; + if let Some(model) = runtime_config.model() { + details.push(format!("Resolved model {model}")); + } + details.push(format!( + "MCP servers {}", + runtime_config.mcp().servers().len() + )); + if discovered_paths.is_empty() { + details.push("Discovered files ".to_string()); + } else { + details.extend( + discovered_paths + .iter() + .map(|path| format!("Discovered file {path}")), + ); + } + DiagnosticCheck::new( + "Config", + if discovered_count == 0 { + DiagnosticLevel::Warn + } else { + DiagnosticLevel::Ok + }, + if discovered_count == 0 { + "no config files were found; defaults are active" + } else { + "runtime config loaded successfully" + }, + ) + .with_details(details) + .with_data(Map::from_iter([ + ("discovered_files".to_string(), json!(discovered_paths)), + ( + "discovered_files_count".to_string(), + json!(discovered_count), + ), + ( + "loaded_config_files".to_string(), + json!(loaded_entries.len()), + ), + ("resolved_model".to_string(), json!(runtime_config.model())), + ( + "mcp_servers".to_string(), + json!(runtime_config.mcp().servers().len()), + ), + ])) + } + Err(error) => DiagnosticCheck::new( + "Config", + DiagnosticLevel::Fail, + format!("runtime config failed to load: {error}"), + ) + .with_details(if discovered_paths.is_empty() { + vec!["Discovered files ".to_string()] + } else { + discovered_paths + .iter() + .map(|path| format!("Discovered file {path}")) + .collect() + }) + .with_data(Map::from_iter([ + ("discovered_files".to_string(), json!(discovered_paths)), + ( + "discovered_files_count".to_string(), + json!(discovered_count), + ), + ("loaded_config_files".to_string(), json!(0)), + ("resolved_model".to_string(), Value::Null), + ("mcp_servers".to_string(), Value::Null), + ("load_error".to_string(), json!(error.to_string())), + ])), + } +} + +fn check_workspace_health(context: &StatusContext) -> DiagnosticCheck { + let in_repo = context.project_root.is_some(); + DiagnosticCheck::new( + "Workspace", + if in_repo { + DiagnosticLevel::Ok + } else { + DiagnosticLevel::Warn + }, + if in_repo { + format!( + "project root detected on branch {}", + context.git_branch.as_deref().unwrap_or("unknown") + ) + } else { + "current directory is not inside a git project".to_string() + }, + ) + .with_details(vec![ + format!("Cwd {}", context.cwd.display()), + format!( + "Project root {}", + context + .project_root + .as_ref() + .map_or_else(|| "".to_string(), |path| path.display().to_string()) + ), + format!( + "Git branch {}", + context.git_branch.as_deref().unwrap_or("unknown") + ), + format!("Git state {}", context.git_summary.headline()), + format!("Changed files {}", context.git_summary.changed_files), + format!( + "Memory files {} · config files loaded {}/{}", + context.memory_file_count, context.loaded_config_files, context.discovered_config_files + ), + ]) + .with_data(Map::from_iter([ + ("cwd".to_string(), json!(context.cwd.display().to_string())), + ( + "project_root".to_string(), + json!(context + .project_root + .as_ref() + .map(|path| path.display().to_string())), + ), + ("in_git_repo".to_string(), json!(in_repo)), + ("git_branch".to_string(), json!(context.git_branch)), + ( + "git_state".to_string(), + json!(context.git_summary.headline()), + ), + ( + "changed_files".to_string(), + json!(context.git_summary.changed_files), + ), + ( + "memory_file_count".to_string(), + json!(context.memory_file_count), + ), + ( + "loaded_config_files".to_string(), + json!(context.loaded_config_files), + ), + ( + "discovered_config_files".to_string(), + json!(context.discovered_config_files), + ), + ])) +} + +fn check_sandbox_health(status: &runtime::SandboxStatus) -> DiagnosticCheck { + let degraded = status.enabled && !status.active; + let mut details = vec![ + format!("Enabled {}", status.enabled), + format!("Active {}", status.active), + format!("Supported {}", status.supported), + format!("Filesystem mode {}", status.filesystem_mode.as_str()), + format!("Filesystem live {}", status.filesystem_active), + ]; + if let Some(reason) = &status.fallback_reason { + details.push(format!("Fallback reason {reason}")); + } + DiagnosticCheck::new( + "Sandbox", + if degraded { + DiagnosticLevel::Warn + } else { + DiagnosticLevel::Ok + }, + if degraded { + "sandbox was requested but is not currently active" + } else if status.active { + "sandbox protections are active" + } else { + "sandbox is not active for this session" + }, + ) + .with_details(details) + .with_data(Map::from_iter([ + ("enabled".to_string(), json!(status.enabled)), + ("active".to_string(), json!(status.active)), + ("supported".to_string(), json!(status.supported)), + ( + "namespace_supported".to_string(), + json!(status.namespace_supported), + ), + ( + "namespace_active".to_string(), + json!(status.namespace_active), + ), + ( + "network_supported".to_string(), + json!(status.network_supported), + ), + ("network_active".to_string(), json!(status.network_active)), + ( + "filesystem_mode".to_string(), + json!(status.filesystem_mode.as_str()), + ), + ( + "filesystem_active".to_string(), + json!(status.filesystem_active), + ), + ("allowed_mounts".to_string(), json!(status.allowed_mounts)), + ("in_container".to_string(), json!(status.in_container)), + ( + "container_markers".to_string(), + json!(status.container_markers), + ), + ("fallback_reason".to_string(), json!(status.fallback_reason)), + ])) +} + +fn check_system_health(cwd: &Path, config: Option<&runtime::RuntimeConfig>) -> DiagnosticCheck { + let default_model = config.and_then(runtime::RuntimeConfig::model); + let mut details = vec![ + format!("OS {} {}", env::consts::OS, env::consts::ARCH), + format!("Working dir {}", cwd.display()), + format!("Version {}", VERSION), + format!("Build target {}", BUILD_TARGET.unwrap_or("")), + format!("Git SHA {}", GIT_SHA.unwrap_or("")), + ]; + if let Some(model) = default_model { + details.push(format!("Default model {model}")); + } + DiagnosticCheck::new( + "System", + DiagnosticLevel::Ok, + "captured local runtime metadata", + ) + .with_details(details) + .with_data(Map::from_iter([ + ("os".to_string(), json!(env::consts::OS)), + ("arch".to_string(), json!(env::consts::ARCH)), + ("working_dir".to_string(), json!(cwd.display().to_string())), + ("version".to_string(), json!(VERSION)), + ("build_target".to_string(), json!(BUILD_TARGET)), + ("git_sha".to_string(), json!(GIT_SHA)), + ("default_model".to_string(), json!(default_model)), + ])) +} + fn resume_command_can_absorb_token(current_command: &str, token: &str) -> bool { matches!( SlashCommand::parse(current_command), @@ -765,26 +1762,54 @@ fn looks_like_slash_command_token(token: &str) -> bool { .any(|spec| spec.name == name || spec.aliases.contains(&name)) } -fn dump_manifests() { +fn dump_manifests(output_format: CliOutputFormat) -> Result<(), Box> { let workspace_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../.."); let paths = UpstreamPaths::from_workspace_dir(&workspace_dir); match extract_manifest(&paths) { Ok(manifest) => { - println!("commands: {}", manifest.commands.entries().len()); - println!("tools: {}", manifest.tools.entries().len()); - println!("bootstrap phases: {}", manifest.bootstrap.phases().len()); - } - Err(error) => { - eprintln!("failed to extract manifests: {error}"); - std::process::exit(1); + match output_format { + CliOutputFormat::Text => { + println!("commands: {}", manifest.commands.entries().len()); + println!("tools: {}", manifest.tools.entries().len()); + println!("bootstrap phases: {}", manifest.bootstrap.phases().len()); + } + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "dump-manifests", + "commands": manifest.commands.entries().len(), + "tools": manifest.tools.entries().len(), + "bootstrap_phases": manifest.bootstrap.phases().len(), + }))? + ), + } + Ok(()) } + Err(error) => Err(format!("failed to extract manifests: {error}").into()), } } -fn print_bootstrap_plan() { - for phase in runtime::BootstrapPlan::claude_code_default().phases() { - println!("- {phase:?}"); +fn print_bootstrap_plan(output_format: CliOutputFormat) -> Result<(), Box> { + let phases = runtime::BootstrapPlan::claude_code_default() + .phases() + .iter() + .map(|phase| format!("{phase:?}")) + .collect::>(); + match output_format { + CliOutputFormat::Text => { + for phase in &phases { + println!("- {phase}"); + } + } + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "bootstrap-plan", + "phases": phases, + }))? + ), } + Ok(()) } fn default_oauth_config() -> OAuthConfig { @@ -802,7 +1827,7 @@ fn default_oauth_config() -> OAuthConfig { } } -fn run_login() -> Result<(), Box> { +fn run_login(output_format: CliOutputFormat) -> Result<(), Box> { let cwd = env::current_dir()?; let config = ConfigLoader::default_for(&cwd).load()?; let default_oauth = default_oauth_config(); @@ -815,11 +1840,18 @@ fn run_login() -> Result<(), Box> { OAuthAuthorizationRequest::from_config(oauth, redirect_uri.clone(), state.clone(), &pkce) .build_url(); - println!("Starting Claude OAuth login..."); - println!("Listening for callback on {redirect_uri}"); + if output_format == CliOutputFormat::Text { + println!("Starting Claude OAuth login..."); + println!("Listening for callback on {redirect_uri}"); + } if let Err(error) = open_browser(&authorize_url) { - eprintln!("warning: failed to open browser automatically: {error}"); - println!("Open this URL manually:\n{authorize_url}"); + emit_login_browser_open_failure( + output_format, + &authorize_url, + &error, + &mut io::stdout(), + &mut io::stderr(), + )?; } let callback = wait_for_oauth_callback(callback_port)?; @@ -840,8 +1872,13 @@ fn run_login() -> Result<(), Box> { } let client = AnthropicClient::from_auth(AuthSource::None).with_base_url(api::read_base_url()); - let exchange_request = - OAuthTokenExchangeRequest::from_config(oauth, code, state, pkce.verifier, redirect_uri); + let exchange_request = OAuthTokenExchangeRequest::from_config( + oauth, + code, + state, + pkce.verifier, + redirect_uri.clone(), + ); let runtime = tokio::runtime::Runtime::new()?; let token_set = runtime.block_on(client.exchange_oauth_code(oauth, &exchange_request))?; save_oauth_credentials(&runtime::OAuthTokenSet { @@ -850,13 +1887,50 @@ fn run_login() -> Result<(), Box> { expires_at: token_set.expires_at, scopes: token_set.scopes, })?; - println!("Claude OAuth login complete."); + match output_format { + CliOutputFormat::Text => println!("Claude OAuth login complete."), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "login", + "callback_port": callback_port, + "redirect_uri": redirect_uri, + "message": "Claude OAuth login complete.", + }))? + ), + } Ok(()) } -fn run_logout() -> Result<(), Box> { +fn emit_login_browser_open_failure( + output_format: CliOutputFormat, + authorize_url: &str, + error: &io::Error, + stdout: &mut impl Write, + stderr: &mut impl Write, +) -> io::Result<()> { + writeln!( + stderr, + "warning: failed to open browser automatically: {error}" + )?; + match output_format { + CliOutputFormat::Text => writeln!(stdout, "Open this URL manually:\n{authorize_url}"), + CliOutputFormat::Json => writeln!(stderr, "Open this URL manually:\n{authorize_url}"), + } +} + +fn run_logout(output_format: CliOutputFormat) -> Result<(), Box> { clear_oauth_credentials()?; - println!("Claude OAuth credentials cleared."); + match output_format { + CliOutputFormat::Text => println!("Claude OAuth credentials cleared."), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "logout", + "message": "Claude OAuth credentials cleared.", + }))? + ), + } Ok(()) } @@ -914,21 +1988,52 @@ fn wait_for_oauth_callback( Ok(callback) } -fn print_system_prompt(cwd: PathBuf, date: String) { - match load_system_prompt(cwd, date, env::consts::OS, "unknown") { - Ok(sections) => println!("{}", sections.join("\n\n")), - Err(error) => { - eprintln!("failed to build system prompt: {error}"); - std::process::exit(1); +fn print_system_prompt( + cwd: PathBuf, + date: String, + output_format: CliOutputFormat, +) -> Result<(), Box> { + let sections = load_system_prompt(cwd, date, env::consts::OS, "unknown")?; + let message = sections.join( + " + +", + ); + match output_format { + CliOutputFormat::Text => println!("{message}"), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "system-prompt", + "message": message, + "sections": sections, + }))? + ), + } + Ok(()) +} + +fn print_version(output_format: CliOutputFormat) -> Result<(), Box> { + match output_format { + CliOutputFormat::Text => println!("{}", render_version_report()), + CliOutputFormat::Json => { + println!("{}", serde_json::to_string_pretty(&version_json_value())?); } } + Ok(()) } -fn print_version() { - println!("{}", render_version_report()); +fn version_json_value() -> serde_json::Value { + json!({ + "kind": "version", + "message": render_version_report(), + "version": VERSION, + "git_sha": GIT_SHA, + "target": BUILD_TARGET, + }) } -fn resume_session(session_path: &Path, commands: &[String]) { +fn resume_session(session_path: &Path, commands: &[String], output_format: CliOutputFormat) { let resolved_path = if session_path.exists() { session_path.to_path_buf() } else { @@ -975,9 +2080,20 @@ fn resume_session(session_path: &Path, commands: &[String]) { Ok(ResumeCommandOutcome { session: next_session, message, + json, }) => { session = next_session; - if let Some(message) = message { + if output_format == CliOutputFormat::Json { + if let Some(value) = json { + println!( + "{}", + serde_json::to_string_pretty(&value) + .expect("resume command json output") + ); + } else if let Some(message) = message { + println!("{message}"); + } + } else if let Some(message) = message { println!("{message}"); } } @@ -993,6 +2109,7 @@ fn resume_session(session_path: &Path, commands: &[String]) { struct ResumeCommandOutcome { session: Session, message: Option, + json: Option, } #[derive(Debug, Clone)] @@ -1061,14 +2178,18 @@ impl GitWorkspaceSummary { #[cfg(test)] fn format_unknown_slash_command_message(name: &str) -> String { let suggestions = suggest_slash_commands(name); - if suggestions.is_empty() { - format!("unknown slash command: /{name}. Use /help to list available commands.") - } else { - format!( - "unknown slash command: /{name}. Did you mean {}? Use /help to list available commands.", - suggestions.join(", ") - ) + let mut message = format!("unknown slash command: /{name}."); + if !suggestions.is_empty() { + message.push_str(" Did you mean "); + message.push_str(&suggestions.join(", ")); + message.push('?'); } + if let Some(note) = omc_compatibility_note_for_unknown_slash_command(name) { + message.push(' '); + message.push_str(note); + } + message.push_str(" Use /help to list available commands."); + message } fn format_model_report(model: &str, message_count: usize, turns: u32) -> String { @@ -1327,6 +2448,7 @@ fn run_resume_command( SlashCommand::Help => Ok(ResumeCommandOutcome { session: session.clone(), message: Some(render_repl_help()), + json: None, }), SlashCommand::Compact => { let result = runtime::compact_session( @@ -1343,6 +2465,7 @@ fn run_resume_command( Ok(ResumeCommandOutcome { session: result.compacted_session, message: Some(format_compact_report(removed, kept, skipped)), + json: None, }) } SlashCommand::Clear { confirm } => { @@ -1352,6 +2475,7 @@ fn run_resume_command( message: Some( "clear: confirmation required; rerun with /clear --confirm".to_string(), ), + json: None, }); } let backup_path = write_session_clear_backup(session, session_path)?; @@ -1367,11 +2491,13 @@ fn run_resume_command( backup_path.display(), session_path.display() )), + json: None, }) } SlashCommand::Status => { let tracker = UsageTracker::from_session(session); let usage = tracker.cumulative_usage(); + let context = status_context(Some(session_path))?; Ok(ResumeCommandOutcome { session: session.clone(), message: Some(format_status_report( @@ -1384,7 +2510,19 @@ fn run_resume_command( estimated_tokens: 0, }, default_permission_mode().as_str(), - &status_context(Some(session_path))?, + &context, + )), + json: Some(status_json_value( + "restored-session", + StatusUsage { + message_count: session.messages.len(), + turns: tracker.turns(), + latest: tracker.current_turn_usage(), + cumulative: usage, + estimated_tokens: 0, + }, + default_permission_mode().as_str(), + &context, )), }) } @@ -1392,12 +2530,11 @@ fn run_resume_command( let cwd = env::current_dir()?; let loader = ConfigLoader::default_for(&cwd); let runtime_config = loader.load()?; + let status = resolve_sandbox_status(runtime_config.sandbox(), &cwd); Ok(ResumeCommandOutcome { session: session.clone(), - message: Some(format_sandbox_report(&resolve_sandbox_status( - runtime_config.sandbox(), - &cwd, - ))), + message: Some(format_sandbox_report(&status)), + json: Some(sandbox_json_value(&status)), }) } SlashCommand::Cost => { @@ -1405,11 +2542,13 @@ fn run_resume_command( Ok(ResumeCommandOutcome { session: session.clone(), message: Some(format_cost_report(usage)), + json: None, }) } SlashCommand::Config { section } => Ok(ResumeCommandOutcome { session: session.clone(), message: Some(render_config_report(section.as_deref())?), + json: None, }), SlashCommand::Mcp { action, target } => { let cwd = env::current_dir()?; @@ -1422,25 +2561,33 @@ fn run_resume_command( Ok(ResumeCommandOutcome { session: session.clone(), message: Some(handle_mcp_slash_command(args.as_deref(), &cwd)?), + json: Some(handle_mcp_slash_command_json(args.as_deref(), &cwd)?), }) } SlashCommand::Memory => Ok(ResumeCommandOutcome { session: session.clone(), message: Some(render_memory_report()?), + json: None, }), - SlashCommand::Init => Ok(ResumeCommandOutcome { - session: session.clone(), - message: Some(init_claude_md()?), - }), + SlashCommand::Init => { + let message = init_claude_md()?; + Ok(ResumeCommandOutcome { + session: session.clone(), + message: Some(message.clone()), + json: Some(init_json_value(&message)), + }) + } SlashCommand::Diff => Ok(ResumeCommandOutcome { session: session.clone(), message: Some(render_diff_report_for( session_path.parent().unwrap_or_else(|| Path::new(".")), )?), + json: None, }), SlashCommand::Version => Ok(ResumeCommandOutcome { session: session.clone(), message: Some(render_version_report()), + json: Some(version_json_value()), }), SlashCommand::Export { path } => { let export_path = resolve_export_path(path.as_deref(), session)?; @@ -1452,6 +2599,7 @@ fn run_resume_command( export_path.display(), session.messages.len(), )), + json: None, }) } SlashCommand::Agents { args } => { @@ -1459,13 +2607,34 @@ fn run_resume_command( Ok(ResumeCommandOutcome { session: session.clone(), message: Some(handle_agents_slash_command(args.as_deref(), &cwd)?), + json: None, }) } SlashCommand::Skills { args } => { + if let SkillSlashDispatch::Invoke(_) = classify_skills_slash_command(args.as_deref()) { + return Err( + "resumed /skills invocations are interactive-only; start `claw` and run `/skills ` in the REPL".into(), + ); + } let cwd = env::current_dir()?; Ok(ResumeCommandOutcome { session: session.clone(), message: Some(handle_skills_slash_command(args.as_deref(), &cwd)?), + json: Some(handle_skills_slash_command_json(args.as_deref(), &cwd)?), + }) + } + SlashCommand::Doctor => Ok(ResumeCommandOutcome { + session: session.clone(), + message: Some(render_doctor_report()?.render()), + json: None, + }), + SlashCommand::History { count } => { + let limit = parse_history_count(count.as_deref()).map_err(|error| -> Box { error.into() })?; + let entries = collect_session_prompt_history(session); + Ok(ResumeCommandOutcome { + session: session.clone(), + message: Some(render_prompt_history_report(&entries, limit)), + json: None, }) } SlashCommand::Unknown(name) => Err(format_unknown_slash_command(name).into()), @@ -1481,7 +2650,6 @@ fn run_resume_command( | SlashCommand::Permissions { .. } | SlashCommand::Session { .. } | SlashCommand::Plugins { .. } - | SlashCommand::Doctor | SlashCommand::Login | SlashCommand::Logout | SlashCommand::Vim @@ -1524,15 +2692,34 @@ fn run_resume_command( } } +/// Stale-base preflight: verify the worktree HEAD matches the expected base +/// commit (from `--base-commit` flag or `.claw-base` file). Emits a warning to +/// stderr when the HEAD has diverged. +fn run_stale_base_preflight(flag_value: Option<&str>) { + let cwd = match env::current_dir() { + Ok(cwd) => cwd, + Err(_) => return, + }; + let source = resolve_expected_base(flag_value, &cwd); + let state = check_base_commit(&cwd, source.as_ref()); + if let Some(warning) = format_stale_base_warning(&state) { + eprintln!("{warning}"); + } +} + fn run_repl( model: String, allowed_tools: Option, permission_mode: PermissionMode, + base_commit: Option, ) -> Result<(), Box> { - let mut cli = LiveCli::new(model, true, allowed_tools, permission_mode)?; + run_stale_base_preflight(base_commit.as_deref()); + let resolved_model = resolve_repl_model(model); + let mut cli = LiveCli::new(resolved_model, true, allowed_tools, permission_mode)?; let mut editor = input::LineEditor::new("> ", cli.repl_completion_candidates().unwrap_or_default()); println!("{}", cli.startup_banner()); + println!("{}", format_connected_line(&cli.model)); loop { editor.set_completions(cli.repl_completion_candidates().unwrap_or_default()); @@ -1560,6 +2747,7 @@ fn run_repl( } } editor.push_history(input); + cli.record_prompt_history(&trimmed); cli.run_turn(&trimmed)?; } input::ReadOutcome::Cancel => {} @@ -1596,6 +2784,13 @@ struct LiveCli { system_prompt: Vec, runtime: BuiltRuntime, session: SessionHandle, + prompt_history: Vec, +} + +#[derive(Debug, Clone)] +struct PromptHistoryEntry { + timestamp_ms: u64, + text: String, } struct RuntimePluginState { @@ -1751,37 +2946,38 @@ impl RuntimeMcpState { .into_iter() .filter(|server_name| !failed_server_names.contains(server_name)) .collect::>(); - let failed_servers = discovery - .failed_servers - .iter() - .map(|failure| runtime::McpFailedServer { - server_name: failure.server_name.clone(), - phase: runtime::McpLifecyclePhase::ToolDiscovery, - error: runtime::McpErrorSurface::new( - runtime::McpLifecyclePhase::ToolDiscovery, - Some(failure.server_name.clone()), - failure.error.clone(), - std::collections::BTreeMap::new(), - true, - ), - }) - .chain(discovery.unsupported_servers.iter().map(|server| { - runtime::McpFailedServer { - server_name: server.server_name.clone(), - phase: runtime::McpLifecyclePhase::ServerRegistration, + let failed_servers = + discovery + .failed_servers + .iter() + .map(|failure| runtime::McpFailedServer { + server_name: failure.server_name.clone(), + phase: runtime::McpLifecyclePhase::ToolDiscovery, error: runtime::McpErrorSurface::new( - runtime::McpLifecyclePhase::ServerRegistration, - Some(server.server_name.clone()), - server.reason.clone(), - std::collections::BTreeMap::from([( - "transport".to_string(), - format!("{:?}", server.transport).to_ascii_lowercase(), - )]), - false, + runtime::McpLifecyclePhase::ToolDiscovery, + Some(failure.server_name.clone()), + failure.error.clone(), + std::collections::BTreeMap::new(), + true, ), - } - })) - .collect::>(); + }) + .chain(discovery.unsupported_servers.iter().map(|server| { + runtime::McpFailedServer { + server_name: server.server_name.clone(), + phase: runtime::McpLifecyclePhase::ServerRegistration, + error: runtime::McpErrorSurface::new( + runtime::McpLifecyclePhase::ServerRegistration, + Some(server.server_name.clone()), + server.reason.clone(), + std::collections::BTreeMap::from([( + "transport".to_string(), + format!("{:?}", server.transport).to_ascii_lowercase(), + )]), + false, + ), + } + })) + .collect::>(); let degraded_report = (!failed_servers.is_empty()).then(|| { runtime::McpDegradedReport::new( working_servers, @@ -1906,13 +3102,7 @@ impl RuntimeMcpState { fn build_runtime_mcp_state( runtime_config: &runtime::RuntimeConfig, -) -> Result< - ( - Option>>, - Vec, - ), - Box, -> { +) -> Result> { let Some((mcp_state, discovery)) = RuntimeMcpState::new(runtime_config)? else { return Ok((None, Vec::new())); }; @@ -2102,6 +3292,7 @@ impl LiveCli { system_prompt, runtime, session, + prompt_history: Vec::new(), }; cli.persist_session()?; Ok(cli) @@ -2236,13 +3427,28 @@ impl LiveCli { &mut self, input: &str, output_format: CliOutputFormat, + compact: bool, ) -> Result<(), Box> { match output_format { + CliOutputFormat::Text if compact => self.run_prompt_compact(input), CliOutputFormat::Text => self.run_turn(input), CliOutputFormat::Json => self.run_prompt_json(input), } } + fn run_prompt_compact(&mut self, input: &str) -> Result<(), Box> { + let (mut runtime, hook_abort_monitor) = self.prepare_turn_runtime(false)?; + let mut permission_prompter = CliPermissionPrompter::new(self.permission_mode); + let result = runtime.run_turn(input, Some(&mut permission_prompter)); + hook_abort_monitor.stop(); + let summary = result?; + self.replace_runtime(runtime)?; + self.persist_session()?; + let final_text = final_assistant_text(&summary); + println!("{final_text}"); + Ok(()) + } + fn run_prompt_json(&mut self, input: &str) -> Result<(), Box> { let (mut runtime, hook_abort_monitor) = self.prepare_turn_runtime(false)?; let mut permission_prompter = CliPermissionPrompter::new(self.permission_mode); @@ -2350,7 +3556,7 @@ impl LiveCli { (Some(action), Some(target)) => Some(format!("{action} {target}")), (None, Some(target)) => Some(target.to_string()), }; - Self::print_mcp(args.as_deref())?; + Self::print_mcp(args.as_deref(), CliOutputFormat::Text)?; false } SlashCommand::Memory => { @@ -2358,7 +3564,7 @@ impl LiveCli { false } SlashCommand::Init => { - run_init()?; + run_init(CliOutputFormat::Text)?; false } SlashCommand::Diff => { @@ -2366,7 +3572,7 @@ impl LiveCli { false } SlashCommand::Version => { - Self::print_version(); + Self::print_version(CliOutputFormat::Text); false } SlashCommand::Export { path } => { @@ -2380,15 +3586,27 @@ impl LiveCli { self.handle_plugins_command(action.as_deref(), target.as_deref())? } SlashCommand::Agents { args } => { - Self::print_agents(args.as_deref())?; + Self::print_agents(args.as_deref(), CliOutputFormat::Text)?; false } SlashCommand::Skills { args } => { - Self::print_skills(args.as_deref())?; + match classify_skills_slash_command(args.as_deref()) { + SkillSlashDispatch::Invoke(prompt) => self.run_turn(&prompt)?, + SkillSlashDispatch::Local => { + Self::print_skills(args.as_deref(), CliOutputFormat::Text)?; + } + } false } - SlashCommand::Doctor - | SlashCommand::Login + SlashCommand::Doctor => { + println!("{}", render_doctor_report()?.render()); + false + } + SlashCommand::History { count } => { + self.print_prompt_history(count.as_deref()); + false + } + SlashCommand::Login | SlashCommand::Logout | SlashCommand::Vim | SlashCommand::Upgrade @@ -2462,6 +3680,56 @@ impl LiveCli { ); } + fn record_prompt_history(&mut self, prompt: &str) { + let timestamp_ms = std::time::SystemTime::now() + .duration_since(UNIX_EPOCH) + .ok() + .map_or(self.runtime.session().updated_at_ms, |duration| { + u64::try_from(duration.as_millis()).unwrap_or(u64::MAX) + }); + let entry = PromptHistoryEntry { + timestamp_ms, + text: prompt.to_string(), + }; + self.prompt_history.push(entry); + if let Err(error) = self.runtime.session_mut().push_prompt_entry(prompt) { + eprintln!("warning: failed to persist prompt history: {error}"); + } + } + + fn print_prompt_history(&self, count: Option<&str>) { + let limit = match parse_history_count(count) { + Ok(limit) => limit, + Err(message) => { + eprintln!("{message}"); + return; + } + }; + let session_entries = &self.runtime.session().prompt_history; + let entries = if session_entries.is_empty() { + if self.prompt_history.is_empty() { + collect_session_prompt_history(self.runtime.session()) + } else { + self.prompt_history + .iter() + .map(|entry| PromptHistoryEntry { + timestamp_ms: entry.timestamp_ms, + text: entry.text.clone(), + }) + .collect() + } + } else { + session_entries + .iter() + .map(|entry| PromptHistoryEntry { + timestamp_ms: entry.timestamp_ms, + text: entry.text.clone(), + }) + .collect() + }; + println!("{}", render_prompt_history_report(&entries, limit)); + } + fn print_sandbox_status() { let cwd = env::current_dir().expect("current dir"); let loader = ConfigLoader::default_for(&cwd); @@ -2487,7 +3755,7 @@ impl LiveCli { return Ok(false); }; - let model = resolve_model_alias(&model).to_string(); + let model = resolve_model_alias_with_config(&model); if model == self.model { println!( @@ -2659,21 +3927,80 @@ impl LiveCli { Ok(()) } - fn print_agents(args: Option<&str>) -> Result<(), Box> { + fn print_agents( + args: Option<&str>, + output_format: CliOutputFormat, + ) -> Result<(), Box> { let cwd = env::current_dir()?; - println!("{}", handle_agents_slash_command(args, &cwd)?); + match output_format { + CliOutputFormat::Text => println!("{}", handle_agents_slash_command(args, &cwd)?), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&handle_agents_slash_command_json(args, &cwd)?)? + ), + } Ok(()) } - fn print_mcp(args: Option<&str>) -> Result<(), Box> { + fn print_mcp( + args: Option<&str>, + output_format: CliOutputFormat, + ) -> Result<(), Box> { + // `claw mcp serve` starts a stdio MCP server exposing claw's built-in + // tools. All other `mcp` subcommands fall through to the existing + // configured-server reporter (`list`, `status`, ...). + if matches!(args.map(str::trim), Some("serve")) { + return run_mcp_serve(); + } let cwd = env::current_dir()?; - println!("{}", handle_mcp_slash_command(args, &cwd)?); + match output_format { + CliOutputFormat::Text => println!("{}", handle_mcp_slash_command(args, &cwd)?), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&handle_mcp_slash_command_json(args, &cwd)?)? + ), + } Ok(()) } - fn print_skills(args: Option<&str>) -> Result<(), Box> { + fn print_skills( + args: Option<&str>, + output_format: CliOutputFormat, + ) -> Result<(), Box> { let cwd = env::current_dir()?; - println!("{}", handle_skills_slash_command(args, &cwd)?); + match output_format { + CliOutputFormat::Text => println!("{}", handle_skills_slash_command(args, &cwd)?), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&handle_skills_slash_command_json(args, &cwd)?)? + ), + } + Ok(()) + } + + fn print_plugins( + action: Option<&str>, + target: Option<&str>, + output_format: CliOutputFormat, + ) -> Result<(), Box> { + let cwd = env::current_dir()?; + let loader = ConfigLoader::default_for(&cwd); + let runtime_config = loader.load()?; + let mut manager = build_plugin_manager(&cwd, &loader, &runtime_config); + let result = handle_plugins_slash_command(action, target, &mut manager)?; + match output_format { + CliOutputFormat::Text => println!("{}", result.message), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "plugin", + "action": action.unwrap_or("list"), + "target": target, + "message": result.message, + "reload_runtime": result.reload_runtime, + }))? + ), + } Ok(()) } @@ -2682,8 +4009,8 @@ impl LiveCli { Ok(()) } - fn print_version() { - println!("{}", render_version_report()); + fn print_version(output_format: CliOutputFormat) { + let _ = crate::print_version(output_format); } fn export_session( @@ -2777,9 +4104,55 @@ impl LiveCli { ); Ok(true) } + Some("delete") => { + let Some(target) = target else { + println!("Usage: /session delete [--force]"); + return Ok(false); + }; + let handle = resolve_session_reference(target)?; + if handle.id == self.session.id { + println!( + "delete: refusing to delete the active session '{}'.\nSwitch to another session first with /session switch .", + handle.id + ); + return Ok(false); + } + if !confirm_session_deletion(&handle.id) { + println!("delete: cancelled."); + return Ok(false); + } + delete_managed_session(&handle.path)?; + println!( + "Session deleted\n Deleted session {}\n File {}", + handle.id, + handle.path.display(), + ); + Ok(false) + } + Some("delete-force") => { + let Some(target) = target else { + println!("Usage: /session delete [--force]"); + return Ok(false); + }; + let handle = resolve_session_reference(target)?; + if handle.id == self.session.id { + println!( + "delete: refusing to delete the active session '{}'.\nSwitch to another session first with /session switch .", + handle.id + ); + return Ok(false); + } + delete_managed_session(&handle.path)?; + println!( + "Session deleted\n Deleted session {}\n File {}", + handle.id, + handle.path.display(), + ); + Ok(false) + } Some(other) => { println!( - "Unknown /session action '{other}'. Use /session list, /session switch , or /session fork [branch-name]." + "Unknown /session action '{other}'. Use /session list, /session switch , /session fork [branch-name], or /session delete [--force]." ); Ok(false) } @@ -2932,9 +4305,9 @@ impl LiveCli { fn sessions_dir() -> Result> { let cwd = env::current_dir()?; - let path = cwd.join(".claw").join("sessions"); - fs::create_dir_all(&path)?; - Ok(path) + let store = runtime::SessionStore::from_cwd(&cwd) + .map_err(|e| Box::new(e) as Box)?; + Ok(store.sessions_dir().to_path_buf()) } fn create_managed_session_handle( @@ -3065,6 +4438,24 @@ fn latest_managed_session() -> Result Result<(), Box> { + if !path.exists() { + return Err(format!("session file does not exist: {}", path.display()).into()); + } + fs::remove_file(path)?; + Ok(()) +} + +fn confirm_session_deletion(session_id: &str) -> bool { + print!("Delete session '{session_id}'? This cannot be undone. [y/N]: "); + io::stdout().flush().unwrap_or(()); + let mut answer = String::new(); + if io::stdin().read_line(&mut answer).is_err() { + return false; + } + matches!(answer.trim(), "y" | "Y" | "yes" | "Yes" | "YES") +} + fn format_missing_session_reference(reference: &str) -> String { format!( "session not found: {reference}\nHint: managed sessions live in .claw/sessions/. Try `{LATEST_SESSION_REFERENCE}` for the most recent session or `/session list` in the REPL." @@ -3161,12 +4552,14 @@ fn render_repl_help() -> String { " /exit Quit the REPL".to_string(), " /quit Quit the REPL".to_string(), " Up/Down Navigate prompt history".to_string(), + " Ctrl-R Reverse-search prompt history".to_string(), " Tab Complete commands, modes, and recent sessions".to_string(), " Ctrl-C Clear input (or exit on empty prompt)".to_string(), " Shift+Enter/Ctrl+J Insert a newline".to_string(), " Auto-save .claw/sessions/.jsonl".to_string(), " Resume latest /resume latest".to_string(), " Browse sessions /session list".to_string(), + " Show prompt history /history [count]".to_string(), String::new(), render_slash_command_help(), ] @@ -3179,25 +4572,85 @@ fn render_repl_help() -> String { fn print_status_snapshot( model: &str, permission_mode: PermissionMode, + output_format: CliOutputFormat, ) -> Result<(), Box> { - println!( - "{}", - format_status_report( - model, - StatusUsage { - message_count: 0, - turns: 0, - latest: TokenUsage::default(), - cumulative: TokenUsage::default(), - estimated_tokens: 0, - }, - permission_mode.as_str(), - &status_context(None)?, - ) - ); + let usage = StatusUsage { + message_count: 0, + turns: 0, + latest: TokenUsage::default(), + cumulative: TokenUsage::default(), + estimated_tokens: 0, + }; + let context = status_context(None)?; + match output_format { + CliOutputFormat::Text => println!( + "{}", + format_status_report(model, usage, permission_mode.as_str(), &context) + ), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&status_json_value( + model, + usage, + permission_mode.as_str(), + &context, + ))? + ), + } Ok(()) } +fn status_json_value( + model: &str, + usage: StatusUsage, + permission_mode: &str, + context: &StatusContext, +) -> serde_json::Value { + json!({ + "kind": "status", + "model": model, + "permission_mode": permission_mode, + "usage": { + "messages": usage.message_count, + "turns": usage.turns, + "latest_total": usage.latest.total_tokens(), + "cumulative_input": usage.cumulative.input_tokens, + "cumulative_output": usage.cumulative.output_tokens, + "cumulative_total": usage.cumulative.total_tokens(), + "estimated_tokens": usage.estimated_tokens, + }, + "workspace": { + "cwd": context.cwd, + "project_root": context.project_root, + "git_branch": context.git_branch, + "git_state": context.git_summary.headline(), + "changed_files": context.git_summary.changed_files, + "staged_files": context.git_summary.staged_files, + "unstaged_files": context.git_summary.unstaged_files, + "untracked_files": context.git_summary.untracked_files, + "session": context.session_path.as_ref().map_or_else(|| "live-repl".to_string(), |path| path.display().to_string()), + "loaded_config_files": context.loaded_config_files, + "discovered_config_files": context.discovered_config_files, + "memory_file_count": context.memory_file_count, + }, + "sandbox": { + "enabled": context.sandbox_status.enabled, + "active": context.sandbox_status.active, + "supported": context.sandbox_status.supported, + "in_container": context.sandbox_status.in_container, + "requested_namespace": context.sandbox_status.requested.namespace_restrictions, + "active_namespace": context.sandbox_status.namespace_active, + "requested_network": context.sandbox_status.requested.network_isolation, + "active_network": context.sandbox_status.network_active, + "filesystem_mode": context.sandbox_status.filesystem_mode.as_str(), + "filesystem_active": context.sandbox_status.filesystem_active, + "allowed_mounts": context.sandbox_status.allowed_mounts, + "markers": context.sandbox_status.container_markers, + "fallback_reason": context.sandbox_status.fallback_reason, + } + }) +} + fn status_context( session_path: Option<&Path>, ) -> Result> { @@ -3358,19 +4811,71 @@ fn format_commit_skipped_report() -> String { .to_string() } -fn print_sandbox_status_snapshot() -> Result<(), Box> { +fn print_sandbox_status_snapshot( + output_format: CliOutputFormat, +) -> Result<(), Box> { let cwd = env::current_dir()?; let loader = ConfigLoader::default_for(&cwd); let runtime_config = loader .load() .unwrap_or_else(|_| runtime::RuntimeConfig::empty()); - println!( - "{}", - format_sandbox_report(&resolve_sandbox_status(runtime_config.sandbox(), &cwd)) - ); + let status = resolve_sandbox_status(runtime_config.sandbox(), &cwd); + match output_format { + CliOutputFormat::Text => println!("{}", format_sandbox_report(&status)), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&sandbox_json_value(&status))? + ), + } Ok(()) } +fn sandbox_json_value(status: &runtime::SandboxStatus) -> serde_json::Value { + json!({ + "kind": "sandbox", + "enabled": status.enabled, + "active": status.active, + "supported": status.supported, + "in_container": status.in_container, + "requested_namespace": status.requested.namespace_restrictions, + "active_namespace": status.namespace_active, + "requested_network": status.requested.network_isolation, + "active_network": status.network_active, + "filesystem_mode": status.filesystem_mode.as_str(), + "filesystem_active": status.filesystem_active, + "allowed_mounts": status.allowed_mounts, + "markers": status.container_markers, + "fallback_reason": status.fallback_reason, + }) +} + +fn render_help_topic(topic: LocalHelpTopic) -> String { + match topic { + LocalHelpTopic::Status => "Status + Usage claw status + Purpose show the local workspace snapshot without entering the REPL + Output model, permissions, git state, config files, and sandbox status + Related /status · claw --resume latest /status" + .to_string(), + LocalHelpTopic::Sandbox => "Sandbox + Usage claw sandbox + Purpose inspect the resolved sandbox and isolation state for the current directory + Output namespace, network, filesystem, and fallback details + Related /sandbox · claw status" + .to_string(), + LocalHelpTopic::Doctor => "Doctor + Usage claw doctor + Purpose diagnose local auth, config, workspace, sandbox, and build metadata + Output local-only health report; no provider request or session resume required + Related /doctor · claw --resume latest /doctor" + .to_string(), + } +} + +fn print_help_topic(topic: LocalHelpTopic) { + println!("{}", render_help_topic(topic)); +} + fn render_config_report(section: Option<&str>) -> Result> { let cwd = env::current_dir()?; let loader = ConfigLoader::default_for(&cwd); @@ -3494,11 +4999,25 @@ fn init_claude_md() -> Result> { Ok(initialize_repo(&cwd)?.render()) } -fn run_init() -> Result<(), Box> { - println!("{}", init_claude_md()?); +fn run_init(output_format: CliOutputFormat) -> Result<(), Box> { + let message = init_claude_md()?; + match output_format { + CliOutputFormat::Text => println!("{message}"), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&init_json_value(&message))? + ), + } Ok(()) } +fn init_json_value(message: &str) -> serde_json::Value { + json!({ + "kind": "init", + "message": message, + }) +} + fn normalize_permission_mode(mode: &str) -> Option<&'static str> { match mode.trim() { "read-only" => Some("read-only"), @@ -3756,6 +5275,110 @@ fn write_temp_text_file( Ok(path) } +const DEFAULT_HISTORY_LIMIT: usize = 20; + +fn parse_history_count(raw: Option<&str>) -> Result { + let Some(raw) = raw else { + return Ok(DEFAULT_HISTORY_LIMIT); + }; + let parsed: usize = raw + .parse() + .map_err(|_| format!("history: invalid count '{raw}'. Expected a positive integer."))?; + if parsed == 0 { + return Err("history: count must be greater than 0.".to_string()); + } + Ok(parsed) +} + +fn format_history_timestamp(timestamp_ms: u64) -> String { + let secs = timestamp_ms / 1_000; + let subsec_ms = timestamp_ms % 1_000; + let days_since_epoch = secs / 86_400; + let seconds_of_day = secs % 86_400; + let hours = seconds_of_day / 3_600; + let minutes = (seconds_of_day % 3_600) / 60; + let seconds = seconds_of_day % 60; + + let (year, month, day) = civil_from_days(i64::try_from(days_since_epoch).unwrap_or(0)); + format!( + "{year:04}-{month:02}-{day:02}T{hours:02}:{minutes:02}:{seconds:02}.{subsec_ms:03}Z" + ) +} + +// Computes civil (Gregorian) year/month/day from days since the Unix epoch +// (1970-01-01) using Howard Hinnant's `civil_from_days` algorithm. +fn civil_from_days(days: i64) -> (i32, u32, u32) { + let z = days + 719_468; + let era = if z >= 0 { z / 146_097 } else { (z - 146_096) / 146_097 }; + let doe = (z - era * 146_097) as u64; // [0, 146_096] + let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399] + let y = yoe as i64 + era * 400; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365] + let mp = (5 * doy + 2) / 153; // [0, 11] + let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31] + let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12] + let y = y + i64::from(m <= 2); + (y as i32, m as u32, d as u32) +} + +fn render_prompt_history_report(entries: &[PromptHistoryEntry], limit: usize) -> String { + if entries.is_empty() { + return "Prompt history\n Result no prompts recorded yet".to_string(); + } + + let total = entries.len(); + let start = total.saturating_sub(limit); + let shown = &entries[start..]; + let mut lines = vec![ + "Prompt history".to_string(), + format!(" Total {total}"), + format!(" Showing {} most recent", shown.len()), + format!(" Reverse search Ctrl-R in the REPL"), + String::new(), + ]; + for (offset, entry) in shown.iter().enumerate() { + let absolute_index = start + offset + 1; + let timestamp = format_history_timestamp(entry.timestamp_ms); + let first_line = entry.text.lines().next().unwrap_or("").trim(); + let display = if first_line.chars().count() > 80 { + let truncated: String = first_line.chars().take(77).collect(); + format!("{truncated}...") + } else { + first_line.to_string() + }; + lines.push(format!(" {absolute_index:>3}. [{timestamp}] {display}")); + } + lines.join("\n") +} + +fn collect_session_prompt_history(session: &Session) -> Vec { + if !session.prompt_history.is_empty() { + return session + .prompt_history + .iter() + .map(|entry| PromptHistoryEntry { + timestamp_ms: entry.timestamp_ms, + text: entry.text.clone(), + }) + .collect(); + } + let timestamp_ms = session.updated_at_ms; + session + .messages + .iter() + .filter(|message| message.role == MessageRole::User) + .filter_map(|message| { + message.blocks.iter().find_map(|block| match block { + ContentBlock::Text { text } => Some(PromptHistoryEntry { + timestamp_ms, + text: text.clone(), + }), + _ => None, + }) + }) + .collect() +} + fn recent_user_context(session: &Session, limit: usize) -> String { let requests = session .messages @@ -3902,6 +5525,172 @@ fn resolve_export_path( Ok(cwd.join(final_name)) } +const SESSION_MARKDOWN_TOOL_SUMMARY_LIMIT: usize = 280; + +fn summarize_tool_payload_for_markdown(payload: &str) -> String { + let compact = match serde_json::from_str::(payload) { + Ok(value) => value.to_string(), + Err(_) => payload.split_whitespace().collect::>().join(" "), + }; + if compact.is_empty() { + return String::new(); + } + truncate_for_summary(&compact, SESSION_MARKDOWN_TOOL_SUMMARY_LIMIT) +} + +fn run_export( + session_reference: &str, + output_path: Option<&Path>, + output_format: CliOutputFormat, +) -> Result<(), Box> { + let handle = resolve_session_reference(session_reference)?; + let session = Session::load_from_path(&handle.path)?; + let markdown = render_session_markdown(&session, &handle.id, &handle.path); + + if let Some(path) = output_path { + fs::write(path, &markdown)?; + let report = format!( + "Export\n Result wrote markdown transcript\n File {}\n Session {}\n Messages {}", + path.display(), + handle.id, + session.messages.len(), + ); + match output_format { + CliOutputFormat::Text => println!("{report}"), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "export", + "message": report, + "session_id": handle.id, + "file": path.display().to_string(), + "messages": session.messages.len(), + }))? + ), + } + return Ok(()); + } + + match output_format { + CliOutputFormat::Text => { + print!("{markdown}"); + if !markdown.ends_with('\n') { + println!(); + } + } + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "export", + "session_id": handle.id, + "file": handle.path.display().to_string(), + "messages": session.messages.len(), + "markdown": markdown, + }))? + ), + } + Ok(()) +} + +fn render_session_markdown(session: &Session, session_id: &str, session_path: &Path) -> String { + let mut lines = vec![ + "# Conversation Export".to_string(), + String::new(), + format!("- **Session**: `{session_id}`"), + format!("- **File**: `{}`", session_path.display()), + format!("- **Messages**: {}", session.messages.len()), + ]; + if let Some(workspace_root) = session.workspace_root() { + lines.push(format!("- **Workspace**: `{}`", workspace_root.display())); + } + if let Some(fork) = &session.fork { + let branch = fork.branch_name.as_deref().unwrap_or("(unnamed)"); + lines.push(format!( + "- **Forked from**: `{}` (branch `{branch}`)", + fork.parent_session_id + )); + } + if let Some(compaction) = &session.compaction { + lines.push(format!( + "- **Compactions**: {} (last removed {} messages)", + compaction.count, compaction.removed_message_count + )); + } + lines.push(String::new()); + lines.push("---".to_string()); + lines.push(String::new()); + + for (index, message) in session.messages.iter().enumerate() { + let role = match message.role { + MessageRole::System => "System", + MessageRole::User => "User", + MessageRole::Assistant => "Assistant", + MessageRole::Tool => "Tool", + }; + lines.push(format!("## {}. {role}", index + 1)); + lines.push(String::new()); + for block in &message.blocks { + match block { + ContentBlock::Text { text } => { + let trimmed = text.trim_end(); + if !trimmed.is_empty() { + lines.push(trimmed.to_string()); + lines.push(String::new()); + } + } + ContentBlock::ToolUse { id, name, input } => { + lines.push(format!( + "**Tool call** `{name}` _(id `{}`)_", + short_tool_id(id) + )); + let summary = summarize_tool_payload_for_markdown(input); + if !summary.is_empty() { + lines.push(format!("> {summary}")); + } + lines.push(String::new()); + } + ContentBlock::ToolResult { + tool_use_id, + tool_name, + output, + is_error, + } => { + let status = if *is_error { "error" } else { "ok" }; + lines.push(format!( + "**Tool result** `{tool_name}` _(id `{}`, {status})_", + short_tool_id(tool_use_id) + )); + let summary = summarize_tool_payload_for_markdown(output); + if !summary.is_empty() { + lines.push(format!("> {summary}")); + } + lines.push(String::new()); + } + } + } + if let Some(usage) = message.usage { + lines.push(format!( + "_tokens: in={} out={} cache_create={} cache_read={}_", + usage.input_tokens, + usage.output_tokens, + usage.cache_creation_input_tokens, + usage.cache_read_input_tokens, + )); + lines.push(String::new()); + } + } + lines.join("\n") +} + +fn short_tool_id(id: &str) -> String { + let char_count = id.chars().count(); + if char_count <= 12 { + return id.to_string(); + } + let prefix: String = id.chars().take(12).collect(); + format!("{prefix}…") +} + fn build_system_prompt() -> Result, Box> { Ok(load_system_prompt( env::current_dir()?, @@ -4477,6 +6266,7 @@ impl runtime::PermissionPrompter for CliPermissionPrompter { struct AnthropicRuntimeClient { runtime: tokio::runtime::Runtime, client: AnthropicClient, + session_id: String, model: String, enable_tools: bool, emit_output: bool, @@ -4500,6 +6290,7 @@ impl AnthropicRuntimeClient { client: AnthropicClient::from_auth(resolve_cli_auth_source()?) .with_base_url(api::read_base_url()) .with_prompt_cache(PromptCache::new(session_id)), + session_id: session_id.to_string(), model, enable_tools, emit_output, @@ -4511,13 +6302,29 @@ impl AnthropicRuntimeClient { } fn resolve_cli_auth_source() -> Result> { - Ok(resolve_startup_auth_source(|| { - let cwd = env::current_dir().map_err(api::ApiError::from)?; - let config = ConfigLoader::default_for(&cwd).load().map_err(|error| { - api::ApiError::Auth(format!("failed to load runtime OAuth config: {error}")) - })?; - Ok(config.oauth().cloned()) - })?) + let cwd = env::current_dir()?; + Ok(resolve_cli_auth_source_for_cwd(&cwd, default_oauth_config)?) +} + +fn resolve_cli_auth_source_for_cwd( + cwd: &Path, + default_oauth: F, +) -> Result +where + F: FnOnce() -> OAuthConfig, +{ + resolve_startup_auth_source(|| { + Ok(Some( + load_runtime_oauth_config_for(cwd)?.unwrap_or_else(default_oauth), + )) + }) +} + +fn load_runtime_oauth_config_for(cwd: &Path) -> Result, api::ApiError> { + let config = ConfigLoader::default_for(cwd).load().map_err(|error| { + api::ApiError::Auth(format!("failed to load runtime OAuth config: {error}")) + })?; + Ok(config.oauth().cloned()) } impl ApiClient for AnthropicRuntimeClient { @@ -4526,6 +6333,7 @@ impl ApiClient for AnthropicRuntimeClient { if let Some(progress_reporter) = &self.progress_reporter { progress_reporter.mark_model_phase(); } + let is_post_tool = request_ends_with_tool_result(&request); let message_request = MessageRequest { model: self.model.clone(), max_tokens: max_tokens_for_model(&self.model), @@ -4539,131 +6347,302 @@ impl ApiClient for AnthropicRuntimeClient { }; self.runtime.block_on(async { - let mut stream = self - .client - .stream_message(&message_request) - .await - .map_err(|error| RuntimeError::new(error.to_string()))?; - let mut stdout = io::stdout(); - let mut sink = io::sink(); - let out: &mut dyn Write = if self.emit_output { - &mut stdout - } else { - &mut sink - }; - let renderer = TerminalRenderer::new(); - let mut markdown_stream = MarkdownStreamState::default(); - let mut events = Vec::new(); - let mut pending_tool: Option<(String, String, String)> = None; - let mut saw_stop = false; + // When resuming after tool execution, apply a stall timeout on the + // first stream event. If the model does not respond within the + // deadline we drop the stalled connection and re-send the request as + // a continuation nudge (one retry only). + let max_attempts: usize = if is_post_tool { 2 } else { 1 }; - while let Some(event) = stream - .next_event() - .await - .map_err(|error| RuntimeError::new(error.to_string()))? - { - match event { - ApiStreamEvent::MessageStart(start) => { - for block in start.message.content { - push_output_block(block, out, &mut events, &mut pending_tool, true)?; - } + for attempt in 1..=max_attempts { + let result = self + .consume_stream(&message_request, is_post_tool && attempt == 1) + .await; + match result { + Ok(events) => return Ok(events), + Err(error) if error.to_string().contains("post-tool stall") && attempt < max_attempts => { + // Stalled after tool completion — nudge the model by + // re-sending the same request. + continue; } - ApiStreamEvent::ContentBlockStart(start) => { + Err(error) => return Err(error), + } + } + + Err(RuntimeError::new( + "post-tool continuation nudge exhausted", + )) + }) + } +} + +impl AnthropicRuntimeClient { + /// Consume a single streaming response, optionally applying a stall + /// timeout on the first event for post-tool continuations. + #[allow(clippy::too_many_lines)] + async fn consume_stream( + &self, + message_request: &MessageRequest, + apply_stall_timeout: bool, + ) -> Result, RuntimeError> { + let mut stream = + self.client + .stream_message(message_request) + .await + .map_err(|error| { + RuntimeError::new(format_user_visible_api_error(&self.session_id, &error)) + })?; + let mut stdout = io::stdout(); + let mut sink = io::sink(); + let out: &mut dyn Write = if self.emit_output { + &mut stdout + } else { + &mut sink + }; + let renderer = TerminalRenderer::new(); + let mut markdown_stream = MarkdownStreamState::default(); + let mut events = Vec::new(); + let mut pending_tool: Option<(String, String, String)> = None; + let mut block_has_thinking_summary = false; + let mut saw_stop = false; + let mut received_any_event = false; + + loop { + let next = if apply_stall_timeout && !received_any_event { + match tokio::time::timeout(POST_TOOL_STALL_TIMEOUT, stream.next_event()).await { + Ok(inner) => inner.map_err(|error| { + RuntimeError::new(format_user_visible_api_error( + &self.session_id, + &error, + )) + })?, + Err(_elapsed) => { + return Err(RuntimeError::new( + "post-tool stall: model did not respond within timeout", + )); + } + } + } else { + stream.next_event().await.map_err(|error| { + RuntimeError::new(format_user_visible_api_error(&self.session_id, &error)) + })? + }; + + let Some(event) = next else { + break; + }; + received_any_event = true; + + match event { + ApiStreamEvent::MessageStart(start) => { + for block in start.message.content { push_output_block( - start.content_block, + block, out, &mut events, &mut pending_tool, true, + &mut block_has_thinking_summary, )?; } - ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta { - ContentBlockDelta::TextDelta { text } => { - if !text.is_empty() { - if let Some(progress_reporter) = &self.progress_reporter { - progress_reporter.mark_text_phase(&text); - } - if let Some(rendered) = markdown_stream.push(&renderer, &text) { - write!(out, "{rendered}") - .and_then(|()| out.flush()) - .map_err(|error| RuntimeError::new(error.to_string()))?; - } - events.push(AssistantEvent::TextDelta(text)); - } - } - ContentBlockDelta::InputJsonDelta { partial_json } => { - if let Some((_, _, input)) = &mut pending_tool { - input.push_str(&partial_json); - } - } - ContentBlockDelta::ThinkingDelta { .. } - | ContentBlockDelta::SignatureDelta { .. } => {} - }, - ApiStreamEvent::ContentBlockStop(_) => { - if let Some(rendered) = markdown_stream.flush(&renderer) { - write!(out, "{rendered}") - .and_then(|()| out.flush()) - .map_err(|error| RuntimeError::new(error.to_string()))?; - } - if let Some((id, name, input)) = pending_tool.take() { + } + ApiStreamEvent::ContentBlockStart(start) => { + push_output_block( + start.content_block, + out, + &mut events, + &mut pending_tool, + true, + &mut block_has_thinking_summary, + )?; + } + ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta { + ContentBlockDelta::TextDelta { text } => { + if !text.is_empty() { if let Some(progress_reporter) = &self.progress_reporter { - progress_reporter.mark_tool_phase(&name, &input); + progress_reporter.mark_text_phase(&text); } - // Display tool call now that input is fully accumulated - writeln!(out, "\n{}", format_tool_call_start(&name, &input)) - .and_then(|()| out.flush()) - .map_err(|error| RuntimeError::new(error.to_string()))?; - events.push(AssistantEvent::ToolUse { id, name, input }); + if let Some(rendered) = markdown_stream.push(&renderer, &text) { + write!(out, "{rendered}") + .and_then(|()| out.flush()) + .map_err(|error| RuntimeError::new(error.to_string()))?; + } + events.push(AssistantEvent::TextDelta(text)); } } - ApiStreamEvent::MessageDelta(delta) => { - events.push(AssistantEvent::Usage(delta.usage.token_usage())); - } - ApiStreamEvent::MessageStop(_) => { - saw_stop = true; - if let Some(rendered) = markdown_stream.flush(&renderer) { - write!(out, "{rendered}") - .and_then(|()| out.flush()) - .map_err(|error| RuntimeError::new(error.to_string()))?; + ContentBlockDelta::InputJsonDelta { partial_json } => { + if let Some((_, _, input)) = &mut pending_tool { + input.push_str(&partial_json); } - events.push(AssistantEvent::MessageStop); + } + ContentBlockDelta::ThinkingDelta { .. } => { + if !block_has_thinking_summary { + render_thinking_block_summary(out, None, false)?; + block_has_thinking_summary = true; + } + } + ContentBlockDelta::SignatureDelta { .. } => {} + }, + ApiStreamEvent::ContentBlockStop(_) => { + block_has_thinking_summary = false; + if let Some(rendered) = markdown_stream.flush(&renderer) { + write!(out, "{rendered}") + .and_then(|()| out.flush()) + .map_err(|error| RuntimeError::new(error.to_string()))?; + } + if let Some((id, name, input)) = pending_tool.take() { + if let Some(progress_reporter) = &self.progress_reporter { + progress_reporter.mark_tool_phase(&name, &input); + } + // Display tool call now that input is fully accumulated + writeln!(out, "\n{}", format_tool_call_start(&name, &input)) + .and_then(|()| out.flush()) + .map_err(|error| RuntimeError::new(error.to_string()))?; + events.push(AssistantEvent::ToolUse { id, name, input }); } } + ApiStreamEvent::MessageDelta(delta) => { + events.push(AssistantEvent::Usage(delta.usage.token_usage())); + } + ApiStreamEvent::MessageStop(_) => { + saw_stop = true; + if let Some(rendered) = markdown_stream.flush(&renderer) { + write!(out, "{rendered}") + .and_then(|()| out.flush()) + .map_err(|error| RuntimeError::new(error.to_string()))?; + } + events.push(AssistantEvent::MessageStop); + } } + } - push_prompt_cache_record(&self.client, &mut events); + push_prompt_cache_record(&self.client, &mut events); - if !saw_stop - && events.iter().any(|event| { - matches!(event, AssistantEvent::TextDelta(text) if !text.is_empty()) - || matches!(event, AssistantEvent::ToolUse { .. }) - }) - { - events.push(AssistantEvent::MessageStop); - } + if !saw_stop + && events.iter().any(|event| { + matches!(event, AssistantEvent::TextDelta(text) if !text.is_empty()) + || matches!(event, AssistantEvent::ToolUse { .. }) + }) + { + events.push(AssistantEvent::MessageStop); + } - if events - .iter() - .any(|event| matches!(event, AssistantEvent::MessageStop)) - { - return Ok(events); - } + if events + .iter() + .any(|event| matches!(event, AssistantEvent::MessageStop)) + { + return Ok(events); + } - let response = self - .client - .send_message(&MessageRequest { - stream: false, - ..message_request.clone() - }) - .await - .map_err(|error| RuntimeError::new(error.to_string()))?; - let mut events = response_to_events(response, out)?; - push_prompt_cache_record(&self.client, &mut events); - Ok(events) - }) + let response = self + .client + .send_message(&MessageRequest { + stream: false, + ..message_request.clone() + }) + .await + .map_err(|error| { + RuntimeError::new(format_user_visible_api_error(&self.session_id, &error)) + })?; + let mut events = response_to_events(response, out)?; + push_prompt_cache_record(&self.client, &mut events); + Ok(events) } } +/// Returns `true` when the conversation ends with a tool-result message, +/// meaning the model is expected to continue after tool execution. +fn request_ends_with_tool_result(request: &ApiRequest) -> bool { + request + .messages + .last() + .is_some_and(|message| message.role == MessageRole::Tool) +} + +fn format_user_visible_api_error(session_id: &str, error: &api::ApiError) -> String { + if error.is_context_window_failure() { + format_context_window_blocked_error(session_id, error) + } else if error.is_generic_fatal_wrapper() { + let mut qualifiers = vec![format!("session {session_id}")]; + if let Some(request_id) = error.request_id() { + qualifiers.push(format!("trace {request_id}")); + } + format!( + "{} ({}): {}", + error.safe_failure_class(), + qualifiers.join(", "), + error + ) + } else { + error.to_string() + } +} + +fn format_context_window_blocked_error(session_id: &str, error: &api::ApiError) -> String { + let mut lines = vec![ + "Context window blocked".to_string(), + " Failure class context_window_blocked".to_string(), + format!(" Session {session_id}"), + ]; + + if let Some(request_id) = error.request_id() { + lines.push(format!(" Trace {request_id}")); + } + + match error { + api::ApiError::ContextWindowExceeded { + model, + estimated_input_tokens, + requested_output_tokens, + estimated_total_tokens, + context_window_tokens, + } => { + lines.push(format!(" Model {model}")); + lines.push(format!(" Input estimate ~{estimated_input_tokens} tokens (heuristic)")); + lines.push(format!(" Requested output {requested_output_tokens} tokens")); + lines.push(format!(" Total estimate ~{estimated_total_tokens} tokens (heuristic)")); + lines.push(format!(" Context window {context_window_tokens} tokens")); + } + api::ApiError::Api { message, body, .. } => { + let detail = message.as_deref().unwrap_or(body).trim(); + if !detail.is_empty() { + lines.push(format!( + " Detail {}", + truncate_for_summary(detail, 120) + )); + } + } + api::ApiError::RetriesExhausted { last_error, .. } => { + let detail = match last_error.as_ref() { + api::ApiError::Api { message, body, .. } => message.as_deref().unwrap_or(body), + other => return format_context_window_blocked_error(session_id, other), + } + .trim(); + if !detail.is_empty() { + lines.push(format!( + " Detail {}", + truncate_for_summary(detail, 120) + )); + } + } + _ => {} + } + + lines.push(String::new()); + lines.push("Recovery".to_string()); + lines.push(" Compact /compact".to_string()); + lines.push(format!( + " Resume compact claw --resume {session_id} /compact" + )); + lines.push(" Fresh session /clear --confirm".to_string()); + lines.push( + " Reduce scope remove large pasted context/files or ask for a smaller slice" + .to_string(), + ); + lines.push(" Retry rerun after compacting or reducing the request".to_string()); + + lines.join("\n") +} + fn final_assistant_text(summary: &runtime::TurnSummary) -> String { summary .assistant_messages @@ -5247,12 +7226,30 @@ fn truncate_output_for_display(content: &str, max_lines: usize, max_chars: usize preview } +fn render_thinking_block_summary( + out: &mut (impl Write + ?Sized), + char_count: Option, + redacted: bool, +) -> Result<(), RuntimeError> { + let summary = if redacted { + "\n▶ Thinking block hidden by provider\n".to_string() + } else if let Some(char_count) = char_count { + format!("\n▶ Thinking ({char_count} chars hidden)\n") + } else { + "\n▶ Thinking hidden\n".to_string() + }; + write!(out, "{summary}") + .and_then(|()| out.flush()) + .map_err(|error| RuntimeError::new(error.to_string())) +} + fn push_output_block( block: OutputContentBlock, out: &mut (impl Write + ?Sized), events: &mut Vec, pending_tool: &mut Option<(String, String, String)>, streaming_tool_input: bool, + block_has_thinking_summary: &mut bool, ) -> Result<(), RuntimeError> { match block { OutputContentBlock::Text { text } => { @@ -5278,7 +7275,14 @@ fn push_output_block( }; *pending_tool = Some((id, name, initial_input)); } - OutputContentBlock::Thinking { .. } | OutputContentBlock::RedactedThinking { .. } => {} + OutputContentBlock::Thinking { thinking, .. } => { + render_thinking_block_summary(out, Some(thinking.chars().count()), false)?; + *block_has_thinking_summary = true; + } + OutputContentBlock::RedactedThinking { .. } => { + render_thinking_block_summary(out, None, true)?; + *block_has_thinking_summary = true; + } } Ok(()) } @@ -5291,7 +7295,15 @@ fn response_to_events( let mut pending_tool = None; for block in response.content { - push_output_block(block, out, &mut events, &mut pending_tool, false)?; + let mut block_has_thinking_summary = false; + push_output_block( + block, + out, + &mut events, + &mut pending_tool, + false, + &mut block_has_thinking_summary, + )?; if let Some((id, name, input)) = pending_tool.take() { events.push(AssistantEvent::ToolUse { id, name, input }); } @@ -5350,16 +7362,13 @@ impl CliToolExecutor { fn execute_search_tool(&self, value: serde_json::Value) -> Result { let input: ToolSearchRequest = serde_json::from_value(value) .map_err(|error| ToolError::new(format!("invalid tool input JSON: {error}")))?; - let (pending_mcp_servers, mcp_degraded) = self - .mcp_state - .as_ref() - .map(|state| { + let (pending_mcp_servers, mcp_degraded) = + self.mcp_state.as_ref().map_or((None, None), |state| { let state = state .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); (state.pending_servers(), state.degraded_report()) - }) - .unwrap_or((None, None)); + }); serde_json::to_string_pretty(&self.tool_registry.search( &input.query, input.max_results.unwrap_or(5), @@ -5549,6 +7558,11 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> { )?; writeln!(out, " claw sandbox")?; writeln!(out, " Show the current sandbox isolation snapshot")?; + writeln!(out, " claw doctor")?; + writeln!( + out, + " Diagnose local auth, config, workspace, and sandbox health" + )?; writeln!(out, " claw dump-manifests")?; writeln!(out, " claw bootstrap-plan")?; writeln!(out, " claw agents")?; @@ -5558,6 +7572,11 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> { writeln!(out, " claw login")?; writeln!(out, " claw logout")?; writeln!(out, " claw init")?; + writeln!(out, " claw export [PATH] [--session SESSION] [--output PATH]")?; + writeln!( + out, + " Dump the latest (or named) session as markdown; writes to PATH or stdout" + )?; writeln!(out)?; writeln!(out, "Flags:")?; writeln!( @@ -5568,6 +7587,10 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> { out, " --output-format FORMAT Non-interactive output format: text or json" )?; + writeln!( + out, + " --compact Strip tool call details; print only the final assistant text (text mode only; useful for piping)" + )?; writeln!( out, " --permission-mode MODE Set read-only, workspace-write, or danger-full-access" @@ -5614,6 +7637,10 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> { out, " claw --output-format json prompt \"explain src/main.rs\"" )?; + writeln!( + out, + " claw --compact \"summarize Cargo.toml\" | wc -l" + )?; writeln!( out, " claw --allowedTools read,glob \"summarize Cargo.toml\"" @@ -5626,50 +7653,73 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> { writeln!(out, " claw agents")?; writeln!(out, " claw mcp show my-server")?; writeln!(out, " claw /skills")?; + writeln!(out, " claw doctor")?; writeln!(out, " claw login")?; writeln!(out, " claw init")?; + writeln!(out, " claw export")?; + writeln!(out, " claw export conversation.md")?; Ok(()) } -fn print_help() { - let _ = print_help_to(&mut io::stdout()); +fn print_help(output_format: CliOutputFormat) -> Result<(), Box> { + let mut buffer = Vec::new(); + print_help_to(&mut buffer)?; + let message = String::from_utf8(buffer)?; + match output_format { + CliOutputFormat::Text => print!("{message}"), + CliOutputFormat::Json => println!( + "{}", + serde_json::to_string_pretty(&json!({ + "kind": "help", + "message": message, + }))? + ), + } + Ok(()) } #[cfg(test)] mod tests { use super::{ build_runtime_plugin_state_with_loader, build_runtime_with_plugin_state, - create_managed_session_handle, describe_tool_progress, filter_tool_specs, - format_bughunter_report, format_commit_preflight_report, format_commit_skipped_report, - format_compact_report, format_cost_report, format_internal_prompt_progress_line, + collect_session_prompt_history, create_managed_session_handle, describe_tool_progress, + filter_tool_specs, format_bughunter_report, format_commit_preflight_report, + format_commit_skipped_report, format_compact_report, format_connected_line, + format_cost_report, format_history_timestamp, format_internal_prompt_progress_line, format_issue_report, format_model_report, format_model_switch_report, format_permissions_report, format_permissions_switch_report, format_pr_report, format_resume_report, format_status_report, format_tool_call_start, format_tool_result, format_ultraplan_report, format_unknown_slash_command, - format_unknown_slash_command_message, normalize_permission_mode, parse_args, - parse_git_status_branch, parse_git_status_metadata_for, parse_git_workspace_summary, - permission_policy, print_help_to, push_output_block, render_config_report, - render_diff_report, render_diff_report_for, render_memory_report, render_repl_help, - render_resume_usage, resolve_model_alias, resolve_session_reference, response_to_events, - resume_supported_slash_commands, run_resume_command, - slash_command_completion_candidates_with_sessions, status_context, validate_no_args, - write_mcp_server_fixture, CliAction, CliOutputFormat, CliToolExecutor, GitWorkspaceSummary, - InternalPromptProgressEvent, InternalPromptProgressState, LiveCli, SlashCommand, - StatusUsage, DEFAULT_MODEL, + format_unknown_slash_command_message, format_user_visible_api_error, + merge_prompt_with_stdin, normalize_permission_mode, parse_args, parse_git_status_branch, + parse_git_status_metadata_for, parse_git_workspace_summary, permission_policy, + print_help_to, push_output_block, render_config_report, render_diff_report, + render_diff_report_for, render_memory_report, render_repl_help, render_resume_usage, + resolve_model_alias, resolve_model_alias_with_config, resolve_repl_model, + resolve_session_reference, response_to_events, resume_supported_slash_commands, + run_resume_command, slash_command_completion_candidates_with_sessions, status_context, + validate_no_args, write_mcp_server_fixture, CliAction, CliOutputFormat, CliToolExecutor, + GitWorkspaceSummary, InternalPromptProgressEvent, InternalPromptProgressState, LiveCli, + LocalHelpTopic, SlashCommand, StatusUsage, DEFAULT_MODEL, LATEST_SESSION_REFERENCE, + PromptHistoryEntry, render_prompt_history_report, parse_history_count, + parse_export_args, render_session_markdown, summarize_tool_payload_for_markdown, short_tool_id, }; - use api::{MessageResponse, OutputContentBlock, Usage}; + use api::{ApiError, MessageResponse, OutputContentBlock, Usage}; use plugins::{ PluginManager, PluginManagerConfig, PluginTool, PluginToolDefinition, PluginToolPermission, }; use runtime::{ - AssistantEvent, ConfigLoader, ContentBlock, ConversationMessage, MessageRole, - PermissionMode, Session, ToolExecutor, + load_oauth_credentials, save_oauth_credentials, AssistantEvent, ConfigLoader, ContentBlock, + ConversationMessage, MessageRole, OAuthConfig, PermissionMode, Session, ToolExecutor, }; use serde_json::json; use std::fs; + use std::io::{Read, Write}; + use std::net::TcpListener; use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::{Mutex, MutexGuard, OnceLock}; + use std::thread; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tools::GlobalToolRegistry; @@ -5697,12 +7747,166 @@ mod tests { .expect("plugin tool registry should build") } + #[test] + fn opaque_provider_wrapper_surfaces_failure_class_session_and_trace() { + let error = ApiError::Api { + status: "500".parse().expect("status"), + error_type: Some("api_error".to_string()), + message: Some( + "Something went wrong while processing your request. Please try again, or use /new to start a fresh session." + .to_string(), + ), + request_id: Some("req_jobdori_789".to_string()), + body: String::new(), + retryable: true, + }; + + let rendered = format_user_visible_api_error("session-issue-22", &error); + assert!(rendered.contains("provider_internal")); + assert!(rendered.contains("session session-issue-22")); + assert!(rendered.contains("trace req_jobdori_789")); + } + + #[test] + fn retry_exhaustion_uses_retry_failure_class_for_generic_provider_wrapper() { + let error = ApiError::RetriesExhausted { + attempts: 3, + last_error: Box::new(ApiError::Api { + status: "502".parse().expect("status"), + error_type: Some("api_error".to_string()), + message: Some( + "Something went wrong while processing your request. Please try again, or use /new to start a fresh session." + .to_string(), + ), + request_id: Some("req_jobdori_790".to_string()), + body: String::new(), + retryable: true, + }), + }; + + let rendered = format_user_visible_api_error("session-issue-22", &error); + assert!(rendered.contains("provider_retry_exhausted"), "{rendered}"); + assert!(rendered.contains("session session-issue-22")); + assert!(rendered.contains("trace req_jobdori_790")); + } + + #[test] + fn context_window_preflight_errors_render_recovery_steps() { + let error = ApiError::ContextWindowExceeded { + model: "claude-sonnet-4-6".to_string(), + estimated_input_tokens: 182_000, + requested_output_tokens: 64_000, + estimated_total_tokens: 246_000, + context_window_tokens: 200_000, + }; + + let rendered = format_user_visible_api_error("session-issue-32", &error); + assert!(rendered.contains("Context window blocked"), "{rendered}"); + assert!(rendered.contains("context_window_blocked"), "{rendered}"); + assert!( + rendered.contains("Session session-issue-32"), + "{rendered}" + ); + assert!( + rendered.contains("Model claude-sonnet-4-6"), + "{rendered}" + ); + assert!( + rendered.contains("Input estimate ~182000 tokens (heuristic)"), + "{rendered}" + ); + assert!( + rendered.contains("Total estimate ~246000 tokens (heuristic)"), + "{rendered}" + ); + assert!(rendered.contains("Compact /compact"), "{rendered}"); + assert!( + rendered.contains("Resume compact claw --resume session-issue-32 /compact"), + "{rendered}" + ); + assert!( + rendered.contains("Fresh session /clear --confirm"), + "{rendered}" + ); + assert!(rendered.contains("Reduce scope"), "{rendered}"); + assert!(rendered.contains("Retry rerun"), "{rendered}"); + } + + #[test] + fn provider_context_window_errors_are_reframed_with_same_guidance() { + let error = ApiError::Api { + status: "400".parse().expect("status"), + error_type: Some("invalid_request_error".to_string()), + message: Some( + "This model's maximum context length is 200000 tokens, but your request used 230000 tokens." + .to_string(), + ), + request_id: Some("req_ctx_456".to_string()), + body: String::new(), + retryable: false, + }; + + let rendered = format_user_visible_api_error("session-issue-32", &error); + assert!(rendered.contains("context_window_blocked"), "{rendered}"); + assert!( + rendered.contains("Trace req_ctx_456"), + "{rendered}" + ); + assert!( + rendered + .contains("Detail This model's maximum context length is 200000 tokens"), + "{rendered}" + ); + assert!(rendered.contains("Compact /compact"), "{rendered}"); + assert!( + rendered.contains("Fresh session /clear --confirm"), + "{rendered}" + ); + } + + #[test] + fn retry_wrapped_context_window_errors_keep_recovery_guidance() { + let error = ApiError::RetriesExhausted { + attempts: 2, + last_error: Box::new(ApiError::Api { + status: "413".parse().expect("status"), + error_type: Some("invalid_request_error".to_string()), + message: Some("Request is too large for this model's context window.".to_string()), + request_id: Some("req_ctx_retry_789".to_string()), + body: String::new(), + retryable: false, + }), + }; + + let rendered = format_user_visible_api_error("session-issue-32", &error); + assert!(rendered.contains("Context window blocked"), "{rendered}"); + assert!(rendered.contains("context_window_blocked"), "{rendered}"); + assert!( + rendered.contains("Trace req_ctx_retry_789"), + "{rendered}" + ); + assert!( + rendered + .contains("Detail Request is too large for this model's context window."), + "{rendered}" + ); + assert!(rendered.contains("Compact /compact"), "{rendered}"); + assert!( + rendered.contains("Resume compact claw --resume session-issue-32 /compact"), + "{rendered}" + ); + } + fn temp_dir() -> PathBuf { + use std::sync::atomic::{AtomicU64, Ordering}; + + static COUNTER: AtomicU64 = AtomicU64::new(0); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("time should be after epoch") .as_nanos(); - std::env::temp_dir().join(format!("rusty-claude-cli-{nanos}")) + let unique = COUNTER.fetch_add(1, Ordering::Relaxed); + std::env::temp_dir().join(format!("rusty-claude-cli-{nanos}-{unique}")) } fn git(args: &[&str], cwd: &Path) { @@ -5725,6 +7929,36 @@ mod tests { .unwrap_or_else(std::sync::PoisonError::into_inner) } + fn sample_oauth_config(token_url: String) -> OAuthConfig { + OAuthConfig { + client_id: "runtime-client".to_string(), + authorize_url: "https://console.test/oauth/authorize".to_string(), + token_url, + callback_port: Some(4545), + manual_redirect_url: Some("https://console.test/oauth/callback".to_string()), + scopes: vec!["org:create_api_key".to_string(), "user:profile".to_string()], + } + } + + fn spawn_token_server(response_body: &'static str) -> String { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind listener"); + let address = listener.local_addr().expect("local addr"); + thread::spawn(move || { + let (mut stream, _) = listener.accept().expect("accept connection"); + let mut buffer = [0_u8; 4096]; + let _ = stream.read(&mut buffer).expect("read request"); + let response = format!( + "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\n\r\n{}", + response_body.len(), + response_body + ); + stream + .write_all(response.as_bytes()) + .expect("write response"); + }); + format!("http://{address}/oauth/token") + } + fn with_current_dir(cwd: &Path, f: impl FnOnce() -> T) -> T { let _guard = cwd_lock() .lock() @@ -5791,6 +8025,7 @@ mod tests { model: DEFAULT_MODEL.to_string(), allowed_tools: None, permission_mode: PermissionMode::DangerFullAccess, + base_commit: None, } ); } @@ -5863,6 +8098,78 @@ mod tests { assert_eq!(resolved, PermissionMode::ReadOnly); } + #[test] + fn load_runtime_oauth_config_for_returns_none_without_project_config() { + let _guard = env_lock(); + let root = temp_dir(); + std::fs::create_dir_all(&root).expect("workspace should exist"); + + let oauth = super::load_runtime_oauth_config_for(&root) + .expect("loading config should succeed when files are absent"); + + std::fs::remove_dir_all(root).expect("temp workspace should clean up"); + + assert_eq!(oauth, None); + } + + #[test] + fn resolve_cli_auth_source_uses_default_oauth_when_runtime_config_is_missing() { + let _guard = env_lock(); + let workspace = temp_dir(); + let config_home = temp_dir(); + std::fs::create_dir_all(&workspace).expect("workspace should exist"); + std::fs::create_dir_all(&config_home).expect("config home should exist"); + + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_api_key = std::env::var("ANTHROPIC_API_KEY").ok(); + let original_auth_token = std::env::var("ANTHROPIC_AUTH_TOKEN").ok(); + std::env::set_var("CLAW_CONFIG_HOME", &config_home); + std::env::remove_var("ANTHROPIC_API_KEY"); + std::env::remove_var("ANTHROPIC_AUTH_TOKEN"); + + save_oauth_credentials(&runtime::OAuthTokenSet { + access_token: "expired-access-token".to_string(), + refresh_token: Some("refresh-token".to_string()), + expires_at: Some(0), + scopes: vec!["org:create_api_key".to_string(), "user:profile".to_string()], + }) + .expect("save expired oauth credentials"); + + let token_url = spawn_token_server( + r#"{"access_token":"refreshed-access-token","refresh_token":"refreshed-refresh-token","expires_at":4102444800,"scopes":["org:create_api_key","user:profile"]}"#, + ); + + let auth = + super::resolve_cli_auth_source_for_cwd(&workspace, || sample_oauth_config(token_url)) + .expect("expired saved oauth should refresh via default config"); + + let stored = load_oauth_credentials() + .expect("load stored credentials") + .expect("stored credentials should exist"); + + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_api_key { + Some(value) => std::env::set_var("ANTHROPIC_API_KEY", value), + None => std::env::remove_var("ANTHROPIC_API_KEY"), + } + match original_auth_token { + Some(value) => std::env::set_var("ANTHROPIC_AUTH_TOKEN", value), + None => std::env::remove_var("ANTHROPIC_AUTH_TOKEN"), + } + std::fs::remove_dir_all(workspace).expect("temp workspace should clean up"); + std::fs::remove_dir_all(config_home).expect("temp config home should clean up"); + + assert_eq!(auth.bearer_token(), Some("refreshed-access-token")); + assert_eq!(stored.access_token, "refreshed-access-token"); + assert_eq!( + stored.refresh_token.as_deref(), + Some("refreshed-refresh-token") + ); + } + #[test] fn parses_prompt_subcommand() { let _guard = env_lock(); @@ -5880,10 +8187,76 @@ mod tests { output_format: CliOutputFormat::Text, allowed_tools: None, permission_mode: PermissionMode::DangerFullAccess, + compact: false, + base_commit: None, } ); } + #[test] + fn merge_prompt_with_stdin_returns_prompt_unchanged_when_no_pipe() { + // given + let prompt = "Review this"; + + // when + let merged = merge_prompt_with_stdin(prompt, None); + + // then + assert_eq!(merged, "Review this"); + } + + #[test] + fn merge_prompt_with_stdin_ignores_whitespace_only_pipe() { + // given + let prompt = "Review this"; + let piped = " \n\t\n "; + + // when + let merged = merge_prompt_with_stdin(prompt, Some(piped)); + + // then + assert_eq!(merged, "Review this"); + } + + #[test] + fn merge_prompt_with_stdin_appends_piped_content_as_context() { + // given + let prompt = "Review this"; + let piped = "fn main() { println!(\"hi\"); }\n"; + + // when + let merged = merge_prompt_with_stdin(prompt, Some(piped)); + + // then + assert_eq!(merged, "Review this\n\nfn main() { println!(\"hi\"); }"); + } + + #[test] + fn merge_prompt_with_stdin_trims_surrounding_whitespace_on_pipe() { + // given + let prompt = "Summarize"; + let piped = "\n\n some notes \n\n"; + + // when + let merged = merge_prompt_with_stdin(prompt, Some(piped)); + + // then + assert_eq!(merged, "Summarize\n\nsome notes"); + } + + #[test] + fn merge_prompt_with_stdin_returns_pipe_when_prompt_is_empty() { + // given + let prompt = ""; + let piped = "standalone body"; + + // when + let merged = merge_prompt_with_stdin(prompt, Some(piped)); + + // then + assert_eq!(merged, "standalone body"); + } + #[test] fn parses_bare_prompt_and_json_output_flag() { let _guard = env_lock(); @@ -5903,10 +8276,58 @@ mod tests { output_format: CliOutputFormat::Json, allowed_tools: None, permission_mode: PermissionMode::DangerFullAccess, + compact: false, + base_commit: None, } ); } + #[test] + fn parses_compact_flag_for_prompt_mode() { + // given a bare prompt invocation that includes the --compact flag + let _guard = env_lock(); + std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); + let args = vec![ + "--compact".to_string(), + "summarize".to_string(), + "this".to_string(), + ]; + + // when parse_args interprets the flag + let parsed = parse_args(&args).expect("args should parse"); + + // then compact mode is propagated and other defaults stay unchanged + assert_eq!( + parsed, + CliAction::Prompt { + prompt: "summarize this".to_string(), + model: DEFAULT_MODEL.to_string(), + output_format: CliOutputFormat::Text, + allowed_tools: None, + permission_mode: PermissionMode::DangerFullAccess, + compact: true, + base_commit: None, + } + ); + } + + #[test] + fn prompt_subcommand_defaults_compact_to_false() { + // given a `prompt` subcommand invocation without --compact + let _guard = env_lock(); + std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); + let args = vec!["prompt".to_string(), "hello".to_string()]; + + // when parse_args runs + let parsed = parse_args(&args).expect("args should parse"); + + // then compact stays false (opt-in flag) + match parsed { + CliAction::Prompt { compact, .. } => assert!(!compact), + other => panic!("expected Prompt action, got {other:?}"), + } + } + #[test] fn resolves_model_aliases_in_args() { let _guard = env_lock(); @@ -5925,6 +8346,8 @@ mod tests { output_format: CliOutputFormat::Text, allowed_tools: None, permission_mode: PermissionMode::DangerFullAccess, + compact: false, + base_commit: None, } ); } @@ -5937,15 +8360,58 @@ mod tests { assert_eq!(resolve_model_alias("claude-opus"), "claude-opus"); } + #[test] + fn user_defined_aliases_resolve_before_provider_dispatch() { + // given + let _guard = env_lock(); + let root = temp_dir(); + let cwd = root.join("project"); + let config_home = root.join("config-home"); + std::fs::create_dir_all(cwd.join(".claw")).expect("project config dir should exist"); + std::fs::create_dir_all(&config_home).expect("config home should exist"); + std::fs::write( + cwd.join(".claw").join("settings.json"), + r#"{"aliases":{"fast":"claude-haiku-4-5-20251213","smart":"opus","cheap":"grok-3-mini"}}"#, + ) + .expect("project config should write"); + + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + std::env::set_var("CLAW_CONFIG_HOME", &config_home); + + // when + let direct = with_current_dir(&cwd, || resolve_model_alias_with_config("fast")); + let chained = with_current_dir(&cwd, || resolve_model_alias_with_config("smart")); + let cross_provider = with_current_dir(&cwd, || resolve_model_alias_with_config("cheap")); + let unknown = with_current_dir(&cwd, || resolve_model_alias_with_config("unknown-model")); + let builtin = with_current_dir(&cwd, || resolve_model_alias_with_config("haiku")); + + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + std::fs::remove_dir_all(root).expect("temp config root should clean up"); + + // then + assert_eq!(direct, "claude-haiku-4-5-20251213"); + assert_eq!(chained, "claude-opus-4-6"); + assert_eq!(cross_provider, "grok-3-mini"); + assert_eq!(unknown, "unknown-model"); + assert_eq!(builtin, "claude-haiku-4-5-20251213"); + } + #[test] fn parses_version_flags_without_initializing_prompt_mode() { assert_eq!( parse_args(&["--version".to_string()]).expect("args should parse"), - CliAction::Version + CliAction::Version { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["-V".to_string()]).expect("args should parse"), - CliAction::Version + CliAction::Version { + output_format: CliOutputFormat::Text, + } ); } @@ -5958,6 +8424,54 @@ mod tests { model: DEFAULT_MODEL.to_string(), allowed_tools: None, permission_mode: PermissionMode::ReadOnly, + base_commit: None, + } + ); + } + + #[test] + fn dangerously_skip_permissions_flag_forces_danger_full_access_in_repl() { + let _guard = env_lock(); + std::env::set_var("RUSTY_CLAUDE_PERMISSION_MODE", "read-only"); + let args = vec!["--dangerously-skip-permissions".to_string()]; + let parsed = parse_args(&args).expect("args should parse"); + std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); + + assert_eq!( + parsed, + CliAction::Repl { + model: DEFAULT_MODEL.to_string(), + allowed_tools: None, + permission_mode: PermissionMode::DangerFullAccess, + base_commit: None, + } + ); + } + + #[test] + fn dangerously_skip_permissions_flag_applies_to_prompt_subcommand() { + let _guard = env_lock(); + std::env::set_var("RUSTY_CLAUDE_PERMISSION_MODE", "read-only"); + let args = vec![ + "--dangerously-skip-permissions".to_string(), + "prompt".to_string(), + "do".to_string(), + "the".to_string(), + "thing".to_string(), + ]; + let parsed = parse_args(&args).expect("args should parse"); + std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); + + assert_eq!( + parsed, + CliAction::Prompt { + prompt: "do the thing".to_string(), + model: DEFAULT_MODEL.to_string(), + output_format: CliOutputFormat::Text, + allowed_tools: None, + permission_mode: PermissionMode::DangerFullAccess, + compact: false, + base_commit: None, } ); } @@ -5982,6 +8496,7 @@ mod tests { .collect() ), permission_mode: PermissionMode::DangerFullAccess, + base_commit: None, } ); } @@ -6007,6 +8522,7 @@ mod tests { CliAction::PrintSystemPrompt { cwd: PathBuf::from("/tmp/project"), date: "2026-04-01".to_string(), + output_format: CliOutputFormat::Text, } ); } @@ -6015,59 +8531,419 @@ mod tests { fn parses_login_and_logout_subcommands() { assert_eq!( parse_args(&["login".to_string()]).expect("login should parse"), - CliAction::Login + CliAction::Login { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["logout".to_string()]).expect("logout should parse"), - CliAction::Logout + CliAction::Logout { + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&["doctor".to_string()]).expect("doctor should parse"), + CliAction::Doctor { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["init".to_string()]).expect("init should parse"), - CliAction::Init + CliAction::Init { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["agents".to_string()]).expect("agents should parse"), - CliAction::Agents { args: None } + CliAction::Agents { + args: None, + output_format: CliOutputFormat::Text + } ); assert_eq!( parse_args(&["mcp".to_string()]).expect("mcp should parse"), - CliAction::Mcp { args: None } + CliAction::Mcp { + args: None, + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["skills".to_string()]).expect("skills should parse"), - CliAction::Skills { args: None } + CliAction::Skills { + args: None, + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&[ + "skills".to_string(), + "help".to_string(), + "overview".to_string() + ]) + .expect("skills help overview should invoke"), + CliAction::Prompt { + prompt: "$help overview".to_string(), + model: DEFAULT_MODEL.to_string(), + output_format: CliOutputFormat::Text, + allowed_tools: None, + permission_mode: crate::default_permission_mode(), + compact: false, + base_commit: None, + } ); assert_eq!( parse_args(&["agents".to_string(), "--help".to_string()]) .expect("agents help should parse"), CliAction::Agents { - args: Some("--help".to_string()) + args: Some("--help".to_string()), + output_format: CliOutputFormat::Text, } ); } + #[test] + fn local_command_help_flags_stay_on_the_local_parser_path() { + assert_eq!( + parse_args(&["status".to_string(), "--help".to_string()]) + .expect("status help should parse"), + CliAction::HelpTopic(LocalHelpTopic::Status) + ); + assert_eq!( + parse_args(&["sandbox".to_string(), "-h".to_string()]) + .expect("sandbox help should parse"), + CliAction::HelpTopic(LocalHelpTopic::Sandbox) + ); + assert_eq!( + parse_args(&["doctor".to_string(), "--help".to_string()]) + .expect("doctor help should parse"), + CliAction::HelpTopic(LocalHelpTopic::Doctor) + ); + } + #[test] fn parses_single_word_command_aliases_without_falling_back_to_prompt_mode() { let _guard = env_lock(); std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); assert_eq!( parse_args(&["help".to_string()]).expect("help should parse"), - CliAction::Help + CliAction::Help { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["version".to_string()]).expect("version should parse"), - CliAction::Version + CliAction::Version { + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["status".to_string()]).expect("status should parse"), CliAction::Status { model: DEFAULT_MODEL.to_string(), permission_mode: PermissionMode::DangerFullAccess, + output_format: CliOutputFormat::Text, } ); assert_eq!( parse_args(&["sandbox".to_string()]).expect("sandbox should parse"), - CliAction::Sandbox + CliAction::Sandbox { + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn parses_bare_export_subcommand_targeting_latest_session() { + // given + let _guard = env_lock(); + std::env::remove_var("RUSTY_CLAUDE_PERMISSION_MODE"); + let args = vec!["export".to_string()]; + + // when + let parsed = parse_args(&args).expect("bare export should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: LATEST_SESSION_REFERENCE.to_string(), + output_path: None, + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn parses_export_subcommand_with_positional_output_path() { + // given + let args = vec!["export".to_string(), "conversation.md".to_string()]; + + // when + let parsed = parse_args(&args).expect("export with path should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: LATEST_SESSION_REFERENCE.to_string(), + output_path: Some(PathBuf::from("conversation.md")), + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn parses_export_subcommand_with_session_and_output_flags() { + // given + let args = vec![ + "export".to_string(), + "--session".to_string(), + "session-alpha".to_string(), + "--output".to_string(), + "/tmp/share.md".to_string(), + ]; + + // when + let parsed = parse_args(&args).expect("export flags should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: "session-alpha".to_string(), + output_path: Some(PathBuf::from("/tmp/share.md")), + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn parses_export_subcommand_with_inline_flag_values() { + // given + let args = vec![ + "export".to_string(), + "--session=session-beta".to_string(), + "--output=/tmp/beta.md".to_string(), + ]; + + // when + let parsed = parse_args(&args).expect("export inline flags should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: "session-beta".to_string(), + output_path: Some(PathBuf::from("/tmp/beta.md")), + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn parses_export_subcommand_with_json_output_format() { + // given + let args = vec![ + "--output-format=json".to_string(), + "export".to_string(), + "/tmp/notes.md".to_string(), + ]; + + // when + let parsed = parse_args(&args).expect("json export should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: LATEST_SESSION_REFERENCE.to_string(), + output_path: Some(PathBuf::from("/tmp/notes.md")), + output_format: CliOutputFormat::Json, + } + ); + } + + #[test] + fn rejects_unknown_export_options_with_helpful_message() { + // given + let args = vec!["export".to_string(), "--bogus".to_string()]; + + // when + let error = parse_args(&args).expect_err("unknown export option should fail"); + + // then + assert!(error.contains("unknown export option: --bogus")); + } + + #[test] + fn rejects_export_with_extra_positional_after_path() { + // given + let args = vec![ + "export".to_string(), + "first.md".to_string(), + "second.md".to_string(), + ]; + + // when + let error = parse_args(&args).expect_err("multiple positionals should fail"); + + // then + assert!(error.contains("unexpected export argument: second.md")); + } + + #[test] + fn parse_export_args_helper_defaults_to_latest_reference_and_no_output() { + // given + let args: Vec = vec![]; + + // when + let parsed = parse_export_args(&args, CliOutputFormat::Text) + .expect("empty export args should parse"); + + // then + assert_eq!( + parsed, + CliAction::Export { + session_reference: LATEST_SESSION_REFERENCE.to_string(), + output_path: None, + output_format: CliOutputFormat::Text, + } + ); + } + + #[test] + fn render_session_markdown_includes_header_and_summarized_tool_calls() { + // given + let mut session = Session::new(); + session.session_id = "session-export-test".to_string(); + session.messages = vec![ + ConversationMessage::user_text("How do I list files?"), + ConversationMessage::assistant(vec![ + ContentBlock::Text { + text: "I'll run a tool.".to_string(), + }, + ContentBlock::ToolUse { + id: "toolu_abcdefghijklmnop".to_string(), + name: "bash".to_string(), + input: r#"{"command":"ls -la"}"#.to_string(), + }, + ]), + ConversationMessage { + role: MessageRole::Tool, + blocks: vec![ContentBlock::ToolResult { + tool_use_id: "toolu_abcdefghijklmnop".to_string(), + tool_name: "bash".to_string(), + output: "total 8\ndrwxr-xr-x 2 user staff 64 Apr 7 12:00 .".to_string(), + is_error: false, + }], + usage: None, + }, + ]; + + // when + let markdown = render_session_markdown( + &session, + "session-export-test", + std::path::Path::new("/tmp/sessions/session-export-test.jsonl"), + ); + + // then + assert!(markdown.starts_with("# Conversation Export")); + assert!(markdown.contains("- **Session**: `session-export-test`")); + assert!(markdown.contains("- **Messages**: 3")); + assert!(markdown.contains("## 1. User")); + assert!(markdown.contains("How do I list files?")); + assert!(markdown.contains("## 2. Assistant")); + assert!(markdown.contains("**Tool call** `bash`")); + assert!(markdown.contains("toolu_abcdef…")); + assert!(markdown.contains("ls -la")); + assert!(markdown.contains("## 3. Tool")); + assert!(markdown.contains("**Tool result** `bash`")); + assert!(markdown.contains("ok")); + assert!(markdown.contains("total 8")); + } + + #[test] + fn render_session_markdown_marks_tool_errors_and_skips_empty_summaries() { + // given + let mut session = Session::new(); + session.session_id = "errs".to_string(); + session.messages = vec![ConversationMessage { + role: MessageRole::Tool, + blocks: vec![ContentBlock::ToolResult { + tool_use_id: "short".to_string(), + tool_name: "read_file".to_string(), + output: " ".to_string(), + is_error: true, + }], + usage: None, + }]; + + // when + let markdown = + render_session_markdown(&session, "errs", std::path::Path::new("errs.jsonl")); + + // then + assert!(markdown.contains("**Tool result** `read_file` _(id `short`, error)_")); + // an empty summary should not produce a stray blockquote line + assert!(!markdown.contains("> \n")); + } + + #[test] + fn summarize_tool_payload_for_markdown_compacts_json_and_truncates_overflow() { + // given + let json_payload = r#"{ + "command": "ls -la", + "cwd": "/tmp" + }"#; + let long_payload = "a".repeat(600); + + // when + let compacted = summarize_tool_payload_for_markdown(json_payload); + let truncated = summarize_tool_payload_for_markdown(&long_payload); + + // then + assert_eq!(compacted, r#"{"command":"ls -la","cwd":"/tmp"}"#); + assert!(truncated.ends_with('…')); + assert!(truncated.chars().count() <= 281); + } + + #[test] + fn short_tool_id_truncates_long_identifiers_with_ellipsis() { + // given + let long = "toolu_01ABCDEFGHIJKLMN"; + let short = "tool_1"; + + // when + let trimmed_long = short_tool_id(long); + let trimmed_short = short_tool_id(short); + + // then + assert_eq!(trimmed_long, "toolu_01ABCD…"); + assert_eq!(trimmed_short, "tool_1"); + } + + #[test] + fn parses_json_output_for_mcp_and_skills_commands() { + assert_eq!( + parse_args(&["--output-format=json".to_string(), "mcp".to_string()]) + .expect("json mcp should parse"), + CliAction::Mcp { + args: None, + output_format: CliOutputFormat::Json, + } + ); + assert_eq!( + parse_args(&[ + "--output-format=json".to_string(), + "/skills".to_string(), + "help".to_string(), + ]) + .expect("json /skills help should parse"), + CliAction::Skills { + args: Some("help".to_string()), + output_format: CliOutputFormat::Json, + } ); } @@ -6086,11 +8962,13 @@ mod tests { parse_args(&["help".to_string(), "me".to_string(), "debug".to_string()]) .expect("prompt shorthand should still work"), CliAction::Prompt { - prompt: "help me debug".to_string(), + prompt: "$help overview".to_string(), model: DEFAULT_MODEL.to_string(), output_format: CliOutputFormat::Text, allowed_tools: None, - permission_mode: PermissionMode::DangerFullAccess, + permission_mode: crate::default_permission_mode(), + compact: false, + base_commit: None, } ); } @@ -6099,24 +8977,64 @@ mod tests { fn parses_direct_agents_mcp_and_skills_slash_commands() { assert_eq!( parse_args(&["/agents".to_string()]).expect("/agents should parse"), - CliAction::Agents { args: None } + CliAction::Agents { + args: None, + output_format: CliOutputFormat::Text + } ); assert_eq!( parse_args(&["/mcp".to_string(), "show".to_string(), "demo".to_string()]) .expect("/mcp show demo should parse"), CliAction::Mcp { - args: Some("show demo".to_string()) + args: Some("show demo".to_string()), + output_format: CliOutputFormat::Text, } ); assert_eq!( parse_args(&["/skills".to_string()]).expect("/skills should parse"), - CliAction::Skills { args: None } + CliAction::Skills { + args: None, + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&["/skill".to_string()]).expect("/skill should parse"), + CliAction::Skills { + args: None, + output_format: CliOutputFormat::Text, + } ); assert_eq!( parse_args(&["/skills".to_string(), "help".to_string()]) .expect("/skills help should parse"), CliAction::Skills { - args: Some("help".to_string()) + args: Some("help".to_string()), + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&["/skill".to_string(), "list".to_string()]) + .expect("/skill list should parse"), + CliAction::Skills { + args: Some("list".to_string()), + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&[ + "/skills".to_string(), + "help".to_string(), + "overview".to_string() + ]) + .expect("/skills help overview should invoke"), + CliAction::Prompt { + prompt: "$help overview".to_string(), + model: DEFAULT_MODEL.to_string(), + output_format: CliOutputFormat::Text, + allowed_tools: None, + permission_mode: crate::default_permission_mode(), + compact: false, + base_commit: None, } ); assert_eq!( @@ -6127,7 +9045,21 @@ mod tests { ]) .expect("/skills install should parse"), CliAction::Skills { - args: Some("install ./fixtures/help-skill".to_string()) + args: Some("install ./fixtures/help-skill".to_string()), + output_format: CliOutputFormat::Text, + } + ); + assert_eq!( + parse_args(&["/skills".to_string(), "/test".to_string()]) + .expect("/skills /test should normalize to a single skill prompt prefix"), + CliAction::Prompt { + prompt: "$test".to_string(), + model: DEFAULT_MODEL.to_string(), + output_format: CliOutputFormat::Text, + allowed_tools: None, + permission_mode: crate::default_permission_mode(), + compact: false, + base_commit: None, } ); let error = parse_args(&["/status".to_string()]) @@ -6161,6 +9093,16 @@ mod tests { assert!(report.contains("Use /help")); } + #[test] + fn formats_namespaced_omc_slash_command_with_contract_guidance() { + let report = format_unknown_slash_command_message("oh-my-claudecode:hud"); + assert!(report.contains("unknown slash command: /oh-my-claudecode:hud")); + assert!(report.contains("Claude Code/OMC plugin command")); + assert!(report.contains("plugin slash commands")); + assert!(report.contains("statusline")); + assert!(report.contains("session hooks")); + } + #[test] fn parses_resume_flag_with_slash_command() { let args = vec![ @@ -6173,6 +9115,7 @@ mod tests { CliAction::ResumeSession { session_path: PathBuf::from("session.jsonl"), commands: vec!["/compact".to_string()], + output_format: CliOutputFormat::Text, } ); } @@ -6184,6 +9127,7 @@ mod tests { CliAction::ResumeSession { session_path: PathBuf::from("latest"), commands: vec![], + output_format: CliOutputFormat::Text, } ); assert_eq!( @@ -6192,6 +9136,7 @@ mod tests { CliAction::ResumeSession { session_path: PathBuf::from("latest"), commands: vec!["/status".to_string()], + output_format: CliOutputFormat::Text, } ); } @@ -6214,6 +9159,7 @@ mod tests { "/compact".to_string(), "/cost".to_string(), ], + output_format: CliOutputFormat::Text, } ); } @@ -6244,6 +9190,7 @@ mod tests { "/export notes.txt".to_string(), "/clear --confirm".to_string(), ], + output_format: CliOutputFormat::Text, } ); } @@ -6262,6 +9209,7 @@ mod tests { CliAction::ResumeSession { session_path: PathBuf::from("session.jsonl"), commands: vec!["/export /tmp/notes.txt".to_string(), "/status".to_string()], + output_format: CliOutputFormat::Text, } ); } @@ -6386,6 +9334,73 @@ mod tests { std::env::remove_var("ANTHROPIC_API_KEY"); } + #[test] + fn format_connected_line_renders_anthropic_provider_for_claude_model() { + let model = "claude-sonnet-4-6"; + + let line = format_connected_line(model); + + assert_eq!(line, "Connected: claude-sonnet-4-6 via anthropic"); + } + + #[test] + fn format_connected_line_renders_xai_provider_for_grok_model() { + let model = "grok-3"; + + let line = format_connected_line(model); + + assert_eq!(line, "Connected: grok-3 via xai"); + } + + #[test] + fn resolve_repl_model_returns_user_supplied_model_unchanged_when_explicit() { + let user_model = "claude-sonnet-4-6".to_string(); + + let resolved = resolve_repl_model(user_model); + + assert_eq!(resolved, "claude-sonnet-4-6"); + } + + #[test] + fn resolve_repl_model_falls_back_to_anthropic_model_env_when_default() { + let _guard = env_lock(); + let root = temp_dir(); + fs::create_dir_all(&root).expect("root dir"); + let config_home = root.join("config"); + fs::create_dir_all(&config_home).expect("config home dir"); + std::env::set_var("CLAW_CONFIG_HOME", &config_home); + std::env::remove_var("ANTHROPIC_MODEL"); + std::env::set_var("ANTHROPIC_MODEL", "sonnet"); + + let resolved = + with_current_dir(&root, || resolve_repl_model(DEFAULT_MODEL.to_string())); + + assert_eq!(resolved, "claude-sonnet-4-6"); + + std::env::remove_var("ANTHROPIC_MODEL"); + std::env::remove_var("CLAW_CONFIG_HOME"); + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + + #[test] + fn resolve_repl_model_returns_default_when_env_unset_and_no_config() { + let _guard = env_lock(); + let root = temp_dir(); + fs::create_dir_all(&root).expect("root dir"); + let config_home = root.join("config"); + fs::create_dir_all(&config_home).expect("config home dir"); + std::env::set_var("CLAW_CONFIG_HOME", &config_home); + std::env::remove_var("ANTHROPIC_MODEL"); + + let resolved = + with_current_dir(&root, || resolve_repl_model(DEFAULT_MODEL.to_string())); + + assert_eq!(resolved, DEFAULT_MODEL); + + std::env::remove_var("CLAW_CONFIG_HOME"); + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + #[test] fn resume_supported_command_list_matches_expected_surface() { let names = resume_supported_slash_commands() @@ -6952,6 +9967,14 @@ UU conflicted.rs", assert!(message.contains("/help")); } + #[test] + fn unknown_omc_slash_command_guidance_explains_runtime_gap() { + let message = format_unknown_slash_command("oh-my-claudecode:hud"); + assert!(message.contains("Unknown slash command: /oh-my-claudecode:hud")); + assert!(message.contains("Claude Code/OMC plugin command")); + assert!(message.contains("does not yet load plugin slash commands")); + } + #[test] fn resume_usage_mentions_latest_shortcut() { let usage = render_resume_usage(); @@ -7016,6 +10039,168 @@ UU conflicted.rs", assert!(help.contains("Up/Down")); assert!(help.contains("Tab")); assert!(help.contains("Shift+Enter/Ctrl+J")); + assert!(help.contains("Ctrl-R")); + assert!(help.contains("Reverse-search prompt history")); + assert!(help.contains("/history [count]")); + } + + #[test] + fn parse_history_count_defaults_to_twenty_when_missing() { + // given + let raw: Option<&str> = None; + + // when + let parsed = parse_history_count(raw); + + // then + assert_eq!(parsed, Ok(20)); + } + + #[test] + fn parse_history_count_accepts_positive_integers() { + // given + let raw = Some("25"); + + // when + let parsed = parse_history_count(raw); + + // then + assert_eq!(parsed, Ok(25)); + } + + #[test] + fn parse_history_count_rejects_zero() { + // given + let raw = Some("0"); + + // when + let parsed = parse_history_count(raw); + + // then + assert!(parsed.is_err()); + assert!(parsed.unwrap_err().contains("greater than 0")); + } + + #[test] + fn parse_history_count_rejects_non_numeric() { + // given + let raw = Some("abc"); + + // when + let parsed = parse_history_count(raw); + + // then + assert!(parsed.is_err()); + assert!(parsed.unwrap_err().contains("invalid count 'abc'")); + } + + #[test] + fn format_history_timestamp_renders_iso8601_utc() { + // given + // 2023-01-15T12:34:56.789Z -> 1673786096789 ms + let timestamp_ms: u64 = 1_673_786_096_789; + + // when + let formatted = format_history_timestamp(timestamp_ms); + + // then + assert_eq!(formatted, "2023-01-15T12:34:56.789Z"); + } + + #[test] + fn format_history_timestamp_renders_unix_epoch_origin() { + // given + let timestamp_ms: u64 = 0; + + // when + let formatted = format_history_timestamp(timestamp_ms); + + // then + assert_eq!(formatted, "1970-01-01T00:00:00.000Z"); + } + + #[test] + fn render_prompt_history_report_lists_entries_with_timestamps() { + // given + let entries = vec![ + PromptHistoryEntry { + timestamp_ms: 1_673_786_096_000, + text: "first prompt".to_string(), + }, + PromptHistoryEntry { + timestamp_ms: 1_673_786_100_000, + text: "second prompt".to_string(), + }, + ]; + + // when + let rendered = render_prompt_history_report(&entries, 10); + + // then + assert!(rendered.contains("Prompt history")); + assert!(rendered.contains("Total 2")); + assert!(rendered.contains("Showing 2 most recent")); + assert!(rendered.contains("Reverse search Ctrl-R in the REPL")); + assert!(rendered.contains("2023-01-15T12:34:56.000Z")); + assert!(rendered.contains("first prompt")); + assert!(rendered.contains("second prompt")); + } + + #[test] + fn render_prompt_history_report_truncates_to_limit_from_the_tail() { + // given + let entries = vec![ + PromptHistoryEntry { + timestamp_ms: 1_000, + text: "older".to_string(), + }, + PromptHistoryEntry { + timestamp_ms: 2_000, + text: "middle".to_string(), + }, + PromptHistoryEntry { + timestamp_ms: 3_000, + text: "latest".to_string(), + }, + ]; + + // when + let rendered = render_prompt_history_report(&entries, 2); + + // then + assert!(rendered.contains("Total 3")); + assert!(rendered.contains("Showing 2 most recent")); + assert!(!rendered.contains("older")); + assert!(rendered.contains("middle")); + assert!(rendered.contains("latest")); + } + + #[test] + fn render_prompt_history_report_handles_empty_history() { + // given + let entries: Vec = Vec::new(); + + // when + let rendered = render_prompt_history_report(&entries, 10); + + // then + assert!(rendered.contains("no prompts recorded yet")); + } + + #[test] + fn collect_session_prompt_history_extracts_user_text_blocks() { + // given + let mut session = Session::new(); + session.push_user_text("hello").unwrap(); + session.push_user_text("world").unwrap(); + + // when + let entries = collect_session_prompt_history(&session); + + // then + assert_eq!(entries.len(), 2); + assert_eq!(entries[0].text, "hello"); + assert_eq!(entries[1].text, "world"); } #[test] @@ -7188,6 +10373,7 @@ UU conflicted.rs", let mut out = Vec::new(); let mut events = Vec::new(); let mut pending_tool = None; + let mut block_has_thinking_summary = false; push_output_block( OutputContentBlock::Text { @@ -7197,6 +10383,7 @@ UU conflicted.rs", &mut events, &mut pending_tool, false, + &mut block_has_thinking_summary, ) .expect("text block should render"); @@ -7210,6 +10397,7 @@ UU conflicted.rs", let mut out = Vec::new(); let mut events = Vec::new(); let mut pending_tool = None; + let mut block_has_thinking_summary = false; push_output_block( OutputContentBlock::ToolUse { @@ -7221,6 +10409,7 @@ UU conflicted.rs", &mut events, &mut pending_tool, true, + &mut block_has_thinking_summary, ) .expect("tool block should accumulate"); @@ -7302,7 +10491,7 @@ UU conflicted.rs", } #[test] - fn response_to_events_ignores_thinking_blocks() { + fn response_to_events_renders_collapsed_thinking_summary() { let mut out = Vec::new(); let events = response_to_events( MessageResponse { @@ -7337,7 +10526,34 @@ UU conflicted.rs", &events[0], AssistantEvent::TextDelta(text) if text == "Final answer" )); - assert!(!String::from_utf8(out).expect("utf8").contains("step 1")); + let rendered = String::from_utf8(out).expect("utf8"); + assert!(rendered.contains("▶ Thinking (6 chars hidden)")); + assert!(!rendered.contains("step 1")); + } + + #[test] + fn login_browser_failure_keeps_json_stdout_clean() { + let mut stdout = Vec::new(); + let mut stderr = Vec::new(); + let error = std::io::Error::new( + std::io::ErrorKind::NotFound, + "no supported browser opener command found", + ); + + super::emit_login_browser_open_failure( + CliOutputFormat::Json, + "https://example.test/oauth/authorize", + &error, + &mut stdout, + &mut stderr, + ) + .expect("browser warning should render"); + + assert!(stdout.is_empty()); + let stderr = String::from_utf8(stderr).expect("utf8"); + assert!(stderr.contains("failed to open browser automatically")); + assert!(stderr.contains("Open this URL manually:")); + assert!(stderr.contains("https://example.test/oauth/authorize")); } #[test] @@ -7371,6 +10587,7 @@ UU conflicted.rs", } #[test] + #[allow(clippy::too_many_lines)] fn build_runtime_plugin_state_discovers_mcp_tools_and_surfaces_pending_servers() { let config_home = temp_dir(); let workspace = temp_dir(); @@ -7509,8 +10726,12 @@ UU conflicted.rs", let runtime_config = loader.load().expect("runtime config should load"); let state = build_runtime_plugin_state_with_loader(&workspace, &loader, &runtime_config) .expect("runtime plugin state should load"); - let mut executor = - CliToolExecutor::new(None, false, state.tool_registry.clone(), state.mcp_state.clone()); + let mut executor = CliToolExecutor::new( + None, + false, + state.tool_registry.clone(), + state.mcp_state.clone(), + ); let search_output = executor .execute("ToolSearch", r#"{"query":"remote","max_results":5}"#) diff --git a/rust/crates/rusty-claude-cli/src/render.rs b/rust/crates/rusty-claude-cli/src/render.rs index d8d87964..cb7828d7 100644 --- a/rust/crates/rusty-claude-cli/src/render.rs +++ b/rust/crates/rusty-claude-cli/src/render.rs @@ -249,13 +249,14 @@ impl TerminalRenderer { #[must_use] pub fn render_markdown(&self, markdown: &str) -> String { + let normalized = normalize_nested_fences(markdown); let mut output = String::new(); let mut state = RenderState::default(); let mut code_language = String::new(); let mut code_buffer = String::new(); let mut in_code_block = false; - for event in Parser::new_ext(markdown, Options::all()) { + for event in Parser::new_ext(&normalized, Options::all()) { self.render_event( event, &mut state, @@ -634,8 +635,180 @@ fn apply_code_block_background(line: &str) -> String { format!("\u{1b}[48;5;236m{with_background}\u{1b}[0m{trailing_newline}") } +/// Pre-process raw markdown so that fenced code blocks whose body contains +/// fence markers of equal or greater length are wrapped with a longer fence. +/// +/// LLMs frequently emit triple-backtick code blocks that contain triple-backtick +/// examples. CommonMark (and pulldown-cmark) treats the inner marker as the +/// closing fence, breaking the render. This function detects the situation and +/// upgrades the outer fence to use enough backticks (or tildes) that the inner +/// markers become ordinary content. +fn normalize_nested_fences(markdown: &str) -> String { + // A fence line is either "labeled" (has an info string ⇒ always an opener) + // or "bare" (no info string ⇒ could be opener or closer). + #[derive(Debug, Clone)] + struct FenceLine { + char: char, + len: usize, + has_info: bool, + indent: usize, + } + + fn parse_fence_line(line: &str) -> Option { + let trimmed = line.trim_end_matches('\n').trim_end_matches('\r'); + let indent = trimmed.chars().take_while(|c| *c == ' ').count(); + if indent > 3 { + return None; + } + let rest = &trimmed[indent..]; + let ch = rest.chars().next()?; + if ch != '`' && ch != '~' { + return None; + } + let len = rest.chars().take_while(|c| *c == ch).count(); + if len < 3 { + return None; + } + let after = &rest[len..]; + if ch == '`' && after.contains('`') { + return None; + } + let has_info = !after.trim().is_empty(); + Some(FenceLine { + char: ch, + len, + has_info, + indent, + }) + } + + let lines: Vec<&str> = markdown.split_inclusive('\n').collect(); + // Handle final line that may lack trailing newline. + // split_inclusive already keeps the original chunks, including a + // final chunk without '\n' if the input doesn't end with one. + + // First pass: classify every line. + let fence_info: Vec> = lines.iter().map(|l| parse_fence_line(l)).collect(); + + // Second pass: pair openers with closers using a stack, recording + // (opener_idx, closer_idx) pairs plus the max fence length found between + // them. + struct StackEntry { + line_idx: usize, + fence: FenceLine, + } + + let mut stack: Vec = Vec::new(); + // Paired blocks: (opener_line, closer_line, max_inner_fence_len) + let mut pairs: Vec<(usize, usize, usize)> = Vec::new(); + + for (i, fi) in fence_info.iter().enumerate() { + let Some(fl) = fi else { continue }; + + if fl.has_info { + // Labeled fence ⇒ always an opener. + stack.push(StackEntry { + line_idx: i, + fence: fl.clone(), + }); + } else { + // Bare fence ⇒ try to close the top of the stack if compatible. + let closes_top = stack + .last() + .is_some_and(|top| top.fence.char == fl.char && fl.len >= top.fence.len); + if closes_top { + let opener = stack.pop().unwrap(); + // Find max fence length of any fence line strictly between + // opener and closer (these are the nested fences). + let inner_max = fence_info[opener.line_idx + 1..i] + .iter() + .filter_map(|fi| fi.as_ref().map(|f| f.len)) + .max() + .unwrap_or(0); + pairs.push((opener.line_idx, i, inner_max)); + } else { + // Treat as opener. + stack.push(StackEntry { + line_idx: i, + fence: fl.clone(), + }); + } + } + } + + // Determine which lines need rewriting. A pair needs rewriting when + // its opener length <= max inner fence length. + struct Rewrite { + char: char, + new_len: usize, + indent: usize, + } + let mut rewrites: std::collections::HashMap = std::collections::HashMap::new(); + + for (opener_idx, closer_idx, inner_max) in &pairs { + let opener_fl = fence_info[*opener_idx].as_ref().unwrap(); + if opener_fl.len <= *inner_max { + let new_len = inner_max + 1; + let info_part = { + let trimmed = lines[*opener_idx] + .trim_end_matches('\n') + .trim_end_matches('\r'); + let rest = &trimmed[opener_fl.indent..]; + rest[opener_fl.len..].to_string() + }; + rewrites.insert( + *opener_idx, + Rewrite { + char: opener_fl.char, + new_len, + indent: opener_fl.indent, + }, + ); + let closer_fl = fence_info[*closer_idx].as_ref().unwrap(); + rewrites.insert( + *closer_idx, + Rewrite { + char: closer_fl.char, + new_len, + indent: closer_fl.indent, + }, + ); + // Store info string only in the opener; closer keeps the trailing + // portion which is already handled through the original line. + // Actually, we rebuild both lines from scratch below, including + // the info string for the opener. + let _ = info_part; // consumed in rebuild + } + } + + if rewrites.is_empty() { + return markdown.to_string(); + } + + // Rebuild. + let mut out = String::with_capacity(markdown.len() + rewrites.len() * 4); + for (i, line) in lines.iter().enumerate() { + if let Some(rw) = rewrites.get(&i) { + let fence_str: String = std::iter::repeat(rw.char).take(rw.new_len).collect(); + let indent_str: String = std::iter::repeat(' ').take(rw.indent).collect(); + // Recover the original info string (if any) and trailing newline. + let trimmed = line.trim_end_matches('\n').trim_end_matches('\r'); + let fi = fence_info[i].as_ref().unwrap(); + let info = &trimmed[fi.indent + fi.len..]; + let trailing = &line[trimmed.len()..]; + out.push_str(&indent_str); + out.push_str(&fence_str); + out.push_str(info); + out.push_str(trailing); + } else { + out.push_str(line); + } + } + out +} + fn find_stream_safe_boundary(markdown: &str) -> Option { - let mut in_fence = false; + let mut open_fence: Option = None; let mut last_boundary = None; for (offset, line) in markdown.split_inclusive('\n').scan(0usize, |cursor, line| { @@ -643,20 +816,21 @@ fn find_stream_safe_boundary(markdown: &str) -> Option { *cursor += line.len(); Some((start, line)) }) { - let trimmed = line.trim_start(); - if trimmed.starts_with("```") || trimmed.starts_with("~~~") { - in_fence = !in_fence; - if !in_fence { + let line_without_newline = line.trim_end_matches('\n'); + if let Some(opener) = open_fence { + if line_closes_fence(line_without_newline, opener) { + open_fence = None; last_boundary = Some(offset + line.len()); } continue; } - if in_fence { + if let Some(opener) = parse_fence_opener(line_without_newline) { + open_fence = Some(opener); continue; } - if trimmed.is_empty() { + if line_without_newline.trim().is_empty() { last_boundary = Some(offset + line.len()); } } @@ -664,6 +838,46 @@ fn find_stream_safe_boundary(markdown: &str) -> Option { last_boundary } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct FenceMarker { + character: char, + length: usize, +} + +fn parse_fence_opener(line: &str) -> Option { + let indent = line.chars().take_while(|c| *c == ' ').count(); + if indent > 3 { + return None; + } + let rest = &line[indent..]; + let character = rest.chars().next()?; + if character != '`' && character != '~' { + return None; + } + let length = rest.chars().take_while(|c| *c == character).count(); + if length < 3 { + return None; + } + let info_string = &rest[length..]; + if character == '`' && info_string.contains('`') { + return None; + } + Some(FenceMarker { character, length }) +} + +fn line_closes_fence(line: &str, opener: FenceMarker) -> bool { + let indent = line.chars().take_while(|c| *c == ' ').count(); + if indent > 3 { + return false; + } + let rest = &line[indent..]; + let length = rest.chars().take_while(|c| *c == opener.character).count(); + if length < opener.length { + return false; + } + rest[length..].chars().all(|c| c == ' ' || c == '\t') +} + fn visible_width(input: &str) -> usize { strip_ansi(input).chars().count() } @@ -778,6 +992,60 @@ mod tests { assert!(strip_ansi(&code).contains("fn main()")); } + #[test] + fn streaming_state_holds_outer_fence_with_nested_inner_fence() { + let renderer = TerminalRenderer::new(); + let mut state = MarkdownStreamState::default(); + + assert_eq!( + state.push(&renderer, "````markdown\n```rust\nfn inner() {}\n"), + None, + "inner triple backticks must not close the outer four-backtick fence" + ); + assert_eq!( + state.push(&renderer, "```\n"), + None, + "closing the inner fence must not flush the outer fence" + ); + let flushed = state + .push(&renderer, "````\n") + .expect("closing the outer four-backtick fence flushes the buffered block"); + let plain_text = strip_ansi(&flushed); + assert!(plain_text.contains("fn inner()")); + assert!(plain_text.contains("```rust")); + } + + #[test] + fn streaming_state_distinguishes_backtick_and_tilde_fences() { + let renderer = TerminalRenderer::new(); + let mut state = MarkdownStreamState::default(); + + assert_eq!(state.push(&renderer, "~~~text\n"), None); + assert_eq!( + state.push(&renderer, "```\nstill inside tilde fence\n"), + None, + "a backtick fence cannot close a tilde-opened fence" + ); + assert_eq!(state.push(&renderer, "```\n"), None); + let flushed = state + .push(&renderer, "~~~\n") + .expect("matching tilde marker closes the fence"); + let plain_text = strip_ansi(&flushed); + assert!(plain_text.contains("still inside tilde fence")); + } + + #[test] + fn renders_nested_fenced_code_block_preserves_inner_markers() { + let terminal_renderer = TerminalRenderer::new(); + let markdown_output = + terminal_renderer.markdown_to_ansi("````markdown\n```rust\nfn nested() {}\n```\n````"); + let plain_text = strip_ansi(&markdown_output); + + assert!(plain_text.contains("╭─ markdown")); + assert!(plain_text.contains("```rust")); + assert!(plain_text.contains("fn nested()")); + } + #[test] fn spinner_advances_frames() { let terminal_renderer = TerminalRenderer::new(); diff --git a/rust/crates/rusty-claude-cli/tests/cli_flags_and_config_defaults.rs b/rust/crates/rusty-claude-cli/tests/cli_flags_and_config_defaults.rs index 9d574c4a..21a93e27 100644 --- a/rust/crates/rusty-claude-cli/tests/cli_flags_and_config_defaults.rs +++ b/rust/crates/rusty-claude-cli/tests/cli_flags_and_config_defaults.rs @@ -104,6 +104,31 @@ fn slash_command_names_match_known_commands_and_suggest_nearby_unknown_ones() { fs::remove_dir_all(temp_dir).expect("cleanup temp dir"); } +#[test] +fn omc_namespaced_slash_commands_surface_a_targeted_compatibility_hint() { + let temp_dir = unique_temp_dir("slash-dispatch-omc"); + fs::create_dir_all(&temp_dir).expect("temp dir should exist"); + + let output = Command::new(env!("CARGO_BIN_EXE_claw")) + .current_dir(&temp_dir) + .arg("/oh-my-claudecode:hud") + .output() + .expect("claw should launch"); + + assert!( + !output.status.success(), + "stdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + let stderr = String::from_utf8(output.stderr).expect("stderr should be utf8"); + assert!(stderr.contains("unknown slash command outside the REPL: /oh-my-claudecode:hud")); + assert!(stderr.contains("Claude Code/OMC plugin command")); + assert!(stderr.contains("does not yet load plugin slash commands")); + + fs::remove_dir_all(temp_dir).expect("cleanup temp dir"); +} + #[test] fn config_command_loads_defaults_from_standard_config_locations() { // given @@ -160,6 +185,79 @@ fn config_command_loads_defaults_from_standard_config_locations() { fs::remove_dir_all(temp_dir).expect("cleanup temp dir"); } +#[test] +fn doctor_command_runs_as_a_local_shell_entrypoint() { + // given + let temp_dir = unique_temp_dir("doctor-entrypoint"); + let config_home = temp_dir.join("home").join(".claw"); + fs::create_dir_all(&config_home).expect("config home should exist"); + + // when + let output = command_in(&temp_dir) + .env("CLAW_CONFIG_HOME", &config_home) + .env_remove("ANTHROPIC_API_KEY") + .env_remove("ANTHROPIC_AUTH_TOKEN") + .env("ANTHROPIC_BASE_URL", "http://127.0.0.1:9") + .arg("doctor") + .output() + .expect("claw doctor should launch"); + + // then + assert_success(&output); + let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8"); + assert!(stdout.contains("Doctor")); + assert!(stdout.contains("Auth")); + assert!(stdout.contains("Config")); + assert!(stdout.contains("Workspace")); + assert!(stdout.contains("Sandbox")); + assert!(!stdout.contains("Thinking")); + + fs::remove_dir_all(temp_dir).expect("cleanup temp dir"); +} + +#[test] +fn local_subcommand_help_does_not_fall_through_to_runtime_or_provider_calls() { + let temp_dir = unique_temp_dir("subcommand-help"); + let config_home = temp_dir.join("home").join(".claw"); + fs::create_dir_all(&config_home).expect("config home should exist"); + + let doctor_help = command_in(&temp_dir) + .env("CLAW_CONFIG_HOME", &config_home) + .env_remove("ANTHROPIC_API_KEY") + .env_remove("ANTHROPIC_AUTH_TOKEN") + .env("ANTHROPIC_BASE_URL", "http://127.0.0.1:9") + .args(["doctor", "--help"]) + .output() + .expect("doctor help should launch"); + let status_help = command_in(&temp_dir) + .env("CLAW_CONFIG_HOME", &config_home) + .env_remove("ANTHROPIC_API_KEY") + .env_remove("ANTHROPIC_AUTH_TOKEN") + .env("ANTHROPIC_BASE_URL", "http://127.0.0.1:9") + .args(["status", "--help"]) + .output() + .expect("status help should launch"); + + assert_success(&doctor_help); + let doctor_stdout = String::from_utf8(doctor_help.stdout).expect("stdout should be utf8"); + assert!(doctor_stdout.contains("Usage claw doctor")); + assert!(doctor_stdout.contains("local-only health report")); + assert!(!doctor_stdout.contains("Thinking")); + + assert_success(&status_help); + let status_stdout = String::from_utf8(status_help.stdout).expect("stdout should be utf8"); + assert!(status_stdout.contains("Usage claw status")); + assert!(status_stdout.contains("local workspace snapshot")); + assert!(!status_stdout.contains("Thinking")); + + let doctor_stderr = String::from_utf8(doctor_help.stderr).expect("stderr should be utf8"); + let status_stderr = String::from_utf8(status_help.stderr).expect("stderr should be utf8"); + assert!(!doctor_stderr.contains("auth_unavailable")); + assert!(!status_stderr.contains("auth_unavailable")); + + fs::remove_dir_all(temp_dir).expect("cleanup temp dir"); +} + fn command_in(cwd: &Path) -> Command { let mut command = Command::new(env!("CARGO_BIN_EXE_claw")); command.current_dir(cwd); diff --git a/rust/crates/rusty-claude-cli/tests/compact_output.rs b/rust/crates/rusty-claude-cli/tests/compact_output.rs new file mode 100644 index 00000000..456862fa --- /dev/null +++ b/rust/crates/rusty-claude-cli/tests/compact_output.rs @@ -0,0 +1,159 @@ +use std::fs; +use std::path::PathBuf; +use std::process::{Command, Output}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use mock_anthropic_service::{MockAnthropicService, SCENARIO_PREFIX}; + +static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0); + +#[test] +fn compact_flag_prints_only_final_assistant_text_without_tool_call_details() { + // given a workspace pointed at the mock Anthropic service and a fixture file + // that the read_file_roundtrip scenario will fetch through a tool call + let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build"); + let server = runtime + .block_on(MockAnthropicService::spawn()) + .expect("mock service should start"); + let base_url = server.base_url(); + + let workspace = unique_temp_dir("compact-read-file"); + let config_home = workspace.join("config-home"); + let home = workspace.join("home"); + fs::create_dir_all(&workspace).expect("workspace should exist"); + fs::create_dir_all(&config_home).expect("config home should exist"); + fs::create_dir_all(&home).expect("home should exist"); + fs::write(workspace.join("fixture.txt"), "alpha parity line\n").expect("fixture should write"); + + // when we run claw in compact text mode against a tool-using scenario + let prompt = format!("{SCENARIO_PREFIX}read_file_roundtrip"); + let output = run_claw( + &workspace, + &config_home, + &home, + &base_url, + &[ + "--model", + "sonnet", + "--permission-mode", + "read-only", + "--allowedTools", + "read_file", + "--compact", + &prompt, + ], + ); + + // then the command exits successfully and stdout contains exactly the final + // assistant text with no tool call IDs, JSON envelopes, or spinner output + assert!( + output.status.success(), + "compact run should succeed\nstdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ); + let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8"); + let trimmed = stdout.trim_end_matches('\n'); + assert_eq!( + trimmed, "read_file roundtrip complete: alpha parity line", + "compact stdout should contain only the final assistant text" + ); + assert!( + !stdout.contains("toolu_"), + "compact stdout must not leak tool_use_id ({stdout:?})" + ); + assert!( + !stdout.contains("\"tool_uses\""), + "compact stdout must not leak json envelopes ({stdout:?})" + ); + assert!( + !stdout.contains("Thinking"), + "compact stdout must not include the spinner banner ({stdout:?})" + ); + + fs::remove_dir_all(&workspace).expect("workspace cleanup should succeed"); +} + +#[test] +fn compact_flag_streaming_text_only_emits_final_message_text() { + // given a workspace pointed at the mock Anthropic service running the + // streaming_text scenario which only emits a single assistant text block + let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build"); + let server = runtime + .block_on(MockAnthropicService::spawn()) + .expect("mock service should start"); + let base_url = server.base_url(); + + let workspace = unique_temp_dir("compact-streaming-text"); + let config_home = workspace.join("config-home"); + let home = workspace.join("home"); + fs::create_dir_all(&workspace).expect("workspace should exist"); + fs::create_dir_all(&config_home).expect("config home should exist"); + fs::create_dir_all(&home).expect("home should exist"); + + // when we invoke claw with --compact for the streaming text scenario + let prompt = format!("{SCENARIO_PREFIX}streaming_text"); + let output = run_claw( + &workspace, + &config_home, + &home, + &base_url, + &[ + "--model", + "sonnet", + "--permission-mode", + "read-only", + "--compact", + &prompt, + ], + ); + + // then stdout should be exactly the assistant text followed by a newline + assert!( + output.status.success(), + "compact streaming run should succeed\nstdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ); + let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8"); + assert_eq!( + stdout, "Mock streaming says hello from the parity harness.\n", + "compact streaming stdout should contain only the final assistant text" + ); + + fs::remove_dir_all(&workspace).expect("workspace cleanup should succeed"); +} + +fn run_claw( + cwd: &std::path::Path, + config_home: &std::path::Path, + home: &std::path::Path, + base_url: &str, + args: &[&str], +) -> Output { + let mut command = Command::new(env!("CARGO_BIN_EXE_claw")); + command + .current_dir(cwd) + .env_clear() + .env("ANTHROPIC_API_KEY", "test-compact-key") + .env("ANTHROPIC_BASE_URL", base_url) + .env("CLAW_CONFIG_HOME", config_home) + .env("HOME", home) + .env("NO_COLOR", "1") + .env("PATH", "/usr/bin:/bin") + .args(args); + command.output().expect("claw should launch") +} + +fn unique_temp_dir(label: &str) -> PathBuf { + let millis = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("clock should be after epoch") + .as_millis(); + let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed); + std::env::temp_dir().join(format!( + "claw-compact-{label}-{}-{millis}-{counter}", + std::process::id() + )) +} diff --git a/rust/crates/rusty-claude-cli/tests/mock_parity_harness.rs b/rust/crates/rusty-claude-cli/tests/mock_parity_harness.rs index b5599068..4091613f 100644 --- a/rust/crates/rusty-claude-cli/tests/mock_parity_harness.rs +++ b/rust/crates/rusty-claude-cli/tests/mock_parity_harness.rs @@ -729,8 +729,7 @@ fn assert_token_cost_reporting(_: &HarnessWorkspace, run: &ScenarioRun) { assert!( run.response["estimated_cost"] .as_str() - .map(|cost| cost.starts_with('$')) - .unwrap_or(false), + .is_some_and(|cost| cost.starts_with('$')), "estimated_cost should be a dollar-prefixed string" ); } diff --git a/rust/crates/rusty-claude-cli/tests/output_format_contract.rs b/rust/crates/rusty-claude-cli/tests/output_format_contract.rs new file mode 100644 index 00000000..d0eb809c --- /dev/null +++ b/rust/crates/rusty-claude-cli/tests/output_format_contract.rs @@ -0,0 +1,428 @@ +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::Value; + +static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0); + +#[test] +fn help_emits_json_when_requested() { + let root = unique_temp_dir("help-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let parsed = assert_json_command(&root, &["--output-format", "json", "help"]); + assert_eq!(parsed["kind"], "help"); + assert!(parsed["message"] + .as_str() + .expect("help text") + .contains("Usage:")); +} + +#[test] +fn version_emits_json_when_requested() { + let root = unique_temp_dir("version-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let parsed = assert_json_command(&root, &["--output-format", "json", "version"]); + assert_eq!(parsed["kind"], "version"); + assert_eq!(parsed["version"], env!("CARGO_PKG_VERSION")); +} + +#[test] +fn status_and_sandbox_emit_json_when_requested() { + let root = unique_temp_dir("status-sandbox-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let status = assert_json_command(&root, &["--output-format", "json", "status"]); + assert_eq!(status["kind"], "status"); + assert!(status["workspace"]["cwd"].as_str().is_some()); + + let sandbox = assert_json_command(&root, &["--output-format", "json", "sandbox"]); + assert_eq!(sandbox["kind"], "sandbox"); + assert!(sandbox["filesystem_mode"].as_str().is_some()); +} + +#[test] +fn inventory_commands_emit_structured_json_when_requested() { + let root = unique_temp_dir("inventory-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let isolated_home = root.join("home"); + let isolated_config = root.join("config-home"); + let isolated_codex = root.join("codex-home"); + fs::create_dir_all(&isolated_home).expect("isolated home should exist"); + + let agents = assert_json_command_with_env( + &root, + &["--output-format", "json", "agents"], + &[ + ("HOME", isolated_home.to_str().expect("utf8 home")), + ( + "CLAW_CONFIG_HOME", + isolated_config.to_str().expect("utf8 config home"), + ), + ( + "CODEX_HOME", + isolated_codex.to_str().expect("utf8 codex home"), + ), + ], + ); + assert_eq!(agents["kind"], "agents"); + assert_eq!(agents["action"], "list"); + assert_eq!(agents["count"], 0); + assert_eq!(agents["summary"]["active"], 0); + assert!(agents["agents"] + .as_array() + .expect("agents array") + .is_empty()); + + let mcp = assert_json_command(&root, &["--output-format", "json", "mcp"]); + assert_eq!(mcp["kind"], "mcp"); + assert_eq!(mcp["action"], "list"); + + let skills = assert_json_command(&root, &["--output-format", "json", "skills"]); + assert_eq!(skills["kind"], "skills"); + assert_eq!(skills["action"], "list"); +} + +#[test] +fn agents_command_emits_structured_agent_entries_when_requested() { + let root = unique_temp_dir("agents-json-populated"); + let workspace = root.join("workspace"); + let project_agents = workspace.join(".codex").join("agents"); + let home = root.join("home"); + let user_agents = home.join(".codex").join("agents"); + let isolated_config = root.join("config-home"); + let isolated_codex = root.join("codex-home"); + fs::create_dir_all(&workspace).expect("workspace should exist"); + write_agent( + &project_agents, + "planner", + "Project planner", + "gpt-5.4", + "medium", + ); + write_agent( + &project_agents, + "verifier", + "Verification agent", + "gpt-5.4-mini", + "high", + ); + write_agent( + &user_agents, + "planner", + "User planner", + "gpt-5.4-mini", + "high", + ); + + let parsed = assert_json_command_with_env( + &workspace, + &["--output-format", "json", "agents"], + &[ + ("HOME", home.to_str().expect("utf8 home")), + ( + "CLAW_CONFIG_HOME", + isolated_config.to_str().expect("utf8 config home"), + ), + ( + "CODEX_HOME", + isolated_codex.to_str().expect("utf8 codex home"), + ), + ], + ); + + assert_eq!(parsed["kind"], "agents"); + assert_eq!(parsed["action"], "list"); + assert_eq!(parsed["count"], 3); + assert_eq!(parsed["summary"]["active"], 2); + assert_eq!(parsed["summary"]["shadowed"], 1); + assert_eq!(parsed["agents"][0]["name"], "planner"); + assert_eq!(parsed["agents"][0]["source"]["id"], "project_claw"); + assert_eq!(parsed["agents"][0]["active"], true); + assert_eq!(parsed["agents"][1]["name"], "verifier"); + assert_eq!(parsed["agents"][2]["name"], "planner"); + assert_eq!(parsed["agents"][2]["active"], false); + assert_eq!(parsed["agents"][2]["shadowed_by"]["id"], "project_claw"); +} + +#[test] +fn bootstrap_and_system_prompt_emit_json_when_requested() { + let root = unique_temp_dir("bootstrap-system-prompt-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let plan = assert_json_command(&root, &["--output-format", "json", "bootstrap-plan"]); + assert_eq!(plan["kind"], "bootstrap-plan"); + assert!(plan["phases"].as_array().expect("phases").len() > 1); + + let prompt = assert_json_command(&root, &["--output-format", "json", "system-prompt"]); + assert_eq!(prompt["kind"], "system-prompt"); + assert!(prompt["message"] + .as_str() + .expect("prompt text") + .contains("interactive agent")); +} + +#[test] +fn dump_manifests_and_init_emit_json_when_requested() { + let root = unique_temp_dir("manifest-init-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let upstream = write_upstream_fixture(&root); + let manifests = assert_json_command_with_env( + &root, + &["--output-format", "json", "dump-manifests"], + &[( + "CLAUDE_CODE_UPSTREAM", + upstream.to_str().expect("utf8 upstream"), + )], + ); + assert_eq!(manifests["kind"], "dump-manifests"); + assert_eq!(manifests["commands"], 1); + assert_eq!(manifests["tools"], 1); + + let workspace = root.join("workspace"); + fs::create_dir_all(&workspace).expect("workspace should exist"); + let init = assert_json_command(&workspace, &["--output-format", "json", "init"]); + assert_eq!(init["kind"], "init"); + assert!(workspace.join("CLAUDE.md").exists()); +} + +#[test] +fn doctor_and_resume_status_emit_json_when_requested() { + let root = unique_temp_dir("doctor-resume-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let doctor = assert_json_command(&root, &["--output-format", "json", "doctor"]); + assert_eq!(doctor["kind"], "doctor"); + assert!(doctor["message"].is_string()); + let summary = doctor["summary"].as_object().expect("doctor summary"); + assert!(summary["ok"].as_u64().is_some()); + assert!(summary["warnings"].as_u64().is_some()); + assert!(summary["failures"].as_u64().is_some()); + + let checks = doctor["checks"].as_array().expect("doctor checks"); + assert_eq!(checks.len(), 5); + let check_names = checks + .iter() + .map(|check| { + assert!(check["status"].as_str().is_some()); + assert!(check["summary"].as_str().is_some()); + assert!(check["details"].is_array()); + check["name"].as_str().expect("doctor check name") + }) + .collect::>(); + assert_eq!( + check_names, + vec!["auth", "config", "workspace", "sandbox", "system"] + ); + + let workspace = checks + .iter() + .find(|check| check["name"] == "workspace") + .expect("workspace check"); + assert!(workspace["cwd"].as_str().is_some()); + assert!(workspace["in_git_repo"].is_boolean()); + + let sandbox = checks + .iter() + .find(|check| check["name"] == "sandbox") + .expect("sandbox check"); + assert!(sandbox["filesystem_mode"].as_str().is_some()); + assert!(sandbox["enabled"].is_boolean()); + assert!(sandbox["fallback_reason"].is_null() || sandbox["fallback_reason"].is_string()); + + let session_path = root.join("session.jsonl"); + fs::write( + &session_path, + "{\"type\":\"session_meta\",\"version\":3,\"session_id\":\"resume-json\",\"created_at_ms\":0,\"updated_at_ms\":0}\n{\"type\":\"message\",\"message\":{\"role\":\"user\",\"blocks\":[{\"type\":\"text\",\"text\":\"hello\"}]}}\n", + ) + .expect("session should write"); + let resumed = assert_json_command( + &root, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 session path"), + "/status", + ], + ); + assert_eq!(resumed["kind"], "status"); + assert_eq!(resumed["model"], "restored-session"); + assert_eq!(resumed["usage"]["messages"], 1); + assert!(resumed["workspace"]["cwd"].as_str().is_some()); + assert!(resumed["sandbox"]["filesystem_mode"].as_str().is_some()); +} + +#[test] +fn resumed_inventory_commands_emit_structured_json_when_requested() { + let root = unique_temp_dir("resume-inventory-json"); + let config_home = root.join("config-home"); + let home = root.join("home"); + fs::create_dir_all(&config_home).expect("config home should exist"); + fs::create_dir_all(&home).expect("home should exist"); + + let session_path = root.join("session.jsonl"); + fs::write( + &session_path, + "{\"type\":\"session_meta\",\"version\":3,\"session_id\":\"resume-inventory-json\",\"created_at_ms\":0,\"updated_at_ms\":0}\n{\"type\":\"message\",\"message\":{\"role\":\"user\",\"blocks\":[{\"type\":\"text\",\"text\":\"inventory\"}]}}\n", + ) + .expect("session should write"); + + let mcp = assert_json_command_with_env( + &root, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 session path"), + "/mcp", + ], + &[ + ( + "CLAW_CONFIG_HOME", + config_home.to_str().expect("utf8 config home"), + ), + ("HOME", home.to_str().expect("utf8 home")), + ], + ); + assert_eq!(mcp["kind"], "mcp"); + assert_eq!(mcp["action"], "list"); + assert!(mcp["servers"].is_array()); + + let skills = assert_json_command_with_env( + &root, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 session path"), + "/skills", + ], + &[ + ( + "CLAW_CONFIG_HOME", + config_home.to_str().expect("utf8 config home"), + ), + ("HOME", home.to_str().expect("utf8 home")), + ], + ); + assert_eq!(skills["kind"], "skills"); + assert_eq!(skills["action"], "list"); + assert!(skills["summary"]["total"].is_number()); + assert!(skills["skills"].is_array()); +} + +#[test] +fn resumed_version_and_init_emit_structured_json_when_requested() { + let root = unique_temp_dir("resume-version-init-json"); + fs::create_dir_all(&root).expect("temp dir should exist"); + + let session_path = root.join("session.jsonl"); + fs::write( + &session_path, + "{\"type\":\"session_meta\",\"version\":3,\"session_id\":\"resume-version-init-json\",\"created_at_ms\":0,\"updated_at_ms\":0}\n", + ) + .expect("session should write"); + + let version = assert_json_command( + &root, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 session path"), + "/version", + ], + ); + assert_eq!(version["kind"], "version"); + assert_eq!(version["version"], env!("CARGO_PKG_VERSION")); + + let init = assert_json_command( + &root, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 session path"), + "/init", + ], + ); + assert_eq!(init["kind"], "init"); + assert!(root.join("CLAUDE.md").exists()); +} + +fn assert_json_command(current_dir: &Path, args: &[&str]) -> Value { + assert_json_command_with_env(current_dir, args, &[]) +} + +fn assert_json_command_with_env(current_dir: &Path, args: &[&str], envs: &[(&str, &str)]) -> Value { + let output = run_claw(current_dir, args, envs); + assert!( + output.status.success(), + "stdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + serde_json::from_slice(&output.stdout).expect("stdout should be valid json") +} + +fn run_claw(current_dir: &Path, args: &[&str], envs: &[(&str, &str)]) -> Output { + let mut command = Command::new(env!("CARGO_BIN_EXE_claw")); + command.current_dir(current_dir).args(args); + for (key, value) in envs { + command.env(key, value); + } + command.output().expect("claw should launch") +} + +fn write_upstream_fixture(root: &Path) -> PathBuf { + let upstream = root.join("claw-code"); + let src = upstream.join("src"); + let entrypoints = src.join("entrypoints"); + fs::create_dir_all(&entrypoints).expect("upstream entrypoints dir should exist"); + fs::write( + src.join("commands.ts"), + "import FooCommand from './commands/foo'\n", + ) + .expect("commands fixture should write"); + fs::write( + src.join("tools.ts"), + "import ReadTool from './tools/read'\n", + ) + .expect("tools fixture should write"); + fs::write( + entrypoints.join("cli.tsx"), + "if (args[0] === '--version') {}\nstartupProfiler()\n", + ) + .expect("cli fixture should write"); + upstream +} + +fn write_agent(root: &Path, name: &str, description: &str, model: &str, reasoning: &str) { + fs::create_dir_all(root).expect("agent root should exist"); + fs::write( + root.join(format!("{name}.toml")), + format!( + "name = \"{name}\"\ndescription = \"{description}\"\nmodel = \"{model}\"\nmodel_reasoning_effort = \"{reasoning}\"\n" + ), + ) + .expect("agent fixture should write"); +} + +fn unique_temp_dir(label: &str) -> PathBuf { + let millis = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("clock should be after epoch") + .as_millis(); + let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed); + std::env::temp_dir().join(format!( + "claw-output-format-{label}-{}-{millis}-{counter}", + std::process::id() + )) +} diff --git a/rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs b/rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs index ccef95fa..640765d1 100644 --- a/rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs +++ b/rust/crates/rusty-claude-cli/tests/resume_slash_commands.rs @@ -7,6 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use runtime::ContentBlock; use runtime::Session; +use serde_json::Value; static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0); @@ -221,6 +222,102 @@ fn resume_latest_restores_the_most_recent_managed_session() { assert!(stdout.contains(newer_path.to_str().expect("utf8 path"))); } +#[test] +fn resumed_status_command_emits_structured_json_when_requested() { + // given + let temp_dir = unique_temp_dir("resume-status-json"); + fs::create_dir_all(&temp_dir).expect("temp dir should exist"); + let session_path = temp_dir.join("session.jsonl"); + + let mut session = Session::new(); + session + .push_user_text("resume status json fixture") + .expect("session write should succeed"); + session + .save_to_path(&session_path) + .expect("session should persist"); + + // when + let output = run_claw( + &temp_dir, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 path"), + "/status", + ], + ); + + // then + assert!( + output.status.success(), + "stdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8"); + let parsed: Value = + serde_json::from_str(stdout.trim()).expect("resume status output should be json"); + assert_eq!(parsed["kind"], "status"); + assert_eq!(parsed["model"], "restored-session"); + assert_eq!(parsed["permission_mode"], "danger-full-access"); + assert_eq!(parsed["usage"]["messages"], 1); + assert!(parsed["usage"]["turns"].is_number()); + assert!(parsed["workspace"]["cwd"].as_str().is_some()); + assert_eq!( + parsed["workspace"]["session"], + session_path.to_str().expect("utf8 path") + ); + assert!(parsed["workspace"]["changed_files"].is_number()); + assert_eq!(parsed["workspace"]["loaded_config_files"].as_u64(), Some(0)); + assert!(parsed["sandbox"]["filesystem_mode"].as_str().is_some()); +} + +#[test] +fn resumed_sandbox_command_emits_structured_json_when_requested() { + // given + let temp_dir = unique_temp_dir("resume-sandbox-json"); + fs::create_dir_all(&temp_dir).expect("temp dir should exist"); + let session_path = temp_dir.join("session.jsonl"); + + Session::new() + .save_to_path(&session_path) + .expect("session should persist"); + + // when + let output = run_claw( + &temp_dir, + &[ + "--output-format", + "json", + "--resume", + session_path.to_str().expect("utf8 path"), + "/sandbox", + ], + ); + + // then + assert!( + output.status.success(), + "stdout:\n{}\n\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8"); + let parsed: Value = + serde_json::from_str(stdout.trim()).expect("resume sandbox output should be json"); + assert_eq!(parsed["kind"], "sandbox"); + assert!(parsed["enabled"].is_boolean()); + assert!(parsed["active"].is_boolean()); + assert!(parsed["supported"].is_boolean()); + assert!(parsed["filesystem_mode"].as_str().is_some()); + assert!(parsed["allowed_mounts"].is_array()); + assert!(parsed["markers"].is_array()); +} + fn run_claw(current_dir: &Path, args: &[&str]) -> Output { run_claw_with_env(current_dir, args, &[]) } diff --git a/rust/crates/tools/Cargo.toml b/rust/crates/tools/Cargo.toml index 04d738b5..86da4e63 100644 --- a/rust/crates/tools/Cargo.toml +++ b/rust/crates/tools/Cargo.toml @@ -7,6 +7,8 @@ publish.workspace = true [dependencies] api = { path = "../api" } +commands = { path = "../commands" } +flate2 = "1" plugins = { path = "../plugins" } runtime = { path = "../runtime" } reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] } diff --git a/rust/crates/tools/src/lane_completion.rs b/rust/crates/tools/src/lane_completion.rs index 28501271..e4eecce7 100644 --- a/rust/crates/tools/src/lane_completion.rs +++ b/rust/crates/tools/src/lane_completion.rs @@ -16,9 +16,10 @@ use runtime::{ use crate::AgentOutput; /// Detects if a lane should be automatically marked as completed. -/// +/// /// Returns `Some(LaneContext)` with `completed = true` if all conditions met, /// `None` if lane should remain active. +#[allow(dead_code)] pub(crate) fn detect_lane_completion( output: &AgentOutput, test_green: bool, @@ -28,29 +29,29 @@ pub(crate) fn detect_lane_completion( if output.error.is_some() { return None; } - + // Must have finished status if !output.status.eq_ignore_ascii_case("completed") && !output.status.eq_ignore_ascii_case("finished") { return None; } - + // Must have no current blocker if output.current_blocker.is_some() { return None; } - + // Must have green tests if !test_green { return None; } - + // Must have pushed code if !has_pushed { return None; } - + // All conditions met — create completed context Some(LaneContext { lane_id: output.agent_id.clone(), @@ -65,9 +66,8 @@ pub(crate) fn detect_lane_completion( } /// Evaluates policy actions for a completed lane. -pub(crate) fn evaluate_completed_lane( - context: &LaneContext, -) -> Vec { +#[allow(dead_code)] +pub(crate) fn evaluate_completed_lane(context: &LaneContext) -> Vec { let engine = PolicyEngine::new(vec![ PolicyRule::new( "closeout-completed-lane", @@ -85,7 +85,7 @@ pub(crate) fn evaluate_completed_lane( 5, ), ]); - + evaluate(&engine, context) } @@ -108,57 +108,58 @@ mod tests { started_at: Some("2024-01-01T00:00:00Z".to_string()), completed_at: Some("2024-01-01T00:00:00Z".to_string()), lane_events: vec![], + derived_state: "working".to_string(), current_blocker: None, error: None, } } - + #[test] fn detects_completion_when_all_conditions_met() { let output = test_output(); let result = detect_lane_completion(&output, true, true); - + assert!(result.is_some()); let context = result.unwrap(); assert!(context.completed); assert_eq!(context.green_level, 3); assert_eq!(context.blocker, LaneBlocker::None); } - + #[test] fn no_completion_when_error_present() { let mut output = test_output(); output.error = Some("Build failed".to_string()); - + let result = detect_lane_completion(&output, true, true); assert!(result.is_none()); } - + #[test] fn no_completion_when_not_finished() { let mut output = test_output(); output.status = "Running".to_string(); - + let result = detect_lane_completion(&output, true, true); assert!(result.is_none()); } - + #[test] fn no_completion_when_tests_not_green() { let output = test_output(); - + let result = detect_lane_completion(&output, false, true); assert!(result.is_none()); } - + #[test] fn no_completion_when_not_pushed() { let output = test_output(); - + let result = detect_lane_completion(&output, true, false); assert!(result.is_none()); } - + #[test] fn evaluate_triggers_closeout_for_completed_lane() { let context = LaneContext { @@ -171,9 +172,9 @@ mod tests { completed: true, reconciled: false, }; - + let actions = evaluate_completed_lane(&context); - + assert!(actions.contains(&PolicyAction::CloseoutLane)); assert!(actions.contains(&PolicyAction::CleanupSession)); } diff --git a/rust/crates/tools/src/lib.rs b/rust/crates/tools/src/lib.rs index e686899d..9366963b 100644 --- a/rust/crates/tools/src/lib.rs +++ b/rust/crates/tools/src/lib.rs @@ -4,28 +4,29 @@ use std::process::Command; use std::time::{Duration, Instant}; use api::{ - max_tokens_for_model, resolve_model_alias, ContentBlockDelta, InputContentBlock, InputMessage, - MessageRequest, MessageResponse, OutputContentBlock, ProviderClient, + max_tokens_for_model, resolve_model_alias, ApiError, ContentBlockDelta, InputContentBlock, + InputMessage, MessageRequest, MessageResponse, OutputContentBlock, ProviderClient, StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock, }; use plugins::PluginTool; use reqwest::blocking::Client; use runtime::{ - check_freshness, edit_file, execute_bash, glob_search, grep_search, load_system_prompt, + check_freshness, dedupe_superseded_commit_events, edit_file, execute_bash, glob_search, + grep_search, load_system_prompt, lsp_client::LspRegistry, mcp_tool_bridge::McpToolRegistry, permission_enforcer::{EnforcementResult, PermissionEnforcer}, read_file, summary_compression::compress_summary_text, - TaskPacket, task_registry::TaskRegistry, team_cron_registry::{CronRegistry, TeamRegistry}, worker_boot::{WorkerReadySnapshot, WorkerRegistry}, write_file, ApiClient, ApiRequest, AssistantEvent, BashCommandInput, BashCommandOutput, - BranchFreshness, ContentBlock, ConversationMessage, ConversationRuntime, GrepSearchInput, - LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass, - McpDegradedReport, MessageRole, PermissionMode, PermissionPolicy, PromptCacheEvent, - RuntimeError, Session, ToolError, ToolExecutor, + BranchFreshness, ConfigLoader, ContentBlock, ConversationMessage, ConversationRuntime, + GrepSearchInput, LaneCommitProvenance, LaneEvent, LaneEventBlocker, LaneEventName, + LaneEventStatus, LaneFailureClass, McpDegradedReport, MessageRole, PermissionMode, + PermissionPolicy, PromptCacheEvent, ProviderFallbackConfig, RuntimeError, Session, TaskPacket, + ToolError, ToolExecutor, }; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; @@ -1705,7 +1706,7 @@ fn run_remote_trigger(input: RemoteTriggerInput) -> Result { "method": method, "status_code": status, "body": truncated_body, - "success": status >= 200 && status < 300 + "success": (200..300).contains(&status) })) } Err(e) => to_pretty_json(json!({ @@ -1878,27 +1879,25 @@ fn branch_divergence_output( dangerously_disable_sandbox: None, return_code_interpretation: Some("preflight_blocked:branch_divergence".to_string()), no_output_expected: Some(false), - structured_content: Some(vec![ - serde_json::to_value( - LaneEvent::new( - LaneEventName::BranchStaleAgainstMain, - LaneEventStatus::Blocked, - iso8601_now(), - ) - .with_failure_class(LaneFailureClass::BranchDivergence) - .with_detail(stderr.clone()) - .with_data(json!({ - "branch": branch, - "mainRef": main_ref, - "commitsBehind": commits_behind, - "commitsAhead": commits_ahead, - "missingCommits": missing_fixes, - "blockedCommand": command, - "recommendedAction": format!("merge or rebase {main_ref} before workspace tests") - })), + structured_content: Some(vec![serde_json::to_value( + LaneEvent::new( + LaneEventName::BranchStaleAgainstMain, + LaneEventStatus::Blocked, + iso8601_now(), ) - .expect("lane event should serialize"), - ]), + .with_failure_class(LaneFailureClass::BranchDivergence) + .with_detail(stderr.clone()) + .with_data(json!({ + "branch": branch, + "mainRef": main_ref, + "commitsBehind": commits_behind, + "commitsAhead": commits_ahead, + "missingCommits": missing_fixes, + "blockedCommand": command, + "recommendedAction": format!("merge or rebase {main_ref} before workspace tests") + })), + ) + .expect("lane event should serialize")]), persisted_output_path: None, persisted_output_size: None, sandbox_status: None, @@ -2368,6 +2367,8 @@ struct AgentOutput { lane_events: Vec, #[serde(rename = "currentBlocker", skip_serializing_if = "Option::is_none")] current_blocker: Option, + #[serde(rename = "derivedState")] + derived_state: String, #[serde(skip_serializing_if = "Option::is_none")] error: Option, } @@ -2973,49 +2974,266 @@ fn todo_store_path() -> Result { } fn resolve_skill_path(skill: &str) -> Result { + let cwd = std::env::current_dir().map_err(|error| error.to_string())?; + match commands::resolve_skill_path(&cwd, skill) { + Ok(path) => Ok(path), + Err(_) => resolve_skill_path_from_compat_roots(skill), + } +} + +fn resolve_skill_path_from_compat_roots(skill: &str) -> Result { let requested = skill.trim().trim_start_matches('/').trim_start_matches('$'); if requested.is_empty() { return Err(String::from("skill must not be empty")); } - let mut candidates = Vec::new(); - if let Ok(codex_home) = std::env::var("CODEX_HOME") { - candidates.push(std::path::PathBuf::from(codex_home).join("skills")); - } - if let Ok(home) = std::env::var("HOME") { - let home = std::path::PathBuf::from(home); - candidates.push(home.join(".agents").join("skills")); - candidates.push(home.join(".config").join("opencode").join("skills")); - candidates.push(home.join(".codex").join("skills")); - } - candidates.push(std::path::PathBuf::from("/home/bellman/.codex/skills")); - - for root in candidates { - let direct = root.join(requested).join("SKILL.md"); - if direct.exists() { - return Ok(direct); - } - - if let Ok(entries) = std::fs::read_dir(&root) { - for entry in entries.flatten() { - let path = entry.path().join("SKILL.md"); - if !path.exists() { - continue; - } - if entry - .file_name() - .to_string_lossy() - .eq_ignore_ascii_case(requested) - { - return Ok(path); - } - } + for root in skill_lookup_roots() { + if let Some(path) = resolve_skill_path_in_root(&root, requested) { + return Ok(path); } } Err(format!("unknown skill: {requested}")) } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum SkillLookupOrigin { + SkillsDir, + LegacyCommandsDir, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct SkillLookupRoot { + path: std::path::PathBuf, + origin: SkillLookupOrigin, +} + +fn skill_lookup_roots() -> Vec { + let mut roots = Vec::new(); + + if let Ok(cwd) = std::env::current_dir() { + push_project_skill_lookup_roots(&mut roots, &cwd); + } + + if let Ok(claw_config_home) = std::env::var("CLAW_CONFIG_HOME") { + push_prefixed_skill_lookup_roots(&mut roots, std::path::Path::new(&claw_config_home)); + } + if let Ok(codex_home) = std::env::var("CODEX_HOME") { + push_prefixed_skill_lookup_roots(&mut roots, std::path::Path::new(&codex_home)); + } + if let Ok(home) = std::env::var("HOME") { + push_home_skill_lookup_roots(&mut roots, std::path::Path::new(&home)); + } + if let Ok(claude_config_dir) = std::env::var("CLAUDE_CONFIG_DIR") { + let claude_config_dir = std::path::PathBuf::from(claude_config_dir); + push_skill_lookup_root( + &mut roots, + claude_config_dir.join("skills"), + SkillLookupOrigin::SkillsDir, + ); + push_skill_lookup_root( + &mut roots, + claude_config_dir.join("skills").join("omc-learned"), + SkillLookupOrigin::SkillsDir, + ); + push_skill_lookup_root( + &mut roots, + claude_config_dir.join("commands"), + SkillLookupOrigin::LegacyCommandsDir, + ); + } + push_skill_lookup_root( + &mut roots, + std::path::PathBuf::from("/home/bellman/.claw/skills"), + SkillLookupOrigin::SkillsDir, + ); + push_skill_lookup_root( + &mut roots, + std::path::PathBuf::from("/home/bellman/.codex/skills"), + SkillLookupOrigin::SkillsDir, + ); + + roots +} + +fn push_project_skill_lookup_roots(roots: &mut Vec, cwd: &std::path::Path) { + for ancestor in cwd.ancestors() { + push_prefixed_skill_lookup_roots(roots, &ancestor.join(".omc")); + push_prefixed_skill_lookup_roots(roots, &ancestor.join(".agents")); + push_prefixed_skill_lookup_roots(roots, &ancestor.join(".claw")); + push_prefixed_skill_lookup_roots(roots, &ancestor.join(".codex")); + push_prefixed_skill_lookup_roots(roots, &ancestor.join(".claude")); + } +} + +fn push_home_skill_lookup_roots(roots: &mut Vec, home: &std::path::Path) { + push_prefixed_skill_lookup_roots(roots, &home.join(".omc")); + push_prefixed_skill_lookup_roots(roots, &home.join(".claw")); + push_prefixed_skill_lookup_roots(roots, &home.join(".codex")); + push_prefixed_skill_lookup_roots(roots, &home.join(".claude")); + push_skill_lookup_root( + roots, + home.join(".agents").join("skills"), + SkillLookupOrigin::SkillsDir, + ); + push_skill_lookup_root( + roots, + home.join(".config").join("opencode").join("skills"), + SkillLookupOrigin::SkillsDir, + ); + push_skill_lookup_root( + roots, + home.join(".claude").join("skills").join("omc-learned"), + SkillLookupOrigin::SkillsDir, + ); +} + +fn push_prefixed_skill_lookup_roots(roots: &mut Vec, prefix: &std::path::Path) { + push_skill_lookup_root(roots, prefix.join("skills"), SkillLookupOrigin::SkillsDir); + push_skill_lookup_root( + roots, + prefix.join("commands"), + SkillLookupOrigin::LegacyCommandsDir, + ); +} + +fn push_skill_lookup_root( + roots: &mut Vec, + path: std::path::PathBuf, + origin: SkillLookupOrigin, +) { + if path.is_dir() && !roots.iter().any(|existing| existing.path == path) { + roots.push(SkillLookupRoot { path, origin }); + } +} + +fn resolve_skill_path_in_root( + root: &SkillLookupRoot, + requested: &str, +) -> Option { + match root.origin { + SkillLookupOrigin::SkillsDir => resolve_skill_path_in_skills_dir(&root.path, requested), + SkillLookupOrigin::LegacyCommandsDir => { + resolve_skill_path_in_legacy_commands_dir(&root.path, requested) + } + } +} + +fn resolve_skill_path_in_skills_dir( + root: &std::path::Path, + requested: &str, +) -> Option { + let direct = root.join(requested).join("SKILL.md"); + if direct.is_file() { + return Some(direct); + } + + let entries = std::fs::read_dir(root).ok()?; + for entry in entries.flatten() { + if !entry.path().is_dir() { + continue; + } + let skill_path = entry.path().join("SKILL.md"); + if !skill_path.is_file() { + continue; + } + if entry + .file_name() + .to_string_lossy() + .eq_ignore_ascii_case(requested) + || skill_frontmatter_name_matches(&skill_path, requested) + { + return Some(skill_path); + } + } + + None +} + +fn resolve_skill_path_in_legacy_commands_dir( + root: &std::path::Path, + requested: &str, +) -> Option { + let direct_dir = root.join(requested).join("SKILL.md"); + if direct_dir.is_file() { + return Some(direct_dir); + } + + let direct_markdown = root.join(format!("{requested}.md")); + if direct_markdown.is_file() { + return Some(direct_markdown); + } + + let entries = std::fs::read_dir(root).ok()?; + for entry in entries.flatten() { + let path = entry.path(); + let candidate_path = if path.is_dir() { + let skill_path = path.join("SKILL.md"); + if !skill_path.is_file() { + continue; + } + skill_path + } else if path + .extension() + .is_some_and(|ext| ext.to_string_lossy().eq_ignore_ascii_case("md")) + { + path + } else { + continue; + }; + + let matches_entry_name = candidate_path + .file_stem() + .is_some_and(|stem| stem.to_string_lossy().eq_ignore_ascii_case(requested)) + || entry + .file_name() + .to_string_lossy() + .trim_end_matches(".md") + .eq_ignore_ascii_case(requested); + if matches_entry_name || skill_frontmatter_name_matches(&candidate_path, requested) { + return Some(candidate_path); + } + } + + None +} + +fn skill_frontmatter_name_matches(path: &std::path::Path, requested: &str) -> bool { + std::fs::read_to_string(path) + .ok() + .and_then(|contents| parse_skill_name(&contents)) + .is_some_and(|name| name.eq_ignore_ascii_case(requested)) +} + +fn parse_skill_name(contents: &str) -> Option { + parse_skill_frontmatter_value(contents, "name") +} + +fn parse_skill_frontmatter_value(contents: &str, key: &str) -> Option { + let mut lines = contents.lines(); + if lines.next().map(str::trim) != Some("---") { + return None; + } + + for line in lines { + let trimmed = line.trim(); + if trimmed == "---" { + break; + } + if let Some(value) = trimmed.strip_prefix(&format!("{key}:")) { + let value = value + .trim() + .trim_matches(|ch| matches!(ch, '"' | '\'')) + .trim(); + if !value.is_empty() { + return Some(value.to_string()); + } + } + } + + None +} + const DEFAULT_AGENT_MODEL: &str = "claude-opus-4-6"; const DEFAULT_AGENT_SYSTEM_DATE: &str = "2026-03-31"; const DEFAULT_AGENT_MAX_ITERATIONS: usize = 32; @@ -3083,6 +3301,7 @@ where completed_at: None, lane_events: vec![LaneEvent::started(iso8601_now())], current_blocker: None, + derived_state: String::from("working"), error: None, }; write_agent_manifest(&manifest)?; @@ -3273,9 +3492,11 @@ fn agent_permission_policy() -> PermissionPolicy { } fn write_agent_manifest(manifest: &AgentOutput) -> Result<(), String> { + let mut normalized = manifest.clone(); + normalized.lane_events = dedupe_superseded_commit_events(&normalized.lane_events); std::fs::write( - &manifest.manifest_file, - serde_json::to_string_pretty(manifest).map_err(|error| error.to_string())?, + &normalized.manifest_file, + serde_json::to_string_pretty(&normalized).map_err(|error| error.to_string())?, ) .map_err(|error| error.to_string()) } @@ -3294,15 +3515,17 @@ fn persist_agent_terminal_state( let mut next_manifest = manifest.clone(); next_manifest.status = status.to_string(); next_manifest.completed_at = Some(iso8601_now()); - next_manifest.current_blocker = blocker.clone(); + next_manifest.current_blocker.clone_from(&blocker); + next_manifest.derived_state = + derive_agent_state(status, result, error.as_deref(), blocker.as_ref()).to_string(); next_manifest.error = error; if let Some(blocker) = blocker { - next_manifest.lane_events.push( - LaneEvent::blocked(iso8601_now(), &blocker), - ); - next_manifest.lane_events.push( - LaneEvent::failed(iso8601_now(), &blocker), - ); + next_manifest + .lane_events + .push(LaneEvent::blocked(iso8601_now(), &blocker)); + next_manifest + .lane_events + .push(LaneEvent::failed(iso8601_now(), &blocker)); } else { next_manifest.current_blocker = None; let compressed_detail = result @@ -3311,10 +3534,92 @@ fn persist_agent_terminal_state( next_manifest .lane_events .push(LaneEvent::finished(iso8601_now(), compressed_detail)); + if let Some(provenance) = maybe_commit_provenance(result) { + next_manifest.lane_events.push(LaneEvent::commit_created( + iso8601_now(), + Some(format!("commit {}", provenance.commit)), + provenance, + )); + } } write_agent_manifest(&next_manifest) } +fn derive_agent_state( + status: &str, + result: Option<&str>, + error: Option<&str>, + blocker: Option<&LaneEventBlocker>, +) -> &'static str { + let normalized_status = status.trim().to_ascii_lowercase(); + let normalized_error = error.unwrap_or_default().to_ascii_lowercase(); + + if normalized_status == "running" { + return "working"; + } + if normalized_status == "completed" { + return if result.is_some_and(|value| !value.trim().is_empty()) { + "finished_cleanable" + } else { + "finished_pending_report" + }; + } + if normalized_error.contains("background") { + return "blocked_background_job"; + } + if normalized_error.contains("merge conflict") || normalized_error.contains("cherry-pick") { + return "blocked_merge_conflict"; + } + if normalized_error.contains("mcp") { + return "degraded_mcp"; + } + if normalized_error.contains("transport") + || normalized_error.contains("broken pipe") + || normalized_error.contains("connection") + || normalized_error.contains("interrupted") + { + return "interrupted_transport"; + } + if blocker.is_some() { + return "truly_idle"; + } + "truly_idle" +} + +fn maybe_commit_provenance(result: Option<&str>) -> Option { + let commit = extract_commit_sha(result?)?; + let branch = current_git_branch().unwrap_or_else(|| "unknown".to_string()); + let worktree = std::env::current_dir() + .ok() + .map(|path| path.display().to_string()); + Some(LaneCommitProvenance { + commit: commit.clone(), + branch, + worktree, + canonical_commit: Some(commit.clone()), + superseded_by: None, + lineage: vec![commit], + }) +} + +fn extract_commit_sha(result: &str) -> Option { + result + .split(|c: char| !c.is_ascii_hexdigit()) + .find(|token| token.len() >= 7 && token.len() <= 40) + .map(str::to_string) +} + +fn current_git_branch() -> Option { + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output() + .ok()?; + output + .status + .success() + .then(|| String::from_utf8_lossy(&output.stdout).trim().to_string()) +} + fn append_agent_output(path: &str, suffix: &str) -> Result<(), String> { use std::io::Write as _; @@ -3395,29 +3700,73 @@ fn classify_lane_failure(error: &str) -> LaneFailureClass { } } +struct ProviderEntry { + model: String, + client: ProviderClient, +} + struct ProviderRuntimeClient { runtime: tokio::runtime::Runtime, - client: ProviderClient, - model: String, + chain: Vec, allowed_tools: BTreeSet, } impl ProviderRuntimeClient { #[allow(clippy::needless_pass_by_value)] fn new(model: String, allowed_tools: BTreeSet) -> Result { - let model = resolve_model_alias(&model).clone(); - let client = ProviderClient::from_model(&model).map_err(|error| error.to_string())?; + let fallback_config = load_provider_fallback_config(); + Self::new_with_fallback_config(model, allowed_tools, &fallback_config) + } + + #[allow(clippy::needless_pass_by_value)] + fn new_with_fallback_config( + model: String, + allowed_tools: BTreeSet, + fallback_config: &ProviderFallbackConfig, + ) -> Result { + let primary_model = fallback_config + .primary() + .map(str::to_string) + .unwrap_or(model); + let primary = build_provider_entry(&primary_model)?; + let mut chain = vec![primary]; + for fallback_model in fallback_config.fallbacks() { + match build_provider_entry(fallback_model) { + Ok(entry) => chain.push(entry), + Err(error) => { + eprintln!( + "warning: skipping unavailable fallback provider {fallback_model}: {error}" + ); + } + } + } Ok(Self { runtime: tokio::runtime::Runtime::new().map_err(|error| error.to_string())?, - client, - model, + chain, allowed_tools, }) } } +fn build_provider_entry(model: &str) -> Result { + let resolved = resolve_model_alias(model).clone(); + let client = ProviderClient::from_model(&resolved).map_err(|error| error.to_string())?; + Ok(ProviderEntry { + model: resolved, + client, + }) +} + +fn load_provider_fallback_config() -> ProviderFallbackConfig { + std::env::current_dir() + .ok() + .and_then(|cwd| ConfigLoader::default_for(cwd).load().ok()) + .map_or_else(ProviderFallbackConfig::default, |config| { + config.provider_fallbacks().clone() + }) +} + impl ApiClient for ProviderRuntimeClient { - #[allow(clippy::too_many_lines)] fn stream(&mut self, request: ApiRequest) -> Result, RuntimeError> { let tools = tool_specs_for_allowed_tools(Some(&self.allowed_tools)) .into_iter() @@ -3427,106 +3776,129 @@ impl ApiClient for ProviderRuntimeClient { input_schema: spec.input_schema, }) .collect::>(); - let message_request = MessageRequest { - model: self.model.clone(), - max_tokens: max_tokens_for_model(&self.model), - messages: convert_messages(&request.messages), - system: (!request.system_prompt.is_empty()).then(|| request.system_prompt.join("\n\n")), - tools: (!tools.is_empty()).then_some(tools), - tool_choice: (!self.allowed_tools.is_empty()).then_some(ToolChoice::Auto), - stream: true, - }; + let messages = convert_messages(&request.messages); + let system = (!request.system_prompt.is_empty()).then(|| request.system_prompt.join("\n\n")); + let tool_choice = (!self.allowed_tools.is_empty()).then_some(ToolChoice::Auto); - self.runtime.block_on(async { - let mut stream = self - .client - .stream_message(&message_request) - .await - .map_err(|error| RuntimeError::new(error.to_string()))?; - let mut events = Vec::new(); - let mut pending_tools: BTreeMap = BTreeMap::new(); - let mut saw_stop = false; + let runtime = &self.runtime; + let chain = &self.chain; + let mut last_error: Option = None; + for (index, entry) in chain.iter().enumerate() { + let message_request = MessageRequest { + model: entry.model.clone(), + max_tokens: max_tokens_for_model(&entry.model), + messages: messages.clone(), + system: system.clone(), + tools: (!tools.is_empty()).then(|| tools.clone()), + tool_choice: tool_choice.clone(), + stream: true, + }; - while let Some(event) = stream - .next_event() - .await - .map_err(|error| RuntimeError::new(error.to_string()))? - { - match event { - ApiStreamEvent::MessageStart(start) => { - for block in start.message.content { - push_output_block(block, 0, &mut events, &mut pending_tools, true); - } - } - ApiStreamEvent::ContentBlockStart(start) => { - push_output_block( - start.content_block, - start.index, - &mut events, - &mut pending_tools, - true, - ); - } - ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta { - ContentBlockDelta::TextDelta { text } => { - if !text.is_empty() { - events.push(AssistantEvent::TextDelta(text)); - } - } - ContentBlockDelta::InputJsonDelta { partial_json } => { - if let Some((_, _, input)) = pending_tools.get_mut(&delta.index) { - input.push_str(&partial_json); - } - } - ContentBlockDelta::ThinkingDelta { .. } - | ContentBlockDelta::SignatureDelta { .. } => {} - }, - ApiStreamEvent::ContentBlockStop(stop) => { - if let Some((id, name, input)) = pending_tools.remove(&stop.index) { - events.push(AssistantEvent::ToolUse { id, name, input }); - } - } - ApiStreamEvent::MessageDelta(delta) => { - events.push(AssistantEvent::Usage(delta.usage.token_usage())); - } - ApiStreamEvent::MessageStop(_) => { - saw_stop = true; - events.push(AssistantEvent::MessageStop); - } + let attempt = runtime.block_on(stream_with_provider(&entry.client, &message_request)); + match attempt { + Ok(events) => return Ok(events), + Err(error) if error.is_retryable() && index + 1 < chain.len() => { + eprintln!( + "provider {} failed with retryable error, falling back: {error}", + entry.model + ); + last_error = Some(error); + continue; + } + Err(error) => return Err(RuntimeError::new(error.to_string())), + } + } + + Err(RuntimeError::new( + last_error + .map(|error| error.to_string()) + .unwrap_or_else(|| String::from("provider chain exhausted with no attempts")), + )) + } +} + +#[allow(clippy::too_many_lines)] +async fn stream_with_provider( + client: &ProviderClient, + message_request: &MessageRequest, +) -> Result, ApiError> { + let mut stream = client.stream_message(message_request).await?; + let mut events = Vec::new(); + let mut pending_tools: BTreeMap = BTreeMap::new(); + let mut saw_stop = false; + + while let Some(event) = stream.next_event().await? { + match event { + ApiStreamEvent::MessageStart(start) => { + for block in start.message.content { + push_output_block(block, 0, &mut events, &mut pending_tools, true); } } - - push_prompt_cache_record(&self.client, &mut events); - - if !saw_stop - && events.iter().any(|event| { - matches!(event, AssistantEvent::TextDelta(text) if !text.is_empty()) - || matches!(event, AssistantEvent::ToolUse { .. }) - }) - { + ApiStreamEvent::ContentBlockStart(start) => { + push_output_block( + start.content_block, + start.index, + &mut events, + &mut pending_tools, + true, + ); + } + ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta { + ContentBlockDelta::TextDelta { text } => { + if !text.is_empty() { + events.push(AssistantEvent::TextDelta(text)); + } + } + ContentBlockDelta::InputJsonDelta { partial_json } => { + if let Some((_, _, input)) = pending_tools.get_mut(&delta.index) { + input.push_str(&partial_json); + } + } + ContentBlockDelta::ThinkingDelta { .. } + | ContentBlockDelta::SignatureDelta { .. } => {} + }, + ApiStreamEvent::ContentBlockStop(stop) => { + if let Some((id, name, input)) = pending_tools.remove(&stop.index) { + events.push(AssistantEvent::ToolUse { id, name, input }); + } + } + ApiStreamEvent::MessageDelta(delta) => { + events.push(AssistantEvent::Usage(delta.usage.token_usage())); + } + ApiStreamEvent::MessageStop(_) => { + saw_stop = true; events.push(AssistantEvent::MessageStop); } - - if events - .iter() - .any(|event| matches!(event, AssistantEvent::MessageStop)) - { - return Ok(events); - } - - let response = self - .client - .send_message(&MessageRequest { - stream: false, - ..message_request.clone() - }) - .await - .map_err(|error| RuntimeError::new(error.to_string()))?; - let mut events = response_to_events(response); - push_prompt_cache_record(&self.client, &mut events); - Ok(events) - }) + } } + + push_prompt_cache_record(client, &mut events); + + if !saw_stop + && events.iter().any(|event| { + matches!(event, AssistantEvent::TextDelta(text) if !text.is_empty()) + || matches!(event, AssistantEvent::ToolUse { .. }) + }) + { + events.push(AssistantEvent::MessageStop); + } + + if events + .iter() + .any(|event| matches!(event, AssistantEvent::MessageStop)) + { + return Ok(events); + } + + let response = client + .send_message(&MessageRequest { + stream: false, + ..message_request.clone() + }) + .await?; + let mut events = response_to_events(response); + push_prompt_cache_record(client, &mut events); + Ok(events) } struct SubagentToolExecutor { @@ -4934,6 +5306,7 @@ fn parse_skill_description(contents: &str) -> Option { } pub mod lane_completion; +pub mod pdf_extract; #[cfg(test)] mod tests { @@ -4950,11 +5323,13 @@ mod tests { use super::{ agent_permission_policy, allowed_tools_for_subagent, classify_lane_failure, - execute_agent_with_spawn, execute_tool, final_assistant_text, mvp_tool_specs, - permission_mode_from_plugin, persist_agent_terminal_state, push_output_block, - run_task_packet, AgentInput, AgentJob, GlobalToolRegistry, LaneEventName, - LaneFailureClass, SubagentToolExecutor, + derive_agent_state, execute_agent_with_spawn, execute_tool, final_assistant_text, + maybe_commit_provenance, mvp_tool_specs, permission_mode_from_plugin, + persist_agent_terminal_state, push_output_block, run_task_packet, AgentInput, AgentJob, + GlobalToolRegistry, LaneEventName, LaneFailureClass, ProviderRuntimeClient, + SubagentToolExecutor, }; + use runtime::ProviderFallbackConfig; use api::OutputContentBlock; use runtime::{ permission_enforcer::PermissionEnforcer, ApiRequest, AssistantEvent, ConversationRuntime, @@ -5704,6 +6079,349 @@ mod tests { fs::remove_dir_all(home).expect("temp home should clean up"); } + #[test] + fn skill_resolves_project_local_skills_and_legacy_commands() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("project-skills"); + let skill_dir = root.join(".claw").join("skills").join("plan"); + let command_dir = root.join(".claw").join("commands"); + fs::create_dir_all(&skill_dir).expect("skill dir should exist"); + fs::create_dir_all(&command_dir).expect("command dir should exist"); + fs::write( + skill_dir.join("SKILL.md"), + "---\nname: plan\ndescription: Project planning guidance\n---\n\n# plan\n", + ) + .expect("skill file should exist"); + fs::write( + command_dir.join("handoff.md"), + "---\nname: handoff\ndescription: Legacy handoff guidance\n---\n\n# handoff\n", + ) + .expect("command file should exist"); + + let original_dir = std::env::current_dir().expect("cwd"); + std::env::set_current_dir(&root).expect("set cwd"); + + let skill_result = execute_tool("Skill", &json!({ "skill": "$plan" })) + .expect("project-local skill should resolve"); + let skill_output: serde_json::Value = + serde_json::from_str(&skill_result).expect("valid json"); + assert!(skill_output["path"] + .as_str() + .expect("path") + .ends_with(".claw/skills/plan/SKILL.md")); + + let command_result = execute_tool("Skill", &json!({ "skill": "/handoff" })) + .expect("legacy command should resolve"); + let command_output: serde_json::Value = + serde_json::from_str(&command_result).expect("valid json"); + assert!(command_output["path"] + .as_str() + .expect("path") + .ends_with(".claw/commands/handoff.md")); + + std::env::set_current_dir(&original_dir).expect("restore cwd"); + fs::remove_dir_all(root).expect("temp project should clean up"); + } + + #[test] + fn skill_loads_project_local_claude_skill_prompt() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("project-skills"); + let home = root.join("home"); + let workspace = root.join("workspace"); + let nested = workspace.join("nested"); + let skill_dir = workspace.join(".claude").join("skills").join("trace"); + fs::create_dir_all(&skill_dir).expect("skill dir should exist"); + fs::create_dir_all(&nested).expect("nested cwd should exist"); + fs::write( + skill_dir.join("SKILL.md"), + "---\nname: trace\ndescription: Project-local trace helper\n---\n# trace\n", + ) + .expect("skill file should exist"); + + let original_home = std::env::var("HOME").ok(); + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_codex_home = std::env::var("CODEX_HOME").ok(); + let original_dir = std::env::current_dir().expect("cwd"); + std::env::set_var("HOME", &home); + std::env::remove_var("CLAW_CONFIG_HOME"); + std::env::remove_var("CODEX_HOME"); + std::env::set_current_dir(&nested).expect("set cwd"); + + let result = execute_tool("Skill", &json!({ "skill": "trace" })) + .expect("project-local skill should resolve"); + + let output: serde_json::Value = serde_json::from_str(&result).expect("valid json"); + assert!(output["path"] + .as_str() + .expect("path") + .ends_with(".claude/skills/trace/SKILL.md")); + assert_eq!(output["description"], "Project-local trace helper"); + + std::env::set_current_dir(&original_dir).expect("restore cwd"); + match original_home { + Some(value) => std::env::set_var("HOME", value), + None => std::env::remove_var("HOME"), + } + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_codex_home { + Some(value) => std::env::set_var("CODEX_HOME", value), + None => std::env::remove_var("CODEX_HOME"), + } + fs::remove_dir_all(root).expect("temp tree should clean up"); + } + + #[test] + fn skill_loads_project_local_omc_and_agents_skill_prompts() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("project-omc-skills"); + let home = root.join("home"); + let workspace = root.join("workspace"); + let nested = workspace.join("nested"); + let omc_skill_dir = workspace.join(".omc").join("skills").join("hud"); + let agents_skill_dir = workspace.join(".agents").join("skills").join("trace"); + fs::create_dir_all(&omc_skill_dir).expect("omc skill dir should exist"); + fs::create_dir_all(&agents_skill_dir).expect("agents skill dir should exist"); + fs::create_dir_all(&nested).expect("nested cwd should exist"); + fs::write( + omc_skill_dir.join("SKILL.md"), + "---\nname: hud\ndescription: Project-local OMC HUD helper\n---\n# hud\n", + ) + .expect("omc skill file should exist"); + fs::write( + agents_skill_dir.join("SKILL.md"), + "---\nname: trace\ndescription: Project-local agents compatibility helper\n---\n# trace\n", + ) + .expect("agents skill file should exist"); + + let original_home = std::env::var("HOME").ok(); + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_codex_home = std::env::var("CODEX_HOME").ok(); + let original_dir = std::env::current_dir().expect("cwd"); + std::env::set_var("HOME", &home); + std::env::remove_var("CLAW_CONFIG_HOME"); + std::env::remove_var("CODEX_HOME"); + std::env::set_current_dir(&nested).expect("set cwd"); + + let omc_result = + execute_tool("Skill", &json!({ "skill": "hud" })).expect("omc skill should resolve"); + let agents_result = execute_tool("Skill", &json!({ "skill": "trace" })) + .expect("agents skill should resolve"); + + let omc_output: serde_json::Value = serde_json::from_str(&omc_result).expect("valid json"); + let agents_output: serde_json::Value = + serde_json::from_str(&agents_result).expect("valid json"); + assert!(omc_output["path"] + .as_str() + .expect("path") + .ends_with(".omc/skills/hud/SKILL.md")); + assert_eq!(omc_output["description"], "Project-local OMC HUD helper"); + assert!(agents_output["path"] + .as_str() + .expect("path") + .ends_with(".agents/skills/trace/SKILL.md")); + assert_eq!( + agents_output["description"], + "Project-local agents compatibility helper" + ); + + std::env::set_current_dir(&original_dir).expect("restore cwd"); + match original_home { + Some(value) => std::env::set_var("HOME", value), + None => std::env::remove_var("HOME"), + } + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_codex_home { + Some(value) => std::env::set_var("CODEX_HOME", value), + None => std::env::remove_var("CODEX_HOME"), + } + fs::remove_dir_all(root).expect("temp tree should clean up"); + } + + #[test] + fn skill_loads_learned_skill_from_claude_config_dir() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("claude-config-learned-skill"); + let home = root.join("home"); + let claude_config_dir = root.join("claude-config"); + let learned_skill_dir = claude_config_dir + .join("skills") + .join("omc-learned") + .join("learned"); + fs::create_dir_all(&learned_skill_dir).expect("learned skill dir should exist"); + fs::write( + learned_skill_dir.join("SKILL.md"), + "---\nname: learned\ndescription: Learned OMC skill\n---\n# learned\n", + ) + .expect("learned skill file should exist"); + + let original_home = std::env::var("HOME").ok(); + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_codex_home = std::env::var("CODEX_HOME").ok(); + let original_claude_config_dir = std::env::var("CLAUDE_CONFIG_DIR").ok(); + std::env::set_var("HOME", &home); + std::env::remove_var("CLAW_CONFIG_HOME"); + std::env::remove_var("CODEX_HOME"); + std::env::set_var("CLAUDE_CONFIG_DIR", &claude_config_dir); + + let result = execute_tool("Skill", &json!({ "skill": "learned" })) + .expect("learned skill should resolve"); + + let output: serde_json::Value = serde_json::from_str(&result).expect("valid json"); + assert!(output["path"] + .as_str() + .expect("path") + .ends_with("skills/omc-learned/learned/SKILL.md")); + assert_eq!(output["description"], "Learned OMC skill"); + + match original_home { + Some(value) => std::env::set_var("HOME", value), + None => std::env::remove_var("HOME"), + } + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_codex_home { + Some(value) => std::env::set_var("CODEX_HOME", value), + None => std::env::remove_var("CODEX_HOME"), + } + match original_claude_config_dir { + Some(value) => std::env::set_var("CLAUDE_CONFIG_DIR", value), + None => std::env::remove_var("CLAUDE_CONFIG_DIR"), + } + fs::remove_dir_all(root).expect("temp tree should clean up"); + } + + #[test] + fn skill_loads_direct_skill_and_legacy_command_from_claude_config_dir() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("claude-config-direct-skill"); + let home = root.join("home"); + let claude_config_dir = root.join("claude-config"); + let skill_dir = claude_config_dir.join("skills").join("statusline"); + let command_dir = claude_config_dir.join("commands"); + fs::create_dir_all(&skill_dir).expect("direct skill dir should exist"); + fs::create_dir_all(&command_dir).expect("command dir should exist"); + fs::write( + skill_dir.join("SKILL.md"), + "---\nname: statusline\ndescription: Claude config skill\n---\n# statusline\n", + ) + .expect("direct skill file should exist"); + fs::write( + command_dir.join("doctor-check.md"), + "---\nname: doctor-check\ndescription: Claude config command\n---\n# doctor-check\n", + ) + .expect("direct command file should exist"); + + let original_home = std::env::var("HOME").ok(); + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_codex_home = std::env::var("CODEX_HOME").ok(); + let original_claude_config_dir = std::env::var("CLAUDE_CONFIG_DIR").ok(); + std::env::set_var("HOME", &home); + std::env::remove_var("CLAW_CONFIG_HOME"); + std::env::remove_var("CODEX_HOME"); + std::env::set_var("CLAUDE_CONFIG_DIR", &claude_config_dir); + + let direct_skill = + execute_tool("Skill", &json!({ "skill": "statusline" })).expect("direct skill"); + let direct_skill_output: serde_json::Value = + serde_json::from_str(&direct_skill).expect("valid skill json"); + assert!(direct_skill_output["path"] + .as_str() + .expect("path") + .ends_with("skills/statusline/SKILL.md")); + assert_eq!(direct_skill_output["description"], "Claude config skill"); + + let legacy_command = + execute_tool("Skill", &json!({ "skill": "doctor-check" })).expect("direct command"); + let legacy_command_output: serde_json::Value = + serde_json::from_str(&legacy_command).expect("valid command json"); + assert!(legacy_command_output["path"] + .as_str() + .expect("path") + .ends_with("commands/doctor-check.md")); + assert_eq!( + legacy_command_output["description"], + "Claude config command" + ); + + match original_home { + Some(value) => std::env::set_var("HOME", value), + None => std::env::remove_var("HOME"), + } + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_codex_home { + Some(value) => std::env::set_var("CODEX_HOME", value), + None => std::env::remove_var("CODEX_HOME"), + } + match original_claude_config_dir { + Some(value) => std::env::set_var("CLAUDE_CONFIG_DIR", value), + None => std::env::remove_var("CLAUDE_CONFIG_DIR"), + } + fs::remove_dir_all(root).expect("temp tree should clean up"); + } + + #[test] + fn skill_loads_project_local_legacy_command_markdown() { + let _guard = env_lock().lock().expect("env lock should acquire"); + let root = temp_path("project-legacy-command"); + let home = root.join("home"); + let workspace = root.join("workspace"); + let nested = workspace.join("nested"); + let command_dir = workspace.join(".claude").join("commands"); + fs::create_dir_all(&command_dir).expect("legacy command dir should exist"); + fs::create_dir_all(&nested).expect("nested cwd should exist"); + fs::write( + command_dir.join("team.md"), + "---\nname: team\ndescription: Legacy team workflow\n---\n# team\n", + ) + .expect("legacy command file should exist"); + + let original_home = std::env::var("HOME").ok(); + let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok(); + let original_codex_home = std::env::var("CODEX_HOME").ok(); + let original_dir = std::env::current_dir().expect("cwd"); + std::env::set_var("HOME", &home); + std::env::remove_var("CLAW_CONFIG_HOME"); + std::env::remove_var("CODEX_HOME"); + std::env::set_current_dir(&nested).expect("set cwd"); + + let result = execute_tool("Skill", &json!({ "skill": "team" })) + .expect("legacy command markdown should resolve"); + + let output: serde_json::Value = serde_json::from_str(&result).expect("valid json"); + assert!(output["path"] + .as_str() + .expect("path") + .ends_with(".claude/commands/team.md")); + assert_eq!(output["description"], "Legacy team workflow"); + + std::env::set_current_dir(&original_dir).expect("restore cwd"); + match original_home { + Some(value) => std::env::set_var("HOME", value), + None => std::env::remove_var("HOME"), + } + match original_config_home { + Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value), + None => std::env::remove_var("CLAW_CONFIG_HOME"), + } + match original_codex_home { + Some(value) => std::env::set_var("CODEX_HOME", value), + None => std::env::remove_var("CODEX_HOME"), + } + fs::remove_dir_all(root).expect("temp tree should clean up"); + } + #[test] fn tool_search_supports_keyword_and_select_queries() { let keyword = execute_tool( @@ -5820,6 +6538,7 @@ mod tests { } #[test] + #[allow(clippy::too_many_lines)] fn agent_fake_runner_can_persist_completion_and_failure() { let _guard = env_lock() .lock() @@ -5839,7 +6558,7 @@ mod tests { persist_agent_terminal_state( &job.manifest, "completed", - Some("Finished successfully"), + Some("Finished successfully in commit abc1234"), None, ) }, @@ -5862,7 +6581,19 @@ mod tests { completed_manifest_json["laneEvents"][1]["event"], "lane.finished" ); + assert_eq!( + completed_manifest_json["laneEvents"][2]["event"], + "lane.commit.created" + ); + assert_eq!( + completed_manifest_json["laneEvents"][2]["data"]["commit"], + "abc1234" + ); assert!(completed_manifest_json["currentBlocker"].is_null()); + assert_eq!( + completed_manifest_json["derivedState"], + "finished_cleanable" + ); let failed = execute_agent_with_spawn( AgentInput { @@ -5909,6 +6640,7 @@ mod tests { failed_manifest_json["laneEvents"][2]["failureClass"], "tool_runtime" ); + assert_eq!(failed_manifest_json["derivedState"], "truly_idle"); let spawn_error = execute_agent_with_spawn( AgentInput { @@ -5942,11 +6674,59 @@ mod tests { spawn_error_manifest_json["currentBlocker"]["failureClass"], "infra" ); + assert_eq!(spawn_error_manifest_json["derivedState"], "truly_idle"); std::env::remove_var("CLAWD_AGENT_STORE"); let _ = std::fs::remove_dir_all(dir); } + #[test] + fn agent_state_classification_covers_finished_and_specific_blockers() { + assert_eq!(derive_agent_state("running", None, None, None), "working"); + assert_eq!( + derive_agent_state("completed", Some("done"), None, None), + "finished_cleanable" + ); + assert_eq!( + derive_agent_state("completed", None, None, None), + "finished_pending_report" + ); + assert_eq!( + derive_agent_state("failed", None, Some("mcp handshake timed out"), None), + "degraded_mcp" + ); + assert_eq!( + derive_agent_state( + "failed", + None, + Some("background terminal still running"), + None + ), + "blocked_background_job" + ); + assert_eq!( + derive_agent_state("failed", None, Some("merge conflict while rebasing"), None), + "blocked_merge_conflict" + ); + assert_eq!( + derive_agent_state( + "failed", + None, + Some("transport interrupted after partial progress"), + None + ), + "interrupted_transport" + ); + } + + #[test] + fn commit_provenance_is_extracted_from_agent_results() { + let provenance = maybe_commit_provenance(Some("landed as commit deadbee with clean push")) + .expect("commit provenance"); + assert_eq!(provenance.commit, "deadbee"); + assert_eq!(provenance.canonical_commit.as_deref(), Some("deadbee")); + assert_eq!(provenance.lineage, vec!["deadbee".to_string()]); + } #[test] fn lane_failure_taxonomy_normalizes_common_blockers() { let cases = [ @@ -5977,7 +6757,10 @@ mod tests { "gateway routing rejected the request", LaneFailureClass::GatewayRouting, ), - ("tool failed: denied tool execution from hook", LaneFailureClass::ToolRuntime), + ( + "tool failed: denied tool execution from hook", + LaneFailureClass::ToolRuntime, + ), ("thread creation failed", LaneFailureClass::Infra), ]; @@ -6000,11 +6783,17 @@ mod tests { (LaneEventName::MergeReady, "lane.merge.ready"), (LaneEventName::Finished, "lane.finished"), (LaneEventName::Failed, "lane.failed"), - (LaneEventName::BranchStaleAgainstMain, "branch.stale_against_main"), + ( + LaneEventName::BranchStaleAgainstMain, + "branch.stale_against_main", + ), ]; for (event, expected) in cases { - assert_eq!(serde_json::to_value(event).expect("serialize lane event"), json!(expected)); + assert_eq!( + serde_json::to_value(event).expect("serialize lane event"), + json!(expected) + ); } } @@ -7051,6 +7840,151 @@ printf 'pwsh:%s' "$1" assert_eq!(output["stdout"], "ok"); } + #[test] + fn provider_runtime_client_chain_uses_only_primary_when_no_fallbacks_configured() { + // given + let _guard = env_lock() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let original_anthropic = std::env::var_os("ANTHROPIC_API_KEY"); + std::env::set_var("ANTHROPIC_API_KEY", "anthropic-test-key"); + let fallback_config = ProviderFallbackConfig::default(); + + // when + let client = ProviderRuntimeClient::new_with_fallback_config( + "claude-sonnet-4-6".to_string(), + BTreeSet::new(), + &fallback_config, + ) + .expect("primary-only chain should construct"); + + // then + assert_eq!(client.chain.len(), 1); + assert_eq!(client.chain[0].model, "claude-sonnet-4-6"); + + match original_anthropic { + Some(value) => std::env::set_var("ANTHROPIC_API_KEY", value), + None => std::env::remove_var("ANTHROPIC_API_KEY"), + } + } + + #[test] + fn provider_runtime_client_chain_appends_configured_fallbacks_in_order() { + // given + let _guard = env_lock() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let original_anthropic = std::env::var_os("ANTHROPIC_API_KEY"); + let original_xai = std::env::var_os("XAI_API_KEY"); + std::env::set_var("ANTHROPIC_API_KEY", "anthropic-test-key"); + std::env::set_var("XAI_API_KEY", "xai-test-key"); + let fallback_config = ProviderFallbackConfig::new( + None, + vec!["grok-3".to_string(), "grok-3-mini".to_string()], + ); + + // when + let client = ProviderRuntimeClient::new_with_fallback_config( + "claude-sonnet-4-6".to_string(), + BTreeSet::new(), + &fallback_config, + ) + .expect("chain with fallbacks should construct"); + + // then + assert_eq!(client.chain.len(), 3); + assert_eq!(client.chain[0].model, "claude-sonnet-4-6"); + assert_eq!(client.chain[1].model, "grok-3"); + assert_eq!(client.chain[2].model, "grok-3-mini"); + + match original_anthropic { + Some(value) => std::env::set_var("ANTHROPIC_API_KEY", value), + None => std::env::remove_var("ANTHROPIC_API_KEY"), + } + match original_xai { + Some(value) => std::env::set_var("XAI_API_KEY", value), + None => std::env::remove_var("XAI_API_KEY"), + } + } + + #[test] + fn provider_runtime_client_chain_primary_override_replaces_constructor_model() { + // given + let _guard = env_lock() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let original_anthropic = std::env::var_os("ANTHROPIC_API_KEY"); + let original_xai = std::env::var_os("XAI_API_KEY"); + std::env::set_var("ANTHROPIC_API_KEY", "anthropic-test-key"); + std::env::set_var("XAI_API_KEY", "xai-test-key"); + let fallback_config = ProviderFallbackConfig::new( + Some("grok-3".to_string()), + vec!["claude-sonnet-4-6".to_string()], + ); + + // when + let client = ProviderRuntimeClient::new_with_fallback_config( + "claude-haiku-4-5-20251213".to_string(), + BTreeSet::new(), + &fallback_config, + ) + .expect("chain with primary override should construct"); + + // then + assert_eq!(client.chain.len(), 2); + assert_eq!(client.chain[0].model, "grok-3"); + assert_eq!(client.chain[1].model, "claude-sonnet-4-6"); + + match original_anthropic { + Some(value) => std::env::set_var("ANTHROPIC_API_KEY", value), + None => std::env::remove_var("ANTHROPIC_API_KEY"), + } + match original_xai { + Some(value) => std::env::set_var("XAI_API_KEY", value), + None => std::env::remove_var("XAI_API_KEY"), + } + } + + #[test] + fn provider_runtime_client_chain_skips_fallbacks_missing_credentials() { + // given + let _guard = env_lock() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let original_anthropic = std::env::var_os("ANTHROPIC_API_KEY"); + let original_xai = std::env::var_os("XAI_API_KEY"); + std::env::set_var("ANTHROPIC_API_KEY", "anthropic-test-key"); + std::env::remove_var("XAI_API_KEY"); + let fallback_config = ProviderFallbackConfig::new( + None, + vec![ + "grok-3".to_string(), + "claude-haiku-4-5-20251213".to_string(), + ], + ); + + // when + let client = ProviderRuntimeClient::new_with_fallback_config( + "claude-sonnet-4-6".to_string(), + BTreeSet::new(), + &fallback_config, + ) + .expect("chain construction should not fail when only some fallbacks are unavailable"); + + // then + assert_eq!(client.chain.len(), 2); + assert_eq!(client.chain[0].model, "claude-sonnet-4-6"); + assert_eq!(client.chain[1].model, "claude-haiku-4-5-20251213"); + + match original_anthropic { + Some(value) => std::env::set_var("ANTHROPIC_API_KEY", value), + None => std::env::remove_var("ANTHROPIC_API_KEY"), + } + if let Some(value) = original_xai { + std::env::set_var("XAI_API_KEY", value); + } + } + #[test] fn run_task_packet_creates_packet_backed_task() { let result = run_task_packet(TaskPacket { diff --git a/rust/crates/tools/src/pdf_extract.rs b/rust/crates/tools/src/pdf_extract.rs new file mode 100644 index 00000000..caa96622 --- /dev/null +++ b/rust/crates/tools/src/pdf_extract.rs @@ -0,0 +1,548 @@ +//! Minimal PDF text extraction. +//! +//! Reads a PDF file, locates `/Contents` stream objects, decompresses with +//! flate2 when the stream uses `/FlateDecode`, and extracts text operators +//! found between `BT` / `ET` markers. + +use std::io::Read as _; +use std::path::Path; + +/// Extract all readable text from a PDF file. +/// +/// Returns the concatenated text found inside BT/ET operators across all +/// content streams. Non-text pages or encrypted PDFs yield an empty string +/// rather than an error. +pub fn extract_text(path: &Path) -> Result { + let data = std::fs::read(path).map_err(|e| format!("failed to read PDF: {e}"))?; + Ok(extract_text_from_bytes(&data)) +} + +/// Core extraction from raw PDF bytes — useful for testing without touching the +/// filesystem. +pub(crate) fn extract_text_from_bytes(data: &[u8]) -> String { + let mut all_text = String::new(); + let mut offset = 0; + + while offset < data.len() { + let Some(stream_start) = find_subsequence(&data[offset..], b"stream") else { + break; + }; + let abs_start = offset + stream_start; + + // Determine the byte offset right after "stream\r\n" or "stream\n". + let content_start = skip_stream_eol(data, abs_start + b"stream".len()); + + let Some(end_rel) = find_subsequence(&data[content_start..], b"endstream") else { + break; + }; + let content_end = content_start + end_rel; + + // Look backwards from "stream" for a FlateDecode hint in the object + // dictionary. We scan at most 512 bytes before the stream keyword. + let dict_window_start = abs_start.saturating_sub(512); + let dict_window = &data[dict_window_start..abs_start]; + let is_flate = find_subsequence(dict_window, b"FlateDecode").is_some(); + + // Only process streams whose parent dictionary references /Contents or + // looks like a page content stream (contains /Length). We intentionally + // keep this loose to cover both inline and referenced content streams. + let raw = &data[content_start..content_end]; + let decompressed; + let stream_bytes: &[u8] = if is_flate { + if let Ok(buf) = inflate(raw) { + decompressed = buf; + &decompressed + } else { + offset = content_end; + continue; + } + } else { + raw + }; + + let text = extract_bt_et_text(stream_bytes); + if !text.is_empty() { + if !all_text.is_empty() { + all_text.push('\n'); + } + all_text.push_str(&text); + } + + offset = content_end; + } + + all_text +} + +/// Inflate (zlib / deflate) compressed data via `flate2`. +fn inflate(data: &[u8]) -> Result, String> { + let mut decoder = flate2::read::ZlibDecoder::new(data); + let mut buf = Vec::new(); + decoder + .read_to_end(&mut buf) + .map_err(|e| format!("flate2 inflate error: {e}"))?; + Ok(buf) +} + +/// Extract text from PDF content-stream operators between BT and ET markers. +/// +/// Handles the common text-showing operators: +/// - `Tj` — show a string +/// - `TJ` — show an array of strings/numbers +/// - `'` — move to next line and show string +/// - `"` — set spacing, move to next line and show string +fn extract_bt_et_text(stream: &[u8]) -> String { + let text = String::from_utf8_lossy(stream); + let mut result = String::new(); + let mut in_bt = false; + + for line in text.lines() { + let trimmed = line.trim(); + if trimmed == "BT" { + in_bt = true; + continue; + } + if trimmed == "ET" { + in_bt = false; + continue; + } + if !in_bt { + continue; + } + + // Tj operator: (text) Tj + if trimmed.ends_with("Tj") { + if let Some(s) = extract_parenthesized_string(trimmed) { + if !result.is_empty() && !result.ends_with('\n') { + result.push(' '); + } + result.push_str(&s); + } + } + // TJ operator: [ (text) 123 (text) ] TJ + else if trimmed.ends_with("TJ") { + let extracted = extract_tj_array(trimmed); + if !extracted.is_empty() { + if !result.is_empty() && !result.ends_with('\n') { + result.push(' '); + } + result.push_str(&extracted); + } + } + // ' operator: (text) ' and " operator: aw ac (text) " + else if is_newline_show_operator(trimmed) { + if let Some(s) = extract_parenthesized_string(trimmed) { + if !result.is_empty() { + result.push('\n'); + } + result.push_str(&s); + } + } + } + + result +} + +/// Returns `true` when `trimmed` looks like a `'` or `"` text-show operator. +fn is_newline_show_operator(trimmed: &str) -> bool { + (trimmed.ends_with('\'') && trimmed.len() > 1) + || (trimmed.ends_with('"') && trimmed.contains('(')) +} + +/// Pull the text from the first `(…)` group, handling escaped parens and +/// common PDF escape sequences. +fn extract_parenthesized_string(input: &str) -> Option { + let open = input.find('(')?; + let bytes = input.as_bytes(); + let mut depth = 0; + let mut result = String::new(); + let mut i = open; + + while i < bytes.len() { + match bytes[i] { + b'(' => { + if depth > 0 { + result.push('('); + } + depth += 1; + } + b')' => { + depth -= 1; + if depth == 0 { + return Some(result); + } + result.push(')'); + } + b'\\' if i + 1 < bytes.len() => { + i += 1; + match bytes[i] { + b'n' => result.push('\n'), + b'r' => result.push('\r'), + b't' => result.push('\t'), + b'\\' => result.push('\\'), + b'(' => result.push('('), + b')' => result.push(')'), + // Octal sequences — up to 3 digits. + d @ b'0'..=b'7' => { + let mut octal = u32::from(d - b'0'); + for _ in 0..2 { + if i + 1 < bytes.len() + && bytes[i + 1].is_ascii_digit() + && bytes[i + 1] <= b'7' + { + i += 1; + octal = octal * 8 + u32::from(bytes[i] - b'0'); + } else { + break; + } + } + if let Some(ch) = char::from_u32(octal) { + result.push(ch); + } + } + other => result.push(char::from(other)), + } + } + ch => result.push(char::from(ch)), + } + i += 1; + } + + None // unbalanced +} + +/// Extract concatenated strings from a TJ array like `[ (Hello) -120 (World) ] TJ`. +fn extract_tj_array(input: &str) -> String { + let mut result = String::new(); + let Some(bracket_start) = input.find('[') else { + return result; + }; + let Some(bracket_end) = input.rfind(']') else { + return result; + }; + let inner = &input[bracket_start + 1..bracket_end]; + + let mut i = 0; + let bytes = inner.as_bytes(); + while i < bytes.len() { + if bytes[i] == b'(' { + // Reconstruct the parenthesized string and extract it. + if let Some(s) = extract_parenthesized_string(&inner[i..]) { + result.push_str(&s); + // Skip past the closing paren. + let mut depth = 0u32; + for &b in &bytes[i..] { + i += 1; + if b == b'(' { + depth += 1; + } else if b == b')' { + depth -= 1; + if depth == 0 { + break; + } + } + } + continue; + } + } + i += 1; + } + + result +} + +/// Skip past the end-of-line marker that immediately follows the `stream` +/// keyword. Per the PDF spec this is either `\r\n` or `\n`. +fn skip_stream_eol(data: &[u8], pos: usize) -> usize { + if pos < data.len() && data[pos] == b'\r' { + if pos + 1 < data.len() && data[pos + 1] == b'\n' { + return pos + 2; + } + return pos + 1; + } + if pos < data.len() && data[pos] == b'\n' { + return pos + 1; + } + pos +} + +/// Simple byte-subsequence search. +fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option { + haystack + .windows(needle.len()) + .position(|window| window == needle) +} + +/// Check if a user-supplied path looks like a PDF file reference. +#[must_use] +pub fn looks_like_pdf_path(text: &str) -> Option<&str> { + for token in text.split_whitespace() { + let cleaned = token.trim_matches(|c: char| c == '\'' || c == '"' || c == '`'); + if let Some(dot_pos) = cleaned.rfind('.') { + if cleaned[dot_pos + 1..].eq_ignore_ascii_case("pdf") && dot_pos > 0 { + return Some(cleaned); + } + } + } + None +} + +/// Auto-extract text from a PDF path mentioned in a user prompt. +/// +/// Returns `Some((path, extracted_text))` when a `.pdf` path is detected and +/// the file exists, otherwise `None`. +#[must_use] +pub fn maybe_extract_pdf_from_prompt(prompt: &str) -> Option<(String, String)> { + let pdf_path = looks_like_pdf_path(prompt)?; + let path = Path::new(pdf_path); + if !path.exists() { + return None; + } + let text = extract_text(path).ok()?; + if text.is_empty() { + return None; + } + Some((pdf_path.to_string(), text)) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Build a minimal valid PDF with a single page containing uncompressed + /// text. This is the smallest PDF structure that exercises the BT/ET + /// extraction path. + fn build_simple_pdf(text: &str) -> Vec { + let content_stream = format!("BT\n/F1 12 Tf\n({text}) Tj\nET"); + let stream_bytes = content_stream.as_bytes(); + let mut pdf = Vec::new(); + + // Header + pdf.extend_from_slice(b"%PDF-1.4\n"); + + // Object 1 — Catalog + let obj1_offset = pdf.len(); + pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + // Object 2 — Pages + let obj2_offset = pdf.len(); + pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + // Object 3 — Page + let obj3_offset = pdf.len(); + pdf.extend_from_slice( + b"3 0 obj\n<< /Type /Page /Parent 2 0 R /Contents 4 0 R >>\nendobj\n", + ); + + // Object 4 — Content stream (uncompressed) + let obj4_offset = pdf.len(); + let length = stream_bytes.len(); + let header = format!("4 0 obj\n<< /Length {length} >>\nstream\n"); + pdf.extend_from_slice(header.as_bytes()); + pdf.extend_from_slice(stream_bytes); + pdf.extend_from_slice(b"\nendstream\nendobj\n"); + + // Cross-reference table + let xref_offset = pdf.len(); + pdf.extend_from_slice(b"xref\n0 5\n"); + pdf.extend_from_slice(b"0000000000 65535 f \n"); + pdf.extend_from_slice(format!("{obj1_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj2_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj3_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj4_offset:010} 00000 n \n").as_bytes()); + + // Trailer + pdf.extend_from_slice(b"trailer\n<< /Size 5 /Root 1 0 R >>\n"); + pdf.extend_from_slice(format!("startxref\n{xref_offset}\n%%EOF\n").as_bytes()); + + pdf + } + + /// Build a minimal PDF with flate-compressed content stream. + fn build_flate_pdf(text: &str) -> Vec { + use flate2::write::ZlibEncoder; + use flate2::Compression; + use std::io::Write as _; + + let content_stream = format!("BT\n/F1 12 Tf\n({text}) Tj\nET"); + let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); + encoder + .write_all(content_stream.as_bytes()) + .expect("compress"); + let compressed = encoder.finish().expect("finish"); + + let mut pdf = Vec::new(); + pdf.extend_from_slice(b"%PDF-1.4\n"); + + let obj1_offset = pdf.len(); + pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + let obj2_offset = pdf.len(); + pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + let obj3_offset = pdf.len(); + pdf.extend_from_slice( + b"3 0 obj\n<< /Type /Page /Parent 2 0 R /Contents 4 0 R >>\nendobj\n", + ); + + let obj4_offset = pdf.len(); + let length = compressed.len(); + let header = format!("4 0 obj\n<< /Length {length} /Filter /FlateDecode >>\nstream\n"); + pdf.extend_from_slice(header.as_bytes()); + pdf.extend_from_slice(&compressed); + pdf.extend_from_slice(b"\nendstream\nendobj\n"); + + let xref_offset = pdf.len(); + pdf.extend_from_slice(b"xref\n0 5\n"); + pdf.extend_from_slice(b"0000000000 65535 f \n"); + pdf.extend_from_slice(format!("{obj1_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj2_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj3_offset:010} 00000 n \n").as_bytes()); + pdf.extend_from_slice(format!("{obj4_offset:010} 00000 n \n").as_bytes()); + + pdf.extend_from_slice(b"trailer\n<< /Size 5 /Root 1 0 R >>\n"); + pdf.extend_from_slice(format!("startxref\n{xref_offset}\n%%EOF\n").as_bytes()); + + pdf + } + + #[test] + fn extracts_uncompressed_text_from_minimal_pdf() { + // given + let pdf_bytes = build_simple_pdf("Hello World"); + + // when + let text = extract_text_from_bytes(&pdf_bytes); + + // then + assert_eq!(text, "Hello World"); + } + + #[test] + fn extracts_text_from_flate_compressed_stream() { + // given + let pdf_bytes = build_flate_pdf("Compressed PDF Text"); + + // when + let text = extract_text_from_bytes(&pdf_bytes); + + // then + assert_eq!(text, "Compressed PDF Text"); + } + + #[test] + fn handles_tj_array_operator() { + // given + let stream = b"BT\n/F1 12 Tf\n[ (Hello) -120 ( World) ] TJ\nET"; + // Build a raw PDF with TJ array operator instead of simple Tj. + let content_stream = std::str::from_utf8(stream).unwrap(); + let raw = format!( + "%PDF-1.4\n1 0 obj\n<< /Type /Catalog >>\nendobj\n\ + 2 0 obj\n<< /Length {} >>\nstream\n{}\nendstream\nendobj\n%%EOF\n", + content_stream.len(), + content_stream + ); + let pdf_bytes = raw.into_bytes(); + + // when + let text = extract_text_from_bytes(&pdf_bytes); + + // then + assert_eq!(text, "Hello World"); + } + + #[test] + fn handles_escaped_parentheses() { + // given + let content = b"BT\n(Hello \\(World\\)) Tj\nET"; + let raw = format!( + "%PDF-1.4\n1 0 obj\n<< /Length {} >>\nstream\n", + content.len() + ); + let mut pdf_bytes = raw.into_bytes(); + pdf_bytes.extend_from_slice(content); + pdf_bytes.extend_from_slice(b"\nendstream\nendobj\n%%EOF\n"); + + // when + let text = extract_text_from_bytes(&pdf_bytes); + + // then + assert_eq!(text, "Hello (World)"); + } + + #[test] + fn returns_empty_for_non_pdf_data() { + // given + let data = b"This is not a PDF file at all"; + + // when + let text = extract_text_from_bytes(data); + + // then + assert!(text.is_empty()); + } + + #[test] + fn extracts_text_from_file_on_disk() { + // given + let pdf_bytes = build_simple_pdf("Disk Test"); + let dir = std::env::temp_dir().join("clawd-pdf-extract-test"); + std::fs::create_dir_all(&dir).unwrap(); + let pdf_path = dir.join("test.pdf"); + std::fs::write(&pdf_path, &pdf_bytes).unwrap(); + + // when + let text = extract_text(&pdf_path).unwrap(); + + // then + assert_eq!(text, "Disk Test"); + + // cleanup + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn looks_like_pdf_path_detects_pdf_references() { + // given / when / then + assert_eq!( + looks_like_pdf_path("Please read /tmp/report.pdf"), + Some("/tmp/report.pdf") + ); + assert_eq!(looks_like_pdf_path("Check file.PDF now"), Some("file.PDF")); + assert_eq!(looks_like_pdf_path("no pdf here"), None); + } + + #[test] + fn maybe_extract_pdf_from_prompt_returns_none_for_missing_file() { + // given + let prompt = "Read /tmp/nonexistent-abc123.pdf please"; + + // when + let result = maybe_extract_pdf_from_prompt(prompt); + + // then + assert!(result.is_none()); + } + + #[test] + fn maybe_extract_pdf_from_prompt_extracts_existing_file() { + // given + let pdf_bytes = build_simple_pdf("Auto Extracted"); + let dir = std::env::temp_dir().join("clawd-pdf-auto-extract-test"); + std::fs::create_dir_all(&dir).unwrap(); + let pdf_path = dir.join("auto.pdf"); + std::fs::write(&pdf_path, &pdf_bytes).unwrap(); + let prompt = format!("Summarize {}", pdf_path.display()); + + // when + let result = maybe_extract_pdf_from_prompt(&prompt); + + // then + let (path, text) = result.expect("should extract"); + assert_eq!(path, pdf_path.display().to_string()); + assert_eq!(text, "Auto Extracted"); + + // cleanup + let _ = std::fs::remove_dir_all(&dir); + } +}