Skip to content

fix(runtime): guard RuntimeBridge JSON.parse against partial output - #1918

Merged
Yeachan-Heo merged 1 commit into
Yeachan-Heo:devfrom
lifrary:fix/runtime-bridge-json-parse-guard
Apr 25, 2026
Merged

Yeachan-Heo merged 1 commit into
Yeachan-Heo:devfrom
lifrary:fix/runtime-bridge-json-parse-guard

Conversation

@lifrary

@lifrary lifrary commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Summary

RuntimeBridge.execCommand, readSnapshot, and readCompatFile each call JSON.parse with no try/catch. A partial Rust write (mid-rename, kill -9, schema drift, panic-before-flush) surfaces as a SyntaxError that 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. RuntimeBridge was not in #548's 16-file scope but exhibits the same hazard.

Reproducer

OMX_RUNTIME_BRIDGE=1 omx team run alpha
# In another shell, while the dispatcher is writing:
printf '{' > .omx/state/team/alpha/.omx-runtime/snapshot.json
omx hud

Before: SyntaxError: Unexpected end of JSON input aborts the entire buildAllState query at src/hud/state.ts:458.
After: HUD falls through to JS-inferred state for this tick.

Fix

Method Behaviour on parse failure
execCommand Throws typed RuntimeBridgeError so dispatch callers (src/team/state/dispatch.ts:169, src/team/state/mailbox.ts:44) can recognise parse failure with instanceof and mark the command failed without tearing down the surrounding watcher.
readSnapshot Same typed error — preserves the failing command name plus a 200-byte stdoutPreview and the underlying cause.
readCompatFile Returns null. The two production callers (src/hud/state.ts:454-458, src/team/state.ts:1488) already initialise to null (runtimeSnapshot) or use optional chaining (existing?.records) and treat null as the legacy-path fallback, so this is behaviour-compatible.

safeJsonParse from src/utils/safe-json.ts (introduced by #548) is reused for the compat-reader path; execCommand / readSnapshot use 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 context
  • RuntimeBridge.readCompatFile (parse guard): missing stateDir, missing file, truncated {, empty content, well-formed JSON
  • RuntimeBridge.execCommand / readSnapshot (parse guard): non-JSON stdout → typed error with command='AcquireAuthority' + cause instanceof SyntaxError, well-formed RuntimeEvent → parses normally, readSnapshot non-JSON → typed error with command='snapshot'

The exec/snapshot tests use a temp shell script as the runtime binary, branching on $1 so the validateSchemaOnce probe always sees a valid manifest (the schemaValidated module singleton would otherwise make test ordering significant).

Verification

  • npm run build
  • npm run lint (553 files)
  • npm run check:no-unused
  • node --test dist/runtime/__tests__/bridge.test.js — 14/14 pass
  • npm test — pre-existing failures observed are all gpt-5.5 frontier-default fixture mismatches from the recent 0.14.4 cut (e.g. dist/team/__tests__/runtime.test.js:312 expects gpt-5.5, gets gpt-5.4); none are in runtime/bridge.ts and the bridge test suites all pass.

Backward compatibility

  • RuntimeBridgeError extends Error. Existing try { … } catch (err) { … } blocks at any call site still catch identically; only instanceof adopters see new typing.
  • execCommand / readSnapshot previously threw a vanilla SyntaxError on this input; now throw RuntimeBridgeError. Both production exec call sites (team/state/dispatch.ts, team/state/mailbox.ts) ignore the return value and the throw propagates the same way.
  • readCompatFile previously threw on parse failure; now returns null. Both callers already handled null, so this strictly reduces the failure surface.

Test plan

  • Unit tests cover all three guarded paths plus the typed-error contract
  • Verified callers (hud/state.ts, team/state.ts) tolerate null returns
  • No platform-specific behaviour beyond the existing execFileSync (POSIX shell fixture for binary mock; same OS scope as existing bridge tests)

🤖 Generated with Claude Code

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]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/runtime/bridge.ts
Comment on lines +236 to +239
} catch {
return null;
}
return safeJsonParse<T | null>(content, null);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 Yeachan-Heo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) 🦞]

@Yeachan-Heo
Yeachan-Heo merged commit d80ad98 into Yeachan-Heo:dev Apr 25, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants