package thin import ( "context" "encoding/json" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/wire" ) // TestStuckToolsRefusedOnAnOlderLink (stuck tasks, protocol 30): each // stuck-task tool is offered to the model, and a link below 30 answers it // here (an older instance has no words for it and Encode refuses the // call); at 30 it goes to the instance. Its description tells the model // that only the person's tap merges. func TestStuckToolsRefusedOnAnOlderLink(t *testing.T) { for _, name := range wire.StuckTools { if !slices.Contains(provider.ConverseTools(), name) { t.Fatalf("%s is not offered", name) } var offered map[string]any for _, tool := range converseTools { if m := tool.(map[string]any); m["name"] == name { offered = m } } if offered == nil { t.Fatalf("%s has no schema", name) } call := wire.ToolCall{CallID: "t/1/" + name, TurnID: "t", Attempt: 1, Name: name, Args: json.RawMessage(`{"task":"tk1"}`)} sent := 0 trip := func(context.Context, wire.ToolCall) wire.ToolResult { sent++ return wire.ToolResult{Code: "ok"} } old := &bridge{speaks: wire.ProtocolPinnedRelease, trip: trip} if res := old.roundTrip(context.Background(), call); !res.IsError || res.Code != "stuck_unsupported" || res.Text != toolStuckOld || sent != 0 { t.Fatalf("%s at 29: %+v, %d sent", name, res, sent) } current := &bridge{speaks: wire.ProtocolStuckTasks, trip: trip} if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { t.Fatalf("%s at 30: %+v, %d sent", name, res, sent) } } for _, tool := range converseTools { if m := tool.(map[string]any); m["name"] == wire.ToolOfferRetry && !strings.Contains(m["description"].(string), "only their tap merges") { t.Fatalf("offer_retry's description: %s", m["description"]) } } } // TestStuckToolUnencodableCallIsRetired: a stuck-task call queued for a // link that turns out to speak 29 is answered at once and leaves the // outbox, so it never replays after an upgrade. func TestStuckToolUnencodableCallIsRetired(t *testing.T) { ctx := context.Background() s, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } defer s.Close() call := wire.ToolCall{CallID: "call", TurnID: "finished-turn", Name: wire.ToolOfferRetry, Args: json.RawMessage(`{"task":"tk1"}`)} if err := s.SendToolCall(ctx, call); err != nil { t.Fatal(err) } frames, _, err := s.Unacked(ctx, 0) if err != nil || len(frames) != 1 { t.Fatal(err, frames) } l := &Link{State: s, proto: wire.ProtocolPinnedRelease} wait := l.waitTool(call.CallID) if err := l.send(ctx, nil, frames[0]); err != nil { t.Fatal(err) } select { case res := <-wait: if res.Code != "stuck_unsupported" { t.Fatal(res) } default: t.Fatal("no immediate unsupported result") } if frames, _, err = s.Unacked(ctx, 0); err != nil || len(frames) != 0 { t.Fatal("an old call can replay after an upgrade", frames, err) } }