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 `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('