oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "slices"
8 "strings"
9
10 "gorm.io/gorm"
11
12 "amadan.net/oficina/equipo/internal/equipo/mergegate"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// Stuck tasks (coordinator powers, Paul 2026-11-25): what the coordinator
17// may do for a task that stopped. It never merges and never publishes: a
18// failed merge it can only put in front of the person (offer_retry), whose
19// tap merges through the person's own path.
20
21// What the model reads back; people never see these.
22const (
23 toolRetryOffered = "Offered: a card in the person's conversation asks whether to try the merge again. Only their tap merges, through Equipo's merge rules; you cannot. Tell them in one line, and never say it is merged."
24 toolNoFailedMerge = "Nothing to offer: this task has no merge that failed and ended. read_task shows where it stands."
25 toolAlreadyOffered = "The person already has a card to try the merge again. Wait for their tap; do not ask again."
26 toolRetryNotMergeable = "Not offered: the person's tap could not merge this task now (the branch moved, it is not approved, or Equipo's merge rules refuse it). Tell them what read_task shows."
27)
28
29// offerRetry is the coordinator's offer_retry: for a merge that failed and
30// ended, the merge card in the person's conversation asking whether to try
31// it again. Nothing is dispatched here. The card's Merge is answerMerge,
32// the person's own path (personMerge, the gate deciding for them then); it
33// is offered only when that tap could merge now, and once while it is open.
34func (t *txn) offerRetry(r turnRecord, args json.RawMessage) (wire.ToolResult, error) {
35 var in struct {
36 Task string `json:"task"`
37 }
38 if wire.Strict(args, &in) != nil {
39 return refused("invalid", toolBadInput), nil
40 }
41 k, res, err := t.ownTask(r.Person, in.Task)
42 if res != nil || err != nil {
43 return deref(res), err
44 }
45 if t.isOwner(r.Person) != nil {
46 return refused("not_owner", toolNotOwner), nil
47 }
48 steps, err := t.steps(k.ID)
49 if err != nil {
50 return wire.ToolResult{}, err
51 }
52 p := progressFor(k, steps)
53 if p.merge == nil || p.merge.State != wire.StepFailed || !p.merge.Terminated || p.eng == nil {
54 return refused("no_failed_merge", toolNoFailedMerge), nil
55 }
56 var open int64
57 if err := t.db.Model(&cardRecord{}).Where("task_id = ? AND kind = ? AND state = ?", k.ID, wire.CardMerge, wire.CardOpen).Count(&open).Error; err != nil {
58 return wire.ToolResult{}, err
59 }
60 if open > 0 {
61 return refused("already_offered", toolAlreadyOffered), nil
62 }
63 if ok, err := t.mergeOffered(k, p); err != nil || !ok {
64 return refused("not_mergeable", toolRetryNotMergeable), err
65 }
66 // What the gate would say to the person's tap now; only a decision,
67 // never recorded or acted on here.
68 online, protocol, err := t.primaryState(p.eng.Person)
69 if err != nil {
70 return wire.ToolResult{}, err
71 }
72 if !online {
73 protocol = 0
74 }
75 gate, _, err := t.mergeInputs(k, p, t.personDecides(k, k.Creator), protocol)
76 if err != nil {
77 return wire.ToolResult{}, err
78 }
79 if mergegate.Decide(gate).Class != mergegate.ClassAct {
80 return refused("not_mergeable", toolRetryNotMergeable), nil
81 }
82 now := t.now()
83 opts, _ := json.Marshal([]string{wire.ChoiceMerge, wire.ChoiceNotNow})
84 c := cardRecord{ID: newID("c_"), Person: k.Creator, Kind: wire.CardMerge, State: wire.CardOpen, TurnID: r.ID, ProjectID: k.ProjectID, TaskID: k.ID, Name: k.Title, Question: t.words("chat.merge.retry_offer", k.Title), Options: string(opts), Revision: 1, CreatedAt: now, UpdatedAt: now, Lane: r.Lane}
85 if err := t.db.Create(&c).Error; err != nil {
86 return wire.ToolResult{}, err
87 }
88 // The card asks about this candidate: answerMerge acts only on it.
89 if err := t.db.Model(&taskRecord{}).Where("id = ?", k.ID).Update("merge_asked", p.candidate).Error; err != nil {
90 return wire.ToolResult{}, err
91 }
92 if t.s.authority.Audit != nil {
93 if err := t.s.authority.Audit(t.db, "coordinator:"+r.Person, "task.merge_retry_offered", k.ID+"/"+p.merge.ID); err != nil {
94 return wire.ToolResult{}, err
95 }
96 }
97 if err := t.cardEvent(c); err != nil {
98 return wire.ToolResult{}, err
99 }
100 return wire.ToolResult{Code: "offered", Text: toolRetryOffered}, nil
101}
102
103// retryPreview runs a preview again (the person's Retry on it, or
104// retry_step), also when a merge step came after it or the task is done:
105// only the task's newest preview, ended and not up, as previewRetryCheck
106// says. On a task still open it is the step's next attempt, of the
107// candidate the task stands on; on a done task a new preview step of
108// previewTarget (rerunPreview). It is recomputed: the export is built and
109// scanned afresh and every publish is decided for that export
110// (authorizePreview), a public one only under the project's own setting.
111// A preview is ancillary, so the task's stage and its other steps are
112// untouched.
113func (t *txn) retryPreview(k *taskRecord, st *stepRecord, person string, coordinator bool) (int64, error) {
114 reason, target, err := t.previewRetryCheck(*k, *st, person)
115 switch {
116 case err != nil:
117 return 0, err
118 case reason == retryNotYours:
119 return 0, refuseRetry(ErrForbidden, reason)
120 case reason == retryTaskCancelled:
121 return 0, refuseRetry(ErrTaskEnded, reason)
122 case reason != "":
123 return 0, refuseRetry(ErrNotRetryable, reason)
124 }
125 if k.Stage == wire.StageDone {
126 return t.rerunPreview(k, st, target, coordinator)
127 }
128 st.Attempt++
129 st.State, st.Reason, st.Terminated, st.Summary, st.LogTail, st.StartedAt, st.FinishedAt, st.Headline, st.Activity = wire.StepQueued, "", false, "", "", nil, nil, "", ""
130 // Recomputed: no export recorded until this attempt reports one.
131 st.Preview, st.Withdrawn, st.RefusedBy = "", false, ""
132 if coordinator {
133 st.CoordinatorAttempt = st.Attempt
134 }
135 if err := t.dispatchStep(k, st); err != nil {
136 return 0, err
137 }
138 return st.Attempt, t.saveStep(st, k)
139}
140
141// More of what the model reads back for retry_step.
142const (
143 toolRetryKinds = "retry_step runs again only a failed preview, checks or review (step: preview, checks or review). A merge is the person's: use offer_retry. Engineering's work is theirs to ask for."
144 toolRetryStarted = "Started again as attempt %d. Tell the person in one line; the task shows it."
145 // A done task's preview runs again as a new step.
146 toolRetryNewPreview = "Started a new preview of the task's merged commit; the task stays done and merged. Tell the person in one line; the task shows it."
147 // The merged commit is not proven (merge_unverified), or Equipo did
148 // not merge it: the approved commit is built.
149 toolRetryApprovedPreview = "Started a new preview of the commit Review approved (Equipo could not prove the merged commit is exactly its merge, or did not merge it); the task stays done. Tell the person in one line; the task shows it."
150 toolStepNotRetried = "Not started: that step has not failed and ended, is not the task's newest (a preview may be behind a merge), or its candidate is no longer the one to judge. read_task shows where it stands."
151)
152
153// retryStepTool is the coordinator's retry_step: the person's Retry
154// (personRetry, its checks for the turn's person), for the task's newest
155// step of a kind in wire.RetryStepKinds, recorded as the coordinator's
156// (CoordinatorAttempt) and audited. It never runs a merge or a publish.
157// On a done task only a preview runs again, as a new step of the merged
158// commit. A refusal says why in a fixed reason (retryNotes).
159func (t *txn) retryStepTool(r turnRecord, args json.RawMessage) (wire.ToolResult, error) {
160 var in struct {
161 Task string `json:"task"`
162 Step string `json:"step"`
163 }
164 if wire.Strict(args, &in) != nil {
165 return refused("invalid", toolBadInput), nil
166 }
167 if !slices.Contains(wire.RetryStepKinds, in.Step) {
168 return refused("invalid", toolRetryKinds), nil
169 }
170 k, err := t.loadTask(strings.TrimSpace(in.Task))
171 if errors.Is(err, ErrNotFound) {
172 return refused("not_found", toolNoTask), nil
173 } else if err != nil {
174 return wire.ToolResult{}, err
175 }
176 if k.Creator != r.Person {
177 return refusedRetry("not_yours", retryNotYours), nil
178 }
179 var st stepRecord
180 if err := t.db.Where("task_id = ? AND specialist = ? AND coalesce(retired, 0) = 0", k.ID, in.Step).Order("idx DESC").First(&st).Error; errors.Is(err, gorm.ErrRecordNotFound) {
181 return refusedRetry("not_retryable", retryNoStep), nil
182 } else if err != nil {
183 return wire.ToolResult{}, err
184 }
185 attempt, err := t.personRetry(r.Person, k.ID, st.ID, true)
186 switch {
187 case errors.Is(err, ErrNotRetryable):
188 return refusedRetry("not_retryable", retryReasonOf(err, "not_retryable")), nil
189 case errors.Is(err, ErrTaskEnded):
190 return refusedRetry("task_ended", retryReasonOf(err, retryTaskCancelled)), nil
191 case errors.Is(err, ErrForbidden):
192 return refusedRetry("not_yours", retryReasonOf(err, retryNotYours)), nil
193 case err != nil:
194 return wire.ToolResult{}, err
195 }
196 text := fmt.Sprintf(toolRetryStarted, attempt)
197 var now stepRecord
198 if err := t.db.Where("task_id = ? AND specialist = ? AND coalesce(retired, 0) = 0", k.ID, in.Step).Order("idx DESC").First(&now).Error; err != nil {
199 return wire.ToolResult{}, err
200 }
201 if now.ID != st.ID {
202 // A done task's preview: a new step of the merged commit, or of the
203 // approved commit when the merged one is not proven.
204 text = toolRetryNewPreview
205 if now.CandidateSHA != k.MergedSHA {
206 text = toolRetryApprovedPreview
207 }
208 }
209 if t.s.authority.Audit != nil {
210 if err := t.s.authority.Audit(t.db, "coordinator:"+r.Person, "task.retry", k.ID+"/"+now.ID+"/"+itoa64(attempt)); err != nil {
211 return wire.ToolResult{}, err
212 }
213 }
214 return wire.ToolResult{Code: "retrying", Text: text}, nil
215}
216