| 1 | //go:build e2elive |
| 2 | |
| 3 | package ownerlive |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "database/sql" |
| 8 | "encoding/json" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 17 | . "amadan.net/oficina/equipo/internal/runner" |
| 18 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 19 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 20 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 21 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 22 | ) |
| 23 | |
| 24 | // Invoked only by the disposable HTTP harness, after its runner has stopped. |
| 25 | func TestOwnerImplementE2ELive(t *testing.T) { |
| 26 | path := os.Getenv("EQUIPO_OWNER_HARNESS") |
| 27 | if path == "" { |
| 28 | t.Skip("invoked by make e2e-live") |
| 29 | } |
| 30 | raw, err := os.ReadFile(path) |
| 31 | mustJournal(t, err) |
| 32 | var env map[string]string |
| 33 | mustJournal(t, json.Unmarshal(raw, &env)) |
| 34 | cfg, err := LoadConfig(func(k string) string { return env[k] }) |
| 35 | mustJournal(t, err) |
| 36 | ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second) |
| 37 | defer cancel() |
| 38 | r, err := OpenOwnerHarness(ctx, cfg) |
| 39 | mustJournal(t, err) |
| 40 | defer func() { mustJournal(t, r.Close()) }() |
| 41 | raw, err = os.ReadFile(cfg.AccountBindingsPath) |
| 42 | mustJournal(t, err) |
| 43 | var bs []AccountBinding |
| 44 | mustJournal(t, json.Unmarshal(raw, &bs)) |
| 45 | if len(bs) != 1 || bs[0].LocalID != "claude-a" { |
| 46 | t.Fatal("harness account changed") |
| 47 | } |
| 48 | b := bs[0] |
| 49 | work := filepath.Join(cfg.WorkspaceRoot, "owner-implement") |
| 50 | mustJournal(t, os.Mkdir(work, 0700)) |
| 51 | git := func(args ...string) string { |
| 52 | t.Helper() |
| 53 | c := exec.Command(cfg.GitPath, append([]string{"-C", work}, args...)...) |
| 54 | c.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") |
| 55 | out, e := c.CombinedOutput() |
| 56 | if e != nil { |
| 57 | t.Fatalf("local git: %v: %s", e, out) |
| 58 | } |
| 59 | return strings.TrimSpace(string(out)) |
| 60 | } |
| 61 | git("init", "-b", "main") |
| 62 | git("config", "user.name", "Harness") |
| 63 | git("config", "user.email", "harness@example.test") |
| 64 | mustJournal(t, os.WriteFile(filepath.Join(work, "value.txt"), []byte("before\n"), 0600)) |
| 65 | git("add", "value.txt") |
| 66 | git("commit", "-m", "Initial fixture") |
| 67 | base := git("rev-parse", "main") |
| 68 | git("checkout", "-b", "equipo/owner-mode") |
| 69 | sel := runnerproto.Selection{RunnerID: r.Credential.RunnerID, Person: r.Credential.Person, ConnectionID: b.LocalID, Provider: b.Provider, Family: b.Family, Model: b.Model, AccountID: b.AccountID, Isolation: team.IsolationSnapshot{Status: "confined", Mode: "owner mode"}} |
| 70 | launcher := r.Launcher |
| 71 | var session provider.Session |
| 72 | proxy, err := OpenToolProxy(ctx, ToolBinding{InstanceID: cfg.InstanceID, TaskID: "owner-implement", Attempt: 1, Fence: 1, Tools: []string{"submit_candidate", "submit_render", "ask_person"}, LeaseDeadline: time.Now().Add(150 * time.Second), Journal: r.Journal, Isolation: sel.Isolation, Copy: ToolCopy{Accepted: "accepted", Rejected: "rejected", Candidate: "submit candidate", Render: "submit render", Ask: "ask"}, Agent: provider.Agent{Provider: b.Provider, Family: b.Family, Model: b.Model, AccountID: b.AccountID, ServerID: "direct"}, Current: func(ctx context.Context) error { return ctx.Err() }, Permission: func(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { |
| 73 | return launcher.(interface { |
| 74 | Permission(context.Context, provider.Session, json.RawMessage) (json.RawMessage, error) |
| 75 | }).Permission(ctx, session, raw) |
| 76 | }}) |
| 77 | mustJournal(t, err) |
| 78 | defer proxy.Close() |
| 79 | claim := RunClaim{Assignment: runnerproto.Assignment{StepKey: "implement", Selection: sel}, ClaimResponse: runnerproto.ClaimResponse{Scope: team.Scope{Account: "harness", Job: 1}, Handle: team.Handle{Attempt: 1, Fence: 1}, Brief: runnerproto.RunnerBrief{Step: runnerproto.StepBrief{Action: "implement"}, Selection: sel}}} |
| 80 | launch, cleanup, err := r.Launch(ctx, claim, workspace.Workspace{Path: work}, proxy) |
| 81 | mustJournal(t, err) |
| 82 | defer func() { mustJournal(t, cleanup()) }() |
| 83 | if launch.Mode != "owner mode" { |
| 84 | t.Fatal("launch mode absent") |
| 85 | } |
| 86 | session, err = launcher.Open(ctx, launch) |
| 87 | mustJournal(t, err) |
| 88 | defer launcher.Close(context.Background(), session) |
| 89 | events, err := launcher.Events(ctx, session) |
| 90 | mustJournal(t, err) |
| 91 | mustJournal(t, DispatchDirectTurn(ctx, proxy, launcher, session, provider.Turn{ID: "implement", AdmissionID: "owner-harness-one-turn", Prompt: "Implement this tiny local fixture change. Use the Write tool to replace /workspace/value.txt with exactly owner mode implemented followed by a newline. Do not run shell commands, use the network, or call Equipo tools. After writing, end your turn. The harness will commit your change on the already checked-out branch."})) |
| 92 | for { |
| 93 | select { |
| 94 | case <-ctx.Done(): |
| 95 | t.Fatal("implement timed out") |
| 96 | case event, ok := <-events: |
| 97 | if !ok { |
| 98 | t.Fatal("implement stream ended") |
| 99 | } |
| 100 | _, err = r.Journal.ObserveDirect(ctx, session, event) |
| 101 | mustJournal(t, err) |
| 102 | if event.Kind == "request" { |
| 103 | mustJournal(t, launcher.Respond(ctx, session, provider.Answer{RequestID: event.RequestID, TurnID: event.TurnID, Behavior: "deny"})) |
| 104 | } |
| 105 | if event.Kind == "turn.failed" { |
| 106 | t.Fatalf("implement failed: %s", RedactJSON(event.Payload, nil)) |
| 107 | } |
| 108 | if event.Kind == "turn.completed" { |
| 109 | goto finished |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | finished: |
| 114 | mustJournal(t, launcher.Close(context.Background(), session)) |
| 115 | raw, err = os.ReadFile(filepath.Join(work, "value.txt")) |
| 116 | mustJournal(t, err) |
| 117 | if string(raw) != "owner mode implemented\n" { |
| 118 | t.Fatalf("model change absent: %q", raw) |
| 119 | } |
| 120 | git("add", "value.txt") |
| 121 | git("commit", "-m", "Implement owner mode fixture") |
| 122 | if git("branch", "--show-current") != "equipo/owner-mode" || git("rev-parse", "HEAD") == base || git("rev-parse", "main") != base { |
| 123 | t.Fatal("branch or default ref changed incorrectly") |
| 124 | } |
| 125 | journal, err := sql.Open("sqlite", filepath.Join(cfg.StateDir, "journal.db")) |
| 126 | mustJournal(t, err) |
| 127 | defer journal.Close() |
| 128 | var mode string |
| 129 | mustJournal(t, journal.QueryRow("SELECT json_extract(launch,'$.Mode') FROM direct_attempts WHERE attempt=?", launch.AttemptKey).Scan(&mode)) |
| 130 | if mode != "owner mode" { |
| 131 | t.Fatal("journal launch lost owner label") |
| 132 | } |
| 133 | t.Log("one Claude implement turn; sandboxed file change committed on local equipo/owner-mode; main unchanged; no remote configured") |
| 134 | } |
| 135 | |
| 136 | func mustJournal(t *testing.T, err error) { |
| 137 | t.Helper() |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | } |
| 142 | func TestMain(m *testing.M) { |
| 143 | if len(os.Args) > 1 && os.Args[1] == "__namespace" { |
| 144 | os.Exit(containment.NamespaceMain(os.Args[2:])) |
| 145 | } |
| 146 | os.Exit(m.Run()) |
| 147 | } |
| 148 | |