Skip to content

fix: render {{thresholds.X}} in rule text and merge thresholds into extra on the preview path - #361

Merged
Sanjay Singh (san360) merged 3 commits into
microsoft:mainfrom
PharSideLabs:pr/template-thresholds
Nov 21, 2026
Merged

Sanjay Singh (san360) merged 3 commits into
microsoft:mainfrom
PharSideLabs:pr/template-thresholds

Conversation

@ZeSnarf

@ZeSnarf Mark Warren (ZeSnarf) commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Four built-in rules (instruction-bloat, mcp-tool-bloat, excessive-file-context and verbose-prompt-no-compression) use {{thresholds.X}} in their "When Triggered" text. Template variables only carried count, total, ratio, pct and extra, so those numbers rendered blank in the finding text.

This PR:

  • adds an optional thresholds field to RuleTemplateVars and fills it from the rule's thresholds in both emissionToAntiPattern (detector registry) and evaluateRule (Rule Editor preview);
  • makes evaluateRule merge thresholds into extra the same way the detector registry already does, so {{extra.<threshold>}} also renders in the preview instead of coming out blank.

No detection logic changes; only the values available to the text templates.

Related Issues

None filed; found while customising rule wording in a fork.

Checklist

  • npm run check passes (typecheck + lint + spellcheck + knip + tests)
  • Changes are covered by tests (src/core/rule-template-thresholds.test.ts covers both the detector path through detectAntiPatterns and the Rule Editor preview through evaluateRule; the rule-engine.test.ts expectation for templateVars now includes thresholds)
  • Documentation updated (if applicable): {{thresholds.<key>}} is now listed in docs/AUTHORING_RULES.md, the descriptionTemplate comment and the DSL cheat sheet, with a note that non-integer numbers print with one decimal

@ZeSnarf

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@san360 Sanjay Singh (san360) 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.

Thank you Mark Warren (@ZeSnarf) for tracking this down and for the clear write-up! The blank numbers in "larger than bytes" and "≥ chars" were easy to miss, and lining up the Rule Editor preview with the detector path is the right fix. The diff is small and focused, and the new test file reads nicely.

What we verified (current main with this PR merged):

  • Dashboard / Anti-Patterns findings: on main, instruction-bloat, excessive-file-context, verbose-prompt-no-compression and mcp-tool-bloat render an empty {{thresholds.X}} ("larger than bytes", "attached ≥ files", "of length ≥ chars"). With the PR they show 4000, 30, 800 and 40. With a personal copy of verbose-prompt-no-compression tuned to minMessageLength: 200, the finding text follows the tuned value. We checked this on real Claude Code sessions in throwaway repos and on synthetic data.
  • Rule Editor → Test Rule: on main, every rule that reads a threshold as {{extra.X}} comes out blank in the preview. That's 8 built-in rules, for example lazy-prompting ("under characters"), mega-sessions, runaway-agent-loops, late-night-coding and verbose-output. With the PR they match the Dashboard text, and so do the four {{thresholds.X}} rules.
  • No other changes: every other page's text is identical between main and the PR on the same data. Markup in a personal rule's text still renders as plain text, and {{thresholds.__proto__}}-style lookups only read values. Nothing gets written to Object.prototype.
  • Your new tests fail on main's source and pass with the fix. Typecheck, lint, knip, lockfile-lint, the full unit suite, build and bundle size all pass locally, and so does the Playwright suite. It also merges cleanly with the other open rules PR (#373), and the combined tests pass.

Two small suggestions:

  1. A test for the detector path. The one-line change in detector-registry.ts drives the Dashboard text most users see, but all three new tests go through evaluateRule (the preview). There's an inline note on the test file with a small test that covers it.

  2. A line in the authoring docs. Rule authors will now rely on {{thresholds.X}}, but docs/AUTHORING_RULES.md (the When Triggered section) lists only {{count}}, {{total}}, {{pct}} and extra.<key>. Something like "and the rule's thresholds as {{thresholds.<key>}}" would help, and so would the same words in the descriptionTemplate comment in src/core/types/rule-types.ts and the When Triggered hint in src/webview/dsl-cheatsheet.ts. One thing worth noting there: the template formats non-integer numbers with one decimal, so a fractional threshold such as maxRatio: 0.005 would render as 0.0. None of the shipped rules print a fractional threshold, so this is just good to know.

Neither point needs to block the PR. Since this comes from a fork, a maintainer will approve the CI run. Thanks again for the fix and the tests. We'd love to see more contributions like this!

expect(result.description).toBe(`limit ${rule.thresholds[key]}`);
});

it('lets the detector extra win over a threshold of the same name', () => {

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.

This test passes on main too: there evaluateRule used only emission.extra, so 77 comes out with or without the change, and the test doesn't pin which value wins. The one-line change in detector-registry.ts (the Dashboard / Anti-Patterns text) isn't covered by any test yet.

One idea that covers both, using a shipped rule whose text has a threshold and a measured value with the same name. It fails on main ("larger than bytes") and passes with this PR:

import { detectAntiPatterns } from './detector-registry';
import { createRequest, createSession } from './parser-shared';

it('renders {{thresholds.X}} in a finding, with the measured {{extra.X}} of the same name (instruction-bloat)', () => {
  const requests = [createRequest({ messageText: 'Explain the parser', responseText: 'ok', timestamp: Date.UTC(2026, 9, 1) })];
  const sessions = [createSession({ sessionId: 's', workspaceId: 'ws', workspaceName: 'ws', harness: 'Local Agent', customInstructionsBytes: 9000, requests })];
  const finding = detectAntiPatterns(requests, sessions, false).patterns.find(p => p.id === 'instruction-bloat');
  expect(finding?.description).toContain('larger than 4000 bytes (largest: 9000 bytes)');
});

…xtra on the preview path

instruction-bloat, mcp-tool-bloat, excessive-file-context and verbose-prompt-no-compression use
{{thresholds.X}} in their When Triggered text, but template variables only carried count, total,
ratio, pct and extra, so those numbers rendered blank. evaluateRule (the Rule Editor preview) also
did not merge thresholds into extra the way the detector registry does, so {{extra.<threshold>}}
rendered blank there.
The previous test went through evaluateRule and passed on main as well. This one runs
detectAntiPatterns, so it covers the Dashboard text, and it pins that the measured
{{extra.maxBytes}} wins over the maxBytes threshold of the same name.
…iables

Add it to AUTHORING_RULES.md, the descriptionTemplate comment and the DSL cheat sheet, and
note that non-integer numbers print with one decimal.
@ZeSnarf

Copy link
Copy Markdown
Contributor Author

Thanks, both make sense. We've replaced the test that passed on main with your instruction-bloat one, so the Dashboard path is covered and it pins that the measured value wins over a threshold of the same name. {{thresholds.}} is now in AUTHORING_RULES.md, the descriptionTemplate comment and the cheat sheet, with a note that non-integer numbers print with one decimal.

@san360

Copy link
Copy Markdown
Contributor

Thanks Mark Warren (@ZeSnarf) for the quick update, and for taking both suggestions on board!

We re-checked the new head on current main:

  • The new instruction-bloat test fails on main and passes with your change. It also pins the order: if the thresholds were spread after the measured values, the finding would read "largest: 4000 bytes" and the test would fail.
  • The notes in AUTHORING_RULES.md, the descriptionTemplate comment and the cheat sheet read well, including the one-decimal note.
  • Typecheck, lint, knip, lockfile-lint, the full unit suite, build and bundle size all pass. On fresh Claude Code sessions, the Anti-Patterns findings and Test Rule show the right numbers, and nothing else on the dashboard changes.

One small thing, and it's on us: we suggested replacing the evaluateRule test, but that test was the one checking the merge order on the Rule Editor preview path (Test Rule). It couldn't tell main and this PR apart, which is why we flagged it, but it would catch { ...emission.extra, ...rule.thresholds } creeping into rule-engine.ts later. If you like, adding it back next to the new test keeps both paths covered:

it('lets the measured extra win over a threshold of the same name on the preview path', () => {
  const rule = load('mcp-tool-bloat');
  rule.descriptionTemplate = '{{extra.maxToolsPerSession}}';
  expect(evaluateRule(rule, { count: 1, total: 1, ratio: 1, examples: [], extra: { maxToolsPerSession: 77 } }).description).toBe('77');
});

That's entirely optional. From our side this looks good to merge once a maintainer approves the CI run. Thanks again for the fix and the quick turnaround!

@san360
Sanjay Singh (san360) merged commit 9eb9084 into microsoft:main Nov 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants