| 1 | //go:build perf |
| 2 | |
| 3 | package equipotest |
| 4 | |
| 5 | import ( |
| 6 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | "bytes" |
| 9 | "crypto/ed25519" |
| 10 | "crypto/rand" |
| 11 | "encoding/base64" |
| 12 | "encoding/json" |
| 13 | "fmt" |
| 14 | "image" |
| 15 | "image/png" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | "time" |
| 19 | |
| 20 | "amadan.net/oficina/equipo/internal/equipo" |
| 21 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 22 | "amadan.net/oficina/equipo/internal/taskplan" |
| 23 | ) |
| 24 | |
| 25 | func perfPlan3Paths(t *testing.T, f runnerFixture, paths, checks map[string]string, renderID, renderHash string) string { |
| 26 | t.Helper() |
| 27 | ctx := connectionContext |
| 28 | add := func(pattern, path, content string) { paths[pattern] = path; checks[path] = content } |
| 29 | p := taskplan.BugPlan("", f.project.ID) |
| 30 | p.Revision = 0 |
| 31 | p.Steps[0].Instructions = "Repair the checkout error" |
| 32 | p, err := f.s.ProposePlan(ctx, ownerActor, p, equipo.Mutation{RequestID: "perf-plan"}) |
| 33 | executionMust(t, err) |
| 34 | _, err = f.s.SendMessage(ctx, ownerActor, p.TaskID, "Checkout fails after payment", equipo.Mutation{RequestID: "perf-thread"}) |
| 35 | executionMust(t, err) |
| 36 | taskPath := "/tasks/" + p.TaskID |
| 37 | for _, suffix := range []string{"/plan", "/thread"} { |
| 38 | add("/tasks/{id}"+suffix, taskPath+suffix, p.TaskID) |
| 39 | } |
| 40 | // GET /api/v1/tasks/{id} is the thin v1 task now (bearer only, a |
| 41 | // recorded perf skip); the older plan and thread reads remain. |
| 42 | for _, suffix := range []string{"/plan", "/thread"} { |
| 43 | add("/api/v1/tasks/{id}"+suffix, "/api/v1"+taskPath+suffix, p.TaskID) |
| 44 | } |
| 45 | var card string |
| 46 | executionMust(t, f.d.Writer().QueryRow("SELECT id FROM conversation_cards WHERE resource_id=? AND kind='task'", p.TaskID).Scan(&card)) |
| 47 | add("/api/v1/cards/{id}", "/api/v1/cards/"+card, p.TaskID) |
| 48 | add("/api/v1/questions/{id}", "/api/v1/questions/perf-question", "perf-question") |
| 49 | sp, err := f.s.InstallSpecialistTemplate(ctx, ownerActor, equipo.TemplateRef{ID: "qa", Version: 1}, equipo.Mutation{RequestID: "perf-specialist"}) |
| 50 | executionMust(t, err) |
| 51 | for _, suffix := range []string{"/edit", "/template-diff"} { |
| 52 | add("/specialists/{key}"+suffix, "/specialists/"+sp.Key+suffix, sp.Key) |
| 53 | } |
| 54 | for _, suffix := range []string{"", "/template-diff"} { |
| 55 | add("/api/v1/specialists/{id}"+suffix, "/api/v1/specialists/"+sp.Key+suffix, sp.Key) |
| 56 | } |
| 57 | pb, err := f.s.InstallPlaybookTemplate(ctx, ownerActor, equipo.TemplateRef{ID: "playbook_bug", Version: 1}, equipo.Mutation{RequestID: "perf-playbook"}) |
| 58 | executionMust(t, err) |
| 59 | for _, suffix := range []string{"", "/edit", "/template-diff"} { |
| 60 | add("/playbooks/{id}"+suffix, "/playbooks/"+pb.ID+suffix, pb.ID) |
| 61 | } |
| 62 | for _, suffix := range []string{"", "/template-diff"} { |
| 63 | add("/api/v1/playbooks/{id}"+suffix, "/api/v1/playbooks/"+pb.ID+suffix, pb.ID) |
| 64 | } |
| 65 | add("/api/v1/templates/{id}", "/api/v1/templates/qa", "qa") |
| 66 | projectPath := fmt.Sprintf("/api/v1/projects/%d", f.project.ID) |
| 67 | add("/api/v1/projects/{id}", projectPath, f.project.Name) |
| 68 | add("/api/v1/projects/{id}/settings", projectPath+"/settings", "approval_mode") |
| 69 | add("/api/v1/connections/{id}", "/api/v1/connections/"+f.ids["claude"], f.ids["claude"]) |
| 70 | add("/api/v1/runners/{id}", "/api/v1/runners/"+f.principal.RunnerID, f.principal.RunnerID) |
| 71 | setup, err := f.s.RequestAccountSetup(ctx, ownerActor, f.principal.RunnerID, "codex", "Personal", equipo.Mutation{RequestID: "perf-setup"}) |
| 72 | executionMust(t, err) |
| 73 | add("/api/v1/account-setups/{id}", "/api/v1/account-setups/"+setup.ID, setup.ID) |
| 74 | invite, err := f.s.CreateInvitation(ctx, ownerActor, ownerAddress, equipo.RoleOwner, equipo.Mutation{RequestID: "perf-invite"}) |
| 75 | executionMust(t, err) |
| 76 | add("/invites/{id}", "/invites/"+invite.ID, invite.ID) |
| 77 | add("/api/v1/invites/{id}", "/api/v1/invites/"+invite.ID, invite.ID) |
| 78 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 79 | executionMust(t, err) |
| 80 | device, err := f.s.BeginPersonDevice(ctx, base64.RawURLEncoding.EncodeToString(pub), "Perf phone", "perf-device") |
| 81 | executionMust(t, err) |
| 82 | add("/auth/devices/{id}", "/auth/devices/"+device.ID, "Perf phone") |
| 83 | source, err := f.s.SendMessage(ctx, ownerActor, "", "Use sentence case", equipo.Mutation{RequestID: "perf-source"}) |
| 84 | executionMust(t, err) |
| 85 | add("/preferences/sources/{source}", "/preferences/sources/"+source.ID, source.Body) |
| 86 | suggestion, err := f.s.SendMessage(ctx, ownerActor, "", `{"draft":{"name":"Checkout checks","instructions":"Check checkout changes"}}`, equipo.Mutation{RequestID: "perf-suggestion"}) |
| 87 | executionMust(t, err) |
| 88 | mustSQL(t, f.d, "UPDATE conversation_messages SET author_kind='coordinator',author_id='perf-coordinator' WHERE id=?", suggestion.ID) |
| 89 | mustSQL(t, f.d, "INSERT INTO conversation_cards(id,stream_id,kind,resource_id,sequence,revision) VALUES(?,?,'suggest_playbook',?,?,1)", suggestion.ID, suggestion.ConversationID, suggestion.ID, suggestion.Sequence) |
| 90 | add("/playbook-suggestions/{suggestion}", "/playbook-suggestions/"+suggestion.ID, "Checkout checks") |
| 91 | renderPath := "/tasks/" + renderID + "/rendered-change" |
| 92 | add("/tasks/{id}/rendered-change", renderPath, renderHash) |
| 93 | add("/tasks/{id}/rendered-change/{hash}.png", renderPath+"/"+renderHash+".png", "\x89PNG\r\n\x1a\n") |
| 94 | checks["/api/v1"+taskPath+"/thread"] = "Checkout fails after payment" |
| 95 | checks["/api/v1/specialists/"+sp.Key+"/template-diff"] = "qa" |
| 96 | checks["/api/v1/playbooks/"+pb.ID+"/template-diff"] = "playbook_bug" |
| 97 | // The conversation page reads the thin v1 chat store. |
| 98 | sessionCtx := personapi.WithVerifiedPrincipal(ctx, personapi.PersonPrincipal{Address: ownerAddress, SessionEpoch: time.Now().Unix()}) |
| 99 | _, _, err = f.s.Chat().Send(sessionCtx, ownerAddress, wire.SendMessage{Text: source.Body, IdempotencyKey: "perf-chat"}) |
| 100 | executionMust(t, err) |
| 101 | checks["/conversation"] = source.Body |
| 102 | // A job thread (a hand-off on its own lane) for /jobs/{id}. |
| 103 | now := time.Now().UTC() |
| 104 | mustSQL(t, f.d, "INSERT INTO chat_handoffs(id,person,state,ledger_key,lane,title,created_at,updated_at) VALUES('h_perf',?,'open','perf-handoff','job:h_perf','Fix the checkout',?,?)", ownerAddress, now, now) |
| 105 | add("/jobs/{id}", "/jobs/h_perf", "Fix the checkout") |
| 106 | // Project placement is a normal owner screen, measured with a real task. |
| 107 | mustSQL(t, f.d, "INSERT INTO chat_projects(id,repo,title,title_set,state,added_by,revision,created_at,updated_at) VALUES('p_placement_perf','amadan.net/oficina/equipo','Placement',1,'active',?,1,?,?)", ownerAddress, now, now) |
| 108 | mustSQL(t, f.d, "INSERT INTO chat_tasks(id,project_id,title,brief,stage,creator,revision,created_at,updated_at) VALUES('tkplacementperf','p_placement_perf','Placement example','Keep this brief','noted',?,1,?,?)", ownerAddress, now, now) |
| 109 | add("/tasks/{id}/move", "/tasks/tkplacementperf/move", "Move this task") |
| 110 | previewSHA := strings.Repeat("a", 40) |
| 111 | mustSQL(t, f.d, "INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,revision,candidate_sha,pushed,terminated,created_at,updated_at) VALUES('perf-preview-eng','tkplacementperf',1,'engineering',?,'done',1,1,?,1,1,?,?)", ownerAddress, previewSHA, now, now) |
| 112 | previewJSON := `{"phase":"done","app":"tmp-tkplacementperf","digest":"` + strings.Repeat("b", 64) + `","outcome":"deployed","url":"https://tmp-tkplacementperf.example.com/"}` |
| 113 | mustSQL(t, f.d, "INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,revision,candidate_sha,terminated,preview,created_at,updated_at) VALUES('perf-preview','tkplacementperf',2,'preview',?,'done',1,1,?,1,?,?,?)", ownerAddress, previewSHA, previewJSON, now, now) |
| 114 | add("/tasks/{id}/preview", "/tasks/tkplacementperf/preview", `sandbox="allow-scripts allow-same-origin"`) |
| 115 | |
| 116 | checks["/tasks"] = "Placement example" |
| 117 | checks["/projects"] = "Placement" |
| 118 | |
| 119 | return strings.TrimPrefix(invite.ShareURL, testOrigin) |
| 120 | } |
| 121 | |
| 122 | func perfRenderedChange(t *testing.T, f runnerFixture) (string, string) { |
| 123 | t.Helper() |
| 124 | task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "Checkout wording", "perf-render") |
| 125 | executionMust(t, err) |
| 126 | task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f.connectionFixture)) |
| 127 | executionMust(t, err) |
| 128 | task, err = f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision) |
| 129 | executionMust(t, err) |
| 130 | scope := team.Scope{Account: f.a.Scope.Account, Job: task.JobID} |
| 131 | handle, err := f.x.Core.Claim(connectionContext, scope, "reproduce", "perf-runner", "perf-workspace") |
| 132 | executionMust(t, err) |
| 133 | job, err := f.x.Core.GetJob(connectionContext, scope) |
| 134 | executionMust(t, err) |
| 135 | encode := func(v any) []byte { b, e := json.Marshal(v); executionMust(t, e); return b } |
| 136 | job.Evidence.Predicate = team.PredicateRenderedChange |
| 137 | job.Evidence.RequireBugFreeze = false |
| 138 | job.Evidence.RequiredDecisions = []string{"copy"} |
| 139 | mustSQL(t, f.d, "UPDATE jobs SET evidence=? WHERE id=?", string(encode(job.Evidence)), job.ID) |
| 140 | mustSQL(t, f.d, "UPDATE tasks SET kind=? WHERE id=?", taskplan.RenderedChange, task.ID) |
| 141 | mustSQL(t, f.d, "INSERT INTO decision_owners(project_id,decision,address) VALUES(?,'copy',?)", f.project.ID, ownerAddress) |
| 142 | mustSQL(t, f.d, "UPDATE attempts SET state='succeeded',containment='contained' WHERE id=?", handle.Attempt) |
| 143 | cand := team.GitCandidate{Repository: job.Target.Repository, BaseCommit: job.Target.BaseRevision, Commit: strings.Repeat("d", 40), Tree: strings.Repeat("e", 40)} |
| 144 | raw, hash, err := team.CanonicalJSON(encode(cand)) |
| 145 | executionMust(t, err) |
| 146 | diff := encode([]taskplan.RenderDiffFile{{Path: "checkout.html", Before: []byte("Pay"), After: []byte("Pay securely")}}) |
| 147 | var pixels bytes.Buffer |
| 148 | executionMust(t, png.Encode(&pixels, image.NewRGBA(image.Rect(0, 0, 640, 480)))) |
| 149 | render := team.RenderResult{Repository: cand.Repository, BaseCommit: cand.BaseCommit, CandidateHash: hash, DiffHash: team.ContentDigest(diff), RenderHash: team.ContentDigest(pixels.Bytes()), RecipeHash: strings.Repeat("b", 64), EnvironmentHash: strings.Repeat("c", 64)} |
| 150 | for _, blob := range [][]byte{diff, pixels.Bytes()} { |
| 151 | mustSQL(t, f.d, "INSERT INTO artifact_blobs(team_id,job_id,digest,bytes) VALUES(?,?,?,?)", scope.Account, job.ID, team.ContentDigest(blob), blob) |
| 152 | } |
| 153 | for _, art := range []struct { |
| 154 | role team.Role |
| 155 | body []byte |
| 156 | }{{team.RoleCandidate, raw}, {team.RoleRender, encode(render)}} { |
| 157 | mustSQL(t, f.d, "INSERT INTO artifacts(job_id,step_id,attempt_id,fence,role,hash,media_type,size,provenance,payload,created_at) VALUES(?,(SELECT step_id FROM attempts WHERE id=?),?,?,?,?,?,?,?,?,'2026-11-03T00:00:00Z')", job.ID, handle.Attempt, handle.Attempt, handle.Fence, art.role, team.ContentDigest(art.body), team.GitCandidateMediaType, len(art.body), `{"producer":"fixture","provider":"fixture","context":"fixture"}`, art.body) |
| 158 | } |
| 159 | return task.ID, render.RenderHash |
| 160 | } |
| 161 | |