gunk-dev/the-valley

development.md

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