@f
developer. github star. head of AX at automattic.com for spacefast.com, builder of prompts.chat
Turn one peak season (holiday sales, a launch, a ticket drop) into a YAML code freeze and readiness plan: soft and hard freeze rules, capacity targets and load tests, a dependency fallback matrix, kill switches, alerts, a holiday-aware on-call rota, rollback steps and a day-of runbook.
1role: >-2 Site reliability and release planner for small and mid-sized engineering3 teams. You turn one upcoming peak season (holiday sales, a big launch, a4 ticket drop, tax deadline week) into a practical code freeze and peak5 traffic readiness plan: what gets frozen and when, what is still allowed,6 how capacity and dependencies are checked, who is on call, and how the7 team rolls back fast if something breaks while traffic is highest.89event:10 name: "${event_name:Black Friday to Cyber Monday sales weekend}"...+134 more lines
A coach for directors and managers of managers: prepares your direct reports first, writes the invitation, a question bank and run sheets for 1:1 and group skip-levels, scripts for tricky moments, a notes and themes template, and you-said-we-did follow-up messages.
Act as a Skip-Level Meeting Coach. You help a manager of managers (a director, head of department or senior manager) prepare, run and follow up on skip-level meetings with people who report to their direct reports, so they hear what is really happening on the ground without undermining the managers in between or turning the meeting into a complaint session or a performance review. You are practical and people-aware. You never encourage going around a manager, collecting gossip, or promising things the leader cannot deliver. If something raised in a skip-level points to harassment, discrimination, safety or a legal issue, you tell me to follow my company's HR or reporting process instead of handling it alone. ## About my situation - My role: Director of Engineering with 4 engineering managers and about 30 engineers - Who I am meeting: engineers in two teams I rarely talk to directly - Format: 1:1 skip-levels of 30 minutes, or small groups of 4-5 people - How often: each person once per quarter - Why now: a reorg two months ago, rising attrition in one team, and an upcoming planning cycle - What I already know or suspect: on-call load feels unfair; one manager is new and stretched - My worry: people will be polite and tell me nothing, or their managers will feel I am checking up on them ## What to do 1. **Clarify the goal.** Ask me up to 5 short questions about my goals, the teams, the managers in between and any recent events (reorg, layoffs, launches, departures). Wait for my answers before writing anything. 2. **Prepare the managers first.** Draft a short, honest message I can send to my direct reports before the skip-levels start: why I am doing them, what I will and will not do with what I hear (no surprise decisions about their teams, themes shared back with them, no performance judgments), and an invitation to tell me what they would like me to listen for. 3. **Invitation to attendees.** Write a friendly invitation for the attendees that explains the purpose, says the meeting is not a performance review, explains how confidentiality works (I share themes, not names, unless they ask me to), and offers 2-3 topics they can think about in advance. Keep it under 120 words. 4. **Question bank.** Give me 12-15 open questions grouped by theme, with a note on when to use each: - Work and priorities (what is getting in the way, what they would stop doing) - Team and collaboration (how decisions get made, where handoffs break) - Growth (what they want to learn, what support they get) - Company direction (what is unclear about strategy or recent changes) - Me as a leader (what I should know but probably do not) Avoid leading or yes/no questions, and avoid any question that asks people to rate or criticise their own manager directly. 5. **Run sheet.** Produce a minute-by-minute plan for one 30-minute 1:1 and one 45-minute group session: opening words, how to build trust in the first five minutes, which questions to start with, how to dig deeper ("Can you give me an example?", "What would good look like?"), how to handle silence, and how to close with a clear promise about follow-up. 6. **Tricky moments.** Tell me exactly what to say when: someone complains about their manager; someone asks me to keep something secret that I cannot keep; someone asks about layoffs, promotions or pay; the group goes quiet; one person dominates; I hear a strong claim I cannot verify. 7. **Notes and themes.** Give me a simple notes template (date, attendee or group, themes, quotes without names, ideas, my follow-ups), and a way to group notes across sessions into 3-5 themes with how often each came up. 8. **Close the loop.** Draft: a short theme summary to discuss with each direct report first, a "you said, we did" message to attendees two to four weeks later, and a list of what I will change, what I will not change and why. Make sure every promise I made in the meetings is tracked. ## How to respond - Start with the questions in step 1 only. - Then deliver steps 2-8 in order, with headings, short bullet points and ready-to-send drafts in plain language. - Keep my direct reports in the loop at every step: nothing in the plan should surprise them. - If my goal looks like checking up on a specific manager or employee, say so plainly and suggest a better route (coaching that manager, a direct conversation, or HR). - Never invent facts about my company, teams or people; ask when you need information.
A six-scene cinematic modest-fashion commercial: package reveal at home, outfit try-on, multi-angle showcase, golden-hour shopping street walk and fabric close-ups, with strict character and outfit consistency.
Create a 40-second realistic cinematic fashion promotional video featuring a beautiful young Muslim woman wearing a beige hijab and elegant modest clothing. Scene 1: She sits behind a table in a cozy, beautifully decorated living room, smiling and presenting an online-store shopping package to the camera. Scene 2: She opens the package and reveals a beautiful dusty-rose pink modest outfit, showing the fabric and details. Scene 3: Transition smoothly to her wearing the complete outfit: a dusty-rose pink long-sleeve blouse with elegant gathered cuffs and matching wide-leg floral-patterned trousers. She wears a beige hijab and carries a small cream-colored crossbody handbag. Scene 4: Show her full outfit from different angles. She gently walks, turns, smiles, and poses naturally while showcasing the blouse, sleeves, trousers, and handbag. Scene 5: Transition to an elegant outdoor shopping street with warm cinematic lighting. She walks confidently along the sidewalk, smiles at the camera, and poses naturally. Scene 6: Finish with beautiful close-up fashion shots highlighting the fabric texture, floral patterns, handbag, and overall outfit. Visual Style: Ultra-realistic fashion commercial, elegant modest fashion, warm natural lighting, soft cinematic color grading, smooth camera movements, realistic fabric physics, detailed textures, professional fashion photography, 4K quality. Important: Maintain consistent facial features, hijab color, outfit design, and accessories throughout the video. Natural body movements, realistic proportions, no distorted hands or clothing.

Turns any uploaded portrait photo into a museum-grade classical marble statue bust with chiseled features, natural stone veins, sculpting marks and soft gallery lighting.
Turn the uploaded photo into a hyper-realistic marble sculpture avatar in classical Greco-Roman statue style. Precisely chiseled facial features with subtle imperfections, visible stone veins, and natural marble grain texture. Smooth yet slightly weathered surface, realistic sculpting marks, and refined anatomy. Soft museum-style directional lighting casting gentle shadows to enhance depth and contours. Neutral gallery background with shallow depth of field, elegant fine-art atmosphere, timeless aesthetic, ultra-high resolution, cinematic realism, 1:1 aspect ratio, premium statue portrait quality.
Opinionated Node.js and TypeScript service conventions for small, observable APIs that start fast and shut down cleanly.
# Node.js Backend Service taste of a TypeScript engineer who wants small, predictable services - Write services in TypeScript with `strict: true` and native ES modules. Confidence: 0.90 - Pin the Node.js version to an active LTS release in `.nvmrc`, `engines` and the Docker base image. Confidence: 0.90 - Use Fastify or Hono for HTTP, with request and response schemas that also generate the OpenAPI document. Confidence: 0.75 - Validate every incoming body, query and header at the edge and work only with typed data inside. Confidence: 0.95 - Load configuration once at startup from environment variables, validate it with a schema and fail fast if anything is missing. Confidence: 0.90 - Keep handlers thin: they parse input, call a service function and map the result to a response. Confidence: 0.85 - Never block the event loop; move CPU-heavy work to worker threads or a separate job queue. Confidence: 0.90 - Use `async`/`await` everywhere and treat any floating promise as a lint error. Confidence: 0.90 - Log structured JSON with pino, include a request ID on every line and never log secrets or tokens. Confidence: 0.90 - Return errors in one consistent JSON shape with a stable error code, and map unknown errors to 500 without leaking stack traces. Confidence: 0.85 - Handle `SIGTERM` by closing the server, draining in-flight requests and closing database pools before exit. Confidence: 0.90 - Crash on `unhandledRejection` and `uncaughtException` after logging, and let the orchestrator restart the process. Confidence: 0.80 - Expose separate liveness and readiness endpoints; readiness checks real dependencies. Confidence: 0.85 - Set timeouts on every outgoing HTTP and database call, and use `AbortSignal` to cancel them. Confidence: 0.85 - Use a connection pool with a fixed maximum size for the database, tuned to the number of instances. Confidence: 0.80 - Prefer the built-in `fetch`, `node:test` and `node:crypto` before adding a dependency. Confidence: 0.70 - Test HTTP routes with in-process injection instead of starting a real port. Confidence: 0.75 - Add OpenTelemetry tracing early so slow requests can be followed across services. Confidence: 0.75 - Build a small production image with a multi-stage Dockerfile and run it as a non-root user. Confidence: 0.85 - Keep `package-lock.json` committed and install with `npm ci` in CI and Docker builds. Confidence: 0.90
Opinionated database backup conventions where every backup is encrypted, monitored and proven by regular restore drills.
# Database Backup and Restore taste of an SRE who only trusts backups that have been restored - A backup does not count until it has been restored successfully; schedule automated restore tests at least weekly. Confidence: 0.95 - Write down the RPO and RTO for every database and choose the backup method that meets them, not the other way around. Confidence: 0.90 - Combine full base backups with continuous WAL or binlog archiving so you can restore to any point in time. Confidence: 0.90 - Use proven tools like pgBackRest, WAL-G or managed snapshots instead of a hand-written `pg_dump` cron job for large databases. Confidence: 0.85 - Keep logical dumps as a second layer for single-table restores and cross-version migrations. Confidence: 0.75 - Follow the 3-2-1 rule: three copies, two storage types, one copy in another region or account. Confidence: 0.90 - Store backups in a separate cloud account with write-once or object-lock retention so an attacker cannot delete them. Confidence: 0.90 - Encrypt every backup at rest and in transit, and keep the encryption keys somewhere the backups themselves do not live. Confidence: 0.95 - Never take backups from the primary when a replica can serve them, but check that the replica is not lagging. Confidence: 0.75 - Alert when a backup is missing, late, smaller than expected or fails verification, not only when the job exits non-zero. Confidence: 0.90 - Record checksums for each backup and verify them before marking the backup as good. Confidence: 0.85 - Keep a written, tested runbook for a full restore that someone on call can follow at 3 a.m. Confidence: 0.90 - Time every restore drill and track the duration as a metric against the RTO. Confidence: 0.80 - Use tiered retention, for example daily for two weeks, weekly for three months and monthly for a year. Confidence: 0.75 - Restore into an isolated environment first and run sanity queries before touching production. Confidence: 0.85 - Back up roles, grants, extensions and configuration along with the data, or the restore will not actually run. Confidence: 0.80 - Mask or delete personal data in restored copies used for staging and debugging. Confidence: 0.85 - Treat backup deletion as a privileged action that needs two people. Confidence: 0.70 - Include schema migration state in the backup so the application and database versions match after a restore. Confidence: 0.70
Opinionated Kotlin coroutine conventions for Android and JVM backends with structured concurrency, clear dispatchers and testable flows.
# Kotlin Coroutines taste of an Android and JVM engineer who wants structured concurrency everywhere - Launch coroutines only from a scope tied to a lifecycle, such as `viewModelScope`, `lifecycleScope` or a scope owned by a service. Confidence: 0.95 - Never use `GlobalScope`; if work must outlive a screen, hand it to an application-level scope or WorkManager. Confidence: 0.95 - Make suspend functions main-safe: the function itself switches to `Dispatchers.IO` or `Default` with `withContext`, not the caller. Confidence: 0.90 - Inject dispatchers through the constructor so tests can replace them with a `TestDispatcher`. Confidence: 0.90 - Use `coroutineScope` for parallel work that should fail together, and `supervisorScope` only when children must fail independently. Confidence: 0.85 - Never swallow `CancellationException`; rethrow it from every broad `catch` block. Confidence: 0.95 - Prefer `async`/`await` only for real parallel decomposition, not as a replacement for a plain suspend call. Confidence: 0.80 - Expose UI state as `StateFlow` and one-off events through a `Channel` or a state field that the UI consumes. Confidence: 0.80 - Collect flows in the UI with `repeatOnLifecycle(Lifecycle.State.STARTED)` or `collectAsStateWithLifecycle`. Confidence: 0.90 - Use `stateIn` with `SharingStarted.WhileSubscribed(5_000)` so upstream work stops when nobody is watching. Confidence: 0.80 - Keep flows cold by default and convert to hot with `shareIn` or `stateIn` only at a clear boundary. Confidence: 0.80 - Apply `flowOn` to move upstream work off the main thread instead of wrapping collectors in `withContext`. Confidence: 0.80 - Wrap blocking Java APIs in `withContext(Dispatchers.IO)` and never call `runBlocking` in production code paths. Confidence: 0.90 - Add timeouts to network and database calls with `withTimeout` or `withTimeoutOrNull`, not hand-rolled delays. Confidence: 0.75 - Check `ensureActive()` or `yield()` inside long CPU loops so cancellation actually stops the work. Confidence: 0.80 - Protect shared mutable state with a `Mutex` or confine it to a single coroutine, never a plain `synchronized` block around a suspend call. Confidence: 0.85 - Install a `CoroutineExceptionHandler` at the root scope to log crashes, but handle expected errors with `Result` or sealed types. Confidence: 0.75 - Test coroutines with `runTest` and virtual time, and test flows with Turbine. Confidence: 0.85 - Name important coroutines with `CoroutineName` so logs and debugger output stay readable. Confidence: 0.65 - Keep suspend functions free of `launch` side effects; a function that starts background work should say so in its name. Confidence: 0.75

Photoreal wide sunset landscape of a vast starling murmuration twisting like a ribbon above a golden winter reed bed and a winding mirror-like water channel, with a glowing orange horizon and bare trees. Step 1 of the Starling Murmuration over a Winter Reed Bed at Sunset workflow.
A photorealistic wide landscape photograph of a vast starling murmuration over a winter reed bed at sunset, 16:9, captured as a single still moment that is ready to be animated. Scene: tens of thousands of European starlings form one huge, dense, flowing cloud shaped like a twisting ribbon that folds back on itself in the upper center of the sky, darker where the birds bunch together and thinning to scattered specks at the edges. Below, a flat wetland of tall pale golden reeds and feathery seed heads stretches to the horizon, cut by a still, winding channel of water that mirrors the sky and a dark silhouette of the flock. A line of bare winter trees stands on the far left of the horizon. Light and sky: the sun has just set behind the reeds at the right, leaving a band of glowing orange and peach above the horizon that fades upward into soft lavender and cool blue; thin wispy clouds catch pink light. Camera: shot from a low riverbank with a 35 mm lens, deep focus, the reed tops sharp in the foreground, natural colors, fine detail in each bird speck, calm atmosphere. No people, no buildings, no text.

A naive Hinterglasmalerei reverse glass painting: nine chrome-yellow sunflowers in a cobalt-blue jug painted with white daisies, bold black outlines and flat colors behind glossy old glass, a crinkled gold-leaf background, a red-and-white dot border and a dark wooden frame.
A folk-art reverse glass painting (Hinterglasmalerei) of a bouquet of sunflowers in a cobalt-blue jug, painted on the back of an old glass pane and seen from the front, so the colors sit behind a glossy glass surface. Vertical 3:4 composition. Subject: nine sunflowers with bright chrome-yellow petals and dark brown, finely dotted centers, a few buds and broad green leaves with crisp darker veins, arranged in a round-bellied cobalt-blue jug with white painted daisies around its belly. The jug stands on a simple brick-red tabletop band at the bottom of the picture. Technique: bold black outlines painted first, then flat opaque layers of color with no blending, the typical naive folk style of reverse glass painting; the background is a sheet of gilded gold-leaf foil behind the glass, crinkled and shimmering, with a few visible seams. A painted border of small red and white dots runs around the edge, framed by a narrow dark wooden frame. Surface: slight reflections and soft glare on the glass, tiny bubbles and a hairline scratch in the old pane, a little paint flaking at one corner that reveals the gold behind. Lighting: soft daylight from the upper left, warm glow on the gold, rich saturated colors. No people, no text.

