Skip to content

fix(viewer): surface health status from non-2xx /agentmemory/health responses#1046

Open
viditchess64 wants to merge 2 commits into
rohitg00:mainfrom
viditchess64:fix/1019-viewer-health-badge
Open

fix(viewer): surface health status from non-2xx /agentmemory/health responses#1046
viditchess64 wants to merge 2 commits into
rohitg00:mainfrom
viditchess64:fix/1019-viewer-health-badge

Conversation

@viditchess64

@viditchess64 viditchess64 commented Jul 10, 2026

Copy link
Copy Markdown

What

The viewer's shared api() fetch helper discarded the response body whenever
the HTTP status wasn't 2xx, returning null. /agentmemory/health intentionally
returns 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 null only if the body isn't valid JSON. This lets
renderDashboard() read the real status field regardless of which HTTP
status code carried it.

How to verify

  1. Point the viewer at a backend reporting critical health (or mock fetch
    to return {ok:false, status:503, json: () => ({status:"critical", ...})}).
  2. Before this change: dashboard health badge shows "unknown".
  3. After this change: dashboard health badge shows "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

  • Bug Fixes
    • Improved handling of non-successful API responses by preserving and returning valid JSON error details.
    • Added support for endpoints that use non-2xx statuses while still providing useful status information.

…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>
@vercel

vercel Bot commented Jul 10, 2026

Copy link
Copy Markdown

@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.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The viewer’s api() helper now attempts to parse and return JSON bodies for non-2xx responses, while preserving status logging and the 401 authentication prompt. It returns null when parsing fails.

Changes

Viewer API response handling

Layer / File(s) Summary
Parse non-OK response payloads
src/viewer/index.html
api() now returns parsed JSON for non-OK responses and falls back to null when parsing fails, while retaining existing status logging and 401 handling.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: preserving health status from non-2xx viewer API responses.
Linked Issues check ✅ Passed The change fixes the unknown-health fallback by parsing non-2xx JSON, letting the UI surface the backend’s critical state as requested in #1019.
Out of Scope Changes check ✅ Passed The diff stays focused on the viewer API helper and a clarifying comment, with no unrelated feature or refactor changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@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

🧹 Nitpick comments (1)
src/viewer/index.html (1)

1258-1259: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Parse 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

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: de0e35e0-f9cc-4e6a-b3e3-172942352b2e

📥 Commits

Reviewing files that changed from the base of the PR and between 93ae9bc and 7b19562.

📒 Files selected for processing (1)
  • src/viewer/index.html

Comment thread src/viewer/index.html
Comment on lines +1256 to +1260
try {
return await res.json();
} catch (parseErr) {
return null;
}

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.

🗄️ 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 -80

Repository: 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 -B3

Repository: 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 -B4

Repository: 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>
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.

Viewer shows unknown health while /agentmemory/health reports critical

1 participant