diff --git a/commands/spec.md b/commands/spec.md index edd072d0..b278209f 100644 --- a/commands/spec.md +++ b/commands/spec.md @@ -197,17 +197,21 @@ Every file is on disk, so every question below refines something that already ex The spec content is now final for this run, so render the human-facing review page. This step is unconditional — every run produces the page — but its failure is never fatal: if `node` is missing (`command -v node`) or the script errors, report that the render was skipped and why, and continue to the report; the markdown remains canonical. -1. Author the **view-model** — the re-shaped, human-friendly representation of what this session learned. Write it as JSON to a temporary file outside the project, following the schema documented at the top of the renderer script. Resolve the temp paths once, with a fallback for an unset `TMPDIR`, and reuse those exact paths for every write, render, and cleanup below: +1. Author the **view-model** — the re-shaped, human-friendly representation of what this session learned. Write it as JSON to a temporary file outside the project, following the schema documented at the top of the renderer script. Create one private temp directory for this run, with a fallback for an unset `TMPDIR`, and keep both temp files inside it: ```bash - view_model_path="${TMPDIR:-/tmp}/spec-view-[index].json" - artifact_path="${TMPDIR:-/tmp}/spec-artifact-[index].html" + spec_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/spec-view.XXXXXX")" + view_model_path="$spec_tmp_dir/view-model.json" + artifact_path="$spec_tmp_dir/artifact.html" + echo "$spec_tmp_dir" ``` + Run `mktemp` once and reuse the directory it printed for every write, render, and cleanup below — a second call creates a different directory. The directory is readable only by the current user and its name is not predictable, so a parallel run on the same spec index or another local user cannot reach the view-model. + Its content, all in the spec's non-technical register: - `at_a_glance` — the feature in about five plain sentences. - `decisions` — every judgment call made in this spec (the things a reviewer might challenge): the decision, the choice, why, and its sources (`interview` / `code` / `web` / `kb`). Facts the user supplied in the prompt or in Steps 8 and 9 count as `interview`. - - `findings` — per research lane, what was discovered and what it changed in the spec (`impact`, with an `anchor` like `#r21` pointing at the requirement it shaped). A lane that did not run gets an explanatory note, not silence. + - `findings` — per research lane, what was discovered and what it changed in the spec (`impact`, with an `anchor` like `#r21` pointing at the requirement it shaped; a two-digit item keeps a dash, `#rN-M`, so requirement 1.12 is `#r1-12`). A lane that did not run gets an explanatory note, not silence. - `requirements` — per requirement (matched by its number, e.g. `"2.1"`): a one-line plain-language statement, source badges, and a short scannable name for each acceptance criterion, in order. - `diagram` (optional) — when the behavior is stateful or flow-heavy, a small declarative diagram: box/arrow/text items with user-language labels, following the schema in the renderer header. The renderer draws the SVG itself — it never accepts raw markup. Include a note that the criteria remain the contract. @@ -219,7 +223,7 @@ The spec content is now final for this run, so render the human-facing review pa node ".awos/scripts/render-spec.mjs" "context/spec/[index]-[short-name]" "$view_model_path" ``` - The script extracts all requirement prose and acceptance criteria verbatim from `functional-spec.md` — the view-model only adds the human layer on top, never replacement text. Keep the temp view-model file until the publish offer below is resolved, then delete it — it never lands in the project. + The script extracts all requirement prose and acceptance criteria verbatim from `functional-spec.md` — the view-model only adds the human layer on top, never replacement text. Keep the temp directory until the publish offer below is resolved, then delete it — the view-model never lands in the project. 3. Offer to publish the review page as a Claude artifact — a link is easier to share with reviewers than a file. Make the offer only when both hold: the render succeeded, and the `Artifact` tool is available in your toolset (introspection — when it is absent, skip this step silently rather than mentioning an option that cannot work). Ask via `AskUserQuestion`: publish as a private artifact, or skip. Publishing sends the spec's content to an external service, so this is a consent gate, not a preference: an unanswered or dismissed question means **no** — never publish by default, and never publish in an unattended run. This deliberately inverts this command's usual unanswered-question rule; outward-facing actions default to withheld consent. If the user opts in: @@ -230,10 +234,10 @@ The spec content is now final for this run, so render the human-facing review pa Publish only a fragment this run produced: if that command fails or writes no file, report that the artifact render failed and publish nothing. Otherwise publish the fragment file with the `Artifact` tool (favicon `📋`) and report the artifact URL — it starts private, and the user shares it from claude.ai when ready. -4. Delete the temp files, whatever the publish decision was: +4. Delete the temp directory, whatever the publish decision was: ```bash - rm -f "$view_model_path" "$artifact_path" + rm -rf "$spec_tmp_dir" ``` This runs on every path — offer declined, `Artifact` tool absent, unattended run, render failed, or published. The view-model holds the spec's content; it does not belong in shared temporary storage once the run is done. diff --git a/scripts/render-spec.mjs b/scripts/render-spec.mjs index a5094e14..df073b40 100644 --- a/scripts/render-spec.mjs +++ b/scripts/render-spec.mjs @@ -46,6 +46,10 @@ * }, * "requirements": [{ "match": "2.1", "one_liner", "sources": [...], "criteria_names": ["...", ...] }] * } + * + * A finding's anchor is the requirement card id: "r" + section + item for a + * one-digit item ("2.1" -> "#r21"), with a dash before a two-digit item + * ("1.12" -> "#r1-12") so that 1.12 and 11.2 never share an id. */ import fs from 'node:fs'; @@ -85,7 +89,10 @@ function badge(src) { function anchorFor(heading) { const num = heading.match(/^(\d+)\.(\d+)/); - if (num) return `r${num[1]}${num[2]}`; + // "2.1" -> "r21". A two-digit item keeps a separator ("1.12" -> "r1-12"): + // plain concatenation would give 1.12 and 11.2 the same id. + if (num) + return num[2].length > 1 ? `r${num[1]}-${num[2]}` : `r${num[1]}${num[2]}`; return ( 'r-' + heading @@ -233,6 +240,25 @@ function parseBody(body) { continue; } + // An indented bullet that is not a checkbox is requirement prose too + // (a detail under a top-level bullet); only the acceptance label is not. + const nested = line.match(/^\s+- (.+)$/); + if (nested) { + let text = nested[1]; + i++; + while (i < body.length) { + const next = body[i]; + if (!next.trim()) break; + if (/^\s*- /.test(next) || /^#{1,6} /.test(next)) break; + text += ' ' + next.trim(); + i++; + } + if (!/^\*\*Acceptance Criteria:?\*\*/i.test(text.trim())) { + pushText(text); + } + continue; + } + // Tables are checked before the paragraph branch below: their rows are // neither bullets nor a bare rule, so that branch would otherwise fold the // whole table — pipes, dashes and all — into one run-on paragraph. @@ -275,6 +301,7 @@ function splitFlatRequirements(section) { if (!parseBody(section.body).criteria.length) return []; const groups = []; + const lead = []; for (const line of section.body) { const top = line.match(/^- (.+)$/); const isCheckbox = /^- \[( |x|X)\]/.test(line); @@ -284,12 +311,19 @@ function splitFlatRequirements(section) { groups.push([line]); } else if (groups.length) { groups[groups.length - 1].push(line); + } else { + lead.push(line); } } - return groups - .map((body, idx) => ({ title: `${sectionNumber}.${idx + 1}`, body })) - .filter((sub) => parseBody(sub.body).criteria.length > 0); + // A requirement without criteria still renders, as it does in the + // subsection shape. Text before the first requirement rides on the first + // unit as `lead` and renders above its card. + return groups.map((body, idx) => ({ + title: `${sectionNumber}.${idx + 1}`, + body, + ...(idx === 0 ? { lead } : {}), + })); } // --------------------------------------------------------------------------- @@ -356,6 +390,17 @@ function renderBlocks(blocks, textTag, textClass) { .join('\n'); } +// Text a requirements section carries before its first requirement. It rides +// on the first unit and renders above that card; a checkbox written there is +// shown too, because the status bar counts it. +function renderLead(lines) { + const parsed = parseBody(lines || []); + const parts = [renderBlocks(parsed.blocks, 'p', 'prose')]; + if (parsed.criteria.length) parts.push(renderCriteria(parsed, null)); + const html = parts.filter(Boolean).join('\n'); + return html ? `${html}\n` : ''; +} + function renderRequirement(sub, vm) { const parsed = parseBody(sub.body); const vmReq = findVmRequirement(vm, sub.title); @@ -368,7 +413,7 @@ function renderRequirement(sub, vm) { ? `

