runner: change facts on review, merge_request confirmation, merge provenance
Review measures the reviewed pair with no model (raw and numstat listings agreeing, both rename paths, modes, binary files, signing lines, a revert's tree check) and reports it at ProtocolMergeFacts. The merge step confirms each hub request with the merge_request purpose and ends merge_not_authorized, nothing sent, when the instance refuses; its report says where the merge SHA came from and, from the hub's own answer, the merge commit's parents. The protocol is named by capability (wire.ProtocolMergeFacts), so a renumber is one line.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+627
−42
internal/equipo/mergegate/gate.go+2 −2internal/equipo/mergegate/gate_test.go+7 −7internal/runner/forge/merge.go+15 −3internal/runner/thin/engineer.go+3 −0internal/runner/thin/facts.go+217 −0internal/runner/thin/facts_test.go+184 −0internal/runner/thin/judge.go+4 −1internal/runner/thin/link.go+26 −4internal/runner/thin/mergestep.go+62 −2internal/runner/thin/mergestep_test.go+74 −0internal/runner/thin/protocol_test.go+2 −2internal/runner/thin/run.go+1 −1internal/wire/frames.go+4 −0internal/wire/runners.go+22 −16internal/wire/runners_test.go+4 −4
Intent
diff --git a/internal/equipo/mergegate/gate.go b/internal/equipo/mergegate/gate.go| index 1321d23c..3723b28c 100644 |
| --- a/internal/equipo/mergegate/gate.go |
| +++ b/internal/equipo/mergegate/gate.go |
| @@ -51,8 +51,8 @@ const ( |
| MaxExtraGlobs = 20 |
| MaxGlobBytes = 200 |
| // MinProtocol is the runner protocol that measures change facts and |
| - // confirms each merge request (wire.Protocol3). |
| - MinProtocol = wire.Protocol3 |
| + // confirms each merge request by its attempt (wire.ProtocolMergeFacts). |
| + MinProtocol = wire.ProtocolMergeFacts |
| ) |
| // Criteria, in the order Decide evaluates and reports them. |
diff --git a/internal/equipo/mergegate/gate_test.go b/internal/equipo/mergegate/gate_test.go| index 69768d27..a02af122 100644 |
| --- a/internal/equipo/mergegate/gate_test.go |
| +++ b/internal/equipo/mergegate/gate_test.go |
| @@ -26,7 +26,7 @@ func smallChange() *wire.ChangeFacts { |
| func passing() Inputs { |
| p := DefaultPolicy() |
| p.MainShips = ShipsNo |
| - return Inputs{Candidate: cand, Verdict: wire.VerdictApprove, Checks: wire.ChecksCannotRun, ChecksReason: "needs_macos", Change: smallChange(), Protocol: wire.Protocol3, Policy: p} |
| + return Inputs{Candidate: cand, Verdict: wire.VerdictApprove, Checks: wire.ChecksCannotRun, ChecksReason: "needs_macos", Change: smallChange(), Protocol: wire.ProtocolMergeFacts, Policy: p} |
| } |
| func failing(d Decision) []string { |
| @@ -47,7 +47,7 @@ func TestDecideEachCriterion(t *testing.T) { |
| } |
| cases := map[string]func(*Inputs){ |
| CritEnvironment: func(in *Inputs) { in.ChecksReason = "checks_timeout" }, |
| - CritProtocol: func(in *Inputs) { in.Protocol = wire.Protocol2 }, |
| + CritProtocol: func(in *Inputs) { in.Protocol = wire.ProtocolMergeFacts - 1 }, |
| CritFacts: func(in *Inputs) { in.Change = nil }, |
| CritSmall: func(in *Inputs) { in.Change.Lines = 301 }, |
| CritSensitive: func(in *Inputs) { in.Change.Entries[0].New = "Woodstar/Billing/Checkout.swift" }, |
| @@ -123,14 +123,14 @@ func TestDecideNever(t *testing.T) { |
| // protocol 3 runner, with no hold and the project not asking. Size, |
| // sensitivity and shipping do not apply to it. |
| func TestDecideChecksPassedPath(t *testing.T) { |
| - ok := Inputs{Candidate: cand, Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed, Eligible: true, Protocol: wire.Protocol3, Policy: DefaultPolicy()} |
| + ok := Inputs{Candidate: cand, Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed, Eligible: true, Protocol: wire.ProtocolMergeFacts, Policy: DefaultPolicy()} |
| if d := Decide(ok); d.Class != ClassAct || !d.Checks { |
| t.Fatalf("checked: %+v", d) |
| } |
| for name, change := range map[string]func(*Inputs){ |
| CritEligible: func(in *Inputs) { in.Eligible = false }, |
| CritSandbox: func(in *Inputs) { in.OutsideSandbox = true }, |
| - CritProtocol: func(in *Inputs) { in.Protocol = wire.Protocol2 }, |
| + CritProtocol: func(in *Inputs) { in.Protocol = wire.ProtocolMergeFacts - 1 }, |
| CritPolicy: func(in *Inputs) { in.Policy.Mode = ModeAsk }, |
| CritHold: func(in *Inputs) { in.Hold = "wait" }, |
| "no protocol": func(in *Inputs) { in.Protocol = 0 }, |
| @@ -189,7 +189,7 @@ func oracle(in Inputs) bool { |
| if in.ChecksReason != "needs_macos" && in.ChecksReason != "runner_offline" { |
| return false |
| } |
| - if in.Protocol < 3 || in.Change == nil || !in.Change.Complete || in.Hold != "" || in.Policy.Mode != ModeRules || in.Policy.MainShips != ShipsNo { |
| + if in.Protocol < wire.ProtocolMergeFacts || in.Change == nil || !in.Change.Complete || in.Hold != "" || in.Policy.Mode != ModeRules || in.Policy.MainShips != ShipsNo { |
| return false |
| } |
| if in.Policy.MaxLines < 0 || in.Policy.MaxLines > 2000 || in.Policy.MaxFiles < 0 || in.Policy.MaxFiles > 100 { |
| @@ -223,7 +223,7 @@ func TestDecideNeverLooser(t *testing.T) { |
| ChecksReason: pick("needs_macos", "runner_offline", "checks_timeout", "needs_network", ""), |
| OutsideSandbox: rng.IntN(4) == 0, |
| Eligible: rng.IntN(2) == 0, |
| - Protocol: rng.IntN(4), |
| + Protocol: rng.IntN(wire.ProtocolMergeFacts + 1), |
| Hold: pick("", "", "", "careful"), |
| Policy: Policy{Mode: pick(ModeRules, ModeRules, ModeAsk, "loose"), MaxLines: rng.IntN(3000) - 500, MaxFiles: rng.IntN(200) - 20, MainShips: pick(ShipsNo, ShipsNo, ShipsYes, ShipsUnknown, "maybe")}, |
| RevertSHA: pick("", "", strings.Repeat("e", 40)), |
| @@ -248,7 +248,7 @@ func TestDecideNeverLooser(t *testing.T) { |
| if in.Hold != "" && d.Class == ClassAct { |
| t.Fatalf("a hold merged: %+v", in) |
| } |
| - if d.Class == ClassAct && d.Checks && (in.Checks != wire.ChecksPassed || !in.Eligible || in.OutsideSandbox || in.Protocol < 3 || in.Policy.Mode != ModeRules) { |
| + if d.Class == ClassAct && d.Checks && (in.Checks != wire.ChecksPassed || !in.Eligible || in.OutsideSandbox || in.Protocol < wire.ProtocolMergeFacts || in.Policy.Mode != ModeRules) { |
| t.Fatalf("checked merge against the rule: %+v", in) |
| } |
| } |
diff --git a/internal/runner/forge/merge.go b/internal/runner/forge/merge.go| index 3652e11d..41e4a397 100644 |
| --- a/internal/runner/forge/merge.go |
| +++ b/internal/runner/forge/merge.go |
| @@ -155,8 +155,20 @@ type BranchMerge struct { |
| type MergeOutcome struct { |
| SHA, Into, Strategy string |
| Reconciled bool |
| + // Source is where SHA came from: SourceHub, this request's own answer |
| + // from the hub; SourceReconciled, the default branch read after the |
| + // fact (its tip when the landing commit was not found); SourceCached, |
| + // this runner's earlier record. Only SourceHub is the hub's word. |
| + Source string |
| } |
| +// Merge outcome sources. |
| +const ( |
| + SourceHub = "hub" |
| + SourceReconciled = "reconciled" |
| + SourceCached = "cached" |
| +) |
| + |
| type mergeRecord struct { |
| Task string `json:"task"` |
| Repo string `json:"repo"` |
| @@ -244,7 +256,7 @@ func (m BranchMerge) landed(rec mergeRecord, st RemoteState, reconciled bool) (M |
| if err := m.save(rec); err != nil { |
| return MergeOutcome{}, ErrMergeUncertain |
| } |
| - return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy, Reconciled: reconciled}, nil |
| + return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy, Reconciled: reconciled, Source: SourceReconciled}, nil |
| } |
| func firstOf(a, b string) string { |
| @@ -282,7 +294,7 @@ func MergeBranch(ctx context.Context, m BranchMerge) (MergeOutcome, error) { |
| return MergeOutcome{}, ErrMergeRefused |
| } |
| if found && rec.Phase == mergeMerged { |
| - return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy}, nil |
| + return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy, Source: SourceCached}, nil |
| } |
| if !found { |
| rec = mergeRecord{Task: m.Task, Repo: m.Repo, Branch: m.Branch, Commit: m.Commit, Phase: mergePrepared, Strategy: m.Strategy} |
| @@ -326,7 +338,7 @@ func MergeBranch(ctx context.Context, m BranchMerge) (MergeOutcome, error) { |
| if err := m.save(rec); err != nil { |
| return MergeOutcome{}, ErrMergeUncertain |
| } |
| - return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy}, nil |
| + return MergeOutcome{SHA: rec.SHA, Into: rec.Into, Strategy: rec.Strategy, Source: SourceHub}, nil |
| } |
| if rerr != nil || res.Reason == "up_to_date" || res.Reason == "moved" || res.Reason == "no_such_branch" || res.Status >= 500 { |
| // No answer, or one that may mean it landed after all: the default |
diff --git a/internal/runner/thin/engineer.go b/internal/runner/thin/engineer.go| index bc1b0ff0..733ccc89 100644 |
| --- a/internal/runner/thin/engineer.go |
| +++ b/internal/runner/thin/engineer.go |
| @@ -63,6 +63,9 @@ type Engineer struct { |
| // AuthorizeStep is that confirmation for one attempt (protocol 2: the |
| // start authorization); ErrWithdrawn means the attempt must not run. |
| AuthorizeStep func(ctx context.Context, step string, attempt int64) error |
| + // ConfirmMerge is the confirmation right before each hub merge request |
| + // (protocol 3): the instance decides the merge again. |
| + ConfirmMerge func(ctx context.Context, step string, attempt int64) error |
| // ConfirmPublication confirms a worker's commit is still to be |
| // published, right before the push. |
| ConfirmPublication func(ctx context.Context, step string, attempt int64, publication string) error |
diff --git a/internal/runner/thin/facts.go b/internal/runner/thin/facts.go| new file mode 100644 |
| index 00000000..f26a3155 |
| --- /dev/null |
| +++ b/internal/runner/thin/facts.go |
| @@ -0,0 +1,217 @@ |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os/exec" |
| + "path" |
| + "strconv" |
| + "strings" |
| + |
| + "amadan.net/oficina/equipo/internal/runner/workspace" |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Merge authority (spec 2026-11-11 §3): what a reviewed change is, |
| +// measured with no model on exactly the reviewed pair, so the instance's |
| +// gate can tell a small, ordinary change from one it must ask about. The |
| +// instance decides; this only measures, and anything it cannot measure |
| +// whole is reported incomplete (the gate then asks). |
| + |
| +// factsOutput bounds each git listing the facts are read from. |
| +const factsOutput = 1 << 20 |
| + |
| +// signingKeys are the Xcode build settings and Info.plist keys whose |
| +// change is a signing or release change. |
| +var signingKeys = []string{"CODE_SIGN", "DEVELOPMENT_TEAM", "PROVISIONING_PROFILE", "ENTITLEMENTS", "PRODUCT_BUNDLE_IDENTIFIER", "MARKETING_VERSION", "CURRENT_PROJECT_VERSION", "CFBundleVersion", "CFBundleShortVersionString"} |
| + |
| +// gitModes are the object modes git writes. |
| +var gitModes = map[string]bool{"000000": true, "100644": true, "100755": true, "120000": true, "160000": true, "040000": true} |
| + |
| +// changeFacts measures base..sha in the tree's private repository. |
| +func changeFacts(ctx context.Context, git, gitDir string, r stepRow) *wire.ChangeFacts { |
| + f := &wire.ChangeFacts{ReviewStep: r.StepID, Attempt: r.Attempt, Repo: r.Repo, Base: r.BaseSHA, SHA: r.CandidateSHA, Entries: []wire.ChangeEntry{}} |
| + if !shaPattern.MatchString(r.BaseSHA) || !shaPattern.MatchString(r.CandidateSHA) { |
| + return f |
| + } |
| + run := func(args ...string) (string, bool) { |
| + cmd := exec.CommandContext(ctx, git, append([]string{"--git-dir=" + gitDir, "-c", "core.quotePath=false"}, args...)...) |
| + env := workspace.GitEnvironment() |
| + env["HOME"] = "/nonexistent" |
| + for k, v := range env { |
| + cmd.Env = append(cmd.Env, k+"="+v) |
| + } |
| + out, clipped, err := boundedOutput(cmd, factsOutput) |
| + return string(out), err == nil && !clipped |
| + } |
| + if _, ok := run("merge-base", "--is-ancestor", r.BaseSHA, r.CandidateSHA); !ok { |
| + return f |
| + } |
| + raw, ok1 := run("diff-tree", "-r", "-z", "--raw", "-M", "--no-abbrev", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) |
| + num, ok2 := run("diff-tree", "-r", "-z", "--numstat", "-M", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) |
| + if !ok1 || !ok2 { |
| + return f |
| + } |
| + entries, ok := parseRaw(raw) |
| + if !ok || len(entries) > wire.MaxChangeEntries { |
| + return f |
| + } |
| + stats, ok := parseNumstat(num) |
| + if !ok || len(stats) != len(entries) { |
| + return f |
| + } |
| + var xcode []string |
| + for i, e := range entries { |
| + s := stats[i] |
| + if s.path != e.New || s.old != e.Old { |
| + return f |
| + } |
| + if s.binary { |
| + f.Binary++ |
| + } else { |
| + f.Lines += s.added + s.deleted |
| + } |
| + if e.OldMode == "120000" || e.NewMode == "120000" || e.OldMode == "160000" || e.NewMode == "160000" || |
| + e.OldMode != "000000" && e.NewMode != "000000" && e.OldMode != e.NewMode || e.OldMode == "000000" && e.NewMode == "100755" { |
| + f.Special = true |
| + } |
| + for _, p := range []string{e.Old, e.New} { |
| + if p != "" && (path.Ext(p) == ".pbxproj" || path.Ext(p) == ".xcconfig" || path.Base(p) == "Info.plist") { |
| + xcode = append(xcode, p) |
| + } |
| + } |
| + } |
| + if len(xcode) > 0 { |
| + patch, ok := run(append([]string{"diff-tree", "-r", "-p", "-U0", "-M", "--no-ext-diff", "--no-textconv", "--no-color", r.BaseSHA, r.CandidateSHA, "--"}, xcode...)...) |
| + if !ok { |
| + return f |
| + } |
| + f.Signing = signingChange(patch) |
| + } |
| + f.Entries, f.Files = entries, len(entries) |
| + if r.RevertSHA != "" { |
| + f.Revert = revertCheck(run, r.CandidateSHA, r.RevertSHA) |
| + } |
| + f.Complete = true |
| + return f |
| +} |
| + |
| +// revertCheck says whether candidate sits right on target and has its |
| +// first parent's tree: an exact revert of target, nothing more. |
| +func revertCheck(run func(...string) (string, bool), candidate, target string) *wire.RevertCheck { |
| + c := &wire.RevertCheck{SHA: target} |
| + if !shaPattern.MatchString(target) { |
| + return c |
| + } |
| + parent, ok := run("rev-parse", "--verify", "--end-of-options", candidate+"^1") |
| + if !ok || strings.TrimSpace(parent) != target { |
| + return c |
| + } |
| + c.OnTop = true |
| + got, ok1 := run("rev-parse", "--verify", "--end-of-options", candidate+"^{tree}") |
| + want, ok2 := run("rev-parse", "--verify", "--end-of-options", target+"^1^{tree}") |
| + c.Exact = ok1 && ok2 && strings.TrimSpace(got) != "" && strings.TrimSpace(got) == strings.TrimSpace(want) |
| + return c |
| +} |
| + |
| +// parseRaw reads `diff-tree -z --raw`: ":old new oldsha newsha status" |
| +// then one path, or two for a rename or copy. |
| +func parseRaw(out string) ([]wire.ChangeEntry, bool) { |
| + toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") |
| + if out == "" { |
| + return []wire.ChangeEntry{}, true |
| + } |
| + var es []wire.ChangeEntry |
| + for i := 0; i < len(toks); { |
| + head := toks[i] |
| + if !strings.HasPrefix(head, ":") { |
| + return nil, false |
| + } |
| + fs := strings.Fields(head[1:]) |
| + if len(fs) != 5 || !gitModes[fs[0]] || !gitModes[fs[1]] || fs[4] == "" { |
| + return nil, false |
| + } |
| + e := wire.ChangeEntry{Status: fs[4], OldMode: fs[0], NewMode: fs[1]} |
| + i++ |
| + two := e.Status[0] == 'R' || e.Status[0] == 'C' |
| + if two { |
| + if i+1 >= len(toks) { |
| + return nil, false |
| + } |
| + e.Old, e.New = toks[i], toks[i+1] |
| + i += 2 |
| + } else { |
| + if i >= len(toks) { |
| + return nil, false |
| + } |
| + e.New = toks[i] |
| + i++ |
| + } |
| + if !strings.ContainsRune("ACDMRTUX", rune(e.Status[0])) || e.New == "" { |
| + return nil, false |
| + } |
| + es = append(es, e) |
| + } |
| + return es, true |
| +} |
| + |
| +type numstat struct { |
| + added, deleted int |
| + binary bool |
| + old, path string |
| +} |
| + |
| +// parseNumstat reads `diff-tree -z --numstat`: "added\tdeleted\tpath", |
| +// or "added\tdeleted\t" then old and new paths for a rename or copy. |
| +func parseNumstat(out string) ([]numstat, bool) { |
| + if out == "" { |
| + return []numstat{}, true |
| + } |
| + toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") |
| + var ss []numstat |
| + for i := 0; i < len(toks); { |
| + parts := strings.SplitN(toks[i], "\t", 3) |
| + if len(parts) != 3 { |
| + return nil, false |
| + } |
| + var s numstat |
| + if parts[0] == "-" && parts[1] == "-" { |
| + s.binary = true |
| + } else { |
| + a, err1 := strconv.Atoi(parts[0]) |
| + d, err2 := strconv.Atoi(parts[1]) |
| + if err := errors.Join(err1, err2); err != nil || a < 0 || d < 0 { |
| + return nil, false |
| + } |
| + s.added, s.deleted = a, d |
| + } |
| + i++ |
| + if parts[2] == "" { |
| + if i+1 >= len(toks) { |
| + return nil, false |
| + } |
| + s.old, s.path = toks[i], toks[i+1] |
| + i += 2 |
| + } else { |
| + s.path = parts[2] |
| + } |
| + ss = append(ss, s) |
| + } |
| + return ss, true |
| +} |
| + |
| +// signingChange reports whether an added or removed line of a patch |
| +// names a signing or version key. |
| +func signingChange(patch string) bool { |
| + for _, line := range strings.Split(patch, "\n") { |
| + if !strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "-") || strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") { |
| + continue |
| + } |
| + for _, k := range signingKeys { |
| + if strings.Contains(line, k) { |
| + return true |
| + } |
| + } |
| + } |
| + return false |
| +} |
diff --git a/internal/runner/thin/facts_test.go b/internal/runner/thin/facts_test.go| new file mode 100644 |
| index 00000000..632a0877 |
| --- /dev/null |
| +++ b/internal/runner/thin/facts_test.go |
| @@ -0,0 +1,184 @@ |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// factsRepo is a scratch repository with one commit; git runs in it. |
| +func factsRepo(t *testing.T) (dir string, git func(...string) string) { |
| + t.Helper() |
| + dir = t.TempDir() |
| + git = func(args ...string) string { |
| + t.Helper() |
| + cmd := exec.Command("/usr/bin/git", append([]string{"-C", dir, "-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false"}, args...)...) |
| + cmd.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null"} |
| + out, err := cmd.CombinedOutput() |
| + if err != nil { |
| + t.Fatalf("git %v: %v\n%s", args, err, out) |
| + } |
| + return strings.TrimSpace(string(out)) |
| + } |
| + git("init", "-q", "-b", "main") |
| + writeFile(t, dir, "README.md", "hello\n") |
| + writeFile(t, dir, "Sources/Pane.swift", "struct Pane {}\n") |
| + writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n};\n") |
| + writeFile(t, dir, "assets/icon.png", "\x89PNG\x00\x01") |
| + git("add", "-A") |
| + git("commit", "-q", "-m", "base") |
| + return dir, git |
| +} |
| + |
| +func writeFile(t *testing.T, dir, name, text string) { |
| + t.Helper() |
| + p := filepath.Join(dir, name) |
| + if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(p, []byte(text), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func measure(t *testing.T, dir, base, sha, revert string) *wire.ChangeFacts { |
| + t.Helper() |
| + r := stepRow{DispatchCommand: wire.DispatchCommand{StepID: "s_r", Attempt: 2, Repo: "amadan.net/oficina/sample", BaseSHA: base, CandidateSHA: sha, RevertSHA: revert}} |
| + return changeFacts(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), r) |
| +} |
| + |
| +// TestReviewReportsChangeFacts: the runner measures exactly the reviewed |
| +// pair: lines and files, both paths of a rename, binary files, symlinks, |
| +// submodules, mode changes, new executables and signing lines, and says |
| +// incomplete when it cannot measure the whole. |
| +func TestReviewReportsChangeFacts(t *testing.T) { |
| + dir, git := factsRepo(t) |
| + base := git("rev-parse", "HEAD") |
| + |
| + // An ordinary change: one edit, one rename, one new file. |
| + writeFile(t, dir, "Sources/Pane.swift", "struct Pane {\n var edge = true\n}\n") |
| + os.MkdirAll(filepath.Join(dir, "docs"), 0o700) |
| + git("mv", "README.md", "docs/README.md") |
| + writeFile(t, dir, "Sources/Edge.swift", "let edge = 1\n") |
| + writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n F1 = {isa = PBXFileReference; path = Edge.swift; };\n};\n") |
| + git("add", "-A") |
| + git("commit", "-q", "-m", "change") |
| + cand := git("rev-parse", "HEAD") |
| + f := measure(t, dir, base, cand, "") |
| + if !f.Complete || f.Files != 4 || f.Binary != 0 || f.Special || f.Signing || f.ReviewStep != "s_r" || f.Attempt != 2 || f.SHA != cand || f.Base != base { |
| + t.Fatalf("ordinary %+v", f) |
| + } |
| + if f.Lines != 1+3+1+1 { |
| + t.Fatalf("lines %d (%+v)", f.Lines, f.Entries) |
| + } |
| + renamed := false |
| + for _, e := range f.Entries { |
| + if strings.HasPrefix(e.Status, "R") && e.Old == "README.md" && e.New == "docs/README.md" { |
| + renamed = true |
| + } |
| + } |
| + if !renamed { |
| + t.Fatalf("rename paths %+v", f.Entries) |
| + } |
| + |
| + // Signing lines in the project file. |
| + writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n DEVELOPMENT_TEAM = ABC123;\n};\n") |
| + git("commit", "-q", "-am", "team") |
| + if f := measure(t, dir, cand, git("rev-parse", "HEAD"), ""); !f.Complete || !f.Signing { |
| + t.Fatalf("signing %+v", f) |
| + } |
| + prev := git("rev-parse", "HEAD") |
| + |
| + // A binary file, a mode change, a symlink and a new executable. |
| + writeFile(t, dir, "assets/icon.png", "\x89PNG\x00\x02\x03") |
| + git("commit", "-q", "-am", "icon") |
| + if f := measure(t, dir, prev, git("rev-parse", "HEAD"), ""); f.Binary != 1 || f.Lines != 0 || f.Special { |
| + t.Fatalf("binary %+v", f) |
| + } |
| + for name, change := range map[string]func(){ |
| + "mode": func() { git("update-index", "--chmod=+x", "Sources/Pane.swift") }, |
| + "symlink": func() { os.Symlink("Sources/Pane.swift", filepath.Join(dir, "link")); git("add", "link") }, |
| + "executable": func() { |
| + writeFile(t, dir, "run.sh", "#!/bin/sh\n") |
| + git("add", "run.sh") |
| + git("update-index", "--chmod=+x", "run.sh") |
| + }, |
| + "submodule": func() { git("update-index", "--add", "--cacheinfo", "160000,"+base+",vendored") }, |
| + } { |
| + before := git("rev-parse", "HEAD") |
| + change() |
| + git("commit", "-q", "-m", name) |
| + if f := measure(t, dir, before, git("rev-parse", "HEAD"), ""); !f.Complete || !f.Special { |
| + t.Errorf("%s not special: %+v", name, f) |
| + } |
| + } |
| + |
| + // Not a descendant of the base, or a base that is not a SHA: |
| + // incomplete. |
| + git("checkout", "-q", "-f", "-b", "other", base) |
| + writeFile(t, dir, "x.txt", "x\n") |
| + git("add", "x.txt") |
| + git("commit", "-q", "-m", "other") |
| + if f := measure(t, dir, cand, git("rev-parse", "HEAD"), ""); f.Complete { |
| + t.Fatalf("unrelated pair complete %+v", f) |
| + } |
| + if f := measure(t, dir, "main", cand, ""); f.Complete { |
| + t.Fatalf("a ref measured %+v", f) |
| + } |
| + |
| + // More entries than the bound: incomplete. |
| + git("checkout", "-q", "-f", "main") |
| + before := git("rev-parse", "HEAD") |
| + for i := 0; i <= wire.MaxChangeEntries; i++ { |
| + writeFile(t, dir, filepath.Join("many", "f"+itoa(int64(i))+".txt"), "x\n") |
| + } |
| + git("add", "-A") |
| + git("commit", "-q", "-m", "many") |
| + if f := measure(t, dir, before, git("rev-parse", "HEAD"), ""); f.Complete { |
| + t.Fatalf("over the bound complete: %d", f.Files) |
| + } |
| +} |
| + |
| +// TestRevertCheck: a revert task's candidate is exact only when it sits |
| +// right on the merge commit and has that commit's first parent's tree. |
| +func TestRevertCheck(t *testing.T) { |
| + dir, git := factsRepo(t) |
| + base := git("rev-parse", "HEAD") |
| + git("checkout", "-q", "-b", "task") |
| + writeFile(t, dir, "Sources/Pane.swift", "struct Pane { var edge = true }\n") |
| + git("commit", "-q", "-am", "task") |
| + git("checkout", "-q", "main") |
| + git("merge", "-q", "--no-ff", "-m", "merge task", "task") |
| + merge := git("rev-parse", "HEAD") |
| + |
| + git("revert", "--no-edit", "-m", "1", merge) |
| + exact := git("rev-parse", "HEAD") |
| + if f := measure(t, dir, merge, exact, merge); f.Revert == nil || !f.Revert.OnTop || !f.Revert.Exact || f.Revert.SHA != merge { |
| + t.Fatalf("exact revert %+v", f.Revert) |
| + } |
| + // The revert plus another edit is not exact. |
| + writeFile(t, dir, "README.md", "hello again\n") |
| + git("commit", "-q", "-a", "--amend", "--no-edit") |
| + more := git("rev-parse", "HEAD") |
| + if f := measure(t, dir, merge, more, merge); f.Revert == nil || !f.Revert.OnTop || f.Revert.Exact { |
| + t.Fatalf("revert and more %+v", f.Revert) |
| + } |
| + // Main moved on before the revert: not on top. |
| + git("reset", "-q", "--hard", merge) |
| + writeFile(t, dir, "NEWS", "news\n") |
| + git("add", "NEWS") |
| + git("commit", "-q", "-m", "news") |
| + git("revert", "--no-edit", "-m", "1", merge) |
| + late := git("rev-parse", "HEAD") |
| + if f := measure(t, dir, merge, late, merge); f.Revert == nil || f.Revert.OnTop || f.Revert.Exact { |
| + t.Fatalf("not on top %+v", f.Revert) |
| + } |
| + if f := measure(t, dir, base, exact, ""); f.Revert != nil { |
| + t.Fatalf("a revert check without a target %+v", f.Revert) |
| + } |
| +} |
diff --git a/internal/runner/thin/judge.go b/internal/runner/thin/judge.go| index 05ca6dc4..6fc37cd8 100644 |
| --- a/internal/runner/thin/judge.go |
| +++ b/internal/runner/thin/judge.go |
| @@ -104,7 +104,7 @@ func (e *Engineer) lease(ctx context.Context, r stepRow) error { |
| if err == nil { |
| return nil |
| } |
| - if errors.Is(err, ErrWithdrawn) { |
| + if errors.Is(err, ErrWithdrawn) || errors.Is(err, ErrMergeNotAuthorized) { |
| return err |
| } |
| select { |
| @@ -787,6 +787,9 @@ func (e *Engineer) review(ctx context.Context, r stepRow, progress func(wire.Ste |
| rep.State, rep.Verdict, rep.ReviewLabel, rep.Findings = wire.StepDone, verdict, label, findings |
| // The person can ask Review about its verdict. |
| rep.Askable = kept |
| + // What the reviewed change is, measured on exactly this pair for the |
| + // instance's merge authority (sent only at protocol 3). |
| + rep.Change = changeFacts(context.WithoutCancel(ctx), tree.Git, tree.GitDir, r) |
| } |
| return rep |
| } |
diff --git a/internal/runner/thin/link.go b/internal/runner/thin/link.go| index 6b7360a4..b99ed0b2 100644 |
| --- a/internal/runner/thin/link.go |
| +++ b/internal/runner/thin/link.go |
| @@ -220,6 +220,10 @@ func (l *Link) Confirm(ctx context.Context) error { |
| // may run; the runner ends it. |
| var ErrWithdrawn = errors.New("the instance withdrew this attempt") |
| +// ErrMergeNotAuthorized means the instance refused a merge request's |
| +// confirmation (merge authority): nothing may be sent to the hub. |
| +var ErrMergeNotAuthorized = errors.New("the instance did not authorize this merge request") |
| + |
| // ConfirmStep is a step attempt's start authorization at protocol 2: the |
| // instance grants it only while the attempt is placed on this runner and |
| // not withdrawn (ErrWithdrawn). At protocol 1 it is the plain lease. |
| @@ -227,6 +231,14 @@ func (l *Link) ConfirmStep(ctx context.Context, step string, attempt int64) erro |
| return l.lease(ctx, wire.LeaseCheck{StepID: step, Attempt: attempt}) |
| } |
| +// ConfirmMerge is the confirmation right before a hub merge request |
| +// (protocol 3, purpose merge_request): the instance decides the merge |
| +// again and grants it only if it may go now. At a lower protocol the |
| +// purpose is dropped by Encode and the instance treats it accordingly. |
| +func (l *Link) ConfirmMerge(ctx context.Context, step string, attempt int64) error { |
| + return l.lease(ctx, wire.LeaseCheck{StepID: step, Attempt: attempt, Purpose: wire.LeasePurposeMerge}) |
| +} |
| + |
| // ConfirmPublication is the primary's confirmation right before it pushes |
| // a worker's commit: refused (ErrWithdrawn) once the publication is no |
| // longer pending (cancelled, superseded). |
| @@ -262,10 +274,7 @@ func (l *Link) lease(ctx context.Context, check wire.LeaseCheck) error { |
| } |
| select { |
| case refused := <-done: |
| - if refused != "" { |
| - return ErrWithdrawn |
| - } |
| - return nil |
| + return leaseRefusal(refused) |
| case <-ctx.Done(): |
| return ctx.Err() |
| case <-time.After(15 * time.Second): |
| @@ -273,6 +282,19 @@ func (l *Link) lease(ctx context.Context, check wire.LeaseCheck) error { |
| } |
| } |
| +// leaseRefusal is a lease answer as an error: nil when granted, |
| +// ErrMergeNotAuthorized when the instance would not let a merge request |
| +// go, ErrWithdrawn for any other refusal. |
| +func leaseRefusal(refused string) error { |
| + switch refused { |
| + case "": |
| + return nil |
| + case wire.RefusedMergeNotAuthorized: |
| + return ErrMergeNotAuthorized |
| + } |
| + return ErrWithdrawn |
| +} |
| + |
| // resolve answers a waiting lease: "" granted, else the refusal. |
| func (l *Link) resolve(id, refused string) bool { |
| l.mu.Lock() |
diff --git a/internal/runner/thin/mergestep.go b/internal/runner/thin/mergestep.go| index 6ae8cef0..366e607b 100644 |
| --- a/internal/runner/thin/mergestep.go |
| +++ b/internal/runner/thin/mergestep.go |
| @@ -49,10 +49,27 @@ func (e *Engineer) mergeStep(ctx context.Context, r stepRow, progress func(wire. |
| return fail("policy_rejected") |
| } |
| if err := e.lease(ctx, r); err != nil { |
| + if errors.Is(err, ErrMergeNotAuthorized) { |
| + return fail("merge_not_authorized") |
| + } |
| rep.State, rep.Reason = wire.StepCancelled, leaseReason(err) |
| return rep |
| } |
| progress(rep) |
| + // Merge authority: right before every hub request the instance decides |
| + // again; a refusal means nothing is sent and the step ends. |
| + confirm := e.confirmFor(r) |
| + if e.ConfirmMerge != nil { |
| + confirm = func(ctx context.Context) error { return e.ConfirmMerge(ctx, r.StepID, r.Attempt) } |
| + } |
| + notAuthorized := false |
| + confirmMerge := func(ctx context.Context) error { |
| + err := confirm(ctx) |
| + if errors.Is(err, ErrMergeNotAuthorized) { |
| + notAuthorized = true |
| + } |
| + return err |
| + } |
| host := e.Host |
| if host == "" { |
| host = "runner" |
| @@ -66,7 +83,7 @@ func (e *Engineer) mergeStep(ctx context.Context, r stepRow, progress func(wire. |
| Request: func(ctx context.Context, req forge.HubMergeRequest) (forge.HubMergeResult, error) { |
| return e.Forge.Merge(ctx, r.Repo, r.Branch, req) |
| }, |
| - Confirm: e.confirmFor(r)} |
| + Confirm: confirmMerge} |
| // Once the request may be with the hub, a cancel never stops the |
| // merge from being settled: the forge calls outlive the step's context |
| // (the instance refuses cancels while merging; shutdown keeps the |
| @@ -78,11 +95,14 @@ func (e *Engineer) mergeStep(ctx context.Context, r stepRow, progress func(wire. |
| cctx, cancel := context.WithTimeout(mctx, 3*time.Minute) |
| out, err = forge.MergeBranch(cctx, m) |
| cancel() |
| - if !errors.Is(err, forge.ErrMergeUncertain) || uncertain+1 >= mergeAttempts { |
| + if notAuthorized || !errors.Is(err, forge.ErrMergeUncertain) || uncertain+1 >= mergeAttempts { |
| break |
| } |
| time.Sleep(time.Duration(uncertain+1) * mergeBackoff) |
| } |
| + if notAuthorized && err != nil { |
| + return fail("merge_not_authorized") |
| + } |
| if errors.Is(err, forge.ErrMergeCIPending) { |
| // The hub's own CI is still running: held for later, releasing |
| // the queue, and run afresh then (lease, owner, judged SHA). |
| @@ -98,6 +118,13 @@ func (e *Engineer) mergeStep(ctx context.Context, r stepRow, progress func(wire. |
| // report says it landed (with the commit and how). |
| e.log("merge watch", e.O.State.endWatch(context.WithoutCancel(ctx), r.TaskID, r.CandidateSHA)) |
| rep.State, rep.MergeSHA, rep.Into, rep.Strategy = wire.StepDone, out.SHA, out.Into, out.Strategy |
| + // Where the SHA came from, and its parents when the hub said it: the |
| + // instance offers Undo only for a merge commit the hub named whose |
| + // second parent is the candidate. |
| + rep.MergeSource = out.Source |
| + if out.Source == forge.SourceHub { |
| + rep.MergeParents = e.mergeParents(mctx, r.Repo, out.SHA) |
| + } |
| return rep |
| case errors.Is(err, forge.ErrMergeConflict): |
| return fail("merge_conflict") |
| @@ -116,6 +143,39 @@ func (e *Engineer) mergeStep(ctx context.Context, r stepRow, progress func(wire. |
| return fail("merge_failed") |
| } |
| +// mergeParents reads a merge commit's parents from the fetched clone, |
| +// nil when it cannot. |
| +func (e *Engineer) mergeParents(ctx context.Context, repo, sha string) []string { |
| + if !shaPattern.MatchString(sha) { |
| + return nil |
| + } |
| + cctx, cancel := context.WithTimeout(ctx, fetchTimeout) |
| + defer cancel() |
| + if err := e.fetch(cctx, repo); err != nil { |
| + return nil |
| + } |
| + cmd := exec.CommandContext(cctx, e.O.Runner.GitPath, "--git-dir="+e.Clones.Path(repo), "rev-list", "--parents", "-n", "1", "--end-of-options", sha) |
| + env := workspace.GitEnvironment() |
| + env["HOME"] = "/nonexistent" |
| + for k, v := range env { |
| + cmd.Env = append(cmd.Env, k+"="+v) |
| + } |
| + out, clipped, err := boundedOutput(cmd, 4096) |
| + if err != nil || clipped { |
| + return nil |
| + } |
| + f := strings.Fields(string(out)) |
| + if len(f) < 2 || f[0] != sha { |
| + return nil |
| + } |
| + for _, p := range f[1:] { |
| + if !shaPattern.MatchString(p) { |
| + return nil |
| + } |
| + } |
| + return f[1:] |
| +} |
| + |
| // inspectMerge reads the repository for a merge: the task branch's head |
| // and the default branch's tip (one ls-remote), and, fetching only when |
| // the tip is not here yet, whether commit is on the default branch and |
diff --git a/internal/runner/thin/mergestep_test.go b/internal/runner/thin/mergestep_test.go| index f20e2a96..4eef562e 100644 |
| --- a/internal/runner/thin/mergestep_test.go |
| +++ b/internal/runner/thin/mergestep_test.go |
| @@ -320,3 +320,77 @@ func TestMergeStepSettlesDespiteCancel(t *testing.T) { |
| t.Fatalf("cancelled mid-merge: %+v (%d requests)", rep, f.requests) |
| } |
| } |
| + |
| +// TestMergeConfirmPurpose: right before each hub request the runner asks |
| +// the instance to confirm the merge itself (ConfirmMerge, the |
| +// merge_request purpose); a refusal ends the step merge_not_authorized |
| +// with nothing sent and the forge record still prepared, and a refused |
| +// start lease ends it the same way. |
| +func TestMergeConfirmPurpose(t *testing.T) { |
| + e, f, r, _ := hubRig(t) |
| + var asked []string |
| + e.ConfirmMerge = func(_ context.Context, step string, attempt int64) error { |
| + asked = append(asked, step+"/"+itoa(attempt)) |
| + return ErrMergeNotAuthorized |
| + } |
| + rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| + if rep.State != wire.StepFailed || rep.Reason != "merge_not_authorized" || !rep.Termination || f.requests != 0 { |
| + t.Fatalf("refused %+v (%d requests)", rep, f.requests) |
| + } |
| + if len(asked) != 1 || asked[0] != "s1/1" { |
| + t.Fatalf("confirmations %v", asked) |
| + } |
| + records, _ := filepath.Glob(filepath.Join(e.root(), "merge", "merge-*.json")) |
| + if len(records) != 1 { |
| + t.Fatalf("merge records %v", records) |
| + } |
| + if raw, _ := os.ReadFile(records[0]); !strings.Contains(string(raw), `"prepared"`) { |
| + t.Fatalf("a refused merge's record: %s", raw) |
| + } |
| + // Granted: the merge goes, after exactly one confirmation per request. |
| + asked = nil |
| + e.ConfirmMerge = func(_ context.Context, step string, attempt int64) error { |
| + asked = append(asked, step) |
| + return nil |
| + } |
| + if rep := e.Exec(context.Background(), r, func(wire.StepReport) {}); rep.State != wire.StepDone || f.requests != 1 || len(asked) != 1 { |
| + t.Fatalf("granted %+v (%d requests, %v)", rep, f.requests, asked) |
| + } |
| + // A refused start lease: the same reason, nothing sent. |
| + e2, f2, r2, _ := hubRig(t) |
| + e2.AuthorizeStep = func(context.Context, string, int64) error { return ErrMergeNotAuthorized } |
| + if rep := e2.Exec(context.Background(), r2, func(wire.StepReport) {}); rep.State != wire.StepFailed || rep.Reason != "merge_not_authorized" || f2.requests != 0 { |
| + t.Fatalf("start refused %+v", rep) |
| + } |
| +} |
| + |
| +// TestMergeSource: the report says where the merge SHA came from. The |
| +// hub's own answer carries the merge commit's parents (second parent the |
| +// candidate); a reconciled or cached landing carries none. |
| +func TestMergeSource(t *testing.T) { |
| + e, f, r, cand := hubRig(t) |
| + rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| + if rep.State != wire.StepDone || rep.MergeSource != wire.MergeSourceHub || len(rep.MergeParents) != 2 || rep.MergeParents[1] != cand || f.requests != 1 { |
| + t.Fatalf("hub %+v", rep) |
| + } |
| + if again := e.Exec(context.Background(), r, func(wire.StepReport) {}); again.MergeSource != wire.MergeSourceCached || again.MergeParents != nil { |
| + t.Fatalf("cached %+v", again) |
| + } |
| + e2, f2, r2, _ := hubRig(t) |
| + f2.answer = func(n int, merge func() (forge.HubMergeResult, error)) (forge.HubMergeResult, error) { |
| + merge() |
| + return forge.HubMergeResult{}, forge.ErrMergeUncertain |
| + } |
| + if rep := e2.Exec(context.Background(), r2, func(wire.StepReport) {}); rep.State != wire.StepDone || rep.MergeSource != wire.MergeSourceReconciled || rep.MergeParents != nil { |
| + t.Fatalf("reconciled %+v", rep) |
| + } |
| +} |
| + |
| +// TestLeaseRefusalReasons: the instance's merge_not_authorized refusal is |
| +// its own error, so the merge step ends with that reason; any other |
| +// refusal is a withdrawal. |
| +func TestLeaseRefusalReasons(t *testing.T) { |
| + if leaseRefusal("") != nil || !errors.Is(leaseRefusal(wire.RefusedMergeNotAuthorized), ErrMergeNotAuthorized) || !errors.Is(leaseRefusal("withdrawn"), ErrWithdrawn) { |
| + t.Fatal("lease refusals") |
| + } |
| +} |
diff --git a/internal/runner/thin/protocol_test.go b/internal/runner/thin/protocol_test.go| index c233794c..52a95042 100644 |
| --- a/internal/runner/thin/protocol_test.go |
| +++ b/internal/runner/thin/protocol_test.go |
| @@ -57,7 +57,7 @@ func TestLinkFallsBackToProtocol1(t *testing.T) { |
| link, _, _, _ := startLink(t, inst.url, &stubCoordinator{}, func(*State) {}) |
| link.Logger = slog.New(slog.NewTextHandler(logs, nil)) |
| first := <-inst.conns |
| - if refused, raw := olderInstance(t, first); !refused || !strings.Contains(string(raw), `"protocol":2`) { |
| + if refused, raw := olderInstance(t, first); !refused || !strings.Contains(string(raw), `"protocol":`+itoa(int64(wire.ProtocolCurrent))) { |
| t.Fatalf("first hello was not protocol 2: %s", raw) |
| } |
| second := <-inst.conns |
| @@ -121,7 +121,7 @@ func TestLinkSpeaksProtocol2(t *testing.T) { |
| var f wire.Frame |
| _ = json.Unmarshal(raw, &f) |
| _, body, _ := wire.ParseFrame(raw) |
| - if body.(*wire.Hello).Protocol != wire.Protocol2 { |
| + if body.(*wire.Hello).Protocol != wire.ProtocolCurrent { |
| t.Fatalf("hello %s", raw) |
| } |
| write(t, c, wiretest.Fixture("frames/welcome-worker.json")) |
diff --git a/internal/runner/thin/run.go b/internal/runner/thin/run.go| index 98c2e132..abcb88eb 100644 |
| --- a/internal/runner/thin/run.go |
| +++ b/internal/runner/thin/run.go |
| @@ -268,7 +268,7 @@ func Run(ctx context.Context, c runner.Config, o Options) error { |
| if err = engine.Start(ctx); err != nil { |
| return err |
| } |
| - engineer := &Engineer{O: o.Claude, Codex: o.Codex, Clones: clones, Forge: fg, Authorize: link.Confirm, AuthorizeStep: link.ConfirmStep, ConfirmPublication: link.ConfirmPublication, Owners: withPerson(coordinatorPersons(c)(), creds.Bearer.Person), MaxStep: o.MaxStep, Note: o.StepNote, Healthy: conns.Usable, Conns: conns, Delta: link.Delta, Activity: link.Activity, StepActivity: link.StepActivity, Retention: o.KeptRetention, Host: c.MachineLabel} |
| + engineer := &Engineer{O: o.Claude, Codex: o.Codex, Clones: clones, Forge: fg, Authorize: link.Confirm, AuthorizeStep: link.ConfirmStep, ConfirmMerge: link.ConfirmMerge, ConfirmPublication: link.ConfirmPublication, Owners: withPerson(coordinatorPersons(c)(), creds.Bearer.Person), MaxStep: o.MaxStep, Note: o.StepNote, Healthy: conns.Usable, Conns: conns, Delta: link.Delta, Activity: link.Activity, StepActivity: link.StepActivity, Retention: o.KeptRetention, Host: c.MachineLabel} |
| link.Forget = engineer.RemoveKept |
| merges := &MergeWatch{State: state, Clones: clones, Git: c.GitPath, Notify: link.Notify, Logger: o.Logger, Every: o.MergeEvery} |
| if merges.Every <= 0 { |
diff --git a/internal/wire/frames.go b/internal/wire/frames.go| index 91bcddba..7eeb45db 100644 |
| --- a/internal/wire/frames.go |
| +++ b/internal/wire/frames.go |
| @@ -373,6 +373,10 @@ type LeaseCheck struct { |
| // request. |
| const LeasePurposeMerge = "merge_request" |
| +// RefusedMergeNotAuthorized is a lease refusal's reason when the instance |
| +// will not let a merge request go (merge authority). |
| +const RefusedMergeNotAuthorized = "merge_not_authorized" |
| + |
| // Sync is sent on every connect, after hello. |
| type Sync struct { |
| Sessions []SessionState `json:"sessions"` |
diff --git a/internal/wire/runners.go b/internal/wire/runners.go| index e6c78a26..d220d410 100644 |
| --- a/internal/wire/runners.go |
| +++ b/internal/wire/runners.go |
| @@ -13,16 +13,19 @@ import ( |
| // Runner stream protocols. 1 is the slice 4 wire: a hello without |
| // protocol. 2 adds roles, capabilities, placement, the attempt-specific |
| // start authorization, pack transfers and publication (Mac runners spec |
| -// §3.3). 3 adds what merge authority needs (spec 2026-11-11): the |
| -// review's change facts, the merge's provenance, the merge request's own |
| -// confirmation and a revert's target. The instance speaks the lower of |
| -// the two on each connection and encodes every frame for it with Encode; |
| -// nothing a runner does not know ever reaches it. |
| +// §3.3). ProtocolMergeFacts adds what merge authority needs (spec |
| +// 2026-11-11): the review's change facts, the merge's provenance, the |
| +// merge request's own confirmation naming the attempt, and a revert's |
| +// target. Code asks for that capability by name, never by number. The |
| +// instance speaks the lower of the two on each connection and encodes |
| +// every frame for it with Encode; nothing a runner does not know ever |
| +// reaches it. |
| const ( |
| - Protocol1 = 1 |
| - Protocol2 = 2 |
| - Protocol3 = 3 |
| - ProtocolCurrent = Protocol3 |
| + Protocol1 = 1 |
| + Protocol2 = 2 |
| + Protocol3 = 3 |
| + ProtocolMergeFacts = Protocol3 |
| + ProtocolCurrent = ProtocolMergeFacts |
| ) |
| // Runner roles. A person's first runner is the primary: the coordinator, |
| @@ -347,9 +350,9 @@ func Speaks(protocol int, kind string) bool { return protocol >= Protocol2 || !p |
| var ErrProtocol = errors.New("wire: frame not in this protocol") |
| // Encode returns f as the given protocol speaks it. At protocol 1 it is |
| -// exactly the slice 4 frame and field set: protocol 2 and 3 fields are |
| -// dropped and a protocol 2 frame type is refused (ErrProtocol). At |
| -// protocol 2 the protocol 3 fields are dropped. |
| +// exactly the slice 4 frame and field set: later fields are dropped and a |
| +// protocol 2 frame type is refused (ErrProtocol). Below |
| +// ProtocolMergeFacts the merge authority fields are dropped. |
| func Encode(f Frame, protocol int) (Frame, error) { |
| if protocol >= ProtocolCurrent { |
| return f, nil |
| @@ -361,7 +364,10 @@ func Encode(f Frame, protocol int) (Frame, error) { |
| if err != nil { |
| return Frame{}, err |
| } |
| - changed := downgrade3(body) |
| + changed := false |
| + if protocol < ProtocolMergeFacts { |
| + changed = downgradeMergeFacts(body) |
| + } |
| if protocol < Protocol2 && downgrade(body) { |
| changed = true |
| } |
| @@ -376,9 +382,9 @@ func Encode(f Frame, protocol int) (Frame, error) { |
| return f, nil |
| } |
| -// downgrade3 clears a body's protocol 3 fields (all omitempty). It |
| -// reports whether the body has any. |
| -func downgrade3(body any) bool { |
| +// downgradeMergeFacts clears a body's ProtocolMergeFacts fields (all |
| +// omitempty). It reports whether the body has any. |
| +func downgradeMergeFacts(body any) bool { |
| switch v := body.(type) { |
| case *DispatchCommand: |
| v.RevertSHA = "" |
diff --git a/internal/wire/runners_test.go b/internal/wire/runners_test.go| index dc4c2901..49c3f3d5 100644 |
| --- a/internal/wire/runners_test.go |
| +++ b/internal/wire/runners_test.go |
| @@ -60,8 +60,8 @@ func TestEncodeProtocol1IsTheSlice4Wire(t *testing.T) { |
| } |
| // Frames protocol 1 does not have are never encoded for it. |
| -// TestWireProtocol3Downgrade: a protocol 3 frame is unchanged at 3; at |
| -// 2 it is exactly its protocol 2 shape (the merge_request purpose, change |
| +// TestWireProtocol3Downgrade: a ProtocolMergeFacts frame is unchanged |
| +// there; one protocol below it is exactly its protocol 2 shape (the merge_request purpose, change |
| // facts, merge provenance and revert target dropped), and at 1 the slice |
| // 4 shape. An old peer's strict decoder never meets a field it refuses. |
| func TestWireProtocol3Downgrade(t *testing.T) { |
| @@ -75,11 +75,11 @@ func TestWireProtocol3Downgrade(t *testing.T) { |
| if err != nil { |
| t.Fatalf("%s: %v", v3, err) |
| } |
| - same, err := wire.Encode(f, wire.Protocol3) |
| + same, err := wire.Encode(f, wire.ProtocolMergeFacts) |
| if err != nil || !bytes.Equal(same.Data, f.Data) { |
| t.Fatalf("%s: protocol 3 changed the frame: %v", v3, err) |
| } |
| - two, err := wire.Encode(f, wire.Protocol2) |
| + two, err := wire.Encode(f, wire.ProtocolMergeFacts-1) |
| if err != nil { |
| t.Fatal(err) |
| } |