diff --git a/packages/agent-core/src/harness/types.test.ts b/packages/agent-core/src/harness/types.test.ts new file mode 100644 index 000000000000..e252c3d41ab3 --- /dev/null +++ b/packages/agent-core/src/harness/types.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "vitest"; +import { toError } from "./types.js"; + +describe("toError", () => { + it("normalizes hostile non-Error values without stringifying them", () => { + const hostile = { + toJSON() { + throw new Error("json denied"); + }, + toString() { + throw new Error("stringification denied"); + }, + }; + + const error = toError(hostile); + + expect(error).toBeInstanceOf(Error); + expect(error.message).toBe("Unknown thrown value"); + }); +}); diff --git a/packages/agent-core/src/harness/types.ts b/packages/agent-core/src/harness/types.ts index f74ecc3cc853..ec55f186936e 100644 --- a/packages/agent-core/src/harness/types.ts +++ b/packages/agent-core/src/harness/types.ts @@ -50,7 +50,7 @@ export function toError(error: unknown): Error { try { return new Error(JSON.stringify(error)); } catch { - return new Error(String(error)); + return new Error("Unknown thrown value"); } }