oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire_test
2
3import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// frozen16Providers are the API-key providers a protocol 16 or older
14// peer knows (S1's table): any other provider value is one it cannot
15// read.
16var frozen16Providers = map[string]bool{wire.ProviderTensorX: true}
17
18// frozen16Harnesses are the harnesses of API-key connections at 16.
19var frozen16Harnesses = map[string]bool{"": true, wire.HarnessPi: true}
20
21// providersIn are every provider and harness value a frame's body names,
22// at any depth: connections, usage rows and reports.
23func providersIn(t *testing.T, raw []byte) (providers, harnesses []string) {
24 t.Helper()
25 var walk func(v any)
26 walk = func(v any) {
27 switch x := v.(type) {
28 case map[string]any:
29 for k, val := range x {
30 if s, ok := val.(string); ok {
31 switch k {
32 case "provider":
33 providers = append(providers, s)
34 case "harness":
35 harnesses = append(harnesses, s)
36 }
37 }
38 walk(val)
39 }
40 case []any:
41 for _, e := range x {
42 walk(e)
43 }
44 }
45 }
46 var v any
47 if err := json.Unmarshal(raw, &v); err != nil {
48 t.Fatal(err)
49 }
50 walk(v)
51 return providers, harnesses
52}
53
54// Protocol 17 (ProtocolOpenCode) adds the OpenCode Go provider behind the
55// OpenCode harness (connectors spec §4.2, slice S5). Below it, nothing an
56// older peer is sent names that provider or harness: its connections
57// leave hello and sync, its usage rows leave every report, and a connect
58// or a report about one is refused. At 17 nothing changes.
59func TestEncodeBelowProtocol17DropsOpenCode(t *testing.T) {
60 if wire.ProtocolOpenCode != wire.Protocol17 || wire.ProtocolCurrent != wire.ProtocolDesign {
61 t.Fatalf("opencode %d, current %d", wire.ProtocolOpenCode, wire.ProtocolCurrent)
62 }
63 budget := &wire.Budget{Day: 1_000_000, Month: 10_000_000, Currency: "USD", Revision: 1}
64 spend := &wire.Spend{Currency: "USD"}
65 held := []wire.HeldConnection{
66 {LocalID: "tensorx-a", 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: budget, Spend: spend},
67 {LocalID: "opencode-go-b", Person: "p", Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Harness: wire.HarnessOpenCode, Upstream: "https://opencode.ai", Billing: wire.BillingMetered, Model: "glm-5.3", Budget: budget, Spend: spend},
68 }
69 n := int64(10)
70 rows := []wire.Usage{{Provider: wire.ProviderTensorX, Connection: "tensorx-a", Model: "z-ai/glm-5.3", InputTokens: &n, OutputTokens: &n, Complete: true}, {Provider: wire.ProviderOpenCodeGo, Connection: "opencode-go-b", Model: "glm-5.3", InputTokens: &n, OutputTokens: &n, Complete: true}}
71 frame := func(id, typ string, v any) wire.Frame {
72 f, err := wire.NewFrame(id, typ, v)
73 if err != nil {
74 t.Fatal(err)
75 }
76 return f
77 }
78 kept := []wire.Frame{
79 frame("h", wire.FrameHello, wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: held, Protocol: wire.ProtocolCurrent}),
80 frame("s", wire.FrameSync, wire.Sync{Connections: held}),
81 frame("u", wire.FrameUsage, wire.UsageReport{Rows: rows}),
82 frame("st", wire.FrameStep, wire.StepReport{TaskID: "t", StepID: "s", Attempt: 1, State: wire.StepDone, Usage: rows}),
83 frame("a", wire.FrameAnswer, wire.AnswerReport{TaskID: "t", QuestionID: "q", Attempt: 1, State: wire.QuestionAnswered, Text: "ok", Usage: rows}),
84 }
85 for _, f := range kept {
86 if same, err := wire.Encode(f, wire.Protocol17); err != nil || string(same.Data) != string(f.Data) {
87 t.Fatalf("%s changed at 17: %v", f.Type, err)
88 }
89 for _, p := range []int{wire.Protocol16, wire.Protocol15} {
90 old, err := wire.Encode(f, p)
91 if err != nil {
92 t.Fatalf("%s at %d: %v", f.Type, p, err)
93 }
94 providers, harnesses := providersIn(t, old.Data)
95 sawTensorX := false
96 for _, name := range providers {
97 if _, apiKey := wire.APIKeyProviders[name]; apiKey && !frozen16Providers[name] {
98 t.Fatalf("%s at %d names provider %q, which an older peer cannot read: %s", f.Type, p, name, old.Data)
99 }
100 sawTensorX = sawTensorX || name == wire.ProviderTensorX
101 }
102 if !sawTensorX {
103 t.Fatalf("%s at %d lost the TensorX entry too: %s", f.Type, p, old.Data)
104 }
105 for _, h := range harnesses {
106 if !frozen16Harnesses[h] {
107 t.Fatalf("%s at %d names harness %q: %s", f.Type, p, h, old.Data)
108 }
109 }
110 if _, _, err := wire.ParseFrame(mustFrame(t, old)); err != nil {
111 t.Fatalf("%s at %d does not parse: %v", f.Type, p, err)
112 }
113 }
114 }
115 if len(held) != 2 || len(rows) != 2 {
116 t.Fatal("Encode changed the caller's values")
117 }
118 refused := []wire.Frame{
119 frame("c", wire.FrameConnect, wire.ConnectCommand{Connection: "c1", Person: "p", Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, Billing: wire.BillingMetered, Budget: budget}),
120 frame("r", wire.FrameConnection, wire.ConnectionReport{Connection: "c1", Person: "p", Provider: wire.ProviderOpenCodeGo, State: wire.ConnHealthy}),
121 frame("r2", wire.FrameConnection, wire.ConnectionReport{Person: "p", LocalID: "opencode-go-b", Provider: wire.ProviderOpenCodeGo, State: wire.ConnSignInAgain}),
122 }
123 for _, f := range refused {
124 if _, err := wire.Encode(f, wire.Protocol17); err != nil {
125 t.Fatalf("%s refused at 17: %v", f.Type, err)
126 }
127 for _, p := range []int{wire.Protocol16, wire.Protocol15} {
128 if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) {
129 t.Fatalf("%s about OpenCode Go encoded at %d: %v", f.Type, p, err)
130 }
131 }
132 }
133 // A TensorX connect still goes to a protocol 16 or 15 runner.
134 tx := frame("c", wire.FrameConnect, wire.ConnectCommand{Connection: "c1", Person: "p", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, Billing: wire.BillingMetered, Budget: budget})
135 for _, p := range []int{wire.Protocol16, wire.Protocol15} {
136 if _, err := wire.Encode(tx, p); err != nil {
137 t.Fatalf("a TensorX connect refused at %d: %v", p, err)
138 }
139 }
140}
141
142// Mixed versions across OpenCode (17) and publishing (16), on the same
143// frames. A protocol 16 runner is never sent an OpenCode Go connect and
144// never names that provider, yet keeps every publish frame and field
145// whole. At 17 both go out unchanged. At 15 both are downgraded: the
146// OpenCode Go connect and the publish dispatch are refused, and the
147// OpenCode Go rows and the publish result and sites capability dropped.
148func TestEncodeMixedVersionsOpenCodeAndPublish(t *testing.T) {
149 frame := func(id, typ string, v any) wire.Frame {
150 f, err := wire.NewFrame(id, typ, v)
151 if err != nil {
152 t.Fatal(err)
153 }
154 return f
155 }
156 budget := &wire.Budget{Day: 1_000_000, Month: 10_000_000, Currency: "USD", Revision: 1}
157 n := int64(10)
158 rows := []wire.Usage{{Provider: wire.ProviderTensorX, Connection: "tensorx-a", Model: "z-ai/glm-5.3", InputTokens: &n, OutputTokens: &n, Complete: true}, {Provider: wire.ProviderOpenCodeGo, Connection: "opencode-go-b", Model: "glm-5.3", InputTokens: &n, OutputTokens: &n, Complete: true}}
159 held := []wire.HeldConnection{{LocalID: "opencode-go-b", Person: "p", Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Harness: wire.HarnessOpenCode, Upstream: "https://opencode.ai", Billing: wire.BillingMetered, Model: "glm-5.3", Budget: budget, Spend: &wire.Spend{Currency: "USD"}}}
160 connect := frame("c", wire.FrameConnect, wire.ConnectCommand{Connection: "c1", Person: "p", Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, Billing: wire.BillingMetered, Budget: budget})
161 publish := frame("d", wire.FrameDispatch, wire.DispatchCommand{Person: "p", TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistPublish, Findings: []wire.Finding{}, CandidateSHA: strings.Repeat("e", 40), Publish: &wire.PublishDispatch{App: "site-sample", Into: "main"}})
162 check := frame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s", Attempt: 1, Purpose: wire.LeasePurposePublish, Digest: strings.Repeat("a", 64)})
163 built := wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}, Usage: rows, Publish: &wire.PublishResult{Phase: wire.PublishBuilt, Digest: strings.Repeat("a", 64), Files: 2, Bytes: 40}}
164 report := frame("r", wire.FrameStep, built)
165 hello := frame("h", wire.FrameHello, wire.Hello{Protocol: wire.ProtocolCurrent, Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: held, Capabilities: &wire.Capabilities{Sites: true, Tags: []string{wire.TagLinux}}})
166
167 // At 17 both go out whole, but a publish dispatch, place-bound since
168 // 31, is refused below 31.
169 for _, f := range []wire.Frame{publish, check} {
170 if _, err := wire.Encode(f, wire.Protocol17); !errors.Is(err, wire.ErrProtocol) {
171 t.Fatalf("a publish %s encoded at 17: %v", f.Type, err)
172 }
173 }
174 for _, f := range []wire.Frame{connect, report, hello} {
175 if same, err := wire.Encode(f, wire.Protocol17); err != nil || !bytes.Equal(same.Data, f.Data) {
176 t.Fatalf("%s changed at 17: %v", f.Type, err)
177 }
178 }
179
180 // At 16 OpenCode Go is refused or dropped; publish is untouched.
181 if _, err := wire.Encode(connect, wire.Protocol16); !errors.Is(err, wire.ErrProtocol) {
182 t.Fatalf("an OpenCode Go connect encoded at 16: %v", err)
183 }
184 got, err := wire.Encode(report, wire.Protocol16)
185 if err != nil || strings.Contains(string(got.Data), wire.ProviderOpenCodeGo) || !strings.Contains(string(got.Data), `"publish":`) || !strings.Contains(string(got.Data), wire.ProviderTensorX) {
186 t.Fatalf("report at 16 (OpenCode Go rows out, publish result kept): %v %s", err, got.Data)
187 }
188 got, err = wire.Encode(hello, wire.Protocol16)
189 if err != nil || strings.Contains(string(got.Data), wire.ProviderOpenCodeGo) || !strings.Contains(string(got.Data), `"sites":true`) {
190 t.Fatalf("hello at 16 (OpenCode Go out, sites kept): %v %s", err, got.Data)
191 }
192
193 // At 15 both are downgraded, OpenCode's step first, then publish's.
194 for _, f := range []wire.Frame{connect, publish, check} {
195 if _, err := wire.Encode(f, wire.Protocol15); !errors.Is(err, wire.ErrProtocol) {
196 t.Fatalf("%s encoded at 15: %v", f.Type, err)
197 }
198 }
199 got, err = wire.Encode(report, wire.Protocol15)
200 if err != nil || strings.Contains(string(got.Data), wire.ProviderOpenCodeGo) || strings.Contains(string(got.Data), `"publish":`) || !strings.Contains(string(got.Data), wire.ProviderTensorX) {
201 t.Fatalf("report at 15 (OpenCode Go rows and publish result out): %v %s", err, got.Data)
202 }
203 got, err = wire.Encode(hello, wire.Protocol15)
204 if err != nil || strings.Contains(string(got.Data), wire.ProviderOpenCodeGo) || strings.Contains(string(got.Data), `"sites"`) {
205 t.Fatalf("hello at 15 (OpenCode Go and sites out): %v %s", err, got.Data)
206 }
207 if _, _, err := wire.ParseFrame(mustFrame(t, got)); err != nil {
208 t.Fatalf("hello at 15 does not parse: %v", err)
209 }
210}
211
212// OpenCode Go is metered whatever the person says (§0 decision 7: no
213// standing overflow-off setting is confirmed), so a pay-as-you-go
214// confirmation is refused and the limit confirmation is required. Its
215// models list and its chat completions live under different paths.
216func TestOpenCodeGoProvider(t *testing.T) {
217 p, ok := wire.APIKeyProviders[wire.ProviderOpenCodeGo]
218 if !ok {
219 t.Fatal("no OpenCode Go row")
220 }
221 if p.Harness != wire.HarnessOpenCode || p.Upstream != "https://opencode.ai" || p.Endpoint != "https://opencode.ai:443" || p.Currency != "USD" {
222 t.Fatalf("row %+v", p)
223 }
224 if p.Billing(true) != wire.BillingMetered || p.Billing(false) != wire.BillingMetered {
225 t.Fatal("OpenCode Go not always metered")
226 }
227 if p.CheckConfirmations(true, true) == nil {
228 t.Fatal("a pay-as-you-go confirmation accepted for OpenCode Go")
229 }
230 if p.CheckConfirmations(false, false) == nil {
231 t.Fatal("OpenCode Go accepted without the limit confirmation")
232 }
233 if p.CheckConfirmations(true, false) != nil {
234 t.Fatal("OpenCode Go refused with the limit confirmed")
235 }
236 if p.ModelsURL() != "https://opencode.ai/inference/go/v1/models" || p.CompletionsURL() != "https://opencode.ai/inference/go/openai/v1/chat/completions" || p.BaseURL() != "https://opencode.ai/inference/go/openai/v1" {
237 t.Fatalf("paths %s %s %s", p.ModelsURL(), p.CompletionsURL(), p.BaseURL())
238 }
239 tx := wire.APIKeyProviders[wire.ProviderTensorX]
240 if tx.ModelsURL() != "https://api.tensorx.ai/v1/models" || tx.CompletionsURL() != "https://api.tensorx.ai/v1/chat/completions" {
241 t.Fatalf("TensorX paths %s %s", tx.ModelsURL(), tx.CompletionsURL())
242 }
243}
244