//go:build e2elive package ownerlive import ( "context" "database/sql" "encoding/json" "os" "os/exec" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/team" . "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) // Invoked only by the disposable HTTP harness, after its runner has stopped. func TestOwnerImplementE2ELive(t *testing.T) { path := os.Getenv("EQUIPO_OWNER_HARNESS") if path == "" { t.Skip("invoked by make e2e-live") } raw, err := os.ReadFile(path) mustJournal(t, err) var env map[string]string mustJournal(t, json.Unmarshal(raw, &env)) cfg, err := LoadConfig(func(k string) string { return env[k] }) mustJournal(t, err) ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second) defer cancel() r, err := OpenOwnerHarness(ctx, cfg) mustJournal(t, err) defer func() { mustJournal(t, r.Close()) }() raw, err = os.ReadFile(cfg.AccountBindingsPath) mustJournal(t, err) var bs []AccountBinding mustJournal(t, json.Unmarshal(raw, &bs)) if len(bs) != 1 || bs[0].LocalID != "claude-a" { t.Fatal("harness account changed") } b := bs[0] work := filepath.Join(cfg.WorkspaceRoot, "owner-implement") mustJournal(t, os.Mkdir(work, 0700)) git := func(args ...string) string { t.Helper() c := exec.Command(cfg.GitPath, append([]string{"-C", work}, args...)...) c.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") out, e := c.CombinedOutput() if e != nil { t.Fatalf("local git: %v: %s", e, out) } return strings.TrimSpace(string(out)) } git("init", "-b", "main") git("config", "user.name", "Harness") git("config", "user.email", "harness@example.test") mustJournal(t, os.WriteFile(filepath.Join(work, "value.txt"), []byte("before\n"), 0600)) git("add", "value.txt") git("commit", "-m", "Initial fixture") base := git("rev-parse", "main") git("checkout", "-b", "equipo/owner-mode") 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"}} launcher := r.Launcher var session provider.Session 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) { return launcher.(interface { Permission(context.Context, provider.Session, json.RawMessage) (json.RawMessage, error) }).Permission(ctx, session, raw) }}) mustJournal(t, err) defer proxy.Close() 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}}} launch, cleanup, err := r.Launch(ctx, claim, workspace.Workspace{Path: work}, proxy) mustJournal(t, err) defer func() { mustJournal(t, cleanup()) }() if launch.Mode != "owner mode" { t.Fatal("launch mode absent") } session, err = launcher.Open(ctx, launch) mustJournal(t, err) defer launcher.Close(context.Background(), session) events, err := launcher.Events(ctx, session) mustJournal(t, err) 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."})) for { select { case <-ctx.Done(): t.Fatal("implement timed out") case event, ok := <-events: if !ok { t.Fatal("implement stream ended") } _, err = r.Journal.ObserveDirect(ctx, session, event) mustJournal(t, err) if event.Kind == "request" { mustJournal(t, launcher.Respond(ctx, session, provider.Answer{RequestID: event.RequestID, TurnID: event.TurnID, Behavior: "deny"})) } if event.Kind == "turn.failed" { t.Fatalf("implement failed: %s", RedactJSON(event.Payload, nil)) } if event.Kind == "turn.completed" { goto finished } } } finished: mustJournal(t, launcher.Close(context.Background(), session)) raw, err = os.ReadFile(filepath.Join(work, "value.txt")) mustJournal(t, err) if string(raw) != "owner mode implemented\n" { t.Fatalf("model change absent: %q", raw) } git("add", "value.txt") git("commit", "-m", "Implement owner mode fixture") if git("branch", "--show-current") != "equipo/owner-mode" || git("rev-parse", "HEAD") == base || git("rev-parse", "main") != base { t.Fatal("branch or default ref changed incorrectly") } journal, err := sql.Open("sqlite", filepath.Join(cfg.StateDir, "journal.db")) mustJournal(t, err) defer journal.Close() var mode string mustJournal(t, journal.QueryRow("SELECT json_extract(launch,'$.Mode') FROM direct_attempts WHERE attempt=?", launch.AttemptKey).Scan(&mode)) if mode != "owner mode" { t.Fatal("journal launch lost owner label") } t.Log("one Claude implement turn; sandboxed file change committed on local equipo/owner-mode; main unchanged; no remote configured") } func mustJournal(t *testing.T, err error) { t.Helper() if err != nil { t.Fatal(err) } } func TestMain(m *testing.M) { if len(os.Args) > 1 && os.Args[1] == "__namespace" { os.Exit(containment.NamespaceMain(os.Args[2:])) } os.Exit(m.Run()) }