Skip to content

refactor: unify requirement reader and surface #498#1281

Open
clay-good wants to merge 9 commits into
Fission-AI:mainfrom
clay-good:fix-spec-parser-fidelity
Open

refactor: unify requirement reader and surface #498#1281
clay-good wants to merge 9 commits into
Fission-AI:mainfrom
clay-good:fix-spec-parser-fidelity

Conversation

@clay-good

@clay-good clay-good commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Status

LGTM — ready to review. Planning artifacts (proposal / design / spec delta / tasks) and the implementation + tests are now all in this PR. Passes openspec validate fix-spec-parser-fidelity --strict; a changeset (patch) is included.

What was wrong

The requirement reader was implemented twice and the two copies had drifted. MarkdownParser.parseRequirements (used by validate <spec>/archive) and Validator.extractRequirementText/countScenarios (used by validate <change>) disagreed in five ways — each reproduced against main with the bundled CLI:

spec reader delta reader
Body capture first line only first line only
Skips **metadata**: no yes
Ignores fenced code in body no no
Counts fenced #### Scenario: no yes
SHALL/MUST predicate substring includes word-boundary \b…\b

Concrete bugs this produced:

How it was fixed

Part A — unify the reader (fixes #361, #418, #312, fenced-scenario). Both readers now delegate to one shared, fence-/metadata-/multi-line-aware extraction in src/core/parsers/requirement-text.ts so they cannot diverge again:

  • Capture the full requirement body up to the first non-fenced #### Scenario:, skipping blank, **metadata**:, and fenced-code lines; run SHALL/MUST detection over the whole body.
  • Count only non-fenced #### headers, so a fenced #### Scenario: no longer counts (parity with validate <spec>).
  • One whole-word \b(SHALL|MUST)\b predicate (containsShallOrMust) shared by the validator and base.schema, replacing the substring/word-boundary split.
  • buildCodeFenceMask extracted into the shared module; MarkdownParser and ChangeParser import the single implementation.

Part B — surface #498 safely. validate <change> emits an INFO note when an ADDED/MODIFIED Requirements section contains a non-### Requirement: level-3 header (one the delta reader silently skips). INFO never changes the valid result, including under --strict (valid = no errors && no warnings), so nothing newly fails.

Recognition tightening was rejected (key finding of the review)

The obvious #498 fix — recognize only ### Requirement: headers — is rejected. Bare ### <statement> headers are a supported, tested requirement format: test/core/validation.test.ts asserts a bare-header spec is valid, and bare headers appear across json-converter/archive/spec tests and the tmp-init fixtures. Tightening would break a large test surface and silently drop requirements from real specs. Part B gives a consistent signal without that breakage.

Proof it works

Safety

  • Write path unaffected: specs-apply rebuilds from raw ### Requirement: blocks (extractRequirementsSection + RequirementBlock.raw), never parseSpec/req.text — so Part A cannot change archived spec content (display/validation only). Displayed text in JSON output and delta descriptions now reflects the full body.
  • Requirement counts in view/list are unchanged.

Notes / scope

Review surface

  • Implementation: src/core/parsers/requirement-text.ts (new shared reader), markdown-parser.ts, change-parser.ts, requirement-blocks.ts, src/core/validation/validator.ts, src/core/schemas/base.schema.ts.
  • Tests: test/core/validation.test.ts, test/core/parsers/markdown-parser.test.ts.
  • Planning: proposal.md, design.md, tasks.md, specs/cli-validate/spec.md; changeset .changeset/spec-parser-reading-fidelity.md.

🤖 Generated with Claude Code

…I#361, Fission-AI#498, Fission-AI#312)

The requirement-parsing layer silently misreads valid Markdown:

- Fission-AI#361: requirement-body extraction returns only the first non-blank line,
  so a SHALL/MUST that wraps onto line 2 fails `validate --strict`.
- Fission-AI#498: `validate` (delta-block parser) and `archive` (full-spec parser)
  recognize requirements by different rules, so a stray `###` header passes
  validate but becomes a phantom requirement that blocks archive.
- Fission-AI#312 (residual): the requirement-body loop breaks on any `#` line without
  consulting the code-fence mask, truncating bodies that contain fenced
  code with `#` comments.

Proposal: one shared, multi-line, fence-aware requirement-body extractor used
by both the validator and the markdown parser; recognize only
`### Requirement:`-prefixed level-3 headers; guarantee validate/archive parity.
Adds regression + parity tests. Fission-AI#559 investigated and deferred (ambiguous root
cause — see design.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@clay-good clay-good requested a review from TabishB as a code owner June 29, 2026 23:25
@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a new fix-spec-parser-fidelity change entry, expands the cli-validate spec, and updates parser/validator code and tests so requirement-body extraction, scenario counting, and SHALL/MUST detection use shared fence-aware logic. It also adds INFO-only handling for non-canonical delta headers.

Changes

Parser Fidelity Fix Planning

Layer / File(s) Summary
Proposal and design documents
openspec/changes/fix-spec-parser-fidelity/.openspec.yaml, openspec/changes/fix-spec-parser-fidelity/proposal.md, openspec/changes/fix-spec-parser-fidelity/design.md
Metadata is added, and the proposal/design describe the extracted-body divergence, shared fence-aware extraction plan, and INFO-only handling for non-canonical delta headers.
Updated spec and implementation checklist
openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md, openspec/changes/fix-spec-parser-fidelity/tasks.md
The spec and tasks define full-body keyword detection, fence-aware body/scenario boundaries, INFO-only delta-header surfacing, and the matching regression checklist.
Shared parsing and validation helpers
src/core/parsers/change-parser.ts, src/core/parsers/markdown-parser.ts, src/core/parsers/requirement-blocks.ts, src/core/parsers/requirement-text.ts, src/core/schemas/base.schema.ts, src/core/validation/validator.ts
Shared helpers are introduced and wired into parsing, schema refinement, and validation; requirement bodies, scenarios, and normative keywords now use the shared fence-aware logic, and delta validation emits INFO for non-canonical level-3 headers.
Parser and validator tests
test/core/parsers/markdown-parser.test.ts, test/core/validation.test.ts
Tests are updated and expanded for wrapped keywords, metadata skipping, fenced content, CRLF handling, and the INFO-only stray-header case.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

  • #361: Directly matches the wrapped SHALL/MUST validation failure that this PR addresses.
  • Fission-AI/OpenSpec#312: Shares fence-aware markdown parsing changes that ignore fenced content during header/scenario handling.
  • Fission-AI/OpenSpec#1156: Overlaps with the main-spec SHALL/MUST detection path and requirement-body validation logic.

Possibly related PRs

  • Fission-AI/OpenSpec#966: Both PRs adjust markdown parsing so fenced content does not affect requirement or section detection.
  • Fission-AI/OpenSpec#1031: Both touch REQUIREMENT_HEADER_REGEX and requirement-header matching behavior in requirement-blocks.ts.
  • Fission-AI/OpenSpec#1135: Both update validator.ts around delta requirement handling and SHALL/MUST-related diagnostics.

Suggested reviewers

  • alfred-openspec
  • TabishB

Poem

🐇 I nibbled the fence and counted the stars,
Then found every SHALL hiding in jars.
Metadata skipped,
Scenario lines zipped,
Now validator hops true by the bars. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 41.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The shared extractor, keyword alignment, fence-aware scenario handling, and INFO-only #498 note satisfy #361, #418, #312, and #498.
Out of Scope Changes check ✅ Passed No clearly unrelated code changes stand out; the added docs, helper exports, validation tweaks, and tests all support the stated fidelity work.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: unifying requirement reading and surfacing the #498 case.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…vidence

Hardened the proposal after reproducing every claim against main with the
bundled CLI and correcting two inaccuracies:

- Fission-AI#498 reframed: archive does NOT hard-fail. validate passes; archive emits
  NON-BLOCKING phantom "Proposal warnings in proposal.md" because
  validateChange/parseRequirements counts every level-3 header as a
  requirement, while the delta-block parser (validate) and specs-apply
  (rebuild) only recognize canonical `### Requirement:`. It is a consistency
  bug, not data loss. Verified the rebuilt spec is clean.
- Fission-AI#312 reframed: the original repro is already fixed by codeFenceLineMask
  (requirement count verified correct). The residual is a regression hazard:
  the body loop is fence-unaware, harmless only while first-line-only, so the
  multi-line fix must be fence-aware from the start.

Also: unify recognition on the canonical REQUIREMENT_HEADER_REGEX
(/^###\s*Requirement:\s*(.+)$/i, case-insensitive); surfaced a third latent
inconsistency (Zod substring includes('SHALL') vs delta word-boundary
\b(SHALL|MUST)\b) and added a single-predicate requirement; verified zero
non-Requirement level-3 headers in repo specs (CI-safe); added edge-case
scenarios (multi-line spec+delta paths, fenced scenario-looking lines,
REMOVED/RENAMED unaffected, display vs detection); replaced broken relative
links with plain paths. Proposal passes `openspec validate --strict`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
openspec/changes/fix-spec-parser-fidelity/design.md (1)

67-68: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Clarify the display-text mitigation strategy.

The risk mitigation states "keep display text concise (first line) while running detection over the full body, or document the widened text" — this ambiguity leaves implementers without a single source of truth. Commit to one approach: either preserve first-line display consistently (with full body for validation only), or explicitly widen req.text and document the breaking change. The "or" creates uncertainty for downstream consumers like view and show.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openspec/changes/fix-spec-parser-fidelity/design.md` around lines 67 - 68,
Clarify the mitigation in the spec parser design by choosing one display-text
behavior and reflecting it consistently in the relevant spec text: either keep
`req.text` as the first line while using the full body only for requirement
detection, or explicitly widen `req.text` and document that as a breaking
change. Update the section mentioning `view`/`show` counts and downstream
display so `design.md` states a single source of truth for `req.text`, removing
the current “or” ambiguity.
openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md (1)

29-36: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Clarify the "whole words" specification.

The requirement states the predicate "SHALL match SHALL or MUST as whole words" but doesn't define what constitutes a word boundary. For spec completeness and to prevent implementation drift, either:

  • Reference the shared predicate by name (e.g., containsShallOrMust), or
  • Include the boundary pattern explicitly (e.g., /\b(SHALL|MUST)\b/).

This ensures the scenario "Keyword detection agrees across paths" has an unambiguous acceptance criterion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md` around
lines 29 - 36, The normative-keyword predicate description is ambiguous because
“whole words” is not defined, which can lead to drift between validation paths.
Update the requirement text in cli-validate/spec.md to either reference the
shared predicate name used by the validator (such as containsShallOrMust) or
state the exact word-boundary rule explicitly so both the delta-spec and
schema-based paths use the same acceptance criterion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@openspec/changes/fix-spec-parser-fidelity/design.md`:
- Around line 67-68: Clarify the mitigation in the spec parser design by
choosing one display-text behavior and reflecting it consistently in the
relevant spec text: either keep `req.text` as the first line while using the
full body only for requirement detection, or explicitly widen `req.text` and
document that as a breaking change. Update the section mentioning `view`/`show`
counts and downstream display so `design.md` states a single source of truth for
`req.text`, removing the current “or” ambiguity.

In `@openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md`:
- Around line 29-36: The normative-keyword predicate description is ambiguous
because “whole words” is not defined, which can lead to drift between validation
paths. Update the requirement text in cli-validate/spec.md to either reference
the shared predicate name used by the validator (such as containsShallOrMust) or
state the exact word-boundary rule explicitly so both the delta-spec and
schema-based paths use the same acceptance criterion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 693c293e-6ee4-435d-aec0-8110b02cbc6e

📥 Commits

Reviewing files that changed from the base of the PR and between 0b07106 and 6d9f44d.

📒 Files selected for processing (6)
  • openspec/changes/fix-spec-parser-fidelity/design.md
  • openspec/changes/fix-spec-parser-fidelity/proposal.md
  • openspec/changes/fix-spec-parser-fidelity/specs/cli-archive/spec.md
  • openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md
  • openspec/changes/fix-spec-parser-fidelity/specs/openspec-conventions/spec.md
  • openspec/changes/fix-spec-parser-fidelity/tasks.md
✅ Files skipped from review due to trivial changes (2)
  • openspec/changes/fix-spec-parser-fidelity/tasks.md
  • openspec/changes/fix-spec-parser-fidelity/specs/cli-archive/spec.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • openspec/changes/fix-spec-parser-fidelity/specs/openspec-conventions/spec.md

@clay-good clay-good self-assigned this Jun 29, 2026
… upgrade Fission-AI#312, tier the risk

Second adversarial bulletproofing pass (reproduced everything against main):

- Add Fission-AI#418 (metadata-before-description): live on the spec path
  (req.text = "**ID**: ...") but ALREADY fixed on the delta path. The
  asymmetry is direct evidence for unifying the two extractors.
- Upgrade Fission-AI#312 from "regression hazard" to LIVE bug: a fenced code block
  before the prose line makes req.text = "```bash" on both paths today
  (distinct from the already-fixed section-count manifestation).
- Tier the fixes by risk after auditing the existing test contract
  (markdown-parser.test.ts, 15 tests green on main):
    Tier 1 (false-negative fixes Fission-AI#361/Fission-AI#418/Fission-AI#312): only widens what is read;
      updates one test (:331, which asserts the first-line bug). Fence tests
      (:106/:139) preserved because skip-and-join keeps SHALL-first bodies.
    Tier 2 (recognition tightening Fission-AI#498): canonical ### Requirement: only;
      a deliberate behavior change that updates bare-header tests (:258/:310)
      and needs a migration note. Flagged for maintainer decision, with a
      conservative opt-in-lint alternative documented.
- Surface the four-column extractor divergence table (capture / metadata /
  recognition / predicate) and an explicit "Behavior changes and test impact"
  section with exact test line refs.

Proposal passes `openspec validate --strict`. Does not claim Fission-AI#1156 (PR Fission-AI#1280).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@clay-good clay-good changed the title Propose: spec parser reading fidelity — multi-line + fence-aware requirement parsing, validate/archive parity (#361, #498, #312) Propose: spec parser reading fidelity — multi-line/metadata/fence-aware parsing + recognition parity (#361, #418, #312, #498) Jun 29, 2026
…d-scenario bug, Fission-AI#498→safe INFO

Third deep pass found the prior Tier 2 (recognition tightening to
`### Requirement:`) was the WRONG fix and over-scoped:

- Bare `### <statement>` headers are a SUPPORTED, tested requirement format:
  test/core/validation.test.ts asserts a bare-header spec is valid, and bare
  headers appear across json-converter/archive/spec tests and tmp-init
  fixtures. Tightening would break a large test surface and silently drop
  requirements from real specs. REJECTED, with evidence documented.
- Replace the Fission-AI#498 fix with a SAFE INFO note in validate <change> that surfaces
  non-`### Requirement:` headers in delta sections. INFO never fails validation
  (strict: valid = no errors && no warnings), so nothing newly fails.
- New bug found and folded in: countScenarios is fence-unaware, so a `####
  Scenario:` inside a fenced block is counted as real — a malformed delta passes
  validate <change> while validate <spec> correctly fails. Same fence family.
- Proved the archive WRITE path is independent of the reader: specs-apply
  rebuilds from raw `### Requirement:` blocks (extractRequirementsSection +
  RequirementBlock.raw), never parseSpec/req.text → Part A cannot change
  archived content.

Net effect: recognition is unchanged, so the proposal now updates exactly ONE
existing test (:331, the first-line assertion) instead of breaking bare-header
tests. Consolidated to a single cli-validate delta (dropped cli-archive and
openspec-conventions deltas). Dropped the no-space-header hypothesis (no
divergence). Passes `openspec validate --strict`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@clay-good clay-good changed the title Propose: spec parser reading fidelity — multi-line/metadata/fence-aware parsing + recognition parity (#361, #418, #312, #498) Propose: spec parser reading fidelity — unify the requirement reader (fence/metadata/multi-line aware) + surface #498 (#361, #418, #312) Jun 30, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md (2)

42-50: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Clarify scope: does the INFO-note requirement apply only to change deltas?

The requirement specifies "When an ## ADDED/## MODIFIED Requirements section in a change delta contains..." which correctly scopes it to change deltas. However, the relationship with the subsequent bare-header requirement (lines 51-54) could be clearer. Consider adding an explicit cross-reference note that this INFO-note behavior is specific to change-delta parsing and does not apply to main-spec bare headers, to prevent implementer confusion about whether bare ### <statement> headers in main specs should also trigger INFO notes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md` around
lines 42 - 50, Clarify that the INFO-note behavior is scoped only to
change-delta parsing in the validate spec, not main-spec bare headers. Update
the Requirement and Scenario around the `## ADDED`/`## MODIFIED Requirements`
section to explicitly reference the separate bare-header behavior so
implementers know `openspec validate` should emit INFO only for non-canonical
level-3 headers inside change deltas, while the bare `### <statement>` handling
in main specs remains unchanged.

22-22: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Capitalize "Markdown" as proper noun.

"Markdown" is a proper noun referring to the specific markup language and should be capitalized.

-The validator and markdown parser SHALL ignore lines inside fenced code blocks
+The validator and Markdown parser SHALL ignore lines inside fenced code blocks
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md` at line
22, Update the spec wording in the CLI validate documentation so “markdown” is
capitalized as “Markdown” wherever it appears in the affected requirement text.
Keep the existing meaning unchanged, and adjust the sentence in the spec entry
that describes the validator and Markdown parser behavior so the proper noun is
consistently capitalized.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md`:
- Around line 42-50: Clarify that the INFO-note behavior is scoped only to
change-delta parsing in the validate spec, not main-spec bare headers. Update
the Requirement and Scenario around the `## ADDED`/`## MODIFIED Requirements`
section to explicitly reference the separate bare-header behavior so
implementers know `openspec validate` should emit INFO only for non-canonical
level-3 headers inside change deltas, while the bare `### <statement>` handling
in main specs remains unchanged.
- Line 22: Update the spec wording in the CLI validate documentation so
“markdown” is capitalized as “Markdown” wherever it appears in the affected
requirement text. Keep the existing meaning unchanged, and adjust the sentence
in the spec entry that describes the validator and Markdown parser behavior so
the proper noun is consistently capitalized.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ec15f7df-979b-49a9-a47c-2d854d0ef52e

📥 Commits

Reviewing files that changed from the base of the PR and between 5506711 and c63913b.

📒 Files selected for processing (4)
  • openspec/changes/fix-spec-parser-fidelity/design.md
  • openspec/changes/fix-spec-parser-fidelity/proposal.md
  • openspec/changes/fix-spec-parser-fidelity/specs/cli-validate/spec.md
  • openspec/changes/fix-spec-parser-fidelity/tasks.md
✅ Files skipped from review due to trivial changes (3)
  • openspec/changes/fix-spec-parser-fidelity/design.md
  • openspec/changes/fix-spec-parser-fidelity/proposal.md
  • openspec/changes/fix-spec-parser-fidelity/tasks.md

@alfred-openspec alfred-openspec left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the proposal against the parser and validator paths. This is the right fix shape: unify requirement body/scenario reading while preserving the separate specs-apply write path and avoiding a breaking tightening of bare requirement headers. Approved, with focused tests needed for multiline bodies, metadata-before-prose, fenced code, fenced scenario exclusion, and INFO-only skipped-header surfacing.

…aware (Fission-AI#361, Fission-AI#418, Fission-AI#312); surface Fission-AI#498

The requirement reader was implemented twice — MarkdownParser.parseRequirements
(validate <spec>/archive) and Validator.extractRequirementText/countScenarios
(validate <change>) — and the two had drifted. Both now delegate to one shared,
fence-/metadata-/multi-line-aware extraction in parsers/requirement-text.ts so
they cannot diverge again.

Part A — unify the reader:
- Capture the full requirement body up to the first non-fenced `#### Scenario:`,
  skipping blank, `**metadata**:`, and fenced-code lines; run SHALL/MUST
  detection over the whole body. Fixes a wrapped keyword being dropped (Fission-AI#361),
  metadata before the description failing validate <spec> (Fission-AI#418), and a fenced
  block before the prose line becoming the requirement text (Fission-AI#312).
- Count only non-fenced `#### ` headers, so a `#### Scenario:` inside a fenced
  example no longer counts as a real scenario in validate <change> (parity with
  validate <spec>).
- One whole-word `\b(SHALL|MUST)\b` predicate (containsShallOrMust) shared by the
  validator and base.schema, replacing the substring/word-boundary split.
- Extract buildCodeFenceMask into the shared module; MarkdownParser and
  ChangeParser import it (single fence implementation).

Part B — surface Fission-AI#498 safely:
- validate <change> emits an INFO note when an ADDED/MODIFIED Requirements
  section contains a non-`### Requirement:` level-3 header (one the delta reader
  silently skips). INFO never changes the valid result, including under --strict,
  so nothing newly fails. Recognition is unchanged: bare `### <statement>`
  headers remain a supported requirement format.

Write path is unaffected: specs-apply rebuilds from raw `### Requirement:`
blocks, never req.text, so archived content cannot change. Displayed text in JSON
output and delta descriptions now reflects the full body.

Tests: markdown-parser.test.ts:331 updated to expect the full body; regression
tests added for Fission-AI#361/Fission-AI#418/Fission-AI#312, the fenced scenario, the Fission-AI#498 INFO note, a
single-line guard, and CRLF. Changeset added (patch). tasks.md completed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/core/parsers/requirement-text.ts`:
- Around line 71-72: Update the scenario header detection in requirement-text so
countScenarios() only counts real `#### Scenario:` headings, not any level-4
heading. Tighten the `SCENARIO_HEADER` pattern to match the documented scenario
format in `requirement-text.ts`, and keep the logic in `countScenarios()`
unchanged aside from using the stricter matcher. Add a regression test covering
a non-scenario H4 like `#### Notes` to confirm it no longer satisfies the “at
least one scenario” rule.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5af75cae-d271-47ec-a695-212ce1643d7f

📥 Commits

Reviewing files that changed from the base of the PR and between c63913b and 4fb8658.

📒 Files selected for processing (10)
  • .changeset/spec-parser-reading-fidelity.md
  • openspec/changes/fix-spec-parser-fidelity/tasks.md
  • src/core/parsers/change-parser.ts
  • src/core/parsers/markdown-parser.ts
  • src/core/parsers/requirement-blocks.ts
  • src/core/parsers/requirement-text.ts
  • src/core/schemas/base.schema.ts
  • src/core/validation/validator.ts
  • test/core/parsers/markdown-parser.test.ts
  • test/core/validation.test.ts
✅ Files skipped from review due to trivial changes (2)
  • .changeset/spec-parser-reading-fidelity.md
  • openspec/changes/fix-spec-parser-fidelity/tasks.md

Comment thread src/core/parsers/requirement-text.ts Outdated
Comment on lines +71 to +72
/** A level-4 scenario header (`#### Scenario: ...`). */
const SCENARIO_HEADER = /^####\s+/;

@coderabbitai coderabbitai Bot Jun 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Restrict scenario detection to real #### Scenario: headers.

Line 72 currently matches any level-4 heading, so countScenarios() will accept #### Notes as satisfying the “at least one scenario” rule. This weakens strict delta validation; match the documented scenario format instead and add a regression for a non-scenario H4.

Proposed fix
-const SCENARIO_HEADER = /^####\s+/;
+const SCENARIO_HEADER = /^####\s+Scenario:/i;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/** A level-4 scenario header (`#### Scenario: ...`). */
const SCENARIO_HEADER = /^####\s+/;
/** A level-4 scenario header (`#### Scenario: ...`). */
const SCENARIO_HEADER = /^####\s+Scenario:/i;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/parsers/requirement-text.ts` around lines 71 - 72, Update the
scenario header detection in requirement-text so countScenarios() only counts
real `#### Scenario:` headings, not any level-4 heading. Tighten the
`SCENARIO_HEADER` pattern to match the documented scenario format in
`requirement-text.ts`, and keep the logic in `countScenarios()` unchanged aside
from using the stricter matcher. Add a regression test covering a non-scenario
H4 like `#### Notes` to confirm it no longer satisfies the “at least one
scenario” rule.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not taking the tightening: the spec path treats every level-4 child of a requirement as a scenario, so restricting the delta counter to #### Scenario: would reintroduce exactly the cross-reader divergence this PR removes. 44fb1d7 documents the permissive match as deliberate and adds a test locking in that #### Notes satisfies the scenario rule on both paths.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clay-good Good catch — I hadn't considered that the spec-path (MarkdownParser) already treats every level-4 child heading as a scenario, so tightening the delta-side matcher would only reintroduce the exact divergence this PR is fixing. Keeping #### ... permissive on both sides for parity makes sense here, and locking it in with a test for #### Notes is a good way to prevent regression. Thanks for the context — withdrawing my suggestion.


✏️ Learnings added
Learnt from: clay-good
Repo: Fission-AI/OpenSpec PR: 1281
File: src/core/parsers/requirement-text.ts:0-0
Timestamp: 2026-07-02T22:26:54.642Z
Learning: In the OpenSpec parser fidelity work (src/core/parsers/requirement-text.ts, requirements-related validators), `countScenarios()`'s `SCENARIO_HEADER` regex deliberately matches any level-4 heading (`/^####\s+/`), not just `#### Scenario:`, because `MarkdownParser` (the main-spec reading path) already treats every level-4 child heading under a requirement as a scenario. Tightening the delta-path matcher to only `#### Scenario:` would reintroduce cross-reader divergence between `validate <change>` and `validate <spec>`. This permissive behavior is intentional and is locked in by a regression test asserting `#### Notes` counts as a scenario on both paths (see commit 44fb1d7).

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

…gn edge cases)

Exhaustive verification of the unified reader surfaced two design "edge cases
for tests" not yet covered by committed unit tests:

- Cross-reader predicate agreement: a SHALL substring inside a word ("MARSHALL")
  is rejected identically by validate <change> and validate <spec> — proving the
  one shared whole-word predicate, and guarding against a regression to the old
  substring check.
- Metadata-only body still fails validation (no requirement text) on the delta
  path.

Behavior unchanged; tests only. Full end-to-end parity across all four spec
requirements confirmed against the real Validator; no spurious INFO note fires on
any existing repo change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@clay-good clay-good changed the title Propose: spec parser reading fidelity — unify the requirement reader (fence/metadata/multi-line aware) + surface #498 (#361, #418, #312) [Bug] Unify the requirement reader (fence/metadata/multi-line aware) + surface #498 (#361, #418, #312) Jul 1, 2026

@TabishB TabishB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review is AI-generated.

}

/** Lines that look like `**ID**: ...` / `**Priority**: ...` metadata. */
const METADATA_LINE = /^\*\*[^*]+\*\*:/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also swallows requirement text written as **Constraint**: The system MUST ... — that spec is valid on main, fails here, and then blocks archive.

And when the skip leaves the body empty, the spec path falls back to the header title (markdown-parser.ts:158) while the delta path errors with "missing requirement text" — same content, different verdicts. Maybe skip metadata only when other body text remains, and put one empty-body rule in this shared helper?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 44fb1d7: metadata lines are now skipped only when other body text remains, so a **Constraint**: The system MUST ... body is kept as the requirement text (regression tests on both paths). The empty-body rule now lives in the shared extractRequirementText: both readers fall back to the header title, so the spec path and delta path reach the same verdict for the same block.

Comment thread src/core/parsers/requirement-text.ts Outdated
for (let i = 0; i < bodyLines.length; i++) {
if (mask[i]) continue; // inside a fenced code block
const line = bodyLines[i];
if (SCENARIO_HEADER.test(line)) break; // reached the first real scenario

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't stop at ### lines. On the delta path, a stray ### Background divider plus its notes gets absorbed into the requirement body, so a MUST in the notes now passes the keyword check (it errored on main) — and the new INFO's "is ignored by validation" becomes untrue. Break on any #-prefixed line like the old reader did?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 44fb1d7: the body now ends at the first non-fenced markdown header (/^#{1,6}\s/), so a stray ### Background divider's notes no longer feed the keyword check — added a regression where the MUST in the notes fails validation again and the divider gets its INFO note.

const METADATA_LINE = /^\*\*[^*]+\*\*:/;

/** A level-4 scenario header (`#### Scenario: ...`). */
const SCENARIO_HEADER = /^####\s+/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the comment says #### Scenario: but this matches any #### header. Worth noting that's deliberate (spec-path parity) so nobody "fixes" it later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 44fb1d7 — the comment now states the any-#### match is deliberate spec-path parity and warns against tightening it one-sided; also added a test locking in that #### Notes counts on both paths.

Comment thread src/core/parsers/requirement-blocks.ts Outdated
* `### Documentation Requirements` divider that `validate <change>` would
* otherwise pass without comment.
*/
export function findNonRequirementLevel3Headers(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scanner is fence-aware but splitTopLevelSections/parseRequirementBlocksFromSection aren't, so the note can describe boundaries the real reader doesn't use — e.g. a fenced ## ... line ends the section for the reader and a silently dropped requirement gets no INFO, and one unclosed fence suppresses all notes. Simpler for parseDeltaSpec to report the headers it actually skips?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked in 44fb1d7 as suggested: the standalone scanner is gone; parseDeltaSpec now records the non-canonical ### headers it skips while parsing (DeltaPlan.skippedHeaders), so the note uses the reader's real section boundaries and there's no whole-file fence mask to suppress notes across sections. Fence masking is per-section, matching how extractRequirementBody treats those lines as content.

Comment thread src/core/parsers/requirement-blocks.ts Outdated

const REQUIREMENT_HEADER_REGEX = /^###\s*Requirement:\s*(.+)\s*$/i;
/** The canonical requirement header the delta reader recognizes. */
export const REQUIREMENT_HEADER_REGEX = /^###\s*Requirement:\s*(.+)\s*$/i;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: nothing imports this — the INFO check ended up in this same file. Drop the export?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the export in 44fb1d7 (and with the scanner folded into parseDeltaSpec, nothing outside the file needs it).

Comment thread src/core/parsers/requirement-blocks.ts Outdated
after: string;
}

import { buildCodeFenceMask } from './requirement-text.js';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: move up top with the other imports.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved up top in 44fb1d7.

Comment thread src/core/validation/validator.ts Outdated
level: 'INFO',
path: entryPath,
line: stray.line,
message: `Header "### ${stray.header}" in ${stray.section} is not a "### Requirement:" header and is ignored by validation. Use "### Requirement: ${stray.header}" if it should be validated as a requirement.`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for a nameless ### Requirement: this reads: it "is not a ### Requirement: header ... Use ### Requirement: Requirement:". Special-case the empty name?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special-cased in 44fb1d7: a nameless ### Requirement: now gets "is missing a requirement name ... Add a name, e.g. "### Requirement: "" instead of the Requirement: Requirement: suggestion. Test added.


### Bug Fixes

- **Requirement reading fidelity** — The requirement reader used by `validate <change>`, `validate <spec>`, and `archive` is now unified into one fence-, metadata-, and multi-line-aware extraction, so the change-delta path and the main-spec path can no longer disagree:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"can no longer disagree" oversells a bit — e.g. a #### Scenario: with no body still passes validate <change> but fails validate <spec> (and blocks archive). Worth softening, and listing the known leftovers in design.md.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Softened in 44fb1d7 to "closing the known divergences" with a pointer to the design doc, and design.md now has a "Known remaining divergences" section listing the empty-scenario case you mention, the deliberate recognition split, and the fence-unaware delta section/block splitting.

@clay-good clay-good changed the title [Bug] Unify the requirement reader (fence/metadata/multi-line aware) + surface #498 (#361, #418, #312) refactor: unify requirement reader and surface #498 Jul 2, 2026
…traction, reader-derived INFO

- Skip **metadata**: lines only when other body text remains; a body written
  entirely as metadata (e.g. `**Constraint**: The system MUST ...`) is kept as
  the requirement text instead of being emptied (was a regression vs main).
- Move the empty-body rule into the shared reader: both paths fall back to the
  header title, so the same block cannot pass one path and fail the other.
- End body extraction at any non-fenced markdown header, restoring old-reader
  parity: a stray `### Background` divider's notes no longer satisfy the
  SHALL/MUST check.
- Replace the standalone fence-aware INFO scanner with skipped-header
  collection inside parseDeltaSpec, so the note reflects exactly what the
  reader skipped (same section boundaries, no whole-file fence mask).
- Special-case the nameless `### Requirement:` INFO message; document that the
  any-#### scenario match is deliberate spec-path parity; un-export
  REQUIREMENT_HEADER_REGEX; move the import up top.
- Soften the changeset claim and list the known remaining divergences in
  design.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mc856

mc856 commented Jul 3, 2026

Copy link
Copy Markdown

Checked this branch out at 44fb1d7 on top of current main (546224e) and ran the parser/validation suites — 69 passing here, and the #361/#418/#312 and fenced-scenario cases behave as described.

One recognition edge slips past the INFO note, found while poking at header variants: ###Requirement: with no space after the hashes. REQUIREMENT_HEADER_REGEX (/^###\s*Requirement:\s*(.+)\s*$/i, requirement-blocks.ts:22) accepts it, but MarkdownParser.parseSections requires whitespace (markdown-parser.ts:90) — matching GFM, which doesn't treat ###Requirement: as a heading at all. For this delta:

## ADDED Requirements

###Requirement: Export headers
The system SHALL export data with headers.

#### Scenario: export
- **WHEN** exporting
- **THEN** headers are included

validateChangeDeltaSpecs under strict returns valid with zero INFO — the reader accepts the no-space header rather than skipping it, so the reader-derived skip note never fires (and the h3 probe at requirement-blocks.ts:215 requires a space anyway). The same content as a main spec fails (Requirement must contain SHALL or MUST plus must have at least one scenario, because the spec reader promotes the #### Scenario: block to a requirement). And extractRequirementsSection on the write path does recognize the no-space header, so the block syncs into the main spec as-is. Net: change validates clean, the synced spec then fails validate <spec> — the same shape as #498.

Both regexes are unchanged from main, so this is pre-existing and I'd call it non-blocking here — but c63913b's message records the no-space hypothesis as dropped with "no divergence", which doesn't match what I see. Requiring one space in REQUIREMENT_HEADER_REGEX (the write path shares the constant, so it stays aligned) or having the scanner flag ###-prefixed lines regardless of spacing would both close it; at minimum it belongs on the known-leftovers list TabishB suggested for design.md.

@alfred-openspec

alfred-openspec commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Jun's no-space edge is real, but I would not tighten REQUIREMENT_HEADER_REGEX in this PR. The branch already treats ###Requirement: as an accepted legacy/header-normalization case in requirement-blocks.test.ts, and changing that shared regex would turn this from a reader-fidelity fix into a canonical Markdown/deprecation change with possible write-path drop behavior.

Please add this to design.md under known remaining divergences instead: no-space ###Requirement: is accepted by the delta/write requirement-block reader, but not by the Markdown spec reader, so it can validate pre-sync and fail post-sync. If we want to close it later, I’d do it as a separate compatibility change: either deprecate no-space headers with an INFO/WARN first, or broaden the skipped-header scanner to catch ^### lines regardless of spacing before tightening recognition.

TabishB and others added 2 commits July 3, 2026 21:59
…-conflict

# Conflicts:
#	src/core/schemas/base.schema.ts
#	src/core/validation/validator.ts
…nown leftover

Jun's edge (reproduced): the delta/write reader's REQUIREMENT_HEADER_REGEX
accepts `###Requirement:` with no space, but MarkdownParser.parseSections
requires whitespace (per GFM) — so a no-space requirement validates as a
change with zero INFO, syncs as-is, then fails validate <spec>. Pre-existing
on main and out of scope here (tightening the shared regex would change
write-path recognition); documented under known remaining divergences with
the follow-up options, folded together with the bullet from the merge
resolution. Corrects c63913b's 'no divergence' note.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@clay-good

Copy link
Copy Markdown
Collaborator Author

@mc856 confirmed — I reproduced your exact trace on this branch: the no-space delta validates clean under --strict with zero INFO, and the same content as a main spec fails with Requirement must contain SHALL or MUST + must have at least one scenario. The mechanism is the \s* / \s+ split you identified (requirement-blocks.ts:22 vs markdown-parser.ts:90). You're also right about c63913b — its "no divergence" note is wrong; the hypothesis was dismissed without testing the cross-path sync shape you demonstrated.

Taking @alfred-openspec's route rather than tightening: the no-space form is a tested normalization case (requirement-blocks.test.ts), and REQUIREMENT_HEADER_REGEX is shared with the write path, so tightening it here would turn a reader-fidelity fix into a recognition/deprecation change. a7cc64c records it under "Known remaining divergences" in design.md — full mechanism, why it's accepted, and the two follow-up options (deprecation INFO/WARN first, or broaden the skipped-header collection to any ^### line before tightening) — folded together with the bullet Tabish added in the merge resolution (aae16e0) so there's a single entry.

Re-verified on the merged head (post-#1280): build clean, 1811 tests passing (the 17 zsh-installer failures are local-env-only — oh-my-zsh present; CI unaffected), openspec validate fix-spec-parser-fidelity --strict passes, and the no-space repro still behaves exactly as now documented.

@alfred-openspec alfred-openspec left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed latest a7cc64c. The docs now clearly call out the remaining no-space divergence as pre-existing/out-of-scope, and the implementation keeps the safer INFO-only behavior for skipped delta headers. CI is green, so this is good to merge from my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line breaks in headers cause validation issues

4 participants