carlos / platform Public

host: fire the disk alert with room for two static trees above the floor

claude pushed by paul@keymail.dev 3fef5d63dc3297dfb4982fafe3d08b395063cb87 parent 00910e3
3 files changed, +116 −0
  • internal/host/agent.go +14 −0
  • internal/host/diskalertline_test.go +74 −0
  • internal/host/metrics_loop.go +28 −0

CI failed — run details

diff --git a/internal/host/agent.go b/internal/host/agent.go
index 15811bbe9..2716a6756 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -1225,6 +1225,7 @@ func runAgent(args []string, buildClaims ClaimFactory) {
geoIPSource.Store(e.GeoIPState)
heldStreamsSource.Store(e.HeldStreamsForStatus)
geoIPAlertSource.Store(e.GeoIPAlertCode)
+ staticTreeSource.Store(e.LargestStaticTree)
// The layout pass reloads routing through this as soon as a move
// commits, instead of waiting for the 2s registry poll.
reload := func() { _ = e.Reload() }
@@ -4597,6 +4598,19 @@ var geoIPAlertSource atomic.Value // func(time.Time) (float64, bool)
// geoIPAlertReading is the rule's reading, or nil before the edge is ready
// (no data never fires and never clears).
+// staticTreeSource is the edge's Edge.LargestStaticTree, published once the
+// edge exists, for the disk rule's line (diskAlertFrac).
+var staticTreeSource atomic.Value // func() int64
+
+// largestStaticTree reads it, 0 before the edge is ready.
+func largestStaticTree() int64 {
+ fn, _ := staticTreeSource.Load().(func() int64)
+ if fn == nil {
+ return 0
+ }
+ return fn()
+}
+
func geoIPAlertReading(now time.Time) *float64 {
fn, _ := geoIPAlertSource.Load().(func(time.Time) (float64, bool))
if fn == nil {
diff --git a/internal/host/diskalertline_test.go b/internal/host/diskalertline_test.go
new file mode 100644
index 000000000..0442ee836
--- /dev/null
+++ b/internal/host/diskalertline_test.go
@@ -0,0 +1,74 @@
+package host
+
+import (
+ "context"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/instancectl"
+ "github.com/carlosframework/platform/internal/metrics"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// The disk rule's default line, 90 percent, is the cold-wake floor itself
+// on any disk over 10 GiB (instancectl.DiskFloor), so on 2026-11-08 annie's
+// alert and its refused wakes began together. The line now leaves room
+// above the floor for two of the box's largest static trees: one being
+// extracted while the previous is still kept.
+func TestDiskAlertFracLeavesRoomForTwoStaticTrees(t *testing.T) {
+ total := uint64(59 * gib)
+ floor := instancectl.DiskFloor(total)
+ tree := int64(26 * gib / 10)
+ got := diskAlertFrac(0.90, total, tree)
+ want := 1 - float64(floor+2*uint64(tree))/float64(total)
+ if got != want {
+ t.Fatalf("line = %v, want %v", got, want)
+ }
+ // annie at 02:31Z: 5.4 GiB free, already under the floor. The line
+ // fires well before that: at 11 GiB free.
+ if free := float64(total) * (1 - got) / float64(gib); free < 11 || free > 11.2 {
+ t.Errorf("fires at %.1f GiB free, want about 11.1", free)
+ }
+ // A lower configured line stays.
+ if got := diskAlertFrac(0.50, total, tree); got != 0.50 {
+ t.Errorf("configured 0.50 became %v", got)
+ }
+ // On a small disk the 1 GiB floor is above 10 percent: the line moves
+ // down to it, so the alert never comes after the refusals.
+ if got, want := diskAlertFrac(0.90, 8*gib, 0), 1-float64(gib)/float64(8*gib); got != want {
+ t.Errorf("8 GiB disk: line = %v, want %v", got, want)
+ }
+ // No reading of the disk's size leaves the configured line.
+ if got := diskAlertFrac(0.90, 0, tree); got != 0.90 {
+ t.Errorf("no total: line = %v", got)
+ }
+ // A disk that cannot hold the floor and two trees is always alerting.
+ if got := diskAlertFrac(0.90, 10*gib, int64(5*gib)); got != 0 {
+ t.Errorf("tiny disk: line = %v, want 0", got)
+ }
+}
+
+// Through alertTick: annie's 2026-11-08 reading an hour before the
+// refusals, 82 percent used with 2.6 GiB apt trees, fires the disk rule;
+// with no static trees it does not.
+func TestAlertTickFiresDiskBeforeTheColdWakeFloor(t *testing.T) {
+ ctx := context.Background()
+ used := 0.82
+ at := time.Date(2026, 11, 8, 1, 30, 0, 0, time.UTC)
+ latest := metrics.Sample{At: at, Host: metrics.HostSample{DiskFrac: &used}}
+ for _, c := range []struct {
+ tree int64
+ want bool
+ }{{int64(26 * gib / 10), true}, {0, false}} {
+ s := release.NewDirStore(t.TempDir())
+ st := &metrics.AlertState{V: 1, Box: "annie", Rules: map[string]metrics.RuleState{}}
+ extra := alertReadings{diskTotal: 59 * gib, staticTree: c.tree}
+ if err := alertTick(ctx, s, "annie", metrics.DefaultRules(), st, latest, extra, filepath.Join(t.TempDir(), "alerts.json"), nil, nil, nil); err != nil {
+ t.Fatal(err)
+ }
+ if got := st.Rules["disk"].Firing; got != c.want {
+ t.Errorf("static tree %d bytes: disk firing = %v, want %v", c.tree, got, c.want)
+ }
+ }
+}
diff --git a/internal/host/metrics_loop.go b/internal/host/metrics_loop.go
index 5ded8ff68..3023811df 100644
--- a/internal/host/metrics_loop.go
+++ b/internal/host/metrics_loop.go
@@ -12,6 +12,7 @@ import (
"context"
"encoding/json"
"errors"
+ "github.com/carlosframework/platform/internal/instancectl"
"log"
"os"
"path/filepath"
@@ -533,6 +534,7 @@ func alertTick(ctx context.Context, s release.Store, box string, rules metrics.R
st *metrics.AlertState, latest metrics.Sample, extra alertReadings, statePath string,
sinks *alertSinks, trend *diskTrend, beat *engineBeatWatch) error {
+ rules.DiskFrac = diskAlertFrac(rules.DiskFrac, extra.diskTotal, extra.staticTree)
obs := metrics.Observation{
At: latest.At,
CPUFrac: latest.Host.CPUFrac,
@@ -600,6 +602,29 @@ func alertTick(ctx context.Context, s release.Store, box string, rules metrics.R
// leaves every rule they feed exactly as it was.
type alertReadings struct {
restarts map[string]metrics.UnitRestarts // see restartWatch.tick
+ // diskTotal is the data disk's size and staticTree the largest static
+ // tree the edge keeps (Edge.LargestStaticTree): the disk rule's line is
+ // lowered to leave room for two trees above the cold-wake floor
+ // (diskAlertFrac). Zero total leaves the configured line.
+ diskTotal uint64
+ staticTree int64
+}
+
+// diskAlertFrac is the disk rule's line on a disk of total bytes: the
+// configured one, or lower, so the rule fires while the box still has
+// instancectl.DiskFloor free plus room for two of its largest static trees
+// (a release being extracted while the previous one is kept). The default
+// line, 90 percent, is the floor itself on any disk over 10 GiB, so on
+// 2026-11-08 annie's alert and its refused cold wakes began together.
+func diskAlertFrac(configured float64, total uint64, staticTree int64) float64 {
+ if total == 0 {
+ return configured
+ }
+ room := instancectl.DiskFloor(total) + 2*uint64(max(staticTree, 0))
+ if room >= total {
+ return 0
+ }
+ return min(configured, 1-float64(room)/float64(total))
}
// persistAlertState writes st to path as indented JSON via the repo's
@@ -932,6 +957,9 @@ func metricsLoop(execRoot string, src unitSources, owners func() map[string]metr
restarts := watchFile.load()
restartTick := func(at time.Time) alertReadings {
extra := alertReadings{restarts: restarts.tick(at, probes.restarts())}
+ if _, total, ok := probes.diskBytes(); ok {
+ extra.diskTotal, extra.staticTree = total, largestStaticTree()
+ }
if err := watchFile.save(restarts); err != nil {
log.Printf("agent: restart watch: save %s: %v", watchFile.path, err)
}