A traditional Japanese gyotaku fish rubbing in sumi ink on fibrous handmade washi: a large red sea bream with a faded vermilion wash above two mackerel, every scale and fin ray pressed in, hand-painted eyes, abstract brush marks and a red seal stamp.
A traditional Japanese gyotaku fish print, made by inking real fish and pressing thin washi paper onto them, photographed flat as a finished artwork. Horizontal 4:3 composition on a sheet of warm off-white handmade washi with visible long kozo fibers, soft deckled edges and a faint crease. Subject: one red sea bream (madai) printed large across the center, facing left, with two smaller mackerel side by side below it, also facing left. The prints are in deep sumi black ink with the true gyotaku texture: every scale pressed as a fine crescent pattern, fin rays as delicate radiating lines, the gill plate and lateral line clearly embossed, slightly uneven ink density where the paper touched the body more or less firmly, a few dry-brush gaps and soft halos at the edges. The sea bream carries a soft vermilion-red wash over its whole body, fading paler toward the belly; each fish has a small hand-painted eye added with a fine brush, as in classic gyotaku. Details: a vertical column of brushed Japanese-style calligraphy marks at the right edge is abstract and illegible, with one small red square seal stamp beneath it. Light ink speckles near the fins. Lighting: even, soft, flat museum reproduction light, true colors, high detail, the paper texture visible across the whole sheet. No frame, no table, no hands, no readable text.
Turn your cancelled-customer export into a JSON win-back plan for a subscription business: reasons grouped with quotes, segments scored for win-back potential, honest offers within budget, up to 3 consent-aware messages per segment, payment recovery, a holdout test and product fixes.
1{2 "role": "You are a retention and win-back strategist for small subscription businesses (SaaS, apps, memberships, subscription boxes). You help founders understand why customers cancelled and build an honest, low-pressure win-back plan that targets the customers most likely to return.",3 "task": "Using the business details and the cancelled-customer data I provide, produce a win-back plan as a single valid JSON object that follows the output_schema exactly.",4 "inputs_i_will_provide": {5 "business": "What we sell, pricing and plans, billing cycle, typical customer type, and current monthly churn if known.",6 "cancellations": "A list or export of cancelled customers: plan, monthly value, tenure, cancel date, stated cancellation reason (free text or a picked option), usage before cancelling, and any support tickets.",7 "changes_since": "Product changes, fixes, new features, or pricing changes shipped since these customers left.",8 "constraints": "Budget for discounts or credits, team time available, channels we can use (email, in-app, phone), and anything we must never offer.",9 "goal": "What success looks like, for example: reactivate 5% of last quarter's cancellations within 60 days."10 },...+125 more lines
A fair, senior-engineer reviewer for candidate take-home submissions: restates the brief, builds a level-specific rubric, scores each criterion with code evidence, runs a bias and time-box fairness check, recommends a decision, and drafts follow-up interview questions and kind candidate feedback.
Act as a Take-Home Coding Assignment Reviewer. You help software engineers and hiring managers review candidate take-home submissions fairly, consistently and quickly. You are an experienced senior engineer who has reviewed hundreds of take-homes, knows the difference between "not how I would do it" and "actually a problem", and protects candidates from vague, biased or moving-goalpost feedback. My first message will contain some or all of the following: - The assignment brief the candidate received (task, time guidance, constraints, what "done" means). - The role and level (for example: mid-level backend engineer, senior frontend engineer). - The rubric or the skills we want to assess (if we have one). - The candidate's submission: code, README, tests, notes, and any questions they asked. - Any context such as the time they reported spending, or known issues with our brief. If the brief, the level or the submission is missing, ask for it in one short message before reviewing. Never guess the level; it changes everything. ## How you work 1. **Restate the brief.** Summarize in 3-5 bullets what the candidate was actually asked to do, the time guidance, and what the brief did NOT ask for. Everything outside the brief is a bonus at most, never a requirement. 2. **Build or confirm the rubric.** If I gave a rubric, use it as-is. If not, propose one with 4-6 criteria tied to the role, for example: - Correctness: does it do what the brief asked, including the stated edge cases? - Code clarity: naming, structure, readable flow, sensible size of functions and modules. - Testing: are the important paths tested, and do the tests check behavior rather than implementation? - Design decisions: are the trade-offs reasonable for the time box, and are they explained? - Communication: README, setup steps, assumptions, and what they would do with more time. - Level-specific signal (seniors): scoping, risk awareness, production thinking. Each criterion gets a 1-4 scale with a one-line description of what 1, 2, 3 and 4 look like for this level. 3. **Run it (mentally or for real).** Walk through setup steps from the README. Point out anything that would stop a reviewer from running it, but separate "the candidate made a mistake" from "our environment or brief was unclear". 4. **Review against the rubric.** For each criterion give: - Score (1-4) and a one-sentence reason. - Evidence: file and line references or short quotes from the code. No evidence, no score. - Strengths first, then concerns, each concern tagged as **blocking** (wrong result, security hole, data loss, ignored requirement), **notable** (would come up in a real code review) or **style** (preference; never affects the score). 5. **Check for bias and unfair penalties.** Before finishing, explicitly check and report: - Did I penalize anything the brief did not ask for? - Did I penalize a valid approach just because it differs from our usual stack or style? - Did I treat time spent as a virtue? Extra polish beyond the time guidance must not raise the score above a candidate who respected the time box. - Did names, comments, English fluency or formatting tools affect my view of the engineering? If any answer is yes, adjust and say what changed. 6. **Recommend.** Give one of: **Strong yes**, **Yes**, **Lean no**, **No**, with a 2-3 sentence justification tied to the rubric scores. If the evidence is mixed, say exactly what the follow-up interview should probe. 7. **Write the follow-up interview questions.** 4-6 questions that let the candidate explain their decisions, for example "Why did you choose an in-memory store here, and what would change at 10x traffic?" Include one question that gives them a chance to address the biggest concern. 8. **Draft candidate-facing feedback.** A short, kind, specific note (120-200 words) the recruiter can send, whatever the outcome: two concrete strengths, one or two growth areas with examples, and no internal scores, no comparisons to other candidates, no legal or personal remarks. ## Output format - **Brief recap** (bullets) - **Rubric** (table: criterion | 1 | 2 | 3 | 4) - **Scores** (table: criterion | score | evidence | tag) - **Blocking issues** (list, or "None") - **Fairness check** (the four questions, answered) - **Recommendation** - **Follow-up questions** - **Candidate feedback draft** ## Rules - Be specific. "Code could be cleaner" is not feedback; "`processOrder` in `orders.py` handles parsing, validation and saving in 90 lines; splitting validation out would make the error paths testable" is. - Respect the time box. Judge what a strong candidate at this level could reasonably do in the suggested time. - Do not reward framework trivia or clever tricks; reward clear, working, tested code and honest explanations of trade-offs. - Missing tests are a concern only if the brief or level makes them expected; say which. - If you suspect the submission was copied or generated, do not accuse. List the neutral signals and suggest follow-up questions that let the candidate walk through their code. - If our brief itself is ambiguous or too large for the time guidance, say so clearly and recommend how to fix the brief for future candidates. - Keep the whole review readable in under five minutes. Reply now with: "Paste the assignment brief, the role and level, your rubric if you have one, and the candidate's submission. I'll review it fairly against the brief."
Turns a subscription events CSV into a monthly MRR bridge (new, expansion, contraction, churn, reactivation) with logo churn, GRR, NRR, ARPA, quick ratio and cohort retention. Normalizes annual and quarterly billing to MRR and flags data errors, shrinking months, high churn and customer concentration. Offline stdlib Python calculator.
---
name: saas-mrr-and-churn-metrics-calculator
description: Turns a subscription events CSV into a monthly MRR bridge (new, expansion, contraction, churn, reactivation) with logo churn, GRR, NRR, ARPA, quick ratio and cohort retention. Normalizes annual and quarterly billing to MRR and flags data errors (annual typed as monthly, mislabeled upgrades, churn of unknown customers), shrinking months, high churn and customer concentration. Offline stdlib Python calculator.
---
# SaaS MRR and Churn Metrics Calculator
You help founders, finance leads and product managers of subscription businesses get honest recurring-revenue numbers. Spreadsheet MRR usually goes wrong in the same places: an annual contract typed in as a monthly price inflates MRR twelvefold, downgrades get logged as upgrades, churn is counted on customers who already left, and one big account hides the fact that everyone else is leaving. You take a plain events export, rebuild MRR month by month so every movement is explained, and say what the numbers mean for the business.
You do not connect to billing systems, change data or give investment or accounting advice. Work only from the CSV the user provides, show every assumption, and remind them that MRR is a management metric, not recognized revenue.
## Files in this skill
- `scripts/mrr_metrics.py` - reads the events CSV and prints the MRR bridge, retention, cohorts, top customers and findings SM01-SM10; `--from/--to` to pick the reporting window, `--json` for further analysis, `--self-test` runs 17 checks (Python 3.8+ standard library only)
- `references/metric-definitions.md` - exact formulas for every metric and how the script classifies each event
- `references/data-prep-and-pitfalls.md` - how to export events from common billing tools, what counts as MRR, and the classic mistakes
- `templates/metrics-report.md` - the monthly metrics report you hand back
- `examples/subscription_events.csv` - ten months of events for a fictional scheduling SaaS with planted errors
## Workflow
### 1. Get the events
Ask for one row per subscription change: `date` (YYYY-MM-DD), `customer`, `event` (`new`, `upgrade`, `downgrade`, `price_change`, `churn`, `reactivate`), `amount` (the recurring price after the change, per billing period, excluding tax and one-off fees), `billing` (`monthly`, `quarterly`, `annual`) and optional `plan`. Use one currency (convert first). See `references/data-prep-and-pitfalls.md` for exports from Stripe, Paddle, Chargebee or a spreadsheet.
### 2. Run the calculator
```
python3 scripts/mrr_metrics.py examples/subscription_events.csv
python3 scripts/mrr_metrics.py events.csv --from 2026-07 --to 2026-10
python3 scripts/mrr_metrics.py events.csv --json --top-share 0.15 --churn-alert 0.03
python3 scripts/mrr_metrics.py --self-test
```
Pass `--from` to get period retention (NRR and logo retention of the customers active just before the window). Exit 0 = no high findings, 1 = high findings, 2 = bad input. Without a shell, build the bridge by hand with `references/metric-definitions.md`; it is just bookkeeping per customer.
### 3. Fix the data, then read the metrics
- Data findings first: SM01 unreadable row, SM02 event that doesn't fit the customer's state (churn of an unknown customer, `new` for an active one), SM03 zero-MRR new customer (free or trial), SM04 monthly amount 8x or more the median (an annual price typed as monthly?), SM05 `upgrade` that lowers MRR or `downgrade` that raises it, SM08 negative amount. Correct the CSV and re-run before drawing conclusions.
- Business findings: SM07 quick ratio below 1 (the business shrank that month, high), SM06 monthly logo churn above the alert level, SM09 period NRR below 100%, SM10 one customer above the concentration threshold.
### 4. Deliver
Fill `templates/metrics-report.md`: headline MRR and growth, the bridge table, retention and cohorts, what changed and why, data fixes made, and three concrete actions (for example: call the customers who downgraded, fix onboarding for the cohort that churns in month 2, diversify away from the largest account). Keep the language plain; define GRR and NRR once.
## Example result
On the sample file, ending MRR in October shows 6,435.40 across 14 paying customers, but 4,788 of it is A09, whose annual Business contract was typed as monthly (SM04); with `billing=annual` it is 399 MRR and the business is about a third of the size it appears. The data also has an "upgrade" from 399 to 149 (SM05), a churn row for unknown customer A99 and a `new` row for already-active A01 (SM02). Business signals: May, June and August lost more MRR than they gained (quick ratio 0.16, 0.75, 0.43), logo churn hit 22% in May and 25% in August, and July to October NRR is 92%. Result: 3 high, 8 medium, 1 low.
## Rules
- Never mix one-off charges, setup fees, usage overages or taxes into MRR unless the user explicitly defines MRR that way; say which definition you used.
- Divide annual and quarterly prices by 12 and 3; do not book the whole contract in the signing month.
- Fix data findings before interpreting trends, and state every correction you made.
- Small numbers lie: with fewer than ~20 customers, one churn moves rates a lot; say so instead of over-reading percentages.
- These are management metrics, not GAAP/IFRS revenue; refer accounting questions to an accountant.
FILE:references/metric-definitions.md
# Metric definitions (as implemented in mrr_metrics.py)
## Normalizing to MRR
`MRR = amount / months in billing period` (monthly 1, quarterly 3, annual 12). The amount is the recurring price after the change, net of recurring discounts, without tax or one-off fees.
## Classifying each event per customer
| Event | Condition | Bridge bucket |
|---|---|---|
| `new` | customer never paid before | New MRR (+ new customer) |
| `new` | customer already active | treated as price change, flagged SM02 |
| `new` / `reactivate` | customer paid before, now at 0 | Reactivation MRR |
| `upgrade` / `downgrade` / `price_change` | active customer | Expansion if MRR rises, Contraction if it falls (label mismatch flagged SM05) |
| change to 0 | active customer | counted as Churn, flagged SM05 |
| `churn` | active customer | Churn MRR = the customer's MRR before churning (+ churned customer) |
| any change / churn | no active subscription | ignored, flagged SM02 |
Bridge identity (checked every month): `start MRR + new + expansion + reactivation - contraction - churn = end MRR`.
## Monthly metrics
- **Net new MRR** = end MRR - start MRR
- **Logo churn rate** = churned customers / paying customers at start of month
- **MRR churn rate (gross)** = (churn + contraction) / start MRR
- **GRR (gross revenue retention)** = (start MRR - churn - contraction) / start MRR, never above 100%
- **NRR (net revenue retention)** = (start MRR - churn - contraction + expansion) / start MRR; reactivation and new are excluded
- **Quick ratio** = (new + expansion + reactivation) / (churn + contraction); above 4 is excellent, 1-2 means growth is fragile, below 1 means shrinking
- **ARPA** = end MRR / paying customers at end of month
## Period and cohort metrics
- **Period NRR** (`--from`): customers paying at the end of the month before the window; their MRR at the end of the window divided by their MRR at the start. New customers acquired in the window are excluded.
- **Logo retention**: share of that base still paying at the end of the window.
- **Cohort retention**: customers grouped by the month of their first paid event; share still paying after 1, 3, 6 and 12 months (reactivated customers count as paying again).
- **Concentration**: largest customer's share of current MRR (default alert above 20%).
## Rules of thumb (context, not targets)
Monthly logo churn for SMB SaaS is often 3-7%, for mid-market 1-2%; annual NRR above 100% means expansion outruns churn. Compare with your own history before comparing with benchmarks.
FILE:references/data-prep-and-pitfalls.md
# Data preparation and pitfalls
## Getting an events file
- **Stripe**: export subscriptions and subscription item changes (or invoice line items for `subscription` items); one row per change with the new recurring price. Use `plan.interval` (month/year) and `interval_count` for `billing`.
- **Paddle / Chargebee / Recurly**: subscription activity or "subscription changes" export; map created -> `new`, plan change -> `upgrade`/`downgrade`, cancelled (effective date, not request date) -> `churn`, reactivated -> `reactivate`.
- **Spreadsheet**: if you only have a monthly list of customers and prices, generate events by comparing each month to the previous one (appeared = new, price up = upgrade, price down = downgrade, disappeared = churn).
## What counts as MRR
- Include: recurring subscription fees after recurring discounts, per-seat fees at the current seat count, committed minimums.
- Exclude: setup and onboarding fees, one-off services, usage overages (track separately or average them explicitly), taxes, refunds and credits, free and trial plans.
- Paused subscriptions: treat as churn when paused and reactivation when resumed, or exclude consistently; say which.
- Use the **effective** date of a cancellation (end of paid period), not the date the customer clicked cancel; otherwise churn shows up a month early.
## Classic mistakes the script catches
1. Annual price typed as monthly (SM04): MRR jumps by the full contract value. Set `billing=annual`.
2. Wrong event labels (SM05): an "upgrade" to a cheaper plan. The script trusts the amounts, not the label, and flags the row.
3. Churn or changes for customers who are not active (SM02): usually duplicate exports or wrong customer ids.
4. Duplicate `new` rows for an active customer (SM02): often a plan change exported as a new subscription.
5. Negative amounts (SM08): refunds or credit notes in the subscription export. Remove them.
## Mistakes the script cannot catch
- Mixed currencies in one file (convert to one currency at a fixed rate first).
- Customer ids that change after a migration (merge them first, or churn + new will be overstated).
- Multiple subscriptions per customer: either sum them per customer before export, or use `customer-subscription` as the id and say that logo metrics count subscriptions.
FILE:templates/metrics-report.md
# Subscription metrics report: [Company], [reporting window]
**Ending MRR:** [x] ([+/- y%] vs previous month, [+/- z%] vs start of window) **ARR run-rate:** [MRR x 12]
**Paying customers:** [n] **ARPA:** [x]
**Data status:** [clean / corrected: list of fixes] (`mrr_metrics.py` output attached)
## MRR bridge
| Month | Start | New | Expansion | Contraction | Churn | Reactivation | End | Logo churn | NRR | Quick ratio |
|---|---|---|---|---|---|---|---|---|---|---|
| [YYYY-MM] | | | | | | | | | | |
## Retention
- Period NRR [x%], GRR trend [..], logo retention [x%] of the [n] customers active on [date]
- Cohorts: [which cohorts hold up, where the drop happens (month 1? month 3?)]
## What changed and why
1. [Biggest movement and its drivers, with customer names if useful]
2. [...]
## Risks
- Concentration: [largest customer share]
- [Churn spike / shrinking months / small-sample caveat]
## Data corrections made
| Row | Problem | Fix |
|---|---|---|
| [line 12, A09] | [annual typed as monthly] | [billing=annual] |
## Recommended actions (next 30 days)
1. [...]
2. [...]
3. [...]
_MRR here is a management metric (recurring subscription value), not recognized revenue._
FILE:examples/subscription_events.csv
date,customer,event,amount,billing,plan
2026-01-06,A01,new,49,monthly,Starter
2026-01-12,A02,new,149,monthly,Team
2026-01-19,A03,new,1430.40,annual,Team
2026-01-27,A04,new,49,monthly,Starter
2026-02-03,A05,new,149,monthly,Team
2026-02-11,A06,new,49,monthly,Starter
2026-02-20,A01,upgrade,149,monthly,Team
2026-03-02,A07,new,399,monthly,Business
2026-03-16,A08,new,49,monthly,Starter
2026-03-30,A04,churn,0,monthly,Starter
2026-04-07,A09,new,4788,monthly,Business
2026-04-15,A10,new,149,monthly,Team
2026-04-22,A06,upgrade,149,monthly,Team
2026-05-05,A11,new,49,monthly,Starter
2026-05-13,A02,downgrade,49,monthly,Starter
2026-05-21,A05,churn,0,monthly,Team
2026-05-28,A08,churn,0,monthly,Starter
2026-06-04,A12,new,149,monthly,Team
2026-06-10,A13,new,470.40,annual,Starter
2026-06-24,A07,upgrade,149,monthly,Team
2026-07-01,A14,new,49,monthly,Starter
2026-07-09,A04,reactivate,149,monthly,Team
2026-07-15,A15,new,0,monthly,Free
2026-07-23,A99,churn,0,monthly,Team
2026-08-04,A16,new,149,monthly,Team
2026-08-12,A10,churn,0,monthly,Team
2026-08-19,A11,churn,0,monthly,Starter
2026-08-26,A12,churn,0,monthly,Team
2026-09-02,A17,new,399,monthly,Business
2026-09-09,A18,new,49,monthly,Starter
2026-09-17,A14,upgrade,149,monthly,Team
2026-10-06,A19,new,149,monthly,Team
2026-10-14,A20,new,49,monthly,Starter
2026-10-20,A06,downgrade,49,monthly,Starter
2026-10-27,A01,new,149,monthly,Team
FILE:scripts/mrr_metrics.py
#!/usr/bin/env python3
"""SaaS MRR and churn metrics calculator (offline, Python 3.8+ standard library only).
Reads a subscription events CSV (date, customer, event, amount, billing, plan), normalizes
annual and quarterly amounts to MRR, builds a month-by-month MRR bridge (new, expansion,
contraction, churn, reactivation), computes logo churn, GRR, NRR, ARPA and quick ratio, and
flags data problems and business warning signs.
Usage:
mrr_metrics.py events.csv [--from 2026-01] [--to 2026-10] [--json] [--top-share 0.2]
mrr_metrics.py --self-test
Exit codes: 0 = no high findings, 1 = high findings, 2 = bad input.
"""
import argparse, csv, json, statistics, sys
from collections import Counter, OrderedDict
from datetime import date
EVENTS = {"new", "upgrade", "downgrade", "churn", "reactivate", "price_change"}
BILLING_MONTHS = {"monthly": 1, "quarterly": 3, "annual": 12, "yearly": 12}
SEV = {"high": 0, "medium": 1, "low": 2}
def month_key(s):
y, m = int(s[:4]), int(s[5:7])
if not (1 <= m <= 12):
raise ValueError("bad month in %r" % s)
return "%04d-%02d" % (y, m)
def next_month(k):
y, m = int(k[:4]), int(k[5:7])
return "%04d-%02d" % (y + (m == 12), 1 if m == 12 else m + 1)
def month_range(a, b):
out = [a]
while out[-1] < b:
out.append(next_month(out[-1]))
return out
def load(path):
with open(path, newline="", encoding="utf-8-sig") as fh:
rows = list(csv.DictReader(fh))
need = {"date", "customer", "event", "amount"}
if not rows or not need <= set(k.strip() for k in rows[0].keys()):
raise ValueError("CSV needs columns: date, customer, event, amount (optional: billing, plan)")
return [{k.strip(): (v or "").strip() for k, v in r.items()} for r in rows]
def compute(rows, start=None, end=None, top_share=0.2, churn_alert=0.05):
findings = []
def add(code, sev, msg, month=None, customer=None):
findings.append({"code": code, "severity": sev, "month": month, "customer": customer, "message": msg})
events = []
for i, r in enumerate(rows, start=2):
try:
d = date.fromisoformat(r["date"][:10])
except ValueError:
add("SM01", "high", "line %d: bad date %r" % (i, r["date"])); continue
ev = r["event"].lower()
if ev not in EVENTS:
add("SM01", "high", "line %d: unknown event %r (use %s)" % (i, r["event"], ", ".join(sorted(EVENTS)))); continue
billing = (r.get("billing") or "monthly").lower()
if billing not in BILLING_MONTHS:
add("SM01", "high", "line %d: unknown billing %r" % (i, billing)); continue
try:
amount = float(r["amount"] or 0)
except ValueError:
add("SM01", "high", "line %d: amount %r is not a number" % (i, r["amount"])); continue
if amount < 0:
add("SM08", "high", "line %d: negative amount %.2f (refunds and credits are not MRR)" % (i, amount), customer=r["customer"]); continue
events.append({"line": i, "date": d, "month": d.strftime("%Y-%m"), "customer": r["customer"], "event": ev,
"mrr": round(amount / BILLING_MONTHS[billing], 2), "billing": billing, "amount": amount, "plan": r.get("plan", "")})
if not events:
raise ValueError("no usable events")
events.sort(key=lambda e: (e["date"], e["line"]))
# SM04: monthly-billed amounts that look like annual totals typed as monthly
monthly_amounts = [e["mrr"] for e in events if e["billing"] == "monthly" and e["mrr"] > 0]
med = statistics.median(monthly_amounts) if monthly_amounts else 0
for e in events:
if med and e["billing"] == "monthly" and e["mrr"] >= 8 * med and e["mrr"] > 0:
add("SM04", "medium", "line %d: monthly amount %.2f is %.0fx the median monthly price: an annual contract entered as monthly? (set billing=annual)" % (e["line"], e["mrr"], e["mrr"] / med), e["month"], e["customer"])
first = start or events[0]["month"]
last = end or events[-1]["month"]
months = month_range(min(first, events[0]["month"]), last)
state = {} # customer -> current MRR (0 = churned)
ever = set()
bridge = OrderedDict()
by_month = {}
for e in events:
by_month.setdefault(e["month"], []).append(e)
cohort_start = {}
active_by_month = {}
snaps = {}
for m in months:
start_mrr = round(sum(state.values()), 2)
start_logos = sum(1 for v in state.values() if v > 0)
b = {"month": m, "start_mrr": start_mrr, "new": 0.0, "expansion": 0.0, "contraction": 0.0, "churn": 0.0, "reactivation": 0.0,
"start_customers": start_logos, "new_customers": 0, "churned_customers": 0, "reactivated_customers": 0}
for e in by_month.get(m, []):
c, ev, new = e["customer"], e["event"], e["mrr"]
cur = state.get(c)
if ev == "new":
if cur is not None and cur > 0:
add("SM02", "medium", "line %d: 'new' for a customer who is already active (%.2f MRR); treated as a price change" % (e["line"], cur), m, c)
ev = "price_change"
elif cur is not None and cur == 0 and c in ever:
add("SM02", "low", "line %d: 'new' for a returning customer; counted as reactivation" % e["line"], m, c)
ev = "reactivate"
if ev == "new":
state[c] = new; b["new"] += new
if new == 0:
add("SM03", "low", "line %d: new customer with 0 MRR (free plan or trial?) not counted in paid metrics" % e["line"], m, c)
else:
b["new_customers"] += 1; cohort_start[c] = m; ever.add(c)
elif ev == "churn":
if not cur:
add("SM02", "medium", "line %d: churn for a customer who is not active; ignored" % e["line"], m, c); continue
b["churn"] += cur; b["churned_customers"] += 1; state[c] = 0.0
elif ev == "reactivate":
if cur:
add("SM02", "medium", "line %d: reactivate for an active customer; treated as a price change" % e["line"], m, c)
ev = "price_change"
else:
if c not in ever:
add("SM02", "low", "line %d: reactivate for an unknown customer; counted as new" % e["line"], m, c)
b["new"] += new; b["new_customers"] += 1; ever.add(c); cohort_start[c] = m
else:
b["reactivation"] += new; b["reactivated_customers"] += 1
state[c] = new
if ev in ("upgrade", "downgrade", "price_change"):
if cur is None or cur == 0:
add("SM02", "medium", "line %d: %s for a customer with no active subscription; ignored" % (e["line"], e["event"]), m, c); continue
delta = round(new - cur, 2)
if e["event"] == "upgrade" and delta < 0:
add("SM05", "medium", "line %d: 'upgrade' lowers MRR %.2f -> %.2f; check the event type" % (e["line"], cur, new), m, c)
if e["event"] == "downgrade" and delta > 0:
add("SM05", "medium", "line %d: 'downgrade' raises MRR %.2f -> %.2f; check the event type" % (e["line"], cur, new), m, c)
if new == 0:
add("SM05", "medium", "line %d: change to 0 MRR; record it as churn instead" % e["line"], m, c)
b["churn"] += cur; b["churned_customers"] += 1; state[c] = 0.0; continue
if delta > 0: b["expansion"] += delta
else: b["contraction"] += -delta
state[c] = new
end_mrr = round(sum(state.values()), 2)
for k in ("new", "expansion", "contraction", "churn", "reactivation"):
b[k] = round(b[k], 2)
b["end_mrr"] = end_mrr
b["net_new_mrr"] = round(end_mrr - start_mrr, 2)
b["end_customers"] = sum(1 for v in state.values() if v > 0)
b["arpa"] = round(end_mrr / b["end_customers"], 2) if b["end_customers"] else 0.0
if start_mrr:
b["grr"] = round((start_mrr - b["churn"] - b["contraction"]) / start_mrr, 4)
b["nrr"] = round((start_mrr - b["churn"] - b["contraction"] + b["expansion"]) / start_mrr, 4)
b["mrr_churn_rate"] = round((b["churn"] + b["contraction"]) / start_mrr, 4)
else:
b["grr"] = b["nrr"] = b["mrr_churn_rate"] = None
b["logo_churn_rate"] = round(b["churned_customers"] / start_logos, 4) if start_logos else None
lost = b["churn"] + b["contraction"]
b["quick_ratio"] = round((b["new"] + b["expansion"] + b["reactivation"]) / lost, 2) if lost else None
assert abs(start_mrr + b["new"] + b["expansion"] + b["reactivation"] - b["contraction"] - b["churn"] - end_mrr) < 0.05, m
active_by_month[m] = {c for c, v in state.items() if v > 0}
snaps[m] = dict(state)
bridge[m] = b
report_months = [m for m in months if m >= first]
rows_out = [bridge[m] for m in report_months]
for b in rows_out:
m = b["month"]
if b["logo_churn_rate"] is not None and b["logo_churn_rate"] > churn_alert and b["start_customers"] >= 5:
add("SM06", "medium", "logo churn %.1f%% in %s (alert above %.1f%%)" % (100 * b["logo_churn_rate"], m, 100 * churn_alert), m)
if b["quick_ratio"] is not None and b["quick_ratio"] < 1:
add("SM07", "high", "quick ratio %.2f in %s: lost MRR exceeded gained MRR (shrinking)" % (b["quick_ratio"], m), m)
# trailing NRR over the reported period (cohort of customers active at period start)
base_month = report_months[0]
prev = None
for m in months:
if m == base_month: break
prev = m
period = {}
if prev and snaps[prev]:
base = {c: v for c, v in snaps[prev].items() if v > 0}
now = snaps[report_months[-1]]
start_sum = sum(base.values()); end_sum = sum(now.get(c, 0) for c in base)
kept = sum(1 for c in base if now.get(c, 0) > 0)
period = {"base_month": prev, "base_customers": len(base), "base_mrr": round(start_sum, 2),
"same_customers_mrr_now": round(end_sum, 2), "period_nrr": round(end_sum / start_sum, 4) if start_sum else None,
"logo_retention": round(kept / len(base), 4) if base else None}
if period["period_nrr"] is not None and period["period_nrr"] < 1:
add("SM09", "medium", "net revenue retention over %s..%s is %.1f%% (<100%%): existing customers shrink" % (report_months[0], report_months[-1], 100 * period["period_nrr"]))
final = {c: v for c, v in state.items() if v > 0}
total = sum(final.values())
if total:
top = sorted(final.items(), key=lambda x: -x[1])[:3]
if top[0][1] / total > top_share:
add("SM10", "medium", "largest customer %s is %.0f%% of MRR (threshold %.0f%%): concentration risk" % (top[0][0], 100 * top[0][1] / total, 100 * top_share), customer=top[0][0])
# cohorts: logo retention by start month
cohorts = OrderedDict()
for c, m in sorted(cohort_start.items(), key=lambda x: x[1]):
cohorts.setdefault(m, []).append(c)
cohort_table = []
for m, cs in cohorts.items():
row = {"cohort": m, "customers": len(cs)}
k = m
for i in range(0, 13):
if k > last: break
if i in (1, 3, 6, 12):
row["m%d" % i] = round(sum(1 for c in cs if c in active_by_month[k]) / len(cs), 2)
k = next_month(k)
cohort_table.append(row)
findings.sort(key=lambda f: (SEV[f["severity"]], f["code"], f["month"] or ""))
s = Counter(f["severity"] for f in findings)
return {"months": rows_out, "period": period, "cohorts": cohort_table,
"top_customers": [{"customer": c, "mrr": v, "share": round(v / total, 3)} for c, v in sorted(final.items(), key=lambda x: -x[1])[:5]] if total else [],
"findings": findings, "summary": {k: s.get(k, 0) for k in ("high", "medium", "low")}}
def pct(x):
return " - " if x is None else "%5.1f%%" % (100 * x)
def print_text(rep):
print("MRR bridge")
print("month start_mrr new expans contr churn react end_mrr cust logo_ch GRR NRR QR ARPA")
for b in rep["months"]:
print("%s %10.2f %8.2f %7.2f %6.2f %8.2f %6.2f %9.2f %5d %s %s %s %5s %7.2f" % (
b["month"], b["start_mrr"], b["new"], b["expansion"], b["contraction"], b["churn"], b["reactivation"],
b["end_mrr"], b["end_customers"], pct(b["logo_churn_rate"]), pct(b["grr"]), pct(b["nrr"]),
"-" if b["quick_ratio"] is None else "%.2f" % b["quick_ratio"], b["arpa"]))
p = rep["period"]
if p:
print("\nPeriod retention (customers active at end of %s: %d, %.2f MRR): NRR %s, logo retention %s" % (
p["base_month"], p["base_customers"], p["base_mrr"], pct(p["period_nrr"]), pct(p["logo_retention"])))
if rep["cohorts"]:
print("\nCohort logo retention (share still paying after N months)")
for c in rep["cohorts"]:
print(" %s n=%-3d m1=%s m3=%s m6=%s" % (c["cohort"], c["customers"], c.get("m1", "-"), c.get("m3", "-"), c.get("m6", "-")))
print("\nTop customers: " + ", ".join("%s %.2f (%.0f%%)" % (t["customer"], t["mrr"], 100 * t["share"]) for t in rep["top_customers"]))
print("\nFindings: %(high)d high, %(medium)d medium, %(low)d low" % rep["summary"])
for f in rep["findings"]:
print(" [%s] %s %s%s: %s" % (f["severity"].upper(), f["code"], f["month"] or "", (" " + f["customer"]) if f["customer"] else "", f["message"]))
def self_test():
ok = total = 0
def t(name, cond):
nonlocal ok, total
total += 1; ok += bool(cond)
print(("PASS " if cond else "FAIL ") + name)
def R(*lines):
return [dict(zip(["date", "customer", "event", "amount", "billing"], l.split(","))) for l in lines]
r = compute(R("2026-01-05,a,new,100,monthly", "2026-01-09,b,new,1200,annual", "2026-02-03,a,upgrade,150,monthly",
"2026-02-10,b,churn,0,annual", "2026-03-01,b,reactivate,50,monthly", "2026-03-15,c,new,200,monthly"))
m = {b["month"]: b for b in r["months"]}
t("annual normalized to MRR", m["2026-01"]["end_mrr"] == 200)
t("expansion counted", m["2026-02"]["expansion"] == 50)
t("churn counted at prior MRR", m["2026-02"]["churn"] == 100 and m["2026-02"]["churned_customers"] == 1)
t("reactivation separate from new", m["2026-03"]["reactivation"] == 50 and m["2026-03"]["new"] == 200)
t("bridge closes", all(abs(b["start_mrr"] + b["new"] + b["expansion"] + b["reactivation"] - b["contraction"] - b["churn"] - b["end_mrr"]) < 0.01 for b in r["months"]))
t("NRR feb = (200-100+50)/200", m["2026-02"]["nrr"] == 0.75 and m["2026-02"]["grr"] == 0.5)
t("SM07 quick ratio < 1 in feb", any(f["code"] == "SM07" and f["month"] == "2026-02" for f in r["findings"]))
r = compute(R("2026-01-01,a,new,50,monthly", "2026-01-01,b,new,50,monthly", "2026-01-01,c,new,55,monthly", "2026-01-02,d,new,1200,monthly"))
t("SM04 annual typed as monthly", any(f["code"] == "SM04" and f["customer"] == "d" for f in r["findings"]))
t("SM10 concentration", any(f["code"] == "SM10" for f in r["findings"]))
r = compute(R("2026-01-01,a,new,50,monthly", "2026-02-01,a,upgrade,40,monthly", "2026-02-02,z,churn,0,monthly", "2026-02-03,a,new,60,monthly"))
codes = Counter(f["code"] for f in r["findings"])
t("SM05 upgrade lowering MRR", codes["SM05"] == 1)
t("SM02 churn of unknown + new for active", codes["SM02"] == 2)
t("contraction from mislabeled upgrade", r["months"][1]["contraction"] == 10 and r["months"][1]["expansion"] == 20)
rows = R(*["2026-01-0%d,c%d,new,100,monthly" % (1 + i % 9, i) for i in range(20)] + ["2026-02-05,c%d,churn,0,monthly" % i for i in range(3)])
r = compute(rows)
t("SM06 logo churn alert (15%)", any(f["code"] == "SM06" for f in r["findings"]) and r["months"][1]["logo_churn_rate"] == 0.15)
r = compute(R("2026-01-01,a,new,100,monthly", "2026-01-01,b,new,100,monthly", "2026-02-01,a,downgrade,60,monthly", "2026-03-01,c,new,500,monthly"), start="2026-02")
t("period NRR for base cohort excludes new logos", r["period"]["period_nrr"] == 0.8 and r["period"]["base_customers"] == 2)
t("SM09 NRR < 100%", any(f["code"] == "SM09" for f in r["findings"]))
r = compute(R("2026-01-01,f,new,0,monthly", "2026-02-01,f,new,49,monthly"))
t("free-to-paid conversion counted as new", r["months"][1]["new"] == 49 and r["months"][1]["new_customers"] == 1 and r["months"][0]["new_customers"] == 0)
try:
compute(R("2026-13-01,a,new,1,monthly")); t("bad date reported", False)
except ValueError:
t("bad date -> no usable events error", True)
print("%d/%d passed" % (ok, total))
return 0 if ok == total else 1
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("file", nargs="?"); ap.add_argument("--from", dest="start"); ap.add_argument("--to", dest="end")
ap.add_argument("--json", action="store_true"); ap.add_argument("--top-share", type=float, default=0.2)
ap.add_argument("--churn-alert", type=float, default=0.05); ap.add_argument("--self-test", action="store_true")
a = ap.parse_args()
if a.self_test:
return self_test()
if not a.file:
ap.print_help(); return 2
try:
rep = compute(load(a.file), a.start and month_key(a.start), a.end and month_key(a.end), a.top_share, a.churn_alert)
except (OSError, ValueError) as e:
print("bad input: %s" % e, file=sys.stderr); return 2
if a.json:
print(json.dumps(rep, indent=2))
else:
print_text(rep)
return 1 if rep["summary"]["high"] else 0
if __name__ == "__main__":
sys.exit(main())
Checks electricity, gas and water meter readings for a home or small business: fixes typos, rollovers, over-estimates and duplicates, works out usage per period and month, prices it with your tariffs (standing charge, VAT, price changes, gas m3 to kWh), checks bills, flags leaks. Offline Python.
---
name: utility-meter-reading-and-bill-checker
description: Checks electricity, gas and water meter readings for a home or small business: fixes typos, rollovers, over-estimates and duplicates, works out usage per period and month, prices it with your tariffs (standing charge, VAT, price changes, gas m3 to kWh), checks bills, flags leaks. Offline Python.
---
# Utility Meter Reading and Bill Checker
You help households, landlords, flat-sharers and small shop or office owners understand their electricity, gas and water use and check that their bills match their meters. Bills go wrong in predictable ways: the supplier estimates a reading that is far too high, a price change is applied to the whole period, a reading is typed from memory with swapped digits, a five-digit meter wraps from 99999 to 00000, and a running toilet or dripping tap doubles the water use for months before anyone notices. You turn the user's own meter readings into clean usage figures, price them with their tariff and compare each bill with what the readings imply.
You work only from the readings, tariffs and bills the user gives you. You do not contact suppliers, log into accounts or give legal or financial advice. Show every assumption (meter digits, gas calorific value, which reading was dropped) and say when a figure is interpolated.
## Files in this skill
- `scripts/meter_check.py` - reads the readings CSV (plus optional tariffs and bills CSVs) and prints per-meter periods, monthly usage and cost, annual estimate, a bill check and findings UM01-UM11; `--digits meter=N` for rollover, `--spike`, `--tolerance`, `--calorific`, `--max-gap`, `--json`, `--self-test` runs 18 checks (Python 3.8+ standard library only)
- `references/reading-meters-and-units.md` - how to read common meters, units, rollover, estimated vs actual readings, gas m3 to kWh conversion
- `references/tariffs-and-bill-checks.md` - how to enter tariffs, how the bill check works, typical billing errors and what to ask the supplier
- `templates/usage-and-bill-report.md` - the report you hand back
- `examples/meter_readings.csv` - ten months of readings for a fictional flat with planted problems
- `examples/tariffs.csv` - electricity (with a July price rise), gas priced per kWh, water
- `examples/bills.csv` - five bills to check, one based on an over-estimate
## Workflow
### 1. Collect the readings
Ask for one row per reading: `date` (YYYY-MM-DD), `meter` (any name, e.g. `electricity`, `gas`, `water`, `ev-charger`), `reading` (the number on the display, without the red decimal digits on gas and water dials unless used consistently), optional `type` (`actual`, `customer`, `estimate`) and `note`. Readings from bills count: mark supplier estimates as `estimate`. Monthly readings on roughly the same day work best; photos of the meter are the best evidence. See `references/reading-meters-and-units.md`.
### 2. Collect tariffs and bills (optional but needed for costs)
- `tariffs.csv`: `meter,from,meter_unit,tariff_unit,unit_price,standing_per_day,vat_pct`, one row per price change. For gas measured in m3 but priced per kWh, set `meter_unit=m3`, `tariff_unit=kWh`.
- `bills.csv`: `meter,start,end,amount[,units]` with inclusive dates and the amount including tax.
- Ask how many digits the meter display has (before the decimal point) so rollovers are detected.
### 3. Run the checker
```
python3 scripts/meter_check.py examples/meter_readings.csv --tariffs examples/tariffs.csv --bills examples/bills.csv --digits electricity=5
python3 scripts/meter_check.py readings.csv --tariffs tariffs.csv --calorific 38.9 --json > usage.json
python3 scripts/meter_check.py readings.csv --spike 1.4 --tolerance 0.03
python3 scripts/meter_check.py --self-test
```
Exit 0 = no high findings, 1 = high findings, 2 = bad input. Without a shell, follow the formulas in `references/tariffs-and-bill-checks.md`: usage = later reading - earlier reading, prorate by days, cost = (units x unit price + days x standing charge) x (1 + VAT).
### 4. Fix the data first
- UM01 unreadable row, UM02 reading lower than the previous one (typo or wrong meter, ignored), UM06 two different readings on the same day (actual beats customer beats estimate), UM09 rollover assumed (check the digit count), UM07 long gap between readings (monthly numbers are interpolated).
- UM03 an estimate that was higher than the next actual reading: the estimate is dropped and the bills based on it overcharged.
- Ask the user to confirm each correction, then re-run.
### 5. Read usage and bills
- UM04 a period whose daily use is well above the recent periods; UM05 high use for the last two periods in a row: on water this is the classic leak signal (running toilet, dripping tap, outdoor pipe), on electricity a new appliance, heater or faulty fridge.
- UM10 a bill more than the tolerance above (high) or below (medium, a catch-up bill will follow) what readings and tariff imply; compare billed units with reading units to see whether the price or the reading is wrong.
- UM08 a bill that ends on an estimated reading; UM11 a bill that cannot be checked (outside the readings or no tariff).
- Seasonal patterns are normal for gas heating: compare with the same months last year if the user has them.
### 6. Deliver
Fill `templates/usage-and-bill-report.md`: usage and cost per meter, the bill check table, data corrections, problems found, money at stake, and next steps (submit an actual reading and ask for a re-bill, do a leak test, check the tariff). Use the user's currency and units; round money to 2 decimals.
## Example result
On the example files the checker uses 8 of 10 electricity readings: the 89650 typed from memory is ignored (UM02, high) and the April supplier estimate of 98650 is dropped because the May actual was 98500 (UM03). The 00050 in October is read as a rollover of the 5-digit display (UM09). The March-April electricity bill charged 760 kWh, 237.62, but the readings imply 305 kWh, 103.85: an overcharge of 133.77 (UM10 high, UM08). The June-July bill correctly splits the 1 July price rise. Gas has two different readings on 4 July; the actual 5085.5 is kept (UM06). Water jumped from about 0.35 to 0.66-0.68 m3/day in the last two months (UM04 twice, UM05 high): a probable leak costing about 0.67 per day (0.32 m3 x 2.10). Result: 3 high, 5 medium, 1 low.
## Rules
- Never invent readings; interpolate only between real readings and say so.
- Prefer actual readings over estimates; recommend submitting a reading before every bill date.
- Explain rollover and digit assumptions before correcting usage by a power of ten.
- Gas conversion uses volume correction 1.02264 and the calorific value from the user's bill (default 39.5 MJ/m3); say which value you used.
- Do not tell the user to stop paying a bill; suggest disputing it with the supplier with the evidence.
- Leak suspicion is a prompt to test (meter reading before and after a no-use period), not a diagnosis.
FILE:references/reading-meters-and-units.md
# Reading meters and units
## Reading the display
- **Digital electricity meters**: press the display button until the cumulative import register appears (often labelled `1.8.0`, `IMP` or `kWh`). Ignore instantaneous kW values. Day/night (two-rate) meters have two registers: use two meter names, e.g. `electricity-day` and `electricity-night`.
- **Smart meters**: readings are sent automatically, but bills can still be estimated when the communication fails; check the bill says "actual" or "smart".
- **Gas meters**: read the black digits from left to right; ignore the red digits after the decimal point (or include them every time, consistently). Older imperial meters show hundreds of cubic feet; multiply by 2.83 to get m3.
- **Water meters**: black digits are m3; red dials are litres. A slowly turning star or triangle with every tap closed is a leak indicator.
- **Dial meters**: read each dial; if a pointer is between two numbers, take the lower one.
## Digits and rollover
A meter with 5 digits before the decimal point goes from 99999 to 00000. If a reading is much lower than the previous one and the previous one was close to the maximum, it is a rollover, not a typo. Pass `--digits meter=5`. Without `--digits`, any backwards reading is treated as an error (UM02).
## Reading types
- `actual`: read by the supplier or a smart meter.
- `customer`: read and submitted by you (as good as actual if it comes with a photo).
- `estimate`: calculated by the supplier. Estimates are often based on last winter or on a national average and can be far off. When a later actual reading is lower, the estimate was too high and every bill since overcharged you (UM03).
## Units
| Meter | Display unit | Usually priced in |
|---|---|---|
| Electricity | kWh | kWh |
| Gas | m3 (or 100 ft3) | kWh (UK, EU) or m3 / therms (some countries) |
| Water | m3 | m3 (often water + wastewater charges) |
| Heat / district heating | kWh or MWh | kWh |
## Gas m3 to kWh
kWh = m3 x volume correction (1.02264) x calorific value (MJ/m3, printed on the bill, typically 37.5-43) / 3.6. The script uses `--calorific 39.5` by default. For therms: 1 therm = 29.3071 kWh.
## Good habits
Read all meters on the same day every month, photograph the display, and submit the reading to the supplier a day or two before the bill date.
FILE:references/tariffs-and-bill-checks.md
# Tariffs and bill checks
## Entering tariffs
One row per meter and price period in `tariffs.csv`:
| column | meaning |
|---|---|
| `meter` | same name as in the readings |
| `from` | first day the price applies (YYYY-MM-DD) |
| `meter_unit` / `tariff_unit` | e.g. `kWh`/`kWh`, `m3`/`kWh` for gas, `m3`/`m3` for water |
| `unit_price` | price per tariff unit, before tax |
| `standing_per_day` | fixed daily charge, before tax (0 if none; convert monthly fees: x12/365) |
| `vat_pct` | VAT or sales tax percent on the energy bill (e.g. 5, 20, 0) |
Water bills with separate fresh-water and wastewater prices: add them into one `unit_price`, or use two meter names with the same readings.
## How the bill check works
1. The bill covers `start` to `end` inclusive, so usage is measured from the reading at the start date to the reading at the day after `end`.
2. Readings on those exact days are rarely available, so the script interpolates linearly between the nearest real readings.
3. If a price change falls inside the bill, the period is split at the change date and each part is priced with its own tariff.
4. Expected cost = sum over parts of (units x unit price + days x standing charge) x (1 + VAT).
5. The bill is flagged (UM10) when it differs by more than `--tolerance` (default 5%). Above = possible overcharge (high), below = likely undercharge followed by a catch-up bill (medium).
Small differences (under 2-3%) usually come from interpolation, rounding, or a slightly different calorific value. Large differences almost always come from an estimated reading, a wrong price period, a missed price change, or the wrong meter number on the account.
## Typical billing errors
- Bill based on an estimate that was too high (UM03 + UM08 + UM10).
- New price applied to the whole period instead of from the change date.
- Wrong meter serial number on the account (your usage does not match the bill at all).
- Standing charge for days outside your tenancy (move-in/move-out bills).
- Two-rate meter registers swapped (night units priced as day units).
## What to send the supplier
The meter serial number, dated photos of the readings, the period and the difference you calculated, and a request to re-bill on actual readings. Keep it short and factual; ask for a corrected statement and a refund or credit of the overcharge.
## Leak test (water)
Close every tap and appliance, read the meter, wait 1-2 hours without using water, read again. Any movement means a leak; repeat with the toilet supply valve closed to see whether the toilet is the cause.
FILE:templates/usage-and-bill-report.md
# Utility usage and bill check: [address / business], [period]
**Meters:** [electricity, gas, water] **Readings used:** [n of m] **Currency:** [ ]
**Tool:** `meter_check.py` (output attached) **Assumptions:** [digits, calorific value, tolerance]
## Usage and cost
| Meter | Avg per day | Last month | Last month cost | Annual estimate | Annual cost (latest tariff) |
|---|---|---|---|---|---|
| [electricity] | [10.6 kWh] | | | | |
## Bill check
| Meter | Bill period | Billed | Expected from readings | Difference | Billed units vs readings | Verdict |
|---|---|---|---|---|---|---|
| [electricity] | [2026-03-06..2026-04-04] | [237.62] | [103.85] | [+128.8%] | [760 vs 305] | [overcharge, based on estimate] |
## Data corrections
| Meter | Reading | Problem | What we did |
|---|---|---|---|
| [electricity] | [89650 on 2026-07-04] | [lower than previous] | [ignored; please re-check the photo] |
## Problems found
1. [Overcharge / leak / spike, with the evidence and money at stake]
2. [...]
## Next steps
1. [Submit an actual reading with photo and ask the supplier to re-bill period X]
2. [Run the leak test in the water meter guide]
3. [Read all meters on the same day each month]
_Figures between readings are interpolated. This is a usage check, not legal or financial advice._
FILE:examples/meter_readings.csv
date,meter,reading,type,note
2026-01-05,electricity,97200,actual,moved in reading
2026-02-04,electricity,97560,actual,
2026-03-06,electricity,97890,actual,
2026-04-05,electricity,98650,estimate,supplier estimate
2026-05-05,electricity,98500,actual,photo of meter
2026-06-04,electricity,98800,actual,
2026-07-04,electricity,89650,actual,typed from memory
2026-08-03,electricity,99400,actual,
2026-09-02,electricity,99720,actual,
2026-10-02,electricity,00050,actual,display wrapped to 0
2026-01-05,gas,4520.0,actual,
2026-02-04,gas,4690.5,actual,
2026-03-06,gas,4840.2,actual,
2026-04-05,gas,4950.0,actual,
2026-05-05,gas,5020.3,actual,
2026-06-04,gas,5060.0,actual,
2026-07-04,gas,5085.5,actual,
2026-07-04,gas,5058.5,customer,second photo (digits swapped?)
2026-08-03,gas,5110.0,actual,
2026-09-02,gas,5134.2,actual,
2026-10-02,gas,5165.0,actual,
2026-01-05,water,812.4,actual,
2026-02-04,water,822.1,actual,
2026-03-06,water,831.6,actual,
2026-04-05,water,841.0,actual,
2026-05-05,water,851.2,actual,
2026-06-04,water,861.5,actual,
2026-07-04,water,872.4,actual,
2026-08-03,water,883.0,actual,
2026-09-02,water,902.8,actual,
2026-10-02,water,923.1,actual,toilet keeps running?
FILE:examples/tariffs.csv
meter,from,meter_unit,tariff_unit,unit_price,standing_per_day,vat_pct
electricity,2026-01-01,kWh,kWh,0.28,0.45,5
electricity,2026-07-01,kWh,kWh,0.31,0.45,5
gas,2026-01-01,m3,kWh,0.07,0.30,5
water,2026-01-01,m3,m3,2.10,0.15,0
FILE:examples/bills.csv
meter,start,end,amount,units
electricity,2026-01-05,2026-02-03,120.02,360
electricity,2026-03-06,2026-04-04,237.62,760
electricity,2026-06-04,2026-07-03,103.32,300
gas,2026-01-05,2026-03-05,283.10,
water,2026-07-04,2026-10-01,119.97,50.7
FILE:scripts/meter_check.py
#!/usr/bin/env python3
"""Utility meter reading and bill checker (offline, Python 3.8+ standard library only).
Reads household or small-business meter readings (electricity, gas, water or any cumulative
meter), cleans them (backwards readings, typos, meter rollover, over-estimates, conflicting
duplicates), works out usage per period and per calendar month, prices it with your tariffs
(unit price, daily standing charge, VAT/sales tax, price changes, gas m3 -> kWh) and checks
each bill against what the readings imply. Flags spikes and sustained rises (leaks).
Usage:
meter_check.py readings.csv [--tariffs tariffs.csv] [--bills bills.csv] [--digits electricity=5]
[--spike 1.6] [--tolerance 0.05] [--calorific 39.5] [--max-gap 70] [--json]
meter_check.py --self-test
readings.csv: date,meter,reading[,type][,note] type = actual (default) | estimate | customer
tariffs.csv: meter,from,meter_unit,tariff_unit,unit_price,standing_per_day,vat_pct
bills.csv: meter,start,end,amount[,units] start/end inclusive, amount incl. tax
Exit codes: 0 = no high findings, 1 = high findings, 2 = bad input.
"""
import argparse, csv, json, statistics, sys
from collections import OrderedDict
from datetime import date, timedelta
SEV = {"high": 0, "medium": 1, "low": 2}
VOLUME_CORRECTION = 1.02264
RANK = {"actual": 0, "customer": 1, "estimate": 2} # preferred reading on the same day
def d(s):
return date.fromisoformat(s.strip())
def num(s):
return float(str(s).replace(",", "").strip())
def read_csv(path):
with open(path, newline="", encoding="utf-8-sig") as f:
return [(i, {(k or "").strip().lower(): (v or "").strip() for k, v in r.items()}) for i, r in enumerate(csv.DictReader(f), 2)]
class Report:
def __init__(self):
self.findings = []
def add(self, code, sev, meter, msg, when=None):
self.findings.append({"code": code, "severity": sev, "meter": meter, "date": str(when) if when else "", "message": msg})
def clean_meter(meter, rows, rep, digits=None):
"""rows: list of (line, date, reading, type). Returns cleaned [(date, cumulative, type, raw)]."""
by_date = OrderedDict()
for line, dt, val, typ in sorted(rows, key=lambda r: (r[1], r[0])):
if dt in by_date:
prev = by_date[dt]
keep = prev if RANK[prev[3]] < RANK[typ] else (line, dt, val, typ)
if abs(prev[2] - val) > 1e-9:
rep.add("UM06", "medium", meter, "lines %d/%d: two readings on %s (%g and %g); kept %g (%s)" % (prev[0], line, dt, prev[2], val, keep[2], keep[3]), dt)
by_date[dt] = keep
continue
by_date[dt] = (line, dt, val, typ)
rows = list(by_date.values())
out, offset = [], 0.0
span = 10 ** digits if digits else None
for line, dt, val, typ in rows:
cum = val + offset
if out and cum < out[-1][1]:
last = out[-1]
if span and last[3] >= 0.9 * span and val <= 0.1 * span:
offset += span
cum = val + offset
rep.add("UM09", "low", meter, "line %d: %g after %g read as a %d-digit meter rollover" % (line, val, last[3], digits), dt)
elif last[2] == "estimate" and typ != "estimate":
rep.add("UM03", "medium", meter, "estimated reading %g on %s was higher than the actual %g on %s: bills based on it overcharged, a credit is due"
% (last[3], last[0], val, dt), last[0])
out.pop()
while out and cum < out[-1][1]:
out.pop()
else:
rep.add("UM02", "high", meter, "line %d: reading %g on %s is lower than %g on %s (typo, swapped digits or wrong meter?); ignored"
% (line, val, dt, last[3], last[0]), dt)
continue
out.append((dt, cum, typ, val))
return out
def at(series, when):
"""Cumulative reading at a date by linear interpolation, or None outside the readings."""
if not series or when < series[0][0] or when > series[-1][0]:
return None
for (d0, c0, *_), (d1, c1, *_) in zip(series, series[1:]):
if d0 <= when <= d1:
span = (d1 - d0).days
return c0 if span == 0 else c0 + (c1 - c0) * (when - d0).days / span
return series[-1][1]
def periods(meter, series, rep, spike=1.6, max_gap=70):
ps = []
for (d0, c0, t0, _), (d1, c1, t1, _) in zip(series, series[1:]):
days = (d1 - d0).days
ps.append({"start": str(d0), "end": str(d1), "days": days, "units": round(c1 - c0, 3),
"per_day": round((c1 - c0) / days, 3) if days else 0.0, "estimate": "estimate" in (t0, t1)})
if days > max_gap:
rep.add("UM07", "low", meter, "%d days between readings %s and %s: monthly figures are interpolated" % (days, d0, d1), d0)
flagged = []
for i, p in enumerate(ps):
hist = [q["per_day"] for q in ps[max(0, i - 3):i]]
if len(hist) >= 2:
base = statistics.median(hist)
if base > 0 and p["per_day"] > spike * base:
flagged.append(i)
rep.add("UM04", "medium", meter, "%s to %s: %.2f/day is %.1fx the recent %.2f/day" % (p["start"], p["end"], p["per_day"], p["per_day"] / base, base), p["start"])
if len(flagged) >= 2 and flagged[-1] == len(ps) - 1 and flagged[-2] == len(ps) - 2:
rep.add("UM05", "high", meter, "usage stayed high for the last two periods (since %s): check for a leak, a faulty appliance or a meter fault" % ps[-2]["start"], ps[-2]["start"])
return ps
def load_tariffs(path):
t = {}
for line, r in read_csv(path):
try:
t.setdefault(r["meter"].lower(), []).append({"from": d(r["from"]), "meter_unit": r.get("meter_unit", "") or "unit",
"tariff_unit": r.get("tariff_unit", "") or r.get("meter_unit", "") or "unit", "unit_price": num(r["unit_price"]),
"standing": num(r.get("standing_per_day") or 0), "vat": num(r.get("vat_pct") or 0) / 100})
except (KeyError, ValueError) as e:
raise ValueError("tariffs line %d: %s" % (line, e))
for v in t.values():
v.sort(key=lambda x: x["from"])
return t
def convert(units, tar, calorific):
if tar["meter_unit"].lower() in ("m3", "m³") and tar["tariff_unit"].lower() == "kwh":
return units * VOLUME_CORRECTION * calorific / 3.6
return units
def cost(series, tariffs, start, end_excl, calorific):
"""Units (in tariff unit) and cost incl. tax for [start, end_excl), split at tariff changes."""
if not tariffs:
return None
cuts = sorted({start, end_excl} | {t["from"] for t in tariffs if start < t["from"] < end_excl})
total_units = total_cost = 0.0
for a, b in zip(cuts, cuts[1:]):
ca, cb = at(series, a), at(series, b)
if ca is None or cb is None:
return None
act = [t for t in tariffs if t["from"] <= a]
if not act:
return None
tar = act[-1]
u = convert(cb - ca, tar, calorific)
total_units += u
total_cost += (u * tar["unit_price"] + (b - a).days * tar["standing"]) * (1 + tar["vat"])
return round(total_units, 2), round(total_cost, 2)
def month_starts(a, b):
m = date(a.year, a.month, 1)
while m <= b:
yield m
m = date(m.year + (m.month == 12), 1 if m.month == 12 else m.month + 1, 1)
def analyse(readings, tariffs=None, bills=None, digits=None, spike=1.6, tolerance=0.05, calorific=39.5, max_gap=70):
rep, digits, tariffs = Report(), digits or {}, tariffs or {}
raw = OrderedDict()
for line, r in readings:
try:
dt, meter, val = d(r["date"]), r["meter"].lower(), num(r["reading"])
typ = (r.get("type") or "actual").lower()
if val < 0 or typ not in ("actual", "estimate", "customer"):
raise ValueError("negative reading or unknown type %r" % typ)
except (KeyError, ValueError) as e:
rep.add("UM01", "medium", r.get("meter", "?"), "readings line %d unreadable: %s" % (line, e))
continue
raw.setdefault(meter, []).append((line, dt, val, typ))
out = OrderedDict()
for meter, rows in raw.items():
series = clean_meter(meter, rows, rep, digits.get(meter))
ps = periods(meter, series, rep, spike, max_gap)
tar = tariffs.get(meter, [])
months = []
if len(series) >= 2:
for m in month_starts(series[0][0], series[-1][0]):
nxt = date(m.year + (m.month == 12), 1 if m.month == 12 else m.month + 1, 1)
a, b = at(series, m), at(series, nxt)
if a is None or b is None:
continue
c = cost(series, tar, m, nxt, calorific)
months.append({"month": m.strftime("%Y-%m"), "units": round(b - a, 2), "billed_units": c[0] if c else None, "cost": c[1] if c else None})
span = (series[-1][0] - series[0][0]).days if len(series) >= 2 else 0
per_day = (series[-1][1] - series[0][1]) / span if span else 0
yr = None
if span and tar:
t = tar[-1]
yu = convert(per_day * 365, t, calorific)
yr = round((yu * t["unit_price"] + 365 * t["standing"]) * (1 + t["vat"]), 2)
out[meter] = {"readings_used": len(series), "periods": ps, "months": months, "avg_per_day": round(per_day, 3),
"annual_units": round(per_day * 365, 1), "annual_cost_at_latest_tariff": yr,
"unit": (tar[-1]["meter_unit"] if tar else "units"), "_series": series}
checks = []
for line, b in bills or []:
try:
meter, s, e, amt = b["meter"].lower(), d(b["start"]), d(b["end"]), num(b["amount"])
except (KeyError, ValueError) as ex:
rep.add("UM01", "medium", b.get("meter", "?"), "bills line %d unreadable: %s" % (line, ex)); continue
series = out.get(meter, {}).get("_series", [])
c = cost(series, tariffs.get(meter, []), s, e + timedelta(days=1), calorific)
row = {"meter": meter, "start": str(s), "end": str(e), "billed": amt, "expected": c[1] if c else None,
"expected_units": c[0] if c else None, "billed_units": num(b["units"]) if b.get("units") else None, "diff_pct": None}
checks.append(row)
if c is None:
rep.add("UM11", "low", meter, "bill %s to %s cannot be checked: outside the readings or no tariff" % (s, e), s); continue
row["diff_pct"] = round((amt - c[1]) / c[1] * 100, 1) if c[1] else None
est = [x for x in series if s <= x[0] <= e + timedelta(days=1) and x[2] == "estimate"]
dropped = [f for f in rep.findings if f["code"] == "UM03" and f["meter"] == meter and s <= d(f["date"]) <= e + timedelta(days=1)]
if est or dropped:
rep.add("UM08", "medium", meter, "bill %s to %s ends on or spans an estimated reading: ask for a bill on an actual reading" % (s, e), s)
if row["diff_pct"] is not None and abs(row["diff_pct"]) > tolerance * 100:
over = row["diff_pct"] > 0
rep.add("UM10", "high" if over else "medium", meter, "bill %s to %s is %.2f, readings and tariff imply %.2f (%+.1f%%)%s"
% (s, e, amt, c[1], row["diff_pct"], "" if over else ": undercharged now, expect a catch-up bill"), s)
for m in out.values():
m.pop("_series")
fs = sorted(rep.findings, key=lambda f: (SEV[f["severity"]], f["code"], f["meter"], f["date"]))
return {"meters": out, "bills": checks, "findings": fs,
"counts": {k: sum(f["severity"] == k for f in fs) for k in SEV}}
def print_text(r):
for meter, m in r["meters"].items():
print("== %s (%s): %d readings used, %.3f %s/day, ~%.0f %s/year%s" % (meter, m["unit"], m["readings_used"], m["avg_per_day"], m["unit"],
m["annual_units"], m["unit"], (", ~%.2f/year at latest tariff" % m["annual_cost_at_latest_tariff"]) if m["annual_cost_at_latest_tariff"] else ""))
print(" period days units per_day")
for p in m["periods"]:
print(" %s .. %s %4d %9.2f %9.3f%s" % (p["start"], p["end"], p["days"], p["units"], p["per_day"], " (estimate)" if p["estimate"] else ""))
if m["months"]:
print(" month units priced_units cost")
for x in m["months"]:
print(" %s %8.2f %14s %9s" % (x["month"], x["units"], "-" if x["billed_units"] is None else "%.2f" % x["billed_units"],
"-" if x["cost"] is None else "%.2f" % x["cost"]))
if r["bills"]:
print("\nBill check")
for b in r["bills"]:
units = ""
if b["billed_units"] is not None and b["expected_units"] is not None:
units = " units billed %g vs readings %g" % (b["billed_units"], b["expected_units"])
print(" %-12s %s..%s billed %8.2f expected %8s %7s%s" % (b["meter"], b["start"], b["end"], b["billed"],
"-" if b["expected"] is None else "%.2f" % b["expected"], "-" if b["diff_pct"] is None else "%+.1f%%" % b["diff_pct"], units))
c = r["counts"]
print("\nFindings: %d high, %d medium, %d low" % (c["high"], c["medium"], c["low"]))
for f in r["findings"]:
print(" [%s] %s %s %s: %s" % (f["severity"].upper(), f["code"], f["meter"], f["date"], f["message"]))
def self_test():
tests = []
def t(name, cond):
tests.append((name, bool(cond)))
def R(*rows):
return [(i + 2, dict(zip(("date", "meter", "reading", "type"), r))) for i, r in enumerate(rows)]
def codes(res):
return [f["code"] for f in res["findings"]]
a = analyse(R(("2026-01-01", "e", "100"), ("2026-01-11", "e", "200"), ("2026-01-21", "e", "300")))
t("per-day usage", a["meters"]["e"]["periods"][0]["per_day"] == 10 and a["meters"]["e"]["avg_per_day"] == 10)
t("interpolation", abs(at([(date(2026, 1, 1), 0, "actual", 0), (date(2026, 1, 11), 100, "actual", 100)], date(2026, 1, 6)) - 50) < 1e-9)
b = analyse(R(("2026-01-01", "e", "99900"), ("2026-01-11", "e", "50")), digits={"e": 5})
t("rollover with --digits", b["meters"]["e"]["periods"][0]["units"] == 150 and "UM09" in codes(b))
c = analyse(R(("2026-01-01", "e", "1000"), ("2026-01-11", "e", "1100"), ("2026-01-21", "e", "110"), ("2026-01-31", "e", "1300")))
t("backwards reading ignored", "UM02" in codes(c) and c["meters"]["e"]["readings_used"] == 3)
e = analyse(R(("2026-01-01", "e", "1000"), ("2026-02-01", "e", "1500", "estimate"), ("2026-03-01", "e", "1400")))
t("over-estimate dropped", "UM03" in codes(e) and e["meters"]["e"]["readings_used"] == 2)
f = analyse(R(("2026-01-01", "w", "10"), ("2026-01-01", "w", "11"), ("2026-01-11", "w", "20")))
t("conflicting duplicate", "UM06" in codes(f))
f2 = analyse(R(("2026-01-01", "w", "10"), ("2026-01-11", "w", "20", "actual"), ("2026-01-11", "w", "02", "customer")))
t("duplicate keeps actual over customer", f2["meters"]["w"]["periods"][0]["units"] == 10)
g = analyse(R(*[("2026-%02d-01" % m, "w", str(v)) for m, v in zip(range(1, 8), (0, 10, 20, 30, 40, 60, 80))]))
t("spike + sustained rise -> leak", codes(g).count("UM04") == 2 and "UM05" in codes(g))
tar = {"e": [{"from": date(2026, 1, 1), "meter_unit": "kWh", "tariff_unit": "kWh", "unit_price": 0.2, "standing": 0.5, "vat": 0.1}]}
s = [(date(2026, 1, 1), 0, "actual", 0), (date(2026, 1, 11), 100, "actual", 100)]
t("cost = (units*price + days*standing)*(1+vat)", cost(s, tar["e"], date(2026, 1, 1), date(2026, 1, 11), 39.5) == (100, 27.5))
tar2 = [{"from": date(2026, 1, 1), "meter_unit": "kWh", "tariff_unit": "kWh", "unit_price": 0.2, "standing": 0, "vat": 0},
{"from": date(2026, 1, 6), "meter_unit": "kWh", "tariff_unit": "kWh", "unit_price": 0.4, "standing": 0, "vat": 0}]
t("price change split by date", cost(s, tar2, date(2026, 1, 1), date(2026, 1, 11), 39.5)[1] == 30.0)
gas = [{"from": date(2026, 1, 1), "meter_unit": "m3", "tariff_unit": "kWh", "unit_price": 1, "standing": 0, "vat": 0}]
t("gas m3 -> kWh", abs(cost(s, gas, date(2026, 1, 1), date(2026, 1, 11), 39.5)[0] - 100 * 1.02264 * 39.5 / 3.6) < 0.01)
bills = [(2, {"meter": "e", "start": "2026-01-01", "end": "2026-01-10", "amount": "27.50"}), (3, {"meter": "e", "start": "2026-01-01", "end": "2026-01-10", "amount": "40"}),
(4, {"meter": "e", "start": "2025-12-01", "end": "2025-12-31", "amount": "9"})]
h = analyse(R(("2026-01-01", "e", "0"), ("2026-01-11", "e", "100")), tariffs=tar, bills=bills)
t("matching bill passes", h["bills"][0]["diff_pct"] == 0.0)
t("overcharge flagged high", any(x["code"] == "UM10" and x["severity"] == "high" for x in h["findings"]))
t("bill outside readings -> UM11", "UM11" in codes(h))
i = analyse(R(("2026-01-01", "e", "0"), ("2026-01-31", "e", "100", "estimate")), tariffs=tar, bills=[(2, {"meter": "e", "start": "2026-01-01", "end": "2026-01-30", "amount": "1"})])
t("bill on estimate -> UM08", "UM08" in codes(i))
j = analyse(R(("2026-01-01", "e", "0"), ("2026-03-01", "e", "590")))
t("monthly split by interpolation", [m["units"] for m in j["meters"]["e"]["months"]] == [310.0, 280.0])
k = analyse(R(("2026-13-01", "e", "1"), ("2026-01-01", "e", "-5")))
t("bad rows -> UM01", codes(k).count("UM01") == 2)
l = analyse(R(("2026-01-01", "e", "0"), ("2026-04-01", "e", "90")))
t("long gap -> UM07", "UM07" in codes(l))
ok = 0
for name, c in tests:
print("%s %s" % ("PASS" if c else "FAIL", name)); ok += c
print("%d/%d passed" % (ok, len(tests)))
return 0 if ok == len(tests) else 1
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("readings", nargs="?"); ap.add_argument("--tariffs"); ap.add_argument("--bills")
ap.add_argument("--digits", action="append", default=[], help="meter=N, e.g. electricity=5")
ap.add_argument("--spike", type=float, default=1.6); ap.add_argument("--tolerance", type=float, default=0.05)
ap.add_argument("--calorific", type=float, default=39.5); ap.add_argument("--max-gap", type=int, default=70)
ap.add_argument("--json", action="store_true"); ap.add_argument("--self-test", action="store_true")
a = ap.parse_args()
if a.self_test:
return self_test()
if not a.readings:
ap.print_usage(); return 2
try:
digits = {k.strip().lower(): int(v) for k, v in (x.split("=", 1) for x in a.digits)}
readings = read_csv(a.readings)
tariffs = load_tariffs(a.tariffs) if a.tariffs else {}
bills = read_csv(a.bills) if a.bills else []
except (OSError, ValueError) as e:
print("error: %s" % e, file=sys.stderr); return 2
if not readings:
print("error: no readings", file=sys.stderr); return 2
r = analyse(readings, tariffs, bills, digits, a.spike, a.tolerance, a.calorific, a.max_gap)
print(json.dumps(r, indent=2)) if a.json else print_text(r)
return 1 if r["counts"]["high"] else 0
if __name__ == "__main__":
sys.exit(main())
Scans plain or JSON-lines logs for data that must never be logged: Luhn-valid cards, IBANs, SSNs, API keys, JWTs, auth headers, passwords, URL tokens, emails, phones, birth dates and public IPs. Masks every value, ranks the loggers to fix and writes a redacted copy. Offline stdlib Python.
---
name: log-pii-and-secret-redaction-checker
description: Scans plain or JSON-lines logs for data that must never be logged: Luhn-valid cards, IBANs, SSNs, API keys, JWTs, auth headers, passwords, URL tokens, emails, phones, birth dates and public IPs. Masks every value, ranks the loggers to fix and writes a redacted copy. Offline stdlib Python.
---
# Log PII and Secret Redaction Checker
You help backend developers, SREs and security or privacy reviewers find personal data and credentials that leaked into application logs, and fix the code that writes them. Logs are copied to log platforms, tickets, chat and laptops, are kept for months and are read by many people, so a card number or bearer token in a debug line is a real incident (PCI DSS, GDPR, leaked credentials). You scan a log sample, explain what was found and where it comes from, and give concrete fixes at the logger, not just a cleaned file.
You work only on the log text the user provides, on their machine. You never print a found secret or personal value in full, never try a found credential, and never upload logs anywhere. Treat any real secret you find as compromised: tell the user to rotate it.
## Files in this skill
- `scripts/log_redaction_check.py` - scans one or more log files (plain text or JSON lines) and prints findings LR01-LR10 grouped by type with masked examples, a ranking of the loggers/components that leak, and DEBUG-level hits; `--redact OUT` writes a redacted copy, `--allow REGEX` skips known safe values, `--json` for tooling, `--self-test` runs 24 checks (Python 3.8+ standard library only)
- `references/detection-rules.md` - every detector, its pattern and validation (Luhn, IBAN mod-97, private IP ranges) and its known false positives
- `references/fixing-leaks-at-the-source.md` - how to stop the leak in code: structured logging, field allowlists, redaction filters for common stacks, and incident steps
- `templates/redaction-report.md` - the report you hand back
- `examples/app.log` - a 25-line mixed text/JSON log from a fictional shop with planted leaks and decoys
## Workflow
### 1. Get a representative sample
Ask for a log excerpt from each service or level that matters (include DEBUG if it is ever enabled in production), ideally a few thousand lines. If the user cannot share logs, have them run the script locally and paste only the report: the report masks values. Ask which values are known safe (system sender addresses, test cards in a sandbox) so you can pass them with `--allow`.
### 2. Run the checker
```
python3 scripts/log_redaction_check.py examples/app.log
python3 scripts/log_redaction_check.py api.log worker.log --allow '^noreply@example\.com$'
python3 scripts/log_redaction_check.py app.log --redact app.redacted.log --json > findings.json
python3 scripts/log_redaction_check.py --self-test
```
Exit 0 = no high findings, 1 = high findings, 2 = bad input. Use `--include-private-ip` when internal addresses also count (for example, office or VPN IPs that identify employees). Without a shell, apply `references/detection-rules.md` by hand: search for the patterns and validate cards with Luhn before reporting them.
### 3. Triage the findings
- High (fix now, usually an incident): LR02 card number, LR03 API key, token, JWT, private key or `Authorization` header value, LR04 password or secret value, LR07 IBAN, LR08 SSN, LR09 token in a URL query string (also leaks through access logs, proxies and the `Referer` header).
- Medium (personal data, needs a lawful reason and retention limit): LR01 email, LR06 phone, LR10 date of birth.
- Low (context dependent): LR05 public IP address, personal data under GDPR when it identifies a user.
- Check every hit before reporting it: decoys such as order numbers, version strings and timestamps can pass a pattern. Mark false positives and re-run with `--allow`.
- Use the "Sources to fix first" table: one logger often causes most of the hits. DEBUG-level hits mean request or payload dumping.
### 4. Fix at the source, then clean up
Follow `references/fixing-leaks-at-the-source.md`: stop logging whole request bodies and headers, log ids instead of values, add a redaction filter as a safety net, and move secrets out of URLs. For high findings, rotate the exposed credentials, purge or restrict the affected logs in every sink (log platform, backups, tickets), and check whether the incident must be reported (PCI DSS, GDPR breach assessment). `--redact` output is for sharing a sample safely, not a substitute for fixing the code.
### 5. Deliver
Fill `templates/redaction-report.md`: scope, counts by severity, a table per finding type with masked examples and source, root causes per logger, code fixes, credentials to rotate, log clean-up steps and a regression check (run the script in CI on a test log, fail on exit 1).
## Example result
On `examples/app.log` the checker finds 20 hits in 25 lines: 11 high, 7 medium, 2 low.
- High: a full test card number in a DEBUG charge request, a bearer token in dumped headers, a plaintext password from a failed login, a Stripe-style secret key in an error message, an AWS access key in a deploy line, a JWT, an OAuth `code`, a download `token` and an upstream `api_key` in URLs, a checksum-valid IBAN and an SSN.
- Medium: 4 emails (3 distinct), 2 phone numbers and a date of birth in a CRM update.
- Correctly ignored decoys: the masked `4111********1111` card, `cvc=[REDACTED]`, `password=***`, the IBAN-like reference with a bad checksum, the parcel tracking number, the duration in ms, the private `10.0.3.14` and `build 1.2.3.4`.
- With `--allow '^noreply@'` the system sender address drops out (19 hits). `auth` is the leakiest logger (5 hits) and 3 hits sit in DEBUG lines.
## Asking the right questions
- Where do these logs go (log platform, files, third-party APM) and how long are they kept?
- Is DEBUG ever enabled in production, even temporarily?
- Which values are test data (sandbox cards, example.com addresses) and may be allowlisted?
- Who owns each logger in the "Sources to fix first" table?
## Rules
- Never echo a full secret or personal value; quote masked values and line numbers only.
- Any real credential found in logs is compromised: recommend rotation, not just deletion.
- Report patterns, not proof: say that a clean scan does not prove a log is clean (free-text names and addresses are not detected).
- Prefer fixes in code (do not log it) over regex filters; filters are a second line of defense.
- Do not give legal advice; point to the user's DPO or security team for breach-notification decisions.
FILE:references/detection-rules.md
# Detection rules
Each match is checked in this order; a later detector never reports text already claimed by an earlier one. Values that look already masked (`***`, `xxx`, `[REDACTED]`, `<redacted>`, `[FILTERED]`, `null`, empty) are skipped.
| Code | Kind | Severity | Pattern (simplified) | Extra validation |
|---|---|---|---|---|
| LR03 | PRIVATE_KEY | high | `-----BEGIN ... PRIVATE KEY-----` | none |
| LR09 | URL_TOKEN | high | `?` or `&` + `access_token`, `refresh_token`, `id_token`, `token`, `api_key`, `apikey`, `sig`, `signature`, `password`, `session`, `sid`, `code` = value | value must not be masked |
| LR03 | AUTH_HEADER | high | `Authorization`, `X-Api-Key`, `api_key`, `X-Auth-Token` `:`/`=` optional `Bearer`/`Basic`/`Token` + 12+ token chars | none |
| LR03 | JWT | high | `eyJ...` three base64url segments | none |
| LR03 | AWS_KEY | high | `AKIA`/`ASIA` + 16 upper-case letters or digits | none |
| LR03 | API_KEY | high | `sk_`/`rk_`/`pk_` + `live_`/`test_` + 16+ chars, GitHub `ghp_`/`gho_`/`ghu_`/`ghs_`/`ghr_`, Slack `xoxa-`/`xoxb-`/`xoxp-`/`xoxr-` | none |
| LR04 | PASSWORD | high | `password`, `passwd`, `pwd`, `secret`, `client_secret`, `passphrase` + `:`/`=` + value (text or JSON) | value must not be masked |
| LR10 | DOB | medium | `dob`, `date_of_birth`, `birthdate`, `birth_date`, `birthday` + a date | none |
| LR07 | IBAN | high | 2 letters, 2 digits, 11-30 more alphanumerics, spaces allowed every 4 | ISO 13616 mod-97 checksum = 1 |
| LR02 | CARD | high | 13-19 digits, optional single spaces or dashes, not part of a decimal, time or longer number | Luhn checksum, not all the same digit |
| LR08 | SSN | high | `NNN-NN-NNNN` | not 000, 666 or 9xx area; group not 00; serial not 0000 |
| LR01 | EMAIL | medium | `local@domain.tld` | none |
| LR06 | PHONE | medium | `+` country code then 2-4 digit groups | 10-15 digits in total |
| LR05 | IP | low | dotted IPv4 | octets 0-255; private, loopback, link-local and multicast skipped unless `--include-private-ip`; skipped right after `version`, `build`, `release`, `tag`, `v` |
## Known false positives
- **Cards**: about 1 in 10 random 13-19 digit numbers pass Luhn. Order ids, millisecond timestamps and tracking numbers can match; confirm with the field name. Separators inside numbers are only single spaces or dashes.
- **Phones**: only numbers written with a leading `+` are detected, which avoids most ids but misses national formats like `(415) 555-0132` or `0171 2345678`.
- **SSN**: any `123-45-6789` shape, for example some part numbers.
- **IPs**: version strings in other forms (`lib 2.10.0.1`), public IPs of your own servers.
- **URL tokens**: `code=` can be a harmless product code; OAuth authorization codes are short-lived but still must not be logged.
## Known misses
Names, postal addresses, free-text messages, national ids other than US SSN, non-`+` phone numbers, secrets without a known prefix or key name, base64-encoded payloads, and data split across lines. Combine the scan with a code review of what each log call writes.
FILE:references/fixing-leaks-at-the-source.md
# Fixing leaks at the source
## Principles
1. **Log ids, not values.** `user_id=8812` instead of the email; `card_last4=1111` plus a payment id instead of the card; `order_id` instead of the address.
2. **Allowlist fields.** Log a fixed set of fields per event instead of whole objects, request bodies or header maps. Dumping `request.headers` is the most common source of LR03 bearer tokens.
3. **Keep secrets out of URLs.** Send tokens in headers or POST bodies; URLs end up in access logs, proxy logs, browser history and `Referer` headers. For signed download links, log the path without the query string.
4. **Redaction filter as a safety net.** Add one central filter that masks known sensitive keys (`password`, `secret`, `token`, `authorization`, `card*`, `iban`, `ssn`, `dob`) and the patterns in `detection-rules.md`, before logs leave the process.
5. **DEBUG is not a free pass.** Debug logs get turned on in production during incidents. Apply the same rules.
## Stack hints
- **Python logging / structlog**: a `logging.Filter` or structlog processor that rewrites `record.msg`/event dict keys; never `logger.debug("req %s", request.__dict__)`.
- **Node (pino / winston)**: pino `redact: ["req.headers.authorization", "*.password", "card.number"]`; winston custom format.
- **Java (Logback / Log4j2)**: a masking `PatternLayout` converter or `RewritePolicy`; avoid `toString()` of entities with personal fields (exclude them from Lombok `@ToString`).
- **Go (slog / zap)**: implement `LogValuer` on sensitive types to return a masked value.
- **Nginx / load balancers**: custom `log_format` that uses `$uri` instead of `$request_uri` (drops the query string) where tokens appear.
- **Log platform**: add ingest-time masking rules too (Datadog sensitive data scanner, Elastic ingest pipeline, Loki/Vector `redact` transforms).
## When a high finding is real (incident steps)
1. Rotate the exposed credential (API key, token, password) and check its use since the first log line.
2. Fix and deploy the logging code.
3. Purge or restrict the affected logs in every sink: log platform, archives and backups, exported files, tickets and chat pastes.
4. Card data: logging full card numbers is a PCI DSS violation; tell the payments owner.
5. Personal data: record the incident and let the privacy owner decide on breach notification (GDPR art. 33 has a 72-hour clock once a reportable breach is known).
## Regression check
Keep a test log produced by the test suite and run `log_redaction_check.py test.log` in CI; fail the build on exit code 1. Add the known safe values with `--allow` so the check stays quiet until a real leak appears.
FILE:templates/redaction-report.md
# Log redaction review: [service / log source], [date range of sample]
**Scope:** [files, services, levels, number of lines] **Tool:** `log_redaction_check.py` (output attached, values masked)
**Result:** [n] findings - [h] high, [m] medium, [l] low **Verdict:** [incident / fix soon / clean sample]
## High findings (act now)
| Code | Type | Hits | Masked example | Line(s) | Source |
|---|---|---|---|---|---|
| [LR03] | [bearer token in headers] | [1] | [to****Nw] | [3] | [auth] |
## Medium and low findings
| Code | Type | Hits | Masked example | Line(s) | Source |
|---|---|---|---|---|---|
| [LR01] | [email] | | | | |
## False positives excluded
- [value pattern, why it is safe, `--allow` used]
## Root causes by logger
1. [logger] - [what it writes, e.g. dumps full request headers at DEBUG]
2. [...]
## Fixes
| Where | Change | Owner |
|---|---|---|
| [auth middleware] | [log user_id only, drop header dump] | [ ] |
## Credentials to rotate
- [key type, where it was found, first seen]
## Log clean-up
- [sinks to purge or restrict, retention changes]
## Regression check
- [CI step running the checker on a test log, fail on exit 1]
_A clean scan does not prove a log is clean: names, addresses and free text are not detected._
FILE:examples/app.log
2026-11-18T09:00:01Z INFO [web] GET /health 200 4ms from 10.0.3.14
2026-11-18T09:00:07Z INFO [auth] login ok user_id=8812 email=ana.lima@example.com ip=203.0.113.24
2026-11-18T09:00:09Z DEBUG [auth] request headers {"Authorization": "Bearer tok_9fA2kLm33QpR7xYz81Nw", "Accept": "application/json"}
2026-11-18T09:00:12Z INFO [auth] login failed user=bob.k@example.org password=Summer2026! attempts=2
2026-11-18T09:00:15Z INFO [auth] password reset requested user_id=4410 password=*** (masked)
2026-11-18T09:01:02Z INFO [checkout] order 100244 created total=59.90 items=3
2026-11-18T09:01:03Z DEBUG [payments] charge request card_number=4111 1111 1111 1111 exp=12/28 cvc=[REDACTED]
2026-11-18T09:01:03Z INFO [payments] charge ok card=4111********1111 amount=59.90
2026-11-18T09:01:04Z ERROR [payments] gateway timeout, retrying with key sk_test_51HxT9mQ2eZvKyL0AbCdEf
2026-11-18T09:01:30Z INFO [web] GET /oauth/callback?code=4/0AY0e-g7Qx2mN8&state=af31 302 from 198.51.100.77
2026-11-18T09:02:11Z INFO [web] GET /download/report.pdf?token=a8f3e2c19d7b44&expires=1763456531 200
2026-11-18T09:02:40Z INFO [crm] contact updated name="Ana Lima" phone=+44 20 7946 0958 dob=1990-04-12
2026-11-18T09:03:05Z WARN [payouts] payout to IBAN GB82 WEST 1234 5698 7654 32 delayed
2026-11-18T09:03:06Z INFO [payouts] payout batch 77 ref=DE00370400440532013000 ok
2026-11-18T09:03:59Z INFO [kyc] verification started ssn=123-45-6789 status=pending
2026-11-18T09:04:10Z INFO [deploy] s3 sync using AKIAIOSFODNN7EXAMPLE region=eu-west-1
2026-11-18T09:04:11Z INFO [deploy] build 1.2.3.4 tagged, artifact 8f2e91 uploaded
{"ts":"2026-11-18T09:05:00Z","level":"info","logger":"orders","msg":"order shipped","order_id":100244,"customer_email":"ana.lima@example.com","tracking":"1Z999AA10123456784"}
{"ts":"2026-11-18T09:05:02Z","level":"debug","logger":"orders","msg":"upstream call","url":"https://api.example.net/v2/rates?api_key=k_live_77QhR2sT9w","status":200}
{"ts":"2026-11-18T09:05:09Z","level":"info","logger":"notify","msg":"sms sent","to":"+1 415 555 0132","template":"shipped"}
{"ts":"2026-11-18T09:05:15Z","level":"info","logger":"notify","msg":"email sent","to":"noreply@example.com","template":"digest"}
{"ts":"2026-11-18T09:06:00Z","level":"error","logger":"session","msg":"token expired","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI4ODEyIiwiZXhwIjoxNzYzNDU2NTMxfQ.Qm9ndXNTaWduYXR1cmVGb3JEZW1v"}
{"ts":"2026-11-18T09:06:20Z","level":"info","logger":"session","msg":"session refreshed","user_id":8812,"password":"[REDACTED]"}
2026-11-18T09:07:00Z INFO [web] GET /api/users/8812 200 12ms from 10.0.3.14
2026-11-18T09:07:45Z INFO [jobs] nightly export finished rows=18233 duration_ms=1731995000123
FILE:scripts/log_redaction_check.py
#!/usr/bin/env python3
"""Log PII and secret redaction checker (offline, Python 3.8+ standard library only).
Scans plain-text or JSON-lines logs for personal data and secrets that should never be logged:
payment cards (Luhn-checked), IBANs (mod-97 checked), US SSNs, API keys and tokens (AWS, Stripe,
GitHub, Slack, JWT, Authorization headers), passwords in key=value or JSON, tokens in URL query
strings, emails, phone numbers, dates of birth and public IP addresses. Values that are already
masked ("***", "[REDACTED]") are ignored. Every value is masked in the report; nothing is sent
anywhere. Optionally writes a redacted copy of the log.
Usage:
log_redaction_check.py app.log [more.log ...] [--json] [--redact OUT] [--allow REGEX ...]
[--include-private-ip] [--max-examples 3]
log_redaction_check.py --self-test
Exit codes: 0 = no high findings, 1 = high findings, 2 = bad input.
"""
import argparse, json, re, sys
from collections import Counter, OrderedDict, defaultdict
SEV = {"high": 0, "medium": 1, "low": 2}
REDACTED = re.compile(r"^(\*+.*|x{3,}|\[?redacted(:\w+)?\]?|<redacted>|\[filtered\]|null|none|\"\"|-|)$", re.I)
# code, kind, severity, compiled regex, value group, validator (or None)
def luhn(s):
d = [int(c) for c in s if c.isdigit()]
if not 13 <= len(d) <= 19 or len(set(d)) == 1:
return False
tot = 0
for i, n in enumerate(reversed(d)):
if i % 2:
n *= 2
n -= 9 if n > 9 else 0
tot += n
return tot % 10 == 0
def iban_ok(s):
s = s.replace(" ", "")
if not 15 <= len(s) <= 34:
return False
r = s[4:] + s[:4]
num = "".join(str(int(c, 36)) for c in r)
return int(num) % 97 == 1
def phone_ok(s):
return 10 <= sum(c.isdigit() for c in s) <= 15
def public_ip(s, include_private=False):
parts = s.split(".")
if len(parts) != 4 or any(not p.isdigit() or int(p) > 255 or (len(p) > 1 and p[0] == "0") for p in parts):
return False
a, b = int(parts[0]), int(parts[1])
private = a in (10, 127, 0) or (a == 172 and 16 <= b <= 31) or (a == 192 and b == 168) or (a == 169 and b == 254) or a >= 224
return include_private or not private
DETECTORS = [
("LR03", "PRIVATE_KEY", "high", re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"), 0, None),
("LR09", "URL_TOKEN", "high", re.compile(r"(?i)[?&](?:access_token|refresh_token|id_token|token|api_key|apikey|sig|signature|password|session|sid|code)=([^&\s\"']+)"), 1, None),
("LR03", "AUTH_HEADER", "high", re.compile(r"(?i)\b(?:authorization|x-api-key|api[_-]?key|x-auth-token)\b\"?\s*[:=]\s*\"?(?:bearer\s+|basic\s+|token\s+)?([A-Za-z0-9._~+/=-]{12,})"), 1, None),
("LR03", "JWT", "high", re.compile(r"\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}"), 0, None),
("LR03", "AWS_KEY", "high", re.compile(r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b"), 0, None),
("LR03", "API_KEY", "high", re.compile(r"\b(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{16,}\b|\bgh[pousr]_[A-Za-z0-9]{30,}\b|\bxox[abpr]-[A-Za-z0-9-]{10,}"), 0, None),
("LR04", "PASSWORD", "high", re.compile(r"(?i)\b(?:password|passwd|pwd|secret|client_secret|passphrase)\b\"?\s*[:=]\s*\"?([^\s\",&}]*)"), 1, None),
("LR10", "DOB", "medium", re.compile(r"(?i)\b(?:dob|date_of_birth|birth_?date|birthday)\b\"?\s*[:=]\s*\"?(\d{4}-\d{2}-\d{2}|\d{1,2}[/.]\d{1,2}[/.]\d{2,4})"), 1, None),
("LR07", "IBAN", "high", re.compile(r"\b[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,4})?\b"), 0, iban_ok),
("LR02", "CARD", "high", re.compile(r"(?<![\d.:-])\d(?:[ -]?\d){12,18}(?![\d.:])"), 0, luhn),
("LR08", "SSN", "high", re.compile(r"\b(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}\b"), 0, None),
("LR01", "EMAIL", "medium", re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"), 0, None),
("LR06", "PHONE", "medium", re.compile(r"(?<![\w+])\+\d{1,3}[ .-]?\(?\d{1,4}\)?(?:[ .-]?\d{2,4}){2,4}\b"), 0, phone_ok),
("LR05", "IP", "low", re.compile(r"(?<![\d.])(?:\d{1,3}\.){3}\d{1,3}(?![\d.])"), 0, "ip"),
]
TITLES = {"LR01": "email address", "LR02": "payment card number (Luhn valid)", "LR03": "API key, token or credential",
"LR04": "password or secret value", "LR05": "public IP address", "LR06": "phone number",
"LR07": "IBAN (checksum valid)", "LR08": "US Social Security number", "LR09": "token in URL query string",
"LR10": "date of birth"}
VERSION_CTX = re.compile(r"(?i)(version|build|release|ver|tag|\bv)[\s:=]*$")
LEVEL_RE = re.compile(r"\b(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\b\s*[:\-]?\s*\[?([A-Za-z][\w.\-]*)\]?")
def mask(kind, v):
if kind == "CARD":
d = re.sub(r"\D", "", v)
return d[:1] + "*" * (len(d) - 5) + d[-4:]
if kind == "EMAIL":
user, _, dom = v.partition("@")
return user[:1] + "***@" + dom
if kind == "PRIVATE_KEY":
return v
if len(v) <= 6:
return "*" * len(v)
return v[:2] + "*" * min(len(v) - 4, 12) + v[-2:]
def source_of(line):
s = line.strip()
if s.startswith("{"):
try:
o = json.loads(s)
for k in ("logger", "service", "component", "module", "name"):
if isinstance(o.get(k), str):
return o[k], str(o.get("level", "")).upper()
except ValueError:
pass
m = LEVEL_RE.search(line)
if m:
return m.group(2), m.group(1).upper()
return "-", ""
def scan_line(line, allow=(), include_private_ip=False):
taken, hits = [], []
for code, kind, sev, rx, grp, val in DETECTORS:
for m in rx.finditer(line):
v = m.group(grp)
a, b = m.span(grp)
if v is None or REDACTED.match(v.strip()) or any(x < b and a < y for x, y in taken):
continue
if val == "ip":
if not public_ip(v, include_private_ip) or VERSION_CTX.search(line[max(0, a - 12):a]):
continue
elif val and not val(v):
continue
if any(r.search(v) for r in allow):
continue
taken.append((a, b))
hits.append({"code": code, "kind": kind, "severity": sev, "start": a, "end": b, "value": v})
return sorted(hits, key=lambda h: h["start"])
def redact_line(line, hits):
out = line
for h in sorted(hits, key=lambda h: -h["start"]):
out = out[:h["start"]] + "[REDACTED:%s]" % h["kind"] + out[h["end"]:]
return out
def scan(named_lines, allow=(), include_private_ip=False, max_examples=3):
findings, by_code, sources, redacted = [], OrderedDict(), defaultdict(Counter), {}
distinct = defaultdict(set)
debug_hits = 0
total_lines = 0
for name, lines in named_lines:
red = []
for i, line in enumerate(lines, 1):
total_lines += 1
hits = scan_line(line, allow, include_private_ip)
red.append(redact_line(line, hits))
if not hits:
continue
src, level = source_of(line)
if level in ("DEBUG", "TRACE"):
debug_hits += len(hits)
for h in hits:
c = h["code"]
by_code.setdefault(c, {"code": c, "severity": h["severity"], "what": TITLES[c], "count": 0, "lines": [], "examples": []})
e = by_code[c]
e["count"] += 1
distinct[c].add(h["value"])
if len(e["lines"]) < 20:
e["lines"].append("%s:%d" % (name, i))
if len(e["examples"]) < max_examples:
e["examples"].append({"where": "%s:%d" % (name, i), "kind": h["kind"], "masked": mask(h["kind"], h["value"]), "source": src})
sources[src][c] += 1
findings.append({"file": name, "line": i, "code": c, "kind": h["kind"], "severity": h["severity"], "source": src, "level": level, "masked": mask(h["kind"], h["value"])})
redacted[name] = red
for c, e in by_code.items():
e["distinct_values"] = len(distinct[c])
summary = sorted(by_code.values(), key=lambda e: (SEV[e["severity"]], e["code"]))
counts = Counter(f["severity"] for f in findings)
src_rank = sorted(((s, sum(c.values()), dict(c)) for s, c in sources.items()), key=lambda x: -x[1])
return {"lines_scanned": total_lines, "findings_total": len(findings), "by_severity": {k: counts.get(k, 0) for k in SEV},
"summary": summary, "sources": [{"source": s, "hits": n, "codes": c} for s, n, c in src_rank],
"debug_level_hits": debug_hits, "findings": findings, "redacted": redacted}
def print_text(rep):
print("Scanned %d lines: %d findings (%d high, %d medium, %d low)" % (rep["lines_scanned"], rep["findings_total"],
rep["by_severity"]["high"], rep["by_severity"]["medium"], rep["by_severity"]["low"]))
if not rep["summary"]:
print("No personal data or secrets found by the detectors (this is not proof the log is clean).")
return
print("\nBy type")
for e in rep["summary"]:
print(" [%s] %s %-34s %3d hits, %2d distinct lines %s" % (e["severity"].upper(), e["code"], e["what"], e["count"],
e["distinct_values"], ", ".join(x.split(":")[-1] for x in e["lines"][:8]) + (" ..." if e["count"] > 8 else "")))
for x in e["examples"]:
print(" %s %-11s %s (source %s)" % (x["where"], x["kind"], x["masked"], x["source"]))
print("\nSources to fix first (logger / component)")
for s in rep["sources"]:
print(" %-22s %3d %s" % (s["source"], s["hits"], " ".join("%s=%d" % kv for kv in sorted(s["codes"].items()))))
if rep["debug_level_hits"]:
print("\nNote: %d hits are in DEBUG/TRACE lines; check that debug logging is off in production." % rep["debug_level_hits"])
def self_test():
ok = 0; tests = []
def t(name, cond):
tests.append((name, bool(cond)))
k = lambda line, **kw: [h["kind"] for h in scan_line(line, **kw)]
t("Luhn-valid card found", k("charge card=4111 1111 1111 1111 ok") == ["CARD"])
t("Luhn-invalid number ignored", k("order 4111111111111112 shipped") == [])
t("epoch ms timestamp not a card", k("ts=1731995000123 done") == [])
t("IBAN mod-97 valid", k("payout to DE89 3704 0044 0532 0130 00") == ["IBAN"])
t("IBAN bad checksum ignored", k("ref DE00370400440532013000") == [])
t("AWS access key id", k("using AKIAIOSFODNN7EXAMPLE for s3") == ["AWS_KEY"])
t("bearer header value", k("Authorization: Bearer abcDEF1234567890xyz") == ["AUTH_HEADER"])
t("JWT", k("token eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.c2lnbmF0dXJlLXZhbHVl") == ["JWT"])
t("password in JSON", k('{"user":"bo","password":"hunter2!"}') == ["PASSWORD"])
t("masked password ignored", k("password=*** user=bo") == [] and k('"password": "[REDACTED]"') == [])
t("token in URL query", k("GET /cb?code=abc123&state=x") == ["URL_TOKEN"])
t("email", k("signup ana.lima@example.com") == ["EMAIL"])
t("phone needs + and 10-15 digits", k("call +44 20 7946 0958") == ["PHONE"] and k("build +1 2") == [])
t("SSN", k("ssn 123-45-6789") == ["SSN"])
t("private IP ignored, public flagged", k("from 10.0.0.5") == [] and k("from 203.0.113.7") == ["IP"])
t("include private IP flag", k("from 10.0.0.5", include_private_ip=True) == ["IP"])
t("version string not an IP", k("lib 1.2.3.4.5 loaded") == [])
t("IP after 'build'/'version' ignored", k("build 1.2.3.4 tagged") == [] and k("version=8.8.8.8") == [])
t("api_key in URL is a URL token", k("GET /r?api_key=abcdef123456789") == ["URL_TOKEN"])
t("allowlist", k("mail noreply@example.com", allow=[re.compile(r"^noreply@")]) == [])
t("date of birth", k("dob=1990-04-12 ok") == ["DOB"])
rep = scan([("t.log", ["INFO [auth] login ana@example.com", "DEBUG [pay] card 5500 0000 0000 0004", "INFO [web] fine"])])
t("report counts + severity", rep["findings_total"] == 2 and rep["by_severity"]["high"] == 1 and rep["debug_level_hits"] == 1)
t("redaction keeps key, hides value", rep["redacted"]["t.log"][1] == "DEBUG [pay] card [REDACTED:CARD]")
t("masked output never shows full card", "5500000000000004" not in json.dumps(rep["findings"]))
for name, c in tests:
print("%s %s" % ("PASS" if c else "FAIL", name)); ok += c
print("%d/%d passed" % (ok, len(tests)))
return 0 if ok == len(tests) else 1
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("files", nargs="*"); ap.add_argument("--json", action="store_true")
ap.add_argument("--redact", metavar="OUT"); ap.add_argument("--allow", action="append", default=[])
ap.add_argument("--include-private-ip", action="store_true"); ap.add_argument("--max-examples", type=int, default=3)
ap.add_argument("--self-test", action="store_true")
a = ap.parse_args()
if a.self_test:
return self_test()
if not a.files:
ap.print_usage(); return 2
try:
allow = [re.compile(x) for x in a.allow]
named = []
for f in a.files:
fh = sys.stdin if f == "-" else open(f, encoding="utf-8", errors="replace")
named.append((f, fh.read().splitlines()))
except (OSError, re.error) as e:
print("error: %s" % e, file=sys.stderr); return 2
rep = scan(named, allow, a.include_private_ip, a.max_examples)
if a.redact:
with open(a.redact, "w", encoding="utf-8") as out:
for f in a.files:
out.write("\n".join(rep["redacted"][f]) + "\n")
red = rep.pop("redacted")
if a.json:
print(json.dumps(rep, indent=2))
else:
print_text(rep)
if a.redact:
print("\nRedacted copy written to %s (%d lines)" % (a.redact, sum(len(v) for v in red.values())))
return 1 if rep["by_severity"]["high"] else 0
if __name__ == "__main__":
sys.exit(main())

A detailed realism prompt for an unedited, straight-out-of-camera phone selfie: a curly-haired man in his twenties reclining on a sofa in a lived-in room, with visible pores, warm household light and none of the plastic, over-retouched AI look.
Candid phone snapshot, shot with front phone camera, real phone photo of an attractive adult man in his early-to-mid 20s, an unedited camera photo, SOOC. He has thick, voluminous dark brown curly hair with naturally defined loose ringlets, styled in a slightly messy shape with several prominent curls falling across his forehead. His hair has natural volume, subtle shine, and individual flyaway strands. He has light olive skin, blue-grey eyes, thick dark eyebrows, a straight nose with a small silver nose stud, naturally full pink lips, and a defined jawline. He has a neatly trimmed dark mustache and a distinctive short, fuller goatee concentrated around his chin. Small sparkling silver diamond-style stud earrings are visible in both ears. He is wearing a relaxed white cotton graphic T-shirt with oversized green and purple printed lettering across the front. The shirt has a slightly loose fit, natural fabric creases, and a simple rounded neckline. He is reclining comfortably against a sofa or bed, holding his iPhone slightly above his face to take a close-up front-facing selfie. His head rests naturally against the surface behind him, shoulders relaxed. He looks directly into the camera with a subtle, confident half-smile and a calm, relaxed expression. His face is the main focal point. The framing is vertical 9:16 portrait, close-up selfie from the upper chest upward, shot with front phone camera at a slightly elevated angle. His face is centered, with his voluminous curly hairstyle occupying much of the upper frame and his white graphic T-shirt visible across the lower foreground. Candid phone shot, spontaneous and slightly imperfect, like a casual selfie taken while relaxing at home. The environment is a cozy, ordinary bedroom or living space. Behind him is a light cream-colored wall with a subtle textured painted finish. A large black flat-screen television is partially visible on the upper-left side of the background. On the right side, a dark black backpack or soft travel bag hangs casually near the wall, with realistic fabric folds and straps visible. A small section of light beige furniture or bedding is visible behind his shoulders. Lighting is warm, soft indoor household lighting, creating natural illumination across his face with gentle shadows beneath his curls, nose, and chin. His skin has natural highlights, and the white shirt retains visible cotton texture without appearing overexposed. Real photo look, IG phone snapshot: visible pores, natural skin-tone variation, under-eye texture, subtle freckles and facial imperfections, individual defined hair curls, natural catchlights in the eyes, accurate facial proportions, natural mustache and goatee texture, visible cotton weave. Natural colors, authentic handheld perspective. Avoid the look of modern high-definition digital fashion photography. No plastic skin, no excessive smoothing, no beauty retouching, no sharp digital look, no cinematic lighting, no dramatic color grading, no filters, no distorted anatomy, no extra fingers, no watermarks or added text overlays. Strictly vertical 9:16 portrait orientation.
A one-take action video prompt: the camera dives from a monumental cliff city onto a speeder racing a narrow ledge road, weaves through falling debris, laundry lines and open windows, then bursts out into a calm reveal of a misty waterfall valley with a rainbow. Works on any text-to-video model.
Speeder chase across a cliff city (single continuous shot) From a monumental cliffside city carved into stone, the camera dives toward a tiny streak of light ripping along a narrow ledge-road. Lock-on: a speeder hugging the wall at insane speed. The camera slingshots ahead, whips back, then drops tight to the rear thrusters: heat haze, grit snapping off the ledge, warning lights flashing. A collapsing balcony rains debris; the rider snaps a last-inch swerve under a falling arch, then threads through hanging laundry lines and open windows in one fluid line. The camera darts through the same openings, staying glued to the motion. One final bend and sudden calm: the camera blasts outward into a reveal of the city opening onto a boundless waterfall-fed valley, mist turning into rainbow.
Opinionated rules for building web forms that are accessible, validate clearly and never lose what the user typed.
# Web Forms taste of a frontend developer who wants forms that never lose user input - Start with a real `<form>` element and a submit button so Enter, autofill and assistive tech work out of the box. Confidence: 0.95 - Give every input a visible `<label>`; placeholders are hints, never labels. Confidence: 0.95 - Use the right input `type`, `inputmode` and `autocomplete` values so mobile keyboards and password managers help the user. Confidence: 0.90 - Validate on the server always; client-side validation is only for faster feedback. Confidence: 0.95 - Share one schema, such as Zod, between client and server so rules never drift apart. Confidence: 0.80 - Show errors after the user leaves a field or submits, not on every keystroke while they are still typing. Confidence: 0.85 - Put the error message next to the field, link it with `aria-describedby`, and move focus to the first invalid field on submit. Confidence: 0.90 - Write error messages that say how to fix the problem, not just that something is wrong. Confidence: 0.90 - Never clear the form after a failed submit; keep every value the user typed. Confidence: 0.95 - Disable double submits by showing a pending state on the button, but keep the button focusable. Confidence: 0.85 - Prefer uncontrolled inputs with a form library like React Hook Form for large forms to avoid re-rendering on every keystroke. Confidence: 0.75 - Ask only for data you actually need; every extra field lowers completion. Confidence: 0.85 - Mark optional fields as optional instead of marking every required field with an asterisk. Confidence: 0.65 - Do not split one value like a phone number or date into several inputs unless there is a strong reason. Confidence: 0.80 - Accept flexible input formats (spaces in card numbers, dashes in phone numbers) and normalize them in code. Confidence: 0.85 - Save drafts of long or multi-step forms locally so a refresh does not wipe the user's work. Confidence: 0.75 - Make the success state clear and say what happens next. Confidence: 0.80 - Protect forms with CSRF tokens and rate limits, and never rely on hidden fields for security. Confidence: 0.90 - Test forms with keyboard only and a screen reader before calling them done. Confidence: 0.85
Opinionated AWS CDK conventions for infrastructure as code that stays safe to review, deploy and roll back.
# AWS CDK Infrastructure taste of a platform engineer who wants boring, reviewable cloud changes - Write CDK apps in TypeScript so constructs, props and IDE hints are fully typed. Confidence: 0.85 - Use L2 constructs by default, drop to L1 `Cfn*` resources only for missing features, and wrap them in your own construct. Confidence: 0.85 - Split the app into small stacks by lifecycle: stateful resources like databases and buckets live apart from stateless compute. Confidence: 0.90 - Set `removalPolicy: RETAIN` and deletion protection on every stateful resource in production. Confidence: 0.95 - Never hardcode account IDs or regions; pass them in through an environment config per stage. Confidence: 0.90 - Model environments as stages of the same app, so dev, staging and prod are built from identical code. Confidence: 0.85 - Run `cdk diff` in every pull request and post the output for review before anything deploys. Confidence: 0.90 - Deploy only from CI with CDK Pipelines or a similar pipeline, never with `cdk deploy` from a laptop to production. Confidence: 0.90 - Grant permissions with construct methods like `bucket.grantRead(fn)` instead of writing IAM policy JSON by hand. Confidence: 0.90 - Add `cdk-nag` with the AWS Solutions rules and justify every suppression in code. Confidence: 0.80 - Keep logical IDs stable; renaming a construct ID replaces the resource, so treat it as a migration. Confidence: 0.90 - Store secrets in Secrets Manager or SSM Parameter Store and reference them, never put secret values in context or props. Confidence: 0.95 - Tag every resource with owner, service and cost center through `Tags.of(app)`. Confidence: 0.80 - Write snapshot tests for stacks and fine-grained assertions for security-critical properties. Confidence: 0.75 - Pin the `aws-cdk-lib` version and upgrade it on purpose, reading the changelog for feature flags. Confidence: 0.80 - Avoid cross-stack exports for things that change often; pass values through SSM parameters instead. Confidence: 0.70 - Use `NodejsFunction` or bundled assets so Lambda code is built by the same pipeline as the infrastructure. Confidence: 0.75 - Enable logs, alarms and dashboards in the same construct that creates the resource. Confidence: 0.80 - Keep `cdk.context.json` committed so lookups are deterministic across machines. Confidence: 0.85
Opinionated Go concurrency conventions for services where every goroutine has an owner, a context and a clean way to stop.
# Go Concurrency taste of a backend engineer who wants goroutines that never leak - Start a goroutine only when you know exactly how and when it will stop. Confidence: 0.95 - Pass a `context.Context` as the first argument to every function that blocks or does I/O, and honor its cancellation. Confidence: 0.95 - Use `errgroup.WithContext` to run related goroutines together so the first error cancels the rest. Confidence: 0.90 - Prefer plain synchronous APIs and let the caller add concurrency, instead of spawning goroutines inside library code. Confidence: 0.85 - Only the sender closes a channel, and only when no other sender can still write to it. Confidence: 0.90 - Use unbuffered channels by default; add a buffer only with a measured reason and a comment explaining its size. Confidence: 0.75 - Protect shared state with a `sync.Mutex` when it is simpler than a channel; channels are for passing ownership, not for every lock. Confidence: 0.85 - Keep the mutex right next to the fields it guards and never copy a struct that contains one. Confidence: 0.85 - Bound fan-out with a worker pool or `errgroup.SetLimit` instead of one goroutine per item. Confidence: 0.85 - Every `select` that waits on work also waits on `ctx.Done()`. Confidence: 0.90 - Use `time.NewTimer` or `context.WithTimeout` instead of `time.After` inside loops. Confidence: 0.75 - Use `sync.Once` or `sync.OnceValue` for lazy initialization, never a hand-rolled boolean flag. Confidence: 0.80 - Reach for `sync/atomic` types only for simple counters and flags, not for complex invariants. Confidence: 0.75 - Avoid `sync.Map` unless profiling shows lock contention on a read-heavy map. Confidence: 0.70 - Recover panics at goroutine boundaries in long-running servers and turn them into logged errors. Confidence: 0.75 - Run all tests with `-race` in CI, and treat any data race report as a release blocker. Confidence: 0.95 - Check for leaked goroutines in tests with `goleak`. Confidence: 0.70 - Make shutdown graceful: stop accepting work, drain in-flight jobs, then exit with a deadline. Confidence: 0.90 - Do not use `context.Value` for optional parameters; it is only for request-scoped data like trace IDs. Confidence: 0.85 - Document in the function comment whether a type is safe for concurrent use. Confidence: 0.80

Photoreal sunrise telephoto photograph of nine wild horses, mostly black and dark brown with three greys, galloping through shallow surf on an empty beach, with backlit spray, a low sun at the left and wet-sand reflections. Step 1 of the Wild Horses Galloping Through Shallow Surf at Dawn workflow.
Photorealistic wide wildlife photograph at sunrise of a herd of nine wild horses galloping through shallow surf along an empty, wide sandy beach. Eight horses run close together in a loose line, moving left to right and slightly toward the camera, legs mid-stride: most are black and dark brown, with three dappled grey horses among them, all with long, wild, wind-tossed manes. A ninth dark horse runs alone a little further back on the right. Their hooves kick up bright arcs and sheets of spray that are frozen in the air and glow in the backlight. The sun sits low on the horizon at the far left, flooding the scene with a soft peach and apricot haze; a band of sea mist hangs over the water and hides the distant low hills. Small waves roll in from the left in thin lines, and the wet sand in the foreground is a smooth mirror that reflects the horses' dark legs and the pale sky. Camera: telephoto lens at about 300-400 mm, low angle near sand level, fast shutter speed, the herd sharp and the background soft; warm golden highlights, soft muted shadows, fine detail in wet coats and droplets, gentle atmospheric haze. Peaceful but powerful mood. No people, no riders, no saddles or tack, no fences, no buildings, no text. Landscape 16:9.

Top-down view of a 17th-century-style Florentine pietra dura panel: two goldfinches on a stone branch above a split pomegranate with glossy red seeds and veined green leaves, all in fitted hardstone pieces on veined black marble, framed by a gold-toned scrolling vine border.
A Florentine pietra dura (commesso) hardstone inlay panel seen straight from above, in the style of 17th-century Medici grand-ducal workshops. The ground is deep black marble crossed by fine white and grey veins. A brown stone branch, built from many small fitted segments, runs diagonally from the upper left to the lower right. Two European goldfinches perch side by side on it, facing right: red faces, black-and-white heads, warm tan breasts, and black wings with bright yellow bars and white spots, all made of separate pieces of coral, onyx, alabaster, Siena marble and yellow jasper. Below them hangs one large ripe pomegranate in mottled red-brown jasper, split open to show its pale pith and clusters of glossy deep-red seeds cut as tiny polished garnet-like stones. Pointed leaves in veined green jasper and malachite branch off on both sides, their light and dark tones following the natural grain of the stone. Every shape reads as a separate, precisely fitted piece of cut stone with hairline seams and no paint. A narrow square border frames the panel: thin gold-toned bands enclosing a scrolling vine of small inlaid rosettes and tendrils on black. Soft, even museum lighting, rich saturated stone colors, crisp detail. No text, no signature. Square 1:1.

Early color Autochrome Lumière photograph in faded sepia-pastel tones: a weathered picnic table with a purple-and-white gingham cloth, wicker basket and white enamel jug under pale pink blossoming apple trees, violet wildflowers, starch grain, dust specks and a dark vignette.
A 1910s Autochrome Lumière color photograph of an old apple orchard in full blossom on a calm, overcast spring afternoon. In the right foreground stands a weathered wooden picnic table with attached benches, its grey planks cracked and silvered with age. A purple-and-white gingham cloth is draped across it, hanging over the near edge, and on it rest a large woven wicker picnic basket with a looped handle and a plain white enamel jug. Behind and to the left, gnarled apple trees with dark twisting trunks are covered in pale pink and white blossom and recede in soft, even rows into a hazy distance; the grass between them is carpeted with small violet wildflowers. Render it exactly like a real Autochrome glass plate: muted, faded sepia-pastel colors (dusty pinks, mauve and violet, soft olive and sage greens, warm brown shadows, a pale milky sky), the visible random grain of dyed potato-starch particles that makes every area look softly speckled, fine dust specks across the surface, gentle soft focus from a long exposure, low contrast, and a thick dark vignette with rounded, burnt-looking edges around the whole frame. Diffuse daylight, no harsh shadows. No people, no text, no modern objects. Landscape 4:3.
Turn one upcoming trade show into a YAML booth plan for a small team: a realistic meeting goal, pre-booked meetings, banner and demo script, shift schedule, consent-aware lead capture and grading, budget with cost per meeting, follow-up sequence and risks.
1role: >-2 Trade show planner for small companies and founders. You turn one upcoming3 trade show or conference into a practical booth plan that a team of two to4 five people can run, with a clear goal, a realistic budget, a lead capture5 process and a follow-up plan that actually turns booth conversations into6 meetings and revenue.78event:9 name: "${event_name:Regional SaaS and IT Expo}"10 city: "${city:Lyon}"...+124 more lines