Refactor: leveled logging + consolidate rule defaults into rule-metadata.ts#221
Merged
Conversation
Replace the JSON + codegen + generated-file pipeline with a single hand-edited `src/rules/rule-metadata.ts` that owns `RuleId`, `RULE_IDS`, and `RULE_DEFAULTS`. The catalog test already enforces id drift between this file and `rules/index.ts`, so the codegen layer was belt-and- suspenders. Add `data/defaults-overrides.example.json` as a starting template for operators using `bun run build --defaults <path>`. The user-supplied override file is a different shape from the (now-deleted) `rule-defaults.json` — flat, no wrapper, sparse — so the example makes the expected format discoverable. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`lib/log.ts` now exports `log.debug / info / warn / error` plus a `createRuleLogger(ruleId)` factory that auto-prefixes `[abs:rule-id]` so devtools console filters can pick out one rule's output. The `debug` level is gated on the same `debugTraceStorage` toggle that controls the structured trace buffer — one switch silences both surfaces. Migrate all ~65 call sites: rule files use `createRuleLogger`; engine/registration/background use the default `log` directly with explicit level. `irrelevant-sections-redact` per-ref diagnostic logs move to `log.debug` so they vanish unless the toggle is on. Registration "...failed" paths move to `log.error`, "...threw" recovery paths to `log.warn`, and "unregister no-op" paths to `log.debug`. Background's inject-script failure paths and IDB write failure also move to `log.error`. Fold the two raw `console.error` calls in `content.ts` and `lib/options-badge.ts` into `log.error`. Update the popup "Debug trace" toggle hint to reflect that it now controls both structured trace and verbose console output. Add a short note in `lib/debug-trace.ts` and `lib/log.ts` headers documenting the relationship. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Pre-commit's mdformat hook reflows the AGENTS.md, README.md, and SKILL.md paragraphs that referenced the renamed `rule-metadata.ts` path. No content changes — purely whitespace + line breaks to match the project's mdformat config. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Picks up the few `log.info("...", { ruleId })` sites in `placeholder.ts`
and `rule-engine.ts` that the initial leveled-logger pass left on the
default logger. Switch them to `createRuleLogger(ruleId).info(...)` so
they participate in the `[abs:rule-id]` devtools console filter alongside
the rest of the rule emit sites. No behavior change beyond the prefix.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The `chrome.tabs.onUpdated` loading transition previously called `clearDebugTraceTab(tabId)` alongside the per-frame badge-count reset. That wiped any captured trace on every full-page navigation, so a single user flow that crossed documents couldn't be exported as one continuous trace. Reproducible on the dark-pattern demo site at `shield-dark-pattern-demo.vercel.app`, which serves each route as a fresh document. Drop the IDB clear and append a `navigation` entry in its place. New `NavigationEvent` variant on `DebugTraceEntry` carries `url` and `timestamp`; emission is gated on `debugTraceStorage.get()` so the trace still stays empty when the toggle is off. Per-frame badge counts still reset on navigation. `clearDebugTraceTab` stays wired to `chrome.tabs.onRemoved` so closing a tab still wipes its trace. `getTabStats` continues to exclude bookkeeping entries (segment markers and now navigation markers) from `eventCount` while including them in `byteSize`. New test in `debug-trace-store.test.ts` locks in that shape. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three related maintainability improvements bundled in one PR.
Rule defaults consolidation
Replace the JSON + codegen + generated-file pipeline with a single hand-edited
extension/src/rules/rule-metadata.tsthat ownsRuleId,RULE_IDS, andRULE_DEFAULTS. The existingcatalog.test.tsinvariant already enforces id parity withrules/index.ts, so the codegen layer was belt-and-suspenders. Addsextension/data/defaults-overrides.example.jsonas a starting template for operators usingbun run build --defaults <path>(the user-supplied override format is sparse and flat — distinct from the wrapped{"defaults": {...}}shape of the deleted source JSON).Deleted:
data/rule-defaults.json,data/rule-defaults.schema.ts,scripts/build-rule-defaults.ts,src/rules/rule-defaults.generated.ts, thebuild-rule-defaultsnpm script, the codegen call frombuild.ts, and the CI codegen-freshness check for rule defaults.Leveled logger with per-rule namespacing
lib/log.tsis now a leveled API (log.debug / info / warn / error) plus acreateRuleLogger(ruleId)factory that auto-prefixes[abs:rule-id], so devtools console filters can pick out one rule's output. Thedebuglevel is gated on the existingdebugTraceStoragetoggle — one switch silences both the structured trace buffer and verbose console output.All ~65 call sites migrated: rule files use
createRuleLogger; engine/registration/background use the defaultlogdirectly with explicit level. Theirrelevant-sections-redactper-ref diagnostic logs move tolog.debug(they dominated devtools on classify-heavy pages). Registration…failedpaths go tolog.error,…threwrecovery paths tolog.warn, "unregister no-op" tolog.debug. The two rawconsole.errorcalls incontent.tsandlib/options-badge.tsfold intolog.error. A follow-up pass picks up the remaininglog.info("...", { ruleId })sites inplaceholder.tsandrule-engine.tsso they participate in the same[abs:rule-id]filter.The popup "Debug trace" toggle hint now reflects that it controls both surfaces.
Persist debug trace across top-level navigations
The
chrome.tabs.onUpdatedloading transition previously calledclearDebugTraceTab(tabId)alongside the per-frame badge-count reset, wiping captured trace on every full-page navigation. A single user flow that crossed documents couldn't be exported as one continuous trace — reproducible onshield-dark-pattern-demo.vercel.app, which serves each route as a fresh document.Drop the IDB clear and append a new
navigationentry instead.DebugTraceEntrygains a{ type: "navigation", url, timestamp }variant; emission is gated on the samedebugTraceStoragetoggle so the trace still stays empty when the toggle is off. Per-frame badge counts still reset on navigation.clearDebugTraceTabremains wired tochrome.tabs.onRemovedso closing a tab still wipes its trace.getTabStatscontinues to exclude bookkeeping entries (segment markers and now navigation markers) fromeventCountwhile including them inbyteSize.Test plan
bun run checkclean (biome + eslint)bun run typecheckcleanbun run knipcleanbun run test— full suite passesrule-metadata.tsand confirmcatalog.test.tsfails clearly[abs:irrelevant-sections-redact]debug linesshield-dark-pattern-demo.vercel.app, open the popup, confirmExportJSONL contains a{"type":"navigation",...}entry between the two pages' rule-application eventsbun run buildsucceeds;check-background-purity.tsreportsok--defaults extension/data/defaults-overrides.example.jsonsucceeds🤖 Generated with Claude Code