//go:build perf package equipotest import ( "amadan.net/oficina/equipo/internal/equipo/personapi" "amadan.net/oficina/equipo/internal/wire" "bytes" "crypto/ed25519" "crypto/rand" "encoding/base64" "encoding/json" "fmt" "image" "image/png" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/taskplan" ) func perfPlan3Paths(t *testing.T, f runnerFixture, paths, checks map[string]string, renderID, renderHash string) string { t.Helper() ctx := connectionContext add := func(pattern, path, content string) { paths[pattern] = path; checks[path] = content } p := taskplan.BugPlan("", f.project.ID) p.Revision = 0 p.Steps[0].Instructions = "Repair the checkout error" p, err := f.s.ProposePlan(ctx, ownerActor, p, equipo.Mutation{RequestID: "perf-plan"}) executionMust(t, err) _, err = f.s.SendMessage(ctx, ownerActor, p.TaskID, "Checkout fails after payment", equipo.Mutation{RequestID: "perf-thread"}) executionMust(t, err) taskPath := "/tasks/" + p.TaskID for _, suffix := range []string{"/plan", "/thread"} { add("/tasks/{id}"+suffix, taskPath+suffix, p.TaskID) } // GET /api/v1/tasks/{id} is the thin v1 task now (bearer only, a // recorded perf skip); the older plan and thread reads remain. for _, suffix := range []string{"/plan", "/thread"} { add("/api/v1/tasks/{id}"+suffix, "/api/v1"+taskPath+suffix, p.TaskID) } var card string executionMust(t, f.d.Writer().QueryRow("SELECT id FROM conversation_cards WHERE resource_id=? AND kind='task'", p.TaskID).Scan(&card)) add("/api/v1/cards/{id}", "/api/v1/cards/"+card, p.TaskID) add("/api/v1/questions/{id}", "/api/v1/questions/perf-question", "perf-question") sp, err := f.s.InstallSpecialistTemplate(ctx, ownerActor, equipo.TemplateRef{ID: "qa", Version: 1}, equipo.Mutation{RequestID: "perf-specialist"}) executionMust(t, err) for _, suffix := range []string{"/edit", "/template-diff"} { add("/specialists/{key}"+suffix, "/specialists/"+sp.Key+suffix, sp.Key) } for _, suffix := range []string{"", "/template-diff"} { add("/api/v1/specialists/{id}"+suffix, "/api/v1/specialists/"+sp.Key+suffix, sp.Key) } pb, err := f.s.InstallPlaybookTemplate(ctx, ownerActor, equipo.TemplateRef{ID: "playbook_bug", Version: 1}, equipo.Mutation{RequestID: "perf-playbook"}) executionMust(t, err) for _, suffix := range []string{"", "/edit", "/template-diff"} { add("/playbooks/{id}"+suffix, "/playbooks/"+pb.ID+suffix, pb.ID) } for _, suffix := range []string{"", "/template-diff"} { add("/api/v1/playbooks/{id}"+suffix, "/api/v1/playbooks/"+pb.ID+suffix, pb.ID) } add("/api/v1/templates/{id}", "/api/v1/templates/qa", "qa") projectPath := fmt.Sprintf("/api/v1/projects/%d", f.project.ID) add("/api/v1/projects/{id}", projectPath, f.project.Name) add("/api/v1/projects/{id}/settings", projectPath+"/settings", "approval_mode") add("/api/v1/connections/{id}", "/api/v1/connections/"+f.ids["claude"], f.ids["claude"]) add("/api/v1/runners/{id}", "/api/v1/runners/"+f.principal.RunnerID, f.principal.RunnerID) setup, err := f.s.RequestAccountSetup(ctx, ownerActor, f.principal.RunnerID, "codex", "Personal", equipo.Mutation{RequestID: "perf-setup"}) executionMust(t, err) add("/api/v1/account-setups/{id}", "/api/v1/account-setups/"+setup.ID, setup.ID) invite, err := f.s.CreateInvitation(ctx, ownerActor, ownerAddress, equipo.RoleOwner, equipo.Mutation{RequestID: "perf-invite"}) executionMust(t, err) add("/invites/{id}", "/invites/"+invite.ID, invite.ID) add("/api/v1/invites/{id}", "/api/v1/invites/"+invite.ID, invite.ID) pub, _, err := ed25519.GenerateKey(rand.Reader) executionMust(t, err) device, err := f.s.BeginPersonDevice(ctx, base64.RawURLEncoding.EncodeToString(pub), "Perf phone", "perf-device") executionMust(t, err) add("/auth/devices/{id}", "/auth/devices/"+device.ID, "Perf phone") source, err := f.s.SendMessage(ctx, ownerActor, "", "Use sentence case", equipo.Mutation{RequestID: "perf-source"}) executionMust(t, err) add("/preferences/sources/{source}", "/preferences/sources/"+source.ID, source.Body) suggestion, err := f.s.SendMessage(ctx, ownerActor, "", `{"draft":{"name":"Checkout checks","instructions":"Check checkout changes"}}`, equipo.Mutation{RequestID: "perf-suggestion"}) executionMust(t, err) mustSQL(t, f.d, "UPDATE conversation_messages SET author_kind='coordinator',author_id='perf-coordinator' WHERE id=?", suggestion.ID) 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) add("/playbook-suggestions/{suggestion}", "/playbook-suggestions/"+suggestion.ID, "Checkout checks") renderPath := "/tasks/" + renderID + "/rendered-change" add("/tasks/{id}/rendered-change", renderPath, renderHash) add("/tasks/{id}/rendered-change/{hash}.png", renderPath+"/"+renderHash+".png", "\x89PNG\r\n\x1a\n") checks["/api/v1"+taskPath+"/thread"] = "Checkout fails after payment" checks["/api/v1/specialists/"+sp.Key+"/template-diff"] = "qa" checks["/api/v1/playbooks/"+pb.ID+"/template-diff"] = "playbook_bug" // The conversation page reads the thin v1 chat store. sessionCtx := personapi.WithVerifiedPrincipal(ctx, personapi.PersonPrincipal{Address: ownerAddress, SessionEpoch: time.Now().Unix()}) _, _, err = f.s.Chat().Send(sessionCtx, ownerAddress, wire.SendMessage{Text: source.Body, IdempotencyKey: "perf-chat"}) executionMust(t, err) checks["/conversation"] = source.Body // A job thread (a hand-off on its own lane) for /jobs/{id}. now := time.Now().UTC() 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) add("/jobs/{id}", "/jobs/h_perf", "Fix the checkout") // Project placement is a normal owner screen, measured with a real task. 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) 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) add("/tasks/{id}/move", "/tasks/tkplacementperf/move", "Move this task") previewSHA := strings.Repeat("a", 40) 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) previewJSON := `{"phase":"done","app":"tmp-tkplacementperf","digest":"` + strings.Repeat("b", 64) + `","outcome":"deployed","url":"https://tmp-tkplacementperf.example.com/"}` 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) add("/tasks/{id}/preview", "/tasks/tkplacementperf/preview", `sandbox="allow-scripts allow-same-origin"`) checks["/tasks"] = "Placement example" checks["/projects"] = "Placement" return strings.TrimPrefix(invite.ShareURL, testOrigin) } func perfRenderedChange(t *testing.T, f runnerFixture) (string, string) { t.Helper() task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "Checkout wording", "perf-render") executionMust(t, err) task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f.connectionFixture)) executionMust(t, err) task, err = f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision) executionMust(t, err) scope := team.Scope{Account: f.a.Scope.Account, Job: task.JobID} handle, err := f.x.Core.Claim(connectionContext, scope, "reproduce", "perf-runner", "perf-workspace") executionMust(t, err) job, err := f.x.Core.GetJob(connectionContext, scope) executionMust(t, err) encode := func(v any) []byte { b, e := json.Marshal(v); executionMust(t, e); return b } job.Evidence.Predicate = team.PredicateRenderedChange job.Evidence.RequireBugFreeze = false job.Evidence.RequiredDecisions = []string{"copy"} mustSQL(t, f.d, "UPDATE jobs SET evidence=? WHERE id=?", string(encode(job.Evidence)), job.ID) mustSQL(t, f.d, "UPDATE tasks SET kind=? WHERE id=?", taskplan.RenderedChange, task.ID) mustSQL(t, f.d, "INSERT INTO decision_owners(project_id,decision,address) VALUES(?,'copy',?)", f.project.ID, ownerAddress) mustSQL(t, f.d, "UPDATE attempts SET state='succeeded',containment='contained' WHERE id=?", handle.Attempt) cand := team.GitCandidate{Repository: job.Target.Repository, BaseCommit: job.Target.BaseRevision, Commit: strings.Repeat("d", 40), Tree: strings.Repeat("e", 40)} raw, hash, err := team.CanonicalJSON(encode(cand)) executionMust(t, err) diff := encode([]taskplan.RenderDiffFile{{Path: "checkout.html", Before: []byte("Pay"), After: []byte("Pay securely")}}) var pixels bytes.Buffer executionMust(t, png.Encode(&pixels, image.NewRGBA(image.Rect(0, 0, 640, 480)))) 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)} for _, blob := range [][]byte{diff, pixels.Bytes()} { mustSQL(t, f.d, "INSERT INTO artifact_blobs(team_id,job_id,digest,bytes) VALUES(?,?,?,?)", scope.Account, job.ID, team.ContentDigest(blob), blob) } for _, art := range []struct { role team.Role body []byte }{{team.RoleCandidate, raw}, {team.RoleRender, encode(render)}} { 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) } return task.ID, render.RenderHash }