| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // TestResearchRefusedBelow12: propose_specialist is answered here on a |
| 13 | // connection below protocol 13, before sending and in place of an older |
| 14 | // instance's refusal of a call queued at 13 (it does not know the tool); |
| 15 | // protocol 13, an unknown protocol, another tool and a stored success are |
| 16 | // never touched. |
| 17 | func TestResearchRefusedBelow12(t *testing.T) { |
| 18 | propose := wire.ToolCall{Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{"profile":"research-v1"}`)} |
| 19 | other := wire.ToolCall{Name: wire.ToolAskPerson, Args: json.RawMessage(`{"question":"q"}`)} |
| 20 | unknown := wire.ToolResult{IsError: true, Code: "unknown_tool", Text: "Unknown tool."} |
| 21 | invalid := wire.ToolResult{IsError: true, Code: "invalid", Text: "bad input"} |
| 22 | ok := wire.ToolResult{Code: "proposed", Text: "asked"} |
| 23 | for name, c := range map[string]struct { |
| 24 | call wire.ToolCall |
| 25 | spoken int |
| 26 | res *wire.ToolResult |
| 27 | refused bool |
| 28 | }{ |
| 29 | "before sending at 11": {propose, wire.ProtocolPreviewHost, nil, true}, |
| 30 | "before sending at 2": {propose, wire.Protocol2, nil, true}, |
| 31 | "an older unknown tool at 11": {propose, wire.ProtocolPreviewHost, &unknown, true}, |
| 32 | "an older refusal at 11": {propose, wire.ProtocolPreviewHost, &invalid, true}, |
| 33 | "a stored success at 11": {propose, wire.ProtocolPreviewHost, &ok, false}, |
| 34 | "at 13": {propose, wire.ProtocolResearch, nil, false}, |
| 35 | "an answer at 13": {propose, wire.ProtocolResearch, &unknown, false}, |
| 36 | "protocol unknown": {propose, 0, nil, false}, |
| 37 | "another tool at 11": {other, wire.ProtocolPreviewHost, &unknown, false}, |
| 38 | } { |
| 39 | res, refused := researchRefused(c.call, c.spoken, c.res) |
| 40 | if refused != c.refused || refused && (res.Code != "research_unsupported" || !res.IsError || res.Text != toolResearchOld) { |
| 41 | t.Errorf("%s: %+v %t", name, res, refused) |
| 42 | } |
| 43 | } |
| 44 | if toolResearchOld != "Equipo needs an update before it can research." { |
| 45 | t.Fatalf("the update message: %q", toolResearchOld) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // TestBridgeRefusesResearchOnAnOlderLink: the bridge never sends |
| 50 | // propose_specialist on a link below 13; at 13 it goes to the instance. |
| 51 | func TestBridgeRefusesResearchOnAnOlderLink(t *testing.T) { |
| 52 | sent := 0 |
| 53 | trip := func(context.Context, wire.ToolCall) wire.ToolResult { |
| 54 | sent++ |
| 55 | return wire.ToolResult{Code: "proposed", Text: "asked"} |
| 56 | } |
| 57 | call := wire.ToolCall{CallID: "t/1/u", TurnID: "t", Attempt: 1, Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{}`)} |
| 58 | old := &bridge{speaks: wire.ProtocolPreviewHost, trip: trip} |
| 59 | if res := old.roundTrip(context.Background(), call); !res.IsError || res.Text != toolResearchOld || sent != 0 { |
| 60 | t.Fatalf("at 11: %+v, %d sent", res, sent) |
| 61 | } |
| 62 | current := &bridge{speaks: wire.ProtocolResearch, trip: trip} |
| 63 | if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { |
| 64 | t.Fatalf("at 13: %+v, %d sent", res, sent) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // TestResearchToolRefusedBelow12 (slice R2): the research tool is one of |
| 69 | // wire.ResearchTools, so an older link answers it here and never sends |
| 70 | // it; at 13 it is offered to the model and goes to the instance. |
| 71 | func TestResearchToolRefusedBelow12(t *testing.T) { |
| 72 | call := wire.ToolCall{CallID: "t/1/r", TurnID: "t", Attempt: 1, Name: wire.ToolResearch, Args: json.RawMessage(`{"task":"tk1","question":"q","urls":[]}`)} |
| 73 | sent := 0 |
| 74 | trip := func(context.Context, wire.ToolCall) wire.ToolResult { |
| 75 | sent++ |
| 76 | return wire.ToolResult{Code: "requested", Text: "ok"} |
| 77 | } |
| 78 | old := &bridge{speaks: wire.ProtocolPreviewHost, trip: trip} |
| 79 | if res := old.roundTrip(context.Background(), call); !res.IsError || res.Code != "research_unsupported" || res.Text != toolResearchOld || sent != 0 { |
| 80 | t.Fatalf("at 11: %+v, %d sent", res, sent) |
| 81 | } |
| 82 | current := &bridge{speaks: wire.ProtocolResearch, trip: trip} |
| 83 | if res := current.roundTrip(context.Background(), call); res.IsError || sent != 1 { |
| 84 | t.Fatalf("at 13: %+v, %d sent", res, sent) |
| 85 | } |
| 86 | var offered map[string]any |
| 87 | for _, tool := range converseTools { |
| 88 | if m := tool.(map[string]any); m["name"] == wire.ToolResearch { |
| 89 | offered = m |
| 90 | } |
| 91 | } |
| 92 | if offered == nil { |
| 93 | t.Fatal("research is not offered") |
| 94 | } |
| 95 | d := offered["description"].(string) |
| 96 | for _, want := range []string{"names an outside site", "starts it only at the task's start (call it before start_task) or after a review round", "never retries"} { |
| 97 | if !strings.Contains(d, want) { |
| 98 | t.Fatalf("the description lacks %q: %s", want, d) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |