diff --git a/.gitignore b/.gitignore index 42b7936..b371f67 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ /code/ /code2/ /dist/ + +# planning artifacts +implementation_plan.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..6c1eaa0 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,136 @@ +# AGENTS.md + +## What this repo is + +Go CLI (`cmd/orchestra`) that orchestrates multiple AI coding agents (Claude Code, OpenCode, Mimo, etc.) through a single supervised interface. Agents run headless; human approves via diff review. + +## Build & verify (run these in order before pushing) + +```sh +go build ./... # compile +go vet ./... # static analysis +go test ./... # all tests +gofmt -w . # format (CI enforces this) +``` + +No shortcuts. CI runs exactly these four steps against Go 1.22. + +## Single-focus commands + +```sh +go test ./internal/scheduler/... # test one package +go vet ./internal/engine/... # vet one package +gofmt -l . # list unformatted files +``` + +## Build the binary + +```sh +go build -o bin/orchestra ./cmd/orchestra +./bin/orchestra --help +``` + +Binary is at `bin/orchestra` (gitignored). + +## Project structure + +- `cmd/orchestra/` — Cobra CLI (main.go + one file per subcommand: run, do, plan, dashboard, agents, etc.) +- `internal/engine/` — the core supervised loop (dispatch → validate → retry → review). Every front door (`run`, shell, `do`) drives this. +- `internal/agent/` — Agent interface + registry. This is the extension point for adding new agents. +- `internal/router/` — AI routing: classifies intent → picks agent. Uses a CLI agent now; API planned. +- `internal/planner/` — decomposes requests into ordered steps with `depends_on` for parallelism. +- `internal/scheduler/` — bounded-concurrency DAG runner for parallel worktrees. +- `internal/worktree/` — git worktree isolation (branch per task, merge + conflict detection). +- `internal/validate/` — build → lint → test pipeline, auto-detects toolchain, stops on first failure. +- `internal/config/` — YAML config + built-in agent defaults + toolchain auto-detection. +- `internal/memory/` — SQLite history at `~/.orchestra/orchestra.db` (never in the repo tree). +- `internal/tui/` — Bubble Tea dashboard (agents, history, benchmarks, chat tabs). +- `internal/ui/` — terminal styling (gradients, spinners, diffs). TTY-aware. +- `internal/shell/` — interactive chat REPL. +- `internal/review/` — diff display + accept/reject prompt. +- `internal/runner/` — generic process exec (stream/capture/timeout). The engine's engine. +- `internal/gitutil/` — git helpers (is-repo, is-clean, diff, restore, commit). +- `internal/fsdiff/` — git-free supervision for plain folders: directory snapshots, a Myers line-diff emitting git-style unified output, and snapshot restore. Used by the engine whenever `--dir` is not a git repository. +- `internal/llm/` — HTTP LLM providers (`Provider` interface; openai-compatible + anthropic). No new deps: stdlib net/http. +- `internal/patch/` — extracts unified diffs / whole-file blocks from model output and applies them (git apply --check dry-run first; traversal-guarded writes). + +## Key conventions + +- **CGO_ENABLED=0** — pure-Go builds (uses `modernc.org/sqlite`, not CGo). Do not introduce C dependencies. +- **Tests use fake agents** — small shell scripts, not real CLIs. Tests are headless, offline, deterministic. Follow this pattern. +- **Test files are colocated** (`*_test.go` next to source). Pure logic (scheduler, config detection, TUI update/view) is tested without a TTY. +- **CI checks**: build, vet, test, gofmt. All four must pass. Run them locally before pushing. +- **Releases**: push a `v*` tag → GoReleaser cross-compiles → GitHub Release. Version stamped via `-ldflags -X main.version=`. +- **Config file**: `orchestra.yaml` (optional, generated by `orchestra init`). Built-in defaults work without one. +- **`dir_flag` in agent config**: some CLIs (e.g. opencode) don't honor process cwd. Set `dir_flag: "--dir"` so parallel worktrees stay isolated. + +## Supervised loop (the core abstraction) + +Every front door (`run`, `do`, shell) drives the same pipeline in `internal/engine/`: + +``` +dispatch → validate → retry (self-correct) → review (accept/reject) +``` + +Git is optional. Inside a repository, diffs and rejects use git and the tree must start clean +(reject does `git restore`). Outside one — like opencode or claude — the engine snapshots the +directory first (`internal/fsdiff`), diffs against that snapshot, and restores from it on reject; +accept just keeps the files. `--parallel` and `benchmark` still require a repository (worktrees). + +If you change validation or retry logic, you change all three entry points. The engine is the product — not the CLI or the TUI. + +## Agent interface contract + +`internal/agent/agent.go` defines the extension point. Implement the `Agent` interface: + +```go +type Agent interface { + Name() string + Run(ctx context.Context, task Task) (Result, error) + Health() error + Capabilities() []Capability +} +``` + +Optional interfaces that agents may also implement: `Querier` (text output for planning), `Prober` (live health check), `QuietRunner` / `QuietQuerier` (no-stream variants for the TUI). Callers type-assert and fall back gracefully. + +Register new agents in `internal/agent/registry.go` — the `Registry.Add()` method preserves insertion order for stable listings. + +There are two concrete implementations: `CLIAgent` (spawns a CLI in headless auto-approve mode) and `APIAgent` (`internal/agent/apiagent.go`, calls an HTTP LLM via `internal/llm`, applies its patch via `internal/patch`). API agents are configured with `type: api` in orchestra.yaml — see docs/EXTENDING.md. A future agent kind can implement the interface without touching the scheduler, router, or shell. + +## Testing with fake agents + +Tests never call real agent CLIs. Instead, use small shell scripts or builtins as stubs: + +```go +// "true" is a valid fake agent — exits 0, does nothing. +reg.Add(agent.New("alpha", "true", nil, "", []agent.Capability{agent.CapImplement})) +``` + +For scheduler tests, pure logic (DAG validation, concurrency caps) is tested without any agent at all. See `internal/scheduler/scheduler_test.go` for examples. + +For config detection tests, temp dirs with marker files (`go.mod`, `package.json`) are used — no real toolchain needed beyond what's on PATH (tests skip gracefully via `t.Skip`). + +## TUI stack + +The dashboard (`internal/tui/`) uses Charm's ecosystem: +- **Bubble Tea** — Elm-architecture TUI framework +- **Lipgloss** — terminal styling +- **Glamour** — markdown rendering +- **Chroma** — syntax-highlighted diffs + +These are not interchangeable with other TUI frameworks. Tests are headless (`tui_test.go` renders views without a TTY by injecting `WindowSizeMsg`). + +## Router is CLI-based + +The AI router (`internal/router/`) classifies intent by dispatching to an agent in query mode (e.g. `claude -p`), not by calling an LLM API itself. It inherits agent CLI quirks: needs `--dangerously-skip-permissions`, uses the same `runner` package, and the classification latency is whatever the agent takes. Since `APIAgent` also implements `Querier`, an api-type agent can serve as the router's classifier today; a dedicated direct-API classifier inside `internal/router` remains planned but unimplemented. + +## Gitignored directories + +`code/` and `code2/` are local dogfooding test projects. They are gitignored — do not create or modify files there as part of your changes. + +## Before pushing + +Always run: `go build ./... && go vet ./... && go test ./... && gofmt -w .` + +Branch off `main`. Keep changes focused; add/adjust tests. Describe the change and how you verified it in the PR. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..6437ed7 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,18 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +The canonical agent instructions live in AGENTS.md (shared with other coding agents). It covers the +build/verify commands, package layout, the supervised loop, the Agent interface, and testing conventions: + +@AGENTS.md + +## Additions for Claude Code + +- Module path is `github.com/crossben/orchestra-code`; CI pins Go 1.22. If `go` tries to download a + newer toolchain and fails offline, run `go env -w GOTOOLCHAIN=local` once. +- Run a single test: `go test ./internal/engine/ -run TestName -v`. +- CI also runs `goreleaser check` against `.goreleaser.yaml`, so keep that file valid if you touch it. + See docs/RELEASING.md for the release flow and docs/EXTENDING.md for adding agents (CLI or `type: api`). +- Orchestra is supervised-first: nothing an agent produces is kept without an explicit accept at diff review. + If a change alters agent behavior or the supervised loop, call that out explicitly in the PR. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a27d2c2..8c3e1ff 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,7 @@ gofmt -w . # format (CI enforces this) ``` cmd/orchestra CLI (cobra): run / do / plan / benchmark / dashboard / agents / init / shell -internal/agent Agent interface + CLIAgent + registry ← the extension point +internal/agent Agent interface + CLIAgent/APIAgent + registry ← the extension point internal/engine supervised pipeline: dispatch → validate → retry → review internal/router AI routing (classify → decide) internal/planner request → ordered steps (+ dependencies) diff --git a/README.md b/README.md index abfcb05..6a2cc64 100644 --- a/README.md +++ b/README.md @@ -260,8 +260,11 @@ A config file overrides defaults and adds agents; matching names replace the bui ``` cmd/orchestra Cobra CLI: run / plan / do / history / agents / init / shell (default) -internal/agent Agent interface + CLIAgent + registry + Querier +internal/agent Agent interface + CLIAgent/APIAgent + registry + Querier internal/config YAML config + built-in agent defaults + validator auto-detection +internal/fsdiff git-free supervision: dir snapshots + unified diff + restore +internal/llm HTTP LLM providers (openai-compatible, anthropic) behind one interface +internal/patch extract changes from model replies and apply them safely internal/ui terminal styling: gradient banner, spinners, colored diffs (TTY-aware) internal/tui Bubble Tea dashboard: agents / history / benchmarks (read-only monitor) internal/router AI routing: Classifier (CLI now, API later) → Decision, 3-tier fallback diff --git a/cmd/orchestra/agents.go b/cmd/orchestra/agents.go index 85dde05..7fce73b 100644 --- a/cmd/orchestra/agents.go +++ b/cmd/orchestra/agents.go @@ -45,7 +45,7 @@ func newAgentsCmd() *cobra.Command { if a.Name() == cfg.DefaultAgent { def = " (default)" } - fmt.Printf("%-12s %-16s %s%s\n", a.Name(), status, capList(a), def) + fmt.Printf("%-12s %-16s %s%s%s\n", a.Name(), status, capList(a), apiTag(a), def) } fmt.Println(ui.Dim("\ntip: `orchestra agents --probe` checks agents can actually run, not just that they're installed")) return nil @@ -105,3 +105,12 @@ func capList(a agent.Agent) string { } return strings.Join(caps, ",") } + +// apiTag labels HTTP-backed agents so `orchestra agents` shows what they are +// at a glance (e.g. " (api: openai/gpt-4o)"). Empty for CLI agents. +func apiTag(a agent.Agent) string { + if api, ok := a.(*agent.APIAgent); ok { + return ui.Dim(fmt.Sprintf(" (api: %s/%s)", api.ProviderName(), api.Model())) + } + return "" +} diff --git a/cmd/orchestra/benchmark.go b/cmd/orchestra/benchmark.go index 35762d6..3f11e53 100644 --- a/cmd/orchestra/benchmark.go +++ b/cmd/orchestra/benchmark.go @@ -71,7 +71,7 @@ func newBenchmarkCmd() *cobra.Command { return err } if !gitutil.IsRepo(flagDir) { - return errNotRepo(flagDir) + return fmt.Errorf("benchmark needs a git repository — every agent runs in an isolated worktree so their changes never collide") } if clean, err := gitutil.IsClean(flagDir); err != nil { return err diff --git a/cmd/orchestra/do.go b/cmd/orchestra/do.go index b5441e0..693e114 100644 --- a/cmd/orchestra/do.go +++ b/cmd/orchestra/do.go @@ -56,14 +56,18 @@ func newDoCmd() *cobra.Command { agentName = cfg.DefaultAgent } - // Git pre-flight — the workflow commits/merges; start clean. - if !gitutil.IsRepo(flagDir) { - return errNotRepo(flagDir) + // Git pre-flight: sequential mode works anywhere (snapshot-tracked), + // but --parallel isolates steps in git worktrees, so it needs a repo. + inRepo := gitutil.IsRepo(flagDir) + if parallel && !inRepo { + return fmt.Errorf("--parallel needs a git repository (each step runs in an isolated worktree); run inside a repo, or drop --parallel for step-by-step supervision") } - if clean, err := gitutil.IsClean(flagDir); err != nil { - return err - } else if !clean { - return errDirty() + if inRepo { + if clean, err := gitutil.IsClean(flagDir); err != nil { + return err + } else if !clean { + return errDirty() + } } reg := cfg.BuildRegistry() diff --git a/cmd/orchestra/errors.go b/cmd/orchestra/errors.go index 2d377ad..38d13ae 100644 --- a/cmd/orchestra/errors.go +++ b/cmd/orchestra/errors.go @@ -2,10 +2,6 @@ package main import "fmt" -func errNotRepo(dir string) error { - return fmt.Errorf("%s is not a git repository (run `git init` first)", dir) -} - func errDirty() error { return fmt.Errorf("working tree has uncommitted changes; the supervised loop " + "reverts on reject and would discard them — commit/stash first, or pass --force") diff --git a/cmd/orchestra/init.go b/cmd/orchestra/init.go index a7af844..e07c0cb 100644 --- a/cmd/orchestra/init.go +++ b/cmd/orchestra/init.go @@ -59,6 +59,16 @@ agents: bin: mimo args: ["run", "--dangerously-skip-permissions"] capabilities: [implement, review] + + # API-backed agent (no CLI needed): Orchestra sends the model a snapshot of + # the repo and applies the returned patch itself. Uncomment and set the key: + # + # - name: gpt + # type: api + # provider: openai # openai | anthropic (OpenRouter/Groq/Ollama work via openai + api_base) + # model: gpt-4o + # api_key_env: OPENAI_API_KEY + # capabilities: [plan, implement, review] ` func newInitCmd() *cobra.Command { diff --git a/cmd/orchestra/root.go b/cmd/orchestra/root.go index 8d2b824..9cfcace 100644 --- a/cmd/orchestra/root.go +++ b/cmd/orchestra/root.go @@ -7,13 +7,14 @@ import ( "github.com/crossben/orchestra-code/internal/gitutil" "github.com/crossben/orchestra-code/internal/router" "github.com/crossben/orchestra-code/internal/shell" + "github.com/crossben/orchestra-code/internal/ui" "github.com/spf13/cobra" ) // version is the build version. It defaults to the last released tag for // `go install`, and is overridden at release time via // -ldflags "-X main.version=" (see .goreleaser.yaml). -var version = "0.7.3" +var version = "0.8.0" // persistent flags shared across subcommands. var ( @@ -53,15 +54,21 @@ func runShell(cmd *cobra.Command) error { if err != nil { return err } - if !gitutil.IsRepo(flagDir) { - return errNotRepo(flagDir) - } - clean, err := gitutil.IsClean(flagDir) - if err != nil { - return err - } - if !clean { - return errDirty() + + // Git is optional: inside a repository the supervised loop uses git for + // diffs/rejects (and wants a clean start); anywhere else it snapshots the + // directory instead, so plain folders work like opencode or claude. + inRepo := gitutil.IsRepo(flagDir) + if inRepo { + clean, err := gitutil.IsClean(flagDir) + if err != nil { + return err + } + if !clean { + return errDirty() + } + } else { + fmt.Printf("%s\n", ui.Dim(fmt.Sprintf("▸ %s is not a git repository — Orchestra will track changes by snapshot (accept keeps files, reject restores)", flagDir))) } mem, err := openMemory() diff --git a/cmd/orchestra/run.go b/cmd/orchestra/run.go index 2dad912..382b0be 100644 --- a/cmd/orchestra/run.go +++ b/cmd/orchestra/run.go @@ -60,16 +60,17 @@ func newRunCmd() *cobra.Command { cfg.Principles = principles } - // Git pre-flight: the supervised loop reverts on reject. - if !gitutil.IsRepo(flagDir) { - return errNotRepo(flagDir) - } - clean, err := gitutil.IsClean(flagDir) - if err != nil { - return err - } - if !clean && !force { - return errDirty() + // Git pre-flight: inside a repository the supervised loop reverts + // on reject, so it wants a clean start (unless --force). Plain + // directories are fine — changes are snapshot-tracked instead. + if gitutil.IsRepo(flagDir) { + clean, err := gitutil.IsClean(flagDir) + if err != nil { + return err + } + if !clean && !force { + return errDirty() + } } reg := cfg.BuildRegistry() diff --git a/docs/EXTENDING.md b/docs/EXTENDING.md index f04e0bc..4507d05 100644 --- a/docs/EXTENDING.md +++ b/docs/EXTENDING.md @@ -1,6 +1,6 @@ # Extending Orchestra — adding agents -Orchestra treats every agent as an interchangeable component behind one interface. There are two +Orchestra treats every agent as an interchangeable component behind one interface. There are three ways to add one, from easiest to deepest. --- @@ -60,7 +60,41 @@ You'll see the full supervised loop against your custom agent. --- -## 2. Implement the `Agent` interface (for a built-in / non-CLI agent) +## 2. Add an API-backed agent via config (no code) + +If you'd rather call a hosted LLM directly than spawn a CLI, register an agent with +`type: api`. Orchestra sends the model a bounded snapshot of your repository plus the task, +the model replies with changes (a unified diff or whole-file blocks), Orchestra applies them +to the working tree, and everything flows through the same supervised loop: +validate → retry → diff review. + +```yaml +agents: + - name: gpt + type: api + provider: openai # openai | anthropic + model: gpt-4o # required + # api_base: https://api.openai.com/v1 # optional override (OpenRouter, Groq, + # # Ollama, vLLM… all speak the openai format) + api_key_env: OPENAI_API_KEY # env var holding the key (provider default) + context_budget: 98304 # repo snapshot byte cap (default 96 KiB) + capabilities: [plan, implement, review] +``` + +Notes: +- The API key is read from the environment at run time — never put keys in the YAML. +- `orchestra agents` shows these as `(api: /)`, and `--probe` runs a real + tiny completion so auth/billing problems surface with actionable detail. +- Because the model cannot explore the repo itself, the snapshot it receives matters: keep + the tree lean, and raise `context_budget` if tasks touch many files. +- Self-correction works like any agent: on validation failure the failure text is fed back + and the model re-edits its own prior changes in place. +- Works everywhere CLI agents do: `run`, shell, dashboard chat, planning, routing, and even + `do --parallel` worktrees (patches apply inside each isolated worktree). + +--- + +## 3. Implement the `Agent` interface (for a built-in / non-CLI agent) For agents that aren't a subprocess (e.g. a direct API/gRPC client), implement the interface in [`internal/agent/agent.go`](../internal/agent/agent.go): diff --git a/go.mod b/go.mod index c6fa7c3..05ff14e 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/ncruces/go-strftime v0.1.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/sergi/go-diff v1.4.0 // indirect github.com/spf13/pflag v1.0.9 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/yuin/goldmark v1.7.4 // indirect diff --git a/go.sum b/go.sum index 39b5166..61b8f5f 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b/go.mod github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= @@ -47,6 +49,9 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -68,6 +73,7 @@ github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -75,10 +81,14 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= +github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= @@ -107,6 +117,10 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ= diff --git a/internal/agent/apiagent.go b/internal/agent/apiagent.go new file mode 100644 index 0000000..8161be8 --- /dev/null +++ b/internal/agent/apiagent.go @@ -0,0 +1,184 @@ +package agent + +import ( + "context" + "errors" + "fmt" + "os" + "strings" + "time" + + "github.com/crossben/orchestra-code/internal/llm" + "github.com/crossben/orchestra-code/internal/patch" +) + +// APIAgent drives a hosted LLM over HTTP instead of spawning a CLI subprocess. +// Because a raw model cannot explore the repository the way a coding CLI can, +// each Run ships it a bounded snapshot of the repo and requires the reply to be +// machine-applicable changes (a unified diff or per-file blocks), which +// Orchestra then applies to the task's directory. From there the supervised +// engine treats it exactly like any other agent: validate, retry, review. +// +// Quiet variants equal their loud counterparts — an HTTP call never writes to +// the terminal — so the TUI can use this agent unmodified. +type APIAgent struct { + name string + model string + keyEnv string // env var holding the API key + prov llm.Provider + caps []Capability + budget int +} + +// Compile-time proof that APIAgent satisfies the whole contract surface, so +// routing, planning, probing, and the TUI all light up via type assertion. +var ( + _ Agent = (*APIAgent)(nil) + _ Querier = (*APIAgent)(nil) + _ Prober = (*APIAgent)(nil) + _ QuietRunner = (*APIAgent)(nil) + _ QuietQuerier = (*APIAgent)(nil) +) + +// NewAPI builds an APIAgent. provider is "openai" or "anthropic"; apiBase may +// be empty (provider default); keyEnv names the environment variable holding +// the API key (provider default when empty); budget caps the repo snapshot in +// bytes (<=0 → DefaultContextBudget). +func NewAPI(name, provider, model, apiBase, keyEnv string, caps []Capability, budget int) (*APIAgent, error) { + if strings.TrimSpace(name) == "" { + return nil, fmt.Errorf("api agent name is required") + } + if strings.TrimSpace(provider) == "" { + provider = "openai" // sensible default; explicit in config preferred + } + if keyEnv == "" { + keyEnv = llm.DefaultKeyEnv(provider) + } + prov, err := llm.New(provider, apiBase, "", model, nil) + if err != nil { + return nil, fmt.Errorf("api agent %s: %w", name, err) + } + copied := make([]Capability, len(caps)) + copy(copied, caps) + if budget <= 0 { + budget = DefaultContextBudget + } + return &APIAgent{name: name, model: model, keyEnv: keyEnv, prov: prov, caps: copied, budget: budget}, nil +} + +// Name implements Agent. +func (a *APIAgent) Name() string { return a.name } + +// Capabilities implements Agent. +func (a *APIAgent) Capabilities() []Capability { return a.caps } + +// Model returns the model id this agent talks to. +func (a *APIAgent) Model() string { return a.model } + +// ProviderName returns the llm provider backing this agent. +func (a *APIAgent) ProviderName() string { return a.prov.Name() } + +// Health reports whether the agent is usable without network I/O: the API key +// env var must be set (mirrors CLIAgent's cheap binary-on-PATH check). +func (a *APIAgent) Health() error { + if v := os.Getenv(a.keyEnv); strings.TrimSpace(v) == "" { + return fmt.Errorf("environment variable %s is not set (required by api agent %q)", a.keyEnv, a.name) + } + return nil +} + +// outputContract is the strict reply format demanded of the model. +const outputContract = `You are a senior coding agent editing files inside the repository provided below. ` + + "You cannot ask questions: make reasonable assumptions and act.\n\n" + + "REPLY FORMAT (strict):\n" + + "- To change files, reply with ONLY your changes in one of these two forms:\n" + + " 1. A single fenced unified diff:\n" + + " ```diff\n" + + " \n" + + " ```\n" + + " 2. One fenced block per file, where the FIRST line of the block is the\n" + + " repo-relative file path and the rest is the ENTIRE new file content:\n" + + " ```\n" + + " path/to/file.go\n" + + " \n" + + " ```\n" + + " To delete a file, make the body exactly on its own line.\n" + + "- No prose before or after. No explanations. No other fences.\n" + + "- If nothing needs changing, reply with one short plain-text sentence saying why.\n" + +// Run implements Agent: snapshot → completion → extract → apply. +func (a *APIAgent) Run(ctx context.Context, task Task) (Result, error) { + start := time.Now() + snap, err := buildSnapshot(task.Dir, a.budget) + if err != nil { + return Result{}, fmt.Errorf("build repo snapshot: %w", err) + } + text, err := a.complete(ctx, outputContract, snap+"\n\n=== TASK ===\n"+task.Prompt, task.Timeout) + if err != nil { + return Result{}, err + } + p, _ := patch.Extract(text) + if !p.Empty() { + if err := patch.Apply(ctx, task.Dir, p); err != nil { + return Result{Duration: time.Since(start)}, fmt.Errorf("apply model changes: %w", err) + } + } + // Output carries the model's raw text so the engine's no-change/question + // detection (package engine) sees exactly what the model said. + return Result{ExitCode: 0, Duration: time.Since(start), Output: text}, nil +} + +// RunQuiet implements QuietRunner. API calls never stream, so this is Run. +func (a *APIAgent) RunQuiet(ctx context.Context, task Task) (Result, error) { + return a.Run(ctx, task) +} + +// Query implements Querier: a plain completion with no patch contract and no +// repo snapshot — used by the router (classification/answers) and planner. +func (a *APIAgent) Query(ctx context.Context, task Task) (string, error) { + return a.complete(ctx, "", task.Prompt, task.Timeout) +} + +// QueryQuiet implements QuietQuerier; identical to Query (never streams). +func (a *APIAgent) QueryQuiet(ctx context.Context, task Task) (string, error) { + return a.Query(ctx, task) +} + +const probeContract = "Reply with exactly the word OK and nothing else. Do not create or modify any files." + +// Probe implements Prober: a trivial live completion that catches auth, +// billing, and connectivity problems a Health check cannot see. +func (a *APIAgent) Probe(ctx context.Context, timeout time.Duration) ProbeResult { + text, err := a.complete(ctx, probeContract, probeContract, timeout) + switch { + case err != nil: + detail := firstMeaningfulLine(err.Error()) + var le *llm.Error + if errors.As(err, &le) && le.UserDetail() != "" { + detail = le.UserDetail() + } + return ProbeResult{OK: false, Detail: detail} + case strings.TrimSpace(text) == "": + return ProbeResult{OK: false, Detail: "empty response"} + default: + return ProbeResult{OK: true, Detail: "responded"} + } +} + +// complete issues one provider call. system may be empty (plain query mode). +func (a *APIAgent) complete(ctx context.Context, system, user string, timeout time.Duration) (string, error) { + if timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, timeout) + defer cancel() + } + resp, err := a.prov.Complete(ctx, llm.Request{ + System: system, + Messages: []llm.Message{{Role: "user", Content: user}}, + Temperature: 0, + }) + if err != nil { + return "", err + } + return resp.Text, nil +} diff --git a/internal/agent/apiagent_test.go b/internal/agent/apiagent_test.go new file mode 100644 index 0000000..ac1e668 --- /dev/null +++ b/internal/agent/apiagent_test.go @@ -0,0 +1,243 @@ +package agent + +import ( + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/crossben/orchestra-code/internal/llm" +) + +// fakeProvider is an in-memory llm.Provider: no network, deterministic replies. +type fakeProvider struct { + name string + text string + err error + last llm.Request + calls int +} + +func (f *fakeProvider) Name() string { + if f.name == "" { + return "fake" + } + return f.name +} + +func (f *fakeProvider) Complete(ctx context.Context, req llm.Request) (llm.Response, error) { + f.calls++ + f.last = req + if f.err != nil { + return llm.Response{}, f.err + } + if err := ctx.Err(); err != nil { + return llm.Response{}, err + } + return llm.Response{Text: f.text}, nil +} + +// apiRepo builds a clean git repo for Run tests (skips without git). +func apiRepo(t *testing.T) string { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not on PATH") + } + dir := t.TempDir() + git := func(args ...string) { + cmd := exec.Command("git", args...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) + } + } + git("init", "-q") + git("config", "user.email", "t@t") + git("config", "user.name", "t") + if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("# repo\n"), 0o644); err != nil { + t.Fatal(err) + } + git("add", "-A") + git("commit", "-qm", "init") // committed baseline so status --porcelain is empty + return dir +} + +const diffReply = "```diff\n" + + "diff --git a/README.md b/README.md\n" + + "--- a/README.md\n" + + "+++ b/README.md\n" + + "@@ -1 +1,2 @@\n" + + " # repo\n" + + "+touched by model\n" + + "```" + +func newTestAPIAgent(t *testing.T, p llm.Provider) *APIAgent { + t.Helper() + a, err := NewAPI("fakeapi", "", "test-model", "http://unused", "FAKE_KEY_ENV", []Capability{CapImplement}, 4096) + if err != nil { + t.Fatal(err) + } + a.prov = p + t.Setenv("FAKE_KEY_ENV", "test-key") + return a +} + +func TestAPIAgentRunAppliesDiff(t *testing.T) { + dir := apiRepo(t) + fp := &fakeProvider{text: diffReply} + a := newTestAPIAgent(t, fp) + + res, err := a.Run(context.Background(), Task{Prompt: "add a line", Dir: dir}) + if err != nil { + t.Fatal(err) + } + if res.ExitCode != 0 { + t.Fatalf("exit code: %d", res.ExitCode) + } + got, rerr := os.ReadFile(filepath.Join(dir, "README.md")) + if rerr != nil { + t.Fatal(rerr) + } + if !strings.Contains(string(got), "touched by model") { + t.Fatalf("patch not applied:\n%s", got) + } + if !strings.Contains(res.Output, "diff --git") { + t.Fatal("Output should carry the raw model text") + } + // The user message must contain the snapshot and the task. + if !strings.Contains(fp.last.Messages[0].Content, "=== TASK ===") || + !strings.Contains(fp.last.Messages[0].Content, "# repo") { + t.Fatalf("user message missing snapshot/task:\n%.200s", fp.last.Messages[0].Content) + } + // The system message must carry the output contract. + if !strings.Contains(fp.last.System, "REPLY FORMAT") { + t.Fatalf("system contract missing:\n%s", fp.last.System) + } +} + +func TestAPIAgentRunProseReplyChangesNothing(t *testing.T) { + dir := apiRepo(t) + fp := &fakeProvider{text: "I could not find anything to change; which file do you mean?"} + a := newTestAPIAgent(t, fp) + + res, err := a.Run(context.Background(), Task{Prompt: "do it", Dir: dir}) + if err != nil { + t.Fatal(err) + } + out, _ := exec.Command("git", "-C", dir, "status", "--porcelain").CombinedOutput() + if strings.TrimSpace(string(out)) != "" { + t.Fatalf("tree changed on prose reply:\n%s", out) + } + if !strings.Contains(res.Output, "which file do you mean") { + t.Fatal("prose should surface via Output for question detection") + } +} + +func TestAPIAgentRunProviderErrorPropagates(t *testing.T) { + dir := apiRepo(t) + fp := &fakeProvider{err: &llm.Error{Provider: "openai", Kind: llm.ErrAuth, Status: 401}} + a := newTestAPIAgent(t, fp) + + _, err := a.Run(context.Background(), Task{Prompt: "x", Dir: dir}) + var le *llm.Error + if !errors.As(err, &le) || le.Kind != llm.ErrAuth { + t.Fatalf("expected typed auth error, got %v", err) + } +} + +func TestAPIAgentQueryIsPlainCompletion(t *testing.T) { + fp := &fakeProvider{text: "42"} + a := newTestAPIAgent(t, fp) + + got, err := a.Query(context.Background(), Task{Prompt: "the answer?"}) + if err != nil { + t.Fatal(err) + } + if got != "42" { + t.Fatalf("query: %q", got) + } + // Query mode must NOT inject the patch contract or a snapshot. + if strings.Contains(fp.last.System, "REPLY FORMAT") { + t.Fatalf("query system should be empty, got: %s", fp.last.System) + } + if strings.Contains(fp.last.Messages[0].Content, "=== TASK ===") { + t.Fatal("query must not wrap the prompt in the task/snapshot envelope") + } + quiet, err := a.QueryQuiet(context.Background(), Task{Prompt: "q"}) + if err != nil || quiet != "42" { + t.Fatalf("quiet query: %q %v", quiet, err) + } +} + +func TestAPIAgentRunQuietMatchesRun(t *testing.T) { + dir := apiRepo(t) + fp := &fakeProvider{text: diffReply} + a := newTestAPIAgent(t, fp) + + res, err := a.RunQuiet(context.Background(), Task{Prompt: "add a line", Dir: dir}) + if err != nil { + t.Fatal(err) + } + if res.ExitCode != 0 || !strings.Contains(res.Output, "diff --git") { + t.Fatalf("unexpected quiet result: %+v", res) + } +} + +func TestAPIAgentHealthRequiresEnvVar(t *testing.T) { + t.Setenv("FAKE_KEY_ENV", "") // present but empty → still unhealthy + a, err := NewAPI("x", "openai", "m", "", "FAKE_KEY_ENV", nil, 0) + if err != nil { + t.Fatal(err) + } + if err := a.Health(); err == nil { + t.Fatal("expected Health to fail with unset key") + } + t.Setenv("FAKE_KEY_ENV", "k") + if err := a.Health(); err != nil { + t.Fatalf("Health with key set: %v", err) + } +} + +func TestAPIAgentProbeMapsErrors(t *testing.T) { + ok := newTestAPIAgent(t, &fakeProvider{text: "OK"}) + if pr := ok.Probe(context.Background(), time.Second); !pr.OK || pr.Detail != "responded" { + t.Fatalf("probe ok: %+v", pr) + } + + bad := newTestAPIAgent(t, &fakeProvider{err: &llm.Error{ + Provider: "openai", Kind: llm.ErrBilling, Status: 402, + }}) + pr := bad.Probe(context.Background(), time.Second) + if pr.OK { + t.Fatal("expected failing probe") + } + if pr.Detail == "" || pr.Detail == "responded" { + t.Fatalf("expected actionable detail, got %q", pr.Detail) + } +} + +func TestNewAPIDefaultsKeyEnvAndBudget(t *testing.T) { + a, err := NewAPI("gpt", "openai", "gpt-4o", "", "", []Capability{CapPlan}, 0) + if err != nil { + t.Fatal(err) + } + if a.keyEnv != "OPENAI_API_KEY" { + t.Fatalf("keyEnv: %s", a.keyEnv) + } + if a.budget != DefaultContextBudget { + t.Fatalf("budget: %d", a.budget) + } + if a.ProviderName() != "openai" || a.Model() != "gpt-4o" { + t.Fatalf("accessors: %s %s", a.ProviderName(), a.Model()) + } + if _, err := NewAPI("", "openai", "m", "", "", nil, 0); err == nil { + t.Fatal("empty name must be rejected") + } + if _, err := NewAPI("x", "nope", "m", "", "", nil, 0); err == nil { + t.Fatal("unknown provider must be rejected") + } +} diff --git a/internal/agent/snapshot.go b/internal/agent/snapshot.go new file mode 100644 index 0000000..a7004eb --- /dev/null +++ b/internal/agent/snapshot.go @@ -0,0 +1,235 @@ +package agent + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "path/filepath" + "sort" + "strings" + + "github.com/crossben/orchestra-code/internal/fsdiff" +) + +// DefaultContextBudget is the default byte cap on the repository snapshot an +// APIAgent sends with every task. CLI agents gather their own context; an API +// agent cannot, so Orchestra ships one to it. +const DefaultContextBudget = 96 << 10 // 96 KiB + +// snapshotSniff is how many leading bytes are inspected to detect binaries. +const snapshotSniff = 800 + +// snapshotSkipNames are lockfiles and generated artifacts that burn budget +// without helping a model understand the code. +var snapshotSkipNames = map[string]bool{ + "go.sum": true, + "package-lock.json": true, + "yarn.lock": true, + "pnpm-lock.yaml": true, + "Cargo.lock": true, + "poetry.lock": true, + "Gemfile.lock": true, +} + +type snapFile struct { + rel string // slash-separated, repo-relative + abs string +} + +// buildSnapshot renders a bounded textual view of the repository at dir: an +// annotated file list plus the contents of text files up to budget bytes. +// Binary files and lockfiles are listed but never inlined; anything that does +// not fit is explicitly marked so the model knows what it did not see. +func buildSnapshot(dir string, budget int) (string, error) { + if budget <= 0 { + budget = DefaultContextBudget + } + files, err := snapList(dir) + if err != nil { + return "", err + } + + var contents strings.Builder + type note struct{ rel, tag string } + var notes []note + remaining := budget + shown := 0 + for _, f := range files { + switch { + case snapshotSkipNames[f.rel]: + notes = append(notes, note{f.rel, "skipped (lockfile)"}) + continue + case snapBinary(f.abs): + notes = append(notes, note{f.rel, "skipped (binary)"}) + continue + } + data, err := os.ReadFile(f.abs) + if err != nil { + notes = append(notes, note{f.rel, "unreadable"}) + continue + } + if len(data) == 0 { + notes = append(notes, note{f.rel, "empty"}) + continue + } + if remaining <= 0 { + notes = append(notes, note{f.rel, "not shown (budget exhausted)"}) + continue + } + fmt.Fprintf(&contents, "----- %s -----\n", f.rel) + if len(data) > remaining { + fmt.Fprintf(&contents, "%s\n[truncated at %d of %d bytes]\n\n", + safeText(data[:remaining]), remaining, len(data)) + remaining = 0 + } else { + contents.Write(data) + if data[len(data)-1] != '\n' { + contents.WriteByte('\n') + } + contents.WriteByte('\n') + remaining -= len(data) + } + shown++ + } + + var b strings.Builder + fmt.Fprintf(&b, "Repository snapshot: %d files total, %d shown below, up to %d bytes of contents:\n\n", + len(files), shown, budget) + b.WriteString("=== FILE LIST ===\n") + for _, f := range files { + b.WriteString(f.rel) + b.WriteString("\n") + } + if len(notes) > 0 { + b.WriteString("\n=== NOTES ===\n") + for _, n := range notes { + fmt.Fprintf(&b, "%s: %s\n", n.rel, n.tag) + } + } + b.WriteString("\n=== CONTENTS ===\n") + b.WriteString(contents.String()) + return b.String(), nil +} + +// snapList enumerates candidate files. Inside a git repository it uses +// git (tracked + untracked-not-ignored, so .gitignore keeps vendor dirs and +// secrets out automatically); anywhere else it falls back to walking the +// directory with Orchestra's standard exclusion rules. +func snapList(dir string) ([]snapFile, error) { + if files, err := gitLsFiles(dir); err == nil { + return files, nil + } + return walkFiles(dir) +} + +// gitLsFiles lists candidates via git; it fails outside a repository or when +// git is unavailable. +func gitLsFiles(dir string) ([]snapFile, error) { + if _, err := exec.LookPath("git"); err != nil { + return nil, fmt.Errorf("git not available") + } + cmd := exec.Command("git", "-C", dir, "ls-files", "-co", "--exclude-standard") + var out bytes.Buffer + cmd.Stdout = &out + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("snapshot: git ls-files: %w", err) + } + return parseFileLines(out.String(), dir), nil +} + +// walkFiles is the git-free fallback: a plain directory walk that skips VCS +// internals, dependency trees, caches, and binaries via fsdiff's rules. +func walkFiles(dir string) ([]snapFile, error) { + root, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + var files []snapFile + err = filepath.WalkDir(root, func(p string, d os.DirEntry, werr error) error { + if werr != nil { + if d != nil && d.IsDir() { + return filepath.SkipDir + } + return nil + } + rel, rerr := filepath.Rel(root, p) + if rerr != nil { + return nil + } + rel = filepath.ToSlash(rel) + if rel == "." { + return nil + } + if d.IsDir() { + if fsdiff.Excluded(rel, true) { + return filepath.SkipDir + } + return nil + } + if !d.Type().IsRegular() || fsdiff.Excluded(rel, false) { + return nil + } + if info, ierr := d.Info(); ierr == nil && info.Size() > maxWalkFileBytes { + return nil // too big to be useful as model context + } + files = append(files, snapFile{rel: rel, abs: p}) + return nil + }) + if err != nil { + return nil, fmt.Errorf("snapshot: walk %s: %w", root, err) + } + sort.Slice(files, func(i, j int) bool { return files[i].rel < files[j].rel }) + return files, nil +} + +// maxWalkFileBytes caps individual file reads during the fallback walk. +const maxWalkFileBytes = 8 << 20 // 8 MiB + +// parseFileLines converts git ls-files output into sorted snapFile entries. +func parseFileLines(out string, dir string) []snapFile { + lines := strings.Split(out, "\n") + sort.Strings(lines) + files := make([]snapFile, 0, len(lines)) + for _, l := range lines { + l = strings.TrimSpace(l) + if l == "" { + continue + } + files = append(files, snapFile{ + rel: filepath.ToSlash(l), + abs: filepath.Join(dir, filepath.FromSlash(l)), + }) + } + return files +} + +// snapBinary reports whether the file looks binary (NUL byte in its first +// snapshotSniff bytes). Missing/unreadable files are treated as binary so they +// are never inlined. +func snapBinary(path string) bool { + f, err := os.Open(path) + if err != nil { + return true + } + defer f.Close() + buf := make([]byte, snapshotSniff) + n, _ := f.Read(buf) + return bytes.IndexByte(buf[:n], 0) >= 0 +} + +// safeText coerces arbitrary bytes into printable text (control characters +// except newline/tab become spaces), so a misdetected "text" file can't inject +// garbage into the prompt. +func safeText(b []byte) string { + var sb strings.Builder + sb.Grow(len(b)) + for _, c := range b { + if c == '\n' || c == '\t' || (c >= 0x20 && c < 0x7f) || c >= 0x80 { + sb.WriteByte(c) + continue + } + sb.WriteByte(' ') + } + return sb.String() +} diff --git a/internal/agent/snapshot_test.go b/internal/agent/snapshot_test.go new file mode 100644 index 0000000..aca76de --- /dev/null +++ b/internal/agent/snapshot_test.go @@ -0,0 +1,134 @@ +package agent + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// snapRepo builds a git repo fixture for snapshot tests. Skips when git is +// unavailable (consistent with worktree/patch test suites). +func snapRepo(t *testing.T) string { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not on PATH") + } + dir := t.TempDir() + git := func(args ...string) { + cmd := exec.Command("git", args...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) + } + } + git("init", "-q") + write(t, filepath.Join(dir, "main.go"), "package main\n\nfunc main() {}\n") + write(t, filepath.Join(dir, "docs", "notes.md"), "# notes\nsome prose\n") + write(t, filepath.Join(dir, "logo.png"), []byte{0x89, 'P', 'N', 'G', 0x00, 0x01}) + write(t, filepath.Join(dir, "go.sum"), strings.Repeat("module v1.0.0 h1:abc=\n", 50)) + git("add", "-A") + return dir +} + +func write(t *testing.T, path string, content any) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + var data []byte + switch c := content.(type) { + case string: + data = []byte(c) + case []byte: + data = c + } + if err := os.WriteFile(path, data, 0o644); err != nil { + t.Fatal(err) + } +} + +func TestSnapshotListsAndInlinesTextFiles(t *testing.T) { + dir := snapRepo(t) + snap, err := buildSnapshot(dir, 0) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"main.go", "docs/notes.md", "package main", "# notes"} { + if !strings.Contains(snap, want) { + t.Errorf("snapshot missing %q:\n%s", want, snap) + } + } +} + +func TestSnapshotSkipsBinaryAndLockfiles(t *testing.T) { + dir := snapRepo(t) + snap, err := buildSnapshot(dir, 0) + if err != nil { + t.Fatal(err) + } + if strings.Contains(snap, "PNG") && strings.Contains(strings.SplitN(snap, "=== CONTENTS ===", 2)[1], "----- logo.png") { + t.Error("binary file content should not be inlined") + } + if contents := strings.SplitN(snap, "=== CONTENTS ===", 2)[1]; strings.Contains(contents, "go.sum") { + t.Error("lockfile should not be inlined") + } + for _, tag := range []string{"skipped (binary)", "skipped (lockfile)"} { + if !strings.Contains(snap, tag) { + t.Errorf("expected note %q in:\n%s", tag, snap) + } + } +} + +func TestSnapshotRespectsBudgetWithTruncationMarker(t *testing.T) { + dir := snapRepo(t) + write(t, filepath.Join(dir, "big.txt"), strings.Repeat("a", 4096)) + snap, err := buildSnapshot(dir, 256) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(snap, "[truncated at") { + t.Errorf("expected truncation marker:\n%s", snap) + } + if !strings.Contains(snap, "not shown (budget exhausted)") { + t.Errorf("expected exhausted-budget notes:\n%s", snap) + } + // The truncated body must not exceed the budget by much (header + list overhead allowed). + if len(snap) > 256+4096 { // generous upper bound; the point is big.txt isn't fully inlined twice + t.Fatalf("snapshot suspiciously large: %d bytes", len(snap)) + } +} + +func TestSnapshotIgnoresGitignoredFiles(t *testing.T) { + dir := snapRepo(t) + write(t, filepath.Join(dir, ".gitignore"), "secret.txt\n") + write(t, filepath.Join(dir, "secret.txt"), "hunter2\n") + snap, err := buildSnapshot(dir, 0) + if err != nil { + t.Fatal(err) + } + if strings.Contains(snap, "hunter2") { + t.Error("gitignored file leaked into the snapshot") + } +} + +// TestBuildSnapshotWithoutGit covers plain directories: no .git anywhere, so +// the git ls-files path fails and the filesystem-walk fallback must kick in. +func TestBuildSnapshotWithoutGit(t *testing.T) { + dir := t.TempDir() // deliberately not a git repository + write(t, filepath.Join(dir, "src", "app.py"), "print('hello')\n") + write(t, filepath.Join(dir, "node_modules", "dep.js"), "should be skipped") + write(t, filepath.Join(dir, "logo.png"), "\x00\x01binary") + + snap, err := buildSnapshot(dir, 0) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(snap, "src/app.py") || !strings.Contains(snap, "print('hello')") { + t.Errorf("fallback walk missed source file:\n%s", snap) + } + if strings.Contains(strings.SplitN(snap, "=== CONTENTS ===", 2)[1], "dep.js") { + t.Error("dependency dir leaked into contents") + } +} diff --git a/internal/config/config.go b/internal/config/config.go index a6fc32a..2bcc18e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,13 +19,23 @@ import ( // ConfigFileNames are the filenames Load looks for, in order. var ConfigFileNames = []string{"orchestra.yaml", "orchestra.yml", ".orchestra.yaml"} -// AgentConfig describes how to invoke one agent CLI. +// AgentConfig describes how to invoke one agent. The default type is "cli": +// bin + args spawn a headless subprocess. type "api" instead calls an LLM over +// HTTP (provider/model), with Orchestra applying the returned patch itself. type AgentConfig struct { Name string `yaml:"name"` - Bin string `yaml:"bin"` // binary on PATH (defaults to Name) - Args []string `yaml:"args"` // headless/auto-approve prefix; task appended - DirFlag string `yaml:"dir_flag"` // flag to pass the working dir (for CLIs that ignore cwd, e.g. opencode "--dir") + Type string `yaml:"type"` // "" | "cli" (default) | "api" + Bin string `yaml:"bin"` // cli: binary on PATH (defaults to Name) + Args []string `yaml:"args"` // cli: headless/auto-approve prefix; task appended + DirFlag string `yaml:"dir_flag"` // cli: flag to pass the working dir (e.g. opencode "--dir") Capabilities []string `yaml:"capabilities"` // plan|implement|review + + // API-agent fields (used when Type == "api"). + Provider string `yaml:"provider"` // openai (default) | anthropic + Model string `yaml:"model"` // required + APIBase string `yaml:"api_base"` // optional endpoint override + APIKeyEnv string `yaml:"api_key_env"` // env var holding the key (provider default) + ContextBudget int `yaml:"context_budget"` // repo-snapshot byte cap (0 → DefaultContextBudget) } // ValidateConfig configures the validation pipeline. Any empty stage is skipped. @@ -327,18 +337,30 @@ func (c *Config) BuildRouter(reg *agent.Registry) (*router.Router, error) { return router.New(cls, answerer, reg, c.Router.Routes, c.DefaultAgent), nil } -// BuildRegistry turns the config's agents into a live agent.Registry. +// BuildRegistry turns the config's agents into a live agent.Registry. Agents +// with type "api" become APIAgent instances (HTTP-backed); everything else is +// a CLIAgent. A misconfigured agent is skipped with a warning rather than +// failing the whole registry — consistent with best-effort startup elsewhere. func (c *Config) BuildRegistry() *agent.Registry { reg := agent.NewRegistry() for _, ac := range c.Agents { - bin := ac.Bin - if bin == "" { - bin = ac.Name - } caps := make([]agent.Capability, 0, len(ac.Capabilities)) for _, cp := range ac.Capabilities { caps = append(caps, agent.Capability(cp)) } + if ac.Type == "api" { + a, err := agent.NewAPI(ac.Name, ac.Provider, ac.Model, ac.APIBase, ac.APIKeyEnv, caps, ac.ContextBudget) + if err != nil { + fmt.Printf("(warning: skipping api agent %q: %v)\n", ac.Name, err) + continue + } + reg.Add(a) + continue + } + bin := ac.Bin + if bin == "" { + bin = ac.Name + } reg.Add(agent.New(ac.Name, bin, ac.Args, ac.DirFlag, caps)) } return reg diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..3227eab --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,132 @@ +package config + +import ( + "os" + "path/filepath" + "testing" + + "github.com/crossben/orchestra-code/internal/agent" +) + +func TestBuildRegistryAPIAgent(t *testing.T) { + yamlSrc := ` +default_agent: gpt +agents: + - name: claude + bin: claude + args: ["-p"] + capabilities: [plan, implement] + - name: gpt + type: api + provider: openai + model: gpt-4o + api_key_env: TEST_OPENAI_KEY + capabilities: [implement] +` + cfg, err := parseForTest(t, yamlSrc) + if err != nil { + t.Fatal(err) + } + reg := cfg.BuildRegistry() + + // The API agent is registered and typed. + a, ok := reg.Get("gpt") + if !ok { + t.Fatal("api agent not registered") + } + api, isAPI := a.(*agent.APIAgent) + if !isAPI { + t.Fatalf("expected *agent.APIAgent, got %T", a) + } + if api.Model() != "gpt-4o" || api.ProviderName() != "openai" { + t.Fatalf("unexpected api fields: model=%s provider=%s", api.Model(), api.ProviderName()) + } + + // CLI agents are untouched. + c, ok := reg.Get("claude") + if !ok { + t.Fatal("cli agent missing") + } + if _, isCLI := c.(*agent.CLIAgent); !isCLI { + t.Fatalf("expected *agent.CLIAgent, got %T", c) + } + // The api agent sits alongside the built-in defaults (Load merges over + // Default), and insertion order is preserved. + names := reg.Names() + if len(names) < 2 || names[len(names)-1] != "gpt" { + t.Fatalf("registry names: %v", names) + } +} + +func TestBuildRegistrySkipsBrokenAPIAgent(t *testing.T) { + yamlSrc := ` +agents: + - name: broken + type: api # no model → construction fails + provider: openai +` + cfg, err := parseForTest(t, yamlSrc) + if err != nil { + t.Fatal(err) + } + reg := cfg.BuildRegistry() + if _, ok := reg.Get("broken"); ok { + t.Fatal("misconfigured api agent should be skipped") + } +} + +func TestLoadMergesAPIAgentOverDefaultByName(t *testing.T) { + dir := t.TempDir() + src := "agents:\n - name: claude\n type: api\n model: gpt-4o\n capabilities: [plan]\n" + if err := writeFile(dir, "orchestra.yaml", src); err != nil { + t.Fatal(err) + } + cfg, err := Load(dir) + if err != nil { + t.Fatal(err) + } + var found bool + for _, ac := range cfg.Agents { + if ac.Name == "claude" { + found = true + if ac.Type != "api" || ac.Model != "gpt-4o" { + t.Fatalf("merge did not carry api fields: %+v", ac) + } + // Whole-entry replacement: unspecified cli-only fields stay empty. + if ac.Bin != "" || len(ac.Args) != 0 { + t.Fatalf("expected clean replacement, got %+v", ac) + } + } + } + if !found { + t.Fatal("claude entry lost in merge") + } +} + +func TestDefaultsUnchangedShape(t *testing.T) { + cfg := Default() + reg := cfg.BuildRegistry() + for _, n := range reg.Names() { + a, _ := reg.Get(n) + if _, isAPI := a.(*agent.APIAgent); isAPI { + t.Fatalf("built-in default %q must remain a CLI agent", n) + } + } +} + +// helpers kept tiny so the test file reads top-down. +func parseForTest(t *testing.T, src string) (*Config, error) { + t.Helper() + return loadFromSrc(t.TempDir(), src) +} + +func loadFromSrc(dir, src string) (*Config, error) { + if err := writeFile(dir, "orchestra.yaml", src); err != nil { + return nil, err + } + return Load(dir) +} + +func writeFile(dir, name, content string) error { + return os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644) +} diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 6b6144a..a869872 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -18,6 +18,7 @@ import ( "time" "github.com/crossben/orchestra-code/internal/agent" + "github.com/crossben/orchestra-code/internal/fsdiff" "github.com/crossben/orchestra-code/internal/gitutil" "github.com/crossben/orchestra-code/internal/memory" "github.com/crossben/orchestra-code/internal/review" @@ -70,6 +71,11 @@ type Outcome struct { // Execute runs the full supervised pipeline once (including retries). The reader // is shared with the caller so the accept/reject prompt reads the same input. +// +// Inside a git repository, diffs and rejects use git exactly as before. In a +// plain directory (no .git) Orchestra snapshots the tree before dispatching +// and diffs/restores against that snapshot instead — so the supervised loop +// works anywhere, like opencode or claude. func Execute(ctx context.Context, in *bufio.Reader, opts Options) (out Outcome, err error) { // Record the outcome to memory on a clean (non-error) completion. defer func() { @@ -78,13 +84,18 @@ func Execute(ctx context.Context, in *bufio.Reader, opts Options) (out Outcome, } }() + before, inRepo, err := captureBaseline(opts.Dir) + if err != nil { + return out, err + } + out, err = runLoop(ctx, opts) if err != nil { return out, err } // Review the diff. - diff, err := gitutil.Diff(opts.Dir) + diff, err := workingTreeDiff(opts.Dir, before) if err != nil { return out, fmt.Errorf("compute diff: %w", err) } @@ -96,24 +107,58 @@ func Execute(ctx context.Context, in *bufio.Reader, opts Options) (out Outcome, out.Accepted = review.Prompt(in, diff, out.Report) if out.Accepted { - if opts.CommitOnAccept { + if opts.CommitOnAccept && inRepo { if err := gitutil.Commit(opts.Dir, commitMessage(opts.Prompt)); err != nil { return out, fmt.Errorf("commit accepted changes: %w", err) } fmt.Println(ui.Success("✓ changes accepted and committed")) - } else { - fmt.Println(ui.Success("✓ changes accepted — left in the working tree")) + return out, nil + } + if opts.CommitOnAccept { + opts.logf(ui.Dim("▸ not a git repository — accepted changes stay as plain files")) } + fmt.Println(ui.Success("✓ changes accepted — left in the working tree")) return out, nil } - if err := gitutil.Restore(opts.Dir); err != nil { + if err := discardChanges(opts.Dir, before); err != nil { return out, fmt.Errorf("restore after reject: %w", err) } fmt.Println(ui.Warn("↺ changes rejected — working tree restored")) return out, nil } +// captureBaseline snapshots dir before a run when it is not a git repository. +// The bool reports repo membership; inside a repo the snapshot stays nil and +// git computes its own diffs. +func captureBaseline(dir string) (*fsdiff.Snapshot, bool, error) { + if gitutil.IsRepo(dir) { + return nil, true, nil + } + snap, err := fsdiff.Capture(dir) + if err != nil { + return nil, false, fmt.Errorf("snapshot %s: %w", dir, err) + } + return snap, false, nil +} + +// workingTreeDiff returns what changed in dir since the run started: git's own +// diff inside a repository, a snapshot diff otherwise (before == nil ⇒ git). +func workingTreeDiff(dir string, before *fsdiff.Snapshot) (string, error) { + if before == nil { + return gitutil.Diff(dir) + } + return fsdiff.Diff(dir, before) +} + +// discardChanges reverts dir to its pre-run state. +func discardChanges(dir string, before *fsdiff.Snapshot) error { + if before == nil { + return gitutil.Restore(dir) + } + return fsdiff.Restore(dir, before) +} + // taskGuardrail (#1) is prepended to every task so headless agents proceed on // reasonable assumptions instead of stalling to ask questions they can't be // answered (there is no interactive channel in headless mode). @@ -176,14 +221,44 @@ type Turn struct { Diff string HadChanges bool Err error + + // Baseline is the pre-turn directory snapshot when the working dir is not + // a git repository (nil inside a repo). The TUI passes it back to Revert. + Baseline *fsdiff.Snapshot +} + +// CommitAccepted commits dir's working tree with message when it is a git +// repository and reports whether it did. In a plain directory there is no +// commit to make — accepted changes simply stay as files — so it is a clean +// no-op (false, nil). +func CommitAccepted(dir, message string) (committed bool, err error) { + if !gitutil.IsRepo(dir) { + return false, nil + } + return true, gitutil.Commit(dir, message) +} + +// Revert restores dir to its pre-turn state: git restore inside a repository, +// snapshot restore otherwise. Pass turn.Baseline (may be nil) from the Turn +// being rejected. +func Revert(dir string, before *fsdiff.Snapshot) error { + return discardChanges(dir, before) } // Produce runs dispatch → validate → self-correct quietly (no streaming, no // printing), captures the resulting diff, and returns it WITHOUT committing or // reviewing. Used by the dashboard's in-pane chat, which renders the result and -// handles accept/reject itself. +// handles accept/reject itself. In non-git directories the pre-turn snapshot is +// carried on the returned Turn so reject can restore it. func Produce(ctx context.Context, opts Options) Turn { var t Turn + before, _, err := captureBaseline(opts.Dir) + if err != nil { + t.Err = err + return t + } + t.Baseline = before + prompt := opts.Prompt maxAttempts := opts.MaxRetries + 1 for attempt := 1; attempt <= maxAttempts; attempt++ { @@ -205,7 +280,7 @@ func Produce(ctx context.Context, opts Options) Turn { } prompt = retryPrompt(opts.Prompt, t.Report) } - diff, err := gitutil.Diff(opts.Dir) + diff, err := workingTreeDiff(opts.Dir, before) if err != nil { t.Err = err return t diff --git a/internal/engine/engine_test.go b/internal/engine/engine_test.go new file mode 100644 index 0000000..213c09c --- /dev/null +++ b/internal/engine/engine_test.go @@ -0,0 +1,173 @@ +package engine + +import ( + "bufio" + "context" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/crossben/orchestra-code/internal/agent" +) + +// writeAgent is an in-process fake agent that "edits" the task directory by +// creating one file — enough to exercise the supervised loop's diff/review/ +// restore paths without any subprocess or network. +type writeAgent struct { + name string + path string + content string +} + +func (w *writeAgent) Name() string { return w.name } +func (w *writeAgent) Health() error { return nil } +func (w *writeAgent) Capabilities() []agent.Capability { return []agent.Capability{agent.CapImplement} } +func (w *writeAgent) Run(_ context.Context, task agent.Task) (agent.Result, error) { + target := filepath.Join(task.Dir, w.path) + if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { + return agent.Result{}, err + } + if err := os.WriteFile(target, []byte(w.content), 0o644); err != nil { + return agent.Result{}, err + } + return agent.Result{ExitCode: 0, Output: "wrote " + w.path}, nil +} + +func optsFor(a agent.Agent, dir string) Options { + return Options{ + Agent: a, + Prompt: "make the file", + Dir: dir, + MaxRetries: 0, + } +} + +func mustWrite(t *testing.T, path, content string) { + t.Helper() + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } +} + +func mustNotExist(t *testing.T, path string) { + t.Helper() + if _, err := os.Stat(path); !os.IsNotExist(err) { + t.Errorf("%s should have been removed", path) + } +} + +func mustContain(t *testing.T, path, want string) { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(b), want) { + t.Errorf("%s = %q, missing %q", path, b, want) + } +} + +func TestExecuteNonGitRejectRestores(t *testing.T) { + dir := t.TempDir() // deliberately NOT a git repository + mustWrite(t, filepath.Join(dir, "pre.txt"), "before\n") + + in := bufio.NewReader(strings.NewReader("n\n")) // reject at the review prompt + out, err := Execute(context.Background(), in, optsFor(&writeAgent{name: "fake", path: "created.txt", content: "hi"}, dir)) + if err != nil { + t.Fatal(err) + } + if !out.HadChanges || out.Accepted { + t.Fatalf("unexpected outcome: %+v", out) + } + mustNotExist(t, filepath.Join(dir, "created.txt")) + mustContain(t, filepath.Join(dir, "pre.txt"), "before") +} + +func TestExecuteNonGitAcceptKeeps(t *testing.T) { + dir := t.TempDir() + mustWrite(t, filepath.Join(dir, "pre.txt"), "before\n") + + in := bufio.NewReader(strings.NewReader("y\n")) + opts := optsFor(&writeAgent{name: "fake", path: "sub/created.txt", content: "kept"}, dir) + opts.CommitOnAccept = true // shell-like: would commit in a repo + + out, err := Execute(context.Background(), in, opts) + if err != nil { + t.Fatal(err) + } + if !out.Accepted { + t.Fatalf("expected acceptance: %+v", out) + } + mustContain(t, filepath.Join(dir, "sub", "created.txt"), "kept") + mustContain(t, filepath.Join(dir, "pre.txt"), "before") +} + +func TestExecuteNonGitNoChange(t *testing.T) { + dir := t.TempDir() + in := bufio.NewReader(strings.NewReader("y\n")) + out, err := Execute(context.Background(), in, optsFor(noopAgent{}, dir)) + if err != nil { + t.Fatal(err) + } + if out.HadChanges || out.Accepted { + t.Fatalf("expected no changes: %+v", out) + } +} + +type noopAgent struct{} + +func (noopAgent) Name() string { return "noop" } +func (noopAgent) Health() error { return nil } +func (noopAgent) Capabilities() []agent.Capability { return nil } +func (noopAgent) Run(context.Context, agent.Task) (agent.Result, error) { + return agent.Result{ExitCode: 0, Output: ""}, nil +} + +func TestExecuteGitRepoAcceptStillCommits(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not on PATH") + } + dir := t.TempDir() + run := func(args ...string) { + t.Helper() + cmd := exec.Command("git", args...) + cmd.Dir = dir + cmd.Env = append(os.Environ(), + "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", + "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) + } + } + run("init", "-q") + run("config", "user.email", "t@t") + run("config", "user.name", "t") + mustWrite(t, filepath.Join(dir, "base.txt"), "base\n") + run("add", "-A") + run("commit", "-qm", "init") + + in := bufio.NewReader(strings.NewReader("y\n")) + opts := optsFor(&writeAgent{name: "fake", path: "committed.txt", content: "in history"}, dir) + opts.CommitOnAccept = true + out, err := Execute(context.Background(), in, opts) + if err != nil { + t.Fatal(err) + } + if !out.Accepted { + t.Fatalf("expected acceptance: %+v", out) + } + var count string + cmd := exec.Command("git", "-C", dir, "rev-list", "--count", "HEAD") + if c, err := cmd.Output(); err == nil { + count = strings.TrimSpace(string(c)) + } + if count != "2" { + t.Fatalf("expected 2 commits after acceptance, got %q", count) + } + status, _ := exec.Command("git", "-C", dir, "status", "--porcelain").CombinedOutput() + if strings.TrimSpace(string(status)) != "" { + t.Errorf("tree should be clean after commit: %q", status) + } +} diff --git a/internal/fsdiff/diff.go b/internal/fsdiff/diff.go new file mode 100644 index 0000000..49b4612 --- /dev/null +++ b/internal/fsdiff/diff.go @@ -0,0 +1,237 @@ +package fsdiff + +import ( + "bytes" + "fmt" + "sort" + "strconv" + "strings" +) + +// Diff renders a git-style unified diff between the snapshot and the current +// contents of dir. Files that are byte-identical produce no output; added, +// modified, deleted, and opaque (binary/withheld) files each get a section in +// git's format so existing rendering/coloring works unchanged. +func Diff(dir string, before *Snapshot) (string, error) { + after, err := Capture(dir) + if err != nil { + return "", err + } + if before == nil { + before = &Snapshot{Files: map[string]*File{}} + } + + paths := make([]string, 0, len(before.Files)+len(after.Files)) + seen := map[string]bool{} + for p := range before.Files { + paths = append(paths, p) + seen[p] = true + } + for p := range after.Files { + if !seen[p] { + paths = append(paths, p) + } + } + sort.Strings(paths) + + var b strings.Builder + for _, p := range paths { + o, hadO := before.Files[p] + c, hadC := after.Files[p] + switch { + case hadO && !hadC: + writeRemoved(&b, p, o) + case !hadO && hadC: + writeAdded(&b, p, c) + case o.Withheld || c.Withheld: + writeOpaque(&b, p) // content unknown on at least one side + case bytes.Equal(o.Content, c.Content): + // unchanged (mode-only changes are not tracked git-free) + default: + writeModified(&b, p, o, c) + } + } + return b.String(), nil +} + +// fileMode renders a permission set the way git does: regular files are +// either 100644 or 100755 (git tracks only the executable bit). +func fileMode(f *File) string { + if f.Mode&0o111 != 0 { + return "100755" + } + return "100644" +} + +func writeHeader(b *strings.Builder, path string, f *File, kind string) { + fmt.Fprintf(b, "diff --git a/%s b/%s\n", path, path) + switch kind { + case "added": + fmt.Fprintf(b, "new file mode %s\n", fileMode(f)) + case "deleted": + fmt.Fprintf(b, "deleted file mode %s\n", fileMode(f)) + } +} + +func writeAdded(b *strings.Builder, path string, f *File) { + if f.Withheld || looksBinary(f.Content) { + writeHeader(b, path, f, "added") + fmt.Fprintln(b, "Binary files /dev/null and b/"+path+" differ") + return + } + lines, nl := splitLines(f.Content) + writeHeader(b, path, f, "added") + fmt.Fprintf(b, "--- /dev/null\n+++ b/%s\n", path) + edits := insAll(lines) + writeHunks(b, nil, lines, edits, true, nl) +} + +func writeRemoved(b *strings.Builder, path string, f *File) { + if f.Withheld || looksBinary(f.Content) { + writeHeader(b, path, f, "deleted") + fmt.Fprintln(b, "Binary files a/"+path+" and /dev/null differ") + return + } + lines, nl := splitLines(f.Content) + writeHeader(b, path, f, "deleted") + fmt.Fprintf(b, "--- a/%s\n+++ /dev/null\n", path) + edits := delAll(lines) + writeHunks(b, lines, nil, edits, nl, true) +} + +func writeOpaque(b *strings.Builder, path string) { + fmt.Fprintf(b, "diff --git a/%s b/%s\nBinary files a/%s and b/%s differ\n", path, path, path, path) +} + +func writeModified(b *strings.Builder, path string, o, c *File) { + if looksBinary(o.Content) || looksBinary(c.Content) { + fmt.Fprintf(b, "diff --git a/%s b/%s\nBinary files a/%s and b/%s differ\n", path, path, path, path) + return + } + aLines, aNL := splitLines(o.Content) + bLines, bNL := splitLines(c.Content) + writeHeader(b, path, c, "") + fmt.Fprintf(b, "--- a/%s\n+++ b/%s\n", path, path) + writeHunks(b, aLines, bLines, lineEdits(aLines, bLines), aNL, bNL) +} + +// ctxLines is how many unchanged lines surround each change cluster, +// matching git's default. +const ctxLines = 3 + +// writeHunks renders edits as unified-diff hunks: changes closer than 2×ctx +// share a hunk, each hunk carries an @@ header, and missing trailing newlines +// get git's "\ No newline at end of file" marker. +func writeHunks(b *strings.Builder, a, bb []string, edits []edit, aNL, bNL bool) { + n := len(edits) + + // Mark included edits: every change plus up to ctxLines of context + // around it. Runs of context bridge nearby changes into one hunk. + include := make([]bool, n) + countdown := 0 + for i, e := range edits { + if e.kind != opEq { + include[i] = true + countdown = ctxLines + continue + } + if countdown > 0 { + include[i] = true + countdown-- + } + } + countdown = 0 + for i := n - 1; i >= 0; i-- { + if edits[i].kind != opEq { + include[i] = true + countdown = ctxLines + continue + } + if countdown > 0 { + include[i] = true + countdown-- + } + } + + // Emit contiguous included blocks as hunks. + posA, posB := 0, 0 + for s := 0; s < n; { + if !include[s] { + advance(&posA, &posB, edits[s]) + s++ + continue + } + e := s + for e < n && include[e] { + e++ + } + writeOneHunk(b, a, bb, edits[s:e], &posA, &posB, aNL, bNL) + s = e + } +} + +// writeOneHunk emits the @@ header and body for one contiguous run of edits, +// updating the running line positions past the whole run. +func writeOneHunk(b *strings.Builder, a, bb []string, run []edit, posA, posB *int, aNL, bNL bool) { + aStart, bStart := *posA, *posB + var aCount, bCount int + for _, e := range run { + switch e.kind { + case opDel: + aCount++ + case opIns: + bCount++ + default: + aCount++ + bCount++ + } + } + fmt.Fprintf(b, "@@ -%s +%s @@\n", rng(aStart, aCount), rng(bStart, bCount)) + + for _, e := range run { + switch e.kind { + case opEq: + b.WriteString(" " + e.line + "\n") + *posA++ + *posB++ + case opDel: + b.WriteString("-" + e.line + "\n") + if *posA == len(a)-1 && !aNL { + b.WriteString("\\ No newline at end of file\n") + } + *posA++ + case opIns: + b.WriteString("+" + e.line + "\n") + if *posB == len(bb)-1 && !bNL { + b.WriteString("\\ No newline at end of file\n") + } + *posB++ + } + } +} + +// advance moves the line cursors past one edit without emitting it. +func advance(posA, posB *int, e edit) { + switch e.kind { + case opDel: + *posA++ + case opIns: + *posB++ + default: + *posA++ + *posB++ + } +} + +// rng formats one side of an @@ header. git omits ",1" for single-line spans +// and writes the insertion point followed by ",0" for empty ones. +func rng(start, count int) string { + switch { + case count == 0: + return fmt.Sprintf("%d,0", start) + case count == 1: + return strconv.Itoa(start + 1) + default: + return fmt.Sprintf("%d,%d", start+1, count) + } +} diff --git a/internal/fsdiff/fsdiff_test.go b/internal/fsdiff/fsdiff_test.go new file mode 100644 index 0000000..91d136b --- /dev/null +++ b/internal/fsdiff/fsdiff_test.go @@ -0,0 +1,279 @@ +package fsdiff + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func writeFile(t *testing.T, path, content string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } +} + +func readFile(t *testing.T, path string) string { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + return string(b) +} + +func TestCaptureExcludesNoise(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "keep.txt"), "yes") + writeFile(t, filepath.Join(dir, "node_modules", "pkg", "x.js"), "dep") + writeFile(t, filepath.Join(dir, ".git", "HEAD"), "ref") + writeFile(t, filepath.Join(dir, "logo.png"), "fake png") + writeFile(t, filepath.Join(dir, "notes.pyc"), "bytecode") + + snap, err := Capture(dir) + if err != nil { + t.Fatal(err) + } + if _, ok := snap.Files["keep.txt"]; !ok { + t.Error("keep.txt should be captured") + } + for _, gone := range []string{"node_modules/pkg/x.js", ".git/HEAD", "logo.png", "notes.pyc"} { + if _, ok := snap.Files[gone]; ok { + t.Errorf("%s should be excluded", gone) + } + } +} + +func TestCaptureWithholdsOversizedFiles(t *testing.T) { + dir := t.TempDir() + big := strings.Repeat("a", MaxFileBytes+1) + writeFile(t, filepath.Join(dir, "big.log"), big) + + snap, err := Capture(dir) + if err != nil { + t.Fatal(err) + } + f, ok := snap.Files["big.log"] + if !ok || !f.Withheld { + t.Fatalf("expected withheld entry, got %+v", f) + } +} + +const baseReadme = "# title\n\nintro line\n" + +func TestDiffAddedModifiedDeleted(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "readme.md"), baseReadme) + writeFile(t, filepath.Join(dir, "gone.txt"), "delete me\n") + + before, err := Capture(dir) + if err != nil { + t.Fatal(err) + } + + // Mutate: modify readme (middle change), delete gone.txt, add new.txt. + writeFile(t, filepath.Join(dir, "readme.md"), "# title\n\nchanged intro line\n") + if err := os.Remove(filepath.Join(dir, "gone.txt")); err != nil { + t.Fatal(err) + } + writeFile(t, filepath.Join(dir, "new", "thing.txt"), "brand new\n") + + diff, err := Diff(dir, before) + if err != nil { + t.Fatal(err) + } + + checks := []string{ + "diff --git a/readme.md b/readme.md", + "--- a/readme.md", + "+++ b/readme.md", + "-intro line", + "+changed intro line", + "@@ -1,3 +1,3 @@", + + "diff --git a/gone.txt b/gone.txt", + "deleted file mode 100644", + "+++ /dev/null", + "-delete me", + + "diff --git a/new/thing.txt b/new/thing.txt", + "new file mode 100644", + "--- /dev/null", + "+brand new", + } + for _, want := range checks { + if !strings.Contains(diff, want) { + t.Errorf("diff missing %q:\n%s", want, diff) + } + } + if strings.Count(diff, "diff --git") != 3 { + t.Errorf("unexpected extra sections:\n%s", diff) + } +} + +func TestDiffIdenticalIsEmpty(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "a.txt"), "same\n") + before, _ := Capture(dir) + diff, err := Diff(dir, before) + if err != nil { + t.Fatal(err) + } + if diff != "" { + t.Fatalf("expected empty diff, got:\n%s", diff) + } +} + +func TestDiffNoTrailingNewlineMarker(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "f.txt"), "one\ntwo") + before, _ := Capture(dir) + writeFile(t, filepath.Join(dir, "f.txt"), "one\nTWO") + diff, err := Diff(dir, before) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(diff, "-two\n\\ No newline at end of file") { + t.Errorf("expected no-newline marker after -two:\n%s", diff) + } +} + +func TestDiffBinaryChangeIsOpaque(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "blob.dat"), "\x00\x01binary v1") + before, _ := Capture(dir) + writeFile(t, filepath.Join(dir, "blob.dat"), "\x00\x01binary v2") + diff, err := Diff(dir, before) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(diff, "Binary files a/blob.dat and b/blob.dat differ") { + t.Errorf("expected opaque binary section:\n%s", diff) + } +} + +func TestDiffEmptyToContentAndBack(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "empty.txt"), "") + before, _ := Capture(dir) + writeFile(t, filepath.Join(dir, "empty.txt"), "now has lines\n") + diff, err := Diff(dir, before) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(diff, "@@ -0,0 +1 @@") || !strings.Contains(diff, "+now has lines") { + t.Errorf("empty→content header wrong:\n%s", diff) + } +} + +func TestRestoreRoundTrip(t *testing.T) { + dir := t.TempDir() + writeFile(t, filepath.Join(dir, "src", "app.go"), "package main\n") + writeFile(t, filepath.Join(dir, "keep.txt"), "original") + writeFile(t, filepath.Join(dir, "doomed.txt"), "bye") + mode0755 := os.FileMode(0o755) + if err := os.WriteFile(filepath.Join(dir, "script.sh"), []byte("#!/bin/sh\n"), mode0755); err != nil { + t.Fatal(err) + } + + before, err := Capture(dir) + if err != nil { + t.Fatal(err) + } + + // Wreck the place: modify two, delete two, create two. + writeFile(t, filepath.Join(dir, "keep.txt"), "modified!") + writeFile(t, filepath.Join(dir, "src", "app.go"), "package main // hacked\n") + os.Remove(filepath.Join(dir, "doomed.txt")) + os.Remove(filepath.Join(dir, "script.sh")) + writeFile(t, filepath.Join(dir, "created.txt"), "new junk") + writeFile(t, filepath.Join(dir, "tmp", "junk.log"), "junk") + + if err := Restore(dir, before); err != nil { + t.Fatal(err) + } + + if got := readFile(t, filepath.Join(dir, "keep.txt")); got != "original" { + t.Errorf("keep.txt not restored: %q", got) + } + if got := readFile(t, filepath.Join(dir, "src", "app.go")); got != "package main\n" { + t.Errorf("src/app.go not restored: %q", got) + } + if _, err := os.Stat(filepath.Join(dir, "doomed.txt")); err != nil { + t.Error("doomed.txt should be recreated") + } + info, err := os.Stat(filepath.Join(dir, "script.sh")) + if err != nil { + t.Fatal("script.sh should be restored") + } + if info.Mode().Perm() != mode0755 { + t.Errorf("mode not restored: %v", info.Mode()) + } + for _, gone := range []string{"created.txt", "tmp/junk.log"} { + if _, err := os.Stat(filepath.Join(dir, gone)); !os.IsNotExist(err) { + t.Errorf("%s should have been removed", gone) + } + } + if _, err := os.Stat(filepath.Join(dir, "tmp")); !os.IsNotExist(err) { + t.Error("emptied tmp/ dir should be pruned") + } +} + +func TestLineEditsMinimalScript(t *testing.T) { + a := []string{"a", "b", "c", "d"} + b := []string{"a", "x", "c", "d"} + edits := lineEdits(a, b) + var gotDel, gotIns, gotEq int + for _, e := range edits { + switch e.kind { + case opDel: + gotDel++ + if e.line != "b" { + t.Errorf("wrong deletion: %q", e.line) + } + case opIns: + gotIns++ + if e.line != "x" { + t.Errorf("wrong insertion: %q", e.line) + } + default: + gotEq++ + } + } + if gotDel != 1 || gotIns != 1 || gotEq != 3 { + t.Errorf("not minimal: del=%d ins=%d eq=%d", gotDel, gotIns, gotEq) + } +} + +func TestLineEditsReconstructB(t *testing.T) { + cases := [][2][]string{ + {{"a", "b", "c"}, {"a", "x", "c"}}, + {{"1", "2"}, {"2", "1"}}, + {{"only"}, {}}, + {{}, {"fresh"}}, + {{"k", "e", "e", "p"}, {"k", "e", "e", "p"}}, + } + for i, tc := range cases { + a, b := tc[0], tc[1] + var out []string + var dels int + for _, e := range lineEdits(a, b) { + switch e.kind { + case opIns, opEq: + out = append(out, e.line) + case opDel: + dels++ + } + } + if strings.Join(out, "|") != strings.Join(b, "|") { + t.Errorf("case %d: reconstruction = %v, want %v", i, out, b) + } + if dels > len(a) { + t.Errorf("case %d: too many deletions (%d)", i, dels) + } + } +} diff --git a/internal/fsdiff/myers.go b/internal/fsdiff/myers.go new file mode 100644 index 0000000..54cdd01 --- /dev/null +++ b/internal/fsdiff/myers.go @@ -0,0 +1,176 @@ +package fsdiff + +import "strings" + +// edit is one step of a line-level diff. +type opKind uint8 + +const ( + opEq opKind = iota // context line (same on both sides) + opDel // line present in a, removed + opIns // line present in b, added +) + +type edit struct { + kind opKind + line string +} + +// splitLines splits data into lines without their newline characters. The +// bool reports whether the data ended with a newline (needed to emit git's +// "\ No newline at end of file" marker correctly). +func splitLines(data []byte) ([]string, bool) { + if len(data) == 0 { + return nil, true + } + s := string(data) + trailing := strings.HasSuffix(s, "\n") + s = strings.TrimSuffix(s, "\n") + if s == "" { + return nil, trailing + } + return strings.Split(s, "\n"), trailing +} + +// lineEdits diffs a against b line-by-line. Common prefix/suffix is trimmed +// first; the remainder goes through Myers' O(ND) algorithm with a size guard +// that degrades to a whole-file replacement for pathological inputs. +func lineEdits(a, b []string) []edit { + p, s := commonPreSuf(a, b) + head := a[:p] + tail := a[len(a)-s:] + midA, midB := a[p:len(a)-s], b[p:len(b)-s] + + var out []edit + for _, l := range head { + out = append(out, edit{opEq, l}) + } + out = append(out, diffMiddle(midA, midB)...) + for _, l := range tail { + out = append(out, edit{opEq, l}) + } + return out +} + +// commonPreSuf returns the lengths of the shared prefix and suffix. +func commonPreSuf(a, b []string) (pre, suf int) { + n, m := len(a), len(b) + for pre < n && pre < m && a[pre] == b[pre] { + pre++ + } + for suf < n-pre && suf < m-pre && a[n-1-suf] == b[m-1-suf] { + suf++ + } + return pre, suf +} + +// Bounds for running full Myers with trace backtracking. Inputs larger than +// these (rare: common prefix/suffix trimming handles almost everything) +// degrade to a single delete-all/insert-all block — correct, just not minimal. +const ( + maxMyersLen = 1024 + maxMyersCells = 1 << 20 +) + +func diffMiddle(a, b []string) []edit { + n, m := len(a), len(b) + switch { + case n == 0 && m == 0: + return nil + case n == 0: + return insAll(b) + case m == 0: + return delAll(a) + } + if n > maxMyersLen || m > maxMyersLen || n*m > maxMyersCells { + return append(delAll(a), insAll(b)...) + } + return myers(a, b) +} + +func delAll(a []string) []edit { + out := make([]edit, len(a)) + for i, l := range a { + out[i] = edit{opDel, l} + } + return out +} + +func insAll(b []string) []edit { + out := make([]edit, len(b)) + for i, l := range b { + out[i] = edit{opIns, l} + } + return out +} + +// myers computes the minimal edit script between a and b using Myers' O((N+M)D) +// shortest-edit-distance algorithm with full V-trace backtracking. +func myers(a, b []string) []edit { + n, m := len(a), len(b) + maxD := n + m + offset := maxD + v := make([]int, 2*maxD+1) + + var trace [][]int + found := false + for d := 0; d <= maxD && !found; d++ { + vc := make([]int, len(v)) + copy(vc, v) + trace = append(trace, vc) + for k := -d; k <= d; k += 2 { + var x int + if k == -d || (k != d && v[offset+k-1] < v[offset+k+1]) { + x = v[offset+k+1] // down: insertion from b + } else { + x = v[offset+k-1] + 1 // right: deletion from a + } + y := x - k + for x < n && y < m && a[x] == b[y] { + x++ + y++ + } + v[offset+k] = x + if x >= n && y >= m { + found = true + break + } + } + } + + // Backtrack through the trace collecting edits (in reverse). + var rev []edit + x, y := n, m + for d := len(trace) - 1; d >= 0; d-- { + vv := trace[d] + k := x - y + var prevK int + if k == -d || (k != d && vv[offset+k-1] < vv[offset+k+1]) { + prevK = k + 1 + } else { + prevK = k - 1 + } + prevX := vv[offset+prevK] + prevY := prevX - prevK + for x > prevX && y > prevY { // snake (equal lines) + x-- + y-- + rev = append(rev, edit{opEq, a[x]}) + } + if d > 0 { + if x == prevX { // came down ⇒ insertion + y-- + rev = append(rev, edit{opIns, b[y]}) + } else { // came right ⇒ deletion + x-- + rev = append(rev, edit{opDel, a[x]}) + } + } + } + // Reverse into forward order. + out := make([]edit, len(rev)) + for i, e := range rev { + out[len(rev)-1-i] = e + } + return out +} diff --git a/internal/fsdiff/restore.go b/internal/fsdiff/restore.go new file mode 100644 index 0000000..922bed2 --- /dev/null +++ b/internal/fsdiff/restore.go @@ -0,0 +1,75 @@ +package fsdiff + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "sort" + "strings" +) + +// Restore reverts dir to the state captured in before: files created since the +// snapshot are deleted (along with parent dirs left empty), modified files are +// rewritten, deleted files are recreated, and modes are restored. Files whose +// baseline content was withheld (too large) are left untouched — there is +// nothing to restore them to. +func Restore(dir string, before *Snapshot) error { + root := before.Dir + after, err := Capture(root) + if err != nil { + return fmt.Errorf("re-snapshot for restore: %w", err) + } + + // 1. Delete files that did not exist at snapshot time. + var emptiedDirs = map[string]bool{} + for p := range after.Files { + if _, existed := before.Files[p]; existed { + continue + } + target := filepath.Join(root, filepath.FromSlash(p)) + if err := os.Remove(target); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("remove %s: %w", p, err) + } + for d := filepath.Dir(target); d != root && strings.HasPrefix(d, root); d = filepath.Dir(d) { + emptiedDirs[d] = true + } + } + + // 2. Bring every baseline file back (content + mode). + for p, f := range before.Files { + if f.Withheld { + continue // unknown baseline content: nothing to restore + } + cur, exists := after.Files[p] + sameContent := exists && cur != nil && !cur.Withheld && bytes.Equal(cur.Content, f.Content) + if sameContent && cur.Mode == f.Mode { + continue + } + target := filepath.Join(root, filepath.FromSlash(p)) + if sameContent { + if err := os.Chmod(target, f.Mode.Perm()); err != nil { + return fmt.Errorf("chmod %s: %w", p, err) + } + continue + } + if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { + return fmt.Errorf("mkdir for %s: %w", p, err) + } + if err := os.WriteFile(target, f.Content, f.Mode.Perm()); err != nil { + return fmt.Errorf("restore %s: %w", p, err) + } + } + + // 3. Prune directories emptied by step 1 (deepest first; Remove is a + // no-op on non-empty dirs). + dirs := make([]string, 0, len(emptiedDirs)) + for d := range emptiedDirs { + dirs = append(dirs, d) + } + sort.Sort(sort.Reverse(sort.StringSlice(dirs))) + for _, d := range dirs { + _ = os.Remove(d) + } + return nil +} diff --git a/internal/fsdiff/snapshot.go b/internal/fsdiff/snapshot.go new file mode 100644 index 0000000..47a61f1 --- /dev/null +++ b/internal/fsdiff/snapshot.go @@ -0,0 +1,166 @@ +// Package fsdiff provides git-free change tracking for directories that are +// not git repositories. Orchestra's supervised loop is built around diffs: +// show what changed, keep it on accept, restore it on reject. Inside a git +// repo those jobs belong to git; everywhere else this package fills in by +// snapshotting the working directory before a run and diffing/restoring +// against that snapshot afterwards. +// +// Diffs are rendered in git's unified format (diff --git / --- +++ / @@), so +// the rest of Orchestra — colored diff rendering, review prompts, TUI panes — +// needs no special-casing. +package fsdiff + +import ( + "bytes" + "errors" + "io/fs" + "os" + "path/filepath" + "strings" +) + +const ( + // MaxFileBytes caps how much of any single file is captured. Larger + // files are listed but their content is withheld (they diff/restore as + // opaque entries rather than line-by-line). + MaxFileBytes = 1 << 20 // 1 MiB + // MaxTotalBytes is a runaway guard on the whole snapshot. + maxTotalBytes = 128 << 20 // 128 MiB +) + +// ErrTooLarge reports that the directory holds more content than Orchestra +// will snapshot for git-free supervision. +var ErrTooLarge = errors.New("directory too large to snapshot (>128 MiB of file contents)") + +// File is one captured file. Content is nil exactly when Withheld is true. +type File struct { + Mode os.FileMode + Content []byte + Withheld bool // too large to capture; treated as opaque +} + +// Snapshot is the captured state of a directory. Keys are slash-separated, +// repo-relative paths ("src/main.go"). +type Snapshot struct { + Dir string // absolute directory the snapshot was taken from + Files map[string]*File +} + +// excludedDirs are directory names never walked: VCS internals, dependency +// trees, caches, and build outputs. Matching is by base name. +var excludedDirs = map[string]bool{ + ".git": true, ".hg": true, ".svn": true, + "node_modules": true, "bower_components": true, "jspm_packages": true, + "vendor": true, "__pycache__": true, ".tox": true, ".venv": true, + "venv": true, ".mypy_cache": true, ".pytest_cache": true, + ".gradle": true, ".terraform": true, ".cache": true, + "dist": true, "build": true, "out": true, "target": true, + ".next": true, ".nuxt": true, "coverage": true, + ".idea": true, ".vscode": true, +} + +// excludedFileNames are files never captured, by base name. +var excludedFileNames = map[string]bool{ + ".DS_Store": true, + "Thumbs.db": true, +} + +// excludedSuffixes are file suffixes never captured (binaries and archives — +// they diff as opaque "Binary files … differ" entries if they change). +var excludedSuffixes = []string{ + ".pyc", ".pyo", ".class", ".o", ".so", ".dylib", ".dll", ".exe", + ".png", ".jpg", ".jpeg", ".gif", ".ico", ".webp", ".pdf", ".zip", + ".tar", ".gz", ".tgz", ".bz2", ".7z", ".rar", ".woff", ".woff2", + ".ttf", ".otf", ".eot", ".mp3", ".mp4", ".mov", ".avi", ".sqlite", + ".db", +} + +// Excluded reports whether a path component should be skipped when walking a +// working directory. Exported so other packages (e.g. the API agent's context +// snapshot) walk with identical rules. +func Excluded(rel string, isDir bool) bool { + base := filepath.Base(rel) + if isDir { + return excludedDirs[base] + } + if excludedFileNames[base] { + return true + } + for _, suf := range excludedSuffixes { + if strings.HasSuffix(base, suf) { + return true + } + } + return false +} + +// Capture snapshots every regular file under dir, subject to the exclusion +// and size rules above. +func Capture(dir string) (*Snapshot, error) { + abs, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + snap := &Snapshot{Dir: abs, Files: map[string]*File{}} + var total int + err = filepath.WalkDir(abs, func(p string, d fs.DirEntry, err error) error { + if err != nil { + if d != nil && d.IsDir() { + return filepath.SkipDir // unreadable subtree: skip, don't fail + } + return nil + } + rel, rerr := filepath.Rel(abs, p) + if rerr != nil { + return nil + } + rel = filepath.ToSlash(rel) + if rel == "." { + return nil + } + if d.IsDir() { + if Excluded(rel, true) { + return filepath.SkipDir + } + return nil + } + if !d.Type().IsRegular() { + return nil // symlinks and specials are not tracked + } + if Excluded(rel, false) { + return nil + } + info, ierr := d.Info() + if ierr != nil { + return nil + } + if info.Size() > MaxFileBytes { + snap.Files[rel] = &File{Mode: info.Mode().Perm(), Withheld: true} + return nil + } + data, rerr := os.ReadFile(p) + if rerr != nil { + return nil // unreadable file: ignore + } + total += len(data) + if total > maxTotalBytes { + return ErrTooLarge + } + snap.Files[rel] = &File{Mode: info.Mode().Perm(), Content: data} + return nil + }) + if err != nil { + return nil, err + } + return snap, nil +} + +// looksBinary reports whether data starts like a binary blob (NUL byte in the +// first 800 bytes, mirroring git's heuristic). +func looksBinary(data []byte) bool { + n := len(data) + if n > 800 { + n = 800 + } + return bytes.IndexByte(data[:n], 0) >= 0 +} diff --git a/internal/llm/anthropic.go b/internal/llm/anthropic.go new file mode 100644 index 0000000..1251bad --- /dev/null +++ b/internal/llm/anthropic.go @@ -0,0 +1,87 @@ +package llm + +import ( + "context" + "strings" +) + +// Anthropic talks to the Anthropic Messages API (POST {base}/v1/messages). +type Anthropic struct { + base string // e.g. https://api.anthropic.com + key string // sent as x-api-key + model string + hc HTTPClient +} + +// Name implements Provider. +func (a *Anthropic) Name() string { return "anthropic" } + +const anthropicVersion = "2023-06-01" + +// anthropicRequest / anthropicResponse model the minimal messages wire shape. +type anthropicRequest struct { + Model string `json:"model"` + System string `json:"system,omitempty"` + Messages []anthropicTurn `json:"messages"` + MaxTokens int `json:"max_tokens"` + Temperature float64 `json:"temperature"` +} + +type anthropicTurn struct { + Role string `json:"role"` // "user" | "assistant" + Content string `json:"content"` +} + +type anthropicResponse struct { + Content []struct { + Type string `json:"type"` // "text" | "tool_use" | ... + Text string `json:"text"` + } `json:"content"` + StopReason string `json:"stop_reason"` + Error *struct { + Message string `json:"message"` + Type string `json:"type"` + } `json:"error"` +} + +// Complete implements Provider. +func (a *Anthropic) Complete(ctx context.Context, req Request) (Response, error) { + maxTokens := req.MaxTokens + if maxTokens <= 0 { + maxTokens = 4096 // Anthropic requires an explicit cap + } + turns := make([]anthropicTurn, 0, len(req.Messages)) + for _, m := range req.Messages { + role := m.Role + if role == "" { + role = "user" + } + turns = append(turns, anthropicTurn{Role: role, Content: m.Content}) + } + payload := anthropicRequest{ + Model: a.model, + System: req.System, + Messages: turns, + MaxTokens: maxTokens, + Temperature: req.Temperature, + } + + headers := map[string]string{ + "x-api-key": a.key, + "anthropic-version": anthropicVersion, + } + + var out anthropicResponse + url := a.base + "/v1/messages" + if err := postJSON(ctx, a.hc, "anthropic", url, headers, payload, &out); err != nil { + return Response{}, err + } + + var b strings.Builder + for _, c := range out.Content { + if c.Type == "text" { + b.WriteString(c.Text) + } + } + return Response{Text: b.String(), StopReason: out.StopReason}, nil +} diff --git a/internal/llm/errors.go b/internal/llm/errors.go new file mode 100644 index 0000000..6ece8e5 --- /dev/null +++ b/internal/llm/errors.go @@ -0,0 +1,99 @@ +package llm + +import ( + "fmt" + "strings" +) + +// ErrorKind classifies API failures so callers can produce human-actionable +// messages (notably in `orchestra agents --probe` output). +type ErrorKind int + +const ( + ErrNetwork ErrorKind = iota // transport failure, timeout, DNS + ErrAuth // 401/403 — bad or missing key + ErrBilling // 402 / quota exhausted + ErrRateLimit // 429 + ErrRequest // 400/404/422 — malformed request or unknown model + ErrServer // 5xx — provider-side failure +) + +// Error is a typed API failure. +type Error struct { + Provider string // "openai" | "anthropic" ("" for generic network failures) + Kind ErrorKind + Status int // HTTP status code (0 when the request never got a response) + Body string // truncated response body or transport error text +} + +// Error implements the error interface. +func (e *Error) Error() string { + name := e.Provider + if name == "" { + name = "llm" + } + switch e.Kind { + case ErrAuth: + return fmt.Sprintf("%s: auth failed (status %d)", name, e.Status) + case ErrBilling: + return fmt.Sprintf("%s: quota/billing error (status %d)", name, e.Status) + case ErrRateLimit: + return fmt.Sprintf("%s: rate limited (status %d)", name, e.Status) + case ErrRequest: + return fmt.Sprintf("%s: rejected the request (status %d)", name, e.Status) + case ErrServer: + if e.Status > 0 { + return fmt.Sprintf("%s: server error (status %d)", name, e.Status) + } + return fmt.Sprintf("%s: bad response: %s", name, e.Body) + default: + return fmt.Sprintf("%s: network error: %s", name, e.Body) + } +} + +// UserDetail returns a short, actionable hint suitable for probe output. +func (e *Error) UserDetail() string { + switch e.Kind { + case ErrAuth: + return "check your API key and that it has access to this provider" + case ErrBilling: + return "account out of credits or over quota" + case ErrRateLimit: + return "rate limited — retry later or lower concurrency" + case ErrRequest: + if e.Body != "" { + return truncate(e.Body, 160) + } + return "request rejected — check the model name and api_base" + case ErrServer: + return "provider temporarily unavailable" + default: + return truncate(e.Body, 160) + } +} + +// kindFromStatus maps an HTTP status code to its failure kind. +func kindFromStatus(status int) ErrorKind { + switch { + case status == 401 || status == 403: + return ErrAuth + case status == 402: + return ErrBilling + case status == 429: + return ErrRateLimit + case status >= 500: + return ErrServer + default: + return ErrRequest + } +} + +// truncate shortens s to at most n runes, appending an ellipsis when cut. +func truncate(s string, n int) string { + s = strings.TrimSpace(s) + r := []rune(s) + if len(r) <= n { + return s + } + return string(r[:n-1]) + "…" +} diff --git a/internal/llm/http.go b/internal/llm/http.go new file mode 100644 index 0000000..a94433e --- /dev/null +++ b/internal/llm/http.go @@ -0,0 +1,85 @@ +package llm + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net" + "net/http" + "time" +) + +// HTTPClient is the subset of *http.Client the providers need. It exists so +// tests can inject a transport without a real network. +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} + +// defaultClient returns a client with no global timeout: deadlines come from +// the request's context (agent Task.Timeout), which is how the rest of +// Orchestra (internal/runner) models timeouts. +func defaultClient() HTTPClient { + return &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{Timeout: 15 * time.Second}).DialContext, + }, + } +} + +// postJSON is the shared request path for all providers: JSON-encode payload, +// attach headers, POST, decode a 2xx response into out, and map every failure +// to a typed *Error. name is used to attribute errors to their provider. +func postJSON(ctx context.Context, hc HTTPClient, name, url string, headers map[string]string, payload, out any) error { + body, err := json.Marshal(payload) + if err != nil { + return &Error{Provider: name, Kind: ErrRequest, Body: "encode request: " + err.Error()} + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return &Error{Provider: name, Kind: ErrRequest, Body: err.Error()} + } + req.Header.Set("Content-Type", "application/json") + for k, v := range headers { + req.Header.Set(k, v) + } + + resp, err := hc.Do(req) + if err != nil { + return &Error{Provider: name, Kind: ErrNetwork, Body: transportText(err)} + } + defer resp.Body.Close() + + raw, _ := io.ReadAll(io.LimitReader(resp.Body, maxBodyRead)) + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return &Error{ + Provider: name, + Kind: kindFromStatus(resp.StatusCode), + Status: resp.StatusCode, + Body: truncate(string(raw), bodyDetailLimit), + } + } + if err := json.Unmarshal(raw, out); err != nil { + return &Error{Provider: name, Kind: ErrServer, Status: resp.StatusCode, Body: "decode response: " + err.Error()} + } + return nil +} + +const ( + maxBodyRead = 1 << 20 // never buffer more than 1 MiB of a response + bodyDetailLimit = 400 // keep error bodies short but useful +) + +// transportText condenses a transport error into its meaningful part: context +// deadlines/cancellation read as themselves, anything else keeps its message. +func transportText(err error) string { + switch { + case errors.Is(err, context.DeadlineExceeded): + return "timed out" + case errors.Is(err, context.Canceled): + return "canceled" + default: + return truncate(err.Error(), bodyDetailLimit) + } +} diff --git a/internal/llm/llm.go b/internal/llm/llm.go new file mode 100644 index 0000000..3da7814 --- /dev/null +++ b/internal/llm/llm.go @@ -0,0 +1,102 @@ +// Package llm abstracts HTTP LLM APIs behind one small Provider interface so +// Orchestra can dispatch tasks to a hosted model without shelling out to a +// CLI. It is deliberately dependency-free (stdlib net/http only) and +// provider-agnostic: each concrete provider is one file, and adding another is +// a matter of implementing Provider and registering it in New. +// +// Errors are typed (*Error with an ErrorKind) so callers — notably the health +// probe — can turn auth/billing/rate-limit failures into actionable messages +// instead of raw HTTP noise. +package llm + +import ( + "context" + "fmt" + "net/url" + "strings" +) + +// Message is one chat turn. +type Message struct { + Role string // "user" | "assistant" + Content string +} + +// Request is a single completion request. +type Request struct { + System string // system preamble (output contract, principles) + Messages []Message // conversation; typically one user message + MaxTokens int // hard cap on the reply; provider default when 0 + Temperature float64 // 0 = deterministic +} + +// Response is a completed completion. +type Response struct { + Text string // assistant text + StopReason string // provider-reported stop reason (informational) +} + +// Provider abstracts one HTTP LLM API. +type Provider interface { + Name() string // "openai" | "anthropic" + Complete(ctx context.Context, req Request) (Response, error) +} + +// DefaultBase returns the default API base URL for a provider name. +func DefaultBase(name string) string { + switch name { + case "anthropic": + return "https://api.anthropic.com" + default: + return "https://api.openai.com/v1" + } +} + +// DefaultKeyEnv returns the conventional environment variable holding the API +// key for a provider name. +func DefaultKeyEnv(name string) string { + switch name { + case "anthropic": + return "ANTHROPIC_API_KEY" + default: + return "OPENAI_API_KEY" + } +} + +// Known reports whether name is a registered provider. +func Known(name string) bool { + switch name { + case "openai", "anthropic": + return true + } + return false +} + +// New builds the Provider named by name (e.g. "openai", "anthropic"). apiBase +// may be empty (the provider default is used). hc may be nil (a plain client +// with sane timeouts derived from request contexts is used). model must be +// non-empty. +func New(name, apiBase, apiKey, model string, hc HTTPClient) (Provider, error) { + if !Known(name) { + return nil, fmt.Errorf("unknown llm provider %q (known: openai, anthropic)", name) + } + if strings.TrimSpace(model) == "" { + return nil, fmt.Errorf("model is required for llm provider %q", name) + } + if apiBase == "" { + apiBase = DefaultBase(name) + } + if _, err := url.Parse(apiBase); err != nil { + return nil, fmt.Errorf("invalid api_base %q for provider %q: %w", apiBase, name, err) + } + client := hc + if client == nil { + client = defaultClient() + } + switch name { + case "anthropic": + return &Anthropic{base: strings.TrimRight(apiBase, "/"), key: apiKey, model: model, hc: client}, nil + default: + return &OpenAI{base: strings.TrimRight(apiBase, "/"), key: apiKey, model: model, hc: client}, nil + } +} diff --git a/internal/llm/llm_test.go b/internal/llm/llm_test.go new file mode 100644 index 0000000..4f5befa --- /dev/null +++ b/internal/llm/llm_test.go @@ -0,0 +1,232 @@ +package llm + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +// --- factory --- + +func TestNewRejectsUnknownProvider(t *testing.T) { + if _, err := New("gemini-http", "", "", "m", nil); err == nil { + t.Fatal("expected error for unknown provider name") + } +} + +func TestNewRequiresModel(t *testing.T) { + if _, err := New("openai", "", "", " ", nil); err == nil { + t.Fatal("expected error for empty model") + } +} + +func TestNewDefaults(t *testing.T) { + o, err := New("openai", "", "k", "gpt-4o", nil) + if err != nil { + t.Fatal(err) + } + if o.(*OpenAI).base != "https://api.openai.com/v1" { + t.Fatalf("unexpected openai base: %s", o.(*OpenAI).base) + } + a, err := New("anthropic", "", "k", "claude-3", nil) + if err != nil { + t.Fatal(err) + } + if a.(*Anthropic).base != "https://api.anthropic.com" { + t.Fatalf("unexpected anthropic base: %s", a.(*Anthropic).base) + } + if DefaultKeyEnv("anthropic") != "ANTHROPIC_API_KEY" || DefaultKeyEnv("openai") != "OPENAI_API_KEY" { + t.Fatal("unexpected default key env vars") + } +} + +// --- openai provider against a fake server --- + +func TestOpenAICompleteRequestShapeAndParsing(t *testing.T) { + var gotPath string + var gotAuth string + var gotBody map[string]any + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotPath = r.URL.Path + gotAuth = r.Header.Get("Authorization") + b, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(b, &gotBody) + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"hello world"},"finish_reason":"stop"}]}`) + })) + defer srv.Close() + + p, err := New("openai", srv.URL, "sk-test", "gpt-4o", nil) + if err != nil { + t.Fatal(err) + } + resp, err := p.Complete(context.Background(), Request{ + System: "be brief", + Messages: []Message{{Role: "user", Content: "hi"}}, + MaxTokens: 100, + Temperature: 0, + }) + if err != nil { + t.Fatal(err) + } + if gotPath != "/chat/completions" { + t.Fatalf("wrong path: %s", gotPath) + } + if gotAuth != "Bearer sk-test" { + t.Fatalf("wrong auth header: %q", gotAuth) + } + msgs, _ := gotBody["messages"].([]any) + if len(msgs) != 2 { + t.Fatalf("expected system+user messages, got %d", len(msgs)) + } + first, _ := msgs[0].(map[string]any) + if first["role"] != "system" || first["content"] != "be brief" { + t.Fatalf("system message not first: %v", first) + } + if gotBody["model"] != "gpt-4o" { + t.Fatalf("model not sent: %v", gotBody["model"]) + } + if resp.Text != "hello world" || resp.StopReason != "stop" { + t.Fatalf("bad response: %+v", resp) + } + if p.Name() != "openai" { + t.Fatalf("name: %s", p.Name()) + } +} + +func TestOpenAIOmitsBearerWhenNoKey(t *testing.T) { + var auth string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + auth = r.Header.Get("Authorization") + io.WriteString(w, `{"choices":[{"message":{"content":"ok"}}]}`) + })) + defer srv.Close() + p, _ := New("openai", srv.URL, "", "m", nil) + if _, err := p.Complete(context.Background(), Request{Messages: []Message{{Role: "user", Content: "x"}}}); err != nil { + t.Fatal(err) + } + if auth != "" { + t.Fatalf("expected no Authorization header, got %q", auth) + } +} + +// --- anthropic provider against a fake server --- + +func TestAnthropicCompleteHeadersShapeAndParsing(t *testing.T) { + var gotPath, gotKey, gotVersion string + var gotBody map[string]any + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotPath = r.URL.Path + gotKey = r.Header.Get("x-api-key") + gotVersion = r.Header.Get("anthropic-version") + b, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(b, &gotBody) + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"content":[{"type":"text","text":"answer"},{"type":"other","text":"ignored"}],"stop_reason":"end_turn"}`) + })) + defer srv.Close() + + p, err := New("anthropic", srv.URL, "ak-test", "claude-3-5-sonnet", nil) + if err != nil { + t.Fatal(err) + } + resp, err := p.Complete(context.Background(), Request{ + System: "sys", + Messages: []Message{{Role: "user", Content: "q"}}, + }) + if err != nil { + t.Fatal(err) + } + if gotPath != "/v1/messages" { + t.Fatalf("wrong path: %s", gotPath) + } + if gotKey != "ak-test" || gotVersion != "2023-06-01" { + t.Fatalf("wrong headers: key=%q version=%q", gotKey, gotVersion) + } + if gotBody["system"] != "sys" { + t.Fatalf("system field: %v", gotBody["system"]) + } + mt, _ := gotBody["max_tokens"].(float64) + if mt <= 0 { + t.Fatal("anthropic requires explicit max_tokens; none sent") + } + if resp.Text != "answer" { // non-text blocks ignored + t.Fatalf("text concat wrong: %q", resp.Text) + } + if resp.StopReason != "end_turn" { + t.Fatalf("stop reason: %q", resp.StopReason) + } +} + +// --- error mapping (shared postJSON path) --- + +func TestErrorMappingByStatus(t *testing.T) { + cases := []struct { + status int + want ErrorKind + }{ + {401, ErrAuth}, {403, ErrAuth}, {402, ErrBilling}, + {429, ErrRateLimit}, {400, ErrRequest}, {404, ErrRequest}, + {500, ErrServer}, {503, ErrServer}, + } + for _, tc := range cases { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, `{"error":{"message":"boom"}}`, tc.status) + })) + p, _ := New("openai", srv.URL, "k", "m", nil) + _, err := p.Complete(context.Background(), Request{Messages: []Message{{Role: "user", Content: "x"}}}) + srv.Close() + var le *Error + if !errors.As(err, &le) { + t.Fatalf("status %d: not an *llm.Error: %v", tc.status, err) + } + if le.Kind != tc.want { + t.Fatalf("status %d: kind=%v want=%v", tc.status, le.Kind, tc.want) + } + if le.Provider != "openai" { + t.Fatalf("provider attribution missing: %+v", le) + } + if le.UserDetail() == "" { + t.Fatalf("status %d: empty UserDetail", tc.status) + } + } +} + +func TestTimeoutMapsToNetworkKind(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(300 * time.Millisecond) + io.WriteString(w, `{}`) + })) + defer srv.Close() + + p, _ := New("openai", srv.URL, "k", "m", nil) + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) + defer cancel() + _, err := p.Complete(ctx, Request{Messages: []Message{{Role: "user", Content: "x"}}}) + var le *Error + if !errors.As(err, &le) || le.Kind != ErrNetwork { + t.Fatalf("expected network-kind timeout error, got %v", err) + } +} + +func TestEmptyChoicesIsServerError(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, `{"choices":[]}`) + })) + defer srv.Close() + p, _ := New("openai", srv.URL, "k", "m", nil) + _, err := p.Complete(context.Background(), Request{Messages: []Message{{Role: "user", Content: "x"}}}) + var le *Error + if !errors.As(err, &le) || le.Kind != ErrServer { + t.Fatalf("expected server-kind error for empty choices, got %v", err) + } + if want := "no choices"; !strings.Contains(le.Error(), want) { + t.Fatalf("unhelpful message %q; want it to contain %q", le.Error(), want) + } +} diff --git a/internal/llm/openai.go b/internal/llm/openai.go new file mode 100644 index 0000000..01b1fb9 --- /dev/null +++ b/internal/llm/openai.go @@ -0,0 +1,71 @@ +package llm + +import "context" + +// OpenAI talks to the OpenAI-compatible chat completions API +// (POST {base}/chat/completions). The same wire format is spoken by many +// gateways (OpenRouter, Groq, Together, Ollama, vLLM, LM Studio), which is why +// it is the default provider. +type OpenAI struct { + base string // e.g. https://api.openai.com/v1 + key string // bearer token; may be empty for local gateways + model string + hc HTTPClient +} + +// Name implements Provider. +func (o *OpenAI) Name() string { return "openai" } + +// openaiRequest / openaiResponse model the minimal chat-completions wire shape. +type openaiRequest struct { + Model string `json:"model"` + Messages []openaiMessage `json:"messages"` + MaxTokens int `json:"max_tokens,omitempty"` + Temperature float64 `json:"temperature"` +} + +type openaiMessage struct { + Role string `json:"role"` // "system" | "user" | "assistant" + Content string `json:"content"` +} + +type openaiResponse struct { + Choices []struct { + Message openaiMessage `json:"message"` + FinishReason string `json:"finish_reason"` + } `json:"choices"` + Error *struct { + Message string `json:"message"` + Type string `json:"type"` + } `json:"error"` +} + +// Complete implements Provider. +func (o *OpenAI) Complete(ctx context.Context, req Request) (Response, error) { + msgs := make([]openaiMessage, 0, len(req.Messages)+1) + if req.System != "" { + msgs = append(msgs, openaiMessage{Role: "system", Content: req.System}) + } + for _, m := range req.Messages { + msgs = append(msgs, openaiMessage{Role: m.Role, Content: m.Content}) + } + payload := openaiRequest{Model: o.model, Messages: msgs, MaxTokens: req.MaxTokens, Temperature: req.Temperature} + + headers := map[string]string{} + if o.key != "" { + headers["Authorization"] = "Bearer " + o.key + } + + var out openaiResponse + url := o.base + "/chat/completions" + if err := postJSON(ctx, o.hc, "openai", url, headers, payload, &out); err != nil { + return Response{}, err + } + if len(out.Choices) == 0 { + return Response{}, &Error{Provider: "openai", Kind: ErrServer, Body: "response contained no choices"} + } + return Response{ + Text: out.Choices[0].Message.Content, + StopReason: out.Choices[0].FinishReason, + }, nil +} diff --git a/internal/patch/apply.go b/internal/patch/apply.go new file mode 100644 index 0000000..76c60cd --- /dev/null +++ b/internal/patch/apply.go @@ -0,0 +1,147 @@ +package patch + +import ( + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" +) + +// Apply applies p inside dir. +// +// Ordering guarantees: +// 1. Every unified diff is first validated with `git apply --check`; if any +// diff fails, nothing has been written (the tree stays byte-identical). +// 2. Every whole-file target path is validated before any write happens. +// 3. Diffs are applied, then deletions run, then creations/updates are +// written via temp-file+rename so a crash never leaves half a file. +// +// dir must be inside a git repository for diffs to apply; whole-file writes +// also work in plain directories. +func Apply(ctx context.Context, dir string, p Patch) error { + // Normalize diffs: git requires each patch to end with a newline, and + // model-extracted fences often drop it. + diffs := make([]string, len(p.Diffs)) + for i := range p.Diffs { + raw := p.Diffs[i].Raw + if strings.TrimSpace(raw) == "" { + continue + } + if !strings.HasSuffix(raw, "\n") { + raw += "\n" + } + diffs[i] = raw + } + + // 1. Dry-run every diff first: atomicity across the whole patch. + for i, raw := range diffs { + if raw == "" { + continue + } + out, err := runGit(ctx, dir, raw, "apply", "--check", "--whitespace=nowarn", "-") + if err != nil { + return fmt.Errorf("diff #%d does not apply (nothing was written): %s", i+1, oneLine(out)) + } + } + + // 2. Validate all file targets up front so a bad path can't produce a + // partial application after the diffs have already landed. + files := make([]FileWrite, len(p.Files)) + for i, f := range p.Files { + rel, ok := cleanRelPath(f.Path) + if !ok { + return fmt.Errorf("invalid file path %q", f.Path) + } + f.Path = rel + files[i] = f + } + + // 3. Apply the diffs for real. + for i, raw := range diffs { + if raw == "" { + continue + } + out, err := runGit(ctx, dir, raw, "apply", "--whitespace=nowarn", "-") + if err != nil { + return fmt.Errorf("apply diff #%d: %s", i+1, oneLine(out)) + } + } + + // 4a. Deletions first, so a write may legitimately recreate the same path. + for _, f := range files { + if !f.Delete { + continue + } + if err := os.Remove(filepath.Join(dir, filepath.FromSlash(f.Path))); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("delete %s: %w", f.Path, err) + } + } + + // 4b. Creations/updates via temp file + rename. + for _, f := range files { + if f.Delete { + continue + } + if err := writeFileAtomic(dir, f.Path, f.Content); err != nil { + return err + } + } + return nil +} + +// writeFileAtomic writes content to dir/path via a temp file in the same +// directory followed by a rename. +func writeFileAtomic(dir, relPath string, content []byte) error { + abs := filepath.Join(dir, filepath.FromSlash(relPath)) + if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil { + return fmt.Errorf("create parent dir for %s: %w", relPath, err) + } + tmp, err := os.CreateTemp(filepath.Dir(abs), ".orchestra-patch-*") + if err != nil { + return fmt.Errorf("write %s: %w", relPath, err) + } + name := tmp.Name() + defer func() { + os.Remove(name) // no-op once renamed away + }() + if _, err := tmp.Write(content); err != nil { + tmp.Close() + return fmt.Errorf("write %s: %w", relPath, err) + } + if err := tmp.Close(); err != nil { + return fmt.Errorf("write %s: %w", relPath, err) + } + if err := os.Chmod(name, 0o644); err != nil { + return fmt.Errorf("write %s: %w", relPath, err) + } + if err := os.Rename(name, abs); err != nil { + return fmt.Errorf("write %s: %w", relPath, err) + } + return nil +} + +// runGit executes git in dir with stdin wired to diffText, returning combined +// output (used for `git apply -`, which reads the diff from stdin). +func runGit(ctx context.Context, dir, diffText string, args ...string) (string, error) { + cmd := exec.CommandContext(ctx, "git", args...) + cmd.Dir = dir + cmd.Stdin = strings.NewReader(diffText) + var buf strings.Builder + cmd.Stdout = &buf + cmd.Stderr = &buf + err := cmd.Run() + return buf.String(), err +} + +// oneLine collapses output to a single short line for error messages. +func oneLine(s string) string { + s = strings.TrimSpace(strings.ReplaceAll(s, "\n", "; ")) + const max = 300 + r := []rune(s) + if len(r) > max { + return string(r[:max-1]) + "…" + } + return s +} diff --git a/internal/patch/apply_test.go b/internal/patch/apply_test.go new file mode 100644 index 0000000..e1e9acd --- /dev/null +++ b/internal/patch/apply_test.go @@ -0,0 +1,209 @@ +package patch + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// initRepo creates a clean git repo in a temp dir with one commit, mirroring +// internal/worktree's test setup. Skips the test when git is unavailable. +func initRepo(t *testing.T) string { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not on PATH") + } + dir := t.TempDir() + run := func(args ...string) { + t.Helper() + cmd := exec.Command("git", args...) + cmd.Dir = dir + cmd.Env = append(os.Environ(), + "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", + "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) + } + } + run("init", "-q") + run("config", "user.email", "t@t") + run("config", "user.name", "t") + if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("# repo\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {}\n"), 0o644); err != nil { + t.Fatal(err) + } + run("add", "-A") + run("commit", "-qm", "init") + return dir +} + +// mustClean fails when dir has uncommitted changes. +func mustClean(t *testing.T, dir string) { + t.Helper() + out, err := exec.Command("git", "-C", dir, "status", "--porcelain").CombinedOutput() + if err != nil { + t.Fatalf("git status: %v: %s", err, out) + } + if strings.TrimSpace(string(out)) != "" { + t.Fatalf("expected clean tree, got:\n%s", out) + } +} + +func readFile(t *testing.T, path string) string { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + return string(b) +} + +const modifyMainDiff = `diff --git a/main.go b/main.go +--- a/main.go ++++ b/main.go +@@ -1,3 +1,4 @@ + package main ++// touched + + func main() {} +` + +func TestApplyModifiesExistingFile(t *testing.T) { + dir := initRepo(t) + p := Patch{Diffs: []DiffText{{Raw: modifyMainDiff}}} + if err := Apply(context.Background(), dir, p); err != nil { + t.Fatal(err) + } + got := readFile(t, filepath.Join(dir, "main.go")) + if !strings.Contains(got, "// touched") { + t.Fatalf("change missing:\n%s", got) + } +} + +func TestApplyCreatesNewFile(t *testing.T) { + dir := initRepo(t) + const diff = `diff --git a/new/pkg/util.go b/new/pkg/util.go +new file mode 100644 +index 0000000..1111111 +--- /dev/null ++++ b/new/pkg/util.go +@@ -0,0 +1,3 @@ ++package util ++ ++func Hi() string { return "hi" } +` + if err := Apply(context.Background(), dir, Patch{Diffs: []DiffText{{Raw: diff}}}); err != nil { + t.Fatal(err) + } + got := readFile(t, filepath.Join(dir, "new", "pkg", "util.go")) + if !strings.Contains(got, "package util") { + t.Fatalf("unexpected content:\n%s", got) + } +} + +func TestApplyDeletesFile(t *testing.T) { + dir := initRepo(t) + const diff = `diff --git a/README.md b/README.md +deleted file mode 100644 +index 1111111..0000000 +--- a/README.md ++++ /dev/null +@@ -1 +0,0 @@ +-# repo +` + if err := Apply(context.Background(), dir, Patch{Diffs: []DiffText{{Raw: diff}}}); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dir, "README.md")); !os.IsNotExist(err) { + t.Fatal("README.md should be gone") + } +} + +func TestApplyConflictingDiffLeavesTreeUntouched(t *testing.T) { + dir := initRepo(t) + // A diff against content that doesn't exist. + const bad = `diff --git a/missing.txt b/missing.txt +--- a/missing.txt ++++ b/missing.txt +@@ -1 +0,0 @@ +-old line +` + err := Apply(context.Background(), dir, Patch{Diffs: []DiffText{{Raw: bad}}}) + if err == nil { + t.Fatal("expected conflict error") + } + if !strings.Contains(err.Error(), "nothing was written") { + t.Fatalf("error should promise atomicity: %v", err) + } + if _, serr := os.Stat(filepath.Join(dir, "missing.txt")); !os.IsNotExist(serr) { + t.Fatal("conflicting patch created a file") + } + mustClean(t, dir) +} + +func TestApplyFileWritesInPlainDir(t *testing.T) { + dir := t.TempDir() // deliberately NOT a git repo + p := Patch{Files: []FileWrite{ + {Path: "nested/dir/app.txt", Content: []byte("hello")}, + }} + if err := Apply(context.Background(), dir, p); err != nil { + t.Fatal(err) + } + if got := readFile(t, filepath.Join(dir, "nested", "dir", "app.txt")); got != "hello" { + t.Fatalf("content: %q", got) + } +} + +func TestApplyUpdateAndDeleteViaFileWrites(t *testing.T) { + dir := initRepo(t) + p := Patch{Files: []FileWrite{ + {Path: "README.md", Content: []byte("# rewritten\n")}, + {Path: "main.go", Delete: true}, + }} + if err := Apply(context.Background(), dir, p); err != nil { + t.Fatal(err) + } + if got := readFile(t, filepath.Join(dir, "README.md")); got != "# rewritten\n" { + t.Fatalf("readme: %q", got) + } + if _, err := os.Stat(filepath.Join(dir, "main.go")); !os.IsNotExist(err) { + t.Fatal("main.go should be deleted") + } + // No stray temp files left behind. + entries, _ := os.ReadDir(dir) + for _, e := range entries { + if strings.HasPrefix(e.Name(), ".orchestra-patch-") { + t.Fatalf("temp file leaked: %s", e.Name()) + } + } +} + +func TestApplyRejectsUnsafeFilePathsBeforeWriting(t *testing.T) { + dir := t.TempDir() + p := Patch{Files: []FileWrite{ + {Path: "ok.txt", Content: []byte("fine")}, + {Path: "../escape.txt", Content: []byte("bad")}, + }} + if err := Apply(context.Background(), dir, p); err == nil { + t.Fatal("expected error for traversal path") + } + if _, err := os.Stat(filepath.Join(dir, "ok.txt")); !os.IsNotExist(err) { + t.Fatal("no file should be written when any path is invalid") + } + if _, err := os.Stat(filepath.Join(t.TempDir(), "escape.txt")); err == nil { + t.Fatal("impossible") + } +} + +func TestApplyEmptyPatchIsNoop(t *testing.T) { + dir := initRepo(t) + if err := Apply(context.Background(), dir, Patch{}); err != nil { + t.Fatal(err) + } + mustClean(t, dir) +} diff --git a/internal/patch/extract.go b/internal/patch/extract.go new file mode 100644 index 0000000..e948add --- /dev/null +++ b/internal/patch/extract.go @@ -0,0 +1,175 @@ +package patch + +import ( + "path" + "strings" +) + +const deleteMarker = "" + +// Extract leniently pulls changes out of free-form model text. It understands, +// in order of preference: +// +// 1. Fenced blocks containing a unified diff (```diff … ``` or unlabeled): +// collected verbatim for `git apply`. +// +// 2. Fenced blocks whose FIRST line is a repo-relative file path and whose +// remainder is the full new file content: +// +// ```main.go +// package main +// ... +// ``` +// +// A body of exactly "" marks a deletion. +// +// 3. An unfenced unified diff pasted anywhere in the reply. +// +// It returns false when no changes could be extracted (a plain prose answer). +func Extract(text string) (Patch, bool) { + var p Patch + p.Diffs, p.Files = extractFenced(text) + if len(p.Diffs) == 0 { + if raw := extractUnfencedDiff(text); raw != "" { + p.Diffs = append(p.Diffs, DiffText{Raw: raw}) + } + } + return p, !p.Empty() +} + +// extractFenced scans ```-delimited blocks, classifying each by shape. +func extractFenced(text string) (diffs []DiffText, files []FileWrite) { + lines := strings.Split(text, "\n") + for i := 0; i < len(lines); i++ { + if !strings.HasPrefix(strings.TrimSpace(lines[i]), "```") { + continue + } + // Collect until a closing fence. + var body []string + closed := false + for j := i + 1; j < len(lines); j++ { + if strings.HasPrefix(strings.TrimSpace(lines[j]), "```") { + i = j + closed = true + break + } + body = append(body, lines[j]) + } + if !closed || len(body) == 0 { + break // unterminated fence: stop scanning, don't guess + } + content := strings.Join(body, "\n") + + switch classifyBlock(body) { + case classDiff: + diffs = append(diffs, DiffText{Raw: content}) + case classFile: + f, ok := parseFileBlock(body) + if ok { + files = append(files, f) + } + } + } + return diffs, files +} + +type blockClass int + +const ( + classOther blockClass = iota + classDiff + classFile +) + +// classifyBlock decides what a fenced block holds by its first non-empty line. +func classifyBlock(body []string) blockClass { + first := "" + for _, l := range body { + t := strings.TrimSpace(l) + if t != "" { + first = t + break + } + } + if first == "" { + return classOther + } + if looksLikeDiffStart(first) { + return classDiff + } + return classFile // tentatively; parseFileBlock may still reject it +} + +// looksLikeDiffStart recognizes the shapes a unified diff can begin with. +func looksLikeDiffStart(line string) bool { + for _, pre := range []string{"diff --git", "diff -", "--- a/", "--- /dev/null", "--- ", "Index: ", "*** ", "@@ -"} { + if strings.HasPrefix(line, pre) { + return true + } + } + return false +} + +// parseFileBlock interprets [path, ...content] bodies. +func parseFileBlock(body []string) (FileWrite, bool) { + if len(body) < 1 { + return FileWrite{}, false + } + p, ok := cleanRelPath(body[0]) + if !ok { + return FileWrite{}, false + } + rest := body[1:] + // Trim a single leading blank separator line between path and content. + if len(rest) > 0 && strings.TrimSpace(rest[0]) == "" { + rest = rest[1:] + } + content := strings.Join(rest, "\n") + if strings.TrimSpace(content) == deleteMarker { + return FileWrite{Path: p, Delete: true}, true + } + return FileWrite{Path: p, Content: []byte(content)}, true +} + +// extractUnfencedDiff grabs a raw unified diff pasted without a fence: from the +// first diff-shaped line to the end of the text (lenient — trailing prose after +// a hunk is rare, and `git apply --check` guards correctness anyway). +func extractUnfencedDiff(text string) string { + lines := strings.Split(text, "\n") + start := -1 + for i, l := range lines { + t := strings.TrimSpace(l) + if strings.HasPrefix(t, "diff --git ") || strings.HasPrefix(t, "--- a/") || strings.HasPrefix(t, "@@ -") { + start = i + break + } + } + if start < 0 { + return "" + } + return strings.Join(lines[start:], "\n") +} + +// cleanRelPath accepts only safe repo-relative slash paths and returns their +// normalized form ("./a/b.go" → "a/b.go"). Any ".." component is rejected +// outright (even when it would clean away) so model output can never climb. +func cleanRelPath(p string) (string, bool) { + p = strings.TrimSpace(p) + if p == "" || strings.ContainsAny(p, " \t\\") { + return "", false + } + p = strings.TrimPrefix(p, "./") + if path.IsAbs(p) { + return "", false + } + for _, part := range strings.Split(p, "/") { + if part == ".." { + return "", false + } + } + c := path.Clean(p) + if c == "." || c == ".." || strings.HasPrefix(c, "../") { + return "", false + } + return c, true +} diff --git a/internal/patch/extract_test.go b/internal/patch/extract_test.go new file mode 100644 index 0000000..94d6087 --- /dev/null +++ b/internal/patch/extract_test.go @@ -0,0 +1,169 @@ +package patch + +import ( + "strings" + "testing" +) + +const sampleDiff = `diff --git a/main.go b/main.go +index 1111111..2222222 100644 +--- a/main.go ++++ b/main.go +@@ -1,3 +1,4 @@ + package main ++ + func main() {} +` + +func TestExtractFencedDiff(t *testing.T) { + text := "Here is my change:\n\n```diff\n" + sampleDiff + "```\n\nHope that helps." + p, ok := Extract(text) + if !ok { + t.Fatal("expected a patch") + } + if len(p.Diffs) != 1 || p.Diffs[0].Raw != strings.TrimSuffix(sampleDiff, "\n") { + t.Fatalf("unexpected diffs: %+v", p.Diffs) + } + if len(p.Files) != 0 { + t.Fatalf("unexpected files: %+v", p.Files) + } +} + +func TestExtractUnlabeledFenceWithDiff(t *testing.T) { + text := "```\n" + sampleDiff + "```" + p, ok := Extract(text) + if !ok || len(p.Diffs) != 1 { + t.Fatalf("got ok=%v diffs=%d", ok, len(p.Diffs)) + } +} + +func TestExtractMultipleFences(t *testing.T) { + text := "```go\nmain.go\npackage main\n```\ntext between\n```diff\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n```" + p, ok := Extract(text) + if !ok { + t.Fatal("expected patch") + } + if len(p.Files) != 1 || p.Files[0].Path != "main.go" { + t.Fatalf("files: %+v", p.Files) + } + if string(p.Files[0].Content) != "package main" { + t.Fatalf("content: %q", p.Files[0].Content) + } + if len(p.Diffs) != 1 || !strings.HasPrefix(p.Diffs[0].Raw, "diff --git") { + t.Fatalf("diffs: %+v", p.Diffs) + } +} + +func TestExtractFullFileBlocksWithBlankSeparator(t *testing.T) { + text := "```\ninternal/app/app.go\n\npackage app\n\nfunc A() {}\n```" + p, ok := Extract(text) + if !ok { + t.Fatal("expected patch") + } + f := p.Files[0] + if f.Path != "internal/app/app.go" { + t.Fatalf("path: %q", f.Path) + } + if string(f.Content) != "package app\n\nfunc A() {}" { + t.Fatalf("content: %q", f.Content) + } +} + +func TestExtractDeleteMarker(t *testing.T) { + text := "```\nold/legacy.txt\n\n```" + p, ok := Extract(text) + if !ok { + t.Fatal("expected patch") + } + f := p.Files[0] + if !f.Delete || f.Path != "old/legacy.txt" { + t.Fatalf("file: %+v", f) + } + if p.Empty() { + t.Fatal("a deletion is content; patch must not report Empty") + } +} + +func TestExtractProseOnlyReturnsFalse(t *testing.T) { + text := "I could not figure out which file you mean. Could you clarify?" + if _, ok := Extract(text); ok { + t.Fatal("expected no patch from prose") + } +} + +func TestExtractEmpty(t *testing.T) { + for _, s := range []string{"", "```\n```", "just words"} { + if _, ok := Extract(s); ok { + t.Fatalf("expected no patch for %q", s) + } + } +} + +func TestExtractRejectsUnsafePaths(t *testing.T) { + for _, bad := range []string{"../evil.txt", "/abs/path.txt", "a b.txt", "", ".."} { + text := "```\n" + bad + "\ncontent\n```" + if p, ok := Extract(text); ok { + t.Fatalf("accepted unsafe path %q: %+v", bad, p.Files) + } + } +} + +func TestExtractNormalizesDotSlash(t *testing.T) { + text := "```\n./cmd/a/main.go\npackage main\n```" + p, _ := Extract(text) + if len(p.Files) != 1 || p.Files[0].Path != "cmd/a/main.go" { + t.Fatalf("files: %+v", p.Files) + } +} + +func TestExtractUnterminatedFenceFallsBackToRawDiff(t *testing.T) { + // A truncated reply inside an unterminated fence still exposes its + // diff-shaped content through the unfenced fallback; if it's corrupt, + // Apply rejects it safely via git apply --check. + text := "```diff\ndiff --git a/x b/x" + p, ok := Extract(text) + if !ok || len(p.Diffs) != 1 || !strings.HasPrefix(p.Diffs[0].Raw, "diff --git") { + t.Fatalf("expected fallback capture, got ok=%v %+v", ok, p) + } + if len(p.Files) != 0 { + t.Fatalf("unexpected files: %+v", p.Files) + } +} + +func TestCleanRelPath(t *testing.T) { + cases := []struct { + in string + want string + ok bool + }{ + {"a/b.go", "a/b.go", true}, + {"./a/b.go", "a/b.go", true}, + {"a/../b.go", "", false}, // contains traversal after clean + {"../x", "", false}, + {"/etc/passwd", "", false}, + {"", "", false}, + {"with space.txt", "", false}, + {"back\\slash.txt", "", false}, + } + for _, tc := range cases { + got, ok := cleanRelPath(tc.in) + if ok != tc.ok || (ok && got != tc.want) { + t.Errorf("cleanRelPath(%q) = %q,%v want %q,%v", tc.in, got, ok, tc.want, tc.ok) + } + } +} + +func TestPatchEmpty(t *testing.T) { + var p Patch + if !p.Empty() { + t.Fatal("zero patch should be empty") + } + p.Files = append(p.Files, FileWrite{Path: "x", Content: []byte("y")}) + if p.Empty() { + t.Fatal("patch with files should not be empty") + } + p2 := Patch{Diffs: []DiffText{{Raw: "diff --git a/x b/x"}}} + if p2.Empty() { + t.Fatal("patch with diffs should not be empty") + } +} diff --git a/internal/patch/patch.go b/internal/patch/patch.go new file mode 100644 index 0000000..68aa65f --- /dev/null +++ b/internal/patch/patch.go @@ -0,0 +1,31 @@ +// Package patch turns free-form model output into changes on disk. An +// API-backed agent cannot edit files itself, so it describes them — as a +// unified diff and/or whole-file blocks — and this package extracts those +// changes leniently (models add prose around the payload) and applies them +// safely inside the working tree. +// +// Safety rules: +// - Unified diffs are validated with `git apply --check` before anything is +// written, so a conflicting diff leaves the tree byte-identical. +// - Whole-file writes happen only after every target path has been validated; +// paths outside the working directory are rejected. +package patch + +// FileWrite is one whole-file create/update/delete. +type FileWrite struct { + Path string // repo-relative slash path, cleaned + Content []byte // ignored when Delete + Delete bool +} + +// DiffText is one raw unified diff to apply with git. +type DiffText struct{ Raw string } + +// Patch is everything extractable from one model reply. +type Patch struct { + Diffs []DiffText + Files []FileWrite +} + +// Empty reports whether the patch would change nothing. +func (p Patch) Empty() bool { return len(p.Diffs) == 0 && len(p.Files) == 0 } diff --git a/internal/shell/shell.go b/internal/shell/shell.go index adbcdf6..eab4a2f 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -55,30 +55,44 @@ func New(reg *agent.Registry, dir, defaultAgent string, stages []validate.Stage, } } -// Run drives the read-eval loop until EOF or /exit. +// Run drives the read-eval loop until EOF, /exit, or context cancellation (Ctrl+C). func (s *Shell) Run(ctx context.Context) error { s.banner() + type lineResult struct { + line string + err error + } for { fmt.Printf("\n%s %s ", ui.Accent("orchestra")+ui.Dim(" ("+s.promptTag()+")"), ui.Accent2("›")) - line, err := s.in.ReadString('\n') - if err == io.EOF { + ch := make(chan lineResult, 1) + go func() { + line, err := s.in.ReadString('\n') + ch <- lineResult{line, err} + }() + select { + case <-ctx.Done(): fmt.Println("\nbye 👋") return nil - } - if err != nil { - return err - } - line = strings.TrimSpace(line) - if line == "" { - continue - } - if strings.HasPrefix(line, "/") { - if s.command(line) { // returns true to exit + case res := <-ch: + if res.err == io.EOF { + fmt.Println("\nbye 👋") return nil } - continue + if res.err != nil { + return res.err + } + line := strings.TrimSpace(res.line) + if line == "" { + continue + } + if strings.HasPrefix(line, "/") { + if s.command(line) { + return nil + } + continue + } + s.handle(ctx, line) } - s.handle(ctx, line) } } diff --git a/internal/tui/highlight.go b/internal/tui/highlight.go index f70fe18..62e0152 100644 --- a/internal/tui/highlight.go +++ b/internal/tui/highlight.go @@ -2,11 +2,14 @@ package tui import ( "bytes" + "fmt" + "strings" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/formatters" "github.com/alecthomas/chroma/v2/lexers" "github.com/alecthomas/chroma/v2/styles" + "github.com/sergi/go-diff/diffmatchpatch" ) // diffStyle is chosen once (fallback-safe) for a consistent dark look. @@ -41,3 +44,57 @@ func highlightDiff(src string) string { } return buf.String() } + +// parseDiffFiles extracts file names from a unified diff string. +func parseDiffFiles(diff string) []string { + var files []string + seen := map[string]bool{} + for _, line := range strings.Split(diff, "\n") { + if strings.HasPrefix(line, "diff --git ") { + parts := strings.Split(line, " b/") + if len(parts) == 2 { + name := parts[1] + if !seen[name] { + seen[name] = true + files = append(files, name) + } + } + } + } + return files +} + +// diffStats returns a summary line like "+12 -5" from a unified diff. +func diffStats(diff string) string { + dmp := diffmatchpatch.New() + lines := strings.Split(diff, "\n") + var a, b []string + for _, line := range lines { + switch { + case strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++"): + b = append(b, line[1:]) + case strings.HasPrefix(line, "-") && !strings.HasPrefix(line, "---"): + a = append(a, line[1:]) + } + } + diffs := dmp.DiffMain(strings.Join(a, "\n"), strings.Join(b, "\n"), false) + added, removed := 0, 0 + for _, d := range diffs { + switch d.Type { + case diffmatchpatch.DiffInsert: + added += strings.Count(d.Text, "\n") + if !strings.HasSuffix(d.Text, "\n") { + added++ + } + case diffmatchpatch.DiffDelete: + removed += strings.Count(d.Text, "\n") + if !strings.HasSuffix(d.Text, "\n") { + removed++ + } + } + } + if added == 0 && removed == 0 { + return "no changes" + } + return okSty.Render(fmt.Sprintf("+%d", added)) + " " + badSty.Render(fmt.Sprintf("-%d", removed)) +} diff --git a/internal/tui/tui.go b/internal/tui/tui.go index f47a8a9..fe7e7d0 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -2,8 +2,8 @@ // agents (with live health probing), run history, benchmark results, and an // in-pane Chat tab. Chat runs the agent quietly in the background (spinner while // it works), then shows the diff in a scrollable pane for accept/reject right in -// the dashboard. The transcript and input use bubbles' viewport/textinput so -// redraw and scrolling are handled correctly. +// the dashboard. The transcript uses bubbles' viewport and the input uses +// bubbles' textarea (multi-line) so redraw and scrolling are handled correctly. package tui import ( @@ -13,7 +13,7 @@ import ( "sync" "time" - "github.com/charmbracelet/bubbles/textinput" + "github.com/charmbracelet/bubbles/textarea" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -25,19 +25,22 @@ import ( "github.com/crossben/orchestra-code/internal/memory" "github.com/crossben/orchestra-code/internal/router" "github.com/crossben/orchestra-code/internal/scheduler" + "github.com/crossben/orchestra-code/internal/ui" "github.com/crossben/orchestra-code/internal/validate" ) type tab int const ( - tabAgents tab = iota + tabLogs tab = iota + tabAgents tabHistory tabBench + tabChanges tabChat ) -var tabNames = []string{"Agents", "History", "Benchmarks", "Chat"} +var tabNames = []string{"Logs", "Agents", "History", "Benchmarks", "Changes", "Chat"} type chatState int @@ -65,8 +68,6 @@ var ( footerSty = lipgloss.NewStyle().Foreground(gray) promptSty = lipgloss.NewStyle().Foreground(accent).Bold(true) youSty = lipgloss.NewStyle().Foreground(accent2).Bold(true) - addSty = lipgloss.NewStyle().Foreground(green) - delSty = lipgloss.NewStyle().Foreground(red) ) var spinnerFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} @@ -92,6 +93,20 @@ type chatLine struct { text string } +type logEntry struct { + time time.Time + kind string // "info" | "error" | "turn" | "route" | "probe" + text string +} + +type changeEntry struct { + time time.Time + agent string + task string + files []string // extracted from diff + diff string // full unified diff +} + // Model is the dashboard state. type Model struct { d Deps @@ -106,30 +121,43 @@ type Model struct { probed map[string]agent.ProbeResult // chat - ti textinput.Model + ta textarea.Model vp viewport.Model cstate chatState messages []chatLine pending engine.Turn frame int + // logs + logs []logEntry + + // changes + changes []changeEntry + selectedChange int // index into changes for browsing + showingDiff bool // true = showing diff for selected change + status string } // New builds the dashboard model. func New(d Deps) Model { - ti := textinput.New() - ti.Placeholder = "ask the agent to do something…" - ti.Prompt = promptSty.Render("› ") - ti.Focus() - ti.CharLimit = 0 + ta := textarea.New() + ta.Placeholder = "ask the agent to do something…" + ta.Prompt = promptSty.Render("┃ ") + ta.ShowLineNumbers = false + ta.CharLimit = 0 + ta.SetHeight(4) + ta.MaxHeight = 8 + ta.FocusedStyle.CursorLine = lipgloss.NewStyle() + ta.KeyMap.InsertNewline.SetEnabled(false) // enter → newline disabled; ctrl+enter for submit handled in updateChat + ta.Focus() m := Model{ d: d, width: 80, height: 24, probed: map[string]agent.ProbeResult{}, - ti: ti, + ta: ta, vp: viewport.New(80, 12), } m.reload() @@ -149,7 +177,7 @@ func (m *Model) reload() { } } -func (m Model) Init() tea.Cmd { return textinput.Blink } +func (m Model) Init() tea.Cmd { return nil } // --- messages --- @@ -165,8 +193,7 @@ func tickCmd() tea.Cmd { } var ( - errNotRepo = fmt.Errorf("not a git repository — chat needs one for the supervised loop") - errDirty = fmt.Errorf("working tree has uncommitted changes — commit/stash first") + errDirty = fmt.Errorf("working tree has uncommitted changes — commit/stash first") ) func (m Model) probeCmd() tea.Cmd { @@ -199,11 +226,13 @@ func (m Model) probeCmd() tea.Cmd { func (m Model) produceCmd(text string) tea.Cmd { d := m.d return func() tea.Msg { - if !gitutil.IsRepo(d.Dir) { - return turnMsg{turn: engine.Turn{Err: errNotRepo}} - } - if clean, _ := gitutil.IsClean(d.Dir); !clean { - return turnMsg{turn: engine.Turn{Err: errDirty}} + // Inside a git repo the tree must start clean (reject uses git + // restore). Plain directories are always allowed: the engine + // snapshots them and restores from that snapshot on reject. + if gitutil.IsRepo(d.Dir) { + if clean, _ := gitutil.IsClean(d.Dir); !clean { + return turnMsg{turn: engine.Turn{Err: errDirty}} + } } agentName := d.DefaultAgent @@ -245,6 +274,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case probeDoneMsg: m.probed = msg.results m.probing = false + m.logEvent("probe", fmt.Sprintf("probed %d agents", len(msg.results))) m.status = "probe complete" return m, nil case tickMsg: @@ -256,27 +286,53 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case turnMsg: return m.onTurn(msg.turn, msg.note) case tea.KeyMsg: + if msg.String() == "ctrl+c" { + return m, tea.Quit + } if m.active == tabChat { return m.updateChat(msg) } + prev := m.active switch msg.String() { - case "q", "ctrl+c", "esc": + case "q", "ctrl+c": + return m, tea.Quit + case "esc": + if m.active == tabChanges && m.showingDiff { + m.showingDiff = false + return m, nil + } return m, tea.Quit case "tab", "right", "l": - m.active = (m.active + 1) % 4 + m.active = (m.active + 1) % 6 case "shift+tab", "left", "h": - m.active = (m.active + 3) % 4 + m.active = (m.active + 5) % 6 case "1": - m.active = tabAgents + m.active = tabLogs case "2": - m.active = tabHistory + m.active = tabAgents case "3": - m.active = tabBench + m.active = tabHistory case "4": + m.active = tabBench + case "5": + m.active = tabChanges + case "6": m.active = tabChat case "r": m.reload() m.status = "refreshed" + case "j": + if m.active == tabChanges && !m.showingDiff && m.selectedChange < len(m.changes)-1 { + m.selectedChange++ + } + case "k": + if m.active == tabChanges && !m.showingDiff && m.selectedChange > 0 { + m.selectedChange-- + } + case "enter": + if m.active == tabChanges && !m.showingDiff && len(m.changes) > 0 { + m.showingDiff = true + } case "p": if m.active == tabAgents && !m.probing { m.probing = true @@ -284,28 +340,45 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, m.probeCmd() } } + if m.active != prev { + if prev == tabChat { + m.ta.Blur() + m.vp.SetContent("") // clear viewport to prevent stale chat leaking + } + if m.active == tabChat { + m.ta.Focus() + } + m.layout() + if m.active == tabChat { + m.setChatContent() + } + } return m, nil } // Non-key messages (e.g. cursor blink) go to the input. if m.active == tabChat { var cmd tea.Cmd - m.ti, cmd = m.ti.Update(msg) + m.ta, cmd = m.ta.Update(msg) return m, cmd } return m, nil } -// updateChat: tab/shift+tab always navigate; ctrl+c quits; otherwise route to +// updateChat: tab/shift+tab always navigate; otherwise route to // the input (idle), the review keys (reviewing), or scroll (running). func (m Model) updateChat(msg tea.KeyMsg) (tea.Model, tea.Cmd) { switch msg.String() { - case "ctrl+c": - return m, tea.Quit case "tab": - m.active = (m.active + 1) % 4 + m.ta.Blur() + m.vp.SetContent("") // clear viewport to prevent stale content leaking + m.active = (m.active + 1) % 6 + m.layout() return m, nil case "shift+tab": - m.active = (m.active + 3) % 4 + m.ta.Blur() + m.vp.SetContent("") // clear viewport to prevent stale content leaking + m.active = (m.active + 5) % 6 + m.layout() return m, nil } @@ -333,22 +406,22 @@ func (m Model) updateChat(msg tea.KeyMsg) (tea.Model, tea.Cmd) { return m, nil case "enter": return m.submitChat() - case "pgup", "pgdown", "ctrl+u", "ctrl+d": + case "pgup", "pgdown": var cmd tea.Cmd m.vp, cmd = m.vp.Update(msg) return m, cmd } var cmd tea.Cmd - m.ti, cmd = m.ti.Update(msg) + m.ta, cmd = m.ta.Update(msg) return m, cmd } func (m Model) submitChat() (tea.Model, tea.Cmd) { - text := strings.TrimSpace(m.ti.Value()) + text := strings.TrimSpace(m.ta.Value()) if text == "" { return m, nil } - m.ti.Reset() + m.ta.Reset() m.messages = append(m.messages, chatLine{"you", text}) m.cstate = chatRunning m.frame = 0 @@ -360,10 +433,12 @@ func (m Model) submitChat() (tea.Model, tea.Cmd) { func (m Model) onTurn(t engine.Turn, note string) (tea.Model, tea.Cmd) { if note != "" { m.messages = append(m.messages, chatLine{"sys", note}) + m.logEvent("route", note) } switch { case t.Err != nil: m.messages = append(m.messages, chatLine{"sys", "error: " + t.Err.Error()}) + m.logEvent("error", t.Err.Error()) m.cstate = chatIdle case !t.HadChanges: resp := strings.TrimSpace(t.AgentText) @@ -371,9 +446,14 @@ func (m Model) onTurn(t engine.Turn, note string) (tea.Model, tea.Cmd) { resp = "(the agent made no file changes)" } m.messages = append(m.messages, chatLine{"agent", resp}) + m.logEvent("turn", "agent replied ("+fmt.Sprintf("%d", len(resp))+" chars)") + ui.Notify("Orchestra", "Agent finished — "+firstLine(resp)) m.cstate = chatIdle default: m.pending = t + m.recordChange(t) + m.logEvent("turn", "agent produced changes — review required") + ui.Notify("Orchestra", "Agent produced changes — review required") m.cstate = chatReviewing } m.setChatContent() @@ -382,10 +462,14 @@ func (m Model) onTurn(t engine.Turn, note string) (tea.Model, tea.Cmd) { func (m Model) accept() (tea.Model, tea.Cmd) { last := lastUserMsg(m.messages) - if err := gitutil.Commit(m.d.Dir, "orchestra: "+firstLine(last)); err != nil { + committed, err := engine.CommitAccepted(m.d.Dir, "orchestra: "+firstLine(last)) + switch { + case err != nil: m.messages = append(m.messages, chatLine{"sys", "commit failed: " + err.Error()}) - } else { + case committed: m.messages = append(m.messages, chatLine{"sys", "✓ accepted & committed"}) + default: + m.messages = append(m.messages, chatLine{"sys", "✓ accepted — kept as plain files (not a git repo)"}) } m.cstate = chatIdle m.pending = engine.Turn{} @@ -395,7 +479,7 @@ func (m Model) accept() (tea.Model, tea.Cmd) { } func (m Model) reject() (tea.Model, tea.Cmd) { - if err := gitutil.Restore(m.d.Dir); err != nil { + if err := engine.Revert(m.d.Dir, m.pending.Baseline); err != nil { m.messages = append(m.messages, chatLine{"sys", "restore failed: " + err.Error()}) } else { m.messages = append(m.messages, chatLine{"sys", "↺ rejected & reverted"}) @@ -406,16 +490,48 @@ func (m Model) reject() (tea.Model, tea.Cmd) { return m, nil } +func (m *Model) logEvent(kind, text string) { + m.logs = append(m.logs, logEntry{time: time.Now(), kind: kind, text: text}) + if len(m.logs) > 200 { + m.logs = m.logs[len(m.logs)-200:] + } +} + +func (m *Model) recordChange(t engine.Turn) { + task := "" + for i := len(m.messages) - 1; i >= 0; i-- { + if m.messages[i].role == "you" { + task = m.messages[i].text + break + } + } + agent := m.d.DefaultAgent + m.changes = append(m.changes, changeEntry{ + time: time.Now(), + agent: agent, + task: task, + files: parseDiffFiles(t.Diff), + diff: t.Diff, + }) + if len(m.changes) > 100 { + m.changes = m.changes[len(m.changes)-100:] + } + m.selectedChange = len(m.changes) - 1 +} + // layout sizes the viewport and input to the window. func (m *Model) layout() { - w := min(m.width, 100) - vpH := m.height - 9 + w := m.width + vpH := m.height - 5 // header(2) + footer(2) + padding(1) + if m.active == tabChat { + vpH -= 6 // textarea(4) + gap(2) + } if vpH < 3 { vpH = 3 } m.vp.Width = w m.vp.Height = vpH - m.ti.Width = w - 4 + m.ta.SetWidth(w) } // setChatContent refreshes the viewport with the transcript or the diff. @@ -442,11 +558,18 @@ func (m Model) renderTranscript() string { } switch msg.role { case "you": - b.WriteString(youSty.Render("you") + "\n" + wrap.Render(msg.text) + "\n") + b.WriteString(youSty.Render("you")) + b.WriteString("\n") + b.WriteString(wrap.Render(msg.text)) + b.WriteString("\n") case "agent": - b.WriteString(headSty.Render("agent") + "\n" + renderMarkdown(msg.text, max(m.vp.Width-1, 20)) + "\n") + b.WriteString(headSty.Render("agent")) + b.WriteString("\n") + b.WriteString(renderMarkdown(msg.text, max(m.vp.Width-1, 20))) + b.WriteString("\n") case "sys": - b.WriteString(dimSty.Render("· "+msg.text) + "\n") + b.WriteString(dimSty.Render("· " + msg.text)) + b.WriteString("\n") } } return b.String() @@ -456,11 +579,14 @@ func (m Model) renderDiff() string { var b strings.Builder switch { case m.pending.Report.Skipped: - b.WriteString(dimSty.Render("validation: skipped") + "\n\n") + b.WriteString(dimSty.Render("validation: skipped")) + b.WriteString("\n\n") case m.pending.Report.Passed(): - b.WriteString(okSty.Render("validation: ✓ passed") + "\n\n") + b.WriteString(okSty.Render("validation: ✓ passed")) + b.WriteString("\n\n") default: - b.WriteString(badSty.Render("validation: ✗ failed") + "\n\n") + b.WriteString(badSty.Render("validation: ✗ failed")) + b.WriteString("\n\n") } b.WriteString(highlightDiff(m.pending.Diff)) // chroma diff highlighting return b.String() @@ -480,12 +606,28 @@ func (m Model) View() string { b.WriteString(m.historyView()) case tabBench: b.WriteString(m.benchView()) + case tabChanges: + b.WriteString(m.changesView()) case tabChat: b.WriteString(m.chatView()) + case tabLogs: + b.WriteString(m.logsView()) } b.WriteString("\n") b.WriteString(m.footer()) - return b.String() + return padHeight(b.String(), m.height) +} + +// padHeight pads a string to exactly n lines, preventing stale content from leaking. +func padHeight(s string, n int) string { + lines := strings.Split(s, "\n") + if len(lines) >= n { + return strings.Join(lines[:n], "\n") + } + for len(lines) < n { + lines = append(lines, "") + } + return strings.Join(lines, "\n") } func (m Model) header() string { @@ -498,7 +640,7 @@ func (m Model) header() string { tabs = append(tabs, tabOff.Render(fmt.Sprintf("%d %s", i+1, name))) } } - return title + " " + strings.Join(tabs, " ") + "\n" + dimSty.Render(strings.Repeat("─", min(m.width, 100))) + return title + " " + strings.Join(tabs, " ") + "\n" + dimSty.Render(strings.Repeat("─", m.width)) } func (m Model) footer() string { @@ -515,6 +657,12 @@ func (m Model) footer() string { default: keys = "enter: send • ↑/↓ pgup/pgdn: scroll • tab: switch • esc: leave" } + case tabChanges: + if m.showingDiff { + keys = "esc: back to list • tab: switch • q: quit" + } else { + keys = "j/k: navigate • enter: view diff • tab: switch • q: quit" + } default: keys = "tab: switch • r: refresh • q: quit" } @@ -522,7 +670,7 @@ func (m Model) footer() string { if m.status != "" { status = " " + m.status } - return footerSty.Render(strings.Repeat("─", min(m.width, 100)) + "\n" + keys + status) + return footerSty.Render(strings.Repeat("─", m.width) + "\n" + keys + status) } func (m Model) chatView() string { @@ -538,14 +686,123 @@ func (m Model) chatView() string { case chatReviewing: bottom = promptSty.Render("accept these changes?") + " [y]es / [n]o" default: - bottom = m.ti.View() + bottom = m.ta.View() } return head + "\n\n" + m.vp.View() + "\n\n" + bottom } +func (m Model) logsView() string { + if len(m.logs) == 0 { + return dimSty.Render("no events yet — actions in Chat and Agents tabs are logged here") + } + var b strings.Builder + b.WriteString(headSty.Render(fmt.Sprintf("%-20s %-8s %s", "TIME", "KIND", "MESSAGE"))) + b.WriteString("\n") + // Show newest first, fit to screen + start := 0 + avail := m.height - 5 // header + footer + padding + if avail < 1 { + avail = 1 + } + if len(m.logs) > avail { + start = len(m.logs) - avail + } + for i := start; i < len(m.logs); i++ { + e := m.logs[i] + kind := dimSty.Render(e.kind) + switch e.kind { + case "error": + kind = badSty.Render(e.kind) + case "turn": + kind = okSty.Render(e.kind) + case "route": + kind = youSty.Render(e.kind) + } + b.WriteString(fmt.Sprintf("%-20s %-8s %s\n", + e.time.Local().Format("01-02 15:04:05"), kind, truncate(e.text, m.width-32))) + } + return b.String() +} + +func (m Model) changesView() string { + if len(m.changes) == 0 { + return dimSty.Render("no file changes yet — send a task in Chat to see changes here") + } + // Diff view + if m.showingDiff && m.selectedChange >= 0 && m.selectedChange < len(m.changes) { + c := m.changes[m.selectedChange] + var b strings.Builder + b.WriteString(headSty.Render(fmt.Sprintf("Change %d/%d", m.selectedChange+1, len(m.changes)))) + b.WriteString(dimSty.Render(fmt.Sprintf(" %s %s %s", + c.time.Local().Format("01-02 15:04:05"), + youSty.Render(c.agent), + truncate(c.task, m.width-50)))) + b.WriteString("\n") + if len(c.files) > 0 { + b.WriteString(dimSty.Render("files: " + strings.Join(c.files, ", "))) + b.WriteString("\n") + } + b.WriteString("\n") + vpH := m.height - 8 + if vpH < 3 { + vpH = 3 + } + w := m.vp.Width + if w < 20 { + w = 20 + } + diff := highlightDiff(c.diff) + lines := strings.Split(diff, "\n") + start := 0 + if len(lines) > vpH { + start = len(lines) - vpH + } + for _, line := range lines[start:] { + b.WriteString(line) + b.WriteString("\n") + } + b.WriteString("\n") + b.WriteString(dimSty.Render("j/k: navigate • esc: back to list")) + return b.String() + } + // List view + var b strings.Builder + b.WriteString(headSty.Render(fmt.Sprintf("%-20s %-10s %-30s %s", "TIME", "AGENT", "TASK", "FILES"))) + b.WriteString("\n") + start := 0 + avail := m.height - 5 + if avail < 1 { + avail = 1 + } + if len(m.changes) > avail { + start = len(m.changes) - avail + } + for i := start; i < len(m.changes); i++ { + c := m.changes[i] + marker := " " + if i == m.selectedChange { + marker = okSty.Render("▸ ") + } + files := strings.Join(c.files, ", ") + if len(c.files) > 3 { + files = strings.Join(c.files[:3], ", ") + fmt.Sprintf(" +%d", len(c.files)-3) + } + b.WriteString(fmt.Sprintf("%s%-20s %-10s %-30s %s\n", + marker, + c.time.Local().Format("01-02 15:04:05"), + dimSty.Render(c.agent), + truncate(c.task, 30), + dimSty.Render(truncate(files, m.width-64)))) + } + b.WriteString("\n") + b.WriteString(dimSty.Render("j/k: navigate • enter: view diff • r: refresh")) + return b.String() +} + func (m Model) agentsView() string { var b strings.Builder - b.WriteString(headSty.Render(fmt.Sprintf("%-12s %-14s %-22s %s", "AGENT", "INSTALLED", "PROBE", "CAPABILITIES")) + "\n") + b.WriteString(headSty.Render(fmt.Sprintf("%-12s %-14s %-22s %s", "AGENT", "INSTALLED", "PROBE", "CAPABILITIES"))) + b.WriteString("\n") for _, a := range m.d.Reg.All() { installed := okSty.Render("✓") if a.Health() != nil { @@ -568,7 +825,8 @@ func (m Model) agentsView() string { } b.WriteString(fmt.Sprintf("%-12s %-14s %-22s %s\n", name, installed, probe, dimSty.Render(caps(a)))) } - b.WriteString("\n" + dimSty.Render("* default agent · press p to live-probe whether each agent can actually run")) + b.WriteString("\n") + b.WriteString(dimSty.Render("* default agent · press p to live-probe whether each agent can actually run")) return b.String() } @@ -577,7 +835,8 @@ func (m Model) historyView() string { return dimSty.Render("no run history yet — try the Chat tab, `orchestra run`, or `orchestra do`") } var b strings.Builder - b.WriteString(headSty.Render(fmt.Sprintf("%-17s %-10s %-10s %-4s %s", "WHEN", "AGENT", "OUTCOME", "ATT", "TASK")) + "\n") + b.WriteString(headSty.Render(fmt.Sprintf("%-17s %-10s %-10s %-4s %s", "WHEN", "AGENT", "OUTCOME", "ATT", "TASK"))) + b.WriteString("\n") for _, r := range m.runs { if m.linesShown(&b) { break @@ -600,7 +859,8 @@ func (m Model) benchView() string { return dimSty.Render("no benchmarks yet — try `orchestra benchmark \"\"`") } var b strings.Builder - b.WriteString(headSty.Render(fmt.Sprintf("%-17s %-10s %-6s %-6s %-8s %s", "WHEN", "AGENT", "WON", "VALID", "TIME", "TASK")) + "\n") + b.WriteString(headSty.Render(fmt.Sprintf("%-17s %-10s %-6s %-6s %-8s %s", "WHEN", "AGENT", "WON", "VALID", "TIME", "TASK"))) + b.WriteString("\n") for _, r := range m.benches { if m.linesShown(&b) { break @@ -675,13 +935,6 @@ func truncate(s string, n int) string { return s } -func min(a, b int) int { - if a < b { - return a - } - return b -} - func max(a, b int) int { if a > b { return a diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go index 52874a7..c6abb4c 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -28,6 +28,7 @@ func key(s string) tea.KeyMsg { func TestViewRendersAgentsTab(t *testing.T) { m := testModel() + m.active = tabAgents view := m.View() for _, want := range []string{"ORCHESTRA", "Agents", "History", "Benchmarks", "AGENT", "alpha", "beta", "quit"} { if !strings.Contains(view, want) { @@ -38,8 +39,8 @@ func TestViewRendersAgentsTab(t *testing.T) { func TestTabSwitching(t *testing.T) { m := testModel() - // switch to History (key "2") - nm, _ := m.Update(key("2")) + // switch to History (key "3") + nm, _ := m.Update(key("3")) m = nm.(Model) if m.active != tabHistory { t.Fatalf("expected History tab, got %d", m.active) @@ -87,13 +88,13 @@ func TestChatTabTypingAndBackspace(t *testing.T) { m = nm.(Model) nm, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x")}) m = nm.(Model) - if m.ti.Value() != "hi x" { - t.Fatalf("expected input %q, got %q", "hi x", m.ti.Value()) + if m.ta.Value() != "hi x" { + t.Fatalf("expected input %q, got %q", "hi x", m.ta.Value()) } nm, _ = m.Update(tea.KeyMsg{Type: tea.KeyBackspace}) m = nm.(Model) - if m.ti.Value() != "hi " { - t.Fatalf("backspace failed, got %q", m.ti.Value()) + if m.ta.Value() != "hi " { + t.Fatalf("backspace failed, got %q", m.ta.Value()) } if !strings.Contains(m.View(), "Chat") { t.Fatal("chat view should render") @@ -113,7 +114,7 @@ func TestChatEscLeavesTab(t *testing.T) { func TestChatEmptySubmitNoop(t *testing.T) { m := testModel() m.active = tabChat - _, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) // empty input + _, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) // empty input = noop if cmd != nil { t.Fatal("submitting empty input should not launch a command") } @@ -121,10 +122,10 @@ func TestChatEmptySubmitNoop(t *testing.T) { func TestChatTabInNav(t *testing.T) { m := testModel() - nm, _ := m.Update(key("4")) + nm, _ := m.Update(key("6")) m = nm.(Model) if m.active != tabChat { - t.Fatalf("key 4 should select Chat, got %d", m.active) + t.Fatalf("key 6 should select Chat, got %d", m.active) } } @@ -139,15 +140,15 @@ func TestChatTabNavigatesWhileTyping(t *testing.T) { if m.active == tabChat { t.Fatal("tab should navigate out of chat while typing") } - if m.ti.Value() != "hello" { - t.Fatalf("input should be preserved on nav, got %q", m.ti.Value()) + if m.ta.Value() != "hello" { + t.Fatalf("input should be preserved on nav, got %q", m.ta.Value()) } } func TestChatSubmitEntersRunning(t *testing.T) { m := testModel() m.active = tabChat - m.ti.SetValue("do a thing") + m.ta.SetValue("do a thing") nm, cmd := m.submitChat() m = nm.(Model) if m.cstate != chatRunning { diff --git a/internal/ui/notify.go b/internal/ui/notify.go new file mode 100644 index 0000000..1ebc710 --- /dev/null +++ b/internal/ui/notify.go @@ -0,0 +1,24 @@ +package ui + +import ( + "os/exec" + "runtime" +) + +// Notify sends a desktop notification. Best-effort — never returns an error +// because notifications are cosmetic. Uses notify-send (Linux), osascript +// (macOS), or is a no-op on unsupported platforms. +func Notify(title, body string) { + if !Enabled { + return + } + switch runtime.GOOS { + case "linux": + cmd := exec.Command("notify-send", "--urgency=low", title, body) + _ = cmd.Run() + case "darwin": + script := `display notification %q with title %q` + cmd := exec.Command("osascript", "-e", script, body, title) + _ = cmd.Run() + } +} diff --git a/plan.md b/plan.md index 4c1e89d..d56e559 100644 --- a/plan.md +++ b/plan.md @@ -234,7 +234,7 @@ The nice-to-haves that make the project stand out — build only once the core i lines. Auto-disables color/animation when piped or `NO_COLOR` is set (regression stays green). The full-screen dashboard below is still the later, larger effort. - **TUI dashboard (Bubble Tea) 🟡 MOSTLY DONE:** `orchestra dashboard` — full-screen tabbed UI: - Agents (+ live health probe), History, Benchmarks, and a **Chat** tab. Chat hands the terminal to the + Logs, Agents (+ live health probe), History, Benchmarks, Changes (per-file diff stats), and a **Chat** tab. Chat hands the terminal to the full supervised engine via `tea.Exec` (route → run → validate → accept/reject, like the shell), then returns and refreshes. Keyboard nav, gradient styling. **Chat is now in-pane:** the agent runs in the background (spinner), then the diff renders inside the dashboard for accept (`y`, commits) / reject @@ -251,6 +251,13 @@ The nice-to-haves that make the project stand out — build only once the core i implement the interface (+ optional Querier/Prober/QuietRunner/QuietQuerier). Documented in [docs/EXTENDING.md](docs/EXTENDING.md) with a runnable example ([examples/](examples/)). A full runtime binary-plugin loader is intentionally skipped — the config path already covers third-party tools. +- **API-backed agents ✅ DONE:** `APIAgent` (`type: api` in orchestra.yaml) calls an HTTP LLM directly — + `internal/llm` (openai-compatible + anthropic providers, stdlib only) — sends a bounded repo snapshot, + and applies the returned patch via `internal/patch` (`git apply --check` first, traversal-guarded + writes). Implements `Querier`/`Prober`, so it can also plan and route. No engine/router/scheduler changes. +- **Git-free supervision ✅ DONE:** outside a repository the engine snapshots the directory + (`internal/fsdiff`), shows a git-style diff against the snapshot, and restores it on reject — so + `run`/`do`/shell work in plain folders. `--parallel` and `benchmark` still require git (worktrees). - **Context engine:** *deferred* — the supported agents gather their own repo context, so pre-selecting a file slice is largely redundant. Revisit only if token/cost becomes a measured problem.