Skip to content

fix: keep scope both and the rest of a rule in the Rule Editor form, and read CRLF rules - #339

Merged
Sanjay Singh (san360) merged 8 commits into
mainfrom
san360-fix/rule-editor-scope-and-crlf
Nov 2, 2026
Merged

Sanjay Singh (san360) merged 8 commits into
mainfrom
san360-fix/rule-editor-scope-and-crlf

Conversation

@san360

@san360 Sanjay Singh (san360) commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Changes since aymenfurter's review (38e8472)

Finding (thread): live threshold tuning used a different thresholds: block than the form and the rule engine. readRuleFields and parseRule use the last thresholds: block, but the Test Rule sliders (parseThresholdsFromMarkdown and applyThresholdOverrides) read and replaced the first one. In a rule with limit: 5 and then limit: 10, the form showed 10 and the slider showed 5. Moving the slider to 6 left the effective threshold at 10, so Test Rule returned CLEAN where the value on screen implies TRIGGERED.

Fix. The sliders now read and set the block that the form and the rule parser use: the last one. Earlier blocks stay as written. I didn't add a parser and kept the change minimal:

  • activeThresholdLines (rule-form-markdown.ts, 8 lines) returns the line range of the block that readRuleFields and mergeRuleMarkdown already find with lastBlockIndex. lastBlockIndex follows parseFrontmatter's rule that the last key wins.
  • parseThresholdsFromMarkdown and applyThresholdOverrides use that range instead of their own regexes, which matched the first block. How they read and write each line is unchanged.
  • rule-parser.ts is untouched, so this doesn't compete with the canonical source-span traversal that fix: change only the edited threshold line when tuning a rule #350 is moving into rule-parser.ts.

