| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/sealbox" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // A Mac worker's own Claude connections (Mac runners slice F, task 11): |
| 12 | // every connection operation goes to the runner its row names, never to |
| 13 | // the primary; the hello of whichever runner sent it records that |
| 14 | // runner's connections only; and the instance holds a Mac as having a |
| 15 | // model credential from the moment a connection there starts connecting |
| 16 | // until the Mac says its files, its Keychain items and any unproven |
| 17 | // launch are all gone. |
| 18 | |
| 19 | // workerConnHello is a Mac worker's hello at 37 after the welcome: its |
| 20 | // seal key and connections, the whole inventory. |
| 21 | func workerConnHello(key string, conns ...wire.HeldConnection) wire.Hello { |
| 22 | h := macEngineerHello("Tito Studio", wire.EngineeringNoConnection, wire.ProtocolMacEngineering, false) |
| 23 | h.SealKey, h.Connections, h.Inventory = key, append([]wire.HeldConnection{}, conns...), true |
| 24 | return h |
| 25 | } |
| 26 | |
| 27 | // connRow is the person's connection on runner with local id. |
| 28 | func (r *runnersRig) connRow(runner, local string) (wire.Connection, bool) { |
| 29 | r.t.Helper() |
| 30 | list, err := r.client.Connections(r.ctx) |
| 31 | if err != nil { |
| 32 | r.t.Fatal(err) |
| 33 | } |
| 34 | for _, c := range list.Items { |
| 35 | if c.RunnerID == runner && c.LocalID == local { |
| 36 | return c, true |
| 37 | } |
| 38 | } |
| 39 | return wire.Connection{}, false |
| 40 | } |
| 41 | |
| 42 | // redialMac connects p's Mac again (an instance restart dropped it), |
| 43 | // saying h at once. |
| 44 | func (r *runnersRig) redialMac(p *peer, h wire.Hello) *peer { |
| 45 | r.t.Helper() |
| 46 | c, ok := r.creds[p.fr] |
| 47 | if !ok { |
| 48 | r.t.Fatal("unknown peer") |
| 49 | } |
| 50 | h.Generation = p.fr.Welcome.Generation |
| 51 | fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], h) |
| 52 | if err != nil { |
| 53 | r.t.Fatal(err) |
| 54 | } |
| 55 | r.t.Cleanup(fr.Close) |
| 56 | r.creds[fr] = c |
| 57 | return &peer{t: r.t, r: r.toolRig, fr: fr} |
| 58 | } |
| 59 | |
| 60 | // TestContractWorkerConnectionLifecycle: start, the sealed token, cancel, |
| 61 | // sign out, check and the preference for a connection on a worker go to |
| 62 | // that worker and never reach the primary; a worker and the primary may |
| 63 | // hold the same local id without either's hello touching the other's |
| 64 | // row; a box sealed for one runner is refused for the other's attempt; |
| 65 | // a worker below 37, or the kinds a Mac does not take, get no connection. |
| 66 | func TestContractWorkerConnectionLifecycle(t *testing.T) { |
| 67 | r := newMacRig(t) |
| 68 | keys := sealKeys(t) |
| 69 | w := r.newMacEngineer("Tito Studio", wire.EngineeringNoConnection) |
| 70 | wid, pid := w.fr.Welcome.RunnerID, r.primary.fr.Welcome.RunnerID |
| 71 | w.heartbeat(workerConnHello(keys.PublicString())) |
| 72 | waitFor(t, "the worker's seal key published", func() bool { |
| 73 | list, err := r.client.Runners(r.ctx) |
| 74 | if err != nil { |
| 75 | return false |
| 76 | } |
| 77 | for _, it := range list.Items { |
| 78 | if it.ID == wid { |
| 79 | return it.SealKey == keys.PublicString() |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | }) |
| 84 | if _, err := r.client.SetPreferences(r.ctx, wire.Preferences{Provider: wire.ProviderClaude}); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | // The primary holds claude-1 of its own. |
| 88 | ph := primaryHello(0) |
| 89 | ph.Protocol = wire.ProtocolMacEngineering |
| 90 | ph.Catalog[0].NeedsMacOS = true |
| 91 | ph.Connections = []wire.HeldConnection{{LocalID: "claude-1", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, Label: "p@example.com", State: wire.ConnHealthy}} |
| 92 | r.primary.heartbeat(ph) |
| 93 | if c, ok := r.connRow(pid, "claude-1"); !ok || c.State != wire.ConnHealthy { |
| 94 | t.Fatalf("the primary's connection: %+v %v", c, ok) |
| 95 | } |
| 96 | connects, secrets, cancels, disconnects, checks := r.mailbox(pid, wire.FrameConnect), r.mailbox(pid, wire.FrameSecret), r.mailbox(pid, wire.FrameCancelConnect), r.mailbox(pid, wire.FrameDisconnect), r.mailbox(pid, wire.FrameCheck) |
| 97 | |
| 98 | // Start on the worker. |
| 99 | a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, Kind: wire.KindToken, RunnerID: wid}) |
| 100 | if err != nil || a.RunnerID != wid || a.State != wire.ConnConnecting { |
| 101 | t.Fatalf("connect on the worker: %+v %v", a, err) |
| 102 | } |
| 103 | if _, body := w.expect(wire.FrameConnect); body.(*wire.ConnectCommand).Connection != a.ID { |
| 104 | t.Fatalf("connect frame %+v", body) |
| 105 | } |
| 106 | // A box sealed for the primary is refused for the worker's attempt. |
| 107 | seal := func(runner string) wire.Sealed { |
| 108 | t.Helper() |
| 109 | s, err := sealbox.Seal(keys.PublicString(), wire.SealedPayload{Secret: "sk-ant-oat01-worker", RunnerID: runner, ConnectionID: a.ID, Provider: wire.ProviderClaude, Endpoint: wire.EndpointAnthropic, Attempt: 1, ExpiresAt: time.Now().UTC().Add(wire.SealedTTL).Truncate(time.Second)}) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | return s |
| 114 | } |
| 115 | if _, err := r.client.Secret(r.ctx, a.ID, seal(pid)); Status(err) != 422 { |
| 116 | t.Fatalf("a box for the primary taken for the worker's attempt: %v", err) |
| 117 | } |
| 118 | if _, err := r.client.Secret(r.ctx, a.ID, seal(wid)); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | w.expect(wire.FrameSecret) |
| 122 | // The worker's connection is claude-1 too. |
| 123 | w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, LocalID: "claude-1", Provider: wire.ProviderClaude, State: wire.ConnHealthy, Label: "w@example.com"}) |
| 124 | held := wire.HeldConnection{LocalID: "claude-1", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, Label: "w@example.com", State: wire.ConnHealthy} |
| 125 | prefs := r.mailbox(pid, wire.FramePreference) |
| 126 | wprefs := r.mailbox(wid, wire.FramePreference) |
| 127 | w.heartbeat(workerConnHello(keys.PublicString(), held)) |
| 128 | if c, ok := r.connRow(wid, "claude-1"); !ok || c.ID != a.ID || c.State != wire.ConnHealthy || c.Label != "w@example.com" { |
| 129 | t.Fatalf("the worker's connection: %+v %v", c, ok) |
| 130 | } |
| 131 | if c, _ := r.connRow(pid, "claude-1"); c.State != wire.ConnHealthy || c.Label != "p@example.com" { |
| 132 | t.Fatalf("the worker's hello changed the primary's row: %+v", c) |
| 133 | } |
| 134 | // The worker's hello says the preference again to the worker alone. |
| 135 | if r.mailbox(pid, wire.FramePreference) != prefs || r.mailbox(wid, wire.FramePreference) <= wprefs { |
| 136 | t.Fatalf("preference: primary %d (was %d), worker %d (was %d)", r.mailbox(pid, wire.FramePreference), prefs, r.mailbox(wid, wire.FramePreference), wprefs) |
| 137 | } |
| 138 | // Nor does the primary's hello touch the worker's claude-1. |
| 139 | r.primary.heartbeat(ph) |
| 140 | if c, _ := r.connRow(wid, "claude-1"); c.State != wire.ConnHealthy || c.Label != "w@example.com" { |
| 141 | t.Fatalf("the primary's hello changed the worker's row: %+v", c) |
| 142 | } |
| 143 | // A new preference reaches the worker holding a connection, and the primary. |
| 144 | wprefs, prefs = r.mailbox(wid, wire.FramePreference), r.mailbox(pid, wire.FramePreference) |
| 145 | now, err := r.client.Preferences(r.ctx) |
| 146 | if err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if _, err := r.client.SetPreferences(r.ctx, wire.Preferences{Provider: wire.ProviderCodex, Revision: now.Revision}); err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | if r.mailbox(wid, wire.FramePreference) != wprefs+1 || r.mailbox(pid, wire.FramePreference) != prefs+1 { |
| 153 | t.Fatal("a new preference did not reach each runner holding a connection once") |
| 154 | } |
| 155 | |
| 156 | // Check again goes to the worker. |
| 157 | if err := r.client.CheckConnection(r.ctx, a.ID); err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | if _, body := w.expect(wire.FrameCheck); body.(*wire.CheckCommand).LocalID != "claude-1" { |
| 161 | t.Fatalf("check frame %+v", body) |
| 162 | } |
| 163 | // Sign out goes to the worker, naming it. |
| 164 | if _, err := r.client.SignOut(r.ctx, a.ID); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | _, body := w.expect(wire.FrameDisconnect) |
| 168 | if d := body.(*wire.DisconnectCommand); d.LocalID != "claude-1" || d.RunnerID != wid || d.Connection != a.ID { |
| 169 | t.Fatalf("disconnect frame %+v", d) |
| 170 | } |
| 171 | // The worker no longer holds it: its row is signed out, the primary's |
| 172 | // claude-1 stays as it was. |
| 173 | w.heartbeat(workerConnHello(keys.PublicString())) |
| 174 | if c, _ := r.connRow(wid, "claude-1"); c.State != wire.ConnSignedOut { |
| 175 | t.Fatalf("the worker's row: %+v", c) |
| 176 | } |
| 177 | if c, _ := r.connRow(pid, "claude-1"); c.State != wire.ConnHealthy || c.Label != "p@example.com" { |
| 178 | t.Fatalf("the worker's hello signed out the primary's row: %+v", c) |
| 179 | } |
| 180 | // Cancel goes to the worker. |
| 181 | b, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid}) |
| 182 | if err != nil || b.RunnerID != wid { |
| 183 | t.Fatalf("sign-in on the worker: %+v %v", b, err) |
| 184 | } |
| 185 | w.expect(wire.FrameConnect) |
| 186 | if _, err := r.client.CancelConnection(r.ctx, b.ID); err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | if _, body := w.expect(wire.FrameCancelConnect); body.(*wire.CancelConnectCommand).Connection != b.ID { |
| 190 | t.Fatalf("cancel frame %+v", body) |
| 191 | } |
| 192 | // None of it reached the primary. |
| 193 | if r.mailbox(pid, wire.FrameConnect) != connects || r.mailbox(pid, wire.FrameSecret) != secrets || r.mailbox(pid, wire.FrameCancelConnect) != cancels || r.mailbox(pid, wire.FrameDisconnect) != disconnects || r.mailbox(pid, wire.FrameCheck) != checks { |
| 194 | t.Fatal("a worker's connection command reached the primary") |
| 195 | } |
| 196 | // The primary's own sign-out still goes to the primary. |
| 197 | pc, _ := r.connRow(pid, "claude-1") |
| 198 | if _, err := r.client.SignOut(r.ctx, pc.ID); err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | if _, body := r.primary.expect(wire.FrameDisconnect); body.(*wire.DisconnectCommand).RunnerID != pid { |
| 202 | t.Fatalf("the primary's disconnect %+v", body) |
| 203 | } |
| 204 | if r.mailbox(wid, wire.FrameDisconnect) != 1 { |
| 205 | t.Fatal("the primary's sign-out reached the worker") |
| 206 | } |
| 207 | |
| 208 | // What a Mac does not take, and a worker below 37. |
| 209 | for _, in := range []wire.Connect{{Provider: wire.ProviderCodex, RunnerID: wid}, {Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, RunnerID: wid, LimitConfirmed: true, Budget: &wire.Budget{Day: 1_000_000, Month: 10_000_000}}} { |
| 210 | if _, err := r.client.Connect(r.ctx, in); Status(err) != 422 { |
| 211 | t.Fatalf("%s %s on a Mac: %v", in.Provider, in.Kind, err) |
| 212 | } |
| 213 | } |
| 214 | oh := macEngineerHello("Old Mac", wire.EngineeringNoConnection, wire.ProtocolChangeDiffs, false) |
| 215 | oh.SealKey, oh.Connections = sealKeys(t).PublicString(), []wire.HeldConnection{{LocalID: "claude-7", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy}} |
| 216 | ofr, err := DialRunnerHello(r.ctx, r.inst.URL, r.inst.EnrolRunner(t).Token, oh) |
| 217 | if err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | t.Cleanup(ofr.Close) |
| 221 | old := &peer{t: t, r: r.toolRig, fr: ofr} |
| 222 | oid := old.fr.Welcome.RunnerID |
| 223 | nothingBelow37 := func(when string) { |
| 224 | t.Helper() |
| 225 | if _, ok := r.connRow(oid, "claude-7"); ok { |
| 226 | t.Fatalf("%s: a worker below 37 recorded a connection", when) |
| 227 | } |
| 228 | list, err := r.client.Runners(r.ctx) |
| 229 | if err != nil { |
| 230 | t.Fatal(err) |
| 231 | } |
| 232 | for _, it := range list.Items { |
| 233 | if it.ID == oid && it.SealKey != "" { |
| 234 | t.Fatalf("%s: a worker below 37 published a seal key", when) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | old.lease("", 0) |
| 239 | nothingBelow37("its first hello") |
| 240 | old.heartbeat(oh) |
| 241 | nothingBelow37("a later hello") |
| 242 | if _, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, Kind: wire.KindToken, RunnerID: oid}); err == nil { |
| 243 | t.Fatal("a connection started on a worker below 37") |
| 244 | } |
| 245 | if r.mailbox(oid, wire.FrameConnect) != 0 { |
| 246 | t.Fatal("a connect reached a worker below 37") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // TestContractCredentialHeldFromConnecting: the instance holds a Mac as |
| 251 | // having a model credential from the moment a connection there starts |
| 252 | // connecting: before the Mac has the frame, through a refused connect |
| 253 | // whose Keychain item stays, across an instance restart, while an |
| 254 | // earlier launch is unproven, and until the Mac says its files and its |
| 255 | // Keychain items are gone. |
| 256 | func TestContractCredentialHeldFromConnecting(t *testing.T) { |
| 257 | r := newMacRig(t) |
| 258 | keys := sealKeys(t) |
| 259 | w := r.newMacEngineer("Tito Studio", wire.EngineeringNoConnection) |
| 260 | wid := w.fr.Welcome.RunnerID |
| 261 | isHeld := func() bool { |
| 262 | t.Helper() |
| 263 | // The store as it is now: an instance restart opens another. |
| 264 | held, err := r.inst.Store.Chat().MacCredentialHeld(r.ctx, wid) |
| 265 | if err != nil { |
| 266 | t.Fatal(err) |
| 267 | } |
| 268 | return held |
| 269 | } |
| 270 | hello := workerConnHello(keys.PublicString()) |
| 271 | w.heartbeat(hello) |
| 272 | if isHeld() { |
| 273 | t.Fatal("held before anything started") |
| 274 | } |
| 275 | a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid}) |
| 276 | if err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | if !isHeld() { |
| 280 | t.Fatal("not held from the moment it started connecting") |
| 281 | } |
| 282 | // The Mac has not taken the frame yet, and says it holds nothing. |
| 283 | w.heartbeat(hello) |
| 284 | if !isHeld() { |
| 285 | t.Fatal("not held from connecting") |
| 286 | } |
| 287 | w.expect(wire.FrameConnect) |
| 288 | // The Mac has it now, and its hello does not say so yet: the attempt |
| 289 | // connecting there holds. |
| 290 | w.heartbeat(hello) |
| 291 | if !isHeld() { |
| 292 | t.Fatal("released while an attempt there is connecting") |
| 293 | } |
| 294 | // The sign-in is refused, and its Keychain item stays. |
| 295 | w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "provider_refused"}) |
| 296 | residue := workerConnHello(keys.PublicString()) |
| 297 | caps := *residue.Capabilities |
| 298 | caps.ModelHold, caps.HoldingItems = true, []string{"Claude Code-credentials-1a2b3c4d"} |
| 299 | residue.Capabilities = &caps |
| 300 | w.heartbeat(residue) |
| 301 | if !isHeld() { |
| 302 | t.Fatal("released with a Keychain item left") |
| 303 | } |
| 304 | // An instance restart: still held. |
| 305 | r.inst.Restart(t) |
| 306 | w = r.redialMac(w, residue) |
| 307 | w.heartbeat(residue) |
| 308 | if !isHeld() { |
| 309 | t.Fatal("the hold did not survive the restart") |
| 310 | } |
| 311 | // Each word of the Mac's alone holds: its hold, or an item it names. |
| 312 | for _, change := range []func(*wire.Capabilities){ |
| 313 | func(c *wire.Capabilities) { c.ModelHold = true }, |
| 314 | func(c *wire.Capabilities) { c.HoldingItems = []string{"Claude Code-credentials-1a2b3c4d"} }, |
| 315 | } { |
| 316 | one := workerConnHello(keys.PublicString()) |
| 317 | caps := *one.Capabilities |
| 318 | change(&caps) |
| 319 | one.Capabilities = &caps |
| 320 | w.heartbeat(one) |
| 321 | if !isHeld() { |
| 322 | t.Fatalf("released while the Mac says %+v", caps) |
| 323 | } |
| 324 | } |
| 325 | // The item is gone but an earlier launch is unproven: held. |
| 326 | unproven := workerConnHello(keys.PublicString()) |
| 327 | caps = *unproven.Capabilities |
| 328 | caps.Unproven = wire.UnprovenCredential |
| 329 | unproven.Capabilities = &caps |
| 330 | w.heartbeat(unproven) |
| 331 | if !isHeld() { |
| 332 | t.Fatal("released while a launch is unproven") |
| 333 | } |
| 334 | // A hello with no capability report proves nothing. |
| 335 | bare := workerConnHello(keys.PublicString()) |
| 336 | bare.Capabilities = nil |
| 337 | w.heartbeat(bare) |
| 338 | if !isHeld() { |
| 339 | t.Fatal("released by a hello that reported nothing") |
| 340 | } |
| 341 | // Everything gone: released. |
| 342 | w.heartbeat(hello) |
| 343 | if isHeld() { |
| 344 | t.Fatal("still held once the Mac said everything is gone") |
| 345 | } |
| 346 | // A connect the Mac has not taken holds by itself: started and |
| 347 | // cancelled at once. |
| 348 | b, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid}) |
| 349 | if err != nil { |
| 350 | t.Fatal(err) |
| 351 | } |
| 352 | if _, err := r.client.CancelConnection(r.ctx, b.ID); err != nil { |
| 353 | t.Fatal(err) |
| 354 | } |
| 355 | w.heartbeat(hello) |
| 356 | if !isHeld() { |
| 357 | t.Fatal("released with a connect the Mac has not taken") |
| 358 | } |
| 359 | w.expect(wire.FrameConnect) |
| 360 | w.expect(wire.FrameCancelConnect) |
| 361 | w.heartbeat(hello) |
| 362 | if isHeld() { |
| 363 | t.Fatal("still held once the Mac took the connect and its cancel") |
| 364 | } |
| 365 | // A connection the Mac reports holds it again, even unasked. |
| 366 | w.heartbeat(workerConnHello(keys.PublicString(), wire.HeldConnection{LocalID: "claude-1", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy})) |
| 367 | if !isHeld() { |
| 368 | t.Fatal("a held connection does not hold") |
| 369 | } |
| 370 | } |
| 371 | |