| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // StreamPath is the runner's one outbound WebSocket. |
| 10 | const StreamPath = "/api/runner/stream" |
| 11 | |
| 12 | // Frame types. Instance to runner: welcome, message, attachment, cancel, drain, ack, |
| 13 | // tool_result, reset, dispatch, cancel_step, recheck, watch_merge, ask, |
| 14 | // cancel_ask, forget_task, and at protocol 2 blob, send_pack, publish and |
| 15 | // refused. Runner to instance: hello, sync, lease, delta, activity, |
| 16 | // tool_call, reply, turn, move_in, notebook, step, merged, answer, ack, |
| 17 | // and at protocol 2 blob and published. |
| 18 | const ( |
| 19 | FrameToolCall = "tool_call" |
| 20 | FrameToolResult = "tool_result" |
| 21 | FrameActivity = "activity" |
| 22 | // FrameMoveIn asks the instance for a move_in or explore turn. |
| 23 | FrameMoveIn = "move_in" |
| 24 | // FrameNotebook reports a person's notebook (runner to instance); |
| 25 | // FrameReset clears it and restarts the coordinator (to the runner). |
| 26 | FrameNotebook = "notebook" |
| 27 | FrameReset = "reset" |
| 28 | |
| 29 | FrameWelcome = "welcome" |
| 30 | FrameMessage = "message" |
| 31 | FrameCancel = "cancel" |
| 32 | FrameDrain = "drain" |
| 33 | FrameAck = "ack" |
| 34 | FrameHello = "hello" |
| 35 | FrameSync = "sync" |
| 36 | FrameDelta = "delta" |
| 37 | FrameReply = "reply" |
| 38 | FrameTurn = "turn" |
| 39 | FrameLease = "lease" |
| 40 | // FrameGlance (runner to instance, protocol 23) is the Assistant's |
| 41 | // instant first line for a person turn. Like a delta it is sent once |
| 42 | // and never acked. |
| 43 | FrameGlance = "glance" |
| 44 | // FrameWarm (instance to runner, protocol 24) asks the person's |
| 45 | // primary to warm their Assistant: they opened Base. |
| 46 | FrameWarm = "warm" |
| 47 | ) |
| 48 | |
| 49 | // Close codes on the runner stream. |
| 50 | const ( |
| 51 | // CloseSuperseded tells an older runner process that a newer generation |
| 52 | // holds the execution lease. It must stop its namespaces and exit. |
| 53 | CloseSuperseded = 4409 |
| 54 | // CloseRevoked means the runner credential or its owner is no longer valid. |
| 55 | CloseRevoked = 4403 |
| 56 | ) |
| 57 | |
| 58 | // Frame is one WebSocket text message. Every frame has an id; a receiver |
| 59 | // acks a durable frame only after committing its effect, and treats a seen |
| 60 | // id as a no-op. Deltas are sent once and never acked. |
| 61 | type Frame struct { |
| 62 | ID string `json:"id"` |
| 63 | Type string `json:"type"` |
| 64 | Data json.RawMessage `json:"data"` |
| 65 | } |
| 66 | |
| 67 | // Durable reports whether a frame must be acked and resent until acked. |
| 68 | func (f Frame) Durable() bool { |
| 69 | switch f.Type { |
| 70 | case FramePrepareBase, FrameBase, FrameChangeDiff, FrameRequestAccess, FrameAccess, FrameMessage, FrameAttachment, FrameCancel, FrameReply, FrameTurn, FrameToolCall, FrameToolResult, FrameMoveIn, FrameNotebook, FrameReset, FrameDispatch, FrameCancelStep, FrameStep, FrameRecheck, FrameWatchMerge, FrameMerged, FrameAsk, FrameCancelAsk, FrameAnswer, FrameForgetTask, FrameBranch, FrameBlob, FrameSendPack, FramePublish, FramePublished, FrameConnect, FrameCancelConnect, FrameConnection, FrameUsage, FrameDisconnect, FramePreference, FrameSecret, FrameCheck, FrameModel, FrameModels, FrameBudget, FrameResumeSpend, FrameResearchFile, FrameRenderFile, FrameRelease, FramePriority, FrameWarm: |
| 71 | return true |
| 72 | } |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | // NewFrame encodes a typed body. |
| 77 | func NewFrame(id, kind string, body any) (Frame, error) { |
| 78 | raw, err := json.Marshal(body) |
| 79 | if err != nil { |
| 80 | return Frame{}, err |
| 81 | } |
| 82 | return Frame{ID: id, Type: kind, Data: raw}, nil |
| 83 | } |
| 84 | |
| 85 | // Decode strictly decodes the body into the type for f.Type. |
| 86 | func (f Frame) Decode() (any, error) { |
| 87 | var v any |
| 88 | switch f.Type { |
| 89 | case FrameWelcome: |
| 90 | v = &Welcome{} |
| 91 | case FrameMessage: |
| 92 | v = &MessageCommand{} |
| 93 | case FrameAttachment: |
| 94 | v = &AttachmentChunk{} |
| 95 | case FrameCancel: |
| 96 | v = &CancelCommand{} |
| 97 | case FrameDrain: |
| 98 | v = &Drain{} |
| 99 | case FrameAck: |
| 100 | v = &Ack{} |
| 101 | case FrameHello: |
| 102 | v = &Hello{} |
| 103 | case FrameSync: |
| 104 | v = &Sync{} |
| 105 | case FrameDelta: |
| 106 | v = &DeltaReport{} |
| 107 | case FrameReply: |
| 108 | v = &Reply{} |
| 109 | case FrameTurn: |
| 110 | v = &TurnReport{} |
| 111 | case FrameLease: |
| 112 | v = &LeaseCheck{} |
| 113 | case FrameToolCall: |
| 114 | v = &ToolCall{} |
| 115 | case FrameToolResult: |
| 116 | v = &ToolResult{} |
| 117 | case FrameActivity: |
| 118 | v = &ActivityReport{} |
| 119 | case FrameMoveIn: |
| 120 | v = &MoveIn{} |
| 121 | case FrameNotebook: |
| 122 | v = &NotebookReport{} |
| 123 | case FrameReset: |
| 124 | v = &ResetCommand{} |
| 125 | case FrameDispatch: |
| 126 | v = &DispatchCommand{} |
| 127 | case FrameCancelStep: |
| 128 | v = &CancelStepCommand{} |
| 129 | case FrameStep: |
| 130 | v = &StepReport{} |
| 131 | case FrameRecheck: |
| 132 | v = &RecheckCommand{} |
| 133 | case FrameWatchMerge: |
| 134 | v = &WatchMergeCommand{} |
| 135 | case FrameMerged: |
| 136 | v = &MergedReport{} |
| 137 | case FrameAsk: |
| 138 | v = &AskCommand{} |
| 139 | case FrameCancelAsk: |
| 140 | v = &CancelAskCommand{} |
| 141 | case FrameAnswer: |
| 142 | v = &AnswerReport{} |
| 143 | case FrameForgetTask: |
| 144 | v = &ForgetTaskCommand{} |
| 145 | case FrameBranch: |
| 146 | v = &BranchReport{} |
| 147 | case FrameConnect: |
| 148 | v = &ConnectCommand{} |
| 149 | case FrameCancelConnect: |
| 150 | v = &CancelConnectCommand{} |
| 151 | case FrameConnection: |
| 152 | v = &ConnectionReport{} |
| 153 | case FrameUsage: |
| 154 | v = &UsageReport{} |
| 155 | case FrameDisconnect: |
| 156 | v = &DisconnectCommand{} |
| 157 | case FramePreference: |
| 158 | v = &PreferenceCommand{} |
| 159 | case FrameSecret: |
| 160 | v = &SecretCommand{} |
| 161 | case FrameCheck: |
| 162 | v = &CheckCommand{} |
| 163 | case FrameModel: |
| 164 | v = &ModelCommand{} |
| 165 | case FrameRequestAccess: |
| 166 | v = &AccessCommand{} |
| 167 | case FrameAccess: |
| 168 | v = &AccessReport{} |
| 169 | case FrameModels: |
| 170 | v = &ModelsCommand{} |
| 171 | case FrameBudget: |
| 172 | v = &BudgetCommand{} |
| 173 | case FrameResumeSpend: |
| 174 | v = &ResumeSpendCommand{} |
| 175 | case FrameStepActivity: |
| 176 | v = &StepActivityReport{} |
| 177 | case FrameBlob: |
| 178 | v = &Blob{} |
| 179 | case FrameSendPack: |
| 180 | v = &SendPackCommand{} |
| 181 | case FramePublish: |
| 182 | v = &PublishCommand{} |
| 183 | case FramePublished: |
| 184 | v = &PublishedReport{} |
| 185 | case FrameRefused: |
| 186 | v = &Refused{} |
| 187 | case FrameChangeDiff: |
| 188 | v = &ChangeDiffUpload{} |
| 189 | case FrameLookupStep: |
| 190 | v = &LookupStep{} |
| 191 | case FrameLookupResult: |
| 192 | v = &LookupResult{} |
| 193 | case FramePrepareBase: |
| 194 | v = &PrepareBaseCommand{} |
| 195 | case FrameBase: |
| 196 | v = &BaseReport{} |
| 197 | case FrameResearchFile: |
| 198 | v = &ResearchFile{} |
| 199 | case FrameRenderFile: |
| 200 | v = &RenderFile{} |
| 201 | case FrameRelease: |
| 202 | v = &ReleaseCommand{} |
| 203 | case FramePriority: |
| 204 | v = &PriorityCommand{} |
| 205 | case FrameGlance: |
| 206 | v = &GlanceReport{} |
| 207 | case FrameWarm: |
| 208 | v = &WarmCommand{} |
| 209 | default: |
| 210 | return nil, fmt.Errorf("%w: frame type %q", ErrStrict, f.Type) |
| 211 | } |
| 212 | if f.ID == "" || len(f.ID) > 128 { |
| 213 | return nil, fmt.Errorf("%w: frame id", ErrStrict) |
| 214 | } |
| 215 | if err := Strict(f.Data, v); err != nil { |
| 216 | return nil, err |
| 217 | } |
| 218 | return v, nil |
| 219 | } |
| 220 | |
| 221 | // ParseFrame strictly decodes one frame and its body. |
| 222 | func ParseFrame(raw []byte) (Frame, any, error) { |
| 223 | if len(raw) > MaxFrameBytes { |
| 224 | return Frame{}, nil, fmt.Errorf("%w: frame size", ErrStrict) |
| 225 | } |
| 226 | var f Frame |
| 227 | if err := Strict(raw, &f); err != nil { |
| 228 | return Frame{}, nil, err |
| 229 | } |
| 230 | body, err := f.Decode() |
| 231 | return f, body, err |
| 232 | } |
| 233 | |
| 234 | // Welcome grants the runner its generation, the execution lease. The runner |
| 235 | // records it and presents it again in hello when it reconnects. |
| 236 | type Welcome struct { |
| 237 | Generation int64 `json:"generation"` |
| 238 | RunnerID string `json:"runner_id"` |
| 239 | // Protocol 2: the protocol the instance speaks on this connection, |
| 240 | // the runner's role, and the runner release this instance serves. |
| 241 | Protocol int `json:"protocol,omitempty"` |
| 242 | Role string `json:"role,omitempty"` |
| 243 | Release string `json:"release,omitempty"` |
| 244 | // Tainted: the instance holds that a check ran outside the sandbox on |
| 245 | // this runner; it stays so whatever the runner's own files say. |
| 246 | Tainted bool `json:"tainted,omitempty"` |
| 247 | } |
| 248 | |
| 249 | // WarmCommand asks the runner to warm Person's Assistant (and its |
| 250 | // glance) ahead of their first message: they opened Base. The runner |
| 251 | // warms only an Assistant they used lately, as on sync. |
| 252 | type WarmCommand struct { |
| 253 | Person string `json:"person"` |
| 254 | } |
| 255 | |
| 256 | // TailMessage is prior conversation, for a fresh coordinator session only. |
| 257 | type TailMessage struct { |
| 258 | Role string `json:"role"` |
| 259 | Text string `json:"text"` |
| 260 | } |
| 261 | |
| 262 | // MessageCommand asks the runner to run one coordinator turn. Kind is |
| 263 | // TurnPerson, or TurnMoveIn or TurnExplore with an empty Text: the runner |
| 264 | // writes those prompts from its own desk and catalog. |
| 265 | type MessageCommand struct { |
| 266 | PlacementNotes []NotebookEntry `json:"placement_notes,omitempty"` |
| 267 | Person string `json:"person"` |
| 268 | TurnID string `json:"turn_id"` |
| 269 | Attempt int64 `json:"attempt"` |
| 270 | Kind string `json:"kind"` |
| 271 | Text string `json:"text"` |
| 272 | // PersonText is the person's own words, one entry per message: the |
| 273 | // message body, or a card answer's value alone. Text may also carry |
| 274 | // the model's question an answer replies to; PersonText never does. |
| 275 | PersonText []string `json:"person_text"` |
| 276 | Tail []TailMessage `json:"tail"` |
| 277 | // Attachments are the files of the turn's messages. Their bytes came |
| 278 | // first, as attachment frames; the runner checks each against SHA256 |
| 279 | // before the turn starts. |
| 280 | Attachments []AttachmentRef `json:"attachments,omitempty"` |
| 281 | // Lane is the lane the turn runs in: empty for the coordinator, |
| 282 | // LaneAssistant for the Assistant (protocol 19; Encode refuses an |
| 283 | // Assistant message below it). |
| 284 | Lane string `json:"lane,omitempty"` |
| 285 | // Earlier are the files the person attached to their own Assistant |
| 286 | // messages within RunnerKeepFiles before this turn, newest first, at |
| 287 | // most MaxEarlierFiles; EarlierMore counts those beyond. No bytes come |
| 288 | // with them: the runner holds them already, or no longer does. Only an |
| 289 | // Assistant person turn carries them (protocol 33; Encode drops both |
| 290 | // below it). |
| 291 | Earlier []AttachmentRef `json:"earlier,omitempty"` |
| 292 | EarlierMore int `json:"earlier_more,omitempty"` |
| 293 | } |
| 294 | |
| 295 | // RunnerKeepFiles is how long a runner keeps a delivered attachment, and |
| 296 | // so how far back a turn's Earlier files reach. A runner may have removed |
| 297 | // one sooner, for room. |
| 298 | const RunnerKeepFiles = 14 * 24 * time.Hour |
| 299 | |
| 300 | // MaxEarlierFiles bounds a message frame's Earlier. |
| 301 | const MaxEarlierFiles = 20 |
| 302 | |
| 303 | // AttachmentRef names one attachment a turn carries. |
| 304 | type AttachmentRef struct { |
| 305 | ID string `json:"id"` |
| 306 | Kind string `json:"kind"` |
| 307 | Name string `json:"name"` |
| 308 | MediaType string `json:"media_type"` |
| 309 | Size int64 `json:"size"` |
| 310 | SHA256 string `json:"sha256"` |
| 311 | } |
| 312 | |
| 313 | // FrameAttachment carries one chunk of an attachment (instance to runner, |
| 314 | // durable). Chunks of a file arrive before the message frame that names |
| 315 | // it; a chunk written twice is the same bytes at the same offset. |
| 316 | const FrameAttachment = "attachment" |
| 317 | |
| 318 | // AttachmentChunk is chunk Index of Count of one attachment: Data is at |
| 319 | // most attach.ChunkBytes, at offset Index*attach.ChunkBytes. |
| 320 | type AttachmentChunk struct { |
| 321 | Person string `json:"person"` |
| 322 | AttachmentRef |
| 323 | Index int `json:"index"` |
| 324 | Count int `json:"count"` |
| 325 | Data []byte `json:"data"` |
| 326 | } |
| 327 | |
| 328 | // CancelCommand interrupts one attempt of one turn. |
| 329 | type CancelCommand struct { |
| 330 | Person string `json:"person"` |
| 331 | TurnID string `json:"turn_id"` |
| 332 | Attempt int64 `json:"attempt"` |
| 333 | } |
| 334 | |
| 335 | // Drain asks the runner to reconnect after RetryAfter seconds. |
| 336 | type Drain struct { |
| 337 | RetryAfter int `json:"retry_after"` |
| 338 | } |
| 339 | |
| 340 | // Ack confirms a durable frame was committed by the receiver. |
| 341 | type Ack struct { |
| 342 | ID string `json:"id"` |
| 343 | } |
| 344 | |
| 345 | // Hello opens the stream and repeats every 30 seconds as the heartbeat. |
| 346 | type Hello struct { |
| 347 | Generation int64 `json:"generation"` |
| 348 | Repos []string `json:"repos"` |
| 349 | // CreateIn lists namespaces the runner's agent may create repositories |
| 350 | // in; Home is where unrelated new work goes, if it is one of them. |
| 351 | CreateIn []string `json:"create_in"` |
| 352 | Home string `json:"home"` |
| 353 | // Agent is the forge agent account's name, for the grant-access hint. |
| 354 | Agent string `json:"agent"` |
| 355 | // Catalog describes every repository the agent can reach: names and |
| 356 | // short summaries only, bounded by MaxCatalog. |
| 357 | Catalog []RepoInfo `json:"catalog"` |
| 358 | // Connections are the accounts the runner holds, per person. |
| 359 | Connections []HeldConnection `json:"connections"` |
| 360 | Versions map[string]string `json:"versions"` |
| 361 | // SealKey is the runner's X25519 public key (base64) for sealed |
| 362 | // secrets. |
| 363 | SealKey string `json:"seal_key,omitempty"` |
| 364 | // Protocol 2: the protocol the runner speaks (absent: 1), its |
| 365 | // platform, its label (the machine's name), how it confines steps and |
| 366 | // what it can do. |
| 367 | Protocol int `json:"protocol,omitempty"` |
| 368 | Platform *Platform `json:"platform,omitempty"` |
| 369 | Label string `json:"label,omitempty"` |
| 370 | Sandbox string `json:"sandbox,omitempty"` |
| 371 | Capabilities *Capabilities `json:"capabilities,omitempty"` |
| 372 | // Inventory (ProtocolMacEngineering): a worker's hello sent after the |
| 373 | // welcome says its connections and its credential hold in full. A |
| 374 | // hello without it (the one before the welcome) says nothing of them |
| 375 | // the instance may release a hold on. |
| 376 | Inventory bool `json:"inventory,omitempty"` |
| 377 | } |
| 378 | |
| 379 | // Catalog bounds. |
| 380 | const ( |
| 381 | MaxCatalog = 100 |
| 382 | MaxRepoNames = 12 |
| 383 | MaxRepoHints = 8 |
| 384 | MaxRepoString = 300 |
| 385 | ) |
| 386 | |
| 387 | // RepoInfo is one catalog entry: what a repository is, from its README, |
| 388 | // AGENTS.md and manifests, so the coordinator can find "Woodstar" by name. |
| 389 | type RepoInfo struct { |
| 390 | Repo string `json:"repo"` |
| 391 | DefaultBranch string `json:"default_branch"` |
| 392 | Summary string `json:"summary"` |
| 393 | Names []string `json:"names"` |
| 394 | Hints []string `json:"hints"` |
| 395 | UpdatedAt time.Time `json:"updated_at"` |
| 396 | // HeadSHA and HeadSubject are the default branch's newest commit |
| 397 | // (UpdatedAt is its time). Gate is the checks gate found at that |
| 398 | // commit (".amadan/ci.d", ".amadan/ci", "make ci" or ""); ChecksReason |
| 399 | // is why this runner cannot run the project's checks (needs_macos), or |
| 400 | // empty when it can. |
| 401 | HeadSHA string `json:"head_sha"` |
| 402 | HeadSubject string `json:"head_subject"` |
| 403 | Gate string `json:"gate"` |
| 404 | ChecksReason string `json:"checks_reason"` |
| 405 | // Protocol 6: Empty is a repository the agent reaches that has no |
| 406 | // commits yet, so nothing to read. Its entry has the repository's |
| 407 | // name and nothing from a default branch. Encode drops such entries |
| 408 | // for an older instance, to which they were never in the catalog. |
| 409 | Empty bool `json:"empty,omitempty"` |
| 410 | // Protocol 2: the repository's gate calls Xcode's tools or it is an |
| 411 | // Xcode project (NeedsMacOS), and it has a shared scheme for the iOS |
| 412 | // simulator (Simulator). The instance derives checks' needs from these. |
| 413 | NeedsMacOS bool `json:"needs_macos,omitempty"` |
| 414 | Simulator bool `json:"ios_simulator,omitempty"` |
| 415 | } |
| 416 | |
| 417 | // SessionState is the runner's durable session for a person in a lane. |
| 418 | type SessionState struct { |
| 419 | Person string `json:"person"` |
| 420 | Provider string `json:"provider"` |
| 421 | Account string `json:"account"` |
| 422 | SessionID string `json:"session_id"` |
| 423 | LastTurn string `json:"last_turn"` |
| 424 | // Lane is the session's lane, empty for the coordinator (protocol |
| 425 | // 19; Encode drops the other lanes' sessions below it). |
| 426 | Lane string `json:"lane,omitempty"` |
| 427 | } |
| 428 | |
| 429 | // Run is one coordinator turn attempt the runner holds. |
| 430 | type Run struct { |
| 431 | TurnID string `json:"turn_id"` |
| 432 | Attempt int64 `json:"attempt"` |
| 433 | State string `json:"state"` |
| 434 | } |
| 435 | |
| 436 | // LeaseCheck asks the instance to confirm, before execution, that this |
| 437 | // connection still holds Generation. The instance acks it only then. |
| 438 | type LeaseCheck struct { |
| 439 | AccessCard string `json:"access_card,omitempty"` |
| 440 | Generation int64 `json:"generation"` |
| 441 | // Protocol 2: the start authorization of one step attempt. The |
| 442 | // instance acks it only while the attempt is assigned to this runner |
| 443 | // and not withdrawn, and records that it started. |
| 444 | StepID string `json:"step_id,omitempty"` |
| 445 | Attempt int64 `json:"attempt,omitempty"` |
| 446 | // Publication: the primary confirms, right before it pushes a worker's |
| 447 | // commit, that this publication is still pending. |
| 448 | Publication string `json:"publication,omitempty"` |
| 449 | // Protocol 3: Purpose merge_request is the confirmation right before |
| 450 | // a hub merge request for the step attempt: the instance decides the |
| 451 | // merge again (merge authority) and acks only if it may go now. |
| 452 | Purpose string `json:"purpose,omitempty"` |
| 453 | // Protocol 9: Digest binds a preview_publish check to the export the |
| 454 | // step reported as built. |
| 455 | Digest string `json:"digest,omitempty"` |
| 456 | // ProtocolPlaces: Destination binds a site_publish check to the |
| 457 | // SiteDestination the runner is about to deploy to. |
| 458 | Destination string `json:"destination,omitempty"` |
| 459 | } |
| 460 | |
| 461 | // LeasePurposeRender is a LeaseCheck's Purpose right before each of a |
| 462 | // Design step's render_page calls (ProtocolDesign): the instance checks |
| 463 | // the step's specialist binding again and acks only while it holds. |
| 464 | const LeasePurposeRender = "page_render" |
| 465 | |
| 466 | // LeasePurposeMerge is a LeaseCheck's Purpose right before a hub merge |
| 467 | // request. |
| 468 | const LeasePurposeMerge = "merge_request" |
| 469 | |
| 470 | // LeasePurposePreview is a LeaseCheck's Purpose right before each of a |
| 471 | // preview step's Carloku mutations, with the export's Digest. |
| 472 | const LeasePurposePreview = "preview_publish" |
| 473 | |
| 474 | // RefusedMergeNotAuthorized is a lease refusal's reason when the instance |
| 475 | // will not let a merge request go (merge authority). |
| 476 | const RefusedMergeNotAuthorized = "merge_not_authorized" |
| 477 | |
| 478 | // RefusedPreviewNotBuilt refuses a preview_publish check whose step's |
| 479 | // built report the instance has not applied yet: the runner asks again. |
| 480 | const RefusedPreviewNotBuilt = "preview_not_built" |
| 481 | |
| 482 | // Sync is sent on every connect, after hello. |
| 483 | type Sync struct { |
| 484 | Sessions []SessionState `json:"sessions"` |
| 485 | Runs []Run `json:"runs"` |
| 486 | // Steps are the step attempts the runner holds (running, or reported |
| 487 | // and not yet acked). |
| 488 | Steps []StepRun `json:"steps"` |
| 489 | // Asks are the question attempts the runner holds, likewise. |
| 490 | Asks []AskRun `json:"asks"` |
| 491 | // Connections are the connections the runner holds, as in hello, so |
| 492 | // a reconnect reconciles them. |
| 493 | Connections []HeldConnection `json:"connections"` |
| 494 | } |
| 495 | |
| 496 | // GlanceReport is the Assistant's instant first line for one attempt of |
| 497 | // a person turn: one plain line (GlanceText), shown until the reply. |
| 498 | type GlanceReport struct { |
| 499 | TurnID string `json:"turn_id"` |
| 500 | Attempt int64 `json:"attempt"` |
| 501 | Text string `json:"text"` |
| 502 | } |
| 503 | |
| 504 | // DeltaReport is streamed coordinator text. |
| 505 | type DeltaReport struct { |
| 506 | TurnID string `json:"turn_id"` |
| 507 | Attempt int64 `json:"attempt"` |
| 508 | Offset int64 `json:"offset"` |
| 509 | Text string `json:"text"` |
| 510 | } |
| 511 | |
| 512 | // Usage is one usage row (Connections spec §6): what one attempt of a |
| 513 | // turn, step or question used on one model, as its harness reported it. |
| 514 | // A figure the harness did not report is null, never zero; Complete is |
| 515 | // true only when the harness's final usage message for the attempt was |
| 516 | // seen. No cost: subscriptions within plan have none, and metered |
| 517 | // estimates come with the endpoints of a later slice. |
| 518 | type Usage struct { |
| 519 | Provider string `json:"provider"` |
| 520 | Connection string `json:"connection"` |
| 521 | Model string `json:"model"` |
| 522 | Attempt int64 `json:"attempt"` |
| 523 | Outcome string `json:"outcome"` |
| 524 | Complete bool `json:"complete"` |
| 525 | InputTokens *int64 `json:"input_tokens"` |
| 526 | CachedInputTokens *int64 `json:"cached_input_tokens"` |
| 527 | CacheWriteTokens *int64 `json:"cache_write_tokens"` |
| 528 | OutputTokens *int64 `json:"output_tokens"` |
| 529 | ReasoningOutputTokens *int64 `json:"reasoning_output_tokens"` |
| 530 | // Turns is how many model requests the attempt made, as the harness |
| 531 | // counted them (Claude's num_turns, Codex's usage notices); null when |
| 532 | // it did not say. A multi-model attempt carries it on one row only. |
| 533 | // Protocol 3: never sent to an older instance (Encode). |
| 534 | Turns *int64 `json:"turns,omitempty"` |
| 535 | // A metered connection's charge (ProtocolMetered, connectors spec |
| 536 | // §2.3): the cost the runner priced from its table (null when it |
| 537 | // cannot price the row), the table's currency and version, and the |
| 538 | // cost the harness reported itself (null when it reported none). |
| 539 | CostMicros *int64 `json:"cost_micros,omitempty"` |
| 540 | Currency string `json:"currency,omitempty"` |
| 541 | PriceVersion string `json:"price_version,omitempty"` |
| 542 | CostReportedMicros *int64 `json:"cost_reported_micros,omitempty"` |
| 543 | // The model's family and tier under Equipo's tier table, and that |
| 544 | // table's version (ProtocolModels, connectors spec §16.2, §16.5); |
| 545 | // ModelMismatch: the provider reported another model than the one |
| 546 | // Equipo chose, so the row is that model's and charged at its price. |
| 547 | Family string `json:"family,omitempty"` |
| 548 | Tier string `json:"tier,omitempty"` |
| 549 | TierVersion string `json:"tier_version,omitempty"` |
| 550 | ModelMismatch bool `json:"model_mismatch,omitempty"` |
| 551 | } |
| 552 | |
| 553 | // Usage outcomes: how the attempt the row belongs to ended. |
| 554 | const ( |
| 555 | OutcomeReplied = "replied" |
| 556 | OutcomeFailed = "failed" |
| 557 | OutcomeCancelled = "cancelled" |
| 558 | OutcomeSuperseded = "superseded" |
| 559 | ) |
| 560 | |
| 561 | // FrameUsage (runner to instance, durable) carries the usage rows of |
| 562 | // attempts that produced no reply (failed, cancelled, refused before a |
| 563 | // failover), of a token connection's health check and of a metered |
| 564 | // connection's probe. Exactly one of TurnID, StepID, QuestionID, CheckID |
| 565 | // or ProbeID is set. |
| 566 | const FrameUsage = "usage" |
| 567 | |
| 568 | // UsageReport is a usage frame. |
| 569 | type UsageReport struct { |
| 570 | TurnID string `json:"turn_id"` |
| 571 | StepID string `json:"step_id"` |
| 572 | QuestionID string `json:"question_id"` |
| 573 | // CheckID names one health check of a token connection: a minimal |
| 574 | // real request whose usage counts like any other (absent otherwise). |
| 575 | CheckID string `json:"check_id,omitempty"` |
| 576 | // ProbeID names one health probe of a metered connection: a |
| 577 | // one-token completion on its model, charged to its budget like any |
| 578 | // other (ProtocolMetered; below it, a check). |
| 579 | ProbeID string `json:"probe_id,omitempty"` |
| 580 | Rows []Usage `json:"rows"` |
| 581 | } |
| 582 | |
| 583 | // Reply is the committed coordinator answer for a turn. |
| 584 | type Reply struct { |
| 585 | TurnID string `json:"turn_id"` |
| 586 | Attempt int64 `json:"attempt"` |
| 587 | Text string `json:"text"` |
| 588 | Usage []Usage `json:"usage"` |
| 589 | Timing *TurnTiming `json:"timing"` |
| 590 | // Switched is set when this turn moved to another connection. |
| 591 | Switched *Switched `json:"switched"` |
| 592 | } |
| 593 | |
| 594 | // Coordinator tools. The runner's tool server lists exactly these for a |
| 595 | // conversation; the instance authorises each call for the turn's person. |
| 596 | const ( |
| 597 | ToolStatus = "status" |
| 598 | ToolAddProject = "add_project" |
| 599 | ToolAskPerson = "ask_person" |
| 600 | // ToolCreateProject is authorised by the instance, carried out by the |
| 601 | // runner (which holds the forge credential), then confirmed with |
| 602 | // ToolProjectCreated, which the model never sees. |
| 603 | ToolCreateProject = "create_project" |
| 604 | ToolProjectCreated = "project_created" |
| 605 | // Read-only context tools: find answers from the catalog on the |
| 606 | // instance; search_code and read_file are authorised by the instance |
| 607 | // and read the runner's clone. |
| 608 | ToolFind = "find" |
| 609 | ToolSearchCode = "search_code" |
| 610 | ToolReadFile = "read_file" |
| 611 | // ToolSetProjectHome sets the team's namespace for new projects, when |
| 612 | // the person asks. |
| 613 | ToolSetProjectHome = "set_project_home" |
| 614 | // ToolStarProject stars or unstars a project for the person, when they |
| 615 | // say it is (or no longer is) a priority: their choice, which Equipo's |
| 616 | // own star rules never override. |
| 617 | ToolStarProject = "star_project" |
| 618 | // ToolWriteNote and ToolReadNotes keep the coordinator's notebook on |
| 619 | // its desk: the instance authorises the call for the turn (on the |
| 620 | // entry count and size), the runner checks each entry's provenance and |
| 621 | // writes the desk, then reports the notebook for the owner's view. |
| 622 | ToolWriteNote = "write_note" |
| 623 | ToolReadNotes = "read_notes" |
| 624 | ) |
| 625 | |
| 626 | // ConverseTools is the coordinator's tool set in a conversation. |
| 627 | var ConverseTools = []string{ToolRequestAccess, ToolStatus, ToolFind, ToolSearchCode, ToolReadFile, ToolAddProject, ToolCreateProject, ToolSetProjectHome, ToolStarProject, ToolAskPerson, ToolWriteNote, ToolReadNotes, ToolRecordTask, ToolAddFinding, ToolStartTask, ToolReadTask, ToolCancelTask, ToolAddReport, ToolHoldMerge, ToolSetMergePolicy, ToolProposeSpecialist, ToolResearch, ToolOfferRetry, ToolRetryStep, ToolPlaces, ToolProposePlace, ToolPublishSite, ToolSiteStatus} |
| 628 | |
| 629 | // MoveInTools are the tools a move_in or explore turn may use: it looks |
| 630 | // around and takes notes, and never acts for the person. |
| 631 | var MoveInTools = []string{ToolStatus, ToolFind, ToolSearchCode, ToolReadFile, ToolWriteNote, ToolReadNotes} |
| 632 | |
| 633 | // Notebook bounds: entries, one entry's text, the whole notebook. |
| 634 | const ( |
| 635 | MaxNoteEntries = 40 |
| 636 | MaxEntryBytes = 400 |
| 637 | MaxNotebookBytes = 12 << 10 |
| 638 | ) |
| 639 | |
| 640 | // Note sources: what the runner verified an entry came from. person is |
| 641 | // the person's own words in the turn it was written (quoted exactly); |
| 642 | // repo is a file the coordinator read in that turn (Repo and Path); own |
| 643 | // is the coordinator's own note (what status, find or the index showed). |
| 644 | const ( |
| 645 | NoteSystem = "system" |
| 646 | NotePerson = "person" |
| 647 | NoteRepo = "repo" |
| 648 | NoteOwn = "own" |
| 649 | ) |
| 650 | |
| 651 | // NotebookEntry is one line of the coordinator's notebook with its |
| 652 | // provenance, recorded by the runner, never by the model. |
| 653 | type NotebookEntry struct { |
| 654 | ID string `json:"id"` |
| 655 | Source string `json:"source"` |
| 656 | Repo string `json:"repo"` |
| 657 | Path string `json:"path"` |
| 658 | Text string `json:"text"` |
| 659 | At time.Time `json:"at"` |
| 660 | } |
| 661 | |
| 662 | // NotebookReport is the runner's notebook for a person after a change, |
| 663 | // for the owner's view (the instance keeps the newest). |
| 664 | type NotebookReport struct { |
| 665 | Person string `json:"person"` |
| 666 | Entries []NotebookEntry `json:"entries"` |
| 667 | } |
| 668 | |
| 669 | // ResetCommand clears a person's notebook and starts their coordinator |
| 670 | // afresh: a new session, and it moves in again. |
| 671 | type ResetCommand struct { |
| 672 | Person string `json:"person"` |
| 673 | } |
| 674 | |
| 675 | // ToolCall is one coordinator tool use, for the turn and attempt it ran |
| 676 | // in. CallID is the provider's tool-use id with the turn, so a repeated |
| 677 | // call gets the stored result. Args is the tool input as JSON. |
| 678 | type ToolCall struct { |
| 679 | CallID string `json:"call_id"` |
| 680 | TurnID string `json:"turn_id"` |
| 681 | Attempt int64 `json:"attempt"` |
| 682 | Name string `json:"name"` |
| 683 | Args json.RawMessage `json:"args"` |
| 684 | } |
| 685 | |
| 686 | // ToolResult answers a tool call. Text is what the model reads; Code is a |
| 687 | // fixed outcome the runner may act on (added means clone the repository). |
| 688 | type ToolResult struct { |
| 689 | CallID string `json:"call_id"` |
| 690 | Person string `json:"person"` |
| 691 | IsError bool `json:"is_error"` |
| 692 | Code string `json:"code"` |
| 693 | Repo string `json:"repo"` |
| 694 | Text string `json:"text"` |
| 695 | // Replayed: an earlier attempt of the turn already made this call |
| 696 | // (same tool, same arguments); this is its stored result, and nothing |
| 697 | // ran again. |
| 698 | Replayed bool `json:"replayed"` |
| 699 | } |
| 700 | |
| 701 | // MutationKinds are the designated mutating tools and the kind of |
| 702 | // operation each makes (Connections spec §3 step 4). The instance keeps |
| 703 | // them in the turn's mutation ledger and holds any later call of the |
| 704 | // turn to them, whatever its wording; every other tool runs fresh. |
| 705 | var MutationKinds = map[string]string{ |
| 706 | ToolAddProject: "project", |
| 707 | ToolCreateProject: "project_create", |
| 708 | ToolSetProjectHome: "home", |
| 709 | ToolStarProject: "star", |
| 710 | ToolRecordTask: "task_record", |
| 711 | ToolStartTask: "task_start", |
| 712 | ToolCancelTask: "task_cancel", |
| 713 | ToolAddFinding: "finding", |
| 714 | ToolRequestAccess: "ask", |
| 715 | ToolAskPerson: "ask", |
| 716 | // One proposal per profile in a turn, in any attempt and wording. |
| 717 | ToolProposeSpecialist: "specialist", |
| 718 | // One research request per task in a turn, in any attempt and wording. |
| 719 | ToolResearch: "research", |
| 720 | // One offer of a failed merge per task in a turn. |
| 721 | ToolOfferRetry: "retry_offer", |
| 722 | // One retry per task and step kind in a turn. |
| 723 | ToolRetryStep: "task_retry", |
| 724 | // One place proposal per project and one publish offer per task in a |
| 725 | // turn. |
| 726 | ToolProposePlace: "place_proposal", |
| 727 | ToolPublishSite: "site_offer", |
| 728 | } |
| 729 | |
| 730 | // WriteTools change something outside the coordinator's session: the |
| 731 | // runner replays an exact repeat of one in a later attempt from its own |
| 732 | // record. |
| 733 | var WriteTools = []string{ToolRequestAccess, ToolAddProject, ToolCreateProject, ToolSetProjectHome, ToolStarProject, ToolAskPerson, ToolWriteNote, ToolRecordTask, ToolAddFinding, ToolStartTask, ToolCancelTask, ToolAddReport, ToolHoldMerge, ToolSetMergePolicy, ToolProjectCreated, ToolProposeSpecialist, ToolResearch, ToolOfferRetry, ToolRetryStep, ToolProposePlace, ToolPublishSite} |
| 734 | |
| 735 | // ActivityReport is what the coordinator started doing: thinking, or a |
| 736 | // tool by name. Sent once, never acked, like a delta. |
| 737 | type ActivityReport struct { |
| 738 | TurnID string `json:"turn_id"` |
| 739 | Attempt int64 `json:"attempt"` |
| 740 | Kind string `json:"kind"` |
| 741 | Tool string `json:"tool"` |
| 742 | // Repo is the repository a read tool was authorised for, or empty. |
| 743 | Repo string `json:"repo"` |
| 744 | } |
| 745 | |
| 746 | // MoveIn asks for a turn the coordinator takes on its own: Kind is |
| 747 | // TurnMoveIn (its desk is empty) or TurnExplore (the reachable namespaces |
| 748 | // changed). The instance starts at most one at a time per person, after |
| 749 | // any person turn, and a person's message always goes first. |
| 750 | type MoveIn struct { |
| 751 | Person string `json:"person"` |
| 752 | Kind string `json:"kind"` |
| 753 | } |
| 754 | |
| 755 | // TurnReport moves a turn forward without a reply: running on a new |
| 756 | // attempt after crash recovery, failed or cancelled. Reason is a fixed |
| 757 | // sanitised code, never provider output. |
| 758 | type TurnReport struct { |
| 759 | TurnID string `json:"turn_id"` |
| 760 | Attempt int64 `json:"attempt"` |
| 761 | State string `json:"state"` |
| 762 | Reason string `json:"reason"` |
| 763 | } |
| 764 | |
| 765 | // Failure reasons a runner may report. Anything else is refused. |
| 766 | var Reasons = map[string]bool{ |
| 767 | "": true, |
| 768 | "cancelled": true, |
| 769 | "interrupted": true, |
| 770 | "runner_restarted": true, |
| 771 | "turn_timeout": true, |
| 772 | "provider_failed": true, |
| 773 | "provider_rejected": true, |
| 774 | "policy_rejected": true, |
| 775 | "account_unavailable": true, |
| 776 | "sign_in_again": true, |
| 777 | "superseded": true, |
| 778 | "no_runner": true, |
| 779 | // desk_unavailable: a move-in or explore turn finished but the |
| 780 | // runner could not record it, so it does not count as done. |
| 781 | "desk_unavailable": true, |
| 782 | // no_connection: nothing connected can answer (with state running, |
| 783 | // the turn waits; see ReasonNoConnection). limited: the provider |
| 784 | // refused on quota. connection_removed: its connection was removed. |
| 785 | ReasonNoConnection: true, |
| 786 | "limited": true, |
| 787 | "connection_removed": true, |
| 788 | // attachment_unavailable: a file the turn carries never arrived |
| 789 | // whole on the runner (its size or SHA-256 did not match). |
| 790 | ReasonAttachmentUnavailable: true, |
| 791 | // runner_storage_full: the runner refused an attachment for room. |
| 792 | ReasonStorageFull: true, |
| 793 | } |
| 794 | |
| 795 | // ReasonStorageFull fails a turn whose attachment the runner had no room |
| 796 | // for (its quota, after the oldest unprotected files made room). |
| 797 | const ReasonStorageFull = "runner_storage_full" |
| 798 | |
| 799 | // ReasonAttachmentUnavailable fails a turn whose attachment is missing. |
| 800 | const ReasonAttachmentUnavailable = "attachment_unavailable" |
| 801 | |