provision: continue a seeded Plan A under an add-apps window
A Plan A whose window closes after the seed is completed by ContinueAdoption: add-apps plans on both roles with existing_team the stored SQID, for the apps still unregistered, as Home's operator admits (plan.go Plan.valid, windowAdmits). Resubmit of a seeded adoption run points to it. homecompat runs the continuation at Home's real code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+240
−4
homecompat/adopt_test.go+94 −0homecompat/compat_test.go+13 −4provision/adopt.go+37 −0provision/adopt_test.go+91 −0provision/engine.go+5 −0
diff --git a/homecompat/adopt_test.go b/homecompat/adopt_test.go| index 7a5c33b..eebf136 100644 |
| --- a/homecompat/adopt_test.go |
| +++ b/homecompat/adopt_test.go |
| @@ -6,7 +6,9 @@ package altacompat |
| import ( |
| "context" |
| + "database/sql" |
| "errors" |
| + "strings" |
| "testing" |
| "time" |
| @@ -83,3 +85,95 @@ func TestHomeAcceptsAltaSignedExistingTeamPlan(t *testing.T) { |
| t.Fatal("directory plan view lost label, name or apps") |
| } |
| } |
| + |
| +// Plan A interrupted after the seed, its windows then closing: Home's real |
| +// operator admits the continuation Alta signs (add-apps, existing_team the |
| +// seeded SQID, label oficina, no owner) and its provision and register |
| +// for the apps still missing (internal/operator plan.go Plan.valid, |
| +// windowAdmits; http_verbs.go provision under fence and routeScope, |
| +// register verifying the env manifest), committing nothing twice. |
| +func TestHomeAcceptsAdoptionContinuation(t *testing.T) { |
| + ctx := context.Background() |
| + env := map[string]string{} |
| + for _, a := range apps { |
| + env[a] = homeop.CanonicalOrigin(a) |
| + } |
| + crash, crashed := false, false |
| + acts := 0 |
| + r := newRigWith(t, env, func(key string) error { |
| + // Dies after sheets' provision committed (owner-seed, seed-team, |
| + // docs' provision and register, sheets' provision). |
| + if crash && acts == 4 && !crashed { |
| + crashed = true |
| + return errors.New("kill -9") |
| + } |
| + if crash { |
| + acts++ |
| + } |
| + return nil |
| + }) |
| + first, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r.home.confirm(t, "directory", "") |
| + if got := r.drive(t, first); got.State != "done" { |
| + t.Fatalf("initialize: %s %s", got.State, got.Code) |
| + } |
| + r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| + |
| + run, err := r.engine.StartAdoption(ctx, "Oficina", owner) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r.home.confirm(t, "directory", "") |
| + r.home.confirm(t, "credential", owner) |
| + crash = true |
| + for i := 0; i < 5 && !crashed; i++ { |
| + _ = r.engine.Advance(ctx, run) |
| + r.home.now = r.home.now.Add(3 * time.Second) |
| + } |
| + if !crashed { |
| + t.Fatal("never reached sheets' provision") |
| + } |
| + r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| + if got := r.drive(t, run); got.State != "parked" || got.Code != "window-expired" { |
| + t.Fatalf("stranded: %s %s", got.State, got.Code) |
| + } |
| + next, err := r.engine.ContinueAdoption(ctx) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + team, err := r.store.Team(ctx, provision.AdoptionLabel) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, role := range []string{"directory", "credential"} { |
| + v := r.home.confirm(t, role, "") |
| + // sheets' register and everything after it are missing (Home lists |
| + // a plan's apps sorted). |
| + if v.Kind != homeop.KindAddApps || v.ExistingTeam != team.TeamSQID || strings.Join(v.Apps, ",") != "calendar,calls,memoria,sheets,slides" || v.OwnerIndex != "" { |
| + t.Fatalf("%s continuation view: kind %s apps %v", role, v.Kind, v.Apps) |
| + } |
| + } |
| + if got := r.drive(t, next); got.State != "done" || got.Code != "awaiting-configuration" { |
| + t.Fatalf("continuation: %s %s", got.State, got.Code) |
| + } |
| + count := func(db *sql.DB, q string) int { |
| + t.Helper() |
| + var n int |
| + if err := db.QueryRow(q).Scan(&n); err != nil { |
| + t.Fatal(err) |
| + } |
| + return n |
| + } |
| + if n := count(r.home.dirDB, "SELECT count(*) FROM teams WHERE label='oficina'"); n != 1 { |
| + t.Fatalf("teams %d", n) |
| + } |
| + if n := count(r.home.dirDB, "SELECT count(*) FROM team_instances WHERE state='pending' AND consumer_id=app AND origin='https://'||app||'.oficina.works'"); n != len(apps) { |
| + t.Fatalf("pending canonical rows %d", n) |
| + } |
| + if n := count(r.home.credDB, "SELECT count(*) FROM consumers"); n != 0 { |
| + t.Fatalf("register inserted %d consumers", n) |
| + } |
| +} |
diff --git a/homecompat/compat_test.go b/homecompat/compat_test.go| index 2e200d3..173b221 100644 |
| --- a/homecompat/compat_test.go |
| +++ b/homecompat/compat_test.go |
| @@ -78,7 +78,9 @@ func open(t *testing.T, set *migrate.Set) *sql.DB { |
| return h.Writer() |
| } |
| -func newHome(t *testing.T, alta *family.Keypair, prefilter string, installation, registry, device string) *home { |
| +// newHome builds both roles. extraEnv adds HOME_CONSUMERS entries (the |
| +// six canonical consumers, for adoption). |
| +func newHome(t *testing.T, alta *family.Keypair, prefilter string, installation, registry, device string, extraEnv map[string]string) *home { |
| t.Helper() |
| h := &home{now: time.Unix(2_000_000_000, 0), pepper: bytes.Repeat([]byte{9}, 32)} |
| raw, _ := base64.RawURLEncoding.DecodeString(prefilter) |
| @@ -164,6 +166,9 @@ func newHome(t *testing.T, alta *family.Keypair, prefilter string, installation, |
| return err == nil && id != "" |
| } |
| env := map[string]string{"directory": dirOrigin, "alta": "https://alta-x.bds.oncarlos.com"} |
| + for id, origin := range extraEnv { |
| + env[id] = origin |
| + } |
| if h.credential, err = operator.NewCredentialHTTP(cred, h.provPub, env, apps, h.index, admit); err != nil { |
| t.Fatal(err) |
| } |
| @@ -284,11 +289,15 @@ type rig struct { |
| client *homeop.Client |
| } |
| -func newRig(t *testing.T) *rig { |
| +func newRig(t *testing.T) *rig { return newRigWith(t, nil, nil) } |
| + |
| +// newRigWith is newRig over a Home whose env manifest also holds |
| +// extraEnv, with the engine's AfterAct hook. |
| +func newRigWith(t *testing.T, extraEnv map[string]string, afterAct func(string) error) *rig { |
| t.Helper() |
| alta, _ := family.Generate() |
| pre, inst, reg, dev := homeop.Token(), homeop.Token(), homeop.Token(), homeop.Token() |
| - h := newHome(t, alta, pre, inst, reg, dev) |
| + h := newHome(t, alta, pre, inst, reg, dev, extraEnv) |
| dir := httptest.NewServer(h.handler("directory")) |
| cred := httptest.NewServer(h.handler("credential")) |
| t.Cleanup(dir.Close) |
| @@ -323,7 +332,7 @@ func newRig(t *testing.T) *rig { |
| Console: &console.Client{Base: con.URL(), Account: account, Tokens: tokens, Guard: provision.Guard{Store: store, Account: account}}, |
| Public: &http.Client{Transport: con.Transport()}, |
| HomeCfg: provision.HomeConfig{Issuer: credOrigin, Directory: dirOrigin, KeysBase64: "eyJrMSI6IkJBPT0ifQ==", Provisioner: h.provPub}, |
| - Account: account, Now: clock, Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| + Account: account, Now: clock, Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), AfterAct: afterAct, |
| Debug: func(err error) { t.Logf("transient: %v", err) }}) |
| return &rig{home: h, con: con, engine: e, store: store, client: client} |
| } |
diff --git a/provision/adopt.go b/provision/adopt.go| index 0b73704..6b03a09 100644 |
| --- a/provision/adopt.go |
| +++ b/provision/adopt.go |
| @@ -100,6 +100,43 @@ func (e *Engine) StartAdoption(ctx context.Context, name, owner string) (int64, |
| return id, e.submitPlans(ctx, id, AdoptionLabel, name, owner, homeop.KindCreateTeam, apps, ins.TeamSQID) |
| } |
| +// ContinueAdoption completes a Plan A whose team is seeded but whose |
| +// window closed before every app's consumer and register steps were done. |
| +// Home admits seeding under create-team alone, and provision and register |
| +// under add-apps too (internal/operator plan.go windowAdmits), so the |
| +// continuation's plan is add-apps on both roles, existing_team the stored |
| +// SQID, no owner, for exactly the apps still unregistered. The run is |
| +// Plan A's phase (create-team in Alta, no seed since the team is seeded): |
| +// done steps are skipped, a held request is replayed exactly, and it ends |
| +// awaiting-configuration. |
| +func (e *Engine) ContinueAdoption(ctx context.Context) (int64, error) { |
| + t, err := e.cfg.Store.Team(ctx, AdoptionLabel) |
| + if err != nil { |
| + return 0, fail("no-team") |
| + } |
| + if t.TeamSQID == "" { |
| + // Unseeded: Resubmit the Plan A run, whose create-team plan seeds. |
| + return 0, fail("team-not-seeded") |
| + } |
| + var missing []string |
| + for _, a := range Slugs() { |
| + if _, ok, err := e.cfg.Store.Done(ctx, AdoptionLabel, a, StepRegister); err != nil { |
| + return 0, err |
| + } else if !ok { |
| + missing = append(missing, a) |
| + } |
| + } |
| + if len(missing) == 0 { |
| + return 0, fail("plan-a-complete") |
| + } |
| + id, err := e.cfg.Store.NewAdoptionRun(ctx, homeop.KindCreateTeam, missing) |
| + if err != nil { |
| + return 0, err |
| + } |
| + e.cfg.Store.Audit(ctx, id, AdoptionLabel, "", "run-start", "continue-adoption") |
| + return id, e.submitPlans(ctx, id, AdoptionLabel, "", "", homeop.KindAddApps, missing, t.TeamSQID) |
| +} |
| + |
| // ActivateAdopted opens Plan B or C: add-apps on both roles for apps |
| // whose Plan A is done, existing_team the seeded team's SQID, no owner. |
| func (e *Engine) ActivateAdopted(ctx context.Context, apps []string) (int64, error) { |
diff --git a/provision/adopt_test.go b/provision/adopt_test.go| index 2520a49..29580da 100644 |
| --- a/provision/adopt_test.go |
| +++ b/provision/adopt_test.go |
| @@ -908,3 +908,94 @@ func TestAdoptionCrashAfterEveryAct(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// Plan A interrupted after the seed, its window then closing: the team is |
| +// seeded, some apps registered. A continuation under a fresh add-apps |
| +// window for the stored SQID completes only the missing steps. |
| +func TestAdoptionContinuesAfterTheWindowCloses(t *testing.T) { |
| + a := newAdoption(t) |
| + e := a.adopter(nil, nil) |
| + _, err := e.ContinueAdoption(a.ctx()) |
| + failCode(t, err, "no-team") |
| + run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + _, err = e.ContinueAdoption(a.ctx()) |
| + failCode(t, err, "team-not-seeded") |
| + a.confirm(homeop.KindCreateTeam) |
| + // Dies after sheets' register committed at Home (acts: owner-seed, |
| + // seed-team, then each app's provision and register: docs, sheets). |
| + crashed := false |
| + dying := a.adopter(crashAt(5, &crashed), nil) |
| + for j := 0; j < 10 && !crashed; j++ { |
| + _ = dying.Advance(a.ctx(), run) |
| + a.tick(5 * time.Second) |
| + } |
| + if !crashed { |
| + t.Fatal("never reached sheets' register") |
| + } |
| + a.tick(homeop.WindowLifetime * time.Second) |
| + if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "window-expired" { |
| + t.Fatalf("stranded run %s %s", r.State, r.Code) |
| + } |
| + // The lost answer was recovered by exact replay, outside the window. |
| + if _, ok, _ := a.store.Done(a.ctx(), AdoptionLabel, "sheets", StepRegister); !ok { |
| + t.Fatal("sheets' committed register was not recovered") |
| + } |
| + failCode(t, e.Resubmit(a.ctx(), run), "use-continue-adoption") |
| + _, err = e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| + failCode(t, err, "label-used") |
| + team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| + next, err := e.ContinueAdoption(a.ctx()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if r, _ := a.store.Run(a.ctx(), run); r.State != "superseded" { |
| + t.Fatalf("stranded run %s", r.State) |
| + } |
| + missing := "slides,calendar,calls,memoria" |
| + if r, _ := a.store.Run(a.ctx(), next); !r.Adoption || r.Kind != homeop.KindCreateTeam || strings.Join(r.Apps, ",") != missing { |
| + t.Fatalf("continuation run %+v", r) |
| + } |
| + plans := a.plans(next) |
| + if len(plans) != 2 { |
| + t.Fatalf("roles %d", len(plans)) |
| + } |
| + for role, p := range plans { |
| + if p.Kind != homeop.KindAddApps || p.ExistingTeam != team.TeamSQID || strings.Join(p.Apps, ",") != missing || p.Owner != "" || p.DisplayName != "" { |
| + t.Fatalf("%s continuation plan: kind %s existing %q apps %v", role, p.Kind, p.ExistingTeam, p.Apps) |
| + } |
| + } |
| + if plans[homeop.Directory].Label != AdoptionLabel { |
| + t.Fatal("continuation directory label") |
| + } |
| + a.confirm(homeop.KindAddApps) |
| + if r := a.drive(e, next, 10); r.State != "done" || r.Code != "awaiting-configuration" { |
| + t.Fatalf("continuation %s %s", r.State, r.Code) |
| + } |
| + if a.home.TeamCount() != 1 || a.home.ConsumerCount() != 0 { |
| + t.Fatal("teams or credential consumers") |
| + } |
| + for _, s := range Slugs() { |
| + if a.home.InstanceState(s) != "pending" { |
| + t.Fatalf("%s %q", s, a.home.InstanceState(s)) |
| + } |
| + if _, ok, _ := a.store.Done(a.ctx(), AdoptionLabel, s, StepRegister); !ok { |
| + t.Fatalf("%s not registered", s) |
| + } |
| + } |
| + reqs := a.home.Requests() |
| + onceEach(t, reqs) |
| + if len(reqs) != 1+2+2*len(Apps) || a.home.Calls[homeop.DirectorySeedTeam] != 1 || a.home.Calls[homeop.CredentialOwnerSeed] != 1 { |
| + t.Fatalf("requests %v", reqs) |
| + } |
| + _, err = e.ContinueAdoption(a.ctx()) |
| + failCode(t, err, "plan-a-complete") |
| + // Plan B follows as usual. |
| + ob := a.adopter(nil, newObserver()) |
| + b := a.activate(ob, "calendar") |
| + if r := a.drive(ob, b, 10); r.State != "done" || a.home.InstanceState("calendar") != "live" { |
| + t.Fatalf("plan B %s %s", r.State, r.Code) |
| + } |
| +} |
diff --git a/provision/engine.go b/provision/engine.go| index 90dc849..81848fa 100644 |
| --- a/provision/engine.go |
| +++ b/provision/engine.go |
| @@ -410,6 +410,11 @@ func (e *Engine) Resubmit(ctx context.Context, runID int64) error { |
| return err |
| } |
| if t.TeamSQID != "" { |
| + if r.Adoption { |
| + // Seeded: the rest of Plan A takes an add-apps window |
| + // (ContinueAdoption), never a second create-team. |
| + return fail("use-continue-adoption") |
| + } |
| return fail("use-add-apps") |
| } |
| name = t.Name |