Skip to content

Fix SPI readiness detection - #409

Merged
mohanagy merged 1 commit into
mainfrom
issue-404-spi-evidence-detector
May 29, 2026
Merged

mohanagy merged 1 commit into
mainfrom
issue-404-spi-evidence-detector

Conversation

@mohanagy

@mohanagy mohanagy commented May 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • persist an explicit spi_mode flag in exported SPI graphs
  • make compare benchmark readiness read SPI status from graph metadata instead of source-file path strings
  • add regressions for SPI-mode app-file graphs and generate-time graph header export

Closes #404

Verification

  • npm run test:run -- tests/unit/compare.test.ts tests/unit/generate-spi-flag.test.ts
  • npm run typecheck
  • npm run build
  • CI=1 npm run test:run

Summary by CodeRabbit

Bug Fixes

  • Improved SPI evidence detection: Benchmark readiness assessments now use direct graph metadata flags to determine SPI support status, replacing the previous file-path-based detection method. This enhances accuracy and reliability of readiness evaluations, providing more consistent and dependable assessment results.

Review Change Stack

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR fixes a false-negative in benchmark-readiness SPI-evidence detection by switching from source-file path pattern matching to an explicit spi_mode metadata flag. The graph is marked during generation when SPI is enabled, exported in JSON output, and the readiness assessment now reads this flag instead of searching for spi-like path patterns.

Changes

SPI-mode graph generation and readiness detection

Layer / File(s) Summary
Graph generation and SPI mode export
src/infrastructure/generate.ts, src/pipeline/export.ts
generateGraph conditionally sets graph.graph.spi_mode = true when options.useSpi is enabled. The JSON export in toJson now includes the spi_mode field in the output payload.
SPI readiness detection via graph metadata
src/infrastructure/compare.ts
New graphPathHasSpiMode(graphPath) helper reads and parses the graph JSON file to check if spi_mode is true. The readiness assessment replaces the path-based retrievalHasSpiEvidence() check with this metadata-based detector.
SPI flag generation test coverage
tests/unit/generate-spi-flag.test.ts
Test suite now parses the generated graph.json and asserts that spi_mode is true when useSpi is enabled and undefined otherwise.
Readiness assessment test refactoring and validation
tests/unit/compare.test.ts
Introduces writeReadinessGraphFixture() helper for fixture creation with optional SPI metadata. Tests now verify that SPI-mode graph metadata counts as evidence (ready state), non-SPI graphs flag missing evidence (degraded state), and top-level SPI paths no longer qualify as evidence without graph metadata.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • mohanagy/madar#392: Introduces the SPI-evidence-driven readiness preflight; this PR fixes the underlying SPI-detection mechanism that the earlier PR depends on.

Poem

🐰 A graph that speaks its truth so clear,
No more false paths that hide the tier!
With spi_mode marked upon the face,
Readiness knows just where to place.
From paths to flags—the rabbit's way! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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
Title check ✅ Passed The title 'Fix SPI readiness detection' directly and concisely describes the main change, which replaces path-string-based SPI evidence detection with graph metadata-based detection.
Description check ✅ Passed The description includes a clear summary, verification steps matching the template checklist items, and references the closed issue #404. All key changes are documented.
Linked Issues check ✅ Passed All acceptance criteria from issue #404 are addressed: SPI-mode graphs with app files are recognized without false 'no SPI evidence' warnings [#404], non-SPI graphs still report correctly [#404], fixture tests are added for both directions [#404], and no regressions are introduced in existing tests [#404].
Out of Scope Changes check ✅ Passed All changes directly support the stated objectives: introducing spi_mode flag in export, updating readiness detection logic, and adding comprehensive regression tests. No unrelated or scope-creeping modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-404-spi-evidence-detector

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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

