| 1 | package wire |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // The scheduler (connectors spec 2026-11-12 rev 3.1, §6 to §8, slice S3): |
| 6 | // a task's priority and deadline, a held step's hold, Run now's release, |
| 7 | // a priority change for a step already sent, and a preference that ends |
| 8 | // by itself. ProtocolScheduler carries them; Encode refuses or drops all |
| 9 | // of them below it (downgradeScheduler). |
| 10 | |
| 11 | // Task priorities (§6). The coordinator may set low or normal, and by, |
| 12 | // only when it creates a task; every later change and every urgent is |
| 13 | // the task creator's own tap. |
| 14 | const ( |
| 15 | PriorityUrgent = "urgent" |
| 16 | PriorityNormal = "normal" |
| 17 | PriorityLow = "low" |
| 18 | ) |
| 19 | |
| 20 | // ValidPriority reports whether p is a priority ("" is not: it is read as |
| 21 | // normal where a priority is optional). |
| 22 | func ValidPriority(p string) bool { |
| 23 | return p == PriorityUrgent || p == PriorityNormal || p == PriorityLow |
| 24 | } |
| 25 | |
| 26 | // CoordinatorPriority reports whether the coordinator may set p when it |
| 27 | // creates a task: low or normal, never urgent. |
| 28 | func CoordinatorPriority(p string) bool { return p == PriorityLow || p == PriorityNormal } |
| 29 | |
| 30 | // Who set a task's priority and deadline: the coordinator from the |
| 31 | // person's message (shown "set from your message"), or the person. |
| 32 | const ( |
| 33 | PrioritySetByCoordinator = "coordinator" |
| 34 | PrioritySetByPerson = "person" |
| 35 | ) |
| 36 | |
| 37 | // Hold reasons a held step reports (§7 rule 5): waiting for headroom on |
| 38 | // a subscription, a metered connection at its budget, its spend unknown, |
| 39 | // its one slot taken (a kept session held on it), or nothing that can |
| 40 | // run the step. |
| 41 | // (HoldBusy, HoldBudget and HoldSpendUnknown are the metered guard's, |
| 42 | // ProtocolMetered.) |
| 43 | const ( |
| 44 | HoldHeadroom = "headroom" |
| 45 | HoldNoConnection = ReasonNoConnection |
| 46 | ) |
| 47 | |
| 48 | // HoldReasons are the values StepHold.Reason may take. |
| 49 | var HoldReasons = map[string]bool{HoldHeadroom: true, HoldBudget: true, HoldSpendUnknown: true, HoldBusy: true, HoldNoConnection: true} |
| 50 | |
| 51 | // What a held step needs from its person (§8, Needs you): the deadline |
| 52 | // check fired, no reset in sight, or nothing can run it. |
| 53 | const ( |
| 54 | HoldNeedsDeadline = "deadline" |
| 55 | HoldNeedsNoReset = "no_reset" |
| 56 | HoldNeedsNoConnection = "no_connection" |
| 57 | ) |
| 58 | |
| 59 | // HoldNeeds are the values StepHold.NeedsPerson may take. |
| 60 | var HoldNeeds = map[string]bool{"": true, HoldNeedsDeadline: true, HoldNeedsNoReset: true, HoldNeedsNoConnection: true} |
| 61 | |
| 62 | // NeedsHeld is Task.NeedsYou for a step the scheduler holds where the |
| 63 | // person's input changes the outcome. |
| 64 | const NeedsHeld = "held" |
| 65 | |
| 66 | // CardHeld is the Needs you card for a held step: Equipo's |
| 67 | // recommendation, with Wait and Run it now. |
| 68 | const CardHeld = "held" |
| 69 | |
| 70 | // Routing reasons the scheduler adds (§10): a kept session, metered work |
| 71 | // for an urgent task or a release, and a hold. |
| 72 | const ( |
| 73 | RoutingSticky = "sticky" |
| 74 | RoutingMeteredUrgent = "metered_urgent" |
| 75 | RoutingMeteredRelease = "metered_release" |
| 76 | RoutingHeld = "held" |
| 77 | ) |
| 78 | |
| 79 | // StepHold is why the runner holds a step (§8): reason, the connection |
| 80 | // it names (the runner's local id and its provider, "" for none), that |
| 81 | // connection's reported reset, when the runner looks again on its own |
| 82 | // (null: only a fact change wakes it), what it needs from the person, |
| 83 | // and the step revision a Run now must name. A step report carries it |
| 84 | // while the step is held, with state running (queued on the instance |
| 85 | // until it starts); a report without it ends the hold. |
| 86 | type StepHold struct { |
| 87 | Reason string `json:"reason"` |
| 88 | Connection string `json:"connection,omitempty"` |
| 89 | Provider string `json:"provider,omitempty"` |
| 90 | ResetsAt *time.Time `json:"resets_at,omitempty"` |
| 91 | WakeAt *time.Time `json:"wake_at"` |
| 92 | NeedsPerson string `json:"needs_person,omitempty"` |
| 93 | StepRevision int64 `json:"step_revision"` |
| 94 | } |
| 95 | |
| 96 | // FrameRelease is Run now (instance to runner, durable, consumed once): |
| 97 | // leave for one step revision to start now, metered connections |
| 98 | // included, the bars still applying. The runner consumes it atomically |
| 99 | // with the admission it causes; it becomes a grant for (step, |
| 100 | // step_revision) that covers that revision's failover attempts and ends |
| 101 | // with the step. A stale or consumed release is acked and ignored. |
| 102 | const FrameRelease = "release" |
| 103 | |
| 104 | // ReleaseCommand is a release. StepRevision is the dispatch it is for: |
| 105 | // the attempt the instance sent (failover attempts the runner makes keep |
| 106 | // it). |
| 107 | // |
| 108 | // Connection (protocol 29) pins it: the runner's local id of the one |
| 109 | // connection the person was told Run now would use; the runner admits the |
| 110 | // step only there, or keeps holding it. "" is leave on any connection. |
| 111 | type ReleaseCommand struct { |
| 112 | ID string `json:"id"` |
| 113 | Person string `json:"person"` |
| 114 | TaskID string `json:"task_id"` |
| 115 | StepID string `json:"step_id"` |
| 116 | StepRevision int64 `json:"step_revision"` |
| 117 | Connection string `json:"connection,omitempty"` |
| 118 | } |
| 119 | |
| 120 | // FramePriority tells the runner a task's priority or deadline changed |
| 121 | // (instance to runner, durable): the steps of the task it holds take it. |
| 122 | // A revision at or below the one held changes nothing. |
| 123 | const FramePriority = "priority" |
| 124 | |
| 125 | // PriorityCommand is a priority change for the runner. Revision is the |
| 126 | // task's priority revision (DispatchCommand.PriorityRevision orders the |
| 127 | // same values). |
| 128 | type PriorityCommand struct { |
| 129 | Person string `json:"person"` |
| 130 | TaskID string `json:"task_id"` |
| 131 | Priority string `json:"priority"` |
| 132 | By *time.Time `json:"by"` |
| 133 | Revision int64 `json:"revision"` |
| 134 | } |
| 135 | |
| 136 | // SetPriority is PUT /api/v1/tasks/{id}/priority: the task creator's |
| 137 | // priority and deadline (by null clears it), against the task's |
| 138 | // revision. |
| 139 | type SetPriority struct { |
| 140 | Priority string `json:"priority"` |
| 141 | By *time.Time `json:"by"` |
| 142 | Revision int64 `json:"revision"` |
| 143 | } |
| 144 | |
| 145 | // RunNow is POST /api/v1/tasks/{id}/run-now: the task creator's leave |
| 146 | // for the held step at the revision its hold shows. Offer is what the |
| 147 | // page showed (paused work review R1): the offer's connection |
| 148 | // (RunNowOffer.Connection), or RunNowPlain for plain Run now. When Run |
| 149 | // now would do anything else now, the tap is refused (409 |
| 150 | // offer_changed) and the page shows the offer as it is. |
| 151 | type RunNow struct { |
| 152 | Step string `json:"step"` |
| 153 | StepRevision int64 `json:"step_revision"` |
| 154 | Offer string `json:"offer,omitempty"` |
| 155 | } |
| 156 | |
| 157 | // RunNowPlain is RunNow.Offer for plain "Run it now anyway". |
| 158 | const RunNowPlain = "plain" |
| 159 | |
| 160 | // RunNowAccepted answers Run now: the release written. A second Run now |
| 161 | // for the same step revision is refused (409 already_running). |
| 162 | type RunNowAccepted struct { |
| 163 | Release string `json:"release"` |
| 164 | } |
| 165 | |
| 166 | // TaskPriority is a task's priority on its read shape: the priority, |
| 167 | // the deadline, who set them and whether the deadline check fired. |
| 168 | type TaskPriority struct { |
| 169 | Priority string `json:"priority"` |
| 170 | By *time.Time `json:"by"` |
| 171 | SetBy string `json:"set_by"` |
| 172 | } |
| 173 | |
| 174 | // MaxByAhead bounds a deadline: at most a year ahead. |
| 175 | const MaxByAhead = 366 * 24 * time.Hour |
| 176 | |
| 177 | // StepPaused is what the instance knows of a waiting step beyond the |
| 178 | // runner's hold (paused work, 2026-11-24). Since is when the hold began, |
| 179 | // or, for a step whose runner is offline (Offline), when that runner was |
| 180 | // last seen. UsedPercent is the held subscription's fullest window as |
| 181 | // last read (100 when it is limited), absent when unknown. RunNow is what |
| 182 | // Run now would do, absent when it would do nothing or the instance |
| 183 | // cannot tell. |
| 184 | type StepPaused struct { |
| 185 | Since *time.Time `json:"since,omitempty"` |
| 186 | Offline bool `json:"offline,omitempty"` |
| 187 | UsedPercent *int `json:"used_percent,omitempty"` |
| 188 | RunNow *RunNowOffer `json:"run_now,omitempty"` |
| 189 | } |
| 190 | |
| 191 | // RunNowOffer is the instance's prediction of Run now for a held step: |
| 192 | // the scheduler's own rule (schedule.Decide) with Run now's leave, over |
| 193 | // the task creator's connections on the step's runner as last read. |
| 194 | // Connection is the connection's id (cn_…), Name how Equipo names it |
| 195 | // ("Codex"), Billing subscription or metered. Same: it is the connection |
| 196 | // the hold names, whose last share it would use; LeftPercent is what is |
| 197 | // left of that one's fullest window. |
| 198 | type RunNowOffer struct { |
| 199 | Connection string `json:"connection,omitempty"` |
| 200 | Provider string `json:"provider,omitempty"` |
| 201 | Name string `json:"name,omitempty"` |
| 202 | Billing string `json:"billing,omitempty"` |
| 203 | Same bool `json:"same,omitempty"` |
| 204 | LeftPercent int `json:"left_percent,omitempty"` |
| 205 | // Plain: the runner is below protocol 29 and cannot be held to one |
| 206 | // connection, so Run now promises nothing (the other fields empty): |
| 207 | // "Run it now anyway". MayBill: and a connection that bills per use |
| 208 | // could take it. |
| 209 | Plain bool `json:"plain,omitempty"` |
| 210 | MayBill bool `json:"may_bill,omitempty"` |
| 211 | } |
| 212 | |