Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions scripts/runtime-subagent-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(")
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions test/runtime/subagent-execution-patch.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Loading