feat(codemode): cross Set, RegExp, and URLSearchParams to the host in a useful form (#49065)

This commit is contained in:
Aiden Cline
2026-09-14 17:32:42 -05:00
committed by GitHub
Unverified
parent a54f2c12a8
commit 00a2fc97f7
5 changed files with 58 additions and 12 deletions
+6 -5
View File
@@ -394,8 +394,8 @@ describe("RegExp", () => {
})
})
test("regexes serialize to {} at the boundary, like JSON", async () => {
expect(await value(`return /a/`)).toEqual({})
test("regexes cross the boundary as their literal form; JSON.stringify keeps {} like JS", async () => {
expect(await value(`return [/a/, { r: /b/gi }]`)).toEqual(["/a/", { r: "/b/gi" }])
expect(await value(`return JSON.stringify({ r: /a/g })`)).toBe('{"r":{}}')
})
@@ -521,7 +521,7 @@ describe("URL and URI helpers", () => {
cannotParse: false,
parsed: "https://example.test/users",
invalidIsTypeError: true,
boundary: ["https://example.test/a", {}],
boundary: ["https://example.test/a", "q=one"],
json: '{"url":"https://example.test/a","params":{}}',
})
})
@@ -715,8 +715,9 @@ describe("Set", () => {
).toBe(6)
})
test("sets serialize to {} at the boundary, like JSON", async () => {
expect(await value(`return { s: new Set([1]) }`)).toEqual({ s: {} })
test("sets cross the boundary as arrays; JSON.stringify keeps {} like JS", async () => {
expect(await value(`return { s: new Set([1, "a", { n: 1 }, undefined]) }`)).toEqual({ s: [1, "a", { n: 1 }, null] })
expect(await value(`return JSON.stringify(new Set([1]))`)).toBe("{}")
})
})
+25
View File
@@ -312,3 +312,28 @@ describe("tool argument prototype safety", () => {
expect(await value(runtime, `return [{ __proto__: 1 }]`)).toEqual([{}])
})
})
describe("tool arguments cross in a useful form where JSON.stringify would give {}", () => {
test("Set, RegExp, and URLSearchParams; Map stays {} like JSON", async () => {
let seen: unknown
const runtime = CodeMode.make({
tools: {
inspect: Tool.make({
description: "Inspect",
input: Schema.Struct({ v: Schema.Unknown }),
output: Schema.Unknown,
execute: (input) =>
Effect.sync(() => {
seen = input.v
return null
}),
}),
},
})
await value(
runtime,
`return await tools.inspect({ v: { s: new Set([1, 2]), r: /x/g, p: new URLSearchParams("a=1&b=2"), m: new Map([["k", 1]]) } })`,
)
expect(seen).toEqual({ s: [1, 2], r: "/x/g", p: "a=1&b=2", m: {} })
})
})