carlos / platform Public

rollup-tenant-tags: subtract CPU and memory counts separately for the uncounted remainder (review P2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev e2e923814d56b1ce67b0605a2f1b30639a21df88 parent 5fe6b58
4 files changed, +64 −5
  • docs/superpowers/notes/remote-instance-metrics.md +7 −0
  • docs/superpowers/reviews/CODEX-FINDINGS-rollup-tenant-tags-r1.md +18 −0
  • internal/console/economicspass.go +22 −5
  • internal/console/usageattrib_tags_test.go +17 −0
diff --git a/docs/superpowers/notes/remote-instance-metrics.md b/docs/superpowers/notes/remote-instance-metrics.md
index 86a5b1903..cf8c43813 100644
--- a/docs/superpowers/notes/remote-instance-metrics.md
+++ b/docs/superpowers/notes/remote-instance-metrics.md
@@ -412,3 +412,10 @@ instance on box B counted for its owner for exactly its tagged samples
the predecessor for the hours its tag is on and never to the new owner,
and to no one while B's row and activator disagree. `TestRollupHourOwned`
in `internal/metrics`. Mutations: 6, all killed.
+
+Review (Astra, billing, 5fe6b58b): READY AFTER FIXES. One P2: the
+uncounted remainder subtracted CPU-else-memory counts, so a tagged
+memory-only sample could cancel an untagged CPU one in the log. Fixed:
+`unchargedSamples` subtracts CPU and memory counts separately and then
+applies the CPU-else-memory rule. Census: `internal/metrics` envelope
+approved. The review stops here.
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-rollup-tenant-tags-r1.md b/docs/superpowers/reviews/CODEX-FINDINGS-rollup-tenant-tags-r1.md
new file mode 100644
index 000000000..94ee4c5d9
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-FINDINGS-rollup-tenant-tags-r1.md
@@ -0,0 +1,18 @@
+Round 1 of 3: no P1 findings; one P2.
+
+1. **P2 — `sweepUsage`: subtract metric counts before selecting the unattributed remainder.**
+ At [economicspass.go:458](/home/paulca/amadan.net/carlos/platform-rollup-tags/internal/console/economicspass.go:458), the code subtracts independently selected instance counts. But [unitHourUsage:624](/home/paulca/amadan.net/carlos/platform-rollup-tags/internal/console/economicspass.go:624) selects CPU’s count when available, otherwise memory’s. Those counts can represent different samples.
+
+ **Failure scenario:** one tagged sample carries only memory; a later untagged sample carries CPU and memory. The whole aggregate selects CPU N=1; the owned aggregate selects memory N=1. Subtraction yields zero, so the untagged sample disappears from the log. A first reading without a CPU delta is supported by [parse.go:168](/home/paulca/amadan.net/carlos/platform-rollup-tags/internal/metrics/parse.go:168). Tenant charges remain isolated.
+
+ **Fix:** subtract accepted owners’ CPU and memory counts separately, then apply the CPU-else-memory rule to the residual counts. Add a regression test asserting the remaining `1/60` unattributed hour is logged.
+
+ **Fault family:** unattributed usage accounting — tagged samples must not cancel unrelated untagged samples in the remainder.
+
+Validation: reviewed both rollup production paths, replacement writes, statement reruns, live status, census and economics consumers, and fleet presence accounting. No new requests were introduced. `git diff --check` passed. Targeted Go tests could not start because the sandbox refused creation of Go’s build directory; the six reported mutation kills were not independently rerun.
+
+**Census verdict: APPROVE `internal/metrics` envelope re-pin.** The independently calculated fingerprint matches the overlay. [RollupHour:138](/home/paulca/amadan.net/carlos/platform-rollup-tags/internal/metrics/rollup.go:138) and [rollupOwned:145](/home/paulca/amadan.net/carlos/platform-rollup-tags/internal/metrics/rollup.go:145) add in-memory grouping over existing samples, calling the existing aggregator and sorting owners. They add no root reads, path resolution, execution, or weakened validation. No unit dispositions change.
+
+Fix the P2 without reopening the review, retaining the required checks on the resulting diff.
+
+**READY AFTER FIXES**
\ No newline at end of file
diff --git a/internal/console/economicspass.go b/internal/console/economicspass.go
index 57d6f4543..29668a523 100644
--- a/internal/console/economicspass.go
+++ b/internal/console/economicspass.go
@@ -455,17 +455,16 @@ func (s *Server) sweepUsage(ctx context.Context, from, to time.Time, attrib *usa
// rollup aggregates each tenant's tagged samples
// alone, metrics.HourAgg.Owned); the rest counts
// against no account.
- _, _, instN, _, _ := unitHourUsage(um)
+ var charged []map[string]metrics.Agg
for _, oa := range hour.Owned[unit] {
tag := oa.Owner
if w := attrib.attribute(unit, &tag); w.class == unitTenant {
charge(w.owner.Account, oa.Metrics)
- _, _, n, _, _ := unitHourUsage(oa.Metrics)
- instN -= n
+ charged = append(charged, oa.Metrics)
}
}
- if instN > 0 {
- unattributed.add(box, unit, float64(instN)/60)
+ if n := unchargedSamples(um, charged); n > 0 {
+ unattributed.add(box, unit, float64(n)/60)
}
}
}
@@ -637,6 +636,24 @@ func unitHourUsage(um map[string]metrics.Agg) (cpuCoreHours, memGBHours float64,
return cpuCoreHours, memGBHours, instN, hasCPU, hasMem
}
+// unchargedSamples is the instance-minutes of a unit's hour left over once
+// the charged owners' parts are taken out: CPU and memory counts are
+// subtracted separately, then the hour's own rule (the CPU count, else the
+// memory count) applies to what remains, so a tagged sample that carried
+// only memory never cancels an untagged one that carried CPU (Astra,
+// rollup-tenant-tags round 1).
+func unchargedSamples(um map[string]metrics.Agg, charged []map[string]metrics.Agg) int {
+ cpuN, memN := um["cpu"].N, um["mem_bytes"].N
+ for _, m := range charged {
+ cpuN -= m["cpu"].N
+ memN -= m["mem_bytes"].N
+ }
+ if cpuN > 0 {
+ return cpuN
+ }
+ return max(memN, 0)
+}
+
// rollupPresenceHours converts one box's day rollup into billable box-hours
// under the fleet-hours presence rule (task-3 brief): fleet-box rollups may
// carry only the host-level aggregate (no per-unit CPU sampling — the box
diff --git a/internal/console/usageattrib_tags_test.go b/internal/console/usageattrib_tags_test.go
index 667413dd4..d025d349c 100644
--- a/internal/console/usageattrib_tags_test.go
+++ b/internal/console/usageattrib_tags_test.go
@@ -148,3 +148,20 @@ func TestUsageTaggedReusedHostname(t *testing.T) {
near(t, "the predecessor's instance_hours (its own process, tagged)", sweep.PerAccount[rival][usage.MeterInstanceHours], 1)
near(t, "ops instance_hours", sweep.PerAccount[release.OpsAccount][usage.MeterInstanceHours], 0)
}
+
+// TestUnchargedSamples (Astra, round 1, P2): a tagged sample with only a
+// memory reading and an untagged one with CPU and memory leave one
+// untagged minute, not zero.
+func TestUnchargedSamples(t *testing.T) {
+ whole := map[string]metrics.Agg{"cpu": {N: 1}, "mem_bytes": {N: 2}}
+ owned := map[string]metrics.Agg{"mem_bytes": {N: 1}}
+ if n := unchargedSamples(whole, []map[string]metrics.Agg{owned}); n != 1 {
+ t.Fatalf("uncharged = %d, want 1", n)
+ }
+ if n := unchargedSamples(map[string]metrics.Agg{"cpu": {N: 60}, "mem_bytes": {N: 60}}, []map[string]metrics.Agg{{"cpu": {N: 60}, "mem_bytes": {N: 60}}}); n != 0 {
+ t.Fatalf("a fully tagged hour leaves %d", n)
+ }
+ if n := unchargedSamples(map[string]metrics.Agg{"mem_bytes": {N: 5}}, nil); n != 5 {
+ t.Fatalf("an untagged memory-only hour = %d, want 5", n)
+ }
+}