| index 021c84707..728f56fa9 100644 |
| --- a/docs/superpowers/plans/2026-10-30-console-protection-tab.md |
| +++ b/docs/superpowers/plans/2026-10-30-console-protection-tab.md |
| @@ -10,7 +10,7 @@ |
| |
| **Spec:** `docs/superpowers/specs/2026-10-30-console-protection-tab-design.md` (rev 2, approved by Paul 2026-10-30). Read it first; this plan argues from it. |
| |
| -**Review history:** plan rev 1 Astra R1 NOT READY (4 P1, 2 P2: tier key conversion, owner-change reconciliation, counting-start tied to the window, group-cap off by one, size gate, Logs link population). Rev 2 fixes all six; findings at `docs/superpowers/plans/CODEX-FINDINGS-CONSOLE-PROTECTION-TAB-PLAN-R1.md`. Two rounds remain (AGENTS.md stopping rules). Plan's call, refining spec §3.1 to meet its own ownership rule: the hourly protection counters live on the host-owner record beside the group tally, not on `usage.EdgeRow`, so an owner change mid-hour erases nothing and each owner's group total always equals their caught total. |
| +**Review history:** plan rev 1 Astra R1 NOT READY (4 P1, 2 P2: tier key conversion, owner-change reconciliation, counting-start tied to the window, group-cap off by one, size gate, Logs link population). Rev 2 fixes all six; findings at `docs/superpowers/plans/CODEX-FINDINGS-CONSOLE-PROTECTION-TAB-PLAN-R1.md`. Plan rev 2 Astra R2 NOT READY: coverage ("counting since", partial days) failed a second round, so under stopping rule 2 Paul chose on 2026-10-30 to **cut the coverage claim**; also the far asset path's checks (P1), the logs filter placement (P2) and the logs link's start time (P2), all fixed in rev 3. Findings at `CODEX-FINDINGS-CONSOLE-PROTECTION-TAB-PLAN-R2.md`. One round remains. Plan's call, refining spec §3.1 to meet its own ownership rule: the hourly protection counters live on the host-owner record beside the group tally, not on `usage.EdgeRow`, so an owner change mid-hour erases nothing and each owner's group total always equals their caught total. Paul's ruling (R2): no coverage claim at all. The page's only evidence is the app's own checked count: zero checked reads "No protection counts yet", never "nothing caught". No "counting since", no partial days, no stored modes. |
| |
| ## Global Constraints |
| |
| @@ -21,6 +21,8 @@ |
| - Protection counts are keyed by host, account and app together, hourly counts and daily groups alike; nothing is reset on an owner change, and `usage.EdgeRow` gains no fields (refines spec §3.1, see Review history). |
| - Group keys are `rule.Tier.String() + "/" + rule.Group` (`reqrec.Tier` is a `uint8`; never `string(rule.Tier)`). |
| - Windows are whole UTC days: 7 (default) or 30, including today (spec §3.2). |
| +- No coverage claims (Paul, R2): nothing on the page or in the JSON says a window was fully counted, partly counted or when counting began. "Nothing caught" needs the app's checked count above zero. |
| +- Counts follow records: every place that creates a guard state and may file its Match record also makes exactly one `RecordGuard` call, under the same "this box answered it" condition, whether or not a rule matched and whether or not records are on. |
| - A fleet box's row counts only when its account is the fleet's owning account (`registry.Fleet.Account`). A fleet box writes what it likes under its own prefix; its rows may vouch only for its own account. |
| - Group tally: at most 32 keys per host-owner-day including `other`, so at most 31 named groups; a new group past that counts under `other`, an existing one keeps counting (spec §3.1). |
| - Copy: every user-facing string comes from the approved deck `docs/superpowers/plans/2026-10-30-console-protection-tab-copy.md`, verbatim. No em dashes. Never "exactly". No tenant names anywhere. |
| @@ -32,7 +34,7 @@ |
| 1. **A request a tunnel delivered, or one backhauled over a verified hop.** Expected: counted once fleet-wide. Counting sits exactly where `out.guard` is set (Task 2), so it follows the guard's own rule; Task 2's test pins the tunnel case. If the hop test shows the guard runs on both legs, that is a pre-existing double count in request records too: report it on the branch, do not fix it here. |
| 2. **A fleet box writing rows for an account that does not own its fleet.** Expected: those rows are ignored. Task 3's test pins it. |
| 3. **A host that changed owner mid-day.** Expected: the new owner sees only their own counts, and the old owner keeps theirs for the earlier hours and their own group tally. Tasks 1 and 4 pin it. |
| -4. **A window reaching back before the release that adds counting.** Expected: "counting started on", never "nothing caught". Task 4 pins it. |
| +4. **An app with traffic but no checked count (servers not yet on the release, or protection off).** Expected: "No protection counts yet", never "nothing caught". Task 6 pins it. |
| 5. **An edge day over the cache's body limit or unreadable.** Expected: the read fails visibly (an error on the page), never a short total presented as complete. Task 3 pins it, following `rateLimitCounts`'s rule. |
| |
| --- |
| @@ -52,9 +54,9 @@ |
| - `func GuardKey(host, account, app string) string` returning `host + "|" + account + "|" + app`. |
| - `const GuardGroupCap = 32`, `const GuardGroupOther = "other"`. |
| - `func AddGroup(groups map[string]int64, group string, n int64)`: adds to an existing key; a new key is admitted only while fewer than `GuardGroupCap-1` named (non-`other`) keys exist, otherwise the count goes to `GuardGroupOther`. Used by the meter and by the day merge. |
| - - `EdgeDay` fields `Guard map[string]GuardTally \`json:"guard,omitempty"\`` (keyed by `GuardKey`) and `GuardModes map[string][]string \`json:"guard_modes,omitempty"\`` (hour label to sorted, de-duplicated modes). |
| - - `type EdgeFlush struct { Rows map[string]EdgeRow; Guard map[string]GuardDelta; Mode string }` (`Guard` keyed by `GuardKey`). |
| - - `func (d *EdgeDay) AddFlush(hour string, f EdgeFlush)`: replaces `AddDeltas`. Folds `Rows` exactly as `AddDeltas` did; folds each `GuardDelta` into `Guard[key]`: `Hours[hour]` gains its three counts, its groups merge via `AddGroup`. When `f.Mode != ""` and the flush carries any row, adds `f.Mode` to `GuardModes[hour]`. |
| + - `EdgeDay` fields `Guard map[string]GuardTally \`json:"guard,omitempty"\`` (keyed by `GuardKey`). |
| + - `type EdgeFlush struct { Rows map[string]EdgeRow; Guard map[string]GuardDelta }` (`Guard` keyed by `GuardKey`). |
| + - `func (d *EdgeDay) AddFlush(hour string, f EdgeFlush)`: replaces `AddDeltas`. Folds `Rows` exactly as `AddDeltas` did; folds each `GuardDelta` into `Guard[key]`: `Hours[hour]` gains its three counts, its groups merge via `AddGroup`. |
| - `func UpsertEdgeDay(ctx context.Context, s release.Store, box string, now time.Time, f EdgeFlush) error`: writes nothing when `len(f.Rows) == 0 && len(f.Guard) == 0`. |
| - `EdgeRow` and `SetOwner` are unchanged. |
| |
| @@ -62,9 +64,8 @@ |
| - `TestAddFlushSumsGuardHours`: two flushes into hour `"09"`, each delta `Checked 10, Watch 2, Block 1, Groups {"probe/wordpress":2,"hard/secrets":1}`. Want `Guard[key].Hours["09"] == {20,4,2}` and `Groups {"probe/wordpress":4,"hard/secrets":2}`. |
| - `TestGuardOwnerChangeMidHourKeepsBoth` (Review Focus 3): in hour `"09"`, a delta for `acct1/shop` with `Watch 3, Groups {"probe/wordpress":3}`, then a row whose `SetOwner` moves the host to `acct2/blog` and a delta for `acct2/blog` with `Watch 2, Groups {"probe/wordpress":2}`. Want two tallies: `acct1/shop` hour `"09"` watch 3 with groups summing to 3, `acct2/blog` watch 2 with groups summing to 2. |
| - `TestAddGroupCapsAtOther`: add 40 distinct groups `g00`..`g39` with 1 each, then `g00` again with 1. Want `len == 32`, 31 named keys, `other == 9`, `g00 == 2`, total 41. |
| - - `TestGuardModesPerHour`: flush with one row and `Mode "catalogue"` in hour `"09"`, then one row and `Mode "block"` in `"09"`, then `Mode "catalogue"` with no rows or deltas in `"10"`. Want `GuardModes == {"09": ["block","catalogue"]}`. |
| - - `TestUpsertEdgeDayEmptyFlushWritesNothing`: `EdgeFlush{Mode: "watch"}` against a store that fails the test on any `Put`. |
| - - `TestEdgeDayOldShapeParses`: unmarshal a day JSON with no guard fields. Want nil `Guard` and `GuardModes`, rows intact. |
| + - `TestUpsertEdgeDayEmptyFlushWritesNothing`: `EdgeFlush{}` against a store that fails the test on any `Put`. |
| + - `TestEdgeDayOldShapeParses`: unmarshal a day JSON with no guard fields. Want nil `Guard`, rows intact. |
| |
| - [ ] **Step 2: Run them and see them fail** |
| |
| @@ -90,37 +91,39 @@ git commit -m "usage: protection counters, group tally and modes on the edge day |
| ### Task 2: Counting at the edge |
| |
| **Files:** |
| -- Modify: `internal/edge/usagemeter.go`, `internal/edge/guard.go`, `internal/edge/edge.go` (the ServeHTTP observer defer, around the `out.guard = e.newGuardState()` site and the `m.Record(...)` call), `internal/host/usage_loop.go`, `internal/host/agent.go:1074` |
| +- Modify: `internal/edge/usagemeter.go`, `internal/edge/edge.go` (the ServeHTTP observer defer, around the `out.guard = e.newGuardState()` site and the `m.Record(...)` call), `internal/edge/assetcatch.go` (`serveAssetFar`'s defer, beside its `observeRecords` call), `internal/host/usage_loop.go`, `internal/host/agent.go:1074` |
| - Test: `internal/edge/usagemeter_test.go`, `internal/edge/guard_usage_test.go` (new) |
| |
| **Interfaces:** |
| - Consumes: Task 1's `EdgeFlush`, `GuardDelta`, `GuardKey`, `AddGroup`. |
| - Produces: |
| - - `func (m guardMode) String() string`: `"catalogue"`, `"off"`, `"watch"`, `"block"`. |
| - - `func (u *UsageMeter) SetGuardMode(mode string)`. |
| - `func (u *UsageMeter) RecordGuard(rt registry.Route, rule *edgeguard.Rule, blocked bool)`: on the delta for `GuardKey(rt.Host, release.RouteAccount(rt), rt.App)`, `Checked++` always; when `rule != nil && rule.Mode != edgeguard.ModeBurst`, `Block++` or `Watch++` and `AddGroup(groups, rule.Tier.String()+"/"+rule.Group, 1)`. It does not touch the host's `EdgeRow`. |
| - - `func (u *UsageMeter) Drain() usage.EdgeFlush`: rows, guard deltas and the mode set by `SetGuardMode`; resets rows and deltas. |
| + - `func (u *UsageMeter) Drain() usage.EdgeFlush`: rows and guard deltas; resets both. |
| + - `func (e *Edge) observeGuardCount(rt registry.Route, out *outcome)`: when the meter is set and `out != nil && out.guard != nil`, one `RecordGuard` with the verdict's rule and block flag (nil rule when there is no verdict). Called from both sites below. |
| - `func usageLoop(drain func() usage.EdgeFlush, am *analytics.Meter)` and `usageFlush(..., drain func() usage.EdgeFlush, ...)`. |
| - - `(*Edge).EnableUsage(m)` calls `m.SetGuardMode(e.guard.String())` when `m != nil`. |
| |
| - [ ] **Step 1: Write the failing tests** |
| - `internal/edge/usagemeter_test.go`, `TestRecordGuardCounts`: rules taken from the real catalogue (`edgeguard`), not built by hand: a watch on a `probe/wordpress` rule, a block on a `hard/secrets` rule, a check with no rule, and a match on a `probe/admin` (burst) rule. Want the delta `Checked 4, Watch 1, Block 1`, groups exactly `{"probe/wordpress":1,"hard/secrets":1}`. |
| - `TestRecordGuardOwnerChangeWithinDrain` (Review Focus 3): one match for route `acct1/shop`, then the same host as `acct2/blog`, then a match for it, all before one `Drain`. Want two deltas, each with `Watch 1` and one group count. |
| - `internal/edge/guard_usage_test.go`, through a real `Edge` serving the real catalogue (production wiring, as `guard_test.go` builds one), with a `UsageMeter` enabled: |
| - - `TestGuardUsageCountsAProbe`: `GET /wp-login.php` on a routed host. Want delta `Checked 1, Watch 1`, groups `{"probe/wordpress":1}`, flush `Mode "catalogue"`. |
| + - `TestGuardUsageCountsAProbe`: `GET /wp-login.php` on a routed host. Want delta `Checked 1, Watch 1`, groups `{"probe/wordpress":1}`. |
| - `TestGuardUsagePlainRequestCountsChecked`: `GET /`. Want `Checked 1`, no watch, no groups. |
| - `TestGuardUsageBurstRuleNotCaught`: `GET /admin`. Want `Checked 1, Watch 0`, no groups. |
| - `TestGuardUsageTunnelDeliveredNotCounted`: the same probe arriving as a tunnel delivery (the context `fromTunnel` reads, as `guard_test.go` sets it). Want no delta. |
| - `TestGuardUsageCountsWithRecordsOff`: no request recorder enabled. Want the probe still counted. |
| - - `TestGuardUsageModeOff`: an Edge with `CARLOS_EDGE_PROTECT=off`. Want no delta and flush `Mode "off"`. |
| + - `TestGuardUsageModeOff`: an Edge with `CARLOS_EDGE_PROTECT=off`. Want no delta. |
| + - `TestGuardUsageFarAssetBlockCounted` (R2 finding 1): a probe on a declared asset path answered by the far edge's block page (`serveAssetFar`, `CARLOS_EDGE_PROTECT=block`). Want `Checked 1, Block 1` and one group count. |
| + - `TestGuardUsageFarAssetHitCounted`: a plain request for a cached asset answered from the far edge's store. Want `Checked 1`. |
| + - `TestGuardUsageFarAssetReachedHomeNotCountedHere`: a request the far edge handed to the home (a fill that reached it). Want no delta on the far edge; the home's own serving counts it. |
| + - `TestGuardUsageCountsFollowRecords`: for every request in this file's tests that runs with a request recorder, the recorder's Match records (excluding burst rules) equal `Watch + Block`, request for request. |
| - Review Focus 1: `TestGuardUsageHopCountsOnce`: a probe backhauled over a verified hop (the setup `homemap` tests use). Want the two edges' `Checked` sum to 1. If it is 2, mark the test `t.Skip` with a comment naming the pre-existing double count and record it on the branch (amadan discussion); do not change the guard's rule. |
| |
| - [ ] **Step 2: Run them and see them fail** |
| |
| Run: `go test ./internal/edge/ -run 'RecordGuard|GuardUsage'` |
| -Expected: FAIL (undefined `RecordGuard`, `SetGuardMode`). |
| +Expected: FAIL (undefined `RecordGuard`). |
| |
| -- [ ] **Step 3: Implement.** In the observer defer, beside `m.Record(...)` and under the same `m != nil` check but not under `!out.unbilled`, call `m.RecordGuard(up.route, rule, blocked)` when `out.guard != nil`, taking `rule` and `blocked` from `out.guard.verdict` (nil rule when there is no verdict). One call per request, so a request cannot be counted twice on one box. Update `usageLoop`/`usageFlush`/`agent.go` to the `EdgeFlush` drain. |
| +- [ ] **Step 3: Implement.** In ServeHTTP's observer defer, call `e.observeGuardCount(up.route, out)` beside `observeRecords` (not under `!out.unbilled`). In `serveAssetFar`'s defer, call it when `out != nil && out.guard != nil && (billed || !reached.Load())`, the same condition that decides its records minus the verdict check. One call per guard state, so a request cannot be counted twice on one box. Update `usageLoop`/`usageFlush`/`agent.go` to the `EdgeFlush` drain. |
| |
| - [ ] **Step 4: Run the tests and see them pass** |
| |
| @@ -190,31 +193,20 @@ git commit -m "console: one edge-day reader for root and fleet boxes; fleet rows |
| **Interfaces:** |
| - Consumes: Task 3's `edgeDaySource`, `rowCounts`, `tallyCounts`. |
| - Produces: |
| - - `type protectionDay struct { Date string; Checked, Watch, Block int64; Partial, NotCounted, Off bool }` (JSON `date`, `checked`, `watch`, `block`, `partial`, `not_counted`, `off`). |
| - - `type protectionSummary struct { From, To string; Days []protectionDay; Checked, Watch, Block int64; Groups map[string]int64; Modes []string; CountingSince string; Partial bool }` (JSON snake_case: `from`, `to`, `days`, `checked`, `watch`, `block`, `groups`, `modes`, `counting_since`, `partial`). |
| + - `type protectionDay struct { Date string; Checked, Watch, Block int64 }` (JSON `date`, `checked`, `watch`, `block`). |
| + - `type protectionSummary struct { From, To string; Days []protectionDay; Checked, Watch, Block int64; Groups map[string]int64 }` (JSON `from`, `to`, `days`, `checked`, `watch`, `block`, `groups`). `From` and `To` are the window's first and last dates. |
| - `func protectionDates(days int, now time.Time) []string`: the last `days` UTC dates including today, oldest first. `days` other than 7 or 30 is read as 7. |
| - `func sumProtection(srcs []edgeDaySource, dates []string, acct, app string) protectionSummary`: per-app (`acct`, `app` non-empty) totals. |
| - `func sumProtectionAll(srcs []edgeDaySource, dates []string) (protectionSummary, []protectionAppRow)` with `type protectionAppRow struct { Account, App string; Checked, Watch, Block int64 }`, sorted by `Watch+Block` descending, then account, then app. Fleet tallies are included only where `tallyCounts` would accept them for their own account. |
| |
| - Rules (spec §3.1, made concrete after plan R1 finding 3). Coverage is decided from what each box wrote, never from where the window starts: |
| - - **Box-hour state**, per source box and hour label: `off` when `GuardModes[hour]` is exactly `["off"]`; `counted` when `GuardModes[hour]` is any other non-empty set; `uncounted` when the box has rows in that hour but no `GuardModes` entry (an older binary wrote them). |
| - - **Switch-over hour:** a `counted` box-hour whose previous hour on the same box is `uncounted` (for hour `"00"`, the previous day's `"23"` when that day is among the sources) is also marked `switched`: the box may have changed binaries inside it. |
| - - **App hours** are the box-hours with an accepted row (`rowCounts`) whose `Requests > 0`. |
| - - **Day:** `NotCounted` when every app hour that day is `uncounted`; `Off` when every one is `off`; `Partial` when the day mixes `uncounted` with `counted` app hours, or has a `switched` app hour; otherwise complete. A day with no app hours is complete with zeros. |
| - - **`CountingSince`** is set only when the window contains an `uncounted` app hour; it is the date of the earliest `counted` app hour after the last `uncounted` one. A window wholly counted has `CountingSince ""` and says nothing about a start, so the 7- and 30-day views never disagree about it. |
| - - `Summary.Partial` is true when any day is `Partial` or `NotCounted`. |
| - - **Counts** sum `Hours` of the accepted tallies (`tallyCounts`) over the window's dates; `Groups` sums their `Groups`. |
| - - `Modes` is the sorted union of `GuardModes` over the app's box-hours. |
| + Rules: counts sum `Hours` of the accepted tallies (`tallyCounts`) over the window's dates, per day and in total; `Groups` sums their `Groups`. Nothing else is derived (no coverage, Paul's R2 ruling). |
| |
| - [ ] **Step 1: Write the failing tests** in `internal/console/protection_test.go`, building `edgeDaySource` values in memory, app `acct1/shop` on host `shop.example.com`: |
| - `TestProtectionDatesWholeDays`: `protectionDates(7, 2026-11-05T13:00Z)` is `2026-10-30` to `2026-11-05`, 7 entries; `protectionDates(9, …)` gives 7. |
| - - `TestSumProtectionTotals`: two boxes, hours `"09"` and `"10"` on `2026-11-05`, all `counted`, tally hours checked 100/50, watch 6/2, block 0/1, groups `probe/wordpress: 5`, `hard/secrets: 4`. Want `Checked 150, Watch 8, Block 1`, `Groups {"probe/wordpress":5,"hard/secrets":4}`, `Partial false`. |
| - - `TestSumProtectionCountingSince` (Review Focus 4): box A has rows without modes on `2026-11-03` (all hours) and on `2026-11-04` hours `"00"`-`"09"`, then modes from `"10"`. Want `2026-11-03 NotCounted`, `2026-11-04 Partial` (mixed and switched), `CountingSince "2026-11-04"`, `Summary.Partial true`. |
| - - `TestSumProtectionSameHistoryBothWindows` (plan R1 finding 3): one history, counting from `2026-10-20` (rows without modes before), now `2026-11-05`. The 30-day window gives `CountingSince "2026-10-20"`, `Partial true`; the 7-day window gives `CountingSince ""`, `Partial false`. |
| - - `TestSumProtectionNothingCaught`: all app hours `counted`, zero watch and block. Want `Watch 0, Block 0, Partial false, CountingSince ""`. |
| - - `TestSumProtectionOff`: all app hours `off`. Want every app day `Off` and `Checked 0`. |
| + - `TestSumProtectionTotals`: two boxes, hours `"09"` and `"10"` on `2026-11-05`, tally hours checked 100/50, watch 6/2, block 0/1, groups `probe/wordpress: 5`, `hard/secrets: 4`. Want `Checked 150, Watch 8, Block 1`, `Groups {"probe/wordpress":5,"hard/secrets":4}`. |
| + - `TestSumProtectionNothingCaught`: tallies with checked counts and zero watch and block. Want `Checked > 0, Watch 0, Block 0`. |
| + - `TestSumProtectionNoTallies`: rows with traffic, no tallies. Want `Checked 0` and every day zero. |
| - `TestSumProtectionOwnerChange` (Review Focus 3): host re-owned to `acct2/blog` in hour `"12"`, both owners' tallies present for that hour. Want `acct1/shop` to see only its tally (caught equals its group total), `acct2/blog` only its own. |
| - - `TestSumProtectionModes`: hours with `GuardModes {"09":["catalogue"],"10":["block","catalogue"]}`. Want `Modes ["block","catalogue"]`. |
| - `TestSumProtectionAllSortsApps`: three apps with caught 5, 12, 0. Want rows ordered 12, 5, 0 and summary totals the sum. |
| |
| - [ ] **Step 2: Run them and see them fail** |
| @@ -259,10 +251,7 @@ Draft rows (status `pending`; placeholders in braces are filled by the template) |
| | PT-C04 | templates/app_protection.html (mixed headline) | In the last {days} days, protection blocked {blocked} requests to this app and recorded {watched} more. | |
| | PT-C05 | templates/app_protection.html (share) | That's {pct} of the {checked} requests protection checked. | |
| | PT-C06 | templates/app_protection.html (nothing caught) | Nothing in the last {days} days looked like an attack. | |
| -| PT-C07 | templates/app_protection.html (counting since) | Protection counts started on {date}. | |
| -| PT-C08 | templates/app_protection.html (partial) | Some of this app's traffic in this window was served before counting started, so the real number may be higher. | |
| -| PT-C09 | templates/app_protection.html (off) | Protection is off on the servers that served this app. | |
| -| PT-C10 | templates/app_protection.html (no counts yet) | No protection counts yet for this app. They start when the servers that run it take the next release. | |
| +| PT-C10 | templates/app_protection.html (no counts yet) | No protection counts yet for this app. Counting starts once the servers that run it take the release that adds it, and only while protection is on. | |
| | PT-C11 | templates/app_protection.html (bars heading) | Day by day | |
| | PT-C12 | templates/app_protection.html (legend) | blocked | |
| | PT-C13 | templates/app_protection.html (legend) | recorded | |
| @@ -310,28 +299,29 @@ git commit -m "console: protection tab copy deck, reviewed by Paul" |
| - Query `range=7d|30d`; anything else is `7d` (a page never 400s over its query, as `handleAppAnalytics`). |
| - `func protectionGroupName(key string) string` in `protectiongroups.go`, from PT-C20..C36; unknown keys give PT-C36. |
| - Reads through `s.usageDayReader(now)` with `instanceStoreTimeout`. |
| - - Logs filter (plan R1 finding 6): the logs door and page accept `protection=1`, which keeps only records with `Fields["kind"]` `watch` or `block` whose `Fields["rule"]` names a catalogue rule that is not `edgeguard.ModeBurst` (lookup `func edgeguard.RuleByID(id string) (*edgeguard.Rule, bool)` over the embedded catalogue, added if none exists). The page form carries it as a hidden input so submitting the form and live following keep it. |
| - - Logs link: `{Nav}/apps/{app}/logs?stream=edge&since=168h&protection=1`, the same population the counts cover. On the 30-day view, PT-C19 sits beside the link. |
| + - Logs filter (plan R1 finding 6): the logs door and page accept `protection=1`, which keeps only records with `Fields["kind"]` `watch` or `block` whose `Fields["rule"]` names a catalogue rule that is not `edgeguard.ModeBurst` (lookup `func edgeguard.RuleByID(id string) (*edgeguard.Rule, bool)` over the embedded catalogue, added if none exists). It is a predicate on `applog.Filter` (new field `Keep func(Record) bool`, consulted by `Filter.Match`), so `ReadWindow` applies it inside `ReadChunks` before the per-box and merged `collectCap` trims and before pagination (R2 finding 3). The page form carries `protection=1` as a hidden input so submitting the form and live following keep it. |
| + - Logs link: `{Nav}/apps/{app}/logs?stream=edge&protection=1&since=<start>`, where `<start>` is the window's first date at `00:00:00Z` in RFC 3339, or `now-168h` when that is later (logs keep 7 days). The same population the counts cover (R2, R1-6). The Logs page keeps a `since` value it does not list as a selected option already (`app_logs.html`). On the 30-day view, PT-C19 sits beside the link. |
| |
| - [ ] **Step 1: Write the failing tests** in `internal/console/protectionview_test.go`, through the real server and router (as the analytics page tests do): |
| - `TestProtectionTabRendersHeadline`: a seeded root edge day with 8 watched of 150 checked today. Want status 200, PT-C02 with `8` and `7`, PT-C05 with `150`. |
| - `TestProtectionTabBlockingHeadline`: block 3, watch 0. Want PT-C03. |
| - - `TestProtectionTabCountingSince`: counting starts mid-window. Want PT-C07 and PT-C08. |
| - - `TestProtectionTabNoCounts`: days with traffic and no `guard_checked`. Want PT-C10 and never PT-C06. |
| - - `TestProtectionTabOff`: rows with traffic, `GuardModes` only `["off"]`, no `guard_checked`. Want PT-C09 and never PT-C06. |
| + - `TestProtectionTabNoCounts` (Review Focus 4): days with traffic and no guard tallies. Want PT-C10 and never PT-C06. |
| + - `TestProtectionTabNothingCaught`: tallies with checked counts, zero watch and block. Want PT-C06. |
| - `TestProtectionTabReadErrorSaysSo`: an unparseable edge day. Want PT-C18 and no headline. |
| - - `TestProtectionDataJSON`: the data door returns the summary's `checked`, `watch`, `block`, `counting_since`, `groups`. |
| + - `TestProtectionDataJSON`: the data door returns the summary's `checked`, `watch`, `block`, `days`, `groups`. |
| - `TestProtectionTabMembersOnly`: a signed-in user without access to the account gets what Analytics gives them (same status). |
| - `TestEveryCatalogueGroupHasAName`: for every rule in `edgeguard`'s embedded catalogue, `protectionGroupName(tier+"/"+group)` is not PT-C36. |
| - `TestAppNavHasProtection`: the app nav shows PT-C01 between Analytics and Activity. |
| - `TestLogsProtectionFilter`: seeded edge records: a `watch` on a `probe/wordpress` rule, a `block` on a `hard/secrets` rule, a `watch` on a `probe/admin` (burst) rule, and a plain `response` record. `GET …/logs/data?stream=edge&protection=1` returns exactly the first two. |
| + - `TestLogsProtectionFilterBeforeCap` (R2 finding 3): one protection match followed by `collectCap`+1 ordinary edge records in the window (cap lowered for the test if the reader exposes it, otherwise that many records). Both the data door and the follow endpoint with `protection=1` return the match. |
| + - `TestProtectionLogsLinkStart`: at `2026-11-05T13:00Z`, the 7-day link's `since` is `2026-10-30T00:00:00Z`; the 30-day link's is `2026-10-29T13:00:00Z`. |
| - `TestLogsPageCarriesProtectionFilter`: `GET …/logs?stream=edge&protection=1` renders with the edge stream selected, PT-C37 shown, and `protection=1` in the form and in the data URL the live follow uses. |
| - [ ] **Step 2: Run them and see them fail** |
| |
| Run: `go test ./internal/console/ -run 'ProtectionTab|ProtectionData|CatalogueGroup|AppNavHasProtection|LogsProtectionFilter|LogsPageCarriesProtection'` |
| Expected: FAIL. |
| |
| -- [ ] **Step 3: Implement.** Template in the Analytics tab's look (`app_analytics.html`): headline, share, day bars (blocked and recorded apart), groups table largest first, logs link, range switch. Add PT-C01..C19 and PT-C20..C37 to `protectionCopyBuilt`. |
| +- [ ] **Step 3: Implement.** Template in the Analytics tab's look (`app_analytics.html`): headline, share, day bars (blocked and recorded apart), groups table largest first, logs link, range switch. Add PT-C01..C06, PT-C10..C19 and PT-C20..C37 to `protectionCopyBuilt`. |
| - [ ] **Step 4: Run the tests and the copy gate and see them pass** |
| |
| Run: `go test ./internal/console/ -run 'Protection|CatalogueGroup|AppNav|LogsPage|TestProtectionCopyDeck'` |