| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Stuck tasks (protocol 30): a hello's tool health is a fixed state per |
| 15 | // pinned tool, never a path, and an older instance never hears it. |
| 16 | func TestProtocol30ToolHealth(t *testing.T) { |
| 17 | if wire.ProtocolStuckTasks != wire.Protocol30 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 { |
| 18 | t.Fatalf("stuck %d, current %d", wire.ProtocolStuckTasks, wire.ProtocolCurrent) |
| 19 | } |
| 20 | caps := func(h *wire.ToolHealth) wire.Capabilities { |
| 21 | return wire.Capabilities{Tags: []string{wire.TagLinux}, Simulators: []wire.Simulator{}, Tools: h} |
| 22 | } |
| 23 | for _, s := range []string{wire.ToolHealthy, wire.ToolMissing, wire.ToolMismatch, wire.ToolUnpinned} { |
| 24 | if err := wire.ValidCapabilities(caps(&wire.ToolHealth{Scanner: s})); err != nil { |
| 25 | t.Fatalf("scanner %s refused: %v", s, err) |
| 26 | } |
| 27 | if err := wire.ValidCapabilities(caps(&wire.ToolHealth{Scanner: wire.ToolHealthy, Renderer: s})); err != nil { |
| 28 | t.Fatalf("renderer %s refused: %v", s, err) |
| 29 | } |
| 30 | } |
| 31 | for _, bad := range []wire.ToolHealth{{}, {Scanner: wire.ToolUnknown}, {Scanner: "fine"}, {Scanner: wire.ToolHealthy, Renderer: wire.ToolUnknown}, {Scanner: "/usr/bin/gitleaks"}} { |
| 32 | if err := wire.ValidCapabilities(caps(&bad)); !errors.Is(err, wire.ErrCapabilities) { |
| 33 | t.Fatalf("%+v accepted", bad) |
| 34 | } |
| 35 | } |
| 36 | hello := wire.Hello{Protocol: wire.Protocol30, Repos: []string{}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagLinux}, Sites: true, Tools: &wire.ToolHealth{Scanner: wire.ToolMismatch}}} |
| 37 | f, _ := wire.NewFrame("h", wire.FrameHello, hello) |
| 38 | if same, err := wire.Encode(f, wire.Protocol30); err != nil || !bytes.Equal(same.Data, f.Data) || !strings.Contains(string(same.Data), `"tools":{"scanner":"mismatch"}`) { |
| 39 | t.Fatalf("hello at 30: %v %s", err, same.Data) |
| 40 | } |
| 41 | got, err := wire.Encode(f, wire.Protocol29) |
| 42 | if err != nil || strings.Contains(string(got.Data), `"tools"`) || !strings.Contains(string(got.Data), `"sites":true`) { |
| 43 | t.Fatalf("hello at 29: %v %s", err, got.Data) |
| 44 | } |
| 45 | if hello.Capabilities.Tools == nil { |
| 46 | t.Fatal("encoding changed the caller's hello") |
| 47 | } |
| 48 | first := hello |
| 49 | if !wire.WithoutToolHealth(&first) || first.Capabilities.Tools != nil || hello.Capabilities.Tools == nil || wire.WithoutToolHealth(&first) { |
| 50 | t.Fatal("WithoutToolHealth") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // The stuck-task tools are coordinator write tools, held to one operation |
| 55 | // per task in a turn, never a move-in turn's, and never sent below 30. |
| 56 | func TestProtocol30StuckTools(t *testing.T) { |
| 57 | for _, name := range wire.StuckTools { |
| 58 | if !slices.Contains(wire.ConverseTools, name) || !slices.Contains(wire.WriteTools, name) || wire.MutationKinds[name] == "" || slices.Contains(wire.MoveInTools, name) { |
| 59 | t.Fatalf("%s is not a coordinator write tool", name) |
| 60 | } |
| 61 | call, _ := wire.NewFrame("c", wire.FrameToolCall, wire.ToolCall{CallID: "c", TurnID: "t", Attempt: 1, Name: name, Args: json.RawMessage(`{"task":"tk"}`)}) |
| 62 | for _, p := range []int{wire.Protocol29, wire.ProtocolAccess, wire.Protocol1} { |
| 63 | if _, err := wire.Encode(call, p); !errors.Is(err, wire.ErrProtocol) { |
| 64 | t.Fatalf("%s encoded at %d: %v", name, p, err) |
| 65 | } |
| 66 | } |
| 67 | if same, err := wire.Encode(call, wire.Protocol30); err != nil || !bytes.Equal(same.Data, call.Data) { |
| 68 | t.Fatalf("%s at 30: %v", name, err) |
| 69 | } |
| 70 | } |
| 71 | if !slices.Contains(wire.StuckTools, wire.ToolOfferRetry) || !slices.Contains(wire.StuckTools, wire.ToolRetryStep) { |
| 72 | t.Fatal("offer_retry and retry_step are not stuck-task tools") |
| 73 | } |
| 74 | // retry_step runs these again, never a merge, a publish or the work |
| 75 | // itself. |
| 76 | if !slices.Equal(wire.RetryStepKinds, []string{wire.SpecialistPreview, wire.SpecialistChecks, wire.SpecialistReview}) { |
| 77 | t.Fatalf("retry_step kinds %v", wire.RetryStepKinds) |
| 78 | } |
| 79 | // Any other tool call goes to an older instance as it was. |
| 80 | other, _ := wire.NewFrame("c", wire.FrameToolCall, wire.ToolCall{CallID: "c", TurnID: "t", Attempt: 1, Name: wire.ToolReadTask, Args: json.RawMessage(`{"task":"tk"}`)}) |
| 81 | if same, err := wire.Encode(other, wire.Protocol29); err != nil || !bytes.Equal(same.Data, other.Data) { |
| 82 | t.Fatalf("read_task at 29: %v", err) |
| 83 | } |
| 84 | } |
| 85 | |