| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Mixed versions (metered connections at 12, Research at 13): a peer at |
| 14 | // 12 keeps every metered frame and field but never sees Research; at 13 |
| 15 | // both go out whole; at 11 both are downgraded. The numbers are written |
| 16 | // out so that pointing ProtocolResearch back at 12 fails here. |
| 17 | func TestEncodeMixedMeteredAndResearch(t *testing.T) { |
| 18 | if wire.ProtocolMetered != wire.Protocol12 || wire.ProtocolResearch != wire.Protocol13 || wire.ProtocolCurrent != wire.ProtocolDesign { |
| 19 | t.Fatalf("metered %d, research %d, current %d", wire.ProtocolMetered, wire.ProtocolResearch, wire.ProtocolCurrent) |
| 20 | } |
| 21 | metered := wire.HeldConnection{LocalID: "tensorx-1", Person: "p", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Harness: wire.HarnessPi, Upstream: "https://api.tensorx.ai", Billing: wire.BillingMetered, Model: "z-ai/glm-5.3", Budget: &wire.Budget{Day: 1, Month: 2, Currency: "USD"}, Spend: &wire.Spend{Currency: "USD"}} |
| 22 | claude := wire.HeldConnection{LocalID: "claude-a", Person: "p", Provider: wire.ProviderClaude, Kind: wire.KindSubscription, State: wire.ConnHealthy} |
| 23 | both := wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{claude, metered}, Capabilities: &wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux}, Research: &wire.ResearchCapability{Addresses: []string{"81.2.69.50"}}}} |
| 24 | hello, _ := wire.NewFrame("h", wire.FrameHello, both) |
| 25 | research, _ := wire.NewFrame("d", wire.FrameDispatch, researchDispatch()) |
| 26 | propose, _ := wire.NewFrame("p", wire.FrameToolCall, wire.ToolCall{CallID: "c", TurnID: "t", Attempt: 1, Name: wire.ToolProposeSpecialist, Args: json.RawMessage(`{"profile":"research-v1"}`)}) |
| 27 | model, _ := wire.NewFrame("m", wire.FrameModel, wire.ModelCommand{Connection: "cn", Person: "p", Model: "m"}) |
| 28 | connect, _ := wire.NewFrame("c", wire.FrameConnect, wire.ConnectCommand{Connection: "cn", Person: "p", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, Billing: wire.BillingMetered}) |
| 29 | |
| 30 | // At 13 everything goes out as it is. |
| 31 | for _, f := range []wire.Frame{hello, research, propose, model, connect} { |
| 32 | if same, err := wire.Encode(f, wire.Protocol13); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 33 | t.Fatalf("%s changed at 13: %v", f.Type, err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // At 12 the Research dispatch and tool are refused; the metered |
| 38 | // frames and the API-key connect still go; the hello loses only its |
| 39 | // Research capability and keeps the metered connection whole. |
| 40 | for _, f := range []wire.Frame{research, propose} { |
| 41 | if _, err := wire.Encode(f, wire.Protocol12); !errors.Is(err, wire.ErrProtocol) { |
| 42 | t.Fatalf("%s at 12: %v", f.Type, err) |
| 43 | } |
| 44 | } |
| 45 | for _, f := range []wire.Frame{model, connect} { |
| 46 | if same, err := wire.Encode(f, wire.Protocol12); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 47 | t.Fatalf("%s changed at 12: %v", f.Type, err) |
| 48 | } |
| 49 | } |
| 50 | at12 := both |
| 51 | at12.Capabilities = &wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux}} |
| 52 | want12, _ := wire.NewFrame("h", wire.FrameHello, at12) |
| 53 | got, err := wire.Encode(hello, wire.Protocol12) |
| 54 | if err != nil || !bytes.Equal(got.Data, want12.Data) { |
| 55 | t.Fatalf("hello at 12: %v\n got %s\nwant %s", err, got.Data, want12.Data) |
| 56 | } |
| 57 | |
| 58 | // At 11 both are downgraded: Research and the metered frames are |
| 59 | // refused, and the hello keeps neither the capability nor the API-key |
| 60 | // connection. |
| 61 | for _, f := range []wire.Frame{research, propose, model, connect} { |
| 62 | if _, err := wire.Encode(f, wire.Protocol11); !errors.Is(err, wire.ErrProtocol) { |
| 63 | t.Fatalf("%s at 11: %v", f.Type, err) |
| 64 | } |
| 65 | } |
| 66 | at11 := at12 |
| 67 | at11.Connections = []wire.HeldConnection{claude} |
| 68 | want11, _ := wire.NewFrame("h", wire.FrameHello, at11) |
| 69 | got, err = wire.Encode(hello, wire.Protocol11) |
| 70 | if err != nil || !bytes.Equal(got.Data, want11.Data) { |
| 71 | t.Fatalf("hello at 11: %v\n got %s\nwant %s", err, got.Data, want11.Data) |
| 72 | } |
| 73 | if strings.Contains(string(got.Data), "api_key") || strings.Contains(string(got.Data), `"research"`) { |
| 74 | t.Fatalf("hello at 11 still carries metered or Research fields: %s", got.Data) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Job lanes at 21: a runner before ProtocolJobs knows the Coordinator |
| 79 | // and the Assistant and no job lane, so a job-lane message is refused |
| 80 | // and the Coordinator's and the Assistant's go unchanged; a sync loses |
| 81 | // its job-lane sessions and keeps the Assistant's. At 22 all of it goes |
| 82 | // out whole. |
| 83 | func TestEncodeJobsBelow22(t *testing.T) { |
| 84 | message := func(lane string) wire.Frame { |
| 85 | f, _ := wire.NewFrame("m", wire.FrameMessage, wire.MessageCommand{Person: "p", TurnID: "t", Attempt: 1, Lane: lane, Text: "hi", PersonText: []string{"hi"}, Tail: []wire.TailMessage{}}) |
| 86 | return f |
| 87 | } |
| 88 | job, coord, assist := message("job:h_a"), message(""), message(wire.LaneAssistant) |
| 89 | session := func(id, lane string) wire.SessionState { |
| 90 | return wire.SessionState{Person: "p", Provider: wire.ProviderClaude, Account: "a", SessionID: id, LastTurn: "t" + id, Lane: lane} |
| 91 | } |
| 92 | coordSession, assistSession := session("s1", ""), session("s2", wire.LaneAssistant) |
| 93 | jobA, jobB := session("s3", "job:h_a"), session("s4", "job:h_b") |
| 94 | all := wire.Sync{Sessions: []wire.SessionState{jobA, coordSession, assistSession, jobB}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}, Connections: []wire.HeldConnection{}} |
| 95 | sync, _ := wire.NewFrame("s", wire.FrameSync, all) |
| 96 | |
| 97 | for _, f := range []wire.Frame{job, coord, assist, sync} { |
| 98 | if same, err := wire.Encode(f, wire.Protocol22); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 99 | t.Fatalf("%s changed at 22: %v", f.Type, err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if _, err := wire.Encode(job, wire.Protocol21); !errors.Is(err, wire.ErrProtocol) { |
| 104 | t.Fatalf("a job-lane message at 21: %v", err) |
| 105 | } |
| 106 | for _, f := range []wire.Frame{coord, assist} { |
| 107 | if same, err := wire.Encode(f, wire.Protocol21); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 108 | t.Fatalf("a message changed at 21: %v\n got %s\nwant %s", err, same.Data, f.Data) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | at21 := all |
| 113 | at21.Sessions = []wire.SessionState{coordSession, assistSession} |
| 114 | wantSync, _ := wire.NewFrame("s", wire.FrameSync, at21) |
| 115 | got, err := wire.Encode(sync, wire.Protocol21) |
| 116 | if err != nil || !bytes.Equal(got.Data, wantSync.Data) || strings.Contains(string(got.Data), "job:") { |
| 117 | t.Fatalf("sync at 21: %v\n got %s\nwant %s", err, got.Data, wantSync.Data) |
| 118 | } |
| 119 | |
| 120 | // A sync with nothing in a job lane goes out at 21 as it is. |
| 121 | plain, _ := wire.NewFrame("s", wire.FrameSync, at21) |
| 122 | if same, err := wire.Encode(plain, wire.Protocol21); err != nil || !bytes.Equal(same.Data, plain.Data) { |
| 123 | t.Fatalf("a sync without job lanes changed at 21: %v", err) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // The Assistant lane at 18: an older runner has no Assistant, so a |
| 128 | // message for it is refused and a coordinator message goes unchanged; a |
| 129 | // forget_task loses keep_preview; a sync loses every session in a lane. |
| 130 | // At 19 all of it goes out whole. |
| 131 | func TestEncodeAssistantBelow19(t *testing.T) { |
| 132 | assist, _ := wire.NewFrame("a", wire.FrameMessage, wire.MessageCommand{Person: "p", TurnID: "t", Attempt: 1, Lane: wire.LaneAssistant, Text: "hi", PersonText: []string{"hi"}, Tail: []wire.TailMessage{}}) |
| 133 | coord, _ := wire.NewFrame("c", wire.FrameMessage, wire.MessageCommand{Person: "p", TurnID: "t", Attempt: 1, Text: "hi", PersonText: []string{"hi"}, Tail: []wire.TailMessage{}}) |
| 134 | forget, _ := wire.NewFrame("f", wire.FrameForgetTask, wire.ForgetTaskCommand{Person: "p", TaskID: "k", KeepPreview: true}) |
| 135 | coordSession := wire.SessionState{Person: "p", Provider: wire.ProviderClaude, Account: "a", SessionID: "s1", LastTurn: "t1"} |
| 136 | assistSession := wire.SessionState{Person: "p", Provider: wire.ProviderClaude, Account: "a", SessionID: "s2", LastTurn: "t2", Lane: wire.LaneAssistant} |
| 137 | both := wire.Sync{Sessions: []wire.SessionState{assistSession, coordSession}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}, Connections: []wire.HeldConnection{}} |
| 138 | sync, _ := wire.NewFrame("s", wire.FrameSync, both) |
| 139 | |
| 140 | for _, f := range []wire.Frame{assist, coord, forget, sync} { |
| 141 | if same, err := wire.Encode(f, wire.Protocol19); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 142 | t.Fatalf("%s changed at 19: %v", f.Type, err) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if _, err := wire.Encode(assist, wire.Protocol18); !errors.Is(err, wire.ErrProtocol) { |
| 147 | t.Fatalf("an Assistant message at 18: %v", err) |
| 148 | } |
| 149 | if same, err := wire.Encode(coord, wire.Protocol18); err != nil || !bytes.Equal(same.Data, coord.Data) { |
| 150 | t.Fatalf("a coordinator message changed at 18: %v", err) |
| 151 | } |
| 152 | |
| 153 | wantForget, _ := wire.NewFrame("f", wire.FrameForgetTask, wire.ForgetTaskCommand{Person: "p", TaskID: "k"}) |
| 154 | got, err := wire.Encode(forget, wire.Protocol18) |
| 155 | if err != nil || !bytes.Equal(got.Data, wantForget.Data) || strings.Contains(string(got.Data), "keep_preview") { |
| 156 | t.Fatalf("forget_task at 18: %v\n got %s\nwant %s", err, got.Data, wantForget.Data) |
| 157 | } |
| 158 | |
| 159 | at18 := both |
| 160 | at18.Sessions = []wire.SessionState{coordSession} |
| 161 | wantSync, _ := wire.NewFrame("s", wire.FrameSync, at18) |
| 162 | got, err = wire.Encode(sync, wire.Protocol18) |
| 163 | if err != nil || !bytes.Equal(got.Data, wantSync.Data) || strings.Contains(string(got.Data), `"lane"`) { |
| 164 | t.Fatalf("sync at 18: %v\n got %s\nwant %s", err, got.Data, wantSync.Data) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // At 16 the Assistant's downgrade is not the last: the models' (18), |
| 169 | // OpenCode's (17) and publishing's (16) run after it. A frame only the |
| 170 | // Assistant's touches still goes out downgraded, so a later downgrade |
| 171 | // that changes nothing never undoes it. A sync whose every session is in |
| 172 | // a lane keeps its sessions as [], never null. |
| 173 | func TestEncodeAssistantBelow16(t *testing.T) { |
| 174 | forget, _ := wire.NewFrame("f", wire.FrameForgetTask, wire.ForgetTaskCommand{Person: "p", TaskID: "k", KeepPreview: true}) |
| 175 | wantForget, _ := wire.NewFrame("f", wire.FrameForgetTask, wire.ForgetTaskCommand{Person: "p", TaskID: "k"}) |
| 176 | assistSession := wire.SessionState{Person: "p", Provider: wire.ProviderClaude, Account: "a", SessionID: "s2", LastTurn: "t2", Lane: wire.LaneAssistant} |
| 177 | only := wire.Sync{Sessions: []wire.SessionState{assistSession}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}, Connections: []wire.HeldConnection{}} |
| 178 | sync, _ := wire.NewFrame("s", wire.FrameSync, only) |
| 179 | none := only |
| 180 | none.Sessions = []wire.SessionState{} |
| 181 | wantSync, _ := wire.NewFrame("s", wire.FrameSync, none) |
| 182 | |
| 183 | // Without their Assistant parts the frames go out at 16 unchanged: |
| 184 | // no other downgrade has anything to take from them. |
| 185 | for _, f := range []wire.Frame{wantForget, wantSync} { |
| 186 | if same, err := wire.Encode(f, wire.Protocol16); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 187 | t.Fatalf("%s changed at 16: %v\n got %s\nwant %s", f.Type, err, same.Data, f.Data) |
| 188 | } |
| 189 | } |
| 190 | for _, c := range []struct{ f, want wire.Frame }{{forget, wantForget}, {sync, wantSync}} { |
| 191 | got, err := wire.Encode(c.f, wire.Protocol16) |
| 192 | if err != nil || !bytes.Equal(got.Data, c.want.Data) { |
| 193 | t.Fatalf("%s at 16: %v\n got %s\nwant %s", c.f.Type, err, got.Data, c.want.Data) |
| 194 | } |
| 195 | } |
| 196 | got, err := wire.Encode(sync, wire.Protocol16) |
| 197 | if err != nil || !strings.Contains(string(got.Data), `"sessions":[]`) { |
| 198 | t.Fatalf("an all-Assistant sync at 16: %s %v", got.Data, err) |
| 199 | } |
| 200 | } |
| 201 | |