diff --git a/scripts/runtime-subagent-execution.ts b/scripts/runtime-subagent-execution.ts index e96c9eac..f0ffa270 100644 --- a/scripts/runtime-subagent-execution.ts +++ b/scripts/runtime-subagent-execution.ts @@ -25,6 +25,11 @@ function ownExecution(method: string, resume: boolean): string { return wrapped.slice(0, -1) + "})}"; } +export function patchSubagentPromiseTracking(port: string): string { + return replaceOnce(port, /(\([\w$]+,[\w$]+=!1(?:,[\w$]+)?\)=>(?:\(|\{if\())([\w$]+\.add\(([\w$]+)\))/u, + (_match, start, add, work) => `${start}${helper}.trackSubagentExecution(${work}),${add}`); +} + export function patchSubagentExecution(runtime: string): string { if (runtime.includes(".withSubagentExecution(")) { if (runtime.split(".withSubagentExecution(").length !== 4 || !runtime.includes(".trackSubagentExecution(") @@ -34,8 +39,7 @@ export function patchSubagentExecution(runtime: string): string { return runtime; } runtime = patchFunction(runtime, "createExploreSubagentPort", port => { - port = replaceOnce(port, /(\([\w$]+,[\w$]+=!1\)=>\()([\w$]+\.add\(([\w$]+)\))/u, - (_match, start, add, work) => `${start}${helper}.trackSubagentExecution(${work}),${add}`); + port = patchSubagentPromiseTracking(port); for (const [name, next] of [["run", "start"], ["start", "getTask"]]) { const begin = port.indexOf(`,async ${name}(`); const end = port.indexOf(`,async ${next}(`, begin + 1); diff --git a/test/runtime/subagent-execution-patch.test.ts b/test/runtime/subagent-execution-patch.test.ts new file mode 100644 index 00000000..f0a73207 --- /dev/null +++ b/test/runtime/subagent-execution-patch.test.ts @@ -0,0 +1,21 @@ +import { expect, test } from "bun:test"; +import { parse } from "acorn"; +import { patchSubagentPromiseTracking } from "../../scripts/runtime-subagent-execution.ts"; + +test("subagent execution tracking supports the expression-bodied runtime port", () => { + const patched = patchSubagentPromiseTracking( + "const track=(work,suppress=!1)=>(pending.add(work),work);" + ); + + expect(patched).toContain(".trackSubagentExecution(work),pending.add(work)"); + expect(() => parse(`function fixture(){${patched}}`, { ecmaVersion: "latest" })).not.toThrow(); +}); + +test("subagent execution tracking supports the block-bodied runtime port", () => { + const patched = patchSubagentPromiseTracking( + "const track=(work,suppress=!1,child)=>{if(pending.add(work),child!==void 0){}};" + ); + + expect(patched).toContain(".trackSubagentExecution(work),pending.add(work)"); + expect(() => parse(`function fixture(){${patched}}`, { ecmaVersion: "latest" })).not.toThrow(); +});