Skip to content

Refactor: leveled logging + consolidate rule defaults into rule-metadata.ts#221

Merged
twschiller merged 5 commits into
mainfrom
refactor/logging-rule-metadata
Jun 9, 2026
Merged

Refactor: leveled logging + consolidate rule defaults into rule-metadata.ts#221
twschiller merged 5 commits into
mainfrom
refactor/logging-rule-metadata

Conversation

@twschiller

@twschiller twschiller commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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.ts that owns RuleId, RULE_IDS, and RULE_DEFAULTS. The existing catalog.test.ts invariant already enforces id parity with rules/index.ts, so the codegen layer was belt-and-suspenders. Adds extension/data/defaults-overrides.example.json as a starting template for operators using bun 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, the build-rule-defaults npm script, the codegen call from build.ts, and the CI codegen-freshness check for rule defaults.

Leveled logger with per-rule namespacing

lib/log.ts is now a leveled API (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 existing debugTraceStorage toggle — 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 default log directly with explicit level. The irrelevant-sections-redact per-ref diagnostic logs move to log.debug (they dominated devtools on classify-heavy pages). Registration …failed paths go to log.error, …threw recovery paths to log.warn, "unregister no-op" to log.debug. The two raw console.error calls in content.ts and lib/options-badge.ts fold into log.error. A follow-up pass picks up the remaining log.info("...", { ruleId }) sites in placeholder.ts and rule-engine.ts so 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.onUpdated loading transition previously called clearDebugTraceTab(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 on shield-dark-pattern-demo.vercel.app, which serves each route as a fresh document.

Drop the IDB clear and append a new navigation entry instead. DebugTraceEntry gains a { type: "navigation", url, timestamp } variant; emission is gated on the same debugTraceStorage toggle so the trace still stays empty when the toggle is off. Per-frame badge counts still reset on navigation. clearDebugTraceTab remains 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.

Test plan

  • bun run check clean (biome + eslint)
  • bun run typecheck clean
  • bun run knip clean
  • bun run test — full suite passes
  • Catalog drift caught: temporarily remove an entry from rule-metadata.ts and confirm catalog.test.ts fails clearly
  • Manual smoke: load unpacked extension, open devtools, verify:
    • With "Debug trace" OFF: no [abs:irrelevant-sections-redact] debug lines
    • With "Debug trace" ON: debug lines appear AND structured trace records as before
  • Manual smoke for trace-across-navigation: with "Debug trace" ON, browse two pages on shield-dark-pattern-demo.vercel.app, open the popup, confirm Export JSONL contains a {"type":"navigation",...} entry between the two pages' rule-application events
  • bun run build succeeds; check-background-purity.ts reports ok
  • Build with --defaults extension/data/defaults-overrides.example.json succeeds

🤖 Generated with Claude Code

twschiller and others added 2 commits June 8, 2026 17:15
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>
@vercel

vercel Bot commented Jun 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 9, 2026 1:30am

Request Review

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>
twschiller and others added 2 commits June 8, 2026 21:29
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>
@twschiller twschiller merged commit bb0beaf into main Jun 9, 2026
8 checks passed
@twschiller twschiller deleted the refactor/logging-rule-metadata branch June 9, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant