Skip to content

Stage-3 S5: anomaly detectors — burn spike, schema drift, plan change (#12699) - #12754

Merged
dezren39 merged 2 commits into
mainfrom
felix/12699-anomaly-detectors
Nov 23, 2026
Merged

dezren39 merged 2 commits into
mainfrom
felix/12699-anomaly-detectors

Conversation

@dezren39

@dezren39 dezren39 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Implements #12699 (Stage-3 S5, design D6 from the approved design in #12702; epic #12694). Three detectors in pm-stack/aggregator/src/quota-anomaly.ts, wired fail-soft into the gatherer tick (scripts/gather-quota.ts) after the S1 forecast publish, plus a store replay tool (scripts/quota-anomaly-replay.ts, bun run replay:quota-anomaly) that produced the acceptance evidence below. Activation note at the end — please do not read "merged" as "live".

The detectors

Burn spike — signal: realized burn over the hour after a forecast origin vs the forecast itself. The band is the origin's projectWindow rate + the p99 of the same pool/window's matured forecast residuals (S2's construction, reused not reimplemented), and a spike must clear band × 1.25 with ≥ 1 pt/h excess over the forecast. Output: one finding per episode (re-arm after 2h back inside the band). Because the band is built from realized residuals, S2's known model bias (weekly windows projecting systematically late) is inside the band by construction — the detector fires on burn the model did not predict, not on the model's known error. Spike origins' own residuals are trimmed at maturation so the band cannot ratchet upward behind a spike. Fewer than 24 matured residuals, or residuals spanning < 12h → the series reports unevaluable with the reason named, never "no anomaly".

Adapter schema drift — signal: the parsed shape of a service's response vs its recorded baseline. Shape = raw response top-level keys (usage:<key>) + canonical window kinds (model-scoped names collapsed — the set of models is data, not schema; canonical windows fold renames, 5-hour ≡ 5h, so a rename is not drift). Additions and parse→unparseable transitions fire at once; removals need consecutive confirming parses (1 live tick; 3 active days in replay, because the store appends only on movement). A service whose first-ever observation is unparseable reports unevaluable (no baseline to drift from), and a lost state file re-baselines silently — no alert storm.

Vendor plan change — signal: a window limit/denominator change between consecutive snapshot records for the same service+metric (null limits neither fire nor update the baseline), plus window-kind set changes per forecast pool under the same field-set machine.

Where findings land (design D6): each new finding is (a) appended to quota-anomaly-history.jsonl beside quota-current.json as an evidence-baseline diff record {kind: "quota-anomaly-finding", version: 1, detector, key, baseline, observed, detail} — the baseline-vs-observed record is the diff; and (b) posted to the alerts ledger via AlertsStore.postIncident with source quota-anomaly, incident key = the finding's transition-unique key (episode dedupe rides the ledger's open-incident semantics). No alerts store → the history record still stands and the skip is logged (meta-keeper posture). State lives in quota-anomaly-state.json (recorded baselines + fired keys), written with the gatherer's writeAtomic.

Evidence

Venue for every run below: the Muse sandbox VM, bun test / bun run in pm-stack/aggregator.

Fail-before: the new suite copied into a pristine origin/main worktree fails (module not found; 0 pass, 1 error) — the fixtures have nothing to pass against before this change.

Unit fixtures (src/quota-anomaly.test.ts, 15 tests): per detector — a planted spike hour fires exactly once; a clean steady series stays silent (a measured silence: origins evaluated, band measurable); the pool's normal staircase lumpiness (residuals ±1 pt/h) stays silent while one outsized step fires; a thin series reports unevaluable with its reason; walled readings are never origins; the field-set machine (baseline / addition / sustained removal / unparseable episode / unparseable first contact); the entry shape builders; the limit leg (fires once with from→to; null limits inert); canonical window folding; tick integration (baselines establish silently, a shape change fires once, no re-fire from the updated state). Neighbouring suites (quota-forecast, quota-forecast-accuracy, quota-gather-map, quota-analytics) run with it: 72/72 pass. tsc --noEmit clean; biome clean.

Store replay (S2's snapshot-store copy: 20,663 records, 2026-10-20 → 2026-11-23; the full report + findings JSONL are the replay tool's output, regenerable with the command above): 84 findings — burn-spike 35, schema-drift 28, plan-change 21.

  • Plan change: the limit leg fires exactly the 9 turso limit-change events an independent pre-implementation scan of the store found (2026-11-08: databases 100→1e9, storage 5→24 GB, sync 3→24; 2026-11-14 flip-flops between two plan views), plus 12 window-kind changes (e.g. meta gained a 5h window on 2026-10-27). Clean week: 2026-11-16 → 2026-11-23, silent.
  • Burn spike: fires on the estate's real heavy-burn incidents — opencode-go weekly 2026-10-29T10:45 burned 72.2 pts/h vs a threshold of 8.3 (the start of its two-day blowout), _openai.alt 5h 2026-11-11T15:08 burned 98.1 vs 91.2 (a near-total 5h burn), anthropic:claude-pro 5h 2026-11-12T19:08 55 vs 32.7. 9 of 38 series stayed silent across the whole span; 17 report unevaluable with named reasons (no percent windows, cadence gaps, unmeasured band). Longest finding-free span: 2026-10-20 → 2026-10-28 (8.4 days) — that span is partly the pre-gatherer manual era, so the stronger silence evidence is per-series: for burn-spike, the anthropic:claude-pro series (both windows) are finding-free from 2026-10-20 to their first finding on 2026-11-12, across the gatherer era.
  • Schema drift: fires on the period's genuine adapter restructures (meta's payg fields appearing/disappearing 2026-10-27→30, openai:codex gaining a 5h window 2026-10-30, anthropic:claude-pro gaining 5h 2026-11-06). No finding-free span ≥ 7 days exists in the store for this detector — the estate restructured adapters continuously through the 33 days; the report states this plainly rather than rounding it away. Per-service silence is the norm (19 of 55 services evaluated silent, 21 baseline-only, 15 fired). Silence on clean data is proven by the unit fixtures.
  • Calibration record: the first replay cut produced 137 findings; diagnosed defects were fixed in order — cold-start thin bands firing marginally (now ≥ 24 matured residuals spanning ≥ 12h), a negative spread narrowing the band below the forecast (spread floored at 0), spike residuals ratcheting the band (trimmed at maturation), plus the ×1.25 margin and the 2h episode re-arm (burn findings 59 → 35) and the canonical rename fold (schema findings 35 → 28). Every remaining finding is enumerated in the replay report for the weekly accuracy review (S2's D7 loop) to judge.
  • Replay fidelity, stated in the report header: burn + limit legs replay at full fidelity; schema drift replays at store fidelity (day-granularity metric sets); the unparseable leg has no replay trace (the store records no parse outcomes) and is covered by the live path + unit fixtures instead.
  • Landing demonstration (replay-injected): the report's final section renders the first replay finding both ways it lands live — the history JSONL record and the postIncident input — from the same code the tick calls.

Activation

No new schedule: the detectors ride the existing quota-gatherer.timer (its registry row already exists), so no registry change. The code lands in the tick, but the laptop aggregator runs a deployed build — first live ticks wait on the next laptop rebuild, the same gate as #12718 and S1's live acceptance week. Nothing was enabled from the sandbox. #12699 stays open for that live acceptance.

No new dependencies.


Start Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…ft, plan change (#12699)

Three detectors (design D6) beside the gatherer tick, in
src/quota-anomaly.ts, on the S1/S2 quota data plane:

- burn spike: realized next-hour burn vs the projectWindow
  forecast rate plus the pool/window's own matured-residual p99
  band, cleared by a x1.25 margin with a >=1 pt/h materiality
  floor. The band is S2's residual construction, so the model's
  known weekly bias sits inside it by construction — bias is
  never an anomaly. Spike residuals are trimmed at maturation so
  the band cannot ratchet behind a spike; an episode fires once
  and re-arms after 2h back inside the band.
- adapter schema drift: parsed-shape changes (usage keys +
  canonical window kinds) and the fail-closed unparseable state,
  against a recorded per-service baseline; additions fire at
  once, removals after consecutive confirming parses; canonical
  windows fold renames (5-hour == 5h) so a rename is not drift.
- vendor plan change: limit/denominator moves between
  consecutive snapshot records, plus per-pool window-kind set
  changes.

Honesty (D6, carried from S1/S2): a detector that cannot
evaluate reports 'unevaluable' with a named reason — never 'no
anomaly' from absent data; baselines establish silently; state
loss re-baselines without an alert storm. Findings carry
baseline vs observed and land as evidence-baseline diff records
(quota-anomaly-history.jsonl) + alerts-ledger incidents
(postIncident, source 'quota-anomaly'); no alerts store = the
history record still stands. The gatherer hook is fail-soft, in
the forecast block's posture. No new schedule (rides the
existing quota-gatherer.timer), no new dependencies.
scripts/quota-anomaly-replay.ts replays the snapshot store for
the acceptance evidence (strict argv per #3352).
@dezren39
dezren39 requested a review from a team as a code owner November 23, 2026 01:22
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 04bfb988-a641-4d6c-adbe-b81e225a527f
Commits

Reviewing files that changed from the base of the PR and between 0be3183 and a507828.

Files selected for processing: 5

  • pm-stack/aggregator/package.json
  • pm-stack/aggregator/scripts/gather-quota.ts
  • pm-stack/aggregator/scripts/quota-anomaly-replay.ts
  • pm-stack/aggregator/src/quota-anomaly.test.ts
  • pm-stack/aggregator/src/quota-anomaly.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

…etectors

# Conflicts:
#	pm-stack/aggregator/scripts/gather-quota.ts
@dezren39

Copy link
Copy Markdown
Member Author

agent-note:v1
event: merge conflict resolved
by: agent felix (kind muse, Muse sandbox seat)
at: 2026-11-22

The conflict with main (from PR #12748, S3 shadow placement, merged after this branch was cut) is resolved in merge commit a507828 — origin/main merged into the branch, no rebase. The PR is MERGEABLE / CLEAN again and ready for Felix's merge. (This supersedes the premature "merged" note on #12699 — the first merge attempt hit this conflict and did not land.)

Resolution: one conflicted file, pm-stack/aggregator/scripts/gather-quota.ts, five hunks, all "both sides added a block" — no semantic collision. Both tick hooks survive intact, in this order after the S1 forecast block: (1) S3 shadow placement (consumes file + forecast), then (2) S5 anomaly detectors (consumes entries + fresh records + tickAt). Neither reads the other's output; both stay fail-soft after every live output is written. The header doc bullets, env-var doc lines, path constants, and imports were unioned (the forecast import keeps main's type QuotaForecastFile).

Evidence re-run on the merged tree (sandbox): bun test quota-anomaly 15/15 (61 expects); quota-shadow-placement 14/14 (63 expects); quota-forecast 31/31 across 3 files (444 expects); tsc --noEmit clean; bun build scripts/gather-quota.ts bundles clean.

@dezren39
dezren39 merged commit 2a064b4 into main Nov 23, 2026
1 check passed
@dezren39
dezren39 deleted the felix/12699-anomaly-detectors branch November 23, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant