Skip to content
Back to search

conorbronsdon-skills

MarketplaceWorks with Claude CodeGitHub

Conor Bronsdon's public Claude Code / Cowork skills.

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 16 Nov 2026. Its scripts were only partly scanned.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/conorbronsdon/avoid-ai-writing with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/conorbronsdon/avoid-ai-writing'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/conorbronsdon-avoid-ai-writing/badge.svg)](/listings/conorbronsdon-avoid-ai-writing)

HTML

<a href="/listings/conorbronsdon-avoid-ai-writing"><img src="/api/listings/conorbronsdon-avoid-ai-writing/badge.svg" alt="findsafeskills safety rating" /></a>

Install

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

Install the complete skill directory

Use the plugin install below or clone the repository into your agent's skills directory. Keep `SKILL.md` with `references/patterns.md`: the entry file loads the catalog before auditing. The bundled `scripts/`, `detector/`, and `examples/` provide optional mechanical verification.

For a single-file rules field, use [`dist/avoid-ai-writing.md`](./dist/avoid-ai-writing.md) (about 138 KB). It includes every rule and profile, with manual fallbacks for commands unavailable outside the bundle. Some rules and instructions fields hold far less than that, so check the size limits under [Other agents](#other-agents) before pasting. Do not copy the slim entry file alone. Older installers that fetch only root `SKILL.md` omit its required reference; use a directory install instead.

Claude Code

**Option 1: Clone into skills directory**

git clone https://github.com/conorbronsdon/avoid-ai-writing ~/.claude/skills/avoid-ai-writing

Then invoke it with `/avoid-ai-writing <your text>`.

**Option 2: Copy a self-contained file**

Download `dist/avoid-ai-writing.md` and place it in any directory that Claude Code can read. Reference it in your `CLAUDE.md`:

- Editing for AI patterns → read `path/to/avoid-ai-writing.md`

**Option 3: Use as a slash command**

Optional alias. Claude Code names a command after its file, so use a different name from `avoid-ai-writing`. If you've done Option 1, the skill already answers to `/avoid-ai-writing` and a command file with the same name would collide with it. Create a command file (e.g., `~/.claude/commands/clean-ai-writing.md`):

---
description: Audit and rewrite content to remove AI writing patterns
---

$ARGUMENTS

Read and follow the instructions in ~/.claude/skills/avoid-ai-writing/SKILL.md

Then use `/clean-ai-writing <your text>` in Claude Code.

Claude Cowork — install as a plugin

[Cowork](https://claude.com/product/cowork) loads skills only from **installed plugins** — it doesn't scan `~/.claude/skills/`, so a bare clone (the Claude Code steps above) won't be discovered there. This repo doubles as a single-plugin [marketplace](https://code.claude.com/docs/en/plugin-marketplaces), so install it as a plugin instead:

/plugin marketplace add conorbronsdon/avoid-ai-writing
/plugin install avoid-ai-writing@conorbronsdon-skills
/reload-plugins   # or restart the session, to activate the skill

In the Cowork desktop app, do the same from **Customize → Plugins → Add marketplace from GitHub** (`conorbronsdon/avoid-ai-writing`), then install **avoid-ai-writing**. The skill auto-triggers from phrases like "remove AI-isms." New releases arrive when the plugin's version is bumped — run `/plugin marketplace update` to pull them.

The same plugin install works in Claude Code if you'd rather have a versioned, updatable plugin than the file clone above.

> Prefer not to install a plugin? Copy `dist/avoid-ai-writing.md` into a folder connected to your Cowork session and tell the agent to follow `./avoid-ai-writing.md` — works as a one-off, no auto-trigger.

Google Antigravity

With the [Antigravity CLI](https://antigravity.google/docs/plugins?tab=cli) installed, clone this repository and install its bundled plugin directory:

git clone https://github.com/conorbronsdon/avoid-ai-writing
cd avoid-ai-writing
agy plugin validate plugins/avoid-ai-writing
agy plugin install ./plugins/avoid-ai-writing
agy plugin list

The Antigravity manifest lives in `plugins/avoid-ai-writing/.antigravity-plugin/plugin.json`; install `plugins/avoid-ai-writing` rather than the repository root. Check that `avoid-ai-writing` is enabled in `agy plugin list`; if disabled, run `agy plugin enable avoid-ai-writing`. Then ask the agent to audit or rewrite a draft using the avoid-ai-writing skill.

**Without the CLI (Antigravity app or IDE):** copy the `plugins/avoid-ai-writing` folder from a clone into `.agents/plugins/` at

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

.claude-plugin/marketplace.json · Manifest · 37 lines
  1. {
  2. "$schema": "https://json.schemastore.org/claude-code-marketplace.json",
  3. "name": "conorbronsdon-skills",
  4. "description": "Conor Bronsdon's public Claude Code / Cowork skills.",
  5. "owner": {
  6. "name": "Conor Bronsdon"
  7. },
  8. "plugins": [
  9. {
  10. "name": "avoid-ai-writing",
  11. "source": "./plugins/avoid-ai-writing",
  12. "description": "Audit & rewrite content to remove AI writing patterns (\"AI-isms\"). Supports detect-only and edit-in-place modes, voice profiles, and iterate-to-convergence.",
  13. "category": "productivity",
  14. "keywords": [
  15. "writing",
  16. "editing",
  17. "ai-detection",
  18. "humanize"
  19. ]
  20. },
  21. {
  22. "name": "fact-check",
  23. "source": {
  24. "source": "github",
  25. "repo": "conorbronsdon/fact-check"
  26. },
  27. "description": "Check the specific claims in a draft before it ships: extract numbers, dates, names, quotes and firsts, try to refute each against primary sources, return a verdict per claim. Flags, never edits.",
  28. "category": "productivity",
  29. "keywords": [
  30. "writing",
  31. "fact-checking",
  32. "verification",
  33. "publishing"
  34. ]
  35. }
  36. ]
  37. }
README.md · README · 771 lines
  1. <div align="center">
  2. # avoid-ai-writing
  3. Audit & rewrite content to remove AI writing patterns. A practical skill for any AI agent. Supports detect-only and edit-in-place modes, plus voice profiles.
  4. [![GitHub stars](https://img.shields.io/github/stars/conorbronsdon/avoid-ai-writing?style=social)](https://github.com/conorbronsdon/avoid-ai-writing/stargazers)
  5. [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](LICENSE)
  6. [![npm version](https://img.shields.io/npm/v/avoid-ai-writing-detector.svg)](https://www.npmjs.com/package/avoid-ai-writing-detector)
  7. [![detector](https://github.com/conorbronsdon/avoid-ai-writing/actions/workflows/detector-test.yml/badge.svg)](https://github.com/conorbronsdon/avoid-ai-writing/actions/workflows/detector-test.yml)
  8. [![Podcast](https://img.shields.io/badge/Podcast-Chain_of_Thought-purple?style=flat-square)](https://chainofthought.show/?utm_source=github&utm_medium=referral&utm_campaign=repo-readme&utm_content=avoid-ai-writing)
  9. [![X](https://img.shields.io/badge/X-@ConorBronsdon-black?style=flat-square&logo=x)](https://x.com/ConorBronsdon)
  10. <img src="docs/demo.gif" alt="The bundled detector engine flagging 13 AI-writing patterns by category in a sample paragraph, then scoring the clean rewrite 0/100" width="800">
  11. </div>
  12. ---
  13. A portable writing skill for [Claude Code](https://docs.anthropic.com/en/docs/claude-code), Codex, [OpenClaw](https://github.com/openclaw/openclaw), [Hermes](https://github.com/NousResearch/hermes-agent), and any other [agentskills.io](https://agentskills.io)-compatible agent. Audits and rewrites content to remove AI writing patterns ("AI-isms").
  14. The rules and bundled detector are calibrated for English. See [community language adaptations](#community--multilingual) for independently maintained versions in other languages.
  15. **Three modes:**
  16. - **Rewrite** (default) — flags AI patterns, applies up to two editing passes, verifies the result when possible, and returns one final version.
  17. - **Detect** — flags AI patterns without rewriting. Shows which flags are real problems vs. judgment calls. Useful when patterns might be intentional, when auditing content you don't want altered, or when you just want a quick scan.
  18. - **Edit** — edits a prose file in place (via the Edit tool) with minimal, targeted changes, preserving passages that are already human. Source code, configuration, and generated data are refused because prose rewrites can corrupt structured content. Returns an edits-made + verification report, not the full file.
  19. Use `--iterate 1|2` to set the editing-pass ceiling. `--iterate 1` allows the initial rewrite only. `--iterate 2` uses the default maximum of an initial rewrite plus one correction or preservation repair, and stops early when no justified in-scope edit remains. Checks do not consume a pass, but any change they prompt does. The skill reports how many editing passes it used and any intentional, protected, or unresolved finding.
  20. An optional **voice profile** (casual / professional / technical / warm / blunt) sets how the prose should sound, independent of the audience context profile.
  21. The [glossary](GLOSSARY.md) defines the terms these docs use: word tiers, severity tiers, modes, profiles, and detector options.
  22. ## Table of contents
  23. - [Quick demo](#quick-demo)
  24. - [Why a skill, not just a prompt](#why-a-skill-not-just-a-prompt)
  25. - [Installation & Usage](#installation--usage)
  26. * [Install the complete skill directory](#install-the-complete-skill-directory)
  27. * [Claude Code](#claude-code)
  28. * [Claude Cowork — install as a plugin](#claude-cowork--install-as-a-plugin)
  29. * [Google Antigravity](#google-antigravity)
  30. * [OpenClaw](#openclaw)
  31. * [Cursor](#cursor)
  32. * [Hermes](#hermes)
  33. * [OpenAI Codex](#openai-codex)
  34. * [Native ChatGPT and Codex plugin package](#native-chatgpt-and-codex-plugin-package)
  35. * [Other agents](#other-agents)
  36. * [Triggering the skill](#triggering-the-skill)
  37. - [Pattern reference](#pattern-reference)
  38. * [Content Patterns](#content-patterns)
  39. * [Language Patterns](#language-patterns)
  40. * [Structure Patterns](#structure-patterns)
  41. * [Communication Patterns](#communication-patterns)
  42. * [Meta Patterns](#meta-patterns)
  43. * [Structural Detection (v3.4)](#structural-detection-v34)
  44. * [AI-tool fingerprints & later additions (v3.5–3.8)](#ai-tool-fingerprints--later-additions-v3538)
  45. * [Conversational-register patterns (v3.15)](#conversational-register-patterns-v315)
  46. * [Share-post framing (v3.20)](#share-post-framing-v320)
  47. * [Narrated candor (v3.21)](#narrated-candor-v321)
  48. * [Unnecessary hyphenation (v3.24)](#unnecessary-hyphenation-v324)
  49. - [Full Example](#full-example)
  50. - [Run the detector](#run-the-detector)
  51. * [Score a file or piped text from the command line](#score-a-file-or-piped-text-from-the-command-line)
  52. * [Gate prose in GitHub Actions or pre-commit](#gate-prose-in-github-actions-or-pre-commit)
  53. * [Use the detector over MCP](#use-the-detector-over-mcp)
  54. - [Pre-publish pipeline](#pre-publish-pipeline)
  55. - [House style is a different job](#house-style-is-a-different-job)
  56. - [More from me](#more-from-me)
  57. - [Credits](#credits)
  58. - [Community / Multilingual](#community--multilingual)
  59. - [Official sources and sponsorship](#official-sources-and-sponsorship)
  60. - [Disclaimer](#disclaimer)
  61. - [License](#license)
  62. ## Quick demo
  63. **Input:**
  64. > Certainly! Acme Analytics, a vibrant startup nestled in the heart of Boulder's thriving tech ecosystem, has secured $40M in Series B funding — marking a watershed moment for the observability landscape. The platform serves as a unified hub, featuring real-time dashboards, boasting sub-second queries, and presenting a seamless integration layer. Moreover, experts believe Acme is poised to disrupt the market. In conclusion, the future looks bright!
  65. **Final rewrite:**
  66. > Acme Analytics, a Boulder-based startup, raised a $40M Series B. Its observability platform has real-time dashboards, runs queries in under a second, and includes an integration layer.
  67. **Verification:** One editing pass. Review found no further justified in-scope edit; no deterministic preservation check ran in this prose-only demo.
  68. **What it caught:** the chatbot opener ("Certainly!"), promotional modifiers, inflated significance, roundabout verbs, vague attribution, and the generic conclusion. The rewrite keeps the funding, location, and three product capabilities. It removes the unsupported market prediction without inventing an investor or an integration mechanism.
  69. ## Why a skill, not just a prompt
  70. A one-shot "make this sound human" prompt catches the obvious stuff. This skill is different:
  71. - **Single final rewrite** — completes its audit, correction, and available verification before returning one full version. A short change summary follows when useful; a detailed audit remains available on request without adding a competing draft.
  72. - **Two-pass editing** — review can catch patterns that survive the first edit, while one shared limit covers both corrective edits and preservation repairs.
  73. - **Surface-aware delivery** — checks how the destination renders single newlines before returning prose, unwraps accidental hard-wrapped paragraphs only when those breaks would show, and preserves intentional structure such as lists, poetry, code, and addresses.
  74. - **112-entry word replacement table across 3 tiers + 10 Tier 3 phrases** — not vibes-based. Every flagged word has a specific, plainer alternative. "Leverage" → "use." "Commence" → "start." Tier 1 matches flag unless a listed exception applies, Tier 2 words flag when they cluster, Tier 3 words flag only at high density. Tier 1 itself splits into **1A frequency markers** (`delve`, `tapestry`) and **1B clarity edits** (`in order to`, `utilize`) — same fix, but only 1A is evidence about how a passage was produced, and 1B is weighted lower so a wordiness fix cannot push a document toward an AI classification. Tier 3 *phrases* (multi-word boilerplate like "the integration of," "decentralized compute") flag on per-phrase repetition or when 3+ distinct phrases stack in one piece — the LLM-self-varies-boilerplate shape.
  75. - **74 pattern categories** — representative examples below, each with before/after. Includes structural detection (hashtag stuffing, bare-NP bullet lists, hedge-stacked predictions), AI-tool fingerprints (placeholders, citation markup, UTM params), rhythm/uniformity checks, conversational-register tells, and writer-side tests. The full catalog lives in [`references/patterns.md`](./references/patterns.md); this count is enforced against it in CI.
  76. - **Detect mode** — flag patterns without rewriting. See which flags are real problems vs. judgment calls. Useful when patterns might be intentional or you're auditing content you don't want altered.
  77. - **Works across platforms** — a directory-based skill runs in Claude Code, Cowork (as a plugin), Google Antigravity (as a plugin), OpenClaw, Cursor (as a ported rule), and other directory-aware agents. See the install paths below.
  78. ## Installation & Usage
  79. ### Install the complete skill directory
  80. Use the plugin install below or clone the repository into your agent's skills directory. Keep `SKILL.md` with `references/patterns.md`: the entry file loads the catalog before auditing. The bundled `scripts/`, `detector/`, and `examples/` provide optional mechanical verification.
  81. For a single-file rules field, use [`dist/avoid-ai-writing.md`](./dist/avoid-ai-writing.md) (about 138 KB). It includes every rule and profile, with manual fallbacks for commands unavailable outside the bundle. Some rules and instructions fields hold far less than that, so check the size limits under [Other agents](#other-agents) before pasting. Do not copy the slim entry file alone. Older installers that fetch only root `SKILL.md` omit its required reference; use a directory install instead.
  82. ### Claude Code
  83. **Option 1: Clone into skills directory**
  84. ```bash
  85. git clone https://github.com/conorbronsdon/avoid-ai-writing ~/.claude/skills/avoid-ai-writing
  86. ```
  87. Then invoke it with `/avoid-ai-writing <your text>`.
  88. **Option 2: Copy a self-contained file**
  89. Download `dist/avoid-ai-writing.md` and place it in any directory that Claude Code can read. Reference it in your `CLAUDE.md`:
  90. ```markdown
  91. - Editing for AI patterns → read `path/to/avoid-ai-writing.md`
  92. ```
  93. **Option 3: Use as a slash command**
  94. Optional alias. Claude Code names a command after its file, so use a different name from `avoid-ai-writing`. If you've done Option 1, the skill already answers to `/avoid-ai-writing` and a command file with the same name would collide with it. Create a command file (e.g., `~/.claude/commands/clean-ai-writing.md`):
  95. ```markdown
  96. ---
  97. description: Audit and rewrite content to remove AI writing patterns
  98. ---
  99. $ARGUMENTS
  100. Read and follow the instructions in ~/.claude/skills/avoid-ai-writing/SKILL.md
  101. ```
  102. Then use `/clean-ai-writing <your text>` in Claude Code.
  103. ### Claude Cowork — install as a plugin
  104. [Cowork](https://claude.com/product/cowork) loads skills only from **installed plugins** — it doesn't scan `~/.claude/skills/`, so a bare clone (the Claude Code steps above) won't be discovered there. This repo doubles as a single-plugin [marketplace](https://code.claude.com/docs/en/plugin-marketplaces), so install it as a plugin instead:
  105. ```bash
  106. /plugin marketplace add conorbronsdon/avoid-ai-writing
  107. /plugin install avoid-ai-writing@conorbronsdon-skills
  108. /reload-plugins # or restart the session, to activate the skill
  109. ```
  110. In the Cowork desktop app, do the same from **Customize → Plugins → Add marketplace from GitHub** (`conorbronsdon/avoid-ai-writing`), then install **avoid-ai-writing**. The skill auto-triggers from phrases like "remove AI-isms." New releases arrive when the plugin's version is bumped — run `/plugin marketplace update` to pull them.
  111. The same plugin install works in Claude Code if you'd rather have a versioned, updatable plugin than the file clone above.
  112. > Prefer not to install a plugin? Copy `dist/avoid-ai-writing.md` into a folder connected to your Cowork session and tell the agent to follow `./avoid-ai-writing.md` — works as a one-off, no auto-trigger.
  113. ### Google Antigravity
  114. With the [Antigravity CLI](https://antigravity.google/docs/plugins?tab=cli)
  115. installed, clone this repository and install its bundled plugin directory:
  116. ```bash
  117. git clone https://github.com/conorbronsdon/avoid-ai-writing
  118. cd avoid-ai-writing
  119. agy plugin validate plugins/avoid-ai-writing
  120. agy plugin install ./plugins/avoid-ai-writing
  121. agy plugin list
  122. ```
  123. The Antigravity manifest lives in
  124. `plugins/avoid-ai-writing/.antigravity-plugin/plugin.json`; install
  125. `plugins/avoid-ai-writing` rather than the repository root. Check that
  126. `avoid-ai-writing` is enabled in `agy plugin list`; if disabled, run
  127. `agy plugin enable avoid-ai-writing`. Then ask the agent to audit or rewrite
  128. a draft using the avoid-ai-writing skill.
  129. **Without the CLI (Antigravity app or IDE):** copy the
  130. `plugins/avoid-ai-writing` folder from a clone into `.agents/plugins/` at
  131. the root of a workspace (that project only) or into
  132. `~/.gemini/config/plugins/` (every workspace). See
  133. [manual plugin installation](https://antigravity.google/docs/plugins). In
  134. the IDE, the Customizations dropdown in the agent side panel shows whether
  135. the plugin's skill loaded.
  136. ### OpenClaw
  137. **Option 1: [Install from ClawHub](https://clawhub.ai/conorbronsdon/avoid-ai-writing)**
  138. ```bash
  139. clawhub install avoid-ai-writing
  140. ```
  141. **Option 2: Clone into skills directory**
  142. ```bash
  143. git clone https://github.com/conorbronsdon/avoid-ai-writing ~/.openclaw/skills/avoid-ai-writing
  144. ```
  145. ### Cursor
  146. Drop the ported rule into your project's `.cursor/rules/`:
  147. ```bash
  148. mkdir -p .cursor/rules
  149. curl -o .cursor/rules/avoid-ai-writing.mdc \
  150. https://raw.githubusercontent.com/conorbronsdon/avoid-ai-writing/main/cursor-rules/avoid-ai-writing.mdc
  151. ```
  152. See [`cursor-rules/README.md`](./cursor-rules/README.md) for activation globs and trigger phrases. Functionally identical to the Claude Code skill — same tier vocabulary, same context profiles, same modes.
  153. ### Hermes
  154. Drop the skill into Hermes's skills directory — it then appears automatically as `/avoid-ai-writing`, no registration needed:
  155. ```bash
  156. git clone https://github.com/conorbronsdon/avoid-ai-writing ~/.hermes/skills/writing/avoid-ai-writing
  157. ```
  158. ### OpenAI Codex
  159. Codex reads [Agent Skills](https://developers.openai.com/codex/skills) in the same `SKILL.md` format. Put it in `.agents/skills/` at the repo root, or `~/.agents/skills/` to use it across all your projects:
  160. ```bash
  161. git clone https://github.com/conorbronsdon/avoid-ai-writing .agents/skills/avoid-ai-writing
  162. ```
  163. ### Native ChatGPT and Codex plugin package
  164. Published in the [OpenAI Plugins Directory](https://chatgpt.com/plugins/plugins_6a9b77b18b8881918efa9c1255868164); install it there for ChatGPT or Codex.
  165. The [plugin package](./OPENAI_PLUGIN.md) keeps the canonical `SKILL.md` as its editorial authority and provides seven focused Skills:
  166. - `avoid-ai-writing` — uses the canonical audit and rewrite instructions
  167. - `avoid-ai-writing-router` — routes mixed and multi-stage requests
  168. - `ai-writing-detector` — runs the bundled deterministic detector
  169. - `voice-preserving-rewriter` — rewrites returned text while retaining voice
  170. - `file-edit-in-place` — edits only explicitly named files
  171. - `preservation-verifier` — checks meaning and constraints before and after a rewrite
  172. - `false-positive-reviewer` — reviews detector findings that need context
  173. From a cloned checkout, build and validate the package with:
  174. ```bash
  175. python3 scripts/package-openai-plugin.py . /tmp/avoid-ai-writing.zip --json
  176. python3 scripts/validate-openai-plugin.py . --json
  177. ```
  178. ### Other agents
  179. Use the destination below for your tool. Measure the downloaded file with `wc -c dist/avoid-ai-writing.md` for its exact UTF-8 byte count before comparing byte limits; character limits count characters instead.
  180. | Tool | Where to put it | Size limit |
  181. |------|-----------------|------------|
  182. | **Windsurf** | clone the skill directory into `.devin/skills/avoid-ai-writing/` (`.windsurf/skills/avoid-ai-writing/` is also supported) ([skills docs](https://docs.windsurf.com/windsurf/cascade/skills)) | Workspace rule files are limited to 12,000 characters ([rules docs](https://docs.windsurf.com/windsurf/cascade/memories)), so the single file does not fit in `.windsurf/rules/` |
  183. | **Cline** | `.clinerules/avoid-ai-writing.md` | None documented; rules use context tokens on every task ([docs](https://docs.cline.bot/features/cline-rules)) |
  184. | **GitHub Copilot** (VS Code) | paste into `.github/copilot-instructions.md` | None documented ([docs](https://docs.github.com/en/copilot/how-tos/configure-custom-instructions/add-repository-instructions)) |
  185. | **Claude.ai Projects** | paste `dist/avoid-ai-writing.md` into the project's custom instructions | None documented ([docs](https://support.claude.com/en/articles/9519177-how-can-i-create-and-manage-projects)) |
  186. | **ChatGPT** | install the [native plugin package](#native-chatgpt-and-codex-plugin-package), or upload `dist/avoid-ai-writing.md` as a Custom GPT Knowledge file | A Custom GPT's Instructions field holds far less than the file; do not paste it there |
  187. For a Custom GPT Knowledge upload, add a short instruction directing the GPT to consult `avoid-ai-writing.md` when auditing or rewriting prose. Knowledge files supply reference material; uploading one alone does not activate the skill. Test the GPT in Preview with an example from this README before relying on it ([GPT configuration docs](https://help.openai.com/en/articles/8554397-creating-a-gpt)).
  188. ### Triggering the skill
  189. Once installed, ask your assistant to clean up AI writing:
  190. - "Remove AI-isms from this post"
  191. - "Audit this draft for AI tells"
  192. - "Make this sound less like AI"
  193. - "Clean up AI writing in this paragraph"
  194. In **rewrite mode** (default), the skill completes its review and available verification before returning the rewritten text exactly once:
  195. 1. **Final rewrite** — the verified or honestly qualified final version
  196. 2. **Changes** — a short summary when edits were made and the summary is useful
  197. 3. **Verification** — editing passes used, checks that ran, stop reason, and any intentional, protected, source-blocked, or unresolved findings
  198. Ask for a detailed or exhaustive audit to add quoted findings before the final rewrite. The response still contains one full version. Clean input can be returned unchanged with zero editing passes.
  199. In **detect mode**, the skill returns two sections:
  200. 1. **Issues found** — every AI-ism identified, grouped by severity (P0/P1/P2)
  201. 2. **Assessment** — which flags are clear problems vs. patterns that may be intentional or effective in context
  202. Trigger detect mode with: "detect," "flag only," "audit only," "just flag," "scan," or similar.
  203. ## Pattern reference
  204. > Representative examples from the catalog — not the exhaustive list (that's [`references/patterns.md`](./references/patterns.md)). The skill's human-facing prose catalog and the [detector engine](./detector/) use **different counts on purpose**: the engine implements 54 `type` categories because it splits the vocabulary tiers and adds stylometric/fingerprint signals (punctuation distribution, function-word entropy, bypass-trick detection) that work as math over a document rather than as a rule you'd look up. The two are mapped in [`detector/CATEGORIES.md`](./detector/CATEGORIES.md); don't "fix" one count to match the other.
  205. ### Content Patterns
  206. | # | Pattern | Before | After |
  207. |---|---------|--------|-------|
  208. | 1 | **Significance inflation** | "marking a pivotal moment in the evolution of..." | State the supported event; do not invent a date or purpose |
  209. | 2 | **Notability name-dropping** | "cited in NYT, BBC, and Wired" | Keep supported citations; remove claims that they prove importance |
  210. | 3 | **Superficial -ing analyses** | "symbolizing... reflecting... showcasing..." | Replace with specific facts or cut |
  211. | 4 | **Promotional language** | "nestled within the breathtaking region" | Keep the supplied location; remove promotional modifiers |
  212. | 5 | **Vague attributions** | "Experts believe it plays a crucial role" | Request the source, or flag the claim for verification |
  213. | 6 | **Formulaic challenges** | "Despite challenges... continues to thrive" | Name the challenge and the response |
  214. | 7 | **Novelty inflation** | "He introduced a term I hadn't heard before" | Keep the personal observation if it matters; do not invent an explanation |
  215. ### Language Patterns
  216. | # | Pattern | Before | After |
  217. |---|---------|--------|-------|
  218. | 8 | **Word/phrase replacements (3 tiers)** | "leverage... robust... seamless... utilize" | "use... reliable... smooth... use" |
  219. | 9 | **Copula avoidance** | "serves as... features... boasts" | "is... has" |
  220. | 10 | **Synonym cycling** | "developers... engineers... practitioners... builders" | "developers" (repeat the clear word) |
  221. | 11 | **Template phrases** | "a [adj] step towards [adj] infrastructure" | Describe the specific outcome |
  222. | 12 | **Filler phrases** | "In order to," "Due to the fact that" | "To," "Because" |
  223. | 13 | **False ranges** | "from the Big Bang to dark matter" | List the actual topics |
  224. | 14 | **Parenthetical hedging** | "tools (like X and Y)" | Name them directly or cut |
  225. ### Structure Patterns
  226. | # | Pattern | Before | After |
  227. |---|---------|--------|-------|
  228. | 15 | **Formatting** | Em dashes (— and --), bold overuse, emoji headers, bullet-heavy | Commas/periods, prose paragraphs |
  229. | 16 | **Sentence structure** | "It's not X, it's Y" + hollow intensifiers + hedging | Direct positive statements |
  230. | 17 | **Structural issues** | Uniform paragraphs, formulaic openings, too-clean grammar | Varied length, lead with the point |
  231. | 18 | **Transition phrases** | "Moreover," "Furthermore," "In today's [X]" | "and," "also," or restructure |
  232. | 19 | **Inline-header lists** | "**Speed:** Speed improved by..." | Write the point directly |
  233. | 20 | **Title case headings** | "Strategic Negotiations And Partnerships" | "Strategic negotiations and partnerships" |
  234. | 21 | **Numbered list inflation** | "Here are 7 reasons why..." | Cut to the 2-3 that matter |
  235. | 22 | **False concession** | "While X has limitations, it's still remarkable" | State the real tradeoff |
  236. | 23 | **Rhetorical question openers** | "What if there were a better way to...?" | Lead with the claim |
  237. ### Communication Patterns
  238. | # | Pattern | Before | After |
  239. |---|---------|--------|-------|
  240. | 24 | **Chatbot artifacts** | "I hope this helps! Let me know if..." | Remove entirely |
  241. | 25 | **"Let's" constructions** | "Let's explore," "Let's break this down" | Just start with the point |
  242. | 26 | **Cutoff disclaimers** | "While details are limited in available sources..." | Find sources or remove |
  243. | 27 | **Generic conclusions** | "The future looks bright," "Only time will tell" | Specific closing thought or cut |
  244. | 28 | **Stock reaction framing** | "What surprised me most," "I was fascinated to discover" | Keep specific, authentic reactions; cut empty stock framing or make the reaction concrete |
  245. | 29 | **Reasoning chain artifacts** | "Let me think step by step," "Breaking this down" | State conclusion, then evidence |
  246. | 30 | **Sycophantic tone** | "Great question!", "You're absolutely right!" | Remove entirely |
  247. | 31 | **Acknowledgment loops** | "You're asking about," "To answer your question" | Just answer directly |
  248. | 32 | **Confidence calibration** | "It's worth noting," "Interestingly," "Surprisingly" | Let the fact speak for itself |
  249. ### Meta Patterns
  250. | # | Pattern | Before | After |
  251. |---|---------|--------|-------|
  252. | 33 | **Excessive structure** | 5 headers in 200 words, "Overview:", "Key Points:" | Merge sections, use specific headers |
  253. | 34 | **Rhythm and uniformity** | All sentences 15–25 words, all paragraphs same length | Mix short/long, fragments, questions |
  254. | 35 | **Over-polishing** | Every irregularity sanded away, perfectly uniform prose | Keep natural disfluency, varied rhythm |
  255. | 36 | **Rewrite-vs-patch threshold** | 5+ vocabulary flags + 3+ pattern categories + uniform rhythm | Advise full rewrite, not patching |
  256. ### Structural Detection (v3.4)
  257. Added in v3.4 to catch LLM output that sidesteps the vocabulary tables by substituting synonyms but still leans on structural shapes detectors can identify. Crypto/web3/AI-infra content is where these patterns concentrate most heavily, but the rules generalize to any social-length post.
  258. | # | Pattern | Before | After |
  259. |---|---------|--------|-------|
  260. | 37 | **Tier 3 phrases (multi-word boilerplate)** | "the integration of," "decentralized compute," "community-driven," "long-term sustainability" stacked across a piece | Replace the repeated phrase with a specific claim, or vary genuinely. Flagged per-phrase at ≥2 hits, or as a cluster when ≥3 distinct phrases appear |
  261. | 38 | **Future-narrative closers** | "may become one of the most important narratives of the next market cycle" | Pick the falsifiable version. "X may exceed Y by 2027" is a prediction; the template form is not |
  262. | 39 | **Hedge-stacked predictions** | "could potentially create," "may eventually unlock" | Pick one. Each hedge cancels the next |
  263. | 40 | **"Real/actual" adjective inflation** | "real on-chain tokenomics," "actual reward sustainability" | Drop the empty intensifier and add the specific claim. Carve-out: "real on-chain settlement, *not* bridged IOUs" is honest contrastive writing — the AI tell is the unsaid contrast |
  264. | 41 | **Hashtag stuffing** | 15-tag trailing block: `#AI #Crypto #Web3 #Innovation #FutureTech…` | 2-3 specific tags max, or none. Empirical threshold: 6+ tags is near-universal in LLM social output, rare in thoughtful human posts |
  265. | 42 | **Bullet lists of bare noun phrases** | `* Stable mining efficiency / Reliable pool connectivity / Optimized RandomX performance / Low failed share rates / Effective hardware utilization / Consistent thermal stability` | Convert to prose, or rewrite each item as a full claim with a verb and a number. Carve-out: genuine list content (changelogs, parameter docs, ingredient lists) where bare NPs are correct |
  266. ### AI-tool fingerprints & later additions (v3.5–3.8)
  267. | # | Pattern | Before | After |
  268. |---|---------|--------|-------|
  269. | 43 | **Unfilled placeholders** | `[Your Name]`, `[INSERT SOURCE]`, `2025-XX-XX` | Fill in with real content or delete — shipped placeholders are a near-definitive tell |
  270. | 44 | **Chatbot citation markup** | `citeturn0search0`, `oai_citation`, `contentReference[oaicite:0]` | Strip the markup token entirely |
  271. | 45 | **AI-tool URL parameters** | `utm_source=chatgpt.com`, `utm_source=copilot.com` | Strip the tracking parameter; keep the URL if the link matters |
  272. | 46 | **Speculative gap-filling** | "maintains a low profile," "likely began his career" | Cut the guess, or replace with a sourced fact |
  273. | 47 | **Hyphenated modifier stacking** | "a high-quality, well-architected, future-proof solution" | Cut to the modifier that matters; the individual hyphens may be correct |
  274. | 48 | **Infomercial engagement hooks** | "The catch?", "The kicker?", "Here's the thing." | Delete the hook, state the thing |
  275. | 49 | **Vocabulary diversity (low TTR)** | Narrow, repetitive word range across 200+ words | Broaden the *what* — name specific things, cite specific cases |
  276. | 50 | **Self-labeling significance** | "That last move is the contrarian one," "This is the interesting part" | Cut the label; let the explanation carry the weight, or reposition the item so it stands out on its own |
  277. | 51 | **List-label periods** | `- **Intros.** Years of conferences and operator network.` (also unbolded: `- Intros. Years of...`) | Use a colon, not a period, on a list label: `- **Intros:** years of conferences and operator network.` |
  278. ### Conversational-register patterns (v3.15)
  279. Added after a real-world exchange in which a maintainer called out an assisted-sounding GitHub issue reply with "I prefer to talk human to human." Both are judgment calls rather than regex-detectable (an ordinary human paragraph and an AI-generated one can look structurally identical; the tell is the register and the redundant context, not a fixed shape) — see the `detector/CATEGORIES.md` §C note for why a first attempt at a wall-of-text detector was reverted.
  280. | # | Pattern | Before | After |
  281. |---|---------|--------|-------|
  282. | 52 | **Wall-of-text replies** | A 4+ sentence, sub-150-word reply delivered as one unbroken paragraph with no line breaks — the shape LLMs default to in issue/PR comments, chat, and DMs | Break at thought boundaries. One idea per line-group, the way a person actually types a reply |
  283. | 53 | **Recap-flattery opener** | "Thanks for all the legwork here — the migration script and the rollback plan you worked through are what made this possible." | Substance first. If thanks is warranted, one plain clause without the recap: "Thanks for the legwork — this looks right to me" |
  284. ### Share-post framing (v3.20)
  285. | # | Pattern | Before | After |
  286. |---|---------|--------|-------|
  287. | 54 | **Lingering-attention claims** | "The line I keep coming back to:", "I can't stop thinking about this," "this has been rattling around in my head all week" | Open on the thing itself. Carve-out: keep the frame when a reason follows ("I keep coming back to exit-voice because it predicts who quits") |
  288. ### Narrated candor (v3.21)
  289. | # | Pattern | Before | After |
  290. |---|---------|--------|-------|
  291. | 55 | **Narrated candor** | "Two caveats I would rather flag than let you discover later:", "I want to be upfront:" | State the caveats. Judgment-only: the same words carry real content in conflict-of-interest disclosure ("in the interest of full disclosure, I own shares in…"), which a regex cannot separate from the empty frame |
  292. ### Unnecessary hyphenation (v3.24)
  293. | # | Pattern | Before | After |
  294. |---|---------|--------|-------|
  295. | 56 | **Unnecessary hyphenation** | "research-impact aggregator," "code-base," "in real-time," "works out-of-the-box" | "research impact aggregator," "codebase," "in real time," "works out of the box." Preserve legitimate modifiers such as "real-time analytics" |
  296. Two writer-side **tests** round out the catalog (judgment checks, not auto-detected): **paragraph-reshuffle immunity** (can you swap two body paragraphs without breaking the piece?) and the **treadmill effect** ("what's actually new in this paragraph?").
  297. ## Full Example
  298. **Before (AI-generated):**
  299. > Certainly! Here's a comprehensive overview of Acme's Series B.
  300. >
  301. > Acme Analytics, a vibrant startup nestled in the heart of Boulder's thriving tech ecosystem, has secured $40M in Series B funding — marking a watershed moment for the company and the observability landscape at large. The round was led by Sequoia, with participation from Andreessen Horowitz, Y Combinator, and Index Ventures, underscoring the robust investor confidence in Acme's vision.
  302. >
  303. > The platform serves as a unified hub for engineering teams, featuring real-time dashboards, boasting sub-second query performance, and presenting a seamless integration layer. It's not just another monitoring tool — it's a paradigm shift in how organizations navigate complex distributed systems. Experts believe Acme is poised to disrupt the $15B observability market. Studies show that companies utilizing comprehensive monitoring solutions can ascertain issues 40% faster.
  304. >
  305. > - 🚀 **Performance:** The platform streamlines incident response, empowering engineers to resolve issues faster.
  306. > - 💡 **Scale:** Acme fosters collaboration across teams, from frontend developers to SRE practitioners to platform builders to infrastructure engineers.
  307. > - ✅ **Adoption:** Customer adoption continues to accelerate, reflecting broader industry trends.
  308. >
  309. > Moreover, Acme's approach showcases the potential of modern observability — symbolizing a shift toward proactive monitoring, reflecting years of R&D investment, and highlighting the intricate interplay between data collection and actionable intelligence. Despite challenges common in the crowded monitoring space, Acme continues to thrive. In order to fully capitalize on this momentum, the company plans to expand its go-to-market endeavors.
  310. >
  311. > In conclusion, the future looks bright for Acme Analytics. Only time will tell how this game-changer will reshape the observability paradigm. Feel free to reach out if you'd like more details!
  312. **After (cleaned up):**
  313. > Acme Analytics, a Boulder-based startup, raised a $40M Series B led by Sequoia, with participation from Andreessen Horowitz, Y Combinator, and Index Ventures.
  314. >
  315. > Its observability platform gives engineering teams real-time dashboards, queries that run in under a second, and an integration layer. The platform helps engineers resolve incidents faster and supports collaboration across frontend, SRE, platform, and infrastructure teams. Customer adoption is accelerating. Acme has invested in R&D for years and plans to expand its go-to-market work in a crowded monitoring market.
  316. **Claims needing sources:** The input attributes a prediction about a $15B market to unnamed experts and a 40% improvement in issue identification to unspecified studies. Verify those sources before publishing the claims. The rewrite leaves them out; it does not replace them with invented benchmarks, customers, or plans. The product and adoption statements above also remain supplied claims, not independently verified facts.
  317. **What the skill caught:** chatbot framing, promotional modifiers, inflated significance, roundabout verbs, repeated role synonyms, empty contrast, list decoration, vague attribution, filler, and generic closing language. The rewrite preserves the named investors and supplied capabilities, consolidates the team list, and flags the unsupported market and study claims for verification.
  318. That's 35+ AI tells.
  319. ## Run the detector
  320. The skill ships a deterministic, zero-dependency detection engine in
  321. [`detector/`](./detector/) — the same engine the rules above
  322. describe, as runnable code. It works in Node (`>=18`) and the browser with no
  323. build step.
  324. It's also the single source of the numeric score: the skill itself (and `detect` mode) report *which* patterns are present and how severe (P0/P1/P2), and the engine is what turns those into one computed 0–100 `score`. There's deliberately no second, prose-estimated score in `SKILL.md` — one scorer, not two.
  325. ```bash
  326. npm install avoid-ai-writing-detector
  327. ```
  328. The npm package includes the detector runtime and CLI entry points, but not the
  329. repository's `scripts/` utilities. Clone the repository to run those scripts.
  330. ```js
  331. const AIDetector = require("avoid-ai-writing-detector");
  332. const { score, label, issues } = AIDetector.analyzeText("Your text here…");
  333. console.log(score, label, issues.length);
  334. ```
  335. ### Score a file or piped text from the command line
  336. The package also ships a zero-dependency CLI:
  337. ```bash
  338. npx --package avoid-ai-writing-detector avoid-ai-writing draft.md
  339. cat draft.md | npx --package avoid-ai-writing-detector avoid-ai-writing --context technical
  340. ```
  341. After a global install (`npm install -g avoid-ai-writing-detector`) the command
  342. is available as `avoid-ai-writing` directly.
  343. It prints the complete `analyzeText()` result as JSON and exits 0; usage and I/O
  344. errors go to stderr with exit code 2. Run `avoid-ai-writing --help` for the
  345. `--context` and `--source-mode` options.
  346. ### Gate prose in GitHub Actions or pre-commit
  347. The repository also ships a deterministic gate that fails on **finding count per
  348. file**, not the composite 0–100 score. That keeps CI policy independent of score
  349. recalibration work such as #70.
  350. [View Avoid AI Writing Gate on the GitHub Marketplace](https://github.com/marketplace/actions/avoid-ai-writing-gate),
  351. or add it directly to a workflow:
  352. ```yaml
  353. # .github/workflows/prose.yml
  354. steps:
  355. - uses: actions/checkout@v7
  356. - id: gate
  357. uses: conorbronsdon/avoid-ai-writing@v3.36.0
  358. with:
  359. glob: "**/*.md"
  360. threshold: "6"
  361. context: technical
  362. ```
  363. The example pins the release tag. For stricter supply-chain controls, pin
  364. `uses:` to the full commit SHA for that release.
  365. Step outputs and the gate CLI's `--json` flag require `v3.36.0` or later. On
  366. `v3.35.0` and earlier, `steps.gate.outputs.*` resolves to empty strings.
  367. The Action exposes step outputs via `$GITHUB_OUTPUT`:
  368. - `pass`: `'true'` when all scanned files are within threshold; `'false'` on a threshold failure or operational error.
  369. - `total-findings`: total count of deterministic findings across scanned files; unset on an operational error (exit 2).
  370. - `failed-files`: count of files exceeding the threshold; unset on an operational error (exit 2).
  371. Downstream steps can consume these outputs:
  372. ```yaml
  373. - name: Report gate summary
  374. if: always() && steps.gate.outputs.total-findings != ''
  375. run: |
  376. echo "Pass: ${{ steps.gate.outputs.pass }}"
  377. echo "Total findings: ${{ steps.gate.outputs.total-findings }}"
  378. echo "Failed files: ${{ steps.gate.outputs.failed-files }}"
  379. ```
  380. The underlying `avoid-ai-writing-gate` CLI also accepts `--json` to emit structured JSON on stdout:
  381. ```json
  382. {
  383. "schemaVersion": 1,
  384. "threshold": 6,
  385. "context": "technical",
  386. "sourceMode": "rendered-markdown",
  387. "pass": false,
  388. "totalFindings": 9,
  389. "failedFiles": 1,
  390. "files": [
  391. { "path": "README.md", "findings": 2, "pass": true, "types": ["em-dash", "tier1"] },
  392. { "path": "docs/guide.md", "findings": 7, "pass": false, "types": ["hedge-stack", "tier1", "tier2"] }
  393. ]
  394. }
  395. ```
  396. Top-level fields report `schemaVersion`, `threshold`, `context`, `sourceMode`, `pass` (boolean), `totalFindings`, `failedFiles`, and `files` (preserving scan order). Each file item reports `path`, `findings`, `pass`, and sorted distinct detector `types`. When no files match the input or glob, `files` is empty with `pass: true`.
  397. `threshold` is the maximum number of deterministic findings allowed in **each**
  398. file. The shipped default is **6**. The
  399. [dated gate calibration](evals/gate-calibration/2026-10-15.md) records the measured
  400. rejection counts, per-register limits and exact detector/corpus revision under
  401. the Action's `technical` + `rendered-markdown` settings. Set
  402. `threshold: "0"` explicitly when a project intentionally wants a strict
  403. zero-findings policy. This is a writing-quality baseline, not an authorship
  404. classifier calibration.
  405. The default context is `technical`, and Markdown is analyzed with
  406. `rendered-markdown` source masking.
  407. Pre-commit users can install the repository hook:
  408. ```yaml
  409. repos:
  410. - repo: https://github.com/conorbronsdon/avoid-ai-writing
  411. rev: main
  412. hooks:
  413. - id: avoid-ai-writing
  414. ```
  415. Pin `rev` to a release tag or commit SHA in shared repositories. The hook scans
  416. staged `.md` / `.mdx` files with the same **6-findings** corpus-backed default.
  417. Use `args` to override the threshold, context, or source mode:
  418. ```yaml
  419. - id: avoid-ai-writing
  420. args: ["--threshold", "0", "--context", "technical", "--source-mode", "plain", "--"]
  421. ```
  422. Pre-commit replaces the hook's default `args: ["--"]` when you provide `args`.
  423. End an overriding list with `"--"` to protect filenames that begin with a dash.
  424. The entry keeps its defaults; later options in `args` take precedence.
  425. The gate only **detects**. Preservation validation still requires an original and
  426. a rewritten file and remains a separate command:
  427. ```bash
  428. node detector/validate.js before.md after.md
  429. ```
  430. When working from a cloned checkout instead of the published npm package:
  431. ```bash
  432. npm test # run the detector's fixtures (no deps to install)
  433. ```
  434. ```js
  435. const AIDetector = require("./detector/patterns.js");
  436. const { score, label, issues } = AIDetector.analyzeText("Your text here…");
  437. ```
  438. When the input is a Markdown source file, pass
  439. `{ sourceMode: "rendered-markdown" }` to exclude initial YAML frontmatter and
  440. HTML comments from the score while keeping issue offsets aligned with the
  441. original file. Plain-text behavior remains the default.
  442. To keep a passage out of the score, such as a specimen of AI prose a page
  443. quotes on purpose, wrap it in ignore markers. They work in every source mode
  444. and in HTML source:
  445. ```html
  446. <!-- avoid-ai-writing:ignore-start -->
  447. It's not just a search index, it's a foundation for trust.
  448. <!-- avoid-ai-writing:ignore-end -->
  449. ```
  450. Each marker must sit on a line of its own. The markers and the text between
  451. them are blanked before scoring, and `stats.ignoredRegions` counts the regions.
  452. Markers inside code blocks, HTML `<pre>` or `<code>` elements, YAML
  453. frontmatter, inline prose, or quotations do nothing. Starts
  454. nest, so each needs its own end, and a start with no end runs to the end of the
  455. text. The
  456. detector reads source text, not rendered HTML, so an HTML-to-text step that
  457. drops comments also drops the markers.
  458. See [`detector/README.md`](./detector/README.md) for the full `analyzeText` API
  459. and [`detector/CATEGORIES.md`](./detector/CATEGORIES.md) for the rule ↔ category
  460. map that keeps `SKILL.md` and the engine in sync.
  461. ### Use the detector over MCP
  462. [`avoid-ai-writing-mcp`](https://github.com/conorbronsdon/avoid-ai-writing-mcp)
  463. wraps the published detector as a local stdio MCP server. It exposes two
  464. read-only tools: `score_text` for a compact result and `audit_text` for the full
  465. list of findings, suggestions, statistics, and highlighted sentence regions.
  466. ```bash
  467. claude mcp add avoid-ai-writing -- npx -y avoid-ai-writing-mcp@0.1.0
  468. ```
  469. The server calls no model and sends no text to a network service. It
  470. intentionally has no rewrite tool; rewriting stays in the skill, where the
  471. agent can apply the full editorial rules and preservation guardrails. See the
  472. [MCP repository](https://github.com/conorbronsdon/avoid-ai-writing-mcp) for
  473. configuration examples for other MCP hosts.
  474. The engine also ships a preservation validator. `detector/validate.js` checks
  475. protected content and structure, including code, frontmatter, blockquotes,
  476. tables, inline code, URLs, paths, and headings. The default gate also blocks
  477. residual pattern growth for compatibility; that quality signal does not prove
  478. content damage. Editorial workflows use the advisory policy:
  479. ```bash
  480. node detector/validate.js --residual-policy warn before.md after.md
  481. ```
  482. Mechanical errors still fail. Warnings and residual findings remain visible
  483. for review. A pass does not verify meaning; check facts and claims separately.
  484. See the [validator API](detector/README.md#validateoriginal-rewritten-options--result)
  485. for result fields, availability states, and the unchanged default gate.
  486. **Does it pass its own pass?** [`PROOF.md`](./PROOF.md) scores this repo's
  487. documentation with this repo's detector and publishes the result, including two
  488. defects the scan found in our own work. `npm run self-scan` reproduces it, and
  489. CI fails when a document drifts past its budget.
  490. ## Pre-publish pipeline
  491. This skill answers one question: does the draft sound like a model? Run [fact-check](https://github.com/conorbronsdon/fact-check?utm_source=github&utm_medium=referral&utm_campaign=repo-readme&utm_content=avoid-ai-writing) first to answer the other one: are the claims true? It pulls out the specific claims it can check (numbers, dates, names, quotes, firsts), tries to refute each against a primary source, and returns a verdict per claim. It flags and never edits, and a claim it couldn't settle is marked unverified, not passed.
  492. Run it before this skill, so the rewrite starts from claims you've already fixed. A rewrite can still change or add a claim, so check any claim that changed before you publish: the first pass covered the earlier draft, not the final text.
  493. Install both as Claude Code plugins from this repo's marketplace:
  494. ```text
  495. /plugin marketplace add conorbronsdon/avoid-ai-writing
  496. /plugin install avoid-ai-writing@conorbronsdon-skills
  497. /plugin install fact-check@conorbronsdon-skills
  498. ```
  499. For other agents, clone each repo into your skills directory, as in [Installation & Usage](#installation--usage).
  500. ## House style is a different job
  501. This skill removes AI-writing tells. Enforcing a published style guide is the
  502. different job: it doesn't do that, and it ships no style guides of its own. The
  503. optional `--style` input takes a house-style config you supply: a `register` list
  504. the model applies, and a `mechanics` object whose checkable rules
  505. `scripts/check-style.js` verifies deterministically (quote form, Latin
  506. abbreviations, and the `colons`, `semicolons`, and `emDash: "never"` bans gate the
  507. exit code; heading case, the `emDash: "sparing"` rate, and number spelling
  508. are advisory). This script requires a cloned checkout. [`examples/`](./examples/) has the schema. You can skip the input
  509. entirely and put your guide in your agent's context alongside a
  510. [voice profile](./references/patterns.md#voice-profiles), as instructions rather than as a checked
  511. rule set.
  512. From a cloned checkout, after a rewrite, `node scripts/normalize-quotes.js draft.md --reference original.md`
  513. prints prose marks normalized to the original document's convention; add `--write`
  514. to save it. Explicit `--quotes straight|curly` overrides inference. It shares the
  515. checker's Markdown protection. See the
  516. [usage and limits](./examples/README.md#normalize-quote-marks-after-a-rewrite).
  517. If you want Google, Microsoft, Red Hat, or Salesforce style checked in CI,
  518. [Vale](https://github.com/vale-cli/vale) already covers that. Its
  519. [package registry](https://github.com/vale-cli/packages) carries
  520. Vale-compatible implementations of those four, alongside ports of `proselint`,
  521. `write-good`, and `alex`. The four style-guide packages are MIT-licensed, though
  522. the guides they implement are not always (see the audit below); the linter ports
  523. vary, and proselint's is BSD-3-Clause. The two tools do different jobs and
  524. compose: Vale gates a document against a rule set, applying fixes one alert at a
  525. time, while this skill rewrites whole passages as you draft.
  526. Paywalled guides (Chicago, APA, MLA, AP) have no machine-readable
  527. implementation here or in Vale, and won't get one here. Nothing in this repo
  528. could verify that a rewrite is Chicago-compliant, so claiming it would fail the
  529. same bar [`PROOF.md`](./PROOF.md) holds every other number to. Passing one of
  530. their names to `--style` bundles nothing; it falls back to the model's own
  531. knowledge, and `SKILL.md` instructs it to say so and to claim no compliance.
  532. That is an instruction rather than a checked rule, which is the point: there is
  533. nothing here to check it against. The
  534. [license audit](https://github.com/conorbronsdon/avoid-ai-writing/issues/88)
  535. behind that line is public.
  536. ## More from me
  537. I run agents against my own email, money, and publishing, so I build the
  538. guardrails first.
  539. [Follow me on GitHub](https://github.com/conorbronsdon) for new tools and practical examples from maintaining them.
  540. - [What I'm building](https://conorbronsdon.com/builds/?utm_source=github&utm_medium=referral&utm_campaign=repo-readme&utm_content=avoid-ai-writing) — public projects across agent skills, MCP servers, creator tools, and Mojo.
  541. - [Chain of Thought](https://chainofthought.show/?utm_source=github&utm_medium=referral&utm_campaign=repo-readme&utm_content=avoid-ai-writing) — conversations with the people shipping AI systems.
  542. - [repo-audit](https://github.com/conorbronsdon/repo-audit) — checks whether repository claims are enforced, advisory, or guidance.
  543. ## Credits
  544. Pattern research informed by:
  545. - [Pangram Labs](https://www.pangram.com/) AI detection research — structural regularity insights, vocabulary flags from a decoder-only classifier trained on 28M human documents
  546. - Wikipedia's [Signs of AI-generated text](https://en.wikipedia.org/wiki/Wikipedia:Signs_of_AI_writing) documentation — the canonical reference for AI writing tells, maintained by Wikipedia editors
  547. - [blader/humanizer](https://github.com/blader/humanizer) Claude Code skill
  548. - [brandonwise/humanizer](https://github.com/brandonwise/humanizer) — tiered vocabulary system, statistical analysis research (burstiness, sentence length variation, trigram repetition), and rewrite philosophy
  549. - [OpenClaw](https://github.com/openclaw/openclaw) humanizer skill ecosystem — community patterns and vocabulary research
  550. Pull requests get an automated first-pass review from [Qodo Merge](https://github.com/marketplace/qodo-merge-pro-for-open-source), free through Qodo's open source program. Thanks to the Qodo team for supporting OSS maintainers.
  551. Authored by [Conor Bronsdon](https://github.com/conorbronsdon) · [LinkedIn](https://www.linkedin.com/in/conorbronsdon/) · [Chain of Thought podcast](https://chainofthought.show/?utm_source=github&utm_medium=referral&utm_campaign=repo-readme&utm_content=avoid-ai-writing)
  552. ## Community / Multilingual
  553. Community-maintained adaptations are linked here for discovery. Their authors own their rules, evidence, releases, and support; a link does not mean this repository maintains or validates their results.
  554. - **[avoid-ai-writing-multilingual](https://github.com/jurigis/avoid-ai-writing-multilingual)** by [Jürgen Kraus](https://github.com/jurigis) — German (`SKILL-DE.md`), French (`SKILL-FR.md`), Italian (`SKILL-IT.md`), Romanian (`SKILL-RO.md`), and Swedish (`SKILL-SV.md`) adaptations, grounded in native-language research rather than translated from English.
  555. - **[evitar-escrita-ia](https://github.com/mariorabeloneto/evitar-escrita-ia)** by [Mário Rabelo Neto](https://github.com/mariorabeloneto) — European Portuguese adaptation with dialect and formal-register guidance. Its vocabulary tables are editorial hypotheses, not corpus measurements.
  556. Built an adaptation? See the [language adaptation guidance](CONTRIBUTING.md#language-adaptations), then open an issue with the language, dialect, maintainer, evidence, limitations, and link. We can link independent versions without taking over their maintenance.
  557. ## Official sources and sponsorship
  558. I publish this project through these channels only:
  559. - This repository, [`conorbronsdon/avoid-ai-writing`](https://github.com/conorbronsdon/avoid-ai-writing), including its [releases](https://github.com/conorbronsdon/avoid-ai-writing/releases) and the Claude plugin marketplace it hosts
  560. - [ClawHub](https://clawhub.ai/conorbronsdon/avoid-ai-writing)
  561. - The [OpenAI Plugins Directory](https://chatgpt.com/plugins/plugins_6a9b77b18b8881918efa9c1255868164)
  562. - npm: [`avoid-ai-writing-detector`](https://www.npmjs.com/package/avoid-ai-writing-detector) and [`avoid-ai-writing-mcp`](https://www.npmjs.com/package/avoid-ai-writing-mcp)
  563. Skill directories and catalogs that copy `SKILL.md` from this repository are run by other people. They can fall behind the latest release, so compare the `version` in their copy's frontmatter with the [latest release](https://github.com/conorbronsdon/avoid-ai-writing/releases/latest). I don't control those pages and can't sell space on them.
  564. **No paid placements.** The skill, pattern catalog, detector, and docs contain no paid mentions, links, or product recommendations, and I decline requests to add them.
  565. **Sponsorship.** If this project saves you or your team time, you can support it through [GitHub Sponsors](https://github.com/sponsors/conorbronsdon). Company sponsors get a disclosed acknowledgment in this README. Sponsorship buys no say over rules, categories, detector output, or recommendations. Products whose main purpose is AI-text detection or getting past detectors aren't a fit, because the skill's flags are signals, not proof of authorship.
  566. ---
  567. ## Disclaimer
  568. *This is an independent personal project, not affiliated with, sponsored by, or endorsed by any company. All views expressed are my own.*
  569. ## License
  570. MIT
detector/patterns.js · Bundled script · 3,751 lines
  1. /**
  2. * Avoid AI Writing — detection engine (canonical source of truth)
  3. * Implements regex, structural, and stylometric pattern detection. This repo's SKILL.md
  4. * catalogs the human-editable pattern rules; this engine is the executable
  5. * expression of the regex-detectable subset and extends it with stylometric and
  6. * AI-tool-fingerprint detectors that don't make sense as skill prose
  7. * (cross-paragraph burstiness, smart-punct signatures, function-word
  8. * trigram entropy, low type-token ratio, AI-tool URL parameters,
  9. * chatbot citation markup leaks, unfilled placeholders).
  10. *
  11. * Scoring model:
  12. * Each category has a weight in the ISSUE_WEIGHTS table. Detection runs
  13. * produce raw (possibly duplicate) issues which are then deduplicated by
  14. * (type, text) pair. rawScore is the sum of category weights across the
  15. * deduped list — so the number reflects the same distinct signals the
  16. * user sees in the issue list.
  17. *
  18. * Weights are deliberately non-flat across severity tags. Cutoff
  19. * disclaimers (10) and chatbot artifacts (8) weigh more than vague
  20. * attributions (5), even though all three are tagged `critical`, because
  21. * the skill treats them as stronger or weaker AI-origin signals.
  22. *
  23. * rawScore is then normalized to 0-100 via `log2(wordCount/50)` so longer
  24. * texts don't accumulate unboundedly on the same density of patterns.
  25. */
  26. const AIDetector = (() => {
  27. // ═══ Tier 1 pre-pass: normalize bypass tricks ══════════════════════
  28. //
  29. // Humanizer tools and prompt-injection bypass techniques insert
  30. // invisible / lookalike chars to defeat exact-string detectors. Strip
  31. // them BEFORE pattern matching so "delve" with a Cyrillic 'е' still
  32. // hits the Tier 1 list. Unicode ranges sourced from
  33. // It-s-AI/llm-detection/detection/attacks/.
  34. //
  35. // Tracks what was stripped so the trinary classifier can use
  36. // "normalization triggered" as a corroborating AI signal — humans don't
  37. // paste ZWSPs into their own writing.
  38. const CYRILLIC_LOOKALIKES = {
  39. 'а': 'a', 'е': 'e', 'о': 'o', 'р': 'p', 'с': 'c', 'х': 'x',
  40. 'у': 'y', 'к': 'k', 'м': 'm', 'н': 'h', 'в': 'b', 'т': 't',
  41. 'А': 'A', 'Е': 'E', 'О': 'O', 'Р': 'P', 'С': 'C', 'Х': 'X',
  42. 'У': 'Y', 'К': 'K', 'М': 'M', 'Н': 'H', 'В': 'B', 'Т': 'T',
  43. };
  44. const GREEK_LOOKALIKES = { 'ο': 'o', 'Ο': 'O', 'α': 'a', 'Α': 'A', 'ρ': 'p', 'Ρ': 'P' };
  45. // ─── Source-coordinate mapping (issue #189) ─────────────────────────
  46. //
  47. // Each entry maps one code unit in the working string to the matching
  48. // code unit in the caller's source. Deletion passes copy the entries for
  49. // retained characters once, so interleaved or overlapping removals cannot
  50. // double-count offsets. Masking and homoglyph replacement keep their input
  51. // length and therefore keep the current map unchanged.
  52. function identitySourceMap(length) {
  53. return Array.from({ length }, (_, index) => index);
  54. }
  55. function appendMapRange(target, source, start, end) {
  56. for (let index = start; index < end; index += 1) target.push(source[index]);
  57. }
  58. function remapFindingsToSource(issues, regions, sourceMap) {
  59. for (const issue of issues) {
  60. if (Number.isInteger(issue.index)) issue.index = sourceMap[issue.index];
  61. }
  62. for (const region of regions) {
  63. region.start = sourceMap[region.start];
  64. region.end = sourceMap[region.end - 1] + 1;
  65. }
  66. }
  67. const ZERO_WIDTH_RE = /[​-‍⁠]/u;
  68. const ZERO_WIDTH_GLOBAL_RE = /[​-‍⁠]/gu;
  69. const HOMOGLYPH_RE = /[Ѐ-ӿͰ-Ͽ]/u;
  70. const HOMOGLYPH_GLOBAL_RE = /[Ѐ-ӿͰ-Ͽ]/gu;
  71. const LATIN_LETTER_RE = /\p{Script=Latin}/u;
  72. // Dates, versions and ASCII filenames keep internal dots; prose may hard-wrap.
  73. // Other sentence punctuation and Markdown/HTML block starts separate units, even
  74. // without a space, so unrelated Russian text cannot hide an English word.
  75. const SENTENCE_UNIT_GLOBAL_RE = /(?:[^.!?\r\n]|(?<=[A-Za-z0-9_])\.(?=[A-Za-z0-9_])|\r?\n(?![ \t]*(?:\r?\n|(?:[-*+]|#{1,6}|\d+[.)])\s|[><|]|`{3}|~{3}|(?:(?:-+|=+)[\t ]*|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})(?:\r?\n|$)|:?-+:?[\t ]*(?:\|[\t ]*:?-+:?[\t ]*)+(?:\|[\t ]*)?(?:\r?\n|$))))+/g;
  76. // Words are letter runs; a hyphen splits them, so "API-сервис" stays two
  77. // words and its Russian half is not swapped.
  78. const LETTER_RUN_GLOBAL_RE = /[\p{L}\p{M}]+/gu;
  79. const ROLEPLAY_VERBS_RE = /^(?:nods|sighs|laughs|smiles|frowns|shrugs|grins|winks|chuckles|gasps|pauses|thinks|wonders|whispers|shouts|gestures|raises|leans|turns|looks|glances|smirks|blinks|nodding|sighing|laughing|smiling|thinking|gesturing)\b/i;
  80. const ROLEPLAY_MARKER_RE = /(?<!\*)\*([^*\n]{1,80}?)\*(?!\*)/gu;
  81. // A word joiner (U+2060) only counts as a bypass character when it splits a
  82. // word: letters on both sides, the shape a humanizer uses to break "delve"
  83. // apart. Show-notes and CMS editors insert word joiners next to URLs and
  84. // punctuation to control line breaking (#351); those are still stripped so
  85. // matching sees clean text, but they are typesetting, not AI evidence.
  86. // Neighbours are read past a short adjacent zero-width run, so a doubled
  87. // joiner inside a word still counts. The bound keeps a long run linear.
  88. const LETTER_END_RE = /\p{L}$/u;
  89. const LETTER_START_RE = /^\p{L}/u;
  90. const MAX_ZERO_WIDTH_SKIP = 8;
  91. function countsAsBypass(chars, i) {
  92. if (chars[i] !== '\u2060') return true;
  93. let before = i - 1;
  94. while (before >= 0 && i - before <= MAX_ZERO_WIDTH_SKIP && ZERO_WIDTH_RE.test(chars[before])) before -= 1;
  95. let after = i + 1;
  96. while (after < chars.length && after - i <= MAX_ZERO_WIDTH_SKIP && ZERO_WIDTH_RE.test(chars[after])) after += 1;
  97. // A two-unit slice lets the Unicode regex see a supplementary-plane letter.
  98. return LETTER_END_RE.test(chars.slice(Math.max(0, before - 1), before + 1))
  99. && LETTER_START_RE.test(chars.slice(after, after + 2));
  100. }
  101. function normalizeText(text, sourceMap) {
  102. const flags = { zeroWidth: 0, homoglyph: 0, roleplay: 0 };
  103. let out = text;
  104. let map = Array.isArray(sourceMap) ? sourceMap : null;
  105. // 1. Strip zero-width chars (ZWSP U+200B, ZWNJ U+200C, ZWJ U+200D,
  106. // BOM U+FEFF, word joiner U+2060). Word joiners outside a word are
  107. // stripped without being counted; see countsAsBypass.
  108. if (map) {
  109. const chars = [];
  110. const nextMap = [];
  111. for (let i = 0; i < out.length; i += 1) {
  112. if (ZERO_WIDTH_RE.test(out[i])) {
  113. if (countsAsBypass(out, i)) flags.zeroWidth += 1;
  114. continue;
  115. }
  116. chars.push(out[i]);
  117. nextMap.push(map[i]);
  118. }
  119. out = chars.join('');
  120. map = nextMap;
  121. } else {
  122. out = out.replace(ZERO_WIDTH_GLOBAL_RE, (_, offset, whole) => {
  123. if (countsAsBypass(whole, offset)) flags.zeroWidth += 1;
  124. return '';
  125. });
  126. }
  127. // 2. Swap Cyrillic / Greek Latin-lookalike chars back to Latin so
  128. // pattern matching catches obfuscated tokens. Swapping every а, е, о
  129. // reported thousands of "homoglyph swaps" on plain Russian text, and
  130. // swapping inside ordinary Russian words flagged bilingual technical
  131. // text, so only two word shapes are swapped:
  132. // - mixed-script words ("pаypal", "dеlve"), anywhere;
  133. // - words of two or more letters spelled entirely in lookalike
  134. // letters ("аст" for "act"), when the sentence around them has no
  135. // Russian or Greek in it: every Cyrillic and Greek letter in the
  136. // sentence is a lookalike. Deciding per sentence, not per document,
  137. // keeps Russian padding from hiding such a word in an English
  138. // sentence.
  139. // Sentence punctuation, blank lines and Markdown block starts separate units.
  140. // Internal ASCII-token dots ("07.10", "v1.2", "report.pdf") and a prose
  141. // hard wrap do not end it: cutting there left pieces such as "поста в
  142. // r/StableDiffusion от 07" that looked like Latin text.
  143. // Ordinary Russian words contain non-lookalike letters (з, п, и, л),
  144. // so short Russian words in Russian technical prose ("на Enum",
  145. // "от 07.10") stay put, and one-letter prepositions (с, о, у) are too
  146. // short.
  147. // Known limits, because no letter-level rule separates these from
  148. // real Russian: a fully substituted word inside or next to Russian
  149. // words ("МЕТА поможет", "пароль: аст"), including across prose hard wraps,
  150. // is left alone; a one-letter
  151. // lookalike split off by a hyphen ("а-ct") is left alone; and a short
  152. // Russian sentence spelled only in lookalike letters ("Он сам.") is
  153. // swapped.
  154. const lookalikeFor = (m) => CYRILLIC_LOOKALIKES[m] ?? GREEK_LOOKALIKES[m];
  155. const swapLookalike = (m) => {
  156. const swap = lookalikeFor(m);
  157. if (swap) { flags.homoglyph++; return swap; }
  158. return m;
  159. };
  160. const letterCount = (word) => (word.match(/\p{L}/gu) || []).length;
  161. const allLookalike = (word) => letterCount(word) >= 2
  162. && [...word].every((ch) => lookalikeFor(ch) || !/\p{L}/u.test(ch));
  163. out = out.replace(SENTENCE_UNIT_GLOBAL_RE, (unit) => {
  164. const scriptLetters = unit.match(HOMOGLYPH_GLOBAL_RE) || [];
  165. const onlyLookalikes = scriptLetters.length > 0 && scriptLetters.every((ch) => lookalikeFor(ch));
  166. const words = [...unit.matchAll(LETTER_RUN_GLOBAL_RE)];
  167. let result = '';
  168. let last = 0;
  169. words.forEach((match) => {
  170. const word = match[0];
  171. let next = word;
  172. if (HOMOGLYPH_RE.test(word)) {
  173. if (LATIN_LETTER_RE.test(word) || (allLookalike(word) && onlyLookalikes)) {
  174. next = word.replace(HOMOGLYPH_GLOBAL_RE, swapLookalike);
  175. }
  176. }
  177. result += unit.slice(last, match.index) + next;
  178. last = match.index + word.length;
  179. });
  180. return result + unit.slice(last);
  181. });
  182. // 3. Strip *roleplay-action* markers — paired *...* containing an
  183. // action verb (nods, sighs, laughs, smiles, etc.) anchored to
  184. // the start of the inner phrase. This is the actual chat-model
  185. // artifact shape. Markdown `**bold**` is rejected by the
  186. // lookbehind/lookahead; legitimate multi-word `*italic*` is
  187. // preserved because the verb whitelist is narrow.
  188. if (map) {
  189. const chars = [];
  190. const nextMap = [];
  191. const matcher = new RegExp(ROLEPLAY_MARKER_RE.source, ROLEPLAY_MARKER_RE.flags);
  192. let cursor = 0;
  193. let match;
  194. while ((match = matcher.exec(out)) !== null) {
  195. if (!ROLEPLAY_VERBS_RE.test(match[1])) continue;
  196. chars.push(out.slice(cursor, match.index));
  197. appendMapRange(nextMap, map, cursor, match.index);
  198. flags.roleplay += 1;
  199. cursor = match.index + match[0].length;
  200. }
  201. chars.push(out.slice(cursor));
  202. appendMapRange(nextMap, map, cursor, map.length);
  203. out = chars.join('');
  204. map = nextMap;
  205. } else {
  206. out = out.replace(ROLEPLAY_MARKER_RE, (m, inner) => {
  207. if (ROLEPLAY_VERBS_RE.test(inner)) {
  208. flags.roleplay += 1;
  209. return '';
  210. }
  211. return m;
  212. });
  213. }
  214. return map ? { text: out, flags, sourceMap: map } : { text: out, flags };
  215. }
  216. // Terms with legitimate technical meaning that are suppressed when contextMode === 'technical'.
  217. // See references/patterns.md and issue #237.
  218. const TECHNICAL_EXEMPT = new Set([
  219. 'robust',
  220. 'comprehensive',
  221. 'seamless',
  222. 'seamlessly',
  223. 'ecosystem',
  224. 'leverage',
  225. 'leverages',
  226. 'leveraging',
  227. 'leveraged',
  228. 'facilitate',
  229. 'facilitates',
  230. 'underpin',
  231. 'underpinning',
  232. 'underpinnings',
  233. 'streamline',
  234. ]);
  235. // ─── Tier 1: Always flag ───────────────────────────────────────────
  236. const TIER1 = {
  237. 'delve': 'explore, dig into, look at',
  238. 'tapestry': 'complex structure, rich history',
  239. 'paradigm': 'model, approach, framework',
  240. 'beacon': 'rewrite entirely',
  241. 'robust': 'strong, reliable, solid',
  242. 'comprehensive': 'thorough, complete, full',
  243. 'cutting-edge': 'latest, newest, advanced',
  244. 'pivotal': 'important, key, critical',
  245. 'meticulous': 'careful, detailed, precise',
  246. 'meticulously': 'carefully, precisely',
  247. 'seamless': 'smooth, easy, without friction',
  248. 'seamlessly': 'smoothly, easily',
  249. 'game-changer': 'changes how [X] works',
  250. 'game-changing': 'changes how [X] works',
  251. 'nestled': 'is located, sits',
  252. 'vibrant': 'lively, active',
  253. 'thriving': 'growing, active',
  254. 'bustling': 'busy, active',
  255. 'intricate': 'complex, detailed',
  256. 'intricacies': 'complexities, details',
  257. 'ever-evolving': 'changing, growing',
  258. 'enduring': 'lasting, long-running',
  259. 'daunting': 'hard, difficult',
  260. 'holistic': 'complete, full, whole',
  261. 'holistically': 'completely, fully',
  262. 'actionable': 'practical, useful, concrete',
  263. 'impactful': 'effective, significant',
  264. 'learnings': 'lessons, findings, takeaways',
  265. 'synergy': 'combined effect, benefit of working together',
  266. 'synergies': 'combined effect, benefit of working together',
  267. 'interplay': 'relationship, connection',
  268. 'symphony': 'coordination, balance',
  269. 'embrace': 'adopt, accept, use',
  270. };
  271. // Multi-word tier 1 phrases
  272. // "features" is copula avoidance only as a verb: "the app features a
  273. // dashboard". The plural noun ("three new features", a "Features" heading,
  274. // "the features we shipped") is ordinary product and technical writing, and
  275. // it is most of what the word does in corpus/ (#351). A verb needs a subject
  276. // before it and an object after it, so the word counts as a noun when it
  277. // opens a sentence, line or list item; follows a determiner, number,
  278. // possessive, quantifier, "of", "or" or a compound modifier; or comes
  279. // before punctuation, a line break, a preposition, a conjunction, a pronoun
  280. // or a verb.
  281. // Singular "this", "that" and "each" cannot determine the plural noun:
  282. // here they are subjects ("each features a...") or a relative pronoun.
  283. const FEATURES_NOUN_BEFORE_RE = /(?:^|[.!?:;(]\s*|(?:^|\n)[ \t]*(?:[-*+>#]+[ \t]*)?|\b(?:the|a|an|these|those|its|their|our|your|my|his|her|of|or|new|key|main|core|top|other|more|most|many|several|some|all|any|few|no|both|only|about|over|just|free|similar|common|specific|distinct|additional|extra|premium|advanced|basic|best|unique|missing|upcoming|existing|latest|major|minor|useful|important|various|different|certain|such|\d+|two|three|four|five|six|seven|eight|nine|ten|whose)\s+|\w['’]s\s+|\w[-/]\w+\s+)$/i;
  284. // "on-device" is an object modifier, not the preposition "on".
  285. const FEATURES_NOUN_AFTER_RE = /^(?:[ \t]*(?:[.,;:!?()[\]"'’”\n]|$)|\s+(?:of|for|in|on|at|into|and|or|but|nor|are|were|is|was|be|been|being|that|which|who|whose|like|such|to|from|with|without|than|help|helps|helped|let|allow|make|keep|give|provide|enable|offer|save|protect|ensure|improve|reduce|support|remain|vary|need|unlock|bring|include|includes|included|work|works|worked|will|can|could|should|would|may|might|must|do|did|does|have|has|had|we|you|they|i|it)(?![\w-]))/i;
  286. // Bare product objects: "we ship features", "teams build features".
  287. // A subject is required so noun subjects such as "the ship"/"the build"
  288. // still flag. One optional modifier preserves "we ship GPT-5 features".
  289. const FEATURES_NOUN_OBJECT_RE = /\b(?:i|we|you|they|teams?|developers?|engineers?|users?|companies|vendors|the\s+(?:release|update))\s+(?:(?:can|could|will|would|should|must)\s+)?(?:ship|ships|shipped|shipping|build|builds|built|building|add|adds|added|adding)\s+(?:[\w./-]+\s+)?$/i;
  290. // Leads that make "which/what features" an indirect question (#384): a question
  291. // verb that opens its clause ("Decide which...", "we should check which..."),
  292. // or which/what at the start of a sentence or list item. A lead word inside a
  293. // noun phrase ("a security check which features...") does not count.
  294. const FEATURES_QUESTION_LEAD_RE = /(?:^|[.!?:;]\s*|(?:^|\n)[ \t]*(?:[-*+>#]+[ \t]*)?|(?:^|[.!?:;,]\s*|(?:^|\n)[ \t]*(?:[-*+>#]+[ \t]*)?|\b(?:i|we|you|they|he|she|let['’]?s|to|can|could|should|must|will|would|please|just|then|first|now|and|or|but|so)\s+)(?:decide|decides|deciding|choose|choosing|pick|check|checking|see|know|knowing|learn|find\s+out|figure\s+out|work\s+out|identify|determine|ask|asking|asked|tell\s+(?:me|us|them|you)|show\s+(?:me|us|them|you)|understand|explain|wonder|discover|confirm|consider|evaluate|prioritize|about)\s+)(?:which|what)\s+$/i;
  295. // A lead word after a determiner is a noun ("a track which features...",
  296. // "the review which features..."), not a question verb.
  297. const FEATURES_LEAD_AS_NOUN_RE = /\b(?:a|an|the|this|that|these|those|my|our|your|their|its|his|her|each|every)\s+[\w-]+\s+(?:which|what)\s+$/i;
  298. // Subjects that definitively make "features" a verb, avoiding ambiguity with
  299. // plural-noun subjects like "The experimental features" or "Security features".
  300. const FEATURES_VERB_SUBJECTS = /\b(?:library|tool|app|application|platform|system|language|service|product|update|release|version|site|website|game|device|model|framework|package|plugin|extension|which|what|that|who|it|he|she)\s+$/i;
  301. function featuresIsNoun(text, index) {
  302. const end = index + 'features'.length;
  303. const before = text.slice(Math.max(0, index - 40), index);
  304. const after = text.slice(end, end + 40);
  305. if (FEATURES_NOUN_OBJECT_RE.test(before)) return true;
  306. // A recognizable model/version followed by an object article is a
  307. // subject: "GPT-5 features a...", "Windows 11 features a...".
  308. // Determiner-led counts ("These 3 features a customer requested") and
  309. // the explicit object contexts above remain nouns. This is a bounded
  310. // context heuristic, not a grammatical parse of every use of the word.
  311. const product = /\b([A-Z][\w]*)(?:[-/]\d[\w.-]*|[ \t]+\d+(?:\.\d+)*)[ \t]+$/.exec(before);
  312. if (product && !FEATURES_NOUN_BEFORE_RE.test(`${product[1]} `)
  313. && /^\s+(?:a|an|the)(?![\w-])/i.test(after)) return false;
  314. // A short comma-delimited aside can precede the verb's article-led
  315. // object. Strong noun contexts still take priority over punctuation.
  316. if (!FEATURES_NOUN_BEFORE_RE.test(before)
  317. && /^[ \t]*,[^,\n]{1,80},\s*(?:a|an|the)(?![\w-])/i.test(text.slice(end, end + 110))) return false;
  318. // "which/what features" is a noun in an indirect question: right after a
  319. // question verb ("Decide which features matter") or at the start of a
  320. // sentence ("Which features matter most..."). Every other context falls
  321. // through to the checks below unchanged, so relative clauses ("a library
  322. // which features dashboards") keep their verb finding (#384).
  323. if (FEATURES_QUESTION_LEAD_RE.test(before) && !FEATURES_LEAD_AS_NOUN_RE.test(before)) {
  324. // If there is no trailing verb (e.g. end of clause), it's the verb case ("Check what features a dashboard and export tools.").
  325. // Look past a compound subject and optional appositive for a trailing verb before returning the verb classification.
  326. const match = /^\s+(?:a|an|the)\s+[\w-]+\s*(?:(?:and\b|or\b)\s+(?:a\s+|an\s+|the\s+)?(?:[\w-]+\s+){0,2}[\w-]+\s*)?(?:([,.!?;])|$)/i.exec(after);
  327. if (match) {
  328. const predicateRe = /\b(?:can|could|should|will|would|may|might|must|is|are|was|were|has|have|had|do|does|did|include|includes|disable|disables|reject|accept|allow|deny|provide|require|use|make|work|help|give|take|need|become|seem|look|show|offer|support|supports|fail|pass|lack|prefer|choose|prevent|stop)\b/i;
  329. // If the matched subject ends with a predicate right before the punctuation,
  330. // the predicate was consumed as part of the compound subject.
  331. const subjectText = match[0].slice(0, match[1] ? -match[1].length : undefined).trim();
  332. const lastWord = subjectText.split(/\s+/).pop();
  333. if (predicateRe.test(lastWord)) {
  334. return true;
  335. }
  336. if (match[1] === ',') {
  337. // If the comma leads into a phrase, limit noun classification to a predicate
  338. // belonging to the article-led subject (after the next comma), excluding predicates inside subordinate clauses.
  339. const remainder = text.slice(end + match[0].length, end + 120);
  340. // An adverb can sit between the closing comma and the predicate.
  341. const remainderPredicateRe = new RegExp(`^[^,]+,\\s*(?:(?:[\\w-]+ly|also|not|never)\\s+){0,2}${predicateRe.source}`, 'i');
  342. if (remainderPredicateRe.test(remainder)) {
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. return true;
  349. }
  350. // "The library features support for..." is a verb.
  351. // "Security features support for..." is a plural noun subject + verb.
  352. if (FEATURES_VERB_SUBJECTS.test(before) && /^\s+support\s+for\b/i.test(after)) {
  353. // A nearby quantifier in an earlier clause cannot make a singular pronoun plural.
  354. if (/\b(?:it|he|she)\s+$/i.test(before)) return false;
  355. const hasPluralDeterminer = /\b(?:these|those|all|some|many|few|various|multiple|several|both)\s+(?:[\w-]+\s+){1,2}$/i.test(before);
  356. // Count words modify "features" only when they follow its determiner.
  357. // A digit inside a product name ("This Windows 11 app") or a date before
  358. // the subject ("In 2024 the library") is not a plural count.
  359. const hasPluralCount = /\b(?:the|a|an|this|that|my|our|your|their|its|his|her|each|every)\s+(?:(?:\d{1,2}|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|dozen)\s+(?:[\w-]+\s+){1,2}|(?!(?:19|20)\d{2}\s)\d{3,}\s+(?!(?:[\w-]+\s+)?(?:app|release|model|version|update)\s+$)(?:[\w-]+\s+){1,2})$/i.test(before);
  360. const extendedAfter = text.slice(end, end + 100);
  361. // A predicate inside a relative clause modifying the object (e.g. "teams that connect and remain online")
  362. // is not coordinated with the main subject. Limit predicate search to the direct prepositional object.
  363. const hasPartitivePlural = /\b(?:all|some|many|most|few|several|both|\d+|two|three|four|five|six|seven|eight|nine|ten)\s+of\s+(?:the|my|our|your|their|its|his|her)\s+(?:[\w-]+\s+){1,3}$/i.test(before);
  364. const hasPluralPredicate = /^\s+support\s+for(?:(?!\b(?:that|which|who|whom|whose|where|when)\b)[^.?!;]){0,80}?\b(?:and|but|or|nor)\s+(?:are|were|have|do|reject|accept|allow|deny|provide|require|use|make|work|help|give|take|need|become|seem|look|show|include|offer|support|remain|fail|pass|lack|prefer|choose|prevent|stop)\b/i.test(extendedAfter);
  365. const hasForObject = /^\s+support\s+for(?:-|\s+)(?:profit|loops?)\b/i.test(after);
  366. // Numeric modifiers and plural cues exclude a singular-subject reading:
  367. // "The two platform features..." has plural noun "features".
  368. // Do not borrow a determiner across a preposition or conjunction:
  369. // "a suite of system features" still has plural noun "features".
  370. // "that" can introduce a clause and "which" can determine plural nouns.
  371. // A relative pronoun needs its own determiner-led subject evidence.
  372. const hasUnambiguousSingular = /\b(?:a|an|this|that|every|each|one)\s+(?:(?!(?:of|for|in|with|and|or|which|what|that|who|two|three|four|five|six|seven|eight|nine|ten|multiple|several|many|few|both|various)\b)[\w-]+\s+){1,3}$/i.test(before)
  373. || /\b(?:it|he|she)\s+$/i.test(before)
  374. || /\b(?:a|an|this|that|each|every|one)\s+[\w-]+\s+(?:which|that|who)\s+$/i.test(before);
  375. const hasSingularEvidence = hasUnambiguousSingular
  376. || /\b(?:the|my|our|your|their|its|his|her)\s+(?:(?!(?:of|for|in|with|and|or|which|what|that|who|two|three|four|five|six|seven|eight|nine|ten|multiple|several|many|few|both|various)\b)[\w-]+\s+){1,3}$/i.test(before)
  377. || /\b(?:the|my|our|your|their|its|his|her)\s+[\w-]+\s+(?:which|that|who)\s+$/i.test(before);
  378. // Unambiguous singular subjects ("It", "This app") cannot be plural-noun phrases,
  379. // so hasForObject ("support for loops") only suppresses ambiguous determiners ("The library").
  380. if (!hasPluralDeterminer && !hasPluralCount && !hasPartitivePlural && !hasPluralPredicate && (hasUnambiguousSingular || (!hasForObject && hasSingularEvidence))) {
  381. return false;
  382. }
  383. }
  384. return FEATURES_NOUN_BEFORE_RE.test(before) || FEATURES_NOUN_AFTER_RE.test(after);
  385. }
  386. const TIER1_PHRASES = [
  387. { pattern: /\bdelve\s+into\b/gi, replace: 'explore, dig into' },
  388. { pattern: /\blandscape\b/gi, replace: 'field, space, industry', filter: true },
  389. { pattern: /\brealm\b/gi, replace: 'area, field, domain' },
  390. { pattern: /\btestament\s+to\b/gi, replace: 'shows, proves' },
  391. { pattern: /\bleverag(?:e|es|ing|ed)\b/gi, replace: 'use' },
  392. { pattern: /\bwatershed\s+moment\b/gi, replace: 'turning point, shift' },
  393. { pattern: /\bmarking\s+a\s+pivotal\s+moment\b/gi, replace: 'changing [X], leading to [Y]' },
  394. { pattern: /\bthe\s+future\s+looks\s+bright\b/gi, replace: 'cut or say something specific' },
  395. { pattern: /\bonly\s+time\s+will\s+tell\b/gi, replace: 'cut or say something specific' },
  396. { pattern: /\bdespite\s+challenges[^.]*continues?\s+to\s+thrive\b/gi, replace: 'keeps growing despite [named difficulty], continues to succeed despite [named difficulty]' },
  397. { pattern: /\bdeep\s+dive\b/gi, replace: 'look at, examine' },
  398. { pattern: /\bdive\s+into\b/gi, replace: 'look at, examine' },
  399. { pattern: /\bunpack(?:ing)?\b/gi, replace: 'explain, break down' },
  400. { pattern: /\bcomplexities\b/gi, replace: 'problems, details' },
  401. { pattern: /\bthought\s+leader(?:ship)?\b/gi, replace: 'expert, authority' },
  402. { pattern: /\bbest\s+practices\b/gi, replace: 'what works, proven methods' },
  403. { pattern: /\bat\s+its\s+core\b/gi, replace: 'cut, just state it' },
  404. { pattern: /\bin\s+order\s+to\b/gi, replace: 'to', clarity: true },
  405. { pattern: /\bdue\s+to\s+the\s+fact\s+that\b/gi, replace: 'because', clarity: true },
  406. { pattern: /\bserves\s+as\b/gi, replace: 'is', clarity: true },
  407. { pattern: /\bfeatures\b/gi, replace: 'has, includes', filter: true, clarity: true, skip: featuresIsNoun },
  408. { pattern: /\bboasts\b/gi, replace: 'has', clarity: true },
  409. { pattern: /\butiliz(?:e|es|ing|ed)\b/gi, replace: 'use', clarity: true },
  410. { pattern: /\bshowcas(?:e|es|ing|ed)\b/gi, replace: 'show, demonstrate' },
  411. { pattern: /\bembark(?:s|ing|ed)?\b/gi, replace: 'start, begin' },
  412. { pattern: /\bcommenc(?:e|es|ing|ed)\b/gi, replace: 'start, begin', clarity: true },
  413. { pattern: /\bascertain(?:s|ing|ed)?\b/gi, replace: 'find out, determine', clarity: true },
  414. { pattern: /\bendeavou?r(?:s|ing|ed)?\b/gi, replace: 'effort, attempt, try', clarity: true },
  415. { pattern: /\bunderscor(?:es|ing|ed)\b/gi, replace: 'highlights, shows' },
  416. // Hyphen required. The unhyphenated "load bearing" is ordinary English —
  417. // "the load bearing down on the bridge" — where `bearing` is a participle,
  418. // not part of a compound modifier. The tell is always hyphenated.
  419. //
  420. // Only match an immediately following abstract noun from this seed list.
  421. // Unknown nouns, mixed physical/abstract nouns, and predicative uses pass:
  422. // precision over recall (#56). Keep the lookahead out of the matched span.
  423. { pattern: /\bload-bearing\b(?=[ \t]+(?:assumptions?|claims?|invariants?|premises?|constraints?|dependenc(?:y|ies)|arguments?|abstractions?)\b)/gi, replace: 'essential, critical, or say what breaks if you remove it' },
  424. ];
  425. // ─── Tier 2: Flag in clusters (2+ per paragraph) ──────────────────
  426. const TIER2 = {
  427. 'harness': 'use, take advantage of',
  428. 'navigate': 'work through, handle',
  429. 'navigating': 'working through, handling',
  430. 'foster': 'encourage, support, build',
  431. 'elevate': 'improve, raise, strengthen',
  432. 'unleash': 'release, enable, unlock',
  433. 'streamline': 'simplify, speed up',
  434. 'empower': 'enable, let, allow',
  435. 'bolster': 'support, strengthen',
  436. 'spearhead': 'lead, drive, run',
  437. 'resonate': 'connect with, appeal to',
  438. 'resonates': 'connects with, appeals to',
  439. 'revolutionize': 'change, transform',
  440. 'facilitate': 'enable, help, allow',
  441. 'facilitates': 'enables, helps, allows',
  442. 'underpin': 'support, form the basis of',
  443. 'nuanced': 'specific, subtle, detailed',
  444. 'crucial': 'important, key, necessary',
  445. 'multifaceted': 'having several parts, with several aspects',
  446. 'ecosystem': 'system, community, network',
  447. 'myriad': 'many, numerous',
  448. 'plethora': 'many, a lot of',
  449. 'encompass': 'include, cover, span',
  450. 'catalyze': 'start, trigger, accelerate',
  451. 'reimagine': 'rethink, redesign, rebuild',
  452. 'galvanize': 'motivate, rally, push',
  453. 'augment': 'add to, expand, supplement',
  454. 'cultivate': 'build, develop, grow',
  455. 'illuminate': 'clarify, explain, show',
  456. 'elucidate': 'explain, clarify',
  457. 'juxtapose': 'compare, contrast',
  458. 'transformative': 'changes how [X] works',
  459. 'transformation': 'major change, overhaul',
  460. 'cornerstone': 'foundation, basis, key part',
  461. 'paramount': 'most important, top priority',
  462. 'poised': 'ready, set, about to',
  463. 'burgeoning': 'growing, emerging',
  464. 'nascent': 'new, early-stage',
  465. 'quintessential': 'typical, classic, defining',
  466. 'overarching': 'main, central, broad',
  467. 'quietly': 'cut, or name the concrete contrast',
  468. 'underpinning': 'basis, foundation',
  469. 'underpinnings': 'basis, foundations',
  470. 'paradigm-shifting': 'changes the basic model, changes how [X] is understood',
  471. };
  472. // Conditional Tier 2 entries: everyday words whose AI tell is a specific
  473. // significance collocation, not the word itself. They join a paragraph's
  474. // Tier 2 cluster only when the collocation matches — bare uses ("deeply
  475. // nested JSON", "cares deeply") never count, because the base rate of
  476. // these words in innocent prose is far higher than the rest of the table
  477. // and an unconditional entry measurably flags clean human writing.
  478. const TIER2_CONDITIONAL = [
  479. {
  480. word: 'deeply',
  481. pattern: /\bdeeply\s+(?:integrated|committed|rooted|personal|human|flawed|resonant|transformative|interconnected|ingrained|embedded|meaningful)\b/i,
  482. suggestion: 'cut, or name what specifically runs deep',
  483. },
  484. ];
  485. // ─── Tier 3: Flag by density ───────────────────────────────────────
  486. const TIER3 = [
  487. 'significant', 'significantly', 'innovative', 'innovation',
  488. 'effective', 'effectively', 'dynamic', 'dynamics',
  489. 'scalable', 'scalability', 'compelling', 'unprecedented',
  490. 'exceptional', 'exceptionally', 'remarkable', 'remarkably',
  491. 'sophisticated', 'instrumental',
  492. 'world-class', 'state-of-the-art', 'best-in-class',
  493. // `verbatim` is usually redundant with the verb it modifies ("copies X
  494. // verbatim" = "copies X"). It has a genuine term-of-art sense in legal,
  495. // research, and QA registers ("verbatim transcript"), so it lives at Tier 3:
  496. // density-gated, it only fires on overuse, not on a single legitimate use.
  497. 'verbatim',
  498. ];
  499. // Multi-word Tier 3 phrases. Density-gated like single Tier 3 words because
  500. // these legitimately show up in human crypto/web3/dev writing. Threshold is
  501. // intentionally lower than single-word Tier 3 (≥2 occurrences of the same
  502. // phrase) — repeating the same multi-word boilerplate is a stronger AI tell
  503. // than re-using "significant."
  504. const TIER3_PHRASES = [
  505. /\bemerging\s+(?:sector|space|category|industry)\b/gi,
  506. /\bthe\s+integration\s+of\b/gi,
  507. /\bthe\s+intersection\s+of\b/gi,
  508. /\bcommunity-?driven\b/gi,
  509. /\blong-?term\s+sustainability\b/gi,
  510. /\buser\s+engagement\b/gi,
  511. /\bdecentralized\s+compute\b/gi,
  512. /\b(?:sustainable\s+)?reward\s+emissions?\b/gi,
  513. /\btokenized\s+incentive\s+structures?\b/gi,
  514. /\bdesigned\s+for\s+long-?term\b/gi,
  515. ];
  516. // O(1) lookup from any token form (hyphenated or dashless) to its canonical
  517. // Tier 3 word. Counting originally did nested-loop word matching which was
  518. // O(tokens × TIER3) — slow on long pastes.
  519. const TIER3_LOOKUP = new Map();
  520. for (const word of TIER3) {
  521. TIER3_LOOKUP.set(word, word);
  522. const dashless = word.replace(/-/g, '');
  523. if (dashless !== word) TIER3_LOOKUP.set(dashless, word);
  524. }
  525. // Per-category score weights. Applied to distinct (deduplicated) issues so
  526. // the score reflects the same signals the user sees in the issue list.
  527. // Non-uniform on purpose: critical rules like cutoff disclaimers (×10) and
  528. // chatbot artifacts (×8) weigh more than vague attributions (×5), even
  529. // though all three are tagged `critical`.
  530. const ISSUE_WEIGHTS = {
  531. tier1: 5,
  532. // Wordiness, not an AI-frequency marker. Weighted like tier2 so a
  533. // clarity fix cannot push a document toward an AI classification.
  534. 'tier1-clarity': 3,
  535. tier2: 3,
  536. tier3: 2,
  537. transition: 2,
  538. chatbot: 8,
  539. sycophantic: 8,
  540. filler: 2,
  541. 'generic-conclusion': 3,
  542. 'lets-construction': 2,
  543. 'reasoning-artifact': 6,
  544. 'significance-inflation': 4,
  545. 'vague-attribution': 5,
  546. 'hollow-intensifier': 2,
  547. // Issue #82 evidence boundary: this style pattern produced no detector hits
  548. // in either corpus class, so it has no measured authorship direction. Keep
  549. // the finding visible, but do not move authorship scores or probabilities
  550. // until a relevant positive evaluation set supports a direction.
  551. 'emotional-flatline': 0,
  552. 'lingering-attention': 3,
  553. 'novelty-inflation': 3,
  554. 'cutoff-disclaimer': 10,
  555. 'template-phrase': 3,
  556. 'false-concession': 2,
  557. 'rhetorical-question': 2,
  558. 'confidence-calibration': 2,
  559. // Writing-quality guidance whose authorship polarity changes across model
  560. // generations. Keep the flag visible without moving authorship outputs.
  561. 'em-dash': 0,
  562. uniformity: 5,
  563. formatting: 3,
  564. 'tier3-phrase': 3,
  565. // Structural / cluster signals are deliberately weighted high. Unlike
  566. // single-vocabulary hits they're near-dispositive on social-length
  567. // posts (a 15-hashtag block, a 6-item bullet-NP list, three distinct
  568. // crypto-shill phrases stacked) and would otherwise be suppressed by
  569. // the log2(words/50) length divisor on short pastes.
  570. 'tier3-phrase-cluster': 12,
  571. 'hashtag-stuff': 12,
  572. 'bullet-np-list': 10,
  573. 'hedge-stack': 6,
  574. 'future-narrative': 12,
  575. 'real-actual-inflation': 5,
  576. // Social endorsement / CTA closer. Weighted like formulaic-opener: a
  577. // strong single-hit social tell that the length divisor would
  578. // otherwise wash out on a short LinkedIn-length post.
  579. 'social-cta-closer': 8,
  580. // Performed-insight tics: single hits are common in human essays, so
  581. // weighted like tier2 vocabulary — density does the classifying.
  582. 'performed-insight': 3,
  583. // Negation chains are a strong single-hit structural tell.
  584. 'negation-chain': 5,
  585. // Negative parallelism ("It's not just X, it's Y"). The frame is also
  586. // how people state a real correction, so it is weighted below the
  587. // negation chain; the per-piece gate in analyzeText does the rest.
  588. 'negative-parallelism': 4,
  589. 'dev-blog-boilerplate': 3,
  590. 'formulaic-opener': 8,
  591. // Speculative scenario opener ("Imagine a world where…"). Weighted like
  592. // formulaic-opener: a single strong opener tell the length divisor would
  593. // otherwise wash out on a short post.
  594. 'speculative-opener': 8,
  595. // Launch-copy introduction ("Enter X.", "Meet X, your new...").
  596. // Weighted like the other single-hit opener tells: strong on the
  597. // short launch posts where it actually appears.
  598. 'launch-intro': 8,
  599. // Dramatized crowd contrast. Gated hard on the dismissive verb, so
  600. // a hit is meaningful, but the surface shares words with ordinary
  601. // narrative — weighted below the opener tells on purpose.
  602. 'crowd-contrast': 6,
  603. // Fake-casual props (stage directions, wink asides). Near-costume
  604. // when present; same class as the opener tells on short posts.
  605. 'fake-casual-prop': 8,
  606. 'title-case-header': 4,
  607. 'parenthetical-hedge': 3,
  608. // House-style typography is a style review prompt, not authorship
  609. // evidence. Keep it neutral in both the score and classifier (#351).
  610. 'smart-punct-signature': 0,
  611. 'punct-distribution': 6,
  612. 'fnword-trigram-entropy': 5,
  613. 'cross-para-burstiness': 5,
  614. 'normalization-flag': 9,
  615. // Vocabulary-diversity signal (type-token ratio). Weighted modestly
  616. // because the threshold (>=200 tokens AND TTR<0.4) is conservative;
  617. // it stacks with structural signals to push borderline scores up.
  618. 'low-ttr': 3,
  619. // AI-tool fingerprints. Weighted higher than statistical patterns
  620. // because each is a near-definitive single-hit signal — the AI tool
  621. // literally left its mark in the text. citation-markup ranks highest
  622. // (smoking gun: the literal internal markup of ChatGPT/Grok/etc.),
  623. // UTM tracking second (auto-appended by the tool to URLs it writes),
  624. // placeholders third (strong but humans use bracketed slots in
  625. // templates legitimately and forget them — still a publishing bug
  626. // but slightly less definitive AI evidence).
  627. 'ai-placeholder': 10,
  628. 'ai-citation-markup': 15,
  629. 'ai-utm-source': 12,
  630. // Curated P2 copyedits, not authorship evidence. Keep them visible in the
  631. // issue list without moving the AI score, label, probabilities, or
  632. // trinary classification on short documents.
  633. 'unnecessary-hyphenation': 0,
  634. };
  635. // ─── Transition phrases ────────────────────────────────────────────
  636. const TRANSITIONS = [
  637. /\bmoreover\b/gi,
  638. /\bfurthermore\b/gi,
  639. /\badditionally\b/gi,
  640. /\bin\s+today'?s\b/gi,
  641. /\bin\s+an\s+era\s+where\b/gi,
  642. /\bit'?s\s+worth\s+noting\s+that\b/gi,
  643. /\bnotably\b/gi,
  644. /\bin\s+conclusion\b/gi,
  645. /\bin\s+summary\b/gi,
  646. /\bto\s+summarize\b/gi,
  647. /\bwhen\s+it\s+comes\s+to\b/gi,
  648. /\bat\s+the\s+end\s+of\s+the\s+day\b/gi,
  649. /\bthat\s+(?:being\s+)?said\b/gi,
  650. ];
  651. // ─── Chatbot artifacts ─────────────────────────────────────────────
  652. const CHATBOT_ARTIFACTS = [
  653. /\bi\s+hope\s+this\s+helps\b/gi,
  654. /\bcertainly!\b/gi,
  655. /\babsolutely!\b/gi,
  656. /\bgreat\s+question!\b/gi,
  657. /\bexcellent\s+point!\b/gi,
  658. /\bfeel\s+free\s+to\s+reach\s+out\b/gi,
  659. /\blet\s+me\s+know\s+if\s+you\s+need\s+anything\b/gi,
  660. /\bin\s+this\s+article,?\s+we\s+will\s+explore\b/gi,
  661. /\blet'?s\s+dive\s+in!?\b/gi,
  662. ];
  663. // ─── Sycophantic tone ──────────────────────────────────────────────
  664. const SYCOPHANTIC = [
  665. /\byou'?re\s+absolutely\s+right\b/gi,
  666. /\bthat'?s\s+a\s+really\s+insightful\b/gi,
  667. /\bthat'?s\s+a\s+great\s+question\b/gi,
  668. /\bexcellent\s+question\b/gi,
  669. ];
  670. // ─── Filler phrases ────────────────────────────────────────────────
  671. const FILLERS = [
  672. /\bit\s+is\s+important\s+to\s+note\s+that\b/gi,
  673. /\bin\s+terms\s+of\b/gi,
  674. /\bthe\s+reality\s+is\s+that\b/gi,
  675. /\bit'?s\s+important\s+to\s+note\s+that\b/gi,
  676. ];
  677. // ─── Generic conclusions ───────────────────────────────────────────
  678. const GENERIC_CONCLUSIONS = [
  679. /\bthe\s+future\s+looks\s+bright\b/gi,
  680. /\bonly\s+time\s+will\s+tell\b/gi,
  681. /\bone\s+thing\s+is\s+certain\b/gi,
  682. /\bas\s+we\s+move\s+forward\b/gi,
  683. ];
  684. // ─── "Let's" constructions ─────────────────────────────────────────
  685. const LETS_PATTERNS = [
  686. /\blet'?s\s+explore\b/gi,
  687. /\blet'?s\s+take\s+a\s+look\b/gi,
  688. /\blet'?s\s+break\s+this\s+down\b/gi,
  689. /\blet'?s\s+examine\b/gi,
  690. /\blet'?s\s+(?:consider|discuss|delve|unpack|walk\s+through)\b/gi,
  691. ];
  692. // ─── Reasoning chain artifacts ─────────────────────────────────────
  693. const REASONING_ARTIFACTS = [
  694. /\blet\s+me\s+think\s+step\s+by\s+step\b/gi,
  695. /\bbreaking\s+this\s+down\b/gi,
  696. /\bto\s+approach\s+this\s+systematically\b/gi,
  697. /\bhere'?s\s+my\s+thought\s+process\b/gi,
  698. /\bfirst,?\s+let'?s\s+consider\b/gi,
  699. /\bworking\s+through\s+this\s+logically\b/gi,
  700. ];
  701. // NOTE: Acknowledgment loops are judgment-only (#239). The three phrases the
  702. // detector matched ("you're asking about", "the question of whether", "to answer
  703. // your question") are also how people open an ordinary reply and standard
  704. // analytical English. The tell is a restatement that adds nothing, which a
  705. // regex cannot see. See detector/CATEGORIES.md §C.
  706. // ─── Significance inflation ────────────────────────────────────────
  707. const SIGNIFICANCE_INFLATION = [
  708. /\bmarking\s+a\s+(?:pivotal|significant|important)\s+moment\b/gi,
  709. /\ba\s+watershed\s+moment\s+for\b/gi,
  710. // Require an inflating word before "in the evolution of" (the bare phrase
  711. // is allowed for literal uses like "a key stage in the evolution of the
  712. // vertebrate eye"). See issue #212.
  713. /\b(?:moment|milestone|chapter|era|turning\s+point|watershed|inflection\s+point|leap|role|new\s+phase)\s+in\s+the\s+evolution\s+of\b/gi,
  714. /\ba\s+(?:pivotal|defining)\s+moment\s+in\b/gi,
  715. ];
  716. // ─── Vague attributions ────────────────────────────────────────────
  717. const VAGUE_ATTRIBUTIONS = [
  718. /\bexperts\s+(?:believe|say|suggest|agree)\b/gi,
  719. /\bstudies\s+(?:show|suggest|indicate)\b/gi,
  720. /\bresearch\s+(?:shows|suggests|indicates)\b/gi,
  721. /\bindustry\s+leaders\s+(?:agree|believe|say)\b/gi,
  722. ];
  723. // ─── Hollow intensifiers ──────────────────────────────────────────
  724. const HOLLOW_INTENSIFIERS = [
  725. /\bgenuine(?:ly)?\b/gi,
  726. /\btruly\b/gi,
  727. /\bquite\s+frankly\b/gi,
  728. /\bto\s+be\s+honest\b/gi,
  729. /\blet'?s\s+be\s+clear\b/gi,
  730. ];
  731. // ─── Emotional flatline ────────────────────────────────────────────
  732. // The "interesting (part|thing|aspect|piece)" family is matched in two
  733. // shapes: (1) "the most interesting X" inline (the canonical AI list
  734. // intro), and (2) bare "Interesting X:" used as a section-header opener,
  735. // which is the section-break variant that slipped past v3.3.x.
  736. const EMOTIONAL_FLATLINE = [
  737. /\bwhat\s+surprised\s+me\s+most\b/gi,
  738. /\bi\s+was\s+fascinated\s+to\b/gi,
  739. /\bwhat\s+struck\s+me\s+was\b/gi,
  740. /\bi\s+was\s+excited\s+to\s+learn\b/gi,
  741. /\bthe\s+most\s+interesting\s+(?:part|thing|aspect|piece)\b/gi,
  742. // Multiline flag (/m) so `^` matches at every line start, including
  743. // position 0 of a pasted text that has no leading newline. The earlier
  744. // `(?:^|\n)` form silently missed bare openers at the very start of
  745. // input — caught by silent-failure audit 2026-05-16. Leading whitespace
  746. // is `[ \t]*`, not `\s*`: with /m every line start is a match attempt,
  747. // and a `\s*` that can cross newlines rescans the whole blank run from
  748. // each of them, which made a long masked block quadratic (#235).
  749. /^[ \t]*interesting\s+(?:part|thing|aspect|piece)(?:\s+of\s+(?:the\s+)?\w+)?\s*:/gim,
  750. ];
  751. // ─── Lingering-attention claims ────────────────────────────────────
  752. // The share-post opener that claims duration of attention instead of
  753. // saying anything about the thing ("the line I keep coming back to").
  754. //
  755. // Precision note: the bare verb phrase "I keep coming back to X" is NOT
  756. // matched on its own, because it is legitimate whenever a reason follows
  757. // ("I keep coming back to Hirschman because it predicts who quits"), and
  758. // the reason clause is not reliably detectable by regex. Only the
  759. // noun-anchored frame ("the line/quote/bit ... I keep coming back to")
  760. // fires, which is the shape that introduces a subject rather than
  761. // asserting something about it. The bare form stays a skill-prose
  762. // judgment call. See SKILL.md §Lingering-attention claims carve-out.
  763. const LINGERING_ATTENTION = [
  764. // "the line I keep coming back to", "the one quote that I keep coming back to"
  765. // Noun-anchored frame only. "can't stop thinking about" is deliberately
  766. // left to its own pattern below rather than folded into this alternation,
  767. // so "the line I can't stop thinking about" scores once, not twice.
  768. /\b(?:the|that|this)\s+(?:one\s+)?(?:line|quote|bit|part|idea|point|framing|comment|thing)\s+(?:that\s+)?i\s+keep\s+(?:coming\s+back\s+to|thinking\s+about)\b/gi,
  769. /\bi\s+can'?t\s+stop\s+thinking\s+about\b/gi,
  770. /\bstill\s+thinking\s+about\s+(?:this|that)\s+one\b/gi,
  771. /\b(?:been|be)\s+rattling\s+around\s+(?:in\s+)?my\s+(?:head|brain)\b/gi,
  772. /\bi'?ve\s+been\s+chewing\s+on\s+(?:this|that)\b/gi,
  773. ];
  774. // ─── Novelty inflation ─────────────────────────────────────────────
  775. const NOVELTY_INFLATION = [
  776. /\bthe\s+failure\s+mode\s+nobody'?s?\s+naming\b/gi,
  777. /\ba\s+problem\s+nobody\s+talks\s+about\b/gi,
  778. /\bthe\s+insight\s+everyone'?s?\s+missing\b/gi,
  779. /\bwhat\s+nobody\s+tells\s+you\b/gi,
  780. ];
  781. // ─── Cutoff disclaimers ────────────────────────────────────────────
  782. // Includes the canonical LLM self-identification phrases — these are
  783. // near-dispositive on their own (real humans don't write "as an AI
  784. // language model" in first person). Patterns cover the major model
  785. // families' default disclaimer language.
  786. const CUTOFF_DISCLAIMERS = [
  787. /\bas\s+of\s+my\s+last\s+update\b/gi,
  788. /\bas\s+of\s+my\s+(?:knowledge\s+)?(?:cut-?off|last\s+training)\b/gi,
  789. /\bi\s+don'?t\s+have\s+access\s+to\s+real-?time\s+(?:data|information)\b/gi,
  790. /\bbased\s+on\s+available\s+information\b/gi,
  791. /\bas\s+an?\s+(?:ai|artificial\s+intelligence|large\s+language|ai\s+language)\s+(?:language\s+)?model\b/gi,
  792. /\bi\s+(?:am|'m)\s+an?\s+(?:ai|artificial\s+intelligence|large\s+language)\s+(?:assistant|model)?\b/gi,
  793. /\bi\s+cannot\s+(?:provide|give|offer)\s+(?:legal|medical|financial|professional)\s+advice\b/gi,
  794. /\bmy\s+training\s+data\s+(?:only\s+)?(?:goes\s+up\s+to|extends\s+to|ends\s+(?:in|at))\b/gi,
  795. ];
  796. // ─── AI-tool fingerprints ──────────────────────────────────────────
  797. // Three near-definitive AI-origin signals adapted from
  798. // Aboudjem/humanizer-skill P33-P35 (see docs/competitive/audits/
  799. // 2026-05-17-aboudjem-humanizer-skill.md). Unlike the statistical
  800. // patterns above, single hit on any of these is strong evidence —
  801. // the AI tool literally left its fingerprint in the text.
  802. // Unfilled slot-fill placeholders. Catches the canonical "[Your Name]"
  803. // family plus dated stubs and HTML/MD comments with placeholder verbs.
  804. const AI_PLACEHOLDERS = [
  805. // Directive stubs ("[Your Name]", "[INSERT SOURCE URL]",
  806. // "[Describe the specific section]") — verb-led, the user was
  807. // told to fill in.
  808. /\[(?:Your|Insert|Add|Enter|Describe|Specify|Choose|Pick)[^\]\n]{1,80}\]/gi,
  809. // Noun-only template variables common in AI-generated email or
  810. // letter boilerplate. Match the bare noun OR noun + qualifier.
  811. // Conservative list: only nouns that almost never appear as
  812. // bracketed real content (citation refs, code identifiers, etc.
  813. // are excluded because they typically contain dots, slashes,
  814. // hyphens, or version numbers).
  815. /\[(?:Recipient|Sender|Topic|Subject|Salutation|Closing|Position|Department|Project Name|Company Name|Date)(?:\s+[^\]\n]{0,60})?\]/gi,
  816. // All-caps directive forms ("[INSERT X]", "[FILL IN]") — the
  817. // uppercase tells you it's a slot, not real content.
  818. /\[(?:INSERT|FILL\s+IN|ADD|TODO|TBD|PLACEHOLDER)[^\]\n]{0,80}\]/g,
  819. // Date stubs.
  820. /\b(?:19|20)\d{2}-XX-XX\b/g,
  821. /\bXX\/XX\/(?:19|20)\d{2}\b/g,
  822. // HTML/Markdown comment placeholders with placeholder verbs.
  823. /<!--\s*(?:add|fill\s+in|insert|todo|placeholder)[^>]{0,120}-->/gi,
  824. ];
  825. // Chatbot citation/markup tokens that leak through copy-paste.
  826. // `citeturn0search0` / `citeturn0news5` from ChatGPT, contentReference
  827. // tokens, oai_citation, attached_file references, grok_card markers.
  828. // Each is a near-definitive signature of a specific tool.
  829. const AI_CITATION_MARKUP = [
  830. /\bcite(?:turn|news|search|navigation)\d+(?:search|turn|news|navigation)\d+/gi,
  831. /contentReference\s*\[oaicite:[^\]]+\]\s*\{[^}]*\}/gi,
  832. /\boai_citation\b/gi,
  833. /\[attached_file:\d+\]/gi,
  834. /\bgrok_card\b/gi,
  835. ];
  836. // UTM/tracking parameters auto-appended by AI tools to URLs they
  837. // generate. Survives copy-paste even when nothing else does.
  838. const AI_UTM_SOURCE = [
  839. /[?&]utm_source=(?:chatgpt|openai|copilot|claude|grok|gemini|perplexity)(?:\.com|\.ai)?\b/gi,
  840. /[?&]referrer=(?:chatgpt|copilot|grok|claude|gemini|perplexity)\.(?:com|ai)\b/gi,
  841. ];
  842. // ─── Template phrases ──────────────────────────────────────────────
  843. const TEMPLATE_PHRASES = [
  844. // template-phrase: only vague-praise adjectives. "a first step towards the full API",
  845. // "a small step towards cutting our storage bill" stay clean.
  846. /\ban?\s+(?:meaningful|significant|major|crucial|important|big|huge|bold|giant|monumental|pivotal|critical|key|vital|massive|tremendous|substantial|decisive|landmark|historic|exciting|transformative)\s+step\s+(?:towards?|forward)\b/gi,
  847. /\bwhether\s+you'?re\s+\w+\s+or\s+\w+/gi,
  848. /\bi\s+recently\s+had\s+the\s+pleasure\s+of\b/gi,
  849. ];
  850. // ─── False concession ──────────────────────────────────────────────
  851. // "While X is impressive, Y remains a challenge" and "Although X has made
  852. // strides, Y is still an open question" only read as the AI tell when both
  853. // halves are vague: an opener that concedes nothing specific, paired with a
  854. // close that names no actual gap. The subject (X) is widened past a single
  855. // word — "while the underlying model architecture is impressive" is as
  856. // hollow as "while it is impressive" — but stays inside one clause (no
  857. // comma or sentence punctuation) so the opener cannot reach across clauses.
  858. // The close is required in the same sentence: a bare opener followed by a
  859. // concrete, specific continuation ("...at this scale, our write pattern is
  860. // append-only, so we moved the hot table to a log-structured store
  861. // instead") is ordinary technical writing, not the empty frame. See #211.
  862. // "Despite X challenges" is dropped entirely: alone it is too common a
  863. // shape in ordinary prose to carry the tell.
  864. // The close must also follow a clause separator (comma, semicolon or
  865. // colon): without one, "While the model is impressive and remains a
  866. // challenge to maintain, we plan to replace it next month" matched both
  867. // phrases inside the opening clause and never looked at the concrete main
  868. // clause that followed. See #359.
  869. // Stop at the next clause separator too: a concrete continuation followed
  870. // by a later vague phrase is not the empty two-half frame.
  871. const FALSE_CONCESSION_SUBJECT = "[^,;:.!?\\n]{1,60}?";
  872. // Known limit: a period inside an abbreviation ("U.S.", "e.g.") ends the
  873. // gap the same as a real sentence boundary would, so a close on the far
  874. // side of one is deliberately missed to keep that boundary guarantee;
  875. // only a comma, semicolon or colon counts as the clause separator #359
  876. // requires.
  877. const FALSE_CONCESSION_GAP = "[^,;:.!?\\n]{0,80}?[,;:]\\s*[^,;:.!?\\n]{0,80}?";
  878. const FALSE_CONCESSION_VAGUE_CLOSE =
  879. "(?:remains?\\s+a\\s+challenge" +
  880. "|(?:is|are)\\s+still\\s+an?\\s+open\\s+questions?" +
  881. "|there\\s+(?:is|are)\\s+still\\s+work\\s+to\\s+do" +
  882. "|remains?\\s+unanswered)\\b";
  883. const FALSE_CONCESSION = [
  884. new RegExp("\\bwhile\\s+" + FALSE_CONCESSION_SUBJECT + "\\s+is\\s+impressive\\b" +
  885. FALSE_CONCESSION_GAP + FALSE_CONCESSION_VAGUE_CLOSE, 'gi'),
  886. new RegExp("\\balthough\\s+" + FALSE_CONCESSION_SUBJECT + "\\s+has\\s+made\\s+strides\\b" +
  887. FALSE_CONCESSION_GAP + FALSE_CONCESSION_VAGUE_CLOSE, 'gi'),
  888. ];
  889. // ─── Rhetorical question openers ───────────────────────────────────
  890. const RHETORICAL_QUESTIONS = [
  891. /\bbut\s+what\s+does\s+this\s+mean\s+for\b/gi,
  892. /\bso\s+why\s+should\s+you\s+care\b/gi,
  893. /\bwhat'?s\s+next\?\s*/gi,
  894. ];
  895. // ─── Hedge-stacked predictions ─────────────────────────────────────
  896. // Stacks a modal with a hedge adverb: "could potentially create new
  897. // opportunities", "may eventually unlock value." Either word alone is
  898. // fine; the stack is the tell.
  899. const HEDGE_STACK = [
  900. // At most one intervening word, and never a negator. The old {0,2} gap
  901. // matched ordinary English: "could not possibly" (plain emphatic
  902. // negation) and inverted questions like "could a savage possibly" both
  903. // fired. Measured on the human-control corpus, 3 of 4 hedge-stack flags
  904. // were this over-match. See issue #69.
  905. /\b(?:could|may|might)\s+(?:(?!not\b|never\b|hardly\b|scarcely\b|barely\b)\w+\s+)?(?:potentially|eventually|ultimately|possibly|conceivably)\b/gi,
  906. /\b(?:potentially|eventually|ultimately)\s+(?:could|may|might)\b/gi,
  907. ];
  908. // ─── Generic future-narrative closers ──────────────────────────────
  909. // The "may become one of the most important narratives" template — vague
  910. // future significance with no falsifiable claim. Covers narratives /
  911. // stories / trends / themes / chapters / movements.
  912. const FUTURE_NARRATIVE = [
  913. /\b(?:may|could|will|is\s+(?:poised|set)\s+to)\s+become\s+(?:one\s+of\s+)?(?:the\s+)?(?:most\s+)?\w+\s+(?:narratives?|stories|developments?|trends?|movements?|chapters?|themes?|forces?)\b/gi,
  914. /\bone\s+of\s+the\s+most\s+important\s+(?:narratives?|stories|trends?|themes?)\s+of\s+the\s+(?:next|coming)\s+\w+\b/gi,
  915. ];
  916. // ─── "Real/actual" adjective inflation ─────────────────────────────
  917. // "Real on-chain tokenomics", "actual reward sustainability" — using
  918. // real/actual/genuine/true as an empty intensifier on an abstract noun
  919. // to imply the rest of the field is fake/superficial.
  920. const REAL_ACTUAL_INFLATION = [
  921. /\b(?:real|actual|genuine|true)\s+(?:on-?chain\s+)?(?:tokenomics|economics|utility|adoption|sustainability|impact|revenue|fundamentals|demand|value|innovation|traction)\b/gi,
  922. ];
  923. // ─── Formulaic openers ─────────────────────────────────────────────
  924. // The "In the rapidly evolving world of X, Y has emerged as..." family
  925. // — LLM-default essay openers.
  926. const FORMULAIC_OPENERS = [
  927. /\bin\s+the\s+(?:rapidly\s+|ever-?\s*)?(?:evolving|changing|expanding|growing|shifting)\s+(?:world|landscape|realm|space|field|domain|era)\s+of\b/gi,
  928. /\bin\s+(?:an?|the)\s+(?:digital\s+)?age\s+(?:where|of)\b/gi,
  929. /\bas\s+(?:we|the\s+world|society|industries?)\s+(?:continue|move|navigate|enter)\s+(?:to\s+)?(?:evolve|forward|into|through)\b/gi,
  930. // "has emerged as a leader/force/category" — gated to the inflated
  931. // nouns that signal pseudo-significance, since bare "has emerged as
  932. // a" matches normal English ("Rust has emerged as a serious systems
  933. // language"). Same gating for "has become increasingly".
  934. /\bhas\s+emerged\s+as\s+(?:a|the|one\s+of)\s+(?:leading|key|major|critical|essential|fundamental|pivotal|prominent|dominant|important)\s+\w+/gi,
  935. /\bhas\s+become\s+increasingly\s+(?:important|critical|popular|relevant|prominent|essential)\b/gi,
  936. ];
  937. // ─── Speculative scenario openers ──────────────────────────────────
  938. // "Imagine a world where...", "Picture a future in which...", "Envision
  939. // a world where..." — the LLM habit of opening an argument with a
  940. // hypothetical that lists desirable outcomes instead of making a claim.
  941. // Gated to the world/future/reality object plus where/in-which so it
  942. // stays off instructional "imagine you have an array" (a teaching device
  943. // pointing at a concrete example, not a speculative world) and bare
  944. // "imagine that" asides. "consider a scenario where…" is deliberately
  945. // excluded: that is analytical framing common in technical reasoning,
  946. // not the marketing-opener tell. An optional short comma interrupter
  947. // catches the equally common "Imagine, for a moment, a world where…"
  948. // cadence. Known accepted false positive: fiction openings and staged
  949. // thought experiments match too — the engine has no fiction context
  950. // mode, so that call is left to the skill's carve-out (highlight-only;
  951. // a lone hit cannot flip a document's classification).
  952. const SPECULATIVE_OPENERS = [
  953. /\b(?:imagine|picture|envision)(?:\s*,[^,\n]{1,30},)?\s+a\s+(?:world|future|reality)\s+(?:where|in\s+which)\b/gi,
  954. ];
  955. // ─── Launch-copy dramatic introductions ────────────────────────────
  956. // "Meet Flowdesk, your new favorite treasury dashboard" / "Think
  957. // Notion meets Figma" — the LLM-default product-introduction move
  958. // in launch and announcement copy. Both surfaces are gated to the
  959. // sentence-initial imperative followed by ONE capitalized token of 2
  960. // to 30 characters, which is a recall limit: a two-token product name
  961. // ("Meet North Star", "Think Google Docs meets Microsoft Word") is a
  962. // deliberate miss. The Meet surface additionally requires one of four
  963. // launch-copy heads — "your new favorite", "your new go-to", and
  964. // "the new home/way/standard", the last three only when followed by
  965. // "of" / "to" / "in|for" or by end-of-clause punctuation. Without
  966. // that tail the head noun swallows a compound noun and ordinary prose
  967. // fires: "Meet Rosa, the new home secretary" and "Meet Emma, the new
  968. // way station manager" both matched before the tail was required.
  969. // Bare "Meet Sarah, your new account manager" is how humans introduce
  970. // colleagues, pets, and babies, so that form stays with the skill's
  971. // judgment side. Two surfaces from
  972. // the same family are deliberately NOT detected. "Say hello to X",
  973. // because "Say hello to Grandma." is ordinary human prose. And bare
  974. // "Enter X.", because the sentence-initial capitalized-noun form is
  975. // how UI and doc instructions are written: "Enter Password.", "Enter
  976. // Amount.", "Enter Username — your work email." Dropping the dash
  977. // terminator does not reach the period-terminated class, and neither
  978. // does a field-name denylist, so that surface stays with the skill's
  979. // judgment side and the UI forms are pinned as must-not-fire
  980. // fixtures. The anchors are lookbehinds so adjacent intros each
  981. // count and the reported span starts at the tell itself.
  982. const LAUNCH_INTROS = [
  983. /(?<=^|[.!?]\s|\n)Meet\s+[A-Z][\w'-]{1,29}\s*,\s*(?:your\s+new\s+(?:favorite|go-to)\b|the\s+new\s+(?:home\s+of\b|way\s+to\b|standard\s+(?:in|for)\b|(?:standard|way|home)(?=\s*(?:[.!?,;:\u2013\u2014]|$))))/g,
  984. /(?<=^|[.!?]\s|\n)[Tt]hink\s+[A-Z][\w'-]{1,29}\s+meets\s+[A-Z][\w'-]{1,29}\b/g,
  985. ];
  986. // ─── Dramatized contrast against the crowd ─────────────────────────
  987. // "shipped it in 2022, while everyone else was still debating
  988. // timelines" — a claim propped on an implied lagging crowd. The gate
  989. // is a dismissive verb PLUS the "was still" dramatization marker,
  990. // because bare "while everyone else" is ordinary simultaneity ("she
  991. // read while everyone else watched the movie") and even the
  992. // dismissive verbs are ordinary English in literal use ("others
  993. // debated the amendment" in wire copy). That gate is on this FIRST
  994. // branch only. Its stems are restricted to the -ing form, so the
  995. // adjective ("was still deliberate about the tradeoff"), the passive
  996. // ("was still debated by pundits") and the bare present ("was still
  997. // debates timelines") all stay clean — allowing e/es/ed let all
  998. // three through. The other two branches carry no "was still"
  999. // requirement: they match their own stereotyped wording ("writing
  1000. // think-pieces", "playing catch-up"), and the skill entry scopes the
  1001. // claim the same way. Verb stems carry explicit inflection tails so
  1002. // agent nouns ("the market speculators") and adverbs ("deliberately
  1003. // ignored") never match. Measured residue, all accepted: branch one
  1004. // fires on ANY literal progressive use of its verbs ("while the
  1005. // market was still speculating about the price"), not only on "was
  1006. // still debating"; branches two and three fire on literal contrasts
  1007. // of their own ("while everyone else wrote think-pieces from
  1008. // Washington", "while everyone else played catch-up in the spring").
  1009. // Recall is deliberately sacrificed: "was busy debating" without
  1010. // "still" stays a miss, per precision-over-recall.
  1011. const CROWD_CONTRAST = [
  1012. /\bwhile\s+(?:everyone\s+else|the\s+(?:industry|market|competition)|others)\s+(?:was|were|is|are)\s+still\s+(?:busy\s+)?(?:(?:debat|deliberat|hesitat|theoriz|philosophiz|pontificat|speculat|argu)ing|(?:dither|bicker)ing)\b/gi,
  1013. /\bwhile\s+(?:everyone\s+else|the\s+(?:industry|market|competition)|others)\s+(?:was\s+|were\s+)?(?:busy\s+)?(?:writing|wrote)\s+think-?\s?pieces\b/gi,
  1014. /\bwhile\s+(?:everyone\s+else|the\s+(?:industry|market|competition)|others)\s+(?:was\s+|were\s+|is\s+|are\s+)?(?:still\s+)?play(?:ed|ing|s)?\s+catch[-\s]?up\b/gi,
  1015. ];
  1016. // ─── Fake-casual props (stage directions and wink asides) ──────────
  1017. // The regexable props from the fake-casual register: theatrical
  1018. // asterisk stage directions ("*checks notes*", "*chef's kiss*",
  1019. // "*mic drop*") and wink asides. Both lists are closed and short, and
  1020. // that is a recall limit: exactly six stage directions ("checks
  1021. // notes", "chef's kiss", "mic drop", "takes a deep breath", "sips
  1022. // coffee|tea", "nervous laughter") and exactly four parentheticals,
  1023. // the full (yes|no) x (really|seriously) grid. Neighbours in the same
  1024. // register are deliberate misses: "*checks calendar*" and "(yes,
  1025. // honestly)" do not fire. The kiss pattern requires the apostrophe —
  1026. // making it optional matched the ordinary sentence "At midnight,
  1027. // *chefs kiss* their spouses goodbye".
  1028. // The rest of the register (one-word verdict closers, label-prefix
  1029. // openers, the self-QA volley) needs register judgment and stays
  1030. // skill-only — "wild." is a word, not a regex target. "because of
  1031. // course …" joins them: a tense gate does not separate the wink from
  1032. // the ordinary human grumble, because "The build failed because of
  1033. // course it did." is that grumble in the same present-tense-plus-did
  1034. // form the wink uses. Under precision-over-recall the surface is
  1035. // judgment-only, and the grumble is pinned as a fixture. The kiss
  1036. // pattern accepts the curly apostrophe (U+2019) — the form smart
  1037. // punctuation and LLMs actually emit. Known accepted false positive:
  1038. // a human writer using a wink aside on purpose; the props are
  1039. // weighted as a strong single hit, not a classification by
  1040. // themselves.
  1041. const FAKE_CASUAL_PROPS = [
  1042. /\*\s?(?:checks\s+notes|chef['\u2019]s\s+kiss|mic\s+drop|takes\s+a\s+deep\s+breath|sips\s+(?:coffee|tea)|nervous\s+laughter)\s?\*/gi,
  1043. /\(\s?(?:yes|no)\s?,\s?(?:really|seriously)\s?\)/gi,
  1044. ];
  1045. // ─── Performed-insight phrases ─────────────────────────────────────
  1046. // Essayist tics that announce profundity instead of delivering it.
  1047. // Curated noun/complement lists keep precision high: "the whole family"
  1048. // and "sit with him" are ordinary English and must not fire. Adapted
  1049. // from Simon Willison's LLM cliché highlighter
  1050. // (tools.simonwillison.net/llm-cliche-highlighter).
  1051. const PERFORMED_INSIGHT = [
  1052. /\bsit(?:s|ting)?\s+with\s+(?:that|this)(?=\s*(?:[.!?,;:)\u2013\u2014\u2019"']|for\s+a\s+(?:moment|minute|second|beat)\b|$))(?:\s+for\s+a\s+(?:moment|minute|second|beat))?/gi,
  1053. /\bsit(?:s|ting)?\s+with\s+(?:the|your)\s+(?:discomfort|tension|uncertainty|ambiguity|grief|unease)\b/gi,
  1054. /\b(?:that|this|it|which)(?:['\u2019]s|\s+(?:is|was))\s+not\s+nothing\b/gi,
  1055. /\byou\s+already\s+know\s+the\s+answer\b/gi,
  1056. /\b(?:do\s+not|don['\u2019]t)\s+(?:have\s+to\s+)?take\s+my\s+word\s+for\s+it\b/gi,
  1057. /(?<=^|[.!?]\s|\n)Turns\s+out\b/g,
  1058. /(?:['\u2019]s|\b(?:is|was|are|were))\s+the\s+(?:whole|entire)\s+(?:point|game|ballgame|trick|pitch|idea|play|business\s+model|value\s+proposition)\b/gi,
  1059. /\b(?:that|this)(?:['\u2019]s|\s+(?:is|was))\s+the\s+part\s+(?:that|I|you|we|nobody|no\s+one|most\s+people)\b/gi,
  1060. /\bthe\s+only\s+[\w'\u2019-]+\s+that\s+(?:matters|counts)\b/gi,
  1061. // "X is dead; long live Y" also names an essay or a literal succession
  1062. // announcement. Whether the contrast earns its place needs reading (#351).
  1063. /\b(?:that|this)(?:['\u2019]s|\s+(?:is|was))\s+why\s+[^.!?\n]{0,60}\s+mattered\b/gi,
  1064. ];
  1065. const STAGED_DISCOVERY = [
  1066. // Staged discovery: a judgment framed as a twist the writer found ("the recording
  1067. // turned out to be the least interesting part"). Superlative + insight noun keeps
  1068. // ordinary "turned out to be the most expensive option" clean.
  1069. /\b(?:turned|turns|turning)\s+out\s+to\s+be\s+the\s+(?:least|most)\s+(?:interesting|important|surprising|revealing|valuable|useful)\s+(?:part|thing|piece|bit)\b/gi,
  1070. // Require a reveal-style continuation; "the real story was covered by..."
  1071. // describes a literal story and is not a staged discovery.
  1072. /\bthe\s+real\s+story\s+(?:here\s+)?(?:is|was)\b(?=\s+(?:the|that|how|why|what)\b)/gi,
  1073. ];
  1074. // ─── Negation chains ───────────────────────────────────────────────
  1075. // "No fluff, no filler, no jargon" / "It didn't ask, didn't wait" /
  1076. // "Don't call it X. Call it Y." Precision guards, in order: the
  1077. // "no …" chain must open its sentence (mid-sentence inventories like
  1078. // "takes no arguments, no headers, and no body" are factual, not
  1079. // rhetorical); the "did not" chain must be comma-joined with the
  1080. // subject elided ("I did not sleep. I did not eat" is ordinary
  1081. // narration and stays clean); the stop-list keeps idiomatic pairs
  1082. // ("no more, no less", "no matter what") from firing. Adapted from
  1083. // Simon Willison's LLM cliché highlighter.
  1084. const NO_ITEM_STOP = "(?!matter\\b|one\\b|doubt\\b|longer\\b|way\\b|less\\b|more\\b|such\\b|other\\b|means\\b)";
  1085. const NO_ITEM_SECOND_STOP = "(?!(?:in|on|at|of|to|for|with|from|by|is|are|was|were|be|been|being|will|would|can|could|should|shall|may|might|must|have|has|had|do|does|did)\\b)";
  1086. const NEGATION_CHAIN = [
  1087. new RegExp(
  1088. "(?<=^|[.!?]\\s|\\n|[:\\u2013\\u2014]\\s)No\\s+" + NO_ITEM_STOP + "[a-z'\u2019-]+(?:\\s+" + NO_ITEM_SECOND_STOP + "[a-z'\u2019-]+)?" +
  1089. "(?:\\s*,\\s*(?:and\\s+|or\\s+|just\\s+)?no\\s+" + NO_ITEM_STOP + "[a-z'\u2019-]+(?:\\s+" + NO_ITEM_SECOND_STOP + "[a-z'\u2019-]+)?){2,}",
  1090. 'gm'
  1091. ),
  1092. /\b(?:did\s+not|didn['\u2019]t)\s+[a-z]+[^,.;!?\n]{0,20},\s*(?:did\s+not|didn['\u2019]t)\s+[a-z]+/gi,
  1093. /\b(?:do\s+not|don['\u2019]t)\s+(?:just\s+)?(\w+)\s+it\b[^.!?\n]{0,60}[.!?;:,][\s'"\u201d\u2019]*(?:just\s+)?\1\s+it\b/gi,
  1094. ];
  1095. // ─── Negative parallelism ──────────────────────────────────────────
  1096. // "It's not just a search index, it's a foundation for trust." A frame is
  1097. // a negated copula, a body X, then a restatement: "it / this / that /
  1098. // they" + be. The engine splits frames in two (#351):
  1099. //
  1100. // reveal a minimizer (just / merely / simply), then a comma,
  1101. // semicolon, colon, or dash and the restatement ("isn't just
  1102. // raining, it's pouring"). The minimizer-then-upgrade move is
  1103. // the tell itself, so a reveal flags on its own.
  1104. // contrast the same joined frame without the minimizer ("isn't X, it's
  1105. // Y", "isn't about X, it's about Y", "are not only X, they're
  1106. // Y") and the split-sentence reveal ("isn't just X. It's Y.").
  1107. // A single plain correction ("It isn't raining, it's
  1108. // snowing.") is ordinary English, and references/patterns.md
  1109. // allows one frame per piece, so a contrast flags only when
  1110. // another frame of either kind starts within
  1111. // NP_WINDOW_SENTENCES sentences of it. The stacked cadence is
  1112. // the tell; two unrelated corrections paragraphs apart are
  1113. // ordinary prose. "only" is not a reveal minimizer: the human
  1114. // control corpus holds "fossil fuels are not only bad for our
  1115. // environment, they're a losing bet".
  1116. //
  1117. // The restated pronoun is the gate. "not only X but (also) Y" and "not X
  1118. // but Y" are ordinary correlatives and are not matched: on the human
  1119. // control corpus "not only ... but" appeared 16 times in 143k human words
  1120. // against 5 in 115k machine words. X is capped at 80 characters with no
  1121. // comma or sentence punctuation, so a frame cannot reach across clauses.
  1122. const NP_NEG = "(?:\\b(?:is|are|was|were)(?:n['\\u2019]t|\\s+not)|\\b(?:it|this|that|they|he|she|we|you)['\\u2019](?:s|re)\\s+not)";
  1123. const NP_MINIMIZER = "(?:just|merely|simply)";
  1124. const NP_BODY = "[^,;:.!?\\n\\u2014\\u2013]{1,80}?";
  1125. const NP_JOIN = "(?:\\s*[,;:]|\\s*[\\u2014\\u2013]|\\s+--)\\s*";
  1126. const NP_RESTATE = "(?:it|this|that|they)(?:['\\u2019](?:s|re)|\\s+(?:is|are|was|were))\\b";
  1127. const NEGATIVE_PARALLELISM_REVEAL = [
  1128. new RegExp(NP_NEG + "\\s+" + NP_MINIMIZER + "\\s+" + NP_BODY + NP_JOIN + NP_RESTATE, 'gi'),
  1129. ];
  1130. const NEGATIVE_PARALLELISM_CONTRAST = [
  1131. new RegExp(NP_NEG + "\\s+(?!" + NP_MINIMIZER + "\\b)" + NP_BODY + NP_JOIN + NP_RESTATE, 'gi'),
  1132. // Split-sentence reveal: the restatement opens the next sentence.
  1133. new RegExp(NP_NEG + "\\s+" + NP_MINIMIZER + "\\s+" + NP_BODY + "[.!]\\s+" + NP_RESTATE, 'gi'),
  1134. ];
  1135. // Two frames pair when their starting sentences are at most this many
  1136. // sentences apart: the same sentence, the next, or the one after.
  1137. const NP_WINDOW_SENTENCES = 2;
  1138. // A blank line in LF, CRLF, or CR-only text.
  1139. const NP_PARAGRAPH_BREAK = /(?:\r\n|\r(?!\n)|\n)[ \t]*(?:\r\n|\r|\n)/;
  1140. // Reveals always flag. A contrast flags only when some other frame, reveal
  1141. // or contrast, starts nearby. Sentence indexes come from the same coarse
  1142. // splitter the highlight regions use.
  1143. function negativeParallelismIssues(text, reveals, contrasts) {
  1144. // Apply the proximity gate to the same distinct frames the caller reports.
  1145. const distinctReveals = deduplicateIssues(reveals);
  1146. const distinctContrasts = deduplicateIssues(contrasts);
  1147. if (distinctContrasts.length === 0 || distinctReveals.length + distinctContrasts.length < 2) return distinctReveals;
  1148. const frames = [...distinctReveals, ...distinctContrasts];
  1149. const starts = splitSentenceSpans(text).map(([start]) => start);
  1150. const sentenceOf = (index) => {
  1151. let lo = 0;
  1152. let hi = starts.length - 1;
  1153. while (lo < hi) {
  1154. const mid = (lo + hi + 1) >> 1;
  1155. if (starts[mid] <= index) lo = mid;
  1156. else hi = mid - 1;
  1157. }
  1158. return lo;
  1159. };
  1160. const sentences = frames.map((frame) => sentenceOf(frame.index));
  1161. // Paired frames must also share a paragraph: no blank line between them.
  1162. const sameParagraph = (a, b) => !NP_PARAGRAPH_BREAK.test(text.slice(Math.min(a, b), Math.max(a, b)));
  1163. const paired = distinctContrasts.filter((contrast, c) => {
  1164. const i = distinctReveals.length + c;
  1165. return sentences.some((other, j) =>
  1166. j !== i
  1167. && frames[j].index !== contrast.index
  1168. && Math.abs(other - sentences[i]) <= NP_WINDOW_SENTENCES
  1169. && sameParagraph(frames[j].index, contrast.index));
  1170. });
  1171. return [...distinctReveals, ...paired].sort((a, b) => a.index - b.index);
  1172. }
  1173. // ─── Dev-blog boilerplate ──────────────────────────────────────────
  1174. // Stock simplicity slogans from developer marketing. Adapted from
  1175. // Simon Willison's LLM cliché highlighter.
  1176. const DEV_BLOG_BOILERPLATE = [
  1177. /\bit\s+just\s+works\b(?!\s+out\b(?![-\s]+of[-\s]+the[-\s]+box\b))/gi,
  1178. /\bzero[-\s]config(?:uration)?\b/gi,
  1179. /\bsane\s+defaults\b/gi,
  1180. /\b(?:hold|fit|fits|holds)\s+in\s+your\s+head\b/gi,
  1181. ];
  1182. // Function words whose presence MID-title marks the AI section-header shape.
  1183. // Word-anchored: without \b the "A" alternative matches inside any word and
  1184. // the guard silently degrades to "four tokens".
  1185. const FUNCTION_WORD = /\b(?:And|Or|Of|The|In|For|To|A|An)\b/;
  1186. // Must accept exactly what TITLE_CASE_HEADER accepts, or the prefix survives
  1187. // into the token count and reintroduces the ##-as-token bug.
  1188. const MD_HEADING_PREFIX = /^#{1,6}[ \t]+/;
  1189. /** Byte ranges covered by fenced code blocks, computed once per scan.
  1190. *
  1191. * A document that documents Markdown is the normal case for this rule -- a
  1192. * fenced `## Heading` example is illustration, not the author's own section
  1193. * header, and flagging it makes every docs page flag itself.
  1194. *
  1195. * This tracks the opening delimiter instead of counting them, because a
  1196. * parity count is wrong on the very case the rule exists for. CommonMark
  1197. * closes a fence only on the same character at the same length or longer, so
  1198. * a four-backtick fence wrapping a three-backtick example -- exactly how you
  1199. * document fences -- nests in practice, and counting delimiters inverts on
  1200. * it. Up to three spaces of indent are legal. An unclosed fence runs to end
  1201. * of document, matching how renderers treat it.
  1202. *
  1203. * Computed once per scan rather than rescanned per hit: the previous version
  1204. * sliced the whole document for every candidate, which is quadratic on a
  1205. * heading-dense file. */
  1206. function fenceRanges(text) {
  1207. const re = /^[ \t]{0,3}(`{3,}|~{3,})([^\n]*)$/gm;
  1208. const ranges = [];
  1209. let open = null;
  1210. let m;
  1211. while ((m = re.exec(text)) !== null) {
  1212. const marker = m[1];
  1213. if (!open) {
  1214. open = { char: marker[0], len: marker.length, start: m.index };
  1215. } else if (
  1216. marker[0] === open.char &&
  1217. marker.length >= open.len &&
  1218. /^[ \t]*\r?$/.test(m[2])
  1219. ) {
  1220. ranges.push([open.start, m.index + m[0].length]);
  1221. open = null;
  1222. }
  1223. }
  1224. if (open) ranges.push([open.start, text.length]);
  1225. return ranges;
  1226. }
  1227. function inFenceRange(ranges, index) {
  1228. return typeof index === 'number' && ranges.some(([a, b]) => index >= a && index < b);
  1229. }
  1230. function blankRange(chars, start, end) {
  1231. for (let i = start; i < end && i < chars.length; i += 1) {
  1232. if (chars[i] !== '\n') chars[i] = ' ';
  1233. }
  1234. }
  1235. function inlineCodeRanges(text) {
  1236. const runs = [];
  1237. for (let i = 0; i < text.length;) {
  1238. if (text[i] === '\n') {
  1239. runs.push(null);
  1240. i += 1;
  1241. continue;
  1242. }
  1243. if (text[i] !== '`') {
  1244. i += 1;
  1245. continue;
  1246. }
  1247. const start = i;
  1248. while (i < text.length && text[i] === '`') i += 1;
  1249. runs.push({ start, end: i, length: i - start });
  1250. }
  1251. const ranges = [];
  1252. let lineStart = 0;
  1253. while (lineStart < runs.length) {
  1254. let lineEnd = runs.indexOf(null, lineStart);
  1255. if (lineEnd === -1) lineEnd = runs.length;
  1256. const nextByLength = new Map();
  1257. const nextSame = new Array(lineEnd - lineStart);
  1258. for (let i = lineEnd - 1; i >= lineStart; i -= 1) {
  1259. nextSame[i - lineStart] = nextByLength.get(runs[i].length);
  1260. nextByLength.set(runs[i].length, i);
  1261. }
  1262. for (let i = lineStart; i < lineEnd;) {
  1263. const close = nextSame[i - lineStart];
  1264. if (close === undefined) {
  1265. i += 1;
  1266. continue;
  1267. }
  1268. ranges.push([runs[i].start, runs[close].end]);
  1269. i = close + 1;
  1270. }
  1271. lineStart = lineEnd + 1;
  1272. }
  1273. return ranges;
  1274. }
  1275. // Copy of the text with fenced blocks and inline code spans blanked out.
  1276. // Index-preserving: each masked character becomes a space and newlines are
  1277. // kept, so offsets into the result still address the same position in the
  1278. // original. For rules where a character inside code is something the author
  1279. // is quoting rather than using — a `#fff` in a CSS sample is not a tag.
  1280. // Fences are blanked first so their backticks cannot pair with a later
  1281. // inline span and swallow the prose between them.
  1282. function maskCode(text) {
  1283. const chars = text.split('');
  1284. for (const [a, b] of fenceRanges(text)) blankRange(chars, a, b);
  1285. // Indented code blocks are deliberately NOT masked. Four spaces is a code
  1286. // block only at top level; under a list marker it is a paragraph
  1287. // continuation, so blanking it silences real tag blocks. #90 reports
  1288. // fences and inline spans, and those are what this masks.
  1289. const withoutFences = chars.join('');
  1290. for (const [start, end] of inlineCodeRanges(withoutFences)) blankRange(chars, start, end);
  1291. return chars.join('');
  1292. }
  1293. function initialFrontmatterRange(text) {
  1294. const lines = [];
  1295. const lineRe = /[^\r\n]*(?:\r\n|\n|\r|$)/g;
  1296. let match;
  1297. while ((match = lineRe.exec(text)) !== null && match[0]) {
  1298. const body = match[0].replace(/(?:\r\n|\n|\r)$/, '');
  1299. lines.push({ body, start: match.index, end: match.index + body.length });
  1300. }
  1301. if (lines.length < 3 || !/^---[ \t]*$/.test(lines[0].body.replace(/^\uFEFF/, ''))) return null;
  1302. let closingLine = -1;
  1303. for (let i = 1; i < lines.length; i += 1) {
  1304. if (/^---[ \t]*$/.test(lines[i].body)) {
  1305. closingLine = i;
  1306. break;
  1307. }
  1308. }
  1309. if (closingLine === -1) return null;
  1310. // A pair of thematic breaks can also surround ordinary Markdown prose.
  1311. // Require the first substantive line to begin like a YAML mapping entry
  1312. // before treating the delimited block as frontmatter. Leading blank lines
  1313. // and YAML comments are valid, and the line parser accepts LF, CRLF, or CR.
  1314. const yamlKey = /^[ \t]*(?:[A-Za-z0-9_.-]+|"[^"\r\n]+"|'[^'\r\n]+')[ \t]*:/;
  1315. const firstContent = lines
  1316. .slice(1, closingLine)
  1317. .find((line) => line.body.trim() && !/^[ \t]*#/.test(line.body));
  1318. if (!firstContent || !yamlKey.test(firstContent.body)) return null;
  1319. return { start: 0, end: lines[closingLine].end };
  1320. }
  1321. // Mask HTML comments in source order while tracking the Markdown constructs
  1322. // that protect a literal `<!--`. A comment wins over code delimiters that
  1323. // occur inside it; a fence, code span, or top-level indented block that
  1324. // starts first wins over comment-looking text inside that code. Each source
  1325. // character participates in a bounded number of forward scans.
  1326. function maskHtmlCommentsOutsideCode(chars) {
  1327. const source = chars.join('');
  1328. const lines = source.split('\n');
  1329. let offset = 0;
  1330. let openFence = null;
  1331. let inIndentedBlock = false;
  1332. let previousBlank = true;
  1333. let listContext = false;
  1334. let maskedHtmlComments = 0;
  1335. const commentClosings = [];
  1336. let closingCursor = 0;
  1337. for (let i = 0; i <= source.length - 3; i += 1) {
  1338. if (source[i] === '-' && source[i + 1] === '-' && source[i + 2] === '>') {
  1339. commentClosings.push(i);
  1340. }
  1341. }
  1342. const backtickRuns = (line) => {
  1343. const runs = [];
  1344. for (let i = 0; i < line.length;) {
  1345. if (line[i] !== '`') {
  1346. i += 1;
  1347. continue;
  1348. }
  1349. const start = i;
  1350. while (i < line.length && line[i] === '`') i += 1;
  1351. runs.push({ start, end: i, length: i - start, next: -1 });
  1352. }
  1353. const nextByLength = new Map();
  1354. for (let i = runs.length - 1; i >= 0; i -= 1) {
  1355. runs[i].next = nextByLength.get(runs[i].length) ?? -1;
  1356. nextByLength.set(runs[i].length, i);
  1357. }
  1358. return runs;
  1359. };
  1360. for (const originalLine of lines) {
  1361. const lineEnd = offset + originalLine.length;
  1362. let visibleLine = chars.slice(offset, lineEnd).join('');
  1363. const fenceMatch = /^[ \t]{0,3}(`{3,}|~{3,})([^\n]*)$/.exec(visibleLine);
  1364. let fencedLine = false;
  1365. if (openFence) {
  1366. fencedLine = true;
  1367. if (
  1368. fenceMatch
  1369. && fenceMatch[1][0] === openFence.char
  1370. && fenceMatch[1].length >= openFence.length
  1371. && /^[ \t]*\r?$/.test(fenceMatch[2])
  1372. ) openFence = null;
  1373. } else if (fenceMatch) {
  1374. fencedLine = true;
  1375. openFence = { char: fenceMatch[1][0], length: fenceMatch[1].length };
  1376. }
  1377. const indented = /^(?: {4}|\t)\S/.test(visibleLine);
  1378. const indentedCode = !fencedLine
  1379. && indented
  1380. && (inIndentedBlock || (previousBlank && !listContext));
  1381. if (!fencedLine && !indentedCode) {
  1382. const runs = backtickRuns(visibleLine);
  1383. let runIndex = 0;
  1384. let cursor = 0;
  1385. while (cursor < visibleLine.length) {
  1386. while (runIndex < runs.length && runs[runIndex].start < cursor) runIndex += 1;
  1387. const commentIndex = visibleLine.indexOf('<!--', cursor);
  1388. const run = runs[runIndex];
  1389. if (run && (commentIndex === -1 || run.start < commentIndex)) {
  1390. if (run.next !== -1) {
  1391. cursor = runs[run.next].end;
  1392. runIndex = run.next + 1;
  1393. } else {
  1394. cursor = run.end;
  1395. runIndex += 1;
  1396. }
  1397. continue;
  1398. }
  1399. if (commentIndex === -1) break;
  1400. const openingIndex = offset + commentIndex;
  1401. while (
  1402. closingCursor < commentClosings.length
  1403. && commentClosings[closingCursor] < openingIndex + 2
  1404. ) closingCursor += 1;
  1405. const closingIndex = commentClosings[closingCursor] ?? -1;
  1406. const end = closingIndex === -1 ? source.length : closingIndex + 3;
  1407. if (closingIndex !== -1) closingCursor += 1;
  1408. blankRange(chars, openingIndex, end);
  1409. maskedHtmlComments += 1;
  1410. cursor = Math.min(visibleLine.length, end - offset);
  1411. }
  1412. }
  1413. visibleLine = chars.slice(offset, lineEnd).join('');
  1414. const layoutChars = visibleLine.split('');
  1415. if (fencedLine) {
  1416. blankRange(layoutChars, 0, layoutChars.length);
  1417. } else {
  1418. const inlineRe = /(`+)(?:(?!\1)[^\n])+\1/g;
  1419. let inlineMatch;
  1420. while ((inlineMatch = inlineRe.exec(visibleLine)) !== null) {
  1421. blankRange(layoutChars, inlineMatch.index, inlineMatch.index + inlineMatch[0].length);
  1422. }
  1423. }
  1424. const layoutLine = layoutChars.join('');
  1425. const blank = layoutLine.trim() === '';
  1426. if (indentedCode) inIndentedBlock = true;
  1427. else if (!blank) inIndentedBlock = false;
  1428. if (!blank && !indentedCode && !fencedLine) {
  1429. if (/^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:\s|$)/.test(layoutLine)) listContext = true;
  1430. else if (/^\S/.test(layoutLine)) listContext = false;
  1431. }
  1432. previousBlank = blank;
  1433. offset = lineEnd + 1;
  1434. }
  1435. return maskedHtmlComments;
  1436. }
  1437. // Mask source-only Markdown spans while preserving source offsets. The
  1438. // detector can then score what a reader sees without making later issue
  1439. // indexes or sentence highlights point at the wrong source location.
  1440. function maskRenderedMarkdown(text) {
  1441. const chars = text.split('');
  1442. let maskedFrontmatter = 0;
  1443. const frontmatter = initialFrontmatterRange(text);
  1444. if (frontmatter) {
  1445. blankRange(chars, frontmatter.start, frontmatter.end);
  1446. maskedFrontmatter = 1;
  1447. }
  1448. const maskedHtmlComments = maskHtmlCommentsOutsideCode(chars);
  1449. return { text: chars.join(''), maskedFrontmatter, maskedHtmlComments };
  1450. }
  1451. // Ignore regions (#351): an author can exclude a passage from scoring, such
  1452. // as a specimen of AI prose quoted on purpose, by wrapping it in
  1453. // <!-- avoid-ai-writing:ignore-start --> … <!-- avoid-ai-writing:ignore-end -->
  1454. // A marker counts only as a whole line: the full comment, at most three
  1455. // spaces of indent, nothing else on the line. Starts nest: each start needs
  1456. // its own end, and the region runs from the outermost start to its matching
  1457. // end. An unclosed start runs to the end of the text; an end with no open
  1458. // start is ignored. The region is blanked in place, so offsets still
  1459. // address the source.
  1460. //
  1461. // Markers are found by ONE left-to-right scan rather than a stack of masks,
  1462. // because separate masks disagree about who owns overlapping text: a fence
  1463. // inside a comment would swallow the comment's `-->`, and a `<pre>` inside
  1464. // a comment would open a container. After skipping initial YAML
  1465. // frontmatter, the scan recognizes, in order of appearance, an HTML
  1466. // comment, a Markdown fence, a top-level indented code line, an inline code
  1467. // span, and an HTML <pre>, <code>, <script>, or <style> element. Whichever
  1468. // construct opens first owns the text until its own close, so a marker, a
  1469. // fence, a tag, or a comment inside another construct is inert. Anything
  1470. // unclosed runs to the end of the text: when in doubt, the marker does
  1471. // nothing and the prose stays scored. Every character is visited a bounded
  1472. // number of times.
  1473. const IGNORE_MARKER_LINE_RE = /^ {0,3}<!--[ \t]*avoid-ai-writing:ignore-(start|end)[ \t]*-->[ \t]*$/i;
  1474. const MARKER_FENCE_RE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
  1475. const HTML_CODE_OPEN_RE = /<(pre|code|script|style)\b[^>]*>/iy;
  1476. const HTML_CODE_CLOSE_RE = {
  1477. pre: /<\/pre/gi,
  1478. code: /<\/code/gi,
  1479. script: /<\/script/gi,
  1480. style: /<\/style/gi,
  1481. };
  1482. function findIgnoreMarkers(text) {
  1483. const n = text.length;
  1484. const markers = [];
  1485. const lineEndAt = (from) => {
  1486. let i = from;
  1487. while (i < n && text[i] !== '\n' && text[i] !== '\r') i += 1;
  1488. return i;
  1489. };
  1490. const nextLineAt = (end) => (text[end] === '\r' && text[end + 1] === '\n' ? end + 2 : end + 1);
  1491. const isLineStart = (i) => i === 0 || text[i - 1] === '\n' || (text[i - 1] === '\r' && text[i] !== '\n');
  1492. // Backtick runs in [from, to), keyed by start, each with the end of the
  1493. // next run of the same length on the line (-1 when unmatched). Built at
  1494. // most once per line segment and never rebuilt after a comment or HTML
  1495. // element closes mid-line: rebuilding made a long line that alternates
  1496. // code spans and <code> elements quadratic. Runs inside a construct the
  1497. // scan jumps over are simply never visited.
  1498. const backtickRuns = (from, to) => {
  1499. const list = [];
  1500. for (let i = from; i < to;) {
  1501. if (text[i] !== '`') { i += 1; continue; }
  1502. const start = i;
  1503. while (i < to && text[i] === '`') i += 1;
  1504. list.push({ start, end: i, length: i - start, closeEnd: -1 });
  1505. }
  1506. const nextByLength = new Map();
  1507. for (let i = list.length - 1; i >= 0; i -= 1) {
  1508. const next = nextByLength.get(list[i].length);
  1509. if (next) list[i].closeEnd = next.end;
  1510. nextByLength.set(list[i].length, list[i]);
  1511. }
  1512. return new Map(list.map((run) => [run.start, run]));
  1513. };
  1514. let pos = 0;
  1515. const frontmatter = initialFrontmatterRange(text);
  1516. if (frontmatter) pos = frontmatter.end;
  1517. let lineEnd = -1;
  1518. let runs = null;
  1519. let prevBlank = true;
  1520. let inIndented = false;
  1521. let listContext = false;
  1522. while (pos < n) {
  1523. if (pos > lineEnd) {
  1524. // Entering a new line, or resuming mid-line after a construct that
  1525. // spanned lines. Only a true line start gets the line-level checks.
  1526. lineEnd = lineEndAt(pos);
  1527. runs = null;
  1528. if (isLineStart(pos)) {
  1529. const line = text.slice(pos, lineEnd);
  1530. const blank = line.trim() === '';
  1531. const fence = MARKER_FENCE_RE.exec(line);
  1532. if (fence && !(fence[1][0] === '`' && fence[2].includes('`'))) {
  1533. let close = n;
  1534. for (let scan = nextLineAt(lineEnd); scan < n;) {
  1535. const end = lineEndAt(scan);
  1536. const closing = MARKER_FENCE_RE.exec(text.slice(scan, end));
  1537. if (
  1538. closing
  1539. && closing[1][0] === fence[1][0]
  1540. && closing[1].length >= fence[1].length
  1541. && closing[2].trim() === ''
  1542. ) {
  1543. close = end;
  1544. break;
  1545. }
  1546. scan = nextLineAt(end);
  1547. }
  1548. pos = close;
  1549. prevBlank = false;
  1550. inIndented = false;
  1551. continue;
  1552. }
  1553. const indented = !blank && /^(?: {4}|\t)/.test(line);
  1554. if (indented && (inIndented || (prevBlank && !listContext))) {
  1555. inIndented = true;
  1556. prevBlank = false;
  1557. pos = lineEnd;
  1558. continue;
  1559. }
  1560. if (!blank) {
  1561. inIndented = false;
  1562. if (/^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:\s|$)/.test(line)) listContext = true;
  1563. else if (/^\S/.test(line)) listContext = false;
  1564. }
  1565. prevBlank = blank;
  1566. const marker = IGNORE_MARKER_LINE_RE.exec(line);
  1567. if (marker) {
  1568. markers.push({ kind: marker[1].toLowerCase(), start: pos, end: lineEnd });
  1569. pos = lineEnd;
  1570. continue;
  1571. }
  1572. }
  1573. }
  1574. const ch = text[pos];
  1575. if (ch === '<') {
  1576. if (text.startsWith('<!--', pos)) {
  1577. const close = text.indexOf('-->', pos + 4);
  1578. pos = close === -1 ? n : close + 3;
  1579. continue;
  1580. }
  1581. HTML_CODE_OPEN_RE.lastIndex = pos;
  1582. const open = HTML_CODE_OPEN_RE.exec(text);
  1583. if (open) {
  1584. const closeRe = HTML_CODE_CLOSE_RE[open[1].toLowerCase()];
  1585. closeRe.lastIndex = pos + open[0].length;
  1586. const close = closeRe.exec(text);
  1587. pos = close === null ? n : close.index;
  1588. continue;
  1589. }
  1590. } else if (ch === '`') {
  1591. if (!runs) runs = backtickRuns(pos, lineEnd);
  1592. const run = runs.get(pos);
  1593. if (run && run.closeEnd !== -1) {
  1594. pos = run.closeEnd;
  1595. continue;
  1596. }
  1597. pos += run ? run.length : 1;
  1598. continue;
  1599. }
  1600. pos += 1;
  1601. }
  1602. return markers;
  1603. }
  1604. function maskIgnoreRegions(text) {
  1605. if (!/avoid-ai-writing:ignore-/i.test(text)) return { text, ignoredRegions: 0 };
  1606. const chars = text.split('');
  1607. let ignoredRegions = 0;
  1608. let depth = 0;
  1609. let openAt = -1;
  1610. for (const marker of findIgnoreMarkers(text)) {
  1611. if (marker.kind === 'start') {
  1612. if (depth === 0) openAt = marker.start;
  1613. depth += 1;
  1614. } else if (depth > 0) {
  1615. depth -= 1;
  1616. if (depth === 0) {
  1617. blankRange(chars, openAt, marker.end);
  1618. ignoredRegions += 1;
  1619. }
  1620. }
  1621. }
  1622. if (depth > 0) {
  1623. blankRange(chars, openAt, chars.length);
  1624. ignoredRegions += 1;
  1625. }
  1626. return { text: chars.join(''), ignoredRegions };
  1627. }
  1628. // Blank the content of double-quoted spans and keep the quote marks, so
  1629. // offsets and the curly-quote signal survive. Based on QUOTED_SPAN in
  1630. // scripts/self-scan.js minus its single-quote branch: apostrophes in
  1631. // contractions and possessives would pair up across ordinary prose. A
  1632. // straight quote touching a letter or digit on its outer side is an inch
  1633. // mark, not a quotation. Empty `""` pairs are consumed so they cannot
  1634. // shift the pairing, and a span never crosses a backtick into code. A
  1635. // nested quotation escaped as a pair (`\"…\"`) stays inside the span. A
  1636. // lone `\"` still closes it, so a path such as `"C:\Temp\"` cannot run on
  1637. // into the next quotation.
  1638. const QUOTED_SPAN_RE = /(?<![\p{L}\p{N}])"(?:\\"[^"`\n]{0,300}\\"|[^"`\n]){0,300}"(?![\p{L}\p{N}])|“[^“”`\n]{0,300}”/gu;
  1639. function maskQuotedSpans(text) {
  1640. let maskedQuotes = 0;
  1641. const masked = text.replace(QUOTED_SPAN_RE, (span) => {
  1642. // Escaped pairs can chain past the 300-character cap; score those.
  1643. // Count code points, as the `u` regex does, so emoji do not trip it.
  1644. if (span.length === 2 || [...span].length > 302) return span;
  1645. maskedQuotes += 1;
  1646. // Keep sentence punctuation so getSentences splits where it did before.
  1647. return span[0] + span.slice(1, -1).replace(/[^.!?]/g, ' ') + span[span.length - 1];
  1648. });
  1649. return { text: masked, maskedQuotes };
  1650. }
  1651. // A `>` opens a blockquote line when a space, the line end, a letter, a
  1652. // nested `>`, an opening double quote, emphasis, a link, or a list marker
  1653. // (`- `, `+ `, `1. `) follows it. That covers compact Markdown (`>text`)
  1654. // without swallowing comparisons such as `>=5` or `>-1`. A single quote is
  1655. // left out, as in the inline quote pass.
  1656. const BLOCKQUOTE_LINE_RE = /^\s*>(?:$|[\s\p{L}>"“*_[]|[-+]\s|\d{1,9}[.)]\s)/u;
  1657. function maskBlockquotes(text) {
  1658. const chars = text.split('');
  1659. const lines = [];
  1660. const lineRe = /[^\r\n]*(?:\r\n|\n|\r|$)/g;
  1661. let match;
  1662. while ((match = lineRe.exec(text)) !== null && match[0]) {
  1663. const body = match[0].replace(/(?:\r\n|\n|\r)$/, '');
  1664. lines.push({ text: body, start: match.index, end: match.index + body.length });
  1665. }
  1666. let quotedLines = 0;
  1667. for (const line of lines) {
  1668. if (BLOCKQUOTE_LINE_RE.test(line.text)) {
  1669. blankRange(chars, line.start, line.end);
  1670. quotedLines += 1;
  1671. }
  1672. }
  1673. return { text: chars.join(''), quotedLines };
  1674. }
  1675. // Keep the historical deletion behavior for default plain mode. Paragraph-
  1676. // scoped rules depend on the surrounding lines being rejoined exactly this
  1677. // way, so changing this prepass would change scores for existing callers.
  1678. function stripBlockquotes(text, sourceMap) {
  1679. const rawLines = text.split(/\r?\n/);
  1680. const stripIndexes = new Set();
  1681. let blankedLines = 0;
  1682. for (let i = 0; i < rawLines.length; i += 1) {
  1683. // A bare CR is not split here, so a CR-only document is one element.
  1684. // Deleting it would take the unquoted lines after the quote with it,
  1685. // so blank each quoted CR-separated line in place instead. Blanking
  1686. // keeps the element length, so the offsets below still line up.
  1687. if (rawLines[i].includes('\r')) {
  1688. rawLines[i] = rawLines[i].split('\r').map((line) => {
  1689. if (!BLOCKQUOTE_LINE_RE.test(line)) return line;
  1690. blankedLines += 1;
  1691. return ' '.repeat(line.length);
  1692. }).join('\r');
  1693. } else if (BLOCKQUOTE_LINE_RE.test(rawLines[i])) {
  1694. stripIndexes.add(i);
  1695. }
  1696. }
  1697. const kept = rawLines
  1698. .map((_, index) => index)
  1699. .filter((index) => !stripIndexes.has(index));
  1700. const result = {
  1701. text: kept.map((index) => rawLines[index]).join('\n'),
  1702. quotedLines: stripIndexes.size + blankedLines,
  1703. };
  1704. if (!Array.isArray(sourceMap)) return result;
  1705. const lineStarts = [];
  1706. let offset = 0;
  1707. for (let i = 0; i < rawLines.length; i += 1) {
  1708. lineStarts.push(offset);
  1709. offset += rawLines[i].length;
  1710. if (i < rawLines.length - 1) {
  1711. if (text[offset] === '\r') offset += 1;
  1712. if (text[offset] === '\n') offset += 1;
  1713. }
  1714. }
  1715. const mapped = [];
  1716. for (let i = 0; i < kept.length; i += 1) {
  1717. const lineIndex = kept[i];
  1718. const start = lineStarts[lineIndex];
  1719. appendMapRange(mapped, sourceMap, start, start + rawLines[lineIndex].length);
  1720. if (i < kept.length - 1) {
  1721. const separatorStart = start + rawLines[lineIndex].length;
  1722. const newlineIndex = text[separatorStart] === '\r' ? separatorStart + 1 : separatorStart;
  1723. mapped.push(sourceMap[newlineIndex]);
  1724. }
  1725. }
  1726. return { ...result, sourceMap: mapped };
  1727. }
  1728. function maskTopLevelIndentedCode(chars, { listAware = false } = {}) {
  1729. const lines = chars.join('').split('\n');
  1730. let offset = 0;
  1731. let inBlock = false;
  1732. let previousBlank = true;
  1733. let listContext = false;
  1734. for (let i = 0; i < lines.length; i += 1) {
  1735. const line = lines[i];
  1736. const indented = /^(?: {4}|\t)\S/.test(line);
  1737. const blank = line.trim() === '';
  1738. const isCode = indented && (inBlock || (previousBlank && (!listAware || !listContext)));
  1739. if (isCode) {
  1740. blankRange(chars, offset, offset + line.length);
  1741. inBlock = true;
  1742. } else if (!blank) {
  1743. inBlock = false;
  1744. }
  1745. if (listAware && !blank && !isCode) {
  1746. if (/^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:\s|$)/.test(line)) listContext = true;
  1747. else if (/^\S/.test(line)) listContext = false;
  1748. }
  1749. previousBlank = blank;
  1750. offset += line.length + 1;
  1751. }
  1752. }
  1753. function maskYamlFrontmatter(chars) {
  1754. const lines = chars.join('').split('\n');
  1755. const bare = (line) => line.replace(/\r$/, '');
  1756. const first = bare(lines[0]).replace(/^\uFEFF/, '');
  1757. if (first !== '---' || lines.length < 2 || /^\s*$/.test(bare(lines[1]))) return;
  1758. let closingLine = -1;
  1759. for (let i = 1; i < lines.length; i += 1) {
  1760. if (bare(lines[i]) === '---') {
  1761. closingLine = i;
  1762. break;
  1763. }
  1764. }
  1765. if (closingLine === -1) return;
  1766. let end = 0;
  1767. for (let i = 0; i <= closingLine; i += 1) {
  1768. end += lines[i].length;
  1769. if (i < lines.length - 1) end += 1;
  1770. }
  1771. blankRange(chars, 0, end);
  1772. }
  1773. function maskYamlMetadata(chars) {
  1774. const lines = chars.join('').split('\n');
  1775. let offset = 0;
  1776. let nestedAfterIndent = null;
  1777. for (const line of lines) {
  1778. const bare = line.replace(/\r$/, '');
  1779. // Lowercase keys are the common unfenced-YAML shape. Keeping this
  1780. // case-sensitive prevents prose labels such as "Note: ..." from being
  1781. // mistaken for metadata and silencing a real copyedit on the line.
  1782. const key = bare.match(/^([ \t]*)(?:-[ \t]+)?[a-z_][a-z0-9_.-]*[ \t]*:(.*)$/);
  1783. const indentation = (bare.match(/^[ \t]*/) || [''])[0].length;
  1784. let shouldMask = false;
  1785. if (key) {
  1786. shouldMask = true;
  1787. nestedAfterIndent = key[2].trim() === '' ? key[1].length : null;
  1788. } else if (nestedAfterIndent !== null && bare.trim() !== '' && indentation > nestedAfterIndent) {
  1789. shouldMask = true;
  1790. } else if (bare.trim() === '') {
  1791. nestedAfterIndent = null;
  1792. } else {
  1793. nestedAfterIndent = null;
  1794. }
  1795. if (shouldMask) blankRange(chars, offset, offset + line.length);
  1796. offset += line.length + 1;
  1797. }
  1798. }
  1799. function maskMarkdownTables(chars) {
  1800. const lines = chars.join('').split('\n');
  1801. // Tested against the trimmed line: the pattern already allows surrounding
  1802. // whitespace, and its adjacent `\s*` groups backtrack quadratically on a
  1803. // long whitespace run, so a line of masked comments or blank padding
  1804. // cost seconds before it was rejected (#235).
  1805. const delimiter = /^\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)+\|?$/;
  1806. const rows = new Set();
  1807. for (let i = 0; i < lines.length; i += 1) {
  1808. const candidate = lines[i].trim();
  1809. if (!candidate.includes('---') || !delimiter.test(candidate)) continue;
  1810. if (i > 0 && lines[i - 1].includes('|')) rows.add(i - 1);
  1811. rows.add(i);
  1812. for (let j = i + 1; j < lines.length && lines[j].includes('|'); j += 1) rows.add(j);
  1813. }
  1814. let offset = 0;
  1815. for (let i = 0; i < lines.length; i += 1) {
  1816. if (rows.has(i)) blankRange(chars, offset, offset + lines[i].length);
  1817. offset += lines[i].length + 1;
  1818. }
  1819. }
  1820. function maskDelimitedQuotes(chars, open, close, apostropheAware = false) {
  1821. const isWord = (char) => char !== undefined && /[a-z0-9]/i.test(char);
  1822. const isEscaped = (index) => {
  1823. let slashes = 0;
  1824. for (let i = index - 1; i >= 0 && chars[i] === '\\'; i -= 1) slashes += 1;
  1825. return slashes % 2 === 1;
  1826. };
  1827. let start = -1;
  1828. for (let i = 0; i < chars.length; i += 1) {
  1829. if (start === -1) {
  1830. if (chars[i] === open && !isEscaped(i) && (!apostropheAware || !isWord(chars[i - 1]))) start = i;
  1831. } else if (chars[i] === close && !isEscaped(i) && (!apostropheAware || !isWord(chars[i + 1]))) {
  1832. blankRange(chars, start, i + 1);
  1833. start = -1;
  1834. }
  1835. }
  1836. }
  1837. // Additional protected spans for the hyphenation copyedit. Unlike general
  1838. // AI-tell matching, this rule must not "correct" a literal spelling in a
  1839. // quote, URL, path, filename, command flag, or Markdown blockquote.
  1840. function maskHyphenationProtected(text) {
  1841. const chars = maskCode(text).split('');
  1842. maskTopLevelIndentedCode(chars);
  1843. maskYamlFrontmatter(chars);
  1844. maskYamlMetadata(chars);
  1845. maskMarkdownTables(chars);
  1846. maskDelimitedQuotes(chars, '"', '"');
  1847. maskDelimitedQuotes(chars, '“', '”');
  1848. maskDelimitedQuotes(chars, "'", "'", true);
  1849. maskDelimitedQuotes(chars, '‘', '’', true);
  1850. const maskMatches = (regex) => {
  1851. const source = chars.join('');
  1852. let match;
  1853. while ((match = regex.exec(source)) !== null) {
  1854. blankRange(chars, match.index, match.index + match[0].length);
  1855. }
  1856. };
  1857. maskMatches(/^[ \t]*>[^\n]*$/gm);
  1858. maskMatches(/\b(?:https?:\/\/|www\.)[^\s<>]+/gi);
  1859. maskMatches(/<[!?/]?[a-z][^<>\n]*>/gi);
  1860. maskMatches(/(?<![a-z0-9_-])--?[a-z0-9][a-z0-9-]{0,127}/gi);
  1861. // Paths and filenames use bounded components. Besides preventing
  1862. // superlinear backtracking on long kebab blobs, the explicit prefix and
  1863. // trailing-slash forms cover single-component paths such as C:\\code-base,
  1864. // ~/code-base, /code-base, and code-base/.
  1865. maskMatches(/(?:[a-z]:[\\/]|\.{1,2}[\\/]|~[\\/]|[\\/])[a-z0-9_.-]{1,255}/gi);
  1866. maskMatches(/(?:[a-z]:[\\/]|(?:\.\.?[\\/])?)(?:[a-z0-9_.-]{1,64}[\\/]){1,128}[a-z0-9_.-]{1,64}/gi);
  1867. maskMatches(/\b[a-z0-9_.]{0,63}-[a-z0-9_.-]{1,64}[\\/]/gi);
  1868. maskMatches(/\b[a-z0-9_.-]{1,64}-[a-z0-9_.-]{1,64}\.[a-z0-9]{1,16}\b/gi);
  1869. // Technical identifiers that are distinguishable from ordinary prose:
  1870. // selectors, scoped packages, versioned tokens, assignments, and kebab
  1871. // names next to an explicit identifier cue. Plain lowercase compounds
  1872. // remain visible to the curated copyedit patterns below.
  1873. maskMatches(/[.#][a-z_][a-z0-9_.-]{0,63}-[a-z0-9_.-]{1,64}\b/gi);
  1874. maskMatches(/@[a-z0-9_.-]{1,64}\/[a-z0-9_.-]{1,64}-[a-z0-9_.-]{1,64}(?:@[^\s,;)\]}]{1,32})?/gi);
  1875. maskMatches(/\b[a-z0-9_.-]{1,64}-[a-z0-9_.-]{1,64}@[~^]?v?\d[a-z0-9*_.+-]{0,31}\b/gi);
  1876. maskMatches(/\b(?:[a-z0-9_.-]{0,64}\d[a-z0-9_.-]{0,64}-[a-z0-9_.-]{1,64}|[a-z0-9_.-]{1,64}-[a-z0-9_.-]{0,64}\d[a-z0-9_.-]{0,64})\b/gi);
  1877. maskMatches(/\b[a-z_][a-z0-9_.]{0,63}(?:-[a-z0-9_.]{1,64}){1,8}(?=[ \t]*[=:])/gi);
  1878. maskMatches(/\b[a-z_][a-z0-9_.]{0,63}(?:-[a-z0-9_.]{1,64}){1,8}(?=[ \t]+(?:npm[ \t]+)?(?:package|module|class|selector|config(?:uration)?[ \t]+key|key|identifier|property|setting|token|slug|command|option)\b)/gi);
  1879. maskMatches(/\b(?:(?:file(?:name)?|directory|folder|package|module|class|selector|config(?:uration)?[ \t]+key|identifier|property|setting|token|slug|command|option)(?:[ \t]+(?:named|called|is|was))?|key[ \t]+(?:named|called|is|was))[ \t]+(?:@[a-z0-9_.-]{1,64}\/)?[a-z_][a-z0-9_.]{0,63}(?:-[a-z0-9_.]{1,64}){1,8}\b/gi);
  1880. maskMatches(/\b(?:npm|pnpm|yarn)[ \t]+(?:add|install)[ \t]+(?:@[a-z0-9_.-]{1,64}\/)?[a-z0-9_.]{1,64}(?:-[a-z0-9_.]{1,64}){1,8}/gi);
  1881. return chars.join('');
  1882. }
  1883. function findUnnecessaryHyphenation(text) {
  1884. const scanText = maskHyphenationProtected(text);
  1885. const issues = [];
  1886. for (const entry of UNNECESSARY_HYPHENATION) {
  1887. const regex = new RegExp(entry.pattern.source, entry.pattern.flags);
  1888. let match;
  1889. while ((match = regex.exec(scanText)) !== null) {
  1890. issues.push({
  1891. type: 'unnecessary-hyphenation',
  1892. text: match[0],
  1893. severity: 'medium',
  1894. suggestion: typeof entry.suggestion === 'function'
  1895. ? entry.suggestion(match[0])
  1896. : entry.suggestion,
  1897. });
  1898. }
  1899. }
  1900. return issues;
  1901. }
  1902. // ─── Forms that open with `#` but are not social tags ──────────────
  1903. // `#` is overloaded in technical prose and the hashtag rule counts every
  1904. // `#word` it sees, so these are subtracted before the threshold applies:
  1905. // #88, #1234 issue and PR references
  1906. // #1a2b3c CSS hex colours, 6 or 8 chars AND containing a digit
  1907. // #include, #ifndef C preprocessor directives
  1908. // Deliberately NOT carving out 3- and 4-digit hex: #dad, #cafe, #b2b, #e2e,
  1909. // #ace, #face and #bad are real tags, and subtracting them cost true
  1910. // positives on exactly the stuffed-block shape this rule exists to catch.
  1911. // Real palettes are dominated by 6-digit values, so a CSS paragraph still
  1912. // lands under the threshold without the short forms.
  1913. // `owner/repo#88` and URL fragments need no carve-out: the char before `#`
  1914. // is a word char, so the rule's own anchor already rejects them.
  1915. // Ambiguous word tags stay counted on purpose. `#general` as a channel and
  1916. // `#general` as a tag are the same token, and separating them needs a guess
  1917. // that costs more precision on real tag blocks than the carve-out buys.
  1918. // Requires at least one digit: #decade, #facade, #deadbeef are a-f words and
  1919. // real tags. Every actual palette value in the wild carries a digit.
  1920. const HEX_COLOUR = /^(?=[0-9a-f]*\d)(?:[0-9a-f]{6}|[0-9a-f]{8})$/i;
  1921. const CPP_DIRECTIVE = /^(?:include|define|undef|if|ifdef|ifndef|elif|else|endif|pragma|error|warning|line)$/;
  1922. function isSocialTag(tag) {
  1923. return !/^\d+$/.test(tag) && !HEX_COLOUR.test(tag) && !CPP_DIRECTIVE.test(tag);
  1924. }
  1925. // ─── Title Case Section Headers in non-technical prose ─────────────
  1926. // "Strategic Negotiations And Key Partnerships" — every content word
  1927. // capitalized. Acceptable in API docs, ML papers, news headlines. Tell
  1928. // in marketing/personal/blog prose. Skipped when contextMode is
  1929. // 'technical'; runs for general, marketing, and personal.
  1930. //
  1931. // The optional `#{1,6}` prefix is load-bearing (#62): without it the `^[A-Z]`
  1932. // anchor required the line to START with a capital, so `## Benefits And
  1933. // Strategic Considerations` never matched — the first character is `#`. The
  1934. // rule missed the single most common way a heading is actually written, while
  1935. // catching the bare-line form it is usually converted from. Reported by a
  1936. // downstream vendoring the detector.
  1937. //
  1938. // Setext headings (`Title`/`=====`) need no prefix: their text line is bare
  1939. // and already matched by this same pattern.
  1940. // Interior tokens accept Title Case words, acronyms (`AI`, `API`, `CLI`) and
  1941. // the capitalised single-letter words `A` and `I`. The first and last tokens
  1942. // stay ordinary `[A-Z][a-z]+` words, which also excludes all-caps banner
  1943. // lines (`## HTTP API REFERENCE`) whose leading token is not Title Case.
  1944. //
  1945. // Separators and trailing whitespace are horizontal only (`[ \t]`), so a
  1946. // match can never run past one physical line: `\s` also eats newlines, which
  1947. // let two unrelated lines or a blank-line-separated fragment combine into a
  1948. // single heading match (GH-291).
  1949. const TITLE_CASE_HEADER = /^(?:#{1,6}[ \t]+)?([A-Z][a-z]+(?:[ \t]+(?:[A-Z][a-z]+|A|I|[A-Z]{2,}|and|or|of|the|in|for|to|a|an))+[ \t]+[A-Z][a-z]+)[ \t]*$/gm;
  1950. // ─── Parenthetical hedging asides ──────────────────────────────────
  1951. // "(and increasingly, X)", "(or more precisely, Y)", "(though to be
  1952. // fair, Z)" — pseudo-aside that adds no information but performs
  1953. // thoughtfulness. Different from genuine human parentheticals which
  1954. // tend to be tangents or clarifications, not hedges.
  1955. const PARENTHETICAL_HEDGE = [
  1956. /\(\s*(?:and\s+)?(?:increasingly|notably|importantly|crucially|interestingly|perhaps)[,]?\s+[^)]{3,60}\)/gi,
  1957. /\(\s*or\s+more\s+(?:precisely|accurately|specifically)[,]?\s+[^)]{3,60}\)/gi,
  1958. /\(\s*though\s+to\s+be\s+fair[,]?\s+[^)]{3,60}\)/gi,
  1959. /\(\s*at\s+least\s+(?:in\s+)?(?:theory|principle|part)[,]?\s+[^)]{0,60}\)/gi,
  1960. ];
  1961. // ─── Confidence calibration ────────────────────────────────────────
  1962. const CONFIDENCE_CALIBRATION = [
  1963. /\binterestingly\b/gi,
  1964. /\bsurprisingly\b/gi,
  1965. /\bimportantly\b/gi,
  1966. /\bsignificantly\b/gi,
  1967. /\bcertainly\b/gi,
  1968. /\bundoubtedly\b/gi,
  1969. /\bwithout\s+a\s+doubt\b/gi,
  1970. ];
  1971. // ─── Social endorsement / CTA closers ──────────────────────────────
  1972. // The curatorial sign-off LLMs append to LinkedIn / X posts that share
  1973. // or recommend something — usually a colon teeing up a link. Distinct
  1974. // from the bare "worth reading" word-table entry (a single weak word)
  1975. // and from infomercial hooks (mid-flow teasers): this is the
  1976. // demonstrative-anchored endorsement — "THIS one is worth your time:",
  1977. // "do yourself a favor and read this", "thank me later" — that performs
  1978. // a recommendation without giving the reader a reason to click.
  1979. //
  1980. // Precision-first: each pattern carries an anchor so it stays off the
  1981. // literal-verb prose a human writes. The demonstrative object ("read
  1982. // THIS", not "read the runbook"), the trailing-terminal lookahead on
  1983. // "miss this" / "bookmark this" (the closing-line shape, not "miss this
  1984. // meeting" / "bookmark this page"), and the sentence-initial lookbehind
  1985. // on "thank me later" / "save this for later" (the imperative CTA, not
  1986. // "she will thank me later") all exist to suppress false positives on
  1987. // ordinary instructional/conversational text. Apostrophe classes admit
  1988. // the curly ' (U+2019) because LinkedIn / Word / macOS auto-curl it —
  1989. // the straight-only form would miss the canonical "you won't" closer.
  1990. const SOCIAL_CTA_CLOSER = [
  1991. /\bthis\s+one['’]?s?\s+(?:is\s+)?(?:well\s+|totally\s+|absolutely\s+|definitely\s+|really\s+|truly\s+|easily\s+|more\s+than\s+)?worth\s+(?:your\s+time|the\s+read|a\s+read|every\s+(?:minute|second)|reading|watching|a\s+listen|a\s+watch|a\s+look|it)\b/gi,
  1992. /\bthis\s+one['’]?s?\s+(?:is\s+)?a\s+must[-\s]?(?:read|watch|listen|see)\b/gi,
  1993. /\b(?:highly|strongly|can['’]?t|cannot)\s+recommend\w*\s+(?:giving\s+)?(?:this|it)\s+(?:one\s+)?a\s+(?:read|listen|watch|look|go)\b/gi,
  1994. /\bdo\s+yourself\s+a\s+favou?r\s+and\s+(?:read|watch|check\s+out)\s+(?:this|it)\b/gi,
  1995. /\byou\s+(?:really\s+)?(?:won['’]?t|do\s*n['’]?t|will\s+not|do\s+not)\s+want\s+to\s+miss\s+this(?:\s+one)?(?=\s*(?:[:.!\n]|$))/gi,
  1996. /(?<=^|[,.!?:\n]\s{0,4})(?:you\s+can\s+)?thank\s+me\s+later\b/gim,
  1997. /(?<=^|[.!?:\n]\s{0,4})save\s+this\s+(?:one\s+)?for\s+later\b/gim,
  1998. /\bbookmark\s+this(?:\s+(?:one|post|thread))?(?=\s*(?:[:.!\n]|$))/gi,
  1999. /\bdo\s*n['’]?t\s+sleep\s+on\s+this\b/gi,
  2000. /\btrust\s+me,?\s+(?:on\s+this|you['’]?ll)\b/gi,
  2001. ];
  2002. // ─── Unnecessary hyphenation (#107) ───────────────────────────────
  2003. // Precision-first subclasses only. The general question of whether a
  2004. // compound modifier is established English needs editorial judgment and
  2005. // remains in SKILL.md; the engine covers only curated open/closed forms and
  2006. // compounds whose surrounding syntax makes the unhyphenated form clear.
  2007. const UNNECESSARY_HYPHENATION = [
  2008. // Welded open noun phrases reported in #107. Match the complete phrase so
  2009. // a project-specific spelling of the pair in another role is not swept in.
  2010. { pattern: /\bresearch-impact\s+aggregat(?:or|ion)s?\b/g, suggestion: (match) => match.replace('research-impact', 'research impact') },
  2011. { pattern: /\bdata-source\s+strateg(?:y|ies)\b/g, suggestion: (match) => match.replace('data-source', 'data source') },
  2012. { pattern: /\bPython-package\s+usage\b/g, suggestion: (match) => match.replace('Python-package', 'Python package') },
  2013. { pattern: /\bRust-crate\s+usage\b/g, suggestion: (match) => match.replace('Rust-crate', 'Rust crate') },
  2014. { pattern: /\bsingle-Project\s+Manifest\b/g, suggestion: (match) => match.replace('single-Project', 'single Project') },
  2015. { pattern: /\btotal-downloads\s+figures?\b/g, suggestion: (match) => match.replace('total-downloads', 'total downloads') },
  2016. { pattern: /\blife-sciences-native\s+citation\s+count\b/g, suggestion: 'citation count from a life sciences source' },
  2017. // Compounds whose standard spelling is closed. Kept as a small curated
  2018. // list rather than guessing that every noun-noun pair should close up.
  2019. { pattern: /\bcode-base\b/g, suggestion: (match) => match.replace('-', '') },
  2020. { pattern: /\bdata-set\b/g, suggestion: (match) => match.replace('-', '') },
  2021. { pattern: /\btime-frame\b/g, suggestion: (match) => match.replace('-', '') },
  2022. { pattern: /\broad-map\b/g, suggestion: (match) => match.replace('-', '') },
  2023. // Attributive-only forms used adverbially or as nouns. The boundary after
  2024. // real-time / long-term is intentionally narrow: "real-time analytics"
  2025. // and "long-term plan" must not fire.
  2026. {
  2027. pattern: /\bin\s+real-time(?=\s*(?:[,.!?;:]|$)|\s+(?:(?:across|as|automatically|because|but|continuously|during|dynamically|every|for|from|immediately|instantly|on|simultaneously|through|throughout|until|via|when|while|with|without)\b))/gi,
  2028. suggestion: 'in real time',
  2029. },
  2030. {
  2031. pattern: /\b(?:for|over)\s+the\s+long-term(?=\s*(?:[,.!?;:]|$)|\s+(?:across|because|but|by|during|for|from|on|through|throughout|until|via|when|while|with|without)\b)/gi,
  2032. suggestion: (match) => match.replace(/long-term/i, 'long term'),
  2033. },
  2034. {
  2035. pattern: /\b(?:functions?|functioned|functioning|operates?|operated|operating|runs?|ran|running|works?|worked|working)\s+out-of-the-box\b/gi,
  2036. suggestion: (match) => match.replace(/out-of-the-box/i, 'out of the box'),
  2037. },
  2038. ];
  2039. // ═══ Helpers ═══════════════════════════════════════════════════════
  2040. function tokenize(text) {
  2041. return text.toLowerCase().match(/[\w'-]+/g) || [];
  2042. }
  2043. // Moving-average type-token ratio (MATTR; Covington & McFall 2010,
  2044. // doi:10.1080/09296171003643098): the share of distinct tokens in each
  2045. // run of `size` consecutive tokens, averaged over every such run. Plain
  2046. // TTR falls as a text grows, because common words keep recurring while
  2047. // new ones arrive more slowly, so a fixed threshold on it measures length.
  2048. // The windowed mean does not drift with length, and on a text of exactly
  2049. // `size` tokens it equals plain TTR.
  2050. function movingAverageTTR(tokens, size) {
  2051. const counts = new Map();
  2052. let distinct = 0;
  2053. const add = (token) => {
  2054. const n = (counts.get(token) || 0) + 1;
  2055. counts.set(token, n);
  2056. if (n === 1) distinct += 1;
  2057. };
  2058. const drop = (token) => {
  2059. const n = counts.get(token) - 1;
  2060. if (n === 0) {
  2061. counts.delete(token);
  2062. distinct -= 1;
  2063. } else {
  2064. counts.set(token, n);
  2065. }
  2066. };
  2067. for (let i = 0; i < size; i += 1) add(tokens[i]);
  2068. let sum = distinct;
  2069. for (let i = size; i < tokens.length; i += 1) {
  2070. drop(tokens[i - size]);
  2071. add(tokens[i]);
  2072. sum += distinct;
  2073. }
  2074. return sum / ((tokens.length - size + 1) * size);
  2075. }
  2076. function countWords(text) {
  2077. return (text.match(/\S+/g) || []).length;
  2078. }
  2079. function getParagraphs(text) {
  2080. return text.split(/\n\s*\n/).filter(p => p.trim().length > 0);
  2081. }
  2082. // A Markdown definition entry: optional list marker, then a bold lead term
  2083. // closed by a colon ("**Term:**" or "**Term**:") or followed by an em
  2084. // dash, plus any wrapped continuation lines up to a blank line, list item,
  2085. // bold lead, heading, or blockquote. Group 1 is the term. Used only to
  2086. // keep glossary entries out of the sentence-length uniformity sample.
  2087. const DEFINITION_ENTRY_RE = /^[ \t]*(?:(?:[-*+]|\d+[.)])[ \t]+)?\*\*([^*\n]+?):?\*\*(?:(?<=:\*\*)|[ \t]*[:—])[^\n]*(?:\n(?![ \t]*(?:$|[-*+][ \t]|\d+[.)][ \t]|\*\*|#|>))[^\n]*)*/gm;
  2088. // A glossary is at least this many entries with distinct terms. A label
  2089. // repeated as a lead ("**Note:**" on every line) is not a glossary term,
  2090. // and its lines stay in the sample.
  2091. const GLOSSARY_MIN_ENTRIES = 3;
  2092. // Text with glossary entries replaced by sentence breaks, or null when the
  2093. // text has no glossary.
  2094. function withoutGlossaryEntries(text) {
  2095. if (!text.includes('**')) return null;
  2096. const entries = [...text.matchAll(DEFINITION_ENTRY_RE)];
  2097. const counts = new Map();
  2098. for (const m of entries) {
  2099. const term = m[1].trim().toLowerCase();
  2100. counts.set(term, (counts.get(term) || 0) + 1);
  2101. }
  2102. const unique = entries.filter((m) => counts.get(m[1].trim().toLowerCase()) === 1);
  2103. if (unique.length < GLOSSARY_MIN_ENTRIES) return null;
  2104. let out = '';
  2105. let last = 0;
  2106. for (const m of unique) {
  2107. out += text.slice(last, m.index) + '.';
  2108. last = m.index + m[0].length;
  2109. }
  2110. return out + text.slice(last);
  2111. }
  2112. function getSentences(text) {
  2113. return text.split(/[.!?]+/).filter(s => s.trim().length > 5);
  2114. }
  2115. function matchPatterns(text, patterns, category, severity) {
  2116. const issues = [];
  2117. for (const pat of patterns) {
  2118. const regex = new RegExp(pat.source, pat.flags);
  2119. let match;
  2120. while ((match = regex.exec(text)) !== null) {
  2121. issues.push({
  2122. type: category,
  2123. text: match[0],
  2124. index: match.index,
  2125. severity,
  2126. suggestion: null,
  2127. });
  2128. }
  2129. }
  2130. return issues;
  2131. }
  2132. // ═══ Main analysis ═════════════════════════════════════════════════
  2133. // Upper bound for one scan. Above this we bail rather than running all
  2134. // regex passes over a huge buffer — protects page perf on pasted novels.
  2135. const MAX_WORDS = 10000;
  2136. // V2 contract defaults so early-exit paths (Empty/tooShort/tooLong)
  2137. // still return the same field shape a v2 consumer expects. Without
  2138. // this, `result.document_classification === 'AI_ONLY'` is `undefined`
  2139. // on edge inputs and fails open.
  2140. // UNSCORED is returned on empty / too-short / too-long inputs where
  2141. // we declined to score. Distinct from HUMAN_ONLY (which is a positive
  2142. // classification) so a caller can't mistake a refused scan for a
  2143. // confident human verdict — a 50k-word LLM-generated document is
  2144. // not "human", it's just outside our scoring window.
  2145. function buildV2Defaults(classification, confidence) {
  2146. const probs = classification === 'HUMAN_ONLY'
  2147. ? { human: 1, mixed: 0, ai: 0 }
  2148. : classification === 'AI_ONLY'
  2149. ? { human: 0, mixed: 0, ai: 1 }
  2150. : { human: 0.333, mixed: 0.334, ai: 0.333 };
  2151. return {
  2152. document_classification: classification,
  2153. class_probabilities: probs,
  2154. confidence_category: confidence,
  2155. highlight_sentence_for_ai: [],
  2156. };
  2157. }
  2158. // The analysis contexts and source modes analyzeText() accepts. The CLIs
  2159. // import these lists instead of keeping their own copies.
  2160. const CONTEXT_MODES = Object.freeze(['general', 'technical', 'marketing', 'personal']);
  2161. const SOURCE_MODES = Object.freeze(['plain', 'rendered-markdown']);
  2162. function analyzeText(text, options = {}) {
  2163. if (typeof text !== 'string') {
  2164. throw new TypeError('analyzeText(text): argument must be a string');
  2165. }
  2166. const VALID_CONTEXT_MODES = new Set(CONTEXT_MODES);
  2167.     const requestedMode = options.contextMode === undefined ? 'general' : options.contextMode;
  2168.     const contextMode = VALID_CONTEXT_MODES.has(requestedMode) ? requestedMode : 'general';
  2169.     const contextModeFallback = requestedMode !== contextMode ? requestedMode : null;
  2170.     // Source mode controls which parts of a Markdown file count as prose.
  2171.     // Plain remains the compatibility default. Rendered Markdown masks only
  2172.     // initial YAML frontmatter and HTML comments; source-hygiene checks for
  2173.     // hidden TODO/placeholder comments remain available through plain mode.
  2174. const VALID_SOURCE_MODES = new Set(SOURCE_MODES);
  2175.     const requestedSourceMode = options.sourceMode === undefined ? 'plain' : options.sourceMode;
  2176.     const sourceMode = VALID_SOURCE_MODES.has(requestedSourceMode) ? requestedSourceMode : 'plain';
  2177.     const sourceModeFallback = requestedSourceMode !== sourceMode ? requestedSourceMode : undefined;
  2178. if (!text || text.trim().length === 0) {
  2179. return {
  2180. ...buildV2Defaults('UNSCORED', 'low'),
  2181. score: 0,
  2182. label: 'Empty',
  2183. issues: [],
  2184. stats: {
  2185. wordCount: 0,
  2186. contextMode,
  2187. contextModeFallback,
  2188. sourceMode,
  2189. sourceModeFallback,
  2190. maskedFrontmatter: 0,
  2191. maskedHtmlComments: 0,
  2192. ignoredRegions: 0,
  2193. quotedLines: 0,
  2194. maskedQuotes: 0,
  2195. },
  2196. tooShort: true,
  2197. };
  2198. }
  2199. // Map each working-string code unit back to the caller's source. Every
  2200. // length-changing preprocessing stage composes this map as it removes
  2201. // characters, and results are translated before they leave the function.
  2202. let sourceMap = identitySourceMap(text.length);
  2203. // Context mode selects context-appropriate flagging. Accepted values:
  2204. // 'general' (default) — full ruleset
  2205. // 'technical' — skip title-case headers; individual prose-only rules
  2206. // apply their own technical-context gates (only mode
  2207. // that currently changes scoring)
  2208. // 'marketing' — accepted; recorded in stats; scores same as general
  2209. // 'personal' — accepted; recorded in stats; scores same as general
  2210. // Invalid values fall back to 'general' with stats.contextModeFallback set.
  2211. // Mode validation: an unknown string (e.g. typo "tecnical") would
  2212. // otherwise silently downgrade to general-mode behavior. Coerce to
  2213. // 'general' and surface the original value in stats for traceability.
  2214. let maskedFrontmatter = 0;
  2215. let maskedHtmlComments = 0;
  2216. // Author-marked ignore regions go first, in every source mode, so no
  2217. // later pass sees the excluded passage. Masking keeps the length, so the
  2218. // source map needs no update.
  2219. const ignored = maskIgnoreRegions(text);
  2220. text = ignored.text;
  2221. const { ignoredRegions } = ignored;
  2222. if (sourceMode === 'rendered-markdown') {
  2223. const rendered = maskRenderedMarkdown(text);
  2224. text = rendered.text;
  2225. maskedFrontmatter = rendered.maskedFrontmatter;
  2226. maskedHtmlComments = rendered.maskedHtmlComments;
  2227. }
  2228. // Pre-pass: mask Markdown blockquotes before scoring. A human
  2229. // reacting to AI text by quoting it shouldn't have the quoted block
  2230. // counted against their own writing. A single `> ` line counts too
  2231. // (#238). Masking instead of deleting keeps later issue and highlight
  2232. // offsets aligned with the source file.
  2233. const blockquotes = sourceMode === 'rendered-markdown'
  2234. ? maskBlockquotes(text)
  2235. : stripBlockquotes(text, sourceMap);
  2236. text = blockquotes.text;
  2237. if (blockquotes.sourceMap) sourceMap = blockquotes.sourceMap;
  2238. const { quotedLines } = blockquotes;
  2239. // Pre-pass: the inline half of the blockquote escape hatch. Words inside
  2240. // a double-quoted span belong to whoever is quoted (#238). Masking runs
  2241. // before normalization, as blockquotes do, so bypass characters inside a
  2242. // quotation raise no normalization flag. Masking keeps the length, so
  2243. // the source map needs no update. The smart-punctuation check below
  2244. // reads the unmasked text, because the blanked span would otherwise
  2245. // count as a double space.
  2246. const unquotedText = normalizeText(text).text;
  2247. const quotes = maskQuotedSpans(text);
  2248. text = quotes.text;
  2249. const { maskedQuotes } = quotes;
  2250. // Pre-pass: strip bypass-trick chars before pattern matching so
  2251. // "delve" with a Cyrillic 'е' still hits Tier 1. Compose the map while
  2252. // deleting characters so later offsets still address the source.
  2253. const norm = normalizeText(text, sourceMap);
  2254. text = norm.text;
  2255. sourceMap = norm.sourceMap;
  2256. const wordCount = countWords(text);
  2257. // Unsegmented-script check (GH-241): Chinese and Japanese carry no
  2258. // inter-word spaces, so word segmentation cannot measure them — a long
  2259. // document counts as one \S+ run and would misreport as "Too short",
  2260. // while newline-wrapped lines each count as a word and would score
  2261. // without segmentation. The check therefore runs before the word gate
  2262. // and declines only when CJK characters dominate the non-whitespace
  2263. // text, so short English documents with an incidental place name or
  2264. // single Han character stay scorable. Unicode script properties cover
  2265. // the complete Han, Hiragana, and Katakana repertoires (including
  2266. // supplementary-plane and halfwidth forms); Hangul is space-separated
  2267. // and segments fine, so it is excluded. Both counts use Unicode mode so
  2268. // supplementary characters count as one code point rather than two UTF-16
  2269. // code units.
  2270. const cjkChars = (text.match(/[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}]/gu) || []).length;
  2271. const nonSpaceChars = (text.match(/\S/gu) || []).length;
  2272. if (cjkChars > 0 && cjkChars * 2 >= nonSpaceChars) {
  2273. return {
  2274. ...buildV2Defaults('UNSCORED', 'low'),
  2275. score: 0,
  2276. label: 'Unsupported script',
  2277. issues: [],
  2278. stats: { wordCount, cjkChars, reason: 'unsegmented-script document: no inter-word spaces to count', contextMode, contextModeFallback, sourceMode, sourceModeFallback, maskedFrontmatter, maskedHtmlComments, ignoredRegions, quotedLines, maskedQuotes },
  2279. unsupportedScript: true,
  2280. };
  2281. }
  2282. if (wordCount < 10) {
  2283. return {
  2284. ...buildV2Defaults('UNSCORED', 'low'),
  2285. score: 0,
  2286. label: 'Too short',
  2287. issues: [],
  2288. stats: { wordCount, contextMode, contextModeFallback, sourceMode, sourceModeFallback, maskedFrontmatter, maskedHtmlComments, ignoredRegions, quotedLines, maskedQuotes },
  2289. tooShort: true,
  2290. };
  2291. }
  2292. if (wordCount > MAX_WORDS) {
  2293. return {
  2294. ...buildV2Defaults('UNSCORED', 'low'),
  2295. score: 0,
  2296. label: 'Text too long',
  2297. issues: [],
  2298. stats: { wordCount, contextMode, contextModeFallback, sourceMode, sourceModeFallback, maskedFrontmatter, maskedHtmlComments, ignoredRegions, quotedLines, maskedQuotes },
  2299. tooLong: true,
  2300. };
  2301. }
  2302. const tokens = tokenize(text);
  2303. const paragraphs = getParagraphs(text);
  2304. const sentences = getSentences(text);
  2305. const issues = [];
  2306. let rawScore = 0;
  2307. // ── 1. Tier 1 words ──────────────────────────────────────────
  2308. const tier1Found = new Set();
  2309. for (const token of tokens) {
  2310. if (contextMode === 'technical' && TECHNICAL_EXEMPT.has(token)) continue;
  2311. if (Object.hasOwn(TIER1, token) && !tier1Found.has(token)) {
  2312. tier1Found.add(token);
  2313. issues.push({
  2314. type: 'tier1',
  2315. text: token,
  2316. severity: 'high',
  2317. suggestion: TIER1[token],
  2318. });
  2319. }
  2320. }
  2321. // Tier 1 multi-word phrases. Adds each distinct phrase (lowercased) to
  2322. // `tier1Found` so the same phrase hit multiple times only produces one
  2323. // issue — matches the downstream dedup behavior.
  2324. for (const phrase of TIER1_PHRASES) {
  2325. const regex = new RegExp(phrase.pattern.source, phrase.pattern.flags);
  2326. let match;
  2327. while ((match = regex.exec(text)) !== null) {
  2328. const lower = match[0].toLowerCase();
  2329. if (contextMode === 'technical' && TECHNICAL_EXEMPT.has(lower)) continue;
  2330. if (phrase.skip && phrase.skip(text, match.index)) continue;
  2331. if (tier1Found.has(lower)) continue;
  2332. tier1Found.add(lower);
  2333. issues.push({
  2334. // Clarity-band entries are wordiness edits, not frequency evidence.
  2335. // Same fix, weaker claim — see the Tier 1A/1B split in SKILL.md.
  2336. type: phrase.clarity ? 'tier1-clarity' : 'tier1',
  2337. text: match[0],
  2338. severity: phrase.clarity ? 'medium' : 'high',
  2339. suggestion: phrase.replace,
  2340. });
  2341. }
  2342. }
  2343. // ── 2. Tier 2 clusters ───────────────────────────────────────
  2344. let tier2Clusters = 0;
  2345. for (const para of paragraphs) {
  2346. const paraTokens = tokenize(para);
  2347. const found = [];
  2348. const suggestions = {};
  2349. for (const token of paraTokens) {
  2350. if (contextMode === 'technical' && TECHNICAL_EXEMPT.has(token)) continue;
  2351. if (Object.hasOwn(TIER2, token) && !found.includes(token)) {
  2352. found.push(token);
  2353. suggestions[token] = TIER2[token];
  2354. }
  2355. }
  2356. for (const cond of TIER2_CONDITIONAL) {
  2357. if (contextMode === 'technical' && TECHNICAL_EXEMPT.has(cond.word)) continue;
  2358. if (!found.includes(cond.word) && cond.pattern.test(para)) {
  2359. found.push(cond.word);
  2360. suggestions[cond.word] = cond.suggestion;
  2361. }
  2362. }
  2363. if (found.length >= 2) {
  2364. tier2Clusters++;
  2365. for (const word of found) {
  2366. issues.push({
  2367. type: 'tier2',
  2368. text: word,
  2369. severity: 'medium',
  2370. suggestion: suggestions[word],
  2371. });
  2372. }
  2373. }
  2374. }
  2375. // ── 3. Tier 3 density ────────────────────────────────────────
  2376. const tier3Counts = {};
  2377. for (const token of tokens) {
  2378. const canonical = TIER3_LOOKUP.get(token);
  2379. if (canonical) tier3Counts[canonical] = (tier3Counts[canonical] || 0) + 1;
  2380. }
  2381. // Flag at 3% of word count, but never below 3 occurrences. Previous
  2382. // floor of 1 meant a 50-word text with one "significant" got flagged
  2383. // as Tier 3 overuse, which was noise.
  2384. const densityThreshold = Math.max(3, Math.floor(wordCount * 0.03));
  2385. let tier3Flags = 0;
  2386. for (const [word, count] of Object.entries(tier3Counts)) {
  2387. if (count >= densityThreshold) {
  2388. tier3Flags++;
  2389. issues.push({
  2390. type: 'tier3',
  2391. text: `"${word}" x${count}`,
  2392. severity: 'low',
  2393. suggestion: `Overused (${count} times in ${wordCount} words)`,
  2394. });
  2395. }
  2396. }
  2397. // ── 4–21. Pattern categories ─────────────────────────────────
  2398. issues.push(...matchPatterns(text, TRANSITIONS, 'transition', 'medium'));
  2399. issues.push(...matchPatterns(text, CHATBOT_ARTIFACTS, 'chatbot', 'critical'));
  2400. issues.push(...matchPatterns(text, SYCOPHANTIC, 'sycophantic', 'critical'));
  2401. issues.push(...matchPatterns(text, FILLERS, 'filler', 'medium'));
  2402. issues.push(...matchPatterns(text, GENERIC_CONCLUSIONS, 'generic-conclusion', 'medium'));
  2403. issues.push(...matchPatterns(text, LETS_PATTERNS, 'lets-construction', 'medium'));
  2404. issues.push(...matchPatterns(text, REASONING_ARTIFACTS, 'reasoning-artifact', 'critical'));
  2405. issues.push(...matchPatterns(text, SIGNIFICANCE_INFLATION, 'significance-inflation', 'high'));
  2406. issues.push(...matchPatterns(text, VAGUE_ATTRIBUTIONS, 'vague-attribution', 'critical'));
  2407. issues.push(...matchPatterns(text, HOLLOW_INTENSIFIERS, 'hollow-intensifier', 'medium'));
  2408. issues.push(...matchPatterns(text, EMOTIONAL_FLATLINE, 'emotional-flatline', 'low'));
  2409. issues.push(...matchPatterns(text, LINGERING_ATTENTION, 'lingering-attention', 'medium'));
  2410. issues.push(...matchPatterns(text, NOVELTY_INFLATION, 'novelty-inflation', 'medium'));
  2411. issues.push(...matchPatterns(text, CUTOFF_DISCLAIMERS, 'cutoff-disclaimer', 'critical'));
  2412. issues.push(...matchPatterns(text, AI_PLACEHOLDERS, 'ai-placeholder', 'critical'));
  2413. issues.push(...matchPatterns(text, AI_CITATION_MARKUP, 'ai-citation-markup', 'critical'));
  2414. issues.push(...matchPatterns(text, AI_UTM_SOURCE, 'ai-utm-source', 'critical'));
  2415. issues.push(...matchPatterns(text, TEMPLATE_PHRASES, 'template-phrase', 'high'));
  2416. issues.push(...matchPatterns(text, FALSE_CONCESSION, 'false-concession', 'medium'));
  2417. issues.push(...matchPatterns(text, RHETORICAL_QUESTIONS, 'rhetorical-question', 'medium'));
  2418. issues.push(...matchPatterns(text, HEDGE_STACK, 'hedge-stack', 'high'));
  2419. issues.push(...matchPatterns(text, FUTURE_NARRATIVE, 'future-narrative', 'high'));
  2420. issues.push(...matchPatterns(text, REAL_ACTUAL_INFLATION, 'real-actual-inflation', 'medium'));
  2421. issues.push(...matchPatterns(text, SOCIAL_CTA_CLOSER, 'social-cta-closer', 'high'));
  2422. issues.push(...matchPatterns(text, PERFORMED_INSIGHT, 'performed-insight', 'medium'));
  2423. const stagedDiscoveryIssues = matchPatterns(text, STAGED_DISCOVERY, 'performed-insight', 'medium');
  2424. issues.push(...stagedDiscoveryIssues);
  2425. issues.push(...matchPatterns(text, NEGATION_CHAIN, 'negation-chain', 'high'));
  2426. // Reveals flag alone; contrasts need a nearby frame. See NEGATIVE_PARALLELISM_*.
  2427. issues.push(...negativeParallelismIssues(
  2428. text,
  2429. matchPatterns(text, NEGATIVE_PARALLELISM_REVEAL, 'negative-parallelism', 'high'),
  2430. matchPatterns(text, NEGATIVE_PARALLELISM_CONTRAST, 'negative-parallelism', 'high'),
  2431. ));
  2432. issues.push(...matchPatterns(text, DEV_BLOG_BOILERPLATE, 'dev-blog-boilerplate', 'medium'));
  2433. issues.push(...findUnnecessaryHyphenation(text));
  2434. // ── Tier 1 v2: formulaic openers + parenthetical hedges ──────────
  2435. issues.push(...matchPatterns(text, FORMULAIC_OPENERS, 'formulaic-opener', 'high'));
  2436. issues.push(...matchPatterns(text, SPECULATIVE_OPENERS, 'speculative-opener', 'high'));
  2437. issues.push(...matchPatterns(text, LAUNCH_INTROS, 'launch-intro', 'high'));
  2438. issues.push(...matchPatterns(text, CROWD_CONTRAST, 'crowd-contrast', 'medium'));
  2439. issues.push(...matchPatterns(text, FAKE_CASUAL_PROPS, 'fake-casual-prop', 'high'));
  2440. issues.push(...matchPatterns(text, PARENTHETICAL_HEDGE, 'parenthetical-hedge', 'medium'));
  2441. // Title-case headers — gated to marketing/personal/general modes
  2442. // (technical mode legitimately uses Title Case section headers).
  2443. if (contextMode !== 'technical') {
  2444. const titleHits = matchPatterns(text, [TITLE_CASE_HEADER], 'title-case-header', 'medium');
  2445. // Drop matches that look like proper-noun titles (single line, all
  2446. // tokens capitalized incl. function words) — that's headline style,
  2447. // not the AI-section-header tell which has mid-sentence "And".
  2448. //
  2449. // The prefix strip is load-bearing. matchPatterns reports match[0], so a
  2450. // Markdown hit arrives as "## Terms Of Service" and `##` counts as a
  2451. // token — silently lowering this guard from four content words to three
  2452. // for headings only, which is exactly the class it exists to protect.
  2453. // "## Terms Of Service", "## Bank Of America" and "## Table Of Contents"
  2454. // all flagged as a result: ordinary human headings, on a detector whose
  2455. // stated first priority is not firing on human writing.
  2456. const filtered = titleHits.filter((h) => {
  2457. const title = h.text.replace(MD_HEADING_PREFIX, '');
  2458. const tokens = title.trim().split(/\s+/);
  2459. if (tokens.length < 4) return false;
  2460. // The function word must be MID-title, which is what the comment above
  2461. // has always said and what the test never enforced. A leading "The"
  2462. // satisfied a bare /\bThe\b/, so ordinary human headings flagged:
  2463. // "## The New Security Landscape", "## The Microsoft Approach to
  2464. // Identity", "### The Four Keys to a Successful and Secure Modern
  2465. // Workplace". Measured across 81 files that provably predate LLMs
  2466. // (2018-19 eBooks, 2020 posts): 13 false positives, every one opening
  2467. // with "The", against zero on main.
  2468. //
  2469. // "## Benefits And Strategic Considerations" -- the actual tell, and
  2470. // this rule's own fixture -- is untouched: its "And" is interior.
  2471. return FUNCTION_WORD.test(tokens.slice(1).join(' '));
  2472. });
  2473. const fences = filtered.length ? fenceRanges(text) : [];
  2474. issues.push(...filtered.filter((h) => !inFenceRange(fences, h.index)));
  2475. }
  2476. // ── Normalization-trigger flag ───────────────────────────────────
  2477. // ZWSPs or homoglyphs in pasted prose are near-dispositive: humans
  2478. // don't insert these into their own writing. Single roleplay marker
  2479. // can be a false positive on Markdown emphasis (filtered to multi-
  2480. // word inner already), so requires ≥2.
  2481. if (norm.flags.zeroWidth > 0 || norm.flags.homoglyph >= 2) {
  2482. issues.push({
  2483. type: 'normalization-flag',
  2484. text: `${norm.flags.zeroWidth} zero-width + ${norm.flags.homoglyph} homoglyph swap${norm.flags.homoglyph === 1 ? '' : 's'}`,
  2485. severity: 'critical',
  2486. suggestion: 'Text contains invisible/lookalike chars typical of AI-humanizer bypass tools. Re-type from your own keyboard.',
  2487. });
  2488. }
  2489. if (norm.flags.roleplay >= 2) {
  2490. issues.push({
  2491. type: 'normalization-flag',
  2492. text: `${norm.flags.roleplay} *roleplay-action* markers stripped`,
  2493. severity: 'high',
  2494. suggestion: 'Paired *action* markers are a chat-model artifact.',
  2495. });
  2496. }
  2497. // Em dashes in list-item separator position — a bulleted or numbered
  2498. // list item opening with a bolded lead term or markdown link, then the
  2499. // dash ("- **Term** — desc", "- [label](url) — desc") — are
  2500. // definition-list typography, not prose punctuation. Shared by the
  2501. // smart-punct signature below and the em-dash frequency check (§22).
  2502. // An optional parenthetical or inline-code span may sit between the bold
  2503. // lead term and the dash — "- **Lingering-attention claims**
  2504. // (`lingering-attention`) — the share-post frame…" is the same definition
  2505. // typography as the bare form. Found by the self-scan (see PROOF.md, #67).
  2506. const SEPARATOR_DASH_RE = /^\s*(?:[-*+]|\d+[.)])\s+(?:\*\*[^*\n]+\*\*|\[[^\]\n]+\]\([^)\n]*\))(?:[ \t]*(?:\([^)\n]*\)|`[^`\n]+`))?[ \t]*—/gm;
  2507. // The same typography in HTML source: an <li> that opens with a
  2508. // <strong>/<b> lead term or an <a> link of at most five words, then the
  2509. // dash (#351). A longer bold span is a clause, not a term. Only the
  2510. // first child of the item counts, so a prose dash later in a list item,
  2511. // or any dash outside <li>, still counts. `&mdash;` entities are never
  2512. // counted as em dashes, so they need no carve-out.
  2513. const SEPARATOR_DASH_HTML_RE = /^[ \t]*<li\b[^>\n]*>[ \t]*(?:<(strong|b)\b[^>\n]*>[ \t]*[^<\s]+(?:[ \t]+[^<\s]+){0,4}[ \t]*<\/\1>|<a\b[^>\n]*>[ \t]*[^<\s]+(?:[ \t]+[^<\s]+){0,4}[ \t]*<\/a>)(?:[ \t]*(?:\([^)\n]*\)|<code\b[^>\n]*>[^<\n]+<\/code>))?[ \t]*—/gim;
  2514. function countSeparatorDashes(value) {
  2515. return (value.match(SEPARATOR_DASH_RE) || []).length
  2516. + (value.match(SEPARATOR_DASH_HTML_RE) || []).length
  2517. + countVersionHeadingDashes(value);
  2518. }
  2519. // Keep-a-Changelog version headings (`## [3.21.0] — 2026-07-30`) join a
  2520. // label to a value exactly as a list separator does. Deliberately narrow:
  2521. // a bracketed or bare semver token, then a dash, then an ISO date, and
  2522. // nothing else on the line. Ordinary prose dashes in headings still count,
  2523. // because SKILL.md applies the em-dash rule to headings too.
  2524. function countVersionHeadingDashes(value) {
  2525. let count = 0;
  2526. for (const rawLine of value.split(/\r\n|\n|\r/)) {
  2527. let end = rawLine.length;
  2528. while (end > 0 && (rawLine[end - 1] === ' ' || rawLine[end - 1] === '\t')) end -= 1;
  2529. if (!/^\d{4}-\d{2}-\d{2}$/.test(rawLine.slice(Math.max(0, end - 10), end))) continue;
  2530. let cursor = end - 10;
  2531. while (cursor > 0 && (rawLine[cursor - 1] === ' ' || rawLine[cursor - 1] === '\t')) cursor -= 1;
  2532. if (rawLine[cursor - 1] !== '\u2014') continue;
  2533. cursor -= 1;
  2534. while (cursor > 0 && (rawLine[cursor - 1] === ' ' || rawLine[cursor - 1] === '\t')) cursor -= 1;
  2535. const prefix = rawLine.slice(0, cursor);
  2536. const heading = /^#{1,6}[ \t]+/.exec(prefix);
  2537. if (!heading) continue;
  2538. let version = prefix.slice(heading[0].length);
  2539. if (version.startsWith('[')) {
  2540. if (!version.endsWith(']')) continue;
  2541. version = version.slice(1, -1);
  2542. } else if (version.endsWith(']')) {
  2543. version = version.slice(0, -1);
  2544. }
  2545. if (version.includes(']')) continue;
  2546. if (/^v?\d+\.\d+\.\d+/.test(version)) count += 1;
  2547. }
  2548. return count;
  2549. }
  2550. // ── Smart-punctuation co-occurrence signature ────────────────────
  2551. // Curly punctuation + prose em dash + Oxford comma, with no sampled
  2552. // spacing or common-contraction anomalies,
  2553. // can also come from house style or editor settings. Keep the existing
  2554. // narrow shape as a low-priority style check in plain source only.
  2555. // Rendered Markdown deliberately preserves publication typography.
  2556. // This finding has no weight or classifier corroborator. Separator-only
  2557. // dashes do not complete the shape, and the spacing/contraction checks
  2558. // below are limited heuristics, not proof that a document has no typos.
  2559. if (sourceMode === 'plain') {
  2560. const hasCurly = /[“”‘’]/.test(text);
  2561. const totalEmDashes = (text.match(/—/g) || []).length;
  2562. const separatorEmDashes = countSeparatorDashes(text);
  2563. const hasEmDash = totalEmDashes > separatorEmDashes;
  2564. const oxfordHit = text.match(/\b\w+,\s+\w+,\s+and\s+\w+/g);
  2565. const hasOxford = (oxfordHit?.length || 0) >= 1;
  2566. const doubleSpaces = (unquotedText.match(/[^.!?] +/g) || []).length;
  2567. const missingApos = /\b(?:dont|wont|cant|isnt|wasnt|shouldnt|wouldnt|couldnt|youre|theyre|its\s+a\s+\w+ing)\b/i.test(text);
  2568. const clean = doubleSpaces === 0 && !missingApos;
  2569. const signals = [hasCurly, hasEmDash, hasOxford, clean].filter(Boolean).length;
  2570. if (signals >= 4 && wordCount >= 80) {
  2571. issues.push({
  2572. type: 'smart-punct-signature',
  2573. text: 'curly punctuation + prose em dash + Oxford comma',
  2574. severity: 'low',
  2575. suggestion: 'Check destination style before changing punctuation. Preserve house-style typography; this combination is not evidence of AI authorship.',
  2576. });
  2577. }
  2578. }
  2579. // ── Punctuation distribution mode ────────────────────────────────
  2580. // Humans cluster trimodal across paragraphs (some paras heavy, some
  2581. // light, some none). AI converges on a normal distribution. We can't
  2582. // run a real modality test client-side, but we can flag the AI
  2583. // signature: low variance of per-paragraph punctuation density.
  2584. // Requires ≥4 paragraphs to be meaningful.
  2585. if (paragraphs.length >= 4) {
  2586. const densities = paragraphs.map((p) => {
  2587. const words = (p.match(/\S+/g) || []).length;
  2588. if (words < 5) return null;
  2589. const puncts = (p.match(/[,;:—()]/g) || []).length;
  2590. return puncts / words;
  2591. }).filter((d) => d !== null);
  2592. if (densities.length >= 4) {
  2593. const mean = densities.reduce((a, b) => a + b, 0) / densities.length;
  2594. const variance = densities.reduce((s, d) => s + (d - mean) ** 2, 0) / densities.length;
  2595. const cv = mean > 0 ? Math.sqrt(variance) / mean : 0;
  2596. // CV < 0.25 across paragraphs means each paragraph has the same
  2597. // punctuation density — the AI signature. Humans usually swing
  2598. // wider. Threshold derived from stylometry papers (arxiv 2507.00838).
  2599. if (cv < 0.25 && mean >= 0.04) {
  2600. issues.push({
  2601. type: 'punct-distribution',
  2602. text: `Punctuation density uniform across paragraphs (CV=${cv.toFixed(2)})`,
  2603. severity: 'medium',
  2604. suggestion: 'AI text holds punctuation density steady; human writers swing between dense and sparse paragraphs.',
  2605. });
  2606. }
  2607. }
  2608. }
  2609. // ── Function-word trigram entropy ────────────────────────────────
  2610. // POS-trigram entropy is the academic signal; function-word trigram
  2611. // entropy approximates it without a tagger (function words ARE the
  2612. // closed-class POS classes). AI text has lower entropy because LLM
  2613. // sampling collapses onto a narrower set of grammatical templates.
  2614. //
  2615. // Method: extract function-word indicators per sentence, build
  2616. // trigrams over the sequence, compute Shannon entropy. Bins below
  2617. // threshold flag.
  2618. if (wordCount >= 150) {
  2619. const FUNC_WORDS = new Set([
  2620. 'the','a','an','and','or','but','of','to','in','on','at','by','for','with',
  2621. 'from','as','is','was','are','were','be','been','being','have','has','had',
  2622. 'do','does','did','will','would','should','could','may','might','must','can',
  2623. 'this','that','these','those','it','its','they','them','their','there','here',
  2624. 'we','our','us','i','you','your','he','she','his','her','him','not','no','so',
  2625. 'if','then','than','when','where','which','who','what','how','why','because',
  2626. ]);
  2627. const seq = tokens.map((t) => FUNC_WORDS.has(t) ? t : '_').filter((_, i, arr) => arr[i] !== '_' || (i > 0 && arr[i - 1] !== '_'));
  2628. if (seq.length >= 50) {
  2629. const trigrams = {};
  2630. for (let i = 0; i < seq.length - 2; i++) {
  2631. const tg = `${seq[i]}|${seq[i + 1]}|${seq[i + 2]}`;
  2632. trigrams[tg] = (trigrams[tg] || 0) + 1;
  2633. }
  2634. const total = seq.length - 2;
  2635. let entropy = 0;
  2636. for (const c of Object.values(trigrams)) {
  2637. const p = c / total;
  2638. entropy -= p * Math.log2(p);
  2639. }
  2640. // Normalize by log2(distinct trigrams) so entropy ranges roughly
  2641. // 0..1 and threshold is interpretable. Empirical threshold: human
  2642. // prose ~0.85-0.95 normalized, AI prose ~0.70-0.82.
  2643. const distinctCount = Object.keys(trigrams).length;
  2644. const normalized = distinctCount > 1 ? entropy / Math.log2(distinctCount) : 1;
  2645. if (normalized < 0.82 && total >= 50) {
  2646. issues.push({
  2647. type: 'fnword-trigram-entropy',
  2648. text: `Function-word trigram entropy ${normalized.toFixed(2)} (low)`,
  2649. severity: 'medium',
  2650. suggestion: 'Grammatical structure is unusually repetitive. AI sampling collapses onto narrower templates than human writing.',
  2651. });
  2652. }
  2653. // Degenerate case: single distinct trigram repeated across the
  2654. // whole document is the strongest possible AI signal but the
  2655. // normalized fallback returns 1.0 (= "fully human"), inverting
  2656. // the signal. Catch it explicitly.
  2657. if (distinctCount === 1 && total >= 50) {
  2658. issues.push({
  2659. type: 'fnword-trigram-entropy',
  2660. text: 'Single function-word trigram repeated across document',
  2661. severity: 'high',
  2662. suggestion: 'Grammatical structure is fully degenerate — every clause uses the same function-word skeleton.',
  2663. });
  2664. }
  2665. }
  2666. }
  2667. // ── Cross-paragraph burstiness ───────────────────────────────────
  2668. // We already check within-paragraph sentence-length uniformity. AI
  2669. // is also flat ACROSS paragraphs — every paragraph has roughly the
  2670. // same sentence-length variance. Humans vary: terse paras next to
  2671. // discursive paras. Measure variance of CV across paragraphs.
  2672. if (paragraphs.length >= 4) {
  2673. const cvs = paragraphs.map((p) => {
  2674. const sents = getSentences(p);
  2675. if (sents.length < 3) return null;
  2676. const lens = sents.map(countWords);
  2677. const m = lens.reduce((a, b) => a + b, 0) / lens.length;
  2678. if (m === 0) return null;
  2679. const v = lens.reduce((s, l) => s + (l - m) ** 2, 0) / lens.length;
  2680. return Math.sqrt(v) / m;
  2681. }).filter((c) => c !== null);
  2682. if (cvs.length >= 4) {
  2683. const cvMean = cvs.reduce((a, b) => a + b, 0) / cvs.length;
  2684. const cvVar = cvs.reduce((s, c) => s + (c - cvMean) ** 2, 0) / cvs.length;
  2685. const cvStd = Math.sqrt(cvVar);
  2686. // Std-of-CV below 0.08 means every paragraph has roughly the same
  2687. // internal rhythm — AI signature. Human prose typically swings
  2688. // 0.15-0.40 across paragraphs of mixed purpose.
  2689. if (cvStd < 0.08 && cvMean < 0.45) {
  2690. issues.push({
  2691. type: 'cross-para-burstiness',
  2692. text: `Sentence-rhythm uniform across paragraphs (σCV=${cvStd.toFixed(2)})`,
  2693. severity: 'medium',
  2694. suggestion: 'Every paragraph has the same internal rhythm. Humans vary cadence between terse and discursive paragraphs.',
  2695. });
  2696. }
  2697. }
  2698. }
  2699. // ── Tier 3 multi-word phrase density ─────────────────────────
  2700. // Two complementary rules:
  2701. // (a) Per-phrase density — same gating as single-word Tier 3: each
  2702. // phrase fine alone, repetition is the tell. Threshold = 2.
  2703. // (b) Cross-phrase clustering — ≥3 *distinct* boilerplate phrases
  2704. // in one piece. LLMs varying their own boilerplate often use
  2705. // each phrase only once but stack 5-10 across the text. The
  2706. // per-phrase rule misses this; the cluster rule catches it.
  2707. // Track non-overlapping match spans so a longer phrase swallowing a
  2708. // shorter one (e.g., "designed for long-term sustainability" matches
  2709. // both "designed for long-term" AND "long-term sustainability") only
  2710. // contributes one distinct hit. Without dedup the cluster threshold
  2711. // can be reached by a single sentence stacking overlapping regexes.
  2712. const claimedSpans = [];
  2713. function spanOverlaps(start, end) {
  2714. for (const [s, e] of claimedSpans) {
  2715. if (start < e && end > s) return true;
  2716. }
  2717. return false;
  2718. }
  2719. let distinctPhrasesHit = 0;
  2720. for (const phrase of TIER3_PHRASES) {
  2721. const regex = new RegExp(phrase.source, phrase.flags);
  2722. const phraseSpans = [];
  2723. let phraseMatch;
  2724. while ((phraseMatch = regex.exec(text)) !== null) {
  2725. const start = phraseMatch.index;
  2726. const end = start + phraseMatch[0].length;
  2727. if (!spanOverlaps(start, end)) {
  2728. phraseSpans.push([start, end, phraseMatch[0]]);
  2729. }
  2730. }
  2731. if (phraseSpans.length === 0) continue;
  2732. for (const [s, e] of phraseSpans) claimedSpans.push([s, e]);
  2733. distinctPhrasesHit++;
  2734. if (phraseSpans.length >= 2) {
  2735. issues.push({
  2736. type: 'tier3-phrase',
  2737. text: `"${phraseSpans[0][2].toLowerCase()}" x${phraseSpans.length}`,
  2738. severity: 'medium',
  2739. suggestion: `Boilerplate phrase repeated ${phraseSpans.length}× — replace at least one with specifics`,
  2740. });
  2741. }
  2742. }
  2743. if (distinctPhrasesHit >= 3) {
  2744. issues.push({
  2745. type: 'tier3-phrase-cluster',
  2746. text: `${distinctPhrasesHit} distinct boilerplate phrases`,
  2747. severity: 'high',
  2748. suggestion: 'Several stock crypto/web3 phrases stacked in one piece. Rewrite around one specific claim or observation.',
  2749. });
  2750. }
  2751. // ── Hashtag stuffing ─────────────────────────────────────────
  2752. // 6+ hashtags in a single post is rare for thoughtful humans and
  2753. // near-universal for LLM-generated social posts. Counted globally,
  2754. // not per-paragraph, since the trailing hashtag block is the shape
  2755. // we care about.
  2756. // Match #tag at start of text or after any non-word char (whitespace,
  2757. // punctuation, line breaks). URL fragments are already excluded
  2758. // because the char immediately before `#` in a URL path is always
  2759. // a word char (e.g. `example.com/page#section` — `e` before `#`).
  2760. // Earlier char class `[\s\\]` had a literal backslash and silently
  2761. // missed hashtags after sentence punctuation; an interim `[\s]` fix
  2762. // on origin only caught whitespace-preceded tags.
  2763. // Code is masked and non-tag `#` forms are subtracted first — see maskCode
  2764. // and isSocialTag. Without them a changelog paragraph citing six issue
  2765. // numbers, or a palette listing six hex colours, scored as a tag block.
  2766. const hashtagMatches = [...maskCode(text).matchAll(/(?:^|\W)#(\w[\w-]*)/g)]
  2767. .filter((m) => isSocialTag(m[1]));
  2768. if (hashtagMatches.length >= 6) {
  2769. issues.push({
  2770. type: 'hashtag-stuff',
  2771. text: `${hashtagMatches.length} hashtags`,
  2772. severity: 'medium',
  2773. suggestion: 'Cut to 2-3 specific tags or none. Long hashtag blocks read as bot output.',
  2774. });
  2775. }
  2776. // ── Bullet list of bare noun phrases ─────────────────────────
  2777. // ≥5 consecutive bullet items that are short (≤6 words) and contain
  2778. // no finite-verb / modal token. Catches the "Stable mining efficiency
  2779. // / Reliable pool connectivity / Optimized RandomX performance ..."
  2780. // shape LLMs default to. Markdown bullets, escaped Markdown bullets,
  2781. // unicode bullets, and dashes are all matched. Numbered lists are
  2782. // excluded — those have a separate "numbered list inflation" rule.
  2783. //
  2784. // Note: verbRe covers auxiliaries and modals ("was", "will", "can",
  2785. // etc.). Regular past-tense verbs ("fixed", "removed") are not
  2786. // matched here; instead, the ≤6-word length gate excludes most
  2787. // real-world changelog lines, which tend to read "fixed the X that
  2788. // was doing Y" (>6 words). Short two-word action items ("* fixed
  2789. // bug") would pass both gates — an acceptable trade-off to avoid
  2790. // false-negative risk from adjectives ending in -ed ("skilled",
  2791. // "advanced") that share the same surface form.
  2792. const lines = text.split(/\r?\n/);
  2793. const parseBullet = (line) => {
  2794. let cursor = 0;
  2795. while (cursor < line.length && /\s/.test(line[cursor])) cursor += 1;
  2796. if (!['*', '-', '•', '+'].includes(line[cursor])) return null;
  2797. cursor += 1;
  2798. const spacingStart = cursor;
  2799. while (cursor < line.length && /\s/.test(line[cursor])) cursor += 1;
  2800. if (cursor === spacingStart) return null;
  2801. if (cursor >= line.length) return cursor - spacingStart >= 2 ? '' : null;
  2802. return line.slice(cursor).trim();
  2803. };
  2804. const verbRe = /\b(?:is|are|was|were|has|have|had|will|would|should|must|do|does|did|can|could|may|might|am|been|being)\b/i;
  2805. const fenceRe = /^\s*(?:```|~~~)/;
  2806. let run = [];
  2807. let blankStreak = 0;
  2808. let inFence = false;
  2809. function flushRun() {
  2810. if (run.length >= 5) {
  2811. const bareNP = run.filter((it) => {
  2812. const wc = (it.match(/\S+/g) || []).length;
  2813. return wc > 0 && wc <= 6 && !verbRe.test(it);
  2814. });
  2815. if (bareNP.length >= 5 && bareNP.length / run.length >= 0.75) {
  2816. issues.push({
  2817. type: 'bullet-np-list',
  2818. text: `${run.length}-item bullet list of bare noun phrases`,
  2819. severity: 'high',
  2820. suggestion: 'Convert to a prose paragraph or merge items. Long lists of bare adj+noun pairs read as AI scaffolding.',
  2821. });
  2822. }
  2823. }
  2824. run = [];
  2825. blankStreak = 0;
  2826. }
  2827. for (const line of lines) {
  2828. if (fenceRe.test(line)) {
  2829. // Code-fence toggle. Bullets inside fences are CLI flag docs or
  2830. // option dumps, not prose AI scaffolding — flush any prose run
  2831. // we were tracking and skip until the fence closes.
  2832. flushRun();
  2833. inFence = !inFence;
  2834. continue;
  2835. }
  2836. if (inFence) continue;
  2837. const bullet = parseBullet(line);
  2838. if (bullet !== null) {
  2839. run.push(bullet);
  2840. blankStreak = 0;
  2841. } else if (line.trim() === '') {
  2842. // A single blank line inside a list is normal Markdown spacing;
  2843. // two or more blank lines break the run, since visually-disjoint
  2844. // bullet sections shouldn't merge into one logical list.
  2845. blankStreak++;
  2846. if (blankStreak >= 2) flushRun();
  2847. } else {
  2848. flushRun();
  2849. }
  2850. }
  2851. flushRun();
  2852. // NOTE: "Wall-of-text replies" (SKILL.md) is deliberately NOT a
  2853. // detector rule here. A first pass tried "reply-length text, >=4
  2854. // sentences, zero newlines" as a structural gate — it broke the
  2855. // "repeated Tier 1 phrase does not inflate score linearly" fixture
  2856. // and, on reflection, would fire on any ordinary short paragraph
  2857. // (a blog intro, a single-paragraph email) since "one paragraph with
  2858. // no internal line break" is simply what continuous prose looks
  2859. // like, not an AI-specific shape. The tell in SKILL.md depends on
  2860. // knowing the text is conversational-reply register in the first
  2861. // place, which the engine can't reliably infer from the bytes alone
  2862. // (CONTRIBUTING.md: "a signal that fires on most normal prose is not
  2863. // worth adding"). Left as an LLM-judgment rule; see CATEGORIES.md §C.
  2864. // Confidence calibration is only flagged when it stacks (3+ instances).
  2865. // Gating happens pre-dedup on raw match count, since that signals actual
  2866. // stacking, not just vocabulary use.
  2867. const confIssues = matchPatterns(text, CONFIDENCE_CALIBRATION, 'confidence-calibration', 'low');
  2868. if (confIssues.length >= 3) issues.push(...confIssues);
  2869. // ── 22. Em dash frequency ────────────────────────────────────
  2870. // Match real em dashes, plus `--` only when surrounded by whitespace on at
  2871. // least one side (skips CLI flags like --save-dev and YAML `---` blocks).
  2872. // Separator-position em dashes (SEPARATOR_DASH_RE above) are excluded
  2873. // from the rate. The list marker is required on purpose: a line-initial
  2874. // "**Bold lead** — full sentence" outside a list is itself an AI tell
  2875. // and still counts, as does a mid-sentence "**bold** — like this"
  2876. // splice. Em dash only — the `--` substitute is never carved out.
  2877. const rawEmDashCount = (text.match(/—|(?<=\s)--(?=\s|$)|(?<=^|\s)--(?=\s)/gm) || []).length;
  2878. const separatorDashCount = countSeparatorDashes(text);
  2879. const emDashCount = rawEmDashCount - separatorDashCount;
  2880. const emDashRate = emDashCount / (wordCount / 1000);
  2881. if (emDashRate > 1) {
  2882. issues.push({
  2883. type: 'em-dash',
  2884. text: `${emDashCount} em dashes in ${wordCount} words`,
  2885. severity: 'medium',
  2886. suggestion: 'Replace with commas, periods, or rewrite',
  2887. });
  2888. }
  2889. // ── 23. Sentence length uniformity ───────────────────────────
  2890. // Glossary entries ("**Term:** gloss", "- **Term** — gloss"; three or
  2891. // more distinct terms, wrapped lines included) are glossary typography: their lengths are parallel by convention, so
  2892. // they say nothing about prose rhythm. They leave the sample here and
  2893. // nowhere else; the five-sentence gate and the 0.25 threshold stay as
  2894. // they are, so prose around a glossary is still measured (#351).
  2895. const glosslessText = withoutGlossaryEntries(text);
  2896. const rhythmSentences = glosslessText === null ? sentences : getSentences(glosslessText);
  2897. if (rhythmSentences.length >= 5) {
  2898. const lengths = rhythmSentences.map(s => countWords(s));
  2899. const avg = lengths.reduce((a, b) => a + b, 0) / lengths.length;
  2900. const variance = lengths.reduce((sum, l) => sum + Math.pow(l - avg, 2), 0) / lengths.length;
  2901. const stdDev = Math.sqrt(variance);
  2902. const cv = avg > 0 ? stdDev / avg : 0;
  2903. if (cv < 0.25 && avg > 10) {
  2904. issues.push({
  2905. type: 'uniformity',
  2906. text: `Sentence lengths cluster around ${Math.round(avg)} words (low variation)`,
  2907. severity: 'medium',
  2908. suggestion: 'Mix short punchy sentences with longer flowing ones',
  2909. });
  2910. }
  2911. }
  2912. // ── Type-token ratio (stylometric — vocabulary diversity) ────
  2913. // TTR = distinct word types / total tokens, taken over each 200-token
  2914. // window and averaged (movingAverageTTR). Whole-text TTR falls with
  2915. // length whoever wrote the text: on the human documents in corpus/ its
  2916. // median is 0.63 at 200 tokens, 0.37 at 2,000 and 0.28 at 6,000, so a
  2917. // fixed threshold on it flagged every 6,000-word public-domain slice.
  2918. // The windowed median stays between 0.62 and 0.64 at every length.
  2919. // Within a window, human prose typically sits around 0.50–0.65 for
  2920. // English; AI prose tends flatter (0.55–0.75 looks normal, but the
  2921. // lower end of the *too-flat* tail at >=200 words is where the signal
  2922. // lives — too FEW unique words for the length). This is the simplest
  2923. // of the four stylometric signals identified in the May 2026 detection-
  2924. // research review: no POS tagger required, no model, pure JS.
  2925. //
  2926. // Threshold tuning: flag only when the sample is large enough
  2927. // that low TTR is meaningfully suspicious (>=200 tokens) AND TTR
  2928. // is below 0.40 (very vocabulary-poor). Conservative on purpose;
  2929. // false positives on short or topic-narrow human prose are easy
  2930. // to trigger and would drown out other signals. The detector-
  2931. // research lens flagged TTR as one of four stylometric add-ons.
  2932. // One of the other three has since shipped in approximated form:
  2933. // `cross-para-burstiness` covers sentence-length burstiness across
  2934. // paragraphs. `fnword-trigram-entropy` is a related tagger-free
  2935. // signal (it approximates POS-trigram entropy, not one of the
  2936. // three). POS-bigram log-odds and function-word z-scores are
  2937. // still TODO.
  2938. if (tokens.length >= 200) {
  2939. const diversity = movingAverageTTR(tokens, 200);
  2940. if (diversity < 0.4) {
  2941. issues.push({
  2942. type: 'low-ttr',
  2943. text: `Vocabulary diversity ${(diversity * 100).toFixed(1)}% (distinct words per 200-token window, ${tokens.length} tokens)`,
  2944. severity: 'low',
  2945. suggestion: 'Text reuses a narrow word set. Vary nouns and verbs deliberately, or check if the topic genuinely warrants the repetition.',
  2946. });
  2947. }
  2948. }
  2949. // ── 24. Paragraph length uniformity ──────────────────────────
  2950. if (paragraphs.length >= 4) {
  2951. const paraLengths = paragraphs.map(p => getSentences(p).length);
  2952. const avg = paraLengths.reduce((a, b) => a + b, 0) / paraLengths.length;
  2953. const allSimilar = paraLengths.every(l => Math.abs(l - avg) <= 1);
  2954. if (allSimilar && avg >= 3) {
  2955. issues.push({
  2956. type: 'uniformity',
  2957. text: `All paragraphs are ~${Math.round(avg)} sentences`,
  2958. severity: 'low',
  2959. suggestion: 'Vary paragraph length deliberately',
  2960. });
  2961. }
  2962. }
  2963. // ── 25. Bold overuse ─────────────────────────────────────────
  2964. const boldMatches = text.match(/\*\*[^*]+\*\*/g) || [];
  2965. if (boldMatches.length > 3) {
  2966. issues.push({
  2967. type: 'formatting',
  2968. text: `${boldMatches.length} bold phrases`,
  2969. severity: 'medium',
  2970. suggestion: 'Strip bold from most; restructure to lead with key info',
  2971. });
  2972. }
  2973. // ── Score from the deduped issue list ───────────────────────
  2974. // Previously rawScore was accumulated inline per pattern hit, so
  2975. // repeated hits of the same phrase (or overlapping matches) inflated
  2976. // the score while the displayed issue list was deduplicated. That
  2977. // produced the UX regression where a "heavy AI patterns" label sat
  2978. // above a list of two items. Now the dedup runs first, then each
  2979. // distinct issue contributes its category weight — so the number
  2980. // reflects the same signals the user actually sees.
  2981. // The staged-discovery phrase can contain the older "the most interesting
  2982. // part" flatline match or sentence-initial "Turns out". Report the
  2983. // enclosing signal once while leaving unrelated findings intact.
  2984. const stagedDiscoverySpans = stagedDiscoveryIssues
  2985. .map((issue) => ({ start: issue.index, end: issue.index + issue.text.length }));
  2986. const nonOverlappingIssues = issues.filter((issue) =>
  2987. (issue.type !== 'emotional-flatline' && issue.type !== 'performed-insight') ||
  2988. stagedDiscoveryIssues.includes(issue) ||
  2989. !Number.isInteger(issue.index) ||
  2990. !stagedDiscoverySpans.some((span) =>
  2991. issue.index >= span.start && issue.index + issue.text.length <= span.end
  2992. )
  2993. );
  2994. const deduped = deduplicateIssues(nonOverlappingIssues);
  2995. for (const issue of deduped) {
  2996. rawScore += ISSUE_WEIGHTS[issue.type] ?? 2;
  2997. }
  2998. // Scale by text length: longer text gets more chances to trigger.
  2999. const lengthFactor = Math.max(1, Math.log2(wordCount / 50));
  3000. const normalizedScore = Math.min(100, Math.round(rawScore / lengthFactor));
  3001. const label = getLabel(normalizedScore);
  3002. // ── Sentence-region smoothing (HMM-style without an HMM) ─────────
  3003. // Map each text-bearing issue back to its sentence indexes, then
  3004. // merge adjacent flagged sentences into contiguous regions for UI
  3005. // highlighting. Borrowed from GPTZero's sentence-highlighting model
  3006. // — gives users "this paragraph is AI" rather than scattered hits.
  3007. const regions = buildSentenceRegions(text, deduped, sourceMode === 'rendered-markdown');
  3008. // Stats derived from the same deduped list so tier counts + patternCount
  3009. // sum to `deduped.length`. Previously patternCount subtracted
  3010. // `tier2Clusters` (a paragraph count) which produced inconsistent totals.
  3011. const tier1Count = deduped.filter((i) => i.type === 'tier1').length;
  3012. const tier2Count = deduped.filter((i) => i.type === 'tier2').length;
  3013. const tier3Count = deduped.filter((i) => i.type === 'tier3').length;
  3014. // ── Trinary classification (GPTZero-shaped) ──────────────────────
  3015. // Decouples confidence from AI-proportion. Maps the 0-100 score plus
  3016. // structural signals into HUMAN_ONLY / MIXED / AI_ONLY with a
  3017. // confidence band. Thresholds are FN-biased: ambiguity routes to
  3018. // MIXED, never AI_ONLY. Quote from GPTZero's design principle:
  3019. // "biases the detector to prefer making less-harmful false-negative
  3020. // errors over false-positive errors."
  3021. // Dense-AI-vocab trifecta: ≥5 distinct tier1 hits + ≥2 tier2 cluster
  3022. // paragraphs + ≥1 transition phrase, AND ≥150 words. Catches
  3023. // saturated ChatGPT prose without firing on dense-jargon human
  3024. // technical writing where the tier1 vocabulary (robust,
  3025. // comprehensive, leverage, ecosystem) legitimately overlaps with
  3026. // systems-programming idiom. Word-count gate prevents short ESL or
  3027. // contrived adversarial sentences from tripping the corroborator.
  3028. const tier1Distinct = new Set(deduped.filter((i) => i.type === 'tier1').map((i) => (i.text || '').toLowerCase())).size;
  3029. const hasTier2Cluster = tier2Clusters >= 2;
  3030. const hasTransition = deduped.some((i) => i.type === 'transition');
  3031. const denseAIVocab = wordCount >= 150 && tier1Distinct >= 5 && hasTier2Cluster && hasTransition;
  3032. const trinary = classifyTrinary({
  3033. score: normalizedScore,
  3034. issues: deduped,
  3035. regions,
  3036. normFlags: norm.flags,
  3037. wordCount,
  3038. denseAIVocab,
  3039. });
  3040. // Translate both dense issue indexes and half-open highlight boundaries.
  3041. // Mapping the region end from its final retained code unit avoids pulling
  3042. // a later removed roleplay marker into the highlighted source slice.
  3043. remapFindingsToSource(deduped, regions, sourceMap);
  3044. return {
  3045. // Legacy fields preserved for existing callers.
  3046. score: normalizedScore,
  3047. label,
  3048. issues: deduped,
  3049. stats: {
  3050. wordCount,
  3051. tier1Count,
  3052. tier2Count,
  3053. tier2Clusters,
  3054. tier3Count,
  3055. tier3Flags,
  3056. patternCount: deduped.length - tier1Count - tier2Count - tier3Count,
  3057. contextMode,
  3058. contextModeFallback,
  3059. sourceMode,
  3060. sourceModeFallback,
  3061. maskedFrontmatter,
  3062. maskedHtmlComments,
  3063. ignoredRegions,
  3064. normalization: norm.flags,
  3065. quotedLines,
  3066. maskedQuotes,
  3067. unmappedHighlights: regions._unmapped ?? 0,
  3068. denseAIVocab,
  3069. tier1Distinct,
  3070. },
  3071. // Trinary API — shape mirrors GPTZero so integrators can swap.
  3072. document_classification: trinary.classification,
  3073. class_probabilities: trinary.probabilities,
  3074. confidence_category: trinary.confidence,
  3075. highlight_sentence_for_ai: regions,
  3076. };
  3077. }
  3078. // ═══ Sentence regions + trinary classifier ═════════════════════════
  3079. // Coarse sentence spans over the whole text, as [start, end) offsets.
  3080. // Produces the same spans as the former /[^.!?]+[.!?]+|\S[^.!?]*$/g scan:
  3081. // each span runs from the end of the previous one through the next run of
  3082. // terminators, and a trailing fragment with no terminator starts at its
  3083. // first non-space character. The regex version backtracked to the end of
  3084. // the input at every position of a long terminator-free run, so a document
  3085. // that ended in blank lines, or whose masked comments became whitespace,
  3086. // cost O(n^2) (#235). This scan touches each character a bounded number
  3087. // of times.
  3088. const SENTENCE_TERMINATOR_RUN = /[.!?]+/g;
  3089. const FIRST_NON_SPACE = /\S/g;
  3090. function splitSentenceSpans(text) {
  3091. const spans = [];
  3092. const length = text.length;
  3093. let pos = 0;
  3094. while (pos < length) {
  3095. SENTENCE_TERMINATOR_RUN.lastIndex = pos;
  3096. const run = SENTENCE_TERMINATOR_RUN.exec(text);
  3097. if (run === null) {
  3098. FIRST_NON_SPACE.lastIndex = pos;
  3099. const head = FIRST_NON_SPACE.exec(text);
  3100. if (head !== null) spans.push([head.index, length]);
  3101. break;
  3102. }
  3103. if (run.index === pos) {
  3104. // Skip the entire bodyless run, not one character at a time: matching
  3105. // every remaining suffix would make a long punctuation run quadratic.
  3106. // If no later sentence terminator exists, the former regex's trailing
  3107. // alternative starts at the LAST terminator of this run.
  3108. const runEnd = pos + run[0].length;
  3109. SENTENCE_TERMINATOR_RUN.lastIndex = runEnd;
  3110. if (SENTENCE_TERMINATOR_RUN.exec(text) === null) {
  3111. spans.push([runEnd - 1, length]);
  3112. break;
  3113. }
  3114. pos = runEnd;
  3115. continue;
  3116. }
  3117. const end = run.index + run[0].length;
  3118. spans.push([pos, end]);
  3119. pos = end;
  3120. }
  3121. return spans;
  3122. }
  3123. function buildSentenceRegions(text, issues, trimBoundaryWhitespace = false) {
  3124. // Split text into sentences with source offsets preserved so the UI
  3125. // can highlight spans accurately. Sentence boundaries are coarse
  3126. // (.!?) — fine for highlighting, not for linguistic correctness.
  3127. const sentences = [];
  3128. for (const [spanStart, spanEnd] of splitSentenceSpans(text)) {
  3129. const raw = text.slice(spanStart, spanEnd);
  3130. const sentenceText = raw.trim();
  3131. if (sentenceText.length < 4) continue;
  3132. let start = spanStart;
  3133. let end = spanEnd;
  3134. if (trimBoundaryWhitespace) {
  3135. // trimStart/trimEnd, not `\s*$`: that regex retries from every
  3136. // position of a leading whitespace run and was quadratic on a span
  3137. // that began with a masked comment block (#235).
  3138. start += raw.length - raw.trimStart().length;
  3139. end -= raw.length - raw.trimEnd().length;
  3140. }
  3141. sentences.push({ start, end, text: sentenceText });
  3142. }
  3143. if (sentences.length === 0) return [];
  3144. // Map issue.text back to sentence indexes via substring search. Two
  3145. // kinds of issue stay out of the AI-highlight regions. Summary signals
  3146. // like "Punctuation density uniform across paragraphs" have no sentence
  3147. // anchor — they contribute to the document-level signal but not to
  3148. // highlights. Any category with authorship weight 0 is style-only by
  3149. // definition, so it belongs in issues[] but never in a field reserved
  3150. // for AI sentence highlights.
  3151. // Filter by issue TYPE not text-regex: text-based filtering used to
  3152. // drop legitimate phrase issues containing "across" / "density".
  3153. const NON_HIGHLIGHT_TYPES = new Set([
  3154. 'punct-distribution',
  3155. 'cross-para-burstiness',
  3156. 'fnword-trigram-entropy',
  3157. 'smart-punct-signature',
  3158. 'normalization-flag',
  3159. 'uniformity',
  3160. 'em-dash',
  3161. 'formatting',
  3162. 'tier3',
  3163. 'tier3-phrase',
  3164. 'tier3-phrase-cluster',
  3165. 'hashtag-stuff',
  3166. 'bullet-np-list',
  3167. 'unnecessary-hyphenation',
  3168. ]);
  3169. const hits = sentences.map(() => ({ count: 0, weight: 0 }));
  3170. const lowerText = text.toLowerCase();
  3171. let unmappedHighlights = 0;
  3172. for (const issue of issues) {
  3173. if (!issue.text || issue.text.length > 200) continue;
  3174. if (NON_HIGHLIGHT_TYPES.has(issue.type)) continue;
  3175. if ((ISSUE_WEIGHTS[issue.type] ?? 2) === 0) continue;
  3176. const needle = issue.text.toLowerCase();
  3177. let idx = 0;
  3178. let matched = false;
  3179. while ((idx = lowerText.indexOf(needle, idx)) !== -1) {
  3180. // Deduplicated verb issues must not highlight skipped noun matches.
  3181. // Reuse the same predicate and retain all qualifying verb locations.
  3182. if (issue.type === 'tier1-clarity' && needle === 'features' && featuresIsNoun(text, idx)) {
  3183. idx += needle.length;
  3184. continue;
  3185. }
  3186. matched = true;
  3187. for (let i = 0; i < sentences.length; i++) {
  3188. if (idx >= sentences[i].start && idx < sentences[i].end) {
  3189. hits[i].count++;
  3190. hits[i].weight += ISSUE_WEIGHTS[issue.type] ?? 2;
  3191. break;
  3192. }
  3193. }
  3194. idx += needle.length;
  3195. }
  3196. if (!matched) unmappedHighlights++;
  3197. }
  3198. // Window-merge contiguous flagged sentences. Allow 1 unflagged
  3199. // sentence gap between two flagged ones (the "smoothing" — keeps
  3200. // a single boring sentence from breaking what's clearly an AI
  3201. // passage). A sentence is "flagged" if it has ≥1 hit.
  3202. const regions = [];
  3203. let cur = null;
  3204. for (let i = 0; i < sentences.length; i++) {
  3205. if (hits[i].count > 0) {
  3206. if (cur === null) {
  3207. cur = { startSentence: i, endSentence: i, start: sentences[i].start, end: sentences[i].end, hitCount: hits[i].count, weight: hits[i].weight };
  3208. } else {
  3209. cur.endSentence = i;
  3210. cur.end = sentences[i].end;
  3211. cur.hitCount += hits[i].count;
  3212. cur.weight += hits[i].weight;
  3213. }
  3214. } else if (cur !== null) {
  3215. // Allow one-sentence gap.
  3216. const next = hits[i + 1];
  3217. if (next && next.count > 0) {
  3218. cur.endSentence = i;
  3219. cur.end = sentences[i].end;
  3220. continue;
  3221. }
  3222. regions.push(finalizeRegion(cur));
  3223. cur = null;
  3224. }
  3225. }
  3226. if (cur !== null) regions.push(finalizeRegion(cur));
  3227. // Expose the unmapped-highlight count via a non-enumerable property
  3228. // so the array length still reads naturally for consumers; the
  3229. // analyzer pulls it into stats.unmappedHighlights for diagnostics.
  3230. Object.defineProperty(regions, '_unmapped', { value: unmappedHighlights, enumerable: false });
  3231. return regions;
  3232. }
  3233. function finalizeRegion(r) {
  3234. // Map cumulative weight inside the region to a 0-1 score. Cap at 20
  3235. // weight = 1.0 (matches Heavy threshold density).
  3236. const score = Math.min(1, r.weight / 20);
  3237. return {
  3238. startSentence: r.startSentence,
  3239. endSentence: r.endSentence,
  3240. start: r.start,
  3241. end: r.end,
  3242. hitCount: r.hitCount,
  3243. score: Math.round(score * 100) / 100,
  3244. };
  3245. }
  3246. // FN-biased: false positives damage trust more than false negatives,
  3247. // so MIXED is wide and AI_ONLY requires multiple signals. Quote from
  3248. // GPTZero: "biases the detector to prefer making less-harmful
  3249. // false-negative errors over false-positive errors."
  3250. function classifyTrinary({ score, issues, regions, normFlags, wordCount, denseAIVocab }) {
  3251. // Strong corroborators — each is near-dispositive on its own:
  3252. // - cutoff-disclaimer (LLM self-identifies as an AI)
  3253. // - reasoning-artifact + chatbot-artifact co-occurrence
  3254. // - normalization-flag at threshold (≥2 ZWSP or homoglyphs).
  3255. // Threshold parity prevents a single stray ZWSP in copy-paste
  3256. // from Word/Notion from flipping to AI_ONLY at score 0.
  3257. // - denseAIVocab: ≥4 distinct tier1 hits AND ≥1 tier2 cluster AND
  3258. // ≥1 transition phrase — the trifecta that saturated ChatGPT
  3259. // prose triggers without needing whitelisted stylometric hits.
  3260. const hasCutoff = issues.some((i) => i.type === 'cutoff-disclaimer');
  3261. const hasNormFlag = normFlags.zeroWidth >= 2 || normFlags.homoglyph >= 2;
  3262. const hasReasoning = issues.some((i) => i.type === 'reasoning-artifact');
  3263. const hasChatbot = issues.some((i) => i.type === 'chatbot');
  3264. const strongCorrob =
  3265. (hasCutoff ? 1 : 0) +
  3266. (hasNormFlag ? 1 : 0) +
  3267. (hasReasoning && hasChatbot ? 1 : 0) +
  3268. (denseAIVocab ? 1 : 0);
  3269. // Weak stylometric corroborators. The style-only smart-punct finding
  3270. // must not alter classification, probabilities, or confidence even when
  3271. // other findings already put the score near a classification boundary.
  3272. const stylometricHits = ['punct-distribution', 'cross-para-burstiness', 'fnword-trigram-entropy']
  3273. .filter((t) => issues.some((i) => i.type === t)).length;
  3274. const weakCorrob = stylometricHits >= 2 ? 1 : 0;
  3275. // Thresholds:
  3276. // score < 15 with no strong → HUMAN_ONLY
  3277. // strong ≥ 1 OR score ≥ 70 → AI_ONLY (lowered from 80; high
  3278. // density of AI vocab is sufficient evidence)
  3279. // score ≥ 40 with any corroborator → AI_ONLY
  3280. // everything else with score ≥ 15 → MIXED
  3281. const totalCorrob = strongCorrob + weakCorrob;
  3282. let classification;
  3283. if (score < 15 && strongCorrob === 0) classification = 'HUMAN_ONLY';
  3284. else if (strongCorrob >= 1 || score >= 70) classification = 'AI_ONLY';
  3285. else if (score >= 40 && totalCorrob >= 1) classification = 'AI_ONLY';
  3286. else classification = 'MIXED';
  3287. // Humanizer-flag escalation: presence of bypass-trick chars is
  3288. // adversarial signal. If a normalization-flag fired we already
  3289. // counted it in strongCorrob → AI_ONLY. Confidence also gets a
  3290. // floor of 'medium' in that case (an adversary actively evading
  3291. // detection should never read as low-confidence noise).
  3292. // Soft probability distribution. Hand-tuned, not calibrated
  3293. // against the labeled corpora the repo now samples
  3294. // (`scripts/dataset-hc3.js`, `scripts/dataset-raid.js`). Largest
  3295. // class is computed as `1 - others` after rounding to guarantee
  3296. // sum=1 exactly. Sub-1% drift would otherwise hide in toFixed.
  3297. const aiSoft = Math.min(0.97, score / 100 + totalCorrob * 0.06 + strongCorrob * 0.08);
  3298. let p;
  3299. if (classification === 'HUMAN_ONLY') p = { human: Math.max(0.6, 1 - aiSoft), mixed: Math.min(0.35, aiSoft * 0.8), ai: Math.min(0.1, aiSoft * 0.3) };
  3300. else if (classification === 'AI_ONLY') p = { human: Math.max(0.02, 1 - aiSoft - 0.05), mixed: 0.1, ai: aiSoft };
  3301. else p = { human: Math.max(0.15, 0.6 - aiSoft * 0.5), mixed: 0.5, ai: aiSoft * 0.7 };
  3302. const rawSum = p.human + p.mixed + p.ai;
  3303. p.human = +(p.human / rawSum).toFixed(3);
  3304. p.mixed = +(p.mixed / rawSum).toFixed(3);
  3305. // Assign ai as the remainder so the three values sum to exactly 1.
  3306. // Clamp to >= 0 in case rounding pushes human+mixed above 1 (the
  3307. // remainder would otherwise show as -0 or -0.001 — surfaces as a
  3308. // negative percentage in any UI doing Math.round(p.ai * 100)).
  3309. p.ai = Math.max(0, +(1 - p.human - p.mixed).toFixed(3));
  3310. const probabilities = p;
  3311. // Confidence band:
  3312. // high — strongCorrob ≥ 2, OR cutoff-disclaimer, OR score < 8 (clean long doc)
  3313. // medium — strongCorrob ≥ 1, OR score ≥ 45 with weak corroborator, OR score < 20
  3314. // low — everything else
  3315. let confidence;
  3316. if (strongCorrob >= 2 || hasCutoff || (score < 8 && wordCount >= 100)) confidence = 'high';
  3317. else if (strongCorrob >= 1 || (score >= 45 && weakCorrob >= 1) || score < 20) confidence = 'medium';
  3318. else confidence = 'low';
  3319. return { classification, probabilities, confidence };
  3320. }
  3321. function getLabel(score) {
  3322. if (score === 0) return 'Clean';
  3323. if (score <= 15) return 'Minimal AI signals';
  3324. if (score <= 35) return 'Some AI patterns';
  3325. if (score <= 60) return 'Moderate AI signals';
  3326. if (score <= 80) return 'Strong AI signals';
  3327. return 'Heavy AI patterns';
  3328. }
  3329. function getColor(score) {
  3330. if (score <= 15) return '#44bb66';
  3331. if (score <= 35) return '#88bb44';
  3332. if (score <= 60) return '#ddaa00';
  3333. if (score <= 80) return '#ff8833';
  3334. return '#ff4444';
  3335. }
  3336. function deduplicateIssues(issues) {
  3337. const seen = new Set();
  3338. return issues.filter(issue => {
  3339. const key = `${issue.type}:${issue.text.toLowerCase()}`;
  3340. if (seen.has(key)) return false;
  3341. seen.add(key);
  3342. return true;
  3343. });
  3344. }
  3345. // ─── Severity labels ──────────────────────────────────────────
  3346. const SEVERITY_LABELS = {
  3347. critical: 'P0',
  3348. high: 'P1',
  3349. medium: 'P2',
  3350. low: 'P3',
  3351. };
  3352. const TYPE_LABELS = {
  3353. 'tier1': 'AI vocabulary',
  3354. 'tier1-clarity': 'Wordiness',
  3355. 'tier2': 'Word cluster',
  3356. 'tier3': 'Overused word',
  3357. 'transition': 'AI transition',
  3358. 'chatbot': 'Chatbot artifact',
  3359. 'sycophantic': 'Sycophantic tone',
  3360. 'filler': 'Filler phrase',
  3361. 'generic-conclusion': 'Generic conclusion',
  3362. 'lets-construction': '"Let\'s" opener',
  3363. 'reasoning-artifact': 'Reasoning artifact',
  3364. 'significance-inflation': 'Significance inflation',
  3365. 'vague-attribution': 'Vague attribution',
  3366. 'hollow-intensifier': 'Hollow intensifier',
  3367. // Keep the public type stable for API consumers; the user-facing name now
  3368. // describes the stock framing that the regexes actually match.
  3369. 'emotional-flatline': 'Stock reaction framing',
  3370. 'lingering-attention': 'Lingering-attention claim',
  3371. 'novelty-inflation': 'Novelty inflation',
  3372. 'cutoff-disclaimer': 'Cutoff disclaimer',
  3373. 'template-phrase': 'Template phrase',
  3374. 'false-concession': 'False concession',
  3375. 'rhetorical-question': 'Rhetorical question',
  3376. 'confidence-calibration': 'Confidence stacking',
  3377. 'em-dash': 'Em dash overuse',
  3378. 'uniformity': 'Rhythm uniformity',
  3379. 'formatting': 'Formatting',
  3380. 'tier3-phrase': 'Boilerplate phrase',
  3381. 'tier3-phrase-cluster': 'Boilerplate cluster',
  3382. 'hashtag-stuff': 'Hashtag stuffing',
  3383. 'bullet-np-list': 'Bullet-NP list',
  3384. 'hedge-stack': 'Hedge-stacked prediction',
  3385. 'future-narrative': 'Generic future narrative',
  3386. 'real-actual-inflation': '"Real/actual" inflation',
  3387. 'social-cta-closer': 'Engagement-bait closer',
  3388. 'formulaic-opener': 'Formulaic opener',
  3389. 'speculative-opener': 'Speculative scenario opener',
  3390. 'launch-intro': 'Launch-copy introduction',
  3391. 'crowd-contrast': 'Dramatized crowd contrast',
  3392. 'fake-casual-prop': 'Fake-casual prop',
  3393. 'title-case-header': 'Title Case header',
  3394. 'parenthetical-hedge': 'Parenthetical hedge',
  3395. 'smart-punct-signature': 'Smart-punct signature',
  3396. 'punct-distribution': 'Punctuation distribution',
  3397. 'fnword-trigram-entropy': 'Grammar repetition',
  3398. 'cross-para-burstiness': 'Cross-paragraph rhythm',
  3399. 'normalization-flag': 'Bypass-trick chars',
  3400. 'low-ttr': 'Low vocabulary diversity',
  3401. 'ai-placeholder': 'Unfilled placeholder',
  3402. 'ai-citation-markup': 'Chatbot citation markup leak',
  3403. 'ai-utm-source': 'AI-tool URL parameter',
  3404. 'unnecessary-hyphenation': 'Unnecessary hyphenation',
  3405. 'performed-insight': 'Performed-insight phrase',
  3406. 'negation-chain': 'Negation chain',
  3407. 'negative-parallelism': 'Negative parallelism',
  3408. 'dev-blog-boilerplate': 'Dev-blog boilerplate',
  3409. };
  3410. // The pattern tables as plain data, keyed by issue type, for tooling that
  3411. // re-emits them in another format (scripts/build-vale.js generates the Vale
  3412. // style in `vale/` from this). Regexes are `{ source, flags }` strings, not
  3413. // the live objects, so no `lastIndex` state is shared. A `gate` names logic
  3414. // analyzeText applies on top of the table, so the table alone does not
  3415. // reproduce a gated finding. Function fields (the `features` noun check,
  3416. // computed hyphenation fixes) cannot be data: they become a `gate` note or
  3417. // are left out. Not used by analyzeText. Bump `schemaVersion` on a change
  3418. // to this shape, not on a change to the patterns.
  3419. const PATTERN_TABLES = (() => {
  3420. const re = (r) => ({ source: r.source, flags: r.flags });
  3421. const list = (patterns) => patterns.map(re);
  3422. const freeze = (value) => {
  3423. if (value && typeof value === 'object') {
  3424. for (const v of Object.values(value)) freeze(v);
  3425. Object.freeze(value);
  3426. }
  3427. return value;
  3428. };
  3429. const NP_GATE = 'Reported only when another negative-parallelism frame starts within two sentences in the same paragraph.';
  3430. return freeze({
  3431. schemaVersion: '0.1.0',
  3432. types: {
  3433. 'tier1': {
  3434. words: Object.entries(TIER1).map(([word, replace]) => ({ word, replace })),
  3435. patterns: TIER1_PHRASES.filter((p) => !p.clarity).map((p) => ({ ...re(p.pattern), replace: p.replace })),
  3436. gate: "`words` match whole lowercase tokens split on [\\w'-]+, so hyphenated and possessive forms do not count. The `technical` context skips TECHNICAL_EXEMPT terms.",
  3437. },
  3438. 'tier1-clarity': {
  3439. patterns: TIER1_PHRASES.filter((p) => p.clarity).map((p) => ({
  3440. ...re(p.pattern),
  3441. replace: p.replace,
  3442. ...(p.skip ? { gate: 'Skipped where "features" reads as a noun (featuresIsNoun).' } : {}),
  3443. })),
  3444. },
  3445. 'tier2': {
  3446. words: Object.entries(TIER2).map(([word, replace]) => ({ word, replace })),
  3447. patterns: TIER2_CONDITIONAL.map((c) => ({ ...re(c.pattern), word: c.word, replace: c.suggestion })),
  3448. gate: 'Reported only when two or more distinct listed words share a paragraph. The `technical` context skips TECHNICAL_EXEMPT terms.',
  3449. },
  3450. 'tier3': {
  3451. words: [...TIER3],
  3452. gate: 'Reported only at max(3, floor(words * 0.03)) uses of one word; hyphenated words also match their dashless form.',
  3453. },
  3454. 'tier3-phrase': {
  3455. patterns: list(TIER3_PHRASES),
  3456. gate: 'Reported only at the second non-overlapping use of one phrase.',
  3457. },
  3458. 'tier3-phrase-cluster': {
  3459. patterns: list(TIER3_PHRASES),
  3460. gate: 'Reported only when three or more distinct phrases appear.',
  3461. },
  3462. 'transition': { patterns: list(TRANSITIONS) },
  3463. 'chatbot': { patterns: list(CHATBOT_ARTIFACTS) },
  3464. 'sycophantic': { patterns: list(SYCOPHANTIC) },
  3465. 'filler': { patterns: list(FILLERS) },
  3466. 'generic-conclusion': { patterns: list(GENERIC_CONCLUSIONS) },
  3467. 'lets-construction': { patterns: list(LETS_PATTERNS) },
  3468. 'reasoning-artifact': { patterns: list(REASONING_ARTIFACTS) },
  3469. 'significance-inflation': { patterns: list(SIGNIFICANCE_INFLATION) },
  3470. 'vague-attribution': { patterns: list(VAGUE_ATTRIBUTIONS) },
  3471. 'hollow-intensifier': { patterns: list(HOLLOW_INTENSIFIERS) },
  3472. 'emotional-flatline': { patterns: list(EMOTIONAL_FLATLINE) },
  3473. 'lingering-attention': { patterns: list(LINGERING_ATTENTION) },
  3474. 'novelty-inflation': { patterns: list(NOVELTY_INFLATION) },
  3475. 'cutoff-disclaimer': { patterns: list(CUTOFF_DISCLAIMERS) },
  3476. 'ai-placeholder': { patterns: list(AI_PLACEHOLDERS) },
  3477. 'ai-citation-markup': { patterns: list(AI_CITATION_MARKUP) },
  3478. 'ai-utm-source': { patterns: list(AI_UTM_SOURCE) },
  3479. 'template-phrase': { patterns: list(TEMPLATE_PHRASES) },
  3480. 'false-concession': { patterns: list(FALSE_CONCESSION) },
  3481. 'rhetorical-question': { patterns: list(RHETORICAL_QUESTIONS) },
  3482. 'hedge-stack': { patterns: list(HEDGE_STACK) },
  3483. 'future-narrative': { patterns: list(FUTURE_NARRATIVE) },
  3484. 'real-actual-inflation': { patterns: list(REAL_ACTUAL_INFLATION) },
  3485. 'social-cta-closer': { patterns: list(SOCIAL_CTA_CLOSER) },
  3486. 'performed-insight': { patterns: list([...PERFORMED_INSIGHT, ...STAGED_DISCOVERY]) },
  3487. 'negation-chain': { patterns: list(NEGATION_CHAIN) },
  3488. 'negative-parallelism': {
  3489. patterns: [
  3490. ...list(NEGATIVE_PARALLELISM_REVEAL),
  3491. ...NEGATIVE_PARALLELISM_CONTRAST.map((r) => ({ ...re(r), gate: NP_GATE })),
  3492. ],
  3493. },
  3494. 'dev-blog-boilerplate': { patterns: list(DEV_BLOG_BOILERPLATE) },
  3495. 'unnecessary-hyphenation': {
  3496. patterns: UNNECESSARY_HYPHENATION.map((e) => ({
  3497. ...re(e.pattern),
  3498. ...(typeof e.suggestion === 'string' ? { replace: e.suggestion } : {}),
  3499. })),
  3500. gate: 'Runs on text with file paths, package names and code identifiers masked (maskHyphenationProtected).',
  3501. },
  3502. 'formulaic-opener': { patterns: list(FORMULAIC_OPENERS) },
  3503. 'speculative-opener': { patterns: list(SPECULATIVE_OPENERS) },
  3504. 'launch-intro': { patterns: list(LAUNCH_INTROS) },
  3505. 'crowd-contrast': { patterns: list(CROWD_CONTRAST) },
  3506. 'fake-casual-prop': { patterns: list(FAKE_CASUAL_PROPS) },
  3507. 'parenthetical-hedge': { patterns: list(PARENTHETICAL_HEDGE) },
  3508. 'confidence-calibration': {
  3509. patterns: list(CONFIDENCE_CALIBRATION),
  3510. gate: 'Reported only when three or more matches occur in one text.',
  3511. },
  3512. },
  3513. });
  3514. })();
  3515. return {
  3516. analyzeText,
  3517. normalizeText,
  3518. getLabel,
  3519. getColor,
  3520. SEVERITY_LABELS,
  3521. TYPE_LABELS,
  3522. CONTEXT_MODES,
  3523. SOURCE_MODES,
  3524. PATTERN_TABLES,
  3525. };
  3526. })();
  3527. if (typeof module !== 'undefined' && module.exports) {
  3528. module.exports = AIDetector;
  3529. }
detector/patterns.test.js · Bundled script · 4,453 lines
  1. /**
  2. * Avoid AI Writing — detector fixtures
  3. *
  4. * Node-runnable smoke tests for the detection engine. Intentionally small and
  5. * dependency-free so they run on any `node >= 18` without installing
  6. * anything. Run by `npm test`, by `node scripts/run-tests.js
  7. * detector/patterns.test.js` for this suite alone, or directly as
  8. * `node detector/patterns.test.js`. CI runs `npm test`.
  9. *
  10. * Failure modes worth catching:
  11. * - AI-heavy text scoring as human (regression in pattern coverage)
  12. * - Plain prose scoring above "minimal" (false-positive drift)
  13. * - Length gates (too-short / too-long) not firing
  14. * - Stats failing to sum to issue count (dedup math drift)
  15. */
  16. const assert = require('node:assert/strict');
  17. const AIDetector = require('./patterns.js');
  18. let failed = 0;
  19. function test(name, fn) {
  20. try {
  21. fn();
  22. console.log(` ✓ ${name}`);
  23. } catch (err) {
  24. failed++;
  25. console.error(` ✗ ${name}`);
  26. console.error(` ${err.message}`);
  27. }
  28. }
  29. /**
  30. * Time `build(n)` at `base` and at 4x `base` (#208).
  31. *
  32. * Returns the best-of-three totals for an identical batch of scans at each
  33. * size, so the assertion compares how the work scales rather than how fast the
  34. * machine is. The batch is sized from the base input to lift both totals clear
  35. * of timer noise; clamping the small total instead — as an earlier revision
  36. * did — turns the ratio into a fixed millisecond allowance, and a quadratic
  37. * regression then passes whenever the machine is fast enough to stay under it.
  38. */
  39. function timeScaling(build, base, options) {
  40. const measure = (n, batch) => {
  41. const text = build(n);
  42. let best = Infinity;
  43. for (let run = 0; run < 3; run += 1) {
  44. const started = performance.now();
  45. for (let i = 0; i < batch; i += 1) AIDetector.analyzeText(text, options);
  46. best = Math.min(best, performance.now() - started);
  47. }
  48. return best;
  49. };
  50. const single = Math.max(measure(base, 1), 0.05);
  51. const batch = Math.min(2000, Math.max(1, Math.ceil(20 / single)));
  52. return { small: measure(base, batch), large: measure(base * 4, batch), batch };
  53. }
  54. console.log('Detector fixtures');
  55. test('empty text returns Empty label', () => {
  56. const r = AIDetector.analyzeText('');
  57. assert.equal(r.label, 'Empty');
  58. assert.equal(r.issues.length, 0);
  59. });
  60. test('text under 10 words returns tooShort flag', () => {
  61. const r = AIDetector.analyzeText('Short unscorable text snippet.');
  62. assert.equal(r.tooShort, true);
  63. assert.equal(r.label, 'Too short');
  64. });
  65. test('text over 10k words returns tooLong flag', () => {
  66. const r = AIDetector.analyzeText('word '.repeat(10001));
  67. assert.equal(r.tooLong, true);
  68. assert.equal(r.label, 'Text too long');
  69. });
  70. test('#234: unscored labels return stats with the same keys', () => {
  71. const empty = AIDetector.analyzeText('');
  72. const tooShort = AIDetector.analyzeText('Short unscorable text snippet.');
  73. const tooLong = AIDetector.analyzeText('word '.repeat(10001));
  74. const expectedKeys = Object.keys(empty.stats).sort();
  75. assert.deepEqual(Object.keys(tooShort.stats).sort(), expectedKeys);
  76. assert.deepEqual(Object.keys(tooLong.stats).sort(), expectedKeys);
  77. });
  78. test('#234: analyzeText throws TypeError for non-string input', () => {
  79. assert.throws(() => AIDetector.analyzeText(123), TypeError);
  80. });
  81. test('AI-heavy paragraph scores in Strong/Heavy range', () => {
  82. const text = [
  83. "In today's ever-evolving landscape, we delve into the intricate",
  84. 'tapestry of innovation. This seamless, robust paradigm showcases a',
  85. 'comprehensive framework. Moreover, it truly is a game-changer.',
  86. 'Furthermore, this pivotal moment underscores how we navigate the',
  87. 'complexities of modern AI.',
  88. ].join(' ');
  89. const r = AIDetector.analyzeText(text);
  90. assert.ok(r.score >= 60, `expected score ≥60, got ${r.score}`);
  91. assert.ok(['Strong AI signals', 'Heavy AI patterns'].includes(r.label), `got label: ${r.label}`);
  92. });
  93. test('plain human bug-report prose stays in Minimal range', () => {
  94. const text = [
  95. 'The build broke again this morning. Rolled back the auth refactor',
  96. 'and tests pass now. Still need to figure out why the token refresh',
  97. 'path hits a 401 for users on Safari but not Firefox — probably a',
  98. 'cookie scope issue but I want to confirm before shipping a fix.',
  99. ].join(' ');
  100. const r = AIDetector.analyzeText(text);
  101. assert.ok(r.score <= 20, `expected score ≤20, got ${r.score}`);
  102. });
  103. test('stats fields sum to issues length', () => {
  104. const text = [
  105. "In today's landscape of innovation, we leverage seamless paradigms",
  106. 'to harness the power of transformation. It is important to note',
  107. 'that experts believe this is pivotal. Let me think step by step.',
  108. ].join(' ');
  109. const r = AIDetector.analyzeText(text);
  110. const sum = r.stats.tier1Count + r.stats.tier2Count + r.stats.tier3Count + r.stats.patternCount;
  111. assert.equal(sum, r.issues.length, `stats sum (${sum}) != issues (${r.issues.length})`);
  112. });
  113. test('#123: rendered Markdown ignores frontmatter and multiline HTML comments', () => {
  114. const prose = [
  115. 'A clerk opened the file before sunrise. The names filled three pages.',
  116. 'He read them twice, signed the order, and passed it down the corridor.',
  117. ].join(' ');
  118. const comment = [
  119. '<!-- ARCHITECTURE',
  120. 'Furthermore, this is a load-bearing transition. Add comprehensive and pivotal context here.',
  121. '-->',
  122. prose,
  123. ].join('\n');
  124. const frontmatter = [
  125. '---',
  126. 'title: Draft',
  127. 'description: A comprehensive and pivotal exploration of a robust ecosystem',
  128. '---',
  129. prose,
  130. ].join('\n');
  131. const baseline = AIDetector.analyzeText(prose);
  132. for (const [name, text, expected] of [
  133. ['comment', comment, { maskedFrontmatter: 0, maskedHtmlComments: 1 }],
  134. ['frontmatter', frontmatter, { maskedFrontmatter: 1, maskedHtmlComments: 0 }],
  135. ]) {
  136. const plain = AIDetector.analyzeText(text);
  137. const rendered = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  138. assert.ok(plain.score > baseline.score, `${name}: plain mode must keep inspecting source text`);
  139. assert.equal(rendered.score, baseline.score, `${name}: rendered score must match visible prose`);
  140. assert.deepEqual(
  141. rendered.issues.map((issue) => [issue.type, issue.text]),
  142. baseline.issues.map((issue) => [issue.type, issue.text]),
  143. `${name}: rendered findings must match visible prose`,
  144. );
  145. assert.equal(rendered.stats.sourceMode, 'rendered-markdown');
  146. assert.equal(rendered.stats.maskedFrontmatter, expected.maskedFrontmatter);
  147. assert.equal(rendered.stats.maskedHtmlComments, expected.maskedHtmlComments);
  148. }
  149. });
  150. test('#123: same-line and unclosed HTML comments are source-only', () => {
  151. const prose = [
  152. 'The carpenter measured the door twice before cutting the oak board.',
  153. 'He shaved one edge, reset the hinges, and checked the latch again.',
  154. ].join(' ');
  155. const hidden = 'Moreover, this seamless and robust paradigm is a testament to progress.';
  156. const baseline = AIDetector.analyzeText(prose);
  157. for (const [name, text] of [
  158. ['same-line', `${prose}\n<!-- ${hidden} -->`],
  159. ['unclosed', `${prose}\n<!-- ${hidden}`],
  160. ]) {
  161. const rendered = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  162. assert.equal(rendered.score, baseline.score, `${name}: hidden text must not affect the score`);
  163. assert.equal(rendered.stats.maskedHtmlComments, 1);
  164. assert.equal(rendered.issues.some((issue) => /seamless|robust|testament/i.test(issue.text || '')), false);
  165. }
  166. });
  167. test('#123: comment markers inside fenced and inline code remain visible', () => {
  168. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  169. const cases = [
  170. ['fenced-unclosed', ['```html', '<!-- example marker stays open', '```', prose].join('\n')],
  171. ['inline-unclosed', `The guide shows \`<!-- example marker\` in code before the sample. ${prose}`],
  172. ['inline-closed', `The guide shows \`<!-- example -->\` in code before the sample. ${prose}`],
  173. ];
  174. for (const [name, text] of cases) {
  175. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  176. assert.notEqual(result.label, 'Too short', `${name}: code must not hide later prose`);
  177. assert.equal(result.stats.maskedHtmlComments, 0, `${name}: code markers are not comments`);
  178. assert.ok(result.issues.some((issue) => issue.type === 'transition'), `${name}: later prose must be analyzed`);
  179. }
  180. });
  181. test('#123: comment markers in top-level indented code remain visible', () => {
  182. const text = [
  183. ' <!-- seamless robust pivotal -->',
  184. '',
  185. 'Moreover, the editor checked the original document before changing the published account for the morning edition.',
  186. ].join('\n');
  187. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  188. assert.equal(result.stats.maskedHtmlComments, 0, 'an indented code marker is not an HTML comment');
  189. assert.deepEqual(
  190. result.issues.filter((issue) => issue.type === 'tier1').map((issue) => issue.text),
  191. ['seamless', 'robust', 'pivotal'],
  192. 'reader-visible indented code must still be analyzed',
  193. );
  194. assert.ok(result.issues.some((issue) => issue.type === 'transition' && issue.text === 'Moreover'));
  195. });
  196. test('#123: an indented HTML comment under a list item stays hidden', () => {
  197. const text = [
  198. '- Keep this item.',
  199. '',
  200. ' <!-- seamless robust pivotal -->',
  201. '',
  202. 'Moreover, the editor checked the original document before changing the published account for the morning edition.',
  203. ].join('\n');
  204. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  205. assert.equal(result.stats.maskedHtmlComments, 1);
  206. assert.equal(result.issues.some((issue) => /seamless|robust|pivotal/i.test(issue.text || '')), false);
  207. assert.ok(result.issues.some((issue) => issue.type === 'transition' && issue.text === 'Moreover'));
  208. });
  209. test('#123: list syntax inside a comment cannot hide later indented code', () => {
  210. const text = [
  211. '<!--',
  212. '- hidden list item',
  213. ' hidden continuation -->',
  214. '',
  215. ' <!-- seamless robust pivotal -->',
  216. '',
  217. 'Moreover, the editor checked the original document before changing the published account for the morning edition.',
  218. ].join('\n');
  219. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  220. assert.equal(result.stats.maskedHtmlComments, 1, 'the indented code marker is not a real comment');
  221. assert.deepEqual(
  222. result.issues.filter((issue) => issue.type === 'tier1').map((issue) => issue.text),
  223. ['seamless', 'robust', 'pivotal'],
  224. );
  225. assert.ok(result.issues.some((issue) => issue.type === 'transition' && issue.text === 'Moreover'));
  226. });
  227. test('#123: backticks inside an HTML comment do not protect its closing delimiter', () => {
  228. const commentPrefix = '<!-- plan `-->';
  229. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  230. const visibleText = `\` Furthermore, seamless robust and pivotal notes stay hidden -->\n${prose}`;
  231. const baseline = AIDetector.analyzeText(visibleText);
  232. const source = commentPrefix + visibleText;
  233. const result = AIDetector.analyzeText(source, { sourceMode: 'rendered-markdown' });
  234. assert.equal(result.stats.maskedHtmlComments, 1);
  235. assert.equal(result.score, 19, 'all reader-visible findings must contribute to the rendered score');
  236. assert.equal(result.score, baseline.score, 'the first literal --> must close the comment');
  237. assert.ok(result.issues.some((issue) => issue.type === 'transition' && issue.text === 'Furthermore'));
  238. assert.deepEqual(
  239. result.issues.filter((issue) => issue.type === 'tier1').map((issue) => issue.text),
  240. ['seamless', 'robust', 'pivotal'],
  241. );
  242. assert.deepEqual(
  243. result.issues.map((issue) => [issue.type, issue.text]),
  244. baseline.issues.map((issue) => [issue.type, issue.text]),
  245. );
  246. assert.deepEqual(
  247. result.issues.filter((issue) => Number.isInteger(issue.index)).map((issue) => issue.index),
  248. baseline.issues
  249. .filter((issue) => Number.isInteger(issue.index))
  250. .map((issue) => issue.index + commentPrefix.length),
  251. );
  252. for (const issue of result.issues.filter((candidate) => Number.isInteger(candidate.index))) {
  253. assert.equal(issue.index, source.indexOf(issue.text), `${issue.text}: source offset must survive masking`);
  254. }
  255. });
  256. test('#123: code delimiters in one comment cannot hide a later comment', () => {
  257. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  258. const hidden = 'Furthermore, this seamless robust paradigm is a testament to progress.';
  259. const text = ['<!--', '```', '-->', prose, `<!-- ${hidden} -->`].join('\n');
  260. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  261. assert.equal(result.stats.maskedHtmlComments, 2);
  262. assert.equal(result.issues.some((issue) => /seamless|robust|paradigm|testament/i.test(issue.text || '')), false);
  263. assert.ok(result.issues.some((issue) => issue.type === 'transition'), 'visible prose must still fire');
  264. });
  265. test('#190: tilde fences and adjacent list markers in comments preserve later context', () => {
  266. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  267. const hidden = 'Furthermore, this seamless robust paradigm is a testament to progress.';
  268. const cases = [
  269. ['tilde fence', ['<!--', '~~~', '-->', prose, `<!-- ${hidden} -->`].join('\n'), 2],
  270. ['adjacent list marker', ['-<!-- hidden -->', '', ` <!-- ${hidden} -->`, '', prose].join('\n'), 2],
  271. ];
  272. for (const [name, text, commentCount] of cases) {
  273. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  274. assert.equal(result.stats.maskedHtmlComments, commentCount, `${name}: comment count`);
  275. assert.equal(
  276. result.issues.some((issue) => /seamless|robust|paradigm|testament/i.test(issue.text || '')),
  277. false,
  278. `${name}: hidden prose must stay hidden`,
  279. );
  280. assert.ok(result.issues.some((issue) => issue.type === 'transition' && issue.text === 'Moreover'));
  281. }
  282. });
  283. test('#123: short HTML comments close at an overlapping delimiter', () => {
  284. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  285. for (const comment of ['<!-->', '<!--->']) {
  286. const result = AIDetector.analyzeText(`${comment}\n${prose}`, { sourceMode: 'rendered-markdown' });
  287. assert.equal(result.stats.maskedHtmlComments, 1);
  288. assert.notEqual(result.label, 'Too short', `${comment}: later prose must remain visible`);
  289. assert.equal(result.issues.find((issue) => issue.type === 'transition').index, comment.length + 1);
  290. }
  291. });
  292. test('#123: rendered Markdown preserves issue offsets after masked source', () => {
  293. const sourceOnly = '<!-- This seamless, robust paradigm should stay hidden. -->\r\n';
  294. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  295. const result = AIDetector.analyzeText(sourceOnly + prose, { sourceMode: 'rendered-markdown' });
  296. const transition = result.issues.find((issue) => issue.type === 'transition');
  297. assert.ok(transition, 'reader-facing prose should still be analyzed');
  298. assert.equal(transition.index, sourceOnly.length);
  299. assert.equal(result.highlight_sentence_for_ai[0].start, sourceOnly.length);
  300. assert.equal(result.highlight_sentence_for_ai[0].end, sourceOnly.length + prose.length);
  301. });
  302. test('#123: multiline blockquote masking preserves later source offsets', () => {
  303. const quote = '> This seamless landscape is a testament to progress.\r\n> Moreover, it is a robust paradigm.\r\n';
  304. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  305. const result = AIDetector.analyzeText(quote + prose, { sourceMode: 'rendered-markdown' });
  306. const transition = result.issues.find((issue) => issue.type === 'transition');
  307. assert.ok(transition, 'reader-facing prose after the quote should still be analyzed');
  308. assert.equal(transition.index, quote.length);
  309. assert.equal(result.highlight_sentence_for_ai[0].start, quote.length);
  310. assert.equal(result.highlight_sentence_for_ai[0].end, quote.length + prose.length);
  311. assert.equal(result.stats.quotedLines, 2);
  312. });
  313. test('#123: plain mode preserves legacy blockquote paragraph scoring', () => {
  314. const text = [
  315. 'We harness practical tools for ordinary work each morning.',
  316. '> This quoted material should not count against the author.',
  317. '> It contains another quoted line for the detector.',
  318. 'We navigate routine problems with care before the daily review.',
  319. ].join('\n');
  320. const result = AIDetector.analyzeText(text);
  321. assert.equal(result.score, 6);
  322. assert.deepEqual(
  323. result.issues.filter((issue) => issue.type === 'tier2').map((issue) => issue.text),
  324. ['harness', 'navigate'],
  325. );
  326. assert.equal(result.stats.quotedLines, 2);
  327. const leadingWhitespace = '\n\nMoreover, the editor checked the original document before changing the published account for the morning edition.';
  328. assert.equal(
  329. AIDetector.analyzeText(leadingWhitespace).highlight_sentence_for_ai[0].start,
  330. 0,
  331. 'plain-mode highlight boundaries must retain their legacy shape',
  332. );
  333. });
  334. test('#123: rendered Markdown recognizes blank-first-line and CR-only frontmatter', () => {
  335. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  336. const baseline = AIDetector.analyzeText(prose);
  337. const cases = [
  338. ['blank-first-line', ['---', '', 'title: Draft', 'description: A comprehensive and pivotal exploration', '---', ''].join('\n')],
  339. ['CR-only', ['---', '', 'title: Draft', 'description: A comprehensive and pivotal exploration', '---', ''].join('\r')],
  340. ];
  341. for (const [name, sourceOnly] of cases) {
  342. const result = AIDetector.analyzeText(sourceOnly + prose, { sourceMode: 'rendered-markdown' });
  343. assert.equal(result.stats.maskedFrontmatter, 1, `${name}: frontmatter must be masked`);
  344. assert.equal(result.score, baseline.score, `${name}: metadata must not affect the score`);
  345. const transition = result.issues.find((issue) => issue.type === 'transition');
  346. assert.ok(transition, `${name}: visible prose must still be analyzed`);
  347. assert.equal(transition.index, sourceOnly.length);
  348. assert.equal(result.highlight_sentence_for_ai[0].start, sourceOnly.length);
  349. }
  350. });
  351. test('#123: rendered Markdown does not mistake a thematic break for frontmatter', () => {
  352. for (const text of [
  353. ['---', '', 'Moreover, the team described a seamless and robust landscape in the final report.', '', '---'].join('\n'),
  354. ['---', 'Moreover, the team described a seamless and robust landscape in the final report.', '---'].join('\n'),
  355. ]) {
  356. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdown' });
  357. assert.equal(result.stats.maskedFrontmatter, 0);
  358. assert.ok(result.issues.length > 0, 'prose between thematic breaks should remain visible');
  359. }
  360. });
  361. test('#123: unknown source modes fall back visibly to plain', () => {
  362. const text = '<!-- Moreover, this seamless and robust paradigm is hidden. -->\nVisible prose has enough words for the detector to score this input normally.';
  363. const result = AIDetector.analyzeText(text, { sourceMode: 'rendered-markdonw' });
  364. assert.equal(result.stats.sourceMode, 'plain');
  365. assert.equal(result.stats.sourceModeFallback, 'rendered-markdonw');
  366. assert.equal(result.stats.maskedHtmlComments, 0);
  367. assert.ok(result.issues.length > 0, 'fallback must retain plain-mode behavior');
  368. for (const requested of ['', false, 0, null]) {
  369. const fallback = AIDetector.analyzeText(text, { sourceMode: requested });
  370. assert.equal(fallback.stats.sourceMode, 'plain');
  371. assert.equal(fallback.stats.sourceModeFallback, requested);
  372. }
  373. const omitted = AIDetector.analyzeText(text);
  374. assert.equal(omitted.stats.sourceModeFallback, undefined);
  375. });
  376. test('#190: many HTML comments avoid quadratic rescanning', () => {
  377. // Ratio, not budget (#208): a linear masker takes about 4x longer on 4x the
  378. // comments, while the per-comment full rescan this test exists to catch takes
  379. // about 13x. An absolute millisecond budget measured the runner's load
  380. // instead of the masker's complexity, so it failed on slow machines.
  381. const count = 4000;
  382. // Every comment contains the unmatched backtick that forced the old
  383. // implementation to rebuild whole-document code masks per comment.
  384. const build = (comments) =>
  385. `${'<!-- ` -->\n'.repeat(comments)}one two three four five six seven eight nine ten`;
  386. const result = AIDetector.analyzeText(build(count), { sourceMode: 'rendered-markdown' });
  387. assert.equal(result.stats.maskedHtmlComments, count);
  388. const { small, large } = timeScaling(build, count / 4, { sourceMode: 'rendered-markdown' });
  389. assert.ok(
  390. large < small * 8,
  391. `masking must not rescan the full document per comment: 4x comments took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`,
  392. );
  393. });
  394. test('adversarial Markdown scans stay within a bounded time', () => {
  395. const ordinary = 'one two three four five six seven eight nine ten';
  396. // Ratio, not budget (#208): every attack is measured at its base size and
  397. // at 4x, so the assertion is about how the scan scales rather than about
  398. // how loaded the runner happens to be.
  399. const attacks = [
  400. ['unclosed code span', (n) => `${ordinary} ${'`'.repeat(n)}${'a'.repeat(n)}`, 2500],
  401. ['unclosed anchor', (n) => `${ordinary} ${'<a'.repeat(n)}`, 10000],
  402. ['heading tab run', (n) => `## 1.1.1${'\t'.repeat(n)}— x\n${ordinary}`, 20000],
  403. ['long version digits', (n) => `## 1.1.${'1'.repeat(n)}]x — 2026-01-01\n${ordinary}`, 64000],
  404. ['indented block run', (n) => `- ${' '.repeat(n)}X\rY\n${ordinary}`, 10000],
  405. ['table delimiter tab run', (n) => `${ordinary}\n-|-${'\t'.repeat(n)}x`, 4000],
  406. ['thematic break tab run', (n) => `${ordinary}\n***${'\t'.repeat(n)}x`, 4000],
  407. ];
  408. for (const [name, build, base] of attacks) {
  409. const { small, large } = timeScaling(build, base);
  410. assert.ok(
  411. large < small * 8,
  412. `${name}: 4x input took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`,
  413. );
  414. }
  415. });
  416. test('version-heading dash carve-out does not swallow prose after a closed version label', () => {
  417. const text = '## [1.1.1] Not a version label — 2026-01-01\n\nOne two three four five six seven eight nine ten.';
  418. const issues = AIDetector.analyzeText(text).issues.filter((issue) => issue.type === 'em-dash');
  419. assert.equal(issues.length, 1, 'text after a closed version label must keep its prose dash visible');
  420. });
  421. test('repeated Tier 1 phrase does not inflate score linearly', () => {
  422. const single = AIDetector.analyzeText('We delve into the landscape of many things today.');
  423. const fivefold = AIDetector.analyzeText(
  424. 'We delve into the landscape. We delve into the landscape. We delve into the landscape. We delve into the landscape. We delve into the landscape of things.'
  425. );
  426. assert.ok(
  427. fivefold.score <= single.score + 20,
  428. `repeated phrase should not 5× the score (single=${single.score}, fivefold=${fivefold.score})`
  429. );
  430. });
  431. test('em-dash detector skips definition-list separators (bold term / link + dash)', () => {
  432. const text = [
  433. '- **Detect mode** — flag patterns without rewriting anything in the file.',
  434. '- **Edit mode** — rewrite the file in place and report what changed.',
  435. '- [agentskills.io](https://agentskills.io) — the SKILL.md format this repo follows.',
  436. 'The catalog lives in one file and a CI check keeps the README count honest.',
  437. ].join('\n');
  438. const r = AIDetector.analyzeText(text);
  439. const emDashIssues = r.issues.filter((i) => i.type === 'em-dash');
  440. assert.equal(emDashIssues.length, 0, 'separator-position dashes should not count toward the rate');
  441. });
  442. test('#351: em-dash carve-out covers HTML <li> separators with a strong, b or link lead', () => {
  443. const items = [
  444. ['Postgres', 'primary store for orders and invoices'],
  445. ['Redis', 'cache for session tokens'],
  446. ['Kafka', 'event bus between services'],
  447. ['S3', 'long-term storage for exports'],
  448. ];
  449. const intro = '<p>The cutover took one evening and two people, and the stack did not change.</p>';
  450. const strong = [intro, '<ul>', ...items.map(([t, d]) => `<li><strong>${t}</strong> — ${d}</li>`), '</ul>'].join('\n');
  451. const bold = [intro, '<ol>', ...items.map(([t, d]) => ` <li class="x"><b>${t}</b> (<code>${t.toLowerCase()}</code>) — ${d}</li>`), '</ol>'].join('\n');
  452. const link = [intro, '<ul>', ...items.map(([t, d]) => `<li><a href="/docs/${t}">${t}</a> — ${d}</li>`), '</ul>'].join('\n');
  453. for (const [name, text] of [['strong', strong], ['b + code', bold], ['link', link]]) {
  454. for (const sourceMode of ['plain', 'rendered-markdown']) {
  455. const hits = AIDetector.analyzeText(text, { sourceMode }).issues.filter((i) => i.type === 'em-dash');
  456. assert.equal(hits.length, 0, `${name} (${sourceMode}): HTML separator dashes should not count`);
  457. }
  458. }
  459. });
  460. test('#351: HTML em dashes outside the lead-term separator position still count', () => {
  461. const intro = '<p>The cutover took one evening and two people, and the stack did not change.</p>';
  462. // Dash after plain text in an <li>, a prose dash later in a separator item,
  463. // and dashes in a <p> all remain prose punctuation.
  464. const plainLead = [intro, '<ul>',
  465. '<li>Postgres — primary store</li>', '<li>Redis — cache</li>',
  466. '<li>Kafka — event bus</li>', '<li>S3 — exports</li>', '</ul>'].join('\n');
  467. const laterDash = [intro, '<ul>',
  468. '<li><strong>Postgres</strong> — the store — and the oldest box</li>',
  469. '<li><strong>Redis</strong> — the cache — and the newest box</li>', '</ul>'].join('\n');
  470. const para = '<p>We moved — slowly — to the new host. Then the disks failed — twice — in one week.</p>';
  471. for (const [name, text, expected] of [['plain-text lead', plainLead, 4], ['later dash', laterDash, 2], ['paragraph', para, 4]]) {
  472. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'em-dash');
  473. assert.equal(hits.length, 1, `${name}: prose dashes must still fire`);
  474. assert.match(hits[0].text, new RegExp(`^${expected} em dashes`), name);
  475. }
  476. });
  477. test('#351: a bold clause in an HTML <li> is not a lead term; its dash still counts', () => {
  478. const intro = '<p>The cutover took one evening and two people, and the stack did not change.</p>';
  479. const text = [intro, '<ul>',
  480. ...Array(4).fill('<li><strong>The committee reviewed the proposal and rejected it</strong> — the chair recorded the dissent.</li>'),
  481. '</ul>'].join('\n');
  482. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'em-dash');
  483. assert.equal(hits.length, 1);
  484. assert.match(hits[0].text, /^4 em dashes/);
  485. });
  486. test('em-dash carve-out covers numbered-list separators too', () => {
  487. const text = [
  488. '1. **Install** — run the setup script from the repo root.',
  489. '2. **Configure** — copy the sample config and set the API token.',
  490. '3. **Verify** — the status command reports green when everything works.',
  491. '4) **Cleanup** — remove the temp files once the run finishes.',
  492. 'The whole flow takes about two minutes on a fresh machine.',
  493. ].join('\n');
  494. const r = AIDetector.analyzeText(text);
  495. const emDashIssues = r.issues.filter((i) => i.type === 'em-dash');
  496. assert.equal(emDashIssues.length, 0, 'numbered-list separators should not count toward the rate');
  497. });
  498. test('em-dash carve-out covers a bold lead term with a parenthetical (#67)', () => {
  499. // Found by the self-scan: the bare `- **Term** —` form was carved out but
  500. // `- **Term** (`slug`) —` was not, though it is the same definition
  501. // typography. 1 of the 84 counted dashes in CHANGELOG.md was this shape.
  502. const text = [
  503. '- **Lingering-attention claims** (`lingering-attention`) — the share-post frame.',
  504. '- **Narrated candor** (judgment-only) — announcing your disclosure instead of disclosing.',
  505. 'Both rules landed in the same release and share a severity tier.',
  506. ].join('\n');
  507. const r = AIDetector.analyzeText(text);
  508. assert.equal(r.issues.filter((i) => i.type === 'em-dash').length, 0);
  509. });
  510. test('em-dash carve-out covers Keep-a-Changelog version headings (#67)', () => {
  511. // `## [3.21.0] — 2026-07-30` joins a label to a value exactly as a list
  512. // separator does. 32 of the 84 counted dashes in CHANGELOG.md were these.
  513. const body = Array.from({ length: 60 }, (_, i) => `word${i}`).join(' ') + '.';
  514. const text = `## [3.21.0] — 2026-07-30\n\n${body}\n\n## [3.20.0] — 2026-07-29\n\n${body}`;
  515. const r = AIDetector.analyzeText(text);
  516. assert.equal(r.issues.filter((i) => i.type === 'em-dash').length, 0);
  517. });
  518. test('em-dash carve-out stays narrow — prose dashes in a heading still fire (#67)', () => {
  519. const text = [
  520. '## Why this matters — and why it did not before now',
  521. 'Short text — with several — prose dashes — packed into it here today.',
  522. ].join('\n');
  523. const r = AIDetector.analyzeText(text);
  524. assert.ok(r.issues.filter((i) => i.type === 'em-dash').length >= 1);
  525. });
  526. test('hedge-stack does not fire on ordinary negation or inverted questions (#69)', () => {
  527. // Measured on the human-control corpus: 3 of 4 hedge-stack flags were this
  528. // over-match. The old pattern allowed two words between modal and adverb.
  529. const frame = (s) => `The committee reviewed the proposal at length and concluded that it ${s} work as designed, given every constraint documented during the previous quarter.`;
  530. for (const phrase of ['could not possibly', 'could never possibly', 'could a savage possibly', 'might a person conceivably']) {
  531. const r = AIDetector.analyzeText(frame(phrase));
  532. assert.equal(r.issues.filter((i) => i.type === 'hedge-stack').length, 0, `"${phrase}" should not fire`);
  533. }
  534. for (const phrase of ['could potentially', 'may eventually unlock', 'might ultimately transform']) {
  535. const r = AIDetector.analyzeText(frame(phrase));
  536. assert.ok(r.issues.filter((i) => i.type === 'hedge-stack').length >= 1, `"${phrase}" should still fire`);
  537. }
  538. });
  539. test('em-dash carve-out requires a list marker — line-initial bold splices still fire', () => {
  540. const text = [
  541. '**The architecture** — it scales horizontally without coordination.',
  542. '**The cache layer** — it absorbs the read traffic before it hits disk.',
  543. '**The result** — latency drops and throughput climbs on every node.',
  544. ].join('\n');
  545. const r = AIDetector.analyzeText(text);
  546. const emDashIssues = r.issues.filter((i) => i.type === 'em-dash');
  547. assert.ok(emDashIssues.length >= 1, 'bold-lead splices outside a list should still count');
  548. });
  549. test('smart-punct signature is not corroborated by separator-only em dashes', () => {
  550. // Curly quotes + Oxford commas + zero typos + ≥80 words, but every em
  551. // dash is a list-item separator. Before the carve-out this fired on
  552. // the dash-as-typography; now the em-dash leg of the co-occurrence
  553. // requires a non-separator dash.
  554. const text = [
  555. '- **Detect mode** — flags “possible issues” without rewriting, so you can review, compare, and decide.',
  556. '- **Edit mode** — rewrites the file in place and keeps the original wording where it already reads fine.',
  557. '- **Voice profiles** — casual, professional, and technical presets tune how hard each rule is enforced.',
  558. 'The catalog lives in one file, the CI check keeps the README count honest, and the plugin copy is generated from the root skill so the two can never drift apart in a release.',
  559. ].join('\n');
  560. const r = AIDetector.analyzeText(text);
  561. const hits = r.issues.filter((i) => i.type === 'smart-punct-signature');
  562. assert.equal(hits.length, 0, 'separator-only dashes should not complete the co-occurrence signature');
  563. });
  564. test('#351: house-style punctuation does not change authorship outputs', () => {
  565. // Synthetic contract fixtures, not labeled human-corpus evidence. Change
  566. // only one apostrophe so word counts, prose dashes, and other hits match.
  567. const plain = "The editor's notes arrived before lunch, so Mara read the first page while the room was still empty. The report covers labels, dates, and names — each checked against the signed copy in the archive. A short note explains why the final page has a blank line near the bottom. Nobody changed the order of the entries after the clerk counted them on Friday. Later that day, Jo found the folder beside the door and put it back on the shelf. The key remained with the desk clerk until the next shift began, when a runner came to collect the files.";
  568. const prefixes = ['', 'Delve intricate tapestry pivotal meticulous testament vibrant bustling nestled beacon. '];
  569. for (const prefix of prefixes) {
  570. for (const contextMode of ['general', 'technical', 'marketing', 'personal']) {
  571. for (const sourceMode of ['plain', 'rendered-markdown']) {
  572. const control = AIDetector.analyzeText(prefix + plain, { contextMode, sourceMode });
  573. const result = AIDetector.analyzeText(prefix + plain.replace("editor's", 'editor’s'), { contextMode, sourceMode });
  574. assert.ok(control.stats.wordCount >= 80 && control.stats.wordCount < 150);
  575. assert.equal(result.stats.wordCount, control.stats.wordCount);
  576. if (prefix) {
  577. assert.ok(control.score >= 40 && control.score < 70, 'exercise the corroborator classification boundary');
  578. assert.equal(control.document_classification, 'MIXED');
  579. }
  580. const hits = result.issues.filter(i => i.type === 'smart-punct-signature');
  581. assert.equal(hits.length, sourceMode === 'plain' ? 1 : 0);
  582. if (hits.length) {
  583. assert.equal(hits[0].severity, 'low');
  584. assert.match(hits[0].suggestion, /not evidence of AI authorship/);
  585. }
  586. assert.deepEqual(result.issues.filter(i => i.type !== 'smart-punct-signature'), control.issues,
  587. 'other findings must stay active');
  588. for (const field of ['score', 'label', 'document_classification', 'confidence_category']) {
  589. assert.equal(result[field], control[field], `${contextMode}/${sourceMode}: typography changed ${field}`);
  590. }
  591. assert.deepEqual(result.class_probabilities, control.class_probabilities);
  592. assert.deepEqual(result.highlight_sentence_for_ai, control.highlight_sentence_for_ai);
  593. }
  594. }
  595. }
  596. });
  597. test('em-dash detector still fires on mid-sentence splices at the same density', () => {
  598. const text =
  599. 'The build is fast — under a second — on most machines. The cache helps — especially on cold starts. Deploys run on push — no manual step — and roll back automatically.';
  600. const r = AIDetector.analyzeText(text);
  601. const emDashIssues = r.issues.filter((i) => i.type === 'em-dash');
  602. assert.ok(emDashIssues.length >= 1, 'prose splices should still flag');
  603. });
  604. test('#73: em-dash overuse remains a P2 edit without affecting authorship output', () => {
  605. const shared = 'The team reviewed the release notes carefully. '.repeat(40);
  606. const baseline = AIDetector.analyzeText(`${shared}One point, another point, and a final point.`);
  607. const result = AIDetector.analyzeText(`${shared}One point — another point — and a final point.`);
  608. const emDashIssues = result.issues.filter((i) => i.type === 'em-dash');
  609. assert.equal(emDashIssues.length, 1, 'over-limit em dashes must remain visible');
  610. assert.equal(AIDetector.SEVERITY_LABELS[emDashIssues[0].severity], 'P2');
  611. assert.equal(result.score, baseline.score, 'em-dash style edits must not change the AI score');
  612. assert.equal(result.label, baseline.label, 'em-dash style edits must not change the AI label');
  613. assert.equal(result.document_classification, baseline.document_classification);
  614. assert.equal(result.confidence_category, baseline.confidence_category);
  615. assert.deepEqual(result.class_probabilities, baseline.class_probabilities);
  616. assert.deepEqual(result.highlight_sentence_for_ai, []);
  617. });
  618. test('em-dash detector ignores CLI flags like --save-dev', () => {
  619. const text = 'Run npm install --save-dev and then npm run build --no-verify --silent. Takes about ten seconds on this machine. The package is installed into node_modules directly after the install command completes successfully.';
  620. const r = AIDetector.analyzeText(text);
  621. const emDashIssues = r.issues.filter((i) => i.type === 'em-dash');
  622. assert.equal(emDashIssues.length, 0, 'CLI flags should not count as em dashes');
  623. });
  624. test('chatbot artifacts score as P0 critical', () => {
  625. const text = "I hope this helps! Let me know if you need anything else. Great question! Feel free to reach out.";
  626. const r = AIDetector.analyzeText(text);
  627. const chatbotIssues = r.issues.filter((i) => i.type === 'chatbot');
  628. assert.ok(chatbotIssues.length >= 2, `expected chatbot detections, got ${chatbotIssues.length}`);
  629. assert.equal(AIDetector.SEVERITY_LABELS[chatbotIssues[0].severity], 'P0');
  630. });
  631. test('crypto-shill social post with hashtag block + bullet-NP lists flags', () => {
  632. // Reported 2026-05-16 as a "skipped" detection. Avoids every Tier 1
  633. // word ("delve", "robust", "leverage") and substitutes synonyms the
  634. // wordlist misses, but stacks structural signals: 6-item bullet-NP
  635. // list, 15-tag hashtag block, "may become one of the most important
  636. // narratives" future-narrative template, "could potentially" hedge
  637. // stack, and ten distinct crypto-shill boilerplate phrases.
  638. const text = `The future of decentralized computational infrastructure is evolving rapidly as blockchain-integrated mining ecosystems continue to merge with artificial intelligence, distributed compute, and tokenized incentive structures.
  639. MineBench represents an interesting example of this emerging sector by combining benchmark-based mining participation, token rewards, and scalable network contribution models into a unified ecosystem designed for long-term sustainability and user engagement.
  640. After several hours of testing, the platform demonstrated:
  641. * Stable mining efficiency
  642. * Reliable pool connectivity
  643. * Optimized RandomX computational performance
  644. * Low failed share rates
  645. * Effective hardware utilization
  646. * Consistent thermal stability
  647. The integration of reward-based participation mechanisms alongside decentralized infrastructure concepts could potentially create new opportunities for community-driven computational networks.
  648. The intersection of AI, DePIN, mining infrastructure, and decentralized compute may become one of the most important narratives of the next market cycle.
  649. #AI #Crypto #Blockchain #DePIN #Mining #Web3 #Solana #RandomX #DecentralizedAI #PassiveIncome #Infrastructure #Innovation #Technology #FutureTech #Tokenomics`;
  650. const r = AIDetector.analyzeText(text);
  651. const types = new Set(r.issues.map((i) => i.type));
  652. assert.ok(types.has('bullet-np-list'), 'expected bullet-np-list flag');
  653. assert.ok(types.has('hashtag-stuff'), 'expected hashtag-stuff flag');
  654. assert.ok(types.has('future-narrative'), 'expected future-narrative flag');
  655. assert.ok(types.has('hedge-stack'), 'expected hedge-stack flag');
  656. assert.ok(types.has('tier3-phrase-cluster'), 'expected tier3-phrase-cluster flag');
  657. assert.ok(r.score >= 25, `expected score ≥25 (Some/Moderate), got ${r.score}`);
  658. });
  659. test('"Interesting part of the project:" header opener flags emotional-flatline', () => {
  660. // The canonical AI list-intro pattern matched "the most interesting
  661. // part" but missed the bare "Interesting part of X:" section-header
  662. // form. v3.4 covers both shapes.
  663. const text = '\nInteresting part of the project:\nSome content follows that talks about the real on-chain tokenomics of the system at length.';
  664. const r = AIDetector.analyzeText(text);
  665. const types = new Set(r.issues.map((i) => i.type));
  666. assert.ok(types.has('emotional-flatline'), 'expected emotional-flatline flag');
  667. });
  668. test('"the line I keep coming back to" flags lingering-attention', () => {
  669. const text = 'Recorded with a guest yesterday. The line I keep coming back to is that agents behave like teenagers on an unbounded goal.';
  670. const r = AIDetector.analyzeText(text);
  671. const types = new Set(r.issues.map((i) => i.type));
  672. assert.ok(types.has('lingering-attention'), 'expected lingering-attention flag');
  673. });
  674. test('"I cannot stop thinking about" flags lingering-attention', () => {
  675. const text = "I can't stop thinking about the runtime guardrail argument he made near the end of our conversation about agent drift.";
  676. const r = AIDetector.analyzeText(text);
  677. const types = new Set(r.issues.map((i) => i.type));
  678. assert.ok(types.has('lingering-attention'), 'expected lingering-attention flag');
  679. });
  680. test('bare "I keep coming back to X because ..." does NOT flag lingering-attention', () => {
  681. // Precision carve-out: the bare verb phrase with a reason attached is
  682. // legitimate analytical writing, so only the noun-anchored frame fires.
  683. const text = 'I keep coming back to the exit-voice framing because it predicts which engineers quit and which ones file the RFC instead.';
  684. const r = AIDetector.analyzeText(text);
  685. const types = new Set(r.issues.map((i) => i.type));
  686. assert.ok(!types.has('lingering-attention'), 'bare reasoned form must not flag');
  687. });
  688. test('"real on-chain tokenomics" flags real-actual-inflation', () => {
  689. const text = 'The team is researching real on-chain tokenomics and actual reward sustainability versus electricity cost across the network deployment phase.';
  690. const r = AIDetector.analyzeText(text);
  691. const types = new Set(r.issues.map((i) => i.type));
  692. assert.ok(types.has('real-actual-inflation'), 'expected real-actual-inflation flag');
  693. });
  694. test('hashtag-stuff does not fire on prose with 2-3 hashtags', () => {
  695. const text = 'Shipped the new build last night. Catching bugs faster with the new instrumentation. Notes are in the doc, and the next push lands tomorrow. #buildinpublic #devlog';
  696. const r = AIDetector.analyzeText(text);
  697. const types = new Set(r.issues.map((i) => i.type));
  698. assert.ok(!types.has('hashtag-stuff'), 'should not flag 2 hashtags as hashtag-stuff');
  699. });
  700. test('"load-bearing" flags only allowlisted attributive abstractions (#56)', () => {
  701. // Every fixture is padded past the wordCount < 10 gate in analyzeText, which
  702. // returns zero issues before any pattern runs. A shorter fixture asserts
  703. // nothing: it would pass with the carve-out deleted entirely.
  704. const lbHits = (text) => {
  705. const r = AIDetector.analyzeText(text);
  706. assert.ok(!r.tooShort, `fixture must clear the length gate (wordCount >= 10): ${text}`);
  707. return { hits: r.issues.filter((i) => /load[- ]bearing/i.test(i.text)), types: new Set(r.issues.map((i) => i.type)) };
  708. };
  709. const metaphors = [
  710. 'The load-bearing assumption here is that users will migrate to the platform voluntarily.',
  711. 'That load-bearing claim never gets defended anywhere in the entire twelve page document.',
  712. 'The whole load-bearing invariant rests on a cache that nobody has actually measured.',
  713. ];
  714. for (const text of metaphors) {
  715. const { hits, types } = lbHits(text);
  716. assert.ok(types.has('tier1'), `expected tier1 flag for metaphor: ${text}`);
  717. assert.deepEqual(hits.map((h) => [h.type, h.text]), [['tier1', 'load-bearing']],
  718. `expected only the modifier in the matched span: ${text}`);
  719. }
  720. // Preserve existing construction controls while removing the noun denylist.
  721. for (const noun of ['assumption', 'claim', 'invariant', 'premise', 'constraint',
  722. 'dependency', 'argument', 'abstraction', 'assumptions', 'claims', 'invariants',
  723. 'premises', 'constraints', 'dependencies', 'arguments', 'abstractions']) {
  724. const text = `The load-bearing ${noun} remained central to the proposal throughout the entire review.`;
  725. assert.deepEqual(lbHits(text).hits.map((h) => [h.type, h.text]), [['tier1', 'load-bearing']], noun);
  726. }
  727. assert.deepEqual(lbHits('The LOAD-BEARING claim remained central to the proposal throughout the entire review.')
  728. .hits.map((h) => [h.type, h.text]), [['tier1', 'LOAD-BEARING']]);
  729. const literals = [
  730. 'The wall in the kitchen is load-bearing and must stay in place.',
  731. 'That claim is load-bearing for the argument presented in the final chapter.',
  732. 'The crew inspected the load-bearing structure before work resumed on the bridge.',
  733. 'The load-bearing structure of his argument collapses once you check the second citation.',
  734. 'The load-bearing element supports the roof above the entire second floor.',
  735. 'The load-bearing frame was installed after the concrete had cured completely.',
  736. 'The load-bearing foundation was reinforced before the next floor was added.',
  737. 'The load-bearing test measured the force required to deform the panel.',
  738. 'The load-bearing detail on the drawing specifies the new steel bracket.',
  739. 'The load-bearing corbel was inspected before the crew removed the scaffolding.',
  740. 'The load-bearing insight was repeated throughout the proposal without any supporting evidence.',
  741. 'The load-bearing hidden assumption remained central to the proposal throughout the entire review.',
  742. 'The load-bearing claimants waited outside the courtroom while their lawyer reviewed the evidence.',
  743. 'The panel is load-bearing. Claims about its capacity require an engineering report.',
  744. 'The panel is load-bearing\nClaims about its capacity require an engineering report.',
  745. 'Install a load-bearing wall between the kitchen and the garage today.',
  746. 'The steel load-bearing beam spans twelve feet across the finished basement ceiling.',
  747. 'The concrete load-bearing column in the parking garage was inspected last week.',
  748. 'Every load-bearing joist under the second floor was replaced during the remodel.',
  749. 'The roof load-bearing truss was engineered to handle heavy snow load safely.',
  750. 'These load-bearing trusses were installed by the framing crew earlier this spring.',
  751. 'Each load-bearing member of the frame must meet the local building code.',
  752. 'The load-bearing footing was poured before the inspector arrived on site Monday.',
  753. 'The load-bearing slab under the garage cracked during the cold winter months.',
  754. 'The load-bearing stud spacing must comply with local residential building code requirements.',
  755. 'The old load-bearing partition was replaced with a steel beam last summer.',
  756. 'The load-bearing masonry needs repair before the inspector will sign off here.',
  757. 'The load-bearing lintel above the window was cracked and needed full replacement.',
  758. 'Each load-bearing pier under the deck was set below the frost line.',
  759. 'The load-bearing rafter was replaced after the storm damaged the roof badly.',
  760. 'The load-bearing girder running under the floor was inspected and approved today.',
  761. 'The engineer calculated the load-bearing capacity of the floor before approving the plan.',
  762. // Optional adjective between `load-bearing` and the structural noun.
  763. 'The crew removed a load-bearing structural wall during the kitchen renovation project.',
  764. 'They reinforced the load-bearing exterior wall before pouring the new concrete footing.',
  765. // Unhyphenated "load bearing" is ordinary English: `bearing` as a
  766. // participle, not a compound modifier. The tell is always hyphenated.
  767. 'The heavy load bearing down on the old bridge finally cracked the concrete support.',
  768. 'Engineers reduced the load bearing on the rear axle by shifting the cargo forward.',
  769. 'You could feel the load bearing down on the whole team that entire quarter.',
  770. ];
  771. for (const text of literals) {
  772. const { hits } = lbHits(text);
  773. assert.equal(hits.length, 0, `out-of-scope use should not fire tier1: ${text}`);
  774. }
  775. });
  776. test('#107: deterministic unnecessary hyphenation subclasses fire with fixes', () => {
  777. const cases = [
  778. ['The team built a research-impact aggregator for the annual reporting workflow.', 'research-impact aggregator', 'research impact aggregator'],
  779. ['The report summarizes two research-impact aggregations from the external evaluation teams.', 'research-impact aggregations', 'research impact aggregations'],
  780. ['We agreed on a data-source strategy before rebuilding the ingestion pipeline.', 'data-source strategy', 'data source strategy'],
  781. ['The guide compares Python-package usage across the supported deployment environments.', 'Python-package usage', 'Python package usage'],
  782. ['The guide also compares Rust-crate usage across the supported deployment environments.', 'Rust-crate usage', 'Rust crate usage'],
  783. ['The repository keeps a single-Project Manifest for every supported deployment environment.', 'single-Project Manifest', 'single Project Manifest'],
  784. ['The audit records a total-downloads figure for every published package each month.', 'total-downloads figure', 'total downloads figure'],
  785. ['The dashboard reports a life-sciences-native citation count beside each indexed article.', 'life-sciences-native citation count', 'citation count from a life sciences source'],
  786. ['The old code-base still powers the internal dashboard used by the support team.', 'code-base', 'codebase'],
  787. ['- Migration note\n The old code-base remains available while customers finish moving their applications.', 'code-base', 'codebase'],
  788. ['The old data-set still feeds the internal dashboard used by the support team.', 'data-set', 'dataset'],
  789. ['The published time-frame leaves enough room for another review before launch.', 'time-frame', 'timeframe'],
  790. ['The product road-map lists every migration milestone planned for the next quarter.', 'road-map', 'roadmap'],
  791. ['The service refreshes the dashboard in real-time, even during the nightly import.', 'in real-time', 'in real time'],
  792. ['The service refreshes the dashboard in real-time every day during the nightly import.', 'in real-time', 'in real time'],
  793. ['The service refreshes the dashboard in real-time continuously during the nightly import.', 'in real-time', 'in real time'],
  794. ['The service refreshes the dashboard in real-time as new records arrive for processing.', 'in real-time', 'in real time'],
  795. ['The service refreshes the dashboard in real-time via the existing event stream.', 'in real-time', 'in real time'],
  796. ['The current release works out-of-the-box on every operating system we support.', 'works out-of-the-box', 'works out of the box'],
  797. ['That shortcut creates maintenance problems over the long-term, despite its early convenience.', 'over the long-term', 'over the long term'],
  798. ['That shortcut creates maintenance problems over the long-term across every department.', 'over the long-term', 'over the long term'],
  799. ['That shortcut creates maintenance problems over the long-term through deferred upgrades.', 'over the long-term', 'over the long term'],
  800. ['The team plans to keep this compatibility layer for the long-term by design.', 'for the long-term', 'for the long term'],
  801. ];
  802. for (const [text, matched, suggestion] of cases) {
  803. const result = AIDetector.analyzeText(text);
  804. const hits = result.issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  805. assert.equal(hits.length, 1, `expected one unnecessary-hyphenation hit: ${text}`);
  806. assert.equal(hits[0].text, matched, `unexpected matched text: ${text}`);
  807. assert.equal(hits[0].suggestion, suggestion, `unexpected suggestion: ${text}`);
  808. assert.equal(hits[0].severity, 'medium', `hyphenation cleanup should be P2: ${text}`);
  809. }
  810. });
  811. test('#107: standard compound modifiers and open forms do not fire', () => {
  812. const clean = [
  813. 'The team ships high-quality reports through a well-tested release process every week.',
  814. 'The highly-skilled editor reviewed the draft before the scheduled publication date.',
  815. 'A family-owned consultancy maintains the third-party integration for our regional offices.',
  816. 'The real-time dashboard shows a field-normalized score from the open-access dataset.',
  817. 'The service publishes changes in real-time monthly reports for each customer account.',
  818. 'The service publishes changes in real-time supply chain analytics for each customer.',
  819. 'The long-term plan includes out-of-the-box support for server-side rendering.',
  820. 'The system improved over the long-term planning horizon measured by the research team.',
  821. 'The report compares results in real-time and historical dashboards for each customer.',
  822. 'The report compares performance over the long-term and short-term planning horizons.',
  823. 'The codebase stores each dataset and roadmap in the same project workspace.',
  824. 'We agreed on a data source strategy before rebuilding the ingestion pipeline.',
  825. ];
  826. for (const text of clean) {
  827. const result = AIDetector.analyzeText(text);
  828. const hits = result.issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  829. assert.equal(hits.length, 0, `legitimate compound should stay clean: ${text}`);
  830. }
  831. });
  832. test('#107: protected spans are excluded from unnecessary-hyphenation detection', () => {
  833. const text = [
  834. 'The docs mention `data-source strategy` only as a literal compatibility key.',
  835. 'The file lives at docs/code-base/notes.md beside the archived code-base.md document.',
  836. 'The Windows copy lives at C:\\docs\\code-base\\notes.md for legacy users.',
  837. 'The Windows fixture directory is C:\\fixtures\\code-base for legacy users.',
  838. 'Run the command with --code-base before starting the local service.',
  839. 'Run the command with -code-base before starting the local service.',
  840. 'The migration guide says "the old code-base remains available" for legacy users.',
  841. "The migration guide calls this 'the old code-base' for legacy users.",
  842. "The migration guide calls this 'the user's old code-base remains available' for legacy users.",
  843. 'The migration guide calls this ‘the old code-base’ for legacy users.',
  844. `The migration guide says "${'word '.repeat(120)}the old code-base remains available" for legacy users.`,
  845. 'See https://example.com/guides/code-base for the archived implementation notes.',
  846. 'The archived exports are named code-base.csv and code-base.go for legacy users.',
  847. '> The old code-base remains available to teams migrating legacy applications.',
  848. '```text',
  849. 'Python-package usage and works out-of-the-box are fixture strings here.',
  850. '```',
  851. '',
  852. ' The old code-base remains in this top-level indented code block.',
  853. ].join('\n');
  854. const result = AIDetector.analyzeText(text);
  855. const hits = result.issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  856. assert.equal(hits.length, 0, `protected text should not fire: ${JSON.stringify(hits)}`);
  857. });
  858. test('#107: proper nouns, identifiers, and version strings stay protected', () => {
  859. const protectedForms = [
  860. 'Code-Base Enterprise publishes its quarterly release notes for customers today.',
  861. 'Road-Map Analytics shared its annual report with the engineering group today.',
  862. 'Research-Impact Aggregator Enterprise shared its annual report with customers today.',
  863. 'Data-Source Strategy Analytics shared its annual report with customers today.',
  864. 'The CODE-BASE heading is a product label used by the documentation team.',
  865. 'Install the code-base package before running the local development server today.',
  866. 'Use the .code-base selector when styling the legacy navigation component today.',
  867. 'The code-base config key remains supported for older deployment manifests today.',
  868. 'The current package release is code-base@2.4.1 for supported production systems.',
  869. 'The migration still targets code-base-v2 across all supported production systems.',
  870. 'The deployment identifier code-base remains supported across production systems today.',
  871. 'The filename is code-base, which the migration script still recognizes for compatibility.',
  872. 'The folder named road-map remains available to older deployment scripts today.',
  873. 'The release notes quote "the old\ncode-base remains available" for compatibility.',
  874. "The release notes quote 'the old\ncode-base remains available' for compatibility.",
  875. 'See https://example.com/guides/(code-base) for the archived implementation notes today.',
  876. ];
  877. for (const text of protectedForms) {
  878. const hits = AIDetector.analyzeText(text).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  879. assert.equal(hits.length, 0, `proper noun or identifier should stay protected: ${text}`);
  880. }
  881. const prose = AIDetector.analyzeText(
  882. 'The old code-base still powers the internal dashboard used by the support team.'
  883. );
  884. assert.equal(
  885. prose.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length,
  886. 1,
  887. 'ordinary lowercase prose must still fire'
  888. );
  889. const keyAsAdjective = AIDetector.analyzeText(
  890. 'The key code-base migration remains unfinished while the support team reviews it.'
  891. );
  892. assert.equal(
  893. keyAsAdjective.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length,
  894. 1,
  895. 'an adjectival "key" must not be mistaken for an identifier cue'
  896. );
  897. });
  898. test('#107: punctuation-adjacent flags and single-component paths stay protected', () => {
  899. const protectedForms = [
  900. 'Use (--code-base) when starting the local compatibility service for older clients.',
  901. 'Pass ,--code-base when starting the local compatibility service for older clients.',
  902. 'Use [--code-base] when documenting the local compatibility service for older clients.',
  903. 'Set mode=--code-base when starting the local compatibility service for older clients.',
  904. 'The Windows file lives at C:\\code-base for users of the legacy client.',
  905. 'The portable file lives at C:/code-base for users of the legacy client.',
  906. 'The home-directory file lives at ~/code-base for users of the legacy client.',
  907. 'The root-level file lives at /code-base for users of the legacy client.',
  908. 'The generated output is copied into code-base/ during every local release build.',
  909. 'The relative file lives at ./code-base for users of the legacy client.',
  910. 'The parent-relative file lives at ../code-base for users of the legacy client.',
  911. `The generated file lives at /${'a'.repeat(65)}-code-base for users of the legacy client.`,
  912. `The generated Windows file lives at C:\\${'a'.repeat(65)}-code-base for legacy users.`,
  913. ];
  914. for (const text of protectedForms) {
  915. const hits = AIDetector.analyzeText(text).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  916. assert.equal(hits.length, 0, `flag or path should stay protected: ${text}`);
  917. }
  918. const prose = AIDetector.analyzeText(
  919. 'Outside those literal paths, the old code-base remains ordinary prose and needs editing.'
  920. );
  921. assert.equal(
  922. prose.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length,
  923. 1,
  924. 'nearby prose must still fire'
  925. );
  926. });
  927. test('#107: frontmatter, YAML, Markdown tables, and HTML attributes stay protected', () => {
  928. const text = [
  929. '---',
  930. 'title: Code-base migration notes',
  931. 'tags:',
  932. ' - code-base',
  933. '---',
  934. '',
  935. 'release-name: code-base',
  936. 'legacy-tags:',
  937. ' - road-map',
  938. '',
  939. '| Setting | Legacy value |',
  940. '| --- | --- |',
  941. '| package | code-base |',
  942. '',
  943. '<div data-package=code-base class=road-map>Rendered content remains ordinary prose here.</div>',
  944. ].join('\n');
  945. const hits = AIDetector.analyzeText(text).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  946. assert.equal(hits.length, 0, `metadata should stay protected: ${JSON.stringify(hits)}`);
  947. const prose = AIDetector.analyzeText(
  948. 'After the metadata, the old code-base remains ordinary prose and needs editing today.'
  949. );
  950. assert.equal(
  951. prose.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length,
  952. 1,
  953. 'ordinary prose outside metadata must still fire'
  954. );
  955. const labelledProse = AIDetector.analyzeText(
  956. 'Note: the old code-base remains ordinary prose and still needs editing before publication.'
  957. );
  958. assert.equal(
  959. labelledProse.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length,
  960. 1,
  961. 'a capitalized prose label must not be mistaken for unfenced YAML'
  962. );
  963. });
  964. test('#107: adversarial filename masking remains within a linear-time budget', () => {
  965. const prefix = 'The generated identifier below has no filename extension and must remain safe to scan.';
  966. const attacks = [
  967. ['hyphen run', (n) => `${prefix} ${'a-'.repeat(n)}a`, 750],
  968. ['path segments', (n) => `${prefix} ${'segment/'.repeat(n)}`, 1000],
  969. ];
  970. for (const [name, build, base] of attacks) {
  971. const { small, large } = timeScaling(build, base);
  972. assert.ok(
  973. large < small * 8,
  974. `${name}: 4x input took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`,
  975. );
  976. }
  977. });
  978. test('#107: long closed quotations are masked without a length cutoff', () => {
  979. const text = `The release notes quote "${'word '.repeat(3600)}the old code-base remains available" for compatibility.`;
  980. const hits = AIDetector.analyzeText(text).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  981. assert.equal(hits.length, 0, 'quoted material must stay protected below the analyzer word limit');
  982. });
  983. test('#107: copyedit-only findings do not affect score or trinary classification', () => {
  984. const clean = 'The team reviewed the release notes before publishing them to customers. Everyone checked the examples, links, headings, and migration steps before the final approval meeting.';
  985. const copyedits = 'The code-base and data-set updates follow the time-frame in the road-map. The service runs in real-time, works out-of-the-box, and remains supported over the long-term for every customer.';
  986. const baseline = AIDetector.analyzeText(clean);
  987. const result = AIDetector.analyzeText(copyedits);
  988. assert.ok(
  989. result.issues.filter((issue) => issue.type === 'unnecessary-hyphenation').length >= 7,
  990. 'fixture must contain enough distinct copyedits to exercise the short-document threshold'
  991. );
  992. assert.equal(result.score, baseline.score, 'copyedit-only issues must not change the AI score');
  993. assert.equal(result.label, baseline.label, 'copyedit-only issues must not change the AI label');
  994. assert.equal(result.document_classification, baseline.document_classification);
  995. assert.deepEqual(result.class_probabilities, baseline.class_probabilities);
  996. assert.deepEqual(
  997. result.highlight_sentence_for_ai,
  998. [],
  999. 'copyedit-only issues must not create AI-highlight regions'
  1000. );
  1001. });
  1002. test('"verbatim" is Tier 3: single use stays clean, overuse flags by density', () => {
  1003. // Tier 3 words fire only at density (max(3, 3% of words)), so a lone
  1004. // "verbatim" — including the legal/QA term-of-art use — never flags, and the
  1005. // word only surfaces when the writer leans on it.
  1006. const single = AIDetector.analyzeText(
  1007. 'The packaging step copies the in-app resource verbatim into the extension bundle today.'
  1008. );
  1009. assert.ok(!single.tooShort, 'single-use fixture must clear the length gate');
  1010. assert.equal(
  1011. single.issues.filter((i) => i.type === 'tier3' && /verbatim/i.test(i.text)).length,
  1012. 0,
  1013. 'one "verbatim" is below the density floor and should not flag'
  1014. );
  1015. // The term of art repeated once in a normal sentence also stays clean.
  1016. const termOfArt = AIDetector.analyzeText(
  1017. 'The verbatim transcript was entered into evidence during the second day of the hearing.'
  1018. );
  1019. assert.equal(
  1020. termOfArt.issues.filter((i) => i.type === 'tier3' && /verbatim/i.test(i.text)).length,
  1021. 0,
  1022. 'a single term-of-art use should not flag'
  1023. );
  1024. // Repeated uses in a short piece clear max(3, floor(wordCount * 0.03)).
  1025. const overused = AIDetector.analyzeText(
  1026. 'He copied the file verbatim, read the note verbatim, typed the line verbatim, and repeated it verbatim to the room.'
  1027. );
  1028. const hits = overused.issues.filter((i) => i.type === 'tier3' && /verbatim/i.test(i.text));
  1029. assert.ok(hits.length > 0, 'repeated "verbatim" uses in a short piece should flag tier3 density');
  1030. });
  1031. test('"quietly" clusters with another Tier 2 word flags tier2', () => {
  1032. // "quietly" alone in a paragraph should not fire; paired with another
  1033. // Tier 2 word in the same paragraph it should produce a tier2 issue.
  1034. const single = AIDetector.analyzeText('The team quietly shipped the update last week without any announcement.');
  1035. const singleTypes = new Set(single.issues.map((i) => i.type));
  1036. assert.ok(!singleTypes.has('tier2'), 'single "quietly" should not fire tier2 on its own');
  1037. const clustered = AIDetector.analyzeText('The team quietly worked to harness new opportunities, building the platform without any announcement.');
  1038. const clusteredTypes = new Set(clustered.issues.map((i) => i.type));
  1039. assert.ok(clusteredTypes.has('tier2'), 'expected tier2 flag when "quietly" clusters with another Tier 2 word');
  1040. });
  1041. test('"deeply" joins a tier2 cluster only in significance collocations', () => {
  1042. // "deeply" is conditional Tier 2: bare uses never count toward a cluster,
  1043. // even next to another Tier 2 word — the base rate of literal "deeply"
  1044. // in human prose is too high for an unconditional entry.
  1045. const literal = [
  1046. 'The parser handles deeply nested JSON, and getting the indentation right is crucial for readability.',
  1047. 'I care deeply about this work, and the reality is more nuanced than the headline suggests.',
  1048. 'This helper is deeply coupled to the session object; could we facilitate testing by injecting it?',
  1049. ];
  1050. for (const text of literal) {
  1051. const r = AIDetector.analyzeText(text);
  1052. const types = new Set(r.issues.map((i) => i.type));
  1053. assert.ok(!types.has('tier2'), `literal "deeply" must not cluster: ${text}`);
  1054. }
  1055. // The significance collocation DOES count when a second Tier 2 word
  1056. // shares the paragraph...
  1057. const clustered = AIDetector.analyzeText('The team is deeply committed to helping partners harness the new platform across every region.');
  1058. const clusteredTypes = new Set(clustered.issues.map((i) => i.type));
  1059. assert.ok(clusteredTypes.has('tier2'), 'expected tier2 when "deeply committed" clusters with another Tier 2 word');
  1060. // ...but the collocation alone, with no second Tier 2 word, stays clean.
  1061. const lone = AIDetector.analyzeText('The billing module is deeply integrated with the ledger, so invoice edits show up in both places.');
  1062. const loneTypes = new Set(lone.issues.map((i) => i.type));
  1063. assert.ok(!loneTypes.has('tier2'), 'lone "deeply integrated" must not fire tier2 by itself');
  1064. });
  1065. test('bullet-np-list does not fire on prose containing short verb-form bullets', () => {
  1066. // Genuine list items with finite verbs should not trip the bare-NP
  1067. // detector. The verb-token guard is what keeps todo lists, changelog
  1068. // entries, and step-by-step instructions out of the false-positive
  1069. // bucket.
  1070. const text = `Today's changelog:
  1071. * fixed the auth bug that was hitting Safari users
  1072. * removed the legacy webhook handler that nobody calls anymore
  1073. * added a retry on the token refresh path
  1074. * shipped the new build to staging this morning
  1075. * will deploy to prod after the smoke tests pass
  1076. That's the full list for this push.`;
  1077. const r = AIDetector.analyzeText(text);
  1078. const types = new Set(r.issues.map((i) => i.type));
  1079. assert.ok(!types.has('bullet-np-list'), 'verb-form bullets should not trip bare-NP detector');
  1080. });
  1081. test('tier3-phrase fires on per-phrase repetition (>=2 hits)', () => {
  1082. // The same boilerplate phrase used twice in one piece. Isolates the
  1083. // per-phrase density rule from the cluster rule — cluster needs >=3
  1084. // distinct phrases, this one needs >=2 hits of one phrase.
  1085. const text = 'The integration of payments matters for adoption. The integration of identity is the next step. Both unlock material flows.';
  1086. const r = AIDetector.analyzeText(text);
  1087. const types = new Set(r.issues.map((i) => i.type));
  1088. assert.ok(types.has('tier3-phrase'), 'expected tier3-phrase flag for 2x repetition');
  1089. });
  1090. test('tier3-phrase-cluster fires on 3 distinct phrases at density 1 each', () => {
  1091. // The cluster-rule boundary: each phrase appears only once, but three
  1092. // distinct phrases stacked is the LLM-self-varies-boilerplate shape.
  1093. // Per-phrase rule should NOT fire here; cluster rule should.
  1094. const text = 'The team works on decentralized compute. Their thesis is community-driven and the long-term sustainability of the network matters most. Adoption is improving.';
  1095. const r = AIDetector.analyzeText(text);
  1096. const types = new Set(r.issues.map((i) => i.type));
  1097. assert.ok(types.has('tier3-phrase-cluster'), 'expected tier3-phrase-cluster flag at 3 distinct phrases');
  1098. assert.ok(!types.has('tier3-phrase'), 'per-phrase rule should NOT fire when each phrase appears once');
  1099. });
  1100. test('tier3-phrase span dedup: overlapping regex matches count as one phrase', () => {
  1101. // "designed for long-term sustainability" matches both
  1102. // "designed for long-term" AND "long-term sustainability" — the second
  1103. // is contained in the first. Span-dedup keeps this as one distinct hit.
  1104. const text = 'This protocol is designed for long-term sustainability and nothing else.';
  1105. const r = AIDetector.analyzeText(text);
  1106. const types = new Set(r.issues.map((i) => i.type));
  1107. assert.ok(!types.has('tier3-phrase-cluster'), 'overlapping matches should not stack toward cluster threshold');
  1108. });
  1109. test('emotional-flatline opener fires at position 0 (no leading newline)', () => {
  1110. // Earlier (^|\n) form silently missed bare openers at true start of
  1111. // input. /m flag fixes it.
  1112. const text = 'Interesting part of the project:\nThey shipped in two weeks.';
  1113. const r = AIDetector.analyzeText(text);
  1114. const types = new Set(r.issues.map((i) => i.type));
  1115. assert.ok(types.has('emotional-flatline'), 'expected emotional-flatline at position 0');
  1116. });
  1117. test('emotional-flatline stays visible without moving the authorship score', () => {
  1118. // #82: compare nearly identical prose so the assertion isolates the category
  1119. // weight instead of assuming every other scoring input remains at zero.
  1120. const text = 'What surprised me most was the rollback time: eleven seconds across all three production hosts after the database migration completed without retries.';
  1121. const control = 'The detail I remember best was the rollback time: eleven seconds across all three production hosts after the database migration completed without retries.';
  1122. const r = AIDetector.analyzeText(text);
  1123. const baseline = AIDetector.analyzeText(control);
  1124. const hits = r.issues.filter((i) => i.type === 'emotional-flatline');
  1125. assert.deepEqual(baseline.issues, [], `control sentence must stay clean: ${JSON.stringify(baseline.issues)}`);
  1126. assert.deepEqual(r.issues.map((i) => i.type), ['emotional-flatline'], `target sentence has confounding findings: ${JSON.stringify(r.issues)}`);
  1127. assert.equal(hits.length, 1, `expected one emotional-flatline hit, got ${JSON.stringify(hits)}`);
  1128. assert.equal(r.score, baseline.score, `style-only emotional-flatline changed score from ${baseline.score} to ${r.score}`);
  1129. assert.deepEqual(
  1130. r.highlight_sentence_for_ai,
  1131. [],
  1132. `style-only emotional-flatline must not create AI sentence highlights: ${JSON.stringify(r.highlight_sentence_for_ai)}`,
  1133. );
  1134. });
  1135. test('bullet-np-list ignores bullets inside fenced code blocks', () => {
  1136. // CLI flag docs / option dumps inside ``` fences are not prose AI
  1137. // scaffolding. False-positive that would fire on most READMEs.
  1138. const text = "Run with one of these modes via `--mode`:\n\n```\n- unit\n- smoke\n- integration\n- e2e\n- perf\n- stress\n```\n\nDefaults to `unit` if omitted.";
  1139. const r = AIDetector.analyzeText(text);
  1140. const types = new Set(r.issues.map((i) => i.type));
  1141. assert.ok(!types.has('bullet-np-list'), 'bullets inside code fences should not flag');
  1142. });
  1143. test('bullet-np-list flushes on 2+ blank lines between bullet sections', () => {
  1144. // A single blank line is normal Markdown spacing inside one list.
  1145. // Two or more blank lines separate visually-disjoint sections — those
  1146. // should not merge into one long run.
  1147. const text = '* alpha\n* beta\n\n\nA paragraph of prose.\n\n\n* gamma\n* delta\n* epsilon';
  1148. const r = AIDetector.analyzeText(text);
  1149. const types = new Set(r.issues.map((i) => i.type));
  1150. assert.ok(!types.has('bullet-np-list'), 'sections separated by 2+ blank lines should not merge');
  1151. });
  1152. test('hashtag-stuff matches tags after sentence punctuation', () => {
  1153. // Hashtags immediately following sentence punctuation — common in
  1154. // LinkedIn/X trailing blocks. The prior regex char class `[\s\\]`
  1155. // had a literal backslash and silently missed any tag not preceded
  1156. // by whitespace.
  1157. const text = "Built a thing this week.\n#startup #crypto #web3 #ai #devlog #shipping #foundermode";
  1158. const r = AIDetector.analyzeText(text);
  1159. const types = new Set(r.issues.map((i) => i.type));
  1160. assert.ok(types.has('hashtag-stuff'), 'expected hashtag-stuff on 7-tag trailing block');
  1161. });
  1162. test('hashtag-stuff excludes URL fragments from the count', () => {
  1163. // URL anchors like example.com/page#section must not count toward
  1164. // the hashtag threshold or every doc post with a fragment link
  1165. // would false-positive.
  1166. const text = 'See the spec at example.com/api#auth and the deploy guide at example.com/ops#rollback and the troubleshooting notes at example.com/help#errors and the changelog at example.com/log#latest. Also kb.example.com/faq#section1 and forum.example.com/t/123#post-4 round out the references.';
  1167. const r = AIDetector.analyzeText(text);
  1168. const types = new Set(r.issues.map((i) => i.type));
  1169. assert.ok(!types.has('hashtag-stuff'), 'URL fragments should not count as hashtags');
  1170. });
  1171. test('hashtag-stuff excludes issue and PR references from the count', () => {
  1172. // `#88` in prose is a GitHub issue reference, not a tag. A changelog
  1173. // paragraph routinely cites six of them, and every such paragraph scored
  1174. // as a stuffed hashtag block. Found when this repo's own README linked an
  1175. // issue and the detector flagged the README.
  1176. const text = 'The regression came in through #88 and stayed hidden until #91 landed. I reverted #92, reopened #93, and then #94 turned out to be the same bug in a different file. #95 is the follow up that fixes it, and #96 tracks the test we still owe.';
  1177. const types = new Set(AIDetector.analyzeText(text).issues.map((i) => i.type));
  1178. assert.ok(!types.has('hashtag-stuff'), 'issue references should not count as hashtags');
  1179. });
  1180. test('hashtag-stuff excludes hex colours and preprocessor directives', () => {
  1181. // `#fff` is a colour and `#include` is a directive. Both appear in
  1182. // technical prose well past six per post.
  1183. const css = 'Background is #fff in light mode and #eee in dark. Body text sits at #1a2b3c, muted text at #6b7280, the link colour is #2563eb, hover is #1d4ed8, and the one accent is #f59e0b on the button.';
  1184. // 8-character RGBA values, so dropping the {8} alternative is caught.
  1185. const rgba = 'Palette is #1a2b3cff for body and #6b7280ee muted and #2563ebdd links and #1d4ed8cc hover and #f59e0bbb accent and #0a1b2cdd border today.';
  1186. const c = 'Put #include <stdio.h> first, then #include <stdlib.h>, then #include <string.h>. Add #include <unistd.h> and #include <fcntl.h> after those, and guard the block with #ifndef and #endif so it stays idempotent.';
  1187. for (const [label, text] of [['hex colours', css], ['rgba colours', rgba], ['directives', c]]) {
  1188. const types = new Set(AIDetector.analyzeText(text).issues.map((i) => i.type));
  1189. assert.ok(!types.has('hashtag-stuff'), `${label} should not count as hashtags`);
  1190. }
  1191. });
  1192. test('hashtag-stuff ignores hashes inside code spans and fences', () => {
  1193. // A tag in backticks is the author documenting the syntax, not using it.
  1194. // Every span here is a tag isSocialTag would otherwise KEEP, so the count is
  1195. // 6 without masking and 0 with it. An earlier version of this fixture quoted
  1196. // #88 and #fff, which the carve-outs already removed, so it passed with
  1197. // inline masking deleted and tested nothing.
  1198. const inline = 'The escape rules trip people up. Write `#AI` for the tag, `#Innovation` for the category, `#Startups` for the vertical, `#Leadership` for the theme, `#Growth` for the metric, and `#FutureOfWork` when you mean the movement.';
  1199. const fenced = 'Here is the config we ship by default, and it has not changed in a year:\n\n```\n#alpha\n#beta\n#gamma\n#delta\n#epsilon\n#zeta\n```\n\nEverything below that line is user overridable and nothing above it is.';
  1200. for (const [label, text] of [['inline code', inline], ['fenced code', fenced]]) {
  1201. const types = new Set(AIDetector.analyzeText(text).issues.map((i) => i.type));
  1202. assert.ok(!types.has('hashtag-stuff'), `${label} should not count as hashtags`);
  1203. }
  1204. });
  1205. test('hashtag-stuff still counts short hex-shaped words, which are real tags', () => {
  1206. // #b2b, #e2e, #dad, #cafe, #ace and #face are ordinary tags. Carving out
  1207. // 3- and 4-digit hex to catch CSS palettes silently deleted true positives on
  1208. // the stuffed-block shape, so only 6- and 8-char forms CONTAINING A DIGIT are
  1209. // subtracted: #decade and #facade are a-f words, not colours.
  1210. const gtm = 'Great conversation on the go-to-market motion this week with the whole revenue team here.\n#b2b #e2e #saas #growth #ace #fade';
  1211. const family = 'Weekend was good and the whole family got outside for once this month together.\n#dad #cafe #beef #face #travel #weekend';
  1212. const decade = 'Reflecting on the last ten years of shipping developer tools to teams everywhere here.\n#decade #facade #deface #beaded #effaced #growth';
  1213. // Four spaces under a list marker is a paragraph continuation, not a code
  1214. // block, which is why indented runs are not masked.
  1215. const listed = '- We shipped the detector and the whole team is happy with how it landed today.\n\n #AI #Innovation #FutureOfWork #MachineLearning #Leadership #Growth';
  1216. for (const [label, text] of [['b2b/e2e block', gtm], ['dad/cafe block', family], ['a-f word tags', decade], ['tags under a list item', listed]]) {
  1217. const types = new Set(AIDetector.analyzeText(text).issues.map((i) => i.type));
  1218. assert.ok(types.has('hashtag-stuff'), `${label} should still flag as hashtag stuffing`);
  1219. }
  1220. });
  1221. test('hashtag-stuff still fires on a tag block that also cites an issue', () => {
  1222. // The carve-outs subtract non-tag forms; they must not let a real block
  1223. // through because a `#88` sits beside it.
  1224. const text = 'Closed out #88 today and the release is live for everyone.\n#AI #Innovation #FutureOfWork #MachineLearning #Leadership #Growth #Startups';
  1225. const r = AIDetector.analyzeText(text);
  1226. const types = new Set(r.issues.map((i) => i.type));
  1227. assert.ok(types.has('hashtag-stuff'), 'expected hashtag-stuff on a 7-tag block');
  1228. assert.equal(
  1229. r.issues.find((i) => i.type === 'hashtag-stuff').text,
  1230. '7 hashtags',
  1231. 'the issue reference must not be counted as a tag'
  1232. );
  1233. });
  1234. test('hashtag-stuff still fires on tags spread inline through a post', () => {
  1235. // Masking code and subtracting non-tag forms must not weaken the inline
  1236. // shape, which is the one a trailing-block-only rule would miss.
  1237. const text = 'Loving the #AI space right now, especially #MachineLearning and #DeepLearning, plus #Startups and #Innovation and #FutureOfWork keep me busy every single day of the week.';
  1238. const types = new Set(AIDetector.analyzeText(text).issues.map((i) => i.type));
  1239. assert.ok(types.has('hashtag-stuff'), 'expected hashtag-stuff on 6 inline tags');
  1240. });
  1241. const featuresHit = (text) =>
  1242. AIDetector.analyzeText(text).issues.find((i) => i.type === 'tier1-clarity' && /^features$/i.test(i.text));
  1243. test('tier1-clarity flags "features" used as a verb', () => {
  1244. // Copula avoidance: "X features Y" stands in for "X has Y".
  1245. for (const text of [
  1246. 'The new app features a clean dashboard and a fast search bar that most users liked.',
  1247. 'The album features guest vocals from three artists who toured with the band last year.',
  1248. 'The cake, which also features blueberries and a bit of chocolate, takes about an hour.',
  1249. ]) {
  1250. assert.ok(featuresHit(text), `expected tier1-clarity on: ${text}`);
  1251. }
  1252. });
  1253. test('features: residual precision checks from #384', () => {
  1254. // Indirect questions: "which/what features" is a noun after a question verb or at sentence start.
  1255. for (const text of [
  1256. 'Decide which features matter most to our users this quarter, before choosing a plan.',
  1257. 'Decide what features matter most to our users this quarter, before choosing a plan.',
  1258. 'Decide which features apply to your team before you choose a plan for the year.',
  1259. 'Check which features rely on the API before you migrate any of the older workspaces.',
  1260. 'Find out which features family members can share on the plan before you upgrade it.',
  1261. 'Check which features also work offline before we choose a plan for the whole team.',
  1262. 'Check which features regularly fail when customers upload large files from home.',
  1263. 'Decide which features the team should prioritize before the next release ships to users.',
  1264. 'Tell me what features a customer can disable from the settings page in the app.',
  1265. 'Tell me what features a customer ordered from the catalog last year.',
  1266. 'Tell me which features a dashboard and export tools can disable for our users.',
  1267. 'Identify which features two teams requested during the planning meeting last week.',
  1268. 'Which features matter most depends on the size of the team and the plan you choose.',
  1269. 'Pick a phone whose features fit your budget and the apps you need every single day.',
  1270. 'Tell me which features a customer, with administrator access, can disable.',
  1271. 'Tell me which features a customer, with administrator access, actually can disable.',
  1272. 'Tell me what features a dashboard, which users can customize, includes.',
  1273. 'Tell me which features a dashboard, which we customized yesterday, supports.',
  1274. ]) {
  1275. assert.equal(featuresHit(text), undefined, `question noun read as a verb: ${text}`);
  1276. }
  1277. for (const text of [
  1278. 'Check what features a dashboard and export tools, before you proceed to the next step.',
  1279. 'Tell me which features a dashboard, before we close the project and go home.',
  1280. ]) {
  1281. assert.ok(featuresHit(text), `question verb skipped: ${text}`);
  1282. }
  1283. // Any other lead keeps the pre-#384 behaviour: relative clauses stay verb findings...
  1284. for (const text of [
  1285. 'The new enterprise service, which features a dashboard, serves teams everywhere in the world.',
  1286. 'This is what features prominently in every review over the past year.',
  1287. 'This is the one dish which features heavily in every review we read over the past year.',
  1288. 'We tested a library which features dashboards and reports for every department in the company.',
  1289. 'We tested a library which features occasionally updated dashboards for every department.',
  1290. 'We listened to a track which features guest vocals from two local artists on the chorus.',
  1291. 'We listened to the bonus track which features guest vocals from two local artists on the chorus.',
  1292. 'Read the review which features detailed comparisons of every plan we tested this year.',
  1293. 'Read our detailed review which features comparisons of every plan we tested this year.',
  1294. 'We completed a security check which features detailed diagnostics for each device in the network.',
  1295. "We listened to the editor's pick which features guest vocals from two local artists on the chorus.",
  1296. 'The library (which features a dashboard and report builder) serves every department in the company.',
  1297. ]) {
  1298. assert.ok(featuresHit(text), `relative-clause verb missed: ${text}`);
  1299. }
  1300. // ...and existing noun evidence still applies.
  1301. for (const text of [
  1302. 'We discussed which features we should remove from the next release of the app.',
  1303. 'I asked the team which features they want most before we plan the next quarter.',
  1304. 'Before we choose a plan, which features can we use offline when we travel for work?',
  1305. 'Security features support for older protocols on devices without hardware acceleration.',
  1306. 'The scripting features support for loops but reject while loops in the embedded language.',
  1307. 'The experimental features support for loops but reject while loops in the embedded language.',
  1308. 'The accessibility features support for-profit organizations that assist people with disabilities.',
  1309. ]) {
  1310. assert.equal(featuresHit(text), undefined, `noun read as a verb: ${text}`);
  1311. }
  1312. // Deferred cases from #384 / #387: singular verb subjects for "support for".
  1313. // The plural-noun subject cases (like "The experimental features support for loops")
  1314. // continue to correctly read as nouns because of their determiner/adjective context.
  1315. for (const text of [
  1316. 'A library features support for asynchronous requests when the device goes offline.',
  1317. 'The library features support for asynchronous requests when the device goes offline.',
  1318. 'Our library features support for asynchronous requests across every supported device in our network.',
  1319. 'This library features support for asynchronous requests across every supported device in our network.',
  1320. 'That JavaScript library features support for rendering interactive charts in real time.',
  1321. 'The JavaScript library features support for rendering interactive charts in real time.',
  1322. 'This Windows 11 app features support for dark mode across the full platform.',
  1323. 'The Windows 11 app features support for dark mode across the full platform.',
  1324. 'In 2024 the library features support for asynchronous requests across every region.',
  1325. 'The 2024 release features support for dark mode across every region we serve.',
  1326. 'The 365 app features support for offline mode across every region we serve.',
  1327. 'Our 2024 release features support for offline caching across every region we serve.',
  1328. 'The 2024 model features support for dark mode across the whole platform.',
  1329. 'The 2025 update features support for offline caching across every region.',
  1330. 'The 365 model features support for offline mode across every region we serve.',
  1331. 'This 365 version features support for older protocols across every region.',
  1332. 'Many say it features support for dark mode across all supported devices.',
  1333. 'Some say it features support for older protocols across every region.',
  1334. 'We tested a library which features support for both protocols seamlessly.',
  1335. 'We tested a library that features support for both protocols on older devices.',
  1336. 'The platform features support for asynchronous requests across every supported region.',
  1337. 'The tool features support for custom plugins across every supported region.',
  1338. 'The app features support for offline caching across every supported region.',
  1339. 'It features support for loops across the compiler pipeline without issues.',
  1340. 'This app features support for loops across every target platform in our test suite.',
  1341. 'This library features support for teams that connect and remain online.',
  1342. ]) {
  1343. assert.ok(featuresHit(text), `verb skipped: ${text}`);
  1344. }
  1345. for (const text of [
  1346. 'Check which features support for-profit organizations that assist people with disabilities.',
  1347. 'These system features support for older protocols on devices without hardware acceleration.',
  1348. 'Decide which features support for-profit organizations that assist people with disabilities.',
  1349. 'The library features support for loops but reject while loops in the embedded language.',
  1350. 'The library features support for-profit organizations that assist people with disabilities.',
  1351. 'The library features support for loops on devices without hardware acceleration.',
  1352. 'The two platform features support for older protocols.',
  1353. 'The two platform features support for older protocols on devices without hardware acceleration.',
  1354. 'The 2 platform features support for older protocols on devices without hardware acceleration.',
  1355. 'Our two platform features support for older protocols on devices without hardware acceleration.',
  1356. 'Many system features support for older protocols on devices without acceleration.',
  1357. 'All of the system features support for older protocols on devices without hardware acceleration.',
  1358. 'Some of our platform features support for legacy clients across every region.',
  1359. 'Two of the platform features support for older protocols on devices without acceleration.',
  1360. 'The twelve platform features support for older protocols on devices without hardware acceleration.',
  1361. 'The 100 platform features support for older protocols on devices without hardware acceleration.',
  1362. 'The 150 library features support for older protocols on devices without hardware acceleration.',
  1363. 'The 100 new platform features support for older protocols on devices without hardware acceleration.',
  1364. 'Both system features support for older protocols on devices without hardware acceleration.',
  1365. 'The system features support for older protocols and remain enabled by default.',
  1366. 'We compare a suite of system features support for legacy applications.',
  1367. 'We confirmed that features support for-profit organizations that assist people with disabilities.',
  1368. 'We discussed which features support for-profit organizations that assist people with disabilities.',
  1369. ]) {
  1370. assert.equal(featuresHit(text), undefined, `noun read as a verb: ${text}`);
  1371. }
  1372. });
  1373. test('tier1-clarity leaves "features" alone as a plural noun', () => {
  1374. // #351: on a product site, every hit was the software noun.
  1375. for (const text of [
  1376. 'The release adds three features: offline sync, export and search for every plan.',
  1377. 'We shipped the features on Monday after two weeks of testing with the beta group.',
  1378. 'Key features include dark mode, export and a command palette for power users.',
  1379. 'Features include dark mode, export and a command palette for power users today.',
  1380. '## Features\n\n- Offline sync and export for every plan, including the free tier.',
  1381. "The product's features are limited, but the export works and sync has never failed.",
  1382. ]) {
  1383. assert.equal(featuresHit(text), undefined, `"features" read as a verb in: ${text}`);
  1384. }
  1385. });
  1386. test('features: noun findings never inherit verb highlight regions', () => {
  1387. const noun = 'We shipped the features on Monday.';
  1388. const verb = 'The app features a dashboard for our team to review.';
  1389. const source = noun + ' ' + verb;
  1390. const result = AIDetector.analyzeText(source);
  1391. assert.ok(featuresHit(source));
  1392. assert.equal(result.highlight_sentence_for_ai.length, 1);
  1393. assert.equal(result.highlight_sentence_for_ai[0].startSentence, 1);
  1394. assert.equal(result.highlight_sentence_for_ai[0].hitCount, 1);
  1395. assert.ok(result.highlight_sentence_for_ai[0].start >= noun.length);
  1396. const twice = AIDetector.analyzeText(verb + ' ' + verb);
  1397. assert.equal(twice.issues.filter((i) => i.text === 'features').length, 1);
  1398. assert.equal(twice.highlight_sentence_for_ai[0].hitCount, 2);
  1399. const markdown = AIDetector.analyzeText('<!-- hidden -->' + source, { sourceMode: 'rendered-markdown' });
  1400. assert.equal(markdown.highlight_sentence_for_ai[0].hitCount, 1);
  1401. assert.ok(markdown.highlight_sentence_for_ai[0].start >= '<!-- hidden -->'.length + noun.length);
  1402. });
  1403. test('features: product subjects, added objects, quantifiers and curly possessives remain nouns', () => {
  1404. for (const text of [
  1405. 'Product features help teams manage projects and keep every task moving on schedule.',
  1406. 'The release added features available offline, and customers use them every day.',
  1407. 'Only 3 features a customer requested shipped last week to our users.',
  1408. 'Notion’s features helped teams ship faster in 2026 after the update.',
  1409. 'These features, among other tools, are available for our staff every day.',
  1410. ]) assert.equal(featuresHit(text), undefined, `expected noun context: ${text}`);
  1411. const verb = 'The package features, among other tools, a parser that our team uses every day.';
  1412. assert.ok(featuresHit(verb), 'parenthetical before the verb object must retain the finding');
  1413. });
  1414. test('features: plural subjects followed by base-form verbs remain nouns', () => {
  1415. const complements = {
  1416. help: 'teams manage projects and keep every task moving on schedule',
  1417. let: 'teams edit the same document while reviewers leave comments',
  1418. allow: 'teams to edit the same document while reviewers leave comments',
  1419. make: 'backups easier for staff who need to recover their files',
  1420. keep: 'your data safe across every device that we support today',
  1421. give: 'managers a clear view of every project across the team',
  1422. provide: 'context for staff who need to review the latest requests',
  1423. enable: 'staff to review the latest requests from their own devices',
  1424. offer: 'a clear view of every project across the entire team',
  1425. save: 'time for staff who review the latest requests every day',
  1426. protect: 'files that staff share across their devices during the week',
  1427. ensure: 'that staff can review requests from all their devices today',
  1428. improve: 'the way staff review requests from all their devices today',
  1429. reduce: 'the time staff spend reviewing requests from their own devices',
  1430. support: 'staff who review requests from all their devices every day',
  1431. remain: 'available for staff who review requests across all their devices',
  1432. vary: 'by plan for staff who review requests across different teams',
  1433. need: 'no setup for staff who review requests across their devices',
  1434. unlock: 'access for staff who review requests from all their devices',
  1435. bring: 'new options to staff who review requests across their devices',
  1436. };
  1437. for (const [verb, complement] of Object.entries(complements)) {
  1438. const noun = `Our security features ${verb} ${complement}.`;
  1439. assert.ok(!AIDetector.analyzeText(noun).tooShort);
  1440. assert.equal(featuresHit(noun), undefined, `plural subject: ${noun}`);
  1441. const verbal = `The app features a ${verb} button for staff who review requests every day.`;
  1442. assert.ok(featuresHit(verbal), `article-led verb object: ${verbal}`);
  1443. }
  1444. for (const noun of [
  1445. 'The collaboration features let teams edit the same document in real time.',
  1446. 'Advanced reporting features give managers a clear view of every project.',
  1447. ]) assert.equal(featuresHit(noun), undefined, `modifier noun: ${noun}`);
  1448. assert.ok(featuresHit('The app features keep-alive connections for staff who review requests every day.'),
  1449. 'a hyphenated object modifier is not the base-form verb keep');
  1450. });
  1451. test('features: relative subjects, object compounds and product versions retain verb findings', () => {
  1452. for (const text of [
  1453. 'The new app that features a clean dashboard and a fast search bar was liked.',
  1454. 'Each features a clean dashboard and a fast search bar for staff.',
  1455. 'This features a clean dashboard and a fast search bar for staff.',
  1456. 'That features a clean dashboard and a fast search bar for staff.',
  1457. 'The phone features on-device AI processing for every request from our test team.',
  1458. 'The guide features in-depth examples of queue failures and retry strategies for operators.',
  1459. 'The app features to-do lists and calendar reminders for staff working from home.',
  1460. 'GPT-5 features a longer context window for our document review workflow.',
  1461. 'Windows 11 features a new Start menu for employees working in our office.',
  1462. 'The X-200 features a six-inch screen and a camera for remote inspections.',
  1463. 'The ship features a clean dining room and cabins for twelve passengers.',
  1464. 'The new build features a clean dashboard and search tools for staff.',
  1465. ]) {
  1466. assert.ok(!AIDetector.analyzeText(text).tooShort, `fixture must clear the length gate: ${text}`);
  1467. assert.ok(featuresHit(text), `expected verb finding: ${text}`);
  1468. }
  1469. });
  1470. test('features: bare objects and product/count modifiers remain ordinary nouns', () => {
  1471. for (const text of [
  1472. 'We ship features faster than competitors do every quarter in production.',
  1473. 'Teams build features users actually want to keep after onboarding.',
  1474. 'We can build features faster than competitors do every quarter in production.',
  1475. 'We ship GPT-5 features a customer requested for their document review workflow.',
  1476. 'These 3 features a customer requested were released after our final review.',
  1477. 'The platform-specific features helped our operators finish their daily work before lunch.',
  1478. 'We shipped three features that users requested during last month\'s beta test.',
  1479. ]) {
  1480. assert.ok(!AIDetector.analyzeText(text).tooShort, `fixture must clear the length gate: ${text}`);
  1481. assert.equal(featuresHit(text), undefined, `unexpected noun finding: ${text}`);
  1482. }
  1483. assert.ok(featuresHit('We ship features faster every quarter. The new app features a clean dashboard for staff.'),
  1484. 'a skipped noun must not consume the later verb finding');
  1485. });
  1486. test('low-ttr fires on a 200+ token text with narrow vocabulary', () => {
  1487. // Vocabulary-poor synthetic sample: same 11-word sentence repeated.
  1488. // ~200 tokens, ~11 unique = ~5% TTR. Well under the 40% threshold.
  1489. // Stylometric signal from the May 2026 detection-research review.
  1490. const sentence = 'The system shows the system improves the system every iteration. ';
  1491. const text = sentence.repeat(20);
  1492. const r = AIDetector.analyzeText(text);
  1493. const types = new Set(r.issues.map((i) => i.type));
  1494. assert.ok(types.has('low-ttr'), `expected low-ttr flag, got types: ${[...types].join(', ')}`);
  1495. });
  1496. test('low-ttr does not fire on natural human prose at 200+ tokens', () => {
  1497. // 200+ tokens of varied human-style prose. TTR should comfortably
  1498. // exceed the 0.40 threshold even with some natural repetition. The
  1499. // length assertion keeps the fixture above the rule's 200-token gate,
  1500. // below which the check never runs.
  1501. const text = `When the build broke this morning, I rolled back the recent auth refactor and
  1502. ran the integration tests again. Most of them passed cleanly, but a handful
  1503. of edge cases around token refresh still tripped the staging environment.
  1504. Safari users hit a 401 on the second request of any session that crossed
  1505. the hour mark, while Firefox sessions stayed authenticated as expected.
  1506. Digging through the logs, the culprit looked like a cookie scope issue
  1507. introduced during the migration to the new domain. I patched the path
  1508. parameter, redeployed to staging, and watched the metrics dashboard for
  1509. twenty minutes before pushing to production. Memory usage stayed flat,
  1510. latency held steady around forty milliseconds, and the error rate dropped
  1511. back below baseline once the rollout completed. Closing the incident
  1512. ticket now and writing up notes for the team retrospective tomorrow.
  1513. Two questions stay open for the retro. The staging suite never waited past
  1514. the hour, so nothing there could have caught this, and nobody seems sure who
  1515. owns the domain migration checklist since the platform lead moved teams. The
  1516. mobile webview may carry the same cookie too: their crash reports spiked on
  1517. Tuesday and nobody has looked at why. I will ask them before standup.`;
  1518. assert.ok((text.match(/[\w'-]+/g) || []).length >= 200, 'fixture must reach the 200-token gate');
  1519. const r = AIDetector.analyzeText(text);
  1520. const types = new Set(r.issues.map((i) => i.type));
  1521. assert.ok(!types.has('low-ttr'), `low-ttr should not fire on natural prose, got types: ${[...types].join(', ')}`);
  1522. });
  1523. test('low-ttr does not fire on a long text whose every window is varied', () => {
  1524. // Whole-text TTR falls as a text grows, whoever wrote it: each
  1525. // 6,000-word public-domain slice in corpus/ sits under 0.40. The rule
  1526. // averages TTR over 200-token windows instead. Here 300 distinct words
  1527. // cycle through 3,000 tokens: plain TTR is 0.10, every window 1.0.
  1528. const words = Array.from({ length: 3000 }, (_, i) => `term${i % 300}`);
  1529. const types = new Set(AIDetector.analyzeText(`${words.join(' ')}.`).issues.map((i) => i.type));
  1530. assert.ok(!types.has('low-ttr'), `low-ttr should not fire on locally varied text, got types: ${[...types].join(', ')}`);
  1531. });
  1532. test('low-ttr still fires on a long text with a narrow vocabulary', () => {
  1533. const sentence = 'The system shows the system improves the system every iteration. ';
  1534. const types = new Set(AIDetector.analyzeText(sentence.repeat(200)).issues.map((i) => i.type));
  1535. assert.ok(types.has('low-ttr'), `expected low-ttr on 2,000 narrow tokens, got types: ${[...types].join(', ')}`);
  1536. });
  1537. test('low-ttr reports plain TTR for a text of exactly 200 tokens', () => {
  1538. // One window covers the whole text, so the averaged value is the plain
  1539. // ratio the 0.40 threshold was set against: 60 distinct words / 200.
  1540. const words = Array.from({ length: 200 }, (_, i) => `term${i % 60}`);
  1541. const issue = AIDetector.analyzeText(`${words.join(' ')}.`).issues.find((i) => i.type === 'low-ttr');
  1542. assert.ok(issue, 'expected low-ttr at 30% diversity');
  1543. assert.match(issue.text, /^Vocabulary diversity 30\.0% /);
  1544. });
  1545. test('low-ttr averages windows whose distinct count changes as they slide', () => {
  1546. // 201 tokens make two windows. The first holds "lead" and 79 cycling
  1547. // terms: 80 distinct, exactly 0.40, which does not fire on its own. The
  1548. // second drops "lead" and repeats term0: 79 distinct. Their mean, 159 /
  1549. // 400 = 0.3975, fires only if the window slides, drops the word that
  1550. // left it, and divides by the number of windows.
  1551. const words = ['lead', ...Array.from({ length: 199 }, (_, i) => `term${i % 79}`), 'term0'];
  1552. const issue = AIDetector.analyzeText(`${words.join(' ')}.`).issues.find((i) => i.type === 'low-ttr');
  1553. assert.ok(issue, 'expected low-ttr at a mean of 0.3975 over two windows');
  1554. assert.match(issue.text, /^Vocabulary diversity 39\.8% .*, 201 tokens\)$/);
  1555. });
  1556. test('low-ttr does not fire on short texts (<200 tokens)', () => {
  1557. // Same vocab-poor pattern but only ~50 tokens — below the sample-size
  1558. // threshold. Avoids drowning short social posts in a stylometric flag
  1559. // that needs more data to be reliable.
  1560. const text = ('The system shows the system improves the system. '.repeat(5));
  1561. const r = AIDetector.analyzeText(text);
  1562. const types = new Set(r.issues.map((i) => i.type));
  1563. assert.ok(!types.has('low-ttr'), 'low-ttr should not fire below 200 tokens');
  1564. });
  1565. test('ai-placeholder fires on common slot-fill bracket patterns', () => {
  1566. // The canonical AI-generated boilerplate that users paste without
  1567. // filling in. Each shape is enough on its own. Catches the "[Your
  1568. // Name]" family, dated stubs, and HTML comment placeholders.
  1569. for (const text of [
  1570. 'Dear [Recipient], I am writing regarding [Topic of Discussion].',
  1571. 'Last updated 2025-XX-XX. Authors: [INSERT TEAM NAMES HERE].',
  1572. 'See the report from XX/XX/2024 for context.',
  1573. '<!-- TODO: add citation when paper publishes -->',
  1574. '<!-- fill in the missing section before shipping -->',
  1575. ]) {
  1576. const r = AIDetector.analyzeText(text + ' Additional padding text to clear the word-count gate. '.repeat(2));
  1577. const types = new Set(r.issues.map((i) => i.type));
  1578. assert.ok(types.has('ai-placeholder'), `expected ai-placeholder for: ${text}`);
  1579. }
  1580. });
  1581. test('ai-placeholder does not fire on legitimate bracketed content', () => {
  1582. // Real bracketed content — citations, optional matches, code
  1583. // references — should NOT trip the placeholder regex. The pattern
  1584. // is gated on placeholder VERBS (Your/Insert/Add/Describe/etc.).
  1585. const text = 'The release notes for [v1.2.3] cover the [auth.refresh] path and reference [@example/user]. We saw it on commit [a3f7b21]. Padding text to clear the word-count gate so the analyzer runs the full pass cleanly.';
  1586. const r = AIDetector.analyzeText(text);
  1587. const types = new Set(r.issues.map((i) => i.type));
  1588. assert.ok(!types.has('ai-placeholder'), `expected no ai-placeholder, got: ${[...types].join(', ')}`);
  1589. });
  1590. test('ai-citation-markup fires on chatbot-internal tokens', () => {
  1591. // Each of these is a near-definitive fingerprint of a specific
  1592. // chat tool. Their presence is proof of copy-paste origin.
  1593. for (const text of [
  1594. 'The school has been recognized as an international centre. citeturn0search1 More details below.',
  1595. 'See the appendix contentReference[oaicite:3]{index=3} for the data.',
  1596. 'According to the source oai_citation provided by the model, the figure is 12.',
  1597. 'The user uploaded [attached_file:1] for review.',
  1598. 'The grok_card here links to the relevant policy. ' + 'Padding text to clear the gate. '.repeat(3),
  1599. ]) {
  1600. const r = AIDetector.analyzeText(text + ' '.repeat(0) + 'Padding text to clear the word-count gate. '.repeat(2));
  1601. const types = new Set(r.issues.map((i) => i.type));
  1602. assert.ok(types.has('ai-citation-markup'), `expected ai-citation-markup for: ${text.slice(0, 50)}...`);
  1603. }
  1604. });
  1605. test('ai-utm-source fires on AI-tool tracking parameters', () => {
  1606. // utm_source values that AI tools auto-append to URLs they generate.
  1607. // Each one is essentially proof the URL came out of a chatbot.
  1608. for (const text of [
  1609. 'See https://example.com/article?utm_source=chatgpt.com for the source.',
  1610. 'Link: https://example.com/?utm_source=copilot.com&utm_medium=referral',
  1611. 'https://docs.example.com/page?utm_source=claude.ai is the canonical reference.',
  1612. 'Reference URL: https://example.com/post?utm_source=perplexity.ai found via search.',
  1613. 'Article: https://example.com/blog?referrer=grok.com via the link.',
  1614. ]) {
  1615. const r = AIDetector.analyzeText(text + ' Padding text to clear the word-count gate so the analyzer runs cleanly across all categories.');
  1616. const types = new Set(r.issues.map((i) => i.type));
  1617. assert.ok(types.has('ai-utm-source'), `expected ai-utm-source for: ${text.slice(0, 60)}...`);
  1618. }
  1619. });
  1620. test('ai-utm-source does not fire on benign utm_source values', () => {
  1621. // Real marketing UTMs from non-AI sources should not flag.
  1622. const text = 'See https://example.com/article?utm_source=newsletter for the source. Padding text to clear the word-count gate so the analyzer runs the full pass cleanly across all categories without surprises.';
  1623. const r = AIDetector.analyzeText(text);
  1624. const types = new Set(r.issues.map((i) => i.type));
  1625. assert.ok(!types.has('ai-utm-source'), 'newsletter UTM should not flag as AI source');
  1626. });
  1627. test('severity labels are distinct across all four tiers', () => {
  1628. const labels = new Set(Object.values(AIDetector.SEVERITY_LABELS));
  1629. assert.equal(labels.size, 4, 'expected P0/P1/P2/P3 as four distinct labels');
  1630. });
  1631. // ─── v2: Tier 1 stylometric + bypass-trick detection ────────────────
  1632. test('v2: zero-width chars trigger normalization-flag', () => {
  1633. // ZWSP between "del" and "ve" defeats naive "delve" exact-match. Pre-
  1634. // pass strips it, then Tier 1 fires AND normalization-flag fires.
  1635. const zwsp = '​';
  1636. const text = `In today's landscape, we del${zwsp}ve into the intricate tapestry of innovation. This robust paradigm showcases comprehensive frameworks. The framework underscores how organizations harness cutting-edge tools.`;
  1637. const r = AIDetector.analyzeText(text);
  1638. const types = new Set(r.issues.map((i) => i.type));
  1639. assert.ok(types.has('normalization-flag'), 'expected normalization-flag on ZWSP injection');
  1640. assert.ok(r.stats.normalization.zeroWidth > 0, 'norm.zeroWidth should count strip');
  1641. });
  1642. test('v2: Cyrillic homoglyph swap restores Tier 1 hit', () => {
  1643. // "dеlve" uses Cyrillic 'е' (U+0435). Without normalization the token
  1644. // 'dеlve' would not equal 'delve' and Tier 1 misses it. After
  1645. // normalization, the Latin form fires Tier 1 AND triggers normalization-flag.
  1646. const text = 'In tоday’s landscape we dеlve intо the intricate tapestry оf the rоbust ecоsystem and dеep dive intо each layer with comprehensive depth.';
  1647. const r = AIDetector.analyzeText(text);
  1648. const types = new Set(r.issues.map((i) => i.type));
  1649. assert.ok(types.has('normalization-flag'), 'expected normalization-flag on homoglyph cluster');
  1650. assert.ok(r.stats.normalization.homoglyph >= 2, `expected >=2 homoglyph swaps, got ${r.stats.normalization.homoglyph}`);
  1651. });
  1652. const russianParagraph = 'Мы проверили новый отчёт вместе с командой и нашли три ошибки в расчётах. '
  1653. + 'Все они касались округления, поэтому исправление заняло меньше часа.';
  1654. const russianPadding = Array(6).fill(russianParagraph).join(' ');
  1655. test('Cyrillic-dominant prose reports no homoglyph swaps', () => {
  1656. const r = AIDetector.analyzeText(russianParagraph);
  1657. assert.equal(r.stats.normalization.homoglyph, 0);
  1658. assert.ok(!r.issues.some((i) => i.type === 'normalization-flag'), 'plain Russian must not look like a bypass tool');
  1659. });
  1660. test('Russian padding and one plain English sentence report no homoglyph attack', () => {
  1661. const r = AIDetector.analyzeText(`${russianPadding} The team reviewed the report today.`);
  1662. assert.equal(r.stats.normalization.homoglyph, 0);
  1663. assert.ok(!r.issues.some((i) => i.type === 'normalization-flag'));
  1664. });
  1665. test('Greek-dominant prose reports no homoglyph swaps', () => {
  1666. const text = 'Η ομάδα έλεγξε την αναφορά και βρήκε τρία λάθη στους υπολογισμούς. '
  1667. + 'Όλα αφορούσαν τη στρογγυλοποίηση, οπότε η διόρθωση πήρε λιγότερο από μία ώρα.';
  1668. const r = AIDetector.analyzeText(text);
  1669. assert.equal(r.stats.normalization.homoglyph, 0);
  1670. assert.ok(!r.issues.some((i) => i.type === 'normalization-flag'), 'plain Greek must not look like a bypass tool');
  1671. });
  1672. test('mixed-script words are still swapped inside Cyrillic prose', () => {
  1673. const normalized = AIDetector.normalizeText('Мы снова обсуждали dеlve и эхо на встрече');
  1674. assert.equal(normalized.text, 'Мы снова обсуждали delve и эхо на встрече');
  1675. assert.equal(normalized.flags.homoglyph, 1);
  1676. });
  1677. test('Cyrillic padding does not hide mixed-script obfuscation', () => {
  1678. const padding = 'Мы проверили новый отчёт вместе с командой и нашли три ошибки в расчётах. '.repeat(3);
  1679. const normalized = AIDetector.normalizeText(`We will dеlve into the report today.\n\n${padding}`);
  1680. assert.ok(normalized.text.startsWith('We will delve into the report today.'));
  1681. assert.equal(normalized.flags.homoglyph, 1);
  1682. });
  1683. test('Latin-dominant text still swaps a fully substituted word', () => {
  1684. const normalized = AIDetector.normalizeText('The team reviewed the report together and found an аре');
  1685. assert.equal(normalized.text, 'The team reviewed the report together and found an ape');
  1686. assert.equal(normalized.flags.homoglyph, 3);
  1687. });
  1688. test('fully substituted аст in English is flagged with or without Russian padding', () => {
  1689. const sentence = 'Your account is at risk. аст now to secure it.';
  1690. for (const text of [sentence, `${sentence} ${russianPadding}`, `${sentence}\n${russianPadding}`]) {
  1691. const r = AIDetector.analyzeText(text);
  1692. assert.equal(r.stats.normalization.homoglyph, 3);
  1693. assert.ok(r.issues.some((i) => i.type === 'normalization-flag'));
  1694. assert.equal(r.document_classification, 'AI_ONLY');
  1695. }
  1696. });
  1697. test('fully substituted аст in a single English sentence is swapped', () => {
  1698. const normalized = AIDetector.normalizeText('аст now to secure it.');
  1699. assert.equal(normalized.text, 'act now to secure it.');
  1700. assert.equal(normalized.flags.homoglyph, 3);
  1701. });
  1702. test('an ordinary Russian word in an English line is left alone', () => {
  1703. const text = 'The note uses жизнь to mean life.';
  1704. const normalized = AIDetector.normalizeText(text);
  1705. assert.equal(normalized.text, text);
  1706. assert.equal(normalized.flags.homoglyph, 0);
  1707. assert.ok(!AIDetector.analyzeText(text).issues.some((i) => i.type === 'normalization-flag'));
  1708. });
  1709. test('two-letter Russian words next to Russian words are not swapped in English sentences', () => {
  1710. for (const word of ['со', 'ее', 'ох', 'ус']) {
  1711. const text = `This guide explains how to run docker compose ${word} флагом build and inspect logs safely.`;
  1712. const normalized = AIDetector.normalizeText(text);
  1713. assert.equal(normalized.text, text, word);
  1714. assert.equal(normalized.flags.homoglyph, 0, word);
  1715. assert.notEqual(AIDetector.analyzeText(text).document_classification, 'AI_ONLY', word);
  1716. }
  1717. });
  1718. test('an isolated fully substituted word is swapped when punctuation separates it', () => {
  1719. const sentence = AIDetector.normalizeText('Your account is at risk. аст. Now secure it immediately through this form before it expires.');
  1720. assert.ok(sentence.text.includes(' act. Now'));
  1721. assert.equal(sentence.flags.homoglyph, 3);
  1722. });
  1723. test('hyphenated bilingual compounds keep their Russian half', () => {
  1724. const text = 'Our API-сервис handles deployment requests while the team monitors logs and reviews customer feedback each day.';
  1725. const normalized = AIDetector.normalizeText(text);
  1726. assert.equal(normalized.text, text);
  1727. assert.equal(normalized.flags.homoglyph, 0);
  1728. assert.notEqual(AIDetector.analyzeText(text).document_classification, 'AI_ONLY');
  1729. });
  1730. test('fully substituted words beside Russian words are a documented limit', () => {
  1731. const inside = 'The new service launches tomorrow and the team expects a detailed report. МЕТА поможет нам после проверки.';
  1732. assert.equal(AIDetector.normalizeText(inside).flags.homoglyph, 0);
  1733. const beside = 'Your account is at risk. пароль: аст now to secure it immediately through this form before it expires.';
  1734. assert.equal(AIDetector.normalizeText(beside).flags.homoglyph, 0);
  1735. assert.equal(AIDetector.normalizeText('Позвоните в поддержку\nаст now to secure it.').flags.homoglyph, 0);
  1736. });
  1737. test('bilingual technical sentences keep Russian words and one-letter prepositions intact', () => {
  1738. const text = 'Запустите docker compose up с флагом build and then watch the container logs closely.';
  1739. const normalized = AIDetector.normalizeText(text);
  1740. assert.equal(normalized.text, text);
  1741. assert.equal(normalized.flags.homoglyph, 0);
  1742. });
  1743. test('Russian technical prose keeps short Russian words next to English terms', () => {
  1744. const text = 'Задача взята: issue #9194 и PR #9195 (RecursionError в `make_json_safe()` на Enum, найдено в Discussions #9189).';
  1745. const normalized = AIDetector.normalizeText(text);
  1746. assert.equal(normalized.text, text);
  1747. assert.equal(normalized.flags.homoglyph, 0);
  1748. assert.notEqual(AIDetector.analyzeText(text).document_classification, 'AI_ONLY');
  1749. });
  1750. test('dates and hard line wraps do not cut a Russian sentence into Latin-looking pieces', () => {
  1751. const text = 'Ответы со ссылкой на stageload там, где обсуждают нехватку памяти на Mac. Охват\n'
  1752. + ' поста в r/StableDiffusion от 07.10 снимем в понедельник.';
  1753. const normalized = AIDetector.normalizeText(text);
  1754. assert.equal(normalized.text, text);
  1755. assert.equal(normalized.flags.homoglyph, 0);
  1756. });
  1757. test('a hard-wrapped English sentence still swaps a fully substituted word', () => {
  1758. const normalized = AIDetector.normalizeText('Your account is at risk. аст now\nto secure it.');
  1759. assert.equal(normalized.text, 'Your account is at risk. act now\nto secure it.');
  1760. assert.equal(normalized.flags.homoglyph, 3);
  1761. });
  1762. test('closing quotes and brackets keep separate sentences from sharing script evidence', () => {
  1763. for (const [open, close] of [['«', '»'], ['"', '"'], ['“', '”'], ['‘', '’'], ['(', ')'], ['[', ']'], ['{', '}']]) {
  1764. const text = `${open}Задача закрыта.${close} аст now to secure it.`;
  1765. const normalized = AIDetector.normalizeText(text);
  1766. assert.equal(normalized.text, `${open}Задача закрыта.${close} act now to secure it.`, close);
  1767. assert.equal(normalized.flags.homoglyph, 3, close);
  1768. }
  1769. });
  1770. test('Markdown blocks do not share script evidence across an unterminated line', () => {
  1771. for (const start of ['- ', '* ', '+ ', '1. ', '2) ', '# ', '### ', '> ', '>', '>>', '| ', '```', '~~~']) {
  1772. for (const newline of ['\n', '\r\n']) {
  1773. const text = `- аст now to secure it${newline}${start}Позвоните в поддержку`;
  1774. const normalized = AIDetector.normalizeText(text);
  1775. assert.equal(normalized.text, `- act now to secure it${newline}${start}Позвоните в поддержку`, start);
  1776. assert.equal(normalized.flags.homoglyph, 3, start);
  1777. }
  1778. }
  1779. });
  1780. test('Markdown separators, table delimiters and HTML starts keep script evidence separate', () => {
  1781. for (const newline of ['\n', '\r\n']) {
  1782. for (const separator of ['---', '--', '***', '___', '===', '==', '=', '- - -', '* * *', '_ _ _', ' --- ', ' -- ']) {
  1783. const text = `аст now to secure it${newline}${separator}${newline}Позвоните в поддержку`;
  1784. const normalized = AIDetector.normalizeText(text);
  1785. assert.equal(normalized.text, text.replace('аст', 'act'), separator);
  1786. assert.equal(normalized.flags.homoglyph, 3, separator);
  1787. }
  1788. for (const text of [
  1789. `Позвоните | поддержку${newline}--- | ---${newline}аст now | to secure it`,
  1790. `Позвоните | поддержку${newline}:--- | ---: |${newline}аст now | to secure it`,
  1791. `<p>Позвоните в поддержку</p>${newline}<p>аст now to secure it</p>`,
  1792. ]) {
  1793. assert.equal(AIDetector.normalizeText(text).text, text.replace('аст', 'act'));
  1794. assert.equal(AIDetector.normalizeText(text).flags.homoglyph, 3);
  1795. }
  1796. const prose = `Ответы со ссылкой на docker v1.2${newline}от 07.10 снимем в понедельник.`;
  1797. assert.equal(AIDetector.normalizeText(prose).text, prose);
  1798. assert.equal(AIDetector.normalizeText(prose).flags.homoglyph, 0);
  1799. }
  1800. });
  1801. test('Setext heading underlines keep script evidence separate across blocks', () => {
  1802. for (const newline of ['\n', '\r\n']) {
  1803. for (const underline of ['===', '==', '=', '---', '--', ' ===', ' --']) {
  1804. const text = `Позвоните в поддержку.${newline}аст now to secure it${newline}${underline}${newline}И еще русский текст.`;
  1805. const normalized = AIDetector.normalizeText(text);
  1806. assert.equal(normalized.text, text.replace('аст', 'act'), underline);
  1807. assert.equal(normalized.flags.homoglyph, 3, underline);
  1808. }
  1809. }
  1810. });
  1811. test('unspaced sentence punctuation does not hide a fully substituted English word', () => {
  1812. for (const ending of ['.', '!', '?', '.(', '.«', '!“']) {
  1813. const text = `Позвоните сейчас${ending}аст now to secure it.`;
  1814. const normalized = AIDetector.normalizeText(text);
  1815. assert.equal(normalized.text, `Позвоните сейчас${ending}act now to secure it.`, ending);
  1816. assert.equal(normalized.flags.homoglyph, 3, ending);
  1817. }
  1818. });
  1819. test('indented CRLF prose continues across a hard wrap and keeps version dots', () => {
  1820. const text = 'Ответы на вопросы о docker v1.2 и report.pdf\r\n там, где обсуждают настройку памяти на Mac.';
  1821. assert.equal(AIDetector.normalizeText(text).text, text);
  1822. assert.equal(AIDetector.normalizeText(text).flags.homoglyph, 0);
  1823. });
  1824. test('equal Latin and Cyrillic counts keep the Cyrillic-dominant tie rule', () => {
  1825. const normalized = AIDetector.normalizeText('а a');
  1826. assert.equal(normalized.text, 'а a');
  1827. assert.equal(normalized.flags.homoglyph, 0);
  1828. });
  1829. test('v2: formulaic opener fires', () => {
  1830. const text = 'In the rapidly evolving world of decentralized finance, new protocols have emerged as critical infrastructure. The market continues to expand at an unprecedented pace each quarter without fail.';
  1831. const r = AIDetector.analyzeText(text);
  1832. const types = new Set(r.issues.map((i) => i.type));
  1833. assert.ok(types.has('formulaic-opener'), 'expected formulaic-opener flag');
  1834. });
  1835. test('v2: speculative scenario opener fires', () => {
  1836. const text = 'Imagine a world where every developer ships bug-free code on the first try. That is the promise this framework keeps making in its docs, and it deserves a much harder look before anyone commits a roadmap to it.';
  1837. const r = AIDetector.analyzeText(text);
  1838. const types = new Set(r.issues.map((i) => i.type));
  1839. assert.ok(types.has('speculative-opener'), 'expected speculative-opener flag');
  1840. // The comma-interrupted cadence is the same tell and must also fire.
  1841. const interrupted = AIDetector.analyzeText('Imagine, for a moment, a world where every deploy is instant. That is the pitch, and the pricing page leans on it hard enough that the claim deserves scrutiny before anyone signs.');
  1842. const interruptedTypes = new Set(interrupted.issues.map((i) => i.type));
  1843. assert.ok(interruptedTypes.has('speculative-opener'), 'expected speculative-opener on "Imagine, for a moment, a world where"');
  1844. });
  1845. test('v2: speculative opener leaves instructional "imagine you have" alone', () => {
  1846. // The gate keys on the world/future/reality/scenario object plus
  1847. // where/in-which, so a teaching device that points at a concrete
  1848. // example must stay clean.
  1849. const clean = [
  1850. 'Imagine you have a sorted array of one million integers and you want to find one value fast.',
  1851. 'Picture the request as it moves through the load balancer, the cache, and finally the database.',
  1852. 'Consider a scenario where the token expires mid-request; the client has to retry with a fresh one.',
  1853. ];
  1854. for (const text of clean) {
  1855. const r = AIDetector.analyzeText(text);
  1856. const types = new Set(r.issues.map((i) => i.type));
  1857. assert.ok(!types.has('speculative-opener'), `false positive on instructional prose: ${text}`);
  1858. }
  1859. });
  1860. test('v2: launch-copy introduction fires', () => {
  1861. const text = 'Meet Flowdesk, your new favorite treasury dashboard. One screen for a full fund position, refreshed every block, with none of the spreadsheet glue that eats an operations hire alive.';
  1862. const r = AIDetector.analyzeText(text);
  1863. const types = new Set(r.issues.map((i) => i.type));
  1864. assert.ok(types.has('launch-intro'), 'expected launch-intro flag');
  1865. // The 'the new home of' head is the launch-copy form the tail
  1866. // requirement has to keep. It must still fire.
  1867. const homeOf = AIDetector.analyzeText('Meet Riverside, the new home of our Saturday farmers market, open from seven every weekend through October.');
  1868. const homeOfTypes = new Set(homeOf.issues.map((i) => i.type));
  1869. assert.ok(homeOfTypes.has('launch-intro'), 'expected launch-intro on "the new home of"');
  1870. // The mashup pitch is the same move and must also fire.
  1871. const mashup = AIDetector.analyzeText('Think Notion meets Figma, except the canvas settles on-chain. The team has shipped four releases since March and the changelog reads like a product that knows where it is going.');
  1872. const mashupTypes = new Set(mashup.issues.map((i) => i.type));
  1873. assert.ok(mashupTypes.has('launch-intro'), 'expected launch-intro on "Think X meets Y"');
  1874. });
  1875. test('v2: launch-copy introduction leaves ordinary narrative alone', () => {
  1876. // The gate keys on the sentence-initial imperative plus a capitalized
  1877. // product name, so instructions and narrative must stay clean.
  1878. const clean = [
  1879. 'Enter the password to continue, then wait for the confirmation code before closing the tab.',
  1880. "You'll meet Sarah, your new manager, at nine tomorrow in the second-floor conference room.",
  1881. 'I think Marseille meets the sea at its old port, and the walk down from the station proves it.',
  1882. // The human people/pet introduction is the same surface shape and must
  1883. // stay clean — only launch-copy heads ("your new favorite") fire.
  1884. 'Quick note before standup this morning. Meet Sarah, your new account manager for the northeast region.',
  1885. 'Meet Buddy, the new face of our shelter this month, and come say hi at the adoption day on Saturday.',
  1886. // Doc field labels are capitalized by convention, and the bare
  1887. // 'Enter X.' surface cannot be separated from them by any
  1888. // terminator or field-name denylist, so it is judgment-only and
  1889. // every one of these forms stays clean.
  1890. 'Complete each field in order before you submit. Enter Username: your work email address. Enter Password: at least twelve characters.',
  1891. 'Complete each field in order before you submit. Enter Password. Enter Amount. Then press Continue and wait for the confirmation code.',
  1892. 'Complete each field in order before you submit. Enter Username — your work email address. Then press Continue and wait for the code.',
  1893. 'The launch post opened the way every launch post opens now. Enter Flowdesk. The deck said nothing else about what it actually does.',
  1894. // The 'the new home/way/standard' heads need the tail launch copy
  1895. // actually has ('of', 'to', 'in|for') or an end of clause. Without
  1896. // it the head noun swallowed the first word of a compound noun and
  1897. // both of these fired.
  1898. 'Meet Rosa, the new home secretary, at the town hall on Thursday evening after the council session.',
  1899. 'Meet Emma, the new way station manager for the northern line, starting the first week of April.',
  1900. ];
  1901. for (const text of clean) {
  1902. const r = AIDetector.analyzeText(text);
  1903. const types = new Set(r.issues.map((i) => i.type));
  1904. assert.ok(!types.has('launch-intro'), `false positive on ordinary prose: ${text}`);
  1905. }
  1906. // Adjacent intros each count: the anchors are lookbehinds, so the first
  1907. // match's terminator still anchors the second.
  1908. const adjacent = AIDetector.analyzeText('Think Flowdesk meets Ledgerly. Think Notion meets Figma. Two launches, one deck, and the same investor list for both companies this quarter.');
  1909. const adjacentHits = adjacent.issues.filter((i) => i.type === 'launch-intro');
  1910. assert.equal(adjacentHits.length, 2, 'expected both adjacent intros to count');
  1911. });
  1912. test('v2: dramatized crowd contrast fires on the dismissive clause', () => {
  1913. const text = 'We shipped it in 2022, while everyone else was still debating timelines. The migration tooling followed six months later and the audit landed before the first competitor published a spec.';
  1914. const r = AIDetector.analyzeText(text);
  1915. const types = new Set(r.issues.map((i) => i.type));
  1916. assert.ok(types.has('crowd-contrast'), 'expected crowd-contrast flag');
  1917. const thinkpieces = AIDetector.analyzeText('They built it in a weekend while the industry wrote thinkpieces about whether the category was even viable, and the launch numbers made the question moot.');
  1918. const thinkpiecesTypes = new Set(thinkpieces.issues.map((i) => i.type));
  1919. assert.ok(thinkpiecesTypes.has('crowd-contrast'), 'expected crowd-contrast on the thinkpieces variant');
  1920. // The space form of catch-up is the same tell as the hyphenated form.
  1921. const catchup = AIDetector.analyzeText('The desk shipped the migration in March while everyone else played catch up on last year\u2019s roadmap and called it strategy.');
  1922. const catchupTypes = new Set(catchup.issues.map((i) => i.type));
  1923. assert.ok(catchupTypes.has('crowd-contrast'), 'expected crowd-contrast on the space form of catch-up');
  1924. });
  1925. test('v2: crowd contrast leaves literal simultaneity alone', () => {
  1926. // The dismissive-verb gate exists so ordinary narrative never fires.
  1927. const clean = [
  1928. 'She read in the kitchen while everyone else watched the movie in the dark.',
  1929. 'The kids played in the yard while the adults talked about the drive home.',
  1930. 'He kept the beat while the rest of the band tuned between songs.',
  1931. // Literal uses of the dismissive verbs themselves must stay clean —
  1932. // the "was still" marker is what separates the tell from wire copy,
  1933. // memoir, and fiction. These three fired before the marker was
  1934. // required; they are pinned here so the gate cannot regress.
  1935. 'The senator said nothing during the hearing while others debated the amendment late into the evening.',
  1936. 'Stocks drifted sideways on Tuesday while the market speculated about the timing of the next rate cut.',
  1937. 'Thanksgiving got loud this year, so I did the dishes while everyone else argued about politics in the living room.',
  1938. // Verb stems carry explicit inflection tails, so agent nouns and
  1939. // adverbs sharing the stem never match.
  1940. 'Retail kept its head down while the market speculators chased the squeeze into Friday expiry.',
  1941. 'She packed the van while everyone else deliberately ignored the checklist taped to the garage door.',
  1942. // Branch one is restricted to the -ing form. The adjective, the
  1943. // passive, and the bare present all read as ordinary prose and all
  1944. // three matched while e/es/ed were allowed.
  1945. 'We shipped it in 2022, while everyone else was still deliberate about the tradeoff between latency and cost.',
  1946. 'We shipped it in 2022, while everyone else was still debated by pundits who had never run the migration themselves.',
  1947. 'We shipped it in 2022, while everyone else was still argued over by a committee that met twice a quarter.',
  1948. ];
  1949. for (const text of clean) {
  1950. const r = AIDetector.analyzeText(text);
  1951. const types = new Set(r.issues.map((i) => i.type));
  1952. assert.ok(!types.has('crowd-contrast'), `false positive on literal simultaneity: ${text}`);
  1953. }
  1954. });
  1955. test('v2: fake-casual props fire', () => {
  1956. const text = '*checks notes* the proposal passed with four abstentions and nobody in the room had read the appendix before the vote.';
  1957. const r = AIDetector.analyzeText(text);
  1958. const types = new Set(r.issues.map((i) => i.type));
  1959. assert.ok(types.has('fake-casual-prop'), 'expected fake-casual-prop flag');
  1960. // The wink aside gets its own case, asserting on the matched text. It
  1961. // used to share the stage-direction fixture above, which meant a
  1962. // never-matching wink regex still left this test green.
  1963. const wink = AIDetector.analyzeText('The fees went up again (yes, really) and nobody sent a notice about it before the billing run.');
  1964. const winkHits = wink.issues.filter((i) => i.type === 'fake-casual-prop');
  1965. assert.equal(winkHits.length, 1, 'expected exactly one wink-aside hit');
  1966. assert.equal(winkHits[0].text, '(yes, really)', 'expected the wink aside itself to be the match');
  1967. // The curly apostrophe (U+2019) is the form smart punctuation and LLMs
  1968. // actually emit; it must fire the same as the ASCII form.
  1969. const curly = AIDetector.analyzeText('The ending was *chef\u2019s kiss* and nobody in the room could pretend otherwise by the time the credits rolled.');
  1970. const curlyTypes = new Set(curly.issues.map((i) => i.type));
  1971. assert.ok(curlyTypes.has('fake-casual-prop'), 'expected fake-casual-prop on curly-apostrophe chef\u2019s kiss');
  1972. });
  1973. test('v2: fake-casual props leave math and plain qualifiers alone', () => {
  1974. const clean = [
  1975. 'The math checks out: 5 * 3 * 2 gives the same total as the ledger, and yes, really careful review found no rounding drift.',
  1976. 'Multiply the base rate * the utilization factor before applying the cap, because of course fees compound under load.',
  1977. // 'because of course ...' is judgment-only: no tense gate separates
  1978. // the wink from the ordinary human grumble, which uses the same
  1979. // present-tense-plus-did form.
  1980. // The kiss pattern requires the apostrophe. With it optional, this
  1981. // ordinary sentence matched.
  1982. 'At midnight, *chefs kiss* their spouses goodbye and head back to the line for the second service.',
  1983. 'We left an hour early and still missed kickoff, because of course it was raining and the bridge was closed.',
  1984. 'The build failed on the last commit of the day, because of course it did, and the on-call engineer had already gone to bed.',
  1985. 'The vendor shipped the patch a week after the window closed, because of course they do, and the invoice arrived on time.',
  1986. ];
  1987. for (const text of clean) {
  1988. const r = AIDetector.analyzeText(text);
  1989. const types = new Set(r.issues.map((i) => i.type));
  1990. assert.ok(!types.has('fake-casual-prop'), `false positive on plain prose: ${text}`);
  1991. }
  1992. });
  1993. test('v2: parenthetical hedge fires', () => {
  1994. const text = 'The protocol works as intended (and increasingly, with better latency than competitors). The team has shipped consistently for six months without missing a single release cadence target.';
  1995. const r = AIDetector.analyzeText(text);
  1996. const types = new Set(r.issues.map((i) => i.type));
  1997. assert.ok(types.has('parenthetical-hedge'), 'expected parenthetical-hedge flag');
  1998. });
  1999. test('v2: social endorsement closer fires on LinkedIn-style share post', () => {
  2000. const text = 'Just finished Sarah\'s deep dive on why context windows leak in long agent runs. She walks through the eviction policy line by line and shows where the tokens actually go. This one is worth your time:';
  2001. const r = AIDetector.analyzeText(text);
  2002. const types = new Set(r.issues.map((i) => i.type));
  2003. assert.ok(types.has('social-cta-closer'), 'expected social-cta-closer flag');
  2004. });
  2005. test('v2: social endorsement closer covers every regex branch', () => {
  2006. // One positive per branch so a typo in any single pattern fails loudly
  2007. // instead of shipping green. Each string is padded to clear the 10-word
  2008. // floor and sit amid normal prose, the shape analyzeText actually sees.
  2009. const variants = [
  2010. 'Sarah broke down the whole eviction policy in plain terms. This one is worth your time:', // worth-endorsement
  2011. 'New deep dive on agent memory just dropped today. This one is a must-read for the whole team.', // must-read
  2012. 'I read the entire thing twice this weekend. I highly recommend giving this a read soon.', // recommend-a-read
  2013. 'The setup is fiddly but the payoff is huge here. Do yourself a favor and read this tonight.', // do-yourself-a-favor
  2014. 'The agenda is packed and the speakers are all sharp. You won\'t want to miss this one.', // won't-want-to-miss
  2015. 'It saved me an entire afternoon of painful debugging. Thank me later, seriously, you will.', // thank-me-later
  2016. 'It is the cleanest reference I have found all year. Save this one for later when you ship.', // save-for-later
  2017. 'Everything you need for the whole migration is in this one. Bookmark this post.', // bookmark-this
  2018. 'The benchmarks completely flip the usual assumptions. Don\'t sleep on this one, honestly.', // don't-sleep-on
  2019. 'The framing reframed the entire debate for me cleanly. Trust me, you\'ll want to read this.', // trust-me-you'll
  2020. ];
  2021. for (const text of variants) {
  2022. const r = AIDetector.analyzeText(text);
  2023. const types = new Set(r.issues.map((i) => i.type));
  2024. assert.ok(types.has('social-cta-closer'), `expected social-cta-closer on: ${text}`);
  2025. }
  2026. });
  2027. test('v2: social endorsement closer matches curly-apostrophe forms', () => {
  2028. // LinkedIn / Word / macOS auto-curl apostrophes, so the canonical
  2029. // "you won't" / "don't" / "you'll" closers ship with U+2019, not ASCII.
  2030. // A straight-only class would miss the dominant real-world input.
  2031. const curly = [
  2032. 'The agenda is packed and the speakers are all sharp. You won’t want to miss this one.',
  2033. 'The benchmarks completely flip the usual assumptions. Don’t sleep on this one, honestly.',
  2034. 'The framing reframed the entire debate for me cleanly. Trust me, you’ll want to read this.',
  2035. ];
  2036. for (const text of curly) {
  2037. const r = AIDetector.analyzeText(text);
  2038. const types = new Set(r.issues.map((i) => i.type));
  2039. assert.ok(types.has('social-cta-closer'), `expected social-cta-closer on curly form: ${text}`);
  2040. }
  2041. });
  2042. test('v2: social endorsement closer leaves literal-verb human prose alone', () => {
  2043. // The anchors (demonstrative object, terminal lookahead, sentence-initial
  2044. // lookbehind) exist to keep the detector off ordinary instructional and
  2045. // conversational text. Each of these uses a trigger word in its literal
  2046. // sense and must stay clean.
  2047. const clean = [
  2048. 'The book is worth reading if you have the time, but the middle third drags and I almost put it down.',
  2049. 'Bookmark this page so you can find the API reference later when you are wiring up the client.',
  2050. 'I usually save this for later in the sprint when the review queue has finally cleared out a bit.',
  2051. 'Do yourself a favor and read the runbook before you ever go on call for this service again.',
  2052. 'You will not want to miss this design review tomorrow because the API contract is changing under us.',
  2053. 'She will thank me later for the heads up once the deploy window actually closes without incident.',
  2054. 'Trust me, this one took an entire afternoon to track down and it was a single off-by-one in the loop.',
  2055. ];
  2056. for (const text of clean) {
  2057. const r = AIDetector.analyzeText(text);
  2058. const types = new Set(r.issues.map((i) => i.type));
  2059. assert.ok(!types.has('social-cta-closer'), `false positive on literal prose: ${text}`);
  2060. }
  2061. });
  2062. test('v2: trinary output present + FN-biased for ambiguous text', () => {
  2063. // A plain human bug-report should not get AI_ONLY even if score lifts.
  2064. const text = 'The build broke again this morning. Rolled back the auth refactor and tests pass now. Still need to figure out why the token refresh path hits a 401 for users on Safari but not Firefox — probably a cookie scope issue but I want to confirm before shipping a fix.';
  2065. const r = AIDetector.analyzeText(text);
  2066. assert.ok(r.document_classification, 'expected document_classification field');
  2067. assert.ok(['HUMAN_ONLY', 'MIXED'].includes(r.document_classification), `human prose got ${r.document_classification}`);
  2068. assert.ok(r.class_probabilities, 'expected class_probabilities');
  2069. const sum = r.class_probabilities.human + r.class_probabilities.mixed + r.class_probabilities.ai;
  2070. assert.ok(Math.abs(sum - 1) < 0.02, `probabilities should sum to ~1, got ${sum}`);
  2071. assert.ok(['high', 'medium', 'low'].includes(r.confidence_category), 'expected confidence_category');
  2072. });
  2073. test('v2: highly AI-marked text reaches AI_ONLY with corroborators', () => {
  2074. // High score + cutoff disclaimer (corroborator) → AI_ONLY at high confidence.
  2075. const text = [
  2076. "As of my last update, I don't have access to real-time data. In the rapidly evolving world of decentralized finance, we delve into the intricate tapestry of innovation.",
  2077. 'This seamless, robust paradigm showcases a comprehensive framework. Moreover, it truly is a game-changer that underscores how we navigate the complexities of modern AI.',
  2078. 'Furthermore, this pivotal moment marks a watershed for the industry. Let me think step by step about how to approach this systematically. I hope this helps!',
  2079. ].join(' ');
  2080. const r = AIDetector.analyzeText(text);
  2081. assert.equal(r.document_classification, 'AI_ONLY', `expected AI_ONLY, got ${r.document_classification} (score=${r.score})`);
  2082. assert.ok(['medium', 'high'].includes(r.confidence_category), `expected medium/high confidence, got ${r.confidence_category}`);
  2083. });
  2084. test('v2: highlight_sentence_for_ai returns regions with start/end offsets', () => {
  2085. const text = 'In the rapidly evolving world of AI, we delve into the intricate tapestry. This is a robust, comprehensive paradigm. Plain second paragraph here is just normal prose without any of the tells. The team shipped a fix on Monday afternoon after the rollback completed successfully.';
  2086. const r = AIDetector.analyzeText(text);
  2087. assert.ok(Array.isArray(r.highlight_sentence_for_ai), 'expected highlight array');
  2088. if (r.highlight_sentence_for_ai.length > 0) {
  2089. const region = r.highlight_sentence_for_ai[0];
  2090. assert.ok(typeof region.start === 'number', 'region has start offset');
  2091. assert.ok(typeof region.end === 'number', 'region has end offset');
  2092. assert.ok(region.end > region.start, 'end > start');
  2093. assert.ok(typeof region.score === 'number' && region.score >= 0 && region.score <= 1, 'region.score 0-1');
  2094. }
  2095. });
  2096. test('v2: context mode "technical" suppresses Title Case header flag', () => {
  2097. const text = 'Strategic Negotiations And Key Partnerships\n\nThe team closed three deals this quarter. Each agreement included revenue-share terms and dispute-resolution clauses. The legal review took two weeks per contract on average.';
  2098. const general = AIDetector.analyzeText(text, { contextMode: 'general' });
  2099. const technical = AIDetector.analyzeText(text, { contextMode: 'technical' });
  2100. const generalHas = general.issues.some((i) => i.type === 'title-case-header');
  2101. const technicalHas = technical.issues.some((i) => i.type === 'title-case-header');
  2102. assert.ok(generalHas, 'general mode should flag title-case header');
  2103. assert.ok(!technicalHas, 'technical mode should suppress title-case header');
  2104. });
  2105. test('#62: Title Case flagged on a Markdown heading, not just a bare line', () => {
  2106. // The `^[A-Z]` anchor required the line to START with a capital, so a
  2107. // `## Heading` never matched — the first character is `#`. The rule missed
  2108. // the commonest way a heading is actually written while catching the bare
  2109. // form it gets converted from. Reported by a downstream vendoring the file.
  2110. const body =
  2111. '\n\nThe team closed three deals this quarter. Each agreement included revenue-share terms and dispute-resolution clauses. The legal review took two weeks per contract on average.';
  2112. for (const prefix of ['#', '##', '######']) {
  2113. const r = AIDetector.analyzeText(`${prefix} Strategic Negotiations And Key Partnerships${body}`, {
  2114. contextMode: 'general',
  2115. });
  2116. assert.ok(
  2117. r.issues.some((i) => i.type === 'title-case-header'),
  2118. `expected title-case-header on a "${prefix}" heading`,
  2119. );
  2120. }
  2121. });
  2122. test('#62: the heading fix does not flag sentence-case or non-headings', () => {
  2123. const body =
  2124. '\n\nThe team closed three deals this quarter. Each agreement included revenue-share terms and dispute-resolution clauses. The legal review took two weeks per contract on average.';
  2125. const cases = [
  2126. ['## Strategic negotiations and key partnerships', 'sentence-case heading is correct, not a tell'],
  2127. ['##Strategic Negotiations And Key Partnerships', 'no space after # is not a Markdown heading'],
  2128. ['####### Strategic Negotiations And Key Partnerships', 'seven hashes is not a heading'],
  2129. ];
  2130. for (const [line, why] of cases) {
  2131. const r = AIDetector.analyzeText(line + body, { contextMode: 'general' });
  2132. assert.ok(!r.issues.some((i) => i.type === 'title-case-header'), why);
  2133. }
  2134. });
  2135. // Prose long enough to clear the ten-word gate, appended to single-line fixtures.
  2136. const HEADING_BODY =
  2137. '\n\nThe team closed three deals this quarter. Each agreement included revenue-share terms and dispute-resolution clauses. The legal review took two weeks per contract on average.';
  2138. function titleCaseHits(text) {
  2139. const r = AIDetector.analyzeText(text, { contextMode: 'general' });
  2140. return r.issues.filter((i) => i.type === 'title-case-header');
  2141. }
  2142. // NOT covered, and deliberately so: a four-content-word Title Case heading such
  2143. // as '# The Art Of War' or '## Notes On The Design' still fires. The >= 4 guard
  2144. // cannot tell those from '## Benefits And Strategic Considerations' -- they are
  2145. // the same shape. That is pre-existing behaviour, identical for the bare-line
  2146. // form on main, and out of scope here.
  2147. test('#62: legitimate three-word Title Case headings are not flagged', () => {
  2148. // The regression the first version of this fix shipped. matchPatterns reports
  2149. // match[0], so '## Terms Of Service' arrived with '##' as a token, silently
  2150. // lowering the proper-noun guard from four content words to three for
  2151. // headings only. These are ordinary human headings, on a detector whose
  2152. // stated first priority is not firing on human writing.
  2153. for (const heading of [
  2154. '## Terms Of Service',
  2155. '## Table Of Contents',
  2156. '## Bank Of America',
  2157. '## Pride And Prejudice',
  2158. '## Statement Of Work',
  2159. ]) {
  2160. assert.equal(titleCaseHits(heading + HEADING_BODY).length, 0, `must not flag: ${heading}`);
  2161. }
  2162. });
  2163. test('#62: a heading inside a fenced block is illustration, not a section header', () => {
  2164. // A document that documents Markdown is the normal case for this rule.
  2165. // Without the fence check, every docs page flags itself.
  2166. const fence = '```';
  2167. const text = [
  2168. 'How to write documentation, briefly, with a worked example that follows.',
  2169. '',
  2170. fence + 'markdown',
  2171. '## Benefits And Strategic Considerations',
  2172. fence,
  2173. '',
  2174. 'That is the shape to avoid in your own prose, not in a code sample.',
  2175. ].join('\n');
  2176. assert.equal(titleCaseHits(text).length, 0, 'a fenced example must not flag');
  2177. });
  2178. test('#62: headings opening with a function word are not the tell', () => {
  2179. // Measured on 81 files that provably predate LLMs (2018-19 eBooks stamped
  2180. // year: 2018/2019, 2020 posts): these fired 13 times on the branch and zero
  2181. // times on main. Every one opens with "The". The rule's own comment has
  2182. // always said the function word marks a MID-sentence "And"; the test never
  2183. // enforced it. Verbatim headings from that corpus.
  2184. for (const heading of [
  2185. '## The New Security Landscape',
  2186. '## The Microsoft Approach to Identity',
  2187. '### The Four Keys to a Successful and Secure Modern Workplace',
  2188. '## The Changing Face of Manufacturing',
  2189. '## The Key to Winning Georgia',
  2190. '## The Chain of Thought Podcast',
  2191. ]) {
  2192. assert.equal(titleCaseHits(heading + HEADING_BODY).length, 0, `must not flag: ${heading}`);
  2193. }
  2194. });
  2195. test('#62: an interior function word still flags, in both forms', () => {
  2196. // The other side of the guard above. If this ever goes quiet the rule is dead.
  2197. for (const heading of [
  2198. '## Benefits And Strategic Considerations',
  2199. 'Benefits And Strategic Considerations',
  2200. '## Strategic Negotiations And Key Partnerships',
  2201. ]) {
  2202. assert.equal(titleCaseHits(heading + HEADING_BODY).length, 1, `must flag: ${heading}`);
  2203. }
  2204. });
  2205. test('#240: an acronym or single-letter function word in the title still flags', () => {
  2206. // `TITLE_CASE_HEADER` previously required every interior token to be
  2207. // [A-Z][a-z]+ or a lowercase function word, so a capitalised `A` and any
  2208. // all-caps acronym (AI, API, CLI) broke the whole match. These are the
  2209. // common shapes in the content this rule actually targets.
  2210. for (const heading of [
  2211. '## Why Your Team Needs A Better Testing Strategy',
  2212. '## The Future Of AI In Production',
  2213. '## Choosing A Database For Your Startup',
  2214. '## Building An API Driven Strategy',
  2215. ]) {
  2216. assert.equal(titleCaseHits(heading + HEADING_BODY).length, 1, `must flag: ${heading}`);
  2217. }
  2218. });
  2219. test('#240: an all-caps banner line is not a title-case header', () => {
  2220. // The first and last tokens must remain ordinary [A-Z][a-z]+ words, so a
  2221. // fully uppercase line like a section banner never matches.
  2222. assert.equal(
  2223. titleCaseHits('## HTTP API REFERENCE' + HEADING_BODY).length,
  2224. 0,
  2225. 'all-caps banner must not flag',
  2226. );
  2227. });
  2228. test('#240: unrelated single-letter capitals do not widen the rule', () => {
  2229. assert.equal(
  2230. titleCaseHits('## What X Means And Other Things' + HEADING_BODY).length,
  2231. 0,
  2232. 'only the capitalised function word A may be a one-letter interior token',
  2233. );
  2234. });
  2235. test('#314: first-person I can appear inside a Title Case heading', () => {
  2236. assert.equal(
  2237. titleCaseHits('## What I Learned And Why It Matters' + HEADING_BODY).length,
  2238. 1,
  2239. 'a first-person title with a targeted capitalized function word must flag',
  2240. );
  2241. for (const heading of [
  2242. '## What X Learned And Why It Matters',
  2243. '## What I learned and why it matters',
  2244. '## What I Learned About Writing',
  2245. ]) {
  2246. assert.equal(titleCaseHits(heading + HEADING_BODY).length, 0, `must not flag: ${heading}`);
  2247. }
  2248. });
  2249. test('#291: blank lines do not manufacture a longer heading', () => {
  2250. // `\s+` between words also eats newlines, so two unrelated lines could
  2251. // combine into one heading match that neither line independently satisfies.
  2252. // The repro from #290's fixed-corpus review.
  2253. const filler = Array.from({ length: 30 }, (_, i) => 'word' + i).join(' ');
  2254. const cases = [
  2255. ['## Benefits\n\nOf Good Writing\n\n' + filler, 'blank-line separated fragments'],
  2256. ['## Benefits\nOf Good Writing\n\n' + filler, 'adjacent-line fragments'],
  2257. ['## Benefits\r\n\r\nOf Good Writing\r\n\r\n' + filler, 'CRLF variants'],
  2258. ];
  2259. for (const [text, why] of cases) {
  2260. assert.equal(
  2261. titleCaseHits(text).length,
  2262. 0,
  2263. `must not combine lines into a heading: ${why}`,
  2264. );
  2265. }
  2266. });
  2267. test('#291: a single physical line still flags with horizontal whitespace', () => {
  2268. const filler = Array.from({ length: 30 }, (_, i) => 'word' + i).join(' ');
  2269. assert.equal(
  2270. titleCaseHits('## Benefits And Strategic Considerations\n\n' + filler).length,
  2271. 1,
  2272. 'a real one-line heading must still flag',
  2273. );
  2274. assert.equal(
  2275. titleCaseHits('##\tBenefits And Strategic Considerations\n\n' + filler).length,
  2276. 1,
  2277. 'tab-indented heading still flags',
  2278. );
  2279. });
  2280. test('#62: fences that a parity count gets wrong', () => {
  2281. const f3 = '```';
  2282. const f4 = '````';
  2283. const intro = 'Documentation about writing Markdown, long enough to clear the word gate.';
  2284. const title = '## Benefits And Strategic Considerations';
  2285. // A four-backtick fence wrapping a three-backtick example is how you document
  2286. // fences — the motivating case. Counting delimiters inverts on it.
  2287. assert.equal(
  2288. titleCaseHits([intro, f4, f3 + 'markdown', title, f3, f4].join('\n') + HEADING_BODY).length,
  2289. 0,
  2290. 'four-backtick outer fence',
  2291. );
  2292. // CommonMark allows up to three spaces of indent.
  2293. assert.equal(
  2294. titleCaseHits([intro, ' ' + f3, title, ' ' + f3].join('\n') + HEADING_BODY).length,
  2295. 0,
  2296. 'indented fence',
  2297. );
  2298. // An unclosed fence runs to end of document, as renderers treat it.
  2299. assert.equal(
  2300. titleCaseHits([intro, f3, title].join('\n') + HEADING_BODY).length,
  2301. 0,
  2302. 'unclosed fence',
  2303. );
  2304. // ...and a correctly closed one must not swallow what follows.
  2305. assert.equal(
  2306. titleCaseHits([intro, f3, 'code', f3, title].join('\n') + HEADING_BODY).length,
  2307. 1,
  2308. 'heading after a closed fence must still flag',
  2309. );
  2310. });
  2311. test('#77: only spaces and tabs may follow a closing fence', () => {
  2312. const intro = 'Documentation about writing Markdown, long enough to clear the word gate.';
  2313. const title = '## Benefits And Strategic Considerations';
  2314. for (const fence of ['```', '~~~']) {
  2315. assert.equal(
  2316. titleCaseHits([intro, fence, fence + 'js', title, fence].join('\n') + HEADING_BODY).length,
  2317. 0,
  2318. 'trailing text cannot close the outer fence',
  2319. );
  2320. assert.equal(
  2321. titleCaseHits([intro, fence, 'code', fence + ' \t', title].join('\n') + HEADING_BODY).length,
  2322. 1,
  2323. 'spaces and tabs may follow a closing fence',
  2324. );
  2325. assert.equal(
  2326. titleCaseHits([intro, fence, 'code', fence + '\u00a0', title].join('\n') + HEADING_BODY).length,
  2327. 0,
  2328. 'non-breaking space is fence content, not an allowed closing-fence suffix',
  2329. );
  2330. assert.equal(
  2331. titleCaseHits([intro, fence, 'code', fence, title].join('\r\n') + HEADING_BODY).length,
  2332. 1,
  2333. 'CRLF line endings still allow a closing fence',
  2334. );
  2335. }
  2336. });
  2337. test('#62: MD_HEADING_PREFIX accepts what the pattern accepts', () => {
  2338. // The two must stay coupled: TITLE_CASE_HEADER matches `#{1,6}[ \t]+`, so if
  2339. // the prefix strip stops accepting a tab, `##` survives into the token list
  2340. // and reintroduces the ##-as-token bug this fix exists for.
  2341. //
  2342. // The probe has to open with a function word. On a heading whose function
  2343. // word is interior, an unstripped `##` only pushes the token COUNT up and the
  2344. // result is unchanged — mutation testing caught that a firing fixture here
  2345. // passes either way. Here the unstripped `##` shifts what slice(1) sees onto
  2346. // the leading "The", which flips the verdict.
  2347. assert.equal(
  2348. titleCaseHits('##\tThe New Security Landscape' + HEADING_BODY).length,
  2349. 0,
  2350. 'a tab-separated heading must strip like a space-separated one',
  2351. );
  2352. assert.equal(
  2353. titleCaseHits('##\tBenefits And Strategic Considerations' + HEADING_BODY).length,
  2354. 1,
  2355. 'and must still flag the real tell',
  2356. );
  2357. });
  2358. test('#62: an indented line is not a Markdown heading', () => {
  2359. // Kills the `#{0,6}` mutant: making the hash count optional turns any indented
  2360. // line into a heading, so four-space code blocks would flag.
  2361. assert.equal(
  2362. titleCaseHits(' Benefits And Strategic Considerations' + HEADING_BODY).length,
  2363. 0,
  2364. 'an indented line must not flag',
  2365. );
  2366. });
  2367. test('#62: the reported text is value-asserted, not merely present', () => {
  2368. // The whole change alters what match[0] contains, so assert the value. A
  2369. // presence-only check is what let the `##`-as-token defect through.
  2370. const hits = titleCaseHits('## Benefits And Strategic Considerations' + HEADING_BODY);
  2371. assert.equal(hits.length, 1);
  2372. assert.equal(hits[0].text.trim(), '## Benefits And Strategic Considerations');
  2373. });
  2374. test('v2: markdown **bold** is preserved by normalize pre-pass', () => {
  2375. // Regression: lookbehind/lookahead added in review fix. The pre-fix
  2376. // regex stripped the inner half of `**bold**` and counted each as
  2377. // a roleplay marker, false-positiving normalization-flag on any
  2378. // README / Substack post with bold runs.
  2379. const text = '**First bold** and **another bold** plus **a third one**.';
  2380. const norm = AIDetector.normalizeText(text);
  2381. assert.equal(norm.flags.roleplay, 0, `expected roleplay=0 on markdown bold, got ${norm.flags.roleplay}`);
  2382. assert.ok(norm.text.includes('**First bold**'), 'bold marker preserved');
  2383. });
  2384. test('v2: punct-distribution fires on uniform per-paragraph density', () => {
  2385. // Four paragraphs, each ~30 words, each with the same number of
  2386. // commas. Uniform punctuation density across paragraphs is the AI
  2387. // signature this rule catches.
  2388. const text = [
  2389. 'The protocol design centers on three core principles, including modularity, composability, and forward compatibility, which together enable predictable behavior across many environments and deployment topologies.',
  2390. 'Implementation choices reflect a deliberate preference for simplicity, including small interfaces, narrow contracts, and explicit invariants, which together make the codebase tractable for new contributors and reviewers.',
  2391. 'Testing strategy emphasizes property-based coverage, including invariants, contract tests, and regression fixtures, which together guard against silent behavior changes in performance-critical paths across releases.',
  2392. 'Documentation follows a layered approach, including conceptual overviews, narrative guides, and reference material, which together orient readers without forcing them through any single rigid sequence of pages.',
  2393. ].join('\n\n');
  2394. const r = AIDetector.analyzeText(text);
  2395. const types = new Set(r.issues.map((i) => i.type));
  2396. assert.ok(types.has('punct-distribution'), 'expected punct-distribution flag on uniform density');
  2397. });
  2398. test('v2: cross-para-burstiness fires on uniform sentence rhythm', () => {
  2399. // Four paragraphs, each with three sentences of similar length.
  2400. // Std-of-CV across paragraphs is low → flag fires.
  2401. const text = [
  2402. 'The system processes events synchronously. Each event triggers a downstream handler. The handler updates state immediately.',
  2403. 'The database uses optimistic locking. Concurrent writes retry transparently. The retry budget allows three attempts.',
  2404. 'Authentication relies on signed tokens. Tokens expire after fifteen minutes. Refresh requests issue new tokens.',
  2405. 'Logging captures every state change. The pipeline routes logs centrally. Storage retains entries for ninety days.',
  2406. ].join('\n\n');
  2407. const r = AIDetector.analyzeText(text);
  2408. const types = new Set(r.issues.map((i) => i.type));
  2409. assert.ok(types.has('cross-para-burstiness'), 'expected cross-para-burstiness on uniform rhythm');
  2410. });
  2411. test('v2: invalid contextMode falls back to general with stats.contextModeFallback set', () => {
  2412. const text = 'Strategic Negotiations And Key Partnerships\n\nThe team closed three deals. Each agreement included revenue-share terms. Legal review took two weeks per contract.';
  2413. const r = AIDetector.analyzeText(text, { contextMode: 'tecnical' });
  2414. assert.equal(r.stats.contextMode, 'general', 'invalid mode coerced to general');
  2415. assert.equal(r.stats.contextModeFallback, 'tecnical', 'fallback echoes original');
  2416. });
  2417. test('#234: falsy invalid contextMode is preserved in fallback stats', () => {
  2418. const r = AIDetector.analyzeText('', { contextMode: '' });
  2419. assert.equal(r.stats.contextMode, 'general');
  2420. assert.equal(r.stats.contextModeFallback, '');
  2421. });
  2422. test('v2: trinary fields present on tooShort / tooLong / empty as UNSCORED', () => {
  2423. // Early-exit paths return UNSCORED (not HUMAN_ONLY) so a caller can't
  2424. // mistake a refused scan for a confident human verdict. A 50k-word
  2425. // LLM-generated document falling into tooLong is not "human."
  2426. const empty = AIDetector.analyzeText('');
  2427. const tooShort = AIDetector.analyzeText('Short text.');
  2428. const tooLong = AIDetector.analyzeText('word '.repeat(10001));
  2429. for (const [name, r] of [['empty', empty], ['tooShort', tooShort], ['tooLong', tooLong]]) {
  2430. assert.equal(r.document_classification, 'UNSCORED', `${name}: expected UNSCORED, got ${r.document_classification}`);
  2431. assert.equal(r.confidence_category, 'low', `${name}: expected low confidence`);
  2432. assert.ok(r.class_probabilities, `${name}: missing class_probabilities`);
  2433. assert.ok(Array.isArray(r.highlight_sentence_for_ai), `${name}: missing highlight array`);
  2434. }
  2435. // Empty case has empty stats so contextMode field absent is fine;
  2436. // tooShort/tooLong should surface contextMode for traceability.
  2437. assert.equal(tooShort.stats.contextMode, 'general', 'tooShort stats includes contextMode');
  2438. assert.equal(tooLong.stats.contextMode, 'general', 'tooLong stats includes contextMode');
  2439. });
  2440. test('v2: probability fields sum to exactly 1.000 (no float drift)', () => {
  2441. const texts = [
  2442. 'In the rapidly evolving world of decentralized finance, we delve into the intricate tapestry of innovation. This seamless, robust paradigm showcases a comprehensive framework that catalyzes transformative change across the ecosystem.',
  2443. 'The build broke. Rolled back. Tests pass. Will investigate the root cause tomorrow afternoon after the standup with the on-call engineer.',
  2444. 'A neutral middle paragraph that mixes some flagged words like robust and comprehensive but in normal context, leveraging some technical terms in the way a real engineer might describe their implementation choices over coffee.',
  2445. ];
  2446. for (const t of texts) {
  2447. const r = AIDetector.analyzeText(t);
  2448. const sum = r.class_probabilities.human + r.class_probabilities.mixed + r.class_probabilities.ai;
  2449. assert.ok(Math.abs(sum - 1) < 0.0005, `probabilities should sum to exactly 1.000, got ${sum} for: ${t.slice(0, 40)}...`);
  2450. }
  2451. });
  2452. test('v2: mid-score isolated stylometric hits do not reach AI_ONLY', () => {
  2453. // Pins the FN-bias contract with fail-loud preconditions. If the corpus
  2454. // drifts and the preconditions break, the test fails on the precondition
  2455. // assertion (not silently passes). Text designed to have NO strong
  2456. // corroborators: no cutoff disclaimer, no chatbot artifact, no homoglyph,
  2457. // no dense-vocab trifecta. Should classify HUMAN_ONLY or MIXED.
  2458. const text = 'The team continues making progress on the platform. The framework supports many needs. Building collaboration across teams stays important. Improving the deployment path is a goal. The setup gives everyone a foundation.';
  2459. const r = AIDetector.analyzeText(text);
  2460. const hasCutoff = r.issues.some((i) => i.type === 'cutoff-disclaimer');
  2461. const hasReasonChat = r.issues.some((i) => i.type === 'reasoning-artifact') && r.issues.some((i) => i.type === 'chatbot');
  2462. const hasNorm = r.issues.some((i) => i.type === 'normalization-flag');
  2463. // Preconditions: assert the test corpus matches the no-strong-corroborator
  2464. // shape. If these fail, the corpus drifted and the test is meaningless.
  2465. assert.ok(!hasCutoff, 'precondition: corpus should not trigger cutoff-disclaimer');
  2466. assert.ok(!hasReasonChat, 'precondition: corpus should not trigger reasoning+chatbot');
  2467. assert.ok(!hasNorm, 'precondition: corpus should not trigger normalization-flag');
  2468. assert.ok(r.score < 70, `precondition: corpus score should be < 70, got ${r.score}`);
  2469. // Contract: without strong corroborators and below the score-only threshold,
  2470. // never AI_ONLY.
  2471. assert.notEqual(r.document_classification, 'AI_ONLY', `no-strong-corroborator below score 70 should not be AI_ONLY, got ${r.document_classification} at score ${r.score}`);
  2472. });
  2473. test('v2: humanizer bypass escalates to AI_ONLY (normalization-flag corroborator)', () => {
  2474. const zwsp = '​';
  2475. const text = `In tоday's landscape we del${zwsp}ve into the intricate tap${zwsp}estry of innovátion. This seamless, robust paradigm showcases comprehensive frameworks. The framework underscores how organizations harness cutting-edge tools to navigate complexities across the ecosystem.`;
  2476. const r = AIDetector.analyzeText(text);
  2477. assert.equal(r.document_classification, 'AI_ONLY', `bypass should reach AI_ONLY, got ${r.document_classification}`);
  2478. assert.ok(['medium', 'high'].includes(r.confidence_category), `bypass should not be low-confidence, got ${r.confidence_category}`);
  2479. });
  2480. test('v2: canonical saturated-AI essay reaches AI_ONLY (calibration regression)', () => {
  2481. // Regression for review finding: pre-recalibration this text scored
  2482. // ~47/MIXED. AI_ONLY was effectively dead code. Threshold now lets
  2483. // a saturated essay actually fire.
  2484. const text = 'In today\'s rapidly evolving landscape, we delve into the intricate tapestry of decentralized finance. It is important to note that this seamless, robust paradigm showcases a comprehensive framework. Moreover, this transformative ecosystem leverages cutting-edge protocols to navigate the complex multifaceted challenges of modern finance. Furthermore, the integration of innovative solutions underscores how pivotal this moment is. The future looks bright for those who embrace these emerging opportunities. By harnessing the power of blockchain technology, organizations can foster unprecedented growth and catalyze meaningful change across the ecosystem.';
  2485. const r = AIDetector.analyzeText(text);
  2486. assert.equal(r.document_classification, 'AI_ONLY', `saturated essay should AI_ONLY, got ${r.document_classification} at score ${r.score}`);
  2487. });
  2488. test('v2: legitimate *italic phrase* is NOT stripped by roleplay rule', () => {
  2489. // Round-2 fix: roleplay regex now requires an action-verb prefix
  2490. // (nods/sighs/laughs/etc.). Markdown italic with arbitrary multi-word
  2491. // content like *italic phrase here* should survive untouched.
  2492. const text = 'We use *italic phrase here* for emphasis and *another phrase too* in some places.';
  2493. const norm = AIDetector.normalizeText(text);
  2494. assert.equal(norm.flags.roleplay, 0, `expected roleplay=0 on plain italic, got ${norm.flags.roleplay}`);
  2495. assert.ok(norm.text.includes('*italic phrase here*'), 'italic preserved');
  2496. });
  2497. test('v2: *roleplay action verb* IS stripped', () => {
  2498. // The actual chat-model artifact — verb-led action description.
  2499. const text = 'I think about the problem *nods thoughtfully* and consider the options *sighs deeply* before answering.';
  2500. const norm = AIDetector.normalizeText(text);
  2501. assert.ok(norm.flags.roleplay >= 2, `expected ≥2 roleplay strips, got ${norm.flags.roleplay}`);
  2502. });
  2503. test('v2: single ZWSP does not flip to AI_ONLY (hair-trigger fix)', () => {
  2504. // Common in copy-paste from Word/Notion/Slack-rendered text. Round-1
  2505. // made single ZWSP a strong corroborator → AI_ONLY at score 0. Round-2
  2506. // raised the threshold to ≥2 for parity with homoglyph.
  2507. const zwsp = '​';
  2508. const text = `Our team shipped a fix on Monday${zwsp} afternoon. Tests pass and the deploy is green. Everything looks good. Plain human text with one accidental zero-width character pasted from a Notion doc.`;
  2509. const r = AIDetector.analyzeText(text);
  2510. assert.notEqual(r.document_classification, 'AI_ONLY', `single ZWSP should not flip to AI_ONLY, got ${r.document_classification}`);
  2511. });
  2512. test('v2: dense-AI-vocab trifecta reaches AI_ONLY (calibration regression)', () => {
  2513. // Saturated ChatGPT prose without cutoff/chatbot/normalization should
  2514. // still reach AI_ONLY via the dense-AI-vocab strong corroborator
  2515. // (≥4 tier1 distinct + tier2 cluster + transition). Round-1 left this
  2516. // class of essay stuck at MIXED.
  2517. const text = 'In the rapidly evolving world of decentralized finance, organizations leverage robust and comprehensive frameworks. Moreover, this seamless paradigm enables them to navigate the intricate tapestry of modern challenges. Furthermore, they harness cutting-edge tools to foster sustainable growth and catalyze transformative change. Additionally, the platform showcases meticulous attention to user experience across the ecosystem.';
  2518. const r = AIDetector.analyzeText(text);
  2519. assert.equal(r.document_classification, 'AI_ONLY', `dense AI vocab should AI_ONLY, got ${r.document_classification} at score ${r.score}`);
  2520. });
  2521. test('v2: blockquoted AI text does not penalize the human wrapper', () => {
  2522. // A human reacting to AI text by quoting it shouldn't have the quoted
  2523. // block scored against their own prose. The `> ` lines get stripped
  2524. // in a pre-pass and the count surfaces in stats.quotedLines.
  2525. const text = [
  2526. 'I asked ChatGPT to describe my project and got this response:',
  2527. '',
  2528. '> In the rapidly evolving world of decentralized finance, we delve into the intricate tapestry of innovation.',
  2529. '> This seamless, robust paradigm showcases a comprehensive framework that catalyzes transformative change.',
  2530. '> Moreover, this represents a pivotal moment in the ecosystem.',
  2531. '',
  2532. 'The response was pretty bad. I rewrote it as a normal sentence about what we actually do.',
  2533. ].join('\n');
  2534. const r = AIDetector.analyzeText(text);
  2535. assert.ok(r.stats.quotedLines >= 3, `expected quotedLines >= 3, got ${r.stats.quotedLines}`);
  2536. assert.notEqual(r.document_classification, 'AI_ONLY', `human wrapping AI quote should not classify AI_ONLY, got ${r.document_classification}`);
  2537. });
  2538. test('v2: probability sum is exactly 1 with no negative components', () => {
  2539. // Round-2 fix: clamp p.ai to >= 0 after the remainder calculation
  2540. // since toFixed(3) rounding can push human+mixed slightly above 1.
  2541. const texts = [
  2542. 'In the rapidly evolving world of decentralized finance, we delve into the intricate tapestry of innovation. This seamless, robust paradigm showcases a comprehensive framework that catalyzes transformative change across the ecosystem. Furthermore, this pivotal moment marks a fundamental shift.',
  2543. 'The build broke. Rolled back. Tests pass.',
  2544. 'A neutral middle paragraph that mixes some words like robust and comprehensive in normal context, leveraging technical terms the way a real engineer might describe their implementation choices over coffee with a teammate.',
  2545. '',
  2546. 'word '.repeat(11000),
  2547. ];
  2548. for (const t of texts) {
  2549. const r = AIDetector.analyzeText(t);
  2550. const { human, mixed, ai } = r.class_probabilities;
  2551. assert.ok(human >= 0, `human prob negative: ${human}`);
  2552. assert.ok(mixed >= 0, `mixed prob negative: ${mixed}`);
  2553. assert.ok(ai >= 0, `ai prob negative: ${ai}`);
  2554. const sum = human + mixed + ai;
  2555. assert.ok(Math.abs(sum - 1) < 0.002, `sum should be ~1, got ${sum} for: ${(t || '<empty>').slice(0, 40)}`);
  2556. }
  2557. });
  2558. test('v2: unmappedHighlights counter surfaced in stats', () => {
  2559. const r = AIDetector.analyzeText('We delve into the landscape of innovation and continue to navigate the comprehensive transformation.');
  2560. assert.equal(typeof r.stats.unmappedHighlights, 'number', 'unmappedHighlights should be numeric');
  2561. });
  2562. test('v2: real Rust technical post does NOT classify AI_ONLY (denseAIVocab FP fix)', () => {
  2563. // Round-3 regression: pre-fix this scored AI_ONLY at 30 because
  2564. // denseAIVocab required only 4 tier1 + 1 tier2 cluster + transition,
  2565. // which legitimate dense-jargon technical writing trips. Threshold
  2566. // raised to 5 tier1 + 2 tier2 clusters + 150-word gate.
  2567. const text = 'Rust offers a robust and comprehensive approach to systems programming. Engineers leverage zero-cost abstractions to navigate intricate memory hierarchies without runtime overhead. The borrow checker provides meticulous compile-time guarantees that catch entire categories of bugs. Furthermore, the type system encourages a holistic approach to API design where contracts are explicit. The ecosystem around cargo, crates.io, and the Rust toolchain has matured significantly over the past five years, with libraries spanning embedded systems, web servers, and game engines.';
  2568. const r = AIDetector.analyzeText(text);
  2569. assert.notEqual(r.document_classification, 'AI_ONLY', `Rust tech post should not classify AI_ONLY, got ${r.document_classification} at score ${r.score}`);
  2570. });
  2571. test('v2: canonical "As an AI language model" disclaimer fires cutoff-disclaimer + AI_ONLY', () => {
  2572. // Round-3 finding: this canonical LLM self-id phrase was missing
  2573. // entirely from CUTOFF_DISCLAIMERS.
  2574. const text = 'As an AI language model, I cannot provide legal advice on this matter. However, I can suggest you consult a licensed attorney. The general principle is that contract law varies by jurisdiction and specific facts matter.';
  2575. const r = AIDetector.analyzeText(text);
  2576. const types = new Set(r.issues.map((i) => i.type));
  2577. assert.ok(types.has('cutoff-disclaimer'), 'expected cutoff-disclaimer flag on AI language model self-id');
  2578. assert.equal(r.document_classification, 'AI_ONLY', `expected AI_ONLY on canonical disclaimer, got ${r.document_classification}`);
  2579. assert.equal(r.confidence_category, 'high', `expected high confidence on canonical disclaimer, got ${r.confidence_category}`);
  2580. });
  2581. test('#238: single-line blockquote is masked like a multi-line one', () => {
  2582. const quoted = 'I asked the model about our roadmap and it said:\n\n> We must delve into the landscape and leverage our synergy.\n\nThen I closed the tab and wrote the plan myself.';
  2583. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2584. const r = AIDetector.analyzeText(quoted, { sourceMode });
  2585. assert.equal(r.stats.quotedLines, 1, sourceMode);
  2586. assert.deepEqual(r.issues.filter((i) => i.type === 'tier1'), [], sourceMode);
  2587. }
  2588. });
  2589. test('#238: double-quoted spans do not score, the same words outside do', () => {
  2590. const inside = 'She told me, "We must delve into the landscape and leverage our synergy," and then she laughed at her own words for a while.';
  2591. const curly = 'She told me, “We must delve into the landscape and leverage our synergy,” and then she laughed at her own words for a while.';
  2592. const outside = 'She told me we must delve into the landscape and leverage our synergy, and then she laughed at her own words for a while.';
  2593. for (const text of [inside, curly]) {
  2594. const r = AIDetector.analyzeText(text);
  2595. assert.equal(r.stats.maskedQuotes, 1);
  2596. assert.deepEqual(r.issues.filter((i) => i.type === 'tier1'), []);
  2597. }
  2598. const two = AIDetector.analyzeText('He said "delve into it" and she said “leverage the synergy” before we left the office for the long weekend.');
  2599. assert.equal(two.stats.maskedQuotes, 2);
  2600. assert.deepEqual(two.issues.filter((i) => i.type === 'tier1'), []);
  2601. const r = AIDetector.analyzeText(outside);
  2602. assert.equal(r.stats.maskedQuotes, 0);
  2603. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'));
  2604. });
  2605. test('#238: inch marks, empty quotes and code quotes do not hide prose', () => {
  2606. const cases = [
  2607. 'A 15" laptop works well, but we must delve into the landscape before buying the 24" monitor for the office.',
  2608. 'She set opt="" and said we must delve into the landscape before "leaving" the office for the day.',
  2609. 'Run `grep -r "test` or review how we delve into the landscape before the "audit" phase starts next week.',
  2610. 'The symbol “ marks a quote, but we must delve into the landscape before “launching” the product next week.',
  2611. ];
  2612. for (const text of cases) {
  2613. const r = AIDetector.analyzeText(text);
  2614. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'), text);
  2615. }
  2616. });
  2617. test('#238: comparison lines, CR-only text and early-exit stats', () => {
  2618. // A line opening with `>=` is prose, not a blockquote.
  2619. const comparison = 'Requirements:\n\n>=5 items must be in stock before we ship, and the warehouse team must delve into the backlog this quarter.\n\nThat is the policy.';
  2620. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2621. const r = AIDetector.analyzeText(comparison, { sourceMode });
  2622. assert.equal(r.stats.quotedLines, 0, sourceMode);
  2623. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'), sourceMode);
  2624. }
  2625. // CR-only line endings: the quote must not take the later prose with it.
  2626. const crOnly = '> We quoted this line from the model.\rLater we delve into the landscape of our own codebase and leverage what the team built.';
  2627. const cr = AIDetector.analyzeText(crOnly);
  2628. assert.ok(cr.issues.some((i) => i.type === 'tier1' && i.text === 'delve'));
  2629. const short = AIDetector.analyzeText('She said "we must delve into the landscape and leverage synergy" today.');
  2630. assert.equal(short.label, 'Too short');
  2631. assert.equal(short.stats.maskedQuotes, 1);
  2632. assert.equal(short.stats.quotedLines, 0);
  2633. });
  2634. test('#238: apostrophes are not treated as quotes', () => {
  2635. const text = "It's the team's plan, and we don't want to delve into someone's rules today, so let's go and ship what's ready.";
  2636. const r = AIDetector.analyzeText(text);
  2637. assert.equal(r.stats.maskedQuotes, 0);
  2638. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'));
  2639. });
  2640. test('#238: issue offsets still address the source after quote masking', () => {
  2641. const source = 'He wrote "it is important to note that we delve" and moved on. It is important to note that the system works well and the team shipped it on time.';
  2642. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2643. const result = AIDetector.analyzeText(source, { sourceMode });
  2644. assert.equal(result.stats.maskedQuotes, 1, sourceMode);
  2645. const fillers = result.issues.filter((i) => i.type === 'filler');
  2646. assert.deepEqual(fillers.map((i) => i.index), [source.lastIndexOf('It is important')], sourceMode);
  2647. assertIndexedIssuesSliceExactly(source, result.issues, `quoted span ${sourceMode}`);
  2648. }
  2649. });
  2650. test('#238: CR-only blockquote lines are masked in both modes with source offsets', () => {
  2651. const quote = '> We must delve into the landscape and leverage our synergy.';
  2652. const prose = 'It is important to note that the team wrote a careful and ordinary response.';
  2653. const cases = [
  2654. `${quote}\r${prose}`,
  2655. `Then I wrote a careful and ordinary reply.\r${quote}\r${prose}`,
  2656. `\r${quote}\r${prose}`,
  2657. ];
  2658. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2659. for (const source of cases) {
  2660. const result = AIDetector.analyzeText(source, { sourceMode });
  2661. assert.equal(result.stats.quotedLines, 1, sourceMode);
  2662. assert.deepEqual(result.issues.filter((i) => i.type === 'tier1'), [], sourceMode);
  2663. const fillers = result.issues.filter((i) => i.type === 'filler');
  2664. assert.deepEqual(fillers.map((i) => i.index), [source.indexOf('It is important')], sourceMode);
  2665. assertIndexedIssuesSliceExactly(source, result.issues, `CR-only blockquote ${sourceMode}`);
  2666. }
  2667. }
  2668. });
  2669. test('#238: escaped quotes stay inside the quoted span', () => {
  2670. const source = 'We delve first. She said "We must \\"leverage\\" the landscape and our synergy" and then the landscape shifted for real.';
  2671. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2672. const result = AIDetector.analyzeText(source, { sourceMode });
  2673. assert.equal(result.stats.maskedQuotes, 1, sourceMode);
  2674. const tier1 = result.issues.filter((i) => i.type === 'tier1').map((i) => i.text).sort();
  2675. assert.deepEqual(tier1, ['delve', 'landscape'], sourceMode);
  2676. assertIndexedIssuesSliceExactly(source, result.issues, `escaped quote ${sourceMode}`);
  2677. }
  2678. const path = AIDetector.analyzeText('He said, "save the landscape file at C:\\Temp\\" before leaving the office for the long weekend.');
  2679. assert.equal(path.stats.maskedQuotes, 1);
  2680. assert.deepEqual(path.issues.filter((i) => i.type === 'tier1'), []);
  2681. const pathThenQuote = AIDetector.analyzeText('Run "C:\\Temp\\" and then delve into "--flag" before the team ships the release next week.');
  2682. assert.equal(pathThenQuote.stats.maskedQuotes, 2);
  2683. assert.ok(pathThenQuote.issues.some((i) => i.type === 'tier1' && i.text === 'delve'));
  2684. const sentence = 'We must delve into the landscape and leverage synergy today. ';
  2685. const chained = AIDetector.analyzeText(`He explained: "${`\\"${sentence}\\"`.repeat(10)}" and that was all.`);
  2686. assert.equal(chained.stats.maskedQuotes, 0);
  2687. assert.ok(chained.issues.some((i) => i.type === 'tier1' && i.text === 'delve'));
  2688. const emoji = AIDetector.analyzeText(`She wrote: "we must delve into the landscape ${'\u{1F600}'.repeat(230)}" in her message, nothing else notable.`);
  2689. assert.equal(emoji.stats.maskedQuotes, 1);
  2690. assert.deepEqual(emoji.issues.filter((i) => i.type === 'tier1'), []);
  2691. });
  2692. test('#238: compact blockquotes without a space are masked, comparisons are not', () => {
  2693. const prose = 'It is important to note that the team wrote a careful and ordinary response.';
  2694. const cases = [
  2695. `>We must delve into the landscape and leverage our synergy.\n${prose}`,
  2696. `Then I wrote a careful and ordinary reply.\r>We must delve into the landscape and leverage our synergy.\r${prose}`,
  2697. `>*We must delve into the landscape and leverage our synergy.*\n${prose}`,
  2698. `>[We must delve](https://example.com) into the landscape and leverage our synergy.\n${prose}`,
  2699. `>>We must delve into the landscape and leverage our synergy.\n${prose}`,
  2700. `>> We must delve into the landscape and leverage our synergy.\n${prose}`,
  2701. `>- We must delve into the landscape and leverage our synergy.\n${prose}`,
  2702. `>+ We must delve into the landscape and leverage our synergy.\n${prose}`,
  2703. `>* We must delve into the landscape and leverage our synergy.\n${prose}`,
  2704. `>10. We must delve into the landscape and leverage our synergy.\n${prose}`,
  2705. `>1. We must delve into the landscape and leverage our synergy.\n${prose}`,
  2706. `>2) We must delve into the landscape and leverage our synergy.\n${prose}`,
  2707. ];
  2708. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2709. for (const source of cases) {
  2710. const result = AIDetector.analyzeText(source, { sourceMode });
  2711. assert.equal(result.stats.quotedLines, 1, sourceMode);
  2712. assert.deepEqual(result.issues.filter((i) => i.type === 'tier1'), [], sourceMode);
  2713. const fillers = result.issues.filter((i) => i.type === 'filler');
  2714. assert.deepEqual(fillers.map((i) => i.index), [source.indexOf('It is important')], sourceMode);
  2715. assertIndexedIssuesSliceExactly(source, result.issues, `compact blockquote ${sourceMode}`);
  2716. }
  2717. for (const line of ['>=5 items', '>5 items', '>-1 items', '>1.5 items', '>2024 items', ">'cause the items"]) {
  2718. const r = AIDetector.analyzeText(`${line} must be in stock before we ship, and the warehouse team must delve into the backlog this quarter.`, { sourceMode });
  2719. assert.equal(r.stats.quotedLines, 0, `${line} ${sourceMode}`);
  2720. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'), `${line} ${sourceMode}`);
  2721. }
  2722. }
  2723. });
  2724. test('#238: bypass characters inside quotes raise no normalization flag', () => {
  2725. const zeroWidth = 'we must del​ve into the lands​cape';
  2726. const homoglyph = 'we must dеlvе into the lаndscаpe';
  2727. const roleplay = '*nods* we must delve into it *sighs*';
  2728. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2729. for (const span of [zeroWidth, homoglyph, roleplay]) {
  2730. const source = `She told me, "${span}," and then she laughed at her own words for a while before we left.`;
  2731. const quoted = AIDetector.analyzeText(source, { sourceMode });
  2732. assert.equal(quoted.stats.maskedQuotes, 1, `${span} ${sourceMode}`);
  2733. assert.deepEqual(quoted.issues.filter((i) => i.type === 'normalization-flag' || i.type === 'tier1'), [], `${span} ${sourceMode}`);
  2734. assertIndexedIssuesSliceExactly(source, quoted.issues, `quoted bypass ${sourceMode}`);
  2735. const bare = AIDetector.analyzeText(source.replace(/"/g, ''), { sourceMode });
  2736. assert.ok(bare.issues.some((i) => i.type === 'normalization-flag'), `${span} ${sourceMode}`);
  2737. }
  2738. }
  2739. });
  2740. test('v2: stats.denseAIVocab and stats.tier1Distinct surface for observability', () => {
  2741. const r = AIDetector.analyzeText('We delve into the landscape with robust comprehensive seamless innovative cutting-edge solutions.');
  2742. assert.equal(typeof r.stats.denseAIVocab, 'boolean', 'denseAIVocab should be boolean');
  2743. assert.equal(typeof r.stats.tier1Distinct, 'number', 'tier1Distinct should be number');
  2744. });
  2745. test('v2: backward compat — score, label, issues, stats still present', () => {
  2746. const r = AIDetector.analyzeText('We delve into the landscape of leveraging robust paradigms. The team continues to navigate this comprehensive transformation.');
  2747. assert.ok(typeof r.score === 'number', 'score still numeric');
  2748. assert.ok(typeof r.label === 'string', 'label still string');
  2749. assert.ok(Array.isArray(r.issues), 'issues still array');
  2750. assert.ok(r.stats && typeof r.stats === 'object', 'stats still object');
  2751. });
  2752. test('#109: Object.prototype names in prose do not fire tier lookups', () => {
  2753. // Plain-object membership tests made TIER1['constructor'] resolve to
  2754. // Object.prototype.constructor (truthy), flagging ordinary prose.
  2755. const text =
  2756. 'The class constructor takes nine arguments in this codebase. Review the constructor before calling it, and check that its prototype chain and toString output match what the documentation describes for each valueOf call.';
  2757. const r = AIDetector.analyzeText(text);
  2758. const protoHits = r.issues.filter(
  2759. (i) => ['constructor', 'prototype', 'tostring', 'valueof', 'hasownproperty'].includes(String(i.text).toLowerCase())
  2760. );
  2761. assert.equal(protoHits.length, 0, `prototype-name tokens flagged: ${JSON.stringify(protoHits)}`);
  2762. });
  2763. test('#109 complement: real tier1 vocabulary still fires after the hasOwn guard', () => {
  2764. const r = AIDetector.analyzeText(
  2765. 'We delve into the constructor design of this system. The team continues to navigate this comprehensive transformation across every module boundary.'
  2766. );
  2767. const tier1Texts = r.issues.filter((i) => i.type === 'tier1').map((i) => String(i.text).toLowerCase());
  2768. assert.ok(tier1Texts.includes('delve'), `expected 'delve' to still fire, got tier1=${JSON.stringify(tier1Texts)}`);
  2769. assert.ok(!tier1Texts.includes('constructor'), 'constructor must not ride along in tier1');
  2770. });
  2771. test('performed-insight: essayist tics fire', () => {
  2772. const r = AIDetector.analyzeText(
  2773. "Turns out the pricing was never the obstacle for any of the customers. That's not nothing. Sit with that for a moment before the next planning meeting, and remember that distribution is the whole game."
  2774. );
  2775. const hits = r.issues.filter((i) => i.type === 'performed-insight').map((i) => i.text);
  2776. assert.ok(hits.length >= 3, `expected >=3 performed-insight hits, got ${JSON.stringify(hits)}`);
  2777. });
  2778. test('performed-insight: staged discovery fires once per phrase', () => {
  2779. const r = AIDetector.analyzeText(
  2780. "I have the device on my desk, and the recording turned out to be the least interesting part. The real story was the queue of work it created."
  2781. );
  2782. const hits = r.issues.filter((i) => i.type === 'performed-insight').map((i) => i.text.toLowerCase());
  2783. assert.deepEqual(hits, ['turned out to be the least interesting part', 'the real story was']);
  2784. });
  2785. test('performed-insight: sentence-initial Turns out is subsumed by staged discovery', () => {
  2786. const r = AIDetector.analyzeText(
  2787. 'Turns out to be the most interesting part was the detour, according to the post.'
  2788. );
  2789. const hits = r.issues.filter((i) => i.type === 'performed-insight').map((i) => i.text.toLowerCase());
  2790. assert.deepEqual(hits, ['turns out to be the most interesting part']);
  2791. });
  2792. test('performed-insight: nested emotional flatline counts once', () => {
  2793. const r = AIDetector.analyzeText(
  2794. 'The recording turned out to be the most interesting part. The most interesting thing was the battery life.'
  2795. );
  2796. const performed = r.issues.filter((i) => i.type === 'performed-insight');
  2797. const flatline = r.issues.filter((i) => i.type === 'emotional-flatline');
  2798. assert.deepEqual(performed.map((i) => i.text.toLowerCase()), ['turned out to be the most interesting part']);
  2799. assert.deepEqual(flatline.map((i) => i.text.toLowerCase()), ['the most interesting thing']);
  2800. assert.equal(r.stats.patternCount, r.issues.length);
  2801. });
  2802. test('performed-insight: older phrases preserve their nested flatline finding', () => {
  2803. const r = AIDetector.analyzeText(
  2804. 'That is why the most interesting part mattered to the team during the launch.'
  2805. );
  2806. const types = r.issues.map((i) => i.type);
  2807. assert.ok(types.includes('performed-insight'));
  2808. assert.ok(types.includes('emotional-flatline'));
  2809. });
  2810. test('performed-insight: literal turned-out and story uses stay clean', () => {
  2811. const r = AIDetector.analyzeText(
  2812. "The cheaper vendor turned out to be the most expensive option once support was priced in. The real story was covered by two local papers, and the real estate market cooled that spring."
  2813. );
  2814. const hits = r.issues.filter((i) => i.type === 'performed-insight');
  2815. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2816. });
  2817. test('performed-insight: ordinary uses do not fire', () => {
  2818. const r = AIDetector.analyzeText(
  2819. "She sat with him through the appointment and the long drive home afterward. The whole family gathered for the reunion photos on Saturday. Naming names in the report was the part of the job he liked least of all."
  2820. );
  2821. const hits = r.issues.filter((i) => i.type === 'performed-insight');
  2822. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2823. });
  2824. test('performed-insight: determiner "sit with that <noun>" and literal "only X I trust" stay clean', () => {
  2825. const r = AIDetector.analyzeText(
  2826. "Please sit with that decision overnight before you call the attorney tomorrow morning. This is the only doctor I trust with a procedure this complicated, and the referral took months to arrange."
  2827. );
  2828. const hits = r.issues.filter((i) => i.type === 'performed-insight');
  2829. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2830. });
  2831. test('performed-insight: literal punchline and naming senses stay clean', () => {
  2832. const r = AIDetector.analyzeText(
  2833. "The comedian rewrote the punchline: the timing was off and the audience missed the joke completely. The storm was worth naming, the meteorologists agreed after reviewing damage reports from every coastal town."
  2834. );
  2835. const hits = r.issues.filter((i) => i.type === 'performed-insight');
  2836. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2837. });
  2838. test('performed-insight: dead/long-live titles and literal announcements need judgment', () => {
  2839. const samples = [
  2840. '# Software Engineering is Dead. Long Live Software Engineering\n\nThe essay compares two definitions of the job and explains which tasks remain.',
  2841. 'The king is dead; long live the king. The herald read the succession announcement outside the palace.',
  2842. 'The old format is dead — long live the new format. The archive retains both versions for readers.',
  2843. ];
  2844. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2845. for (const sample of samples) {
  2846. const r = AIDetector.analyzeText(sample, { sourceMode });
  2847. assert.ok(r.stats.wordCount >= 10, 'exercise detection rather than the too-short return');
  2848. assert.deepEqual(r.issues.filter(i => i.type === 'performed-insight'), [], `${sourceMode}: ${sample}`);
  2849. }
  2850. const r = AIDetector.analyzeText(samples[0] + '\n\nSit with that for a moment. The files remain in the archive.', { sourceMode });
  2851. assert.ok(r.issues.some(i => i.type === 'performed-insight' && /sit with that/i.test(i.text)),
  2852. 'an unrelated deterministic phrase must still fire beside the title');
  2853. }
  2854. });
  2855. test('performed-insight: sentence-initial Turns out keeps a clean text and index', () => {
  2856. const r = AIDetector.analyzeText(
  2857. "A stable introduction goes here. Turns out the service was already running on the host, so the whole incident closed within about fifteen minutes."
  2858. );
  2859. const hits = r.issues.filter((i) => i.type === 'performed-insight');
  2860. assert.equal(hits.length, 1, `expected 1 hit, got ${JSON.stringify(hits.map((i) => i.text))}`);
  2861. assert.equal(hits[0].text, 'Turns out', 'boundary punctuation must not be consumed into the issue text');
  2862. assert.equal(hits[0].index, 33, 'index must point at the phrase, not the preceding sentence boundary');
  2863. });
  2864. test('negation-chain: no-chains, didn\'t-chains, and don\'t-verb-it fire', () => {
  2865. const r = AIDetector.analyzeText(
  2866. "No fluff, no filler, no jargon. No padding, no throat-clearing, no detours will survive this editing pass. They did not ask for permission, did not wait for the committee. Don't call it a pivot. Call it a correction, plain and simple, colleagues."
  2867. );
  2868. const hits = r.issues.filter((i) => i.type === 'negation-chain');
  2869. assert.equal(hits.length, 4, `expected 4 negation-chain hits, got ${JSON.stringify(hits.map((i) => i.text))}`);
  2870. assert.equal(hits[0].text, 'No fluff, no filler, no jargon', 'no-chain must stop before trailing prose');
  2871. assert.equal(hits[1].text, 'No padding, no throat-clearing, no detours', 'no-chain must not consume a trailing auxiliary');
  2872. });
  2873. test('negation-chain: idiomatic pairs stay clean', () => {
  2874. const r = AIDetector.analyzeText(
  2875. "No more, no less, no matter what the final contract required from the vendor after the audit."
  2876. );
  2877. const hits = r.issues.filter((i) => i.type === 'negation-chain');
  2878. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2879. });
  2880. test('negation-chain: ordinary narration with restated subjects stays clean', () => {
  2881. const r = AIDetector.analyzeText(
  2882. "I did not sleep well last night. I did not eat breakfast either, so I left home early and caught the first train into the city before sunrise."
  2883. );
  2884. const hits = r.issues.filter((i) => i.type === 'negation-chain');
  2885. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2886. });
  2887. test('negation-chain: mid-sentence technical inventories stay clean', () => {
  2888. const r = AIDetector.analyzeText(
  2889. "The endpoint takes no arguments, no headers, and no body when called in health-check mode. The parser accepts no flags, no options, and no positional parameters in its default configuration."
  2890. );
  2891. const hits = r.issues.filter((i) => i.type === 'negation-chain');
  2892. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2893. });
  2894. test('negation-chain: two-item sentence-initial factual inventories stay clean', () => {
  2895. const r = AIDetector.analyzeText(
  2896. "No tickets, no badges will be issued at the door for the conference this year. No maps, no lists were handed to the interns before the long field exercise began."
  2897. );
  2898. const hits = r.issues.filter((i) => i.type === 'negation-chain');
  2899. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  2900. });
  2901. // ── Issue #351 — negative parallelism, ignore markers, word joiners ────
  2902. const npHits = (text, options) => AIDetector.analyzeText(text, options)
  2903. .issues.filter((i) => i.type === 'negative-parallelism');
  2904. test('negative-parallelism: the #351 repro no longer scores Clean', () => {
  2905. const text = "Our new retrieval layer is fast. It's not just a search index, it's a foundation for trust. The breakthrough was simple: it quietly reshaped how teams actually work.";
  2906. const r = AIDetector.analyzeText(text);
  2907. const hits = r.issues.filter((i) => i.type === 'negative-parallelism');
  2908. assert.deepEqual(hits.map((i) => i.text), ["It's not just a search index, it's"]);
  2909. assert.equal(text.slice(hits[0].index, hits[0].index + hits[0].text.length), hits[0].text);
  2910. assert.ok(r.score > 0, `expected a non-zero score, got ${r.score}`);
  2911. assert.equal(AIDetector.TYPE_LABELS['negative-parallelism'], 'Negative parallelism');
  2912. });
  2913. test('negative-parallelism: a minimizer reveal flags on its own across joiners', () => {
  2914. for (const sentence of [
  2915. // Maintainer ruling on #353 review: the minimizer reveal is the tell even
  2916. // in an everyday sentence.
  2917. "It isn't just raining, it's pouring.",
  2918. "This isn't just about speed — it's about trust.",
  2919. 'The release was not merely a patch; it was a rewrite.',
  2920. 'It’s not simply a cache: it’s the source of truth.',
  2921. "These dashboards aren't simply reports -- they're the operating plan.",
  2922. "The new layer isn't just faster, this is a different way to work.",
  2923. ]) {
  2924. const hits = npHits(`${sentence} The team shipped it on Tuesday after a long review.`);
  2925. assert.equal(hits.length, 1, `${sentence} -> ${JSON.stringify(hits.map((i) => i.text))}`);
  2926. }
  2927. });
  2928. test('negative-parallelism: one plain correction stays clean', () => {
  2929. for (const text of [
  2930. "It isn't raining, it's snowing. We waited at the bus stop for twenty minutes before walking home.",
  2931. "This isn't about money, it's about respect, and the union said so plainly at the meeting.",
  2932. "The new layer isn't just faster. It's a different way to think about retrieval for every team.",
  2933. // Pre-LLM human prose from the control corpus (#351): "not only" is a
  2934. // contrast, not a reveal, so it needs a nearby second frame.
  2935. 'Clean energy is cheaper than ever, and fossil fuels are not only bad for our environment, they’re a losing bet in the long run as well as the short term.',
  2936. ]) {
  2937. assert.deepEqual(npHits(text), [], text);
  2938. }
  2939. });
  2940. test('negative-parallelism: contrasts flag when another frame is within three sentences', () => {
  2941. const plain = npHits("It isn't raining, it's snowing. The problem isn't the bus, it's the schedule. We walked home.");
  2942. assert.deepEqual(plain.map((i) => i.text), ["isn't raining, it's", "isn't the bus, it's"]);
  2943. const split = npHits("It's not just a tool, it's a platform. The rollout isn't just fast. It's invisible to users.");
  2944. assert.equal(split.length, 2, JSON.stringify(split.map((i) => i.text)));
  2945. const gapOfOne = npHits("The layer isn't a cache, it's a store. We measured it twice. The API isn't a wrapper, it's the contract.");
  2946. assert.equal(gapOfOne.length, 2, JSON.stringify(gapOfOne.map((i) => i.text)));
  2947. });
  2948. test('negative-parallelism: repeated identical contrasts do not pass a distinct-findings gate', () => {
  2949. const repeated = "It isn't raining, it's snowing. It isn't raining, it's snowing.";
  2950. assert.deepEqual(npHits(repeated), []);
  2951. const distinct = "It isn't raining, it's snowing. The problem isn't the bus, it's the schedule.";
  2952. assert.equal(npHits(distinct).length, 2);
  2953. });
  2954. test('negative-parallelism: unrelated corrections far apart stay clean', () => {
  2955. const text = [
  2956. "It isn't raining, it's snowing, so the school closed early and the buses stopped at noon.",
  2957. '',
  2958. 'We spent the afternoon at the library. The heating worked. Nobody minded the walk back.',
  2959. 'By evening the roads were clear again and the plows had moved on to the side streets.',
  2960. '',
  2961. "The bill isn't due today, it's due Friday, according to the notice the landlord left on the door.",
  2962. ].join('\n');
  2963. assert.deepEqual(npHits(text), []);
  2964. // Adjacent sentences in separate paragraphs do not pair either.
  2965. const adjacentParagraphs = "It isn't raining, it's snowing.\n\nThe invoice isn't due today, it's due Friday.";
  2966. for (const eol of ['\n', '\r\n', '\r']) {
  2967. for (const sourceMode of ['plain', 'rendered-markdown']) {
  2968. assert.deepEqual(npHits(adjacentParagraphs.replace(/\n/g, eol), { sourceMode }), [], `${JSON.stringify(eol)} ${sourceMode}`);
  2969. }
  2970. }
  2971. // A single line break inside a paragraph is not a break, in any line ending.
  2972. for (const eol of ['\n', '\r\n', '\r']) {
  2973. const oneBreak = adjacentParagraphs.replace('\n\n', eol);
  2974. assert.equal(npHits(oneBreak).length, 2, `single ${JSON.stringify(eol)} keeps one paragraph`);
  2975. }
  2976. assert.equal(npHits(adjacentParagraphs.replace('\n\n', ' ')).length, 2, 'precondition: same paragraph pairs');
  2977. // A reveal far from a contrast flags alone and does not pull the contrast in.
  2978. const mixed = npHits(text.replace("It isn't raining, it's snowing", "It isn't just raining, it's pouring"));
  2979. assert.deepEqual(mixed.map((i) => i.text), ["isn't just raining, it's"]);
  2980. });
  2981. test('negative-parallelism: correlatives and ordinary negations are not frames', () => {
  2982. for (const text of [
  2983. 'The result is not only faster but also cheaper, which the finance team noticed within a week.',
  2984. 'The problem is not the code but the process, and nobody on the team disputes that anymore.',
  2985. "It's not just a tool but a whole workflow, according to the vendor's own documentation page.",
  2986. "It's not just me who noticed the delay; the support queue doubled over the same weekend.",
  2987. "This isn't just a bug; its effects spread to every downstream service we operate in the region.",
  2988. "It is not only possible to run it locally but also cheap, since the model fits on one laptop.",
  2989. "The contract isn't signed yet, and we are still waiting on legal to send the final version.",
  2990. ]) {
  2991. assert.deepEqual(npHits(text).map((i) => i.text), [], text);
  2992. // Next to one plain contrast, a correlative that counted as a frame would
  2993. // pair with it and flag.
  2994. assert.deepEqual(npHits(`It isn't a feature, it's a platform. ${text}`).map((i) => i.text), [], `paired: ${text}`);
  2995. }
  2996. });
  2997. test('negative-parallelism: a quoted frame belongs to the speaker', () => {
  2998. const text = 'The vendor told us, "It\'s not just a tool, it\'s a platform," and then asked for a three-year contract.';
  2999. assert.deepEqual(npHits(text), []);
  3000. assert.equal(npHits(text.replace(/"/g, '')).length, 1, 'precondition: unquoted frame fires');
  3001. });
  3002. const SPECIMEN = "In today's ever-evolving landscape, we delve into the intricate tapestry of innovation. It's not just a tool, it's a paradigm.";
  3003. const IGNORE_START = '<!-- avoid-ai-writing:ignore-start -->';
  3004. const IGNORE_END = '<!-- avoid-ai-writing:ignore-end -->';
  3005. const stripIgnoreMarkers = (source) => source.split(IGNORE_START).join('').split(IGNORE_END).join('');
  3006. test('ignore markers: the unmarked control removes only directive markers', () => {
  3007. const otherComment = '<!-- ordinary note -->';
  3008. const multilineComment = '<!-- ordinary\nnote -->';
  3009. const source = [IGNORE_START, otherComment, multilineComment, SPECIMEN, IGNORE_END].join('\n');
  3010. assert.equal(stripIgnoreMarkers(source), ['', otherComment, multilineComment, SPECIMEN, ''].join('\n'));
  3011. });
  3012. test('ignore markers: a marked specimen is excluded and the rest is still scored', () => {
  3013. const source = [
  3014. 'Here is the kind of paragraph we warn readers about on this robust page.',
  3015. '',
  3016. '<!-- avoid-ai-writing:ignore-start -->',
  3017. SPECIMEN,
  3018. '<!-- avoid-ai-writing:ignore-end -->',
  3019. '',
  3020. 'The rest of the page explains what each of those phrases hides from a reader.',
  3021. ].join('\n');
  3022. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3023. const r = AIDetector.analyzeText(source, { sourceMode });
  3024. assert.equal(r.stats.ignoredRegions, 1, sourceMode);
  3025. assert.deepEqual(r.issues.map((i) => i.text), ['robust'], `${sourceMode}: ${JSON.stringify(r.issues.map((i) => i.text))}`);
  3026. assertIndexedIssuesSliceExactly(source, r.issues, `ignore ${sourceMode}`);
  3027. for (const region of r.highlight_sentence_for_ai) {
  3028. assert.ok(!source.slice(region.start, region.end).includes('tapestry'), `${sourceMode}: highlight reached the ignored region`);
  3029. }
  3030. }
  3031. const unmarked = AIDetector.analyzeText(stripIgnoreMarkers(source));
  3032. assert.ok(unmarked.issues.some((i) => i.text === 'delve'), 'precondition: the specimen scores without markers');
  3033. });
  3034. test('ignore markers: CRLF source masks the same region', () => {
  3035. const source = ['Intro line with a robust claim in it for the page.', IGNORE_START, SPECIMEN, IGNORE_END, 'The closing line explains the specimen to readers.'].join('\r\n');
  3036. const r = AIDetector.analyzeText(source);
  3037. assert.equal(r.stats.ignoredRegions, 1);
  3038. assert.deepEqual(r.issues.map((i) => i.text), ['robust']);
  3039. });
  3040. test('ignore markers: nested starts need their own ends', () => {
  3041. const source = [
  3042. 'The team met on Tuesday and agreed the next steps for the release.',
  3043. IGNORE_START,
  3044. 'We must delve into the first specimen here.',
  3045. IGNORE_START,
  3046. 'The second specimen is a vibrant tapestry.',
  3047. IGNORE_END,
  3048. 'This line is still inside the outer region, a robust claim.',
  3049. IGNORE_END,
  3050. 'The release went out on Friday without trouble or delay.',
  3051. ].join('\n');
  3052. const r = AIDetector.analyzeText(source);
  3053. assert.equal(r.stats.ignoredRegions, 1);
  3054. assert.deepEqual(r.issues.map((i) => i.text), []);
  3055. });
  3056. test('ignore markers: an unmatched inner start keeps the outer region open', () => {
  3057. const source = [
  3058. 'The team met on Tuesday and agreed the next steps for the release.',
  3059. IGNORE_START,
  3060. 'We must delve into the first specimen here.',
  3061. IGNORE_START,
  3062. 'The second specimen is a vibrant tapestry.',
  3063. IGNORE_END,
  3064. 'This robust line remains inside the unclosed outer region.',
  3065. ].join('\n');
  3066. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3067. const result = AIDetector.analyzeText(source, { sourceMode });
  3068. assert.equal(result.stats.ignoredRegions, 1, sourceMode);
  3069. assert.deepEqual(result.issues, [], sourceMode);
  3070. }
  3071. });
  3072. test('ignore markers: code, inline mentions, quotations, and stray ends do not act', () => {
  3073. const after = `\n\n${SPECIMEN}`;
  3074. const cases = {
  3075. fenced: ['```html', IGNORE_START, '```'].join('\n') + after,
  3076. indented: ['Example:', '', ` ${IGNORE_START}`, ''].join('\n') + after,
  3077. inlineCode: `Use \`${IGNORE_START}\` to start a region.` + after,
  3078. quoted: `The docs say to write "${IGNORE_START}" before the specimen.` + after,
  3079. prose: `Write ${IGNORE_START} on its own line.` + after,
  3080. strayEnd: IGNORE_END + after,
  3081. };
  3082. for (const [name, source] of Object.entries(cases)) {
  3083. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3084. const r = AIDetector.analyzeText(source, { sourceMode });
  3085. assert.equal(r.stats.ignoredRegions, 0, `${name} ${sourceMode}`);
  3086. assert.ok(r.issues.some((i) => i.text === 'delve'), `${name} ${sourceMode}: prose after the marker must still score`);
  3087. }
  3088. }
  3089. });
  3090. test('ignore markers: HTML code containers and frontmatter hold no directives', () => {
  3091. const after = `\n\n${SPECIMEN}`;
  3092. const cases = {
  3093. preCode: ['<pre><code>', IGNORE_START, '</code></pre>'].join('\n') + after,
  3094. code: ['<code>', IGNORE_START, '</code>'].join('\n') + after,
  3095. preUpper: ['<PRE class="html">', IGNORE_START, '</PRE>'].join('\n') + after,
  3096. script: ['<script type="text/plain">', IGNORE_START, '</script>'].join('\n') + after,
  3097. style: ['<style>', IGNORE_START, '</style>'].join('\n') + after,
  3098. unclosedPre: ['<pre>', IGNORE_START].join('\n') + after,
  3099. frontmatter: ['---', 'title: Tells', 'example: |', ` ${IGNORE_START}`, '---'].join('\n') + after,
  3100. frontmatterLeftAligned: ['---', 'title: Tells', 'example: |', IGNORE_START, '---'].join('\n') + after,
  3101. };
  3102. for (const [name, source] of Object.entries(cases)) {
  3103. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3104. const r = AIDetector.analyzeText(source, { sourceMode });
  3105. assert.equal(r.stats.ignoredRegions, 0, `${name} ${sourceMode}`);
  3106. assert.ok(r.issues.some((i) => i.text === 'delve'), `${name} ${sourceMode}: prose after the container must still score`);
  3107. }
  3108. }
  3109. // A tag or marker inside an ordinary HTML comment opens nothing, so a
  3110. // later valid region still applies.
  3111. for (const note of ['<!-- remember to wrap samples in <pre> -->', `<!--\n<code>\n${IGNORE_START}\n-->`]) {
  3112. const commented = [note, '', 'The team met on Tuesday to agree next steps.', IGNORE_START, SPECIMEN, IGNORE_END].join('\n');
  3113. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3114. const c = AIDetector.analyzeText(commented, { sourceMode });
  3115. assert.equal(c.stats.ignoredRegions, 1, `${note} ${sourceMode}`);
  3116. assert.ok(!c.issues.some((i) => i.text === 'delve'), `${note} ${sourceMode}: specimen must stay ignored`);
  3117. }
  3118. }
  3119. // Round 4: one left-to-right scan decides ownership. Whichever construct
  3120. // opens first owns the text until its own close.
  3121. const tail = ['', 'The team met on Tuesday to agree next steps.', IGNORE_START, SPECIMEN, IGNORE_END].join('\n');
  3122. const owned = {
  3123. fenceInsideComment: ['<!--', '```', 'not a fence here', '-->'].join('\n') + '\n' + tail,
  3124. tildeFenceInsideComment: ['<!-- ~~~ -->'].join('\n') + '\n' + tail,
  3125. commentInsideFence: ['```html', '<!-- an unclosed comment inside a fence', '```'].join('\n') + '\n' + tail,
  3126. preInsideFence: ['```', '<pre>', '```'].join('\n') + '\n' + tail,
  3127. fenceInsidePre: ['<pre>', '```', '</pre>'].join('\n') + '\n' + tail,
  3128. commentInsideInlineCode: 'Write `<!--` to open a comment.\n' + tail,
  3129. preInsideInlineCode: 'Wrap samples in `<pre>` tags.\n' + tail,
  3130. commentInsideIndentedCode: ['Example:', '', ' <!-- unclosed', ''].join('\n') + '\n' + tail,
  3131. };
  3132. for (const [name, source] of Object.entries(owned)) {
  3133. for (const eol of ['\n', '\r\n']) {
  3134. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3135. const input = source.replace(/\n/g, eol);
  3136. const o = AIDetector.analyzeText(input, { sourceMode });
  3137. assert.equal(o.stats.ignoredRegions, 1, `${name} ${JSON.stringify(eol)} ${sourceMode}`);
  3138. assert.ok(!o.issues.some((i) => i.text === 'delve'), `${name} ${JSON.stringify(eol)} ${sourceMode}: specimen must stay ignored`);
  3139. }
  3140. }
  3141. }
  3142. // And the reverse: a marker inside a fence that sits inside a comment, or a
  3143. // comment that sits inside a fence, still does nothing.
  3144. const inert = {
  3145. markerInFenceInComment: ['<!--', '```', IGNORE_START, '```', '-->'].join('\n') + `\n\n${SPECIMEN}`,
  3146. markerInFenceAfterComment: ['```', '<!-- note -->', IGNORE_START, '```'].join('\n') + `\n\n${SPECIMEN}`,
  3147. markerInPreInComment: ['<!-- <pre> -->', '<pre>', IGNORE_START, '</pre>'].join('\n') + `\n\n${SPECIMEN}`,
  3148. };
  3149. for (const [name, source] of Object.entries(inert)) {
  3150. const i = AIDetector.analyzeText(source);
  3151. assert.equal(i.stats.ignoredRegions, 0, name);
  3152. assert.ok(i.issues.some((x) => x.text === 'delve'), `${name}: prose must still score`);
  3153. }
  3154. // A real marker after a closed container still works.
  3155. const later = ['<pre>', IGNORE_START, '</pre>', '', 'The team met on Tuesday to agree next steps.', IGNORE_START, SPECIMEN, IGNORE_END].join('\n');
  3156. const r = AIDetector.analyzeText(later);
  3157. assert.equal(r.stats.ignoredRegions, 1);
  3158. assert.ok(!r.issues.some((i) => i.text === 'delve'));
  3159. });
  3160. test('ignore markers: the marker scan stays linear on adversarial input', () => {
  3161. const ordinary = 'one two three four five six seven eight nine ten';
  3162. const shapes = {
  3163. backticks: (k) => `${'` `` '.repeat(k)}\n${IGNORE_START}\n${ordinary}`,
  3164. comments: (k) => `${'<!-- x --> '.repeat(k)}\n${IGNORE_START}\n${ordinary}`,
  3165. unclosedTags: (k) => `${IGNORE_START}\n${ordinary}\n${IGNORE_END}\n${'<pr <cod '.repeat(k)}`,
  3166. fences: (k) => `${IGNORE_START}\n${'```\nx\n```\n'.repeat(k)}${IGNORE_END}\n${ordinary}`,
  3167. };
  3168. for (const [name, build] of Object.entries(shapes)) {
  3169. const { small, large } = timeScaling(build, 1500);
  3170. assert.ok(large < small * 8, `${name}: 4x input took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`);
  3171. }
  3172. });
  3173. test('ignore markers: a long line mixing code spans and <code> stays linear', () => {
  3174. // Round-5 review: rebuilding the backtick table after each <code> element
  3175. // made this shape quadratic (116 KB took about 2 s).
  3176. const build = (k) => `${IGNORE_START}\nThe team met on Tuesday.\n${IGNORE_END}\n${'`x` <code>y</code> '.repeat(k)}`;
  3177. const { small, large } = timeScaling(build, 1500);
  3178. assert.ok(large < small * 6, `4x input took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`);
  3179. const big = build(Math.ceil(116000 / '`x` <code>y</code> '.length));
  3180. assert.ok(big.length >= 116000);
  3181. const started = performance.now();
  3182. const r = AIDetector.analyzeText(big);
  3183. const elapsed = performance.now() - started;
  3184. assert.equal(r.stats.ignoredRegions, 1);
  3185. assert.ok(elapsed < 500, `116 KB mixed line took ${elapsed.toFixed(0)}ms`);
  3186. });
  3187. test('ignore markers: an unclosed start runs to the end of the text', () => {
  3188. const source = `The team met on Tuesday and agreed the next steps for the release.\n\n <!-- AVOID-AI-WRITING:IGNORE-START --> \n${SPECIMEN}`;
  3189. const r = AIDetector.analyzeText(source);
  3190. assert.equal(r.stats.ignoredRegions, 1);
  3191. assert.deepEqual(r.issues, []);
  3192. });
  3193. test('word joiners: U+2060 around URLs is stripped without a bypass flag', () => {
  3194. const wj = '\u2060';
  3195. const text = `Listen at ${wj}https://example.com/ep75${wj} or on ${wj}https://podcasts.example.com/show${wj} and share it with a friend who likes long interviews.`;
  3196. const r = AIDetector.analyzeText(text);
  3197. assert.equal(r.stats.normalization.zeroWidth, 0);
  3198. assert.ok(!r.issues.some((i) => i.type === 'normalization-flag'), JSON.stringify(r.issues));
  3199. assert.notEqual(r.document_classification, 'AI_ONLY');
  3200. assert.ok(!AIDetector.normalizeText(text).text.includes(wj), 'word joiners are still stripped');
  3201. });
  3202. test('word joiners: U+2060 inside a word still counts as a bypass character', () => {
  3203. const wj = '\u2060';
  3204. for (const [word, count] of [[`del${wj}ve`, 1], [`del${wj}${wj}ve`, 2], [`del${wj}\u200Bve`, 2]]) {
  3205. const r = AIDetector.analyzeText(`We must ${word} into this now and then talk it over with the rest of the team next week.`);
  3206. assert.equal(r.stats.normalization.zeroWidth, count, word);
  3207. assert.ok(r.issues.some((i) => i.type === 'normalization-flag'), word);
  3208. assert.ok(r.issues.some((i) => i.type === 'tier1' && i.text === 'delve'), word);
  3209. }
  3210. });
  3211. test('word joiners: astral letters on both sides count as a bypass character', () => {
  3212. const word = '\u{10400}\u2060\u{10428}';
  3213. assert.equal(AIDetector.normalizeText(word).flags.zeroWidth, 1);
  3214. const result = AIDetector.analyzeText(`We read ${word} and then met the team to discuss the plan at length.`);
  3215. assert.equal(result.stats.normalization.zeroWidth, 1);
  3216. assert.ok(result.issues.some((issue) => issue.type === 'normalization-flag'));
  3217. });
  3218. test('dev-blog-boilerplate: simplicity slogans fire', () => {
  3219. const r = AIDetector.analyzeText(
  3220. "The framework ships with sane defaults, and honestly it just works out of the box from the first install. The whole API is small enough to fit in your head after one afternoon of reading."
  3221. );
  3222. const hits = r.issues.filter((i) => i.type === 'dev-blog-boilerplate').map((i) => i.text);
  3223. assert.ok(hits.length >= 3, `expected >=3 dev-blog-boilerplate hits, got ${JSON.stringify(hits)}`);
  3224. assert.ok(hits.includes('it just works'), `out-of-the-box slogan must fire: ${JSON.stringify(hits)}`);
  3225. });
  3226. test('dev-blog-boilerplate: spaced and hyphenated out-of-the-box slogans fire', () => {
  3227. for (const phrase of ['it just works out of the box', 'it just works out-of-the-box']) {
  3228. const r = AIDetector.analyzeText(
  3229. `The framework claims ${phrase} for every developer who follows the documented installation steps today.`
  3230. );
  3231. const hits = r.issues.filter((i) => i.type === 'dev-blog-boilerplate').map((i) => i.text);
  3232. assert.deepEqual(hits, ['it just works'], `${phrase} must fire with an exact issue span`);
  3233. }
  3234. });
  3235. test('dev-blog-boilerplate: ordinary prose stays clean', () => {
  3236. const r = AIDetector.analyzeText(
  3237. "The configuration file documents every default value we chose and why we chose it. The batteries included with the flashlight were already dead when we opened the sealed package at camp. It just works out to two queries after the optimizer merges identical branches."
  3238. );
  3239. const hits = r.issues.filter((i) => i.type === 'dev-blog-boilerplate');
  3240. assert.equal(hits.length, 0, `false positives: ${JSON.stringify(hits.map((i) => i.text))}`);
  3241. });
  3242. // ── Issue #189 — preserve source offsets through preprocessing ─────────
  3243. // Preprocessing that changes string length (plain-mode blockquote stripping,
  3244. // zero-width-character and *roleplay-action* removal) must not shift the
  3245. // issue.index and highlight-range coordinates reported against the caller's
  3246. // original source. For every index-carrying issue, the source slice at its
  3247. // coordinates must reproduce its text exactly; highlight regions must land on
  3248. // real source chars. Masking stages are already covered by #123 — these tests
  3249. // pin the length-changing stages.
  3250. function assertIndexedIssuesSliceExactly(source, issues, label) {
  3251. for (const issue of issues) {
  3252. if (!Number.isInteger(issue.index)) continue;
  3253. assert.equal(
  3254. source.slice(issue.index, issue.index + issue.text.length),
  3255. issue.text,
  3256. `${label}: source.slice(index, index+len) must reproduce the reported ${issue.type} text`,
  3257. );
  3258. }
  3259. }
  3260. test('#189: plain-mode multiline blockquote (LF) keeps issue and highlight offsets on source', () => {
  3261. const source = '> quoted line one\n> quoted line two\n\nIt is important to note that the system works well and the team shipped it.';
  3262. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3263. const filler = result.issues.find((issue) => issue.type === 'filler');
  3264. assert.equal(result.stats.quotedLines, 2);
  3265. assert.ok(filler, 'prose after the stripped quote must still be analyzed');
  3266. assert.equal(filler.index, source.indexOf('It is important'), 'filler index must address the source');
  3267. assert.equal(source.slice(filler.index, filler.index + filler.text.length), filler.text);
  3268. assertIndexedIssuesSliceExactly(source, result.issues, 'plain LF blockquote');
  3269. const [region] = result.highlight_sentence_for_ai;
  3270. assert.deepEqual([region.start, region.end], [36, 112], 'highlight must span from the newline before the sentence to the end');
  3271. assert.equal(
  3272. source.slice(region.start, region.end),
  3273. '\nIt is important to note that the system works well and the team shipped it.',
  3274. );
  3275. });
  3276. test('#189: plain-mode multiline blockquote (CRLF) keeps issue and highlight offsets on source', () => {
  3277. const source = '> quoted line one\r\n> quoted line two\r\n\r\nIt is important to note that the system works well and the team shipped it.';
  3278. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3279. const filler = result.issues.find((issue) => issue.type === 'filler');
  3280. assert.equal(result.stats.quotedLines, 2);
  3281. assert.ok(filler, 'prose after the stripped quote must still be analyzed');
  3282. assert.equal(filler.index, source.indexOf('It is important'), 'filler index must address the source');
  3283. assert.equal(source.slice(filler.index, filler.index + filler.text.length), filler.text);
  3284. assertIndexedIssuesSliceExactly(source, result.issues, 'plain CRLF blockquote');
  3285. const [region] = result.highlight_sentence_for_ai;
  3286. assert.deepEqual([region.start, region.end], [39, 115], 'the leading \\r of the CRLF blank line must be accounted for too');
  3287. assert.equal(
  3288. source.slice(region.start, region.end),
  3289. '\nIt is important to note that the system works well and the team shipped it.',
  3290. );
  3291. });
  3292. test('#189: rendered-markdown multiline blockquotes keep offsets on source (LF and CRLF)', () => {
  3293. const prose = 'It is important to note that the system works well and the team shipped it.';
  3294. for (const [name, quote] of [
  3295. ['LF', '> quoted line one\n> quoted line two\n\n'],
  3296. ['CRLF', '> quoted line one\r\n> quoted line two\r\n\r\n'],
  3297. ]) {
  3298. const source = quote + prose;
  3299. const result = AIDetector.analyzeText(source, { sourceMode: 'rendered-markdown' });
  3300. const filler = result.issues.find((issue) => issue.type === 'filler');
  3301. assert.equal(result.stats.quotedLines, 2, `${name}: both quote lines counted`);
  3302. assert.ok(filler, `${name}: visible prose after the masked quote must still be analyzed`);
  3303. assert.equal(filler.index, quote.length, `${name}: masked quote must not shift the filler`);
  3304. assertIndexedIssuesSliceExactly(source, result.issues, `rendered ${name}`);
  3305. const [region] = result.highlight_sentence_for_ai;
  3306. assert.deepEqual([region.start, region.end], [quote.length, source.length], `${name}: highlight spans the visible prose`);
  3307. assert.equal(source.slice(region.start, region.end), prose);
  3308. }
  3309. });
  3310. test('#189: zero-width characters do not shift later offsets', () => {
  3311. const source = 'Powerful is the new baseline​‌ and truly robust and tough.';
  3312. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3313. const truly = result.issues.find((issue) => issue.type === 'hollow-intensifier' && issue.text === 'truly');
  3314. assert.equal(result.stats.normalization.zeroWidth, 2);
  3315. assert.ok(truly, 'word after the zero-width chars must still be flagged');
  3316. assert.equal(truly.index, source.indexOf('truly'));
  3317. assert.equal(source.slice(truly.index, truly.index + truly.text.length), truly.text);
  3318. assertIndexedIssuesSliceExactly(source, result.issues, 'zero-width');
  3319. });
  3320. test('#189: zero-width chars separated by content record exact source runs', () => {
  3321. const source = 'This system works well​ across the board‌ and truly robust and tough for the final release today.';
  3322. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3323. const truly = result.issues.find((issue) => issue.type === 'hollow-intensifier' && issue.text === 'truly');
  3324. assert.equal(result.stats.normalization.zeroWidth, 2);
  3325. assert.ok(truly, 'word after both separated zero-width chars must still be flagged');
  3326. assert.equal(truly.index, source.indexOf('truly'));
  3327. assert.equal(source.slice(truly.index, truly.index + truly.text.length), truly.text);
  3328. assertIndexedIssuesSliceExactly(source, result.issues, 'separated zero-width');
  3329. });
  3330. test('#189: roleplay-action markers do not shift later offsets', () => {
  3331. const source = 'It is important to note that the system works well. *nods* This is truly robust and the team shipped it.';
  3332. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3333. const truly = result.issues.find((issue) => issue.type === 'hollow-intensifier' && issue.text === 'truly');
  3334. assert.equal(result.stats.normalization.roleplay, 1);
  3335. assert.ok(truly, 'word after the *nods* marker must still be flagged');
  3336. assert.equal(truly.index, source.indexOf('truly'));
  3337. assert.equal(source.slice(truly.index, truly.index + truly.text.length), truly.text);
  3338. assertIndexedIssuesSliceExactly(source, result.issues, 'roleplay marker');
  3339. });
  3340. test('#189: interleaved preprocessing stages compose source offsets', () => {
  3341. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  3342. for (const [name, prefix] of [
  3343. ['zero-width before quote', '\u200b\n> x\n> y\n'],
  3344. ['roleplay before quote', '*nods*\n> x\n> y\n'],
  3345. ['roleplay before zero-width', '*nods* later\u200b '],
  3346. ]) {
  3347. const source = prefix + prose;
  3348. const result = AIDetector.analyzeText(source);
  3349. const transition = result.issues.find((issue) => issue.type === 'transition');
  3350. assert.ok(transition, `${name}: visible prose must still be analyzed`);
  3351. assert.equal(transition.index, source.indexOf('Moreover'), `${name}: issue index must address source`);
  3352. const [region] = result.highlight_sentence_for_ai;
  3353. assert.ok(region.start <= transition.index && transition.index < region.end, `${name}: issue must sit inside highlight`);
  3354. assert.ok(region.end <= source.length, `${name}: highlight must stay within source`);
  3355. }
  3356. });
  3357. test('#189: overlapping normalization removals map once and keep staged semantics', () => {
  3358. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  3359. const source = '*nod\u200bs* *nоds* ' + prose;
  3360. const result = AIDetector.analyzeText(source);
  3361. const transition = result.issues.find((issue) => issue.type === 'transition');
  3362. assert.deepEqual(result.stats.normalization, { zeroWidth: 1, homoglyph: 1, roleplay: 2 });
  3363. assert.ok(transition, 'prose after normalized roleplay markers must still be analyzed');
  3364. assert.equal(transition.index, source.indexOf('Moreover'));
  3365. assert.ok(result.highlight_sentence_for_ai.every((region) => region.end <= source.length));
  3366. });
  3367. test('#189: malformed emphasis is not reclassified as roleplay', () => {
  3368. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  3369. const source = '**nods* **sighs* ' + prose;
  3370. const result = AIDetector.analyzeText(source);
  3371. const transition = result.issues.find((issue) => issue.type === 'transition');
  3372. assert.equal(result.stats.normalization.roleplay, 0);
  3373. assert.equal(transition.index, source.indexOf('Moreover'));
  3374. });
  3375. test('#189: a zero-width character inside a finding preserves its source start and region', () => {
  3376. const obfuscated = 'only ti\u200bme will tell';
  3377. const source = `Alpha beta gamma delta epsilon zeta eta theta iota kappa ${obfuscated} about systems.`;
  3378. const result = AIDetector.analyzeText(source);
  3379. const generic = result.issues.find((issue) => issue.type === 'generic-conclusion');
  3380. assert.ok(generic, 'normalization must not hide a phrase from detection');
  3381. assert.equal(generic.index, source.indexOf('only'));
  3382. assert.equal(source.slice(generic.index, generic.index + obfuscated.length), obfuscated);
  3383. const [region] = result.highlight_sentence_for_ai;
  3384. assert.ok(source.slice(region.start, region.end).includes(obfuscated));
  3385. assert.ok(region.end <= source.length);
  3386. });
  3387. test('#189: trailing removed markers stay outside the preceding highlight', () => {
  3388. const prose = 'Moreover, the editor checked the original document before changing the published account for the morning edition.';
  3389. const source = `${prose} *nods*`;
  3390. const result = AIDetector.analyzeText(source);
  3391. const [region] = result.highlight_sentence_for_ai;
  3392. assert.equal(region.end, prose.length);
  3393. assert.equal(source.slice(region.start, region.end), prose);
  3394. });
  3395. test('#189: direct normalization handles every occurrence and mapped analysis stays linear', () => {
  3396. const normalized = AIDetector.normalizeText('x\u200by\u200bz еx еy');
  3397. assert.equal(normalized.text, 'xyz ex ey');
  3398. assert.deepEqual(normalized.flags, { zeroWidth: 2, homoglyph: 2, roleplay: 0 });
  3399. const denseCount = 75000;
  3400. const buildDense = (n) => '\u200b'.repeat(n)
  3401. + 'Alpha beta gamma delta epsilon zeta eta theta iota kappa only time will tell about systems.';
  3402. const denseResult = AIDetector.analyzeText(buildDense(denseCount));
  3403. assert.equal(denseResult.stats.normalization.zeroWidth, denseCount);
  3404. const { small, large } = timeScaling(buildDense, denseCount);
  3405. assert.ok(
  3406. large < small * 8,
  3407. `dense mapped analysis: 4x zero-width characters took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`,
  3408. );
  3409. });
  3410. test('#189: ordinary unchanged text reports native indexes and exact slices', () => {
  3411. const source = 'It is important to note that the system works well, and it is truly robust and tough.';
  3412. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3413. for (const issue of result.issues) {
  3414. if (!Number.isInteger(issue.index)) continue;
  3415. assert.equal(issue.index, source.indexOf(issue.text), `${issue.type}: unchanged text keeps a native index`);
  3416. }
  3417. assertIndexedIssuesSliceExactly(source, result.issues, 'ordinary text');
  3418. });
  3419. test('#189: highlight regions land on real source bytes and slice back to the flagged sentence', () => {
  3420. for (const [name, lineEnd] of [['LF', '\n'], ['CRLF', '\r\n']]) {
  3421. const source = [
  3422. '> quoted line one',
  3423. '> quoted line two',
  3424. '',
  3425. 'It is important to note that the system works well and the team shipped it.',
  3426. ].join(lineEnd);
  3427. const result = AIDetector.analyzeText(source, { sourceMode: 'plain' });
  3428. const filler = result.issues.find((issue) => issue.type === 'filler');
  3429. const [region] = result.highlight_sentence_for_ai;
  3430. assert.ok(Number.isInteger(region.start) && Number.isInteger(region.end), `${name}: region coords are integers`);
  3431. assert.ok(region.start < region.end && region.end <= source.length, `${name}: region stays inside source bounds`);
  3432. assert.ok(
  3433. source.slice(region.start, region.end).includes('It is important to note'),
  3434. `${name}: source slice at the region covers the flagged sentence`,
  3435. );
  3436. assert.ok(region.start <= filler.index && filler.index < region.end, `${name}: issue index sits inside the highlight`);
  3437. }
  3438. });
  3439. test('#235: sentence spans match the former regex scan on every boundary shape', () => {
  3440. // Oracle: the regex the single-pass scanner replaced. Every flagged sentence
  3441. // below is isolated by at least two clean sentences so region merging never
  3442. // joins them, and each region must reproduce the oracle span exactly.
  3443. const oracle = (text) => {
  3444. const spans = [];
  3445. const re = /[^.!?]+[.!?]+|\S[^.!?]*$/g;
  3446. let m;
  3447. while ((m = re.exec(text)) !== null) spans.push([m.index, m.index + m[0].length, m[0]]);
  3448. return spans;
  3449. };
  3450. const clean = 'The bus was late. We waited by the shop.';
  3451. const shapes = [
  3452. 'We must delve into it.',
  3453. ' We must delve into it?!',
  3454. '\n\nWe must delve into it...',
  3455. 'We must delve into it',
  3456. ' We must delve into it.',
  3457. 'We must delve into it.\r\n',
  3458. ];
  3459. for (const flagged of shapes) {
  3460. for (const trailing of ['', ' ', '\n\n', ' \n ']) {
  3461. const text = `${clean} ${flagged} ${clean}${trailing}`;
  3462. const expected = oracle(text).filter(([, , raw]) => raw.includes('delve'));
  3463. assert.equal(expected.length, 1, `oracle isolates one flagged span in ${JSON.stringify(text)}`);
  3464. const [[start, end]] = expected;
  3465. const regions = AIDetector.analyzeText(text, { sourceMode: 'plain' }).highlight_sentence_for_ai;
  3466. assert.equal(regions.length, 1, `one region for ${JSON.stringify(text)}: ${JSON.stringify(regions)}`);
  3467. assert.equal(regions[0].start, start, `region start for ${JSON.stringify(text)}`);
  3468. assert.equal(regions[0].end, end, `region end for ${JSON.stringify(text)}`);
  3469. }
  3470. }
  3471. // Terminator-only prefixes and a document with no terminator at all. Each
  3472. // stays above the ten-word gate so the document is scored.
  3473. for (const text of [
  3474. `...We must delve into it. ${clean} ${clean}`,
  3475. `. We must delve into it. ${clean} ${clean}`,
  3476. ...['.', '?!', '...!?'].flatMap((prefix) => [
  3477. `${prefix}We must delve into it and then keep going for a good while longer`,
  3478. `${prefix}\r\nWe must delve into it and then keep going for a good while longer`,
  3479. `${prefix}We must delve into it. ${clean} ${clean}`,
  3480. ]),
  3481. 'We must delve into it and then keep going for a good while longer without stopping',
  3482. ]) {
  3483. const [[start, end]] = oracle(text).filter(([, , raw]) => raw.includes('delve'));
  3484. const regions = AIDetector.analyzeText(text, { sourceMode: 'plain' }).highlight_sentence_for_ai;
  3485. assert.equal(regions.length, 1, `one region for ${JSON.stringify(text)}`);
  3486. assert.deepEqual([regions[0].start, regions[0].end], [start, end], `span for ${JSON.stringify(text)}`);
  3487. }
  3488. });
  3489. test('#260: punctuation-prefix analysis scales without rescanning each suffix', () => {
  3490. const timeFor = (n, ending) => {
  3491. const text = '.!?'.repeat(n) + ' We must delve into it and then keep going for a good while longer' + ending;
  3492. let best = Infinity;
  3493. for (let run = 0; run < 3; run += 1) {
  3494. const start = performance.now();
  3495. AIDetector.analyzeText(text, { sourceMode: 'plain' });
  3496. best = Math.min(best, performance.now() - start);
  3497. }
  3498. return best;
  3499. };
  3500. for (const ending of ['', '.']) {
  3501. // Cover both the trailing-fragment fallback and a later terminator. The
  3502. // sizes keep the small run well above timer noise: at 3000 prefixes it
  3503. // measured 6 ms on a CI runner and the ratio read 8x on a linear scan.
  3504. timeFor(100, ending);
  3505. const small = Math.max(timeFor(20000, ending), 20);
  3506. const large = timeFor(80000, ending);
  3507. assert.ok(large < small * 8,
  3508. `punctuation prefix (${JSON.stringify(ending)}): 4x input took ${(large / small).toFixed(1)}x time (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`);
  3509. }
  3510. });
  3511. test('#235: analysis time grows linearly on whitespace-heavy input', () => {
  3512. // Ratio, not budget: a linear scan takes about 4x longer on 4x the input;
  3513. // the quadratic regexes this replaces took about 16x. The floor keeps
  3514. // timer noise on a fast machine from turning a few milliseconds into a
  3515. // meaningless ratio.
  3516. const timeFor = (build) => {
  3517. let best = Infinity;
  3518. for (let run = 0; run < 3; run += 1) {
  3519. const text = build();
  3520. const started = performance.now();
  3521. AIDetector.analyzeText(text, { sourceMode: text.startsWith('<!--') ? 'rendered-markdown' : 'plain' });
  3522. best = Math.min(best, performance.now() - started);
  3523. }
  3524. return best;
  3525. };
  3526. const cases = [
  3527. ['trailing whitespace, no terminator', (n) => `${' '.repeat(n)}one two three four five six seven eight nine ten`],
  3528. ['blank-line run before prose', (n) => `${'\n'.repeat(n)}Interesting part: it still works. Another sentence follows here.`],
  3529. ['masked HTML comments', (n) => `${'<!-- ` -->\n'.repeat(n / 10)}one two three four five six seven eight nine ten`],
  3530. ['masked HTML comments before a terminated sentence', (n) => `${'<!-- x -->\n'.repeat(n / 10)}one two three four five six seven eight nine ten. Then more.`],
  3531. ];
  3532. for (const [name, build] of cases) {
  3533. const small = Math.max(timeFor(() => build(40000)), 15);
  3534. const large = timeFor(() => build(160000));
  3535. assert.ok(
  3536. large < small * 8,
  3537. `${name}: 4x input took ${(large / small).toFixed(1)}x longer (${small.toFixed(1)}ms vs ${large.toFixed(1)}ms)`,
  3538. );
  3539. }
  3540. });
  3541. test('#235: table delimiter rows still mask with surrounding whitespace and CR', () => {
  3542. const table = [
  3543. ' | Setting | Legacy value | ',
  3544. ' | --- | --- | \r',
  3545. ' | package | code-base | ',
  3546. ].join('\n');
  3547. const hits = AIDetector.analyzeText(table).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  3548. assert.equal(hits.length, 0, `padded table rows stay protected: ${JSON.stringify(hits)}`);
  3549. const notTable = 'A dash line --- followed by a code-base mention that is ordinary prose here.\n| --- |';
  3550. const prose = AIDetector.analyzeText(notTable).issues.filter((issue) => issue.type === 'unnecessary-hyphenation');
  3551. assert.equal(prose.length, 1, `prose next to a single-cell delimiter still edits: ${JSON.stringify(prose)}`);
  3552. });
  3553. test('#237: technical context mode suppresses technical-legitimate vocabulary terms', () => {
  3554. const text = 'We built a robust, comprehensive, seamless pipeline that can leverage the ecosystem to facilitate and streamline the work that underpin delivery. '.repeat(3);
  3555. const generalResult = AIDetector.analyzeText(text, { contextMode: 'general' });
  3556. const technicalResult = AIDetector.analyzeText(text, { contextMode: 'technical' });
  3557. // Verify all eight exception terms fire under general mode
  3558. const genIssueTexts = generalResult.issues.map((i) => i.text.toLowerCase());
  3559. const exemptTerms = ['robust', 'comprehensive', 'seamless', 'leverage', 'ecosystem', 'facilitate', 'streamline', 'underpin'];
  3560. for (const term of exemptTerms) {
  3561. assert.ok(genIssueTexts.includes(term), `general mode must report issue for "${term}"`);
  3562. }
  3563. // Verify all eight exception terms stay suppressed under technical mode
  3564. assert.equal(technicalResult.score, 0, 'technical mode must score 0 on technical-legitimate terms');
  3565. assert.equal(technicalResult.issues.length, 0, 'technical mode must report 0 issues for technical-legitimate terms');
  3566. // Verify inflections also stay clean under technical mode
  3567. const inflections = 'Leveraging the leveraged leverages of the ecosystems and seamlessly streamlining what facilitates and underpins the underpinning underpinnings.';
  3568. const techInflect = AIDetector.analyzeText(inflections, { contextMode: 'technical' });
  3569. assert.equal(techInflect.issues.length, 0, `technical mode must suppress inflections: ${JSON.stringify(techInflect.issues.map((i) => i.text))}`);
  3570. // Non-exempt terms must STILL fire under technical mode
  3571. // Include harness alongside another Tier 2 term (navigate) to satisfy cluster threshold (2+ per paragraph)
  3572. const nonExemptText = 'We delve into the tapestry and beacon to embark on a testament to a game-changer. We harness the power to navigate.';
  3573. const techNonExempt = AIDetector.analyzeText(nonExemptText, { contextMode: 'technical' });
  3574. const nonExemptIssueTexts = techNonExempt.issues.map((i) => i.text.toLowerCase());
  3575. const requiredNonExempt = ['delve', 'tapestry', 'beacon', 'embark', 'testament to', 'game-changer', 'harness'];
  3576. for (const term of requiredNonExempt) {
  3577. assert.ok(nonExemptIssueTexts.includes(term), `technical mode must still flag non-exempt term "${term}"`);
  3578. }
  3579. });
  3580. test('reply openers and analytical framing are not reported as acknowledgment loops (#239)', () => {
  3581. // Acknowledgment loops are judgment-only: these phrases also open ordinary
  3582. // replies, and "the question of whether" is standard analytical English. The
  3583. // tell is a restatement that adds nothing, which the engine cannot read.
  3584. const clean = [
  3585. 'The question of whether the effect persists after controlling for income is still open, and the two replications disagree with each other.',
  3586. 'To answer your question from Tuesday: the invoice went out on the 3rd and the payment cleared last week, so nothing is outstanding on our side.',
  3587. "You're asking about the retry limit. It is five by default and configurable with RETRY_MAX, though we do not recommend raising it past ten.",
  3588. ];
  3589. for (const text of clean) {
  3590. for (const contextMode of [undefined, 'technical']) {
  3591. const r = AIDetector.analyzeText(text, contextMode ? { contextMode } : {});
  3592. const hits = r.issues.filter((i) => /question of whether|answer your question|asking about/i.test(i.text));
  3593. assert.deepEqual(hits.map((i) => `${i.type}:${i.text}`), [], `${contextMode || 'default'}: ${text}`);
  3594. assert.ok(!r.issues.some((i) => i.type === 'acknowledgment-loop'));
  3595. }
  3596. }
  3597. });
  3598. test('#241: unsegmented-script documents are declined, not scored "Too short"', () => {
  3599. // countWords counts \S+ runs; Chinese and Japanese carry no inter-word
  3600. // spaces, so segmentation cannot measure them. The script check runs
  3601. // before the word gate and declines only when CJK characters dominate
  3602. // the non-whitespace text. Han + kana ranges (including halfwidth
  3603. // katakana) signal an unsegmented script; Hangul is space-separated and
  3604. // segments fine, so it is excluded.
  3605. const zh = '这个函数返回一个承诺,调用方不应假设句柄之后仍可重用。'.repeat(50);
  3606. const rzh = AIDetector.analyzeText(zh);
  3607. assert.equal(rzh.label, 'Unsupported script', `expected Unsupported script, got ${rzh.label}`);
  3608. assert.equal(rzh.unsupportedScript, true);
  3609. assert.equal(rzh.document_classification, 'UNSCORED');
  3610. assert.ok(rzh.stats.cjkChars > 0, 'stats must carry the cjkChars count');
  3611. assert.match(rzh.stats.reason, /unsegmented-script/);
  3612. const ja = 'この関数はプロミスを返します。呼び出し側は、ハンドルがその後も再利用できると仮定してはいけません。'.repeat(40);
  3613. assert.equal(AIDetector.analyzeText(ja).label, 'Unsupported script');
  3614. // Halfwidth katakana (U+FF66–U+FF9D) is also an unsegmented script.
  3615. const jaHw = 'テスト'.repeat(100);
  3616. assert.equal(AIDetector.analyzeText(jaHw).label, 'Unsupported script');
  3617. // Supplementary-plane Han and kana must be counted by code point. Explicit
  3618. // BMP ranges miss these characters and a non-Unicode regex counts each
  3619. // surrogate pair twice in the dominance denominator.
  3620. const zhSupplementary = '𠀀'.repeat(100); // CJK Unified Ideographs Extension B
  3621. const rzhSupplementary = AIDetector.analyzeText(zhSupplementary);
  3622. assert.equal(rzhSupplementary.label, 'Unsupported script');
  3623. assert.equal(rzhSupplementary.stats.cjkChars, 100);
  3624. const jaSupplementary = '𛀀'.repeat(100); // Kana Supplement
  3625. assert.equal(AIDetector.analyzeText(jaSupplementary).label, 'Unsupported script');
  3626. // Newline-wrapped CJK lines each count as a word, so the script check
  3627. // must not sit inside the minimum word-count condition.
  3628. const zhLines = Array(10).fill('这个函数返回一个承诺。').join('\n');
  3629. assert.equal(AIDetector.analyzeText(zhLines).label, 'Unsupported script');
  3630. // A genuinely short English document still reports Too short.
  3631. const en = AIDetector.analyzeText('Short text here.');
  3632. assert.equal(en.label, 'Too short');
  3633. assert.equal(en.unsupportedScript, undefined);
  3634. // An incidental CJK place name in a short English document is not an
  3635. // unsegmented-script document: the dominance check keeps it scorable.
  3636. const mixed = AIDetector.analyzeText('The Tokyo (東京) office owns the retry limit docs.');
  3637. assert.equal(mixed.label, 'Too short');
  3638. assert.equal(mixed.unsupportedScript, undefined);
  3639. // Korean is space-separated: it segments and scores normally.
  3640. const ko = '이 함수는 프라미스를 반환합니다. 호출자는 핸들이 나중에 재사용 가능하다고 가정해서는 안 됩니다. '.repeat(30);
  3641. const rko = AIDetector.analyzeText(ko);
  3642. assert.notEqual(rko.label, 'Unsupported script');
  3643. assert.equal(rko.unsupportedScript, undefined);
  3644. });
  3645. // #242: pin the stylometric signals independently of aggregate score, which
  3646. // can stay green when another detector happens to fire on the same document.
  3647. test('uniformity: five equal long sentences fire, varied rhythm stays clean', () => {
  3648. const sentence = 'The worker reads every queued message before it writes the result to disk.';
  3649. const issues = AIDetector.analyzeText(Array(5).fill(sentence).join(' ')).issues
  3650. .filter(i => i.type === 'uniformity');
  3651. assert.deepEqual(issues.map(i => i.text), [
  3652. 'Sentence lengths cluster around 13 words (low variation)',
  3653. ]);
  3654. assert.equal(issues[0].severity, 'medium');
  3655. const varied = [
  3656. 'Stop.', sentence, 'We waited for the retry.',
  3657. 'After the connection closed, Mara checked the logs, restored the backup, and reran the batch with a smaller request limit.',
  3658. 'It worked.',
  3659. ].join(' ');
  3660. assert.equal(AIDetector.analyzeText(varied).issues.filter(i => i.type === 'uniformity').length, 0);
  3661. assert.equal(AIDetector.analyzeText(Array(4).fill(sentence).join(' ')).issues
  3662. .filter(i => i.type === 'uniformity').length, 0, 'four sentences are below the sample gate');
  3663. });
  3664. test('uniformity: sentence-length spread pins the 0.25 variation threshold', () => {
  3665. // Identical sentences sit at CV 0, so they cannot tell 0.25 from 0.01.
  3666. // These bracket the threshold: 9/12/14/16/19 words is CV 0.243 and fires;
  3667. // widening the ends to 8 and 20 words is CV 0.286 and stays clean.
  3668. const middle = [
  3669. 'The queue had cleared by noon, so we closed the incident early.',
  3670. 'After the connection dropped, the worker retried twice before it finally gave up completely.',
  3671. 'We restored the backup from Tuesday and reran the batch with a smaller request limit overnight.',
  3672. ];
  3673. const nearThreshold = [
  3674. 'Mara checked the logs and found nothing unusual there.',
  3675. ...middle,
  3676. 'Nobody could explain why the second worker still held the lock after the scheduler had already marked it done.',
  3677. ].join(' ');
  3678. const issues = AIDetector.analyzeText(nearThreshold).issues.filter(i => i.type === 'uniformity');
  3679. assert.deepEqual(issues.map(i => i.text), [
  3680. 'Sentence lengths cluster around 14 words (low variation)',
  3681. ]);
  3682. const justOver = [
  3683. 'Mara checked the logs and found nothing unusual.',
  3684. ...middle,
  3685. 'Nobody could explain why the second worker still held the lock after the scheduler had already marked it as done.',
  3686. ].join(' ');
  3687. assert.equal(AIDetector.analyzeText(justOver).issues.filter(i => i.type === 'uniformity').length, 0,
  3688. 'CV 0.286 must sit above the threshold');
  3689. });
  3690. test('#351: glossary definition lines leave the sentence-length uniformity sample', () => {
  3691. const defs = [
  3692. '**Embedding:** A list of numbers that stands for a piece of text in a search index.',
  3693. '**Token:** The unit a model reads, usually a word piece of three or four letters.',
  3694. '**Context window:** The number of tokens the model can see at one time during a call.',
  3695. '**Temperature:** A setting that controls how much randomness goes into the next token.',
  3696. '**Latency:** The time between sending a request and getting the first byte of the reply.',
  3697. '**Throughput:** The number of requests the server can finish in one second under load.',
  3698. ];
  3699. const variants = {
  3700. 'colon inside bold': defs.join('\n'),
  3701. 'bulleted, colon after bold': defs.map((d) => '- ' + d.replace(':**', '**:')).join('\n'),
  3702. 'numbered, em dash': defs.map((d, i) => `${i + 1}. ` + d.replace(':**', '** —')).join('\n'),
  3703. };
  3704. for (const [name, text] of Object.entries(variants)) {
  3705. for (const sourceMode of ['plain', 'rendered-markdown']) {
  3706. const hits = AIDetector.analyzeText('## Glossary\n\n' + text, { sourceMode }).issues
  3707. .filter((i) => i.type === 'uniformity');
  3708. assert.equal(hits.length, 0, `${name} (${sourceMode}): glossary entries are not prose rhythm`);
  3709. }
  3710. }
  3711. });
  3712. test('#351: uniform prose still fires beside a glossary and without bold leads', () => {
  3713. const sentence = 'The worker reads every queued message before it writes the result to disk.';
  3714. const prose = Array(5).fill(sentence).join(' ');
  3715. const glossary = '**Token:** The unit a model reads, usually a word piece.\n**Latency:** The time before the first byte of the reply arrives.\n**Throughput:** Requests finished per second.';
  3716. const withGlossary = AIDetector.analyzeText(prose + '\n\n' + glossary).issues.filter((i) => i.type === 'uniformity');
  3717. assert.deepEqual(withGlossary.map((i) => i.text), ['Sentence lengths cluster around 13 words (low variation)']);
  3718. // The same glossary text without bold lead terms is ordinary prose and is measured.
  3719. const unbolded = [
  3720. 'Embedding means a list of numbers that stands for a piece of text in a search index.',
  3721. 'Token means the unit a model reads, usually a word piece of three or four letters.',
  3722. 'Context window means the number of tokens the model can see at one time during a call.',
  3723. 'Temperature means a setting that controls how much randomness goes into the next token.',
  3724. 'Latency means the time between sending a request and getting the first byte of the reply.',
  3725. ].join('\n');
  3726. assert.equal(AIDetector.analyzeText(unbolded).issues.filter((i) => i.type === 'uniformity').length, 1,
  3727. 'unbolded uniform lines are prose and still fire');
  3728. // A bold phrase mid-line is emphasis, not a definition lead.
  3729. const midBold = Array(5).fill('The worker reads every **queued** message before it writes the result to disk.').join('\n');
  3730. assert.equal(AIDetector.analyzeText(midBold).issues.filter((i) => i.type === 'uniformity').length, 1,
  3731. 'mid-line bold is not a definition lead');
  3732. });
  3733. test('#351: a repeated bold label is not a glossary, and needs three distinct terms', () => {
  3734. const sentence = 'The worker reads every queued message before it writes the result to disk.';
  3735. const notes = Array(5).fill('**Note:** ' + sentence).join('\n');
  3736. assert.equal(AIDetector.analyzeText(notes).issues.filter((i) => i.type === 'uniformity').length, 1,
  3737. 'five **Note:** lines are prose with a label and stay in the sample');
  3738. const twoTerms = ['**Token:** ' + sentence, '**Latency:** ' + sentence, sentence, sentence, sentence].join('\n');
  3739. assert.equal(AIDetector.analyzeText(twoTerms).issues.filter((i) => i.type === 'uniformity').length, 1,
  3740. 'two bold leads are below the glossary minimum');
  3741. });
  3742. test('#351: a wrapped glossary entry leaves the sample whole', () => {
  3743. // Removing only the first physical line of a wrapped entry used to leave the
  3744. // continuation behind as an extra sentence beside uniform prose.
  3745. const p = 'Mara checked the logs and found nothing unusual in them today.';
  3746. const text = [
  3747. p, p, p, p,
  3748. '**Retry budget:** The number of attempts a worker makes before it gives up on a job and',
  3749. 'moves it to the dead-letter queue for a person to inspect later.',
  3750. '**Lease:** How long a worker holds a job before another worker may take it.',
  3751. '**Backoff:** The wait between attempts, doubled after each failure.',
  3752. ].join('\n');
  3753. assert.equal(AIDetector.analyzeText(text).issues.filter((i) => i.type === 'uniformity').length, 0,
  3754. 'four prose sentences remain, below the five-sentence gate');
  3755. });
  3756. test('uniformity: equal paragraph sizes fire, varied paragraph sizes stay clean', () => {
  3757. const paragraph = 'Mara checked the logs. The queue had cleared. We closed the incident.';
  3758. const issues = AIDetector.analyzeText(Array(4).fill(paragraph).join('\n\n')).issues
  3759. .filter(i => i.type === 'uniformity');
  3760. assert.deepEqual(issues.map(i => i.text), ['All paragraphs are ~3 sentences']);
  3761. assert.equal(issues[0].severity, 'low');
  3762. const varied = [
  3763. 'Mara checked the logs.',
  3764. 'The queue had cleared. We closed the incident.',
  3765. paragraph,
  3766. 'One task failed. ' + paragraph + ' The workers stopped.',
  3767. ].join('\n\n');
  3768. assert.equal(AIDetector.analyzeText(varied).issues.filter(i => i.type === 'uniformity').length, 0);
  3769. assert.equal(AIDetector.analyzeText(Array(3).fill(paragraph).join('\n\n')).issues
  3770. .filter(i => i.type === 'uniformity').length, 0, 'three paragraphs are below the sample gate');
  3771. });
  3772. test('formatting: four bold spans fire, three ordinary emphases stay clean', () => {
  3773. const clauses = [
  3774. 'Read the **log** before you change the limit.',
  3775. 'Check the **queue** when the worker stops.',
  3776. 'Keep a **backup** until the migration finishes.',
  3777. 'Use the **timestamp** to find the failed request.',
  3778. ];
  3779. const issues = AIDetector.analyzeText(clauses.join(' ')).issues.filter(i => i.type === 'formatting');
  3780. assert.deepEqual(issues.map(i => i.text), ['4 bold phrases']);
  3781. assert.equal(AIDetector.analyzeText(clauses.slice(0, 3).join(' ')).issues
  3782. .filter(i => i.type === 'formatting').length, 0);
  3783. });
  3784. test('confidence-calibration: three raw matches fire, two stay clean', () => {
  3785. const clauses = [
  3786. 'Interestingly, the retry completed after the connection reopened.',
  3787. 'Surprisingly, the old worker still held the lock.',
  3788. 'Importantly, no messages were lost during the restart.',
  3789. ];
  3790. const issues = AIDetector.analyzeText(clauses.join(' ')).issues
  3791. .filter(i => i.type === 'confidence-calibration');
  3792. assert.deepEqual(issues.map(i => i.text.toLowerCase()).sort(), [
  3793. 'importantly', 'interestingly', 'surprisingly',
  3794. ]);
  3795. assert.equal(AIDetector.analyzeText(clauses.slice(0, 2).join(' ')).issues
  3796. .filter(i => i.type === 'confidence-calibration').length, 0);
  3797. });
  3798. test('confidence-calibration: repeated wording satisfies the pre-dedup gate', () => {
  3799. const sentence = 'Interestingly, the retry completed after the connection reopened.';
  3800. const issues = AIDetector.analyzeText(Array(3).fill(sentence).join(' ')).issues
  3801. .filter(i => i.type === 'confidence-calibration');
  3802. assert.equal(issues.length, 1, 'three raw matches become one displayed finding');
  3803. assert.equal(issues[0].text.toLowerCase(), 'interestingly');
  3804. assert.equal(AIDetector.analyzeText(Array(2).fill(sentence).join(' ')).issues
  3805. .filter(i => i.type === 'confidence-calibration').length, 0);
  3806. });
  3807. test('fnword-trigram-entropy: low entropy fires, varied grammar stays clean', () => {
  3808. const repeated = 'the and of '.repeat(50) + 'I can send it if you want to see what she has written.';
  3809. const issues = AIDetector.analyzeText(repeated).issues
  3810. .filter(i => i.type === 'fnword-trigram-entropy');
  3811. assert.equal(issues.length, 1);
  3812. assert.match(issues[0].text, /^Function-word trigram entropy .* \(low\)$/);
  3813. assert.equal(issues[0].severity, 'medium');
  3814. const varied = [
  3815. 'Mara checked the logs before lunch and found no failed requests.',
  3816. 'If you can reproduce this locally, send us the input and the timestamp.',
  3817. 'She had already read our notes when the second worker stopped.',
  3818. 'We will use the old queue until they tell us why their patch failed.',
  3819. 'It was a small change, but he could not deploy it without a review.',
  3820. 'The scheduler should wait for a response rather than assume that the task is done.',
  3821. 'I have kept your backup here so that you can restore it when needed.',
  3822. 'Those jobs were started by another process which has since exited.',
  3823. 'Do not remove this check because it catches a failure we have seen before.',
  3824. 'What happens after a timeout depends on whether there is room in the queue.',
  3825. 'They may retry from the saved cursor or ask for a new snapshot.',
  3826. 'Our test writes a file to disk and then reads its contents back.',
  3827. ].join(' ');
  3828. const clean = AIDetector.analyzeText(varied);
  3829. assert.ok(clean.stats.wordCount >= 150, 'negative control must reach the entropy gate');
  3830. assert.equal(clean.issues.filter(i => i.type === 'fnword-trigram-entropy').length, 0);
  3831. });
  3832. test('fnword-trigram-entropy: single trigram fires at 150 words, not 149', () => {
  3833. const issues = AIDetector.analyzeText('the '.repeat(150)).issues
  3834. .filter(i => i.type === 'fnword-trigram-entropy');
  3835. assert.deepEqual(issues.map(i => i.text), ['Single function-word trigram repeated across document']);
  3836. assert.equal(issues[0].severity, 'high');
  3837. assert.equal(AIDetector.analyzeText('the '.repeat(149)).issues
  3838. .filter(i => i.type === 'fnword-trigram-entropy').length, 0);
  3839. });
  3840. test('#234: non-string input reports the expected type', () => {
  3841. for (const input of [123, null, undefined, false, {}, []]) {
  3842. assert.throws(() => AIDetector.analyzeText(input), {
  3843. name: 'TypeError',
  3844. message: 'analyzeText(text): argument must be a string',
  3845. });
  3846. }
  3847. });
  3848. test('#234: empty and whitespace-only input preserve selected modes', () => {
  3849. for (const text of ['', ' \n\t']) {
  3850. const r = AIDetector.analyzeText(text, {
  3851. contextMode: 'technical', sourceMode: 'rendered-markdown',
  3852. });
  3853. assert.equal(r.label, 'Empty');
  3854. assert.equal(r.stats.wordCount, 0);
  3855. assert.equal(r.stats.contextMode, 'technical');
  3856. assert.equal(r.stats.sourceMode, 'rendered-markdown');
  3857. }
  3858. });
  3859. test('false-concession requires the vague second half in the same sentence (#211)', () => {
  3860. const mustFire = [
  3861. 'While the model is impressive, real-world deployment remains a challenge.',
  3862. 'Although OpenAI has made strides, meaningful questions still remain unanswered.',
  3863. 'While the model is impressive, there is still work to do.',
  3864. // Paired with the must-not-fire case below: moving the same vague close
  3865. // into the second clause, after the opening clause's own separator, is
  3866. // exactly the two-half structure #211 asks for.
  3867. 'While the model is impressive, it remains a challenge to maintain, so we plan to replace it next month.',
  3868. // The vague close still fires when a later clause supplies a concrete action.
  3869. 'While the model is impressive, deployment remains a challenge; we reduced p99 latency by 30 percent.',
  3870. ];
  3871. for (const text of mustFire) {
  3872. const r = AIDetector.analyzeText(text, { contextMode: 'technical' });
  3873. assert.ok(!r.tooShort, `fixture must clear the length gate (wordCount >= 10): ${text}`);
  3874. const types = new Set(r.issues.map((i) => i.type));
  3875. assert.ok(types.has('false-concession'), `expected false-concession flag: ${text}`);
  3876. }
  3877. const mustNotFire = [
  3878. 'Despite these challenges, the team shipped the release on schedule and the error rate held flat through the first week of traffic.',
  3879. 'While Postgres is impressive at this scale, our write pattern is append-only, so we moved the hot table to a log-structured store instead.',
  3880. // A later vague phrase cannot turn a concrete continuation into an empty frame.
  3881. 'While the model is impressive, it reduced p99 latency by 30 percent; deployment remains a challenge.',
  3882. // Qodo's own example sentence for this rule, verbatim: "do" without a
  3883. // trailing word boundary used to match as a bare prefix of "download".
  3884. 'While the model is impressive, there is still work to download before we can run it.',
  3885. // A variant of Qodo's sentence, not verbatim, covering the same
  3886. // trailing-word-boundary bug with a different tail after "download".
  3887. 'While the model is impressive, there is still work to download the update.',
  3888. // Both phrases sit inside the opening "while" clause, with no separator
  3889. // between "is impressive" and the vague close; the concrete main clause
  3890. // ("we plan to replace it next month") is never reached. See #359.
  3891. 'While the model is impressive and remains a challenge to maintain, we plan to replace it next month.',
  3892. // Known limitation (#359): a period inside an abbreviation ends the gap
  3893. // like a real sentence boundary, so a close past "U.S." is deliberately
  3894. // missed rather than risk crossing a genuine sentence.
  3895. 'While the model is impressive, U.S. deployment remains a challenge for the team and its partners.',
  3896. ];
  3897. for (const text of mustNotFire) {
  3898. const r = AIDetector.analyzeText(text, { contextMode: 'technical' });
  3899. const types = new Set(r.issues.map((i) => i.type));
  3900. assert.ok(!types.has('false-concession'), `bare opener without a vague close in the same sentence must not flag: ${text}`);
  3901. }
  3902. });
  3903. test('#211: exact nine-word example is length-gated alone, flags within a longer input', () => {
  3904. // The issue's own example is nine words, one short of the ten-word
  3905. // scoring minimum. Padding it (e.g. adding "still") to clear that gate,
  3906. // as an earlier fixture did, leaves the exact reported sentence untested.
  3907. // This covers both outcomes without touching the minimum-word threshold.
  3908. const original = 'Although OpenAI has made strides, meaningful questions remain unanswered.';
  3909. const short = AIDetector.analyzeText(original, { contextMode: 'technical' });
  3910. assert.equal(short.stats.wordCount, 9, 'the reported example is nine words');
  3911. assert.equal(short.tooShort, true, 'nine words alone must stay under the length gate');
  3912. assert.equal(short.issues.length, 0, 'too-short input reports no issues');
  3913. const withinLongerInput = AIDetector.analyzeText(`${original} More analysis follows.`, { contextMode: 'technical' });
  3914. assert.ok(!withinLongerInput.tooShort, 'appending a sentence must clear the length gate');
  3915. const types = new Set(withinLongerInput.issues.map((i) => i.type));
  3916. assert.ok(types.has('false-concession'), 'the exact reported sentence must flag once the input clears the length gate');
  3917. });
  3918. // #213: a must-fire and a must-not-fire fixture for six phrase-level types.
  3919. // Each must-not-fire sentence is ordinary prose that shares the trigger's
  3920. // words without its frame, so it pins the pattern boundary rather than a
  3921. // random clean sentence.
  3922. const PHRASE_FIXTURES = [
  3923. {
  3924. type: 'sycophantic',
  3925. fires: "You're absolutely right, and I should have caught that earlier. The function returns null when the cache is empty, so the caller needs a guard.",
  3926. clean: 'Mara said the estimate was absolutely right for the first phase, and the second phase came in two weeks late because of the vendor delay.',
  3927. },
  3928. {
  3929. type: 'lets-construction',
  3930. fires: "Let's explore what changed in the scheduler between the two releases and why the queue backs up every Monday morning.",
  3931. clean: "Let's meet at the station at nine and walk to the venue together after we drop off the bags at the hotel.",
  3932. },
  3933. {
  3934. type: 'rhetorical-question',
  3935. fires: 'The vendor raised prices by 30 percent this quarter. But what does this mean for small teams on the starter plan who budgeted annually?',
  3936. clean: 'What does this mean in practice? Rent goes up by 40 dollars a month for tenants in the older buildings starting in March.',
  3937. },
  3938. {
  3939. type: 'novelty-inflation',
  3940. fires: 'Here is a problem nobody talks about: most teams never test their backups, so the first restore happens during an outage.',
  3941. clean: 'Nobody talks about the budget at the Monday meeting, so the problem stays on the agenda until finance joins on Thursday.',
  3942. },
  3943. {
  3944. type: 'vague-attribution',
  3945. fires: 'Experts believe the new policy will cut wait times, but the hospital has not published any numbers since the change in June.',
  3946. clean: 'Studies of the 2019 cohort, published by the state health department in May, found that wait times fell by 12 percent.',
  3947. },
  3948. {
  3949. type: 'tier1-clarity',
  3950. fires: 'We moved the job to the night shift in order to free up the build machines during the day for the release team.',
  3951. clean: 'The forms are filed in order of arrival, and the clerk stamps each one with the date before it goes to the archive room.',
  3952. },
  3953. ];
  3954. for (const { type, fires, clean } of PHRASE_FIXTURES) {
  3955. test(`#213: ${type} fires on its frame and stays clean on nearby ordinary prose`, () => {
  3956. const hit = AIDetector.analyzeText(fires);
  3957. assert.ok(!hit.tooShort, 'must-fire fixture must clear the length gate');
  3958. assert.ok(hit.issues.some((i) => i.type === type), `${type} should fire on: ${fires}`);
  3959. const miss = AIDetector.analyzeText(clean);
  3960. assert.ok(!miss.tooShort, 'must-not-fire fixture must clear the length gate');
  3961. assert.ok(!miss.issues.some((i) => i.type === type), `${type} must not fire on: ${clean}`);
  3962. });
  3963. }
  3964. // #216: Tier 3 density is per word, not aggregate. The threshold is
  3965. // max(3, floor(wordCount * 0.03)) uses of ONE listed form, so many different
  3966. // Tier 3 words can pass 3% together without a flag.
  3967. test('#216: tier3 counts each word on its own; spread vocabulary stays clean, one repeated word fires', () => {
  3968. const filler = 'The crew met on Tuesday to walk the site and check the drainage before the concrete pour.';
  3969. const forms = ['significant', 'innovative', 'effective', 'dynamic', 'scalable', 'compelling',
  3970. 'unprecedented', 'exceptional', 'remarkable', 'sophisticated', 'instrumental', 'verbatim',
  3971. 'innovation', 'dynamics', 'scalability', 'remarkably', 'effectively', 'significantly'];
  3972. const spread = forms.map((w) => `The ${w} part came up again. ${filler}`).join(' ');
  3973. const spreadResult = AIDetector.analyzeText(spread);
  3974. assert.ok(forms.length / spreadResult.stats.wordCount >= 0.03,
  3975. 'the spread fixture must exceed 3% Tier 3 vocabulary in aggregate');
  3976. assert.ok(!spreadResult.issues.some((i) => i.type === 'tier3'),
  3977. 'distinct Tier 3 words under the per-word threshold must not fire');
  3978. const repeated = Array.from({ length: 9 }, () => `The significant part came up again. ${filler}`)
  3979. .concat([filler, filler]).join(' ');
  3980. const repeatedResult = AIDetector.analyzeText(repeated);
  3981. const threshold = Math.max(3, Math.floor(repeatedResult.stats.wordCount * 0.03));
  3982. assert.ok(9 >= threshold, `nine uses must reach the per-word threshold (${threshold})`);
  3983. assert.ok(repeatedResult.issues.some((i) => i.type === 'tier3' && i.text.includes('"significant"')),
  3984. 'one word repeated past the threshold must fire');
  3985. });
  3986. // #216: Pin the floor and minimum at exact threshold boundaries.
  3987. test('#216: tier3 rounds down the percentage and requires at least three uses', () => {
  3988. for (const [wordCount, uses, fires] of [[133, 2, false], [133, 3, true], [241, 6, false], [241, 7, true]]) {
  3989. const text = Array(uses).fill('significant').concat(Array(wordCount - uses).fill('crew')).join(' ');
  3990. const result = AIDetector.analyzeText(text);
  3991. assert.strictEqual(result.stats.wordCount, wordCount);
  3992. assert.strictEqual(result.issues.some((i) => i.type === 'tier3' && i.text.includes('"significant"')), fires,
  3993. `${uses} uses in ${wordCount} words must ${fires ? 'fire' : 'stay clean'}`);
  3994. }
  3995. });
  3996. test('#216: separately listed Tier 3 inflections do not share a density bucket', () => {
  3997. const text = ['significant', 'significant', 'significantly', 'significantly', ...Array(129).fill('crew')].join(' ');
  3998. const result = AIDetector.analyzeText(text);
  3999. assert.strictEqual(result.stats.wordCount, 133);
  4000. assert.ok(!result.issues.some((i) => i.type === 'tier3'), 'two uses of each listed form must stay below the three-use floor');
  4001. });
  4002. // #212: Literal-sense carve-outs for significance-inflation and template-phrase.
  4003. test('#212: significance-inflation fires when inflating word precedes evolution-of', () => {
  4004. const cases = [
  4005. 'This is a new chapter in the evolution of artificial intelligence.',
  4006. 'We are proud to play a role in the evolution of modern software development.',
  4007. 'It represents a turning point in the evolution of how teams build products.',
  4008. ];
  4009. for (const text of cases) {
  4010. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'significance-inflation');
  4011. assert.ok(hits.length >= 1, `expected significance-inflation hit on: "${text}"`);
  4012. }
  4013. });
  4014. test('#212: significance-inflation stays clean on literal evolution-of', () => {
  4015. const cases = [
  4016. 'The lens represents a key stage in the evolution of the vertebrate eye, and the fossil record shows the transition happened more than once.',
  4017. 'Modula-2 occupies an odd place in the evolution of systems languages.',
  4018. ];
  4019. for (const text of cases) {
  4020. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'significance-inflation');
  4021. assert.equal(hits.length, 0, `false positive on: "${text}" — hits: ${JSON.stringify(hits.map((i) => i.text))}`);
  4022. }
  4023. });
  4024. test('#212: template-phrase fires on vague-praise adjective before step', () => {
  4025. const cases = [
  4026. 'This is a crucial step towards democratizing AI for everyone.',
  4027. 'The launch is a major step forward for developers everywhere.',
  4028. 'It is a significant step towards building a more trustworthy web.',
  4029. ];
  4030. for (const text of cases) {
  4031. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'template-phrase');
  4032. assert.ok(hits.length >= 1, `expected template-phrase hit on: "${text}"`);
  4033. }
  4034. });
  4035. test('#212: template-phrase stays clean on concrete adjectives before step', () => {
  4036. const cases = [
  4037. 'Shipping the read-only endpoint was a first step towards the full API.',
  4038. 'That was a small step towards cutting our storage bill.',
  4039. ];
  4040. for (const text of cases) {
  4041. const hits = AIDetector.analyzeText(text).issues.filter((i) => i.type === 'template-phrase');
  4042. assert.equal(hits.length, 0, `false positive on: "${text}" — hits: ${JSON.stringify(hits.map((i) => i.text))}`);
  4043. }
  4044. });
  4045. if (failed > 0) {
  4046. console.error(`\n${failed} test(s) failed`);
  4047. process.exit(1);
  4048. }
  4049. console.log('\nAll detector fixtures passed.');

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.