Merge authority: Astra branch review fixes (fence, cards, Undo)
Astra's one round on the branch (REQUEST CHANGES):
1. An older runner reconnecting (protocol 1 included) is fenced at the
connection: its automatic merge attempts are settled, unsent
dispatches dropped, received ones cancelled, the person asked.
3. Open merge cards are re-decided and rewritten, with a new revision,
on hold_merge and on every policy change.
4. Undo needs the open receipt and its current revision, on the card
and on POST /tasks/{id}/undo-merge; a person's merge has none.
Finding 2 (a Mac re-run re-arms an asked SHA) is the controller's
re-run ruling, left as decided and reported. Tests and mutations in
MERGEAUTH-PROGRESS.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+298
−16
.rastrillo/budgets.txt+2 −2docs/superpowers/plans/MERGEAUTH-PROGRESS.md+30 −0internal/equipo/chat/http.go+6 −1internal/equipo/chat/mergeauth.go+142 −6internal/equipo/chat/runner.go+4 −0internal/thintest/mergeauth_contract_test.go+108 −7internal/wire/merge.go+6 −0
CI failed — run details
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index df321b8c..0da3e3a7 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -49,7 +49,7 @@ perf GET /api/v1/notebook skip Thin v1 person API: bearer-only JSON for devices; |
| perf GET /api/v1/thinking skip Thin v1 person API: bearer-only JSON for devices; the browser reads thinking words on /settings/thinking. |
| perf GET /api/v1/tasks skip Thin v1 person API: bearer-only JSON for devices; the browser reads tasks through /tasks. |
| perf GET /api/v1/tasks/{id} skip Thin v1 person API: bearer-only JSON for devices; the browser reads a task through /tasks/{id}. |
| -size internal/equipo/chat 13820 - Merge authority thin v1 adds the gate's store side (mergeauth.go: authorizeMerge before dispatch and at each hub request, the fence for runners below the merge-facts protocol, the ask card in Equipo's words, the ships question, receipts with Undo, the policy tool and API, about 1000 lines); the gate is pure, in internal/equipo/mergegate, but its inputs and audit must be read and written inside the store's transactions. Measured 13204. Mac recheck runs a waiting task's needs_macos checks again when a Mac that fits arrives (macrecheck.go, about 90 lines); measured 12485. Spend 1 merged onto Projects UX: measured 12376 with both. Spend 1 adds a task's spend from its usage rows (TaskSpend, about 60 lines) and the usage rows' round; measured 12047. Projects UX adds per-person project stars with the auto-star reconciliation, the person's and the coordinator's star_project (stars.go, about 210 lines) and the project's own repository in its view; measured 12206. Claude's sign-in by a link adds the code route beside the token's in SendSecret, the sign-in prompt and the kind it becomes (about 40 lines); measured 11933. Mac runners slices A and B add per-runner mailboxes, placement, withdrawal, the offline rule, the runners view, transfers and publication (runners.go, transfers.go, about 1100 lines). Composer attachments add uploads, the message link, the mailbox relay with chunk frames hydrated from the stored file, and serving (attachments.go, attachments_http.go, about 330 lines); the link and relay run inside the send and dispatch transactions, so they live beside them; sniffing and stripping are pure, in internal/attach. Measured 10292. Merged with UX1 thinking words (f2602d3), measured 9801. Housekeeping slice 1 adds the loop's ledger and state machine, the housekeeping principal's checks inside the shared store functions, reservations, Pause, quiet hours, the digest, claim recovery and the API (housekeeping.go, housekeeping_control.go, housekeeping_http.go, about 1,350 lines); the checks must run inside the store's transactions, so they live beside them; the detectors themselves are pure, in internal/equipo/housekeeping. Measured 9637. Thin v1 UX1 thinking words adds the per-person set and its API (thinking.go, about 100 lines); measured 8305. Connections slice 1 review round 2: the mutation ledger matches record_task by subject only and refuses an ambiguous call with the earlier operations, the preference and forget (about 120 lines); measured 8236. Thin v1 UX1 round 3 adds Equipo combining duplicate tasks reversibly (duplicates.go: detection, the in-transaction fold checks, fold, Undo, about 400 lines) and headlines on findings and steps (headline.go); measured 7898. Connections slice 1 adds connection records from hello and attempts, the connect mailbox, the API, usage rows and the turn's mutation ledger (connections.go and tools.go, about 600 lines); measured 7400 after merging UX1. Thin v1 UX1 adds step activity lines, duplicates and their fold, the coordinator at work on a task, project facts from the runner catalog and open merge cards for the pages (inner.go, about 330 lines); measured 6610 with slice 4c's review fixes. Thin v1 slice 4c adds the merge policy, the merge card, the rebase round and duplicate reports (merge.go, about 380 lines). Thin v1 instance core: conversation, projects, tasks, review and, from slice 4b, questions to the specialists (questions.go, about 470 lines) share one store, transaction and event log; splitting would export the transaction internals. Measured 5514 source lines; revisit at the slice 7 sweep. |
| +size internal/equipo/chat 13970 - Merge authority thin v1 adds the gate's store side (mergeauth.go: authorizeMerge before dispatch and at each hub request, the fence for runners below the merge-facts protocol, the ask card in Equipo's words, the ships question, receipts with Undo, the policy tool and API, about 1000 lines); the gate is pure, in internal/equipo/mergegate, but its inputs and audit must be read and written inside the store's transactions. Measured 13204. Mac recheck runs a waiting task's needs_macos checks again when a Mac that fits arrives (macrecheck.go, about 90 lines); measured 12485. Spend 1 merged onto Projects UX: measured 12376 with both. Spend 1 adds a task's spend from its usage rows (TaskSpend, about 60 lines) and the usage rows' round; measured 12047. Projects UX adds per-person project stars with the auto-star reconciliation, the person's and the coordinator's star_project (stars.go, about 210 lines) and the project's own repository in its view; measured 12206. Claude's sign-in by a link adds the code route beside the token's in SendSecret, the sign-in prompt and the kind it becomes (about 40 lines); measured 11933. Mac runners slices A and B add per-runner mailboxes, placement, withdrawal, the offline rule, the runners view, transfers and publication (runners.go, transfers.go, about 1100 lines). Composer attachments add uploads, the message link, the mailbox relay with chunk frames hydrated from the stored file, and serving (attachments.go, attachments_http.go, about 330 lines); the link and relay run inside the send and dispatch transactions, so they live beside them; sniffing and stripping are pure, in internal/attach. Measured 10292. Merged with UX1 thinking words (f2602d3), measured 9801. Housekeeping slice 1 adds the loop's ledger and state machine, the housekeeping principal's checks inside the shared store functions, reservations, Pause, quiet hours, the digest, claim recovery and the API (housekeeping.go, housekeeping_control.go, housekeeping_http.go, about 1,350 lines); the checks must run inside the store's transactions, so they live beside them; the detectors themselves are pure, in internal/equipo/housekeeping. Measured 9637. Thin v1 UX1 thinking words adds the per-person set and its API (thinking.go, about 100 lines); measured 8305. Connections slice 1 review round 2: the mutation ledger matches record_task by subject only and refuses an ambiguous call with the earlier operations, the preference and forget (about 120 lines); measured 8236. Thin v1 UX1 round 3 adds Equipo combining duplicate tasks reversibly (duplicates.go: detection, the in-transaction fold checks, fold, Undo, about 400 lines) and headlines on findings and steps (headline.go); measured 7898. Connections slice 1 adds connection records from hello and attempts, the connect mailbox, the API, usage rows and the turn's mutation ledger (connections.go and tools.go, about 600 lines); measured 7400 after merging UX1. Thin v1 UX1 adds step activity lines, duplicates and their fold, the coordinator at work on a task, project facts from the runner catalog and open merge cards for the pages (inner.go, about 330 lines); measured 6610 with slice 4c's review fixes. Thin v1 slice 4c adds the merge policy, the merge card, the rebase round and duplicate reports (merge.go, about 380 lines). Thin v1 instance core: conversation, projects, tasks, review and, from slice 4b, questions to the specialists (questions.go, about 470 lines) share one store, transaction and event log; splitting would export the transaction internals. Measured 5514 source lines; revisit at the slice 7 sweep. |
| perf GET /api/v1/questions/{id} skip Thin v1 person API: bearer-only JSON for devices; the browser reads questions on /tasks/{id}. |
| perf GET /api/v1/steps/{id}/log skip Thin v1 person API: bearer-only JSON for devices; the browser shows the log tail on /tasks/{id}. |
| perf GET /api/v1/connections/preference skip Connections slice 1 person API: bearer-only JSON for devices; the browser shows the preference on /conversation/connections. |
| @@ -62,5 +62,5 @@ perf GET /conversation/attachments/{id}/preview skip Composer attachments: an im |
| perf GET /api/v1/runners skip Mac runners person API: bearer-only JSON for devices; the browser shows runners in the Team section. |
| time cmd/equipo-runner 60s Mac runners slice B: TestReleaseRunnerHasNoFakeWorker builds the release runner exactly as make release-runner does and the thin package under the testbuild tag (measured 11.9s locally with a warm cache). |
| size internal/headless - 8300 Claude's sign-in by a link adds its end-to-end, contained-launch and real runner kill tests (signin_test.go, signin_contained_test.go, about 400 lines); measured 8090. |
| -size internal/thintest - 12500 Merge authority thin v1 adds its contracts (mergeauth_contract_test.go, about 650 lines) the protocol-aware rigs and the Mac re-run contract (macrecheck_mergeauth_contract_test.go); measured 12434 with main merged. Mac recheck adds its enrolment, skip and outcome contracts (macrecheck_contract_test.go, about 210 lines); measured 11537. Spend 1 merged onto the four UI branches: measured 11325. Spend 1 marks overcounting Claude rows (about 120 lines); it adds the task page's spend contract and the 0059 upgrade (spend_pages_test.go, about 100 lines) and the fix round feedback by protocol (about 40); measured 10224. The team panel merged onto Projects UX and the Settings UX pass: measured 11064 with all three. The team panel adds its page contract and the drawer's browser drive at 1280 and 390 (team_panel_test.go, team_panel_browser_test.go, about 300 lines); measured 10235. Projects UX merged onto the Settings UX pass: measured 10768 with both. Projects UX adds the Projects page contracts (order, quiet fold, filter, stars and Undo, the coordinator's star, repository counts, whole words), the decision cards' Mac and Ask about this contracts and their browser drive (projects_pages_test.go, projects_browser_test.go, about 330 lines); measured 10321. The Settings UX pass adds the Settings pattern's page contracts and the savebar, Housekeeping groups and Teams browser drives (settings_ux_pages_test.go, settings_ux_browser_test.go, about 430 lines); measured 10390. Claude's sign-in by a link adds the code contract (signin_contract_test.go: the card, the code route's refusals, the wrong-code card, the kind it becomes and the canary sweep, about 220 lines); measured 9896. The Connections Add page and the Settings rail add their page contracts and the rail's browser drive (connections_add_pages_test.go, settings_rail_pages_test.go, about 230 lines); measured 9676. Claude setup tokens add the token and Check again contracts; measured 9444 with the composer and Mac lines merged. The instant thinking line adds its browser drive (instant_browser_test.go, about 230 lines); measured 9064 with the composer and Mac lines merged. Mac runners add the role, scoping, migration, placement, withdrawal, transfer and publication contracts (about 900 test lines) beside the thin v1 contracts; one package keeps one instance harness. |
| +size internal/thintest - 12600 Merge authority thin v1 adds its contracts (mergeauth_contract_test.go, about 650 lines) the protocol-aware rigs and the Mac re-run contract (macrecheck_mergeauth_contract_test.go) and the review fixes' contracts; measured 12535 with main merged. Mac recheck adds its enrolment, skip and outcome contracts (macrecheck_contract_test.go, about 210 lines); measured 11537. Spend 1 merged onto the four UI branches: measured 11325. Spend 1 marks overcounting Claude rows (about 120 lines); it adds the task page's spend contract and the 0059 upgrade (spend_pages_test.go, about 100 lines) and the fix round feedback by protocol (about 40); measured 10224. The team panel merged onto Projects UX and the Settings UX pass: measured 11064 with all three. The team panel adds its page contract and the drawer's browser drive at 1280 and 390 (team_panel_test.go, team_panel_browser_test.go, about 300 lines); measured 10235. Projects UX merged onto the Settings UX pass: measured 10768 with both. Projects UX adds the Projects page contracts (order, quiet fold, filter, stars and Undo, the coordinator's star, repository counts, whole words), the decision cards' Mac and Ask about this contracts and their browser drive (projects_pages_test.go, projects_browser_test.go, about 330 lines); measured 10321. The Settings UX pass adds the Settings pattern's page contracts and the savebar, Housekeeping groups and Teams browser drives (settings_ux_pages_test.go, settings_ux_browser_test.go, about 430 lines); measured 10390. Claude's sign-in by a link adds the code contract (signin_contract_test.go: the card, the code route's refusals, the wrong-code card, the kind it becomes and the canary sweep, about 220 lines); measured 9896. The Connections Add page and the Settings rail add their page contracts and the rail's browser drive (connections_add_pages_test.go, settings_rail_pages_test.go, about 230 lines); measured 9676. Claude setup tokens add the token and Check again contracts; measured 9444 with the composer and Mac lines merged. The instant thinking line adds its browser drive (instant_browser_test.go, about 230 lines); measured 9064 with the composer and Mac lines merged. Mac runners add the role, scoping, migration, placement, withdrawal, transfer and publication contracts (about 900 test lines) beside the thin v1 contracts; one package keeps one instance harness. |
| perf GET /api/v1/projects/{id}/merge-policy skip Merge authority person API: bearer-only JSON for devices; the browser reads the policy on /projects/{id}. |
diff --git a/docs/superpowers/plans/MERGEAUTH-PROGRESS.md b/docs/superpowers/plans/MERGEAUTH-PROGRESS.md| index ac24c3f5..a4014e06 100644 |
| --- a/docs/superpowers/plans/MERGEAUTH-PROGRESS.md |
| +++ b/docs/superpowers/plans/MERGEAUTH-PROGRESS.md |
| @@ -151,3 +151,33 @@ the two tool descriptions, the house-rule sentence) are not runtime copy. |
| Verification on the merged commit, the trust counter, automatic fix |
| tasks, a revert step, a same-SHA checks retry after Wait, and the forge's |
| tip fallback fix for every merge (a separate issue). |
| + |
| +## Astra branch review (one round, on 4d37307): REQUEST CHANGES |
| + |
| +1. Critical, protocol 1 bypasses the lease fence: fixed. The fence now |
| + also runs when a runner connects below `wire.ProtocolMergeFacts` |
| + (`fenceOldRunner` in `Connect`): every automatic merge attempt placed |
| + on it is settled failed `merge_not_authorized`, an unsent dispatch is |
| + never sent, a received one is cancelled, and the person is asked. |
| + `TestContractMergeFenceOldRunner` covers a 4 to 2 and a 4 to 1 |
| + reconnect. Mutations (no fence at connect, no cancel) killed. Limit: |
| + an older build already mid-request when it reconnects cannot be |
| + stopped by the instance; the forge record and the merge watch settle |
| + that landing. |
| +2. Critical, a Mac re-run re-arms an asked SHA: not changed. The |
| + controller ruled that a re-run pass on the exact candidate merges a |
| + routine change on its own through `authorizeMerge` (sensitive, |
| + shipping, held and asking cases ask); this is that ruling, reported |
| + back as a product decision. |
| +3. Important, open cards miss later holds and policy changes: fixed. |
| + `refreshMergeCards` re-decides (audited as `card_refresh`, never shown |
| + as the task's decision, never acted on) and rewrites an open card's |
| + words and options with a new revision on `hold_merge` and every |
| + policy change. `TestContractCardRefresh`; mutations (no refresh on a |
| + hold, words not rewritten) killed. |
| +4. Important, Undo not bound to the receipt: fixed. Undo needs the open |
| + receipt offering it (only Equipo's own merge without checks with a |
| + hub-named merge commit has one) and its current revision on both the |
| + card and `POST /api/v1/tasks/{id}/undo-merge` (`{"revision": n}`). |
| + `TestContractUndoMerge` adds a person's merge with the hub's answer |
| + (no Undo) and a stale revision (409); mutations killed. |
diff --git a/internal/equipo/chat/http.go b/internal/equipo/chat/http.go| index 612244b5..fc2e31ed 100644 |
| --- a/internal/equipo/chat/http.go |
| +++ b/internal/equipo/chat/http.go |
| @@ -694,7 +694,12 @@ func (h *HTTP) mergeTask(w http.ResponseWriter, r *http.Request, p personapi.Per |
| // undoMerge is the creator's Undo of Equipo's own merge: an ordinary |
| // revert task, returned. |
| func (h *HTTP) undoMerge(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| - out, err := h.Store.UndoMerge(r.Context(), p.Address, chi.URLParam(r, "id")) |
| + var in wire.UndoMerge |
| + if err := readJSON(r, &in); err != nil { |
| + h.fail(w, r, err) |
| + return |
| + } |
| + out, err := h.Store.UndoMerge(r.Context(), p.Address, chi.URLParam(r, "id"), in.Revision) |
| if err != nil { |
| h.fail(w, r, err) |
| return |
diff --git a/internal/equipo/chat/mergeauth.go b/internal/equipo/chat/mergeauth.go| index 1a48d356..b9e89840 100644 |
| --- a/internal/equipo/chat/mergeauth.go |
| +++ b/internal/equipo/chat/mergeauth.go |
| @@ -73,9 +73,11 @@ const ( |
| auditPolicy = "policy" |
| auditShips = "ships" |
| auditHold = "hold" |
| + auditRefresh = "card_refresh" |
| atDispatch = "dispatch" |
| atConfirm = "confirm" |
| atFence = "fence" |
| + atRefresh = "card_refresh" |
| ) |
| // decidedPolicy and decidedPerson prefix a merge step's DecidedBy. |
| @@ -130,12 +132,57 @@ func (t *txn) writePolicy(r *mergePolicyRecord, by string, touch bool) error { |
| if !touch { |
| return nil |
| } |
| + return t.refreshMergeCards("project_id = ?", r.ProjectID) |
| +} |
| + |
| +// refreshMergeCards decides again for each open merge card matching the |
| +// condition (a policy change, a hold) and rewrites its words and options |
| +// when it still asks, with a new revision either way: the person answers |
| +// what holds now, and an answer given before the change is stale. |
| +func (t *txn) refreshMergeCards(cond string, arg any) error { |
| var open []cardRecord |
| - if err := t.db.Where("project_id = ? AND kind = ? AND state = ?", r.ProjectID, wire.CardMerge, wire.CardOpen).Find(&open).Error; err != nil { |
| + if err := t.db.Where(cond+" AND kind = ? AND state = ?", arg, wire.CardMerge, wire.CardOpen).Find(&open).Error; err != nil { |
| return err |
| } |
| for i := range open { |
| - if err := t.saveCard(&open[i]); err != nil { |
| + c := &open[i] |
| + k, err := t.loadTask(c.TaskID) |
| + if err != nil { |
| + return err |
| + } |
| + steps, err := t.steps(k.ID) |
| + if err != nil { |
| + return err |
| + } |
| + p := progressFor(k, steps) |
| + if p.eng != nil && !p.moved && p.candidate == k.MergeAsked { |
| + _, protocol, err := t.primaryState(p.eng.Person) |
| + if err != nil { |
| + return err |
| + } |
| + // The card's words only: recorded, but never shown as the task's |
| + // decision and never acted on (only the person acts on an asked SHA). |
| + in, _, err := t.mergeInputs(k, p, false, protocol) |
| + if err != nil { |
| + return err |
| + } |
| + d := mergegate.Decide(in) |
| + if err := t.auditDecision(k, in, d, auditRefresh, atRefresh, ""); err != nil { |
| + return err |
| + } |
| + if d.Class == mergegate.ClassAsk { |
| + question, choices, err := t.mergeQuestion(k, p, d) |
| + if err != nil { |
| + return err |
| + } |
| + opts, _ := json.Marshal(choices) |
| + c.Question, c.Options = question, string(opts) |
| + if err := t.db.Model(&cardRecord{}).Where("id = ?", c.ID).Updates(map[string]any{"question": c.Question, "options": c.Options}).Error; err != nil { |
| + return err |
| + } |
| + } |
| + } |
| + if err := t.saveCard(c); err != nil { |
| return err |
| } |
| } |
| @@ -335,6 +382,61 @@ func (t *txn) authorizeMergeLease(l Lease, s *stepRecord, purpose string) (strin |
| return wire.RefusedMergeNotAuthorized, t.refuseMerge(&k, s, p, d) |
| } |
| +// fenceOldRunner is the fence when a runner connects below |
| +// wire.ProtocolMergeFacts: such a connection cannot confirm a merge |
| +// request by its attempt (protocol 1 cannot even name the step), so every |
| +// automatic merge attempt placed on it is settled now, before it can run: |
| +// a dispatch it has not received is never sent, one it holds is |
| +// cancelled, and the person is asked. A request it already sent before |
| +// it reconnected is settled by the merge watch from the default branch. |
| +func (t *txn) fenceOldRunner(l Lease) error { |
| + if l.Protocol >= wire.ProtocolMergeFacts { |
| + return nil |
| + } |
| + var held []stepRecord |
| + if err := t.db.Where("runner_id = ? AND specialist = ? AND state IN ?", l.Runner.RunnerID, wire.SpecialistMerge, []string{wire.StepQueued, wire.StepRunning}).Find(&held).Error; err != nil { |
| + return err |
| + } |
| + for i := range held { |
| + s := &held[i] |
| + if !mergeByPolicy(*s) { |
| + continue |
| + } |
| + k, err := t.loadTask(s.TaskID) |
| + if err != nil { |
| + return err |
| + } |
| + steps, err := t.steps(k.ID) |
| + if err != nil { |
| + return err |
| + } |
| + p := progressFor(k, steps) |
| + now := t.now() |
| + res := t.db.Model(&commandRecord{}).Where("runner_id = ? AND kind = ? AND acked_at IS NULL AND body LIKE ?", s.RunnerID, wire.FrameDispatch, `%"step_id":"`+s.ID+`"%`).Update("acked_at", now) |
| + if res.Error != nil { |
| + return res.Error |
| + } |
| + if res.RowsAffected == 0 { |
| + if err := t.commandTo(s.RunnerID, s.Person, wire.FrameCancelStep, wire.CancelStepCommand{Person: s.Person, TaskID: k.ID, StepID: s.ID, Attempt: s.Attempt}); err != nil { |
| + return err |
| + } |
| + } |
| + d := mergegate.Decision{Class: mergegate.ClassNever} |
| + if p.candidate == s.CandidateSHA { |
| + if d, err = t.authorizeMerge(&k, p, atFence, "", l.Protocol); err != nil { |
| + return err |
| + } |
| + if d.Class == mergegate.ClassAct { |
| + d.Class, d.Stance = mergegate.ClassAsk, mergegate.StanceMerge |
| + } |
| + } |
| + if err := t.refuseMerge(&k, s, p, d); err != nil { |
| + return err |
| + } |
| + } |
| + return nil |
| +} |
| + |
| // refuseMerge ends a merge attempt the gate refused before any request |
| // went, and asks the person when the decision is to ask. |
| func (t *txn) refuseMerge(k *taskRecord, s *stepRecord, p progress, d mergegate.Decision) error { |
| @@ -638,18 +740,38 @@ func mergeVerified(v *wire.StepReport, candidate string) bool { |
| } |
| // undoOffered reports whether the creator may Undo the task's merge now. |
| +// Only Equipo's own merge without checks has a receipt, and only one |
| +// whose merge commit the hub named offers Undo on it: the receipt, open, |
| +// is the eligibility (a person's merge has none). |
| func (t *txn) undoOffered(k taskRecord) (bool, error) { |
| + _, ok, err := t.undoReceipt(k) |
| + return ok, err |
| +} |
| + |
| +// undoReceipt is the task's open receipt offering Undo, while Undo is |
| +// still possible. |
| +func (t *txn) undoReceipt(k taskRecord) (cardRecord, bool, error) { |
| + var c cardRecord |
| if !k.MergeVerified || k.MergedSHA == "" || k.MergedAt == nil || !k.MergedWithoutChecks || t.now().Sub(*k.MergedAt) > housekeeping.UndoWindow { |
| - return false, nil |
| + return c, false, nil |
| + } |
| + err := t.db.Where("task_id = ? AND kind = ? AND state = ? AND options = ?", k.ID, wire.CardMergeReceipt, wire.CardOpen, `["undo"]`).First(&c).Error |
| + if errors.Is(err, gorm.ErrRecordNotFound) { |
| + return c, false, nil |
| + } |
| + if err != nil { |
| + return c, false, err |
| } |
| var n int64 |
| - err := t.db.Model(&taskRecord{}).Where("revert_of = ?", k.ID).Count(&n).Error |
| - return n == 0, err |
| + err = t.db.Model(&taskRecord{}).Where("revert_of = ?", k.ID).Count(&n).Error |
| + return c, n == 0, err |
| } |
| // UndoMerge is the creator's Undo of Equipo's own merge: an ordinary |
| // revert task through the normal flow, and the project to ask mode. |
| -func (s *Store) UndoMerge(ctx context.Context, person, id string) (out wire.Task, err error) { |
| +// revision is the receipt's revision as the person saw it; a stale one |
| +// is a conflict. |
| +func (s *Store) UndoMerge(ctx context.Context, person, id string, revision int64) (out wire.Task, err error) { |
| err = s.tx(ctx, func(t *txn) error { |
| if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| return err |
| @@ -664,6 +786,16 @@ func (s *Store) UndoMerge(ctx context.Context, person, id string) (out wire.Task |
| if err := t.isOwner(person); err != nil { |
| return err |
| } |
| + receipt, ok, err := t.undoReceipt(k) |
| + if err != nil { |
| + return err |
| + } |
| + if !ok { |
| + return ErrNotUndoable |
| + } |
| + if receipt.Revision != revision { |
| + return ErrRevision |
| + } |
| rev, err := t.undoMerge(person, k) |
| if err != nil { |
| return err |
| @@ -825,6 +957,10 @@ func (t *txn) holdMerge(r turnRecord, args json.RawMessage) (wire.ToolResult, er |
| if err := t.saveTask(&k); err != nil { |
| return wire.ToolResult{}, err |
| } |
| + // An open card says so now, and an answer from before is stale. |
| + if err := t.refreshMergeCards("task_id = ?", k.ID); err != nil { |
| + return wire.ToolResult{}, err |
| + } |
| if err := t.auditRow(mergeAuditRecord{ProjectID: k.ProjectID, TaskID: k.ID, Kind: auditHold, At: "tool", DecidedBy: "coordinator:" + r.Person, Inputs: fmt.Sprintf(`{"reason":%q}`, reason)}); err != nil { |
| return wire.ToolResult{}, err |
| } |
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go| index 3646113e..af66e6a6 100644 |
| --- a/internal/equipo/chat/runner.go |
| +++ b/internal/equipo/chat/runner.go |
| @@ -94,6 +94,10 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string, |
| if err := t.runnerEvent(p.Person, r); err != nil { |
| return err |
| } |
| + // Merge authority: an older runner holds no automatic merge. |
| + if err := t.fenceOldRunner(lease); err != nil { |
| + return err |
| + } |
| // A runner that fits may have come online. |
| if err := t.placeWaiting(p.Person); err != nil { |
| return err |
diff --git a/internal/thintest/mergeauth_contract_test.go b/internal/thintest/mergeauth_contract_test.go| index 16c50f4f..e28b0fc8 100644 |
| --- a/internal/thintest/mergeauth_contract_test.go |
| +++ b/internal/thintest/mergeauth_contract_test.go |
| @@ -23,8 +23,8 @@ func (c *Client) SetMergePolicy(ctx context.Context, project string, in wire.Set |
| return out, c.do(ctx, "PUT", "/api/v1/projects/"+project+"/merge-policy", in, &out) |
| } |
| -func (c *Client) UndoMerge(ctx context.Context, id string) (out wire.Task, err error) { |
| - return out, c.do(ctx, "POST", "/api/v1/tasks/"+id+"/undo-merge", nil, &out) |
| +func (c *Client) UndoMerge(ctx context.Context, id string, revision int64) (out wire.Task, err error) { |
| + return out, c.do(ctx, "POST", "/api/v1/tasks/"+id+"/undo-merge", wire.UndoMerge{Revision: revision}, &out) |
| } |
| // facts is a small, ordinary change as the runner measures it for review |
| @@ -263,8 +263,23 @@ func TestContractMergeFenceOldRunner(t *testing.T) { |
| t.Fatal(err) |
| } |
| t.Cleanup(two.Close) |
| - if refused := r.lease(two, "f-lease", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused != wire.RefusedMergeNotAuthorized { |
| - t.Fatalf("an old runner's lease for an automatic merge: %q", refused) |
| + // Fenced at the connection: the attempt is settled and cancelled, and |
| + // its lease refused. |
| + cancelled := false |
| + for !cancelled { |
| + f, body, err := two.Next(r.ctx) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if c, ok := body.(*wire.CancelStepCommand); ok && c.StepID == m.StepID && c.Attempt == m.Attempt { |
| + cancelled = true |
| + } |
| + if f.Durable() { |
| + two.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| + } |
| + } |
| + if refused := r.lease(two, "f-lease", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused == "" { |
| + t.Fatal("an old runner's lease for an automatic merge was granted") |
| } |
| r.wait("ready again, asking", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| card := r.mergeCardsWait(id) |
| @@ -288,6 +303,38 @@ func TestContractMergeFenceOldRunner(t *testing.T) { |
| if refused := r.lease(two, "f-lease-2", wire.LeaseCheck{StepID: again.StepID, Attempt: again.Attempt}); refused != "" { |
| t.Fatalf("the person's merge refused on an old runner: %q", refused) |
| } |
| + |
| + // Protocol 1 cannot name the step in its lease at all: the fence at the |
| + // connection is all there is. A merge dispatched at ProtocolMergeFacts |
| + // is settled, cancelled and asked about the moment the runner comes |
| + // back at protocol 1. |
| + r1 := newToolRigAt(t, wire.ProtocolMergeFacts) |
| + id1 := r1.judgedTask("p1", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| + m1 := r1.dispatched(wire.SpecialistMerge, base, cand, 0) |
| + one, err := DialRunnerHello(r1.ctx, r1.inst.URL, r1.rig.runner, wire.Hello{Generation: r1.runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "old"}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(one.Close) |
| + if one.Welcome.Protocol > wire.Protocol1 { |
| + t.Fatalf("welcome %+v", one.Welcome) |
| + } |
| + for { |
| + f, body, err := one.Next(r1.ctx) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if f.Durable() { |
| + one.Send(r1.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| + } |
| + if c, ok := body.(*wire.CancelStepCommand); ok && c.StepID == m1.StepID { |
| + break |
| + } |
| + } |
| + r1.wait("asked after the fence", func(c *Conversation) bool { return c.Tasks[id1].Stage == wire.StageReadyToMerge }) |
| + if card := r1.mergeCardsWait(id1); !strings.Contains(card.Question, "your runner needs an update before I merge on my own") { |
| + t.Fatalf("protocol 1 fence card %+v", card) |
| + } |
| } |
| // mergeCardsWait waits for the task's merge card. |
| @@ -475,9 +522,12 @@ func TestContractUndoMerge(t *testing.T) { |
| return out, true |
| }() |
| mate := personToolRig(t, r, "mate@example.com", equipo.RoleOwner) |
| - if _, err := mate.client.UndoMerge(r.ctx, id); Status(err) != http.StatusNotFound { |
| + if _, err := mate.client.UndoMerge(r.ctx, id, receipt.Revision); Status(err) != http.StatusNotFound { |
| t.Fatalf("teammate undo: %v", err) |
| } |
| + if _, err := r.client.UndoMerge(r.ctx, id, receipt.Revision+1); Status(err) != http.StatusConflict { |
| + t.Fatalf("undo on a stale receipt: %v", err) |
| + } |
| out, err := r.client.Answer(r.ctx, receipt.ID, wire.ChoiceUndo, receipt.Revision) |
| if err != nil { |
| t.Fatal(err) |
| @@ -493,7 +543,7 @@ func TestContractUndoMerge(t *testing.T) { |
| if p, _ := r.client.MergePolicy(r.ctx, project); p.Mode != "ask" || !strings.Contains(p.ModeReason, "eeeeeee") { |
| t.Fatalf("policy after undo %+v", p) |
| } |
| - if _, err := r.client.UndoMerge(r.ctx, id); Status(err) != http.StatusConflict { |
| + if _, err := r.client.UndoMerge(r.ctx, id, out.Card.Revision); Status(err) != http.StatusConflict { |
| t.Fatalf("undo twice: %v", err) |
| } |
| // The revert's review measures whether it reverts exactly that commit. |
| @@ -536,9 +586,25 @@ func TestContractUndoMerge(t *testing.T) { |
| if len(unverified.Options) != 0 || !strings.Contains(unverified.Question, "so I can't undo it") { |
| t.Fatalf("unverified receipt %+v", unverified) |
| } |
| - if _, err := r2.client.UndoMerge(r2.ctx, id2); Status(err) != http.StatusConflict { |
| + if _, err := r2.client.UndoMerge(r2.ctx, id2, unverified.Revision); Status(err) != http.StatusConflict { |
| t.Fatalf("undo of an unverified merge: %v", err) |
| } |
| + // A person's merge without checks, even with the hub's own answer, |
| + // has no receipt and no Undo: it was their call. |
| + id3, _ := r2.measured("p", "Haptics", base, strings.Repeat("3", 40), wire.ChecksCannotRun, "needs_macos", nil) |
| + card3 := r2.mergeCardsWait(id3) |
| + if _, err := r2.client.Answer(r2.ctx, card3.ID, wire.ChoiceMerge, card3.Revision); err != nil { |
| + t.Fatal(err) |
| + } |
| + m3 := r2.dispatched(wire.SpecialistMerge, base, strings.Repeat("3", 40), 0) |
| + r2.step("p-m", wire.StepReport{TaskID: id3, StepID: m3.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: m3.CandidateSHA, Termination: true, Findings: []wire.Finding{}, MergeSHA: strings.Repeat("d", 40), Into: "main", Strategy: wire.MergeCommit, MergeSource: wire.MergeSourceHub, MergeParents: []string{strings.Repeat("9", 40), m3.CandidateSHA}}) |
| + r2.wait("person's merge done", func(c *Conversation) bool { return c.Tasks[id3].Stage == wire.StageDone }) |
| + if k, _ := r2.client.Task(r2.ctx, id3); k.UndoOffered { |
| + t.Fatal("Undo offered on a person's merge") |
| + } |
| + if _, err := r2.client.UndoMerge(r2.ctx, id3, 1); Status(err) != http.StatusConflict { |
| + t.Fatalf("undo of a person's merge: %v", err) |
| + } |
| } |
| // TestContractMergePolicyTool: the coordinator's set_merge_policy applies |
| @@ -656,3 +722,38 @@ func TestContractRosterDenial(t *testing.T) { |
| t.Fatal("dispatched for a demoted creator") |
| } |
| } |
| + |
| +// TestContractCardRefresh: a hold or a stricter policy after the card |
| +// opened rewrites the card's words, with a new revision, so the person |
| +// answers what holds now; an answer from before the change is stale. |
| +func TestContractCardRefresh(t *testing.T) { |
| + r := newToolRigAt(t, wire.ProtocolMergeFacts) |
| + project := r.project() |
| + base := strings.Repeat("1", 40) |
| + id, _ := r.measured("c", "Swipe back", base, strings.Repeat("2", 40), wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| + before := r.mergeCardsWait(id) |
| + if res := r.call("c-hold", wire.ToolHoldMerge, map[string]string{"task": id, "reason": "the person wants to try it first"}); res.IsError { |
| + t.Fatalf("hold %+v", res) |
| + } |
| + held := r.cardOf(id) |
| + if held.Revision <= before.Revision || !strings.Contains(held.Question, "the person wants to try it first") { |
| + t.Fatalf("after the hold %+v", held) |
| + } |
| + if _, err := r.client.Answer(r.ctx, before.ID, wire.ChoiceMerge, before.Revision); Status(err) != http.StatusConflict { |
| + t.Fatalf("an answer from before the hold: %v", err) |
| + } |
| + p, _ := r.client.MergePolicy(r.ctx, project) |
| + if _, err := r.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: "ask", MaxLines: p.MaxLines, MaxFiles: p.MaxFiles, MainShips: p.MainShips, ExtraSensitive: p.ExtraSensitive, Revision: p.Revision}); err != nil { |
| + t.Fatal(err) |
| + } |
| + asking := r.cardOf(id) |
| + if asking.Revision <= held.Revision || !strings.Contains(asking.Question, "this project asks before every merge") { |
| + t.Fatalf("after ask mode %+v", asking) |
| + } |
| + if _, err := r.client.Answer(r.ctx, held.ID, wire.ChoiceMerge, held.Revision); Status(err) != http.StatusConflict { |
| + t.Fatalf("an answer from before the policy change: %v", err) |
| + } |
| + if k, _ := r.client.Task(r.ctx, id); k.MergeDecision == nil || k.MergeDecision.Class != "ask" || r.heldKinds(wire.FrameDispatch) != 0 { |
| + t.Fatalf("refresh acted %+v", k.MergeDecision) |
| + } |
| +} |
diff --git a/internal/wire/merge.go b/internal/wire/merge.go| index cb7d6663..748d463a 100644 |
| --- a/internal/wire/merge.go |
| +++ b/internal/wire/merge.go |
| @@ -84,3 +84,9 @@ type SetMergePolicy struct { |
| Words string `json:"words"` |
| Revision int64 `json:"revision"` |
| } |
| + |
| +// UndoMerge is POST /api/v1/tasks/{id}/undo-merge: the receipt's |
| +// revision as the person saw it. |
| +type UndoMerge struct { |
| + Revision int64 `json:"revision"` |
| +} |