Impact. Only rules that repeat thresholds: change, and none of the 45 built-in rules does. This machine has no personal or project rule files. For each of the 45 built-in rules, the old and new helpers read the same values, which match parseRule's thresholds, with LF and CRLF line endings, and write byte-identical markdown for the same overrides. Two edge cases now also follow the form and the parser:

  • the entries after a comment at column 0 inside the block (the old regex stopped at the comment);
  • a thresholds: line followed by spaces or | (the old regex didn't find that block).

Regression tests, each of which fails without the fix:

  • page-antipatterns-editor-form.test.ts › Rule Editor Test Rule › tunes the thresholds block the rule uses with the sliders, and keeps an earlier one. This is the reviewer's case run through the wired editor, the real testRuleLive handler and the real parser. It's scaled to the shared fixture of 40 requests: limit: 5, then limit: 50, with match: true and check: count >= thresholds.limit. The test checks the slider's value and label, the threshold that parseRule reads from the markdown sent to testRuleLive, and the result. First they're 50 and CLEAN. After the slider moves to 35, they're 35 and TRIGGERED (40 / 40), and the earlier block is unchanged.
  • page-antipatterns-editor.test.ts: with more than one thresholds block (2 tests, which compare against parseRule and readRuleFields), and reads and sets the entries after a comment in the thresholds block, as the rule parser does.
  • tests/e2e/antipatterns.spec.ts › Anti-Patterns Rule Editor › tunes the thresholds block the rule uses with a slider, and keeps an earlier one, in Chromium with the built bundle.

Mutation checks

Id Mutant Verdict
M43 activeThresholdLines uses the first thresholds: block killed (3 vitest)
M44 The slider helpers as at 654ac61, with the fix reverted killed: 4 vitest; the new e2e spec fails (slider "5", expected "30")

Validation on 38e8472. Local runs used Node 22.23.4, with HOME, USERPROFILE, APPDATA, LOCALAPPDATA, XDG_CONFIG_HOME, XDG_DATA_HOME, COPILOT_HOME and TEMP set to temporary directories.

Command Result
npm run check passed: typecheck clean; lint 0 errors; spellcheck, knip and lockfile-lint clean; vitest 93 files, 2,230 passed, 5 skipped
npx vitest run src/webview/rule-form-markdown.test.ts src/webview/page-antipatterns-editor-form.test.ts src/webview/page-antipatterns-editor.test.ts src/webview/panel-rpc.test.ts passed: 513 tests (the reviewer's 509 and the 4 new ones)
npm run build passed
npm run check-size passed: dist/extension.js 1.64 MB, within the 2 MB budget
npx playwright test tests/e2e/antipatterns.spec.ts --workers=2, with a temporary config and spec copy on port 4339 because this base predates #324 16 of 16 passed, including the new spec. An earlier run, with the CPU at 100%, timed out once in the shared beforeEach navigation of an unrelated test (keeps Curiosity under Prompt Quality). That test passed on re-run.
npm run test:vscode not run locally, because the change affects only the webview's Test Rule sliders. CI ran the packaged-VSIX suites (below).
CI verify (run 41328191618) passed: vitest 93 files, 2,231 passed, 4 skipped; Playwright 133 passed; VS Code desktop suites 1 and 7 passing
Copilot review of 38e8472 (review) no open issues, no inline comments
git merge-tree --write-tree HEAD origin/san360-fix-rule-edit-source-listener (#348, stacked on this PR) clean, exit 0. On the merged tree, the 5 Rule Editor and RPC suites pass (532 tests), and tsc --noEmit is clean.

Overview

Fixes two Rule Editor bugs in the Anti-Patterns page: the form dropped scope: both (#328), and it read rules with CRLF line endings as blank (#332).

Redesign of the form round trip. The form used to rebuild the whole rule from its fields, so anything it didn't show was lost. Now the form writes only the fields you change back into the loaded source, in place (front-matter keys by key, sections by heading, the detect block where the rule parser reads it). Everything else stays as it was: front-matter keys without a field, the # Tests and # Test Cases sections, other sections, text around the detect block, and the order. If nothing is edited, the rule is written back unchanged. The hidden rf-patterns, rf-filetypes and rf-extra-fm textareas are removed. The parsing and merging live in a new DOM-free module, src/webview/rule-form-markdown.ts.

Also fixed by the same round trip (found by the corpus test of all 45 built-in rules):

  • patterns and fileTypes are kept. The hidden textareas trimmed the first pattern's indentation, so saving frustration-signals, low-constraint-usage, low-markdown-ratio or no-spec-driven-development from the form lost their patterns, and low-markdown-ratio its fileTypes.
  • Multi-line Examples are kept. #rf-examples was a single-line <input>, which drops line breaks (no-plan-mode, no-skills, no-slash-commands). It's a textarea now, in its own commit (60028f9).
  • A draft without front matter still gets the form's front matter. This covers an AI draft kept after a failed retry, or a source whose front matter was deleted in the Source tab.

User-visible changes (beyond the fixes above; from the review rounds below, accepted for this PR):

  • Test Rule and Save need detection logic: a detect block with a non-empty match: or check:. Otherwise the editor shows an error and runs or saves nothing. This also applies to rules whose only logic is a # Filter/# Trigger section (as the Rule Playground writes them), a metric: or an extends: parent. Why: Test Rule and detection run only the detect block, and a pipeline with neither expression matches every row (rule-pipeline.ts:65-68, :186-187, :253-255). An extends: child also keeps its own source, so it doesn't run its parent's logic (:326). On the real-data snapshot, each such rule triggers on all 2,503 of 2,503 requests; the same filter as a detect match: matches 0. These rules already ran that way on main, so the change is that the editor now refuses them instead of running or saving them. Rules loaded from disk and the Playground's own Save are unchanged.
  • The form reads sections the way the rule parser does. Every line that starts with # and whitespace starts a section, even inside a code block, so the form shows and edits the text the rule actually uses.
  • A section whose heading is inside the detect block is read-only in the form, with a hint to use the Source tab, because rewriting it would rewrite detection directives.
  • Saving from the editor writes LF line endings, also for a rule loaded with CRLF.

AI rule generator. validateRuleMarkdown (panel-rpc.ts) now accepts scope: both, which its own prompt shows in an example, and cleanRuleMarkdown normalizes CRLF replies. The DSL cheat sheet lists both.

Review follow-up (24ed4e2). From the Copilot review:

  • Quoted values such as scope: "both" are read without their quotes, as the rule parser reads them.
  • A comment inside a front-matter key's block keeps the entries after it with the key, as in the rule parser. Comments after a rewritten key are kept.
  • A code fence closes only on a matching fence, so a ~~~ line inside a backtick fence no longer hides the sections after it. (In 401b258, the scanner stopped tracking fences altogether, as the rule parser does; see below.)
  • In a text section that contains a detect block, the form edits only the text before the block; the block stays in place.
  • Test Rule and Save refuse a rule with no detection logic, which the rule pipeline would run as match-every-row (tightened in e284ba1, below). On main, the form silently filled in the template's logic instead.
  • The AI generator's scope must be exactly requests, sessions or both.

Second review follow-up (e284ba1). From the Copilot re-review:

  • Test Rule and Save need a detect block with a non-empty match: or check:, read the way parsePipeline reads it: trimmed lines, backslash continuations, case-insensitive keys, last value wins. Detection and Test Rule run nothing else. So a rule whose only logic is a # Filter section, metric:, extends: or an empty block is refused instead of being run as match-every-row. The tests check each case against the real parsePipeline.
  • Clearing Detection Logic in an existing rule empties its detect block instead of writing the new-rule template's match: messageLength > 0. Only a new rule gets the template.
  • Text sections end at the detect marker the rule parser finds: three backticks and detect anywhere in the line. Four-backtick and indented detect blocks stay out of the Description and Examples text, so replacing that text keeps them.
  • The AI generator strips a reply's closing fence only when an outer fence was opened. Before, a raw rule that ended with its detect block lost its closing fence.

Third review follow-up (401b258). From the summary of the third Copilot review, which had no inline threads:

  • The form reads sections the way the rule parser does. Like parseSection, the scanner splits at every line that starts with # and whitespace, even inside a code fence. A four-space-indented fence or a mid-line detect opener no longer hides the sections after it. A fenced heading that the parser reads as a section is the one the form shows and edits. rule-parser.ts is unchanged.
  • The form reads and rewrites the last occurrence of a duplicated front-matter key, which is the one the rule parser uses.
  • Only a form opened for a new rule gets the template's detect logic. A loaded rule that's rebuilt from the form after its Source text was deleted keeps cleared logic cleared.
  • The AI generator strips the closing fence only when a fence is still open where the rule starts, not after a closed example in the prose.

None of the 45 built-in rules has a fenced or deeply indented heading, a mid-line detect opener or a duplicated key, so none of them changes.

Fourth review follow-up (654ac61). Because the form now reads sections like the parser, a heading inside the detect block, such as a # Examples line among its directives, starts a section. Rewriting that section from the form would rewrite detection directives and the closing fence. Such a section is now read-only in the form, with a hint to edit it in the Source tab, and the merge never writes it. No built-in rule has one.

LF-only parsing paths fixed:

Path Before
page-antipatterns-editor.ts markdownToForm: front matter, sections, detect block, thresholds, patterns, fileTypes ^---\n…\n---, # H\n…(?=\n# ), ```detect\n, key:\n
page-antipatterns-editor.ts parseThresholdsFromMarkdown (threshold sliders) ^---\n…\n---, thresholds:\n
page-antipatterns-editor.ts applyThresholdOverrides (slider overrides) thresholds:\n
panel-rpc.ts validateRuleMarkdown, via cleanRuleMarkdown (Generate) ^---\n, \n---\n, ```detect\n

Audited and already CRLF-tolerant, so unchanged: rule-parser.ts, rule-pipeline.ts (parsePipeline), metric-engine.ts (parseMetric, parseRuleExtensions, # Test Cases), rule-compiler.ts, and the Rule Playground (# Filter\s*\n). These use \s*\n and trimmed lines. The webview has no metric editor, and page-rule-editor.ts is unused and parses nothing.

Validation

Local runs on the head, 654ac61, except where noted:

Command Result
npm run check passed: typecheck clean; lint 0 errors, 430 warnings (431 on base); spellcheck 0 issues; knip and lockfile-lint clean; vitest 93 files, 2,226 passed, 5 skipped
npm run build passed
npm run package passed: ai-engineer-coach-0.1.0.vsix, 71 files, 1.50 MB
npm run check-size passed: dist/extension.js 1.64 MB, within the 2 MB budget
npm run test:vscode -- --vsix ai-engineer-coach-0.1.0.vsix passed: empty scenario 1 passing, populated 7 passing
Playwright e2e, all specs (temporary config on a private port, since this base predates #324) 131 passed, 1 failed, 7 skipped; the 2 new Rule Editor specs pass. The failure was the Curiosity wheel-scroll spec, which this PR doesn't touch; it passed 10 of 10 on re-run. That's the Chromium scroll flake that #324 fixes. On 401b258, all 132 passed.

New tests:

  • rule-form-markdown.test.ts:
    • all 45 built-in rules, LF and CRLF, read the same and round-trip unchanged; with every field rewritten, they parse to the same rule and parseRuleExtensions, with # Tests kept;
    • edits in place, additions, and a body-only base;
    • quoted values, comments inside a front-matter block, nested fences, and detect blocks inside a text section (including four-backtick and indented ones);
    • headings inside code fences, four-space-indented fences and mid-line detect openers, read as the rule parser reads them; the last occurrence of a duplicated key; a section whose heading is inside the detect block, which is never written (and no built-in rule has one);
    • cleared detect logic: the block is emptied, none is added, and an empty base gets the template's logic only for a new rule;
    • hasDetectionLogic, case by case against the real parsePipeline, and on every built-in rule.
  • page-antipatterns-editor-form.test.ts (jsdom, the page's own markup through renderAntiPatterns, real testRuleLive handler on fixture sessions):
    • every built-in rule, LF and CRLF: fills the form the same way, round-trips unchanged with nothing edited or with the name edited, and shows every field exactly;
    • Both is selected for the two both rules, and for a quoted scope: "both";
    • Test Rule on context-engineering-gaps and no-devcontainer returns ok: true and the same result as the rule sent directly, also with an edited threshold;
    • Save keeps form and Source tab edits; a new rule gets the template; a generated draft without front matter gets the form's front matter;
    • neither Test Rule nor Save runs for a draft without detection logic, a rule or generated draft whose detect logic was cleared (also after its Source text was deleted), or a rule whose only logic is a # Filter section;
    • a section whose heading is inside the detect block is read-only, with a hint; Save keeps the detect block; New Rule clears the lock.
  • page-antipatterns-editor.test.ts: threshold sliders and overrides with CRLF.
  • panel-rpc.test.ts:
    • the generator accepts a CRLF reply and scope: both (quoted or not) without asking again, and asks again for a scope that only starts with an allowed one;
    • a raw rule that ends with its detect block keeps its closing fence, also after a closed example in the prose;
    • an outer fence is stripped with or without a language, after prose, after a closed example, and around a rule without front matter.
  • tests/e2e/antipatterns.spec.ts:
    • a both rule shows Both, and Test Rule sends it with scope: both and its # Tests;
    • a CRLF rule fills the form, and Test Rule sends that rule, not the template.

Mutation checks (vitest on 654ac61). Each mutant was applied temporarily, the tests were run, and the file was restored.

Id Mutant Verdict
M1 Scope select without Both killed: 10 vitest failures; 2 of 2 e2e failures (60028f9)
M2 Form rebuilds the rule from its fields (old behavior) killed: 44 vitest failures; 2 of 2 e2e failures (60028f9)
M3 Every field counts as changed killed (4)
M4 Examples back to a single-line input killed (5)
M5 A rewritten text section drops its detect block killed (4)
M6 An empty scalar is written as key: killed (1)
M7 No line-ending normalization killed (235)
M8 readRuleFields reads CRLF as is killed (183 vitest). In e2e it survives as an equivalent mutant: the new regexes and textarea normalization already absorb \r in the browser.
M9 mergeRuleMarkdown reads a CRLF base as is killed (92)
M10 Threshold sliders read CRLF as is killed (1)
M11 Threshold overrides read CRLF as is killed (1)
M12 Generator validates the raw CRLF reply killed (1)
M13 Generator rejects scope: both killed (2)
M14 No fallback to the loaded markdown without a Source view killed (1)
M15 No front matter: only changed keys are written killed (2)
M16 Quoted values keep their quotes killed (2)
M17 A comment ends a front-matter key's block killed (2)
M18 Any fence closes a fence retired in 401b258: the scanner no longer tracks fences (see M37)
M19 A text section isn't split at its detect block killed (5)
M20 No detection-logic check killed (16)
M21 Generator accepts a scope that only starts with an allowed one killed (2)
M22 Comments in thresholds are dropped on write killed (1)
M23 The comment after a rewritten key is dropped killed (3)
M24 Generator strips the last fence of every reply killed (2)
M25 # Filter, metric: and extends: count as detection logic killed (5)
M26 Any closed detect block counts, even an empty one killed (8)
M27 Backslash continuations are ignored killed (1)
M28 Cleared detect logic gets the template's killed (6)
M29 Cleared detect logic leaves a blank line in the block killed (1)
M30 The detect marker is anchored at the line start (24ed4e2) killed (1)
M31 Empty logic adds a detect block killed (2)
M32 A rule rebuilt from the form after its Source was deleted gets the template's detect logic killed (1)
M33 A loaded draft still counts as a new rule killed (1)
M34 New rules never get the template's detect logic killed (1)
M35 A closed fence in the prose counts as an outer fence killed (1)
M36 Only replies with front matter lose an outer fence killed (1)
M37 The fence-aware section scanner of e284ba1 killed (5)
M38 The first occurrence of a front-matter key is used killed (1)
M39 The merge writes a section whose heading is inside the detect block killed (1)
M40 No section counts as inside the detect block killed (2)
M41 New Rule keeps the previous rule's read-only sections killed (1)
M42 A loaded rule's sections inside the detect block aren't read-only killed (1)
base The 2 new e2e specs on main e6cedb7 2 of 2 fail: the scope select is empty, and the CRLF rule leaves the form blank

UI check with real data

Run on 60028f9 (base against head) and re-checked on the final head, 654ac61 (below). The later review follow-ups change only edge cases that no built-in rule has: the corpus tests show all 45 built-in rules behave the same on 654ac61, and all of them pass the detection-logic check.

Canvas host on a frozen read-only snapshot of real logs, with an isolated HOME per run. This Windows checkout has core.autocrlf=true, so npm run build copies CRLF rules into dist/rules. Base main e6cedb7 ran twice: as built (CRLF), and with dist/rules converted to LF as a Linux or CI build has them. Head 60028f9 ran as built (CRLF). "Direct" means the rule source sent to testRuleLive as is.

Step Base, CRLF build Base, LF rules Head, CRLF build
Edit Source on 5 built-in rules form blank in 5 of 5 filled; Scope blank for the 2 both rules; no-plan-mode Examples shows 1 of 3 lines filled in 5 of 5; Scope shows Both for the 2 both rules; Examples shows 3 of 3 lines
Test Rule on context-engineering-gaps (direct: TRIGGERED 1 / 5) runs the default template: TRIGGERED 2,227 / 2,449 Failed to parse rule sends the rule unchanged with scope: both: TRIGGERED 1 / 5
Test Rule on no-devcontainer (direct: CLEAN 0 / 0) default template: TRIGGERED 2,227 / 2,449 Failed to parse rule CLEAN 0 / 0
Save from the form with the name edited, no-devcontainer writes the template as my-custom-rule.md not saved: parse fails no-devcontainer.md keeps scope: both and requiresIdeContext: true; identical to the source (LF) except the name
Save from the form with the name edited, frustration-signals writes my-custom-rule.md again saved without its # Tests and patterns frustration-signals.md keeps # Tests and patterns; identical to the source (LF) except the name
After saving n/a n/a both cards show Personal; after a host restart, both load as Personal with the edited name. The canvas host has no trust gate; in VS Code, the existing gate keeps local rules pending until approved.
Console and page errors 0 and 0 0 and 0 0 and 0

Re-check on the final head, 654ac61. A fresh read-only snapshot with an isolated HOME and the CRLF build (45 of 45 dist/rules files CRLF). The snapshot is newer, so totals differ from the run above.

Step Head 654ac61, CRLF build
Edit Source on context-engineering-gaps, no-devcontainer and lazy-prompting (CRLF sources) filled in 3 of 3; Scope shows Both for the 2 both rules and Requests for lazy-prompting
Test Rule from the form sends each source unchanged (LF), with the same result as direct: context-engineering-gaps TRIGGERED 1 / 5, no-devcontainer CLEAN 0 / 0, lazy-prompting CLEAN 173 / 2,503; no parse errors
Save from the form with only the name edited no-devcontainer.md: 37 of 37 lines, 1 changed (the name), scope: both kept. frustration-signals.md: 45 of 45 lines, 1 changed (the name), # Tests kept. Both saved with LF.
Detection Logic cleared on lazy-prompting Test Rule shows the no-detection-logic error, and Save shows the same message in an alert; 0 testRuleLive and 0 saveRule calls, no file written, the editor stays open
Console and page errors 0 and 0

Related Issues

Fixes #328
Fixes #332

Found while testing #322. The new e2e specs reuse the spec's existing HARNESS_URL through navigateToAntiPatterns, so #324's URL change applies to them.

Checklist

  • npm run check passes (typecheck + lint + spellcheck + knip + tests)
  • Changes are covered by tests (if applicable)
  • Documentation updated (if applicable): docs/content/improve/rule-editor.md (scope can be both; the form writes back only what you change; CRLF rules load; a section that starts inside the detect block is read-only in the form; a draft without a match: or check: in its detect block isn't tested or saved)

Sanjay Singh (san360) and others added 3 commits October 31, 2026 21:28
…, and read CRLF rules

The form now writes only the fields changed since a rule was loaded back
into its source, in place, and keeps everything it doesn't show: other
frontmatter keys (patterns, fileTypes, requiresIdeContext and the rest),
the # Tests and # Test Cases sections, other sections, and text around
the detect block. The hidden patterns, fileTypes and extra-frontmatter
fields are gone: they re-indented patterns and fileTypes so that a saved
rule lost them. Markdown without frontmatter, such as an AI draft or a
source whose frontmatter was deleted, still gets the form's frontmatter.
The scope select offers Both.

The editor's parsers normalize CRLF line endings, so built-in rules from a
Windows checkout with core.autocrlf, and local rules saved with CRLF, fill
the form. The editor saves rules with LF line endings. The AI rule
generator normalizes replies the same way and accepts scope: both, which
its own prompt shows.

Fixes #328
Fixes #332

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Unit tests check that every built-in rule, with LF or CRLF line endings,
fills the form the same way and round-trips through it unchanged, with its
# Tests section and the same parsed rule and rule extensions, also when
every field is rewritten or one is edited. jsdom tests use the
Anti-Patterns page's own form markup, run Test Rule on
context-engineering-gaps and no-devcontainer through the real
testRuleLive handler, and save edits, Source tab changes, new rules and
a generated draft without frontmatter. Playwright tests open a scope: both
rule, with LF and CRLF line endings, in the Rule Editor and check what
Test Rule sends.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The Examples Template field was a single-line input, which drops line
breaks, so editing the multi-line examples of no-plan-mode, no-skills or
no-slash-commands in the form joined their lines. It's a textarea now,
like the other text fields.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

⚠️ Deprecation Warning: The deny-licenses option is deprecated for possible removal in the next major release. For more information, see issue 997.

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Incomplete drafts can match every request, while form parsing and merging can mishandle scopes and silently alter or delete rule content.

6 open issues
What changed in this PR

This PR fixes Rule Editor handling of scope: both and CRLF line endings, and replaces whole-rule reconstruction with targeted edits that preserve content outside the form.

Changes:

  • Adds a DOM-free markdown reader and merger to preserve unedited fields, sections, and inline tests.
  • Adds the Both scope option, multiline Examples input, and CRLF normalization in editor and generator paths.
  • Adds round-trip, integration, and browser coverage, plus updated editor documentation.
File Description
tests/​e2e/​antipatterns.spec.ts Adds browser coverage for Both scope and CRLF rules.
src/​webview/​rule-form-markdown.ts Reads form fields and merges edits into existing markdown.
src/​webview/​rule-form-markdown.test.ts Tests preservation, edits, and built-in rule round trips.
src/​webview/​panel-rpc.ts Normalizes generated line endings and accepts Both scope.
src/​webview/​panel-rpc.test.ts Tests generator acceptance of CRLF and Both scope.
src/​webview/​page-antipatterns.ts Adds Both and multiline Examples; removes hidden preservation fields.
src/​webview/​page-antipatterns-editor.ts Integrates targeted merging and normalizes threshold parsing.
src/​webview/​page-antipatterns-editor.test.ts Adds CRLF threshold-helper coverage.
src/​webview/​page-antipatterns-editor-form.test.ts Tests actual form rendering, testing, saving, and source synchronization.
src/​webview/​dsl-cheatsheet.ts Documents Both as an allowed scope.
docs/​content/​improve/​rule-editor.md Explains field preservation and CRLF support.

Review effort: Balanced

Comment thread src/webview/panel-rpc.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
- Read quoted frontmatter values without their quotes, as the rule parser
  does, so scope: "both" shows Both.
- Keep comments inside a frontmatter key's block, as the rule parser
  does, so the entries after a comment stay with their key, and keep the
  comments after a rewritten key.
- Close a code fence only with a matching fence, so a ~~~ line inside a
  backtick fence doesn't hide the sections after it.
- Edit a text section only up to a detect block in it, and keep the block
  and the text after it in place.
- Refuse to test or save a rule with no detection logic (no detect block,
  # Filter section, metric or parent rule): the rule pipeline would match
  every row.
- Require the AI generator's scope to be exactly requests, sessions or
  both.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@san360

Copy link
Copy Markdown
Contributor Author

All six points are fixed in 24ed4e2, each with tests, and each thread has a reply:

  • quoted values are read without their quotes;
  • the generator's scope must match exactly;
  • comments stay inside a front-matter key's block;
  • a fence closes only on a matching fence;
  • a text section is edited only up to a detect block in it;
  • a rule with no detection logic can't be tested or saved.

🤖 AI-authored reply via Agency Finish PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cleared, empty, or unsupported detection logic can pass Test and Save as rules that match virtually every request, potentially overwriting the original detector.

4 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve indented top-level front matter keys

src/​webview/​rule-form-markdown.ts:89

This treats every indented line as part of the preceding key, but parseFrontmatter accepts indented top-level keys outside a nested block. With name: Original followed by requiresIdeContext: true, editing the name silently deletes the IDE-only restriction; uniformly indented root keys also open as empty fields. Track whether the preceding value starts a nested block and otherwise recognize trimmed keys as the rule parser does.

Medium severity Keep the editor open when required name is missing

src/​webview/​rule-form-markdown.ts:228

[P2] A retained AI draft can have front matter and detection logic but omit name:. Because the blank Name field is unchanged, this leaves the required key missing; saveRule returns { ok: false }, yet the editor closes and discards the draft. The previous form supplied a default name. Validate or fill missing required fields and keep the editor open when saving fails.

6 resolved since last review

Review effort: Balanced

Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Comment thread src/webview/panel-rpc.ts Resolved
Comment thread src/webview/rule-form-markdown.ts Resolved
Address the second Copilot review on #339:

- Test Rule and Save need a detect block with a match: or check:
  expression, read the way the rule pipeline reads it. A # Filter
  section, metric:, extends: or an empty block would match every row,
  so the editor refuses them instead of running them.
- Clearing the Detection Logic field of a rule empties its detect block
  instead of writing the new-rule template's logic.
- Text sections end at the detect marker the rule parser finds, so a
  four-backtick or indented detect block isn't edited as text.
- The rule generator strips a closing fence only when an outer fence was
  opened, so a raw rule keeps its detect block's closing fence.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@san360

Copy link
Copy Markdown
Contributor Author

Thanks for the second review. All four points are fixed in e284ba1, with replies in each thread:

  • Unsupported or empty detection logic: Test and Save need a non-empty match: or check: in the detect block, read the way parsePipeline reads it. # Filter, metric:, extends: and empty blocks are refused.
  • Cleared Detection Logic: clearing the field of an existing rule empties its block instead of writing the template's logic, so the guard refuses it.
  • The generator's closing fence: stripped only when an outer fence was opened.
  • Detect markers: text sections end at the marker the rule parser finds, including four-backtick and indented fences.

The tests are cross-checked against the real parsePipeline. All 31 mutants are killed, npm run check passes (2,212 tests), and the e2e suite passes (132).

🤖 AI-authored reply via Agency Finish PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Clearing an existing draft can silently replace and overwrite its detector, while parsing mismatches can discard or ignore other edits.

No open issues 🎉

6 previously missed issues

In code that hasn't changed since last review

High severity Do not restore the template when existing logic is cleared

src/​webview/​rule-form-markdown.ts:288

For an existing rule, deleting all Source text, returning to Form and clearing Detection Logic makes the empty base select the generic messageLength > 0 template. Test and Save then accept that replacement, and Save can overwrite the existing detector instead of reporting missing logic. Track new-rule initialization separately from an empty source so clearing an existing draft cannot restore the template.

Medium severity Avoid stripping closed prose fences as detect-block fences

src/​webview/​panel-rpc.ts:614

A fenced example in the leading prose is also treated as an outer rule fence, even if that example was already closed before the front matter. For a raw rule ending in its detect block, this strips the detect block's closing fence; validation then accepts the unclosed block, but Test and Save refuse the draft. Check that a fence is still open around the front matter before stripping the final fence.

Medium severity Do not treat indented code as an opening fence

src/​webview/​rule-form-markdown.ts:101

A four-space-indented literal backtick line in Description is treated as an opening fence, hiding the following When Triggered, How to Improve and Detection Logic headings. Replacing Description then deletes those untouched sections while retaining a detect block that still passes the save guard. Distinguish ordinary indented code from fences while preserving the rule parser's special handling of indented detect markers.

Medium severity Recognize mid-line detect openers in the section scanner

src/​webview/​rule-form-markdown.ts:101

A mid-line detect opener such as Text: ```detect is recognized by the rule parser and splitAtDetectBlock, but not by this section scanner. Its closing fence is then treated as a new opener, hiding later sections; editing When Triggered adds a duplicate that the rule parser ignores. Recognize these detect openers here too so subsequent headings remain editable.

Medium severity Ignore fenced headings in the rule parser

src/​webview/​rule-form-markdown.ts:102

The form ignores headings inside fences, but parseSection in src/core/rule-parser.ts treats them as real headings. For example, a fenced Markdown example in Description containing # When Triggered takes precedence over the actual section when the rule runs, while the form displays and edits the later section. Saving a new When Triggered value therefore has no effect on the result. Make the rule parser and form agree on fenced headings.

Medium severity Use the last front-matter key occurrence consistently

src/​webview/​rule-form-markdown.ts:255

The rule parser uses the last occurrence of a front-matter key, but the form reads and rewrites the first one. For example, with scope: requests followed by scope: both, selecting Sessions rewrites only the first line, so Test Rule and Save still use Both. Use the last occurrence when merging here and when reading fields at line 219.

4 resolved since last review

Review effort: Balanced

Address the summary of the third Copilot review on #339:

- The section scanner splits at every line that starts with # and
  whitespace, as parseSection does, even inside a code fence. A
  four-space-indented fence or a mid-line detect opener no longer hides
  the sections after it, and a fenced heading that the parser reads as a
  section is edited as one.
- The form reads and rewrites the last occurrence of a front-matter key,
  which is the one the rule parser uses.
- Only a form opened for a new rule gets the template's detect logic. A
  loaded rule rebuilt after its Source text was deleted keeps cleared
  logic cleared.
- The rule generator strips a closing fence only when a fence is still
  open where the rule starts, not after a closed example in the prose.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@san360

Copy link
Copy Markdown
Contributor Author

Thanks. All six items in this summary are addressed in 401b258:

  1. Template restored after the Source is deleted. Only a form opened for a new rule gets the template's detect logic. A loaded rule or draft that's rebuilt from the form after its Source text was deleted leaves cleared logic out, so Test and Save refuse it. Covered by jsdom tests for both paths and mutants M32–M34.
  2. Closed prose fences. The generator strips the reply's last fence only when a fence is still open where the rule starts, at its front matter or, without front matter, its first heading. Covered by tests for a closed example before a raw rule and before an outer fence, plus mutants M35 and M36.
  3. Indented code, mid-line detect openers and fenced headings (items 3–5). The form's section scanner now splits at every line that starts with # and whitespace, exactly as parseSection does, even inside a code fence. A four-space-indented fence or a mid-line detect opener can no longer hide later sections. A fenced # When Triggered that the parser reads as the section is now the one the form shows and edits. I made the form match the parser rather than changing rule-parser.ts, which every rule and the detection engine use; that would be outside this PR's scope. Covered by a test per case, and mutant M37 swaps the old fence-aware scanner back in.
  4. Duplicate front-matter keys. The form reads and rewrites the last occurrence, which is the one the parser uses. Covered by a test with scope: requests followed by scope: both, and mutant M38.

None of the 45 built-in rules has a fenced or deeply indented heading, a mid-line detect opener or a duplicate key, so the corpus results are unchanged. npm run check passes (2,222 tests), as do e2e (132), package, check-size and test:vscode. All 37 mutants are killed.

🤖 AI-authored reply via Agency Finish PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Editing Examples inside a detect block can overwrite detection directives and the closing fence, potentially allowing altered detection logic to be saved.

1 open issue

Review effort: Balanced

Comment thread src/webview/rule-form-markdown.ts Resolved
Address the fourth Copilot review on #339. A heading inside the detect
block, such as a "# Examples" line among its directives, starts a section
for the rule parser. Rewriting that section from the form would rewrite
detection directives and the block's closing fence. So the form shows it
read-only, with a hint to edit it in the Source tab, and never writes it.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@san360

Copy link
Copy Markdown
Contributor Author

Thanks. The one open issue in this review, about Examples edits replacing detection directives, is fixed in 654ac61, with details in its thread.

A text section whose heading lies inside the detect block is now read-only in the form, with a hint to use the Source tab, and the merge never writes it. Test, Save and the detect block are covered by new tests and mutants M39–M42.

npm run check passes (2,226 tests), as do package, check-size and test:vscode. All 41 mutants are killed. The full e2e run had one failure, in the Curiosity wheel-scroll spec, which this PR doesn't touch; it passed 10 of 10 on re-run. That's the Chromium scroll flake that #324 fixes.

🤖 AI-authored reply via Agency Finish PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because preserved Markdown can cause edits to change rule behavior unexpectedly and live tests to use settings different from those displayed.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@san360

Copy link
Copy Markdown
Contributor Author

Thanks. This review has no open issues. On the "needs a closer look" note:

  • Keeping the markdown the form doesn't show is the point of Rule Editor form drops scope: both, so Test Rule fails for context-engineering-gaps and no-devcontainer #328. The form writes back only the fields you change, so # Tests, front-matter keys without a field and other sections survive.
  • Where the form can't show a value faithfully, it doesn't rewrite it. A select can't show an unknown value, so the original line stays. A section whose heading is inside the detect block is read-only.
  • The form reads fields the way the rule parser does: sections, the last occurrence of a key, quotes and CRLF.
  • Test Rule sends exactly the markdown that Save would write, except for threshold-slider overrides, which the slider panel shows and which are applied only to the test run.

I've flagged the note for human review in my report to the coordinator.

🤖 AI-authored reply via Agency Finish PR.

@aymenfurter Aymen (aymenfurter) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes: retained source and live threshold tuning use different parsing policies. The form and rule engine use the last thresholds block, but the sliders read and update the first block. This leaves the displayed threshold and test result inconsistent. Use one threshold read/write policy for both paths; do not add another independent source parser.

Reviewed the full net diff at 654ac613493fa4a5771bdcc2753758296218eddb against the live base e6cedb79f49e465216b2a7c1d138f28823d16fb0, including surrounding parser/pipeline code and PR discussion. No file crosses from below 1,000 lines to above 1,000 lines. The focused markdown module and smaller editor are otherwise reasonable; the inline comment describes the blocking parser-boundary defect.

Validation:

  • npm run test -- src/webview/rule-form-markdown.test.ts src/webview/page-antipatterns-editor-form.test.ts src/webview/page-antipatterns-editor.test.ts src/webview/panel-rpc.test.ts: passed, 509 tests.
  • npm run build: passed. npm run check-size: passed, 1.64 MB within the 2 MB budget.
  • npm run test:e2e -- tests/e2e/antipatterns.spec.ts --workers=2: passed, 15 tests.
  • npm run check: typecheck, lint, spellcheck, knip and lockfile validation passed; unit tests had 2 failures in unchanged FIFO image tests (src/core/image-data.test.ts, both replacement cases). These same failures reproduced with a read-only base-source overlay; they are not attributed to this PR. A second run with a canonical macOS temporary path still failed those tests with child-process timeouts.
  • An isolated jsdom test of the base and head editor, using the real rule parser and pipeline, confirmed the slider defect: a displayed override of 6 produces an effective threshold of 6 and TRIGGERED on the base, but an effective threshold of 10 and CLEAN on this head for seven requests.

Limits: the full browser suite and VS Code integration suite were not run. No tracked files were changed.

Comment thread src/webview/page-antipatterns-editor.ts Resolved
The Rule Editor form and the rule parser use the last `thresholds:` block,
but Test Rule's sliders read and replaced the first one. With a rule that
repeats the key, a slider showed the inactive value, and moving it left the
threshold the rule runs with unchanged, so Test Rule could report CLEAN
where the value shown implies TRIGGERED.

The sliders now find their block with the form's front-matter reader in
rule-form-markdown.ts, which follows the rule parser's last-key-wins rule.
No new parser: activeThresholdLines gives the lines of the block that
readRuleFields and mergeRuleMarkdown already use, and
parseThresholdsFromMarkdown and applyThresholdOverrides read and set only
that block, keeping earlier blocks as written. rule-parser.ts is unchanged.

Built-in rules are unaffected: none repeats `thresholds:`, and for all 45
the sliders read and write exactly what they did before.

Addresses review thread PRRT_kwDOSWQFdM6ybz-n on #339.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Subtle interactions between form edits and rule parsing warrant human assessment because saved or executed detection logic can differ from what the form displays.

No open issues 🎉

Review effort: Balanced

@aymenfurter Aymen (aymenfurter) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate-threshold-block blocker is resolved in this revision. The form, slider reader and slider writer now use the same active-block model. Inactive blocks stay unchanged. The correction removes the competing block-selection regexes rather than adding another parser. The focused helper, ownership boundaries and file sizes meet the strict code-quality bar; no new PR blocker was found.

Reviewed head: 38e84723a209110a410ba9ea4195de268e197563
Live base: e6cedb79f49e465216b2a7c1d138f28823d16fb0 (main)

I checked the correction delta, relevant net changes, author reply and regression tests. Independent wired-editor probes with the real parser/pipeline covered Form and Source, LF and CRLF, comments between entries, repeated tuning, reset and Save. The active threshold changes from 10 to 6 and the seven-request result changes from CLEAN to TRIGGERED; the earlier block stays at 5. Reset and Save keep the untuned draft threshold. Threshold reads and writes also match the parser for all 45 built-in rules.

Validation:

  • npm run check with a canonical macOS TMPDIR: passed, 2,231 tests and 4 skipped. The default TMPDIR run still had the two unchanged image-data FIFO failures already reproduced on the base in the first review.
  • npm run build and npm run check-size: passed; extension bundle is 1.64 MB within the 2 MB budget.
  • CI=1 npm run test:e2e -- tests/e2e/antipatterns.spec.ts --workers=1: passed, all 16 tests, including the new slider regression. An earlier two-worker attempt had three failures with old-form symptoms; its serving source was not verified, so I do not attribute that attempt to this revision.

Non-blocking baseline observation: the extra jsdom probe found that the adjacent number label can retain the last manually entered value after Test Rule resets the slider and executed threshold. The same issue reproduced on the actual base; this PR does not introduce it.

Limits: the full browser suite and VS Code integration suite were not run. No tracked files were changed. This approval replaces my earlier changes-requested verdict for the duplicate-block finding.

@san360
Sanjay Singh (san360) merged commit 1d976fa into main Nov 2, 2026
7 checks passed
Sanjay Singh (san360) added a commit that referenced this pull request Nov 2, 2026
Brings in #357, #339, #348, #340, stack 318 (#316, #317, #323) and #324.
Two conflicts:
- src/core/cache.ts: CACHE_VERSION is 28 on main and 29 here. Kept 29.
- src/core/parser-vscode.ts: kept this branch's per-request mode
  (requestModeFromInfo, and its comment), followed by #323's two
  modelContextLimit lines. Dropped main's old comment and its
  session-only agentMode line.
Nothing else changed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sanjay Singh (san360) added a commit that referenced this pull request Nov 2, 2026
…-activity-patterns

Brings in #322's head 112c931, which merges main 99f92d2 (#357, #339,
#348, #340, stack 318 and #324).

Conflicts, all between this branch and main:
- src/core/parser-opencode.ts: both sides added a function; kept both.
- src/core/schemas.ts: kept both sides' imports.
- src/mcp/formatters.ts: merged the type import and kept this branch's
  typed-prompt helpers.
- src/mcp/formatters.test.ts (add/add): one file with both suites.

src/core/parser-claude.ts merged without a conflict but declared
subagentFile twice. #316's parseClaudeSessionFile takes it as an
option, which its callers set for every file under subagents/, so this
branch's copy in the function body is dropped and the sub-agent marker
reads the option. CACHE_VERSION stays 30.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sanjay Singh (san360) added a commit that referenced this pull request Nov 3, 2026
Brings in main 1e1c49c: #320, #315, #357, #339, #348, #340, stack 318
(#316, #317, #323), #324, #322 and #335.

Conflicts, all between this branch and main:
- src/core/rule-parser.ts and src/core/rule-parser.test.ts (add/add):
  #340 was squash-merged, so its lines met this branch's from the old
  merge base. Merged with git merge-file and #340's head e3d59b9 as the
  base, with no hand edits. #357's and #335's lines are kept.
- src/core/detector-registry.ts: #322's buildDetector, detectorContext
  and sessionOf, with this branch's emitOrFail in buildDetector's run and
  in runEmitters. detectAntiPatterns takes sessionOf, and runDetectors
  returns its patterns. runDetector, which runInlineTests uses for a
  rule's # Tests, rethrows a RuleFailure, so a broken rule's inline tests
  raise its error instead of reading as triggered.
- src/core/analyzer-patterns.ts: detectAntiPatterns with #322's
  this.requestSessionMap.
- src/webview/panel-rpc.ts (getRuleEditor, getRulePreview): #322's
  sessionOf, and this branch's failures passed to getRulePreviewStats.
- src/mcp/formatters.ts: one type import with both sides' names; main's
  helpers kept.
- src/mcp/formatters.test.ts (add/add): main's suites, then this
  branch's, with both sides' imports.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants