Skip to content

fix: check res.ok before parsing knowledge-graph.json in dashboard#418

Draft
eldar702 wants to merge 1 commit into
Egonex-AI:mainfrom
eldar702:fix/288-knowledge-graph-fetch-res-ok
Draft

fix: check res.ok before parsing knowledge-graph.json in dashboard#418
eldar702 wants to merge 1 commit into
Egonex-AI:mainfrom
eldar702:fix/288-knowledge-graph-fetch-res-ok

Conversation

@eldar702

@eldar702 eldar702 commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Closes #288. The dashboard's knowledge-graph.json fetch in App.tsx did not check res.ok before calling res.json() — the four sibling fetches in the same effect (meta.json, config.json, diff-overlay.json, domain-graph.json) all do. So when the graph file is missing (e.g. before /understand has run → HTTP 404), the 404 body was parsed and run through validateGraph, surfacing the misleading "Invalid knowledge graph: Missing or invalid project metadata" instead of a clear HTTP error.

Thanks to @ZebangCheng for diagnosing the exact root cause and file in the issue.

Root cause

packages/dashboard/src/App.tsx (knowledge-graph fetch) used .then((res) => res.json()) with no res.ok guard, unlike the sibling fetches.

Fix

Extracted the fetch+validate step into a small, testable helper utils/fetchAndValidateGraph.ts that guards on res.ok and returns a discriminated result (loaded / http-error / network-error). App.tsx now surfaces a clear HTTP/network error for a missing graph instead of a schema-validation message; the valid-graph and invalid-JSON paths are unchanged. (The helper extraction lets the behavior be unit-tested — packages/dashboard had no test harness.)

Testing

Added utils/__tests__/fetchAndValidateGraph.test.ts: a mocked fetch returning {ok:false, status:404} now surfaces an HTTP-status error (not the schema message); 200-with-invalid-JSON still yields a validation error; network rejection is handled. RED before the fix, GREEN after.


AI-assisted contribution — drafted with Claude and verified against current main.

The dashboard's knowledge-graph.json fetch parsed the response body
unconditionally (`.then((res) => res.json())`) with no `res.ok` guard,
while its four sibling fetches in the same component (meta.json,
config.json, diff-overlay.json, domain-graph.json) all check `res.ok`
first. When the graph file was missing or returned a non-2xx status,
the 404/error body was handed to `validateGraph`, which then surfaced
the misleading "Invalid knowledge graph: Missing or invalid project
metadata" instead of a clear HTTP error.

Diagnosis credited to collaborator ZebangCheng, who identified the exact
missing guard.

Fix: extract the fetch + HTTP-status guard + parse + validate chain into
a small pure helper (`fetchAndValidateGraph`) that mirrors the sibling
fetches by short-circuiting on `!res.ok` with an HTTP-status error, and
wire App.tsx to use it. A RED-first unit test (mocked fetch) asserts that
a 404 surfaces an HTTP error containing the status code rather than the
schema-validation message; the 200-with-invalid-body and 401 paths are
also covered.

Closes Egonex-AI#288

AI-assisted contribution.
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.

Invalid knowledge graph: Missing or invalid project metadata

1 participant