| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // TestStuckToolsRefusedOnAnOlderLink (stuck tasks, protocol 30): each |
| 15 | // stuck-task tool is offered to the model, and a link below 30 answers it |
| 16 | // here (an older instance has no words for it and Encode refuses the |
| 17 | // call); at 30 it goes to the instance. Its description tells the model |
| 18 | // that only the person's tap merges. |
| 19 | func TestStuckToolsRefusedOnAnOlderLink(t *testing.T) { |
| 20 | for _, name := range wire.StuckTools { |
| 21 | if !slices.Contains(provider.ConverseTools(), name) { |
| 22 | t.Fatalf("%s is not offered", name) |
| 23 | } |
| 24 | var offered map[string]any |
| 25 | for _, tool := range converseTools { |
| 26 | if m := tool.(map[string]any); m["name"] == name { |
| 27 | offered = m |
| 28 | } |
| 29 | } |
| 30 | if offered == nil { |
| 31 | t.Fatalf("%s has no schema", name) |
| 32 | } |
| 33 | call := wire.ToolCall{CallID: "t/1/" + name, TurnID: "t", Attempt: 1, Name: name, Args: json.RawMessage(`{"task":"tk1"}`)} |
| 34 | sent := 0 |
| 35 | trip := func(context.Context, wire.ToolCall) wire.ToolResult { |
| 36 | sent++ |
| 37 | return wire.ToolResult{Code: "ok"} |
| 38 | } |
| 39 | old := &bridge{speaks: wire.ProtocolPinnedRelease, trip: trip} |
| 40 | if res := old.roundTrip(context.Background(), call); !res.IsError || res.Code != "stuck_unsupported" || res.Text != toolStuckOld || sent != 0 { |
| 41 | t.Fatalf("%s at 29: %+v, %d sent", name, res, sent) |
| 42 | } |
| 43 | current := &bridge{speaks: wire.ProtocolStuckTasks, trip: trip} |
| 44 | if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { |
| 45 | t.Fatalf("%s at 30: %+v, %d sent", name, res, sent) |
| 46 | } |
| 47 | } |
| 48 | for _, tool := range converseTools { |
| 49 | if m := tool.(map[string]any); m["name"] == wire.ToolOfferRetry && !strings.Contains(m["description"].(string), "only their tap merges") { |
| 50 | t.Fatalf("offer_retry's description: %s", m["description"]) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // TestStuckToolUnencodableCallIsRetired: a stuck-task call queued for a |
| 56 | // link that turns out to speak 29 is answered at once and leaves the |
| 57 | // outbox, so it never replays after an upgrade. |
| 58 | func TestStuckToolUnencodableCallIsRetired(t *testing.T) { |
| 59 | ctx := context.Background() |
| 60 | s, err := OpenState(ctx, t.TempDir()) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | defer s.Close() |
| 65 | call := wire.ToolCall{CallID: "call", TurnID: "finished-turn", Name: wire.ToolOfferRetry, Args: json.RawMessage(`{"task":"tk1"}`)} |
| 66 | if err := s.SendToolCall(ctx, call); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | frames, _, err := s.Unacked(ctx, 0) |
| 70 | if err != nil || len(frames) != 1 { |
| 71 | t.Fatal(err, frames) |
| 72 | } |
| 73 | l := &Link{State: s, proto: wire.ProtocolPinnedRelease} |
| 74 | wait := l.waitTool(call.CallID) |
| 75 | if err := l.send(ctx, nil, frames[0]); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | select { |
| 79 | case res := <-wait: |
| 80 | if res.Code != "stuck_unsupported" { |
| 81 | t.Fatal(res) |
| 82 | } |
| 83 | default: |
| 84 | t.Fatal("no immediate unsupported result") |
| 85 | } |
| 86 | if frames, _, err = s.Unacked(ctx, 0); err != nil || len(frames) != 0 { |
| 87 | t.Fatal("an old call can replay after an upgrade", frames, err) |
| 88 | } |
| 89 | } |
| 90 | |