alta-adopt: re-observe before every rollback restart, not just resumed
rollbackRestart now runs rollbackDrift itself, so every restart path is covered: after a fresh unset, after a RolledBack reread, and on a resume at unset. A preserved key or identity change that lands during the unset now stops the rollback (stopped, preserved-changed or identity-changed) with nothing restarted; the separate check on the resume path is folded into it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+69
−8
adopt/write.go+18 −8adopt/write_test.go+51 −0
CI passed — run details
CI log
=== vet === go vet ./... === fmt === === test === CGO_ENABLED=0 go test ./... ok amadan.net/oficina/alta/adopt 17.458s ok amadan.net/oficina/alta/carlosop 0.036s ? amadan.net/oficina/alta/carlosop/fakecarlos [no test files] ok amadan.net/oficina/alta/cmd/alta (cached) ok amadan.net/oficina/alta/cmd/alta-adopt 16.854s ok amadan.net/oficina/alta/console (cached) ? amadan.net/oficina/alta/console/fakeconsole [no test files] ok amadan.net/oficina/alta/homeop (cached) ok amadan.net/oficina/alta/homeop/fakehome (cached) ok amadan.net/oficina/alta/internal/web (cached) ok amadan.net/oficina/alta/provision (cached) === compat === cd homecompat && go vet ./... && CGO_ENABLED=0 go test ./... ok amadan.net/oficina/home/altacompat 0.543s
diff --git a/adopt/write.go b/adopt/write.go| index 65ffc19..eeeb5a4 100644 |
| --- a/adopt/write.go |
| +++ b/adopt/write.go |
| @@ -561,7 +561,7 @@ func CaptureAfter(ctx context.Context, c *carlosop.Client, h *http.Client, s *St |
| // identity stops the rollback (identity-changed), as does any |
| // preserved key that changed anywhere in the effective config, or a |
| // layer the write did not touch that moved (preserved-changed); |
| -// - unset then goes straight to the restart; |
| +// - unset goes to the restart, which re-observes again first; |
| // - otherwise the write layer that observation read is classified |
| // (ClassifyRollback). RetryUnset journals sent and sends one |
| // conditional PUT unsetting both keys at the version just read; a |
| @@ -623,13 +623,6 @@ func RollbackAgentKeys(ctx context.Context, c *carlosop.Client, h *http.Client, |
| case "stopped": |
| return stoppedErr(rb.Code) |
| case "unset": |
| - // The restart is a mutation too: the host is the baseline's, and |
| - // keeps every preserved key, or a person looks first. |
| - if _, code, diff, err := rollbackDrift(ctx, c, app, base); err != nil { |
| - return err |
| - } else if code != "" { |
| - return stop(code, ErrStopPreserved, diff...) |
| - } |
| return rollbackRestart(ctx, c, h, s, app, base, rb, apply) |
| } |
| for attempt := 1; ; attempt++ { |
| @@ -737,7 +730,24 @@ func rollbackDrift(ctx context.Context, c *carlosop.Client, app provision.App, b |
| // rollbackRestart restarts the canonical host and waits for Reverted |
| // against the baseline, then journals the rollback restarted. A failure |
| // journals it unset, code restart-failed, for a resume. |
| +// |
| +// Every rollback restart comes through here, after a fresh unset, a |
| +// RolledBack reread or a resume at unset, so here the host is |
| +// re-observed against the baseline first (rollbackDrift): the restart is |
| +// a mutation too, and a change that landed during the unset, or since, |
| +// stops the rollback with nothing restarted. |
| func rollbackRestart(ctx context.Context, c *carlosop.Client, h *http.Client, s *Store, app provision.App, base Snapshot, rb RollbackRecord, apply bool) error { |
| + if _, code, diff, err := rollbackDrift(ctx, c, app, base); err != nil { |
| + return err |
| + } else if code != "" { |
| + if apply { |
| + rb.State, rb.Code = "stopped", code |
| + if err := advanceRollback(ctx, s, rb); err != nil { |
| + return err |
| + } |
| + } |
| + return fmt.Errorf("%w (%s): %s", ErrStopPreserved, code, strings.Join(diff, ", ")) |
| + } |
| _, err := RestartAndWait(ctx, c, h, app, base.Identity, func(d Delivery) error { return Reverted(base.Delivery, d) }, apply) |
| if !apply { |
| return err |
diff --git a/adopt/write_test.go b/adopt/write_test.go| index 5f96eae..d0574d6 100644 |
| --- a/adopt/write_test.go |
| +++ b/adopt/write_test.go |
| @@ -1384,6 +1384,57 @@ func TestRollbackStopsWhenIdentityChanged(t *testing.T) { |
| } |
| } |
| +// A change that lands while the unset is in flight, in a layer the unset |
| +// does not touch, is caught before the restart: the unset succeeds, the |
| +// restart never runs, and the rollback stops. |
| +func TestRollbackStopsBeforeRestartOnChangeDuringUnset(t *testing.T) { |
| + cases := map[string]struct { |
| + edit func(w *writeFixture) |
| + code, want string |
| + }{ |
| + "secret resealed in the environment": {func(w *writeFixture) { w.f.SetSecret("docs", "production", "DOCS_DEK") }, "preserved-changed", "changed:DOCS_DEK"}, |
| + "instance record replaced": {func(w *writeFixture) { |
| + in := fixtureInstance(w.app) |
| + in.Environment, in.RecordSeq = "production", in.RecordSeq+1 |
| + w.f.AddInstance(in) |
| + }, "identity-changed", "identity-record"}, |
| + } |
| + for name, tc := range cases { |
| + t.Run(name, func(t *testing.T) { |
| + w := newWriteFixture(t, "docs") |
| + w.f.SetSecret("docs", "production", "DOCS_DEK") |
| + w.bindTo("production") |
| + w.configured(t) |
| + p, r := w.puts(), w.f.RestartCount("docs.oficina.works") |
| + w.f.BeforePut = once(func() { tc.edit(w) }) |
| + err := w.rollback(true) |
| + if !errors.Is(err, ErrStopPreserved) || !strings.Contains(err.Error(), tc.want) { |
| + t.Fatalf("err = %v, want ErrStopPreserved naming %s", err, tc.want) |
| + } |
| + noValues(t, err) |
| + if n := w.rollbackPuts(p); n != 1 { |
| + t.Fatalf("rollback PUTs = %d", n) |
| + } |
| + if present, _ := w.stored(t); present != [2]bool{} { |
| + t.Fatal("the unset did not land") |
| + } |
| + if w.f.RestartCount("docs.oficina.works") != r { |
| + t.Fatal("restarted past a change made during the unset") |
| + } |
| + rec, _ := w.rollbackRecord(t) |
| + if rec.State != "stopped" || rec.Code != tc.code { |
| + t.Fatalf("state %q code %q", rec.State, rec.Code) |
| + } |
| + if err := w.rollback(true); !errors.Is(err, ErrStopPreserved) { |
| + t.Fatalf("resume: err = %v", err) |
| + } |
| + if w.f.RestartCount("docs.oficina.works") != r { |
| + t.Fatal("a resume restarted") |
| + } |
| + }) |
| + } |
| +} |
| + |
| // A rollback resumed after its unset re-observes too: the restart is a |
| // mutation, and a preserved key that changed since stops it. |
| func TestRollbackResumeAfterUnsetStopsOnPreservedChange(t *testing.T) { |