oficina / equipo Public

runner: publish a fix round in an empty repository

A fix round's base is the task branch's tip. In a repository whose
default branch is unborn, publish took the unborn default as "main"
only when the step had no base, so round 2 read ErrEmptyRepo, retried
three times and stopped with push_failed without ever pushing
(threatmin's round 2 on staging, s_4rl7n2plt7rnurz7lqd6ytzvdy).

publishDefault names the unborn default main whatever the base, for the
step push and the worker publication alike. The push stays a
fast-forward: forge pushes only a descendant of the base, and replaces
only Equipo's own earlier commit under a lease.

TestEmptyRepoFixRoundPublishes failed first on 4644e8a:

    --- FAIL: TestEmptyRepoFixRoundPublishes (0.43s)
        emptypublish_test.go:64: round 2: {... State:failed
        Reason:push_failed BaseSHA:1dee345935c28b4ca60b13418646a03eab709115
        CandidateSHA:b3bcdafe167e94e9bd0fe58e9a92d5bb4a50efe3 Pushed:false
        LogTail:error: reading the default branch: the repository has no
        commits yet ...}
    FAIL

The headless witness 3_empty_fix_round (a review-asked fix round in an
empty repository, the forge refusing non-fast-forwards) passes; with
the r.Base == "" condition put back, both it (a step stopped with
push_failed) and the unit test fail.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev e7b0365fedcd8ab03add43b9dfd1d7a205cf4f43 parent 4644e8a
5 files changed, +151 −7
  • internal/headless/engineering_test.go +60 −0
  • internal/headless/scenarios_test.go +1 −0
  • internal/runner/thin/emptypublish_test.go +75 −0
  • internal/runner/thin/engineer.go +14 −6
  • internal/runner/thin/relay.go +1 −1
diff --git a/internal/headless/engineering_test.go b/internal/headless/engineering_test.go
index 399343ff..5944a17d 100644
--- a/internal/headless/engineering_test.go
+++ b/internal/headless/engineering_test.go
@@ -397,6 +397,66 @@ func scenarioEmptyEngineering(t *testing.T, env *Env) {
}
}
+// A fix round in a repository with no commits (threatmin's round 2 on
+// staging, which stopped with push_failed): the first round pushes the
+// task branch's initial commit; review asks for one more file and the fix
+// round, its base the task branch's tip, pushes again as a fast-forward
+// of that tip (the forge refuses any other). Both rounds pushed, nothing
+// stopped, and the default branch stays unborn.
+func scenarioEmptyFixRound(t *testing.T, env *Env) {
+ if env.Live || env.GrantEmpty == nil {
+ t.Skip("local forge only")
+ }
+ const repo = "amadan.net/paulca/parlour"
+ env.GrantEmpty(t, repo)
+ bare := env.ForgePath(repo)
+ if out, err := exec.Command("git", "--git-dir="+bare, "config", "receive.denyNonFastForwards", "true").CombinedOutput(); err != nil {
+ t.Fatalf("git config: %v %s", err, out)
+ }
+ s := open(t, env)
+ s.keep()
+ s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online })
+ deadline := time.Now().Add(45 * time.Second)
+ for found, empty := env.Indexed(t, repo); !found || !empty; found, empty = env.Indexed(t, repo) {
+ if time.Now().After(deadline) {
+ t.Fatalf("not catalogued as empty: found %v", found)
+ }
+ time.Sleep(200 * time.Millisecond)
+ }
+ sent := s.send(t, "tool:record_task:paulca/parlour|Create the parlour mockup|One page and its hours.\ntool:start_task:$task|review:need:hours.txt|open late|create=hours.txt~open late; create:index.html|<h1>Parlour</h1>")
+ id := s.taskOf(t, sent.Turn.ID)
+ k := s.settled(t, id, 10*time.Minute)
+ var engs []wire.Step
+ for _, st := range k.Steps {
+ if st.State == wire.StepFailed {
+ t.Fatalf("a step stopped: %+v", st)
+ }
+ if st.Specialist == wire.SpecialistEngineering {
+ engs = append(engs, st)
+ }
+ }
+ t.Logf("task %s: %s needs_you=%q fix rounds %d verdict %s on %s", id, k.Stage, k.NeedsYou, k.FixRounds, k.Verdict, k.CandidateSHA)
+ if len(engs) != 2 || k.FixRounds != 1 {
+ t.Fatalf("engineering rounds %+v (fix rounds %d)", engs, k.FixRounds)
+ }
+ first, fix := engs[0], engs[1]
+ if !first.Pushed || first.BaseSHA != "" || !fix.Pushed || fix.State != wire.StepDone || fix.BaseSHA != first.CandidateSHA || fix.CandidateSHA != k.CandidateSHA {
+ t.Fatalf("rounds: first %+v fix %+v", first, fix)
+ }
+ if got := forgeHead(t, bare, k.Branch); got != fix.CandidateSHA {
+ t.Fatalf("forge branch at %q, want the fix round's %s", got, fix.CandidateSHA)
+ }
+ if parents := strings.TrimSpace(forgeShow(t, bare, "--no-patch", "--format=%P", fix.CandidateSHA)); parents != first.CandidateSHA {
+ t.Fatalf("the fix round's parents %q, want the first round's %s", parents, first.CandidateSHA)
+ }
+ if got := forgeHead(t, bare, "main"); got != "" {
+ t.Fatalf("main was created at %s", got)
+ }
+ if k.Verdict != wire.VerdictApprove || k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" {
+ t.Fatalf("task ended %s, verdict %s", k.Stage, k.Verdict)
+ }
+}
+
// The first merge into an empty repository, through the real runner and
// both hubs. Before the hub update the person's Merge is refused: the
// step stops with merge_unborn_default, the task stays Ready to merge
diff --git a/internal/headless/scenarios_test.go b/internal/headless/scenarios_test.go
index 7713bbcb..46854177 100644
--- a/internal/headless/scenarios_test.go
+++ b/internal/headless/scenarios_test.go
@@ -457,6 +457,7 @@ var scenarios = []struct {
{"3_empty_engineering", scenarioEmptyEngineering},
{"3_empty_merge", scenarioEmptyMerge},
{"3_empty_merge_rebuilt", scenarioEmptyMergeRebuilt},
+ {"3_empty_fix_round", scenarioEmptyFixRound},
{"4_review", scenarioReview},
{"4_checks_honest", scenarioChecksHonest},
{"4_witness", scenarioReviewWitness},
diff --git a/internal/runner/thin/emptypublish_test.go b/internal/runner/thin/emptypublish_test.go
new file mode 100644
index 00000000..0fca1af6
--- /dev/null
+++ b/internal/runner/thin/emptypublish_test.go
@@ -0,0 +1,75 @@
+package thin
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// TestEmptyRepoFixRoundPublishes: in a repository with no commits, the
+// first round pushes the task branch's initial commit, and a fix round
+// (its base the task branch's tip, the default branch still unborn)
+// pushes again as a fast-forward of that tip (the remote refuses any
+// other), with no push_failed; the default branch stays unborn.
+func TestEmptyRepoFixRoundPublishes(t *testing.T) {
+ old := pushBackoff
+ pushBackoff = time.Millisecond
+ t.Cleanup(func() { pushBackoff = old })
+ e, f, r, _ := hubRig(t)
+ dir := filepath.Dir(f.dir)
+ // An empty repository: no refs at all, HEAD naming an unborn main,
+ // and only fast-forwards accepted.
+ empty := filepath.Join(dir, "empty.git")
+ gitOut(t, dir, "init", "-q", "--bare", "-b", "main", empty)
+ gitOut(t, dir, "--git-dir="+empty, "config", "receive.denyNonFastForwards", "true")
+ f.dir = empty
+ r.Specialist, r.Branch = wire.SpecialistEngineering, "equipo/tk1-salon"
+ work := filepath.Join(dir, "salon")
+ os.MkdirAll(work, 0700)
+ commit := func(gitDir, file, text string, parents ...string) string {
+ env := []string{"--git-dir=" + gitDir, "--work-tree=" + work}
+ os.WriteFile(filepath.Join(work, file), []byte(text), 0600)
+ gitOut(t, work, append(env, "add", "-A")...)
+ tree := gitOut(t, work, append(env, "write-tree")...)
+ args := append(env, "commit-tree", tree, "-m", file)
+ for _, p := range parents {
+ args = append(args, "-p", p)
+ }
+ return gitOut(t, work, args...)
+ }
+
+ // Round 1: no base, the repository's initial commit.
+ r1 := r
+ r1.StepID, r1.Base = "s1", ""
+ git1 := e.tree(r1, "").GitDir
+ gitOut(t, dir, "init", "-q", "--bare", git1)
+ r1.Candidate = commit(git1, "index.html", "<h1>Salon</h1>\n")
+ if rep := e.publish(context.Background(), r1, wire.StepReport{}); !rep.Pushed || rep.State != wire.StepDone {
+ t.Fatalf("round 1: %+v", rep)
+ }
+
+ // Round 2: the base is the task branch's tip; main is still unborn.
+ r2 := r
+ r2.StepID, r2.Base = "s2", r1.Candidate
+ git2 := e.tree(r2, r2.Base).GitDir
+ gitOut(t, dir, "clone", "-q", "--bare", git1, git2)
+ r2.Candidate = commit(git2, "hours.txt", "open late\n", r1.Candidate)
+ rep := e.publish(context.Background(), r2, wire.StepReport{})
+ if !rep.Pushed || rep.State != wire.StepDone || rep.Reason != "" {
+ t.Fatalf("round 2: %+v", rep)
+ }
+ if tip := gitOut(t, dir, "--git-dir="+empty, "rev-parse", "refs/heads/"+r.Branch); tip != r2.Candidate {
+ t.Fatalf("task branch at %s, want %s", tip, r2.Candidate)
+ }
+ if parent := gitOut(t, dir, "--git-dir="+empty, "rev-parse", r2.Candidate+"^"); parent != r1.Candidate {
+ t.Fatalf("round 2's parent %s, want round 1's %s", parent, r1.Candidate)
+ }
+ if out, _ := exec.Command("git", "--git-dir="+empty, "rev-parse", "--verify", "--quiet", "refs/heads/main").Output(); len(out) > 0 {
+ t.Fatalf("main was created at %s", out)
+ }
+}
diff --git a/internal/runner/thin/engineer.go b/internal/runner/thin/engineer.go
index 9d2b8312..847856a0 100644
--- a/internal/runner/thin/engineer.go
+++ b/internal/runner/thin/engineer.go
@@ -369,12 +369,7 @@ func (e *Engineer) publish(ctx context.Context, r stepRow, rep wire.StepReport)
var err error
for i := 0; i < pushAttempts; i++ {
var def string
- def, err = e.Clones.DefaultBranch(ctx, r.Repo)
- if errors.Is(err, ErrEmptyRepo) && r.Base == "" {
- // The default branch is unborn: its name is not advertised,
- // and a task branch is never it.
- def, err = "main", nil
- }
+ def, err = e.publishDefault(ctx, r.Repo)
if err != nil {
err = fmt.Errorf("reading the default branch: %w", err)
} else {
@@ -408,6 +403,19 @@ func (e *Engineer) publish(ctx context.Context, r stepRow, rep wire.StepReport)
return rep
}
+// publishDefault is the default branch a push to a task branch must never
+// be: the one HEAD names or, while it is unborn (no commits on it yet),
+// the name its first merge creates. Unborn is unborn whatever the step's
+// base: a fix round's base is the task branch's tip, and forge still
+// pushes only a descendant of that base over Equipo's own earlier commit.
+func (e *Engineer) publishDefault(ctx context.Context, repo string) (string, error) {
+ def, err := e.Clones.DefaultBranch(ctx, repo)
+ if errors.Is(err, ErrEmptyRepo) {
+ return unbornDefault, nil
+ }
+ return def, err
+}
+
// Sweep removes the private repositories and logs of attempts whose
// final report the instance has acked.
func (e *Engineer) Sweep(ctx context.Context, s *State) {
diff --git a/internal/runner/thin/relay.go b/internal/runner/thin/relay.go
index b8f5b746..fe069220 100644
--- a/internal/runner/thin/relay.go
+++ b/internal/runner/thin/relay.go
@@ -462,7 +462,7 @@ func (e *Engineer) publishWorker(ctx context.Context, p pubRow) (wire.PublishedR
e.log("publication verify", err)
return fail("candidate_unavailable")
}
- def, err := e.Clones.DefaultBranch(ctx, p.Repo)
+ def, err := e.publishDefault(ctx, p.Repo)
if err != nil {
// The hub is unreachable: a failed push, which Retry publishes again.
e.logDetail("publication default branch", err)