Seeding is one transaction, and the address waits for it
Fable's P3s on 0efb351 (oficina/alta #1, APPROVED): 1. TestForgetOwnerKeepsTheAddressBeforeTheSeed calls forgetOwnerWhenWritten on an unseeded team (no recipe needs the owner any more, so only the seed guard holds it back) and finds the address kept: owner-seed still needs it. Dropping the guard fails it. 2. SetSeeded writes Home's team row and the team step in one transaction, so no crash can leave a team sqid with its step undone. For a database already torn that way by the earlier two writes, the forget guard also accepts a recorded team sqid, and provision() records the missing team step from it before the app steps. TestTornSeedStillForgetsTheAddress builds that state (a crash right after Home commits seed-team, then the old first write alone) and resumes under add-apps to the full end state with the address gone and the step repaired. Removing the repair, or the repair and the guard's sqid check together, fails it; the sqid check alone is redundant beside the repair and kept as defence in depth.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+110
−8
provision/review_test.go+73 −0provision/steps.go+16 −5provision/store.go+21 −3
CI passed — run details
diff --git a/provision/review_test.go b/provision/review_test.go| index b7fe0de..c2db4de 100644 |
| --- a/provision/review_test.go |
| +++ b/provision/review_test.go |
| @@ -269,3 +269,76 @@ func TestOwnerForgottenOnceSeeded(t *testing.T) { |
| t.Fatal("owner-seed request kept after the seed") |
| } |
| } |
| + |
| +// Fable's P3 on 0efb351: before the seed the address is owner-seed's |
| +// input, so forgetting must not touch an unseeded team, even though no |
| +// recipe needs the owner any more. |
| +func TestForgetOwnerKeepsTheAddressBeforeTheSeed(t *testing.T) { |
| + h := newHarness(t) |
| + h.initialized() |
| + e := h.engine(nil) |
| + run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: allApps}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r, _ := h.store.Run(h.ctx(), run) |
| + if err := e.forgetOwnerWhenWritten(h.ctx(), &pass{run: r}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, held, _ := h.store.Owner(h.ctx(), "lagged"); !held || got != ownerCanary { |
| + t.Fatal("the address was forgotten before the seed: owner-seed has nothing to send") |
| + } |
| +} |
| + |
| +// Fable's P3 on 0efb351: SetSeeded writes the team row and the team step |
| +// in one transaction. A database left torn by the earlier code (sqid |
| +// recorded, step lost to a crash) still forgets the address and finishes. |
| +func TestTornSeedStillForgetsTheAddress(t *testing.T) { |
| + h := newHarness(t) |
| + h.initialized() |
| + crashed := false |
| + e := h.engine(func(k string) error { |
| + if k == "seed-team" && !crashed { |
| + crashed = true |
| + return errors.New("kill -9") |
| + } |
| + return nil |
| + }) |
| + run := h.createLagged(e) |
| + _ = e.Advance(h.ctx(), run) |
| + if !crashed { |
| + t.Fatal("never reached seed-team") |
| + } |
| + id, sqid, ok := h.home.Team("lagged") |
| + if !ok { |
| + t.Fatal("Home did not seed") |
| + } |
| + // The torn state: the team row as the old two-write SetSeeded left it |
| + // after its first write, the team step never recorded. |
| + ref := str(mustDone(h.ctx(), h.store, "lagged", "", StepOwner), "owner_ref") |
| + ins, _, _ := h.engine(nil).cfg.Home.Inspect(h.ctx(), homeop.Directory) |
| + if _, err := h.store.DB().Exec("UPDATE teams SET team_id=?,team_sqid=?,epoch=1,generation=?,owner_ref=? WHERE label='lagged'", id, sqid, *ins.Generation, ref); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); done { |
| + t.Fatal("setup: the team step is recorded") |
| + } |
| + // The crashed holder's lock goes stale, and the create-team window |
| + // closes: the rest runs under a new add-apps plan. |
| + h.tick(homeop.WindowLifetime * time.Second) |
| + next, err := h.engine(nil).AddApps(h.ctx(), "lagged", allApps) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + h.confirm(homeop.KindAddApps) |
| + if r := h.drive(h.engine(nil), next, 40); r.State != "done" { |
| + t.Fatalf("%s %s", r.State, r.Code) |
| + } |
| + if _, held, _ := h.store.Owner(h.ctx(), "lagged"); held { |
| + t.Fatal("a torn seed kept the address") |
| + } |
| + if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); !done { |
| + t.Fatal("the team step was not repaired") |
| + } |
| + checkEndState(t, h) |
| +} |
diff --git a/provision/steps.go b/provision/steps.go| index f19da28..1a5d9cb 100644 |
| --- a/provision/steps.go |
| +++ b/provision/steps.go |
| @@ -95,6 +95,12 @@ func (e *Engine) provision(ctx context.Context, p *pass) error { |
| return err |
| } |
| p.team = t |
| + } else if _, ok, _ := e.cfg.Store.Done(ctx, p.run.Label, "", StepTeam); !ok { |
| + // A team row seeded before SetSeeded was one transaction, with its |
| + // step lost to a crash: the sqid is Home's answer, so record it. |
| + if err := e.record(ctx, p, "", StepTeam, map[string]any{"team_sqid": p.team.TeamSQID, "epoch": p.team.Epoch}); err != nil { |
| + return err |
| + } |
| } |
| // Before any app step: when no recipe holds the owner, the seed was |
| // the address's last use, and an app that fails must not keep it. |
| @@ -228,10 +234,12 @@ func (e *Engine) seeded(ctx context.Context, p *pass, res homeop.Result) error { |
| if owner == "" { |
| return fail("seed-owner-unknown") |
| } |
| - if err := e.cfg.Store.SetSeeded(ctx, p.run.Label, res.TeamID, res.TeamSQID, res.Epoch, res.Generation, owner); err != nil { |
| + if err := e.cfg.Store.SetSeeded(ctx, p.run.Label, res.TeamID, res.TeamSQID, res.Epoch, res.Generation, owner, map[string]any{"team_sqid": res.TeamSQID, "epoch": res.Epoch}); err != nil { |
| return err |
| } |
| - return e.record(ctx, p, "", StepTeam, map[string]any{"team_sqid": res.TeamSQID, "epoch": res.Epoch}) |
| + e.log(p.run.ID, "", StepTeam, "done") |
| + e.cfg.Store.Audit(ctx, p.run.ID, p.run.Label, "", "step-done", StepTeam) |
| + return nil |
| } |
| func mustDone(ctx context.Context, s *Store, label, app, step string) map[string]any { |
| @@ -895,13 +903,16 @@ func (e *Engine) forgetOwnerWhenWritten(ctx context.Context, p *pass) error { |
| if _, held, _ := st.Owner(ctx, p.run.Label); !held { |
| return nil |
| } |
| - if _, ok, _ := st.Done(ctx, p.run.Label, "", StepTeam); !ok { |
| - return nil |
| - } |
| t, err := st.Team(ctx, p.run.Label) |
| if err != nil { |
| return err |
| } |
| + // Seeded: the team step done, or Home's team sqid recorded (either |
| + // alone is enough; SetSeeded writes both at once). Before the seed the |
| + // address is still owner-seed's input. |
| + if _, done, _ := st.Done(ctx, p.run.Label, "", StepTeam); !done && t.TeamSQID == "" { |
| + return nil |
| + } |
| for app, state := range t.Apps { |
| spec, _ := AppBySlug(app) |
| if state != "active" || !NeedsOwner(spec) { |
diff --git a/provision/store.go b/provision/store.go| index 9fa2836..684567c 100644 |
| --- a/provision/store.go |
| +++ b/provision/store.go |
| @@ -232,9 +232,27 @@ func (s *Store) Teams(ctx context.Context) ([]Team, error) { |
| } |
| // SetSeeded records what seed-team committed. |
| -func (s *Store) SetSeeded(ctx context.Context, label string, id int64, sqid string, epoch int64, generation, ownerRef string) error { |
| - _, err := s.db.ExecContext(ctx, "UPDATE teams SET team_id=?,team_sqid=?,epoch=?,generation=?,owner_ref=? WHERE label=?", id, sqid, epoch, generation, ownerRef, label) |
| - return err |
| +// SetSeeded records what seed-team committed and marks the team step |
| +// done, in one transaction: a crash between the two would leave a team |
| +// sqid with its step undone, a state the owner-forgetting guard would |
| +// otherwise never see as seeded. |
| +func (s *Store) SetSeeded(ctx context.Context, label string, id int64, sqid string, epoch int64, generation, ownerRef string, step map[string]any) error { |
| + data, err := json.Marshal(step) |
| + if err != nil { |
| + return err |
| + } |
| + tx, err := s.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + return err |
| + } |
| + defer tx.Rollback() |
| + if _, err := tx.ExecContext(ctx, "UPDATE teams SET team_id=?,team_sqid=?,epoch=?,generation=?,owner_ref=? WHERE label=?", id, sqid, epoch, generation, ownerRef, label); err != nil { |
| + return err |
| + } |
| + if _, err := tx.ExecContext(ctx, "INSERT INTO steps(label,app,step,state,code,data,updated_at) VALUES(?,'',?,'done','',?,?) ON CONFLICT(label,app,step) DO UPDATE SET state='done',code='',data=excluded.data,updated_at=excluded.updated_at", label, StepTeam, string(data), s.now().Unix()); err != nil { |
| + return err |
| + } |
| + return tx.Commit() |
| } |
| // ---- owner input ---- |