| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | ) |
| 7 | |
| 8 | // Stuck tasks (coordinator powers, Paul 2026-11-25): ProtocolStuckTasks |
| 9 | // adds the runner's tool health to its capabilities, so the coordinator's |
| 10 | // status can say why a preview could not be scanned, and the |
| 11 | // coordinator's offer_retry and retry_step. Below it Encode drops a hello's tool health |
| 12 | // and refuses the tools' calls: an older instance decodes a hello |
| 13 | // strictly and has no words for either. |
| 14 | |
| 15 | // ToolOfferRetry puts a card in front of the person to try a failed merge |
| 16 | // again; only their tap merges (the merge card's Merge). ToolRetryStep |
| 17 | // runs a failed step of RetryStepKinds again, as the person's Retry would. |
| 18 | const ( |
| 19 | ToolOfferRetry = "offer_retry" |
| 20 | ToolRetryStep = "retry_step" |
| 21 | ) |
| 22 | |
| 23 | // StuckTools are ProtocolStuckTasks's coordinator tools. |
| 24 | var StuckTools = []string{ToolOfferRetry, ToolRetryStep} |
| 25 | |
| 26 | // RetryStepKinds are the steps retry_step may run again: never a merge, a |
| 27 | // publish, or the work itself (Engineering, Copy). |
| 28 | var RetryStepKinds = []string{SpecialistPreview, SpecialistChecks, SpecialistReview} |
| 29 | |
| 30 | // RetriedByCoordinator is a step's RetriedBy when the coordinator's |
| 31 | // retry_step started its current attempt. |
| 32 | const RetriedByCoordinator = "coordinator" |
| 33 | |
| 34 | // ToolHealth is whether the runner's pinned tools are installed and |
| 35 | // match their pins, one fixed state each (ToolHealthy and the rest): |
| 36 | // never a path, a digest or a tool's output. Scanner is the preview's |
| 37 | // secret scanner (gitleaks at release.GitleaksVersion); Renderer is a |
| 38 | // page renderer's, absent while the runner has none. |
| 39 | type ToolHealth struct { |
| 40 | Scanner string `json:"scanner"` |
| 41 | Renderer string `json:"renderer,omitempty"` |
| 42 | // Carlos (ProtocolPlaces) is the carlos CLI's health (CarlosOK and |
| 43 | // the rest) and CarlosVersion the release it said, vX.Y.Z. |
| 44 | Carlos string `json:"carlos,omitempty"` |
| 45 | CarlosVersion string `json:"carlos_version,omitempty"` |
| 46 | } |
| 47 | |
| 48 | // Tool health states. ToolUnknown is the instance's word for a runner |
| 49 | // that did not say (an older one); a runner never reports it. |
| 50 | const ( |
| 51 | ToolHealthy = "ok" |
| 52 | ToolMissing = "missing" |
| 53 | ToolMismatch = "mismatch" |
| 54 | ToolUnpinned = "unpinned" |
| 55 | ToolUnknown = "unknown" |
| 56 | ) |
| 57 | |
| 58 | var toolStates = []string{ToolHealthy, ToolMissing, ToolMismatch, ToolUnpinned} |
| 59 | |
| 60 | // validToolHealth reports whether a runner's report is in the fixed set: |
| 61 | // the scanner always said, the renderer only when there is one. |
| 62 | func validToolHealth(h ToolHealth) bool { |
| 63 | return slices.Contains(toolStates, h.Scanner) && (h.Renderer == "" || slices.Contains(toolStates, h.Renderer)) && validCarlosHealth(h.Carlos, h.CarlosVersion) |
| 64 | } |
| 65 | |
| 66 | // WithoutToolHealth drops a hello's tool health, in a copy. The first |
| 67 | // hello is sent before the instance says its protocol: an older |
| 68 | // instance's strict decoder would refuse it. It reports whether it |
| 69 | // dropped anything. |
| 70 | func WithoutToolHealth(h *Hello) bool { |
| 71 | if h.Capabilities == nil || h.Capabilities.Tools == nil { |
| 72 | return false |
| 73 | } |
| 74 | c := *h.Capabilities |
| 75 | c.Tools = nil |
| 76 | h.Capabilities = &c |
| 77 | return true |
| 78 | } |
| 79 | |
| 80 | // downgradeStuck clears ProtocolStuckTasks's fields for an older peer |
| 81 | // and refuses its tools' calls outright. |
| 82 | func downgradeStuck(body any, protocol int) (bool, error) { |
| 83 | switch v := body.(type) { |
| 84 | case *Hello: |
| 85 | return WithoutToolHealth(v), nil |
| 86 | case *ToolCall: |
| 87 | if slices.Contains(StuckTools, v.Name) { |
| 88 | return false, fmt.Errorf("%w: %s at protocol %d", ErrProtocol, v.Name, protocol) |
| 89 | } |
| 90 | } |
| 91 | return false, nil |
| 92 | } |
| 93 | |