Skip to content

Commit 7e58341

Browse files
fix(server): answer every plugin result with a message the server can read
A handler result with no JSON form (a function, a symbol, or a toJSON that returns undefined) was sent as a Succeeded reply without its value, and a result whose serialization threw a long message overran the 2000-character Failed limit once prefixed. The server could not decode either line and killed the child as malformed, counting it against the restart budget. Both now come back as an ordinary failed call. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c63ec5c commit 7e58341

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎apps/server/src/plugins/pluginHostChild.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const loadEntry = (entryPath: string): Partial<PluginModule> =>
2424
const isAsyncModuleError = (error: unknown) =>
2525
error instanceof Error && "code" in error && error.code === "ERR_REQUIRE_ASYNC_MODULE";
2626

27-
const errorMessage = (error: unknown) =>
28-
(error instanceof Error ? error.message : String(error)).slice(0, 2000);
27+
// The server refuses a longer `Failed.message`, and a line it cannot decode kills the child.
28+
const errorMessage = (error: unknown, prefix = "") =>
29+
`${prefix}${error instanceof Error ? error.message : String(error)}`.slice(0, 2000);
2930

3031
/** Serves one plugin over fd 3 until the server deactivates it or goes away. */
3132
export const runPluginHostChild = (): void => {
@@ -59,13 +60,19 @@ export const runPluginHostChild = (): void => {
5960
send({ _tag: "Failed", requestId, message: errorMessage(outcome) });
6061
return;
6162
}
62-
let line: string;
63+
let value: string | undefined;
6364
try {
64-
line = JSON.stringify({ _tag: "Succeeded", requestId, value: outcome ?? null });
65+
value = JSON.stringify(outcome ?? null);
6566
} catch (error) {
66-
send({ _tag: "Failed", requestId, message: `Result is not JSON: ${errorMessage(error)}` });
67+
send({ _tag: "Failed", requestId, message: errorMessage(error, "Result is not JSON: ") });
6768
return;
6869
}
70+
// A function, symbol or `toJSON` returning undefined has no JSON form at all.
71+
if (value === undefined) {
72+
send({ _tag: "Failed", requestId, message: "Result is not JSON." });
73+
return;
74+
}
75+
const line = `{"_tag":"Succeeded","requestId":${requestId},"value":${value}}`;
6976
if (Buffer.byteLength(line) > maxBytes) {
7077
send({ _tag: "Failed", requestId, message: `Result exceeds ${maxBytes} bytes.` });
7178
return;

0 commit comments

Comments
 (0)