carlos / platform Public

economics: record what each AWS bill covers

Every stored bill gains an additive basis (omitempty: unstated bills
marshal byte-identically to before). CONSOLE_COST_ACCOUNT_CARLOS_ONLY
declares the AWS account CARLOS-only (Config.CostScope); the hourly
billPass stamps the basis billBasisFor gives and re-pulls a stored bill
once when its basis differs, ahead of the settle and same-day rules.
The ceCosts seam now takes a ceQuery.

Census: internal/economics envelope re-pinned (overlay
billing-dash-3-cost); record-write census unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1c7c02c3c71295de074a94640bc78c801c800cec parent 8e8d248
16 files changed, +342 −24
  • cmd/console/costscope.go +26 −0
  • cmd/console/costscope_test.go +34 −0
  • cmd/console/main.go +7 −0
  • docs/superpowers/notes/census-claims-billing-dash-3-cost.tsv +3 −0
  • docs/superpowers/notes/census-proposal-billing-dash-3-cost.md +11 −0
  • internal/console/cepull.go +6 −6
  • internal/console/claim_invariant_probes_worker_test.go +1 −1
  • internal/console/costscope.go +34 −0
  • internal/console/economicsbill_test.go +131 −10
  • internal/console/economicspass.go +15 −4
  • internal/console/server.go +6 −1
  • internal/economics/bill.go +22 −2
  • internal/economics/bill_test.go +40 −0
  • internal/hostops/storage_classifier_test.go +2 −0
  • internal/provision/testdata/plan14-root-reads-billing-dash-3-cost.tsv +3 −0
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0

CI passed — run details

diff --git a/cmd/console/costscope.go b/cmd/console/costscope.go
new file mode 100644
index 000000000..dde6244e6
--- /dev/null
+++ b/cmd/console/costscope.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ "github.com/carlosframework/platform/internal/console"
+)
+
+// costScopeFromEnv reads CONSOLE_COST_ACCOUNT_CARLOS_ONLY, the operator's
+// declaration that the deployment's AWS account runs CARLOS and nothing
+// else (console.Config.CostScope). Unset means the zero scope; a value
+// strconv.ParseBool cannot read is an error, and main stops the boot on it,
+// the way domainProof does for CONSOLE_DOMAIN_PROOF.
+func costScopeFromEnv(getenv func(string) string) (console.CostScope, error) {
+ v := strings.TrimSpace(getenv("CONSOLE_COST_ACCOUNT_CARLOS_ONLY"))
+ if v == "" {
+ return console.CostScope{}, nil
+ }
+ b, err := strconv.ParseBool(v)
+ if err != nil {
+ return console.CostScope{}, fmt.Errorf("console: CONSOLE_COST_ACCOUNT_CARLOS_ONLY %q is not a boolean (use true or false)", v)
+ }
+ return console.CostScope{AccountCarlosOnly: b}, nil
+}
diff --git a/cmd/console/costscope_test.go b/cmd/console/costscope_test.go
new file mode 100644
index 000000000..123617bbf
--- /dev/null
+++ b/cmd/console/costscope_test.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/console"
+)
+
+func TestCostScopeFromEnv(t *testing.T) {
+ env := func(v string) func(string) string {
+ return func(k string) string {
+ if k == "CONSOLE_COST_ACCOUNT_CARLOS_ONLY" {
+ return v
+ }
+ return ""
+ }
+ }
+ got, err := costScopeFromEnv(env(""))
+ if err != nil || got != (console.CostScope{}) {
+ t.Fatalf("unset: got %+v, %v; want zero scope", got, err)
+ }
+ got, err = costScopeFromEnv(env(" true "))
+ if err != nil || !got.AccountCarlosOnly {
+ t.Fatalf("true: got %+v, %v; want AccountCarlosOnly", got, err)
+ }
+ got, err = costScopeFromEnv(env("false"))
+ if err != nil || got.AccountCarlosOnly {
+ t.Fatalf("false: got %+v, %v; want zero scope", got, err)
+ }
+ if _, err := costScopeFromEnv(env("maybe")); err == nil || !strings.Contains(err.Error(), "CONSOLE_COST_ACCOUNT_CARLOS_ONLY") {
+ t.Fatalf("maybe: err = %v, want an error naming CONSOLE_COST_ACCOUNT_CARLOS_ONLY", err)
+ }
+}
diff --git a/cmd/console/main.go b/cmd/console/main.go
index 111f1f64f..8ce8d7810 100644
--- a/cmd/console/main.go
+++ b/cmd/console/main.go
@@ -483,6 +483,10 @@ func serve() {
if err != nil {
log.Fatal(err)
}
+ costScope, err := costScopeFromEnv(os.Getenv)
+ if err != nil {
+ log.Fatal(err)
+ }
cfg := console.Config{
DemoAlerts: demoAlerts,
DemoAPIOrigin: os.Getenv("CONSOLE_DEMO_API_ORIGIN"),
@@ -505,6 +509,9 @@ func serve() {
// The deployment's AWS account id, the trust anchor of the Phase 1
// move door's box identity check (unset: no box verifies).
AWSAccount: strings.TrimSpace(os.Getenv("CARLOS_AWS_ACCOUNT")),
+ // What the operator declares that AWS account holds: the bill pull
+ // stamps each bill's basis from it.
+ CostScope: costScope,
AppRegions: appRegionsSwitch(os.Getenv("CONSOLE_APP_REGIONS")),
// Region HA P1a: hosts created in these regions are born HA.
HARegions: haRegionsList(os.Getenv("CONSOLE_HA_REGIONS")),
diff --git a/docs/superpowers/notes/census-claims-billing-dash-3-cost.tsv b/docs/superpowers/notes/census-claims-billing-dash-3-cost.tsv
new file mode 100644
index 000000000..98be6003e
--- /dev/null
+++ b/docs/superpowers/notes/census-claims-billing-dash-3-cost.tsv
@@ -0,0 +1,3 @@
+# Billing dashboard Plan 3 (branch billing-dash-3-cost). One line per changed root-read unit: <file>:<unit><TAB><what changed>.
+# Task 1 changes no unit; it re-pins the internal/economics envelope only, which census-propose takes without a claim. The brief's claim, kept here for the reviewer:
+# internal/economics/bill.go:Bill: one additive string field and two consts; no read added
diff --git a/docs/superpowers/notes/census-proposal-billing-dash-3-cost.md b/docs/superpowers/notes/census-proposal-billing-dash-3-cost.md
new file mode 100644
index 000000000..ec1460c35
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-billing-dash-3-cost.md
@@ -0,0 +1,11 @@
+# Census approval: billing-dash-3-cost
+
+Ruling `census-ruling-billing-dash-3-cost`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+Envelopes re-pinned: `internal/economics`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Seal: `7ac2bde8301c37c7546e9aa19d375f1468fc8061876a618fa8f93dd4543c5652` → `7ac2bde8301c37c7546e9aa19d375f1468fc8061876a618fa8f93dd4543c5652`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-billing-dash-3-cost.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/console/cepull.go b/internal/console/cepull.go
index 52c4c08b1..2a1aad57b 100644
--- a/internal/console/cepull.go
+++ b/internal/console/cepull.go
@@ -39,18 +39,18 @@ import (
// rather than a mutex because billPass only ever runs from
// StartEconomicsPass's single-goroutine hourly ticker (economicspass.go) —
// there is no concurrent caller for these fields to race against.
-func (s *Server) resolveCECosts(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+func (s *Server) resolveCECosts(ctx context.Context, q ceQuery) (map[string]float64, error) {
if s.ceCosts != nil {
- return s.ceCosts(ctx, start, end)
+ return s.ceCosts(ctx, q)
}
- return s.realCECosts(ctx, start, end)
+ return s.realCECosts(ctx, q)
}
// realCECosts builds the Cost Explorer client once (s.ceClientOnce) and
// reuses it for the life of the process. See the package doc comment above
// for which failures latch s.ceDisabled (auth-shaped, permanent) versus
// which just fail this one tick (everything else, retried next hour).
-func (s *Server) realCECosts(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+func (s *Server) realCECosts(ctx context.Context, q ceQuery) (map[string]float64, error) {
if s.ceDisabled {
return nil, fmt.Errorf("console: cost explorer disabled for this process")
}
@@ -95,8 +95,8 @@ func (s *Server) realCECosts(ctx context.Context, start, end time.Time) (map[str
{Type: cetypes.GroupDefinitionTypeDimension, Key: aws.String("SERVICE")},
},
TimePeriod: &cetypes.DateInterval{
- Start: aws.String(start.Format("2006-01-02")),
- End: aws.String(end.Format("2006-01-02")),
+ Start: aws.String(q.Start.Format("2006-01-02")),
+ End: aws.String(q.End.Format("2006-01-02")),
},
})
if err != nil {
diff --git a/internal/console/claim_invariant_probes_worker_test.go b/internal/console/claim_invariant_probes_worker_test.go
index 0d5a44c5e..dff8f90fd 100644
--- a/internal/console/claim_invariant_probes_worker_test.go
+++ b/internal/console/claim_invariant_probes_worker_test.go
@@ -154,7 +154,7 @@ var claimWorkerProbes = map[string]claimProbe{
"worker StartEconomicsPass": {
setup: func(t *testing.T, cx *claimCase) {
// The bill pull is the one stage that would reach AWS: a seam answers it.
- cx.srv.ceCosts = func(context.Context, time.Time, time.Time) (map[string]float64, error) {
+ cx.srv.ceCosts = func(context.Context, ceQuery) (map[string]float64, error) {
return map[string]float64{"AmazonEC2": 1}, nil
}
},
diff --git a/internal/console/costscope.go b/internal/console/costscope.go
new file mode 100644
index 000000000..fef99914e
--- /dev/null
+++ b/internal/console/costscope.go
@@ -0,0 +1,34 @@
+package console
+
+import (
+ "time"
+
+ "github.com/carlosframework/platform/internal/economics"
+)
+
+// CostScope is what the operator has declared about the deployment's AWS
+// account (Config.CostScope). The zero value declares nothing: the account
+// may hold non-CARLOS resources, so a pulled bill is stored BasisUnstated.
+type CostScope struct {
+ // AccountCarlosOnly (CONSOLE_COST_ACCOUNT_CARLOS_ONLY) says the AWS
+ // account runs CARLOS and nothing else, so a Cost Explorer pull of the
+ // whole account is CARLOS platform cost and is stamped BasisAccount.
+ AccountCarlosOnly bool
+}
+
+// ceQuery is one Cost Explorer request through the ceCosts seam: the
+// billing period, Start inclusive and End exclusive, as CE takes it.
+type ceQuery struct {
+ Start, End time.Time
+}
+
+// billBasisFor decides, for one bill period, what to ask Cost Explorer and
+// which basis the resulting bill is stamped with. The query is the whole
+// account either way; only the declared scope decides the basis.
+func (s *Server) billBasisFor(start, end time.Time) (q ceQuery, basis string) {
+ q = ceQuery{Start: start, End: end}
+ if s.cfg.CostScope.AccountCarlosOnly {
+ return q, economics.BasisAccount
+ }
+ return q, economics.BasisUnstated
+}
diff --git a/internal/console/economicsbill_test.go b/internal/console/economicsbill_test.go
index 901639a1c..bcfeea909 100644
--- a/internal/console/economicsbill_test.go
+++ b/internal/console/economicsbill_test.go
@@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
+ "strings"
"testing"
"time"
@@ -23,7 +24,7 @@ func TestBillPassPullsAndManualWins(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
return map[string]float64{"AmazonEC2": 55.5}, nil
}
@@ -93,7 +94,7 @@ func TestBillPassNoCEIsQuiet(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
return nil, errors.New("AccessDeniedException: user is not authorized")
}
@@ -121,9 +122,9 @@ func TestBillPassHandlesMonthBoundary(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 9, 1, 3, 0, 0, 0, time.UTC)
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
- if start.Format("2006-01-02") == end.Format("2006-01-02") {
- t.Fatalf("ceCosts called with start == end == %s (day 1 collapse)", start.Format("2006-01-02"))
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
+ if q.Start.Format("2006-01-02") == q.End.Format("2006-01-02") {
+ t.Fatalf("ceCosts called with start == end == %s (day 1 collapse)", q.Start.Format("2006-01-02"))
}
return map[string]float64{"AmazonEC2": 10}, nil
}
@@ -147,7 +148,7 @@ func TestBillPassSkipsSecondPullSameDay(t *testing.T) {
now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
var calls int
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
calls++
return map[string]float64{"AmazonEC2": 10}, nil
}
@@ -185,7 +186,7 @@ func TestBillPassStopsPullingSettledClosedMonth(t *testing.T) {
putStoreJSON(t, fx.rel, economics.BillKey("2026-07"), settled)
var calls int
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
calls++
return map[string]float64{"AmazonEC2": 99}, nil
}
@@ -228,8 +229,8 @@ func TestBillPassRepullsJustClosedMonth(t *testing.T) {
putStoreJSON(t, fx.rel, economics.BillKey("2026-07"), notYetSettled)
var julyPulled bool
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
- if start.Format("2006-01") == "2026-07" {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
+ if q.Start.Format("2006-01") == "2026-07" {
julyPulled = true
}
return map[string]float64{"AmazonEC2": 99}, nil
@@ -268,7 +269,7 @@ func TestBillPassRetriesAfterTransientCEFailure(t *testing.T) {
now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
var calls int
- fx.srv.ceCosts = func(ctx context.Context, start, end time.Time) (map[string]float64, error) {
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
calls++
if calls <= 2 { // both months fail on the first tick
return nil, errors.New("ThrottlingException: rate exceeded")
@@ -446,3 +447,123 @@ func TestAPIBillDoor(t *testing.T) {
t.Fatalf("stored bill = %+v", stored)
}
}
+
+// readBillRaw returns the stored bytes and parsed bill for month.
+func readBillRaw(t *testing.T, fx *consoleFixture, month string) ([]byte, economics.Bill) {
+ t.Helper()
+ raw, _, err := fx.rel.Get(context.Background(), economics.BillKey(month))
+ if err != nil {
+ t.Fatalf("bill %s: %v", month, err)
+ }
+ var b economics.Bill
+ if err := json.Unmarshal(raw, &b); err != nil {
+ t.Fatal(err)
+ }
+ return raw, b
+}
+
+// TestBillPassStampsBasis: billPass records what each pulled bill covers.
+// With the AWS account declared CARLOS-only both months are stamped
+// "account"; with the zero scope both are unstated and carry no basis key.
+func TestBillPassStampsBasis(t *testing.T) {
+ now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
+ for _, tc := range []struct {
+ name string
+ scope CostScope
+ want string
+ }{
+ {"account", CostScope{AccountCarlosOnly: true}, economics.BasisAccount},
+ {"zero", CostScope{}, economics.BasisUnstated},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.CostScope = tc.scope
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
+ return map[string]float64{"AmazonEC2": 7}, nil
+ }
+ if err := fx.srv.billPass(context.Background(), now); err != nil {
+ t.Fatal(err)
+ }
+ for _, month := range []string{"2026-08", "2026-07"} {
+ raw, b := readBillRaw(t, fx, month)
+ if b.Basis != tc.want {
+ t.Fatalf("%s basis = %q, want %q", month, b.Basis, tc.want)
+ }
+ if tc.want == economics.BasisUnstated && strings.Contains(string(raw), `"basis"`) {
+ t.Fatalf("%s unstated bill carries a basis key: %s", month, raw)
+ }
+ }
+ })
+ }
+}
+
+// TestBillPassRepullsOnBasisChange: declaring the account CARLOS-only
+// changes what a pulled bill means, so the same-day and settle rules must
+// not keep a bill stamped with the old basis. Each month re-pulls exactly
+// once after the switch, then the usual rules apply again.
+func TestBillPassRepullsOnBasisChange(t *testing.T) {
+ ctx := context.Background()
+ now := time.Date(2026, 8, 11, 3, 0, 0, 0, time.UTC)
+
+ t.Run("same day", func(t *testing.T) {
+ fx := newConsoleFixture(t)
+ var calls int
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
+ calls++
+ return map[string]float64{"AmazonEC2": 3}, nil
+ }
+ if err := fx.srv.billPass(ctx, now); err != nil {
+ t.Fatal(err)
+ }
+ if calls != 2 {
+ t.Fatalf("zero-scope billPass made %d ceCosts calls, want 2", calls)
+ }
+ fx.srv.cfg.CostScope = CostScope{AccountCarlosOnly: true}
+ if err := fx.srv.billPass(ctx, now.Add(time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if calls != 4 {
+ t.Fatalf("billPass after the basis change made %d more ceCosts calls, want 2", calls-2)
+ }
+ for _, month := range []string{"2026-08", "2026-07"} {
+ if _, b := readBillRaw(t, fx, month); b.Basis != economics.BasisAccount {
+ t.Fatalf("%s basis = %q after re-pull, want account", month, b.Basis)
+ }
+ }
+ if err := fx.srv.billPass(ctx, now.Add(2*time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if calls != 4 {
+ t.Fatalf("third billPass made %d more ceCosts calls, want 0", calls-4)
+ }
+ })
+
+ t.Run("settled closed month", func(t *testing.T) {
+ fx := newConsoleFixture(t)
+ settled := economics.Bill{
+ Schema: 1, Month: "2026-07", TotalUSD: 10,
+ Services: map[string]float64{"AmazonEC2": 10}, Source: economics.SourceCostExplorer,
+ PulledAt: time.Date(2026, 8, 5, 3, 0, 0, 0, time.UTC),
+ }
+ putStoreJSON(t, fx.rel, economics.BillKey("2026-07"), settled)
+ fx.srv.cfg.CostScope = CostScope{AccountCarlosOnly: true}
+ var julyCalls int
+ fx.srv.ceCosts = func(ctx context.Context, q ceQuery) (map[string]float64, error) {
+ if q.Start.Format("2006-01") == "2026-07" {
+ julyCalls++
+ }
+ return map[string]float64{"AmazonEC2": 20}, nil
+ }
+ for i := 0; i < 3; i++ {
+ if err := fx.srv.billPass(ctx, now.AddDate(0, 0, i)); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if julyCalls != 1 {
+ t.Fatalf("settled July re-pulled %d times after the basis change, want exactly 1", julyCalls)
+ }
+ if _, b := readBillRaw(t, fx, "2026-07"); b.Basis != economics.BasisAccount || b.TotalUSD != 20 {
+ t.Fatalf("July after re-pull: basis %q total %v, want account 20", b.Basis, b.TotalUSD)
+ }
+ })
+}
diff --git a/internal/console/economicspass.go b/internal/console/economicspass.go
index e965260cf..5f29fa84a 100644
--- a/internal/console/economicspass.go
+++ b/internal/console/economicspass.go
@@ -1068,17 +1068,19 @@ func (s *Server) billPass(ctx context.Context, now time.Time) error {
// $14/mo (2 months × 24 ticks/day × 30 days) pulling data that, most hours,
// hasn't changed since the last pull. shouldSkipCEPull enforces "at most
// once per UTC day" for both months, plus the day-3-of-next-month
-// settle-and-stop rule for closedMonth.
+// settle-and-stop rule for closedMonth, except that a stored bill whose
+// basis differs from the one billBasisFor gives now is re-pulled once.
func (s *Server) pullBillMonth(ctx context.Context, month string, start, end, now time.Time, closedMonth bool) error {
+ q, basis := s.billBasisFor(start, end)
existing, err := s.getBill(ctx, month)
if err != nil {
return fmt.Errorf("read existing bill: %w", err)
}
- if shouldSkipCEPull(existing, closedMonth, now) {
+ if shouldSkipCEPull(existing, closedMonth, now, basis) {
return nil
}
- costs, err := s.resolveCECosts(ctx, start, end)
+ costs, err := s.resolveCECosts(ctx, q)
if err != nil {
return fmt.Errorf("cost explorer: %w", err)
}
@@ -1093,6 +1095,7 @@ func (s *Server) pullBillMonth(ctx context.Context, month string, start, end, no
Services: costs,
Source: economics.SourceCostExplorer,
PulledAt: now,
+ Basis: basis,
}
return s.upsertBill(ctx, bill, true)
}
@@ -1103,10 +1106,18 @@ func (s *Server) pullBillMonth(ctx context.Context, month string, start, end, no
// ever consults a SourceCostExplorer bill: a SourceManual bill is a
// different caller's business (upsertBill's manualWins check handles it at
// write time), not a reason to skip a pull here.
-func shouldSkipCEPull(existing *economics.Bill, closedMonth bool, now time.Time) bool {
+//
+// basis is the basis a pull made now would be stamped with. A stored bill
+// with a different basis was pulled under a different cost scope, so it is
+// re-pulled whatever the settle and same-day rules say. The re-pull stamps
+// the new basis, so this fires once per change, not every tick.
+func shouldSkipCEPull(existing *economics.Bill, closedMonth bool, now time.Time, basis string) bool {
if existing == nil || existing.Source != economics.SourceCostExplorer {
return false
}
+ if existing.Basis != basis {
+ return false
+ }
if closedMonth && usage.MonthOf(existing.PulledAt) > existing.Month && existing.PulledAt.Day() >= billCEDayGrace {
// Settled: a closed month pulled on or after day 3 of a LATER
// month has already seen CE's late-arriving line items land. Its
diff --git a/internal/console/server.go b/internal/console/server.go
index 797b15c9e..a8796e5ac 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -759,6 +759,11 @@ type Config struct {
// record against (boxid.Verifier.Account). Unset, no box verifies and
// no region can take a move. Only the Phase 1 move door reads it.
AWSAccount string
+ // CostScope (CONSOLE_COST_ACCOUNT_CARLOS_ONLY) is what the operator
+ // declares the AWS account holds. The hourly bill pull stamps each bill
+ // with the basis it implies (billBasisFor) and re-pulls a stored bill
+ // once when that basis changes. The zero value declares nothing.
+ CostScope CostScope
// DomainProof (CONSOLE_DOMAIN_PROOF) is the opt-in for proof of control
// on custom hostnames (#215). With it set, a member's attach is stored
// pending until a TXT record at _carlos-challenge.<host> carries the
@@ -1333,7 +1338,7 @@ type Server struct {
// economicspass.go): nil means "use the real, lazily-built CE client"
// (s.realCECosts); tests inject a fake so a pull can be proven to
// succeed, or to degrade quietly, without live AWS billing IAM.
- ceCosts func(ctx context.Context, start, end time.Time) (map[string]float64, error)
+ ceCosts func(ctx context.Context, q ceQuery) (map[string]float64, error)
// ceClientOnce/ceClient/ceWarnOnce/ceDisabled cache s.realCECosts'
// Cost Explorer client and its process-lifetime disabled state — see
// cepull.go's doc comment for why a mutex isn't needed here.
diff --git a/internal/economics/bill.go b/internal/economics/bill.go
index 506dd7aaa..98dd54c1b 100644
--- a/internal/economics/bill.go
+++ b/internal/economics/bill.go
@@ -8,9 +8,26 @@ const (
SourceCostExplorer = "costexplorer"
)
+// Bill bases: what a stored bill covers. Basis is additive on the wire: a
+// bill written before it existed, or one whose basis nobody stated, reads
+// as BasisUnstated and marshals with no basis key at all.
+const (
+ // BasisUnstated: nobody said what the bill covers. Every bill written
+ // before Basis existed, and every pull on a console without a declared
+ // cost scope.
+ BasisUnstated = ""
+ // BasisAccount: the whole AWS account, declared CARLOS-only (the
+ // console's CONSOLE_COST_ACCOUNT_CARLOS_ONLY cost scope).
+ BasisAccount = "account"
+ // BasisWhole: the whole AWS account, stated to hold more than CARLOS.
+ // Only manual entry writes it.
+ BasisWhole = "whole"
+)
+
// Bill is economics/bills/<month>.json — the real AWS bill for one month,
-// whole-account by-service (attribution to CARLOS is a dashboard knob, not
-// a pull-time filter — both AWS accounts hold non-CARLOS resources).
+// whole-account by-service. The pull never filters to CARLOS resources;
+// Basis records what the stored bill covers, so the dashboard can tell a
+// CARLOS-only account's bill from one that also holds other resources.
type Bill struct {
Schema int `json:"schema"` // 1
Month string `json:"month"` // "2006-01"
@@ -22,6 +39,9 @@ type Bill struct {
// own rule), so a manually-entered bill — which never sets this field —
// would otherwise marshal a lying "0001-01-01T00:00:00Z" pulled_at key.
PulledAt time.Time `json:"pulled_at,omitzero"`
+ // Basis is BasisUnstated | BasisAccount | BasisWhole; omitempty keeps an
+ // unstated bill byte-identical to one written before the field existed.
+ Basis string `json:"basis,omitempty"`
}
// BillsPrefix lists stored bills.
diff --git a/internal/economics/bill_test.go b/internal/economics/bill_test.go
index bf505ab6e..c5e1e8e79 100644
--- a/internal/economics/bill_test.go
+++ b/internal/economics/bill_test.go
@@ -26,3 +26,43 @@ func TestManualBillMarshalsWithoutPulledAt(t *testing.T) {
t.Fatalf("manual bill marshalled a pulled_at key: %s", raw)
}
}
+
+// TestBillBasisWireShape: basis is additive. A bill stored before basis
+// existed reads as BasisUnstated, an unstated bill marshals with no basis
+// key (byte-identical to the old shape), and the two stated bases round-trip.
+func TestBillBasisWireShape(t *testing.T) {
+ old := `{"schema":1,"month":"2026-10","total_usd":5,"services":{},"source":"costexplorer"}`
+ var b Bill
+ if err := json.Unmarshal([]byte(old), &b); err != nil {
+ t.Fatal(err)
+ }
+ if b.Basis != BasisUnstated {
+ t.Fatalf("old bill basis = %q, want unstated", b.Basis)
+ }
+ raw, err := json.Marshal(Bill{Schema: 1, Month: "2026-10", TotalUSD: 5, Services: map[string]float64{}, Source: SourceCostExplorer, Basis: BasisUnstated})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(raw), `"basis"`) {
+ t.Fatalf("unstated bill marshalled a basis key: %s", raw)
+ }
+ if string(raw) != old {
+ t.Fatalf("unstated bill wire shape changed:\n got %s\n want %s", raw, old)
+ }
+ for _, basis := range []string{BasisAccount, BasisWhole} {
+ raw, err := json.Marshal(Bill{Schema: 1, Month: "2026-10", Source: SourceManual, Basis: basis})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(raw), `"basis":"`+basis+`"`) {
+ t.Fatalf("basis %q not on the wire: %s", basis, raw)
+ }
+ var back Bill
+ if err := json.Unmarshal(raw, &back); err != nil {
+ t.Fatal(err)
+ }
+ if back.Basis != basis {
+ t.Fatalf("basis round-trip: got %q, want %q", back.Basis, basis)
+ }
+ }
+}
diff --git a/internal/hostops/storage_classifier_test.go b/internal/hostops/storage_classifier_test.go
index 8c9352a89..317c2d9db 100644
--- a/internal/hostops/storage_classifier_test.go
+++ b/internal/hostops/storage_classifier_test.go
@@ -54,6 +54,8 @@ func TestReviewedStorageBodiesBindIdentityAndScope(t *testing.T) {
{"status/consoles/console-a.json", `{"component":"console","actor":"console-b","instance_id":"console-b"}`, false},
{"control/moves-console.json", `{"v":99,"protocol":1}`, false},
{"economics/bills/2026-10.json", `{"schema":1,"month":"2026-09","source":"manual"}`, false},
+ {"economics/bills/2026-10.json", "{\"schema\":1,\"month\":\"2026-10\",\"source\":\"costexplorer\",\"basis\":\"account\"}", true},
+ {"economics/bills/2026-10.json", "{\"schema\":1,\"month\":\"2026-10\",\"source\":\"costexplorer\"}", true},
} {
t.Run(tc.key+tc.body, func(t *testing.T) {
_, _, err := classifyDeploymentFamily(nil, MutationTarget{Method: "Put", Key: tc.key, After: []byte(tc.body)})
diff --git a/internal/provision/testdata/plan14-root-reads-billing-dash-3-cost.tsv b/internal/provision/testdata/plan14-root-reads-billing-dash-3-cost.tsv
new file mode 100644
index 000000000..da7df45f8
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-billing-dash-3-cost.tsv
@@ -0,0 +1,3 @@
+# Census overlay billing-dash-3-cost, written by census-propose (#369). census-ruling-billing-dash-3-cost, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-billing-dash-3-cost.md.
+# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@coverage internal/economics 2c7a25baab91215d83101249e4966e37781b06f5a4d0ba889c03e88cb465408a
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 4c12aba2c..935ab5114 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -136,3 +136,4 @@ plan14-root-reads-edge-page-cache-1.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4
plan14-root-reads-cli-default-console-carloku.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216
plan14-root-reads-gvisor-demos.tsv f05716181a82721b38cba9b2d89c47925dc2ca426e5c4876ebf1df7b45895ee3
plan14-root-reads-dns-fresh-lookup-724.tsv 7ac2bde8301c37c7546e9aa19d375f1468fc8061876a618fa8f93dd4543c5652
+plan14-root-reads-billing-dash-3-cost.tsv 7ac2bde8301c37c7546e9aa19d375f1468fc8061876a618fa8f93dd4543c5652