dbverify v2: one restore-drill record per box (#898)
Slice 2a task 4. The single control/restore-verify.json, which every box's publish overwrote, gives way to control/verify/<ref>.json, written only by box <ref>. KeyFor refuses a ref boxself.ValidRef would refuse; Load, Publish and RemoveHost take the ref and touch only that key. Verdicts carry a result (passed, failed, unverified) and reason, the instance identity, replica prefix, bracket time, restored TXID, header time and size, and freshness. Merge keeps the bookkeeping the alerts read: FirstSeen, UnverifiedRuns (not_reached never counts) and LastDecidedAt (moved only by passed or failed); a different identity starts all three over, an unknown one does not. Never-seen fleet hosts are added as unverified/not_reached. ChangeScope maps each record to its own scope control/verify/<ref>; the mutation classifier admits control/verify/ puts in place of the old exact row. Callers adapted to keep the build green: ops.go passes boxLabel() as the ref (Task 5 replaces it), hibernate_verify.go builds v2 verdicts, movespass.go wires RemoveHost on cfg.Box. The record-write census pins the old Publish/RemoveHost digests and fails until Task 9.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/dbverify/dbverify.go+225 −72internal/dbverify/dbverify_test.go+355 −37internal/host/movesland_test.go+6 −4internal/host/movespass.go+4 −2internal/hostops/mutation_classifier.go+1 −1internal/hostops/verify_classifier_test.go+42 −0internal/moves/offline.go+1 −1internal/ops/hibernate_verify.go+13 −5internal/ops/hibernate_verify_test.go+7 −7internal/ops/ops.go+8 −5internal/release/changeindex.go+10 −1internal/release/changeindex_test.go+5 −0
Intent
<task-notification> <task-id>a50620bd7d4400a46</task-id> <tool-use-id>toolu_01F9iYsDQHcdh6Se1sfvRbm6</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ab362fc0-f177-4a7f-a92e-4a29ea210aad/tasks/a50620bd7d4400a46.output</output-file> <status>completed</status> <summary>Agent "Implement slice 2a Task 3" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE Commit: e1d33194 `lease: VerifyReplicaAt restores pinned to a planned TXID (#898)` (not pushed). `Restore` now shares the new `restoreFrom` helper. Tests: the new pinned-restore and cancellation tests plus the existing VerifyReplica, CheckDB and Restore tests all pass. RED (wrong restore point, wrong error on cancel) and GREEN are in the report. gofmt, vet and `go build ./...` are clean. Concerns: 1. In WAL mode SQLite doesn't move the change counter: restores at batch two and batch three both read 5. The brief's counter check alone can't tell them apart, so the test also checks the newest transaction the restore actually read. This also means the drill's `stale` verdict will rarely be proved on WAL databases. That fails safe: an alarm can be missed, but never raised falsely. 2. Compaction between `PlanFrom` and the restore could make the planned TXID unrestorable. The plan would then record it as `failed`/`restore_error`. The error can be told apart (it wraps `ErrTxNotAvailable`), so the drill task may want to treat it differently. 3. `internal/lease` is a root-binary package; census testdata is left to Task 9. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/member-backups-2a/.superpowers/sdd/2026-11-11-member-backups-slice2a/task-3-report.md</result> <usage><subagent_tokens>127828</subagent_tokens><tool_uses>34</tool_uses><duration_ms>574376</duration_ms></usage> </task-notification>
<task-notification> <task-id>b9zujii67</task-id> <tool-use-id>toolu_01GUUeqvGbt24XtpyBVEuhgQ</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ab362fc0-f177-4a7f-a92e-4a29ea210aad/tasks/b9zujii67.output</output-file> <status>killed</status> <summary>Background command "Wait for slice 1 copy review result" was stopped after reaching its background time limit</summary> <note>If the work in progress still needs it, start it again with `run_in_background` and a longer `timeout`. If it already had the longest `timeout` allowed, do not restart it. Either way, report that it was stopped.</note> </task-notification>
<task-notification> <task-id>bgxgdnlcy</task-id> <tool-use-id>toolu_017WCWozLAvJ9UUvN7Tc1jG9</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ab362fc0-f177-4a7f-a92e-4a29ea210aad/tasks/bgxgdnlcy.output</output-file> <status>killed</status> <summary>Background command "Wait for slice 2a copy review result" was stopped after reaching its background time limit</summary> <note>If the work in progress still needs it, start it again with `run_in_background` and a longer `timeout`. If it already had the longest `timeout` allowed, do not restart it. Either way, report that it was stopped.</note> </task-notification>
Attributed to this commit: 10.2 g CO₂e (1,271,799 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on member-backups-2a, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/dbverify/dbverify.go b/internal/dbverify/dbverify.go| index 154b2190e..8aebd9ecc 100644 |
| --- a/internal/dbverify/dbverify.go |
| +++ b/internal/dbverify/dbverify.go |
| @@ -1,14 +1,15 @@ |
| // Package dbverify is the fleet's restore-drill record: which databases have |
| -// been PROVEN restorable, when, and which could not be decided. |
| +// been PROVEN restorable, when, and which could not be decided. Each box |
| +// keeps its own record, control/verify/<ref>.json, written only by that box. |
| // |
| -// It exists as its own package for the reason internal/usage does — the |
| -// writer (the box-side `carlos ops restore-verify` pass) and any future |
| +// It exists as its own package for the reason internal/usage does: the |
| +// writer (the box-side `carlos ops hibernate-verify` pass) and any future |
| // reader (the console) must agree on one shape and one key without either |
| // importing the other's machinery. |
| // |
| // The vocabulary is deliberately three-valued, following |
| -// usage.AppStorage.StoreUnswept: a replica is `ok`, `failed`, or |
| -// `unverified` — never a silent absence and never a 0/false that reads as |
| +// usage.AppStorage.StoreUnswept: a replica is `passed`, `failed`, or |
| +// `unverified`, never a silent absence and never a 0/false that reads as |
| // "fine". A pass that could not reach S3 has learned NOTHING about the |
| // replica, and saying so is the whole point of keeping this record. |
| package dbverify |
| @@ -20,78 +21,215 @@ import ( |
| "fmt" |
| "os" |
| "time" |
| + "unicode" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| -// Key is where the record lives in the deployment bucket, beside |
| -// control/serial and control/registry-snapshot.json. |
| -const Key = "control/restore-verify.json" |
| +// keyPrefix is where every box's record lives in the deployment bucket, |
| +// beside control/serial and control/registry-snapshot.json. |
| +const keyPrefix = "control/verify/" |
| + |
| +// ErrInvalidRef refuses a box reference that cannot be one segment of a |
| +// bucket key. |
| +var ErrInvalidRef = errors.New("dbverify: not a valid box reference") |
| + |
| +// KeyFor is the key of ref's record, control/verify/<ref>.json. Each box |
| +// writes only its own, so two boxes publishing at once never erase each |
| +// other's verdicts. An invalid ref (validRef) gets "", which Load, Publish |
| +// and RemoveHost refuse with ErrInvalidRef. |
| +func KeyFor(ref string) string { |
| + if !validRef(ref) { |
| + return "" |
| + } |
| + return keyPrefix + ref + ".json" |
| +} |
| + |
| +// validRef is boxself.ValidRef, repeated so this leaf package imports |
| +// nothing new: non-empty, at most 128 bytes, printable, with no space, |
| +// slash or backslash, and not "." or "..". |
| +func validRef(ref string) bool { |
| + if ref == "" || len(ref) > 128 || ref == "." || ref == ".." { |
| + return false |
| + } |
| + for _, r := range ref { |
| + if r == '/' || r == '\\' || unicode.IsSpace(r) || !unicode.IsPrint(r) { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +// The three results. |
| +const ( |
| + ResultPassed = "passed" |
| + ResultFailed = "failed" |
| + ResultUnverified = "unverified" |
| +) |
| + |
| +// The freshness a restore found. Never "current": stale only when proved, |
| +// not_stale only when the comparison ran and found the copy keeping up, |
| +// unknown otherwise. |
| +const ( |
| + FreshnessStale = "stale" |
| + FreshnessUnknown = "unknown" |
| + FreshnessNotStale = "not_stale" |
| +) |
| + |
| +// ReasonNotReached is an unverified verdict for a host the run did not |
| +// get to, or abandoned when it ran out of time. It learned nothing about the replica, so it does not count |
| +// toward UnverifiedRuns. |
| +const ReasonNotReached = "not_reached" |
| + |
| +// Identity is the instance a verdict is about. It mirrors dbreads.Identity |
| +// field for field (the drill converts one to the other), so a host deleted and |
| +// recreated under the same name is a different instance: its incarnation |
| +// differs. |
| +type Identity struct { |
| + Account string `json:"account,omitempty"` |
| + App string `json:"app,omitempty"` |
| + Host string `json:"host,omitempty"` |
| + Region string `json:"region,omitempty"` |
| + Incarnation string `json:"incarnation,omitempty"` |
| +} |
| // Verdict is one database's standing. |
| type Verdict struct { |
| - // OK is true only when the replica was actually restored and passed its |
| - // integrity check on this box, at VerifiedAt. |
| - OK bool `json:"ok"` |
| - // Unverified marks "we could not tell" — an S3 error, a prefix that has |
| - // never been replicated to, or a host the run's cap did not reach. It is |
| - // never a failure and never a pass. |
| - Unverified bool `json:"unverified,omitempty"` |
| - // VerifiedAt is when this verdict was reached — NOT when the replica was |
| - // written. An old timestamp on an ok verdict is itself a finding. |
| - VerifiedAt time.Time `json:"verified_at"` |
| - // Detail carries the failure or the reason it could not be decided. |
| + // Result is passed, failed or unverified. Passed only when the replica |
| + // was actually restored and passed its integrity check on this box. |
| + // Unverified is "we could not tell" and is never a failure and never |
| + // a pass. |
| + Result string `json:"result"` |
| + // Reason says why a verdict is failed or unverified, as a code |
| + // (ReasonNotReached and the drill's own). |
| + Reason string `json:"reason,omitempty"` |
| + // Identity is the instance the bracket captured; zero when the |
| + // capture itself could not decide one. |
| + Identity Identity `json:"identity"` |
| + // Prefix is the replica prefix restored from. |
| + Prefix string `json:"prefix,omitempty"` |
| + // BracketOpenedAt is when the identity was captured, before planning. |
| + BracketOpenedAt time.Time `json:"bracket_opened_at,omitzero"` |
| + // VerifiedAt is when this verdict was reached, NOT when the replica |
| + // was written. An old timestamp on a pass is itself a finding. |
| + VerifiedAt time.Time `json:"verified_at,omitzero"` |
| + // TXID and WrittenAt are the restored transaction and its header |
| + // time; RestoredBytes is the restored database's size. |
| + TXID uint64 `json:"txid,omitempty"` |
| + WrittenAt time.Time `json:"written_at,omitzero"` |
| + RestoredBytes int64 `json:"restored_bytes,omitempty"` |
| + // Freshness is stale, unknown or not_stale. |
| + Freshness string `json:"freshness,omitempty"` |
| + // Detail is free text for an operator reading the record: the error a |
| + // failure or an unverified verdict came from. Not copy; Reason is |
| + // what readers key on. |
| Detail string `json:"detail,omitempty"` |
| + |
| + // The bookkeeping Merge keeps, whatever the caller set. |
| + // |
| + // UnverifiedRuns is how many runs in a row ended unverified for a |
| + // reason other than not_reached; any other result sets it to 0. |
| + UnverifiedRuns int `json:"unverified_runs"` |
| + // FirstSeen is the first run that counted this host, for this |
| + // identity. |
| + FirstSeen time.Time `json:"first_seen"` |
| + // LastDecidedAt is the VerifiedAt of the last passed or failed |
| + // verdict for this identity; unverified runs leave it where it was. |
| + LastDecidedAt time.Time `json:"last_decided_at,omitzero"` |
| } |
| -// Record is the whole box's standing at CheckedAt. |
| +// OK reports a pass. |
| +func (v Verdict) OK() bool { return v.Result == ResultPassed } |
| + |
| +// Unverified reports a verdict the run could not decide. |
| +func (v Verdict) Unverified() bool { return v.Result == ResultUnverified } |
| + |
| +// Record is one box's standing at CheckedAt, at KeyFor(Box). |
| type Record struct { |
| - V int `json:"v"` // 1 |
| + V int `json:"v"` // 2 |
| Box string `json:"box"` |
| CheckedAt time.Time `json:"checked_at"` |
| Hosts map[string]Verdict `json:"hosts"` |
| } |
| -// Merge folds this run's verdicts into the prior record. prior may be nil. |
| +// Merge folds a run's verdicts into the prior record of box ref. prior may |
| +// be nil. Every publish within one run passes the record loaded at the run's |
| +// start as prior, never the run's own last publish, so a host's streak moves |
| +// at most once per run. |
| // |
| -// Two rules, both about not overstating coverage: |
| +// Coverage is never overstated: |
| // |
| -// - A capped run visits only some hosts, so untouched hosts KEEP their |
| -// previous verdict — with its old timestamp, which is what makes staleness |
| +// - A run visits only some hosts, so untouched hosts KEEP their previous |
| +// verdict, with its old timestamp, which is what makes staleness |
| // visible rather than hidden behind a fresh CheckedAt. |
| -// - A host no longer in `fleet` is DROPPED. A retired instance's last "ok" |
| -// would otherwise sit in the record forever, padding the coverage of a |
| -// database that no longer exists. |
| -func Merge(prior *Record, box string, now time.Time, fleet []string, results map[string]Verdict) *Record { |
| - rec := &Record{V: 1, Box: box, CheckedAt: now.UTC(), Hosts: map[string]Verdict{}} |
| - inFleet := map[string]bool{} |
| - for _, h := range fleet { |
| - inFleet[h] = true |
| - } |
| - if prior != nil { |
| - for host, v := range prior.Hosts { |
| - if inFleet[host] { |
| - rec.Hosts[host] = v |
| - } |
| +// - A host no longer in fleet is DROPPED. A retired instance's last pass |
| +// would otherwise sit in the record forever. |
| +// - A fleet host with no verdict at all is added unverified, not_reached, |
| +// first seen now, so it is counted rather than absent. |
| +// |
| +// For each result, Merge keeps the host's bookkeeping: FirstSeen carries |
| +// over, LastDecidedAt advances only on passed or failed, and |
| +// UnverifiedRuns counts unverified runs (not not_reached ones) in a row. |
| +// A result about a different identity than the prior verdict starts all |
| +// three over. A zero identity on either side is unknown, not different. |
| +func Merge(prior *Record, ref string, now time.Time, fleet []string, results map[string]Verdict) *Record { |
| + now = now.UTC() |
| + rec := &Record{V: 2, Box: ref, CheckedAt: now, Hosts: map[string]Verdict{}} |
| + for _, host := range fleet { |
| + var p Verdict |
| + had := false |
| + if prior != nil { |
| + p, had = prior.Hosts[host] |
| } |
| - } |
| - for host, v := range results { |
| - if !inFleet[host] { |
| - continue |
| + r, visited := results[host] |
| + switch { |
| + case visited: |
| + rec.Hosts[host] = mergeOne(p, had, r, now) |
| + case had: |
| + rec.Hosts[host] = p |
| + default: |
| + rec.Hosts[host] = Verdict{Result: ResultUnverified, Reason: ReasonNotReached, FirstSeen: now} |
| } |
| - rec.Hosts[host] = v |
| } |
| return rec |
| } |
| -// Failures counts the verdicts that are an alarm — restored and damaged. |
| +func mergeOne(p Verdict, had bool, r Verdict, now time.Time) Verdict { |
| + if had && p.Identity != (Identity{}) && r.Identity != (Identity{}) && p.Identity != r.Identity { |
| + had = false |
| + } |
| + if !had { |
| + p = Verdict{FirstSeen: now} |
| + } |
| + r.FirstSeen = p.FirstSeen |
| + if r.FirstSeen.IsZero() { |
| + r.FirstSeen = now |
| + } |
| + r.LastDecidedAt = p.LastDecidedAt |
| + r.UnverifiedRuns = 0 |
| + switch r.Result { |
| + case ResultPassed, ResultFailed: |
| + r.LastDecidedAt = r.VerifiedAt |
| + if r.LastDecidedAt.IsZero() { |
| + r.LastDecidedAt = now |
| + } |
| + case ResultUnverified: |
| + if r.Reason != ReasonNotReached { |
| + r.UnverifiedRuns = p.UnverifiedRuns + 1 |
| + } |
| + } |
| + return r |
| +} |
| + |
| +// Failures counts the verdicts that are an alarm: restored and damaged, or |
| +// a result this version does not know, which is never read as fine. |
| // Unverified rows are deliberately not counted here: they are a coverage |
| // problem, not a corruption one, and conflating them would either cry wolf or |
| // train operators to ignore a red run. |
| func (r *Record) Failures() int { |
| n := 0 |
| for _, v := range r.Hosts { |
| - if !v.OK && !v.Unverified { |
| + if !v.OK() && !v.Unverified() { |
| n++ |
| } |
| } |
| @@ -102,17 +240,21 @@ func (r *Record) Failures() int { |
| func (r *Record) Unverified() int { |
| n := 0 |
| for _, v := range r.Hosts { |
| - if v.Unverified { |
| + if v.Unverified() { |
| n++ |
| } |
| } |
| return n |
| } |
| -// Load reads the current record. A missing key is (nil, nil) — a deployment |
| -// that has never run the drill, not an error. |
| -func Load(ctx context.Context, s release.Store) (*Record, error) { |
| - body, _, err := s.Get(ctx, Key) |
| +// Load reads box ref's record. A missing key is (nil, nil): a box that has |
| +// never published, not an error. |
| +func Load(ctx context.Context, s release.Store, ref string) (*Record, error) { |
| + key := KeyFor(ref) |
| + if key == "" { |
| + return nil, fmt.Errorf("%w: %q", ErrInvalidRef, ref) |
| + } |
| + body, _, err := s.Get(ctx, key) |
| if errors.Is(err, os.ErrNotExist) { |
| return nil, nil |
| } |
| @@ -121,29 +263,35 @@ func Load(ctx context.Context, s release.Store) (*Record, error) { |
| } |
| var rec Record |
| if err := json.Unmarshal(body, &rec); err != nil { |
| - return nil, fmt.Errorf("parse %s: %w", Key, err) |
| + return nil, fmt.Errorf("parse %s: %w", key, err) |
| } |
| return &rec, nil |
| } |
| -// RemoveHost drops host's verdict from the record: a moved instance's |
| -// replica is no longer this fleet's to verify where it was (moves spec |
| -// §6.3, §7.6 local). The load, the delete and the put run inside each CAS |
| -// attempt (moves Plan 2 ruling G31), so a record another writer changed |
| -// between the read and the write is re-read, never overwritten with a stale |
| -// body. No record, or no entry for the host, is nothing to do. |
| -func RemoveHost(ctx context.Context, s release.Store, host string) error { |
| +// RemoveHost drops host's verdict from box ref's own record: a moved |
| +// instance's replica is no longer this box's to verify (moves spec §6.3, |
| +// §7.6 local). Other boxes' records are never touched; their verdicts for |
| +// the host leave at their next run, when Merge drops hosts no longer in |
| +// their fleet. The load, the delete and the put run inside each CAS |
| +// attempt (moves Plan 2 ruling G31), so a record changed between the read |
| +// and the write is re-read, never overwritten with a stale body. No |
| +// record, or no entry for the host, is nothing to do. |
| +func RemoveHost(ctx context.Context, s release.Store, ref, host string) error { |
| + key := KeyFor(ref) |
| + if key == "" { |
| + return fmt.Errorf("%w: %q", ErrInvalidRef, ref) |
| + } |
| for attempt := 0; attempt < 3; attempt++ { |
| - body, etag, err := s.Get(ctx, Key) |
| + body, etag, err := s.Get(ctx, key) |
| if errors.Is(err, os.ErrNotExist) { |
| return nil |
| } |
| if err != nil { |
| - return fmt.Errorf("get %s: %w", Key, err) |
| + return fmt.Errorf("get %s: %w", key, err) |
| } |
| var rec Record |
| if err := json.Unmarshal(body, &rec); err != nil { |
| - return fmt.Errorf("parse %s: %w", Key, err) |
| + return fmt.Errorf("parse %s: %w", key, err) |
| } |
| if _, ok := rec.Hosts[host]; !ok { |
| return nil |
| @@ -153,35 +301,40 @@ func RemoveHost(ctx context.Context, s release.Store, host string) error { |
| if err != nil { |
| return err |
| } |
| - if _, err := s.Put(ctx, Key, next, etag); err == nil { |
| + if _, err := s.Put(ctx, key, next, etag); err == nil { |
| return nil |
| } else if !errors.Is(err, release.ErrCASConflict) { |
| - return fmt.Errorf("put %s: %w", Key, err) |
| + return fmt.Errorf("put %s: %w", key, err) |
| } |
| } |
| - return fmt.Errorf("put %s: lost three CAS races", Key) |
| + return fmt.Errorf("put %s: lost three CAS races", key) |
| } |
| -// Publish writes rec, replacing whatever is there. Three attempts on a lost |
| -// CAS race, the recipe usage.UpsertEdgeDay and host.putReplacing both use. |
| +// Publish writes rec at KeyFor(rec.Box), replacing that box's whole |
| +// record and nothing else. Three attempts on a lost CAS race, the recipe |
| +// usage.UpsertEdgeDay and host.putReplacing both use. |
| func Publish(ctx context.Context, s release.Store, rec *Record) error { |
| + key := KeyFor(rec.Box) |
| + if key == "" { |
| + return fmt.Errorf("%w: %q", ErrInvalidRef, rec.Box) |
| + } |
| body, err := json.Marshal(rec) |
| if err != nil { |
| return err |
| } |
| for attempt := 0; attempt < 3; attempt++ { |
| - _, etag, err := s.Get(ctx, Key) |
| + _, etag, err := s.Get(ctx, key) |
| switch { |
| case errors.Is(err, os.ErrNotExist): |
| etag = "" |
| case err != nil: |
| - return fmt.Errorf("get %s: %w", Key, err) |
| + return fmt.Errorf("get %s: %w", key, err) |
| } |
| - if _, err := s.Put(ctx, Key, body, etag); err == nil { |
| + if _, err := s.Put(ctx, key, body, etag); err == nil { |
| return nil |
| } else if !errors.Is(err, release.ErrCASConflict) { |
| - return fmt.Errorf("put %s: %w", Key, err) |
| + return fmt.Errorf("put %s: %w", key, err) |
| } |
| } |
| - return fmt.Errorf("put %s: lost three CAS races", Key) |
| + return fmt.Errorf("put %s: lost three CAS races", key) |
| } |
diff --git a/internal/dbverify/dbverify_test.go b/internal/dbverify/dbverify_test.go| index c9cb5a8b2..671a2b6b7 100644 |
| --- a/internal/dbverify/dbverify_test.go |
| +++ b/internal/dbverify/dbverify_test.go |
| @@ -1,7 +1,11 @@ |
| package dbverify |
| import ( |
| + "bytes" |
| "context" |
| + "errors" |
| + "slices" |
| + "strings" |
| "testing" |
| "time" |
| @@ -16,18 +20,47 @@ func at(s string) time.Time { |
| return t |
| } |
| -// A capped run must not erase the hosts it did not reach — and must not |
| +var ( |
| + idA = Identity{Account: "bab", App: "shop", Host: "a.example.com", Region: "eu-west-1", Incarnation: "1"} |
| + idB = Identity{Account: "bab", App: "shop", Host: "a.example.com", Region: "eu-west-1", Incarnation: "2"} |
| +) |
| + |
| +func passed(id Identity, when time.Time) Verdict { |
| + return Verdict{Result: ResultPassed, Identity: id, VerifiedAt: when} |
| +} |
| + |
| +func failed(id Identity, when time.Time) Verdict { |
| + return Verdict{Result: ResultFailed, Reason: "restore_error", Identity: id, VerifiedAt: when} |
| +} |
| + |
| +func unverified(id Identity, reason string, when time.Time) Verdict { |
| + return Verdict{Result: ResultUnverified, Reason: reason, Identity: id, VerifiedAt: when} |
| +} |
| + |
| +// One record per box, at a key only that box writes. |
| +func TestKeyFor(t *testing.T) { |
| + if got := KeyFor("box-a"); got != "control/verify/box-a.json" { |
| + t.Fatalf("KeyFor(box-a) = %q", got) |
| + } |
| + for _, ref := range []string{"", ".", "..", "a/b", `a\b`, "a b", "a\tb", "a\x00b", strings.Repeat("x", 129)} { |
| + if got := KeyFor(ref); got != "" { |
| + t.Errorf("KeyFor(%q) = %q, want a refusal", ref, got) |
| + } |
| + } |
| +} |
| + |
| +// A capped run must not erase the hosts it did not reach, and must not |
| // restamp them either. The old timestamp is what makes thin coverage visible. |
| func TestMergeKeepsUnvisitedHostsWithTheirOldTimestamp(t *testing.T) { |
| old := at("2026-08-01T00:00:00Z") |
| - prior := &Record{V: 1, Box: "box-a", CheckedAt: old, Hosts: map[string]Verdict{ |
| - "a.example.com": {OK: true, VerifiedAt: old}, |
| - "b.example.com": {OK: true, VerifiedAt: old}, |
| - }} |
| + prior := Merge(nil, "box-a", old, []string{"a.example.com", "b.example.com"}, map[string]Verdict{ |
| + "a.example.com": passed(idA, old), |
| + "b.example.com": passed(Identity{Host: "b.example.com", Incarnation: "1"}, old), |
| + }) |
| now := at("2026-08-15T09:00:00Z") |
| rec := Merge(prior, "box-a", now, []string{"a.example.com", "b.example.com"}, |
| - map[string]Verdict{"a.example.com": {OK: true, VerifiedAt: now}}) |
| + map[string]Verdict{"a.example.com": passed(idA, now)}) |
| if got := rec.Hosts["a.example.com"].VerifiedAt; !got.Equal(now) { |
| t.Fatalf("visited host kept the old stamp: %v", got) |
| @@ -36,45 +69,215 @@ func TestMergeKeepsUnvisitedHostsWithTheirOldTimestamp(t *testing.T) { |
| if !ok { |
| t.Fatal("a host the capped run never reached was dropped from the record") |
| } |
| - if !b.VerifiedAt.Equal(old) { |
| - t.Fatalf("an unvisited host was restamped %v — its coverage would read as fresh", b.VerifiedAt) |
| + if b != prior.Hosts["b.example.com"] { |
| + t.Fatalf("an unvisited host was changed: %+v, was %+v", b, prior.Hosts["b.example.com"]) |
| } |
| } |
| -// A retired instance's last "ok" must not linger and pad the coverage of a |
| +// A retired instance's last pass must not linger and pad the coverage of a |
| // database that no longer exists. |
| func TestMergeDropsHostsNoLongerInTheFleet(t *testing.T) { |
| old := at("2026-08-01T00:00:00Z") |
| - prior := &Record{V: 1, Hosts: map[string]Verdict{ |
| - "gone.example.com": {OK: true, VerifiedAt: old}, |
| - "live.example.com": {OK: true, VerifiedAt: old}, |
| + prior := &Record{V: 2, Hosts: map[string]Verdict{ |
| + "gone.example.com": passed(Identity{}, old), |
| + "live.example.com": passed(Identity{}, old), |
| }} |
| - rec := Merge(prior, "box-a", at("2026-08-15T09:00:00Z"), []string{"live.example.com"}, nil) |
| + rec := Merge(prior, "box-a", at("2026-08-15T09:00:00Z"), []string{"live.example.com"}, |
| + map[string]Verdict{"other.example.com": passed(Identity{}, old)}) |
| if _, ok := rec.Hosts["gone.example.com"]; ok { |
| t.Fatal("a retired host kept its verdict") |
| } |
| + if _, ok := rec.Hosts["other.example.com"]; ok { |
| + t.Fatal("a result for a host outside the fleet was recorded") |
| + } |
| if _, ok := rec.Hosts["live.example.com"]; !ok { |
| t.Fatal("a live host lost its verdict") |
| } |
| + if rec.V != 2 || rec.Box != "box-a" { |
| + t.Fatalf("record header: v %d box %q", rec.V, rec.Box) |
| + } |
| +} |
| + |
| +// A fleet host this box has never verdicted is counted, not absent: it is |
| +// unverified, not reached, first seen now, so coverage can be measured |
| +// from the day it appeared. |
| +func TestMergeAddsNeverSeenFleetHostsAsNotReached(t *testing.T) { |
| + now := at("2026-08-15T09:00:00Z") |
| + rec := Merge(nil, "box-a", now, []string{"new.example.com"}, nil) |
| + v, ok := rec.Hosts["new.example.com"] |
| + if !ok { |
| + t.Fatal("a never-seen fleet host is missing from the record") |
| + } |
| + if v.Result != ResultUnverified || v.Reason != ReasonNotReached || !v.FirstSeen.Equal(now) || v.UnverifiedRuns != 0 || !v.LastDecidedAt.IsZero() { |
| + t.Fatalf("never-seen host: %+v", v) |
| + } |
| + if rec.Unverified() != 1 || rec.Failures() != 0 { |
| + t.Fatalf("Unverified %d Failures %d", rec.Unverified(), rec.Failures()) |
| + } |
| + |
| + // A later run keeps its first sighting, whatever it finds. |
| + later := now.Add(24 * time.Hour) |
| + rec = Merge(rec, "box-a", later, []string{"new.example.com"}, |
| + map[string]Verdict{"new.example.com": unverified(idA, "nothing_replicated", later)}) |
| + if got := rec.Hosts["new.example.com"].FirstSeen; !got.Equal(now) { |
| + t.Fatalf("FirstSeen = %v, want the first sighting %v", got, now) |
| + } |
| + rec = Merge(rec, "box-a", later.Add(24*time.Hour), []string{"new.example.com"}, |
| + map[string]Verdict{"new.example.com": passed(idA, later.Add(24*time.Hour))}) |
| + if got := rec.Hosts["new.example.com"].FirstSeen; !got.Equal(now) { |
| + t.Fatalf("FirstSeen after a pass = %v, want %v", got, now) |
| + } |
| +} |
| + |
| +// The streak counts runs that tried and could not decide; a decision or a |
| +// run that never reached the host ends it. |
| +func TestMergeUnverifiedRuns(t *testing.T) { |
| + fleet := []string{"a.example.com"} |
| + day := at("2026-08-01T03:00:00Z") |
| + var rec *Record |
| + run := func(v Verdict) Verdict { |
| + rec = Merge(rec, "box-a", day, fleet, map[string]Verdict{"a.example.com": v}) |
| + day = day.Add(24 * time.Hour) |
| + return rec.Hosts["a.example.com"] |
| + } |
| + for i := 1; i <= 3; i++ { |
| + if got := run(unverified(idA, "record_changed", day)).UnverifiedRuns; got != i { |
| + t.Fatalf("after %d unverified runs UnverifiedRuns = %d", i, got) |
| + } |
| + } |
| + if got := run(passed(idA, day)).UnverifiedRuns; got != 0 { |
| + t.Fatalf("a pass left UnverifiedRuns = %d", got) |
| + } |
| + run(unverified(idA, "moving", day)) |
| + if got := run(failed(idA, day)).UnverifiedRuns; got != 0 { |
| + t.Fatalf("a failure left UnverifiedRuns = %d", got) |
| + } |
| + run(unverified(idA, "moving", day)) |
| + if got := run(unverified(idA, ReasonNotReached, day)).UnverifiedRuns; got != 0 { |
| + t.Fatalf("not_reached counted toward the streak: %d", got) |
| + } |
| + if got := run(unverified(idA, ReasonNotReached, day)).UnverifiedRuns; got != 0 { |
| + t.Fatalf("two not_reached runs counted: %d", got) |
| + } |
| +} |
| + |
| +// LastDecidedAt is the last time a restore was decided, passed or failed. |
| +// Undecided nights leave it where it was, which is what lets coverage |
| +// alert on a host that has been unverified for a week. |
| +func TestMergeLastDecidedAt(t *testing.T) { |
| + fleet := []string{"a.example.com"} |
| + pass := at("2026-08-01T03:00:00Z") |
| + rec := Merge(nil, "box-a", pass, fleet, map[string]Verdict{"a.example.com": passed(idA, pass)}) |
| + if got := rec.Hosts["a.example.com"].LastDecidedAt; !got.Equal(pass) { |
| + t.Fatalf("a pass set LastDecidedAt = %v, want %v", got, pass) |
| + } |
| + night5 := pass.Add(5 * 24 * time.Hour) |
| + night6 := pass.Add(6 * 24 * time.Hour) |
| + rec = Merge(rec, "box-a", night5, fleet, map[string]Verdict{"a.example.com": unverified(idA, "nothing_replicated", night5)}) |
| + rec = Merge(rec, "box-a", night6, fleet, map[string]Verdict{"a.example.com": unverified(idA, "restore_unreachable", night6)}) |
| + v := rec.Hosts["a.example.com"] |
| + if !v.LastDecidedAt.Equal(pass) { |
| + t.Fatalf("unverified nights moved LastDecidedAt to %v, want the pass's %v", v.LastDecidedAt, pass) |
| + } |
| + if !v.VerifiedAt.Equal(night6) || v.Result != ResultUnverified { |
| + t.Fatalf("the latest verdict was not recorded: %+v", v) |
| + } |
| + |
| + // A failure is a decision too. |
| + night7 := pass.Add(7 * 24 * time.Hour) |
| + rec = Merge(rec, "box-a", night7, fleet, map[string]Verdict{"a.example.com": failed(idA, night7)}) |
| + if got := rec.Hosts["a.example.com"].LastDecidedAt; !got.Equal(night7) { |
| + t.Fatalf("a failure set LastDecidedAt = %v, want %v", got, night7) |
| + } |
| +} |
| + |
| +// A verdict about another incarnation says nothing about this one: the |
| +// first sighting, the last decision and the streak all start over. |
| +func TestMergeIdentityChangeResets(t *testing.T) { |
| + fleet := []string{"a.example.com"} |
| + first := at("2026-08-01T03:00:00Z") |
| + rec := Merge(nil, "box-a", first, fleet, map[string]Verdict{"a.example.com": passed(idA, first)}) |
| + for i := 1; i <= 2; i++ { |
| + n := first.Add(time.Duration(i) * 24 * time.Hour) |
| + rec = Merge(rec, "box-a", n, fleet, map[string]Verdict{"a.example.com": unverified(idA, "record_changed", n)}) |
| + } |
| + if v := rec.Hosts["a.example.com"]; v.UnverifiedRuns != 2 || !v.LastDecidedAt.Equal(first) || !v.FirstSeen.Equal(first) { |
| + t.Fatalf("setup: %+v", v) |
| + } |
| + |
| + now := first.Add(3 * 24 * time.Hour) |
| + got := Merge(rec, "box-a", now, fleet, map[string]Verdict{"a.example.com": unverified(idB, "nothing_replicated", now)}).Hosts["a.example.com"] |
| + if !got.FirstSeen.Equal(now) || !got.LastDecidedAt.IsZero() || got.UnverifiedRuns != 1 || got.Identity != idB { |
| + t.Fatalf("a new incarnation's unverified verdict: %+v", got) |
| + } |
| + |
| + got = Merge(rec, "box-a", now, fleet, map[string]Verdict{"a.example.com": passed(idB, now)}).Hosts["a.example.com"] |
| + if !got.FirstSeen.Equal(now) || !got.LastDecidedAt.Equal(now) || got.UnverifiedRuns != 0 || got.Identity != idB { |
| + t.Fatalf("a new incarnation's pass: %+v", got) |
| + } |
| +} |
| + |
| +// A verdict with no identity (the capture itself could not decide one) is |
| +// not evidence of a different instance, and neither is a prior that never |
| +// had one (a not_reached placeholder): nothing resets, so a host that |
| +// cannot be captured for a week still trips coverage on time. |
| +func TestMergeUnknownIdentityIsNotAChange(t *testing.T) { |
| + fleet := []string{"a.example.com"} |
| + first := at("2026-08-01T03:00:00Z") |
| + rec := Merge(nil, "box-a", first, fleet, map[string]Verdict{"a.example.com": passed(idA, first)}) |
| + n1 := first.Add(24 * time.Hour) |
| + rec = Merge(rec, "box-a", n1, fleet, map[string]Verdict{"a.example.com": unverified(Identity{}, "moving", n1)}) |
| + n2 := first.Add(48 * time.Hour) |
| + rec = Merge(rec, "box-a", n2, fleet, map[string]Verdict{"a.example.com": unverified(idA, "nothing_replicated", n2)}) |
| + v := rec.Hosts["a.example.com"] |
| + if !v.FirstSeen.Equal(first) || !v.LastDecidedAt.Equal(first) || v.UnverifiedRuns != 2 { |
| + t.Fatalf("an unknown identity reset the host: %+v", v) |
| + } |
| + |
| + placeholder := Merge(nil, "box-a", first, fleet, nil) |
| + v = Merge(placeholder, "box-a", n1, fleet, map[string]Verdict{"a.example.com": unverified(idA, "nothing_replicated", n1)}).Hosts["a.example.com"] |
| + if !v.FirstSeen.Equal(first) || v.UnverifiedRuns != 1 { |
| + t.Fatalf("the first real verdict after a not_reached placeholder: %+v", v) |
| + } |
| +} |
| + |
| +// Merge owns the bookkeeping: whatever the caller put in FirstSeen, |
| +// LastDecidedAt or UnverifiedRuns is replaced. |
| +func TestMergeOwnsTheBookkeeping(t *testing.T) { |
| + now := at("2026-08-15T09:00:00Z") |
| + in := unverified(idA, "moving", now) |
| + in.FirstSeen, in.LastDecidedAt, in.UnverifiedRuns = at("2020-01-01T00:00:00Z"), at("2020-01-01T00:00:00Z"), 40 |
| + v := Merge(nil, "box-a", now, []string{"a.example.com"}, map[string]Verdict{"a.example.com": in}).Hosts["a.example.com"] |
| + if !v.FirstSeen.Equal(now) || !v.LastDecidedAt.IsZero() || v.UnverifiedRuns != 1 { |
| + t.Fatalf("Merge kept the caller's bookkeeping: %+v", v) |
| + } |
| } |
| // The three-valued vocabulary is the point: unreachable is not a pass and not |
| // an alarm. |
| func TestUnverifiedIsNeitherPassNorFailure(t *testing.T) { |
| rec := &Record{Hosts: map[string]Verdict{ |
| - "ok.example.com": {OK: true}, |
| - "bad.example.com": {OK: false, Detail: "integrity_check: page 4 corrupt"}, |
| - "unknown.example.com": {Unverified: true, Detail: "s3: connection refused"}, |
| + "ok.example.com": {Result: ResultPassed}, |
| + "bad.example.com": {Result: ResultFailed, Reason: "restore_error"}, |
| + "unknown.example.com": {Result: ResultUnverified, Reason: "nothing_replicated"}, |
| }} |
| if got := rec.Failures(); got != 1 { |
| - t.Fatalf("Failures() = %d, want 1 — only the damaged replica is an alarm", got) |
| + t.Fatalf("Failures() = %d, want 1: only the damaged replica is an alarm", got) |
| } |
| if got := rec.Unverified(); got != 1 { |
| t.Fatalf("Unverified() = %d, want 1", got) |
| } |
| - if rec.Hosts["unknown.example.com"].OK { |
| + if v := rec.Hosts["unknown.example.com"]; v.OK() || !v.Unverified() { |
| t.Fatal("an undecided verdict reported OK") |
| } |
| + if v := rec.Hosts["ok.example.com"]; !v.OK() || v.Unverified() { |
| + t.Fatal("a pass did not report OK") |
| + } |
| + // A result this version does not know is never read as fine. |
| + odd := &Record{Hosts: map[string]Verdict{"x.example.com": {Result: "current"}}} |
| + if odd.Failures() != 1 || odd.Hosts["x.example.com"].OK() { |
| + t.Fatal("an unknown result read as a pass") |
| + } |
| } |
| func TestLoadPublishRoundTrip(t *testing.T) { |
| @@ -82,7 +285,7 @@ func TestLoadPublishRoundTrip(t *testing.T) { |
| store := release.NewDirStore(t.TempDir()) |
| // Never run: absent is not an error. |
| - got, err := Load(ctx, store) |
| + got, err := Load(ctx, store, "box-a") |
| if err != nil { |
| t.Fatalf("Load on a fresh deployment: %v", err) |
| } |
| @@ -91,30 +294,44 @@ func TestLoadPublishRoundTrip(t *testing.T) { |
| } |
| now := at("2026-08-15T09:00:00Z") |
| - rec := Merge(nil, "box-a", now, []string{"a.example.com"}, |
| - map[string]Verdict{"a.example.com": {OK: true, VerifiedAt: now}}) |
| + full := Verdict{ |
| + Result: ResultPassed, Identity: idA, Prefix: "hib/a.example.com", |
| + BracketOpenedAt: now.Add(-time.Minute), VerifiedAt: now, TXID: 0x2a, |
| + WrittenAt: now.Add(-time.Hour), RestoredBytes: 4096, Freshness: FreshnessUnknown, |
| + Detail: "restored clean", |
| + } |
| + rec := Merge(nil, "box-a", now, []string{"a.example.com"}, map[string]Verdict{"a.example.com": full}) |
| if err := Publish(ctx, store, rec); err != nil { |
| t.Fatalf("Publish: %v", err) |
| } |
| - got, err = Load(ctx, store) |
| + got, err = Load(ctx, store, "box-a") |
| if err != nil { |
| t.Fatalf("Load: %v", err) |
| } |
| - if got == nil || !got.Hosts["a.example.com"].OK { |
| - t.Fatalf("round-trip lost the verdict: %+v", got) |
| + if got == nil || got.Hosts["a.example.com"] != rec.Hosts["a.example.com"] { |
| + t.Fatalf("round-trip lost the verdict: %+v, want %+v", got, rec.Hosts["a.example.com"]) |
| } |
| - if got.Box != "box-a" || !got.CheckedAt.Equal(now) { |
| + if got.V != 2 || got.Box != "box-a" || !got.CheckedAt.Equal(now) { |
| t.Fatalf("round-trip lost the provenance: %+v", got) |
| } |
| + body, _, err := store.Get(ctx, "control/verify/box-a.json") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, field := range []string{`"result":"passed"`, `"incarnation":"1"`, `"txid":42`, `"restored_bytes":4096`, `"freshness":"unknown"`, `"first_seen"`, `"last_decided_at"`, `"bracket_opened_at"`, `"written_at"`} { |
| + if !bytes.Contains(body, []byte(field)) { |
| + t.Errorf("the published record has no %s: %s", field, body) |
| + } |
| + } |
| // And a second publish replaces rather than conflicting. |
| rec2 := Merge(got, "box-a", now.Add(time.Hour), []string{"a.example.com"}, |
| - map[string]Verdict{"a.example.com": {OK: false, Detail: "integrity_check: bad"}}) |
| + map[string]Verdict{"a.example.com": failed(idA, now.Add(time.Hour))}) |
| if err := Publish(ctx, store, rec2); err != nil { |
| t.Fatalf("second Publish: %v", err) |
| } |
| - got, err = Load(ctx, store) |
| + got, err = Load(ctx, store, "box-a") |
| if err != nil { |
| t.Fatalf("Load after replace: %v", err) |
| } |
| @@ -123,6 +340,68 @@ func TestLoadPublishRoundTrip(t *testing.T) { |
| } |
| } |
| +func TestInvalidRefIsRefused(t *testing.T) { |
| + ctx := context.Background() |
| + store := release.NewDirStore(t.TempDir()) |
| + if _, err := Load(ctx, store, "a/b"); !errors.Is(err, ErrInvalidRef) { |
| + t.Fatalf("Load: %v", err) |
| + } |
| + if err := Publish(ctx, store, &Record{V: 2, Box: ".."}); !errors.Is(err, ErrInvalidRef) { |
| + t.Fatalf("Publish: %v", err) |
| + } |
| + if err := RemoveHost(ctx, store, "", "a.example.com"); !errors.Is(err, ErrInvalidRef) { |
| + t.Fatalf("RemoveHost: %v", err) |
| + } |
| + if keys, _ := store.List(ctx, ""); len(keys) != 0 { |
| + t.Fatalf("a refused call wrote %v", keys) |
| + } |
| +} |
| + |
| +// Two boxes publishing at once never touch each other's record: each |
| +// writes only its own key, so neither can erase the other's verdicts. |
| +func TestPublishWritesOnlyItsOwnKey(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + fleetA, fleetB := []string{"a.example.com"}, []string{"b.example.com"} |
| + var recA, recB *Record |
| + for i := 0; i < 3; i++ { |
| + now := at("2026-08-15T09:00:00Z").Add(time.Duration(i) * 10 * time.Minute) |
| + recA = Merge(recA, "box-a", now, fleetA, map[string]Verdict{"a.example.com": passed(idA, now)}) |
| + recB = Merge(recB, "box-b", now, fleetB, map[string]Verdict{"b.example.com": unverified(Identity{}, "moving", now)}) |
| + // box-b publishes between box-a's read and box-a's write. |
| + racing := releaseStoreFunc{Store: s, beforePut: func() { |
| + if err := Publish(ctx, s, recB); err != nil { |
| + t.Fatal(err) |
| + } |
| + }} |
| + if err := Publish(ctx, racing, recA); err != nil { |
| + t.Fatalf("box-a publish %d: %v", i, err) |
| + } |
| + } |
| + gotA, err := Load(ctx, s, "box-a") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + gotB, err := Load(ctx, s, "box-b") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if gotA.Box != "box-a" || len(gotA.Hosts) != 1 || !gotA.Hosts["a.example.com"].OK() { |
| + t.Fatalf("box-a's record: %+v", gotA) |
| + } |
| + if gotB.Box != "box-b" || len(gotB.Hosts) != 1 || !gotB.Hosts["b.example.com"].Unverified() || gotB.Hosts["b.example.com"].UnverifiedRuns != 3 { |
| + t.Fatalf("box-b's record: %+v", gotB) |
| + } |
| + keys, err := s.List(ctx, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + slices.Sort(keys) |
| + if want := []string{"control/verify/box-a.json", "control/verify/box-b.json"}; !slices.Equal(keys, want) { |
| + t.Fatalf("keys written: %v, want %v", keys, want) |
| + } |
| +} |
| + |
| // A moved host's restore-verification entry is removed by the landing |
| // (moves spec §6.3, §7.6 local): the load, the delete and the put happen |
| // inside each CAS attempt (Plan 2 ruling G31), so a record that changed |
| @@ -131,17 +410,18 @@ func TestLoadPublishRoundTrip(t *testing.T) { |
| func TestRemoveHostDeletesInsideTheCAS(t *testing.T) { |
| ctx := context.Background() |
| s := release.NewDirStore(t.TempDir()) |
| - if err := RemoveHost(ctx, s, "a.example"); err != nil { |
| + if err := RemoveHost(ctx, s, "b", "a.example"); err != nil { |
| t.Fatalf("no record: %v", err) |
| } |
| - rec := &Record{V: 1, Box: "b", CheckedAt: at("2026-10-07T09:00:00Z"), Hosts: map[string]Verdict{ |
| - "a.example": {OK: true, VerifiedAt: at("2026-10-07T08:00:00Z")}, |
| - "b.example": {OK: true, VerifiedAt: at("2026-10-07T08:00:00Z")}, |
| + v := passed(Identity{}, at("2026-10-07T08:00:00Z")) |
| + rec := &Record{V: 2, Box: "b", CheckedAt: at("2026-10-07T09:00:00Z"), Hosts: map[string]Verdict{ |
| + "a.example": v, |
| + "b.example": v, |
| }} |
| if err := Publish(ctx, s, rec); err != nil { |
| t.Fatal(err) |
| } |
| - if err := RemoveHost(ctx, s, "c.example"); err != nil { |
| + if err := RemoveHost(ctx, s, "b", "c.example"); err != nil { |
| t.Fatalf("no entry: %v", err) |
| } |
| // Another writer adds a host between this remove's read and its put: the |
| @@ -152,20 +432,20 @@ func TestRemoveHostDeletesInsideTheCAS(t *testing.T) { |
| return |
| } |
| raced = true |
| - cur, _ := Load(ctx, s) |
| - cur.Hosts["d.example"] = Verdict{Unverified: true, VerifiedAt: at("2026-10-07T09:30:00Z")} |
| + cur, _ := Load(ctx, s, "b") |
| + cur.Hosts["d.example"] = unverified(Identity{}, "moving", at("2026-10-07T09:30:00Z")) |
| if err := Publish(ctx, s, cur); err != nil { |
| t.Fatal(err) |
| } |
| }} |
| - if err := RemoveHost(ctx, racing, "a.example"); err != nil { |
| + if err := RemoveHost(ctx, racing, "b", "a.example"); err != nil { |
| t.Fatal(err) |
| } |
| - got, err := Load(ctx, s) |
| + got, err := Load(ctx, s, "b") |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if _, ok := got.Hosts["a.example"]; ok || len(got.Hosts) != 2 || !got.Hosts["d.example"].Unverified || !got.Hosts["b.example"].OK { |
| + if _, ok := got.Hosts["a.example"]; ok || len(got.Hosts) != 2 || !got.Hosts["d.example"].Unverified() || !got.Hosts["b.example"].OK() { |
| t.Fatalf("after the remove: %+v", got.Hosts) |
| } |
| if !raced { |
| @@ -173,6 +453,44 @@ func TestRemoveHostDeletesInsideTheCAS(t *testing.T) { |
| } |
| } |
| +// RemoveHost edits only the named box's record: another box's verdict for |
| +// the same host stays, byte for byte. |
| +func TestRemoveHostOwnRecordOnly(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + now := at("2026-10-07T09:00:00Z") |
| + for _, ref := range []string{"box-a", "box-b"} { |
| + rec := Merge(nil, ref, now, []string{"a.example.com", "other.example.com"}, map[string]Verdict{ |
| + "a.example.com": passed(idA, now), |
| + "other.example.com": passed(Identity{}, now), |
| + }) |
| + if err := Publish(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + beforeB, _, err := s.Get(ctx, KeyFor("box-b")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := RemoveHost(ctx, s, "box-a", "a.example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + gotA, err := Load(ctx, s, "box-a") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok := gotA.Hosts["a.example.com"]; ok || len(gotA.Hosts) != 1 { |
| + t.Fatalf("box-a's record after the remove: %+v", gotA.Hosts) |
| + } |
| + afterB, _, err := s.Get(ctx, KeyFor("box-b")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !bytes.Equal(beforeB, afterB) { |
| + t.Fatalf("box-b's record changed:\n%s\n%s", beforeB, afterB) |
| + } |
| +} |
| + |
| // releaseStoreFunc is a release.Store whose Put first runs beforePut. |
| type releaseStoreFunc struct { |
| release.Store |
diff --git a/internal/host/movesland_test.go b/internal/host/movesland_test.go| index 82e741d71..9890e863a 100644 |
| --- a/internal/host/movesland_test.go |
| +++ b/internal/host/movesland_test.go |
| @@ -345,8 +345,8 @@ func newLandingHarness(t *testing.T, opts ...p1Option) *landingHarness { |
| return appconfig.HeldRender{Path: filepath.Join(h.tdir, p1Host+".json"), SHA256: "x", BundleETags: in.BundleETags, OverlayETag: in.OverlayETag}, nil |
| } |
| // The restore-verification record names the host: local removes it. |
| - if err := dbverify.Publish(context.Background(), h.s, &dbverify.Record{V: 1, Box: h.dstBox, CheckedAt: h.clock.Now(), |
| - Hosts: map[string]dbverify.Verdict{p1Host: {OK: true, VerifiedAt: h.clock.Now()}, "other.example": {OK: true, VerifiedAt: h.clock.Now()}}}); err != nil { |
| + if err := dbverify.Publish(context.Background(), h.s, &dbverify.Record{V: 2, Box: h.dstBox, CheckedAt: h.clock.Now(), |
| + Hosts: map[string]dbverify.Verdict{p1Host: {Result: dbverify.ResultPassed, VerifiedAt: h.clock.Now()}, "other.example": {Result: dbverify.ResultPassed, VerifiedAt: h.clock.Now()}}}); err != nil { |
| t.Fatal(err) |
| } |
| h.lcfg = h.landingCfg(t) |
| @@ -417,7 +417,9 @@ func (h *landingHarness) landingCfg(t *testing.T) moveCfg { |
| } |
| c.SetReplication = func(host, class string) error { h.repl = append(h.repl, class); return nil } |
| c.SetNextDue = func(host string, due int64) error { h.nextDue = append(h.nextDue, due); return nil } |
| - c.RemoveVerifyEntry = dbverify.RemoveHost |
| + c.RemoveVerifyEntry = func(ctx context.Context, s release.Store, host string) error { |
| + return dbverify.RemoveHost(ctx, s, h.dstBox, host) |
| + } |
| c.WriteGen = hostbox.WriteGenerationMarker |
| if h.probe != nil { |
| c.Probe = h.probe |
| @@ -652,7 +654,7 @@ func TestLandingBuildsInstallsAndRestores(t *testing.T) { |
| if len(h.repl) != 1 || h.repl[0] != src.Replication || len(h.nextDue) != 1 || h.nextDue[0] != src.NextDue { |
| t.Fatalf("columns: replication %v next_due %v, source %+v", h.repl, h.nextDue, src) |
| } |
| - dv, err := dbverify.Load(ctx, h.s) |
| + dv, err := dbverify.Load(ctx, h.s, h.dstBox) |
| if err != nil || dv == nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/host/movespass.go b/internal/host/movespass.go| index 36c4520b8..c74154783 100644 |
| --- a/internal/host/movespass.go |
| +++ b/internal/host/movespass.go |
| @@ -573,7 +573,9 @@ func armPhase1(cfg *moveCfg, w *movesWorker, hib activator.Config, dataDir strin |
| cfg.Local = park.Local{DataDir: dataDir, Box: cfg.Box} |
| cfg.OwnLease = hostbox.RenewingLeases{Region: hib.Region}.Acquire |
| cfg.RenderHeld = heldRenderer(writes, cfg.SecretsPriv, dataDir, cfg.Box) |
| - cfg.RemoveVerifyEntry = dbverify.RemoveHost |
| + cfg.RemoveVerifyEntry = func(ctx context.Context, s release.Store, h string) error { |
| + return dbverify.RemoveHost(ctx, s, cfg.Box, h) |
| + } |
| cfg.WriteGen = hostbox.WriteGenerationMarker |
| if reg != nil { |
| cfg.ReplaceTimers = heldTimersReplacer(writes, reg) |
| @@ -877,7 +879,7 @@ type moveCfg struct { |
| // registry's through schedule.SelectedTimers with a held-timers Work |
| // (heldTimersReplacer); SetReplication and SetNextDue are the |
| // registry's; RemoveVerifyEntry is |
| - // dbverify.RemoveHost; WriteGen is hostbox.WriteGenerationMarker, the |
| + // dbverify.RemoveHost on this box's own record; WriteGen is hostbox.WriteGenerationMarker, the |
| // one <dbPath>.gen writer (ruling G2). |
| Guard hostguard.Guard |
| Local park.Local |
diff --git a/internal/hostops/mutation_classifier.go b/internal/hostops/mutation_classifier.go| index 22846a64d..130cf8093 100644 |
| --- a/internal/hostops/mutation_classifier.go |
| +++ b/internal/hostops/mutation_classifier.go |
| @@ -61,7 +61,7 @@ var nonAppFamilies = []nonAppFamily{ |
| {prefix: "control/config-ack/", deletes: true, reason: "per-box config acknowledgements"}, |
| {prefix: "control/relay-ack/", deletes: true, reason: "per-box relay heartbeats"}, |
| {exact: "control/layout.json", reason: "deployment layout intent"}, |
| - {exact: "control/restore-verify.json", reason: "console database restore verification marker"}, |
| + {prefix: "control/verify/", reason: "per-box restore drill records keyed by box"}, |
| {prefix: "control/registry-snapshot", deletes: true, reason: "console registry snapshots"}, |
| {prefix: "control/housekeeper-lease", deletes: true, reason: "deployment housekeeper lease"}, |
| } |
diff --git a/internal/hostops/verify_classifier_test.go b/internal/hostops/verify_classifier_test.go| new file mode 100644 |
| index 000000000..2527c79a5 |
| --- /dev/null |
| +++ b/internal/hostops/verify_classifier_test.go |
| @@ -0,0 +1,42 @@ |
| +package hostops |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| +) |
| + |
| +// TestClassifyVerifyRecords: each box's restore-drill record, |
| +// control/verify/<ref>.json (member backups slice 2a), is a reviewed |
| +// non-app family, put only. The single deployment-wide record it replaces, |
| +// control/restore-verify.json, is no longer written and no longer admitted. |
| +func TestClassifyVerifyRecords(t *testing.T) { |
| + ctx := context.Background() |
| + for _, c := range []struct { |
| + name string |
| + target MutationTarget |
| + }{ |
| + {"first put", MutationTarget{Surface: "bucket", Method: "Put", Key: "control/verify/box-a.json", After: []byte(`{}`)}}, |
| + {"replacing put", MutationTarget{Surface: "bucket", Method: "Put", Key: "control/verify/box-b.json", ETag: "x", Before: []byte(`{}`), After: []byte(`{}`)}}, |
| + } { |
| + got, err := ClassifyMutation(ctx, nil, nil, c.target) |
| + if err != nil { |
| + t.Errorf("%s: %v", c.name, err) |
| + continue |
| + } |
| + if got.Family != "non-app" || got.NonApp == "" || len(got.Scopes) != 0 || got.MailGate { |
| + t.Errorf("%s: classified %+v, want the non-app family with no scopes", c.name, got) |
| + } |
| + } |
| + for _, c := range []struct { |
| + name string |
| + target MutationTarget |
| + }{ |
| + {"a delete", MutationTarget{Surface: "bucket", Method: "Delete", Key: "control/verify/box-a.json"}}, |
| + {"the retired record", MutationTarget{Surface: "bucket", Method: "Put", Key: "control/restore-verify.json", After: []byte(`{}`)}}, |
| + } { |
| + if got, err := ClassifyMutation(ctx, nil, nil, c.target); !errors.Is(err, ErrUnclassified) { |
| + t.Errorf("%s: classified %+v, err %v; want ErrUnclassified", c.name, got, err) |
| + } |
| + } |
| +} |
diff --git a/internal/moves/offline.go b/internal/moves/offline.go| index 670a9d1c6..61296d78c 100644 |
| --- a/internal/moves/offline.go |
| +++ b/internal/moves/offline.go |
| @@ -69,7 +69,7 @@ var OfflineManifest = []OfflineItem{ |
| {"routing", "edges file, backhaul, certificates, derived aliases", "not carried: produced by the existing passes once the row is ready, and the runbook confirms a certificate path before the move", OfflineOwnerNone, ""}, |
| {"source-retire", "everything the source box keeps for the host after open", "parked: renamed, never deleted, and listed path by path in the source's parked report", OfflineOwnerNone, ""}, |
| {"sidecar", "<host>-sidecar.env", "refused: a source report that sees one fails the seal", OfflineOwnerSeal, OfflineSourceSeal}, |
| - {"restore-verify", "control/restore-verify.json entry", "not carried: the next drill writes a fresh verdict, and until then the old one describes the source copy", OfflineOwnerNone, ""}, |
| + {"restore-verify", "control/verify/<ref>.json entry", "not carried: the next drill writes a fresh verdict, and until then the old one describes the source copy", OfflineOwnerNone, ""}, |
| {"warm-cache", "local DB, meta dirs, files and env a box holds for the host", "parked: renamed under .offline-moves on the source after open and on a target that already has them", OfflineOwnerLanding, ""}, |
| {"receipts", "move-receipts/<host>.json in the source bucket", "not carried: cleared at finish, and self-checking", OfflineOwnerNone, ""}, |
| {"class", "kind, addr, fleet, pool, hibernate, replica, replicas, steering, unit, seed and open run", "refused: the start door and the start transition refuse every class the move cannot carry", OfflineOwnerDoor, ""}, |
diff --git a/internal/ops/hibernate_verify.go b/internal/ops/hibernate_verify.go| index 4f649c022..acc540aba 100644 |
| --- a/internal/ops/hibernate_verify.go |
| +++ b/internal/ops/hibernate_verify.go |
| @@ -96,25 +96,33 @@ func VerifyHibernating(ctx context.Context, cfg *activator.Config, hosts []Hiber |
| // embedded replicator, under the lease), and an owner that has |
| // stopped writing leaves a replica that still restores perfectly. |
| if f, detail := hibernateFreshness(rt.DBPath, counter); f == freshnessStale || f == freshnessNoSource { |
| - out[host] = dbverify.Verdict{VerifiedAt: now.UTC(), Detail: detail} |
| + fresh := dbverify.FreshnessStale |
| + if f == freshnessNoSource { |
| + fresh = dbverify.FreshnessUnknown |
| + } |
| + out[host] = dbverify.Verdict{Result: dbverify.ResultFailed, Reason: "stale", Freshness: fresh, Prefix: prefix, VerifiedAt: now.UTC(), Detail: detail} |
| fmt.Printf("STALE %s: %s\n", host, detail) |
| continue |
| } |
| - out[host] = dbverify.Verdict{OK: true, VerifiedAt: now.UTC()} |
| + fresh := dbverify.FreshnessNotStale |
| + if rt.DBPath == "" { |
| + fresh = dbverify.FreshnessUnknown // nothing was compared |
| + } |
| + out[host] = dbverify.Verdict{Result: dbverify.ResultPassed, Freshness: fresh, Prefix: prefix, VerifiedAt: now.UTC()} |
| fmt.Printf("ok %s\n", host) |
| case errors.Is(err, lease.ErrNoReplica): |
| // Never replicated: a registered instance that has not woken yet. |
| // Not a failure — but not a pass either, because nothing was |
| // proven about anything. |
| - out[host] = dbverify.Verdict{Unverified: true, VerifiedAt: now.UTC(), Detail: "no replica yet"} |
| + out[host] = dbverify.Verdict{Result: dbverify.ResultUnverified, Reason: "nothing_replicated", Prefix: prefix, VerifiedAt: now.UTC(), Detail: "no replica yet"} |
| fmt.Printf("unverified %s: no replica yet (never woken)\n", host) |
| case ctx.Err() != nil: |
| // The run is being torn down; what we could not reach is |
| // unverified, never failed. |
| - out[host] = dbverify.Verdict{Unverified: true, VerifiedAt: now.UTC(), Detail: err.Error()} |
| + out[host] = dbverify.Verdict{Result: dbverify.ResultUnverified, Reason: dbverify.ReasonNotReached, Prefix: prefix, VerifiedAt: now.UTC(), Detail: err.Error()} |
| fmt.Printf("unverified %s: %v\n", host, err) |
| default: |
| - out[host] = dbverify.Verdict{VerifiedAt: now.UTC(), Detail: err.Error()} |
| + out[host] = dbverify.Verdict{Result: dbverify.ResultFailed, Reason: "restore_error", Prefix: prefix, VerifiedAt: now.UTC(), Detail: err.Error()} |
| fmt.Printf("FAIL %s: %v\n", host, err) |
| } |
| } |
diff --git a/internal/ops/hibernate_verify_test.go b/internal/ops/hibernate_verify_test.go| index d8795965e..a3826526c 100644 |
| --- a/internal/ops/hibernate_verify_test.go |
| +++ b/internal/ops/hibernate_verify_test.go |
| @@ -136,13 +136,13 @@ func TestVerifyHibernatingVerdicts(t *testing.T) { |
| got := VerifyHibernating(context.Background(), cfg, |
| routes("good.example.com", "damaged.example.com", "new.example.com"), 0, now, t.TempDir()) |
| - if v := got["good.example.com"]; !v.OK || v.Unverified { |
| + if v := got["good.example.com"]; !v.OK() || v.Unverified() { |
| t.Fatalf("clean replica verdict = %+v", v) |
| } |
| - if v := got["damaged.example.com"]; v.OK || v.Unverified { |
| + if v := got["damaged.example.com"]; v.OK() || v.Unverified() { |
| t.Fatalf("damaged replica must be a failure, got %+v", v) |
| } |
| - if v := got["new.example.com"]; v.OK || !v.Unverified { |
| + if v := got["new.example.com"]; v.OK() || !v.Unverified() { |
| t.Fatalf("never-woken instance must be unverified (nothing was proven), got %+v", v) |
| } |
| @@ -197,8 +197,8 @@ func TestVerifyHibernatingHonoursTheCap(t *testing.T) { |
| // instead of re-drilling the same alphabetical head every night. |
| func TestOldestFirstPutsNeverVerifiedThenStalest(t *testing.T) { |
| prior := &dbverify.Record{Hosts: map[string]dbverify.Verdict{ |
| - "recent.example.com": {OK: true, VerifiedAt: at("2026-08-14T00:00:00Z")}, |
| - "stale.example.com": {OK: true, VerifiedAt: at("2026-08-01T00:00:00Z")}, |
| + "recent.example.com": {Result: dbverify.ResultPassed, VerifiedAt: at("2026-08-14T00:00:00Z")}, |
| + "stale.example.com": {Result: dbverify.ResultPassed, VerifiedAt: at("2026-08-01T00:00:00Z")}, |
| }} |
| got := hostsOf(OldestFirst(routes("recent.example.com", "stale.example.com", "never.example.com"), prior)) |
| want := []string{"never.example.com", "stale.example.com", "recent.example.com"} |
| @@ -238,10 +238,10 @@ func TestVerifyHibernatingCatchesAStaleReplica(t *testing.T) { |
| []HibernatingRoute{{Host: "frozen.example.com", DBPath: live}}, 0, now, t.TempDir()) |
| v := got["frozen.example.com"] |
| - if v.OK { |
| + if v.OK() { |
| t.Fatalf("a replica frozen while its instance kept writing was reported OK: %+v", v) |
| } |
| - if v.Unverified { |
| + if v.Unverified() { |
| t.Fatalf("a stale replica must be a FAILURE, not merely unverified: %+v", v) |
| } |
| if !strings.Contains(v.Detail, "replication has stopped") { |
diff --git a/internal/ops/ops.go b/internal/ops/ops.go| index 0a7b3f23e..6482f823b 100644 |
| --- a/internal/ops/ops.go |
| +++ b/internal/ops/ops.go |
| @@ -318,11 +318,14 @@ func runHibernateVerify(args []string) { |
| // The prior record does two jobs: it orders this run (stalest first) and |
| // it carries the verdicts this run's cap will not revisit. |
| + // ref names this box's own record. Until the drill reads the agent's |
| + // self reference it is boxLabel(), as before. |
| + ref := boxLabel() |
| store, storeErr := openDeployStore(ctx) |
| var prior *dbverify.Record |
| if storeErr == nil { |
| - if prior, err = dbverify.Load(ctx, store); err != nil { |
| - log.Printf("hibernate-verify: reading %s: %v (continuing; this run starts the record over)", dbverify.Key, err) |
| + if prior, err = dbverify.Load(ctx, store, ref); err != nil { |
| + log.Printf("hibernate-verify: reading %s: %v (continuing; this run starts the record over)", dbverify.KeyFor(ref), err) |
| prior = nil |
| } |
| } |
| @@ -339,7 +342,7 @@ func runHibernateVerify(args []string) { |
| for _, h := range hosts { |
| fleet = append(fleet, h.Host) |
| } |
| - rec := dbverify.Merge(prior, boxLabel(), now, fleet, results) |
| + rec := dbverify.Merge(prior, ref, now, fleet, results) |
| // Publication is best-effort and LOUD when it fails: the drill's value is |
| // that someone can read the result without a session on this box, so a |
| @@ -352,7 +355,7 @@ func runHibernateVerify(args []string) { |
| if err := dbverify.Publish(ctx, store, rec); err != nil { |
| log.Printf("hibernate-verify: NOT PUBLISHED: %v — the verdicts below exist only in this log", err) |
| } else { |
| - fmt.Printf("hibernate-verify: published %s (%d hosts, %d unverified)\n", dbverify.Key, len(rec.Hosts), rec.Unverified()) |
| + fmt.Printf("hibernate-verify: published %s (%d hosts, %d unverified)\n", dbverify.KeyFor(ref), len(rec.Hosts), rec.Unverified()) |
| } |
| } |
| @@ -365,7 +368,7 @@ func runHibernateVerify(args []string) { |
| // lie this verb exists to remove. |
| proven := 0 |
| for _, v := range results { |
| - if v.OK { |
| + if v.OK() { |
| proven++ |
| } |
| } |
diff --git a/internal/release/changeindex.go b/internal/release/changeindex.go| index 16d771cdf..84b5d3efa 100644 |
| --- a/internal/release/changeindex.go |
| +++ b/internal/release/changeindex.go |
| @@ -50,7 +50,8 @@ var dataPrefixes = map[string]bool{ |
| // ChangeScope maps a bucket key to the scope whose generation a write to it |
| // bumps. The scopes are apps/<a>/<b> (except the data prefixes, and also |
| // bumped by that app's policy, control/app-policy/<a>/<b>.json), accounts/<a>, |
| -// control/certs, control/edges and the exact system/managed-fleets.json key; |
| +// control/certs, control/edges, control/verify/<ref> (one per box's |
| +// control/verify/<ref>.json) and the exact system/managed-fleets.json key; |
| // every other key reports ok=false. |
| func ChangeScope(key string) (scope string, ok bool) { |
| if key == "system/managed-fleets.json" || key == "control/demo-moved.json" { |
| @@ -85,6 +86,14 @@ func ChangeScope(key string) (scope string, ok bool) { |
| return "apps/" + parts[2] + "/" + b, true |
| } |
| } |
| + // control/verify/<ref>.json is one box's restore-drill record |
| + // (member backups spec §3): each record is its own scope, so a |
| + // reader re-reads only the box whose record moved. |
| + if parts[1] == "verify" && len(parts) == 3 { |
| + if ref, ok := strings.CutSuffix(parts[2], ".json"); ok && ref != "" { |
| + return "control/verify/" + ref, true |
| + } |
| + } |
| } |
| return "", false |
| } |
diff --git a/internal/release/changeindex_test.go b/internal/release/changeindex_test.go| index c8f4e29d7..2b2a5119d 100644 |
| --- a/internal/release/changeindex_test.go |
| +++ b/internal/release/changeindex_test.go |
| @@ -19,6 +19,8 @@ func TestChangeScope(t *testing.T) { |
| "control/certs/example.com": "control/certs", |
| "control/edges/box-a.json": "control/edges", |
| "control/app-policy/bab/shop.json": "apps/bab/shop", |
| + "control/verify/box-a.json": "control/verify/box-a", |
| + "control/verify/i-0abc.eu-west-1.json": "control/verify/i-0abc.eu-west-1", |
| } |
| for k, want := range cases { |
| got, ok := ChangeScope(k) |
| @@ -36,6 +38,9 @@ func TestChangeScope(t *testing.T) { |
| "fleets/f/apps/bab/shop/config.json", |
| "control/app-policy/bab/shop.json/x", "control/app-policy/bab", "control/app-policy/bab/.json", |
| "control/app-policy/bab/shop.txt", "control/app-policy//shop.json", "control/app-policy-lease/lock.json", |
| + "control/verify/box-a", "control/verify/.json", "control/verify/box-a.json/x", |
| + "control/verify/a/b.json", "control/verify/", "control/verify", "control/verify/box-a.txt", |
| + "control/restore-verify.json", |
| } { |
| if s, ok := ChangeScope(k); ok { |
| t.Errorf("ChangeScope(%q) = %q, want none", k, s) |