| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // Asking the implementer (Paul, 2026-11-07): from a task's page its |
| 9 | // creator asks the specialist that did the work a question, and the |
| 10 | // answer streams into the task's thread. The runner resumes that step's |
| 11 | // own session, read-only, in a fresh tree of the commit the step made or |
| 12 | // judged. The coordinator never relays technical detail; it points here. |
| 13 | |
| 14 | // Question states. A question ends in exactly one of answered, failed or |
| 15 | // cancelled. One question of a task runs at a time; later ones queue. |
| 16 | const ( |
| 17 | QuestionQueued = "queued" |
| 18 | QuestionRunning = "running" |
| 19 | QuestionAnswered = "answered" |
| 20 | QuestionFailed = "failed" |
| 21 | QuestionCancelled = "cancelled" |
| 22 | ) |
| 23 | |
| 24 | // QuestionTerminal reports whether a question has ended. |
| 25 | func QuestionTerminal(state string) bool { |
| 26 | return state == QuestionAnswered || state == QuestionFailed || state == QuestionCancelled |
| 27 | } |
| 28 | |
| 29 | // EventQuestion carries a Question to every active team member. While it |
| 30 | // runs, its answer streams as delta and activity events whose resource is |
| 31 | // "question" and whose turn_id is the question's id; the answered |
| 32 | // question replaces the draft, as a committed message does in the chat. |
| 33 | const EventQuestion = "question" |
| 34 | |
| 35 | // ResourceQuestion is the event resource for a question and its stream. |
| 36 | const ResourceQuestion = "question" |
| 37 | |
| 38 | // Bounds for questions. |
| 39 | const ( |
| 40 | MaxQuestionText = 4000 |
| 41 | MaxAnswerText = 16 << 10 |
| 42 | MaxQuestions = 50 |
| 43 | ) |
| 44 | |
| 45 | // Question is one question a task's creator asked a specialist, and its |
| 46 | // answer. StepID is the step whose session answers: by default the |
| 47 | // newest Engineering step, or the newest Review step when the person |
| 48 | // addresses Review. Answer is "" until it is answered. |
| 49 | type Question struct { |
| 50 | ID string `json:"id"` |
| 51 | Revision int64 `json:"revision"` |
| 52 | TaskID string `json:"task_id"` |
| 53 | StepID string `json:"step_id"` |
| 54 | Specialist string `json:"specialist"` |
| 55 | Asker string `json:"asker"` |
| 56 | Text string `json:"text"` |
| 57 | State string `json:"state"` |
| 58 | Reason string `json:"reason"` |
| 59 | Attempt int64 `json:"attempt"` |
| 60 | Answer string `json:"answer"` |
| 61 | CreatedAt time.Time `json:"created_at"` |
| 62 | UpdatedAt time.Time `json:"updated_at"` |
| 63 | Timing *TurnTiming `json:"timing"` |
| 64 | } |
| 65 | |
| 66 | // AskQuestion is the body of POST /api/v1/tasks/{id}/questions. |
| 67 | // Specialist is "engineering", "review" or "" (Review when the text |
| 68 | // addresses it, else Engineering). The same key with another body is 409. |
| 69 | // The response is 202 with the Question; POST /api/v1/questions/{id}/ |
| 70 | // cancel answers 202 with TaskAccepted, or 409 once it has ended. |
| 71 | type AskQuestion struct { |
| 72 | Text string `json:"text"` |
| 73 | IdempotencyKey string `json:"idempotency_key"` |
| 74 | Specialist string `json:"specialist"` |
| 75 | } |
| 76 | |
| 77 | var reviewWord = regexp.MustCompile(`(?i)\breview(?:er|ers|ed|ing|s)?\b`) |
| 78 | |
| 79 | // AddressesReview reports whether a question with no chosen specialist |
| 80 | // goes to Review: the person addresses Review or asks about the review. |
| 81 | func AddressesReview(text string) bool { return reviewWord.MatchString(text) } |
| 82 | |
| 83 | // Frames for questions. ask and cancel_ask go to the runner; answer |
| 84 | // comes back; forget_task tells the runner a task ended, so it can |
| 85 | // delete the sessions it kept for the task's steps. |
| 86 | const ( |
| 87 | FrameAsk = "ask" |
| 88 | FrameCancelAsk = "cancel_ask" |
| 89 | FrameAnswer = "answer" |
| 90 | FrameForgetTask = "forget_task" |
| 91 | ) |
| 92 | |
| 93 | // AskCommand asks the step's own session one question. CandidateSHA is |
| 94 | // the commit the step made (Engineering) or judged (Review); BaseSHA the |
| 95 | // task's base, for the change. The runner keeps the session id and its |
| 96 | // account; neither leaves the runner. |
| 97 | type AskCommand struct { |
| 98 | Person string `json:"person"` |
| 99 | TaskID string `json:"task_id"` |
| 100 | StepID string `json:"step_id"` |
| 101 | QuestionID string `json:"question_id"` |
| 102 | Attempt int64 `json:"attempt"` |
| 103 | Specialist string `json:"specialist"` |
| 104 | Repo string `json:"repo"` |
| 105 | Branch string `json:"branch"` |
| 106 | BaseSHA string `json:"base_sha"` |
| 107 | CandidateSHA string `json:"candidate_sha"` |
| 108 | Title string `json:"title"` |
| 109 | Text string `json:"text"` |
| 110 | // Protocol 37: the Engineering attempt, account and kept session the |
| 111 | // question resumes, frozen when it was created (exact pinning). |
| 112 | EngAttempt int64 `json:"eng_attempt,omitempty"` |
| 113 | EngAccount string `json:"eng_account,omitempty"` |
| 114 | EngSession string `json:"eng_session,omitempty"` |
| 115 | } |
| 116 | |
| 117 | // CancelAskCommand stops one attempt of a question. |
| 118 | type CancelAskCommand struct { |
| 119 | Person string `json:"person"` |
| 120 | TaskID string `json:"task_id"` |
| 121 | QuestionID string `json:"question_id"` |
| 122 | Attempt int64 `json:"attempt"` |
| 123 | } |
| 124 | |
| 125 | // AnswerReport moves a question forward: running when its session |
| 126 | // starts, then answered with the text, or failed or cancelled with a |
| 127 | // reason from AnswerReasons. |
| 128 | type AnswerReport struct { |
| 129 | TaskID string `json:"task_id"` |
| 130 | QuestionID string `json:"question_id"` |
| 131 | Attempt int64 `json:"attempt"` |
| 132 | State string `json:"state"` |
| 133 | Reason string `json:"reason"` |
| 134 | Text string `json:"text"` |
| 135 | Timing *TurnTiming `json:"timing"` |
| 136 | Usage []Usage `json:"usage"` |
| 137 | } |
| 138 | |
| 139 | // ForgetTaskCommand: the task is done or cancelled. |
| 140 | type ForgetTaskCommand struct { |
| 141 | Person string `json:"person"` |
| 142 | TaskID string `json:"task_id"` |
| 143 | // KeepUntil, when set, keeps the task's sessions until then (a merged |
| 144 | // task can still be asked about for KeepMergedSessions); null forgets |
| 145 | // them now (a cancelled task). |
| 146 | KeepUntil *time.Time `json:"keep_until"` |
| 147 | // KeepPreview keeps the task's preview running after the task is |
| 148 | // forgotten (protocol 19; Encode drops it below). |
| 149 | KeepPreview bool `json:"keep_preview,omitempty"` |
| 150 | } |
| 151 | |
| 152 | // KeepMergedSessions is how long a done task's specialists can still be |
| 153 | // asked about their work (Paul, 2026-11-08). |
| 154 | const KeepMergedSessions = 14 * 24 * time.Hour |
| 155 | |
| 156 | // AskRun is a question attempt the runner holds, in sync. |
| 157 | type AskRun struct { |
| 158 | QuestionID string `json:"question_id"` |
| 159 | Attempt int64 `json:"attempt"` |
| 160 | State string `json:"state"` |
| 161 | } |
| 162 | |
| 163 | // AnswerReasons are the codes a runner may report for a question that |
| 164 | // was not answered. session_gone: the step's session is no longer kept |
| 165 | // (it ran before sessions were kept, or its retention ended). |
| 166 | var AnswerReasons = map[string]bool{ |
| 167 | "": true, |
| 168 | "cancelled": true, |
| 169 | "interrupted": true, |
| 170 | "runner_restarted": true, |
| 171 | "turn_timeout": true, |
| 172 | "provider_failed": true, |
| 173 | "provider_rejected": true, |
| 174 | "policy_rejected": true, |
| 175 | "account_unavailable": true, |
| 176 | "sign_in_again": true, |
| 177 | "clone_failed": true, |
| 178 | "candidate_unavailable": true, |
| 179 | "session_gone": true, |
| 180 | "superseded": true, |
| 181 | "task_ended": true, |
| 182 | // limited: the step's own connection is limited by its provider now. |
| 183 | "limited": true, |
| 184 | // A question held by its metered connection's guard (queued, with |
| 185 | // the hold as its reason; ProtocolMetered). |
| 186 | HoldBusy: true, |
| 187 | HoldBudget: true, |
| 188 | HoldSpendUnknown: true, |
| 189 | // The question's session model was removed from its connection's |
| 190 | // allowed set or is unhealthy (ProtocolModels, §16.3): it fails, never |
| 191 | // moving to another model. |
| 192 | ReasonModelUnavailable: true, |
| 193 | } |
| 194 | |