| 1 | package wire |
| 2 | |
| 3 | import "encoding/json" |
| 4 | |
| 5 | // Exploration tasks (spec 2026-11-12-equipo-exploration-tasks.md): |
| 6 | // mockups, prototypes, design options and spikes end at Review's |
| 7 | // approval, a preview and the person's choice of direction, never a |
| 8 | // merge. |
| 9 | |
| 10 | // Task kinds (Task.Kind). "" reads as KindChange. |
| 11 | const ( |
| 12 | KindChange = "change" |
| 13 | KindExplore = "explore" |
| 14 | ) |
| 15 | |
| 16 | // NormalKind is a kind as stored: "" is a change. |
| 17 | func NormalKind(k string) string { |
| 18 | if k == KindExplore { |
| 19 | return KindExplore |
| 20 | } |
| 21 | return KindChange |
| 22 | } |
| 23 | |
| 24 | // NeedsDirection: an exploration is approved; the person chooses build |
| 25 | // this one, try again or drop it. |
| 26 | const NeedsDirection = "direction" |
| 27 | |
| 28 | // CardDirection asks the task's creator which direction to take, once per |
| 29 | // candidate; its answers are ChoiceBuild, ChoiceAgain (with a note) and |
| 30 | // ChoiceDrop. |
| 31 | const ( |
| 32 | CardDirection = "direction" |
| 33 | ChoiceBuild = "build" |
| 34 | ChoiceAgain = "again" |
| 35 | ChoiceDrop = "drop" |
| 36 | ) |
| 37 | |
| 38 | // MaxDirectionNote bounds Try again's note and Build this's. |
| 39 | const MaxDirectionNote = 1000 |
| 40 | |
| 41 | // TaskDirection is what the direction card shows: the candidate's |
| 42 | // deployed preview, else its branch on Amadan, and whether the primary |
| 43 | // said previews are off. Options are what the newest Design attempt kept |
| 44 | // (protocol 38), by option; with more than one, NoteNeeded: Build this |
| 45 | // needs the person's note saying which. |
| 46 | type TaskDirection struct { |
| 47 | PreviewURL string `json:"preview_url,omitempty"` |
| 48 | BranchURL string `json:"branch_url"` |
| 49 | PreviewsOff bool `json:"previews_off,omitempty"` |
| 50 | Options []StepDesignOption `json:"options,omitempty"` |
| 51 | NoteNeeded bool `json:"note_needed,omitempty"` |
| 52 | } |
| 53 | |
| 54 | // ConflictDirectionNote is the card conflict a Build this without a note |
| 55 | // gets on a direction card with more than one option. |
| 56 | const ConflictDirectionNote = "direction_note" |
| 57 | |
| 58 | // SetKind is POST /api/v1/tasks/{id}/kind: the creator switches a task |
| 59 | // between a change and an exploration, at the task's current revision. |
| 60 | type SetKind struct { |
| 61 | Kind string `json:"kind"` |
| 62 | Revision int64 `json:"revision"` |
| 63 | } |
| 64 | |
| 65 | // WithoutPreviews drops a hello's previews capability (in a copy, so the |
| 66 | // caller's capabilities are untouched). The runner's first hello is sent |
| 67 | // before the instance says its protocol, and an older instance's strict |
| 68 | // decoder would refuse it; the next hello carries it once the welcome |
| 69 | // says ProtocolExplore. It reports whether it dropped anything. |
| 70 | func WithoutPreviews(h *Hello) bool { |
| 71 | if h.Capabilities == nil || !h.Capabilities.Previews && h.Capabilities.PreviewHost == nil { |
| 72 | return false |
| 73 | } |
| 74 | c := *h.Capabilities |
| 75 | c.Previews, c.PreviewHost = false, nil |
| 76 | h.Capabilities = &c |
| 77 | return true |
| 78 | } |
| 79 | |
| 80 | // WithoutPreviewHost drops a hello's preview host (ProtocolPreviewHost), |
| 81 | // in a copy, keeping the previews flag. It reports whether it dropped it. |
| 82 | func WithoutPreviewHost(h *Hello) bool { |
| 83 | if h.Capabilities == nil || h.Capabilities.PreviewHost == nil { |
| 84 | return false |
| 85 | } |
| 86 | c := *h.Capabilities |
| 87 | c.PreviewHost = nil |
| 88 | h.Capabilities = &c |
| 89 | return true |
| 90 | } |
| 91 | |
| 92 | // ExploreCall reports whether a tool call records an exploration |
| 93 | // (record_task with kind explore). |
| 94 | func ExploreCall(v *ToolCall) bool { |
| 95 | if v.Name != ToolRecordTask { |
| 96 | return false |
| 97 | } |
| 98 | var args struct { |
| 99 | Kind string `json:"kind"` |
| 100 | } |
| 101 | return json.Unmarshal(v.Args, &args) == nil && args.Kind == KindExplore |
| 102 | } |
| 103 | |
| 104 | // downgradeExplore clears ProtocolExplore's fields: a hello's previews |
| 105 | // capability, and record_task's kind when it says change. A kind that |
| 106 | // says explore is kept, so an older instance's strict decoder refuses the |
| 107 | // whole call: an exploration never becomes a change. It reports whether |
| 108 | // anything was dropped. |
| 109 | func downgradeExplore(body any) bool { |
| 110 | switch v := body.(type) { |
| 111 | case *Hello: |
| 112 | return WithoutPreviews(v) |
| 113 | case *ToolCall: |
| 114 | if v.Name != ToolRecordTask || ExploreCall(v) { |
| 115 | return false |
| 116 | } |
| 117 | var args map[string]json.RawMessage |
| 118 | if json.Unmarshal(v.Args, &args) != nil { |
| 119 | return false |
| 120 | } |
| 121 | if _, ok := args["kind"]; !ok { |
| 122 | return false |
| 123 | } |
| 124 | delete(args, "kind") |
| 125 | raw, err := json.Marshal(args) |
| 126 | if err != nil { |
| 127 | return false |
| 128 | } |
| 129 | v.Args = raw |
| 130 | return true |
| 131 | } |
| 132 | return false |
| 133 | } |
| 134 | |