| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | const dataRef = "refs/klaus/data" |
| 17 | |
| 18 | // refSHA returns the sha ref resolves to in the git dir, or "" if absent. |
| 19 | func (h *Harness) refSHA(gitDir, ref string) string { |
| 20 | h.t.Helper() |
| 21 | cmd := exec.Command("git", "--git-dir", gitDir, "rev-parse", "--verify", "--quiet", ref) |
| 22 | cmd.Env = append(os.Environ(), "HOME="+h.Home, "GIT_CONFIG_GLOBAL="+filepath.Join(h.Home, ".gitconfig"), "GIT_CONFIG_NOSYSTEM=1") |
| 23 | out, err := cmd.Output() |
| 24 | if err != nil { |
| 25 | return "" |
| 26 | } |
| 27 | return strings.TrimSpace(string(out)) |
| 28 | } |
| 29 | |
| 30 | // launchAndFinalize launches an agent, lets the fake claude finish, and waits |
| 31 | // for _finalize to complete (cost recorded, pane released). It returns the |
| 32 | // run id. |
| 33 | func (h *Harness) launchAndFinalize(prompt string) string { |
| 34 | h.t.Helper() |
| 35 | res := h.RunKlaus("launch", prompt) |
| 36 | if res.ExitCode != 0 { |
| 37 | h.t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 38 | } |
| 39 | ids := h.RunIDs() |
| 40 | if len(ids) != 1 { |
| 41 | h.t.Fatalf("expected 1 run, got %d: %v", len(ids), ids) |
| 42 | } |
| 43 | h.WaitForClaudeStart(30 * time.Second) |
| 44 | h.ReleaseClaude() |
| 45 | h.WaitForState(ids[0], func(s *run.State) bool { |
| 46 | return s.CostUSD != nil && s.TmuxPane == nil |
| 47 | }, 30*time.Second) |
| 48 | return ids[0] |
| 49 | } |
| 50 | |
| 51 | // TestDataRefStaysLocalByDefault: with the default config, finalize commits |
| 52 | // the run to the sandbox repo's data ref and origin never receives it. |
| 53 | // push-log publishes it only when given --push. |
| 54 | func TestDataRefStaysLocalByDefault(t *testing.T) { |
| 55 | t.Parallel() |
| 56 | h := NewHarness(t) |
| 57 | localGitDir := filepath.Join(h.RepoDir, ".git") |
| 58 | |
| 59 | runID := h.launchAndFinalize("add a health check endpoint") |
| 60 | |
| 61 | if h.refSHA(localGitDir, dataRef+":runs/"+runID+".json") == "" { |
| 62 | t.Fatalf("local %s has no runs/%s.json after finalize", dataRef, runID) |
| 63 | } |
| 64 | if sha := h.refSHA(h.OriginDir, dataRef); sha != "" { |
| 65 | t.Fatalf("origin has %s at %s after finalize; the default config must not push it", dataRef, sha) |
| 66 | } |
| 67 | |
| 68 | res := h.RunKlaus("push-log", runID) |
| 69 | if res.ExitCode != 0 { |
| 70 | t.Fatalf("push-log exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 71 | } |
| 72 | if !strings.Contains(res.Stdout, "Not pushed") { |
| 73 | t.Errorf("push-log should say it did not push; stdout:\n%s", res.Stdout) |
| 74 | } |
| 75 | if sha := h.refSHA(h.OriginDir, dataRef); sha != "" { |
| 76 | t.Fatalf("push-log without --push published %s", dataRef) |
| 77 | } |
| 78 | |
| 79 | res = h.RunKlaus("push-log", "--push", runID) |
| 80 | if res.ExitCode != 0 { |
| 81 | t.Fatalf("push-log --push exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 82 | } |
| 83 | if !strings.Contains(res.Stdout, "Pushed "+dataRef+" to origin") { |
| 84 | t.Errorf("push-log --push should report the push; stdout:\n%s", res.Stdout) |
| 85 | } |
| 86 | local := h.refSHA(localGitDir, dataRef) |
| 87 | if got := h.refSHA(h.OriginDir, dataRef); got == "" || got != local { |
| 88 | t.Errorf("origin %s = %q after push-log --push, want %s", dataRef, got, local) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // TestDataRefPushedWhenEnabled: push_data_ref: true makes finalize push the |
| 93 | // data ref to origin. It is set globally because a host-repo run's finalize |
| 94 | // reads the repo config from the agent's worktree, which holds the committed |
| 95 | // copy. |
| 96 | func TestDataRefPushedWhenEnabled(t *testing.T) { |
| 97 | t.Parallel() |
| 98 | h := NewHarness(t) |
| 99 | if err := os.MkdirAll(filepath.Join(h.Home, ".klaus"), 0o755); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if err := os.WriteFile(filepath.Join(h.Home, ".klaus", "config.json"), []byte(`{"push_data_ref": true}`+"\n"), 0o644); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | |
| 106 | runID := h.launchAndFinalize("add a health check endpoint") |
| 107 | |
| 108 | local := h.refSHA(filepath.Join(h.RepoDir, ".git"), dataRef) |
| 109 | if local == "" { |
| 110 | t.Fatalf("local %s missing after finalize of %s", dataRef, runID) |
| 111 | } |
| 112 | if got := h.refSHA(h.OriginDir, dataRef); got != local { |
| 113 | t.Errorf("origin %s = %q, want the local commit %s", dataRef, got, local) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // TestDataRefPushRefusedAtFinalize: when origin's receive hook refuses the |
| 118 | // data ref, the run finalizes with its own outcome and records the refusal, |
| 119 | // status shows it, and the conversation replays from the clone. Once the hook |
| 120 | // is gone, the next run's finalize in the same clone publishes the ref, and |
| 121 | // with it the first run's commit. |
| 122 | func TestDataRefPushRefusedAtFinalize(t *testing.T) { |
| 123 | t.Parallel() |
| 124 | h := NewHarness(t) |
| 125 | if err := os.MkdirAll(filepath.Join(h.Home, ".klaus"), 0o755); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if err := os.WriteFile(filepath.Join(h.Home, ".klaus", "config.json"), []byte(`{"push_data_ref": true}`+"\n"), 0o644); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | const hookReason = "policy denies refs/klaus/data" |
| 132 | hookPath := filepath.Join(h.OriginDir, "hooks", "pre-receive") |
| 133 | hook := "#!/bin/sh\nwhile read old new name; do\n if [ \"$name\" = \"" + dataRef + "\" ]; then\n echo \"" + hookReason + "\" >&2\n exit 1\n fi\ndone\n" |
| 134 | if err := os.WriteFile(hookPath, []byte(hook), 0o755); err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | h.recordConversation() |
| 138 | localGitDir := filepath.Join(h.RepoDir, ".git") |
| 139 | |
| 140 | first, _ := h.launchRun("add a health check endpoint") |
| 141 | h.WaitForClaudeStart(30 * time.Second) |
| 142 | h.pushWork(first, "health.go", "Add a health check") |
| 143 | first = h.finishRun(first.ID) |
| 144 | |
| 145 | if first.FailureReason != nil || first.NeedsAttention != nil || first.PushedSHA == nil { |
| 146 | t.Fatalf("run outcome changed by the refused push: failure=%v attention=%v pushed=%v", first.FailureReason, first.NeedsAttention, first.PushedSHA) |
| 147 | } |
| 148 | rec := first.DataRefPush |
| 149 | if rec == nil || rec.Status != run.DataRefFailed || rec.Class != "refused" || !strings.Contains(rec.Message, hookReason) || rec.Commit != h.refSHA(localGitDir, dataRef) { |
| 150 | t.Fatalf("DataRefPush = %+v, want refused at the local tip with %q", rec, hookReason) |
| 151 | } |
| 152 | if sha := h.refSHA(h.OriginDir, dataRef); sha != "" { |
| 153 | t.Fatalf("origin has %s at %s though its hook refused it", dataRef, sha) |
| 154 | } |
| 155 | |
| 156 | st := h.RunKlaus("status") |
| 157 | for _, want := range []string{"awaiting-integration", "failed at finalize", first.ID + " publishing " + dataRef, hookReason, "git push origin " + dataRef} { |
| 158 | if !strings.Contains(st.Stdout, want) { |
| 159 | t.Errorf("status lacks %q:\n%s", want, st.Stdout) |
| 160 | } |
| 161 | } |
| 162 | if watch := h.RunKlaus("watch", "--since-start", "--once"); !strings.Contains(watch.Stdout, "agent:data-ref-push-failed") || !strings.Contains(watch.Stdout, hookReason) { |
| 163 | t.Errorf("the default watch stream lacks the failure:\n%s", watch.Stdout) |
| 164 | } |
| 165 | |
| 166 | if err := os.Remove(hookPath); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | h.resetWorker() |
| 170 | follow, res := h.launchRun("--change", first.ChangeID, "follow up on the health check") |
| 171 | if !strings.Contains(res.Stdout, "resuming conversation from run "+first.ID) { |
| 172 | t.Errorf("the follow-up did not replay the conversation from the clone:\n%s", res.Stdout) |
| 173 | } |
| 174 | h.WaitForClaudeStart(30 * time.Second) |
| 175 | h.pushWork(follow, "health_test.go", "Test the health check") |
| 176 | follow = h.finishRun(follow.ID) |
| 177 | |
| 178 | if follow.DataRefPush == nil || follow.DataRefPush.Status != run.DataRefPublished { |
| 179 | t.Fatalf("follow-up DataRefPush = %+v, want published", follow.DataRefPush) |
| 180 | } |
| 181 | if h.refSHA(h.OriginDir, rec.Commit+"^{commit}") == "" || h.refSHA(h.OriginDir, dataRef+":sessions/"+first.ID+".jsonl") == "" { |
| 182 | t.Errorf("origin lacks the first run's commit %s after the follow-up published", rec.Commit) |
| 183 | } |
| 184 | if again, err := h.ReadState(first.ID); err != nil { |
| 185 | t.Errorf("reading the first run: %v", err) |
| 186 | } else if again.DataRefPush == nil || *again.DataRefPush != *rec { |
| 187 | t.Errorf("the first run's record changed after finalize: %+v", again.DataRefPush) |
| 188 | } |
| 189 | } |