| 1 | package wire |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Connections (Equipo Connections spec §§2, 7, slice 1). A connection is |
| 6 | // one signed-in account on one runner, owned by one person. The instance |
| 7 | // holds this record and never a secret: no key, token, credential or path |
| 8 | // field exists on any connection type (TestConnectionTypesCarryNoSecrets). |
| 9 | |
| 10 | // Connection states. |
| 11 | const ( |
| 12 | ConnConnecting = "connecting" |
| 13 | ConnHealthy = "healthy" |
| 14 | ConnLimited = "limited" |
| 15 | ConnSignInAgain = "sign_in_again" |
| 16 | ConnError = "error" |
| 17 | ConnSignedOut = "signed_out" |
| 18 | // ConnForgotten is only ever an event's: the person removed a record |
| 19 | // the runner no longer holds, and clients drop it. |
| 20 | ConnForgotten = "forgotten" |
| 21 | ) |
| 22 | |
| 23 | // Providers and kinds a slice 1 connection may have. |
| 24 | const ( |
| 25 | ProviderClaude = "claude" |
| 26 | ProviderCodex = "codex" |
| 27 | KindSubscription = "subscription" |
| 28 | ) |
| 29 | |
| 30 | // Prompt kinds on a connecting card: a device code to enter at URL, or |
| 31 | // Claude's sign-in link whose code comes back sealed (PromptAuthorize). |
| 32 | const ( |
| 33 | PromptDeviceCode = "device_code" |
| 34 | // PromptAuthorize is Claude's sign-in with no terminal (Paul, |
| 35 | // 2026-11-10): the runner runs the unmodified `claude setup-token` |
| 36 | // itself and shows the authorize URL the CLI prints; the person signs |
| 37 | // in at Anthropic, which shows a one-time code; the page seals the |
| 38 | // code to the runner (EndpointClaudeCode, Attempt the prompt's), and |
| 39 | // the runner types it into the waiting CLI. The token the CLI prints |
| 40 | // is kept as a pasted one is. A Claude Connect with Kind subscription |
| 41 | // starts it; the record is kind subscription |
| 42 | // while it connects and kind token once healthy. With no URL and |
| 43 | // Attempt 0 the runner is starting the CLI; with no URL and an |
| 44 | // Attempt, that attempt's code was sent and the runner is finishing. |
| 45 | PromptAuthorize = "authorize_link" |
| 46 | ) |
| 47 | |
| 48 | // MaxCodeAttempts bounds the codes one sign-in attempt takes: a wrong |
| 49 | // code asks for another (the same link while the CLI waits, a new link |
| 50 | // when it had to start again), up to this many. |
| 51 | const MaxCodeAttempts = 5 |
| 52 | |
| 53 | // ConnectReasons end a connection attempt in error. Never provider text. |
| 54 | var ConnectReasons = map[string]bool{ |
| 55 | "": true, |
| 56 | "timed_out": true, |
| 57 | "cancelled": true, |
| 58 | "provider_refused": true, |
| 59 | "runner_offline": true, |
| 60 | "identity_mismatch": true, |
| 61 | "already_connected": true, |
| 62 | // An API-key connection: the provider refused the key, or listed no |
| 63 | // model Equipo can price (ProtocolMetered). |
| 64 | "key_refused": true, |
| 65 | "no_priced_model": true, |
| 66 | // A Mac worker keeps only Claude token connections (slice F, |
| 67 | // ProtocolMacEngineering): any other kind is refused with this. |
| 68 | ReasonMacClaudeOnly: true, |
| 69 | } |
| 70 | |
| 71 | // ConnectionCommands are the instance's commands about a runner's |
| 72 | // connections. A worker takes them, and says anything of its |
| 73 | // connections, only at ProtocolMacEngineering. |
| 74 | var ConnectionCommands = map[string]bool{FrameConnect: true, FrameCancelConnect: true, FrameDisconnect: true, FrameSecret: true, FrameCheck: true, FramePreference: true, FrameModel: true, FrameModels: true, FrameBudget: true, FrameResumeSpend: true} |
| 75 | |
| 76 | // ReasonMacClaudeOnly ends an attempt a Mac worker does not take: only |
| 77 | // Claude, by a pasted token or its own sign-in, runs on a Mac. |
| 78 | const ReasonMacClaudeOnly = "mac_claude_only" |
| 79 | |
| 80 | // EventConnection fires on every state or headroom change of one of the |
| 81 | // person's connections; Data is the Connection. |
| 82 | const EventConnection = "connection" |
| 83 | |
| 84 | // Window is one provider rate-limit window as the provider reported it: |
| 85 | // Codex gives a primary and a secondary window, Claude one per kind. |
| 86 | type Window struct { |
| 87 | Name string `json:"name"` |
| 88 | UsedPercent float64 `json:"used_percent"` |
| 89 | Minutes int64 `json:"minutes"` |
| 90 | ResetsAt *time.Time `json:"resets_at"` |
| 91 | } |
| 92 | |
| 93 | // Headroom is what the provider reported and nothing else. Utilization is |
| 94 | // 0 to 1 (null when unreported); ResetsAt is when a limited connection |
| 95 | // may answer again (null when the provider did not say). CheckedAt is |
| 96 | // when the runner took the reading (ProtocolReadings; absent from an |
| 97 | // older runner). Fetched distinguishes a Claude OAuth read from a turn. |
| 98 | type Headroom struct { |
| 99 | Utilization *float64 `json:"utilization"` |
| 100 | ResetsAt *time.Time `json:"resets_at"` |
| 101 | Windows []Window `json:"windows"` |
| 102 | CheckedAt *time.Time `json:"checked_at,omitempty"` |
| 103 | // ProtocolUsageRefresh. Unavailable leaves any last reading intact. |
| 104 | Fetched bool `json:"fetched,omitempty"` |
| 105 | Unavailable bool `json:"unavailable,omitempty"` |
| 106 | } |
| 107 | |
| 108 | // ConnectPrompt is what a connecting card shows: Kind is a Prompt* |
| 109 | // constant; URL is the provider's own page and Code the provider's |
| 110 | // display code (device code only). Neither is a secret. For |
| 111 | // PromptAuthorize, Attempt is the code attempt the page seals for (from |
| 112 | // 1) and WrongCode says the last code did not work. |
| 113 | type ConnectPrompt struct { |
| 114 | Kind string `json:"kind"` |
| 115 | URL string `json:"url"` |
| 116 | Code string `json:"code"` |
| 117 | Attempt int64 `json:"attempt,omitempty"` |
| 118 | WrongCode bool `json:"wrong_code,omitempty"` |
| 119 | // Models are the models the provider listed for a pasted API key, |
| 120 | // that Equipo can price (PromptModel): the person picks one. |
| 121 | Models []string `json:"models,omitempty"` |
| 122 | // Choices are every model the key listed, each saying whether it can |
| 123 | // be allowed (priced and tiered), for the models card |
| 124 | // (PromptModels, ProtocolModels): the person ticks the allowed set. |
| 125 | Choices []ModelChoice `json:"choices,omitempty"` |
| 126 | } |
| 127 | |
| 128 | // Connection is GET /api/v1/connections/{id}: one of the person's own |
| 129 | // connections. LocalID is the runner's name for it (its account home), |
| 130 | // empty while an attempt is connecting. |
| 131 | type Connection struct { |
| 132 | ID string `json:"id"` |
| 133 | Revision int64 `json:"revision"` |
| 134 | RunnerID string `json:"runner_id"` |
| 135 | LocalID string `json:"local_id"` |
| 136 | Provider string `json:"provider"` |
| 137 | Kind string `json:"kind"` |
| 138 | Label string `json:"label"` |
| 139 | State string `json:"state"` |
| 140 | Reason string `json:"reason"` |
| 141 | Plan string `json:"plan"` |
| 142 | Headroom *Headroom `json:"headroom"` |
| 143 | Prompt *ConnectPrompt `json:"prompt"` |
| 144 | CreatedAt time.Time `json:"created_at"` |
| 145 | UpdatedAt time.Time `json:"updated_at"` |
| 146 | // ExpiresAt is when the connection's own credential expires (a pasted |
| 147 | // setup token), null when it renews itself. ExpiresEstimated: the |
| 148 | // date is an estimate (neither the token nor the CLI says it). |
| 149 | ExpiresAt *time.Time `json:"expires_at"` |
| 150 | ExpiresEstimated bool `json:"expires_estimated"` |
| 151 | // An API-key connection (KindAPIKey, ProtocolMetered): its harness, |
| 152 | // its provider's origin, how it is billed, its one model, when the |
| 153 | // person confirmed the provider-side limit and, for a subscription |
| 154 | // that can bill overflow, that pay-as-you-go is off; its soft budget |
| 155 | // and spend as the runner holds them, and BudgetPending while a |
| 156 | // budget change waits for the runner to apply it. |
| 157 | Harness string `json:"harness,omitempty"` |
| 158 | Upstream string `json:"upstream,omitempty"` |
| 159 | Billing string `json:"billing,omitempty"` |
| 160 | Model string `json:"model,omitempty"` |
| 161 | LimitConfirmedAt *time.Time `json:"limit_confirmed_at,omitempty"` |
| 162 | PaygOffConfirmedAt *time.Time `json:"payg_off_confirmed_at,omitempty"` |
| 163 | Budget *Budget `json:"budget,omitempty"` |
| 164 | Spend *Spend `json:"spend,omitempty"` |
| 165 | BudgetPending bool `json:"budget_pending,omitempty"` |
| 166 | // Models is the allowed set of a metered connection (ProtocolModels, |
| 167 | // §16.1): every model its key listed, whether the person allows it, |
| 168 | // whether the runner applied that, and its state there. |
| 169 | // ModelsRevision is the set's revision; ModelsPending while the |
| 170 | // runner has not applied it. ModelsTooOld: the runner is below |
| 171 | // ProtocolModels, so the set cannot change. |
| 172 | Models []ConnectionModel `json:"models,omitempty"` |
| 173 | ModelsRevision int64 `json:"models_revision,omitempty"` |
| 174 | ModelsPending bool `json:"models_pending,omitempty"` |
| 175 | ModelsTooOld bool `json:"models_too_old,omitempty"` |
| 176 | } |
| 177 | |
| 178 | // ConnectionList is GET /api/v1/connections: the person's own, every runner. |
| 179 | type ConnectionList struct { |
| 180 | Items []Connection `json:"items"` |
| 181 | } |
| 182 | |
| 183 | // Connect is POST /api/v1/connections. RunnerID empty means the person's |
| 184 | // primary runner. SameMachine is refused when true: the sign-in finished |
| 185 | // at the runner's machine is gone (2026-11-10); the field stays so the |
| 186 | // documents keep their shape. |
| 187 | type Connect struct { |
| 188 | Provider string `json:"provider"` |
| 189 | Kind string `json:"kind"` |
| 190 | RunnerID string `json:"runner_id"` |
| 191 | SameMachine bool `json:"same_machine"` |
| 192 | // An API-key connection (KindAPIKey) needs LimitConfirmed when it is |
| 193 | // metered, and may carry PaygOffConfirmed only for a subscription |
| 194 | // that can bill overflow (APIKeyProvider.CheckConfirmations). Budget |
| 195 | // is a metered connection's day and month figures, both required. |
| 196 | LimitConfirmed bool `json:"limit_confirmed,omitempty"` |
| 197 | PaygOffConfirmed bool `json:"payg_off_confirmed,omitempty"` |
| 198 | Budget *Budget `json:"budget,omitempty"` |
| 199 | } |
| 200 | |
| 201 | // Frames for connections. |
| 202 | const ( |
| 203 | // FrameConnect (instance to runner, durable) starts an attempt. |
| 204 | FrameConnect = "connect" |
| 205 | // FrameCancelConnect (instance to runner, durable) ends one. |
| 206 | FrameCancelConnect = "cancel_connect" |
| 207 | // FrameConnection (runner to instance, durable) reports an attempt's |
| 208 | // progress or a connection's change of state. |
| 209 | FrameConnection = "connection" |
| 210 | // FrameDisconnect (instance to runner, durable) signs a connection out. |
| 211 | FrameDisconnect = "disconnect" |
| 212 | // FramePreference (instance to runner, durable) carries the person's |
| 213 | // preference, so the runner's choice has it offline. |
| 214 | FramePreference = "preference" |
| 215 | ) |
| 216 | |
| 217 | // Preferences is GET and PUT /api/v1/connections/preference: the provider the |
| 218 | // person prefers to answer, "" for Automatic. A preference orders the |
| 219 | // choice among connections that can answer now; it never overrides |
| 220 | // health, and a coordinator session moves to it only at the start of a |
| 221 | // turn (spec §3, "The preference"). Slice 1 keeps one per person, by |
| 222 | // provider; per-role rows come with routing (slice 3). Revision is the |
| 223 | // one the change was made against on PUT. |
| 224 | type Preferences struct { |
| 225 | Provider string `json:"provider"` |
| 226 | Revision int64 `json:"revision"` |
| 227 | // Until ends the preference by itself (protocol 14); absent: no end. |
| 228 | // Once it passes the preference is Automatic again. |
| 229 | Until *time.Time `json:"until,omitempty"` |
| 230 | } |
| 231 | |
| 232 | // PreferenceProviders are the values Preferences.Provider may take. |
| 233 | var PreferenceProviders = map[string]bool{"": true, "claude": true, "codex": true} |
| 234 | |
| 235 | // EventPreference fires when the person's preference changes; Data is |
| 236 | // the Preferences. |
| 237 | const EventPreference = "preference" |
| 238 | |
| 239 | // PreferenceCommand is the person's preference for the runner. A command |
| 240 | // with a revision below the one the runner holds changes nothing. |
| 241 | type PreferenceCommand struct { |
| 242 | Person string `json:"person"` |
| 243 | Provider string `json:"provider"` |
| 244 | Revision int64 `json:"revision"` |
| 245 | // Until (protocol 14): the preference ends by itself then. Encode |
| 246 | // refuses a preference with an end below 14. |
| 247 | Until *time.Time `json:"until,omitempty"` |
| 248 | } |
| 249 | |
| 250 | // DisconnectCommand signs one of the person's connections out: the |
| 251 | // harness's own logout, the home deleted, the binding gone. The runner |
| 252 | // answers with a connection report in state signed_out. |
| 253 | type DisconnectCommand struct { |
| 254 | Connection string `json:"connection"` |
| 255 | Person string `json:"person"` |
| 256 | LocalID string `json:"local_id"` |
| 257 | // RunnerID (ProtocolMacEngineering) is the runner whose connection it |
| 258 | // is: a runner signs out only its own (local ids repeat across |
| 259 | // runners). Dropped below 37. |
| 260 | RunnerID string `json:"runner_id,omitempty"` |
| 261 | } |
| 262 | |
| 263 | // ConnectCommand asks the runner to connect a new account for Person. |
| 264 | // SameMachine is always false from this instance and ignored by the |
| 265 | // runner; it stays because every connect frame an older instance sends |
| 266 | // carries it, and frames are parsed strictly. |
| 267 | type ConnectCommand struct { |
| 268 | Connection string `json:"connection"` |
| 269 | Person string `json:"person"` |
| 270 | Provider string `json:"provider"` |
| 271 | Kind string `json:"kind"` |
| 272 | SameMachine bool `json:"same_machine"` |
| 273 | // Billing and Budget come with an API-key connection |
| 274 | // (ProtocolMetered; Encode refuses the frame below it). |
| 275 | Billing string `json:"billing,omitempty"` |
| 276 | Budget *Budget `json:"budget,omitempty"` |
| 277 | } |
| 278 | |
| 279 | // CancelConnectCommand ends an attempt the person cancelled. |
| 280 | type CancelConnectCommand struct { |
| 281 | Connection string `json:"connection"` |
| 282 | Person string `json:"person"` |
| 283 | } |
| 284 | |
| 285 | // ConnectionReport is the runner's word on one attempt (Connection is the |
| 286 | // instance's id) or, with Connection empty, on a connection it holds |
| 287 | // (LocalID). Reason is from ConnectReasons when State is error. |
| 288 | type ConnectionReport struct { |
| 289 | Connection string `json:"connection"` |
| 290 | Person string `json:"person"` |
| 291 | LocalID string `json:"local_id"` |
| 292 | Provider string `json:"provider"` |
| 293 | State string `json:"state"` |
| 294 | Reason string `json:"reason"` |
| 295 | Label string `json:"label"` |
| 296 | Plan string `json:"plan"` |
| 297 | Headroom *Headroom `json:"headroom"` |
| 298 | Prompt *ConnectPrompt `json:"prompt"` |
| 299 | } |
| 300 | |
| 301 | // HeldConnection is one connection in hello: what the runner holds for |
| 302 | // which person, and how it stands. Replaces Account. |
| 303 | type HeldConnection struct { |
| 304 | LocalID string `json:"local_id"` |
| 305 | Person string `json:"person"` |
| 306 | Provider string `json:"provider"` |
| 307 | Kind string `json:"kind"` |
| 308 | Label string `json:"label"` |
| 309 | State string `json:"state"` |
| 310 | Plan string `json:"plan"` |
| 311 | Headroom *Headroom `json:"headroom"` |
| 312 | // ExpiresAt is when a pasted credential expires (absent otherwise); |
| 313 | // ExpiresEstimated says the date is an estimate. |
| 314 | ExpiresAt *time.Time `json:"expires_at,omitempty"` |
| 315 | ExpiresEstimated bool `json:"expires_estimated,omitempty"` |
| 316 | // An API-key connection's facts (ProtocolMetered): Encode drops the |
| 317 | // connection itself below it. |
| 318 | Harness string `json:"harness,omitempty"` |
| 319 | Upstream string `json:"upstream,omitempty"` |
| 320 | Billing string `json:"billing,omitempty"` |
| 321 | Model string `json:"model,omitempty"` |
| 322 | Budget *Budget `json:"budget,omitempty"` |
| 323 | Spend *Spend `json:"spend,omitempty"` |
| 324 | // The allowed set the runner applied, at ModelsRevision, with each |
| 325 | // model's state, and every model the key listed when it was last read |
| 326 | // (Choices; the page's list), each saying whether it can be allowed |
| 327 | // (ProtocolModels; dropped below it). |
| 328 | Models []ModelState `json:"models,omitempty"` |
| 329 | ModelsRevision int64 `json:"models_revision,omitempty"` |
| 330 | Choices []ModelChoice `json:"choices,omitempty"` |
| 331 | } |
| 332 | |
| 333 | // Switched is the quiet line under a reply (or on a step) that moved to |
| 334 | // another connection: from what, to what, and when the first one's limit |
| 335 | // resets when the provider said (null otherwise). |
| 336 | type Switched struct { |
| 337 | From string `json:"from"` |
| 338 | To string `json:"to"` |
| 339 | Window string `json:"window"` |
| 340 | ResetsAt *time.Time `json:"resets_at"` |
| 341 | } |
| 342 | |
| 343 | // ReasonNoConnection holds a turn: nothing the person has connected can |
| 344 | // answer now. The turn stays running (the message waits) and goes on its |
| 345 | // own when a connection returns; the person may cancel it. |
| 346 | const ReasonNoConnection = "no_connection" |
| 347 | |
| 348 | // KindToken is a Claude connection made from a pasted `claude setup-token` |
| 349 | // OAuth token (Paul, 2026-11-09): sealed in the browser to the runner's |
| 350 | // seal key, opened and kept only on the runner, passed to the CLI as |
| 351 | // CLAUDE_CODE_OAUTH_TOKEN. PromptToken is its card: run the command, |
| 352 | // approve, paste. |
| 353 | const ( |
| 354 | KindToken = "token" |
| 355 | PromptToken = "token" |
| 356 | // EndpointAnthropic is the endpoint a Claude token is bound to. |
| 357 | EndpointAnthropic = "https://api.anthropic.com:443" |
| 358 | // EndpointClaudeCode is what a sealed sign-in code is bound to: the |
| 359 | // redirect of the authorize URL that made it (PromptAuthorize). A |
| 360 | // box with it is a code, typed into the waiting CLI and never kept; |
| 361 | // a box with EndpointAnthropic is a token. Neither is taken as the |
| 362 | // other. |
| 363 | EndpointClaudeCode = "https://platform.claude.com/oauth/code/callback" |
| 364 | // SealedTTL is how long a sealed box may wait to be opened. |
| 365 | SealedTTL = 10 * time.Minute |
| 366 | // MaxSealedBytes caps a box (base64 included). |
| 367 | MaxSealedBytes = 4096 |
| 368 | // TokenLifetime estimates how long a setup token lasts from when it |
| 369 | // is pasted: the CLI says a year when it makes one, but neither the |
| 370 | // token nor the CLI reveals the date and the token may be older. So |
| 371 | // the expiry is marked an estimate, and TokenRenewal, when the row |
| 372 | // asks for a new one, is six weeks ahead. |
| 373 | TokenLifetime = 365 * 24 * time.Hour |
| 374 | TokenRenewal = 42 * 24 * time.Hour |
| 375 | ) |
| 376 | |
| 377 | // Sealed is a secret sealed to one runner (spec §2.4): Box is |
| 378 | // base64 (standard, padded) libsodium crypto_box_seal of a SealedPayload |
| 379 | // to the runner's seal key; the other fields travel in the clear beside |
| 380 | // it and the runner refuses the box when any differs from the payload |
| 381 | // inside, when ExpiresAt has passed, when the attempt is not the one it |
| 382 | // is running, or when it has seen (ConnectionID, Attempt) before. |
| 383 | type Sealed struct { |
| 384 | Box string `json:"box"` |
| 385 | RunnerID string `json:"runner_id"` |
| 386 | ConnectionID string `json:"connection_id"` |
| 387 | Provider string `json:"provider"` |
| 388 | Endpoint string `json:"endpoint"` |
| 389 | Attempt int64 `json:"attempt"` |
| 390 | ExpiresAt time.Time `json:"expires_at"` |
| 391 | } |
| 392 | |
| 393 | // SealedPayload is what a Sealed box holds: the secret and the binding. |
| 394 | type SealedPayload struct { |
| 395 | Secret string `json:"secret"` |
| 396 | RunnerID string `json:"runner_id"` |
| 397 | ConnectionID string `json:"connection_id"` |
| 398 | Provider string `json:"provider"` |
| 399 | Endpoint string `json:"endpoint"` |
| 400 | Attempt int64 `json:"attempt"` |
| 401 | ExpiresAt time.Time `json:"expires_at"` |
| 402 | } |
| 403 | |
| 404 | // FrameSecret (instance to runner, durable) carries a sealed secret for a |
| 405 | // connection attempt the runner is running. The instance forgets the box |
| 406 | // once the runner acks it. |
| 407 | const FrameSecret = "secret" |
| 408 | |
| 409 | // SecretCommand is the sealed secret for one of the person's attempts. |
| 410 | type SecretCommand struct { |
| 411 | Connection string `json:"connection"` |
| 412 | Person string `json:"person"` |
| 413 | Sealed Sealed `json:"sealed"` |
| 414 | } |
| 415 | |
| 416 | // FrameCheck (instance to runner, durable) is the person's Check again on |
| 417 | // a connection: the runner runs its real check (for a token, one small |
| 418 | // request), the only thing that clears a rejection besides an answered |
| 419 | // run. The outcome comes back as the connection's state in hello. |
| 420 | const FrameCheck = "check" |
| 421 | |
| 422 | // CheckCommand names the connection to check. |
| 423 | type CheckCommand struct { |
| 424 | Connection string `json:"connection"` |
| 425 | Person string `json:"person"` |
| 426 | LocalID string `json:"local_id"` |
| 427 | } |
| 428 | |