fix(server): strip terminal escape sequences from opencode CLI output - #7988
fix(server): strip terminal escape sequences from opencode CLI output#7988Exotic209093 wants to merge 1 commit into
Conversation
ApprovabilityVerdict: Skipped Macroscope did not run approvability analysis for this PR. Macroscope could not determine whether this PR modifies its approvability configuration, so the PR was not approved automatically. A PR that may change the rules that govern approval is never approved automatically. |
opencode <= 1.18 emits an OSC title set (ESC ]0;<cwd>: ready BEL) on stdout even when piped, which leaked into the server-ready line match, model slugs, agent names, and skill payloads parsed from its output. Add a shared stripTerminalEscapes helper that removes OSC (BEL- and ST-terminated) and CSI sequences while leaving ordinary text untouched, apply it to parseServerUrlFromOutput and the models/agents/skills CLI parsers, and cover both the helper and the parsers with tests. Escapes embedded inside JSON string values only surface after JSON decoding, so all three parsers also strip decoded strings via a JSON.parse reviver; unterminated escape prefixes stay literal text.
e6bdd7d to
842a0dd
Compare
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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 |
Problem
opencode <= 1.18 emits an OSC title set (
ESC ]0;<cwd>: ready BEL) on stdout even when stdout is a pipe. The escape bytes leak into everything parsed from its CLI output:parseServerUrlFromOutputmisses the ready line, model slugs gain a prefix, agent names pick up garbage, and skill JSON fails to decode.Fix
Add a shared helper,
@t3tools/shared/stripTerminalEscapes, that removes OSC sequences (terminated by BEL or ST) and CSI sequences while leaving ordinary text alone (unterminated prefixes are preserved). Apply it inopencodeRuntime.tsbefore parsing server output, models, agents, and skills. SinceJSON.stringifyencodes control bytes textually inside JSON values, all three CLI parsers also strip decoded string values via aJSON.parsereviver — model fields at any depth, agent permission rules, and skill metadata — while non-string values and object/array structure pass through untouched.Tests: direct unit tests for the helper plus parser coverage for raw-byte titles, escapes embedded in decoded model/permission/skill fields, and unterminated-sequence preservation.
Note
Strip terminal escape sequences from opencode CLI output parsers
stripTerminalEscapesto@t3tools/shared, which removes OSC and CSI sequences from strings, exported via the new./stripTerminalEscapessubpathstripTerminalEscapesJsonRevivertoparseServerUrlFromOutput,parseModelsCliOutput,parseAgentListCliOutput, andparseSkillsCliOutputso CLI output is cleaned before line splitting and JSON decodingparseSkillsCliOutputnow usesJSON.parsewith the reviver before schema validation (decodeOpenCodeSkillsArrayExit) instead of decoding from JSON text, returning an empty array on parse failureparseSkillsCliOutputno longer validates raw JSON text via schema; malformed input returns[]instead of throwing, so downstream callers expecting schema errors on bad input will see an empty arrayMacroscope summarized 842a0dd.