${inline(vmReq.one_liner)}

` : ''; const prose = renderBlocks(parsed.blocks, 'p', 'prose'); - return `
+ return `${renderLead(sub.lead)}

${inline(sub.title)}${badges}

${oneLiner} ${prose} @@ -412,7 +457,8 @@ function renderScope(section) { }) .join('\n'); if (!cols) return renderGenericSection(section); - return `

${inline(section.title)}

\n
${cols}
`; + const lead = renderBlocks(parseBody(section.body).blocks, 'p', ''); + return `

${inline(section.title)}

\n${lead}\n
${cols}
`; } function renderDecisions(vm) { @@ -639,7 +685,7 @@ const CSS = ` .d-head { fill: var(--muted); } .tabs { margin-top: 12px; } .tabbar { display: flex; gap: 4px; border-bottom: 2px solid var(--line); } - .tabbar button { appearance: none; border: none; background: none; color: var(--muted); font: 600 14px/1.4 inherit; padding: 8px 14px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; } + .tabbar button { appearance: none; border: none; background: none; color: var(--muted); font: inherit; font-weight: 600; font-size: 14px; line-height: 1.4; padding: 8px 14px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; } .tabbar button.active { color: var(--ink); border-bottom-color: var(--accent); } .tabbar button .count { font-weight: 400; color: var(--muted); font-size: 12.5px; } .tabpanel { display: none; padding: 6px 2px 0; } @@ -731,7 +777,9 @@ function render(md, rawVm) { // yields a unit, the section still renders generically below: showing it // unenriched beats dropping it. const subs = section.subs.length - ? section.subs + ? section.subs.map((sub, idx) => + idx === 0 ? { ...sub, lead: section.body } : sub + ) : splitFlatRequirements(section); if (subs.length) requirementSubs.push(...subs); else genericSections.push(section); diff --git a/tests/lint-prompts.test.js b/tests/lint-prompts.test.js index fe71c60c..c55743fa 100644 --- a/tests/lint-prompts.test.js +++ b/tests/lint-prompts.test.js @@ -2286,8 +2286,16 @@ test('commands/spec.md keeps the research-backed spec contracts (fan-out, unatte 'the scope boundary must be drafted and marked rather than asked — Step 7 runs before the spec file exists', ], [ - 'view_model_path="${TMPDIR:-/tmp}/spec-view-[index].json"', - 'the temp paths must be resolved once with a TMPDIR fallback and reused — re-interpolating a bare $TMPDIR per step lets the file written and the file rendered diverge, and makes the cleanup rm a path that was never created', + 'spec_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/spec-view.XXXXXX")"', + "the temp files must live in one mktemp -d directory per run, with a TMPDIR fallback — a predictable name in shared temp storage can be pre-created by another local user, and two runs on the same spec index overwrite and delete each other's file", + ], + [ + 'rm -rf "$spec_tmp_dir"', + 'the cleanup must remove the per-run temp directory itself — the view-model holds the spec content and must not outlive the run', + ], + [ + '`#rN-M`', + 'the anchor guidance must name the two-digit form — the renderer gives requirement 1.12 the id r1-12, so an anchor written as #r112 would point at 11.2 or at nothing', ], [ 'MANUAL SOURCES ONLY', diff --git a/tests/render-spec.test.js b/tests/render-spec.test.js index 085cc6fe..70a32b25 100644 --- a/tests/render-spec.test.js +++ b/tests/render-spec.test.js @@ -374,7 +374,7 @@ test("renderer matches requirements by exact number — 2.1 must not claim 2.10' ], }; const { html } = renderInTemp(vm, { md }); - const cardTwoTen = html.slice(html.indexOf('id="r210"')); + const cardTwoTen = html.slice(html.indexOf('id="r2-10"')); assert.ok( cardTwoTen.includes('ONE-LINER FOR TWO-POINT-TEN') && !cardTwoTen.startsWith('ONE-LINER FOR TWO-POINT-ONE'), @@ -551,3 +551,210 @@ test('renderer degrades to a generic render when no view-model is given — it n 'view-model-only sections must be omitted, not rendered empty, when no view-model is given' ); }); + +// Content the renderer used to drop or mis-anchor. Both requirement shapes are +// covered: the template's flat bullets and numbered "### N.M" subsections. +const FLAT_LOSSLESS_MD = `# Functional Specification: Fixture + +- **Status:** Draft + +## 1. Overview + +Some overview. + +## 2. Functional Requirements + +LEADIN-FLAT intro paragraph. + +- **Rule one:** first requirement. + - NESTED-DETAIL plain sub bullet. + - **Acceptance Criteria:** + - [ ] Criterion A +- BARE-REQUIREMENT with no criteria. +- **Rule three:** third. + - **Acceptance Criteria:** + - [x] Criterion C + +## 3. Scope and Boundaries + +SCOPE-LEADIN text before subsections. + +### In-Scope + +- thing in + +### Out-of-Scope + +- thing out +`; + +const SUBSECTIONS_LOSSLESS_MD = `# Functional Specification: Subs + +- **Status:** Draft + +## 1. Functional Requirements + +LEADIN-SUBS intro. + +### 1.1 Req + +- [ ] c1 + +### 1.2 Req + +- [ ] c2 + +### 1.12 Req + +- [ ] c12 + +## 11. More Requirements + +### 11.2 Other + +- [ ] cx +`; + +function cardIds(html) { + return [...html.matchAll(/
m[1] + ); +} + +test('renderer keeps an indented plain bullet under a requirement and still hides the Acceptance Criteria label', () => { + const { html } = renderInTemp(undefined, { md: FLAT_LOSSLESS_MD }); + assert.ok( + html.includes('

NESTED-DETAIL plain sub bullet.

'), + 'an indented non-checkbox bullet is requirement prose and must reach the page — dropping it loses contract text the header promises to extract verbatim' + ); + assert.ok( + !/Acceptance Criteria:/.test(html), + 'the nested **Acceptance Criteria:** label must not render as prose — the criteria block already carries its own heading' + ); + assert.ok( + html.includes('Criterion A') && html.includes('Criterion C'), + 'criteria nested under the label must still be extracted' + ); +}); + +test('renderer keeps a flat requirement that has no acceptance criteria, without renumbering its neighbours', () => { + const { html } = renderInTemp(undefined, { md: FLAT_LOSSLESS_MD }); + assert.ok( + html.includes('BARE-REQUIREMENT with no criteria.'), + 'a flat requirement bullet without criteria must still render, as a criteria-less ### subsection does' + ); + assert.deepStrictEqual( + cardIds(html), + ['r21', 'r22', 'r23'], + 'flat requirements are numbered by position, so the criteria-less one is card r22 and the third stays r23' + ); +}); + +test('renderer keeps text written before the first requirement, in the flat shape and in the ### N.M shape', () => { + const flat = renderInTemp(undefined, { md: FLAT_LOSSLESS_MD }).html; + assert.ok( + flat.includes('

LEADIN-FLAT intro paragraph.

'), + 'flat shape: a paragraph between the section heading and the first requirement bullet must reach the page' + ); + assert.ok( + flat.indexOf('LEADIN-FLAT') < flat.indexOf('id="r21"'), + 'flat shape: the lead-in text must render above the first requirement card' + ); + const subs = renderInTemp(undefined, { md: SUBSECTIONS_LOSSLESS_MD }).html; + assert.ok( + subs.includes('

LEADIN-SUBS intro.

'), + '### N.M shape: text between the section heading and the first subsection must reach the page' + ); + assert.ok( + subs.indexOf('LEADIN-SUBS') < subs.indexOf('id="r11"'), + '### N.M shape: the lead-in text must render above the first requirement card' + ); + assert.strictEqual( + subs.split('LEADIN-SUBS').length - 1, + 1, + 'the lead-in text must render once, not once per requirement card' + ); +}); + +test('renderer shows a checkbox written before the first requirement — the status bar counts it', () => { + const md = SUBSECTIONS_LOSSLESS_MD.replace( + 'LEADIN-SUBS intro.', + 'LEADIN-SUBS intro.\n\n- [x] LEAD-CRITERION holds for every requirement' + ); + const { html } = renderInTemp(undefined, { md }); + assert.ok( + html.includes('1 of 5 criteria verified'), + 'the status bar counts the section-level checkbox together with the four requirement criteria' + ); + assert.ok( + html.includes('LEAD-CRITERION holds for every requirement'), + 'a counted criterion must also be shown — the page must never count criteria it does not show' + ); +}); + +test('renderer keeps text written under the Scope heading before its subsections', () => { + const { html } = renderInTemp(undefined, { md: FLAT_LOSSLESS_MD }); + assert.ok( + html.includes('

SCOPE-LEADIN text before subsections.

'), + 'text directly under the Scope heading must reach the page' + ); + assert.ok( + html.indexOf('id="scope"') < html.indexOf('SCOPE-LEADIN') && + html.indexOf('SCOPE-LEADIN') < html.indexOf('
'), + 'the Scope lead-in must sit between the Scope heading and the in/out columns' + ); + assert.ok( + html.includes('
  • thing in
  • ') && html.includes('
  • thing out
  • '), + 'the in-scope and out-of-scope columns must still render' + ); +}); + +test('renderer gives every requirement card a unique id — 1.12 and 11.2 must not share one, and one-digit items keep the rNM form', () => { + const vm = { + findings: { + code: [ + { text: 'FINDING-FOR-1-12', impact: 'x', anchor: '#r1-12' }, + { text: 'FINDING-FOR-11-2', impact: 'x', anchor: '#r112' }, + ], + }, + }; + const { html } = renderInTemp(vm, { md: SUBSECTIONS_LOSSLESS_MD }); + const ids = cardIds(html); + assert.deepStrictEqual( + ids, + ['r11', 'r12', 'r1-12', 'r112'], + 'a two-digit item keeps a dash (1.12 -> r1-12) so it cannot collide with 11.2 (r112); one-digit items stay rNM' + ); + assert.strictEqual( + new Set(ids).size, + ids.length, + 'two cards sharing an HTML id makes a finding link land on the wrong requirement' + ); + assert.ok( + html.includes('href="#r1-12"') && html.includes('href="#r112"'), + 'findings anchored to #r1-12 and #r112 must both link, each to its own card' + ); + const sample = renderInTemp(SAMPLE_VM).html; + assert.deepStrictEqual( + cardIds(sample), + ['r21'], + 'the documented #r21 anchor for requirement 2.1 must stay unchanged' + ); +}); + +test('renderer styles the findings tab buttons with a valid font declaration', () => { + const { html } = renderInTemp(SAMPLE_VM); + const rule = (html.match(/\.tabbar button \{[^}]*\}/) || [''])[0]; + assert.ok(rule, 'the page stylesheet must carry a .tabbar button rule'); + assert.ok( + !/font:\s*[^;]*\S\s+inherit\s*;/.test(rule), + 'the font shorthand must not mix inherit with other values — browsers discard the whole declaration' + ); + assert.ok( + /font:\s*inherit;/.test(rule) && + /font-weight:\s*600;/.test(rule) && + /font-size:\s*14px;/.test(rule) && + /line-height:\s*1\.4;/.test(rule), + 'the tab buttons must inherit the page font and set weight 600, size 14px and line-height 1.4 as separate valid declarations' + ); +});