@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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/infrastructure/compare.ts`:
- Around line 1675-1685: The graphPathHasSpiMode function can throw when
readFileSync/JSON.parse encounters IO or invalid JSON, turning a readiness check
into a crash; wrap the file read and JSON.parse in a try/catch and return false
on any error, keep the existing existsSync check and the parsed null/object/type
guard, and finally return whether (parsed as { spi_mode?: unknown }).spi_mode
=== true; this ensures failures are handled closed (false) rather than throwing.

In `@src/infrastructure/generate.ts`:
- Around line 441-443: The code sets graph.graph.spi_mode when options.useSpi is
true but never clears it, causing stale true values on update paths; update the
logic around options.useSpi so that when options.useSpi is false you explicitly
clear or set graph.graph.spi_mode = false (or delete the property) to ensure
non-SPI generation paths do not retain a previous SPI flag; look for the symbols
options.useSpi and graph.graph.spi_mode in generate.ts to make this change.
🪄 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 Plus

Run ID: 092ed08c-351a-4d22-a91c-fb023a5b0d69

📥 Commits

Reviewing files that changed from the base of the PR and between 1b9684c and 78d0317.

📒 Files selected for processing (5)
  • src/infrastructure/compare.ts
  • src/infrastructure/generate.ts
  • src/pipeline/export.ts
  • tests/unit/compare.test.ts
  • tests/unit/generate-spi-flag.test.ts

Comment on lines +1675 to +1685
function graphPathHasSpiMode(graphPath: string): boolean {
if (!existsSync(graphPath)) {
return false
}

const parsed = JSON.parse(readFileSync(graphPath, 'utf8')) as unknown
if (parsed === null || typeof parsed !== 'object') {
return false
}

return (parsed as { spi_mode?: unknown }).spi_mode === true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard graph JSON parsing in SPI readiness detection.

graphPathHasSpiMode() can throw on invalid JSON or read races (Line 1680), which turns a readiness check into a hard failure. This path should fail closed (false) instead.

Proposed fix
 function graphPathHasSpiMode(graphPath: string): boolean {
   if (!existsSync(graphPath)) {
     return false
   }
-
-  const parsed = JSON.parse(readFileSync(graphPath, 'utf8')) as unknown
-  if (parsed === null || typeof parsed !== 'object') {
-    return false
-  }
-
-  return (parsed as { spi_mode?: unknown }).spi_mode === true
+  try {
+    const parsed = JSON.parse(readFileSync(graphPath, 'utf8')) as unknown
+    if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
+      return false
+    }
+    return (parsed as { spi_mode?: unknown }).spi_mode === true
+  } catch {
+    return false
+  }
 }
📝 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
function graphPathHasSpiMode(graphPath: string): boolean {
if (!existsSync(graphPath)) {
return false
}
const parsed = JSON.parse(readFileSync(graphPath, 'utf8')) as unknown
if (parsed === null || typeof parsed !== 'object') {
return false
}
return (parsed as { spi_mode?: unknown }).spi_mode === true
function graphPathHasSpiMode(graphPath: string): boolean {
if (!existsSync(graphPath)) {
return false
}
try {
const parsed = JSON.parse(readFileSync(graphPath, 'utf8')) as unknown
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
return false
}
return (parsed as { spi_mode?: unknown }).spi_mode === true
} catch {
return false
}
}
🤖 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/infrastructure/compare.ts` around lines 1675 - 1685, The
graphPathHasSpiMode function can throw when readFileSync/JSON.parse encounters
IO or invalid JSON, turning a readiness check into a crash; wrap the file read
and JSON.parse in a try/catch and return false on any error, keep the existing
existsSync check and the parsed null/object/type guard, and finally return
whether (parsed as { spi_mode?: unknown }).spi_mode === true; this ensures
failures are handled closed (false) rather than throwing.

Comment on lines +441 to +443
if (options.useSpi) {
graph.graph.spi_mode = true
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Clear stale spi_mode on non-SPI generation paths.

spi_mode is only ever set, never unset. On Line 441, --update runs that reuse an old SPI graph can keep spi_mode: true even when useSpi is false, causing false-positive SPI readiness later.

Proposed fix
   graph.graph.root_path = resolvedRootPath
-  if (options.useSpi) {
-    graph.graph.spi_mode = true
-  }
+  if (!options.clusterOnly) {
+    if (options.useSpi) {
+      graph.graph.spi_mode = true
+    } else {
+      delete graph.graph.spi_mode
+    }
+  }
📝 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
if (options.useSpi) {
graph.graph.spi_mode = true
}
if (options.useSpi) {
graph.graph.spi_mode = true
} else {
delete graph.graph.spi_mode
}
🤖 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/infrastructure/generate.ts` around lines 441 - 443, The code sets
graph.graph.spi_mode when options.useSpi is true but never clears it, causing
stale true values on update paths; update the logic around options.useSpi so
that when options.useSpi is false you explicitly clear or set
graph.graph.spi_mode = false (or delete the property) to ensure non-SPI
generation paths do not retain a previous SPI flag; look for the symbols
options.useSpi and graph.graph.spi_mode in generate.ts to make this change.

@mohanagy
mohanagy merged commit c5b49c3 into main May 29, 2026
7 checks passed
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.

[P1] benchmark_readiness SPI-evidence detector false-negative: SPI-mode packs of app files report "no SPI evidence"

1 participant