system recover: settle a legacy update-begun marker through the deferral wrapper, barrier failure fatal
Astra (round 3, finding 1b on the unarmed path): with root participation, no
armed floor, a legacy update-begun.json and neither an update record nor a
journal, checkUnarmedParticipationRecovery returned without settling and the
run's end settled the marker through clearUpdateBegun directly, logging any
error. Its BeginUpdate("recover-legacy") was refused by the census, which
withdrew the record it had written, so recover exited 0 with Acquire open.
settleUpdateBegun now settles through settleWithoutJournal with the
recover-legacy barrier: a census refusal comes back as errSettlementDeferred
with root's update record holding acquisitions refused; a barrier that could
not be written comes back as errAcquisitionBarrier (new sentinel, also used by
settleWithoutJournal). settleMarker, at both end-of-run sites in Run, defers
the former under --recover, joins the latter into the run's error in every
mode, and logs everything else as on main.
Witnesses: TestRecoverHoldsAcquisitionsForALegacyMarker (deferral, barrier
under recover-legacy, Acquire refused, settled once the census passes) and
TestRecoverFailsALegacyMarkerWhoseBarrierCannotBeWritten (a store that refuses
the barrier's write: recover fails, the marker stays). Mutation checks:
bypassing the wrapper fails both; swallowing the barrier error fails the second.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
4 files changed,
+143
−8
internal/host/guardtransaction.go+25 −1internal/host/recover_settlement_test.go+96 −0internal/host/systemapply.go+2 −2internal/host/systemparticipation.go+20 −5
diff --git a/internal/host/guardtransaction.go b/internal/host/guardtransaction.go| index 047d0133e..da1caae37 100644 |
| --- a/internal/host/guardtransaction.go |
| +++ b/internal/host/guardtransaction.go |
| @@ -304,6 +304,11 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error { |
| // treats a refused census as a deferral too (systemparticipation.go). |
| var errSettlementDeferred = errors.New("participation settlement deferred") |
| +// errAcquisitionBarrier marks a deferral that could not hold claim |
| +// acquisitions refused (Ledger.HoldUpdate failed). It is never swallowed: |
| +// recover must not exit 0 with acquisitions open (Astra rounds 2 and 3). |
| +var errAcquisitionBarrier = errors.New("acquisition barrier") |
| + |
| // settleWithoutJournal is the result of one settlement made with no |
| // journal open: the pending-controller recovery, the completion or |
| // withdrawal, and the end of root's update record, whose census runs again |
| @@ -328,7 +333,7 @@ func (a *systemApplier) settleWithoutJournal(ctx context.Context, barrier string |
| if holdErr := a.participationDisk().WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { |
| return l.HoldUpdate(ctx, barrier) |
| }, func() error { return rootparticipation.ErrUnavailable }); holdErr != nil { |
| - return errors.Join(err, fmt.Errorf("acquisition barrier: %w", holdErr)) |
| + return errors.Join(err, fmt.Errorf("%w: %w", errAcquisitionBarrier, holdErr)) |
| } |
| return fmt.Errorf("%w: no journal is open, so the files are whole; root's update record holds claim acquisitions refused until the settlement passes: %w", errSettlementDeferred, err) |
| } |
| @@ -359,6 +364,25 @@ func (a *systemApplier) deferSettlement(recover bool, err error) bool { |
| return true |
| } |
| +// settleMarker settles the update-begun marker at the end of a run |
| +// (settleUpdateBegun) and decides what its outcome means for the run: a |
| +// deferral under --recover is logged and left to a later run; a barrier |
| +// that could not be written joins the run's error, whatever the mode, |
| +// because acquisitions would otherwise be open with the settlement |
| +// unmade; a deferral under the apply is logged, as every marker failure |
| +// was on main, with acquisitions held refused by the barrier. |
| +func (a *systemApplier) settleMarker(ctx context.Context, recover bool, retErr *error) { |
| + err := a.settleUpdateBegun(ctx) |
| + if err == nil || a.deferSettlement(recover, err) { |
| + return |
| + } |
| + if errors.Is(err, errAcquisitionBarrier) { |
| + *retErr = errors.Join(*retErr, err) |
| + return |
| + } |
| + a.ops.logf("system apply: %v", err) |
| +} |
| + |
| func (a *systemApplier) endGuard(ctx context.Context) error { |
| if !a.guarded() { |
| return nil |
diff --git a/internal/host/recover_settlement_test.go b/internal/host/recover_settlement_test.go| index 5fc2d47ea..16dbd5dce 100644 |
| --- a/internal/host/recover_settlement_test.go |
| +++ b/internal/host/recover_settlement_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
| + "github.com/carlosframework/platform/internal/rootstate" |
| "os" |
| "path/filepath" |
| "strings" |
| @@ -659,3 +660,98 @@ func TestRecoverDoesNotDeferWithoutABarrier(t *testing.T) { |
| t.Fatalf("fixture: %v", err) |
| } |
| } |
| + |
| +// failingBarrierStore is the participation store with one write refused: |
| +// the barrier's update.json, written after BeginUpdate's own write of it |
| +// was withdrawn by a census refusal. |
| +type failingBarrierStore struct { |
| + rootstate.Store |
| + writes int |
| +} |
| + |
| +func (f *failingBarrierStore) Write(name string, raw []byte) error { |
| + if name == "update.json" { |
| + f.writes++ |
| + if f.writes == 2 { |
| + return errors.New("barrier write refused for the test") |
| + } |
| + } |
| + return f.Store.Write(name, raw) |
| +} |
| + |
| +// legacyMarker writes the update-begun marker alone: root participation, |
| +// no armed floor, no update record, no journal (Astra round 3). |
| +func legacyMarker(t *testing.T, f *applyFixture) { |
| + t.Helper() |
| + a := f.a |
| + if a.guarded() { |
| + t.Fatal("fixture has an armed floor") |
| + } |
| + if err := os.MkdirAll(systemGuardDir(a.optDir), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := writeJSONDurable(updateBegunPath(a.optDir), updateBegun{Target: "new", At: f.now}, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := a.participation.UpdateID(); !os.IsNotExist(err) { |
| + t.Fatalf("fixture has an update record: %v", err) |
| + } |
| + if j, err := readSystemJournal(a.optDir); err != nil || j != nil { |
| + t.Fatalf("fixture has a journal: %v %v", j, err) |
| + } |
| +} |
| + |
| +func TestRecoverHoldsAcquisitionsForALegacyMarker(t *testing.T) { |
| + // checkUnarmedParticipationRecovery sees no update record and no |
| + // journal and returns; the run's end settles the marker through |
| + // clearUpdateBegun, whose BeginUpdate the census refuses (and which |
| + // withdraws the record it wrote). Recover must exit 0 with the marker |
| + // kept and acquisitions held refused, and settle both once the census |
| + // passes. |
| + f := newApplyFixture(t, "stable", "old") |
| + a := f.a |
| + legacyMarker(t, f) |
| + refuseCensus(t, f) |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatalf("recover = %v, want a deferral that exits 0", err) |
| + } |
| + if _, err := os.Lstat(updateBegunPath(a.optDir)); err != nil { |
| + t.Fatalf("the marker went without a settlement: %v", err) |
| + } |
| + if id, err := a.participation.UpdateID(); err != nil || id != "recover-legacy" { |
| + t.Fatalf("barrier = %q, %v; want root's update record under recover-legacy", id, err) |
| + } |
| + if err := a.participation.Acquire(t.Context(), func() error { return nil }); err == nil { |
| + t.Fatal("an acquisition passed while recover had deferred the legacy marker's settlement") |
| + } |
| + a.participation.VerifyLaunches = func(context.Context, uint64) error { return nil } |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := os.Lstat(updateBegunPath(a.optDir)); !os.IsNotExist(err) { |
| + t.Fatalf("marker left after settlement: %v", err) |
| + } |
| + if _, err := a.participation.UpdateID(); !os.IsNotExist(err) { |
| + t.Fatalf("barrier left after settlement: %v", err) |
| + } |
| + if err := a.participation.Acquire(t.Context(), func() error { return nil }); err != nil { |
| + t.Fatalf("acquisition after settlement = %v", err) |
| + } |
| +} |
| + |
| +func TestRecoverFailsALegacyMarkerWhoseBarrierCannotBeWritten(t *testing.T) { |
| + // The same start, with the barrier's write refused: recover must fail, |
| + // not exit 0 with acquisitions open; the marker stays for the next run. |
| + f := newApplyFixture(t, "stable", "old") |
| + a := f.a |
| + legacyMarker(t, f) |
| + refuseCensus(t, f) |
| + a.participation.State = &failingBarrierStore{Store: a.participation.State} |
| + err := a.Run(t.Context(), true) |
| + if err == nil || !errors.Is(err, errAcquisitionBarrier) { |
| + t.Fatalf("recover with no barrier = %v, want it fatal", err) |
| + } |
| + if _, err := os.Lstat(updateBegunPath(a.optDir)); err != nil { |
| + t.Fatalf("the marker went without a settlement: %v", err) |
| + } |
| +} |
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go| index ff29f79aa..da966c116 100644 |
| --- a/internal/host/systemapply.go |
| +++ b/internal/host/systemapply.go |
| @@ -346,7 +346,7 @@ func (a *systemApplier) Run(ctx context.Context, recover bool) (retErr error) { |
| } |
| if !active { |
| if !deferred { |
| - a.settleUpdateBegun(ctx) |
| + a.settleMarker(ctx, recover, &retErr) |
| } |
| _ = os.RemoveAll(a.stagingDir()) |
| return |
| @@ -358,7 +358,7 @@ func (a *systemApplier) Run(ctx context.Context, recover bool) (retErr error) { |
| } |
| return |
| } |
| - a.settleUpdateBegun(ctx) |
| + a.settleMarker(ctx, recover, &retErr) |
| _ = os.RemoveAll(a.stagingDir()) |
| } |
| }() |
diff --git a/internal/host/systemparticipation.go b/internal/host/systemparticipation.go| index 380a61827..a067c70ae 100644 |
| --- a/internal/host/systemparticipation.go |
| +++ b/internal/host/systemparticipation.go |
| @@ -146,16 +146,31 @@ func (a *systemApplier) clearLegacyUpdateBegun() error { |
| // got past its first step. A kept journal (a rollback that could not |
| // finish) keeps the marker with it. Run with or without CARLOS_REGION_HA, |
| // so a marker left behind after the flag is turned off still goes. |
| -func (a *systemApplier) settleUpdateBegun(ctx context.Context) { |
| +// |
| +// On a census box the settlement goes through settleWithoutJournal (Astra |
| +// round 3): a legacy marker with no update record and no journal has |
| +// clearUpdateBegun begin root's update record, and a census refusal there |
| +// withdrew it and left acquisitions open while the error was only logged. |
| +// Now a census refusal comes back as errSettlementDeferred with the |
| +// "recover-legacy" barrier held, and a barrier that could not be written as |
| +// errAcquisitionBarrier; the caller decides what each means for the run. |
| +// Every other failure is logged and swallowed, as on main. |
| +func (a *systemApplier) settleUpdateBegun(ctx context.Context) error { |
| if active, err := a.activeFloor(); err != nil || active { |
| - return |
| + return nil |
| } |
| if j, err := readSystemJournal(a.optDir); err != nil || j != nil { |
| - return |
| + return nil |
| + } |
| + err := a.settleWithoutJournal(ctx, "recover-legacy", a.clearUpdateBegun(ctx)) |
| + if err == nil { |
| + return nil |
| } |
| - if err := a.clearUpdateBegun(ctx); err != nil { |
| - a.ops.logf("system apply: %v", err) |
| + if errors.Is(err, errSettlementDeferred) || errors.Is(err, errAcquisitionBarrier) { |
| + return err |
| } |
| + a.ops.logf("system apply: %v", err) |
| + return nil |
| } |
| // closeGuardRollback is closeGuardJournal for the backward paths: rollback |