fix(viewer): surface health status from non-2xx /agentmemory/health responses#1046
fix(viewer): surface health status from non-2xx /agentmemory/health responses#1046viditchess64 wants to merge 2 commits into
Conversation
…esponses The health endpoint intentionally returns HTTP 503 when status is "critical" (src/triggers/api.ts), with a valid JSON body describing the degraded state. The viewer's shared api() fetch helper treated any non-ok response as a hard failure and discarded the body, returning null - so renderDashboard's `h.status || 'unknown'` fallback always showed "unknown" for a critical backend instead of "critical". Parse and return the JSON body on non-ok responses too (falling back to null only if the body isn't valid JSON), so the dashboard reflects the real health status regardless of the HTTP status code used to carry it. Fixes rohitg00#1019 Signed-off-by: Vidit Gujrathi <223816573+viditchess64@users.noreply.github.com>
|
@viditchess64 is attempting to deploy a commit to the rohitg00's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe viewer’s ChangesViewer API response handling
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/viewer/index.html (1)
1258-1259: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low valueParse error is silently swallowed.
The
catch (parseErr)block discards the error without logging. While this is intentional for the fallback-to-null behavior, logging at debug/trace level would help diagnose cases where a non-2xx response has an unexpected content type.🤖 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/viewer/index.html` around lines 1258 - 1259, In the response parsing catch block, retain the fallback return of null but log parseErr at debug or trace level with context about the non-2xx response and unexpected content type. Update the catch handling near the response parsing logic without changing its fallback behavior.
🤖 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/viewer/index.html`:
- Around line 1256-1260: Ensure api() preserves the null-on-HTTP-error contract:
after parsing the response, return null for non-2xx statuses so
graphDisabledResponse() and loadGraph() trigger their existing error handling;
keep parsed JSON only for successful responses, while retaining the parse-error
fallback.
---
Nitpick comments:
In `@src/viewer/index.html`:
- Around line 1258-1259: In the response parsing catch block, retain the
fallback return of null but log parseErr at debug or trace level with context
about the non-2xx response and unexpected content type. Update the catch
handling near the response parsing logic without changing its fallback behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| try { | ||
| return await res.json(); | ||
| } catch (parseErr) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Find callers that check for null returns from api/apiGet/apiPost
rg -n '=== null|== null|!== null|!= null' src/viewer/index.html -B2 -A2 | head -80Repository: rohitg00/agentmemory
Length of output: 2650
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== api helper and nearby callers =="
sed -n '1180,1715p' src/viewer/index.html | cat -n | sed -n '1,560p'
echo
echo "== search for api() callers and null checks =="
rg -n 'apiGet\(|apiPost\(|api\(' src/viewer/index.html -A3 -B3
rg -n '=== null|== null|!== null|!= null' src/viewer/index.html -A2 -B2
echo
echo "== server endpoints that may return non-2xx =="
rg -n 'unauthorized|res\.status|statusCode|json\(|text/plain|graph/query|health' src -A3 -B3Repository: rohitg00/agentmemory
Length of output: 50376
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== graph/query handler =="
rg -n 'graph/query|queryGraph|/graph/query|status\s*[:=].*500|res\.status\(|throw .*graph|return .*graph' src -A6 -B6
echo
echo "== health handler =="
rg -n 'health|/health|status\s*[:=].*critical|degraded|res\.status\(|res\.json\(|res\.send\(' src -A6 -B6
echo
echo "== viewer callers that treat null specially =="
rg -n 'if \([^)]*=== null\)|if \([^)]*!== null\)|\|\| \[\]' src/viewer/index.html -A2 -B2 | sed -n '1,120p'Repository: rohitg00/agentmemory
Length of output: 50376
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== graphDisabledResponse and graph/query shapes =="
sed -n '1,220p' src/triggers/api.ts | cat -n | sed -n '1,220p'
echo
sed -n '620,760p' src/functions/graph.ts | cat -n | sed -n '1,220p'
echo
echo "== any explicit non-2xx JSON responses relevant to viewer =="
rg -n 'status_code:\s*(4|5)\d\d|status_code:\s*503|graphDisabledResponse|body:\s*\{' src/triggers/api.ts src/functions/graph.ts -A4 -B4Repository: rohitg00/agentmemory
Length of output: 50376
Preserve the null-on-HTTP-error contract here.
api() now parses JSON for non-2xx responses, so graphDisabledResponse() and other 503 bodies will reach this branch as objects instead of null. loadGraph() will treat HTTP failures as empty data and skip the error banner/rebuild path. Keep non-2xx as null, or return status alongside the body and check it here.
🤖 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/viewer/index.html` around lines 1256 - 1260, Ensure api() preserves the
null-on-HTTP-error contract: after parsing the response, return null for non-2xx
statuses so graphDisabledResponse() and loadGraph() trigger their existing error
handling; keep parsed JSON only for successful responses, while retaining the
parse-error fallback.
Address CodeRabbit review: don't silently swallow the parse error when a non-2xx response body isn't valid JSON - log it at debug level to help diagnose unexpected content types. Signed-off-by: Vidit Gujrathi <223816573+viditchess64@users.noreply.github.com>
What
The viewer's shared
api()fetch helper discarded the response body wheneverthe HTTP status wasn't 2xx, returning
null./agentmemory/healthintentionallyreturns HTTP 503 when status is "critical" (still with a valid JSON body), so
the dashboard's health badge always fell back to "unknown" instead of showing
"critical".
Fix
api()now attempts to parse and return the JSON body on non-ok responses too,falling back to
nullonly if the body isn't valid JSON. This letsrenderDashboard()read the realstatusfield regardless of which HTTPstatus code carried it.
How to verify
fetchto return
{ok:false, status:503, json: () => ({status:"critical", ...})}).Existing viewer test suite (
viewer-session-id.test.ts,viewer-security.test.ts,viewer-host.test.ts) passes unchanged.Fixes #1019
Summary by CodeRabbit