Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test-plugin-install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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: Validate manifest versions
run: node scripts/validate-versions.js

Expand Down
107 changes: 103 additions & 4 deletions scripts/lib/skill-lint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

'use strict';

/**
* Unit tests for `lintSkillContent`.
*
* Two groups, added by separate changes and merged here:
* - section exemptions, and guardrails on the rules beside them (#505)
* - the fence-stripping helper behind the required-section rule (#444)
*/

const assert = require('node:assert/strict');
const { test } = require('node:test');

const { lintSkillContent } = require('./skill-lint.js');

const KNOWN = new Set(['alpha', 'beta']);
const KNOWN = new Set(['alpha', 'beta', 'portable-skill']);

/** A SKILL.md body carrying every required section, so tests can isolate frontmatter. */
function withAllSections(frontmatter) {
Expand Down Expand Up @@ -82,9 +90,7 @@ test('a skill claiming its own exemption without being allowlisted fails loud',
assert.match(errors[0], /not in the validator's SECTION_EXEMPT_SKILLS allowlist/);
});

// ─── Guardrails on the rules this change sits beside ─────────────────────────
// Deliberately narrow: #428 rewrites frontmatter parsing and the cross-reference
// patterns, so asserting their behaviour here would collide with that work.
// ─── Guardrails on the rules those exemptions sit beside ─────────────────────

test('a fully valid skill produces no errors', () => {
const { errors } = lintSkillContent('alpha', withAllSections(VALID_FRONTMATTER), KNOWN);
Expand Down Expand Up @@ -114,3 +120,96 @@ test('reports a missing frontmatter block', () => {
assert.equal(errors.length, 1);
assert.match(errors[0], /Missing or malformed YAML frontmatter/);
});

// ─── Fenced-block stripping ──────────────────────────────────────────────────
//
// 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.

/**
* 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);
});
33 changes: 32 additions & 1 deletion scripts/lib/skill-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down