package wire import ( "fmt" "slices" ) // Stuck tasks (coordinator powers, Paul 2026-11-25): ProtocolStuckTasks // adds the runner's tool health to its capabilities, so the coordinator's // status can say why a preview could not be scanned, and the // coordinator's offer_retry and retry_step. Below it Encode drops a hello's tool health // and refuses the tools' calls: an older instance decodes a hello // strictly and has no words for either. // ToolOfferRetry puts a card in front of the person to try a failed merge // again; only their tap merges (the merge card's Merge). ToolRetryStep // runs a failed step of RetryStepKinds again, as the person's Retry would. const ( ToolOfferRetry = "offer_retry" ToolRetryStep = "retry_step" ) // StuckTools are ProtocolStuckTasks's coordinator tools. var StuckTools = []string{ToolOfferRetry, ToolRetryStep} // RetryStepKinds are the steps retry_step may run again: never a merge, a // publish, or the work itself (Engineering, Copy). var RetryStepKinds = []string{SpecialistPreview, SpecialistChecks, SpecialistReview} // RetriedByCoordinator is a step's RetriedBy when the coordinator's // retry_step started its current attempt. const RetriedByCoordinator = "coordinator" // ToolHealth is whether the runner's pinned tools are installed and // match their pins, one fixed state each (ToolHealthy and the rest): // never a path, a digest or a tool's output. Scanner is the preview's // secret scanner (gitleaks at release.GitleaksVersion); Renderer is a // page renderer's, absent while the runner has none. type ToolHealth struct { Scanner string `json:"scanner"` Renderer string `json:"renderer,omitempty"` // Carlos (ProtocolPlaces) is the carlos CLI's health (CarlosOK and // the rest) and CarlosVersion the release it said, vX.Y.Z. Carlos string `json:"carlos,omitempty"` CarlosVersion string `json:"carlos_version,omitempty"` } // Tool health states. ToolUnknown is the instance's word for a runner // that did not say (an older one); a runner never reports it. const ( ToolHealthy = "ok" ToolMissing = "missing" ToolMismatch = "mismatch" ToolUnpinned = "unpinned" ToolUnknown = "unknown" ) var toolStates = []string{ToolHealthy, ToolMissing, ToolMismatch, ToolUnpinned} // validToolHealth reports whether a runner's report is in the fixed set: // the scanner always said, the renderer only when there is one. func validToolHealth(h ToolHealth) bool { return slices.Contains(toolStates, h.Scanner) && (h.Renderer == "" || slices.Contains(toolStates, h.Renderer)) && validCarlosHealth(h.Carlos, h.CarlosVersion) } // WithoutToolHealth drops a hello's tool health, in a copy. The first // hello is sent before the instance says its protocol: an older // instance's strict decoder would refuse it. It reports whether it // dropped anything. func WithoutToolHealth(h *Hello) bool { if h.Capabilities == nil || h.Capabilities.Tools == nil { return false } c := *h.Capabilities c.Tools = nil h.Capabilities = &c return true } // downgradeStuck clears ProtocolStuckTasks's fields for an older peer // and refuses its tools' calls outright. func downgradeStuck(body any, protocol int) (bool, error) { switch v := body.(type) { case *Hello: return WithoutToolHealth(v), nil case *ToolCall: if slices.Contains(StuckTools, v.Name) { return false, fmt.Errorf("%w: %s at protocol %d", ErrProtocol, v.Name, protocol) } } return false, nil }