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