carlos / platform Public

host: test every ledger write counts once; honest prune cost note

Astra review of fddc42f1: a repeated flush into one hour, or a late
flush into an older hour, must add exactly once (a fresh walk must
agree, both families). The cost comment now counts the daily walk and
pagination.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev dfc9c8bf73a4049edfbc9fccb3fd9ad15c29f523 parent fddc42f
2 files changed, +56 −7
  • internal/host/logretention.go +11 −7
  • internal/host/logretention_test.go +45 −0

CI cancelled — superseded by a newer push — run details

diff --git a/internal/host/logretention.go b/internal/host/logretention.go
index 5737045cb..89e3653c5 100644
--- a/internal/host/logretention.go
+++ b/internal/host/logretention.go
@@ -5,13 +5,17 @@
// constants were before the settings existed, so an unset box behaves as
// it always did.
//
-// The prune's cost no longer grows with retention. It used to LIST every
-// hour it kept, every hour: ~168 LISTs per app per box per hour at 7 days,
-// ~2,160 at 90. pruneLedger remembers each hour's size after one walk,
-// adds what this box's own flushes write, and LISTs only the hours it
-// deletes. Steady state is one ListDirs per app per hour, plus a full
-// re-walk once a day to correct any drift (an ambiguous Put that landed
-// after reporting failure).
+// The prune no longer sizes every hour every hour. It used to LIST each
+// hour it kept on every pass: ~168 LISTs per app per box per hour at 7
+// days, ~2,160 at 90. pruneLedger remembers each hour's size after a walk,
+// adds what this box's own flushes write, and between walks LISTs only the
+// hours it deletes: one ListDirs per app per hour (paginated, 1,000 hours
+// a page on S3) plus one List per deleted hour. The walk itself still
+// costs one List per kept hour and runs once per process and once a day,
+// to correct drift (an ambiguous Put that landed after reporting
+// failure): at 90 days that averages ~90 LISTs per app per box per hour,
+// against ~2,160 before. These counts are Store calls; a List over more
+// than 1,000 keys pages too.
package host
import (
diff --git a/internal/host/logretention_test.go b/internal/host/logretention_test.go
index c10cbc190..e8647b965 100644
--- a/internal/host/logretention_test.go
+++ b/internal/host/logretention_test.go
@@ -3,9 +3,11 @@ package host
import (
"context"
"errors"
+ "maps"
"testing"
"time"
+ "github.com/carlosframework/platform/internal/apperr"
"github.com/carlosframework/platform/internal/applog"
"github.com/carlosframework/platform/internal/release"
)
@@ -276,3 +278,46 @@ func TestPruneLedgersArePerFamily(t *testing.T) {
t.Fatalf("errors prune: errors chunk survived=%v, logs chunk survived=%v; want false, true", f.exists(errKey), f.exists(logKey))
}
}
+
+// TestPruneLedgerCountsEveryWriteOnce: after a walk, every chunk a flush
+// writes adds its size exactly once, including repeated writes into one
+// hour and late writes into an older hour (records held through a bucket
+// outage). The ledger must then match a fresh walk byte for byte, for both
+// shipping families.
+func TestPruneLedgerCountsEveryWriteOnce(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ now := time.Date(2026, 11, 14, 12, 30, 0, 0, time.UTC)
+ stamps := []time.Time{now, now.Add(time.Minute), now.Add(-3 * time.Hour), now.Add(-3*time.Hour + time.Minute), now}
+ ret := logRetention{maxAge: 30 * 24 * time.Hour, maxBytes: 1 << 30}
+
+ for _, fam := range []applog.Family{applog.FamilyLogs, applog.FamilyErrors} {
+ f := newPruneFixture(t)
+ l := newPruneLedger(fam, "box1", ret)
+ if fam == applog.FamilyLogs {
+ f.put("box1", now.Add(-3*time.Hour), 77) // an existing older hour
+ } else {
+ key := mustKey(t)(applog.FamilyChunkKey(fam, f.k.Acct, f.k.App, "box1", applog.HourOf(now.Add(-3*time.Hour)), 1, 77))
+ if _, err := f.s.Store.Put(ctx, key, make([]byte, 77), ""); err != nil {
+ t.Fatal(err)
+ }
+ }
+ l.prune(ctx, f.s, f.k, now) // walk
+ for i, ts := range stamps {
+ seq := uint64(100 + i)
+ if fam == applog.FamilyLogs {
+ logshipFlush(ctx, f.s, map[applog.AppKey][]applog.Record{f.k: {{TS: ts, Box: "box1", Seq: seq, Msg: "line"}}}, l)
+ } else {
+ errshipFlush(ctx, f.s, map[applog.AppKey][]apperr.Record{f.k: {{TS: ts, Box: "box1", Seq: seq, Source: "log", Name: "boom", FP: "fp"}}}, l)
+ }
+ }
+ got := maps.Clone(l.apps[f.k].bytes)
+
+ fresh := newPruneLedger(fam, "box1", ret)
+ fresh.prune(ctx, f.s, f.k, now)
+ want := fresh.apps[f.k].bytes
+ if len(want) != 2 || !maps.Equal(got, want) {
+ t.Fatalf("%s: ledger %v, fresh walk %v (want two hours, equal)", fam, got, want)
+ }
+ }
+}