| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "encoding/json" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func coordinatorFixture(t *testing.T) (*fixture, *equipo.Execution, equipo.RunnerPrincipal, equipo.CoordinatorBinding) { |
| 14 | f := newFixture(t) |
| 15 | task := f.createTask(t) |
| 16 | f.planConnections(t, task.ProjectID) |
| 17 | var path string |
| 18 | must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) |
| 19 | x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil), Adapters: map[team.Operation]team.Adapter{team.OpProviderCall: f.Store.RunnerAdapter(team.OpProviderCall)}}) |
| 20 | must(t, err) |
| 21 | t.Cleanup(func() { must(t, x.Core.Close()) }) |
| 22 | _, err = x.Core.Resume(ctx) |
| 23 | must(t, err) |
| 24 | f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC()) |
| 25 | p := equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"} |
| 26 | must(t, f.Store.SetCoordinatorLimits(ctx, owner, owner.Address, team.Limits{Calls: 24, InputTokens: 200000, OutputTokens: 40000, Seconds: 10000}, equipo.Mutation{RequestID: "limits"})) |
| 27 | b, err := f.Store.SaveCoordinatorBinding(ctx, owner, p.RunnerID, "claude", equipo.Mutation{RequestID: "bind"}) |
| 28 | must(t, err) |
| 29 | return f, x, p, b |
| 30 | } |
| 31 | func queueCoordinator(t *testing.T, f *fixture, body, key string) equipo.CoordinatorTurn { |
| 32 | t.Helper() |
| 33 | msg, err := f.Store.SendMessage(ctx, owner, "", body, equipo.Mutation{RequestID: key}) |
| 34 | must(t, err) |
| 35 | turn, err := f.Store.QueueCoordinatorTurn(ctx, owner, msg.ID, 0, equipo.Mutation{RequestID: key}) |
| 36 | must(t, err) |
| 37 | return turn |
| 38 | } |
| 39 | func (f *fixture) planConnections(t *testing.T, project int64) { |
| 40 | t.Helper() |
| 41 | var tuples []team.IsolationTuple |
| 42 | f.exec(t, "INSERT INTO runners(id,person,instance_id,revision) VALUES('fixture-runner',?,?,1)", owner.Address, f.TeamID) |
| 43 | for _, v := range []struct{ provider, family, version string }{{"claude", "claude", "2.1.312"}, {"codex", "gpt", "0.165.0"}} { |
| 44 | r := equipo.ConnectionReport{LocalID: v.provider, Provider: v.provider, Family: v.family, Model: "fixture-model", ProviderVersion: v.version, LauncherVersion: "direct-v2", BwrapVersion: "0.11.0", PolicyVersion: "policy-v2", Kind: "subscription", Available: true, ReadIsolation: true, IsolationConfigHash: strings.Repeat("a", 64), Routing: equipo.ConnectionRouting{BindingID: v.provider, AccountID: v.provider, ObservedAccountID: v.provider, CredentialHomeHash: strings.Repeat("b", 64), AccountPinned: true, NoFailover: true, TimelineRecovery: true, ProcessOwnership: true}} |
| 45 | tuples = append(tuples, team.IsolationTuple{Provider: r.Provider, ProviderVersion: r.ProviderVersion, LauncherVersion: r.LauncherVersion, BwrapVersion: r.BwrapVersion, PolicyVersion: r.PolicyVersion, IsolationConfigHash: r.IsolationConfigHash}) |
| 46 | raw, err := json.Marshal([]equipo.ConnectionReport{r}) |
| 47 | must(t, err) |
| 48 | f.exec(t, "INSERT INTO connections(id,runner_id,person,local_id,revision,last_seen,reports_json,security_hash) VALUES(?,'fixture-runner',?,?,1,?,?,'fixture')", v.provider, owner.Address, v.provider, time.Now().UTC(), string(raw)) |
| 49 | } |
| 50 | f.exec(t, `INSERT INTO connection_choices(person,project_id,revision,connection_ids_json,pinned_id) VALUES(?,?,1,'["claude","codex"]','claude')`, owner.Address, project) |
| 51 | f.Store = equipo.NewStore(f.DB, equipo.WithVerifiedIsolationTuples(tuples)) |
| 52 | } |
| 53 | |
| 54 | func toolCall(t *testing.T, x *equipo.Execution, p equipo.RunnerPrincipal, c equipo.CoordinatorClaim, id, name string, args any) runnerproto.CoordinatorToolResult { |
| 55 | t.Helper() |
| 56 | raw, err := json.Marshal(args) |
| 57 | must(t, err) |
| 58 | out, err := x.CallCoordinatorTool(ctx, p, c, runnerproto.CoordinatorToolCall{RequestID: id, Name: name, Arguments: raw}) |
| 59 | must(t, err) |
| 60 | return out |
| 61 | } |
| 62 | func claimTools(t *testing.T) (*fixture, *equipo.Execution, equipo.RunnerPrincipal, equipo.CoordinatorClaim) { |
| 63 | f, x, p, _ := coordinatorFixture(t) |
| 64 | turn := queueCoordinator(t, f, "Please plan this change", "tools") |
| 65 | c, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) |
| 66 | must(t, err) |
| 67 | // Trusted session event validation belongs to the runner event validator. |
| 68 | f.exec(t, "UPDATE coordinator_turns SET provider_session_id='fixture-session' WHERE id=?", turn.ID) |
| 69 | return f, x, p, c |
| 70 | } |
| 71 | |