| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "strconv" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Jobs when building is off (spec 2026-11-26-assistant-abilities §6.3): |
| 14 | // a job whose person can't have building done is held at its next step. |
| 15 | // The hold is stored on the hand-off (ability_held_at), so neither the |
| 16 | // switch coming back nor a runner reconnecting resumes it: only the |
| 17 | // person's own Carry on now does (resumeAbilityHold). |
| 18 | |
| 19 | // buildOffCoordinatorText is the Coordinator's refusal for a tool that |
| 20 | // would start or change work in a held job (model-facing, reviewed as |
| 21 | // code). |
| 22 | const buildOffCoordinatorText = "Refused: the person this work is for can't have new work done right now. Stop here; don't start or change anything." |
| 23 | |
| 24 | // buildOffTools start or change work in a job's thread: refused while the |
| 25 | // job is held. Reads, status, notes and cancelling still run, and so does |
| 26 | // project_created, which records a repository an earlier create_project |
| 27 | // already authorised (projectCreated checks that binding). |
| 28 | var buildOffTools = append(slices.Clone(workTools), wire.ToolCreateProject, wire.ToolAddProject, wire.ToolPublishSite) |
| 29 | |
| 30 | // buildAllowed reports whether h's person may have building done now. |
| 31 | func (t *txn) buildAllowed(h handoffRecord) (bool, error) { |
| 32 | sw, err := t.switches(h.Person) |
| 33 | if err != nil { |
| 34 | return false, err |
| 35 | } |
| 36 | return sw.Can(wire.AbilityBuild), nil |
| 37 | } |
| 38 | |
| 39 | // abilityHeld reports whether h's work is held, holding it first when |
| 40 | // its person can't have building done: what every shared dispatch path |
| 41 | // and a step's start check before letting any of its work go. |
| 42 | func (t *txn) abilityHeld(h handoffRecord) (bool, error) { |
| 43 | if h.AbilityHeldAt != nil { |
| 44 | return true, nil |
| 45 | } |
| 46 | ok, err := t.buildAllowed(h) |
| 47 | if err != nil || ok { |
| 48 | return false, err |
| 49 | } |
| 50 | return true, t.holdForAbility(h) |
| 51 | } |
| 52 | |
| 53 | // holdForAbility records the hold on h, once, and redraws its job card. |
| 54 | func (t *txn) holdForAbility(h handoffRecord) error { |
| 55 | if h.AbilityHeldAt != nil { |
| 56 | return nil |
| 57 | } |
| 58 | now := t.now() |
| 59 | res := t.db.Model(&handoffRecord{}).Where("id = ? AND ability_held_at IS NULL", h.ID).Update("ability_held_at", now) |
| 60 | if res.Error != nil || res.RowsAffected == 0 { |
| 61 | return res.Error |
| 62 | } |
| 63 | h.AbilityHeldAt = &now |
| 64 | return t.projectJob(h) |
| 65 | } |
| 66 | |
| 67 | // laneHeld reports whether lane is a held job's thread: its queued turns |
| 68 | // wait. |
| 69 | func (t *txn) laneHeld(lane string) (bool, error) { |
| 70 | id, ok := wire.JobOf(lane) |
| 71 | if !ok { |
| 72 | return false, nil |
| 73 | } |
| 74 | var h handoffRecord |
| 75 | if err := t.db.Where("id = ?", id).Limit(1).Find(&h).Error; err != nil || h.ID == "" { |
| 76 | return false, err |
| 77 | } |
| 78 | return t.abilityHeld(h) |
| 79 | } |
| 80 | |
| 81 | // stepHeld reports whether st's start is held: its task is followed by |
| 82 | // a held job. A Publish is going live, which go_live and its own last |
| 83 | // checks govern, not building; every other step (a Preview rebuilt on a |
| 84 | // finished task included) is building. |
| 85 | func (t *txn) stepHeld(st stepRecord) (bool, error) { |
| 86 | if st.TaskID == "" || st.Specialist == wire.SpecialistPublish { |
| 87 | return false, nil |
| 88 | } |
| 89 | h, ok, err := t.handoffOf("", st.TaskID) |
| 90 | if err != nil || !ok { |
| 91 | return false, err |
| 92 | } |
| 93 | return t.abilityHeld(h) |
| 94 | } |
| 95 | |
| 96 | // requireBuild refuses a team operation that starts or moves work on for |
| 97 | // a person who can't have building done: ability_off, with the instance's |
| 98 | // text naming it and who to ask. |
| 99 | func (t *txn) requireBuild(person string) error { |
| 100 | sw, err := t.switches(person) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | if sw.Can(wire.AbilityBuild) { |
| 105 | return nil |
| 106 | } |
| 107 | res := t.s.abilityRefusal(sw, wire.AbilityBuild) |
| 108 | return assist.Refusal{Code: res.Code, Message: res.Text} |
| 109 | } |
| 110 | |
| 111 | // heldCarryOn marks a held job's carry_on: Carry on now resumes the hold |
| 112 | // (resumeAbilityHold) rather than releasing a pause on another plan, so |
| 113 | // the card carries no carry_on_connection. |
| 114 | const heldCarryOn = "\x00held" |
| 115 | |
| 116 | // heldView is a held job's card: stopped, saying why and who can turn |
| 117 | // building back on, or, once it is on again, that it is and that Carry |
| 118 | // on now resumes it. |
| 119 | func (t *txn) heldView(h handoffRecord) (result, detail, carryOn string, err error) { |
| 120 | sw, err := t.switches(h.Person) |
| 121 | if err != nil { |
| 122 | return "", "", "", err |
| 123 | } |
| 124 | result = t.copy("assistant.job." + wire.JobStopped) |
| 125 | if sw.Can(wire.AbilityBuild) { |
| 126 | return result, t.copy("assistant.job.detail.ability_back"), heldCarryOn, nil |
| 127 | } |
| 128 | if sw.Ask() == "" { |
| 129 | return result, t.copy("assistant.job.detail." + wire.JobStopped), "", nil |
| 130 | } |
| 131 | return result, fmt.Sprintf(t.copy("assistant.job.detail.ability_off"), sw.Ask()), "", nil |
| 132 | } |
| 133 | |
| 134 | // resumeAbilityHold is Carry on now on a held job, once building is on |
| 135 | // again for its person (§6.3): the hold is cleared, a step attempt that |
| 136 | // ended without its start being granted (the hold refused it) is tried |
| 137 | // afresh, the thread's waiting turns are dispatched in order, a job that |
| 138 | // reached no work is given its request again (continueHeld), and the |
| 139 | // card is redrawn. Steps never dispatched simply become eligible again. |
| 140 | func (t *txn) resumeAbilityHold(h handoffRecord) error { |
| 141 | ok, err := t.buildAllowed(h) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | if !ok { |
| 146 | return ErrAbilityOff |
| 147 | } |
| 148 | if err := t.db.Model(&handoffRecord{}).Where("id = ?", h.ID).Update("ability_held_at", nil).Error; err != nil { |
| 149 | return err |
| 150 | } |
| 151 | held := t.now() |
| 152 | if h.AbilityHeldAt != nil { |
| 153 | held = *h.AbilityHeldAt |
| 154 | } |
| 155 | h.AbilityHeldAt = nil |
| 156 | tasks, err := t.handoffTasks(h) |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | for _, k := range tasks { |
| 161 | var refused []stepRecord |
| 162 | if err := t.db.Where("task_id = ? AND ability_held = ?", k.ID, true).Find(&refused).Error; err != nil { |
| 163 | return err |
| 164 | } |
| 165 | for i := range refused { |
| 166 | if err := t.freshAttempt(&k, &refused[i]); err != nil { |
| 167 | return err |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if h.Lane != "" { |
| 172 | if err := t.dispatchNext(h.Person, h.Lane); err != nil { |
| 173 | return err |
| 174 | } |
| 175 | } |
| 176 | if len(tasks) == 0 { |
| 177 | if err := t.continueHeld(h, held); err != nil { |
| 178 | return err |
| 179 | } |
| 180 | } |
| 181 | return t.projectJob(h) |
| 182 | } |
| 183 | |
| 184 | // actionResume is the request a held job that never reached any work |
| 185 | // was given again when it resumed (continueHeld). |
| 186 | const actionResume = "resume" |
| 187 | |
| 188 | // continueHeld starts again a held job that reached no work: its turns |
| 189 | // all ended with nothing recorded and no question open (the hold refused |
| 190 | // what it tried), so nothing else would carry it on. The person's request |
| 191 | // goes to its thread once more, with the hand-off's context, once for |
| 192 | // the hold that was set at heldAt. |
| 193 | func (t *txn) continueHeld(h handoffRecord, heldAt time.Time) error { |
| 194 | stopped, err := t.handoffStopped(h) |
| 195 | if err != nil || !stopped { |
| 196 | return err |
| 197 | } |
| 198 | var first messageRecord |
| 199 | if err := t.db.Where("id = ? AND person = ?", h.MessageID, h.Person).Limit(1).Find(&first).Error; err != nil || first.ID == "" { |
| 200 | return err |
| 201 | } |
| 202 | key := handoffKey + h.ID + ":resume:" + strconv.FormatInt(heldAt.UnixNano(), 10) |
| 203 | _, _, err = t.send(h.Lane, h.Person, key, first.Text, nil, func(m messageRecord) error { |
| 204 | if err := t.db.Create(&handoffActionRecord{LedgerKey: key, HandoffID: h.ID, Person: h.Person, Kind: actionResume, Context: h.Context, MessageID: m.ID, Result: "resumed", CreatedAt: t.now()}).Error; err != nil { |
| 205 | return err |
| 206 | } |
| 207 | return t.db.Create(&handoffTurnRecord{HandoffID: h.ID, TurnID: m.TurnID}).Error |
| 208 | }) |
| 209 | return err |
| 210 | } |
| 211 | |
| 212 | // reprojectHeld redraws the person's open jobs' cards: building or |
| 213 | // putting live turned on or off changes what they say. |
| 214 | func (t *txn) reprojectHeld(person string) error { |
| 215 | var held []handoffRecord |
| 216 | if err := t.db.Where("person = ? AND state = ?", person, handoffOpen).Find(&held).Error; err != nil { |
| 217 | return err |
| 218 | } |
| 219 | for _, h := range held { |
| 220 | if err := t.projectJob(h); err != nil { |
| 221 | return err |
| 222 | } |
| 223 | } |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | // goLiveForTask reports whether the person a task's hand-off is for may |
| 228 | // put things live: true for a task no hand-off follows (work started in |
| 229 | // Equipo's own pages is outside the board). Merge authority's automatic |
| 230 | // merge and the last merge and publish checks read it. |
| 231 | func (t *txn) goLiveForTask(taskID string) (bool, error) { |
| 232 | h, ok, err := t.handoffOf("", taskID) |
| 233 | if err != nil || !ok { |
| 234 | return err == nil, err |
| 235 | } |
| 236 | sw, err := t.switches(h.Person) |
| 237 | if err != nil { |
| 238 | return false, err |
| 239 | } |
| 240 | return sw.Can(wire.AbilityGoLive), nil |
| 241 | } |
| 242 | |
| 243 | // goLiveByActor reports whether actor, answering for a task, may put it |
| 244 | // live: true for a task no hand-off follows. The taps read it. |
| 245 | func (t *txn) goLiveByActor(actor, taskID string) (bool, error) { |
| 246 | _, ok, err := t.handoffOf("", taskID) |
| 247 | if err != nil || !ok { |
| 248 | return err == nil, err |
| 249 | } |
| 250 | sw, err := t.switches(actor) |
| 251 | if err != nil { |
| 252 | return false, err |
| 253 | } |
| 254 | return sw.Can(wire.AbilityGoLive), nil |
| 255 | } |
| 256 | |
| 257 | // requireGoLive is goLiveByActor as an error: ErrAbilityOff when off. |
| 258 | func (t *txn) requireGoLive(actor, taskID string) error { |
| 259 | ok, err := t.goLiveByActor(actor, taskID) |
| 260 | if err == nil && !ok { |
| 261 | return ErrAbilityOff |
| 262 | } |
| 263 | return err |
| 264 | } |
| 265 | |
| 266 | // goLiveOptions takes the go-ahead and Publish options off a card in the |
| 267 | // Assistant lane while its person can't put things live (§6.4): read as |
| 268 | // the card is shown, so it follows the board. Wait and Not now stay. |
| 269 | func (t *txn) goLiveOptions(v *wire.AssistantCard, person string) error { |
| 270 | live := slices.ContainsFunc(v.Options, func(o wire.CardOption) bool { |
| 271 | return o.Value == wire.ChoiceMerge || o.Value == wire.ChoicePublish |
| 272 | }) |
| 273 | if !live { |
| 274 | return nil |
| 275 | } |
| 276 | sw, err := t.switches(person) |
| 277 | if err != nil || sw.Can(wire.AbilityGoLive) { |
| 278 | return err |
| 279 | } |
| 280 | v.Options = slices.DeleteFunc(v.Options, func(o wire.CardOption) bool { |
| 281 | return o.Value == wire.ChoiceMerge || o.Value == wire.ChoicePublish |
| 282 | }) |
| 283 | return nil |
| 284 | } |
| 285 | |
| 286 | // turnHeld reports whether turn belongs to a held job: a job thread's |
| 287 | // turn, or a legacy hand-off's turn in the shared Coordinator lane. |
| 288 | func (t *txn) turnHeld(turn, lane string) (bool, error) { |
| 289 | if held, err := t.laneHeld(lane); err != nil || held { |
| 290 | return held, err |
| 291 | } |
| 292 | if turn == "" { |
| 293 | return false, nil |
| 294 | } |
| 295 | h, ok, err := t.handoffOf(turn, "") |
| 296 | if err != nil || !ok { |
| 297 | return false, err |
| 298 | } |
| 299 | return t.abilityHeld(h) |
| 300 | } |
| 301 | |
| 302 | // freshAttempt runs again an attempt the build hold refused at its |
| 303 | // start: it did no work, so its next attempt starts from where it was, |
| 304 | // through the step's own dispatch. |
| 305 | func (t *txn) freshAttempt(k *taskRecord, st *stepRecord) error { |
| 306 | if !wire.StepTerminal(st.State) || st.StartedAt != nil { |
| 307 | return nil |
| 308 | } |
| 309 | st.Attempt++ |
| 310 | st.State, st.Reason, st.Terminated, st.FinishedAt, st.AbilityHeld = wire.StepQueued, "", false, nil, false |
| 311 | st.Withdrawn, st.RefusedBy = false, "" |
| 312 | if err := t.dispatchStep(k, st); err != nil { |
| 313 | return err |
| 314 | } |
| 315 | return t.saveStep(st, k) |
| 316 | } |
| 317 | |