gunk-dev/the-valley

tmux_isolation_test.go

source30e24c82839bmain's published tip history
4.2 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "os"
7 "path/filepath"
8 "regexp"
9 "strings"
10 "testing"
11 "time"
12
13 "the-valley/flow/internal/run"
14 "the-valley/flow/internal/tmux"
15)
16
17// killServerWorker is the fake worker with, once released, what one ran on
18// 2026-10-18 to kill what it took for its own test server: in a klaus pane
19// $TMUX overrode TMUX_TMPDIR, and that killed the shared server, with the
20// operator's session and every agent in it. It records the tmux environment
21// it got and what tmux said, then asks klaus for status, as a worker's tools
22// do, before printing its canned stream. tmux skips a TMUX_TMPDIR that doesn't
23// exist for /tmp, where a developer's own server may be, so the worker only
24// runs tmux where every TMUX_TMPDIR it uses exists.
25func (h *Harness) killServerWorker() string {
26 return strings.Replace(h.claudeStubScript(), `cat "$dir/claude.output.jsonl"`,
27 `printf '%s\n' "TMUX=${TMUX-unset}" "TMUX_PANE=${TMUX_PANE-unset}" "TMUX_TMPDIR=${TMUX_TMPDIR-unset}" "`+tmux.SocketEnv+`=${`+tmux.SocketEnv+`-unset}" > "$dir/worker.env"
28mkdir -p -m 700 "$dir/x"
29if [ -z "${TMUX-}" ] && [ ! -d "${TMUX_TMPDIR-}" ]; then
30 echo "not running tmux: TMUX_TMPDIR ${TMUX_TMPDIR-} is missing, so tmux would use /tmp" > "$dir/worker.tmux"
31else
32 {
33 tmux kill-server
34 TMUX_TMPDIR="$dir/x" tmux kill-server
35 tmux ls
36 } > "$dir/worker.tmux" 2>&1
37fi
38klaus status > "$dir/worker.status" 2>&1
39cat "$dir/claude.output.jsonl"`, 1)
40}
41
42// TestWorkerCannotKillTheSharedServer: a worker of each backend runs
43// `tmux kill-server`, `TMUX_TMPDIR=<dir> tmux kill-server` and `tmux ls`. The
44// harness's server stands in for the shared one, its first session for the
45// operator's, and both survive. The worker saw no $TMUX or $TMUX_PANE and its
46// run's own TMUX_TMPDIR, yet klaus status in it still found its live pane on
47// the shared server, and the run finalized as ever: state written, pane closed.
48func TestWorkerCannotKillTheSharedServer(t *testing.T) {
49 for _, kind := range []string{"claude", "codex", "agy"} {
50 t.Run(kind, func(t *testing.T) {
51 t.Parallel()
52 h := NewHarness(t)
53 h.useWorker(kind, kind+"-session")
54 h.WriteStub(kind, h.killServerWorker())
55
56 st, _ := h.launchRun("--backend", kind, "test the systemd timer")
57 h.WaitForClaudeStart(30 * time.Second)
58 pane := *st.TmuxPane
59 if !h.PaneExists(pane) {
60 t.Fatalf("worker pane %s missing before the worker ran", pane)
61 }
62 read := func(name string) string {
63 b, _ := os.ReadFile(filepath.Join(h.E2EDir, name))
64 return string(b)
65 }
66
67 // Watch the server while the run ends: a worker that reaches it
68 // kills its own pane too, and the run never finalizes.
69 h.ReleaseClaude()
70 var final *run.State
71 for deadline := time.Now().Add(60 * time.Second); final == nil; time.Sleep(100 * time.Millisecond) {
72 if !h.SessionExists("main") || !h.PaneExists(h.InitialPane) {
73 t.Fatalf("the worker's tmux killed the shared server's operator session; tmux said:\n%s", read("worker.tmux"))
74 }
75 s, err := h.ReadState(st.ID)
76 if err == nil && s.DurationMS != nil && s.TmuxPane == nil && s.Worktree == "" {
77 final = s
78 } else if time.Now().After(deadline) {
79 t.Fatalf("run did not finalize within 60s (last: %+v, %v)", s, err)
80 }
81 }
82
83 tmuxDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID, "tmux", st.ID)
84 env := read("worker.env")
85 for _, want := range []string{"TMUX=unset", "TMUX_PANE=unset", "TMUX_TMPDIR=" + tmuxDir, tmux.SocketEnv + "=" + h.Sock} {
86 if !strings.Contains(env, want+"\n") {
87 t.Errorf("worker environment lacks %q:\n%s", want, env)
88 }
89 }
90 if fi, err := os.Stat(tmuxDir); err != nil || !fi.IsDir() || fi.Mode().Perm() != 0o700 {
91 t.Errorf("worker's TMUX_TMPDIR %s should be a 0700 directory: %v %v", tmuxDir, fi, err)
92 }
93
94 if status := read("worker.status"); !regexp.MustCompile(`(?m)^` + st.ID + `\s+running\s`).MatchString(status) {
95 t.Errorf("klaus status in the worker did not find its live pane on the shared server:\n%s", status)
96 }
97
98 if final.Backend != kind || final.FailureReason != nil {
99 t.Errorf("run did not finalize normally: %+v", final)
100 }
101 if h.PaneExists(pane) {
102 t.Errorf("finalize left worker pane %s open", pane)
103 }
104 })
105 }
106}