// Package wire is the thin v1 contract: person API v1 resources and events, // and the runner stream frames. The instance, the runner, the headless suite // and the native apps all build against these types and the recorded // fixtures in testdata. package wire import ( "bytes" "encoding/json" "errors" "io" "time" ) // Turn states. A turn ends in exactly one of replied, failed or cancelled. const ( TurnQueued = "queued" TurnRunning = "running" // TurnWaiting is a dispatched turn held by its runner because nothing // the person connected can answer now (reason no_connection). It is // not an ending: the turn goes on its own when a connection returns, // and the person may cancel it. TurnWaiting = "waiting" TurnReplied = "replied" TurnFailed = "failed" TurnCancelled = "cancelled" ) // Message roles. const ( RolePerson = "person" RoleCoordinator = "coordinator" ) // Event types on GET /api/v1/events. const ( EventMessage = "message" EventTurn = "turn" EventDelta = "delta" EventRunner = "runner" EventPing = "ping" EventCard = "card" EventProject = "project" EventActivity = "activity" // EventGlance carries the Assistant's instant first line for a turn // (Glance), on the Assistant's stream only. Like a delta it has // revision 0 and nothing stores it. EventGlance = "glance" // EventCoordinator carries the person's Coordinator resource. EventCoordinator = "coordinator" ) // Turn kinds. A person turn answers the person's messages; the runner // starts the others itself: move_in when the coordinator's desk is empty // (it looks around and introduces itself once) and explore when the // reachable namespaces changed (it looks at the difference only). const ( TurnPerson = "" TurnMoveIn = "move_in" TurnExplore = "explore" // TurnHousekeeping is a housekeeping judgement turn (spec // 2026-11-08-equipo-housekeeping.md, slice 3): queued after the // person's own turns and after move-in and explore. TurnHousekeeping = "housekeeping" // TurnSpecialistAdded is the coordinator's continuation after an // owner's Add on a specialist card for one project (spec // 2026-11-28-dynamic-specialists.md, section 1, Routing): queued by // the instance in the Add's transaction, once per card, in the // proposing turn's lane. It may only record the card's task in the // card's project and start it with the card's specialist. TurnSpecialistAdded = "specialist_added" // ReasonYielded ends a move_in or explore turn that gave way to a // person's message; it continues in a new turn after the answer. ReasonYielded = "yielded" ) // Coordinator states: new (has not moved in), moving_in, ready. const ( CoordinatorNew = "new" CoordinatorMovingIn = "moving_in" CoordinatorReady = "ready" ) // Notebook is GET /api/v1/notebook: the person's coordinator's notebook // as its runner last reported it, each entry with where it came from. ID // is the person. POST /api/v1/notebook/reset clears it and starts the // coordinator afresh (202 with the Coordinator, 409 while a turn runs). type Notebook struct { ID string `json:"id"` Revision int64 `json:"revision"` Entries []NotebookEntry `json:"entries"` UpdatedAt *time.Time `json:"updated_at"` } // Coordinator is the person's coordinator as the person sees it: whether // it has moved in, and the move-in turn while one runs. ID is the person. type Coordinator struct { ID string `json:"id"` Revision int64 `json:"revision"` State string `json:"state"` TurnID string `json:"turn_id"` MovedInAt *time.Time `json:"moved_in_at"` } // Project states. const ( ProjectActive = "active" ProjectRemoved = "removed" ) // Card kinds and states. A project card says what the coordinator added // and offers Remove; a question card waits for one answer. const ( CardProject = "project" CardQuestion = "question" CardAccess = "access" CardAdded = "added" CardWorking = "working" CardCreated = "created" CardChanged = "changed" CardRemoved = "removed" CardOpen = "open" CardAnswered = "answered" // CardHome reports a change of where new projects go (Name is the // namespace, empty when cleared) and offers Undo while CardHomeSet. CardHome = "home" CardHomeSet = "set" CardUndone = "undone" // CardHomeAsk asks the owner to confirm a change the coordinator was // not sure the person asked for; ChoiceConfirm applies it. CardHomeAsk = "ask" // ChoiceUndo is the answer that undoes a home card. ChoiceUndo = "undo" ChoiceConfirm = "confirm" ActivityThink = "thinking" ActivityTool = "tool" MaxQuestion = 500 MaxOptions = 6 MaxOption = 80 MaxProjectCards = 50 ) // Terminal reports whether a turn state is final. // InFlight are the states of a turn that has not ended. var InFlight = []string{TurnQueued, TurnRunning, TurnWaiting} func Terminal(state string) bool { return state == TurnReplied || state == TurnFailed || state == TurnCancelled } // Me is GET /api/v1/me. Person repeats the identity in the shape the // earlier person API used. type Me struct { ID string `json:"id"` Address string `json:"address"` Role string `json:"role"` Person Person `json:"person"` TeamID string `json:"team_id"` TeamName string `json:"team_name"` Revision int64 `json:"revision"` } // Person is a team member. type Person struct { Address string `json:"address"` DisplayIdentity string `json:"display_identity"` Active bool `json:"active"` Role string `json:"role"` } // Message is one line of the person's conversation. type Message struct { ProjectID string `json:"project_id,omitempty"` ID string `json:"id"` Revision int64 `json:"revision"` Role string `json:"role"` Text string `json:"text"` TurnID string `json:"turn_id"` CreatedAt time.Time `json:"created_at"` // Switched is the quiet line under a reply that moved to another // connection; clients render it with the reset in the person's zone. Switched *Switched `json:"switched"` // Attachments are the images and files sent with a person's message, // in the order they were attached; omitted when there are none. Attachments []MessageAttachment `json:"attachments,omitempty"` // Lookups are the answers and sources shown under an Assistant reply, // in call order, at most MaxLookupsPerReply; omitted when there are // none (spec 2026-11-26-assistant-abilities §4.6). Lookups []AssistantLookup `json:"lookups,omitempty"` } // MessageAttachment is an image or file the person uploaded (POST // /api/v1/attachments) and then sent with a message. Kind is image or // file; MediaType was sniffed from the bytes. Width and Height are an // image's upright size, 0 for a file. MessageID is empty until sent. The // bytes are GET /api/v1/attachments/{id}; an image's thumbnail (at most // 1024 px) is GET /api/v1/attachments/{id}/preview. type MessageAttachment struct { ID string `json:"id"` Revision int64 `json:"revision"` Kind string `json:"kind"` Name string `json:"name"` MediaType string `json:"media_type"` Size int64 `json:"size"` SHA256 string `json:"sha256"` Width int `json:"width"` Height int `json:"height"` MessageID string `json:"message_id"` CreatedAt time.Time `json:"created_at"` } // Message attachment kinds. const ( AttachmentImage = "image" AttachmentFile = "file" ) // Turn is one coordinator answer to one or more queued person messages, // or a turn the coordinator takes on its own (Kind move_in or explore). type Turn struct { ID string `json:"id"` Revision int64 `json:"revision"` Kind string `json:"kind"` State string `json:"state"` Reason string `json:"reason"` Attempt int64 `json:"attempt"` MessageIDs []string `json:"message_ids"` ReplyID string `json:"reply_id"` // Connection is the runner's connection (LocalID) that answered. Connection string `json:"connection"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` // Timing is where a replied turn's time went, for diagnosis only. Timing *TurnTiming `json:"timing"` } // TurnTiming splits one turn's wall time. Each side measures on its own // clock: the instance from the message commit to the runner's ack, the // runner from receiving the frame. Milliseconds; zero means not seen. type TurnTiming struct { // QueuedMs is message commit to the runner's ack of the command. QueuedMs int64 `json:"queued_ms"` // StartMs is the runner receiving the frame to the user frame on the // provider's stdin: lease check, process start or copy rotation. StartMs int64 `json:"start_ms"` // The rest count from the user frame on stdin. FirstEventMs int64 `json:"first_event_ms"` FirstThinkingMs int64 `json:"first_thinking_ms"` FirstToolMs int64 `json:"first_tool_ms"` FirstTextMs int64 `json:"first_text_ms"` LastTextMs int64 `json:"last_text_ms"` ResultMs int64 `json:"result_ms"` // Cold is true when the provider process started for this turn. Cold bool `json:"cold"` } // Runner is the person's own runner as the person sees it. type Runner struct { ID string `json:"id"` Revision int64 `json:"revision"` Online bool `json:"online"` // Label is the machine's name ("Tito Studio"); Role primary or worker; // Since when it last came online or went offline; Platform and // Capabilities what it reported (null until it has). Label string `json:"label"` Role string `json:"role"` Since *time.Time `json:"since"` Platform *Platform `json:"platform"` Capabilities *Capabilities `json:"capabilities"` // Removing: the person removed it and it is stopping the work it may // have started; it is revoked once that work's end is proven. Removing bool `json:"removing"` // SealKey is the runner's X25519 public key (base64), derived from its // enrolment ed25519 key: secrets are sealed to it (spec §2.4). SealKey string `json:"seal_key"` } // RunnerList is GET /api/v1/runners: the person's runners, primary first. type RunnerList struct { Items []Runner `json:"items"` } // Delta appends streamed text to the draft of a running turn. type Delta struct { TurnID string `json:"turn_id"` Offset int64 `json:"offset"` Text string `json:"text"` } // Glance is a glance event's data: the line to show for the turn until // its reply arrives. type Glance struct { TurnID string `json:"turn_id"` Text string `json:"text"` } // Activity is what the coordinator is doing before or between its text: // thinking, or using one of its tools. Label is the line to show; it never // carries tool input. Activity events have revision 0, like deltas. type Activity struct { TurnID string `json:"turn_id"` Attempt int64 `json:"attempt"` Kind string `json:"kind"` Tool string `json:"tool"` // Repo is the repository named by the activity, when known (a name // from the person's catalog, never a path or query). Repo string `json:"repo"` Label string `json:"label"` } // Project is a repository the team works on. Repo is the forge key, for // example amadan.net/oficina/sample; Name drops the host. type Project struct { Starred bool `json:"starred,omitempty"` Names []string `json:"names,omitempty"` PersonalNames []string `json:"personal_names,omitempty"` TitleSource string `json:"title_source,omitempty"` DescriptionSource string `json:"description_source,omitempty"` Forge string `json:"forge,omitempty"` ConnectionID string `json:"connection_id,omitempty"` Autonomy ProjectAutonomy `json:"autonomy,omitzero"` CopyOwner string `json:"copy_owner,omitempty"` CopyRevision int64 `json:"copy_revision,omitempty"` ID string `json:"id"` Revision int64 `json:"revision"` Repo string `json:"repo"` Name string `json:"name"` // Title is the product's name (Woodstar); Attachments are what the // project is made of, its repositories first. Title string `json:"title"` Attachments []Attachment `json:"attachments"` State string `json:"state"` AddedBy string `json:"added_by"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` // Description is one line about the product (its README's first // paragraph until the coordinator writes one); Stack the languages // and platforms found in its repositories (go, swift, swiftui...); // Checks whether its checks run on the team's runner. People are the // person who added it and everyone who filed a task in it. Description string `json:"description"` Stack []string `json:"stack"` Checks *ProjectChecks `json:"checks"` People []PersonRef `json:"people"` } // ProjectList is GET /api/v1/projects. Allowed is the person's // runner-advertised allowlist: the repositories add may take. type ProjectList struct { Cursor int64 `json:"cursor"` Connections []ProjectConnection `json:"connections,omitempty"` CanAdd []ReachableProject `json:"can_add,omitempty"` Items []Project `json:"items"` Allowed []string `json:"allowed"` // Home is the team's namespace for new projects; empty means the // coordinator uses the best-fitting existing repository (never set // falls back to the runner's own home; an explicit clear does not). Home string `json:"home"` } // AddProject is the body of POST /api/v1/projects. type AddProject struct { Repo string `json:"repo"` } // Card is something in the conversation the person can act on: a project // the coordinator added (Remove) or a question (answer). type Card struct { PlacementByName bool `json:"placement_by_name,omitempty"` // PlacementQuestion marks every placement question, with options or // by name: it is answered in the app, never from a notification. PlacementQuestion bool `json:"placement_question,omitempty"` RememberName string `json:"remember_name,omitempty"` Access *AccessRequest `json:"access,omitempty"` ID string `json:"id"` Revision int64 `json:"revision"` Kind string `json:"kind"` State string `json:"state"` TurnID string `json:"turn_id"` ProjectID string `json:"project_id"` Name string `json:"name"` Question string `json:"question"` Options []string `json:"options"` Choice string `json:"choice"` // TaskID names the task a task card reports. TaskID string `json:"task_id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` // ActionID is the housekeeping action a receipt reports (the combined // card); VisibleAt is when it shows in the conversation, after quiet // hours. Both are absent on every other card. ActionID string `json:"action_id,omitempty"` VisibleAt *time.Time `json:"visible_at,omitempty"` // Specialist is a specialist card's proposal; absent on every other // card. Specialist *CardSpecialistView `json:"specialist,omitempty"` // Research is a sources card's step and URLs; absent on every other // card. Research *CardResearchView `json:"research,omitempty"` // Inventory is a research_inventory card's primary and the addresses // it lists; absent on every other card. Inventory *CardInventoryView `json:"inventory,omitempty"` // PreviewURL is a merge card's candidate preview, once it is up; // absent otherwise. PreviewURL string `json:"preview_url,omitempty"` // Site is a publish card's site; absent on every other card. Site *CardSite `json:"site,omitempty"` // Place is a place card's proposal; absent on every other card. Place *CardPlaceView `json:"place,omitempty"` // Advice is an advice card's advice; absent on every other card. Advice *Advice `json:"advice,omitempty"` // Aside: the person set the card aside (POST /cards/{id}/visibility). // It still waits on them and stays in Needs you; absent when not. Aside bool `json:"aside,omitempty"` } // AnswerCard is the body of POST /api/v1/cards/{id}/answer. Revision must // be the card's current revision. type AnswerCard struct { IdempotencyKey string `json:"idempotency_key,omitempty"` RememberName *string `json:"remember_name,omitempty"` Choice string `json:"choice"` Revision int64 `json:"revision"` // Note is the person's words with an answer that takes them (a // direction card's Try again, and its Build this naming the option // they want), at most MaxDirectionNote bytes. Note string `json:"note,omitempty"` } // Answered is the response: the answered card, and the answer as the // person's message with the turn that carries it to the coordinator. type Answered struct { Card Card `json:"card"` Message Message `json:"message"` Turn Turn `json:"turn"` } // CardConflict is the 409 body for a stale or repeated answer. type CardConflict struct { Code string `json:"code"` CopyKey string `json:"copy_key"` RequestID string `json:"request_id"` Card Card `json:"card"` } // Event is the data of one Server-Sent Event. Cursor is also the SSE id. // Clients apply an event only if Revision is newer than what they hold; // deltas apply only when Offset equals the draft length. type Event struct { Cursor int64 `json:"cursor"` Type string `json:"type"` Resource string `json:"resource"` ID string `json:"id"` Revision int64 `json:"revision"` TurnID string `json:"turn_id,omitempty"` MessageID string `json:"message_id,omitempty"` Attempt int64 `json:"attempt,omitempty"` Data json.RawMessage `json:"data"` } // Snapshot is everything a client shows, read in one transaction. type Snapshot struct { Me Me `json:"me"` Messages []Message `json:"messages"` Turns []Turn `json:"turns"` Runner Runner `json:"runner"` Projects []Project `json:"projects"` Cards []Card `json:"cards"` // Tasks are the team's tasks the cards and the Tasks page show. Tasks []Task `json:"tasks"` // Coordinator says whether the coordinator has moved in. Coordinator Coordinator `json:"coordinator"` // Connections are the person's own, every runner. Connections []Connection `json:"connections"` // HousekeepingSummary is the person's tidying loop at a glance. HousekeepingSummary HousekeepingSummary `json:"housekeeping_summary"` Cursor int64 `json:"cursor"` // Runners are all the person's runners (Runner stays, the primary, // for one release). Runners []Runner `json:"runners"` } // SendMessage is the body of POST /api/v1/messages. type SendMessage struct { ProjectID string `json:"project_id,omitempty"` Text string `json:"text"` IdempotencyKey string `json:"idempotency_key"` // Attachments are ids from POST /api/v1/attachments, the sender's own // and on no other message, at most five. Text may then be empty. Attachments []string `json:"attachments,omitempty"` } // Sent is the response to POST /api/v1/messages. type Sent struct { Message Message `json:"message"` Turn Turn `json:"turn"` } // MessagePage is GET /api/v1/messages, oldest first. Before is the cursor // for the previous page, empty at the start of the conversation. type MessagePage struct { Items []Message `json:"items"` Before string `json:"before"` } // CancelAccepted is the 202 body of POST /api/v1/turns/{id}/cancel. type CancelAccepted struct { Attempt int64 `json:"attempt"` } // Error is every non-2xx JSON body. It matches personapi.Error. type Error struct { Code string `json:"code"` CopyKey string `json:"copy_key"` RequestID string `json:"request_id"` CurrentRevision *int64 `json:"current_revision,omitempty"` } // Limits shared by both ends. const ( MaxMessageBytes = 16 << 10 MaxFrameBytes = 256 << 10 MaxTailMessages = 50 PingInterval = 20 * time.Second EventRetention = 24 * time.Hour ) var ErrStrict = errors.New("wire: invalid document") // Strict decodes exactly one JSON value with no unknown fields. func Strict(raw []byte, v any) error { d := json.NewDecoder(bytes.NewReader(raw)) d.DisallowUnknownFields() if err := d.Decode(v); err != nil { return err } if d.Decode(new(json.RawMessage)) != io.EOF { return ErrStrict } return nil }