Skip to content

fix(rules): measure keyword density in main copy - #1190

Merged
nc9 merged 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/fix-keyword-stuffing-1182
Oct 28, 2026
Merged

nc9 merged 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/fix-keyword-stuffing-1182

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

  • measure keyword density over the page main rendered copy and exclude site chrome on body fallback
  • tokenize bounded Unicode words without splitting umlauts or ß
  • ignore repeated German and English price-note terms and add focused regressions

Closes #1182

Validation

  • bun run scripts/test-suites.ts --run rules (7,753 passed, 25 todo)
  • bun run --cwd packages/rules typecheck
  • targeted oxlint on the changed rule and test
  • bun run docs:check
  • bun run docs:build
  • regression proof: the navigation fixture warns when main-content selection is removed
  • detect-secrets 1.5.0

Docs: Updated the keyword-stuffing rule reference. Skill: none.

@nc9

nc9 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Thank you @WilliamK112, the main-copy measurement is the right fix for #1182, and the tokenizer keeping umlauts and ß whole is a good catch. The rules suite, the canonical golden test and the time-budget test all pass on this head. Before merging, four things to look at:

  1. No fallback when <main> is thin. mainContentRoot always takes the first <main>. A tiny first <main> with 130 words of stuffing outside it now returns the "too short" info where the old code warned (same with one small <article> beside the real copy). Falling back to the body when the chosen root has under 100 words, or picking the largest <main>, would close it.
  2. Chrome exclusion inside articles. On the body fallback every <header> and <footer> under the body is dropped, including an article's own header (its h1 and lede). A stuffed article header on a page with no <main> and several articles now passes. Excluding only chrome that is not inside an <article> or <section> would keep the intent.
  3. text.normalize("NFKC") cost. NFKC can expand a character (U+FDFA becomes 18), so a 5 MB page of those takes about 1.2 s and a 15 MB one 9 s. A length cap on the text before normalizing (or normalizing per token) bounds it.
  4. Non-Latin scripts are now scored. \p{L} means a Japanese page of one repeated clause now warns at 100% (each punctuation-delimited clause is one "word"). Either skip Han, Kana and Hangul runs or say so in the docs and the CHANGELOG.

Smaller notes: <article> is documented as a preferred landmark but is only used when there is exactly one; incl vat is two stop words, so "VAT" is ignored everywhere. Happy to merge once 1 to 3 are in and 4 is decided. (Nik)

@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch from 09017e6 to 503ec5d Compare October 27, 2026 13:11
@WilliamK112

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review — I pushed 503ec5d addressing all four points:

  • select the largest substantial main / role="main" (100-word floor), otherwise fall back to the body; a sole article now has the same floor
  • exclude only site-wide chrome during body fallback, retaining header/nav/footer/aside inside an article or section
  • normalize one bounded token at a time instead of applying NFKC to the full page; the hostile U+FDFA benchmark is now ~123 ms for 5M characters and ~376 ms for 15M on my machine
  • skip Han, Kana, and Hangul runs until the rule has real word segmentation, and document that behavior

I also added regression coverage for each case. Validation is green locally: 36 focused tests, full rules suite (7,759 pass), rules typecheck/lint, docs checks/build, and the secret scan.

@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch from 503ec5d to b9bfe7a Compare October 27, 2026 13:12
@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch 3 times, most recently from cf5fcd1 to 34f921c Compare October 28, 2026 04:14
@nc9

nc9 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Reviewed head: 34f921c

Thanks @WilliamK112. All four points from the first review are addressed:

  • the largest substantial main with a 100-word floor and body fallback, plus the same floor for a sole article;
  • header and footer kept inside an article or section;
  • NFKC applied per token, capped at 64 code points;
  • Han, Kana and Hangul skipped and documented.

