From 534c344e496cd19b54c2b665c5a8a24c4ff573fa Mon Sep 17 00:00:00 2001 From: ujjwalks <16404231+ujjwalks@users.noreply.github.com> Date: Thu, 30 Jul 2026 19:28:32 +0530 Subject: [PATCH] fix(lint): strip alternate fenced code blocks Replace the whole-document regex with a line-by-line scanner that tracks each fence marker and length. This prevents headings inside tilde, indented, longer-closed, mismatched, or unterminated fences from satisfying required-section checks. Add focused node:test coverage and run it in CI. Fixes #437. --- .github/workflows/test-plugin-install.yml | 3 + scripts/lib/skill-lint-test.js | 104 ++++++++++++++++++++++ scripts/lib/skill-lint.js | 33 ++++++- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 scripts/lib/skill-lint-test.js diff --git a/.github/workflows/test-plugin-install.yml b/.github/workflows/test-plugin-install.yml index f41a66d8ed..e6d7f4c1b0 100644 --- a/.github/workflows/test-plugin-install.yml +++ b/.github/workflows/test-plugin-install.yml @@ -20,6 +20,9 @@ jobs: - name: Validate all skills run: node scripts/validate-skills.js + - name: Test skill lint rules + run: node --test scripts/lib/skill-lint-test.js + - name: Test skill eval runner run: node --test scripts/run-evals-test.js diff --git a/scripts/lib/skill-lint-test.js b/scripts/lib/skill-lint-test.js new file mode 100644 index 0000000000..1c0c8de03f --- /dev/null +++ b/scripts/lib/skill-lint-test.js @@ -0,0 +1,104 @@ +#!/usr/bin/env node + +'use strict'; + +/** + * Unit tests for the fence-stripping helper behind the required-section rule. + * + * The rule is observed indirectly: a heading inside a fenced block must NOT + * satisfy `## Overview`, so "Missing required section: ## Overview" is the + * signal that the block was stripped — and its absence is the signal that + * prose outside the block survived. + */ + +const assert = require('node:assert/strict'); +const test = require('node:test'); +const { lintSkillContent } = require('./skill-lint'); + +const KNOWN = new Set(['portable-skill']); + +/** + * A structurally valid SKILL.md whose `## Overview` heading is supplied by the + * caller — either as real prose or buried inside a fenced block. + */ +function skillWithOverview(overviewBlock) { + return `--- +name: portable-skill +description: Guides agents through portable work. Use when testing skill portability. +--- + +# Portable Skill + +${overviewBlock} + +## When to Use +Use for fence-parsing tests. + +## Common Rationalizations +None. + +## Red Flags +None. + +## Verification +Verify the result. +`; +} + +const overviewMissing = result => result.errors.includes('Missing required section: ## Overview'); + +const OVERVIEW = '## Overview\nPortable workflow.'; + +test('a real Overview heading satisfies the required-section check', () => { + const result = lintSkillContent('portable-skill', skillWithOverview(OVERVIEW), KNOWN); + assert.deepEqual(result.errors, []); +}); + +// A heading buried in a fenced block must not satisfy the check. +for (const [description, block] of [ + ['a plain backtick fence', '```markdown\n## Overview\n```'], + ['an unlabeled fence', '```\n## Overview\n```'], + ['a tilde fence', '~~~markdown\n## Overview\n~~~'], + ['an indented fence', ' ```markdown\n## Overview\n ```'], + ['a fence with a longer closer', '```markdown\n## Overview\n````'], +]) { + test(`a heading inside ${description} does not satisfy the check`, () => { + const result = lintSkillContent('portable-skill', skillWithOverview(block), KNOWN); + assert.equal(overviewMissing(result), true); + }); +} + +// Closing-fence handling: prose after the block must survive, so a closing +// fence that goes unrecognized shows up as a missing section. +for (const [description, block] of [ + ['a same-length closing fence', `\`\`\`markdown\nfenced example\n\`\`\`\n\n${OVERVIEW}`], + ['a longer closing fence', `\`\`\`markdown\nfenced example\n\`\`\`\`\`\n\n${OVERVIEW}`], + ['an indented closing fence', `\`\`\`markdown\nfenced example\n \`\`\`\n\n${OVERVIEW}`], +]) { + test(`prose after ${description} is still linted`, () => { + const result = lintSkillContent('portable-skill', skillWithOverview(block), KNOWN); + assert.equal(overviewMissing(result), false); + }); +} + +test('a shorter run of backticks does not close a longer fence', () => { + // The inner ``` and the heading it wraps belong to the ````-fenced block. + const block = '````markdown\n```\n## Overview\n````'; + const result = lintSkillContent('portable-skill', skillWithOverview(block), KNOWN); + assert.equal(overviewMissing(result), true); +}); + +test('a backtick fence does not close a tilde fence', () => { + const block = '~~~markdown\n```\n## Overview\n~~~'; + const result = lintSkillContent('portable-skill', skillWithOverview(block), KNOWN); + assert.equal(overviewMissing(result), true); +}); + +test('an unterminated fence fails loud rather than passing silently', () => { + // Everything after an unclosed fence is treated as fenced, so the sections + // below it are reported missing instead of being silently accepted. + const block = '```markdown\n## Overview'; + const result = lintSkillContent('portable-skill', skillWithOverview(block), KNOWN); + assert.equal(overviewMissing(result), true); + assert.equal(result.errors.includes('Missing required section: ## Verification'), true); +}); diff --git a/scripts/lib/skill-lint.js b/scripts/lib/skill-lint.js index 2513bf7c05..c95462dbc1 100644 --- a/scripts/lib/skill-lint.js +++ b/scripts/lib/skill-lint.js @@ -80,9 +80,40 @@ const SKILL_REF_PATTERNS = [ /** * Strip fenced code blocks from markdown content so that headings, references, * and trigger phrases inside examples or templates are not matched by lint rules. + * + * Scans line by line rather than matching a single regex, so the three fence + * forms CommonMark allows — tilde fences, fences indented up to three spaces, + * and closing fences longer than their opener — are stripped like plain + * ```-fences. A regex over the whole document misses all three, which lets a + * heading inside such a block satisfy the required-section check. */ function stripFencedCodeBlocks(content) { - return content.replace(/^(`{3,})[^\n]*\n[\s\S]*?^\1\s*$/gm, ''); + const kept = []; + let fence = null; + + for (const line of content.split(/\r?\n/)) { + if (fence) { + const closingFence = line.match(/^ {0,3}(`+|~+)[ \t]*$/); + if ( + closingFence + && closingFence[1][0] === fence.marker + && closingFence[1].length >= fence.length + ) { + fence = null; + } + continue; + } + + const openingFence = line.match(/^ {0,3}(`{3,}|~{3,})/); + if (openingFence) { + fence = { marker: openingFence[1][0], length: openingFence[1].length }; + continue; + } + + kept.push(line); + } + + return kept.join('\n'); } /**