carlos / platform Public

plans 4b: email and storage after-grace stages plan

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 4944c8bcd2d8af4bf7e70a26eede16a4d7e93d71 parent 9623e47
1 file changed, +207 −0
  • docs/superpowers/plans/2026-10-25-plans-4b-email-and-storage-stages.md +207 −0

CI failed — run details

diff --git a/docs/superpowers/plans/2026-10-25-plans-4b-email-and-storage-stages.md b/docs/superpowers/plans/2026-10-25-plans-4b-email-and-storage-stages.md
new file mode 100644
index 000000000..7616d4382
--- /dev/null
+++ b/docs/superpowers/plans/2026-10-25-plans-4b-email-and-storage-stages.md
@@ -0,0 +1,207 @@
+# Plans 4b: Email and Storage After-Grace Stages Implementation Plan
+
+> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
+
+**Goal:** The two console-side after-grace stages.
+- An account that sends twice its daily email limit has every sender paused until the UTC day rolls.
+- An account over its storage cap, past grace and past notice, has new uploads refused on every one of its stores. Reads and deletes keep working.
+- Both undo themselves: email at the UTC day roll, storage once usage is back under the cap or the cap is raised.
+
+**Architecture:**
+- **Email:** an account-wide check joins `enforceMailLimits`, which runs on every SES event. It pauses senders with a new pause kind, `plan`. The hourly grace pass resumes those senders after the day rolls, because a paused sender sends nothing, so no event arrives to resume it.
+- **Storage:** `objstore.Record` gains `WritesDenied`, and the store policy gains an unconditional `Deny s3:PutObject` while it is set. The hourly grace pass converges each store's record and re-runs the grant, which rewrites the policy (the `regrantForScanning` precedent).
+
+**Tech Stack:** Go; `internal/console`, `internal/mail`, `internal/objstore`.
+
+**Spec:** `docs/superpowers/specs/2026-10-20-plans-caps-packs-design.md`:
+- §5.6 (email: "warn at 80%, allow up to 2x the cap, pause at 2x"; the 10,000 hard ceiling);
+- §5.5 (storage: after grace, `Deny s3:PutObject`, reads and deletes keep working);
+- §6 (stages; the notice rule; "two things do not wait for notice: hard ceilings…").
+
+The plan builds on Plan 4a, merged as 9623e47: `plan_cap_state`, `afterGraceDue`, `planGracePass`, `measureCaps`.
+
+## Scope split (rest of Plan 4)
+
+- **4b (this plan):** email pause and storage write deny, both console-side.
+- **4c:** the box-side throttle for bandwidth and awake hours:
+ - throttle state in the limits record;
+ - edge throttling;
+ - open WebSockets closed and new upgrades refused;
+ - the 15-minute continuous-awake ceiling for instances.
+- **4d:** the lapse lifecycle (§9.2), including disabling running things over the cap, newest first. Nothing in the code can disable an app or instance today, and that mechanism serves lapse.
+
+## Calls for Paul (taken as stated unless he changes them)
+
+1. **The email pause does not wait for notice.** Email's grace is "the rest of the day up to 2x" (§6), so its after-grace moment is 2x, inside one day. The 72-hour notice rule could therefore never be met. Sending reputation is shared by every tenant (§5.6), the reason §6 gives for hard ceilings skipping notice. So the pause at 2x is treated as a ceiling. Owners already get the 80% and over emails that day from Plan 4a.
+2. **Storage at 2x the cap is a hard ceiling too** (table 4.2, "2x cap"). It denies writes without waiting for grace or notice. Below 2x, writes are denied only once `afterGraceDue` holds.
+
+## Global Constraints
+
+- **No catalog, no change:**
+ - every new check returns at once when `EffectiveLimits(acct)` is nil;
+ - no pause, no policy change, no record field written;
+ - existing per-sender caps, rate pauses and scanning denies behave exactly as today.
+- **Email pause point:** `min(2*EmailPerDay, Ceilings.EmailPerDay)`, where a ceiling of 0 means none. It applies to the account's `AccountSends` for the UTC day.
+- **Storage deny when** `afterGraceDue(st, now) || st.Used >= 2*st.Limit`. It is cleared when neither holds (back under, or a bigger limit). A storage measurement that can't be read leaves every store as it is.
+- **Only the capped action is refused.**
+ - Email: senders paused. Apps keep serving.
+ - Storage: `s3:PutObject` denied. `Get`, `List` and `Delete` stay allowed, so the customer can clean up.
+- **Per-instance senders are left alone.** `SetSenderPaused` cannot resume them (`ErrPerInstance`), and pausing one deactivates every instance's key. They are logged once per day roll. An operator's per-sender `DailyCap` still governs them.
+- **Every pause, resume, deny and clear is audited** on the account, with the figures behind it (the `mail.paused` precedent, `mailcounts.go:222`).
+- **Copy:** new strings join `planNoticeCopy`, and the Luna pass reviews them (Paul, 2026-10-25).
+- **Census:** the console envelope is re-pinned. A new objstore record writer needs a record-write census layer. Astra approves the census card, and only decisions go to Paul.
+- **Tests:** touched packages only, with `-run`; CI runs the full suite. Use `planNow` and inject `now`.
+
+## Review Focus
+
+1. **A sender paused for the plan comes back the next UTC day** even though it sends nothing, and only `plan` pauses come back. Rate and manual pauses never do. Pinned in Task 1 (`TestPlanMailPauseResumesOnDayRoll`, `TestPlanResumeLeavesOtherPausesAlone`).
+2. **Writes denied, reads and deletes allowed:** the policy keeps List, Get and Delete, keeps both anti-forgery denies and the scanning deny, and adds only the write deny. Pinned in Task 2 (`TestStorePolicyDeniesWritesOnly`).
+3. **A deny lifts by itself** when usage falls under the cap or the plan grows. Pinned in Task 3 (`TestStorageDenyClearsWhenBackUnder`).
+4. **Unknown is not over.** An unreadable storage sweep neither sets nor clears a deny. Pinned in Task 3 (`TestStorageDenyUnchangedWhenUnmeasured`).
+5. **The banner says why.** While a stage is active the account banner and CLI line say what is refused and until when. Pinned in Task 4 (`TestBannerSaysEmailPaused`, `TestBannerSaysStorageWritesDenied`).
+
+---
+
+### Task 1: Account-wide email pause and day-roll resume
+
+**Files:**
+- Modify: `internal/mail/mail.go:84` (add `PauseKindPlan = "plan"`).
+- Modify: `internal/console/mailcounts.go`:
+ - `enforceMailLimits` (:222) calls `s.enforceAccountMail` after its per-sender checks;
+ - `resumeStaleCapPause` (:176) also resumes `PauseKindPlan`.
+- Create: `internal/console/planmail.go`.
+- Modify: `internal/console/plangracepass.go`: `planGraceAccount` calls `s.resumePlanMail(ctx, acct, now)`.
+- Test: `internal/console/planmail_test.go`.
+
+**Interfaces:**
+- Consumes: `AccountSends` (`mailcounts.go:325`), `mail.ListSenders` (`mail.go:288`), `mail.SetSenderPaused` (`walk.go:2006`), `s.store.AppsIn`, `EffectiveLimits`, `cfg.PlanCatalog.Ceilings`.
+- Produces:
+ ```go
+ func planMailPauseAt(l *plans.Limits, ceilings plans.Limits) int64 // 0 when the plan sets no email limit
+ func (s *Server) enforceAccountMail(ctx context.Context, acct string, now time.Time) (bool, error) // pauses every active, non-per-instance sender of every app of acct when AccountSends >= planMailPauseAt; audits "mail.paused" once per sender with "plan: <n> of <limit> emails today"
+ func (s *Server) resumePlanMail(ctx context.Context, acct *Account, now time.Time) error // calls resumeStaleCapPause for every sender of every app (it resumes only plan/cap pauses from an earlier UTC day)
+ ```
+
+- [ ] **Step 1: Write the failing tests** (fixture: `mailFixture` / `seedCounts` from `mailcounts_test.go`, `examplePlanCatalog`, free plan `email_per_day: 50`)
+ ```go
+ func TestPlanMailPauseAt(t *testing.T) // limit 50, ceiling 10000 -> 100; limit 8000 -> 10000; ceiling 0 -> 2x; no email limit -> 0
+ func TestAccountMailPausesEverySenderAt2x(t *testing.T) // two apps, two senders; 100 sends today: every sender Active=false, PauseKind "plan"; audit mail.paused
+ func TestAccountMailBelow2xLeavesSenders(t *testing.T) // 99 sends: all active
+ func TestAccountMailWithoutACatalogUnchanged(t *testing.T) // 500 sends, no catalog: all active (per-sender caps aside)
+ func TestAccountMailSkipsPerInstanceSenders(t *testing.T)
+ func TestPlanMailPauseResumesOnDayRoll(t *testing.T) // paused at planNow; resumePlanMail at planNow+1 UTC day: active again, audit mail.resumed; same day: still paused
+ func TestPlanResumeLeavesOtherPausesAlone(t *testing.T) // a rate pause and a manual pause from yesterday stay paused
+ ```
+- [ ] **Step 2: Run** `go test ./internal/console/ -run 'PlanMailPauseAt|AccountMail|PlanMailPause|PlanResume'`. Expected: FAIL.
+- [ ] **Step 3: Implement.** In `enforceMailLimits`, call `enforceAccountMail` after the per-sender pause decision, whether or not this sender paused. Errors are logged and don't stop the per-sender result.
+- [ ] **Step 4: Run** again, plus `go test ./internal/console/ -run 'Enforce|MailCount|ResumeStale'`. Expected: PASS.
+- [ ] **Step 5: Commit** `console: account-wide email pause at twice the plan (plans 4b)`.
+
+### Task 2: Store write deny in the policy
+
+**Files:**
+- Modify: `internal/objstore/objstore.go:140` (`Record` gains `WritesDenied *WritesDenied`).
+- Modify: `internal/objstore/aws.go:180` (`storePolicyJSON`) and `:391` (`EnsureUser`).
+- Modify: `internal/objstore/provisioner.go:21` (the interface).
+- Modify: `internal/objstore/memprovisioner.go:79`.
+- Modify: `internal/objstore/walk.go:297` (`claimRawGrantIn` passes the flag).
+- Modify: the console wrappers at `internal/console/claim_backends.go:205`, `claim_store_effects.go:67` and `claim_purge_backend.go:45`, plus every test fake the compiler names.
+- Test: `internal/objstore/aws_policy_test.go` (or beside the existing `storePolicyJSON` tests), `internal/objstore/walk_test.go`.
+
+**Interfaces:**
+- Produces:
+ ```go
+ type WritesDenied struct { Reason string `json:"reason"`; At time.Time `json:"at"` } // on Record as `json:"writes_denied,omitempty"`
+ func storePolicyJSON(bucketARN string, deny *DenyTag, denyWrites bool) string // denyWrites adds {"Effect":"Deny","Action":"s3:PutObject","Resource":"<bucketARN>/*"}
+ // Provisioner:
+ EnsureUser(ctx context.Context, name, path, bucketARN string, deny *DenyTag, denyWrites bool) (arn string, err error)
+ func SetWritesDenied(ctx context.Context, s release.Store, acct, app, env string, wd *WritesDenied) (*Record, error) // CAS write of the record's field only; nil clears; a missing record is os.ErrNotExist
+ ```
+ `claimRawGrantIn` passes `rec.WritesDenied != nil`.
+
+- [ ] **Step 1: Write the failing tests**
+ ```go
+ func TestStorePolicyDeniesWritesOnly(t *testing.T) // denyWrites: the PutObject deny is present; Allow still has List/Get/Put/Delete/GetObjectTagging; both anti-forgery denies present; with a DenyTag the infected GetObject deny is still present
+ func TestStorePolicyUnchangedWithoutDeny(t *testing.T) // denyWrites false: byte-identical to today's output for the same inputs
+ func TestGrantWritesTheWriteDeny(t *testing.T) // record with WritesDenied, GrantIn on MemProvisioner: PolicyFor(user) holds the deny; cleared and re-granted: gone
+ func TestSetWritesDeniedRoundTrip(t *testing.T) // set then Get: field equal; nil clears; missing record: os.ErrNotExist
+ ```
+- [ ] **Step 2: Run** `go test ./internal/objstore/ -run 'StorePolicy|GrantWritesTheWriteDeny|SetWritesDenied'`. Expected: FAIL.
+- [ ] **Step 3: Implement.**
+- [ ] **Step 4: Run** again, plus `go test ./internal/objstore/ ./internal/console/ -run 'Store|Grant|Scan|Purge'`. Expected: PASS.
+- [ ] **Step 5: Commit** `objstore: a store policy that refuses new uploads (plans 4b)`.
+
+### Task 3: Storage stage in the grace pass
+
+**Files:**
+- Create: `internal/console/planstorage.go`.
+- Modify: `internal/console/plangracepass.go`: `planGraceAccount` calls `s.convergeStorageDeny(ctx, acct, st, measured, now)` after storing the storage row.
+- Test: `internal/console/planstorage_test.go`.
+
+**Interfaces:**
+- Consumes: `afterGraceDue`; `objstore.StoreEnvironments`, `objstore.GetIn`, `objstore.SetWritesDenied`, `objstore.GrantIn`; `claimAppCall` (the `regrantForScanning` precedent, `apiscan.go:624`); `appconfig.ParsePublicKey(s.cfg.SecretsPub, "CARLOS_SECRETS_PUBKEY")`.
+- Produces:
+ ```go
+ func storageDenyDue(st capState, now time.Time) bool // afterGraceDue(st, now) || (st.Level.over() && st.Used >= 2*st.Limit)
+ func (s *Server) convergeStorageDeny(ctx context.Context, acct *Account, st capState, measured bool, now time.Time) error
+ ```
+ `convergeStorageDeny` does nothing when `measured` is false. Otherwise, for each provisioned store of every app (the default store and every environment), it compares `rec.WritesDenied != nil` with `storageDenyDue(st, now)`. When they differ it:
+ - writes the field (`Reason`: `"plan storage: <used> of <limit> bytes"`);
+ - re-runs the grant with the record's own grant source, as `regrantForScanning` does;
+ - audits `store.writes_denied` or `store.writes_allowed`.
+
+ With no secrets key configured, it logs once and returns.
+
+- [ ] **Step 1: Write the failing tests** (`privateStorageFixture` or the `apistore` fixtures with `*objstore.MemProvisioner`; a storage day file through `putStorageDay`)
+ ```go
+ func TestStorageDenyDue(t *testing.T) // in grace: false; grace over + notice 72h: true; 2x without notice: true
+ func TestStorageDenySetAfterGrace(t *testing.T) // over row, grace ended, notice 72h old: record WritesDenied set, policy holds the deny, audit store.writes_denied
+ func TestStorageDenyClearsWhenBackUnder(t *testing.T) // then usage under the cap: field cleared, deny gone, audit store.writes_allowed
+ func TestStorageDenyUnchangedWhenUnmeasured(t *testing.T) // denied store, sweep unreadable: still denied
+ func TestStorageDenyWithoutACatalogUnchanged(t *testing.T)
+ ```
+- [ ] **Step 2: Run** `go test ./internal/console/ -run 'StorageDeny'`. Expected: FAIL.
+- [ ] **Step 3: Implement.**
+- [ ] **Step 4: Run** again. Expected: PASS.
+- [ ] **Step 5: Commit** `console: refuse new uploads past storage grace (plans 4b)`.
+
+### Task 4: The banner and CLI say why
+
+**Files:**
+- Modify: `internal/console/plancaps_copy.go` (keys `banner.email_paused`, `cli.email_paused`, `banner.storage_denied`, `cli.storage_denied`).
+- Modify: `internal/console/plangracepass.go`: `planNoticeText`, and `planNotice` gains `Stage string` (`""`, `"email_paused"`, `"storage_denied"`).
+- Test: `internal/console/plangrace_view_test.go`, `plangrace_cli_test.go`.
+
+**Interfaces:**
+- Produces: `planNoticesOf` sets `Stage`:
+ - `"email_paused"` for an `email` row whose `Used >= planMailPauseAt`. It needs the limits, so `planNoticesOf(states, now, l *plans.Limits, ceilings plans.Limits)`. Callers pass `EffectiveLimits` and the catalog's ceilings.
+ - `"storage_denied"` for a `storage` row where `storageDenyDue`.
+
+ `planNoticeText` uses the stage key when `Stage != ""`. Draft copy:
+ - email paused: "Email is paused until midnight UTC: this account sent %s." / cli: "email is paused until midnight utc: this account sent %s".
+ - storage denied: "New uploads to this account's storage are refused: %s. Downloads and deletes still work." / cli: "new uploads are refused: %s. downloads and deletes still work".
+
+- [ ] **Step 1: Write the failing tests**
+ ```go
+ func TestBannerSaysEmailPaused(t *testing.T) // email row at 2x: banner contains "Email is paused until midnight UTC"
+ func TestBannerSaysStorageWritesDenied(t *testing.T) // storage row past grace with notice: banner contains "New uploads to this account's storage are refused"
+ func TestCLIWarningSaysStorageWritesDenied(t *testing.T)
+ ```
+- [ ] **Step 2: Run** `go test ./internal/console/ -run 'BannerSays|CLIWarningSays'`. Expected: FAIL.
+- [ ] **Step 3: Implement.** Update every `planNoticesOf` caller.
+- [ ] **Step 4: Run** again, plus `go test ./internal/console/ -run 'Banner|CLIWarning|GracePass'`. Expected: PASS.
+- [ ] **Step 5: Commit** `console: banner and CLI say which plan stage is active (plans 4b)`.
+
+### Task 5: Docs, Luna, census and gates
+
+- [ ] Run a Luna copy pass on the four new strings: a committed brief carrying the house rules and example renderings. Apply its changes and show Paul only what it flags.
+- [ ] Extend `docs/site/plans.md` "Warnings and grace" with what happens after grace for email and storage, and how each lifts.
+- [ ] Census:
+ - run `make census-card`, add hand decision rows if a new unit appears, then `make census-propose NAME=plans-4b-after-grace CLAIMS=<file>`;
+ - add a record-write census layer for `objstore.SetWritesDenied`;
+ - Astra approves the card, and decisions go to Paul.
+- [ ] Merge main, push, wait for green CI, then Astra's whole-branch review.
+
+## Done when
+
+Every task's tests pass and Review Focus 1–5 are pinned. The census card is approved. CI is green after merging current main, and Astra's whole-branch review has no open Critical or Important finding.