diff --git a/apps/hosted/self-host/src/mcp.ts b/apps/hosted/self-host/src/mcp.ts index dfd794b435..4834a1f215 100644 --- a/apps/hosted/self-host/src/mcp.ts +++ b/apps/hosted/self-host/src/mcp.ts @@ -46,6 +46,9 @@ export const selfHostMcp = Effect.gen(function* () { timeoutMs: yield* Config.Number("EXECUTOR_MCP_TIMEOUT_MS").pipe( Config.withDefault(defaultMcpLimits.timeoutMs), ), + maxSteps: yield* Config.Number("EXECUTOR_MCP_MAX_STEPS").pipe( + Config.withDefault(defaultMcpLimits.maxSteps), + ), }); const sessions = yield* RcMap.make({ // Each session keeps what its callers' last tool calls read (CallStateCache). diff --git a/apps/local/server/README.md b/apps/local/server/README.md index ff173b5ec6..a0cb8d6f41 100644 --- a/apps/local/server/README.md +++ b/apps/local/server/README.md @@ -55,22 +55,23 @@ are resolved from the process working directory. `bun run with:local …` loads ## Configuration -| Variable | Meaning | -| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `EXECUTOR_DATA_DIR` | Directory containing `executor.pglite/`, `browser-auth.pglite/`, `builds/` and the [service files](#background-service); defaults to `.local/executor` (`~/.executor/v2/cli` in releases). | -| `EXECUTOR_PORT` | Loopback listener port; wins over the background service's `port` setting, then defaults to `4312`. `0` chooses any free port. | -| `EXECUTOR_API_KEY` | Optional supplied bearer token; set with the encryption key. At least 32 characters. | -| `EXECUTOR_ENCRYPTION_KEY` | Optional supplied AES key; set with the API key. Exactly 64 hexadecimal characters. | -| `EXECUTOR_KEY_STORAGE` | Optional key storage for a new directory: `file` for `keys.json`, `os` for the OS credential store without the key file fallback. See [Key storage](#key-storage). | -| `EXECUTOR_MCP_TIMEOUT_MS` | Catalog discovery plus program timeout; defaults to `300000`. | -| `EXECUTOR_MCP_MAX_TOOL_CALLS` | Admitted calls per execute, including search; defaults to `100`. | -| `EXECUTOR_TOOL_LISTING_FRESH_SECONDS` | Reuse an app's evaluated tool list this long before refreshing it in the background; defaults to `30`. | -| `EXECUTOR_TOOL_LISTING_RENEW_SECONDS` | Renew the tool lists search serves in the background once they are this old; defaults to `86400`. | -| `EXECUTOR_TOOL_LISTING_MAX_AGE_SECONDS` | Never serve an evaluated tool list older than this; `0` evaluates every list. Defaults to `604800`. | -| `EXECUTOR_TOOL_LISTING_LOAD_SECONDS` | Stop evaluating an app's tool list after this long when no request is waiting for it; defaults to `45`. | -| `EXECUTOR_EVALUATION_MEMORY_MB` | Memory for kept tool lists and app declarations; defaults to `256`. | -| `EXECUTOR_APP_WORKERS` | Most app and data Workers kept loaded; defaults to `64`. Idle ones above it unload and reload on their next call. An app with a database runs its calls in a data Worker, which counts too. | -| `EXECUTOR_APP_WORKER_IDLE_SECONDS` | Unload an app or data Worker not called for this long, even below `EXECUTOR_APP_WORKERS`; `0` turns this off. Defaults to `300`. | +| Variable | Meaning | +| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `EXECUTOR_DATA_DIR` | Directory containing `executor.pglite/`, `browser-auth.pglite/`, `builds/` and the [service files](#background-service); defaults to `.local/executor` (`~/.executor/v2/cli` in releases). | +| `EXECUTOR_PORT` | Loopback listener port; wins over the background service's `port` setting, then defaults to `4312`. `0` chooses any free port. | +| `EXECUTOR_API_KEY` | Optional supplied bearer token; set with the encryption key. At least 32 characters. | +| `EXECUTOR_ENCRYPTION_KEY` | Optional supplied AES key; set with the API key. Exactly 64 hexadecimal characters. | +| `EXECUTOR_KEY_STORAGE` | Optional key storage for a new directory: `file` for `keys.json`, `os` for the OS credential store without the key file fallback. See [Key storage](#key-storage). | +| `EXECUTOR_MCP_TIMEOUT_MS` | Catalog discovery plus program timeout; defaults to `300000`. | +| `EXECUTOR_MCP_MAX_TOOL_CALLS` | Admitted calls per execute, including search; defaults to `100`. | +| `EXECUTOR_MCP_MAX_STEPS` | Interpreter steps one execute program may spend: statements and expressions, plus native calls, whole-value operators, storing values and string `+` in proportion to the data they read, copy or return, not counting time spent waiting on tools; past it the program fails with `TimeoutExceeded`. Defaults to `5000000`, which stops a comparator sort of about 100,000 objects (about 20 million steps). | +| `EXECUTOR_TOOL_LISTING_FRESH_SECONDS` | Reuse an app's evaluated tool list this long before refreshing it in the background; defaults to `30`. | +| `EXECUTOR_TOOL_LISTING_RENEW_SECONDS` | Renew the tool lists search serves in the background once they are this old; defaults to `86400`. | +| `EXECUTOR_TOOL_LISTING_MAX_AGE_SECONDS` | Never serve an evaluated tool list older than this; `0` evaluates every list. Defaults to `604800`. | +| `EXECUTOR_TOOL_LISTING_LOAD_SECONDS` | Stop evaluating an app's tool list after this long when no request is waiting for it; defaults to `45`. | +| `EXECUTOR_EVALUATION_MEMORY_MB` | Memory for kept tool lists and app declarations; defaults to `256`. | +| `EXECUTOR_APP_WORKERS` | Most app and data Workers kept loaded; defaults to `64`. Idle ones above it unload and reload on their next call. An app with a database runs its calls in a data Worker, which counts too. | +| `EXECUTOR_APP_WORKER_IDLE_SECONDS` | Unload an app or data Worker not called for this long, even below `EXECUTOR_APP_WORKERS`; `0` turns this off. Defaults to `300`. | ### Key storage diff --git a/apps/local/server/src/contracts/config.ts b/apps/local/server/src/contracts/config.ts index fdc6e0fc48..dfe03ba04a 100644 --- a/apps/local/server/src/contracts/config.ts +++ b/apps/local/server/src/contracts/config.ts @@ -64,6 +64,9 @@ export const config = Config.all({ maxToolCalls: Config.Number("EXECUTOR_MCP_MAX_TOOL_CALLS").pipe( Config.withDefault(defaultMcpLimits.maxToolCalls), ), + maxSteps: Config.Number("EXECUTOR_MCP_MAX_STEPS").pipe( + Config.withDefault(defaultMcpLimits.maxSteps), + ), }), }).pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.toType(ServerConfig)))); diff --git a/e2e/ci-selection.ts b/e2e/ci-selection.ts index d699480c41..41cf4beb2a 100644 --- a/e2e/ci-selection.ts +++ b/e2e/ci-selection.ts @@ -634,6 +634,7 @@ const patchGuards: Readonly>> = { "mcp-catalog.spec.ts", "mcp-execute-concurrency.spec.ts", "mcp-execute-lazy.spec.ts", + "mcp-execute-step-budget.spec.ts", "local-mcp-connections.spec.ts", ], effect: [ diff --git a/e2e/test-plan.ts b/e2e/test-plan.ts index 81cb0d2203..e6aa5b7a8c 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -1228,6 +1228,17 @@ export const scenarios = plan({ cloud: na("Hosted products run the same programs in mcpExecuteCallDepth."), }, }, + mcpExecuteStepBudget: { + fixtures: "actors", + file: "mcp-execute-step-budget.spec.ts", + title: + "Hosted MCP execute stops a program that only computes, in the interpreter, through native calls, operators or string joins, at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs, including grouping, shared values and `in` lookups, complete", + targets: { + "self-host": scheduled, + cloud: na("Shared execute path and CodeMode patch; covered on self-host"), + local: na("Shared execute path and CodeMode patch; covered on self-host"), + }, + }, mcpExecuteReach: { fixtures: "actors", file: "mcp-execute-reach.spec.ts", diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts new file mode 100644 index 0000000000..205d63cc79 --- /dev/null +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -0,0 +1,393 @@ +/** + * Execute runs programs in an interpreter on the product's own thread. A program that only computes + * used to run until its five-minute wall-time budget, keeping the product busy all that time: on + * production one program spent 203 s in the interpreter and other requests slowed until clients + * gave up. The step budget stops such a program with `TimeoutExceeded` long before its wall time, + * whatever its own `catch` does, while other requests keep answering and an ordinary program that + * calls tools and loops still completes. Native calls are charged for the data they read and + * return, so a loop of large `split` calls, one step each, stops in seconds too, and so are + * operators that walk or coerce a whole value. A value a call returns that the program already + * holds, such as a group `Map.get` returns, is not charged, so grouping many rows into few groups + * completes. Storing a value walks it once to reject cycles; that walk is charged, and a value + * holding no objects is not walked again, so attaching one shared value to every row completes. + * Storing the value a property already holds skips that walk, so grouping row objects with + * `g[k] = g[k] || []` completes. Joining strings with `+` or a template is charged by the length + * of the result, so a loop that joins a megabyte string stops in seconds. + */ +import { expect, layer } from "@effect/vitest"; +import { Duration, Effect, Fiber, Schema } from "effect"; +import { scenarios } from "../test-plan.ts"; +import { Actors } from "../support/actors.ts"; +import { Api, body } from "../support/api.ts"; +import { HostedLive, withHostedCase } from "../support/case.ts"; +import { Evidence } from "../support/evidence.ts"; +import { McpClient } from "../support/mcp-client.ts"; + +/** The products' default `maxSteps` (`defaultMcpLimits` in packages/mcp). */ +const defaultMaxSteps = 5_000_000; +/** The products' default wall-time budget, which the runaway program must end well inside. */ +const defaultTimeoutMs = 5 * 60_000; + +const Executed = Schema.Struct({ + status: Schema.Literal("completed"), + execution: Schema.Struct({ + ok: Schema.Boolean, + value: Schema.optional(Schema.Unknown), + error: Schema.optional(Schema.Struct({ kind: Schema.String, message: Schema.String })), + toolCalls: Schema.Array(Schema.Struct({ name: Schema.String, outcome: Schema.String })), + }), +}); + +type Connected = Effect.Success["connect"]>>; + +const execute = (client: Connected, name: string, code: string) => + Effect.gen(function* () { + const [took, { execution }] = yield* client + .use(`Run the ${name} program`, (client, signal) => + client.callTool({ name: "execute", arguments: { code } }, undefined, { signal }), + ) + .pipe( + Effect.flatMap((result) => Schema.decodeUnknownEffect(Executed)(result.structuredContent)), + Effect.timed, + ); + const ms = Math.round(Duration.toMillis(took)); + yield* (yield* Evidence).json(`${name}.json`, { ms, execution }); + return { ms, execution }; + }); + +/** Connects to hosted MCP execute with a new API key for the owner, deleted when the case ends. */ +const connectHosted = (label: string) => + Effect.gen(function* () { + const api = yield* Api, + actors = yield* Actors, + mcp = yield* McpClient; + const key = yield* body( + Schema.Struct({ id: Schema.String, key: Schema.RedactedFromValue(Schema.String) }), + yield* api.request(actors.owner, "POST", "/api/auth/api-key/create", { name: label }), + ); + yield* Effect.addFinalizer(() => + api + .request(actors.owner, "POST", "/api/auth/api-key/delete", { keyId: key.id }) + .pipe(Effect.orDie), + ); + return yield* mcp.connect(key.key, label, { organization: actors.organization.id }); + }); + +/** Calls a tool every product serves without setup, then loops briefly, as ordinary programs do. */ +const ordinary = ` +const found = await tools.search({ query: 'executor', limit: 1 }); +let sum = 0; +for (let i = 0; i < 1000; i++) sum += i; +return { items: Array.isArray(found.items), sum };`; +const ordinaryValue = { items: true, sum: 499_500 }; + +/** + * Never returns on its own, and tries to outlast the budget: each iteration catches what it + * throws and runs a finally block, and the program returns from its outer catch. + */ +const runaway = ` +let n = 0; +try { + while (true) { + try { n++; if (n % 1000 === 0) throw new Error('again'); } catch (e) { n++; } finally { n++; } + } +} catch (e) { + return 'caught: ' + String(e); +}`; + +/** + * The review's proof that native calls escaped the budget: each `split` is one step, but builds a + * million-element array. Uncharged, it ran 83 s and outlasted a 60 s wall-time budget. + */ +const nativeRunaway = ` +const s = "x".repeat(1000000); +let n = 0; +for (let i = 0; i < 100000; i++) { n += s.split("").length } +return n;`; + +/** + * The review's proof that operators escaped the budget: comparing a million-element array to a + * string coerces the whole array each time, in one step. Uncharged, it ran to the wall clock. + */ +const operatorRunaway = ` +const a = Array.from({ length: 1000000 }, (_, i) => i); +let n = 0; +for (let i = 0; i < 100000; i++) { if (a == "x") n++ } +return n;`; + +/** + * The review's realistic operator proof: merging 20,000 records into an index of 20,000 by checking + * `id in index`. `in` walked the whole index on every check and this ran to the 120 s wall + * clock; it reads one key now. + */ +const dedupeWithIn = ` +const a = Array.from({ length: 20000 }, (_, i) => ({ id: 'r' + i, name: 'n' + i, tags: ['x', 'y'] })); +const b = Array.from({ length: 20000 }, (_, i) => ({ id: 'r' + i * 2, name: 'm' + i, tags: ['z'] })); +const byId = {}; +for (const r of a) byId[r.id] = r; +let added = 0; +for (const r of b) { if (!(r.id in byId)) { byId[r.id] = r; added++ } } +return added;`; + +/** + * Groups 50,000 rows into a few groups with `Map.get`, which returns a group of thousands of rows + * every time. Charging that group as if copied stopped it in under a second. + */ +const groupBy = (groups: number) => ` +const rows = Array.from({ length: 50000 }, (_, i) => ({ k: 'k' + (i % ${groups}), v: i })); +const g = new Map(); +for (const r of rows) { const a = g.get(r.k) ?? []; a.push(r.v); g.set(r.k, a) } +return g.size;`; + +/** + * The same grouping into an object, reassigning each group after the push. Storing a group walked + * it to reject cycles, thousands of rows every time, uncharged; the walk now stops at groups + * already known to hold no objects. + */ +const groupByObject = ` +const rows = Array.from({ length: 50000 }, (_, i) => ({ k: 'k' + (i % 10), v: i })); +const g = {}; +for (const r of rows) { const a = g[r.k] || []; a.push(r.v); g[r.k] = a } +return Object.keys(g).length;`; + +/** + * Attaches one shared value of 50,000 columns to each of 20,000 rows. Each store walked all + * 50,000 columns, uncharged, and ran for minutes. + */ +const sharedMeta = ` +const meta = { columns: Array.from({ length: 50000 }, (_, i) => 'c' + i) }; +const rows = Array.from({ length: 20000 }, (_, i) => ({ id: i })); +for (const r of rows) { r.meta = meta } +return rows.length;`; + +/** + * The review's proof that string `+` escaped the budget: joining a megabyte string to a number is + * one step, and reading the result copies the megabyte. Uncharged, it ran to the wall clock. + */ +const concatRunaway = ` +const s = "x".repeat(1000000); +let n = 0; +for (let i = 0; i < 1000000; i++) { const t = s + i; n += t.charCodeAt(500000) } +return n;`; + +/** + * Groups 50,000 row objects into 10 groups by storing each group back into itself before the push. + * The store walked the whole group, every row in it, on every row, and stopped at the budget. + */ +const groupRowsOrPush = ` +const rows = Array.from({ length: 50000 }, (_, i) => ({ k: 'k' + (i % 10), v: i, name: 'n' + i })); +const g = {}; +for (const r of rows) { g[r.k] = g[r.k] || []; g[r.k].push(r) } +return Object.keys(g).length;`; + +/** The same grouping in the `reduce` form. */ +const groupRowsReduce = ` +const rows = Array.from({ length: 50000 }, (_, i) => ({ k: 'k' + (i % 10), v: i, name: 'n' + i })); +const g = rows.reduce((acc, r) => { (acc[r.k] = acc[r.k] || []).push(r); return acc }, {}); +return Object.keys(g).length;`; + +/** + * The review's proof that templates escaped the budget: interpolating a megabyte string is one + * step, and reading the result copies the megabyte. Uncharged, it ran to the wall clock. + */ +const templateRunaway = ` +const s = "x".repeat(1000000); +let n = 0; +for (let i = 0; i < 1000000; i++) { const t = \`\${s}\${i}\`; n += t.charCodeAt(500000) } +return n;`; + +/** Filters and maps 10,000 records, as programs do with a large tool result. */ +const recordsTransform = ` +const records = Array.from({ length: 10000 }, (_, i) => ({ id: 'r' + i, price: i % 100, tags: ['a', 'b'] })); +const cheap = records.filter((record) => record.price < 50).map((record) => ({ id: record.id, price: record.price * 2 })); +return { kept: cheap.length, json: JSON.stringify(cheap).length > 0 };`; + +/** + * Three tool calls with a 200,000-iteration loop between them: several times the interpreter work + * of the programs agents send, which spend well under a second before their first call. + */ +const heavyButFine = ` +const first = await tools.search({ query: 'executor', limit: 1 }); +let total = 0; +for (let i = 0; i < 200000; i++) total = (total + i) % 1000003; +const second = await tools.search({ query: 'github', limit: 1 }); +const third = await tools.search({ query: 'slack', limit: 1 }); +return { total, calls: [first, second, third].length };`; + +layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget", (it) => { + it.effect( + scenarios.mcpExecuteStepBudget.title, + (context) => + withHostedCase( + context, + Effect.gen(function* () { + const evidence = yield* Evidence; + const runner = yield* connectHosted("execute-step-budget-runaway"); + const other = yield* connectHosted("execute-step-budget-other"); + + // How long the ordinary program takes on this server when nothing else runs. + const quiet: Array = []; + for (let round = 0; round < 3; round++) { + const { ms, execution } = yield* execute(other, `ordinary-quiet-${round}`, ordinary); + expect(execution).toMatchObject({ ok: true, value: ordinaryValue }); + quiet.push(ms); + } + + // The runaway program runs while the other caller keeps sending ordinary programs. + const running = yield* Effect.forkChild(execute(runner, "runaway", runaway)); + const finished = Effect.sync(() => running.pollUnsafe() !== undefined); + const busy: Array = []; + while (!(yield* finished)) { + const { ms, execution } = yield* execute( + other, + `ordinary-busy-${busy.length}`, + ordinary, + ); + expect(execution).toMatchObject({ ok: true, value: ordinaryValue }); + // Only rounds that ended while the runaway program still ran count as beside it. + if (!(yield* finished)) busy.push(ms); + } + const stopped = yield* Fiber.join(running); + + // It fails as a timeout that names the budget, although its own catch tried to return. + expect(stopped.execution).toMatchObject({ + ok: false, + error: { + kind: "TimeoutExceeded", + message: expect.stringContaining( + `Execution exceeded its compute budget of ${defaultMaxSteps} interpreter steps`, + ), + }, + toolCalls: [], + }); + expect(stopped.execution.error?.message).toContain("move heavy computation"); + // Far inside the five-minute wall-time budget it would otherwise have run to. + expect(stopped.ms).toBeLessThan(defaultTimeoutMs / 5); + + // Other callers kept being answered while it ran, and promptly. + expect(busy.length).toBeGreaterThanOrEqual(3); + const slowestQuiet = Math.max(...quiet); + expect(Math.max(...busy)).toBeLessThan(Math.max(5_000, slowestQuiet * 20)); + + // An ordinary program that calls tools between longer loops still completes. + const fine = yield* execute(runner, "heavy-but-fine", heavyButFine); + expect(fine.execution).toMatchObject({ + ok: true, + value: { total: expect.any(Number), calls: 3 }, + }); + expect(fine.execution.toolCalls.map((call) => call.outcome)).toEqual([ + "success", + "success", + "success", + ]); + + // A loop of large native calls stops at the same budget, in seconds. + const native = yield* execute(runner, "native-runaway", nativeRunaway); + expect(native.execution).toMatchObject({ + ok: false, + error: { + kind: "TimeoutExceeded", + message: expect.stringContaining( + `Execution exceeded its compute budget of ${defaultMaxSteps} interpreter steps`, + ), + }, + }); + expect(native.ms).toBeLessThan(20_000); + + // So does a loop of operators that coerce a whole array. + const operator = yield* execute(runner, "operator-runaway", operatorRunaway); + expect(operator.execution).toMatchObject({ + ok: false, + error: { + kind: "TimeoutExceeded", + message: expect.stringContaining( + `Execution exceeded its compute budget of ${defaultMaxSteps} interpreter steps`, + ), + }, + }); + expect(operator.ms).toBeLessThan(20_000); + + // Checking membership with `in` against a large index stays fast and inside it. + const dedupe = yield* execute(runner, "dedupe-with-in", dedupeWithIn); + expect(dedupe.execution).toMatchObject({ ok: true, value: 10000 }); + expect(dedupe.ms).toBeLessThan(20_000); + + // Grouping 50,000 rows into 10 or 50 groups completes. + const grouped10 = yield* execute(runner, "group-by-10", groupBy(10)); + expect(grouped10.execution).toMatchObject({ ok: true, value: 10 }); + const grouped50 = yield* execute(runner, "group-by-50", groupBy(50)); + expect(grouped50.execution).toMatchObject({ ok: true, value: 50 }); + + // Grouping into an object and attaching a shared value to every row complete promptly. + const groupedObject = yield* execute(runner, "group-by-object", groupByObject); + expect(groupedObject.execution).toMatchObject({ ok: true, value: 10 }); + expect(groupedObject.ms).toBeLessThan(20_000); + const shared = yield* execute(runner, "shared-meta", sharedMeta); + expect(shared.execution).toMatchObject({ ok: true, value: 20000 }); + expect(shared.ms).toBeLessThan(20_000); + + // Grouping row objects by storing each group back into itself completes, in both forms. + const groupedRows = yield* execute(runner, "group-rows-or-push", groupRowsOrPush); + expect(groupedRows.execution).toMatchObject({ ok: true, value: 10 }); + expect(groupedRows.ms).toBeLessThan(20_000); + const reducedRows = yield* execute(runner, "group-rows-reduce", groupRowsReduce); + expect(reducedRows.execution).toMatchObject({ ok: true, value: 10 }); + expect(reducedRows.ms).toBeLessThan(20_000); + + // A loop that interpolates a megabyte string stops at the budget, in seconds. + const template = yield* execute(runner, "template-runaway", templateRunaway); + expect(template.execution).toMatchObject({ + ok: false, + error: { + kind: "TimeoutExceeded", + message: expect.stringContaining( + `Execution exceeded its compute budget of ${defaultMaxSteps} interpreter steps`, + ), + }, + }); + expect(template.ms).toBeLessThan(20_000); + + // A loop that joins a megabyte string stops at the budget, in seconds. + const concat = yield* execute(runner, "concat-runaway", concatRunaway); + expect(concat.execution).toMatchObject({ + ok: false, + error: { + kind: "TimeoutExceeded", + message: expect.stringContaining( + `Execution exceeded its compute budget of ${defaultMaxSteps} interpreter steps`, + ), + }, + }); + expect(concat.ms).toBeLessThan(20_000); + + // Transforming 10,000 records with native calls stays well inside it. + const records = yield* execute(runner, "records-transform", recordsTransform); + expect(records.execution).toMatchObject({ ok: true, value: { kept: 5000, json: true } }); + + yield* evidence.measure("runaway-ms", stopped.ms); + yield* evidence.measure("ordinary-quiet-max-ms", slowestQuiet); + yield* evidence.measure("ordinary-busy-max-ms", Math.max(...busy)); + yield* evidence.json("summary.json", { + stepsPerSecond: Math.round(defaultMaxSteps / (stopped.ms / 1000)), + busyRounds: busy.length, + quiet, + busy, + }); + yield* evidence.measure("heavy-but-fine-ms", fine.ms); + yield* evidence.measure("native-runaway-ms", native.ms); + yield* evidence.measure("records-transform-ms", records.ms); + yield* evidence.measure("operator-runaway-ms", operator.ms); + yield* evidence.measure("dedupe-with-in-ms", dedupe.ms); + yield* evidence.measure("group-by-10-ms", grouped10.ms); + yield* evidence.measure("group-by-50-ms", grouped50.ms); + yield* evidence.measure("group-by-object-ms", groupedObject.ms); + yield* evidence.measure("shared-meta-ms", shared.ms); + yield* evidence.measure("concat-runaway-ms", concat.ms); + yield* evidence.measure("group-rows-or-push-ms", groupedRows.ms); + yield* evidence.measure("group-rows-reduce-ms", reducedRows.ms); + yield* evidence.measure("template-runaway-ms", template.ms); + }).pipe(Effect.provide(McpClient.layer)), + ), + { timeout: 180_000 }, + ); +}); diff --git a/packages/mcp/src/contracts/execute.ts b/packages/mcp/src/contracts/execute.ts index 3f97dd4ace..0cc72b6552 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -526,12 +526,30 @@ export class CatalogUnavailable extends Schema.TaggedError() export const McpLimits = Schema.Struct({ timeoutMs: Schema.Int.check(Schema.isGreaterThan(0)), maxToolCalls: Schema.Int.check(Schema.isGreaterThan(0)), + /** + * Interpreter steps one program may spend: one per statement or expression evaluated, plus steps + * for native calls, for operators that walk or coerce a whole value, and for storing a value (which + * walks it for cycles), in proportion to the data they read, copy and return (about one per two + * array elements or object keys and one per 256 string characters); `+` or a template that + * yields a string costs one per 4,096 characters of the result. A value a call returns that the + * program already holds, such as the group `Map.get` returns, is not charged. Waiting on tool calls and approvals spends nothing. This + * limits the work a program does on the server's own thread, which `timeoutMs` alone does not; + * one native call, or one backtracking regular expression match, still runs to its end before it + * is charged. The default stops a comparator `sort` of about 100,000 objects, which takes about 20 + * million steps; self-host and local raise it with `EXECUTOR_MCP_MAX_STEPS`. + */ + maxSteps: Schema.Int.check(Schema.isGreaterThan(0)), }); export type McpLimits = typeof McpLimits.Type; /** Default interpreter budgets; each product may supply different limits. */ export const defaultMcpLimits: McpLimits = { timeoutMs: 5 * 60_000, maxToolCalls: 100, + // About 9 s of interpreter time in workerd, which runs about 550,000 steps a second (Node runs + // about 2 million); native-heavy programs are charged so they stop sooner, not later. Ordinary + // programs spend 0.1-0.5 s before their first call; the program that held production's thread + // for 203 s took about 110 million steps. + maxSteps: 5_000_000, }; /** UTF-8 bytes of JSON one search page may hold; `next` continues past it. */ export const searchPageBytes = 25_000; diff --git a/packages/mcp/src/implementation/execute.ts b/packages/mcp/src/implementation/execute.ts index 4ab198cc59..9aa5a66ada 100644 --- a/packages/mcp/src/implementation/execute.ts +++ b/packages/mcp/src/implementation/execute.ts @@ -1704,6 +1704,9 @@ export const timeoutMessage = (timeoutMs: number, phase: "discovery" | "program" ? `Execution timed out after ${timeoutMs}ms while loading app tools; no program code ran.` : `Execution timed out after ${timeoutMs}ms; earlier tool calls may have completed.`; +/** How the patched CodeMode starts the message of a program that spent `maxSteps`. */ +const stepBudgetMessage = "Execution exceeded its compute budget of "; + /** * After the budget is spent, CodeMode interrupts the program and returns its calls and logs. * The driver waits this long for that result. If it does not arrive, the driver reports the @@ -1845,7 +1848,11 @@ function runProgram( }), ), // No output limit: the program's value and logs come back whole. - limits: { timeoutMs: limits.timeoutMs, maxToolCalls: limits.maxToolCalls }, + limits: { + timeoutMs: limits.timeoutMs, + maxToolCalls: limits.maxToolCalls, + maxSteps: limits.maxSteps, + }, // Both hooks run on the fiber that makes the call. onToolCallStart: ({ index, name }) => Effect.map(Effect.fiberId, (fiber) => { @@ -1873,7 +1880,12 @@ function runProgram( result.toolCalls.forEach(({ name }, index) => { progress.calls[index] ??= { name, outcome: "interrupted" }; }); - const timedOut = !result.ok && result.error.kind === "TimeoutExceeded"; + // A spent step budget keeps CodeMode's message, which names the budget and the line. + const stepsSpent = + !result.ok && + result.error.kind === "TimeoutExceeded" && + result.error.message.startsWith(stepBudgetMessage); + const timedOut = !result.ok && result.error.kind === "TimeoutExceeded" && !stepsSpent; const unavailable = result.ok ? undefined : unavailableTarget(result.error, prepared.namespaces); @@ -1907,6 +1919,11 @@ function runProgram( toolCalls: reportedCalls(progress), }); if (timedOut) yield* Effect.annotateCurrentSpan("executor.timeout.phase", "program"); + if (stepsSpent) + yield* Effect.annotateCurrentSpan({ + "executor.timeout.phase": "program", + "executor.timeout.budget": "steps", + }); // Signatures cost CPU per tool; a program renders them only for its global `search()` pages. yield* Effect.annotateCurrentSpan( "executor.codemode.signatures_rendered", diff --git a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch index 17f1a5e352..375ee40b51 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -1,5 +1,5 @@ diff --git a/dist/codemode.d.ts b/dist/codemode.d.ts -index 7e9424e..47295db 100644 +index 7e9424edcc57ed3c533ac1a55da8f6f0b6a1c8a4..847f79c154eb8a5d1a4f629f932b74ec31eb4fd5 100644 --- a/dist/codemode.d.ts +++ b/dist/codemode.d.ts @@ -1,8 +1,8 @@ @@ -13,7 +13,28 @@ index 7e9424e..47295db 100644 /** Signature-construction helpers for host-owned catalog instructions. */ export { searchSignature, toolExpression } from "./tool-runtime.js"; /** Resource budgets enforced independently during each CodeMode program execution. */ -@@ -37,6 +37,11 @@ export type ExecuteOptions = {}> = { +@@ -14,6 +14,12 @@ export type ExecutionLimits = { + readonly timeoutMs?: number; + /** Maximum number of tool calls admitted by the runtime. No default: absent means unlimited. */ + readonly maxToolCalls?: number; ++ /** ++ * Maximum statements and expressions the interpreter evaluates, across every function and job of ++ * the program. Waiting on tool calls evaluates nothing. Past it the program fails with ++ * `TimeoutExceeded`, which its own catch cannot stop. No default: absent means unlimited. ++ */ ++ readonly maxSteps?: number; + /** + * Maximum UTF-8 bytes retained from the result and logs. Warnings have a separate equal budget; + * truncation notices and host formatting are additional. +@@ -23,6 +29,7 @@ export type ExecutionLimits = { + export type ResolvedExecutionLimits = { + readonly timeoutMs: number | undefined; + readonly maxToolCalls: number | undefined; ++ readonly maxSteps: number | undefined; + readonly maxOutputBytes: number | undefined; + }; + /** Options for one CodeMode execution. */ +@@ -37,6 +44,11 @@ export type ExecuteOptions = {}> = { onToolCallStart?: (call: ToolRuntime.ToolCallStarted) => Effect.Effect>; /** Observes each admitted tool call as it succeeds, fails, or is interrupted. */ onToolCallEnd?: (call: ToolRuntime.ToolCallEnded) => Effect.Effect>; @@ -25,7 +46,7 @@ index 7e9424e..47295db 100644 }; /** A JSON value that can cross the confined interpreter boundary. */ export type DataValue = Schema.Json; -@@ -58,6 +63,8 @@ export declare const Diagnostic: Schema.Struct<{ +@@ -58,6 +70,8 @@ export declare const Diagnostic: Schema.Struct<{ readonly column: Schema.Number; }>>; readonly suggestions: Schema.optionalKey>; @@ -34,7 +55,7 @@ index 7e9424e..47295db 100644 }>; /** A normalized program diagnostic safe to return across an agent tool boundary. */ export type Diagnostic = typeof Diagnostic.Type; -@@ -141,6 +148,8 @@ export type Result = typeof Result.Type; +@@ -141,6 +155,8 @@ export type Result = typeof Result.Type; export type Runtime = { readonly catalog: () => ReadonlyArray; readonly execute: (code: string) => Effect.Effect; @@ -44,7 +65,7 @@ index 7e9424e..47295db 100644 /** Executes one Effect-native CodeMode program without constructing a reusable runtime. */ export declare const execute: >(options: ExecuteOptions) => Effect.Effect>; diff --git a/dist/codemode.js b/dist/codemode.js -index e94b56b..8064157 100644 +index e94b56b920a396f6bae79fb51cd60bd07b83d591..4bcf465da622258b15b75e3de0520416791cd2c9 100644 --- a/dist/codemode.js +++ b/dist/codemode.js @@ -23,6 +23,8 @@ export const Diagnostic = Schema.Struct({ @@ -56,7 +77,15 @@ index e94b56b..8064157 100644 }); const ToolCallSchema = Schema.Struct({ name: Schema.String }); export const Success = Schema.Struct({ -@@ -65,6 +67,7 @@ export const make = (options = {}) => { +@@ -51,6 +53,7 @@ const validateLimit = (name, value, minimum) => { + const resolveExecutionLimits = (limits) => ({ + timeoutMs: validateLimit("timeoutMs", limits?.timeoutMs, 1), + maxToolCalls: validateLimit("maxToolCalls", limits?.maxToolCalls, 0), ++ maxSteps: validateLimit("maxSteps", limits?.maxSteps, 1), + maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0), + }); + /** Executes one Effect-native CodeMode program without constructing a reusable runtime. */ +@@ -65,6 +68,7 @@ export const make = (options = {}) => { const prepared = ToolRuntime.prepare(tools); return { catalog: () => prepared.catalog, @@ -66,7 +95,7 @@ index e94b56b..8064157 100644 }; }; diff --git a/dist/interpreter/errors.js b/dist/interpreter/errors.js -index 78e59e5..078dab7 100644 +index 78e59e5eb170dfcf427d136b52fb1682ff010d39..078dab75e4755260cb9229405e3cea42bfdf41cc 100644 --- a/dist/interpreter/errors.js +++ b/dist/interpreter/errors.js @@ -22,7 +22,11 @@ export const normalizeError = (error) => { @@ -100,7 +129,7 @@ index 78e59e5..078dab7 100644 export const constructErrorValue = (name, args) => createErrorValue(name, args[0] === undefined ? "" : coerceToString(args[0])); export const constructAggregateErrorValue = (runner, args, node) => Effect.gen(function* () { diff --git a/dist/interpreter/execute.d.ts b/dist/interpreter/execute.d.ts -index e17d584..aeb1771 100644 +index e17d5849420a0266e37bf14b7f0fc257ebce2818..aeb177171d6c924b02c4174edb767bc70aa232b4 100644 --- a/dist/interpreter/execute.d.ts +++ b/dist/interpreter/execute.d.ts @@ -1,4 +1,4 @@ @@ -110,9 +139,18 @@ index e17d584..aeb1771 100644 -export declare const executeWithLimits: >(options: ExecuteOptions, limits: ResolvedExecutionLimits, searchIndex: ToolRuntime.DiscoveryPlan["searchIndex"]) => Effect.Effect>; +export declare const executeWithLimits: >(options: ExecuteOptions, limits: ResolvedExecutionLimits, searchIndex: ToolRuntime.DiscoveryPlan["searchIndexOnUse"]) => Effect.Effect>; diff --git a/dist/interpreter/execute.js b/dist/interpreter/execute.js -index 3d7f507..41edd0a 100644 +index 3d7f507ef37117b1d3ca12a3fa1d9754abca4031..82ffd78ab9661d809225c8e6326b142e225fd60d 100644 --- a/dist/interpreter/execute.js +++ b/dist/interpreter/execute.js +@@ -5,7 +5,7 @@ import { Cause, Effect, Scope } from "effect"; + import { transpile } from "#transpile"; + import { copyIn, copyOut, ToolRuntime } from "../tool-runtime.js"; + import { normalizeError } from "./errors.js"; +-import { InterpreterRuntimeError, isRecord } from "./model.js"; ++import { InterpreterRuntimeError, isRecord, StepBudget } from "./model.js"; + import { PromiseRuntime } from "./promises.js"; + import { Interpreter } from "./runtime.js"; + export const executeWithLimits = (options, limits, searchIndex) => { @@ -21,7 +21,7 @@ export const executeWithLimits = (options, limits, searchIndex) => { const tools = ToolRuntime.make((options.tools ?? {}), limits.maxToolCalls, searchIndex, { onToolCallStart: options.onToolCallStart, @@ -122,10 +160,11 @@ index 3d7f507..41edd0a 100644 const logs = []; const logged = () => (logs.length > 0 ? { logs: [...logs] } : {}); // Set only after copy-out so timeouts cannot report invalid values as completed. -@@ -30,17 +30,21 @@ export const executeWithLimits = (options, limits, searchIndex) => { +@@ -29,18 +29,22 @@ export const executeWithLimits = (options, limits, searchIndex) => { + const base = Effect.acquireUseRelease(Scope.make("parallel"), (scope) => Effect.gen(function* () { const program = parseProgram(options.code); const promises = new PromiseRuntime(scope); - const interpreter = new Interpreter(tools.execute, tools.search, tools.keys, promises, logs); +- const interpreter = new Interpreter(tools.execute, tools.search, tools.keys, promises, logs); - const value = yield* interpreter.run(program); - const result = copyOut(copyIn(value, "Execution result"), "nullify"); - returned = { value: result, promises }; @@ -137,6 +176,7 @@ index 3d7f507..41edd0a 100644 - ...logged(), - toolCalls: tools.calls, - }; ++ const interpreter = new Interpreter(tools.execute, tools.search, tools.keys, promises, logs, new StepBudget(limits.maxSteps)); + // The program's main job holds the turn until its leftover jobs are interrupted. + return yield* Effect.gen(function* () { + yield* promises.turn.take; @@ -156,20 +196,144 @@ index 3d7f507..41edd0a 100644 const timeoutMs = limits.timeoutMs; const operation = timeoutMs === undefined diff --git a/dist/interpreter/methods.d.ts b/dist/interpreter/methods.d.ts -index db276e3..d5b7dfc 100644 +index db276e322e6eb9108c09dff9b5218bc4e29df616..ec1067b6c1d80e8688dd006858d4924fb0769086 100644 --- a/dist/interpreter/methods.d.ts +++ b/dist/interpreter/methods.d.ts -@@ -6,6 +6,8 @@ export type CallbackRunner = { +@@ -5,12 +5,15 @@ import { type SyncIteratorRunner } from "./iterator.js"; + export type CallbackRunner = { readonly invokeFunction: (fn: CodeModeFunction, args: Array) => Effect.Effect; readonly invokeCallable: (callable: unknown, args: Array, node: AstNode) => Effect.Effect; ++ readonly meterWalk: (node: AstNode) => (visited: number) => void; readonly settlePromise: (promise: CodeModePromise) => Effect.Effect; + readonly suspend: (wait: Effect.Effect) => Effect.Effect; + readonly leave: (wait: Effect.Effect) => Effect.Effect; }; export type SupportedCallback = CodeModeFunction | CoercionFunction | UriFunction | PromiseCapabilityFunction | GlobalMethodReference | JsonMethodReference | IntrinsicReference | ErrorConstructorReference | GlobalNamespace | PromiseNamespace; export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback; + export declare const invokeIntrinsic: (runner: CallbackRunner, ref: IntrinsicReference, args: Array, node: AstNode) => Effect.Effect; +-export declare const invokeGlobalMethod: (ref: GlobalMethodReference, args: Array, node: AstNode) => unknown; ++export declare const invokeGlobalMethod: (ref: GlobalMethodReference, args: Array, node: AstNode, meter?: (visited: number) => void) => unknown; + export declare const arrayStatics: Set; + export declare const invokeArrayFrom: (runner: CallbackRunner & SyncIteratorRunner, args: Array, node: AstNode) => Effect.Effect; + export declare const invokeGroupBy: (runner: CallbackRunner & SyncIteratorRunner, namespace: "Map" | "Object", args: Array, node: AstNode) => Effect.Effect; +diff --git a/dist/interpreter/methods.js b/dist/interpreter/methods.js +index 320c1cada40e573468be75551742898317b0bd5f..70a5cce54ec26ea422ff5ee20fcddb5d4ba602bb 100644 +--- a/dist/interpreter/methods.js ++++ b/dist/interpreter/methods.js +@@ -92,11 +92,11 @@ const coerceNumericArgument = (runner, value, node) => { + throw new InterpreterRuntimeError("Cannot convert object to primitive value.", node).as("TypeError"); + }); + }; +-export const invokeGlobalMethod = (ref, args, node) => { ++export const invokeGlobalMethod = (ref, args, node, meter) => { + if (ref.namespace === "console") + throw new InterpreterRuntimeError(`console.${ref.name} is not available.`, node); + if (ref.namespace === "Object") +- return invokeObjectMethod(ref.name, args, node); ++ return invokeObjectMethod(ref.name, args, node, meter); + if (ref.namespace === "Math") + return invokeMathMethod(ref.name, args, node); + if (ref.namespace === "Array") +@@ -810,13 +810,13 @@ const invokeArrayMethod = (runner, target, name, args, node) => { + case "push": { + // Validate all insertions before mutating to avoid partial cyclic updates. + for (const item of args) +- rejectCircularInsertion(target, item, "Array.push result", node); ++ rejectCircularInsertion(target, item, "Array.push result", node, undefined, runner.meterWalk(node)); + target.push(...args); + return Effect.succeed(target.length); + } + case "unshift": { + for (const item of args) +- rejectCircularInsertion(target, item, "Array.unshift result", node); ++ rejectCircularInsertion(target, item, "Array.unshift result", node, undefined, runner.meterWalk(node)); + target.unshift(...args); + return Effect.succeed(target.length); + } +@@ -833,7 +833,7 @@ const invokeArrayMethod = (runner, target, name, args, node) => { + const deleteCount = optNumber(args[1], "delete count") ?? 0; + const inserted = args.slice(2); + for (const item of inserted) +- rejectCircularInsertion(target, item, "Array.splice result", node); ++ rejectCircularInsertion(target, item, "Array.splice result", node, undefined, runner.meterWalk(node)); + return Effect.succeed(target.splice(start, deleteCount, ...inserted)); + } + case "toSpliced": { +@@ -851,7 +851,7 @@ const invokeArrayMethod = (runner, target, name, args, node) => { + return Effect.succeed(copied); + } + case "fill": { +- rejectCircularInsertion(target, args[0], "Array.fill result", node); ++ rejectCircularInsertion(target, args[0], "Array.fill result", node, undefined, runner.meterWalk(node)); + return Effect.succeed(target.fill(args[0], optNumber(args[1], "start"), optNumber(args[2], "end"))); + } + case "copyWithin": +diff --git a/dist/interpreter/model.d.ts b/dist/interpreter/model.d.ts +index ed2accf68c9f24fa26154e266617ef587340445a..852ea1d07aeb70be1730579dc561f636e9ae938f 100644 +--- a/dist/interpreter/model.d.ts ++++ b/dist/interpreter/model.d.ts +@@ -136,6 +136,18 @@ export declare class InterpreterRuntimeError extends Error { + constructor(message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray | undefined); + as(errorName: string): this; + } ++/** Thrown at every interpreter step after an execution spends its step budget. */ ++export declare class StepBudgetExceeded extends InterpreterRuntimeError { ++ constructor(limit: number, node?: AstNode); ++} ++/** Counts the statements and expressions one execution evaluates; past its limit every step throws. */ ++export declare class StepBudget { ++ readonly limit: number | undefined; ++ steps: number; ++ constructor(limit: number | undefined); ++ step(node?: AstNode): void; ++ charge(steps: number, node?: AstNode): void; ++} + export declare const unsupportedSyntax: (kind: string, node: AstNode) => InterpreterRuntimeError; + export declare const isRecord: (value: unknown) => value is Record; + export declare const asNode: (value: unknown, context: string) => AstNode; +diff --git a/dist/interpreter/model.js b/dist/interpreter/model.js +index d3a203d737409a4a51759206a156d90fed0ef918..1184bdb43edd332f54cc50134fcc05a7f663c1c4 100644 +--- a/dist/interpreter/model.js ++++ b/dist/interpreter/model.js +@@ -141,6 +141,36 @@ export class InterpreterRuntimeError extends Error { + return this; + } + } ++/** Thrown at every interpreter step after an execution spends its step budget. */ ++export class StepBudgetExceeded extends InterpreterRuntimeError { ++ constructor(limit, node) { ++ super(`Execution exceeded its compute budget of ${limit} interpreter steps and was stopped. Programs run in an interpreter many times slower than native code, so move heavy computation, such as parsing, diffing or rewriting large text or data, out of the program and pass it only the results.`, node, "TimeoutExceeded"); ++ } ++} ++/** ++ * Counts the statements and expressions one execution evaluates, across all of its functions and ++ * jobs. Time spent waiting on tool calls evaluates nothing, so it is not counted. Once the limit is ++ * spent every further step throws, so a program's catch or finally cannot keep it running. ++ */ ++export class StepBudget { ++ limit; ++ steps = 0; ++ constructor(limit) { ++ this.limit = limit; ++ } ++ step(node) { ++ if (this.limit !== undefined && ++this.steps > this.limit) ++ throw new StepBudgetExceeded(this.limit, node); ++ } ++ /** Charges native work, measured in steps, done or about to be done on the program's behalf. */ ++ charge(steps, node) { ++ if (this.limit === undefined || steps < 1) ++ return; ++ this.steps += Math.floor(steps); ++ if (this.steps > this.limit) ++ throw new StepBudgetExceeded(this.limit, node); ++ } ++} + export const unsupportedSyntax = (kind, node) => new InterpreterRuntimeError(`Syntax '${kind}' is not supported. ${supportedSyntaxMessage}`, node, "UnsupportedSyntax", [supportedSyntaxMessage]); + export const isRecord = (value) => typeof value === "object" && value !== null; + export const asNode = (value, context) => { diff --git a/dist/interpreter/promises.d.ts b/dist/interpreter/promises.d.ts -index 3c4016c..51e2419 100644 +index 3c4016c3d1e0d7c2b2bf29382489e2957fe7cce2..51e241995cdbf2598a0283161b8db99dc8d0f6d2 100644 --- a/dist/interpreter/promises.d.ts +++ b/dist/interpreter/promises.d.ts @@ -1,21 +1,44 @@ @@ -221,7 +385,7 @@ index 3c4016c..51e2419 100644 interrupt(): Effect.Effect>; } diff --git a/dist/interpreter/promises.js b/dist/interpreter/promises.js -index 996f9a5..55843d5 100644 +index 996f9a5090879de5104b8ecb439ee15fecee6f40..55843d5d6d27b370a235d66e1a242ab274b03770 100644 --- a/dist/interpreter/promises.js +++ b/dist/interpreter/promises.js @@ -5,9 +5,212 @@ import { applyCollectionCallback, isSupportedCallback } from "./methods.js"; @@ -593,16 +757,131 @@ index 996f9a5..55843d5 100644 const chainReaction = (runner, promises, source, onFulfilled, onRejected, method, node) => { const box = {}; const body = Effect.gen(function* () { +diff --git a/dist/interpreter/references.d.ts b/dist/interpreter/references.d.ts +index 7b1492c21fbe95d6b995327ad7aec31ba345bf6b..a2ebe70b30121ff5e4b6ca60e0b6109ac317f633 100644 +--- a/dist/interpreter/references.d.ts ++++ b/dist/interpreter/references.d.ts +@@ -2,5 +2,5 @@ import { type AstNode } from "./model.js"; + export declare const isRuntimeReference: (value: unknown) => boolean; + export declare const containsRuntimeReference: (value: unknown) => boolean; + export declare const containsOpaqueReference: (value: unknown) => boolean; +-export declare const rejectCircularInsertion: (container: object, value: unknown, label: string, node: AstNode, seen?: Set) => void; ++export declare const rejectCircularInsertion: (container: object, value: unknown, label: string, node: AstNode, seen?: Set, meter?: (visited: number) => void) => void; + export declare const typeofValue: (value: unknown) => string; +diff --git a/dist/interpreter/references.js b/dist/interpreter/references.js +index 314a7c3752745de0b8680531b64cf37bc584a812..a4bf237d4e716d3259b0f617695268cc47c5b289 100644 +--- a/dist/interpreter/references.js ++++ b/dist/interpreter/references.js +@@ -70,23 +70,82 @@ export const containsOpaqueReference = (value) => { + } + return false; + }; +-// Reject cycles before mutation so later boundary walks remain safe. +-export const rejectCircularInsertion = (container, value, label, node, seen = new Set()) => { ++// Like containsOpaqueReference, but reports every value it visits to `meter` in batches of ++// METER_BATCH, so the caller can charge the walk and stop it partway. Primitives return at once. ++const METER_BATCH = 1024; ++export const containsOpaqueReferenceMetered = (value, meter) => { ++ if (value === null || typeof value !== "object") ++ return false; + const pending = [[value].values()]; ++ const seen = new Set(); ++ let visited = 0; + while (pending.length > 0) { + const next = pending.at(-1).next(); + if (next.done) { + pending.pop(); + continue; + } ++ if (++visited === METER_BATCH) { ++ meter(visited); ++ visited = 0; ++ } + const current = next.value; +- if (current === container) +- throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue"); +- if (current === null || typeof current !== "object" || isRuntimeReference(current) || seen.has(current)) ++ if (isCodeModeValue(current)) ++ continue; ++ if (isRuntimeReference(current)) { ++ meter(visited); ++ return true; ++ } ++ if (current === null || typeof current !== "object" || seen.has(current)) + continue; + seen.add(current); + pending.push(childValues(current)); + } ++ meter(visited); ++ return false; ++}; ++/** An interpreter reference that is not CodeMode data, such as a function or the tools root. */ ++export const isOpaqueReference = (value) => !isCodeModeValue(value) && isRuntimeReference(value); ++// Reject cycles before mutation so later boundary walks remain safe. ++// Data objects whose own values are all primitives or interpreter references, so a cycle walk ++// that reaches one can stop there: the container it looks for is a data object and cannot be among ++// them. An object leaves the set when an object is inserted into it, and every insertion of a ++// value into existing program data runs rejectCircularInsertion first, so membership stays exact. ++const leafData = new WeakSet(); ++// The walk visits each value once and reports visits to `meter` in batches of METER_BATCH, so the ++// caller can charge it and stop it partway; a leaf seen before costs one visit. ++export const rejectCircularInsertion = (container, value, label, node, seen = new Set(), meter) => { ++ if (value === null || typeof value !== "object") ++ return; ++ leafData.delete(container); ++ const pending = [{ children: [value].values(), owner: undefined, leaf: true }]; ++ let visited = 0; ++ while (pending.length > 0) { ++ const frame = pending.at(-1); ++ const next = frame.children.next(); ++ if (next.done) { ++ pending.pop(); ++ if (frame.owner !== undefined && frame.leaf) ++ leafData.add(frame.owner); ++ continue; ++ } ++ if (meter !== undefined && ++visited === METER_BATCH) { ++ meter(visited); ++ visited = 0; ++ } ++ const current = next.value; ++ if (current === container) ++ throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue"); ++ if (current === null || typeof current !== "object" || isRuntimeReference(current)) ++ continue; ++ frame.leaf = false; ++ if (seen.has(current) || leafData.has(current)) ++ continue; ++ seen.add(current); ++ pending.push({ children: childValues(current), owner: current, leaf: true }); ++ } ++ if (meter !== undefined) ++ meter(visited); + }; + export const typeofValue = (value) => { + if (value instanceof CodeModeFunction || diff --git a/dist/interpreter/runtime.d.ts b/dist/interpreter/runtime.d.ts -index bd1bc70..3ad5894 100644 +index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59590d8675 100644 --- a/dist/interpreter/runtime.d.ts +++ b/dist/interpreter/runtime.d.ts -@@ -11,11 +11,12 @@ export declare class Interpreter { +@@ -1,6 +1,7 @@ + import { Effect } from "effect"; + import { type ProgramNode } from "./model.js"; + import { PromiseRuntime } from "./promises.js"; ++import { StepBudget } from "./model.js"; + export declare class Interpreter { + private scopes; + private readonly executeTool; +@@ -8,14 +9,16 @@ export declare class Interpreter { + private readonly toolKeys; + private readonly logs; + private readonly promises; ++ private readonly budget; private generatorState?; private generatorAsync; private readonly runner; - constructor(executeTool: (path: ReadonlyArray, args: Array) => Effect.Effect, invokeSearch: (args: Array) => Effect.Effect, toolKeys: (path: ReadonlyArray) => ReadonlyArray, promises: PromiseRuntime, logs?: Array); -+ constructor(executeTool: (path: ReadonlyArray, args: Array) => Effect.Effect, invokeSearch: (args: Array) => Effect.Effect, toolKeys: (path: ReadonlyArray) => Effect.Effect, unknown, R>, promises: PromiseRuntime, logs?: Array); ++ constructor(executeTool: (path: ReadonlyArray, args: Array) => Effect.Effect, invokeSearch: (args: Array) => Effect.Effect, toolKeys: (path: ReadonlyArray) => Effect.Effect, unknown, R>, promises: PromiseRuntime, logs?: Array, budget?: StepBudget); run(program: ProgramNode): Effect.Effect; private createToolCallPromise; private createPromise; @@ -611,20 +890,153 @@ index bd1bc70..3ad5894 100644 private evaluateStatement; private evaluateBlock; private createFunction; +@@ -71,6 +74,7 @@ export declare class Interpreter { + private evaluateUpdateExpression; + private evaluateCallExpression; + private invokeCallable; ++ private dispatchCallable; + private invokeObjectMethodOnTools; + private invokeConsole; + private evaluateCallArguments; diff --git a/dist/interpreter/runtime.js b/dist/interpreter/runtime.js -index 8c1b549..3575409 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..bc213c7be488a22deac6d42fe4b1f630647bb577 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js -@@ -154,6 +154,8 @@ export class Interpreter { +@@ -1,11 +1,11 @@ + import { Cause, Deferred, Effect, Exit } from "effect"; + import { isBlockedMember, ToolReference, ToolRuntimeError } from "../tool-runtime.js"; +-import { AsyncIteratorSymbol, asNode, CodeModeFunction, CodeModeGenerator, CoercionFunction, ComputedValue, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, GeneratorMethodReference, GeneratorReturn, getArray, getBoolean, getNode, getOptionalNode, getString, IntrinsicReference, InterpreterRuntimeError, isRecord, IteratorSymbol, IteratorSymbols, JsonMethodReference, OptionalShortCircuit, PromiseCapabilityFunction, PromiseInstanceMethodReference, PromiseMethodReference, PromiseNamespace, ProgramThrow, SearchFunction, SymbolNamespace, unsupportedSyntax, UriFunction, } from "./model.js"; ++import { AsyncIteratorSymbol, asNode, CodeModeFunction, CodeModeGenerator, CoercionFunction, ComputedValue, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, GeneratorMethodReference, GeneratorReturn, getArray, getBoolean, getNode, getOptionalNode, getString, IntrinsicReference, InterpreterRuntimeError, isRecord, IteratorSymbol, IteratorSymbols, JsonMethodReference, OptionalShortCircuit, PromiseCapabilityFunction, PromiseInstanceMethodReference, PromiseMethodReference, PromiseNamespace, ProgramThrow, SearchFunction, StepBudget, StepBudgetExceeded, SymbolNamespace, unsupportedSyntax, UriFunction, } from "./model.js"; + import { caughtErrorValue, constructAggregateErrorValue, constructErrorValue } from "./errors.js"; + import { arrayStatics, invokeArrayFrom, invokeGlobalMethod, invokeGroupBy, invokeIntrinsic, } from "./methods.js"; + import { preserveConsumerError } from "./iterator.js"; + import { constructPromise, invokePromiseInstanceMethod, invokePromiseMethod, PromiseRuntime, resolvePromise, resolvePromiseValue, } from "./promises.js"; +-import { containsOpaqueReference, isRuntimeReference, rejectCircularInsertion, typeofValue } from "./references.js"; ++import { containsOpaqueReference, containsOpaqueReferenceMetered, isOpaqueReference, isRuntimeReference, rejectCircularInsertion, typeofValue } from "./references.js"; + import { ScopeStack } from "./scope.js"; + import { arrayMethods, mapMethods, mapStatics, setMethods } from "../stdlib/collections.js"; + import { consoleMethods, formatConsoleMessage } from "../stdlib/console.js"; +@@ -140,6 +140,96 @@ const copyIteratorSymbols = (source, target, consumed) => { + } + }; + const promiseResolutionNode = { type: "PromiseResolution" }; ++// Native work runs at host speed but still copies or walks its data. Each native call is charged ++// steps for the data it reads and returns: one per ELEMENTS_PER_STEP array elements, object keys ++// or Map and Set entries, and one per CHARS_PER_STEP string characters. An array of records or ++// arrays counts each element as wide as the widest of up to RECORD_WIDTH_SAMPLES evenly spaced ++// elements (at most RECORD_WIDTH_LIMIT), as copying a result walks every field. ++const ELEMENTS_PER_STEP = 2; ++const CHARS_PER_STEP = 256; ++const RECORD_WIDTH_LIMIT = 64; ++const RECORD_WIDTH_SAMPLES = 8; ++const SCAN_ITEMS_PER_STEP = 512; ++const CONCAT_CHARS_PER_STEP = 4096; ++const isPlainData = (value) => { ++ if (value === null || typeof value !== "object") ++ return false; ++ const prototype = Object.getPrototypeOf(value); ++ return prototype === Object.prototype || prototype === null; ++}; ++const shallowWidth = (value) => Array.isArray(value) ? value.length : isPlainData(value) ? Object.keys(value).length : 0; ++const recordWidth = (array) => { ++ const length = array.length; ++ if (length === 0) ++ return 0; ++ const stride = Math.max(1, Math.floor(length / RECORD_WIDTH_SAMPLES)); ++ let width = shallowWidth(array[length - 1]); ++ for (let index = 0; index < length && width < RECORD_WIDTH_LIMIT; index += stride) ++ width = Math.max(width, shallowWidth(array[index])); ++ return Math.min(width, RECORD_WIDTH_LIMIT); ++}; ++const nativeSteps = (value) => { ++ if (typeof value === "string") ++ return value.length / CHARS_PER_STEP; ++ if (Array.isArray(value)) ++ return (value.length * (1 + recordWidth(value))) / ELEMENTS_PER_STEP; ++ if (value instanceof CodeModeMap) ++ return value.map.size / ELEMENTS_PER_STEP; ++ if (value instanceof CodeModeSet) ++ return value.set.size / ELEMENTS_PER_STEP; ++ if (isPlainData(value)) ++ return Object.keys(value).length / ELEMENTS_PER_STEP; ++ return 0; ++}; ++// Spread and Set and Map construction take a string one character at a time, so a string costs as ++// many elements as it has characters there. ++const iterationSteps = (value) => typeof value === "string" ? value.length / ELEMENTS_PER_STEP : nativeSteps(value); ++// Comparing two strings reads them up to their first difference; equality first compares lengths. ++const comparisonSteps = (operator, l, r) => { ++ if (typeof l !== "string" || typeof r !== "string") ++ return 0; ++ if (operator === "===" || operator === "!==" || operator === "==" || operator === "!=") ++ return l.length === r.length ? l.length / CHARS_PER_STEP : 0; ++ return Math.min(l.length, r.length) / CHARS_PER_STEP; ++}; ++// Methods whose native work is bounded by their result, or is a native-speed move, rather than a ++// pass over their receiver: only the result is charged, so loops that call them stay linear. ++const resultSizedMethods = new Set([ ++ "at", "charAt", "charCodeAt", "codePointAt", "push", "pop", "shift", "unshift", "slice", ++ "substring", "substr", "splice", "startsWith", "endsWith", "padStart", "padEnd", "concat", ++]); ++// Searches compare without copying, scanning their receiver from their start position at most to ++// its end, at native scan speed: one step per SCAN_ITEMS_PER_STEP elements or characters. ++const searchMethods = new Set(["indexOf", "includes", "lastIndexOf"]); ++// String methods that do more per character than copying it, charged this many times its length. ++const heavyStringMethods = new Map([["normalize", 8], ["localeCompare", 8]]); ++// Methods that return a value the program already holds, an element or an accumulator, not a copy: ++// their result is not charged. Their work is charged for their receiver or counted in callbacks. ++const elementReturningMethods = new Set(["get", "at", "pop", "shift", "find", "findLast", "reduce", "reduceRight"]); ++// Global functions that read one key or the type of their argument, not its contents. ++const constantGlobalMethods = new Set(["Object.hasOwn", "Array.isArray"]); ++const isObjectValue = (value) => value !== null && typeof value === "object"; ++const receiverSteps = (receiver, name, args) => { ++ if (typeof receiver !== "string" && !Array.isArray(receiver)) ++ return 0; ++ if (resultSizedMethods.has(name)) ++ return 0; ++ if (typeof receiver === "string" && heavyStringMethods.has(name)) ++ return heavyStringMethods.get(name) * nativeSteps(receiver); ++ if (!searchMethods.has(name)) ++ return nativeSteps(receiver); ++ const from = typeof args[1] === "number" ? Math.min(Math.max(Math.trunc(args[1]), 0), receiver.length) : 0; ++ const scanned = name === "lastIndexOf" && typeof args[1] === "number" ? from + 1 : receiver.length - from; ++ return scanned / SCAN_ITEMS_PER_STEP; ++}; ++// Calls that run program code or wait on the host. Program code counts its own steps. ++const isProgramCall = (callable) => callable instanceof ToolReference || ++ callable instanceof PromiseMethodReference || ++ callable instanceof PromiseInstanceMethodReference || ++ callable instanceof CodeModeFunction || ++ callable instanceof GeneratorMethodReference || ++ callable instanceof SearchFunction || ++ callable instanceof PromiseCapabilityFunction; + export class Interpreter { + scopes; + executeTool; +@@ -147,6 +237,7 @@ export class Interpreter { + toolKeys; + logs; + promises; ++ budget; + generatorState; + generatorAsync = false; + runner = { +@@ -154,9 +245,13 @@ export class Interpreter { invokeCallable: (callable, args, node) => this.invokeCallable(callable, args, node), settlePromise: (promise) => this.settlePromise(promise), syncIterator: (value, node) => this.syncIterator(value, node), + suspend: (wait) => this.promises.turn.suspend(wait), + leave: (wait) => this.promises.turn.leave(wait), ++ meterWalk: (node) => this.meterWalk(node), }; - constructor(executeTool, invokeSearch, toolKeys, promises, logs = []) { +- constructor(executeTool, invokeSearch, toolKeys, promises, logs = []) { ++ constructor(executeTool, invokeSearch, toolKeys, promises, logs = [], budget = new StepBudget(undefined)) { const globalScope = new Map(); -@@ -223,21 +225,26 @@ export class Interpreter { ++ this.budget = budget; + this.scopes = new ScopeStack([globalScope]); + this.executeTool = executeTool; + this.invokeSearch = invokeSearch; +@@ -223,22 +318,28 @@ export class Interpreter { return value; }).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop()))); } @@ -654,9 +1066,29 @@ index 8c1b549..3575409 100644 + return this.promises.turn.suspend(Effect.yieldNow); + } evaluateStatement(node) { ++ this.budget.step(node); switch (node.type) { case "ExpressionStatement": -@@ -510,7 +517,7 @@ export class Interpreter { + return Effect.as(this.evaluateExpression(getNode(node, "expression")), { kind: "none" }); +@@ -346,7 +447,7 @@ export class Interpreter { + const self = this; + return Effect.gen(function* () { + const discriminant = yield* self.evaluateExpression(getNode(node, "discriminant")); +- if (containsOpaqueReference(discriminant)) { ++ if (containsOpaqueReferenceMetered(discriminant, self.meterWalk(node))) { + throw new InterpreterRuntimeError("Switch discriminants must be data values.", node, "InvalidDataValue"); + } + self.scopes.push(); +@@ -362,7 +463,7 @@ export class Interpreter { + continue; + } + const candidate = yield* self.evaluateExpression(test); +- if (containsOpaqueReference(candidate)) { ++ if (containsOpaqueReferenceMetered(candidate, self.meterWalk(test))) { + throw new InterpreterRuntimeError("Switch case values must be data values.", test, "InvalidDataValue"); + } + if (candidate === discriminant) { +@@ -510,7 +611,7 @@ export class Interpreter { const close = () => iterator ? self.closeIterator(iterator, node, awaiting) : awaiting @@ -665,7 +1097,7 @@ index 8c1b549..3575409 100644 : (cursor?.close ?? Effect.void); let assignment; if (left.type !== "VariableDeclaration" && -@@ -656,7 +663,7 @@ export class Interpreter { +@@ -656,7 +757,7 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(iterator.next, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -674,7 +1106,7 @@ index 8c1b549..3575409 100644 return yield* Effect.failCause(called.cause); } const captured = yield* Effect.exit(Effect.sync(() => { -@@ -665,7 +672,7 @@ export class Interpreter { +@@ -665,7 +766,7 @@ export class Interpreter { })); if (!Exit.isSuccess(captured)) { if (awaiting) @@ -683,7 +1115,7 @@ index 8c1b549..3575409 100644 return yield* Effect.failCause(captured.cause); } return { -@@ -681,7 +688,7 @@ export class Interpreter { +@@ -681,7 +782,7 @@ export class Interpreter { ? new GeneratorMethodReference(iterator.iterator, "return") : iterator.iterator.return; if (close === undefined || close === null) @@ -692,7 +1124,7 @@ index 8c1b549..3575409 100644 const self = this; return Effect.gen(function* () { const method = self.requireIteratorMethod(close, "Iterator return", node); -@@ -692,13 +699,13 @@ export class Interpreter { +@@ -692,13 +793,13 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(method, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -708,7 +1140,7 @@ index 8c1b549..3575409 100644 return yield* Effect.failCause(captured.cause); } if (awaiting) -@@ -720,17 +727,18 @@ export class Interpreter { +@@ -720,17 +821,18 @@ export class Interpreter { return value; throw new InterpreterRuntimeError(`${context} must be a function.`, node).as("TypeError"); } @@ -731,16 +1163,197 @@ index 8c1b549..3575409 100644 } evaluateForInStatement(node, labels) { const left = getNode(node, "left"); -@@ -743,7 +751,7 @@ export class Interpreter { +@@ -743,7 +845,9 @@ export class Interpreter { self.predeclarePattern(declared.pattern, declared.mutable, left); const right = yield* self.evaluateExpression(getNode(node, "right")); const body = getNode(node, "body"); - const keys = self.enumerableKeys(right); + const keys = yield* self.enumerableKeys(right); ++ if (keys !== undefined) ++ self.budget.charge(keys.length / ELEMENTS_PER_STEP, node); if (keys === undefined) { throw new InterpreterRuntimeError("for...in requires a plain object, array, or tools reference. Use for...of for arrays/strings/Maps/Sets, or Object.keys(value) for a key list.", node); } -@@ -1592,7 +1600,7 @@ export class Interpreter { +@@ -833,7 +937,10 @@ export class Interpreter { + const self = this; + const attempted = Effect.matchCauseEffect(this.evaluateStatement(body), { + onFailure: (cause) => { +- if (cause.reasons.some(Cause.isInterruptReason) || Cause.squash(cause) instanceof GeneratorReturn || !handler) { ++ if (cause.reasons.some(Cause.isInterruptReason) || ++ Cause.squash(cause) instanceof GeneratorReturn || ++ Cause.squash(cause) instanceof StepBudgetExceeded || ++ !handler) { + return Effect.failCause(cause); + } + const caught = caughtErrorValue(Cause.squash(cause)); +@@ -897,6 +1004,7 @@ export class Interpreter { + for (const propertyValue of getArray(pattern, "properties")) { + const property = asNode(propertyValue, "properties"); + if (property.type === "RestElement") { ++ self.budget.charge(nativeSteps(value), property); + const rest = Object.create(null); + for (const [key, item] of Object.entries(value)) { + if (!consumed.has(key) && !isBlockedMember(key)) +@@ -946,6 +1054,7 @@ export class Interpreter { + for (const propertyValue of getArray(pattern, "properties")) { + const property = asNode(propertyValue, "properties"); + if (property.type === "RestElement") { ++ self.budget.charge(nativeSteps(source), property); + const rest = Object.create(null); + for (const [key, item] of Object.entries(source)) { + if (!consumed.has(key) && !isBlockedMember(key)) +@@ -998,6 +1107,8 @@ export class Interpreter { + if (!step.done) + rest.push(step.value); + while (!done) { ++ // Each element is one interpreter-level iterator step. ++ self.budget.step(element); + const next = yield* cursor.next; + done = next.done; + if (!done) +@@ -1037,6 +1148,7 @@ export class Interpreter { + return undefined; + } + evaluateExpression(node) { ++ this.budget.step(node); + switch (node.type) { + case "Literal": { + const regex = node.regex; +@@ -1114,7 +1226,11 @@ export class Interpreter { + } + // Array and Object construct identically with or without new, like JS. + if (name === "Array") { +- return Effect.map(this.evaluateCallArguments(argNodes), (args) => self.constructArray(args, node)); ++ return Effect.map(this.evaluateCallArguments(argNodes), (args) => { ++ const array = self.constructArray(args, node); ++ self.budget.charge(nativeSteps(array), node); ++ return array; ++ }); + } + if (name === "Object") { + return Effect.map(this.evaluateCallArguments(argNodes), (args) => self.constructObject(args, node)); +@@ -1122,6 +1238,8 @@ export class Interpreter { + if (valueConstructors.has(name)) { + return Effect.gen(function* () { + const args = yield* self.evaluateCallArguments(argNodes); ++ for (const arg of args) ++ self.budget.charge(iterationSteps(arg), node); + switch (name) { + case "Date": + return yield* self.constructDate(args, node); +@@ -1333,12 +1451,22 @@ export class Interpreter { + return boundedData(self.applyBinaryOperator(operator, lhs, rhs, node), "Binary expression result"); + }); + } ++ // Charges a walk over operands at the rate of copying them, as the walk visits every value. ++ meterWalk(node) { ++ return (visited) => this.budget.charge(visited / ELEMENTS_PER_STEP, node); ++ } + applyBinaryOperator(operator, lhs, rhs, node) { +- if (operator === "===") +- return lhs === rhs; +- if (operator === "!==") +- return lhs !== rhs; +- if (containsOpaqueReference(lhs) || containsOpaqueReference(rhs)) { ++ if (operator === "===" || operator === "!==") { ++ this.budget.charge(comparisonSteps(operator, lhs, rhs), node); ++ return operator === "===" ? lhs === rhs : lhs !== rhs; ++ } ++ // `in` reads one own key of its right-hand object, so only that object itself must be data; ++ // its contents are never read. Other operators coerce whole operands, so both are walked, ++ // and the walk is charged. ++ const meter = this.meterWalk(node); ++ if (operator === "in" ++ ? containsOpaqueReferenceMetered(lhs, meter) || isOpaqueReference(rhs) ++ : containsOpaqueReferenceMetered(lhs, meter) || containsOpaqueReferenceMetered(rhs, meter)) { + throw new InterpreterRuntimeError("Binary operators require data values.", node, "InvalidDataValue"); + } + // Null-prototype data needs explicit primitive coercion; identity and `in` retain raw objects. +@@ -1352,9 +1480,16 @@ export class Interpreter { + const bothObjects = lhs !== null && typeof lhs === "object" && rhs !== null && typeof rhs === "object"; + const l = coerceOperand(lhs); + const r = coerceOperand(rhs); ++ if (!bothObjects) ++ this.budget.charge(comparisonSteps(operator, l, r), node); + switch (operator) { +- case "+": +- return l + r; ++ case "+": { ++ // Joining strings makes a rope; whatever reads it next flattens the whole result. ++ const sum = l + r; ++ if (typeof sum === "string") ++ this.budget.charge(sum.length / CONCAT_CHARS_PER_STEP, node); ++ return sum; ++ } + case "-": + return l - r; + case "*": +@@ -1429,7 +1564,7 @@ export class Interpreter { + return !value; + if (operator === "void") + return undefined; +- if (containsOpaqueReference(value)) { ++ if (containsOpaqueReferenceMetered(value, this.meterWalk(node))) { + throw new InterpreterRuntimeError("Unary operators require data values.", node, "InvalidDataValue"); + } + const operand = value instanceof CodeModeDate +@@ -1561,8 +1696,54 @@ export class Interpreter { + return yield* self.invokeCallable(callable, args, node, callee); + }); + } +- // The single dispatch for every invocation: call expressions and callbacks share it. ++ // The single dispatch for every invocation: call expressions and callbacks share it. A native ++ // call is charged for its receiver and arguments before it runs and for its result after. + invokeCallable(callable, args, node, callee = node) { ++ if (isProgramCall(callable)) ++ return this.dispatchCallable(callable, args, node, callee); ++ return Effect.suspend(() => { ++ let input = 0; ++ if (callable instanceof IntrinsicReference) { ++ input += receiverSteps(callable.receiver, callable.name, args); ++ // Arguments of a method are charged when they are text, such as a separator or a needle. ++ for (const arg of args) ++ if (typeof arg === "string") ++ input += nativeSteps(arg); ++ } ++ else { ++ // Object, Array, JSON and coercion functions read their whole arguments, except ++ // Object.assign's target, which it only writes, and functions that read one key. ++ const qualified = callable instanceof GlobalMethodReference ? `${callable.namespace}.${callable.name}` : undefined; ++ const constant = constantGlobalMethods.has(qualified); ++ for (const [index, arg] of args.entries()) { ++ if (constant ? typeof arg !== "string" : qualified === "Object.assign" && index === 0) ++ continue; ++ input += nativeSteps(arg); ++ } ++ } ++ this.budget.charge(input, node); ++ return Effect.map(this.dispatchCallable(callable, args, node, callee), (result) => { ++ // Only fresh results are charged. A result the program already holds (its receiver, ++ // an argument, or an element a method returns) was paid for when it was built. ++ if (!isObjectValue(result)) { ++ this.budget.charge(nativeSteps(result), node); ++ return result; ++ } ++ if (callable instanceof IntrinsicReference) { ++ if (elementReturningMethods.has(callable.name)) ++ return result; ++ // sort, reverse and fill return their array: charged again for the pass. ++ if (result === callable.receiver && !Array.isArray(result)) ++ return result; ++ } ++ if (args.includes(result)) ++ return result; ++ this.budget.charge(nativeSteps(result), node); ++ return result; ++ }); ++ }); ++ } ++ dispatchCallable(callable, args, node, callee) { + const self = this; + return Effect.gen(function* () { + if (callable instanceof ToolReference) { +@@ -1592,12 +1773,12 @@ export class Interpreter { if (callable.namespace === "console") return self.invokeConsole(callable.name, args, node); if (callable.namespace === "Object" && args[0] instanceof ToolReference) { @@ -749,7 +1362,13 @@ index 8c1b549..3575409 100644 } if (callable.namespace === "Object" && objectMethodsPreservingIdentity.has(callable.name)) { if (callable.name === "fromEntries") -@@ -1665,7 +1673,7 @@ export class Interpreter { + return yield* invokeObjectFromEntries(self.runner, args[0], node); +- return invokeGlobalMethod(callable, args, node); ++ return invokeGlobalMethod(callable, args, node, self.meterWalk(node)); + } + if (callable.namespace === "Array" && callable.name === "from") { + return yield* invokeArrayFrom(self.runner, args, node); +@@ -1665,7 +1846,7 @@ export class Interpreter { } invokeObjectMethodOnTools(name, ref, node) { if (name === "keys") { @@ -758,7 +1377,24 @@ index 8c1b549..3575409 100644 } throw new InterpreterRuntimeError(`Object.${name}(...) cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, node, "InvalidDataValue"); } -@@ -1730,7 +1738,7 @@ export class Interpreter { +@@ -1683,6 +1864,7 @@ export class Interpreter { + const argNode = asNode(arg, `arguments[${index}]`); + if (argNode.type === "SpreadElement") { + const spread = yield* self.evaluateExpression(getNode(argNode, "argument")); ++ self.budget.charge(iterationSteps(spread), argNode); + const cursor = yield* self.syncIterator(spread, argNode); + if (cursor === undefined) + throw new InterpreterRuntimeError("Spread arguments require a synchronous iterable.", argNode).as("TypeError"); +@@ -1701,7 +1883,7 @@ export class Interpreter { + }); + } + invokeFunction(fn, args) { +- const invocation = new Interpreter(this.executeTool, this.invokeSearch, this.toolKeys, this.promises, this.logs); ++ const invocation = new Interpreter(this.executeTool, this.invokeSearch, this.toolKeys, this.promises, this.logs, this.budget); + invocation.scopes = new ScopeStack([...fn.capturedScopes, new Map()]); + const run = Effect.gen(function* () { + // Seed all parameters first so defaults cannot fall through to same-named outer bindings. +@@ -1730,7 +1912,7 @@ export class Interpreter { return run; // The initial yield assigns the promise before the body can self-resolve. const box = {}; @@ -767,7 +1403,7 @@ index 8c1b549..3575409 100644 box.promise = promise; return promise; }); -@@ -1739,8 +1747,17 @@ export class Interpreter { +@@ -1739,8 +1921,17 @@ export class Interpreter { const state = { started: false, completed: false, draining: false, pending: [], pendingIndex: 0 }; invocation.generatorState = state; invocation.generatorAsync = asynchronous; @@ -786,7 +1422,7 @@ index 8c1b549..3575409 100644 if (!asynchronous && state.active) { return Effect.fail(new InterpreterRuntimeError("Generator is already running.", node).as("TypeError")); } -@@ -1749,11 +1766,11 @@ export class Interpreter { +@@ -1749,11 +1940,11 @@ export class Interpreter { state.completed = true; state.pending.push(request); if (state.draining) @@ -800,7 +1436,7 @@ index 8c1b549..3575409 100644 } if (state.completed) { if (kind === "throw") -@@ -1766,11 +1783,19 @@ export class Interpreter { +@@ -1766,11 +1957,19 @@ export class Interpreter { return Effect.fail(new ProgramThrow(value)); return Effect.succeed({ value, done: true }); } @@ -821,7 +1457,7 @@ index 8c1b549..3575409 100644 } if (!state.started) { state.started = true; -@@ -1789,10 +1814,10 @@ export class Interpreter { +@@ -1789,10 +1988,10 @@ export class Interpreter { yield* invocation.completeGeneratorRequests(state, asynchronous); state.completed = true; }); @@ -835,7 +1471,7 @@ index 8c1b549..3575409 100644 return generator; } completeGeneratorRequests(state, asynchronous) { -@@ -1820,7 +1845,7 @@ export class Interpreter { +@@ -1820,7 +2019,7 @@ export class Interpreter { if (next) return Effect.succeed(next); state.available = Deferred.makeUnsafe(); @@ -844,8 +1480,60 @@ index 8c1b549..3575409 100644 } dequeueGeneratorRequest(state) { const request = state.pending[state.pendingIndex]; +@@ -1952,6 +2151,7 @@ export class Interpreter { + if (typeof spread !== "object" || Array.isArray(spread) || isRuntimeReference(spread)) { + throw new InterpreterRuntimeError("Object spread requires a data object.", property, "InvalidDataValue"); + } ++ self.budget.charge(nativeSteps(spread), property); + for (const [key, value] of Object.entries(spread)) { + if (isBlockedMember(key)) + throw new InterpreterRuntimeError(`Property '${key}' is not available.`, property); +@@ -2004,6 +2204,7 @@ export class Interpreter { + const element = asNode(elementValue, "elements"); + if (element.type === "SpreadElement") { + const spread = yield* self.evaluateExpression(getNode(element, "argument")); ++ self.budget.charge(iterationSteps(spread), element); + const cursor = yield* self.syncIterator(spread, element); + if (cursor === undefined) + throw new InterpreterRuntimeError("Array spread requires a synchronous iterable.", element).as("TypeError"); +@@ -2036,9 +2237,14 @@ export class Interpreter { + output += rawValue.cooked; + if (index < expressions.length) { + const raw = yield* self.evaluateExpression(asNode(expressions[index], "expressions")); ++ // An array or object is copied and then joined. ++ if (Array.isArray(raw) || isPlainData(raw)) ++ self.budget.charge(2 * nativeSteps(raw), node); + output += coerceToString(boundedData(raw, "Template interpolation")); + } + } ++ // Like string `+`: the result is a rope that the next read copies whole. ++ self.budget.charge(output.length / CONCAT_CHARS_PER_STEP, node); + return output; + }); + } +@@ -2312,7 +2518,9 @@ export class Interpreter { + if (typeof key !== "number" || parseArrayIndex(key) === undefined) { + throw new InterpreterRuntimeError("Array assignment index must be a valid array index.", node, "InvalidDataValue"); + } +- rejectCircularInsertion(target, next, "Array assignment result", node); ++ // Storing the value a slot already holds cannot change the graph, so it skips the walk. ++ if (!(Object.hasOwn(target, key) && target[key] === next)) ++ rejectCircularInsertion(target, next, "Array assignment result", node, undefined, this.meterWalk(node)); + target[key] = next; + return; + } +@@ -2337,7 +2545,8 @@ export class Interpreter { + return; + } + const target = reference.target; +- rejectCircularInsertion(target, next, "Object assignment result", node); ++ if (!(Object.hasOwn(target, key) && target[key] === next)) ++ rejectCircularInsertion(target, next, "Object assignment result", node, undefined, this.meterWalk(node)); + Reflect.set(target, key, next); + } + toPropertyKey(value, node) { diff --git a/dist/namespace.d.ts b/dist/namespace.d.ts -index e464076..220d157 100644 +index e4640762c1139d5a864f966035446f5d890040f0..220d15700f5e0f2d4dbee3f5500875ccbf3cb86d 100644 --- a/dist/namespace.d.ts +++ b/dist/namespace.d.ts @@ -1,9 +1,21 @@ @@ -877,7 +1565,7 @@ index e464076..220d157 100644 +/** Declares a namespace whose tools are loaded when a program first reaches it. */ +export declare const lazy: (options: LazyOptions) => Namespace; diff --git a/dist/namespace.js b/dist/namespace.js -index 93db240..e0e44b0 100644 +index 93db2408fe35817f6d53add795822e91dadcedfd..e0e44b0e8c823133cc390d3a12d7b87873006236 100644 --- a/dist/namespace.js +++ b/dist/namespace.js @@ -5,3 +5,10 @@ export const make = (options) => ({ @@ -892,7 +1580,7 @@ index 93db240..e0e44b0 100644 + load: options.load, +}); diff --git a/dist/openapi/index.js b/dist/openapi/index.js -index 1702476..2425925 100644 +index 1702476510e1019a2a2b6b7e14423c84a350e8c9..2425925a7bd394917d57f36b84e6f0fa29e5f79e 100644 --- a/dist/openapi/index.js +++ b/dist/openapi/index.js @@ -1,4 +1,4 @@ @@ -902,7 +1590,7 @@ index 1702476..2425925 100644 import { invoke } from "./runtime.js"; import { componentDefinitions, hasDirectionalSchemas, inputSchema, isRecord, methods, nonEmptyString, operationInput, operationOutput, operationPath, operationSecurityRequirements, securityRequirements, securitySchemes, specServerUrl, validateBaseUrl, } from "./spec.js"; diff --git a/dist/openapi/runtime.d.ts b/dist/openapi/runtime.d.ts -index 86ae0c3..2da6eeb 100644 +index 86ae0c3d2183e89bc3d83c1548d860bc8d69c78b..2da6eeb1b50263f69bc593a4fe0eb0cc4cc3a1d3 100644 --- a/dist/openapi/runtime.d.ts +++ b/dist/openapi/runtime.d.ts @@ -1,4 +1,4 @@ @@ -912,7 +1600,7 @@ index 86ae0c3..2da6eeb 100644 import type { Plan } from "./types.js"; export declare const invoke: (plan: Plan, input: unknown) => Effect.Effect; diff --git a/dist/openapi/runtime.js b/dist/openapi/runtime.js -index 0fa6088..4e2d042 100644 +index 0fa6088d164442dfc9c41fff6ab71357f7386317..4e2d042f4f2b20b800bd26cd270014eee6b8dac6 100644 --- a/dist/openapi/runtime.js +++ b/dist/openapi/runtime.js @@ -1,5 +1,5 @@ @@ -923,7 +1611,7 @@ index 0fa6088..4e2d042 100644 import { isRecord, own } from "./spec.js"; const decodeJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown)); diff --git a/dist/openapi/types.d.ts b/dist/openapi/types.d.ts -index 52d2cb7..80e1db3 100644 +index 52d2cb7dbb75f6998a9dd2a4ad4e8a780ba9ceb0..80e1db35c82b1434144608a797077030f2c386b1 100644 --- a/dist/openapi/types.d.ts +++ b/dist/openapi/types.d.ts @@ -1,5 +1,5 @@ @@ -934,15 +1622,48 @@ index 52d2cb7..80e1db3 100644 /** A parsed OpenAPI 3.x document. YAML must be parsed by the host. */ export type Document = Record; diff --git a/dist/openapi/types.js b/dist/openapi/types.js -index bc386d2..483cc3f 100644 +index bc386d2cd78bffa1bd1f76bf98509183f61c57d1..483cc3fd953ab434bf0209462d9a88ddce404fb0 100644 --- a/dist/openapi/types.js +++ b/dist/openapi/types.js @@ -1,2 +1,2 @@ import { Effect } from "effect"; -import { HttpClient } from "effect/unstable/http"; +import { HttpClient } from "effect/http"; +diff --git a/dist/stdlib/object.d.ts b/dist/stdlib/object.d.ts +index 5d8d15cd307e58e08f9b3757d31eee35501d72b7..7ce4a7e5491c6c0ce1f0d26ee49f82fe438fac06 100644 +--- a/dist/stdlib/object.d.ts ++++ b/dist/stdlib/object.d.ts +@@ -3,5 +3,5 @@ import { type AstNode } from "../interpreter/model.js"; + import { type SyncIteratorRunner } from "../interpreter/iterator.js"; + export declare const objectMethodsPreservingIdentity: Set; + export declare const objectStatics: Set; +-export declare const invokeObjectMethod: (name: string, args: Array, node: AstNode) => unknown; ++export declare const invokeObjectMethod: (name: string, args: Array, node: AstNode, meter?: (visited: number) => void) => unknown; + export declare const invokeObjectFromEntries: (runner: SyncIteratorRunner, source: unknown, node: AstNode) => Effect.Effect, unknown, R>; +diff --git a/dist/stdlib/object.js b/dist/stdlib/object.js +index fe2d225693f15eece4838b62a22fab7b43351da1..6e874045f38719d8032186938d55435388718814 100644 +--- a/dist/stdlib/object.js ++++ b/dist/stdlib/object.js +@@ -7,7 +7,7 @@ import { boundedData, coerceToString } from "./value.js"; + import { preserveConsumerError } from "../interpreter/iterator.js"; + export const objectMethodsPreservingIdentity = new Set(["assign", "values", "entries", "fromEntries"]); + export const objectStatics = new Set(["keys", "values", "entries", "hasOwn", "is", "assign", "fromEntries", "groupBy"]); +-export const invokeObjectMethod = (name, args, node) => { ++export const invokeObjectMethod = (name, args, node, meter) => { + const requireObject = () => { + const input = args[0]; + if (Array.isArray(input)) +@@ -50,7 +50,7 @@ export const invokeObjectMethod = (name, args, node) => { + const guardedSet = (key, item) => { + if (typeof key === "string" && isBlockedMember(key)) + throw new InterpreterRuntimeError(`Property '${key}' is not available.`, node); +- rejectCircularInsertion(out, item, "Object.assign result", node, seen); ++ rejectCircularInsertion(out, item, "Object.assign result", node, seen, meter); + if (!Reflect.set(out, key, item)) + throw new InterpreterRuntimeError(`Object.assign could not assign property '${String(key)}'.`, node).as("TypeError"); + }; diff --git a/dist/tool-error.d.ts b/dist/tool-error.d.ts -index 1adc0b7..9489b77 100644 +index 1adc0b7af422b5ca0ffe5b79abc7a14ceccb7627..9489b77bae4a0833ee2ff82811a859b22621775f 100644 --- a/dist/tool-error.d.ts +++ b/dist/tool-error.d.ts @@ -2,10 +2,11 @@ import { Schema } from "effect"; @@ -960,7 +1681,7 @@ index 1adc0b7..9489b77 100644 +export declare const toolError: (message: string, cause?: unknown, properties?: Readonly>) => ToolError; export {}; diff --git a/dist/tool-error.js b/dist/tool-error.js -index 6a8d1d2..492ad48 100644 +index 6a8d1d2243d9fc43281a19793e296dfa1556ee0f..492ad4845a7cc5023ee9e23b2eb264ce78a1f279 100644 --- a/dist/tool-error.js +++ b/dist/tool-error.js @@ -3,7 +3,16 @@ import { Schema } from "effect"; @@ -983,7 +1704,7 @@ index 6a8d1d2..492ad48 100644 + ...(properties === undefined ? {} : { properties }), +}); diff --git a/dist/tool-runtime.d.ts b/dist/tool-runtime.d.ts -index 7b04d8e..98c08f7 100644 +index 7b04d8e485966e56e673286c9ac9415ac5f60a7f..98c08f773aeb548386a0b6d48ff26c72445d543c 100644 --- a/dist/tool-runtime.d.ts +++ b/dist/tool-runtime.d.ts @@ -4,7 +4,12 @@ export type Services = ServicesOf; @@ -1047,7 +1768,7 @@ index 7b04d8e..98c08f7 100644 +export declare const make: (tools: Tools, maxToolCalls: number | undefined, searchIndex: () => ReadonlyArray, hooks?: ToolCallHooks, search?: Search) => ToolRuntime; export * as ToolRuntime from "./tool-runtime.js"; diff --git a/dist/tool-runtime.js b/dist/tool-runtime.js -index f649a9a..8ccc826 100644 +index f649a9a04eea271c852510a4deef6efa7b6252bc..8ccc826028e82460cfe2227170f6cfea1c05d75d 100644 --- a/dist/tool-runtime.js +++ b/dist/tool-runtime.js @@ -1,5 +1,5 @@ diff --git a/patches/codemode-step-budget.md b/patches/codemode-step-budget.md new file mode 100644 index 0000000000..16a1cca4f1 --- /dev/null +++ b/patches/codemode-step-budget.md @@ -0,0 +1,185 @@ +# Codemode step budget + +`@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch` adds `maxSteps` to `ExecutionLimits`: the number +of statements and expressions one program may evaluate, across all of its functions, promise jobs +and generators. Executor passes `McpLimits.maxSteps` (5,000,000 by default; self-host and local +read `EXECUTOR_MCP_MAX_STEPS`). + +The interpreter runs on the product's own thread, about a thousand times slower than native code. +Its only budgets were wall time (`timeoutMs`, five minutes) and tool calls. On Nov 18 2026 one +production program spent about 203 s in the interpreter with no tool calls, inside its five +minutes, and the product isolate stayed near 100% CPU throughout. Effect preempts the program's +fiber every 2,048 operations, so other requests still ran, but slowly, and clients gave up. + +`StepBudget` (`interpreter/model.js`) is shared by every `Interpreter` of one execution, as +`invokeFunction` creates one per call. `evaluateStatement` and `evaluateExpression` count one step +each before they dispatch. A tool call, an approval or a timer evaluates nothing while it waits, +so only interpreter work counts. Steps, not CPU time: workerd's clock does not advance while code +runs, and a count gives the same answer on every host and under any load. + +Past the limit, every step throws `StepBudgetExceeded`, an `InterpreterRuntimeError` of kind +`TimeoutExceeded` whose message names the budget and asks for heavy computation to move out of +the program. `try` hands it on without running `catch`, and because each later step throws again, +a `catch`, `finally` or promise reaction cannot keep the program running. Execute reports it with +the line it stopped at, and keeps that message instead of its wall-time timeout message. + +Counting adds no measurable time: a 300,000-iteration loop took a median of 1,356-1,372 ms patched +against 1,331-1,363 ms unpatched in Node (7 runs each). + +The default comes from the incident. Measured on one host, the interpreter runs about 550,000 +steps a second in workerd (the runtime of the self-host image and Cloud), about 2 million in Node +and about 0.9 million in the Bun self-host e2e server. Ordinary programs spend 0.1-0.5 s before +their first tool call; the runaway spent 203 s, about 110 million steps in workerd. 5,000,000 steps +is about 9 s in workerd, near the geometric middle: 18 times the slowest ordinary program and 22 +times less than the runaway. Counted programs: a 100,000-iteration `for` loop is 800,000 steps, +filtering and mapping 10,000 records 177,000, the `mcp-execute-concurrency` reporter program +about 400,000 and its 1,001 x 300 loop reference about 1.5 million. + +## Native calls and operators + +A native method can do far more work than the step that calls it: `s.split("")` on a 1 MB string +is one step, but returns a million-element array that codemode then copies. Before native calls +were charged, a loop of them ran 83 s inside a 5,000,000-step budget. `invokeCallable` is the +one place every call goes through, so it charges there. Calls that run program code or wait on the +host (tools, program functions, promise and generator methods, `tools.search`) count their own +steps and are not charged. Every other call is charged with `StepBudget.charge`: + +- before it runs, for its input: the receiver of a method and the string arguments of a method; + every argument of a global function (`JSON`, `Object`, `Array`, `Math` and the like), except + `Object.assign`'s target, which it only writes, and `Object.hasOwn` and `Array.isArray`, which + read one key or the type. This stops a call whose input is already too large before it starts. +- after it returns, for its result, but only when the result is new. A result the program + already holds is not charged: the method's own receiver (`Map.set`), one of its arguments + (`Object.assign`), or an element or accumulator (`Map.get`, `at`, `pop`, `shift`, `find`, + `findLast`, `reduce`). `sort`, `reverse` and `fill` return their array and are charged for it + again, for the pass. Before this, `g.get(k)` was charged for the whole group each time, and + grouping 50,000 rows into 10 or 50 groups stopped at the budget in under a second. +- spread arguments, array spread and object spread, and the arguments and result of `new`, at the + same rate. Spread and `new Set` or `new Map` take a string one character at a time, so there a + string costs one step per 2 characters. + +Operators and patterns that read a whole value are charged too: + +- Binary operators other than `===` and `!==`, and unary `+`, `-` and `~`, walk their operands to + reject interpreter references before coercing them. The walk is charged as it goes, one step + per 2 values visited, so it stops partway once the budget is spent. Primitives skip it. +- `in` reads one own key, so it only checks that its right-hand object is data and does not walk + it. Before this, `id in index` walked the whole index on each check, and merging 20,000 records + into a 20,000-record index ran to its wall time. It no longer throws when the object holds a + function somewhere inside; it still does when the object itself is one. +- Comparing two strings is charged one step per 256 characters it may read: equal lengths for + equality, the shorter length for `<`, `<=`, `>` and `>=`. +- `+` that yields a string is charged one step per 4,096 characters of its result, and so are + `+=` and a template literal. The engine joins strings as a rope, so the join itself is cheap, but the next read of the + result copies all of it: `t = s + i` on a 1 MB `s` followed by `t.charCodeAt(…)` copied 1 MB per + step and ran to the wall time; it now stops in about 1.5 s. The rate is per 4,096 rather than + 256 because building output with `out += line` is charged its whole length on every append, + which grows with the square of the line count: a 554 KB report of 5,000 lines costs 0.43 + million steps, and about 18,000 lines (2.2 MB) spend the default budget. Collect lines in an + array and `join` them instead, which is charged once. +- Storing a value walks it to reject cycles: assignment to a property or index, `push`, + `unshift`, `splice`, `fill` and `Object.assign`. `switch` walks its discriminant and case + values to reject interpreter references. Both walks are charged as they go, one step per 2 + values visited. The store walk also remembers, in a `WeakSet`, every object it finds holding no + objects, and stops there next time; storing an object into one removes it, and every store into + existing data runs this walk first, so the walk still finds every cycle. The one store that + skips it is a property or index assignment of the value the slot already holds, which cannot + change the graph: `g[k] = g[k] || []` followed by `g[k].push(row)` walked the whole group, every + row in it, on every row, and grouping 50,000 row objects stopped at the budget in about 2 s. Before this, attaching + one shared `meta` of 50,000 columns to each of 20,000 rows walked all the columns on each store, + uncharged, and ran for minutes; it now takes 0.3 s. Grouping 50,000 rows into an object with + `a = g[k] || []; a.push(v); g[k] = a` walked each group on each store; it now takes 0.7 s. +- A template interpolating an array or object is charged for copying and joining it. +- Rest patterns: an object rest is charged for the object, an array rest one step per element it + takes. `for...in` is charged for its key list. + +Data costs one step per 2 array elements, object keys or `Map` and `Set` entries, and one per 256 +string characters. An array of records or arrays counts each element as wide as the widest of up +to 8 evenly spaced elements, up to 64, because copying a result walks every field. Other values +(numbers, functions, class instances) cost nothing. Some methods are charged differently: +methods whose work is bounded by their result (`slice`, `substring`, `at`, `push`, `pop`, +`concat` and similar) charge only their result, so loops that call them stay linear; `indexOf`, +`includes` and `lastIndexOf` charge one step per 512 elements or characters they can scan from +their start position; `normalize` and `localeCompare` charge 8 times their receiver. + +One native call still runs to its end before its result is charged; codemode cannot interrupt it. +Its input is charged before it runs, so the largest single call a program can make is bounded by +data it already paid to build, and the next step after an overrun stops the program. + +### Not charged yet + +These still run work the budget does not see, and are left for a follow-up: + +- A backtracking regular expression. Its input is small, but `/^(a+)+$/` on a 25-character string + can take tens of milliseconds per match, and a loop of 1,000 such matches runs to the wall time + (44 s in Node, inside its 60 s). Codemode has no backtracking limit to set, and refusing nested + quantifiers would refuse ordinary patterns. +- Walks that reject interpreter references and are not metered: `++` and `--` on a value held in + a property (`o.x++` with `o.x` a million-element array walks it every time), the entries + `Object.fromEntries` reads, and the non-string arguments of string methods (`"a".padEnd(1, big)`). + A loop of each of these with a million-element array runs to its 30 s wall time. + `console.table` and `Object.is` walk too, but are already stopped by the charge for their + arguments. +- A shared value made of many small records is walked in full on every store, because only + objects holding no objects are remembered: attaching a 1,000-record `meta` to each of 20,000 + rows stops at the budget in about 2 s. +- Not a hole, but worth knowing: grouping with `g[k] = (g[k] || []).concat([v])` copies the group + on every row and is charged for it, so 50,000 rows in 10 groups stop at the budget in under a + second. `push` into the group instead. + +Hot loops cost about what they did on `main`; primitives skip the operand walk. Medians of 7 runs +in Node 24 (base is `main`'s patch, previous is the version before store walks and string `+` +were charged): + +| Loop (100,000-300,000 iterations) | base ms | previous ms | now ms | +| ----------------------------------------- | ------: | ----------: | -----: | +| `s += i % 7` | 1,370 | 1,291 | 1,283 | +| comparisons and `in` | 884 | 790 | 780 | +| `push`, `Math.max` and `slice` every turn | 1,667 | 1,710 | 1,713 | +| `o.x = {v: i, t: [i]}` and `a[0] = i` | 1,109 | 1,134 | 1,129 | +| `s = 'key-' + i + '-' + (i % 7)` | 768 | 703 | 714 | + +The rates were measured with each workload looped under the default 5,000,000-step budget until it +stopped, on one host (wall seconds of one program; in workerd, one request with nothing else +running). Most setups build a million-element array with `Array.from` and a mapper, a million +callback calls; callbacks cost about twice a loop step in workerd, so that setup alone takes up +to 7 s there. + +| Workload | Node 24 s | Bun s | workerd s | +| ------------------------------------------------ | --------: | ----: | --------: | +| pure interpreter loop | 1.68 | 0.78 | 8.53 | +| `split("")` of 1 MB (round 0 PoC) | 1.23 | 2.80 | 0.98 | +| `JSON.stringify` 20k records + `includes` | 2.74 | 0.90 | 2.30 | +| `JSON.parse` of 1.3 MB | 1.77 | 0.98 | 1.84 | +| `join` of 1M elements | 5.61 | 4.18 | 11.61 | +| `Object.keys` of 100k keys | 1.71 | 1.30 | 2.64 | +| spread of 1M elements | 5.15 | 2.68 | 14.87 | +| `replaceAll` on 1 MB | 3.92 | 3.30 | 8.03 | +| `JSON.stringify` of `[0, ...20k 200-key records]` | 6.76 | 3.73 | 6.08 | +| `includes` on 1M strings | 7.16 | 5.95 | 15.17 | +| `normalize` of 1M characters | 1.19 | 2.80 | 1.24 | +| `"zz" in` a 200k-key object | 2.56 | 1.25 | 8.68 | +| `a == "x"` on 1M elements | 6.02 | 4.76 | 11.96 | +| `"" + a` on 1M elements | 5.96 | 4.60 | 11.84 | +| template of 1M elements | 5.25 | 3.42 | 11.18 | +| `===` of two 4 MB strings | 0.05 | 0.05 | 0.05 | +| `<` of two 4 MB strings | 0.04 | 0.07 | 0.07 | +| `[x, ...r] = ` 1M elements | 4.92 | 2.66 | 13.16 | +| `{k0, ...r} = ` 100k keys | 4.07 | 1.28 | 4.24 | +| `for...in` 200k keys, then `break` | 1.91 | 0.86 | 4.34 | +| `new Set` of a 1M-character string | 0.59 | 0.35 | 5.47 | + +On a quiet host every workload above stops within about 7.5 s in Node and Bun and about 15 s in +workerd, where the interpreted loop alone takes 8.5 s; the ones above 8.5 s spend most of it +building their input with callbacks. Under load the same programs take longer: the reviewer, with +other programs running, saw 8.3 to 9.6 s in Bun, and a loop that sorts a million-element array in +place with a comparator, a callback per comparison, takes 10 to 16 s. These stop at the budget all +the same; what they take is the steps per second the host gives them. Ordinary programs fit, with the steps they are charged: grouping 50,000 rows with +`Map.get` 1.48 million whatever the number of groups; merging 20,000 records with `in` 0.85 +million (with `Object.hasOwn` 0.63 million); filtering and mapping 20,000 records 0.49 million; +splitting and matching 20,000 log lines 0.48 million; and a comparator `sort` of 50,000 objects +3.9 million (17 s in workerd, where every comparison is a callback). A comparator `sort` of +100,000 objects needs about 20,000,000 steps, so the default stops it; self-host and local raise +the budget with `EXECUTOR_MCP_MAX_STEPS`. + +Remove the hunks when a codemode release has a step or CPU budget.