diff --git a/.github/workflows/sdk-check.yml b/.github/workflows/sdk-check.yml index 86b917b..5c8ddae 100644 --- a/.github/workflows/sdk-check.yml +++ b/.github/workflows/sdk-check.yml @@ -6,6 +6,8 @@ on: - "crewcoder-agent/**" - "crewcoder-client/**" - "crewcoder-tui/**" + - "crewcoder-tui-core/**" + - "crewcoder-opentui/**" - "crewcoder-sdk/**" - "bin/**" - "lib/**" @@ -23,6 +25,8 @@ on: - "crewcoder-agent/**" - "crewcoder-client/**" - "crewcoder-tui/**" + - "crewcoder-tui-core/**" + - "crewcoder-opentui/**" - "crewcoder-sdk/**" - "bin/**" - "lib/**" @@ -50,3 +54,18 @@ jobs: cache: npm - run: npm ci --ignore-scripts - run: npm run release:check:sdk + + opentui: + # OpenTUI's native layer needs Node.js >=26.4, so its renderer tests run on a + # separate runtime from the Node 22 packages. + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 26 + cache: npm + - run: npm ci --ignore-scripts + - run: npm run test -w @onpoint-dev-tools/crewcoder-tui-core + - run: npm run typecheck -w @onpoint-dev-tools/crewcoder-opentui + - run: npm run test -w @onpoint-dev-tools/crewcoder-opentui diff --git a/.github/workflows/sdk-release.yml b/.github/workflows/sdk-release.yml index 3c096bf..b5e02a3 100644 --- a/.github/workflows/sdk-release.yml +++ b/.github/workflows/sdk-release.yml @@ -42,7 +42,9 @@ jobs: run: npm install --global npm@11.8.0 - run: npm ci --ignore-scripts - run: npm run build -w @onpoint-dev-tools/crewcoder-agent + - run: npm run build -w @onpoint-dev-tools/crewcoder-tui-core - run: npm run build -w @onpoint-dev-tools/crewcoder-tui + - run: npm run build -w @onpoint-dev-tools/crewcoder-opentui - run: npm run build -w @onpoint-dev-tools/crewcoder-client - run: npm run build -w @onpoint-dev-tools/crewcoder-sdk - name: Verify explicit publish gates @@ -59,7 +61,17 @@ jobs: fi - name: Verify published agent runtime run: node scripts/wait-for-npm-package.cjs @onpoint-dev-tools/crewcoder-agent crewcoder-agent/package.json - - name: Publish terminal UI + - name: Publish TUI core + run: | + PACKAGE_VERSION="$(node -p "require('./crewcoder-tui-core/package.json').version")" + if npm view "@onpoint-dev-tools/crewcoder-tui-core@${PACKAGE_VERSION}" version >/dev/null 2>&1; then + echo "TUI core ${PACKAGE_VERSION} is already published; continuing the partial release." + else + npm publish -w @onpoint-dev-tools/crewcoder-tui-core --access public --provenance + fi + - name: Verify published TUI core + run: node scripts/wait-for-npm-package.cjs @onpoint-dev-tools/crewcoder-tui-core crewcoder-tui-core/package.json + - name: Publish legacy terminal UI run: | PACKAGE_VERSION="$(node -p "require('./crewcoder-tui/package.json').version")" if npm view "@onpoint-dev-tools/crewcoder-tui@${PACKAGE_VERSION}" version >/dev/null 2>&1; then @@ -67,8 +79,18 @@ jobs: else npm publish -w @onpoint-dev-tools/crewcoder-tui --access public --provenance fi - - name: Verify published terminal UI + - name: Verify published legacy terminal UI run: node scripts/wait-for-npm-package.cjs @onpoint-dev-tools/crewcoder-tui crewcoder-tui/package.json + - name: Publish OpenTUI terminal UI + run: | + PACKAGE_VERSION="$(node -p "require('./crewcoder-opentui/package.json').version")" + if npm view "@onpoint-dev-tools/crewcoder-opentui@${PACKAGE_VERSION}" version >/dev/null 2>&1; then + echo "OpenTUI terminal UI ${PACKAGE_VERSION} is already published; continuing the partial release." + else + npm publish -w @onpoint-dev-tools/crewcoder-opentui --access public --provenance + fi + - name: Verify published OpenTUI terminal UI + run: node scripts/wait-for-npm-package.cjs @onpoint-dev-tools/crewcoder-opentui crewcoder-opentui/package.json - name: Publish browser client run: | PACKAGE_VERSION="$(node -p "require('./crewcoder-client/package.json').version")" diff --git a/AGENTS.md b/AGENTS.md index 3d68bc8..5101bf5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -47,9 +47,11 @@ Update corresponding Docs `crewcoder-agent/docs/*`, `crewcoder-tui/docs/*`, and ## CLI umbrella package -The public root `crewcoder` package is the one-command installer and launcher for both scoped runtime packages. Its `crewcoder` and `crew` bins must open `@onpoint-dev-tools/crewcoder-tui` with no arguments and forward argument-bearing invocations to `@onpoint-dev-tools/crewcoder-agent`. Never restore the `cc` alias because it shadows the standard Unix C compiler command. Keep both runtime dependencies in the root package and publish the umbrella only after those exact versions exist on npm. +The public root `crewcoder` package is the one-command installer and launcher. Its `crewcoder` and `crew` bins open `@onpoint-dev-tools/crewcoder-opentui` with no arguments and forward argument-bearing invocations to `@onpoint-dev-tools/crewcoder-agent`. Never restore the `cc` alias because it shadows the standard Unix C compiler command. The agent is a required dependency; `crewcoder-opentui` is an **optionalDependency** because it needs Node.js >=26.4 and OpenTUI native binaries, and headless VPS/sandbox installs must succeed without it. Do not make it required. For a bare call, `lib/launcher.js` refuses with guidance (legacy UI and agent commands) when there is no interactive terminal, the TUI is not installed, or Node.js is below the TUI's `engines.node` floor; it adds `--disable-warning=ExperimentalWarning` only for the TUI child. Publish the umbrella only after the exact pinned versions exist on npm. -The scoped agent package still provides `crewcoder`, `crew`, and `crewcoder-agent`; the scoped TUI package provides `crewcoder-tui` for independent development and packaging. +`@onpoint-dev-tools/crewcoder-tui` is the legacy custom-renderer UI. It is published separately for users who prefer it and must not be an umbrella dependency. Both UIs share `@onpoint-dev-tools/crewcoder-tui-core` (bridge, event parser, reducer, state); keep UI-independent code there instead of duplicating it. See `crewcoder-opentui/README.md` for the OpenTUI preview's ported and missing features. + +The scoped agent package still provides `crewcoder`, `crew`, and `crewcoder-agent`; its own bare invocation launches the legacy `crewcoder-tui` from `PATH`. The scoped TUI packages provide `crewcoder-tui` and `crewcoder-opentui` for independent installs. ## MonoRepo DIR @@ -57,10 +59,18 @@ CODING AGENT - /home/aura/my-cmd/CrewCoder-Mono/crewcoder/crewcoder-agent -TUI +TUI (OpenTUI, launched by the umbrella; Node.js >=26.4) + +- /home/aura/my-cmd/CrewCoder-Mono/crewcoder/crewcoder-opentui + +Legacy TUI - /home/aura/my-cmd/CrewCoder-Mono/crewcoder/crewcoder-tui +Shared TUI runtime (bridge, reducer, state) + +- /home/aura/my-cmd/CrewCoder-Mono/crewcoder/crewcoder-tui-core + SDK - /home/aura/my-cmd/CrewCoder-Mono/crewcoder/crewcoder-sdk @@ -201,9 +211,7 @@ crewcoder/ ├── tsconfig.json └── src/ ├── cli.ts - ├── bridge/ - │ ├── crewcoder-process.ts - │ └── event-parser.ts + ├── bridge/ # live-ui-*.ts; agent bridge lives in crewcoder-tui-core ├── components/ │ ├── App.ts │ ├── CommandPalette.ts @@ -219,11 +227,6 @@ crewcoder/ │ ├── markdown-renderer.ts │ ├── modal-view.ts │ └── path-suggestions.ts - ├── state/ - │ ├── effort-levels.ts - │ ├── event-reducer.ts - │ ├── tui-store.ts - │ └── usage.ts ├── tests/ │ ├── app-home.test.ts │ ├── app-input.test.ts @@ -268,7 +271,7 @@ crewcoder/ - Durable `/goal` behavior belongs to CrewCoder's detached supervisor (`goal-store.ts` / `goal-runner.ts`), not provider adapters. When `goals.checkModel` is configured, its same-provider tool-free verifier verdict is authoritative; verifier failures must pause rather than fall back to maker self-grading. Goals wait durably for explicit approvals. - Crew task sidebar labels are session-local presentation numbers that restart at `1` and remain stable across status sorting. Crew task tools and persisted dependency edges must continue using project-wide durable task IDs. - CI transports (`action.yml`, the GitLab template, and `scripts/run-ci.sh`) must preserve `run --ci` stdout JSON and exit codes. `crewcoder hook install` may only update its managed marker block, must back up unrelated hooks before an explicit force replacement, and must refuse shared/global hook paths outside the repository. -- Coordinated releases use `npm run release -- ` to bump the umbrella, agent, browser client, and SDK together while preserving the independent TUI version. Keep `scripts/release.cjs` synchronized with those manifests, exact internal dependency pins, runtime version constants, generated version API baselines, the root lockfile, and the protected npm publishing workflow. Automatic API baseline refreshes may only change the client and SDK `api/version.d.ts` files. +- Coordinated releases use `npm run release -- ` to bump the umbrella, agent, browser client, and SDK together while preserving the independent `crewcoder-tui`, `crewcoder-tui-core`, and `crewcoder-opentui` versions (publish order: agent, tui-core, both TUIs, client, SDK, umbrella). Keep `scripts/release.cjs` synchronized with those manifests, exact internal dependency pins, runtime version constants, generated version API baselines, the root lockfile, and the protected npm publishing workflow. Automatic API baseline refreshes may only change the client and SDK `api/version.d.ts` files. - Validate provider stream changes with package-local checks using isolated env: `CREWCODER_HOME=/tmp/.crewcoder npm run typecheck -w @onpoint-dev-tools/crewcoder-agent` `env -u OPENCODE_API_KEY CREWCODER_HOME=/tmp/.crewcoder npm test -w @onpoint-dev-tools/crewcoder-agent` diff --git a/README.md b/README.md index 83e0552..f5f16d7 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ This directory is an npm-workspaces monorepo for the CrewCoder Packages. The roo ## Install CrewCoder -Requires Node.js 22 or newer. The `crewcoder` umbrella package includes both the CrewCoder agent and terminal UI. +Requires Node.js 22 or newer for the agent. The terminal UI that bare `crewcoder` opens ([`crewcoder-opentui`](crewcoder-opentui/README.md), a preview built on OpenTUI) needs Node.js 26.4 or newer and is an optional part of the install: on older Node.js, unsupported platforms, or headless machines, `crewcoder` still installs and every agent command works. The original UI stays available as the [legacy `crewcoder-tui`](crewcoder-tui/README.md) package on Node.js 22+. ### Try it with npx @@ -34,7 +34,7 @@ Requires Node.js 22 or newer. The `crewcoder` umbrella package includes both the npx crewcoder ``` -`npx` downloads CrewCoder into npm's cache and launches the TUI without creating a persistent global installation. +`npx` downloads CrewCoder into npm's cache and launches the TUI without creating a persistent global installation. Without Node.js 26.4+ or an interactive terminal, it prints how to continue instead. ### Install globally @@ -73,24 +73,29 @@ The scoped packages remain available separately for development and custom packa ```sh npm install --global @onpoint-dev-tools/crewcoder-agent -npm install --global @onpoint-dev-tools/crewcoder-tui +npm install --global @onpoint-dev-tools/crewcoder-tui # legacy UI, Node.js 22+ +npm install --global @onpoint-dev-tools/crewcoder-opentui # OpenTUI UI, Node.js 26.4+ ``` -Installing only `@onpoint-dev-tools/crewcoder-agent` does not install the TUI. Most CLI users should install the `crewcoder` umbrella package. See the [installation guide](docs/INSTALLATION.md) for install and update details. +Installing only `@onpoint-dev-tools/crewcoder-agent` does not install a TUI; its own bare `crewcoder` opens `crewcoder-tui` when that is on `PATH`. Most CLI users should install the `crewcoder` umbrella package. See the [installation guide](docs/INSTALLATION.md) for install and update details. ## Packages | Package | npm name | Runs on | What it is | Install command | |---|---|---|---|---| -| CrewCoder umbrella | `crewcoder` | Node.js 22+ terminal | Recommended CLI package containing both the agent and TUI. | +| CrewCoder umbrella | `crewcoder` | Node.js 22+ (TUI: 26.4+) | Recommended CLI package: the agent plus the optional OpenTUI terminal UI. | `npm install --global crewcoder` | | [`crewcoder-agent`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-agent) | [`@onpoint-dev-tools/crewcoder-agent`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-agent) | Node.js 22+ / standalone binary | The agent harness: evented agent loop, providers, local tools, durable sessions, goals, crews, extensions, ACP and fleet servers, and the `crewcoder` / `crew` CLI. | `npm install --global @onpoint-dev-tools/crewcoder-agent` | -| [`crewcoder-tui`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-tui) | [`@onpoint-dev-tools/crewcoder-tui`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-tui) | Node.js 22+ terminal | Custom terminal UI (no Ink/React/blessed/curses) driven by the agent's JSON event stream, locally or over SSH. | `npm install --global @onpoint-dev-tools/crewcoder-tui` | +| [`crewcoder-opentui`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-opentui) | [`@onpoint-dev-tools/crewcoder-opentui`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-opentui) | Node.js 26.4+ terminal | Preview terminal UI built on OpenTUI and React; opened by bare `crewcoder`. Not yet at parity with the legacy UI. | `npm install --global @onpoint-dev-tools/crewcoder-opentui` | +| [`crewcoder-tui`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-tui) | [`@onpoint-dev-tools/crewcoder-tui`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-tui) | Node.js 22+ terminal | Legacy custom terminal UI (no Ink/React/blessed/curses) driven by the agent's JSON event stream, locally or over SSH. | `npm install --global @onpoint-dev-tools/crewcoder-tui` | +| [`crewcoder-tui-core`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-tui-core) | [`@onpoint-dev-tools/crewcoder-tui-core`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-tui-core) | Node.js 22+ | Shared, UI-independent TUI runtime: agent process bridge, event parser, reducer, and state. | Installed by both TUIs | | [`crewcoder-sdk`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-sdk) | [`@onpoint-dev-tools/crewcoder-sdk`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-sdk) | Node.js 22+ host process | Supported TypeScript API for embedding CrewCoder in-process, plus an authenticated fleet client for remote runners. | `npm install --global @onpoint-dev-tools/crewcoder-sdk` | | [`crewcoder-client`](https://github.com/OnPoint-Dev-Tools/crewcoder/tree/main/crewcoder-client) | [`@onpoint-dev-tools/crewcoder-client`](https://www.npmjs.com/package/@onpoint-dev-tools/crewcoder-client) | Browsers, Electron renderers, webviews | Browser-safe client for authenticated CrewCoder runners. Web-platform APIs only: no Node.js imports, no local files, no tool execution, no stored provider credentials. | `npm install --global @onpoint-dev-tools/crewcoder-client` | Package READMEs: [agent](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-agent/README.md) · -[tui](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-tui/README.md) · +[opentui](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-opentui/README.md) · +[tui (legacy)](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-tui/README.md) · +[tui-core](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-tui-core/README.md) · [sdk](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-sdk/README.md) · [client](https://github.com/OnPoint-Dev-Tools/crewcoder/blob/main/crewcoder-client/README.md) @@ -168,7 +173,8 @@ Run one workspace directly: ```sh npm run dev -w @onpoint-dev-tools/crewcoder-agent npm run typecheck -w @onpoint-dev-tools/crewcoder-sdk -npm run dev -w @onpoint-dev-tools/crewcoder-tui +npm run dev -w @onpoint-dev-tools/crewcoder-tui # legacy UI +mise exec node@26 -- npm run dev -w @onpoint-dev-tools/crewcoder-opentui # OpenTUI UI, Node.js 26.4+ ``` ## TypeScript SDK and browser client diff --git a/crewcoder-agent/AGENTS.md b/crewcoder-agent/AGENTS.md index dee9374..b00666c 100644 --- a/crewcoder-agent/AGENTS.md +++ b/crewcoder-agent/AGENTS.md @@ -502,6 +502,16 @@ User-message `background` is deliberate. It should be passed to the model and vi Interactive approvals are part of the live JSON-events control channel. `src/core/stdin-control.ts` accepts `{"type":"control","action":"approval","approvalId":"...","approved":true|false}` and `src/core/agent-loop.ts` waits for matching decisions when an approval signal is available. Do not replace this with saved-session polling; approvals must control the active run. +## Reply throughput timing + +`message_end.durationMs` is generation time from `GenerationClock` (`src/core/generation-clock.ts`), not +wall-clock around `modelClient.complete()`. Claude Agent SDK and Codex run tools inside that call, so +every non-generation interval must stay excluded: provider-native tools (start to end, including the +diff snapshot), `executeTool`, `requestQuestion`, follow-up delivery, and `onUsage` pricing. A new +stream callback that waits on tools, the user, or disk must run inside `generationClock.excluding`. +An unmatched native tool start means the duration is omitted, never guessed. Guarded by +`src/tests/generation-clock.test.ts` and the throughput tests in `src/tests/agent-loop.test.ts`. + ## Session durability Sessions are saved **after every completed turn**, not just at the end of the run @@ -843,9 +853,25 @@ iterationCapReached -> an explicit --max-iterations truncated the task Guarded by `src/tests/stall-detector.test.ts` and `src/tests/agent-loop.test.ts`. +## Per-tool file diffs + +Only file edit/write tools (CrewCoder `edit`/`write`/`edit_transaction`/`edit_symbol`, +Claude `Edit`/`MultiEdit`/`Write`/`NotebookEdit`, Codex `Codex file change`/`apply_patch`; +`isFileEditTool`) emit `file_diff`, with per-file unified git patches for **created and +modified files only**, before `tool_execution_end` (`src/core/workspace-diffs.ts`). Shell +commands, git primitives, generators, and delegation are never diffed and deletions are never +reported (owner decision: command diffs are not practical). Snapshots use a private git directory under +`/snapshots`; never point it at, or let it read or write, the user's own +repository, index, or hooks, and keep dropping inherited `GIT_*` variables. Await the +baseline before CrewCoder-executed tools run; provider-native tools use the `prime()` +baseline taken before each model turn because they may write before they are announced. +Tracking must fail closed: any git problem disables it for the run with one `backend_debug` +warning and never fails or delays a tool beyond the snapshot timeout. Off for virtual/hosted +filesystems and `fileDiffs: false`. See `docs/FILE_DIFFS.md`. + ## Runtime guardrails -Filesystem checkpoints are default-on full bounded snapshots (up to 2,000 files/25 MB) and retain only the 10 newest per session; creating another checkpoint removes the oldest snapshot and its session metadata. `config.checkpointsEnabled=false` disables automatic pre-mutation snapshots for future runs without deleting existing checkpoints. +Filesystem checkpoints are opt-in (default off; `crewcoder config set checkpointsEnabled true` or `/checkpoints on`) full bounded snapshots (up to 2,000 files/25 MB) and retain only the 10 newest per session; creating another checkpoint removes the oldest snapshot and its session metadata. A missing or non-`true` `config.checkpointsEnabled` means off; turning it off again stops pre-mutation snapshots for future runs without deleting existing checkpoints. Per-session token budgets are cumulative provider-reported totals, distinct from `currentContextTokens()` live context occupancy. `--budget`/`--max-tokens` persist through `UsageSummary.tokenBudget`; emit warning at 80%, compact before continued tool-driven work when possible, and stop before pending tools or another model turn after the limit. Budget-exhaustion handoff creates a fresh parent-linked session whose only conversation input is the bounded compact summary; never copy the original transcript or exhausted usage into that child. Never change auto-compaction to use cumulative budget totals. `--verify` runs package typecheck/test scripts plus executable trusted extension `validators` and emits `verification_start`/`verification_end`. See `docs/RUNTIME_GUARDRAILS.md`. diff --git a/crewcoder-agent/docs/FILE_DIFFS.md b/crewcoder-agent/docs/FILE_DIFFS.md new file mode 100644 index 0000000..c39d117 --- /dev/null +++ b/crewcoder-agent/docs/FILE_DIFFS.md @@ -0,0 +1,74 @@ +# Per-tool file diffs (`file_diff`) + +Every file edit/write tool call emits a `file_diff` event with the exact changes it made to +files it **created or modified**: CrewCoder `edit`, `write`, `edit_transaction`, +`edit_symbol`, and provider-native edit tools (Claude `Edit`/`MultiEdit`/`Write`/ +`NotebookEdit`, Codex `Codex file change`, `apply_patch`). Shell commands, git primitives, +generators, and delegated workers are **never** diffed, and deleted files are never reported +(owner decision: diffs belong to file edits; command diffs are not practical). Hosts render +it inline under the tool (OpenTUI: `crewcoder-opentui/docs/TRANSCRIPT.md`, "Inline diffs"). + +```txt +{ "type": "file_diff", "toolCallId": "...", "toolName": "edit", + "files": [ { "path": "src/a.ts", "status": "added|modified", + "additions": 3, "deletions": 1, "binary": false, "truncated": false, + "patch": "diff --git a/src/a.ts b/src/a.ts\n..." } ] } +``` + +- Emitted **before** the tool's `tool_execution_end`, only when something changed. +- `path` is workspace-relative. `patch` is one file's unified git diff (3 lines of context); + it is omitted for binary files and for patches over 64 KB (`truncated: true`, counts kept). + At most 50 files per tool call. +- `file_diff` does not replace `file_changed`, which still reports CrewCoder mutation-log paths + for extension file triggers and audit. + +## How it works (`src/core/workspace-diffs.ts`) + +`WorkspaceDiffTracker` keeps a **private snapshot git directory** per workspace at +`/snapshots/`, with the workspace as its work tree. A +snapshot is `git add --all` into that private index plus `git write-tree`; a diff is +`git diff `. + +- The user's own repository, index, refs, and hooks are never read or written. Inherited + `GIT_*` variables are dropped and `GIT_DIR`/`GIT_WORK_TREE` point at the private store. + Works in non-git workspaces too. +- The workspace's `.gitignore` applies, plus default excludes (`node_modules/`, `dist/`, + `build/`, `coverage/`, `.next/`, `.turbo/`, `.crewcoder/`, `.venv/`, `target/`, + `__pycache__/`). +- Cost: the first snapshot of a workspace hashes its files (about 100 ms on this monorepo); + later ones reuse git's stat cache (about 10 ms). +- Operations are serialized (one private index). Any git failure, a missing `git`, or a + snapshot slower than 10 s disables tracking for the rest of the run with one + `backend_debug` warning (`source: "workspace-diffs"`); tools are never slowed or failed by it. +- Disabled for the filesystem root and the home directory, for virtual/hosted filesystems + (ACP remote files, hosted text tools), and by `crewcoder config set fileDiffs false`. + +## Which calls are snapshotted + +`isFileEditTool(name)`, by tool name only: `edit`, `multiedit`, `write`, `notebookedit`, +`strreplace`, `createfile`, `editsymbol`, `edittransaction`, `codexfilechange`, `applypatch` +(normalized: lower case, separators and MCP prefix removed). CrewCoder's `isMutation` flag is +deliberately not used: it also covers git primitives and generators. Reads, searches, and +commands are never snapshotted. + +- **CrewCoder-executed tools**: the "before" snapshot is awaited before the tool runs. +- **Provider-native tools** run in the provider, which may announce them after they start + writing. The loop calls `prime()` before every model turn; their baseline is that + snapshot (or the last finished tool's), never one taken mid-flight. +- **Overlapping (parallel) tools**: each diff starts from the later of the tool's own + baseline and the last finished tool's snapshot, so a change is credited to the first + tool that finishes after it and never reported twice. + +## Limits + +- Changes made by the user or another process while a tool runs are attributed to it. +- The snapshot store grows with changed file contents (unchanged blobs are shared). It is + not pruned yet; deleting `/snapshots` is safe at any time. + +## Tests + +`src/tests/workspace-diffs.test.ts` (created/modified only, no deletions, excludes, the +user's repo untouched, parallel attribution, primed baselines, home-directory refusal, binary +and oversized patches, tool classification) and `src/tests/agent-loop.test.ts` (edit and +write diffs before their `tool_execution_end` and none for `bash`; a provider-native file +change that wrote before it was announced, and none for a native command). diff --git a/crewcoder-agent/docs/SDK.md b/crewcoder-agent/docs/SDK.md index a46557b..37946c3 100644 --- a/crewcoder-agent/docs/SDK.md +++ b/crewcoder-agent/docs/SDK.md @@ -162,6 +162,10 @@ const handoff = await orchestrator.handoff({ Crew workers execute sequentially in the supplied order and each receives a separate durable session. A terminal provider/stall result is reported through lifecycle events and later workers still run; a thrown runtime error stops the crew. `runTeam()` reads a normalized team from `crewcoder.json` and applies role prompts, handoff rules, and shared memory. `handoff()` creates a child session and inherits the source transcript, mutation log, usage, compactions, checkpoints, and extension entries. It is not a detached or parallel operation. The orchestrator rejects overlapping operations. `approve()` and `abort()` are accepted only while it is running. Custom `modelClient` and `heuristic` options are supported for controlled hosts and tests. +Such a client has no provider-native tool executor, so unless you also pass `provider`, the +session runs with CrewCoder's own tools (`read`, `write`, `edit`, `bash`, ...) whatever the +configured tool mode. Pass `provider` only when your client really is that provider's +executor (for example a wrapper around its model client), so native mode applies. ## Session options diff --git a/crewcoder-agent/docs/SESSION_CHECKPOINTS.md b/crewcoder-agent/docs/SESSION_CHECKPOINTS.md index 3a99e2e..924a769 100644 --- a/crewcoder-agent/docs/SESSION_CHECKPOINTS.md +++ b/crewcoder-agent/docs/SESSION_CHECKPOINTS.md @@ -39,7 +39,7 @@ The session record also stores checkpoint metadata in `SessionRecord.checkpoints ## Enable or disable automatic checkpoints -Automatic checkpoints are enabled by default, including for existing config files that do not yet contain the setting. Persist the preference with: +Automatic checkpoints are off by default, including for existing config files that do not yet contain the setting; only an explicit `true` turns them on (and `/rewind` has nothing to restore until then). Persist the preference with: ```bash crewcoder config set checkpointsEnabled true diff --git a/crewcoder-agent/docs/WORKER_CREWS.md b/crewcoder-agent/docs/WORKER_CREWS.md index 92c2666..7fa8ae9 100644 --- a/crewcoder-agent/docs/WORKER_CREWS.md +++ b/crewcoder-agent/docs/WORKER_CREWS.md @@ -28,6 +28,8 @@ crewcoder crew run \ Use `--json` for a machine-readable summary. Use `--json-events` to stream the underlying worker run events. Crew orchestration adds `crew_start`, `crew_worker_start`, `crew_worker_end`, and `crew_end` lifecycle events around each worker stream so hosts can display pending, active, completed, and failed agents without inferring identity from nested agent events. +With `--json-events`, `crew run` and `crew team run` read the same stdin control channel as `run` (`src/core/run-controls.ts`): approvals, follow-ups, extension answers, and compaction requests reach whichever worker is running. Without it, review-mode approvals inside a crew had no answer path and were denied as non-interactive, so every gated tool failed. Without `--json-events` there is still no host, so approvals stay non-interactive (denied). `--effort` now reaches each worker's loop as well as its provider. Hosts should pass the task after `--` so it is never read as options (`crew run --workers a,b --json-events -- `). + ## Use workers and crews from the TUI Workers must already exist before the TUI can use them. Create them from a shell: @@ -94,7 +96,7 @@ Run one of those teams: Each team role receives the task plus its configured description, role prompt, handoff rules, and shared memory. Team roles run sequentially and create separate durable worker sessions. -All commands are also discoverable from `/help` or the command palette. Errors and run summaries are printed in the conversation viewport. See `../../crewcoder-tui/docs/WORKER_CREWS.md` for the full TUI workflow. +All commands are also discoverable from `/help` or the command palette. In the legacy TUI, errors and run summaries are printed in the conversation viewport (see `../../crewcoder-tui/docs/WORKER_CREWS.md`). The OpenTUI UI streams `/crew` and `/team` live, with worker-labelled transcript sections, the sidebar AGENTS roster, and approvals answered from the UI (see `../../crewcoder-opentui/docs/CREWS.md`). ## Handoff to another worker diff --git a/crewcoder-agent/docs/contributor/TUI_BACKEND_CONTRACT.md b/crewcoder-agent/docs/contributor/TUI_BACKEND_CONTRACT.md index ec5e3d5..1558ab1 100644 --- a/crewcoder-agent/docs/contributor/TUI_BACKEND_CONTRACT.md +++ b/crewcoder-agent/docs/contributor/TUI_BACKEND_CONTRACT.md @@ -43,6 +43,7 @@ tool_execution_start tool_delta tool_execution_end file_changed +file_diff background_job_start / background_job_output / background_job_status / background_job_end approval_required approval_resolved @@ -87,11 +88,12 @@ Interactive JSON-event hosts receive `approval_required` before tool execution a - `message_start`: no-op, or use it to prepare per-message UI state. - `assistant_delta`: append to the current assistant bubble. - `thinking_delta`: append to a streaming thinking/reasoning block. CrewCoder forwards provider-supplied reasoning for every provider that exposes it; hosts should render rather than suppress these events. Claude effort levels explicitly enable adaptive thinking, and completed Claude thinking blocks are forwarded when partial deltas are unavailable. `/thinking off` persists `thinkingEnabled=false` and requests `none`/disabled reasoning from all built-in provider paths; `/thinking on` restores the selected effort. -- `message_end`: commit the final user or assistant message if it was not already streamed. Model-generated assistant events optionally include `durationMs` and `outputTokens`; divide output tokens by wall-clock seconds to display the turn's generation throughput. Synthetic and historical messages omit these fields. +- `message_end`: commit the final user or assistant message if it was not already streamed. Model-generated assistant events optionally include `durationMs` and `outputTokens`; divide output tokens by `durationMs` seconds to display the turn's generation throughput. `durationMs` is generation time, not wall-clock: `GenerationClock` (`src/core/generation-clock.ts`) times the model call minus everything inside it that is not the provider producing output (provider-native tools between `onProviderToolStart`/`onProviderToolEnd` and their diff snapshots, CrewCoder tools run through `executeTool` including their approvals, `requestQuestion` waits, follow-up delivery, and usage pricing). It still includes time to first token, and `outputTokens` includes reasoning tokens. Without this, Claude Agent SDK and Codex turns counted their own tool runs as generation and reported a fraction of the real speed. If a provider tool never reports its end, `durationMs` is omitted rather than guessed. Synthetic and historical messages omit both fields. - `tool_execution_start`: open a running tool block with the tool name and arguments. - `tool_delta`: append to the matching tool block output. - `tool_execution_end`: mark the matching tool block success/error and append result details. - `file_changed`: add the path to the changed-files panel. +- `file_diff`: attach the files an edit/write tool created or modified (unified git patches from workspace snapshots; never shell commands, never deletions) to the matching running tool block, before its `tool_execution_end`. See `docs/FILE_DIFFS.md`. - `approval_required`: open an approval UI/block with risk, reason, and arguments. - `approval_resolved`: record the approval outcome in system/status metadata. - `validation_start`: open a running validation block for the target. diff --git a/crewcoder-agent/src/cli.ts b/crewcoder-agent/src/cli.ts index 83f7e65..2f0044e 100644 --- a/crewcoder-agent/src/cli.ts +++ b/crewcoder-agent/src/cli.ts @@ -17,9 +17,8 @@ import { branchSession } from "./core/session-branch.js"; import { whenSessionWritesSettle } from "./core/session-store.js"; import { compactDurableSession } from "./core/compact-session.js"; import { renderSessionHtml, renderSessionMarkdown } from "./core/session-export.js"; -import { attachStdinControlListener, type CompactionPreviewDecision } from "./core/stdin-control.js"; +import { createRunControls } from "./core/run-controls.js"; import { runAcpStdioServer } from "./acp/acp-server.js"; -import { createExtensionUiBridge } from "./core/extension-ui-bridge.js"; import { createPlugin, isSupportedPluginKind } from "./generators/plugin-generator.js"; import { listTemplates, supportedPluginKinds } from "./generators/template-registry.js"; import { createCrewCoderExtension } from "./generators/extension-generator.js"; @@ -486,18 +485,7 @@ session.command("resume").argument("").argument("[prompt...]").option("--jso const jsonSink = options.jsonEvents ? createJsonEventSink() : undefined; const debug = createBackendDebugLogger({ emit: jsonSink, stderr: shouldDebugToStderr(options), runId: `resume-${id}-${Date.now()}` }); await debug.event({ level: "info", source: "cli", message: "backend debug logger initialized", details: { logPath: debug.logPath, jsonEvents: Boolean(options.jsonEvents), stderr: shouldDebugToStderr(options) } }); - const manualCompactSignal = { requested: false, preview: false }; - const compactionPreviewSignal = { decisions: [] as CompactionPreviewDecision[] }; - const followUpSignal = { messages: [] as string[] }; - const approvalSignal = { decisions: [] as Array<{ approvalId: string; approved: boolean; reason?: string }> }; - const uiBridge = createExtensionUiBridge({ emit: jsonSink, hasUI: Boolean(options.jsonEvents) }); - const detachControl = options.jsonEvents ? attachStdinControlListener({ - onCompact: (opts) => { manualCompactSignal.requested = true; if (opts.preview) manualCompactSignal.preview = true; }, - onCompactPreviewDecision: (decision) => { compactionPreviewSignal.decisions.push(decision); }, - onFollowUp: (message) => { followUpSignal.messages.push(message); }, - onApprovalDecision: (decision) => { approvalSignal.decisions.push(decision); }, - onUiResponse: (response) => { uiBridge.resolveResponse(response.requestId, response.value); } - }) : undefined; + const controls = createRunControls(jsonSink); const contextWindow = (await resolveModel(providerId, model))?.metadata?.contextWindow; try { const result = await runAgentLoopContinue({ sessionId: id, prompt: promptParts?.join(" "), mode: normalizeMode(options.mode ?? readConfig().defaultMode), externalDirectories, images: options.image }, { @@ -512,18 +500,13 @@ session.command("resume").argument("").argument("[prompt...]").option("--jso workerName: options.worker, tokenBudget: resolveTokenBudget(options), verify: options.verify, - manualCompactSignal, - compactionPreviewSignal, - followUpSignal, - approvalSignal, - uiBridge, + ...controls.options, emit: jsonSink }); if (!options.jsonEvents) printRunResult(result, providerId, model); if (result.providerError || result.stallError || result.iterationCapReached) process.exitCode = 1; } finally { - uiBridge.cancelAll(); - detachControl?.(); + controls.close(); } }); session.command("branch").argument("").action(async (id: string) => { @@ -1656,7 +1639,7 @@ program.command("config").argument("", "show or set").argument("[key]"). } if (action !== "set") throw new Error("Supported actions: show, set."); if (!key || value === undefined) throw new Error("Usage: config set "); - const allowed = ["toolMode", "defaultMode", "defaultProvider", "defaultModel", "workerIdentityEnabled", "maxIterations", "stallDetection", "stallRepeatThreshold", "stallErrorThreshold", "allowExtensionTools", "allowExtensionHooks", "allowExtensionModules", "allowExtensionLiveUi", "disabledExtensions", "trustedExtensions", "sandboxedExtensions", "sandboxAllowedHosts", "sandboxNetworkIsolation", "checkpointsEnabled", "autoCompact", "autoCompactThresholdTokens", "compactionPreview", "goals.maxTurns", "goals.checkModel", "goals.timeoutMinutes"] as const; + const allowed = ["toolMode", "defaultMode", "defaultProvider", "defaultModel", "workerIdentityEnabled", "maxIterations", "stallDetection", "stallRepeatThreshold", "stallErrorThreshold", "allowExtensionTools", "allowExtensionHooks", "allowExtensionModules", "allowExtensionLiveUi", "disabledExtensions", "trustedExtensions", "sandboxedExtensions", "sandboxAllowedHosts", "sandboxNetworkIsolation", "checkpointsEnabled", "fileDiffs", "autoCompact", "autoCompactThresholdTokens", "compactionPreview", "goals.maxTurns", "goals.checkModel", "goals.timeoutMinutes"] as const; if (!allowed.includes(key as typeof allowed[number])) throw new Error(`Supported keys: ${allowed.join(", ")}`); const config = setConfigValue(key as typeof allowed[number], value); console.log(JSON.stringify(config, null, 2)); @@ -1747,18 +1730,7 @@ async function executeRunPrompt(prompt: string, options: RunOptions) { const jsonSink = jsonEvents ? createJsonEventSink() : undefined; const debug = createBackendDebugLogger({ emit: jsonSink, stderr: shouldDebugToStderr(options), runId: `run-${Date.now()}` }); await debug.event({ level: "info", source: "cli", message: "backend debug logger initialized", details: { logPath: debug.logPath, jsonEvents, stderr: shouldDebugToStderr(options) } }); - const manualCompactSignal = { requested: false, preview: false }; - const compactionPreviewSignal = { decisions: [] as CompactionPreviewDecision[] }; - const followUpSignal = { messages: [] as string[] }; - const approvalSignal = { decisions: [] as Array<{ approvalId: string; approved: boolean; reason?: string }> }; - const uiBridge = createExtensionUiBridge({ emit: jsonSink, hasUI: jsonEvents }); - const detachControl = jsonEvents ? attachStdinControlListener({ - onCompact: (opts) => { manualCompactSignal.requested = true; if (opts.preview) manualCompactSignal.preview = true; }, - onCompactPreviewDecision: (decision) => { compactionPreviewSignal.decisions.push(decision); }, - onFollowUp: (message) => { followUpSignal.messages.push(message); }, - onApprovalDecision: (decision) => { approvalSignal.decisions.push(decision); }, - onUiResponse: (response) => { uiBridge.resolveResponse(response.requestId, response.value); } - }) : undefined; + const controls = createRunControls(jsonSink); const contextWindow = (await resolveModel(providerId, model))?.metadata?.contextWindow; try { const response = await runAgentLoop({ prompt, requestedMode, cwd: process.cwd(), externalDirectories, images: options.image }, { @@ -1775,11 +1747,7 @@ async function executeRunPrompt(prompt: string, options: RunOptions) { tokenBudget: resolveTokenBudget(options), verify: Boolean(options.verify || options.ci), parentSessionId: options.parentSession, - manualCompactSignal, - compactionPreviewSignal, - followUpSignal, - approvalSignal: jsonEvents ? approvalSignal : undefined, - uiBridge, + ...controls.options, emit: jsonSink ?? (async (event) => { const write = options.ci ? console.error : console.log; if (event.type === "tool_execution_start") write(pc.gray(`tool:start ${event.toolName}`)); @@ -1796,8 +1764,7 @@ async function executeRunPrompt(prompt: string, options: RunOptions) { if (response.providerError || response.stallError || response.iterationCapReached) process.exitCode = 1; } } finally { - uiBridge.cancelAll(); - detachControl?.(); + controls.close(); } } @@ -1822,21 +1789,32 @@ async function runCrewPromptWithWorkers(prompt: string, workers: string[], optio const debug = createBackendDebugLogger({ emit: jsonSink, stderr: shouldDebugToStderr(options), runId: `${label}-run-${Date.now()}` }); await debug.event({ level: "info", source: "cli", message: `${label} run backend debug logger initialized`, details: { logPath: debug.logPath, jsonEvents, stderr: shouldDebugToStderr(options) } }); const contextWindow = (await resolveModel(providerId, model))?.metadata?.contextWindow; - const result = await runWorkerCrew({ prompt, workers, workerPrompts, requestedMode, cwd: process.cwd() }, { - providerId, - model, - contextWindow, - maxIterations, - approvalMode: normalizeApprovalMode(options.approval ?? "never"), - dumpModelInput: shouldDumpModelInput(options), - systemPromptName: options.systemPrompt, - createModelClient: () => options.heuristic ? undefined : new ProviderModelClient(providerId, process.cwd(), model, debug, normalizeEffortOption(options.effort)), - emit: jsonSink ?? (async (event) => { - if (event.type === "tool_execution_start") console.log(pc.gray(`tool:start ${event.toolName}`)); - if (event.type === "tool_execution_end") console.log(event.isError ? pc.red(`tool:error ${event.toolName}`) : pc.gray(`tool:end ${event.toolName}`)); - if (event.type === "approval_required") console.log(pc.yellow(`approval required: ${event.reason}`)); - }) - }); + const effort = normalizeEffortOption(options.effort); + // The same live control channel as `run`: without it, review-mode approvals inside a + // TUI-driven crew were auto-denied as non-interactive and every gated tool failed. + const controls = createRunControls(jsonSink); + let result: Awaited>; + try { + result = await runWorkerCrew({ prompt, workers, workerPrompts, requestedMode, cwd: process.cwd() }, { + providerId, + model, + effort, + contextWindow, + maxIterations, + approvalMode: normalizeApprovalMode(options.approval ?? "never"), + dumpModelInput: shouldDumpModelInput(options), + systemPromptName: options.systemPrompt, + ...controls.options, + createModelClient: () => options.heuristic ? undefined : new ProviderModelClient(providerId, process.cwd(), model, debug, effort), + emit: jsonSink ?? (async (event) => { + if (event.type === "tool_execution_start") console.log(pc.gray(`tool:start ${event.toolName}`)); + if (event.type === "tool_execution_end") console.log(event.isError ? pc.red(`tool:error ${event.toolName}`) : pc.gray(`tool:end ${event.toolName}`)); + if (event.type === "approval_required") console.log(pc.yellow(`approval required: ${event.reason}`)); + }) + }); + } finally { + controls.close(); + } if (options.json) { console.log(JSON.stringify(result, null, 2)); return; diff --git a/crewcoder-agent/src/core/agent-loop.ts b/crewcoder-agent/src/core/agent-loop.ts index ce63762..848ef09 100644 --- a/crewcoder-agent/src/core/agent-loop.ts +++ b/crewcoder-agent/src/core/agent-loop.ts @@ -43,7 +43,9 @@ import { recordModelUsageCost } from "./cost-ledger.js"; import { readCrewTasksConfig } from "../crew-tasks/config.js"; import { readMemoryContext } from "./memory-store.js"; import { readRulesContext } from "./rules-store.js"; +import { isFileEditTool, WorkspaceDiffTracker, type DiffBaseline } from "./workspace-diffs.js"; import { formatExternalDirectories, validateExternalDirectories } from "./external-directories.js"; +import { GenerationClock } from "./generation-clock.js"; import type { ApprovalControlDecision, CompactionPreviewDecision } from "./stdin-control.js"; import { createCrewcoderWorkflowTools } from "../modes/crewcoder-tools.js"; import { @@ -372,7 +374,15 @@ export async function runAgentLoop(request: AgentRequest, options: AgentLoopOpti const sandbox = buildSandboxContext(approvalMode, request.cwd, externalDirectories); const workerDelegationDepth = options.workerDelegationDepth ?? 0; const maxChildWorkerDepth = options.maxChildWorkerDepth ?? 1; + // Per-tool diffs need the real local workspace; a virtual/hosted filesystem has none here. + const workspaceDiffs = !virtualFilesystem && runtimeConfig.fileDiffs + ? new WorkspaceDiffTracker(request.cwd, { + onDisabled: (reason) => void emit({ type: "backend_debug", timestamp: new Date().toISOString(), level: "warn", source: "workspace-diffs", message: "per-tool file diffs disabled", details: { reason } }) + }) + : undefined; + const providerDiffBaselines = new Map>(); const toolContext: ToolContext = { + workspaceDiffs, cwd: request.cwd, externalDirectories, mode, @@ -660,7 +670,10 @@ export async function runAgentLoop(request: AgentRequest, options: AgentLoopOpti const dumpPath = await dumpModelInput(modelInput, { sessionId, iteration, providerId: options.providerId, model: options.model }); await emit({ type: "backend_debug", timestamp: new Date().toISOString(), level: "info", source: "agent-loop", message: "model input dumped", details: { path: dumpPath, iteration, providerId: options.providerId, model: options.model } }); } - const generationStartedAt = performance.now(); + // Baseline for provider-native tools that this turn may run (see onProviderToolStart). + await workspaceDiffs?.prime(); + // Throughput time: the call minus tools and user waits inside it (see GenerationClock). + const generationClock = new GenerationClock(); assistant = await modelClient.complete(modelInput, options.signal, { async deliverFollowUps(deliver) { // Native tools can finish concurrently; only one boundary owns a batch. @@ -668,7 +681,7 @@ export async function runAgentLoop(request: AgentRequest, options: AgentLoopOpti if (await drainFollowUps(deliver)) await persistTurn(iteration); }); followUpDelivery = delivery.catch(() => undefined); - await delivery; + await generationClock.excluding(() => delivery); }, async onAssistantDelta(text, phase) { streamedAssistantText = true; @@ -681,45 +694,61 @@ export async function runAgentLoop(request: AgentRequest, options: AgentLoopOpti if (options.providerId) providerSessionIds[options.providerId] = providerSessionId; }, async onProviderToolStart(call) { + generationClock.toolStarted(call.id); if (crewcoderWorkflow) providerToolArgs.set(call.id, call.arguments); + // The provider runs the tool itself; snapshot now so its file changes can be diffed at the end. + if (workspaceDiffs && isFileEditTool(call.name)) providerDiffBaselines.set(call.id, workspaceDiffs.begin({ reuseLast: true })); await emit({ type: "tool_execution_start", toolCallId: call.id, toolName: call.name, args: call.arguments }); }, async onProviderToolEnd(result) { - const message: ToolResultMessage = { role: "toolResult", toolCallId: result.toolCallId, toolName: result.toolName, content: [{ type: "text", text: result.text }], isError: result.isError, timestamp: Date.now() }; - recordCrewcoderInspection(crewcoderWorkflow, message, providerToolArgs.get(result.toolCallId) ?? {}); - providerToolArgs.delete(result.toolCallId); - await emit({ type: "tool_execution_end", toolCallId: result.toolCallId, toolName: result.toolName, result: message, isError: result.isError }); + // The tool counts as running until its diff snapshot and events are done: that is CrewCoder time, not generation. + try { + const message: ToolResultMessage = { role: "toolResult", toolCallId: result.toolCallId, toolName: result.toolName, content: [{ type: "text", text: result.text }], isError: result.isError, timestamp: Date.now() }; + recordCrewcoderInspection(crewcoderWorkflow, message, providerToolArgs.get(result.toolCallId) ?? {}); + providerToolArgs.delete(result.toolCallId); + const baseline = providerDiffBaselines.get(result.toolCallId); + providerDiffBaselines.delete(result.toolCallId); + await emitFileDiffs(workspaceDiffs, baseline, result.toolCallId, result.toolName, emit); + await emit({ type: "tool_execution_end", toolCallId: result.toolCallId, toolName: result.toolName, result: message, isError: result.isError }); + } finally { + generationClock.toolEnded(result.toolCallId); + } }, async executeTool(call) { - const [result] = await executeToolCalls([call], tools, toolContext, emit, approvalMode, extensionHooks, extensionFileTriggers, extensionApprovalPolicies, extensionRuntime, checkpoints, checkpointsEnabled, options.signal, options.approvalSignal, options.uiBridge); + const [result] = await generationClock.excluding(() => executeToolCalls([call], tools, toolContext, emit, approvalMode, extensionHooks, extensionFileTriggers, extensionApprovalPolicies, extensionRuntime, checkpoints, checkpointsEnabled, options.signal, options.approvalSignal, options.uiBridge)); return result ?? { role: "toolResult", toolCallId: call.id, toolName: call.name, content: [{ type: "text", text: `Tool ${call.name} did not return a result.` }], isError: true, timestamp: Date.now() }; }, async requestQuestion(question) { - if (options.requestQuestion) return options.requestQuestion(question); - if (!options.uiBridge) return undefined; - const ui = options.uiBridge.uiFor("claude-sdk"); - return question.options?.length - ? await ui.select(question.title, question.options) - : await ui.input(question.title, { placeholder: question.placeholder }); + const ask = options.requestQuestion; + if (ask) return generationClock.excluding(() => ask(question)); + const uiBridge = options.uiBridge; + if (!uiBridge) return undefined; + const ui = uiBridge.uiFor("claude-sdk"); + return generationClock.excluding(() => (question.options?.length + ? ui.select(question.title, question.options) + : ui.input(question.title, { placeholder: question.placeholder }))); }, async onUsage(reportedUsage) { - const usage = await priceTurn(reportedUsage); - turnUsage = usage; - usageSummary = addUsage(usageSummary, usage); - refreshCompactionPolicy(); - if (typeof tokenBudget === "number") { - const status = tokenBudgetStatus(usageSummary, tokenBudget); - budgetExceeded = status.exceeded; - usageSummary = { ...usageSummary, tokenBudget, budgetExceeded }; - await emit({ type: "usage_update", usage, summary: usageSummary }); - if (!budgetWarningEmitted && status.warningReached) { - budgetWarningEmitted = true; - budgetDownshiftRequested = !status.exceeded; - await emit({ type: "token_budget_warning", limit: status.limit, used: status.used, remaining: status.remaining, percent: status.percent }); + // Pricing (catalog lookup) and budget bookkeeping are CrewCoder time, not generation. + await generationClock.excluding(async () => { + const usage = await priceTurn(reportedUsage); + turnUsage = usage; + usageSummary = addUsage(usageSummary, usage); + refreshCompactionPolicy(); + if (typeof tokenBudget === "number") { + const status = tokenBudgetStatus(usageSummary, tokenBudget); + budgetExceeded = status.exceeded; + usageSummary = { ...usageSummary, tokenBudget, budgetExceeded }; + await emit({ type: "usage_update", usage, summary: usageSummary }); + if (!budgetWarningEmitted && status.warningReached) { + budgetWarningEmitted = true; + budgetDownshiftRequested = !status.exceeded; + await emit({ type: "token_budget_warning", limit: status.limit, used: status.used, remaining: status.remaining, percent: status.percent }); + } + return; } - return; - } - await emit({ type: "usage_update", usage, summary: usageSummary }); + await emit({ type: "usage_update", usage, summary: usageSummary }); + }); }, async onProviderCompaction(update) { await emit({ @@ -731,7 +760,7 @@ export async function runAgentLoop(request: AgentRequest, options: AgentLoopOpti }); } }); - generationDurationMs = performance.now() - generationStartedAt; + generationDurationMs = generationClock.finish(); assistant = assignAssistantHashes(assistant, modelInput); modelTurns.push({ iteration, input: modelInput, promptHash: assistant.promptHash ?? "", responseHash: assistant.responseHash ?? "", responseId: assistant.id ?? "" }); } catch (error) { @@ -1234,6 +1263,8 @@ async function executeToolCallsSequential( const startMetadata = toolEventMetadata(toolCall.name); await emit({ type: "tool_execution_start", toolCallId: toolCall.id, toolName: toolCall.name, args: toolCall.arguments, metadata: startMetadata }); + // Awaited: the snapshot must finish before the tool can write. + const diffBaseline = context.workspaceDiffs && isFileEditTool(toolCall.name) ? Promise.resolve(await context.workspaceDiffs.begin()) : undefined; const isValidation = toolCall.name === "validatePlugin"; if (isValidation) { @@ -1316,6 +1347,7 @@ async function executeToolCallsSequential( }); } + await emitFileDiffs(context.workspaceDiffs, diffBaseline, toolCall.id, toolCall.name, emit); await emit({ type: "tool_execution_end", toolCallId: toolCall.id, toolName: toolCall.name, result, isError: result.isError, metadata: mergeToolMetadata(startMetadata, result.details) }); results.push(result); if (result.terminate) break; @@ -1352,6 +1384,19 @@ async function appendAuditEvent( } } +/** Emit the files a tool changed since `baseline`, before its `tool_execution_end`. Never throws. */ +async function emitFileDiffs( + tracker: WorkspaceDiffTracker | undefined, + baseline: Promise | undefined, + toolCallId: string, + toolName: string, + emit: AgentEventSink +): Promise { + if (!tracker || !baseline) return; + const files = await tracker.end(await baseline).catch(() => []); + if (files.length > 0) await emit({ type: "file_diff", toolCallId, toolName, files }); +} + function toolEventMetadata(toolName: string): Record | undefined { if (!toolName.startsWith("extension_")) return undefined; return { source: "extension" }; diff --git a/crewcoder-agent/src/core/config.ts b/crewcoder-agent/src/core/config.ts index cb9e076..ba36ad8 100644 --- a/crewcoder-agent/src/core/config.ts +++ b/crewcoder-agent/src/core/config.ts @@ -63,6 +63,8 @@ export interface CrewCoderConfig { workerIdentityEnabled: boolean; /** Create bounded filesystem snapshots before mutating tools. */ checkpointsEnabled: boolean; + /** Emit per-tool `file_diff` events from private workspace snapshots (default on). */ + fileDiffs: boolean; autoCompact: boolean; /** Optional fallback used only when the active model's context window is unknown. */ autoCompactThresholdTokens?: number; @@ -113,6 +115,7 @@ const defaultConfig: CrewCoderConfig = { activeWorker: "Crew", workerIdentityEnabled: false, checkpointsEnabled: false, + fileDiffs: true, autoCompact: true, compactionPreview: false, autoActivateExtensionSkills: true, @@ -190,6 +193,8 @@ export function setConfigValue(key: CrewCoderConfigSetKey, value: string): CrewC next.allowExtensionLiveUi = value === "true"; } else if (key === "checkpointsEnabled") { next.checkpointsEnabled = value === "true"; + } else if (key === "fileDiffs") { + next.fileDiffs = value === "true"; } else if (key === "autoCompact") { next.autoCompact = value === "true"; } else if (key === "compactionPreview") { @@ -296,7 +301,9 @@ function normalizeConfig(input: Partial): CrewCoderConfig { sandboxNetworkIsolation: input.sandboxNetworkIsolation === "strict" ? "strict" : "proxy", activeWorker: typeof input.activeWorker === "string" && input.activeWorker.trim() ? input.activeWorker.trim() : defaultConfig.activeWorker, workerIdentityEnabled: input.workerIdentityEnabled === true, - checkpointsEnabled: input.checkpointsEnabled !== false, + // Opt-in: automatic checkpoints stay off until the user turns them on. + checkpointsEnabled: input.checkpointsEnabled === true, + fileDiffs: input.fileDiffs !== false, autoCompact: input.autoCompact !== false, ...(typeof input.autoCompactThresholdTokens === "number" && Number.isInteger(input.autoCompactThresholdTokens) ? { autoCompactThresholdTokens: Math.min(Math.max(input.autoCompactThresholdTokens, MIN_COMPACT_THRESHOLD), MAX_COMPACT_THRESHOLD) } diff --git a/crewcoder-agent/src/core/events.ts b/crewcoder-agent/src/core/events.ts index ef29c7e..6a597ec 100644 --- a/crewcoder-agent/src/core/events.ts +++ b/crewcoder-agent/src/core/events.ts @@ -2,6 +2,7 @@ import type { AgentMessage, AssistantMessage, ToolResultMessage } from "./messag import type { CrewCoderExtUiAction, CrewCoderExtUiComponent } from "../extensions/api.js"; import type { BackendDebugEvent } from "./backend-debug-logger.js"; import type { ModelUsage, UsageSummary } from "./usage.js"; +import type { WorkspaceFileDiff } from "./workspace-diffs.js"; import type { ProviderProgressUpdate } from "../providers/codex-progress.js"; @@ -35,6 +36,8 @@ export type AgentEvent = | { type: "tool_delta"; toolCallId: string; toolName: string; text: string; metadata?: Record } | { type: "tool_execution_end"; toolCallId: string; toolName: string; result: ToolResultMessage; isError: boolean; metadata?: Record } | { type: "file_changed"; path: string; toolName: string } + /** Files a tool call changed, from workspace snapshots before and after it (`workspace-diffs.ts`). Emitted before its `tool_execution_end`. */ + | { type: "file_diff"; toolCallId: string; toolName: string; files: WorkspaceFileDiff[] } | { type: "background_job_start"; bgId: string; command: string; cwd: string; startedAt: string } | { type: "background_job_output"; bgId: string; text: string } | { type: "background_job_status"; bgId: string; command: string; status: "running" | "completed" | "failed" | "stopped"; output: string; exitCode?: number | null; startedAt: string; endedAt?: string } diff --git a/crewcoder-agent/src/core/generation-clock.ts b/crewcoder-agent/src/core/generation-clock.ts new file mode 100644 index 0000000..505e414 --- /dev/null +++ b/crewcoder-agent/src/core/generation-clock.ts @@ -0,0 +1,67 @@ +/** + * Time a model call spends producing output, for the `durationMs` that hosts + * divide `outputTokens` by to show throughput (tok/s). + * + * One `ModelClient.complete()` call can contain more than generation: Claude + * Agent SDK and Codex run their own tool loop inside it, CrewCoder tools run + * through `executeTool`, and approvals and questions wait for the user. Those + * intervals are excluded. What remains is the provider's own time, including + * time to first token and reasoning (reasoning tokens are part of the output). + * + * Pauses nest and overlap (parallel native tools, an approval inside a tool), + * so time is excluded while at least one is open. If a provider tool never + * reports its end, the duration is unknown and `finish()` returns undefined + * rather than a number that silently excludes the rest of the call. + */ +export class GenerationClock { + private readonly now: () => number; + private readonly startedAt: number; + private readonly openTools = new Set(); + private openPauses = 0; + private pausedAt = 0; + private pausedTotal = 0; + + constructor(now: () => number = () => performance.now()) { + this.now = now; + this.startedAt = now(); + } + + /** A provider-native tool started; time stops until it (and every other open pause) ends. */ + toolStarted(id: string): void { + if (this.openTools.has(id)) return; + this.openTools.add(id); + this.pause(); + } + + /** A provider-native tool ended. An end without a start is ignored. */ + toolEnded(id: string): void { + if (!this.openTools.delete(id)) return; + this.resume(); + } + + /** Run work that is not generation (tool execution, waiting for the user) with the clock paused. */ + async excluding(work: () => Promise): Promise { + this.pause(); + try { + return await work(); + } finally { + this.resume(); + } + } + + /** Milliseconds of generation so far, or undefined while a provider tool is still open. */ + finish(): number | undefined { + if (this.openTools.size > 0) return undefined; + const pausedNow = this.openPauses > 0 ? this.now() - this.pausedAt : 0; + return Math.max(0, this.now() - this.startedAt - this.pausedTotal - pausedNow); + } + + private pause(): void { + if (this.openPauses++ === 0) this.pausedAt = this.now(); + } + + private resume(): void { + if (this.openPauses === 0) return; + if (--this.openPauses === 0) this.pausedTotal += this.now() - this.pausedAt; + } +} diff --git a/crewcoder-agent/src/core/run-controls.ts b/crewcoder-agent/src/core/run-controls.ts new file mode 100644 index 0000000..5673744 --- /dev/null +++ b/crewcoder-agent/src/core/run-controls.ts @@ -0,0 +1,50 @@ +import type { AgentLoopOptions } from "./agent-loop.js"; +import type { AgentEventSink } from "./events.js"; +import { createExtensionUiBridge } from "./extension-ui-bridge.js"; +import { attachStdinControlListener, type ApprovalControlDecision, type CompactionPreviewDecision } from "./stdin-control.js"; + +/** The live-control options a `--json-events` run hands to every agent loop it starts. */ +export type RunControlOptions = Required> + & Pick; + +export type RunControls = { + options: RunControlOptions; + /** Cancel open extension questions and stop reading stdin. */ + close(): void; +}; + +/** + * The stdin control channel for a CLI run: approvals, follow-ups, compaction, + * and extension answers from the TUI. Without a JSON event sink there is no + * host to answer, so approvals stay non-interactive (the loop denies them) and + * stdin is left alone. `run`, `crew run`, and `crew team run` share this so a + * crew answers approvals exactly like a single run; a crew passes the same + * signals to each worker, so decisions reach whichever worker is running. + */ +export function createRunControls(jsonSink: AgentEventSink | undefined): RunControls { + const manualCompactSignal = { requested: false, preview: false }; + const compactionPreviewSignal = { decisions: [] as CompactionPreviewDecision[] }; + const followUpSignal = { messages: [] as string[] }; + const approvalSignal = { decisions: [] as ApprovalControlDecision[] }; + const uiBridge = createExtensionUiBridge({ emit: jsonSink, hasUI: Boolean(jsonSink) }); + const detach = jsonSink ? attachStdinControlListener({ + onCompact: (opts) => { manualCompactSignal.requested = true; if (opts.preview) manualCompactSignal.preview = true; }, + onCompactPreviewDecision: (decision) => { compactionPreviewSignal.decisions.push(decision); }, + onFollowUp: (message) => { followUpSignal.messages.push(message); }, + onApprovalDecision: (decision) => { approvalSignal.decisions.push(decision); }, + onUiResponse: (response) => { uiBridge.resolveResponse(response.requestId, response.value); } + }) : undefined; + return { + options: { + manualCompactSignal, + compactionPreviewSignal, + followUpSignal, + approvalSignal: jsonSink ? approvalSignal : undefined, + uiBridge + }, + close() { + uiBridge.cancelAll(); + detach?.(); + } + }; +} diff --git a/crewcoder-agent/src/core/tool-types.ts b/crewcoder-agent/src/core/tool-types.ts index 83c9040..3d42944 100644 --- a/crewcoder-agent/src/core/tool-types.ts +++ b/crewcoder-agent/src/core/tool-types.ts @@ -4,6 +4,7 @@ import type { SandboxContext } from "./sandbox.js"; import type { ResolvedAgentMode } from "./types.js"; import type { IntegrationProfile } from "./integration-profile.js"; import type { CrewcoderWorkflowState } from "../modes/crewcoder-workflow.js"; +import type { WorkspaceDiffTracker } from "./workspace-diffs.js"; export type ToolExecutionMode = "sequential" | "parallel"; export type JsonSchema = boolean | JsonObjectSchema | JsonArraySchema | JsonStringSchema | JsonNumberSchema | JsonBooleanSchema; export type JsonObjectSchema = { type: "object"; properties?: Record; required?: string[]; additionalProperties?: boolean | JsonSchema; description?: string }; @@ -29,7 +30,7 @@ export type TextFileHost = { writeTextFile?(absolutePath: string, content: string): Promise; }; -export type ToolContext = { cwd: string; externalDirectories?: string[]; mode: ResolvedAgentMode; integrationProfile?: IntegrationProfile; sessionId: string; mutationLog: string[]; sandbox?: SandboxContext; emit?: AgentEventSink; textFiles?: TextFileHost; delegateWorker?: (request: ChildWorkerDelegateRequest, signal?: AbortSignal) => Promise; crewcoderWorkflow?: CrewcoderWorkflowState }; +export type ToolContext = { cwd: string; externalDirectories?: string[]; mode: ResolvedAgentMode; integrationProfile?: IntegrationProfile; sessionId: string; mutationLog: string[]; sandbox?: SandboxContext; emit?: AgentEventSink; textFiles?: TextFileHost; delegateWorker?: (request: ChildWorkerDelegateRequest, signal?: AbortSignal) => Promise; crewcoderWorkflow?: CrewcoderWorkflowState; workspaceDiffs?: WorkspaceDiffTracker }; export type ToolDefinition = Record> = { name: string; description: string; diff --git a/crewcoder-agent/src/core/workspace-diffs.ts b/crewcoder-agent/src/core/workspace-diffs.ts new file mode 100644 index 0000000..7a1fef8 --- /dev/null +++ b/crewcoder-agent/src/core/workspace-diffs.ts @@ -0,0 +1,257 @@ +import { execFile } from "node:child_process"; +import { createHash } from "node:crypto"; +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { getCrewCoderHome } from "./crewcoder-home.js"; + +/** + * One file a tool call changed, as a unified git diff. `patch` is omitted for + * binary files and for patches over MAX_FILE_PATCH_BYTES (`truncated`). + */ +export type WorkspaceFileDiff = { + path: string; + /** Only created and modified files are reported; deletions are not. */ + status: "added" | "modified"; + additions: number; + deletions: number; + binary: boolean; + truncated: boolean; + patch?: string; +}; + +/** A snapshot taken when a tool started; see `WorkspaceDiffTracker.end`. */ +export type DiffBaseline = { tree: string; seq: number }; + +const MAX_FILE_PATCH_BYTES = 64 * 1024; +const MAX_FILES = 50; +const SNAPSHOT_TIMEOUT_MS = 10_000; +const DIFF_MAX_BUFFER = 16 * 1024 * 1024; +/** Heavy or generated directories never snapshotted, on top of the workspace's own .gitignore. */ +const DEFAULT_EXCLUDES = ["node_modules/", "dist/", "build/", "coverage/", ".next/", ".turbo/", ".crewcoder/", ".venv/", "target/", "__pycache__/"]; + +/** + * Normalized names (lower case, no separators, MCP prefix removed) of the + * tools that create or modify files: CrewCoder's and the provider-native edit + * and write tools (Claude, Codex). Shell commands, git primitives, generators, + * and delegated workers are deliberately not diffed (owner decision). + */ +const FILE_EDIT_TOOLS = new Set([ + "edit", "multiedit", "write", "notebookedit", "strreplace", "createfile", "editsymbol", "edittransaction", + "codexfilechange", "applypatch" +]); + +export function isFileEditTool(toolName: string): boolean { + const key = toolName.replace(/^mcp__[^_]+(?:_[^_]+)*__/, "").toLowerCase().replace(/[^a-z0-9]/g, ""); + return FILE_EDIT_TOOLS.has(key); +} + +export type WorkspaceDiffOptions = { + /** Where snapshot git directories live; defaults to `/snapshots`. */ + snapshotsDir?: string; + /** Reports why tracking turned itself off (git missing, too slow, ...). */ + onDisabled?: (reason: string) => void; +}; + +/** + * Per-tool file diffs from workspace snapshots, so changes made any way (shell + * commands, provider-native tools) show up, not only CrewCoder edit tools. + * + * Snapshots live in a private git directory under CrewCoder's home with the + * workspace as its work tree: `git add --all` + `write-tree` against a private + * index. The user's own repository, index, and history are never read or + * written, and non-git workspaces work too. The workspace's .gitignore applies, + * plus DEFAULT_EXCLUDES. Operations are serialized (one index). Any git failure + * or a snapshot slower than SNAPSHOT_TIMEOUT_MS disables tracking for the run + * rather than slowing tools down. + */ +export class WorkspaceDiffTracker { + private readonly cwd: string; + private readonly options: WorkspaceDiffOptions; + private gitDir: string | undefined; + private ready: Promise | undefined; + private disabled = false; + private queue: Promise = Promise.resolve(); + private seq = 0; + private lastEnd: DiffBaseline | undefined; + + constructor(cwd: string, options: WorkspaceDiffOptions = {}) { + this.cwd = path.resolve(cwd); + this.options = options; + } + + /** + * Snapshot the workspace as the current baseline (before a model turn), so a + * provider-native tool that is already running when it is announced still + * has a baseline from before it started (see `begin({ reuseLast })`). + */ + prime(): Promise { + return this.serial(async () => { + const tree = await this.snapshot(); + if (tree) this.lastEnd = { tree, seq: ++this.seq }; + }); + } + + /** + * Baseline before a tool runs; await it before running the tool. `reuseLast` + * takes the latest snapshot instead of a new one, for provider-native tools + * that may already be writing when they are announced. Undefined when + * tracking is off or failed. + */ + begin(options: { reuseLast?: boolean } = {}): Promise { + return this.serial(async () => { + if (options.reuseLast && this.lastEnd) return { tree: this.lastEnd.tree, seq: this.seq }; + const tree = await this.snapshot(); + return tree ? { tree, seq: ++this.seq } : undefined; + }); + } + + /** + * Snapshot after the tool and diff it against the most recent of its own + * baseline and the last finished tool's snapshot, so overlapping (parallel) + * tools never report the same change twice: a change is credited to the + * first tool that finishes after it. + */ + end(baseline: DiffBaseline | undefined): Promise { + if (!baseline) return Promise.resolve([]); + return this.serial(async () => { + const tree = await this.snapshot(); + if (!tree) return []; + const from = this.lastEnd && this.lastEnd.seq > baseline.seq ? this.lastEnd.tree : baseline.tree; + this.lastEnd = { tree, seq: ++this.seq }; + return from === tree ? [] : await this.diff(from, tree); + }); + } + + private serial(work: () => Promise): Promise { + const next = this.queue.then(work, work); + this.queue = next.catch(() => undefined); + return next; + } + + private async snapshot(): Promise { + if (this.disabled || !(await this.init())) return undefined; + try { + await this.git(["add", "--all", "--ignore-errors", "--", "."], SNAPSHOT_TIMEOUT_MS).catch((error: unknown) => { + // --ignore-errors still exits non-zero for unreadable files after adding the rest. + if (!isExitError(error)) throw error; + }); + return (await this.git(["write-tree"], SNAPSHOT_TIMEOUT_MS)).trim(); + } catch (error) { + this.disable(`snapshot failed: ${errorText(error)}`); + return undefined; + } + } + + private async diff(from: string, to: string): Promise { + try { + const base = ["diff", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames"]; + const [names, counts, patch] = await Promise.all([ + this.git([...base, "--name-status", "-z", from, to]), + this.git([...base, "--numstat", "-z", from, to]), + this.git([...base, "--unified=3", from, to]) + ]); + return buildFileDiffs(names, counts, patch); + } catch (error) { + this.disable(`diff failed: ${errorText(error)}`); + return []; + } + } + + /** Create the private snapshot repository once. False disables tracking. */ + private init(): Promise { + this.ready ??= (async () => { + if (this.cwd === path.parse(this.cwd).root || this.cwd === os.homedir()) { + this.disable("workspace is the filesystem root or home directory"); + return false; + } + const root = this.options.snapshotsDir ?? path.join(getCrewCoderHome().root, "snapshots"); + const gitDir = path.join(root, createHash("sha256").update(this.cwd).digest("hex").slice(0, 16)); + try { + await fs.mkdir(root, { recursive: true, mode: 0o700 }); + this.gitDir = gitDir; + await fs.access(path.join(gitDir, "HEAD")).catch(async () => { + await this.git(["init", "--quiet"]); + await fs.mkdir(path.join(gitDir, "info"), { recursive: true }); + await fs.writeFile(path.join(gitDir, "info", "exclude"), `${DEFAULT_EXCLUDES.join("\n")}\n`, "utf8"); + }); + return true; + } catch (error) { + this.disable(`git unavailable: ${errorText(error)}`); + return false; + } + })(); + return this.ready; + } + + private disable(reason: string): void { + if (this.disabled) return; + this.disabled = true; + this.options.onDisabled?.(reason); + } + + /** Runs git against the private snapshot repository only; inherited GIT_* variables are dropped. */ + private git(args: string[], timeout = SNAPSHOT_TIMEOUT_MS): Promise { + const env: NodeJS.ProcessEnv = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_"))); + env.GIT_DIR = this.gitDir; + env.GIT_WORK_TREE = this.cwd; + env.GIT_TERMINAL_PROMPT = "0"; + const config = ["-c", "core.autocrlf=false", "-c", "core.quotepath=off", "-c", "core.fsmonitor=false", "-c", "core.safecrlf=false", "-c", "advice.addEmbeddedRepo=false", "-c", "gc.auto=0"]; + return new Promise((resolve, reject) => { + execFile("git", [...config, ...args], { cwd: this.cwd, env, timeout, maxBuffer: DIFF_MAX_BUFFER, encoding: "utf8" }, (error, stdout) => { + if (error) reject(error); + else resolve(stdout); + }); + }); + } +} + +/** Combine `--name-status -z`, `--numstat -z`, and the full patch into per-file diffs. */ +export function buildFileDiffs(nameStatus: string, numstat: string, patch: string): WorkspaceFileDiff[] { + const status = new Map(); + const names = nameStatus.split("\0").filter(Boolean); + for (let index = 0; index + 1 < names.length; index += 2) { + const code = names[index]!; + status.set(names[index + 1]!, code.startsWith("A") ? "added" : code.startsWith("D") ? "deleted" : "modified"); + } + const patches = splitPatch(patch); + const changed = numstat.split("\0").filter(Boolean).filter((line) => status.get(line.split("\t")[2] ?? "") !== "deleted"); + return changed.slice(0, MAX_FILES).map((line) => { + const [added = "0", deleted = "0", file = ""] = line.split("\t"); + const binary = added === "-"; + const text = patches.get(file); + const truncated = !binary && text !== undefined && Buffer.byteLength(text) > MAX_FILE_PATCH_BYTES; + return { + path: file, + status: status.get(file) === "added" ? "added" : "modified", + additions: binary ? 0 : Number(added), + deletions: binary ? 0 : Number(deleted), + binary, + truncated, + ...(binary || truncated || text === undefined ? {} : { patch: text }) + }; + }); +} + +/** Split a multi-file `git diff` into one patch per path (the `b/` side, or `a/` for deletions). */ +function splitPatch(patch: string): Map { + const files = new Map(); + for (const part of patch.split(/^(?=diff --git )/m)) { + if (!part.startsWith("diff --git ")) continue; + const plus = /^\+\+\+ b\/(.+)$/m.exec(part)?.[1]; + const minus = /^--- a\/(.+)$/m.exec(part)?.[1]; + const header = /^diff --git a\/(.+) b\/(.+)$/m.exec(part); + const file = plus ?? minus ?? header?.[2]; + if (file) files.set(file, part); + } + return files; +} + +/** `git add --ignore-errors` exits 1 when some files could not be read; anything else is a real failure. */ +function isExitError(error: unknown): boolean { + return typeof error === "object" && error !== null && (error as { code?: unknown }).code === 1; +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message.split("\n", 1)[0] ?? error.message : String(error); +} diff --git a/crewcoder-agent/src/providers/codex-app-server-provider.ts b/crewcoder-agent/src/providers/codex-app-server-provider.ts index 5abf100..e324430 100644 --- a/crewcoder-agent/src/providers/codex-app-server-provider.ts +++ b/crewcoder-agent/src/providers/codex-app-server-provider.ts @@ -243,10 +243,22 @@ export async function runCodexCliProvider(input: ProviderRunInput, signal?: Abor /** codex-cli has no fallback transport, so the real start failure is the only useful hint. */ export function codexCliStartFailure(command: string, cause?: { message: string; code?: string; stderr: string }): string { if (cause?.code === "ENOENT") return `Codex CLI was not found: \`${command}\` is not on the PATH CrewCoder was started with. Add the Codex CLI's bin directory to PATH or set CREWCODER_CODEX_PATH to the codex executable.`; - const detail = [cause?.message, cause?.stderr.split("\n").filter(Boolean).slice(-3).join(" | ")].filter(Boolean).join("; ").slice(0, 600); + const detail = [cause?.message, stderrSummary(cause?.stderr ?? "")].filter(Boolean).join("; ").slice(0, 600); return `Codex CLI app-server could not start${detail ? ` (${detail})` : ""}. Check \`${command} login status\`, or set CREWCODER_CODEX_PATH.`; } +/** + * The first error line of a crash plus its last lines. A Node or Rust crash names its + * cause near the top (`Error: Cannot find module '/x'`) and ends with a stack tail + * (`requireStack: []`, `}`, `Node.js v22`); the tail alone hides the cause. + */ +function stderrSummary(stderr: string): string { + const lines = stderr.split("\n").map((line) => line.trim()).filter(Boolean); + const firstError = lines.find((line) => /\b(error|panic(?:ked)?)\b/i.test(line)); + const tail = lines.slice(-3); + return [...new Set([...(firstError ? [firstError] : []), ...tail])].join(" | "); +} + async function runAppServerTurn(input: ProviderRunInput, signal: AbortSignal | undefined, launch: AppServerLaunch): Promise { if (!input.modelInput) return undefined; const invocation = launch; diff --git a/crewcoder-agent/src/sdk-runtime.ts b/crewcoder-agent/src/sdk-runtime.ts index c717e14..f4ad464 100644 --- a/crewcoder-agent/src/sdk-runtime.ts +++ b/crewcoder-agent/src/sdk-runtime.ts @@ -73,7 +73,12 @@ export async function runCrewCoderRuntimeTurn( const modelClient = options.modelClient ?? (options.heuristic ? new HeuristicModelClient() : new ProviderModelClient(providerId, cwd, model, undefined, config.thinkingEnabled ? options.effort : "none")); + // A caller's own client (or the heuristic one) has no provider-native tool executor unless the + // caller names its provider. The fallback providerId above (env/config default, e.g. codex-cli) + // would otherwise select native mode and drop CrewCoder's file tools (`read`, `write`, ...). + const ownClientWithoutProvider = Boolean(options.modelClient || options.heuristic) && !options.provider; const common = { + ...(ownClientWithoutProvider ? { toolMode: "crewcoder" as const } : {}), providerId, model, contextWindow: resolvedModel?.metadata?.contextWindow, diff --git a/crewcoder-agent/src/tests/agent-loop.test.ts b/crewcoder-agent/src/tests/agent-loop.test.ts index fc00f61..42b0f3e 100644 --- a/crewcoder-agent/src/tests/agent-loop.test.ts +++ b/crewcoder-agent/src/tests/agent-loop.test.ts @@ -164,6 +164,64 @@ describe("agent loop", () => { expect(events).toContain("usage_update"); expect(result.usage).toMatchObject({ inputTokens: 10, outputTokens: 4, totalTokens: 14, turns: 1 }); }); + it("times generation without the provider's own tools or questions for message_end throughput", async () => { + const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); + const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + const ends: AgentEvent[] = []; + const modelClient: ModelClient = { + async complete(_input, _signal, stream) { + // A native-loop provider (Claude SDK, Codex): its tool and an approval run inside the call. + await stream?.onProviderToolStart?.({ type: "toolCall", id: "native-1", name: "Bash", arguments: { command: "npm test" } }); + await sleep(300); + await stream?.onProviderToolEnd?.({ toolCallId: "native-1", toolName: "Bash", text: "ok", isError: false }); + await stream?.requestQuestion?.({ title: "Allow?", options: [{ label: "Yes", value: "yes" }] }); + await stream?.onUsage?.({ providerId: "test", model: "model", inputTokens: 10, outputTokens: 40, totalTokens: 50 }); + return assistantText("done"); + } + }; + await runAgentLoop({ prompt: "hello", requestedMode: "general", cwd }, { + maxIterations: 1, + providerId: "test", + model: "model", + modelClient, + requestQuestion: async () => { + await sleep(300); + return "yes"; + }, + emit: (event) => { + if (event.type === "message_end" && event.message.role === "assistant") ends.push(event); + } + }); + const end = ends.at(-1); + expect(end).toMatchObject({ type: "message_end", outputTokens: 40 }); + const durationMs = end?.type === "message_end" ? end.durationMs : undefined; + expect(durationMs).toBeTypeOf("number"); + // 600ms of tool and question time inside the call; generation itself is near zero. + expect(durationMs!).toBeLessThan(150); + }); + + it("omits throughput time when a provider tool never reports its end", async () => { + const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); + const ends: AgentEvent[] = []; + const modelClient: ModelClient = { + async complete(_input, _signal, stream) { + await stream?.onProviderToolStart?.({ type: "toolCall", id: "lost", name: "Bash", arguments: {} }); + await stream?.onUsage?.({ providerId: "test", model: "model", inputTokens: 10, outputTokens: 40, totalTokens: 50 }); + return assistantText("done"); + } + }; + await runAgentLoop({ prompt: "hello", requestedMode: "general", cwd }, { + maxIterations: 1, + providerId: "test", + model: "model", + modelClient, + emit: (event) => { + if (event.type === "message_end" && event.message.role === "assistant") ends.push(event); + } + }); + expect(ends.at(-1)).toMatchObject({ type: "message_end", outputTokens: 40, durationMs: undefined }); + }); + it("records every billed turn in the cost ledger and rolls the dollar total into the usage summary", async () => { const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); const current = readConfig(); @@ -1444,6 +1502,60 @@ describe("agent loop", () => { ])); }); + it("emits a file_diff for edit and write tools, before their tool_execution_end, and none for shell commands", async () => { + const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); + fs.writeFileSync(path.join(cwd, "notes.txt"), "keep\nold line\n", "utf8"); + let turn = 0; + const modelClient: ModelClient = { + async complete() { + turn += 1; + if (turn > 1) return assistantText("done"); + return { + role: "assistant", + content: [ + { type: "toolCall", id: "edit-1", name: "edit", arguments: { path: "notes.txt", find: "old line", replace: "new line" } }, + { type: "toolCall", id: "write-1", name: "write", arguments: { path: "created.txt", content: "hi\n" } }, + { type: "toolCall", id: "sh-1", name: "bash", arguments: { command: "printf 'x\\n' > shell.txt" } } + ], + stopReason: "tool_calls", + timestamp: Date.now() + }; + } + }; + const events: AgentEvent[] = []; + await runAgentLoop({ prompt: "change files", requestedMode: "general", cwd }, { maxIterations: 2, approvalMode: "full-access", modelClient, emit: (event) => { events.push(event); } }); + + const diffs = events.filter((event): event is Extract => event.type === "file_diff"); + expect(diffs.map((diff) => [diff.toolCallId, diff.files.map((file) => [file.path, file.status, file.additions, file.deletions])])).toEqual([ + ["edit-1", [["notes.txt", "modified", 1, 1]]], + ["write-1", [["created.txt", "added", 1, 0]]] + ]); + expect(diffs[0]?.files[0]?.patch).toContain("-old line\n+new line"); + expect(fs.existsSync(path.join(cwd, "shell.txt"))).toBe(true); + const order = events.map((event) => event.type === "file_diff" ? `diff:${event.toolCallId}` : event.type === "tool_execution_end" ? `end:${event.toolCallId}` : "").filter(Boolean); + expect(order).toEqual(["diff:edit-1", "end:edit-1", "diff:write-1", "end:write-1", "end:sh-1"]); + }); + + it("emits a file_diff for a provider-native file change that writes before it is announced, and none for native commands", async () => { + const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); + const modelClient: ModelClient = { + async complete(_input, _signal, callbacks) { + fs.writeFileSync(path.join(cwd, "native.txt"), "from codex\n", "utf8"); + await callbacks?.onProviderToolStart?.({ type: "toolCall", id: "codex-1", name: "Codex file change", arguments: { changes: [] } }); + await callbacks?.onProviderToolEnd?.({ toolCallId: "codex-1", toolName: "Codex file change", text: "[]", isError: false }); + await callbacks?.onProviderToolStart?.({ type: "toolCall", id: "codex-2", name: "Codex command", arguments: { command: "touch other.txt" } }); + fs.writeFileSync(path.join(cwd, "other.txt"), "from a command\n", "utf8"); + await callbacks?.onProviderToolEnd?.({ toolCallId: "codex-2", toolName: "Codex command", text: "", isError: false }); + return assistantText("done"); + } + }; + const events: AgentEvent[] = []; + await runAgentLoop({ prompt: "native write", requestedMode: "general", cwd }, { maxIterations: 1, modelClient, emit: (event) => { events.push(event); } }); + + const diffs = events.filter((event): event is Extract => event.type === "file_diff"); + expect(diffs).toEqual([expect.objectContaining({ toolCallId: "codex-1", toolName: "Codex file change", files: [expect.objectContaining({ path: "native.txt", status: "added" })] })]); + }); + it("emits file_changed with the actual tool name", async () => { const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-loop-")); const result = await runAgentLoop({ prompt: "create a CrewCode plugin named file-change-test", requestedMode: "plugin", cwd }, { maxIterations: 2, integrationProfile: "crewcode" }); diff --git a/crewcoder-agent/src/tests/checkpoint-settings.test.ts b/crewcoder-agent/src/tests/checkpoint-settings.test.ts index 3de275f..51cc082 100644 --- a/crewcoder-agent/src/tests/checkpoint-settings.test.ts +++ b/crewcoder-agent/src/tests/checkpoint-settings.test.ts @@ -16,12 +16,14 @@ afterEach(() => { }); describe("checkpoint settings", () => { - it("keeps automatic checkpoints enabled for existing configs", () => { + it("keeps automatic checkpoints off until the user turns them on", () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "crewcoder-checkpoint-config-")); process.env.CREWCODER_HOME = home; fs.mkdirSync(home, { recursive: true }); fs.writeFileSync(path.join(home, "config.json"), JSON.stringify({ defaultProvider: "codex" }), "utf8"); + expect(readConfig().checkpointsEnabled).toBe(false); + expect(setConfigValue("checkpointsEnabled", "true").checkpointsEnabled).toBe(true); expect(readConfig().checkpointsEnabled).toBe(true); expect(setConfigValue("checkpointsEnabled", "false").checkpointsEnabled).toBe(false); expect(readConfig().checkpointsEnabled).toBe(false); diff --git a/crewcoder-agent/src/tests/codex-approval-question.test.ts b/crewcoder-agent/src/tests/codex-approval-question.test.ts index 8d3a3fc..1a17f69 100644 --- a/crewcoder-agent/src/tests/codex-approval-question.test.ts +++ b/crewcoder-agent/src/tests/codex-approval-question.test.ts @@ -18,9 +18,13 @@ const provider: ProviderDefinition = { describe("native approval questions", () => { it.each(["accept", "acceptForSession", "decline", undefined])("keeps the reason as title, includes permission detail, and preserves decision %s", async (answer) => { const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "crew-question-")); - // Node runs this extensionless fixture on Windows as well as POSIX. - process.env.CREWCODER_CODEX_PATH = process.execPath; - fs.writeFileSync(path.join(cwd, "app-server"), ` + // An executable fake CLI, like codex-cli-provider.test.ts: the provider passes `-c` config + // overrides before `app-server` (features.default_mode_request_user_input when a host can + // answer questions), which `node