Repository navigation
fix(dashboard): agent-bridge page crash — normalize /state response (#3318) #3326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 1 commit into
release/v3.8.13
from
fix/3318-agent-bridge-state-normalize
Jun 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/app/(dashboard)/dashboard/tools/agent-bridge/normalizeState.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import type { | ||
| AgentBridgePageData, | ||
| AgentBridgeServerState, | ||
| AgentStateEntry, | ||
| AgentMappingsMap, | ||
| } from "./AgentBridgePageClient"; | ||
|
|
||
| function defaultServerState(): AgentBridgeServerState { | ||
| return { | ||
| running: false, | ||
| port: 443, | ||
| certTrusted: false, | ||
| upstreamCa: null, | ||
| lastStartedAt: null, | ||
| activeConns: 0, | ||
| interceptedCount: 0, | ||
| }; | ||
| } | ||
|
|
||
| export const DEFAULT_AGENT_BRIDGE_STATE: AgentBridgePageData = { | ||
| serverState: defaultServerState(), | ||
| agentStates: [], | ||
| bypassPatterns: [], | ||
| mappings: {}, | ||
| }; | ||
|
|
||
| function isRecord(v: unknown): v is Record<string, unknown> { | ||
| return typeof v === "object" && v !== null && !Array.isArray(v); | ||
| } | ||
|
|
||
| /** | ||
| * Normalize whatever `/api/tools/agent-bridge/state` returns into the shape the | ||
| * page/components require, ALWAYS returning a well-formed object (#3318). | ||
| * | ||
| * The page previously assigned the raw response straight into `initialData`, but | ||
| * the route returns `{ server, agents }` while the UI reads | ||
| * `{ serverState, agentStates, bypassPatterns, mappings }` — leaving | ||
| * `serverState` undefined and crashing `serverState.running`. | ||
| * | ||
| * This maps the known server fields through (incl. the legacy `server` key → | ||
| * `serverState`, `server.certExists` → `certTrusted`) and falls back to safe | ||
| * defaults for everything else, so the page renders instead of crashing. | ||
| * Note: the legacy `agents` payload has a different per-entry shape than | ||
| * `AgentStateEntry`, so it is intentionally NOT coerced — `agentStates` defaults | ||
| * to `[]` unless the response already provides the correct `agentStates` key. | ||
| */ | ||
| export function normalizeAgentBridgeState(raw: unknown): AgentBridgePageData { | ||
| if (!isRecord(raw)) return { ...DEFAULT_AGENT_BRIDGE_STATE, serverState: defaultServerState() }; | ||
|
|
||
| const serverState = defaultServerState(); | ||
| const source = isRecord(raw.serverState) | ||
| ? raw.serverState | ||
| : isRecord(raw.server) | ||
| ? raw.server | ||
| : undefined; | ||
| if (source) { | ||
| if (typeof source.running === "boolean") serverState.running = source.running; | ||
| if (typeof source.port === "number") serverState.port = source.port; | ||
| // accept both the canonical `certTrusted` and the legacy `certExists` | ||
| if (typeof source.certTrusted === "boolean") serverState.certTrusted = source.certTrusted; | ||
| else if (typeof source.certExists === "boolean") serverState.certTrusted = source.certExists; | ||
| if (typeof source.upstreamCa === "string") serverState.upstreamCa = source.upstreamCa; | ||
| if (typeof source.lastStartedAt === "string") serverState.lastStartedAt = source.lastStartedAt; | ||
| if (typeof source.activeConns === "number") serverState.activeConns = source.activeConns; | ||
| if (typeof source.interceptedCount === "number") { | ||
| serverState.interceptedCount = source.interceptedCount; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| serverState, | ||
| agentStates: Array.isArray(raw.agentStates) ? (raw.agentStates as AgentStateEntry[]) : [], | ||
| bypassPatterns: Array.isArray(raw.bypassPatterns) ? (raw.bypassPatterns as string[]) : [], | ||
| mappings: isRecord(raw.mappings) ? (raw.mappings as AgentMappingsMap) : {}, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| const { normalizeAgentBridgeState, DEFAULT_AGENT_BRIDGE_STATE } = await import( | ||
| "../../src/app/(dashboard)/dashboard/tools/agent-bridge/normalizeState.ts" | ||
| ); | ||
|
|
||
| // #3318: the /api/tools/agent-bridge/state route returns `{ server, agents }`, | ||
| // but the page/components read `{ serverState, agentStates, bypassPatterns, | ||
| // mappings }`. The page replaced its well-shaped default with the raw response, | ||
| // so `serverState` became undefined → `serverState.running` crashed the page | ||
| // with the full "Internal Server Error" boundary. The normalizer must always | ||
| // return a well-shaped object (never an undefined serverState), mapping the | ||
| // known server fields through. | ||
|
|
||
| test("the raw /state route shape lacks the keys the page reads (documents the bug)", () => { | ||
| const routeShape = { | ||
| server: { running: true, pid: 123, dnsConfigured: true, certExists: true }, | ||
| agents: [{ id: "claude-code", name: "Claude Code", hosts: [], viability: "ok" }], | ||
| }; | ||
| // This is exactly what the old code assigned straight into initialData. | ||
| assert.equal(routeShape.serverState, undefined); | ||
| }); | ||
|
|
||
| test("normalizeAgentBridgeState maps the route shape and never leaves serverState undefined (#3318)", () => { | ||
| const routeShape = { | ||
| server: { running: true, pid: 123, dnsConfigured: true, certExists: true }, | ||
| agents: [{ id: "claude-code", name: "Claude Code", hosts: [], viability: "ok" }], | ||
| }; | ||
| const result = normalizeAgentBridgeState(routeShape); | ||
|
|
||
| assert.ok(result.serverState, "serverState must be defined"); | ||
| assert.equal(result.serverState.running, true, "server.running maps through"); | ||
| assert.equal(result.serverState.certTrusted, true, "server.certExists -> certTrusted"); | ||
| assert.ok(Array.isArray(result.agentStates), "agentStates is always an array"); | ||
| assert.ok(Array.isArray(result.bypassPatterns), "bypassPatterns is always an array"); | ||
| assert.equal(typeof result.mappings, "object", "mappings is always an object"); | ||
| }); | ||
|
|
||
| test("normalizeAgentBridgeState falls back to safe defaults for empty/garbage input", () => { | ||
| for (const bad of [null, undefined, {}, 42, "x", []]) { | ||
| const result = normalizeAgentBridgeState(bad); | ||
| assert.ok(result.serverState, `serverState defined for ${JSON.stringify(bad)}`); | ||
| assert.equal(result.serverState.running, false); | ||
| assert.ok(Array.isArray(result.agentStates)); | ||
| assert.ok(Array.isArray(result.bypassPatterns)); | ||
| assert.equal(typeof result.mappings, "object"); | ||
| } | ||
| }); | ||
|
|
||
| test("normalizeAgentBridgeState passes a correctly-shaped payload through intact", () => { | ||
| const correct = { | ||
| ...DEFAULT_AGENT_BRIDGE_STATE, | ||
| serverState: { ...DEFAULT_AGENT_BRIDGE_STATE.serverState, running: true, port: 8443 }, | ||
| agentStates: [ | ||
| { | ||
| agent_id: "claude-code", | ||
| dns_enabled: true, | ||
| cert_trusted: true, | ||
| setup_completed: true, | ||
| last_started_at: null, | ||
| last_error: null, | ||
| }, | ||
| ], | ||
| bypassPatterns: ["*.internal"], | ||
| mappings: { "claude-code": [] }, | ||
| }; | ||
| const result = normalizeAgentBridgeState(correct); | ||
| assert.equal(result.serverState.running, true); | ||
| assert.equal(result.serverState.port, 8443); | ||
| assert.equal(result.agentStates.length, 1); | ||
| assert.equal(result.agentStates[0].agent_id, "claude-code"); | ||
| assert.deepEqual(result.bypassPatterns, ["*.internal"]); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning a shallow copy of
DEFAULT_AGENT_BRIDGE_STATEwhenrawis not a record can lead to shared reference mutation bugs. If a caller mutates the returnedagentStates,bypassPatterns, ormappingsarrays/objects, those mutations will affect the globalDEFAULT_AGENT_BRIDGE_STATEconstant and persist across subsequent calls. Returning a fresh object literal with new arrays and objects avoids this risk.