Repository navigation
fix(runtime): guard RuntimeBridge JSON.parse against partial output - #1918
Conversation
execCommand, readSnapshot, and readCompatFile each called JSON.parse with no try/catch. Partial Rust writes (mid-rename, kill -9, schema drift) surface as a SyntaxError that aborts the HUD query path or the dispatch watcher loop instead of degrading gracefully. - execCommand / readSnapshot now throw a typed RuntimeBridgeError that preserves the failing command name plus a 200-byte stdout preview, so dispatch callers (team/state/dispatch.ts, mailbox.ts) can recognise parse failure with `instanceof` and mark the command failed without tearing down the surrounding watcher. - readCompatFile returns null on parse failure so HUD/dispatch fall through to JS-inferred state for the current tick. The existing callers in hud/state.ts and team/state.ts already treat null as the legacy-path fallback (`existing?.records` / `runtimeSnapshot` already initialised to null), so this is behaviour-compatible. Builds on PR Yeachan-Heo#548's safe-json utility (Issue Yeachan-Heo#506); RuntimeBridge was not in that PR's scope but exhibits the same crash hazard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 75475b9989
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } catch { | ||
| return null; | ||
| } | ||
| return safeJsonParse<T | null>(content, null); |
There was a problem hiding this comment.
Keep legacy fallback on second compat read failures
Returning null here turns parse/read failures into non-exceptional results, but readDispatchRequests and readMailbox in src/team/state.ts read the same compat file twice (first readCompatFile(...) as a probe, then readDispatchRecords()/readMailboxRecords()). If the runtime rewrites the file between those two reads, the first read can pass while the second now quietly becomes [] instead of throwing, so the outer try/catch no longer drops to the legacy-file path; this can transiently hide pending records and break dedupe behavior (for example, duplicate dispatch enqueue).
Useful? React with 👍 / 👎.
Yeachan-Heo
left a comment
There was a problem hiding this comment.
Approved. This is a narrow runtime bug fix: it converts malformed/non-JSON RuntimeBridge output into typed bridge errors or null compat fallback without expanding the runtime surface. CI is green and the added coverage matches the guarded JSON boundary.
—
[repo owner's gaebal-gajae (clawdbot) 🦞]
Summary
RuntimeBridge.execCommand,readSnapshot, andreadCompatFileeach callJSON.parsewith no try/catch. A partial Rust write (mid-rename,kill -9, schema drift, panic-before-flush) surfaces as aSyntaxErrorthat aborts the HUD render path or the dispatch watcher loop instead of degrading gracefully.This builds on #548 (
fix(quality): safe JSON.parse, …) and #506.RuntimeBridgewas not in #548's 16-file scope but exhibits the same hazard.Reproducer
Before:
SyntaxError: Unexpected end of JSON inputaborts the entirebuildAllStatequery atsrc/hud/state.ts:458.After: HUD falls through to JS-inferred state for this tick.
Fix
execCommandRuntimeBridgeErrorso dispatch callers (src/team/state/dispatch.ts:169,src/team/state/mailbox.ts:44) can recognise parse failure withinstanceofand mark the command failed without tearing down the surrounding watcher.readSnapshotstdoutPreviewand the underlyingcause.readCompatFilenull. The two production callers (src/hud/state.ts:454-458,src/team/state.ts:1488) already initialise tonull(runtimeSnapshot) or use optional chaining (existing?.records) and treatnullas the legacy-path fallback, so this is behaviour-compatible.safeJsonParsefromsrc/utils/safe-json.ts(introduced by #548) is reused for the compat-reader path;execCommand/readSnapshotuse direct try/catch because they need to surface a typed error, not a fallback.Tests added
src/runtime/__tests__/bridge.test.ts(+165 lines, 14 new assertions across 3 suites):RuntimeBridgeError:instanceof,.name, context fields (command,stdoutPreview,cause), default empty contextRuntimeBridge.readCompatFile (parse guard): missingstateDir, missing file, truncated{, empty content, well-formed JSONRuntimeBridge.execCommand / readSnapshot (parse guard): non-JSON stdout → typed error withcommand='AcquireAuthority'+cause instanceof SyntaxError, well-formedRuntimeEvent→ parses normally,readSnapshotnon-JSON → typed error withcommand='snapshot'The exec/snapshot tests use a temp shell script as the runtime binary, branching on
$1so thevalidateSchemaOnceprobe always sees a valid manifest (theschemaValidatedmodule singleton would otherwise make test ordering significant).Verification
npm run buildnpm run lint(553 files)npm run check:no-unusednode --test dist/runtime/__tests__/bridge.test.js— 14/14 passnpm test— pre-existing failures observed are allgpt-5.5frontier-default fixture mismatches from the recent 0.14.4 cut (e.g.dist/team/__tests__/runtime.test.js:312expectsgpt-5.5, getsgpt-5.4); none are inruntime/bridge.tsand the bridge test suites all pass.Backward compatibility
RuntimeBridgeError extends Error. Existingtry { … } catch (err) { … }blocks at any call site still catch identically; onlyinstanceofadopters see new typing.execCommand/readSnapshotpreviously threw a vanillaSyntaxErroron this input; now throwRuntimeBridgeError. Both production exec call sites (team/state/dispatch.ts,team/state/mailbox.ts) ignore the return value and the throw propagates the same way.readCompatFilepreviously threw on parse failure; now returnsnull. Both callers already handlednull, so this strictly reduces the failure surface.Test plan
hud/state.ts,team/state.ts) toleratenullreturnsexecFileSync(POSIX shell fixture for binary mock; same OS scope as existing bridge tests)🤖 Generated with Claude Code