activator: recognise escrow alerts by type, not by "ALERT: " prefix
The launch path and the escrow sweep told an alert from other failures with strings.HasPrefix(err.Error(), "ALERT: "). stageEscrowReads joins a SealedOpenError ahead of the conflict alert, and a wrapped alert has context in front, so the prefix test missed both: the sweep logged a joined alert as a routine, hourly "retried every sweep" failure. The three alerts (conflict marker, divergence, second writer) are now an escrowAlert made by newEscrowAlert with the same text as before, and both sites use isEscrowAlert (errors.As). Behaviour is otherwise unchanged; at the launch site a joined alert was already refused through the errEscrowUnreadable branch and is now refused as an alert.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+116
−5
internal/activator/escrow.go+25 −5internal/activator/escrow_alert_test.go+91 −0
CI cancelled — superseded by a newer push — run details
Intent
diff --git a/internal/activator/escrow.go b/internal/activator/escrow.go| index cfc7b0542..fc1b3e113 100644 |
| --- a/internal/activator/escrow.go |
| +++ b/internal/activator/escrow.go |
| @@ -499,7 +499,27 @@ func (b *ExecBackend) escrowConflict(host string) error { |
| if _, err := os.Lstat(path); errors.Is(err, os.ErrNotExist) { |
| return nil |
| } |
| - return fmt.Errorf("ALERT: %s: escrow: the host's local secrets differ from its escrow in the bucket; every launch of the host on this box is refused until an operator settles them against the escrow and removes %s", host, path) |
| + return newEscrowAlert("ALERT: %s: escrow: the host's local secrets differ from its escrow in the bucket; every launch of the host on this box is refused until an operator settles them against the escrow and removes %s", host, path) |
| +} |
| + |
| +// escrowAlert is an escrow refusal an operator has to act on. Its text is |
| +// unchanged, "ALERT: " first (operators and tests read it), but callers |
| +// recognise one by type with isEscrowAlert, never by that prefix: an alert |
| +// errors.Join puts second (stageEscrowReads joins a SealedOpenError ahead of |
| +// one), or one wrapped with context, does not start with it. |
| +type escrowAlert struct{ msg string } |
| + |
| +func (e *escrowAlert) Error() string { return e.msg } |
| + |
| +// newEscrowAlert formats an escrowAlert exactly as fmt.Errorf would. |
| +func newEscrowAlert(format string, args ...any) error { |
| + return &escrowAlert{msg: fmt.Sprintf(format, args...)} |
| +} |
| + |
| +// isEscrowAlert reports whether err is, wraps or joins an escrowAlert. |
| +func isEscrowAlert(err error) bool { |
| + var a *escrowAlert |
| + return errors.As(err, &a) |
| } |
| // writeEscrowConflict persists host's conflict marker, durably. |
| @@ -1031,7 +1051,7 @@ func (b *ExecBackend) escrowConflictFound(rt registry.Route, p instancePaths, di |
| if err := b.writeEscrowConflict(rt.Host, diverged); err != nil { |
| log.Printf("activator: %s: escrow: the conflict marker %s was not written: %v (the launch stays refused while the divergence lasts)", rt.Host, path, err) |
| } |
| - err := fmt.Errorf("ALERT: %s: escrow: %s differs from what the host's escrow in the bucket holds, and an escrowed instance.key, report.token or platform-minted env variable is never replaced; the bucket's copy came first, so this launch stops and the local files (%s and the files directory) are left for an operator; every launch of the host on this box is refused until the operator settles them against the escrow and removes %s", |
| + err := newEscrowAlert("ALERT: %s: escrow: %s differs from what the host's escrow in the bucket holds, and an escrowed instance.key, report.token or platform-minted env variable is never replaced; the bucket's copy came first, so this launch stops and the local files (%s and the files directory) are left for an operator; every launch of the host on this box is refused until the operator settles them against the escrow and removes %s", |
| rt.Host, strings.Join(diverged, " and "), p.envFile, path) |
| log.Print(err) |
| return err |
| @@ -1306,7 +1326,7 @@ func (b *ExecBackend) escrowEnvOf(ctx context.Context, rt registry.Route, m host |
| // errEscrowWriter is the alert a second supersession raises. |
| func errEscrowWriter(host string) error { |
| - return fmt.Errorf("ALERT: %s: escrow: another writer publishes this host's escrow", host) |
| + return newEscrowAlert("ALERT: %s: escrow: another writer publishes this host's escrow", host) |
| } |
| // escrowReread is a superseded publish's second look: the bucket's |
| @@ -1435,7 +1455,7 @@ func (b *ExecBackend) escrowedLaunchEnv(ctx context.Context, rt registry.Route) |
| return nil, instancekey.LayerNone, fmt.Errorf("instance key for %s: %w", rt.Host, err) |
| } |
| continue |
| - case err != nil && strings.HasPrefix(err.Error(), "ALERT: "): |
| + case isEscrowAlert(err): |
| return nil, instancekey.LayerNone, err |
| case errors.Is(err, errEscrowNoLease): |
| // The lease was lost on the way to the commit, and nothing was |
| @@ -1650,7 +1670,7 @@ func (b *ExecBackend) EscrowSweep(ctx context.Context, rows []registry.Route) Es |
| tally.add(func(r *EscrowSweepReport) { r.Failing = append(r.Failing, c.rt.Host) }) |
| // A failure that repeats every sweep (a file the tenant made |
| // unreadable, say) is said once an hour; an alert every time. |
| - if strings.HasPrefix(err.Error(), "ALERT: ") { |
| + if isEscrowAlert(err) { |
| log.Print(err) |
| } else { |
| logPerFile("escrow", c.rt.Host, fmt.Sprintf("activator: %s: escrow: %v (retried every sweep)", c.rt.Host, err)) |
diff --git a/internal/activator/escrow_alert_test.go b/internal/activator/escrow_alert_test.go| new file mode 100644 |
| index 000000000..dff6ae942 |
| --- /dev/null |
| +++ b/internal/activator/escrow_alert_test.go |
| @@ -0,0 +1,91 @@ |
| +package activator |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "errors" |
| + "fmt" |
| + "log" |
| + "os" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestEscrowAlertsAreRecognisedJoinedOrWrapped: each of the three escrow |
| +// alerts keeps its "ALERT: " text, and is recognised by type whether it is |
| +// bare, wrapped with %w, or second in an errors.Join, as stageEscrowReads |
| +// puts it behind a SealedOpenError. A prefix test on the message missed the |
| +// joined and wrapped forms; an error that merely says "ALERT: " is not one. |
| +func TestEscrowAlertsAreRecognisedJoinedOrWrapped(t *testing.T) { |
| + b, rt := escrowBackend(t, escrowHost) |
| + if err := writeEscrowMarker(b.DataDir, rt.Host, []byte("{}")); err != nil { |
| + t.Fatal(err) |
| + } |
| + alerts := map[string]error{ |
| + "conflict marker": b.escrowConflict(rt.Host), |
| + "divergence": b.escrowConflictFound(rt, b.pathsFor(rt), []string{"instance.key"}), |
| + "second writer": errEscrowWriter(rt.Host), |
| + } |
| + other := errors.New("hostfiles: sealed item env: the envelope does not open") |
| + for name, alert := range alerts { |
| + if alert == nil || !strings.HasPrefix(alert.Error(), "ALERT: "+rt.Host+": escrow: ") { |
| + t.Fatalf("%s: %v, want the ALERT text unchanged", name, alert) |
| + } |
| + for form, err := range map[string]error{ |
| + "bare": alert, |
| + "wrapped": fmt.Errorf("secrets for %s: %w", rt.Host, alert), |
| + "joined": errors.Join(other, alert), |
| + } { |
| + if !isEscrowAlert(err) { |
| + t.Errorf("%s, %s: %q not recognised as an alert", name, form, err) |
| + } |
| + } |
| + } |
| + for _, err := range []error{nil, other, errors.New("ALERT: not an escrow alert"), errEscrowUnreadable} { |
| + if isEscrowAlert(err) { |
| + t.Errorf("%v recognised as an escrow alert", err) |
| + } |
| + } |
| +} |
| + |
| +// TestSweepLogsAJoinedAlertAsAnAlert: after a key change the sealed env no |
| +// longer opens, so stageEscrowReads joins that read failure with the |
| +// conflict alert, the read failure first. The sweep must still treat it as |
| +// an alert: logged as it stands, every time, not as an hourly "retried every |
| +// sweep" failure. The old prefix test saw only the read failure's text. |
| +func TestSweepLogsAJoinedAlertAsAnAlert(t *testing.T) { |
| + // A host of its own: logPerFile's once-an-hour memory is per process. |
| + const host = "alertjoin.example.com" |
| + ctx := context.Background() |
| + b, rt := escrowBackend(t, host) |
| + b.EscrowKey = testEscrowKey(t) |
| + writeEnv(t, b, rt, envA) |
| + mustPublish(t, b, rt) |
| + |
| + nb := restartEscrow(t, b) |
| + nb.EscrowKey = testEscrowKey(t) |
| + holdLease(t, nb, rt) |
| + |
| + var buf bytes.Buffer |
| + prev := log.Writer() |
| + log.SetOutput(&buf) |
| + defer log.SetOutput(prev) |
| + rep := nb.EscrowSweep(ctx, []registry.Route{rt}) |
| + log.SetOutput(prev) |
| + |
| + if len(rep.Failing) != 1 || rep.Failing[0] != host { |
| + t.Fatalf("sweep = %+v, want %s failing", rep, host) |
| + } |
| + if _, err := os.Lstat(escrowConflictPath(nb.DataDir, host)); err != nil { |
| + t.Fatalf("no conflict marker, so the joined alert was not raised: %v", err) |
| + } |
| + out := buf.String() |
| + if !strings.Contains(out, "ALERT: "+host+": escrow: ") { |
| + t.Fatalf("log = %q, want the alert", out) |
| + } |
| + if strings.Contains(out, "(retried every sweep)") { |
| + t.Fatalf("the joined alert was logged as a routine sweep failure:\n%s", out) |
| + } |
| +} |