CI is green. One fix before merge, then a few optional ones.

  1. (medium, please fix) Nested main landmarks do quadratic work. selectMainContent runs getRenderedProseText and keywordTokens on every main, [role="main"] match before the floor check. A hostile page with N nested <main> or <div role="main"> elements costs O(N × page size). Either cap the candidates you measure (for example the first 8 in document order) or skip a landmark nested inside one you've already measured. Please add a regression test with a few thousand nested landmarks that stays fast.
  2. (low, optional) The tokenizer now scores every \p{L} script, but only English and the six European languages have function-word lists, so Cyrillic, Greek or Arabic copy is scored without one. A docs sentence saying so is enough. A test showing a natural Cyrillic paragraph doesn't warn would be even better.
  3. (low, optional) incl and vat sit in the global COMMON_WORDS, so they are ignored on every page. Moving them to the English-only path, like the German price terms, would keep a page stuffed with "VAT" detectable.
  4. (low, noted) On body fallback, footer and nav keyword lists are no longer measured. That's the intended trade-off and the docs say so; nothing to change.
  5. (low, negligible) A <header> inside a thin <main> counts as site chrome on fallback, because only article and section ancestors protect it.

Once 1 is in, this is good to merge.

@nc9 nc9 added bug Something isn't working rules packages/rules — audit rule definitions labels Oct 28, 2026
@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch from 34f921c to 7c39463 Compare October 28, 2026 06:35
@WilliamK112

WilliamK112 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for flagging the nested-landmark performance issue. I capped main landmark measurement at the first 8 eligible candidates and added a regression with 5,000 nested main elements. On my machine, the regression took about 1.76 seconds without the cap and about 14 ms with it. Validation completed: focused keyword-stuffing tests (37 passed), full rules suite (8,057 passed, 25 todo, 0 failed), rules typecheck, targeted oxlint, and git diff check. The update is rebased onto current main and pushed in 57ea6de.

@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch from 7c39463 to 081414a Compare October 28, 2026 06:35
@nc9

nc9 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Reviewed head: 081414a

Thanks @WilliamK112, that closes the medium finding. I read the delta since 34f921c, which is just the cap and its test once main's rebase is set aside.

  • MAX_MAIN_LANDMARK_CANDIDATES = 8 bounds the extraction, and since querySelectorAll returns landmarks in document order, the outermost ones (the largest, when nested) are measured first. Landmarks in site chrome are skipped before the count, and the chrome test is cached per element, so a hostile page full of chrome-wrapped mains stays linear.
  • The 5,000-deep regression test is a good guard. One small note: the < 1_000 ms assertion leaves plenty of headroom over your ~14 ms, but timing assertions can flake on loaded CI runners. If it ever does, a looser bound or a check on how many landmarks get measured would hold up better. Not blocking.
  • Optional items 2 and 3 from the last review (no function-word list for scripts outside the listed languages, and incl/vat ignored globally) are still open. They're fine as follow-ups and don't block this.

Verdict: good to merge once the remaining CI job (CLI tests) is green.

@nc9

nc9 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Reviewed head: ddc279d

A rebase onto current main only. git range-diff shows the patch is identical to 081414a, so the earlier review stands. Good to merge once CI is green.

@WilliamK112
WilliamK112 force-pushed the codex/fix-keyword-stuffing-1182 branch from ddc279d to 57ea6de Compare October 28, 2026 20:07
@nc9

nc9 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Reviewed head: 57ea6de

A rebase onto current main again. git range-diff shows the patch is identical to ddc279d, so the review stands. Good to merge once CI is green.

@nc9
nc9 merged commit 1ab9855 into squirrelscan:main Oct 28, 2026
15 checks passed
nc9 added a commit that referenced this pull request Oct 29, 2026
…er the tags (#1248)

Restores the released v0.0.139 and v0.0.138 CHANGELOG sections byte-for-byte from their tags and puts the bullets of #1215, #1222, #1223 and #1190, deduplicated, under Unreleased. The guard against this is tracked in #1234. Docs: CHANGELOG only. Skill: no change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rules packages/rules — audit rule definitions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

content/keyword-stuffing still counts nav links, splits umlauts and counts German price notes

2 participants