Skip to content

Commit c213963

Browse files
committed
fix: format acpx doctor details safely
1 parent 757aee4 commit c213963

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎extensions/acpx/src/service.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,32 @@ describe("createAcpxRuntimeService", () => {
276276
await service.stop?.(ctx);
277277
});
278278

279+
it("formats non-string doctor details without losing object payloads", async () => {
280+
const workspaceDir = await makeTempDir();
281+
const ctx = createServiceContext(workspaceDir);
282+
const runtime = createMockRuntime({
283+
doctor: async () => ({
284+
ok: false,
285+
message: "probe failed",
286+
details: [{ code: "ACP_CLOSED", agent: "codex" }, new Error("stdin closed")],
287+
}),
288+
isHealthy: () => false,
289+
});
290+
const service = createAcpxRuntimeService({
291+
runtimeFactory: () => runtime as never,
292+
});
293+
294+
await service.start(ctx);
295+
296+
await vi.waitFor(() => {
297+
expect(ctx.logger.warn).toHaveBeenCalledWith(
298+
'embedded acpx runtime backend probe failed: probe failed ({"code":"ACP_CLOSED","agent":"codex"}; stdin closed)',
299+
);
300+
});
301+
302+
await service.stop?.(ctx);
303+
});
304+
279305
it("can skip the embedded runtime backend via env", async () => {
280306
process.env.OPENCLAW_SKIP_ACPX_RUNTIME = "1";
281307
const workspaceDir = await makeTempDir();

‎extensions/acpx/src/service.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs/promises";
2+
import { inspect } from "node:util";
23
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
34
import type {
45
AcpRuntime,
@@ -79,8 +80,36 @@ function warnOnIgnoredLegacyCompatibilityConfig(params: {
7980
);
8081
}
8182

82-
function formatDoctorFailureMessage(report: { message: string; details?: string[] }): string {
83-
const detailText = report.details?.filter(Boolean).join("; ").trim();
83+
function formatDoctorDetail(detail: unknown): string | null {
84+
if (!detail) {
85+
return null;
86+
}
87+
if (typeof detail === "string") {
88+
return detail.trim() || null;
89+
}
90+
if (detail instanceof Error) {
91+
return formatErrorMessage(detail);
92+
}
93+
if (typeof detail === "object") {
94+
try {
95+
return JSON.stringify(detail) ?? inspect(detail, { breakLength: Infinity, depth: 3 });
96+
} catch {
97+
return inspect(detail, { breakLength: Infinity, depth: 3 });
98+
}
99+
}
100+
if (
101+
typeof detail === "number" ||
102+
typeof detail === "boolean" ||
103+
typeof detail === "bigint" ||
104+
typeof detail === "symbol"
105+
) {
106+
return detail.toString();
107+
}
108+
return inspect(detail, { breakLength: Infinity, depth: 3 });
109+
}
110+
111+
function formatDoctorFailureMessage(report: { message: string; details?: unknown[] }): string {
112+
const detailText = report.details?.map(formatDoctorDetail).filter(Boolean).join("; ").trim();
84113
return detailText ? `${report.message} (${detailText})` : report.message;
85114
}
86115

0 commit comments

Comments
 (0)