Skip to content

Commit d5fc7e6

Browse files
morlutoXander-AJ
andcommitted
fix(rizin): preserve output whitespace before completion markers
Adapt #4044 at the shared normal/interrupted frame owner. Preserve the decoded prefix before byte-budget accounting and cover MCP delivery across framing and UTF-8 boundaries. Refs #4016. Co-authored-by: Xander-AJ <[email protected]>
1 parent 279c590 commit d5fc7e6

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

‎src/rizin/RizinDebugSessionManager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ const waitForCommandFrames = async (
14371437
outputTruncated ||= frame.truncated;
14381438
const markerIndex = frame.text.indexOf(marker);
14391439
const text =
1440-
markerIndex < 0 ? frame.text : frame.text.slice(0, markerIndex).trimEnd();
1440+
markerIndex < 0 ? frame.text : frame.text.slice(0, markerIndex);
14411441
if (markerIndex >= 0 && text.length === 0) return true;
14421442
const separatorBytes = output.length > 0 ? 1 : 0;
14431443
const remaining = maxCommandOutputBytes - outputBytes - separatorBytes;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
2+
import { McpServer } from "@modelcontextprotocol/server";
3+
import { expect, it } from "vitest";
4+
5+
import { registerRizinDebugTools } from "../../../src/server/registerRizinDebugTools.js";
6+
import { createToolResultDelivery } from "../../../src/server/toolResult.js";
7+
import { toolContract } from "../../../src/contracts/toolContracts.js";
8+
import { silentLogger } from "../../../src/logger.js";
9+
import { createInjectedProcess } from "../providers/RizinDebugSessionCoverage.fixtures.js";
10+
import {
11+
createRizinTestManager,
12+
rizinTestInferiorTty,
13+
} from "../providers/RizinTestInferiorTty.js";
14+
15+
const test = it.skipIf(process.platform === "win32");
16+
17+
const observe = async (
18+
prefix: string,
19+
framing: "coalesced" | "separate" | "split-transport",
20+
maximum?: number,
21+
) => {
22+
const injected = createInjectedProcess({
23+
onWrite: (text, control) => {
24+
const marker = /!echo (.+)\n/u.exec(text)?.[1];
25+
if (marker === undefined) return;
26+
setImmediate(() => {
27+
if (framing === "separate") control.output(`${prefix}\0${marker}\n\0`);
28+
else if (framing === "split-transport") {
29+
const bytes = Buffer.from(`${prefix}${marker}\n\0`);
30+
const split = Math.max(1, Buffer.byteLength(prefix) - 1);
31+
control.output(bytes.subarray(0, split));
32+
control.output(bytes.subarray(split));
33+
} else control.output(`${prefix}${marker}\n\0`);
34+
});
35+
},
36+
});
37+
const manager = createRizinTestManager(
38+
{},
39+
{
40+
spawnProcess: injected.spawnProcess,
41+
frameTimeoutMs: 100,
42+
...(maximum === undefined ? {} : { maxCommandOutputBytes: maximum }),
43+
},
44+
);
45+
const server = new McpServer({ name: "rizin-output-test", version: "1" });
46+
registerRizinDebugTools(
47+
server,
48+
manager,
49+
silentLogger,
50+
createToolResultDelivery(undefined),
51+
);
52+
const client = new Client({ name: "rizin-output-test", version: "1" });
53+
const [clientTransport, serverTransport] =
54+
InMemoryTransport.createLinkedPair();
55+
try {
56+
await server.connect(serverTransport);
57+
await client.connect(clientTransport);
58+
const startReply = await client.callTool({
59+
name: "start_rizin_debug_session",
60+
arguments: {
61+
path: "fixture.bin",
62+
inferior_tty: await rizinTestInferiorTty(),
63+
},
64+
});
65+
expect(startReply.isError).not.toBe(true);
66+
const started = toolContract(
67+
"start_rizin_debug_session",
68+
).outputSchema.parse(startReply.structuredContent);
69+
const reply = await client.callTool({
70+
name: "rizin_debug_command",
71+
arguments: { session_id: started.session_id, command: "dr" },
72+
});
73+
expect(reply.isError).not.toBe(true);
74+
return toolContract("rizin_debug_command").outputSchema.parse(
75+
reply.structuredContent,
76+
);
77+
} finally {
78+
injected.control.exit();
79+
await client.close();
80+
await server.close();
81+
await manager.closeAll();
82+
}
83+
};
84+
85+
test.each(
86+
["value \n", " \t\r\n", "Ω雪\t\r\n", "", "😀"].flatMap((prefix) =>
87+
(["coalesced", "separate", "split-transport"] as const).map((framing) => ({
88+
prefix,
89+
framing,
90+
})),
91+
),
92+
)(
93+
"preserves output %j across $framing framing",
94+
async ({ prefix, framing }) => {
95+
const evidence = await observe(prefix, framing);
96+
const expected = {
97+
output: prefix,
98+
completion_status: "complete",
99+
output_truncated: false,
100+
};
101+
expect(evidence.normalized_result).toMatchObject(expected);
102+
expect(evidence.raw_result).toMatchObject(expected);
103+
},
104+
);
105+
106+
test.each([7, 8])(
107+
"counts trailing whitespace in the exact %s-byte budget",
108+
async (maximum) => {
109+
const evidence = await observe("value \n", "coalesced", maximum);
110+
const truncated = maximum < 8;
111+
const expected = {
112+
output: truncated ? "value " : "value \n",
113+
output_truncated: truncated,
114+
completion_status: truncated ? "unknown" : "complete",
115+
};
116+
expect(evidence.normalized_result).toMatchObject(expected);
117+
expect(evidence.raw_result).toMatchObject(expected);
118+
},
119+
);

0 commit comments

Comments
 (0)