|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +provider = Path("packages/acp/src/provider.ts") |
| 4 | +test = Path("packages/test-agent/tests/session-configuration.test.ts") |
| 5 | + |
| 6 | +def replace_once(path, old, new): |
| 7 | + source = path.read_text() |
| 8 | + if source.count(old) != 1: |
| 9 | + raise ValueError(f"{path}: unexpected source for {old!r}") |
| 10 | + path.write_text(source.replace(old, new)) |
| 11 | + |
| 12 | +replace_once( |
| 13 | + provider, |
| 14 | + "export function* useAcpxProvider(", |
| 15 | + "let timingRuntimeCount = 0;\n\nexport function* useAcpxProvider(", |
| 16 | +) |
| 17 | +replace_once( |
| 18 | + provider, |
| 19 | + """ const dir = yield* agentCwd(); |
| 20 | + const options: AcpRuntimeOptions = {""", |
| 21 | + """ const dir = yield* agentCwd(); |
| 22 | + const runtime = ++timingRuntimeCount; |
| 23 | + const launches = new Map<string, number>(); |
| 24 | + const sessions = new Map<string, number>(); |
| 25 | + const active = new Set<number>(); |
| 26 | + const requests = new Map<number | string, string>(); |
| 27 | + function trace(event: Record<string, string | number | null | number[]>) { |
| 28 | + console.log("XMD_TIMING " + JSON.stringify({ ms: performance.now(), runtime, ...event })); |
| 29 | + } |
| 30 | + const options: AcpRuntimeOptions = {""", |
| 31 | +) |
| 32 | +replace_once( |
| 33 | + provider, |
| 34 | + """ agentRegistry: registry, |
| 35 | + permissionMode: providerOptions.permissionMode, |
| 36 | + nonInteractivePermissions: "deny", |
| 37 | + }; |
| 38 | + if (agentName !== undefined""", |
| 39 | + """ agentRegistry: registry, |
| 40 | + permissionMode: providerOptions.permissionMode, |
| 41 | + nonInteractivePermissions: "deny", |
| 42 | + processLifecycle: { |
| 43 | + onBeforeSpawn({ launchId, scope }) { |
| 44 | + const launch = launches.size + 1; |
| 45 | + launches.set(launchId, launch); |
| 46 | + if (scope.kind === "runtime-session" && !sessions.has(scope.sessionKey)) { |
| 47 | + sessions.set(scope.sessionKey, sessions.size + 1); |
| 48 | + } |
| 49 | + trace({ event: "before-spawn", launch, scope: scope.kind, |
| 50 | + session: scope.kind === "runtime-session" ? sessions.get(scope.sessionKey) ?? null : null }); |
| 51 | + }, |
| 52 | + onSpawned({ launchId, scope }) { |
| 53 | + const launch = launches.get(launchId); |
| 54 | + if (launch !== undefined) { |
| 55 | + if (scope.kind === "runtime-session") { |
| 56 | + active.add(launch); |
| 57 | + } |
| 58 | + trace({ event: "spawned", launch }); |
| 59 | + } |
| 60 | + }, |
| 61 | + onSpawnFailed({ launchId }) { |
| 62 | + trace({ event: "spawn-failed", launch: launches.get(launchId) ?? null }); |
| 63 | + }, |
| 64 | + onExit({ launchId, exitCode, signal }) { |
| 65 | + const launch = launches.get(launchId); |
| 66 | + if (launch !== undefined) { |
| 67 | + active.delete(launch); |
| 68 | + trace({ event: "exit", launch, exitCode, signal }); |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + onAcpMessage(direction, message) { |
| 73 | + if (typeof message !== "object" || message === null || |
| 74 | + !("id" in message) || (typeof message.id !== "string" && typeof message.id !== "number")) { |
| 75 | + return; |
| 76 | + } |
| 77 | + if (direction === "outbound" && "method" in message && typeof message.method === "string") { |
| 78 | + requests.set(message.id, message.method); |
| 79 | + trace({ event: "request", method: message.method, launches: [...active] }); |
| 80 | + } else if (direction === "inbound" && !("method" in message)) { |
| 81 | + const method = requests.get(message.id); |
| 82 | + if (method !== undefined) { |
| 83 | + requests.delete(message.id); |
| 84 | + trace({ event: "response", method, launches: [...active] }); |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + }; |
| 89 | + if (agentName !== undefined""", |
| 90 | +) |
| 91 | +replace_once( |
| 92 | + test, |
| 93 | + """ const origin = performance.now(); |
| 94 | + yield* NativeSessionObserver.set((report) => |
| 95 | + sessions.push({ ...report, elapsedMs: performance.now() - origin }), |
| 96 | + );""", |
| 97 | + """ const origin = performance.now(); |
| 98 | + const scenarios = new Map<string, number>(); |
| 99 | + console.log("XMD_TIMING " + JSON.stringify({ |
| 100 | + event: "run-start", ms: origin, fixture: path.basename(doc), |
| 101 | + })); |
| 102 | + yield* NativeSessionObserver.set((report) => { |
| 103 | + if (!scenarios.has(report.scenarioId)) { |
| 104 | + scenarios.set(report.scenarioId, scenarios.size + 1); |
| 105 | + } |
| 106 | + const ms = performance.now(); |
| 107 | + sessions.push({ ...report, elapsedMs: ms - origin }); |
| 108 | + console.log("XMD_TIMING " + JSON.stringify({ |
| 109 | + event: "receipt", ms, scenario: scenarios.get(report.scenarioId), |
| 110 | + })); |
| 111 | + });""", |
| 112 | +) |
| 113 | +replace_once( |
| 114 | + test, |
| 115 | + """ const results = yield* testing.results; |
| 116 | + return { result, output: next.value, results, sessions };""", |
| 117 | + """ const results = yield* testing.results; |
| 118 | + console.log("XMD_TIMING " + JSON.stringify({ |
| 119 | + event: "run-result", ms: performance.now(), fixture: path.basename(doc), |
| 120 | + results: results.map(({ name, status, error }) => ({ name, status, error: error?.kind })), |
| 121 | + })); |
| 122 | + return { result, output: next.value, results, sessions };""", |
| 123 | +) |
0 commit comments