Skip to content

Commit ee2e15f

Browse files
fix(cli): escape human output while preserving structured exports (#6459)
Carry luvs01#697 onto current dev, preserving its implementation and regression coverage. Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 2e3acab commit ee2e15f

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎src/cli/export-command.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ export async function handleExportCommand(argv: string[], deps: ExportCommandDep
194194
// `--out` is the path that writes the selected client's native format.
195195
// Format metadata rides in the human lines below.
196196
printData(clientConfig, wantsJson, [
197-
text.trimEnd(),
197+
// `lines` entries print one console line each and are control-escaped, so
198+
// the document goes in as individual lines rather than one multi-line blob.
199+
...text.trimEnd().split("\n"),
198200
"",
199201
...(out !== undefined ? [`Wrote ${out}`] : []),
200202
`Destination: ${spec.destination(process.env)}`,

‎src/cli/runtime-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export async function readSecretBytes(
471471

472472
export function printData(value: unknown, wantsJson: boolean, lines?: string[]): void {
473473
if (wantsJson || !lines) console.log(JSON.stringify(value, null, 2));
474-
else for (const line of lines) console.log(line);
474+
else for (const line of lines) console.log(terminalSafeText(line));
475475
}
476476

477477
/**

‎structure/runtime.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ this wire projection does not change the usage ledger.
5353

5454
## CLI readiness diagnostics
5555

56-
Catalog-derived reasoning-level diagnostics are escaped only at the human-output boundary, which `src/cli/runtime-api.ts` owns alongside the human/JSON print split. Every CLI path that prints a hub-supplied catalog value renders it there: the first-time refusal in `src/cli/connect.ts` and the connected `ocx sync` refusal in `src/cli/dispatch.ts`. C0/C1 controls, DEL, and Unicode line/paragraph separators print as visible hexadecimal escapes; structured status retains the exact reason, and a rendered failure keeps the domain error as its `cause`. The ready/unverified/incompatible classification and exit policy are unchanged. `src/cli/capabilities-command.ts` rejects leftover positional arguments, unknown or repeated flags, and blank route filters with exit 64 before emitting a capability index; a valid unmatched route remains exit 4.
56+
Catalog-derived reasoning-level diagnostics are escaped only at the human-output boundary, which `src/cli/runtime-api.ts` owns alongside the human/JSON print split. Every CLI path that prints a hub-supplied catalog value renders it there: the first-time refusal in `src/cli/connect.ts` and the connected `ocx sync` refusal in `src/cli/dispatch.ts`. C0/C1 controls, DEL, and Unicode line/paragraph separators print as visible hexadecimal escapes; structured status retains the exact reason, and a rendered failure keeps the domain error as its `cause`. The ready/unverified/incompatible classification and exit policy are unchanged. `src/cli/capabilities-command.ts` rejects leftover positional arguments, unknown or repeated flags, and blank route filters with exit 64 before emitting a capability index; a valid unmatched route remains exit 4. `printData` in `src/cli/runtime-api.ts` escapes each human-output line at the shared renderer, including role and delegation-model suggestion rationale. JSON output retains the exact original values. `src/cli/export-command.ts` passes native serialized documents as individual lines so their multiline layout is preserved. `tests/cli/cli-headless-parity.test.ts` checks both suggestion paths independently from their parsed JSON output; `tests/cli/cli-export-command.test.ts` covers export framing.
5757

5858
## CLI resolve and stop contracts for embedding shells
5959

‎tests/cli/cli-export-command.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ describe("ocx export human output (accept criterion 2)", () => {
249249

250250
expect(result.code).toBe(0);
251251
expect(result.stdout.startsWith("{\n")).toBe(true);
252+
logs.length = 0;
253+
const structured = await run(["--client", "opencode", "--json"], { baseUrl: proxy.baseUrl });
254+
expect(structured.code).toBe(0);
255+
expect(JSON.parse(result.stdout.split("\n\n")[0]!)).toEqual(JSON.parse(structured.stdout));
252256
expect(result.stdout).toContain(join("opencode", "opencode.json"));
253257
expect(result.stdout).toContain("Merge this generated configuration into that file; do not replace it.");
254258
expect(result.stdout).toContain("export OPENCODEX_OPENCODE_API_KEY=");

‎tests/cli/cli-headless-parity.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,19 @@ describe("headless GUI parity CLI", () => {
10001000
sizingModel: "gpt-5.5",
10011001
proposals: [
10021002
{ role: "explorer", model: "gpt-5.5", status: "proposed", tier: "fast", effortIntent: "glance", proposedModel: "a/small", proposedEffort: "low" },
1003-
{ role: "worker", model: null, status: "proposed", tier: "standard", effortIntent: "measured", proposedModel: "a/mid", proposedEffort: null },
1003+
{ role: "worker", model: null, status: "proposed", tier: "standard", effortIntent: "measured", rationale: "Use \x1b]52;c;cG9pc29uZWQ=\x07 carefully.", proposedModel: "a/mid", proposedEffort: null },
10041004
{ role: "vague", model: null, status: "unsized", reason: "no JSON" },
10051005
],
10061006
};
10071007
const runtime = fakeRuntime(req => req.method === "POST" ? proposals : { ok: true });
10081008
const logSpy = spyOn(console, "log").mockImplementation(() => {});
1009+
let output = "";
10091010
try {
10101011
expect(await handleAgentCommand(["roles", "suggest", "--model", "a/sizer", "--json"], runtime.deps)).toBe(0);
1012+
expect(JSON.parse(logSpy.mock.calls.flat().join("\n"))).toEqual(proposals);
1013+
logSpy.mockClear();
10111014
expect(await handleAgentCommand(["roles", "suggest", "--apply"], runtime.deps)).toBe(0);
1015+
output = logSpy.mock.calls.flat().join("\n");
10121016
} finally {
10131017
logSpy.mockRestore();
10141018
}
@@ -1018,6 +1022,8 @@ describe("headless GUI parity CLI", () => {
10181022
{ path: "/api/codex-agent-roles/explorer", method: "PUT", body: { model: "a/small", effort: "low" } },
10191023
{ path: "/api/codex-agent-roles/worker", method: "PUT", body: { model: "a/mid" } },
10201024
]);
1025+
expect(output).not.toMatch(/[\x07\x1b]/);
1026+
expect(output).toContain("Use \\x1b]52;c;cG9pc29uZWQ=\\x07 carefully.");
10211027
});
10221028

10231029
test("agent roles suggest --apply skips proposals that already match the role's pin and says so", async () => {
@@ -1102,13 +1108,20 @@ describe("headless GUI parity CLI", () => {
11021108
test("agent injection suggest prints the proposal, and --apply writes it through PUT /api/injection-model", async () => {
11031109
const suggestion = {
11041110
sizingModel: "gpt-5.5",
1105-
proposal: { model: "a/big", effort: "high", status: "proposed", tier: "fast", effortIntent: "glance", rationale: "Bounded edits.", moveUpIf: "It crosses modules.", moveDownIf: "Never.", proposedModel: "a/small", proposedEffort: "low", reason: null },
1111+
proposal: { model: "a/big", effort: "high", status: "proposed", tier: "fast", effortIntent: "glance", rationale: "Bounded\x1b]52;c;payload\x07 edits.", moveUpIf: "It crosses\rmodules.", moveDownIf: "Never\u009b31m.", proposedModel: "a/small", proposedEffort: "low", reason: null },
11061112
};
11071113
const runtime = fakeRuntime(req => req.method === "POST" ? suggestion : { ok: true });
11081114
const logSpy = spyOn(console, "log").mockImplementation(() => {});
11091115
try {
11101116
expect(await handleAgentCommand(["injection", "suggest", "rename", "symbols", "--model", "a/sizer", "--json"], runtime.deps)).toBe(0);
1117+
expect(JSON.parse(logSpy.mock.calls.flat().join("\n"))).toEqual(suggestion);
1118+
logSpy.mockClear();
11111119
expect(await handleAgentCommand(["injection", "suggest", "rename symbols", "--apply"], runtime.deps)).toBe(0);
1120+
const output = logSpy.mock.calls.flat().join("\n");
1121+
expect(output).not.toMatch(/[\x07\x1b\r\u009b]/);
1122+
expect(output).toContain("Bounded\\x1b]52;c;payload\\x07 edits.");
1123+
expect(output).toContain("It crosses\\x0dmodules.");
1124+
expect(output).toContain("Never\\u009b31m.");
11121125
expect(await handleAgentCommand(["injection", "suggest"], runtime.deps)).not.toBe(0);
11131126
} finally {
11141127
logSpy.mockRestore();

0 commit comments

Comments
 (0)