| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "amadan.net/oficina/equipo/internal/wire/wiretest" |
| 14 | ) |
| 15 | |
| 16 | // Slice F (Mac Engineering) is protocol 37, one above lookups at 36 |
| 17 | // (ProtocolLookup), so a peer at 36 has lookups but is below Mac |
| 18 | // Engineering. The values are written out so that moving a constant |
| 19 | // fails here. |
| 20 | func TestProtocol37Constants(t *testing.T) { |
| 21 | if wire.Protocol37 != 37 || wire.ProtocolMacEngineering != wire.Protocol37 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.ProtocolChangeDiffs != 35 || wire.ProtocolLookup != 36 { |
| 22 | t.Fatalf("mac engineering %d, current %d", wire.ProtocolMacEngineering, wire.ProtocolCurrent) |
| 23 | } |
| 24 | if wire.Spoken(99) != 38 || wire.Spoken(37) != 37 || wire.Spoken(36) != 36 || wire.Spoken(35) != 35 { |
| 25 | t.Fatal("spoken protocol") |
| 26 | } |
| 27 | for kind, want := range map[string]bool{wire.FramePrepareBase: true, wire.FrameBase: true} { |
| 28 | if (wire.Frame{Type: kind}).Durable() != want { |
| 29 | t.Fatalf("%s durable=%v", kind, !want) |
| 30 | } |
| 31 | if wire.Speaks(36, kind) || !wire.Speaks(37, kind) { |
| 32 | t.Fatalf("%s spoken below 37 or not at 37", kind) |
| 33 | } |
| 34 | } |
| 35 | for got, want := range map[string]string{ |
| 36 | wire.FramePrepareBase: "prepare_base", |
| 37 | wire.FrameBase: "base", |
| 38 | wire.TransferKindEngineering: "engineering", |
| 39 | wire.XcodeAccessSandboxed: "sandboxed", |
| 40 | wire.OutcomeCommitted: "committed", |
| 41 | wire.OutcomeUnfinished: "unfinished", |
| 42 | wire.OutcomeNoShell: "no_shell", |
| 43 | wire.ReasonSecretInCandidate: "secret_in_candidate", |
| 44 | wire.ReasonWorkspaceTooLarge: "workspace_too_large", |
| 45 | wire.ReasonEngineeringUnavailable: "engineering_unavailable", |
| 46 | wire.ReasonBaseUnavailable: "base_unavailable", |
| 47 | wire.PlacementMacConnection: "waiting_mac_connection", |
| 48 | wire.PlacementMacUpdate: "waiting_mac_update", |
| 49 | wire.PlacementBase: "waiting_base", |
| 50 | wire.NeedsMacConnection: "mac_connection", |
| 51 | wire.NeedsMacUpdate: "mac_update", |
| 52 | } { |
| 53 | if got != want { |
| 54 | t.Errorf("constant %q, want %q", got, want) |
| 55 | } |
| 56 | } |
| 57 | states := []string{"ready", "no_connection", "signed_out", "limited", "probes_pending", "probes_failed", "cli_unpinned", "home_open", "tainted"} |
| 58 | if !slices.Equal(wire.EngineeringStates, states) || slices.Contains(wire.EngineeringStates, "busy") || wire.EngineeringReady != "ready" { |
| 59 | t.Fatalf("engineering states %v", wire.EngineeringStates) |
| 60 | } |
| 61 | if !slices.Equal(wire.EngineeringProbeIDs, []string{"F1", "F2", "F3", "F4", "F5", "F6", "F7"}) { |
| 62 | t.Fatalf("engineering probes %v", wire.EngineeringProbeIDs) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func keysOf(t *testing.T, raw []byte) map[string]bool { |
| 67 | t.Helper() |
| 68 | var m map[string]json.RawMessage |
| 69 | if err := json.Unmarshal(raw, &m); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | out := map[string]bool{} |
| 73 | for k := range m { |
| 74 | out[k] = true |
| 75 | } |
| 76 | return out |
| 77 | } |
| 78 | |
| 79 | // Every field slice F adds is dropped for a peer below 37 (the protocol |
| 80 | // 35 primary and the 36 specialists build), every new frame and every |
| 81 | // Engineering transfer is refused there, and at 37 each fixture goes out |
| 82 | // byte for byte. Where an older fixture records the same frame, the |
| 83 | // downgraded frame has exactly its keys. |
| 84 | func TestEncodeDropsMacEngineeringBelow37(t *testing.T) { |
| 85 | drops := []struct { |
| 86 | name, older string |
| 87 | gone []string |
| 88 | }{ |
| 89 | {"frames/dispatch-mac-engineering.json", "frames/dispatch-needs.json", []string{"xcode_access"}}, |
| 90 | {"frames/step-mac-engineering.json", "", []string{"xcode_access", "session_id", "outcome_phase"}}, |
| 91 | {"frames/published-change-files.json", "frames/published.json", []string{"change_files", "diff_collection", "outcome_phase"}}, |
| 92 | {"frames/ask-pinned.json", "frames/ask.json", []string{"eng_attempt", "eng_account", "eng_session"}}, |
| 93 | } |
| 94 | for _, d := range drops { |
| 95 | f, _, err := wire.ParseFrame(wiretest.Fixture(d.name)) |
| 96 | if err != nil { |
| 97 | t.Fatalf("%s: %v", d.name, err) |
| 98 | } |
| 99 | if same, err := wire.Encode(f, 37); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 100 | t.Fatalf("%s changed at 37: %v", d.name, err) |
| 101 | } |
| 102 | for _, p := range []int{35, 36} { |
| 103 | old, err := wire.Encode(f, p) |
| 104 | if err != nil { |
| 105 | t.Fatalf("%s at %d: %v", d.name, p, err) |
| 106 | } |
| 107 | keys := keysOf(t, old.Data) |
| 108 | for _, k := range d.gone { |
| 109 | if keys[k] { |
| 110 | t.Errorf("%s kept %s at %d", d.name, k, p) |
| 111 | } |
| 112 | } |
| 113 | want := keysOf(t, f.Data) |
| 114 | for _, k := range d.gone { |
| 115 | delete(want, k) |
| 116 | } |
| 117 | for k := range want { |
| 118 | if !keys[k] { |
| 119 | t.Errorf("%s lost %s at %d", d.name, k, p) |
| 120 | } |
| 121 | } |
| 122 | raw, _ := json.Marshal(old) |
| 123 | if d.older != "" { |
| 124 | if err := wiretest.SameShape(wiretest.Fixture(d.older), raw); err != nil { |
| 125 | t.Errorf("%s at %d is not %s: %v\n%s", d.name, p, d.older, err, raw) |
| 126 | } |
| 127 | } |
| 128 | if _, _, err := wire.ParseFrame(raw); err != nil { |
| 129 | t.Errorf("%s at %d does not parse: %v", d.name, p, err) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // The worker's hello: the Engineering status and the F probes go; the |
| 135 | // P probes stay, and what is left is a capability report an older |
| 136 | // instance accepts. |
| 137 | f, _, err := wire.ParseFrame(wiretest.Fixture("frames/hello-worker-conns.json")) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if same, err := wire.Encode(f, 37); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 142 | t.Fatalf("hello changed at 37: %v", err) |
| 143 | } |
| 144 | for _, p := range []int{35, 36} { |
| 145 | old, err := wire.Encode(f, p) |
| 146 | if err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if bytes.Contains(old.Data, []byte(`"engineering"`)) || bytes.Contains(old.Data, []byte(`"F`)) || !bytes.Contains(old.Data, []byte(`"P2"`)) { |
| 150 | t.Fatalf("hello at %d: %s", p, old.Data) |
| 151 | } |
| 152 | var h wire.Hello |
| 153 | if err := wire.Strict(old.Data, &h); err != nil || h.Capabilities == nil || len(h.Capabilities.Probes) != 7 || wire.ValidCapabilities(*h.Capabilities) != nil { |
| 154 | t.Fatalf("hello at %d: %v %+v", p, err, h.Capabilities) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Refused below 37: the new frames, an Engineering transfer's chunks |
| 159 | // and its pack request, a publish carrying the worker's outcome only |
| 160 | // as far as it is dropped, and a diff upload bound to a publication. |
| 161 | refused := map[string]wire.Frame{} |
| 162 | for _, name := range []string{"frames/prepare-base.json", "frames/base.json", "frames/blob-engineering.json"} { |
| 163 | f, _, err := wire.ParseFrame(wiretest.Fixture(name)) |
| 164 | if err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | refused[name] = f |
| 168 | } |
| 169 | header := wire.TransferHeader{ID: "tr_1", Direction: wire.TransferInbound, Repo: "amadan.net/oficina/sample", TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: strings.Repeat("a", 40), Kind: wire.TransferKindEngineering} |
| 170 | refused["send_pack engineering"], _ = wire.NewFrame("sp", wire.FrameSendPack, wire.SendPackCommand{Transfer: header, Have: []string{}}) |
| 171 | old := header |
| 172 | old.Kind, old.OldBaseSHA = "", strings.Repeat("b", 40) |
| 173 | refused["blob old base"], _ = wire.NewFrame("b", wire.FrameBlob, wire.Blob{Transfer: old}) |
| 174 | refused["change_diff publication"], _ = wire.NewFrame("cd", wire.FrameChangeDiff, wire.ChangeDiffUpload{Publication: "pb_1"}) |
| 175 | for name, f := range refused { |
| 176 | for _, p := range []int{wire.Protocol1, 2, 35, 36} { |
| 177 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 178 | t.Errorf("%s encoded at %d: %v", name, p, err) |
| 179 | } |
| 180 | } |
| 181 | if same, err := wire.Encode(f, 37); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 182 | t.Errorf("%s changed at 37: %v", name, err) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // A publish's outcome is dropped, the rest kept. |
| 187 | pub, _ := wire.NewFrame("pub", wire.FramePublish, wire.PublishCommand{Publication: "pb_1", OutcomePhase: wire.OutcomeUnfinished}) |
| 188 | for _, p := range []int{35, 36} { |
| 189 | got, err := wire.Encode(pub, p) |
| 190 | if err != nil || bytes.Contains(got.Data, []byte(`"outcome_phase"`)) || !bytes.Contains(got.Data, []byte(`"pb_1"`)) { |
| 191 | t.Fatalf("publish at %d: %v %s", p, err, got.Data) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Today's frames are untouched below 37: a checks transfer and a diff |
| 196 | // upload with no publication. |
| 197 | for _, name := range []string{"frames/blob.json", "frames/send_pack.json", "frames/publish.json", "frames/published.json", "frames/ask.json", "frames/dispatch-needs.json", "frames/step-process-group.json"} { |
| 198 | f, _, err := wire.ParseFrame(wiretest.Fixture(name)) |
| 199 | if err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | for _, p := range []int{35, 36} { |
| 203 | if got, err := wire.Encode(f, p); err != nil || !bytes.Equal(got.Data, f.Data) { |
| 204 | t.Errorf("%s changed at %d: %v", name, p, err) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | plain, _ := wire.NewFrame("cd", wire.FrameChangeDiff, wire.ChangeDiffUpload{}) |
| 209 | if _, err := wire.Encode(plain, 35); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // A frame may carry a slice F key with an empty, zero or null value (a |
| 215 | // peer that writes the key without omitempty, or a hand-built frame). Below |
| 216 | // 37 the key itself must go, not only a set value: an older peer's strict |
| 217 | // decoder refuses any key it does not know, whatever its value. |
| 218 | func TestEncodeDropsEmptyMacEngineeringKeysBelow37(t *testing.T) { |
| 219 | with := func(name string, at []string, extra map[string]any) wire.Frame { |
| 220 | t.Helper() |
| 221 | var frame map[string]any |
| 222 | if err := json.Unmarshal(wiretest.Fixture(name), &frame); err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | obj := frame["data"].(map[string]any) |
| 226 | for _, k := range at { |
| 227 | obj = obj[k].(map[string]any) |
| 228 | } |
| 229 | for k, v := range extra { |
| 230 | obj[k] = v |
| 231 | } |
| 232 | raw, _ := json.Marshal(frame) |
| 233 | f, _, err := wire.ParseFrame(raw) |
| 234 | if err != nil { |
| 235 | t.Fatalf("%s with %v: %v", name, extra, err) |
| 236 | } |
| 237 | return f |
| 238 | } |
| 239 | diff, _ := wire.NewFrame("cd", wire.FrameChangeDiff, wire.ChangeDiffUpload{}) |
| 240 | var diffFrame map[string]any |
| 241 | raw, _ := json.Marshal(diff) |
| 242 | _ = json.Unmarshal(raw, &diffFrame) |
| 243 | diffFrame["data"].(map[string]any)["publication"] = "" |
| 244 | raw, _ = json.Marshal(diffFrame) |
| 245 | emptyDiff, _, err := wire.ParseFrame(raw) |
| 246 | if err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | for _, c := range []struct { |
| 250 | name string |
| 251 | frame wire.Frame |
| 252 | keys []string |
| 253 | }{ |
| 254 | {"dispatch", with("frames/dispatch-needs.json", nil, map[string]any{"xcode_access": ""}), []string{"xcode_access"}}, |
| 255 | {"step", with("frames/step-process-group.json", nil, map[string]any{"xcode_access": "", "session_id": "", "outcome_phase": ""}), []string{"xcode_access", "session_id", "outcome_phase"}}, |
| 256 | {"publish", with("frames/publish.json", nil, map[string]any{"outcome_phase": ""}), []string{"outcome_phase"}}, |
| 257 | {"published", with("frames/published.json", nil, map[string]any{"change_files": nil, "diff_collection": "", "outcome_phase": ""}), []string{"change_files", "diff_collection", "outcome_phase"}}, |
| 258 | {"ask", with("frames/ask.json", nil, map[string]any{"eng_attempt": 0, "eng_account": "", "eng_session": ""}), []string{"eng_attempt", "eng_account", "eng_session"}}, |
| 259 | {"blob", with("frames/blob.json", []string{"transfer"}, map[string]any{"kind": "", "old_base_sha": ""}), []string{"kind", "old_base_sha"}}, |
| 260 | {"send_pack", with("frames/send_pack.json", []string{"transfer"}, map[string]any{"kind": "", "old_base_sha": ""}), []string{"kind", "old_base_sha"}}, |
| 261 | {"hello", with("frames/hello-mac-probes.json", []string{"capabilities"}, map[string]any{"engineering": nil}), []string{"engineering"}}, |
| 262 | {"change_diff", emptyDiff, []string{"publication"}}, |
| 263 | // Slice F task 11: the credential hold, and the runner a sign-out |
| 264 | // names, set or empty. |
| 265 | {"hello hold", with("frames/hello-mac-probes.json", []string{"capabilities"}, map[string]any{"model_hold": true, "holding_items": []string{"Claude Code-credentials"}}), []string{"model_hold", "holding_items"}}, |
| 266 | {"hello inventory", with("frames/hello-mac-probes.json", nil, map[string]any{"inventory": true}), []string{"inventory"}}, |
| 267 | {"disconnect", with("frames/disconnect.json", nil, map[string]any{"runner_id": ""}), []string{"runner_id"}}, |
| 268 | {"disconnect named", with("frames/disconnect.json", nil, map[string]any{"runner_id": "run_1"}), []string{"runner_id"}}, |
| 269 | } { |
| 270 | for _, p := range []int{35, 36} { |
| 271 | got, err := wire.Encode(c.frame, p) |
| 272 | if err != nil { |
| 273 | t.Errorf("%s at %d: %v", c.name, p, err) |
| 274 | continue |
| 275 | } |
| 276 | for _, k := range c.keys { |
| 277 | if bytes.Contains(got.Data, []byte(`"`+k+`"`)) { |
| 278 | t.Errorf("%s at %d kept the empty %s: %s", c.name, p, k, got.Data) |
| 279 | } |
| 280 | } |
| 281 | out, _ := json.Marshal(got) |
| 282 | if _, _, err := wire.ParseFrame(out); err != nil { |
| 283 | t.Errorf("%s at %d does not parse: %v", c.name, p, err) |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // A worker's hello carries its connections and seal key only at 37: the |
| 290 | // instance ignores both from a worker below it. |
| 291 | func TestWithoutWorkerConnections(t *testing.T) { |
| 292 | _, body, err := wire.ParseFrame(wiretest.Fixture("frames/hello-worker-conns.json")) |
| 293 | if err != nil { |
| 294 | t.Fatal(err) |
| 295 | } |
| 296 | h := *body.(*wire.Hello) |
| 297 | if len(h.Connections) != 1 || h.SealKey == "" { |
| 298 | t.Fatal("fixture") |
| 299 | } |
| 300 | at37 := h |
| 301 | if wire.WithoutWorkerConnections(&at37, 37) || len(at37.Connections) != 1 || at37.SealKey == "" { |
| 302 | t.Fatal("dropped at 37") |
| 303 | } |
| 304 | for _, p := range []int{1, 2, 35, 36} { |
| 305 | old := h |
| 306 | if !wire.WithoutWorkerConnections(&old, p) || len(old.Connections) != 0 || old.Connections == nil || old.SealKey != "" { |
| 307 | t.Fatalf("kept at %d: %+v", p, old) |
| 308 | } |
| 309 | if wire.WithoutWorkerConnections(&old, p) { |
| 310 | t.Fatal("reported a change twice") |
| 311 | } |
| 312 | } |
| 313 | if len(h.Connections) != 1 { |
| 314 | t.Fatal("mutated the caller's connections") |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | func TestTransferHeaderCommits(t *testing.T) { |
| 319 | a, b, c := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("c", 64) |
| 320 | base := wire.TransferHeader{ID: "tr_1", Direction: wire.TransferInbound, Repo: "amadan.net/oficina/sample", TaskID: "tk1", StepID: "s1", Attempt: 1} |
| 321 | if wire.MaxTransferCommits != 3 { |
| 322 | t.Fatalf("bound %d", wire.MaxTransferCommits) |
| 323 | } |
| 324 | for _, tc := range []struct { |
| 325 | name string |
| 326 | edit func(*wire.TransferHeader) |
| 327 | commits []string |
| 328 | valid bool |
| 329 | }{ |
| 330 | {"checks", func(h *wire.TransferHeader) { h.BaseSHA, h.CandidateSHA = a, b }, []string{b}, true}, |
| 331 | {"checks without base", func(h *wire.TransferHeader) { h.CandidateSHA = b }, []string{b}, true}, |
| 332 | {"checks outbound", func(h *wire.TransferHeader) { h.Direction, h.BaseSHA, h.CandidateSHA = wire.TransferOutbound, a, b }, []string{b}, true}, |
| 333 | {"checks without candidate", func(h *wire.TransferHeader) { h.BaseSHA = a }, []string{}, false}, |
| 334 | {"checks with old base", func(h *wire.TransferHeader) { h.BaseSHA, h.OldBaseSHA, h.CandidateSHA = a, c, b }, []string{b}, false}, |
| 335 | {"engineering first", func(h *wire.TransferHeader) { h.Kind, h.BaseSHA = wire.TransferKindEngineering, a }, []string{a}, true}, |
| 336 | {"engineering rebase", func(h *wire.TransferHeader) { |
| 337 | h.Kind, h.BaseSHA, h.OldBaseSHA, h.CandidateSHA = wire.TransferKindEngineering, a, c, b |
| 338 | }, []string{a, c, b}, true}, |
| 339 | {"engineering rebase, base unchanged", func(h *wire.TransferHeader) { |
| 340 | h.Kind, h.BaseSHA, h.OldBaseSHA, h.CandidateSHA = wire.TransferKindEngineering, a, a, b |
| 341 | }, []string{a, b}, true}, |
| 342 | {"engineering rebase onto candidate", func(h *wire.TransferHeader) { |
| 343 | h.Kind, h.BaseSHA, h.OldBaseSHA, h.CandidateSHA = wire.TransferKindEngineering, b, a, b |
| 344 | }, []string{b, a}, true}, |
| 345 | {"engineering without base", func(h *wire.TransferHeader) { h.Kind, h.CandidateSHA = wire.TransferKindEngineering, b }, []string{b}, false}, |
| 346 | {"engineering candidate without old base", func(h *wire.TransferHeader) { |
| 347 | h.Kind, h.BaseSHA, h.CandidateSHA = wire.TransferKindEngineering, a, b |
| 348 | }, []string{a, b}, false}, |
| 349 | {"engineering old base without candidate", func(h *wire.TransferHeader) { |
| 350 | h.Kind, h.BaseSHA, h.OldBaseSHA = wire.TransferKindEngineering, a, c |
| 351 | }, []string{a, c}, false}, |
| 352 | {"engineering outbound", func(h *wire.TransferHeader) { |
| 353 | h.Kind, h.Direction, h.BaseSHA = wire.TransferKindEngineering, wire.TransferOutbound, a |
| 354 | }, []string{a}, false}, |
| 355 | {"unknown kind", func(h *wire.TransferHeader) { h.Kind, h.BaseSHA = "design", a }, []string{a}, false}, |
| 356 | {"short sha", func(h *wire.TransferHeader) { h.Kind, h.BaseSHA = wire.TransferKindEngineering, "abc123" }, []string{"abc123"}, false}, |
| 357 | {"upper-case sha", func(h *wire.TransferHeader) { h.Kind, h.BaseSHA = wire.TransferKindEngineering, strings.ToUpper(a) }, []string{strings.ToUpper(a)}, false}, |
| 358 | {"option-like sha", func(h *wire.TransferHeader) { h.BaseSHA, h.CandidateSHA = a, "--all" }, []string{"--all"}, false}, |
| 359 | {"bad base on checks", func(h *wire.TransferHeader) { h.BaseSHA, h.CandidateSHA = "HEAD", b }, []string{b}, false}, |
| 360 | } { |
| 361 | t.Run(tc.name, func(t *testing.T) { |
| 362 | h := base |
| 363 | tc.edit(&h) |
| 364 | if got := h.Commits(); !slices.Equal(got, tc.commits) { |
| 365 | t.Fatalf("commits %v, want %v", got, tc.commits) |
| 366 | } |
| 367 | if h.Valid() != tc.valid { |
| 368 | t.Fatalf("valid %v, want %v", !tc.valid, tc.valid) |
| 369 | } |
| 370 | }) |
| 371 | } |
| 372 | // The header stays comparable: the instance checks a chunk's header |
| 373 | // against its own row with !=, and a kind or old base is part of it. |
| 374 | x := base |
| 375 | x.Kind, x.BaseSHA = wire.TransferKindEngineering, a |
| 376 | y := x |
| 377 | if x != y { |
| 378 | t.Fatal("equal headers differ") |
| 379 | } |
| 380 | y.OldBaseSHA = c |
| 381 | if x == y { |
| 382 | t.Fatal("old base not compared") |
| 383 | } |
| 384 | y = x |
| 385 | y.Kind = "" |
| 386 | if x == y { |
| 387 | t.Fatal("kind not compared") |
| 388 | } |
| 389 | // A checks header still encodes as it did before slice F. |
| 390 | raw, _ := json.Marshal(wire.TransferHeader{ID: "tr_1", Direction: wire.TransferInbound, BaseSHA: a, CandidateSHA: b}) |
| 391 | if bytes.Contains(raw, []byte(`"kind"`)) || bytes.Contains(raw, []byte(`"old_base_sha"`)) { |
| 392 | t.Fatalf("checks header %s", raw) |
| 393 | } |
| 394 | if !wire.ValidCommitSHA(a) || !wire.ValidCommitSHA(c) || wire.ValidCommitSHA(strings.Repeat("a", 41)) || wire.ValidCommitSHA("") { |
| 395 | t.Fatal("commit sha") |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // F1 to F7 ride in the same probe list as P1 to P7. Each F probe has one |
| 400 | // success verdict: F4 (the process group) passes; every other one proves |
| 401 | // a denial, so it reports denied, never passed. |
| 402 | func TestValidCapabilitiesF(t *testing.T) { |
| 403 | _, body, err := wire.ParseFrame(wiretest.Fixture("frames/hello-worker-conns.json")) |
| 404 | if err != nil { |
| 405 | t.Fatal(err) |
| 406 | } |
| 407 | good := *body.(*wire.Hello).Capabilities |
| 408 | if err := wire.ValidCapabilities(good); err != nil { |
| 409 | t.Fatal(err) |
| 410 | } |
| 411 | with := func(id, verdict string) wire.Capabilities { |
| 412 | c := good |
| 413 | c.Probes = slices.Clone(good.Probes) |
| 414 | for i := range c.Probes { |
| 415 | if c.Probes[i].ID == id { |
| 416 | c.Probes[i].Verdict = verdict |
| 417 | } |
| 418 | } |
| 419 | return c |
| 420 | } |
| 421 | for _, id := range wire.EngineeringProbeIDs { |
| 422 | success := wire.ProbeDenied |
| 423 | if id == "F4" { |
| 424 | success = wire.ProbePassed |
| 425 | } |
| 426 | if wire.EngineeringProbeSuccess(id) != success { |
| 427 | t.Errorf("%s success %q", id, wire.EngineeringProbeSuccess(id)) |
| 428 | } |
| 429 | for _, v := range []string{wire.ProbePassed, wire.ProbeDenied, wire.ProbeFailed, wire.ProbeSkipped, wire.ProbeUnavailable} { |
| 430 | ok := v == success || v == wire.ProbeFailed || v == wire.ProbeSkipped || v == wire.ProbeUnavailable |
| 431 | if err := wire.ValidCapabilities(with(id, v)); (err == nil) != ok { |
| 432 | t.Errorf("%s %s: %v", id, v, err) |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | if wire.EngineeringProbeSuccess("P2") != "" || wire.EngineeringProbeSuccess("F8") != "" { |
| 437 | t.Fatal("success for a probe that is not an F probe") |
| 438 | } |
| 439 | // P probes keep their rules: only P2 may say denied. |
| 440 | if wire.ValidCapabilities(with("P4", wire.ProbeDenied)) == nil { |
| 441 | t.Fatal("P4 denied accepted") |
| 442 | } |
| 443 | bad := map[string]func(*wire.Capabilities){ |
| 444 | "F8": func(c *wire.Capabilities) { |
| 445 | c.Probes = append(slices.Clone(c.Probes), wire.Probe{ID: "F8", Verdict: wire.ProbeFailed}) |
| 446 | }, |
| 447 | "F1 twice": func(c *wire.Capabilities) { |
| 448 | c.Probes = append(slices.Clone(c.Probes), wire.Probe{ID: "F1", Verdict: wire.ProbeFailed}) |
| 449 | }, |
| 450 | "lower-case f1": func(c *wire.Capabilities) { c.Probes = []wire.Probe{{ID: "f1", Verdict: wire.ProbeDenied}} }, |
| 451 | "state": func(c *wire.Capabilities) { c.Engineering = &wire.EngineeringStatus{State: "busy"} }, |
| 452 | "empty state": func(c *wire.Capabilities) { c.Engineering = &wire.EngineeringStatus{} }, |
| 453 | "ready reason": func(c *wire.Capabilities) { |
| 454 | c.Engineering = &wire.EngineeringStatus{State: wire.EngineeringReady, Reason: "x"} |
| 455 | }, |
| 456 | "path in reason": func(c *wire.Capabilities) { |
| 457 | c.Engineering = &wire.EngineeringStatus{State: wire.EngineeringHomeOpen, Reason: "Users/paulequipo"} |
| 458 | }, |
| 459 | "long reason": func(c *wire.Capabilities) { |
| 460 | c.Engineering = &wire.EngineeringStatus{State: wire.EngineeringLimited, Reason: strings.Repeat("x", 301)} |
| 461 | }, |
| 462 | "newline reason": func(c *wire.Capabilities) { |
| 463 | c.Engineering = &wire.EngineeringStatus{State: wire.EngineeringLimited, Reason: "a\nb"} |
| 464 | }, |
| 465 | "token in detail": func(c *wire.Capabilities) { |
| 466 | c.Probes = []wire.Probe{{ID: "F3", Verdict: wire.ProbeFailed, Detail: "TOKEN=x"}} |
| 467 | }, |
| 468 | } |
| 469 | for name, edit := range bad { |
| 470 | c := good |
| 471 | edit(&c) |
| 472 | if wire.ValidCapabilities(c) == nil { |
| 473 | t.Errorf("%s accepted", name) |
| 474 | } |
| 475 | } |
| 476 | for _, state := range wire.EngineeringStates { |
| 477 | c := good |
| 478 | reason := "until 18:00" |
| 479 | if state == wire.EngineeringReady { |
| 480 | reason = "" |
| 481 | } |
| 482 | c.Engineering = &wire.EngineeringStatus{State: state, Reason: reason} |
| 483 | if err := wire.ValidCapabilities(c); err != nil { |
| 484 | t.Errorf("%s refused: %v", state, err) |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | func TestPrepareBaseAndBaseReport(t *testing.T) { |
| 490 | a, b := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 491 | first := wire.PrepareBaseCommand{TaskID: "tk1", StepID: "s1", Attempt: 1, Repo: "amadan.net/oficina/sample"} |
| 492 | rebase := wire.PrepareBaseCommand{TaskID: "tk1", StepID: "s1", Attempt: 2, Repo: "amadan.net/oficina/sample", Rebase: true, FromBase: a, CandidateSHA: b} |
| 493 | if !first.Valid() || !rebase.Valid() { |
| 494 | t.Fatal("valid commands refused") |
| 495 | } |
| 496 | for name, edit := range map[string]func(*wire.PrepareBaseCommand){ |
| 497 | "no task": func(c *wire.PrepareBaseCommand) { c.TaskID = "" }, |
| 498 | "no step": func(c *wire.PrepareBaseCommand) { c.StepID = "" }, |
| 499 | "no attempt": func(c *wire.PrepareBaseCommand) { c.Attempt = 0 }, |
| 500 | "no repo": func(c *wire.PrepareBaseCommand) { c.Repo = "" }, |
| 501 | "rebase without from": func(c *wire.PrepareBaseCommand) { c.FromBase = "" }, |
| 502 | "rebase bad candidate": func(c *wire.PrepareBaseCommand) { c.CandidateSHA = "HEAD" }, |
| 503 | "first with candidate": func(c *wire.PrepareBaseCommand) { c.Rebase = false }, |
| 504 | "first with from base": func(c *wire.PrepareBaseCommand) { c.Rebase, c.CandidateSHA = false, "" }, |
| 505 | "rebase onto candidate": func(c *wire.PrepareBaseCommand) { c.FromBase = c.CandidateSHA }, |
| 506 | } { |
| 507 | c := rebase |
| 508 | edit(&c) |
| 509 | if c.Valid() { |
| 510 | t.Errorf("%s accepted", name) |
| 511 | } |
| 512 | } |
| 513 | ok := wire.BaseReport{TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: a} |
| 514 | gone := wire.BaseReport{TaskID: "tk1", StepID: "s1", Attempt: 1, Reason: wire.ReasonBaseUnavailable} |
| 515 | if !ok.Valid() || !gone.Valid() { |
| 516 | t.Fatal("valid reports refused") |
| 517 | } |
| 518 | for name, r := range map[string]wire.BaseReport{ |
| 519 | "both": {TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: a, Reason: wire.ReasonBaseUnavailable}, |
| 520 | "neither": {TaskID: "tk1", StepID: "s1", Attempt: 1}, |
| 521 | "other reason": {TaskID: "tk1", StepID: "s1", Attempt: 1, Reason: "clone_failed"}, |
| 522 | "bad sha": {TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: "main"}, |
| 523 | "no attempt": {TaskID: "tk1", StepID: "s1", BaseSHA: a}, |
| 524 | "no step": {TaskID: "tk1", Attempt: 1, BaseSHA: a}, |
| 525 | } { |
| 526 | if r.Valid() { |
| 527 | t.Errorf("%s accepted", name) |
| 528 | } |
| 529 | } |
| 530 | for name, want := range map[string]any{ |
| 531 | "frames/prepare-base.json": &wire.PrepareBaseCommand{}, |
| 532 | "frames/base.json": &wire.BaseReport{}, |
| 533 | "frames/dispatch-mac-engineering.json": &wire.DispatchCommand{}, |
| 534 | "frames/blob-engineering.json": &wire.Blob{}, |
| 535 | "frames/published-change-files.json": &wire.PublishedReport{}, |
| 536 | "frames/hello-worker-conns.json": &wire.Hello{}, |
| 537 | "frames/ask-pinned.json": &wire.AskCommand{}, |
| 538 | "frames/step-mac-engineering.json": &wire.StepReport{}, |
| 539 | } { |
| 540 | _, body, err := wire.ParseFrame(wiretest.Fixture(name)) |
| 541 | if err != nil { |
| 542 | t.Fatalf("%s: %v", name, err) |
| 543 | } |
| 544 | if got, w := strings.TrimPrefix(typeName(body), "*"), strings.TrimPrefix(typeName(want), "*"); got != w { |
| 545 | t.Fatalf("%s decodes as %s, want %s", name, got, w) |
| 546 | } |
| 547 | } |
| 548 | _, body, _ := wire.ParseFrame(wiretest.Fixture("frames/prepare-base.json")) |
| 549 | if !body.(*wire.PrepareBaseCommand).Valid() { |
| 550 | t.Fatal("prepare-base fixture invalid") |
| 551 | } |
| 552 | _, body, _ = wire.ParseFrame(wiretest.Fixture("frames/base.json")) |
| 553 | if !body.(*wire.BaseReport).Valid() { |
| 554 | t.Fatal("base fixture invalid") |
| 555 | } |
| 556 | _, body, _ = wire.ParseFrame(wiretest.Fixture("frames/blob-engineering.json")) |
| 557 | if h := body.(*wire.Blob).Transfer; !h.Valid() || len(h.Commits()) != 3 { |
| 558 | t.Fatalf("blob-engineering header %+v", h) |
| 559 | } |
| 560 | _, body, _ = wire.ParseFrame(wiretest.Fixture("frames/published-change-files.json")) |
| 561 | if p := body.(*wire.PublishedReport); !wire.ValidChangeFiles(p.ChangeFiles) || !wire.ValidSHA256(p.DiffCollection) { |
| 562 | t.Fatalf("published-change-files %+v", p) |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | func typeName(v any) string { return fmt.Sprintf("%T", v) } |
| 567 | |
| 568 | // The Mac's credential hold names at most MaxHoldingItems Keychain items, |
| 569 | // each within the capability string rules. |
| 570 | func TestHoldingItemsBounded(t *testing.T) { |
| 571 | items := make([]string, wire.MaxHoldingItems) |
| 572 | for i := range items { |
| 573 | items[i] = "Claude Code-credentials-" + strings.Repeat("a", i+1) |
| 574 | } |
| 575 | if err := wire.ValidCapabilities(wire.Capabilities{ModelHold: true, HoldingItems: items}); err != nil { |
| 576 | t.Fatalf("%d items: %v", len(items), err) |
| 577 | } |
| 578 | if wire.ValidCapabilities(wire.Capabilities{ModelHold: true, HoldingItems: append(items, "one more")}) == nil { |
| 579 | t.Fatal("more than MaxHoldingItems taken") |
| 580 | } |
| 581 | if wire.ValidCapabilities(wire.Capabilities{HoldingItems: []string{"a/b"}}) == nil { |
| 582 | t.Fatal("a path taken as an item") |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // The reasons a worker's Engineering first sends (slice F task 12) are |
| 587 | // known reasons: an instance records them, with their words, never as |
| 588 | // unknown. |
| 589 | func TestMacEngineeringWorkerReasonsKnown(t *testing.T) { |
| 590 | for _, r := range []string{wire.ReasonSecretInCandidate, wire.ReasonEngineeringUnavailable} { |
| 591 | if !wire.StepReasons[r] { |
| 592 | t.Errorf("%s is not a step reason", r) |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // A Mac worker names its runner user (a macOS short name, nothing else) |
| 598 | // at 37 only, so the Machines page can say as whom to change a folder; |
| 599 | // WithoutMacEngineering drops it, alone or with the rest. |
| 600 | func TestRunnerUserInCapabilities(t *testing.T) { |
| 601 | for _, name := range []string{"paulequipo", "equipo-paul", "_runner", "a.b"} { |
| 602 | if err := wire.ValidCapabilities(wire.Capabilities{RunnerUser: name}); err != nil { |
| 603 | t.Errorf("%q refused: %v", name, err) |
| 604 | } |
| 605 | } |
| 606 | for _, name := range []string{"Users/paulequipo", "/Users/x", "a b", "x=y", "-x", "1x", strings.Repeat("a", 33), "pau\nl", "paulé"} { |
| 607 | if wire.ValidCapabilities(wire.Capabilities{RunnerUser: name}) == nil { |
| 608 | t.Errorf("%q taken as a runner user", name) |
| 609 | } |
| 610 | } |
| 611 | h := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagMacOS}, RunnerUser: "paulequipo"}} |
| 612 | if !wire.WithoutMacEngineering(&h) || h.Capabilities.RunnerUser != "" { |
| 613 | t.Fatalf("below 37 the runner user stays: %+v", h.Capabilities) |
| 614 | } |
| 615 | } |
| 616 | |