gunk-dev/the-valley

development.md

sourceccb7354855a5main's published tip history
11.2 KiB · Rendered
1# Developing klaus
2
3klaus is the Go module `the-valley/flow` in `flow/`, with its entry point in `cmd/klaus` and its
4packages under `internal/`. Changes to it are changes of this repository and follow its rules
5([AGENTS.md](../../AGENTS.md)): one commit per change, build output in a scratch directory outside
6the tree.
7
8## Building
9
10```bash
11nix build .#klaus
12```
13
14The flake's `packages.x86_64-linux.klaus` (Linux x86_64 only) builds `cmd/klaus` with the version
15stamped through `-X the-valley/flow/internal/cmd.version=<v>`: the CLI version of
16`nix/packages.nix`, `0.1.0-<short commit>`, with `-dirty` for a tree with uncommitted changes, or
17`0.1.0-unknown` when nix knows no commit. The package's source holds no tests and no prose, and the
18package runs no tests: the checks below do. It depends on none of the programs klaus runs.
19
20## Testing
21
22klaus's tests run only through nix checks, each built on its own in the build sandbox:
23
24```bash
25nix build --option sandbox true --no-link .#checks.x86_64-linux.flow-build
26nix build --option sandbox true --no-link .#checks.x86_64-linux.flow-unit
27nix build --option sandbox true --no-link .#checks.x86_64-linux.flow-vet
28nix build --option sandbox true --no-link .#checks.x86_64-linux.flow-e2e
29```
30
31| Check | Runs |
32| ------------ | --------------------------------------------------------------------------------- |
33| `flow-build` | the shipping package; `klaus --version` must print `klaus version <v>` |
34| `flow-unit` | `go test ./...`, with git, tmux and the newest nix |
35| `flow-vet` | `go vet ./...` |
36| `flow-e2e` | `go test -tags e2e ./e2e/...` (slow), with git, tmux, bash and the valley command |
37
38A change touching `flow/` requires all four; `valley checks` lists what a change requires. The
39checks rebuild the package over the whole module, tests included, with the package's own vendored
40modules: one fixed-output derivation, `klaus-go-modules`, pinned by `vendorHash` in
41`nix/packages.nix` and resolved from the whole module with its tests. A change to `go.mod` or
42`go.sum` changes that hash.
43
44The checks set `KLAUS_TEST_TOOLS=required`: a test that would skip for a missing tool (tmux, nix,
45valley, or a valley lacking a verb it needs) fails instead, so a scenario that did not run cannot
46pass. The tmux checks clear `TMUX` and `TMUX_PANE`, put a private `TMUX_TMPDIR`, `HOME` and XDG
47directories under the build's temporary directory, and fail if a socket is left under `TMUX_TMPDIR`
48(which means a test reached tmux through the default socket directory). `flow-unit` runs nix against
49a dummy store, so the test that has nix parse self-update's flake references needs no store; it
50takes the newest nix because nix 2.31 refuses `git+git://` references. `flow-e2e` runs this
51repository's `packages.valley-unstamped` for its restack and landing scenarios.
52
53## Dependencies
54
55`flow/` is the one module of this repository that may import third-party Go modules: Bubble Tea, Lip
56Gloss, `charmbracelet/x/ansi`, fsnotify, Cobra and pflag, with their indirect dependencies. Nothing
57outside `flow/` imports from it, directly or transitively, and nothing a host runs with authority
58does; the `go-dependencies` check enforces this. No vendor directory or `go.work` is committed.
59
60## Layout
61
62| Package | Holds |
63| ----------------------------------------------------------------- | ---------------------------------------------------------- |
64| `internal/cmd` | the Cobra commands, the dashboard TUI, launch and finalize |
65| `internal/run` | run state and the session store |
66| `internal/backend` | the claude, codex and agy CLI contracts |
67| `internal/stream` | the JSONL stream formatter and check timing |
68| `internal/scan` | the sensitivity scanner |
69| `internal/git`, `internal/tmux`, `internal/nix` | git worktrees, hooks and the data ref; tmux panes; nix |
70| `internal/config` | configuration, prompt templates and worker rules |
71| `internal/pipeline` | the GitHub pipeline's state machine |
72| `internal/github`, `internal/webhook` | GitHub queries; the webhook receiver |
73| `internal/valley` | the `valley` CLI: ready, unready, changes, knots |
74| `internal/integration` | whether a pushed branch landed |
75| `internal/restack`, `internal/checksfix` | restack-conflict and failed-check dispatch |
76| `internal/draft` | budget pause |
77| `internal/review`, `internal/prose` | linters, peer and cross-model review; the prose pass |
78| `internal/consult`, `internal/jobs` | consults; job records |
79| `internal/event`, `internal/report` | the event log; `klaus report time` |
80| `internal/project`, `internal/projectsync`, `internal/houserules` | the registry; syncing; house rules |
81| `internal/selfupdate` | `klaus self-update` |
82
83Pure-logic packages are unit-tested directly; wrappers around git and tmux are tested against
84temporary repositories and private tmux servers.
85
86## Keeping tests away from real tmux
87
88Agent panes export `KLAUS_SESSION_ID`, so everything an agent runs in its worktree, `go test`
89included, inherits the coordinator's session ID. A test that handed a real tmux client a session
90name derived from it would aim at the live `klaus-agents-<session-id>` session every running agent
91lives in. Three layers prevent that:
92
93- Unit tests in `internal/cmd` use `isolatedTmux(t)`, an in-memory tmux client with a pinned
94 `KLAUS_SESSION_ID`; `TestUnitTestsNeverUseRealTmux` fails if a test there reaches for the
95 exec-backed client.
96- Every package whose code can reach the real client (`internal/cmd`, `internal/run`,
97 `internal/pipeline`, `internal/tmux`) runs its tests through `tmuxtest.Main`, which starts them as
98 launch starts a worker: no `TMUX`, `TMUX_PANE` or `KLAUS_TMUX_SOCKET`, and a `TMUX_TMPDIR` of
99 their own. A test that wants a server starts one with `tmux -S <its temp dir>/…`.
100- The checks fail on any socket left in the default socket directory.
101
102## The end-to-end suite
103
104`e2e/`, behind the `e2e` build tag, drives the real `klaus` binary (built once by `TestMain`)
105against a real, isolated tmux server and real git repositories: pane creation, the
106`worker | tee | _format-stream; _finalize` pipeline, worktree lifecycle and run state.
107
108| Concern | Isolation |
109| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
110| tmux | a private server per test on `tmux -S <tmpdir>/tmux.sock`; klaus reaches it through `$TMUX`; its panes get a per-test `TMUX_TMPDIR` and no `KLAUS_TMUX_SOCKET`; killed in `t.Cleanup` |
111| state | `HOME` is a per-test temporary directory, so `~/.klaus` lands under it |
112| externals | a per-test bin directory first on `PATH` holds klaus and fake `claude` and `gh`; git, tmux and (where a scenario asks for it) `valley` stay real |
113| git | a sandbox repository with a bare local `origin`, so pushes need no network |
114
115A unique socket and HOME per test let tests run in parallel. In the check there is no network, no
116terminal and no `/usr/bin/env`: `WriteStub` runs a `#!/usr/bin/env bash` stub under the `bash` on
117`PATH`, and other scripts need `#!/bin/sh`.
118
119tmux runs pane commands through its default shell, and a login shell can rewrite `PATH` and hide the
120stubs, so the harness starts the server with `-f /dev/null` and points `default-shell` at a wrapper
121that forces a fixed `PATH` and `HOME`.
122
123`TestWorkerCannotKillTheSharedServer` launches a worker of each backend that runs
124`tmux kill-server`, `TMUX_TMPDIR=<dir> tmux kill-server` and `tmux ls`. It checks that the harness's
125server and its first session survive, the worker saw no `TMUX` and its own `TMUX_TMPDIR`,
126`klaus status` in the worker still found its pane through `KLAUS_TMUX_SOCKET`, and the run
127finalized. A fake worker runs tmux only when every `TMUX_TMPDIR` it passes exists, since tmux falls
128back to `/tmp` for a missing one.
129
130### The fake `claude`
131
132`_finalize` parses the run's captured JSONL. The stub emits an `assistant` event whose text holds a
133GitHub URL matching `https?://github\.com/[^\s"<>\]]+/pull/\d+` (placeholder slugs such as
134`owner/repo` are rejected) and a `result` event with `total_cost_usd`, `duration_ms` and
135`session_id`, which become the run's cost, duration and URL. The sandbox's origin is a local path,
136so its runs deliver by push and take no URL from the log; a test that wants GitHub delivery calls
137`UseGitHubOrigin` before launching. The stub blocks until `ReleaseClaude`, so a test can observe the
138live pane before finalize tears it down. `WriteDefaultClaudeOutput` replaces its output, and
139`WriteStub` replaces any stub.
140
141### Adding a scenario
142
143```go
144//go:build e2e
145
146func TestSomething(t *testing.T) {
147 t.Parallel()
148 h := NewHarness(t)
149
150 res := h.RunKlaus("launch", "do the thing")
151 // assert on res, h.ReadState(id), h.ListPanes(), h.PaneTitle(id), ...
152}
153```
154
155The `Harness` helpers in `harness_test.go`:
156
157- `RunKlaus(args...)`, `RunKlausIn(dir, args...)`: run the binary in the isolated environment, from
158 the sandbox repository or a given directory (such as `h.E2EDir`, which is no repository);
159- `RegisterProject(ref, path)`: register a checkout in the temporary HOME's registry;
160- `ReadState(id)`, `WaitForState(id, pred, timeout)`, `RunIDs()`, `SeedState(state)`: run state;
161- `ListPanes()`, `PaneExists(id)`, `PaneTitle(id)`: the isolated tmux server;
162- `WaitForClaudeStart`, `ReleaseClaude`, `ClaudeArgv`, `GHArgv`: coordinate the stubs;
163- `WriteStub(name, script)`, `WriteDefaultClaudeOutput(jsonl)`, `UseGitHubOrigin()`: shape the
164 externals.
165
166A scenario that needs a tool or a valley verb calls `skipWithout` when it is missing, which fails
167instead of skipping under `KLAUS_TEST_TOOLS=required`.