/** * D-CMD-REF-HAND (#456, AC-405, AC-406): a command moves a rare branch out of its own text into a * hand-authored file under `src/assets/skills/agent-references/references/commands/` and keeps only the * condition and the read path. A run that never takes the branch then never carries its text. * * What each arm holds: * - Byte-identity. Every moved section equals the text `main` held before the move, pinned by length and * SHA-256 (a digest, because a guard that shells out to `git show main:` reads nothing on a shallow * clone). A deliberate later edit of a section updates its digest in the same commit. * - Read semantics. The sections are found by a fence-aware reader (the exception grammar's template * fence holds a `## ` line), so a moved block is read back through the extractor the guards use, not * only compared as bytes. * - Scope of the move. Only text with no curation arm and no Git-spawn fence moves: a curation arm cannot * be split across files, and a Git fence is read by guards that scan the command. * - Read paths. Every path a command names exists in the skill sources, every reference is named by a * command, the read resolves the config directory in Bash first, and the compiled command carries it. * A host may read a reference on its default path (`/code-review` at `standard` or `lean`) as well as on * a rare branch; either way the read site passes through one named collector. `/implement` resolves the * directory once, in one paragraph that binds `{ref_dir}` to the references folder, and each of its read * sites names its file under `{ref_dir}`; the collector then checks that paragraph in place of the site. * - Installation. The skill belongs to `devflow-core-skills` alone, which every selection installs, so a * selection of any plugin that uses a reference installs it; the install goldens list each file. * - D-10: no plugin lists `agent-references` in `requires:`. A command reads the file by path, which the * closure guard does not see, and a `requires:` entry would install the skill for a plugin that does * not own it. * * Every guard has a named collector, a non-empty-corpus assertion and a known-bad probe run through the * same collector. */ import { describe, it, expect } from 'vitest' import { createHash } from 'crypto' import { existsSync, readFileSync, readdirSync } from 'fs' import * as path from 'path' import { AGENT_REFERENCES_SKILL, RETIRED_INLINE_EXPANSION, ROOT, SANCTIONED_RESOLUTION, SANCTIONED_RESOLUTION_PRINT, agentReferencePath, commandReferenceSections, loadGolden, readAgentReference, requireDistFile, } from '../helpers.js' import { COMPLIANCE_OFF_REMOVED_SKILL_FILES } from '../../src/core/compliance-off-files.js' import { DEVFLOW_PLUGINS, skillsOf } from '../../src/core/plugins.js' import { INSTALL_CONFIGS } from '../install-snapshot-helpers.js' import { resolvePluginsToInstall } from '../../src/cli/commands/init-seed.js' const REFERENCE_DIR = 'commands' const COMMANDS_DIR = path.join(ROOT, 'src', 'assets', 'commands') /** The root a read site resolves first: the sanctioned resolution line. */ const READ_ROOTS = [SANCTIONED_RESOLUTION] as const /** The placeholder a host may read its references through: one paragraph of the host binds it to the references folder under the resolved directory. */ const REFS_PLACEHOLDER = '{ref_dir}' /** A read path: the full path under the references folder, or a file under the placeholder. */ const READ_PATH_RE = /(?:skills\/devflow:agent-references\/references\/commands|\{ref_dir\})\/([a-z0-9-]+\.md)/g /** One section a command lost and a reference now holds, exactly. */ interface Moved { readonly reference: string readonly heading: string /** UTF-8 length of the removed text without its trailing newline. */ readonly bytes: number /** SHA-256 of those bytes in the file that `was` names. The continuation path's hash covers its text, which holds a guarded `BASE_BRANCH` record that Phases 3-7 read for the branch diff, a step that takes `GATE_LEVEL` from the Phase 1 settings step for Phase 6, the base resolution (pr mode for a PR, local mode without one) so Phases 3-7 run the branch diff from a fresh base, a strict branch-name validation, and a checkout root that the path resolves itself when Phase 1 does not run. The path reads the base name into a fresh file and passes it to the resolver on stdin. The same settings step sets the compliance lens and directive from that line, so the Fix agents of Phases 5-7 get the directive of a full run, and takes the review-publication value with the evidence policy and the stub rule, so Phase 11 passes the value of a full run. The skip step keeps Harden at every level, and the first step says that "Phase 1 ran" means all of Phase 1. */ readonly sha256: string readonly was: string } const MOVED: readonly Moved[] = [ { reference: 'commands/implement-evidence-asks.md', heading: 'Ticket link ask', bytes: 1028, sha256: '438a2876f9ebcfc76694d8f0e4ce9b2fd2e5698eda65e4ec2f03b0c50348ae0c', was: 'implement.mds, the ticket-link ask and its two options' }, { reference: 'commands/implement-evidence-asks.md', heading: 'Exception grammar', bytes: 1450, sha256: 'ef7b06a10105c86e3d2a572a6d2e2fbb95385bc6e8bf7b971a834e4386e89051', was: '_evidence_policy.mds, the body of evidence_exception()' }, { reference: 'commands/implement-evidence-asks.md', heading: 'Record the exception', bytes: 566, sha256: '458cf3e415744aab954b2686f16d4c9fb5f1f3b9bfed1790e32a6a429d821ba8', was: 'implement.mds, the record paragraph' }, { reference: 'commands/implement-evidence-asks.md', heading: 'Missing test plan ask', bytes: 1358, sha256: '032972c8f3a0443a7aed0863ce248e68393f26b666a940da80c1b0284558d59d', was: 'implement.mds, the missing-test-plan ask and its two options' }, { reference: 'commands/implement-revalidate.md', heading: 'Continuation path', bytes: 4302, sha256: '3049ede5e5810c50e6cb7bc4201f0d358c41503be71bfc0b02a2c83139bd8a58', was: 'implement.mds, the re-validation continuation path' }, ] const digest = (text: string): string => createHash('sha256').update(Buffer.from(text, 'utf-8')).digest('hex') /** Named collector: the moved sections whose text is absent or no longer what `main` held. */ export function collectMoveDefects( moved: readonly Moved[], read: (reference: string) => string, ): string[] { const out: string[] = [] for (const m of moved) { const section = commandReferenceSections(read(m.reference)).find(s => s.heading === m.heading) if (section === undefined) { out.push(`${m.reference}: no \`## ${m.heading}\` section (was ${m.was})`); continue } const bytes = Buffer.byteLength(section.body, 'utf-8') if (bytes !== m.bytes) out.push(`${m.reference} ## ${m.heading}: ${bytes} bytes, main held ${m.bytes}`) else if (digest(section.body) !== m.sha256) out.push(`${m.reference} ## ${m.heading}: same length, different bytes`) } return out } describe('AC-405: the moved text is byte-identical to the text main held', () => { it('every moved section equals its pinned digest', () => { expect(MOVED.length, 'the move table is empty, so this guard proves nothing').toBeGreaterThanOrEqual(5) expect(collectMoveDefects(MOVED, readAgentReference)).toEqual([]) }) it('known-bad probes: one changed character, a lost section and a re-wrapped line are each reported', () => { const asks = readAgentReference('commands/implement-evidence-asks.md') const changed = asks.replace('closed set; no other kind', 'closed set; on other kind') expect(changed, 'the seed must land').not.toBe(asks) expect(collectMoveDefects(MOVED, ref => (ref.endsWith('asks.md') ? changed : readAgentReference(ref)))).toEqual([ 'commands/implement-evidence-asks.md ## Exception grammar: same length, different bytes', ]) expect(collectMoveDefects(MOVED, ref => (ref.endsWith('asks.md') ? asks.replace('## Record the exception', '## Record') : readAgentReference(ref)))).toEqual([ 'commands/implement-evidence-asks.md: no `## Record the exception` section (was implement.mds, the record paragraph)', ]) const wrapped = asks.replace('Render it with the grammar below, as kind `ticket-link`;', 'Render it with the grammar below,\nas kind `ticket-link`;') expect(wrapped, 'the seed must land').not.toBe(asks) expect(collectMoveDefects(MOVED, ref => (ref.endsWith('asks.md') ? wrapped : readAgentReference(ref)))).toEqual([ 'commands/implement-evidence-asks.md ## Ticket link ask: same length, different bytes', ]) }) it('the fence-aware reader keeps the exception grammar whole across its `## ` template line', () => { const grammar = commandReferenceSections(readAgentReference('commands/implement-evidence-asks.md')).find(s => s.heading === 'Exception grammar') expect(grammar?.body).toContain('## Evidence Exceptions\n- `` self-attested by') expect(grammar?.body).toContain('the requester authors the reason.') expect(commandReferenceSections('## A\nx\n```\n## B\n```\ny\n## C\nz')).toEqual([ { heading: 'A', body: 'x\n```\n## B\n```\ny' }, { heading: 'C', body: 'z' }, ]) }) }) /** The `references/commands/` files, sorted. */ function referenceFiles(): string[] { return readdirSync(agentReferencePath(REFERENCE_DIR)).filter(f => f.endsWith('.md')).sort() } /** Every `.mds` under `src/assets/commands/` (hosts and partials), as {name, content}. */ function commandSources(): Array<{ name: string; content: string }> { const out: Array<{ name: string; content: string }> = [] for (const dir of [COMMANDS_DIR, path.join(COMMANDS_DIR, '_partials')]) { for (const f of readdirSync(dir).filter(n => n.endsWith('.mds')).sort()) { out.push({ name: f.replace(/\.mds$/, ''), content: readFileSync(path.join(dir, f), 'utf-8') }) } } return out } /** Named collector: the reference files a text names by read path. */ export function namedReferences(text: string): string[] { return [...new Set([...text.matchAll(READ_PATH_RE)].map(m => m[1]))].sort() } /** * Named collector: what is wrong with one paragraph that names a reference by read path. A paragraph that reads its file under * the placeholder takes its root and its Read-tool phrase from the one paragraph of the host that binds the placeholder, and that * paragraph comes first. The root, the order and the phrase are the same three checks, run on the binder. */ export function collectReadSiteDefects(name: string, paragraph: string, host: string = paragraph): string[] { const out: string[] = [] let site = paragraph if (paragraph.includes(`${REFS_PLACEHOLDER}/`)) { const binder = host.split('\n\n').find(p => p.includes(`\`${REFS_PLACEHOLDER}\` is \``)) if (binder === undefined) return [`${name}.mds: ${REFS_PLACEHOLDER} is read but no paragraph binds it`] if (host.indexOf(binder) > host.indexOf(paragraph)) out.push(`${name}.mds: ${REFS_PLACEHOLDER} is read before the paragraph that binds it`) if (!binder.includes(`\`${REFS_PLACEHOLDER}\` is \`/skills/devflow:agent-references/references/commands\``)) out.push(`${name}.mds: ${REFS_PLACEHOLDER} is not bound to the references folder under `) site = binder } const at = site.search(/skills\/devflow:agent-references\//) const roots = READ_ROOTS.map(r => site.indexOf(r)).filter(i => i >= 0) if (roots.length === 0) out.push(`${name}.mds: the read site must resolve a config-directory root before the path`) else if (at !== -1 && Math.min(...roots) > at) out.push(`${name}.mds: the root comes after the path`) if (!site.includes('with the Read tool')) out.push(`${name}.mds: the read goes through the Read tool`) return out } describe('AC-406: every read path exists, is read, and resolves the config directory first', () => { it('each path a command names is a file of the skill, and each file is named by a command', () => { const files = referenceFiles() expect(files, 'the reference directory is empty, so this guard proves nothing').toEqual(['code-review-levels.md', 'implement-evidence-asks.md', 'implement-levels.md', 'implement-revalidate.md', 'plan-levels.md', 'resolve-levels.md']) const named = new Map(commandSources().map(s => [s.name, namedReferences(s.content)] as const).filter(([, refs]) => refs.length > 0)) expect([...named.keys()], 'only a host reads a reference, never a shared partial').toEqual(['code-review', 'implement', 'plan', 'resolve']) const all = [...new Set([...named.values()].flat())].sort() expect(all.filter(f => !existsSync(agentReferencePath(`${REFERENCE_DIR}/${f}`))), 'a named path with no file').toEqual([]) expect(files.filter(f => !all.includes(f)), 'a reference no command reads').toEqual([]) }) it('each read site resolves the config directory in Bash first, and the compiled command carries the path', () => { for (const { name, content } of commandSources()) { const paragraphs = content.split('\n\n').filter(p => namedReferences(p).length > 0) for (const p of paragraphs) expect(collectReadSiteDefects(name, p, content)).toEqual([]) if (paragraphs.length > 0) { const built = requireDistFile(`${name}.md`) expect(namedReferences(built), `${name}: the compiled command names the same files`).toEqual(namedReferences(content)) } } expect(namedReferences(requireDistFile('implement.md'))).toEqual(['implement-evidence-asks.md', 'implement-levels.md', 'implement-revalidate.md']) expect(namedReferences(requireDistFile('code-review.md'))).toEqual(['code-review-levels.md']) expect(namedReferences(requireDistFile('plan.md'))).toEqual(['plan-levels.md']) expect(namedReferences(requireDistFile('resolve.md'))).toEqual(['resolve-levels.md']) }) it('known-bad probes: a path with no file and a read without the config-directory step are told apart', () => { expect(namedReferences('Read `/skills/devflow:agent-references/references/commands/nope.md`')).toEqual(['nope.md']) expect(existsSync(agentReferencePath(`${REFERENCE_DIR}/nope.md`))).toBe(false) expect(namedReferences('no path here')).toEqual([]) }) it('known-bad probes: no root, a root after the path and a missing Read-tool phrase are each reported, and the sanctioned resolution line is accepted as the read root', () => { const target = 'skills/devflow:agent-references/references/commands/x.md' const sanctioned = READ_ROOTS[0] expect(collectReadSiteDefects('probe', `Read \`/${target}\` with the Read tool.`)) .toEqual(['probe.mds: the read site must resolve a config-directory root before the path']) expect(collectReadSiteDefects('probe', `Read \`/${target}\` with the Read tool, after resolving ${sanctioned}.`)) .toEqual(['probe.mds: the root comes after the path']) expect(collectReadSiteDefects('probe', `Resolve ${sanctioned} in Bash and Read \`/${target}\`.`)) .toEqual(['probe.mds: the read goes through the Read tool']) expect(collectReadSiteDefects('probe', `Run \`d="$HOME/.claude";\` in Bash and Read \`/${target}\` with the Read tool.`)) .toEqual(['probe.mds: the read site must resolve a config-directory root before the path']) expect(collectReadSiteDefects('probe', `Resolve ${RETIRED_INLINE_EXPANSION} in Bash and Read \`/${target}\` with the Read tool.`)) .toEqual(['probe.mds: the read site must resolve a config-directory root before the path']) expect(collectReadSiteDefects('probe', `Run \`${SANCTIONED_RESOLUTION_PRINT}\` in Bash and Read \`/${target}\` with the Read tool.`)).toEqual([]) }) it('known-bad probes: a read under the placeholder needs a paragraph that binds it first, with the sanctioned root, the references folder and the Read-tool phrase', () => { const folder = '/skills/devflow:agent-references/references/commands' const binder = `Run \`${SANCTIONED_RESOLUTION_PRINT}\` in Bash. \`${REFS_PLACEHOLDER}\` is \`${folder}\`. Read each reference with the Read tool.` const read = `Read \`${REFS_PLACEHOLDER}/x.md\` and follow it.` const defects = (host: string): string[] => collectReadSiteDefects('probe', read, host) expect(namedReferences(read)).toEqual(['x.md']) expect(defects(`${binder}\n\n${read}`)).toEqual([]) expect(defects(read)).toEqual([`probe.mds: ${REFS_PLACEHOLDER} is read but no paragraph binds it`]) expect(defects(`${read}\n\n${binder}`)).toEqual([`probe.mds: ${REFS_PLACEHOLDER} is read before the paragraph that binds it`]) expect(defects(`${binder.replace(SANCTIONED_RESOLUTION_PRINT, 'd="$HOME/.claude"')}\n\n${read}`)) .toEqual(['probe.mds: the read site must resolve a config-directory root before the path']) expect(defects(`${binder.replace(' Read each reference with the Read tool.', '')}\n\n${read}`)) .toEqual(['probe.mds: the read goes through the Read tool']) expect(defects(`${binder.replace(folder, '/skills')}\n\n${read}`)) .toEqual([`probe.mds: ${REFS_PLACEHOLDER} is not bound to the references folder under `]) }) it('a selection of any plugin that uses a reference installs the skill, and the install goldens list each file', () => { const hosts = commandSources().filter(s => namedReferences(s.content).length > 0).map(s => s.name) expect(hosts.length).toBeGreaterThanOrEqual(1) for (const host of hosts) { const users = DEVFLOW_PLUGINS.filter(p => p.commands.includes(`/${host}`)) expect(users.map(p => p.name), `${host}: no plugin owns the command`).not.toEqual([]) for (const plugin of users) { const installed = resolvePluginsToInstall([plugin.name], false, DEVFLOW_PLUGINS) expect(installed.map(p => p.name), `${plugin.name}: devflow-core-skills joins every selection`).toContain('devflow-core-skills') expect(skillsOf(installed).has(AGENT_REFERENCES_SKILL), `${plugin.name}: the skill is in its install closure`).toBe(true) } } const installedPrefix = `devflow:${AGENT_REFERENCES_SKILL}/references/${REFERENCE_DIR}/` const removedWithCompliance = new Set( COMPLIANCE_OFF_REMOVED_SKILL_FILES.filter(entry => entry.startsWith(installedPrefix)).map(entry => entry.slice(installedPrefix.length)), ) for (const config of ['github', 'jira-hipaa']) { const complianceOn = INSTALL_CONFIGS.find(c => c.name === config)?.initArgs.includes('--compliance') ?? false const golden = loadGolden(`install-snapshot-${config}.txt`) for (const f of referenceFiles()) { const line = `- .claude/skills/devflow:agent-references/references/commands/${f}` if (!complianceOn && removedWithCompliance.has(f)) { expect(golden, `install-snapshot-${config}.txt (compliance off) omits ${f}`).not.toContain(line) } else { expect(golden, `install-snapshot-${config}.txt lists ${f}`).toContain(line) } } } }) }) describe('the moved text carries no curation arm and no Git spawn (D-10)', () => { it('no reference holds a curation marker or a Git agent fence', () => { const files = referenceFiles() expect(files.length).toBeGreaterThanOrEqual(2) for (const f of files) { const text = readAgentReference(`${REFERENCE_DIR}/${f}`) expect(text, `${f}: a curation arm cannot be split across files`).not.toMatch(/