From f18dcc5e55d93f550bb0ea868a96fd4dc4953b51 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Wed, 18 Nov 2026 16:55:49 -0800 Subject: [PATCH 1/5] fix(mcp): stop execute programs at a CodeMode step budget A program that only computes ran on the product's own thread until its five-minute wall-time budget. On Nov 18 one execute spent 203 s in the interpreter with no tool calls and kept production's product isolate near 100% CPU. The CodeMode patch adds ExecutionLimits.maxSteps: statements and expressions evaluated across the whole program. Waiting on tools evaluates nothing, so only interpreter work counts. Past the limit every step throws a TimeoutExceeded the program's own catch, finally or reactions cannot stop. McpLimits gains maxSteps (default 5,000,000, about 9 s in workerd); self-host and local read EXECUTOR_MCP_MAX_STEPS. Execute keeps CodeMode's message, which names the budget and asks for heavy work to move out of the program, and tags the span executor.timeout.budget=steps. --- apps/hosted/self-host/src/mcp.ts | 3 + apps/local/server/README.md | 1 + apps/local/server/src/contracts/config.ts | 3 + e2e/ci-selection.ts | 1 + e2e/test-plan.ts | 11 + e2e/tests/mcp-execute-step-budget.spec.ts | 183 +++++++++++++ packages/mcp/src/contracts/execute.ts | 10 + packages/mcp/src/implementation/execute.ts | 21 +- ...encode-ai%2Fcodemode@0.0.0-dev-19272.patch | 253 ++++++++++++++---- patches/codemode-step-budget.md | 42 +++ 10 files changed, 479 insertions(+), 49 deletions(-) create mode 100644 e2e/tests/mcp-execute-step-budget.spec.ts create mode 100644 patches/codemode-step-budget.md 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..8a8c77aa26 100644 --- a/apps/local/server/README.md +++ b/apps/local/server/README.md @@ -64,6 +64,7 @@ are resolved from the process working directory. `bun run with:local …` loads | `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` | Statements and expressions one execute program may evaluate, not counting time spent waiting on tools; past it the program fails with `TimeoutExceeded`. Defaults to `5000000`. | | `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`. | 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..131b9149c3 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 at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs 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..46b3c3d794 --- /dev/null +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -0,0 +1,183 @@ +/** + * 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. + */ +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); +}`; + +/** + * 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", + ]); + + 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); + }).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..922c1c2223 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -526,12 +526,22 @@ 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)), + /** + * Statements and expressions one program may evaluate. Waiting on tool calls and approvals + * evaluates nothing, so this bounds the CPU a program spends in the interpreter on the server's + * own thread, which `timeoutMs` alone does not. + */ + 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). 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..fa3c43625d 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,7 +196,7 @@ 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..d5b7dfca9c0020d63c828d776e044061411ec2d1 100644 --- a/dist/interpreter/methods.d.ts +++ b/dist/interpreter/methods.d.ts @@ -6,6 +6,8 @@ export type CallbackRunner = { @@ -168,8 +208,63 @@ index db276e3..d5b7dfc 100644 }; export type SupportedCallback = CodeModeFunction | CoercionFunction | UriFunction | PromiseCapabilityFunction | GlobalMethodReference | JsonMethodReference | IntrinsicReference | ErrorConstructorReference | GlobalNamespace | PromiseNamespace; export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback; +diff --git a/dist/interpreter/model.d.ts b/dist/interpreter/model.d.ts +index ed2accf68c9f24fa26154e266617ef587340445a..6b0470af7409e5b43b0030e3010c12c6b5b6955c 100644 +--- a/dist/interpreter/model.d.ts ++++ b/dist/interpreter/model.d.ts +@@ -136,6 +136,17 @@ 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; ++} + 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..37b7e94df0318d385fcf74a3f125f7f41eb586c0 100644 +--- a/dist/interpreter/model.js ++++ b/dist/interpreter/model.js +@@ -141,6 +141,28 @@ 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); ++ } ++} + 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 +316,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"; @@ -594,15 +689,27 @@ index 996f9a5..55843d5 100644 const box = {}; const body = Effect.gen(function* () { diff --git a/dist/interpreter/runtime.d.ts b/dist/interpreter/runtime.d.ts -index bd1bc70..3ad5894 100644 +index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..1bd8c078b0dd2399206284f42052b5efce50012e 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; @@ -612,19 +719,40 @@ index bd1bc70..3ad5894 100644 private evaluateBlock; private createFunction; diff --git a/dist/interpreter/runtime.js b/dist/interpreter/runtime.js -index 8c1b549..3575409 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1f4dee1e2 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js -@@ -154,6 +154,8 @@ export class Interpreter { +@@ -1,6 +1,6 @@ + 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"; +@@ -147,6 +147,7 @@ export class Interpreter { + toolKeys; + logs; + promises; ++ budget; + generatorState; + generatorAsync = false; + runner = { +@@ -154,9 +155,12 @@ 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), }; - 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 +227,28 @@ export class Interpreter { return value; }).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop()))); } @@ -654,9 +782,11 @@ 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" }); +@@ -510,7 +520,7 @@ export class Interpreter { const close = () => iterator ? self.closeIterator(iterator, node, awaiting) : awaiting @@ -665,7 +795,7 @@ index 8c1b549..3575409 100644 : (cursor?.close ?? Effect.void); let assignment; if (left.type !== "VariableDeclaration" && -@@ -656,7 +663,7 @@ export class Interpreter { +@@ -656,7 +666,7 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(iterator.next, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -674,7 +804,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 +675,7 @@ export class Interpreter { })); if (!Exit.isSuccess(captured)) { if (awaiting) @@ -683,7 +813,7 @@ index 8c1b549..3575409 100644 return yield* Effect.failCause(captured.cause); } return { -@@ -681,7 +688,7 @@ export class Interpreter { +@@ -681,7 +691,7 @@ export class Interpreter { ? new GeneratorMethodReference(iterator.iterator, "return") : iterator.iterator.return; if (close === undefined || close === null) @@ -692,7 +822,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 +702,13 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(method, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -708,7 +838,7 @@ index 8c1b549..3575409 100644 return yield* Effect.failCause(captured.cause); } if (awaiting) -@@ -720,17 +727,18 @@ export class Interpreter { +@@ -720,17 +730,18 @@ export class Interpreter { return value; throw new InterpreterRuntimeError(`${context} must be a function.`, node).as("TypeError"); } @@ -731,7 +861,7 @@ index 8c1b549..3575409 100644 } evaluateForInStatement(node, labels) { const left = getNode(node, "left"); -@@ -743,7 +751,7 @@ export class Interpreter { +@@ -743,7 +754,7 @@ export class Interpreter { self.predeclarePattern(declared.pattern, declared.mutable, left); const right = yield* self.evaluateExpression(getNode(node, "right")); const body = getNode(node, "body"); @@ -740,7 +870,27 @@ index 8c1b549..3575409 100644 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 +844,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)); +@@ -1037,6 +1051,7 @@ export class Interpreter { + return undefined; + } + evaluateExpression(node) { ++ this.budget.step(node); + switch (node.type) { + case "Literal": { + const regex = node.regex; +@@ -1592,7 +1607,7 @@ export class Interpreter { if (callable.namespace === "console") return self.invokeConsole(callable.name, args, node); if (callable.namespace === "Object" && args[0] instanceof ToolReference) { @@ -749,7 +899,7 @@ index 8c1b549..3575409 100644 } if (callable.namespace === "Object" && objectMethodsPreservingIdentity.has(callable.name)) { if (callable.name === "fromEntries") -@@ -1665,7 +1673,7 @@ export class Interpreter { +@@ -1665,7 +1680,7 @@ export class Interpreter { } invokeObjectMethodOnTools(name, ref, node) { if (name === "keys") { @@ -758,7 +908,16 @@ 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 { +@@ -1701,7 +1716,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 +1745,7 @@ export class Interpreter { return run; // The initial yield assigns the promise before the body can self-resolve. const box = {}; @@ -767,7 +926,7 @@ index 8c1b549..3575409 100644 box.promise = promise; return promise; }); -@@ -1739,8 +1747,17 @@ export class Interpreter { +@@ -1739,8 +1754,17 @@ export class Interpreter { const state = { started: false, completed: false, draining: false, pending: [], pendingIndex: 0 }; invocation.generatorState = state; invocation.generatorAsync = asynchronous; @@ -786,7 +945,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 +1773,11 @@ export class Interpreter { state.completed = true; state.pending.push(request); if (state.draining) @@ -800,7 +959,7 @@ index 8c1b549..3575409 100644 } if (state.completed) { if (kind === "throw") -@@ -1766,11 +1783,19 @@ export class Interpreter { +@@ -1766,11 +1790,19 @@ export class Interpreter { return Effect.fail(new ProgramThrow(value)); return Effect.succeed({ value, done: true }); } @@ -821,7 +980,7 @@ index 8c1b549..3575409 100644 } if (!state.started) { state.started = true; -@@ -1789,10 +1814,10 @@ export class Interpreter { +@@ -1789,10 +1821,10 @@ export class Interpreter { yield* invocation.completeGeneratorRequests(state, asynchronous); state.completed = true; }); @@ -835,7 +994,7 @@ index 8c1b549..3575409 100644 return generator; } completeGeneratorRequests(state, asynchronous) { -@@ -1820,7 +1845,7 @@ export class Interpreter { +@@ -1820,7 +1852,7 @@ export class Interpreter { if (next) return Effect.succeed(next); state.available = Deferred.makeUnsafe(); @@ -845,7 +1004,7 @@ index 8c1b549..3575409 100644 dequeueGeneratorRequest(state) { const request = state.pending[state.pendingIndex]; 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 +1036,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 +1051,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 +1061,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 +1071,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 +1082,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,7 +1093,7 @@ 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 @@ @@ -942,7 +1101,7 @@ index bc386d2..483cc3f 100644 -import { HttpClient } from "effect/unstable/http"; +import { HttpClient } from "effect/http"; 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 +1119,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 +1142,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 +1206,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..48aef236b2 --- /dev/null +++ b/patches/codemode-step-budget.md @@ -0,0 +1,42 @@ +# 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. + +A step can still call a native method that does much more work, such as `split` on a large +string. Those run at native speed, so they are not where the time went; weighting them is not +done. + +Remove the hunks when a codemode release has a step or CPU budget. From 4213e74da5438e3e7b3283647fd2eac291a903ce Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Wed, 18 Nov 2026 17:38:58 -0800 Subject: [PATCH 2/5] Charge codemode native calls for the data they read and return A loop of large native calls, such as split on a 1 MB string, was one step per call and ran 83 s inside a 5,000,000-step budget. invokeCallable now charges every native call for its input before it runs and its result after, at one step per 2 elements or keys and one per 256 characters; spread and new are charged at the same rate. The review's split PoC now stops in 3.4 s on the self-host e2e server. --- apps/local/server/README.md | 34 +-- e2e/test-plan.ts | 2 +- e2e/tests/mcp-execute-step-budget.spec.ts | 38 ++- packages/mcp/src/contracts/execute.ts | 15 +- ...encode-ai%2Fcodemode@0.0.0-dev-19272.patch | 219 +++++++++++++++--- patches/codemode-step-budget.md | 58 ++++- 6 files changed, 312 insertions(+), 54 deletions(-) diff --git a/apps/local/server/README.md b/apps/local/server/README.md index 8a8c77aa26..444539942c 100644 --- a/apps/local/server/README.md +++ b/apps/local/server/README.md @@ -55,23 +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_MCP_MAX_STEPS` | Statements and expressions one execute program may evaluate, not counting time spent waiting on tools; past it the program fails with `TimeoutExceeded`. Defaults to `5000000`. | -| `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 in proportion to their data, 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/e2e/test-plan.ts b/e2e/test-plan.ts index 131b9149c3..10ab7b782b 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -1232,7 +1232,7 @@ export const scenarios = plan({ fixtures: "actors", file: "mcp-execute-step-budget.spec.ts", title: - "Hosted MCP execute stops a program that only computes at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs complete", + "Hosted MCP execute stops a program that only computes, in the interpreter or through native calls, at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs complete", targets: { "self-host": scheduled, cloud: na("Shared execute path and CodeMode patch; covered on self-host"), diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts index 46b3c3d794..1972994632 100644 --- a/e2e/tests/mcp-execute-step-budget.spec.ts +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -4,7 +4,8 @@ * 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. + * 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. */ import { expect, layer } from "@effect/vitest"; import { Duration, Effect, Fiber, Schema } from "effect"; @@ -87,6 +88,22 @@ try { 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;`; + +/** 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. @@ -166,6 +183,23 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget "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); + + // 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)); @@ -176,6 +210,8 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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); }).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 922c1c2223..cb8c57bf52 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -527,9 +527,13 @@ export const McpLimits = Schema.Struct({ timeoutMs: Schema.Int.check(Schema.isGreaterThan(0)), maxToolCalls: Schema.Int.check(Schema.isGreaterThan(0)), /** - * Statements and expressions one program may evaluate. Waiting on tool calls and approvals - * evaluates nothing, so this bounds the CPU a program spends in the interpreter on the server's - * own thread, which `timeoutMs` alone does not. + * Interpreter steps one program may spend: one per statement or expression evaluated, plus steps + * for native calls in proportion to the data they read and return (about one per two array + * elements or object keys and one per 256 string characters). 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 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)), }); @@ -539,8 +543,9 @@ 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). 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. + // 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. */ 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 fa3c43625d..c1a951ddef 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -209,10 +209,10 @@ index db276e322e6eb9108c09dff9b5218bc4e29df616..d5b7dfca9c0020d63c828d776e044061 export type SupportedCallback = CodeModeFunction | CoercionFunction | UriFunction | PromiseCapabilityFunction | GlobalMethodReference | JsonMethodReference | IntrinsicReference | ErrorConstructorReference | GlobalNamespace | PromiseNamespace; export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback; diff --git a/dist/interpreter/model.d.ts b/dist/interpreter/model.d.ts -index ed2accf68c9f24fa26154e266617ef587340445a..6b0470af7409e5b43b0030e3010c12c6b5b6955c 100644 +index ed2accf68c9f24fa26154e266617ef587340445a..852ea1d07aeb70be1730579dc561f636e9ae938f 100644 --- a/dist/interpreter/model.d.ts +++ b/dist/interpreter/model.d.ts -@@ -136,6 +136,17 @@ export declare class InterpreterRuntimeError extends Error { +@@ -136,6 +136,18 @@ export declare class InterpreterRuntimeError extends Error { constructor(message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray | undefined); as(errorName: string): this; } @@ -226,15 +226,16 @@ index ed2accf68c9f24fa26154e266617ef587340445a..6b0470af7409e5b43b0030e3010c12c6 + 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..37b7e94df0318d385fcf74a3f125f7f41eb586c0 100644 +index d3a203d737409a4a51759206a156d90fed0ef918..1184bdb43edd332f54cc50134fcc05a7f663c1c4 100644 --- a/dist/interpreter/model.js +++ b/dist/interpreter/model.js -@@ -141,6 +141,28 @@ export class InterpreterRuntimeError extends Error { +@@ -141,6 +141,36 @@ export class InterpreterRuntimeError extends Error { return this; } } @@ -259,6 +260,14 @@ index d3a203d737409a4a51759206a156d90fed0ef918..37b7e94df0318d385fcf74a3f125f7f4 + 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; @@ -689,7 +698,7 @@ index 996f9a5090879de5104b8ecb439ee15fecee6f40..55843d5d6d27b370a235d66e1a242ab2 const box = {}; const body = Effect.gen(function* () { diff --git a/dist/interpreter/runtime.d.ts b/dist/interpreter/runtime.d.ts -index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..1bd8c078b0dd2399206284f42052b5efce50012e 100644 +index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59590d8675 100644 --- a/dist/interpreter/runtime.d.ts +++ b/dist/interpreter/runtime.d.ts @@ -1,6 +1,7 @@ @@ -718,8 +727,16 @@ index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..1bd8c078b0dd2399206284f42052b5ef 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 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1f4dee1e2 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfbfaa02068 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js @@ -1,6 +1,6 @@ @@ -730,7 +747,73 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 import { caughtErrorValue, constructAggregateErrorValue, constructErrorValue } from "./errors.js"; import { arrayStatics, invokeArrayFrom, invokeGlobalMethod, invokeGroupBy, invokeIntrinsic, } from "./methods.js"; import { preserveConsumerError } from "./iterator.js"; -@@ -147,6 +147,7 @@ export class Interpreter { +@@ -140,6 +140,65 @@ 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 its first one (up to RECORD_WIDTH_LIMIT), as copying a ++// result walks every field. ++const ELEMENTS_PER_STEP = 2; ++const CHARS_PER_STEP = 256; ++const RECORD_WIDTH_LIMIT = 32; ++const SCAN_ITEMS_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 nativeSteps = (value) => { ++ if (typeof value === "string") ++ return value.length / CHARS_PER_STEP; ++ if (Array.isArray(value)) { ++ const width = value.length === 0 ? 0 : Math.min(shallowWidth(value[0]), RECORD_WIDTH_LIMIT); ++ return (value.length * (1 + width)) / 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; ++}; ++// 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"]); ++const receiverSteps = (receiver, name, args) => { ++ if (typeof receiver !== "string" && !Array.isArray(receiver)) ++ return 0; ++ if (resultSizedMethods.has(name)) ++ return 0; ++ 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 +206,7 @@ export class Interpreter { toolKeys; logs; promises; @@ -738,7 +821,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 generatorState; generatorAsync = false; runner = { -@@ -154,9 +155,12 @@ export class Interpreter { +@@ -154,9 +214,12 @@ export class Interpreter { invokeCallable: (callable, args, node) => this.invokeCallable(callable, args, node), settlePromise: (promise) => this.settlePromise(promise), syncIterator: (value, node) => this.syncIterator(value, node), @@ -752,7 +835,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 this.scopes = new ScopeStack([globalScope]); this.executeTool = executeTool; this.invokeSearch = invokeSearch; -@@ -223,22 +227,28 @@ export class Interpreter { +@@ -223,22 +286,28 @@ export class Interpreter { return value; }).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop()))); } @@ -786,7 +869,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 switch (node.type) { case "ExpressionStatement": return Effect.as(this.evaluateExpression(getNode(node, "expression")), { kind: "none" }); -@@ -510,7 +520,7 @@ export class Interpreter { +@@ -510,7 +579,7 @@ export class Interpreter { const close = () => iterator ? self.closeIterator(iterator, node, awaiting) : awaiting @@ -795,7 +878,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 : (cursor?.close ?? Effect.void); let assignment; if (left.type !== "VariableDeclaration" && -@@ -656,7 +666,7 @@ export class Interpreter { +@@ -656,7 +725,7 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(iterator.next, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -804,7 +887,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 return yield* Effect.failCause(called.cause); } const captured = yield* Effect.exit(Effect.sync(() => { -@@ -665,7 +675,7 @@ export class Interpreter { +@@ -665,7 +734,7 @@ export class Interpreter { })); if (!Exit.isSuccess(captured)) { if (awaiting) @@ -813,7 +896,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 return yield* Effect.failCause(captured.cause); } return { -@@ -681,7 +691,7 @@ export class Interpreter { +@@ -681,7 +750,7 @@ export class Interpreter { ? new GeneratorMethodReference(iterator.iterator, "return") : iterator.iterator.return; if (close === undefined || close === null) @@ -822,7 +905,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 const self = this; return Effect.gen(function* () { const method = self.requireIteratorMethod(close, "Iterator return", node); -@@ -692,13 +702,13 @@ export class Interpreter { +@@ -692,13 +761,13 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(method, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -838,7 +921,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 return yield* Effect.failCause(captured.cause); } if (awaiting) -@@ -720,17 +730,18 @@ export class Interpreter { +@@ -720,17 +789,18 @@ export class Interpreter { return value; throw new InterpreterRuntimeError(`${context} must be a function.`, node).as("TypeError"); } @@ -861,7 +944,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } evaluateForInStatement(node, labels) { const left = getNode(node, "left"); -@@ -743,7 +754,7 @@ export class Interpreter { +@@ -743,7 +813,7 @@ export class Interpreter { self.predeclarePattern(declared.pattern, declared.mutable, left); const right = yield* self.evaluateExpression(getNode(node, "right")); const body = getNode(node, "body"); @@ -870,7 +953,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 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); } -@@ -833,7 +844,10 @@ export class Interpreter { +@@ -833,7 +903,10 @@ export class Interpreter { const self = this; const attempted = Effect.matchCauseEffect(this.evaluateStatement(body), { onFailure: (cause) => { @@ -882,7 +965,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 return Effect.failCause(cause); } const caught = caughtErrorValue(Cause.squash(cause)); -@@ -1037,6 +1051,7 @@ export class Interpreter { +@@ -1037,6 +1110,7 @@ export class Interpreter { return undefined; } evaluateExpression(node) { @@ -890,7 +973,65 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 switch (node.type) { case "Literal": { const regex = node.regex; -@@ -1592,7 +1607,7 @@ export class Interpreter { +@@ -1114,7 +1188,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 +1200,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(nativeSteps(arg), node); + switch (name) { + case "Date": + return yield* self.constructDate(args, node); +@@ -1561,8 +1641,34 @@ 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. ++ for (const arg of args) ++ input += nativeSteps(arg); ++ } ++ this.budget.charge(input, node); ++ return Effect.map(this.dispatchCallable(callable, args, node, callee), (result) => { ++ if (!(callable instanceof IntrinsicReference && result === callable.receiver && !Array.isArray(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,7 +1698,7 @@ export class Interpreter { if (callable.namespace === "console") return self.invokeConsole(callable.name, args, node); if (callable.namespace === "Object" && args[0] instanceof ToolReference) { @@ -899,7 +1040,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } if (callable.namespace === "Object" && objectMethodsPreservingIdentity.has(callable.name)) { if (callable.name === "fromEntries") -@@ -1665,7 +1680,7 @@ export class Interpreter { +@@ -1665,7 +1771,7 @@ export class Interpreter { } invokeObjectMethodOnTools(name, ref, node) { if (name === "keys") { @@ -908,7 +1049,15 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } 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"); } -@@ -1701,7 +1716,7 @@ export class Interpreter { +@@ -1683,6 +1789,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(nativeSteps(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 +1808,7 @@ export class Interpreter { }); } invokeFunction(fn, args) { @@ -917,7 +1066,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 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 +1745,7 @@ export class Interpreter { +@@ -1730,7 +1837,7 @@ export class Interpreter { return run; // The initial yield assigns the promise before the body can self-resolve. const box = {}; @@ -926,7 +1075,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 box.promise = promise; return promise; }); -@@ -1739,8 +1754,17 @@ export class Interpreter { +@@ -1739,8 +1846,17 @@ export class Interpreter { const state = { started: false, completed: false, draining: false, pending: [], pendingIndex: 0 }; invocation.generatorState = state; invocation.generatorAsync = asynchronous; @@ -945,7 +1094,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 if (!asynchronous && state.active) { return Effect.fail(new InterpreterRuntimeError("Generator is already running.", node).as("TypeError")); } -@@ -1749,11 +1773,11 @@ export class Interpreter { +@@ -1749,11 +1865,11 @@ export class Interpreter { state.completed = true; state.pending.push(request); if (state.draining) @@ -959,7 +1108,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } if (state.completed) { if (kind === "throw") -@@ -1766,11 +1790,19 @@ export class Interpreter { +@@ -1766,11 +1882,19 @@ export class Interpreter { return Effect.fail(new ProgramThrow(value)); return Effect.succeed({ value, done: true }); } @@ -980,7 +1129,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } if (!state.started) { state.started = true; -@@ -1789,10 +1821,10 @@ export class Interpreter { +@@ -1789,10 +1913,10 @@ export class Interpreter { yield* invocation.completeGeneratorRequests(state, asynchronous); state.completed = true; }); @@ -994,7 +1143,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 return generator; } completeGeneratorRequests(state, asynchronous) { -@@ -1820,7 +1852,7 @@ export class Interpreter { +@@ -1820,7 +1944,7 @@ export class Interpreter { if (next) return Effect.succeed(next); state.available = Deferred.makeUnsafe(); @@ -1003,6 +1152,22 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..d225674fb6014a81d43a2b1e05cecbb1 } dequeueGeneratorRequest(state) { const request = state.pending[state.pendingIndex]; +@@ -1952,6 +2076,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 +2129,7 @@ export class Interpreter { + const element = asNode(elementValue, "elements"); + if (element.type === "SpreadElement") { + const spread = yield* self.evaluateExpression(getNode(element, "argument")); ++ self.budget.charge(nativeSteps(spread), element); + const cursor = yield* self.syncIterator(spread, element); + if (cursor === undefined) + throw new InterpreterRuntimeError("Array spread requires a synchronous iterable.", element).as("TypeError"); diff --git a/dist/namespace.d.ts b/dist/namespace.d.ts index e4640762c1139d5a864f966035446f5d890040f0..220d15700f5e0f2d4dbee3f5500875ccbf3cb86d 100644 --- a/dist/namespace.d.ts diff --git a/patches/codemode-step-budget.md b/patches/codemode-step-budget.md index 48aef236b2..746fa0d0e2 100644 --- a/patches/codemode-step-budget.md +++ b/patches/codemode-step-budget.md @@ -35,8 +35,60 @@ times less than the runaway. Counted programs: a 100,000-iteration `for` loop is 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. -A step can still call a native method that does much more work, such as `split` on a large -string. Those run at native speed, so they are not where the time went; weighting them is not -done. +## Native calls + +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). This stops + a call whose input is already too large before it starts. +- after it returns, for its result. +- spread arguments, array spread and object spread, and the arguments and result of `new`, at the + same rate. + +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 its first one, up +to 32, because copying a result walks every field. Other values (numbers, functions, class +instances) cost nothing. Two kinds of method are charged less, so loops that call them stay +linear: methods whose work is bounded by their result (`slice`, `substring`, `at`, `push`, `pop`, +`concat` and similar) charge only their result, and `indexOf`, `includes` and `lastIndexOf` charge +one step per 4,096 elements or characters they can scan from their start position. A method that +returns its own receiver, such as `Map.set`, is not charged again for it. + +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. + +Charging adds about 4% to a loop that makes three native calls per iteration (100,000 iterations of +`Math.max`, `slice` and `push`: medians of 2,083-2,098 ms against 2,003-2,004 ms without native +charging, Node 24, 7 runs each). + +The rates were measured with each workload looped under the default 5,000,000-step budget until it +stopped (CPU seconds in Node 24 and Bun; wall seconds of one request in workerd, where it was the +only work): + +| Workload | Node s | Bun s | workerd s | +| ----------------------------------- | -----: | ----: | --------: | +| pure interpreter loop | 2.35 | 1.02 | 8.52 | +| `split("")` of 1 MB (review PoC) | 2.05 | 3.83 | 1.07 | +| `JSON.stringify` 20k records + scan | 4.87 | 1.24 | 2.79 | +| `JSON.parse` of 1.3 MB | 3.73 | 1.34 | 1.53 | +| `join` of 1M elements | 1.89 | 3.17 | 0.87 | +| `Object.keys` of 100k keys | 3.15 | 2.27 | 2.89 | +| spread of 1M elements | 0.77 | 0.82 | 4.93 | +| `replaceAll` on 1 MB | 5.40 | 3.62 | 9.43 | + +Native costs differ by engine, so no one rate matches all three, but every workload stops within +about 10 s on each runtime. In workerd, where the budget is about 9 s of interpreted work, each +native-heavy loop stops at or before the interpreted one. Ordinary programs fit: filtering and +mapping 20,000 records needs under 2,000,000 steps, grouping 50,000 records under 5,000,000, and a +comparator `sort` of 50,000 objects passes. A comparator `sort` of 100,000 objects needs about +20,000,000 steps, as every comparison is interpreted, 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. From 4bfe7248b37f70cebc2ff4ad521e8db0f8eac0ab Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Wed, 18 Nov 2026 18:52:16 -0800 Subject: [PATCH 3/5] Charge codemode operators for the data they walk, not results a program holds Review round 1 found two holes in the native-call charge. Binary operators deep-walked both operands for one step, so `id in index` over a 20,000-record index ran to its 120 s wall time; and every call's result was charged by size, even an existing reference, so grouping 50,000 rows with Map.get into 10 or 50 groups stopped at the budget in under a second. Results the program already holds (receiver, an argument, or an element or accumulator from get, at, pop, shift, find, findLast, reduce) are no longer charged. `in` reads one key and no longer walks its object; other operators' walks are charged as they go and stop partway, and primitives skip them. String comparisons, templates of arrays, rest patterns, for...in key lists and string-consuming Set, Map and spread are charged too. Record width samples 8 elements, capped at 64; scans charge per 512 items; normalize and localeCompare charge 8x. The e2e scenario adds the operator runaway, the `in` dedupe and the 10- and 50-group group-by. Backtracking regexes remain uncharged and documented. --- apps/local/server/README.md | 34 +-- e2e/test-plan.ts | 2 +- e2e/tests/mcp-execute-step-budget.spec.ts | 67 ++++- packages/mcp/src/contracts/execute.ts | 14 +- ...encode-ai%2Fcodemode@0.0.0-dev-19272.patch | 274 +++++++++++++++--- patches/codemode-step-budget.md | 122 +++++--- 6 files changed, 407 insertions(+), 106 deletions(-) diff --git a/apps/local/server/README.md b/apps/local/server/README.md index 444539942c..f08f2be568 100644 --- a/apps/local/server/README.md +++ b/apps/local/server/README.md @@ -55,23 +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_MCP_MAX_STEPS` | Interpreter steps one execute program may spend: statements and expressions, plus native calls in proportion to their data, 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`. | +| 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 and whole-value operators 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/e2e/test-plan.ts b/e2e/test-plan.ts index 10ab7b782b..de68d8ae12 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -1232,7 +1232,7 @@ export const scenarios = plan({ fixtures: "actors", file: "mcp-execute-step-budget.spec.ts", title: - "Hosted MCP execute stops a program that only computes, in the interpreter or through native calls, at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs complete", + "Hosted MCP execute stops a program that only computes, in the interpreter, through native calls or through operators, at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs, including grouping and `in` lookups, complete", targets: { "self-host": scheduled, cloud: na("Shared execute path and CodeMode patch; covered on self-host"), diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts index 1972994632..5d3fd3d25c 100644 --- a/e2e/tests/mcp-execute-step-budget.spec.ts +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -5,7 +5,10 @@ * 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. + * 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. */ import { expect, layer } from "@effect/vitest"; import { Duration, Effect, Fiber, Schema } from "effect"; @@ -98,6 +101,40 @@ 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;`; + /** 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'] })); @@ -196,6 +233,30 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget }); 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 }); + // 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 } }); @@ -212,6 +273,10 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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); }).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 cb8c57bf52..e56cdfaf30 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -528,12 +528,14 @@ export const McpLimits = Schema.Struct({ maxToolCalls: Schema.Int.check(Schema.isGreaterThan(0)), /** * Interpreter steps one program may spend: one per statement or expression evaluated, plus steps - * for native calls in proportion to the data they read and return (about one per two array - * elements or object keys and one per 256 string characters). 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 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`. + * for native calls, and for operators that walk or coerce a whole value, 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). 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)), }); 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 c1a951ddef..8001b0f707 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -697,6 +697,53 @@ index 996f9a5090879de5104b8ecb439ee15fecee6f40..55843d5d6d27b370a235d66e1a242ab2 const chainReaction = (runner, promises, source, onFulfilled, onRejected, method, node) => { const box = {}; const body = Effect.gen(function* () { +diff --git a/dist/interpreter/references.js b/dist/interpreter/references.js +index 314a7c3752745de0b8680531b64cf37bc584a812..a22d1c44e05cbb97427cf86a82d68ee79ea3a41d 100644 +--- a/dist/interpreter/references.js ++++ b/dist/interpreter/references.js +@@ -70,6 +70,42 @@ export const containsOpaqueReference = (value) => { + } + return false; + }; ++// 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 (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. + export const rejectCircularInsertion = (container, value, label, node, seen = new Set()) => { + const pending = [[value].values()]; diff --git a/dist/interpreter/runtime.d.ts b/dist/interpreter/runtime.d.ts index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59590d8675 100644 --- a/dist/interpreter/runtime.d.ts @@ -736,10 +783,10 @@ index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59 private invokeConsole; private evaluateCallArguments; diff --git a/dist/interpreter/runtime.js b/dist/interpreter/runtime.js -index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfbfaa02068 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2c3d292d9 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js -@@ -1,6 +1,6 @@ +@@ -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"; @@ -747,19 +794,26 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb import { caughtErrorValue, constructAggregateErrorValue, constructErrorValue } from "./errors.js"; import { arrayStatics, invokeArrayFrom, invokeGlobalMethod, invokeGroupBy, invokeIntrinsic, } from "./methods.js"; import { preserveConsumerError } from "./iterator.js"; -@@ -140,6 +140,65 @@ const copyIteratorSymbols = (source, target, consumed) => { + 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,95 @@ 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 its first one (up to RECORD_WIDTH_LIMIT), as copying a -+// result walks every field. ++// 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 = 32; -+const SCAN_ITEMS_PER_STEP = 4096; ++const RECORD_WIDTH_LIMIT = 64; ++const RECORD_WIDTH_SAMPLES = 8; ++const SCAN_ITEMS_PER_STEP = 512; +const isPlainData = (value) => { + if (value === null || typeof value !== "object") + return false; @@ -767,13 +821,21 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb + 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)) { -+ const width = value.length === 0 ? 0 : Math.min(shallowWidth(value[0]), RECORD_WIDTH_LIMIT); -+ return (value.length * (1 + width)) / ELEMENTS_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) @@ -782,6 +844,17 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb + 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([ @@ -791,11 +864,21 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb +// 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; @@ -813,7 +896,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb export class Interpreter { scopes; executeTool; -@@ -147,6 +206,7 @@ export class Interpreter { +@@ -147,6 +236,7 @@ export class Interpreter { toolKeys; logs; promises; @@ -821,7 +904,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb generatorState; generatorAsync = false; runner = { -@@ -154,9 +214,12 @@ export class Interpreter { +@@ -154,9 +244,12 @@ export class Interpreter { invokeCallable: (callable, args, node) => this.invokeCallable(callable, args, node), settlePromise: (promise) => this.settlePromise(promise), syncIterator: (value, node) => this.syncIterator(value, node), @@ -835,7 +918,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb this.scopes = new ScopeStack([globalScope]); this.executeTool = executeTool; this.invokeSearch = invokeSearch; -@@ -223,22 +286,28 @@ export class Interpreter { +@@ -223,22 +316,28 @@ export class Interpreter { return value; }).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop()))); } @@ -869,7 +952,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb switch (node.type) { case "ExpressionStatement": return Effect.as(this.evaluateExpression(getNode(node, "expression")), { kind: "none" }); -@@ -510,7 +579,7 @@ export class Interpreter { +@@ -510,7 +609,7 @@ export class Interpreter { const close = () => iterator ? self.closeIterator(iterator, node, awaiting) : awaiting @@ -878,7 +961,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb : (cursor?.close ?? Effect.void); let assignment; if (left.type !== "VariableDeclaration" && -@@ -656,7 +725,7 @@ export class Interpreter { +@@ -656,7 +755,7 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(iterator.next, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -887,7 +970,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb return yield* Effect.failCause(called.cause); } const captured = yield* Effect.exit(Effect.sync(() => { -@@ -665,7 +734,7 @@ export class Interpreter { +@@ -665,7 +764,7 @@ export class Interpreter { })); if (!Exit.isSuccess(captured)) { if (awaiting) @@ -896,7 +979,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb return yield* Effect.failCause(captured.cause); } return { -@@ -681,7 +750,7 @@ export class Interpreter { +@@ -681,7 +780,7 @@ export class Interpreter { ? new GeneratorMethodReference(iterator.iterator, "return") : iterator.iterator.return; if (close === undefined || close === null) @@ -905,7 +988,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb const self = this; return Effect.gen(function* () { const method = self.requireIteratorMethod(close, "Iterator return", node); -@@ -692,13 +761,13 @@ export class Interpreter { +@@ -692,13 +791,13 @@ export class Interpreter { const called = yield* Effect.exit(self.invokeCallable(method, [], node)); if (!Exit.isSuccess(called)) { if (awaiting) @@ -921,7 +1004,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb return yield* Effect.failCause(captured.cause); } if (awaiting) -@@ -720,17 +789,18 @@ export class Interpreter { +@@ -720,17 +819,18 @@ export class Interpreter { return value; throw new InterpreterRuntimeError(`${context} must be a function.`, node).as("TypeError"); } @@ -944,16 +1027,18 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } evaluateForInStatement(node, labels) { const left = getNode(node, "left"); -@@ -743,7 +813,7 @@ export class Interpreter { +@@ -743,7 +843,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); } -@@ -833,7 +903,10 @@ export class Interpreter { +@@ -833,7 +935,10 @@ export class Interpreter { const self = this; const attempted = Effect.matchCauseEffect(this.evaluateStatement(body), { onFailure: (cause) => { @@ -965,7 +1050,32 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb return Effect.failCause(cause); } const caught = caughtErrorValue(Cause.squash(cause)); -@@ -1037,6 +1110,7 @@ export class Interpreter { +@@ -897,6 +1002,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 +1052,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 +1105,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 +1146,7 @@ export class Interpreter { return undefined; } evaluateExpression(node) { @@ -973,7 +1083,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb switch (node.type) { case "Literal": { const regex = node.regex; -@@ -1114,7 +1188,11 @@ export class Interpreter { +@@ -1114,7 +1224,11 @@ export class Interpreter { } // Array and Object construct identically with or without new, like JS. if (name === "Array") { @@ -986,16 +1096,62 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } if (name === "Object") { return Effect.map(this.evaluateCallArguments(argNodes), (args) => self.constructObject(args, node)); -@@ -1122,6 +1200,8 @@ export class Interpreter { +@@ -1122,6 +1236,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(nativeSteps(arg), node); ++ self.budget.charge(iterationSteps(arg), node); switch (name) { case "Date": return yield* self.constructDate(args, node); -@@ -1561,8 +1641,34 @@ export class Interpreter { +@@ -1333,12 +1449,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,6 +1478,8 @@ 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; +@@ -1429,7 +1557,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 +1689,54 @@ export class Interpreter { return yield* self.invokeCallable(callable, args, node, callee); }); } @@ -1015,14 +1171,34 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb + input += nativeSteps(arg); + } + else { -+ // Object, Array, JSON and coercion functions read their whole arguments. -+ for (const arg of args) ++ // 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) => { -+ if (!(callable instanceof IntrinsicReference && result === callable.receiver && !Array.isArray(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; + }); + }); @@ -1031,7 +1207,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb const self = this; return Effect.gen(function* () { if (callable instanceof ToolReference) { -@@ -1592,7 +1698,7 @@ export class Interpreter { +@@ -1592,7 +1766,7 @@ export class Interpreter { if (callable.namespace === "console") return self.invokeConsole(callable.name, args, node); if (callable.namespace === "Object" && args[0] instanceof ToolReference) { @@ -1040,7 +1216,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } if (callable.namespace === "Object" && objectMethodsPreservingIdentity.has(callable.name)) { if (callable.name === "fromEntries") -@@ -1665,7 +1771,7 @@ export class Interpreter { +@@ -1665,7 +1839,7 @@ export class Interpreter { } invokeObjectMethodOnTools(name, ref, node) { if (name === "keys") { @@ -1049,15 +1225,15 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } 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"); } -@@ -1683,6 +1789,7 @@ export class Interpreter { +@@ -1683,6 +1857,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(nativeSteps(spread), argNode); ++ 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 +1808,7 @@ export class Interpreter { +@@ -1701,7 +1876,7 @@ export class Interpreter { }); } invokeFunction(fn, args) { @@ -1066,7 +1242,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb 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 +1837,7 @@ export class Interpreter { +@@ -1730,7 +1905,7 @@ export class Interpreter { return run; // The initial yield assigns the promise before the body can self-resolve. const box = {}; @@ -1075,7 +1251,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb box.promise = promise; return promise; }); -@@ -1739,8 +1846,17 @@ export class Interpreter { +@@ -1739,8 +1914,17 @@ export class Interpreter { const state = { started: false, completed: false, draining: false, pending: [], pendingIndex: 0 }; invocation.generatorState = state; invocation.generatorAsync = asynchronous; @@ -1094,7 +1270,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb if (!asynchronous && state.active) { return Effect.fail(new InterpreterRuntimeError("Generator is already running.", node).as("TypeError")); } -@@ -1749,11 +1865,11 @@ export class Interpreter { +@@ -1749,11 +1933,11 @@ export class Interpreter { state.completed = true; state.pending.push(request); if (state.draining) @@ -1108,7 +1284,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } if (state.completed) { if (kind === "throw") -@@ -1766,11 +1882,19 @@ export class Interpreter { +@@ -1766,11 +1950,19 @@ export class Interpreter { return Effect.fail(new ProgramThrow(value)); return Effect.succeed({ value, done: true }); } @@ -1129,7 +1305,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } if (!state.started) { state.started = true; -@@ -1789,10 +1913,10 @@ export class Interpreter { +@@ -1789,10 +1981,10 @@ export class Interpreter { yield* invocation.completeGeneratorRequests(state, asynchronous); state.completed = true; }); @@ -1143,7 +1319,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb return generator; } completeGeneratorRequests(state, asynchronous) { -@@ -1820,7 +1944,7 @@ export class Interpreter { +@@ -1820,7 +2012,7 @@ export class Interpreter { if (next) return Effect.succeed(next); state.available = Deferred.makeUnsafe(); @@ -1152,7 +1328,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb } dequeueGeneratorRequest(state) { const request = state.pending[state.pendingIndex]; -@@ -1952,6 +2076,7 @@ export class Interpreter { +@@ -1952,6 +2144,7 @@ export class Interpreter { if (typeof spread !== "object" || Array.isArray(spread) || isRuntimeReference(spread)) { throw new InterpreterRuntimeError("Object spread requires a data object.", property, "InvalidDataValue"); } @@ -1160,14 +1336,24 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..30c943808d3c19e7094a87b1cd548dfb for (const [key, value] of Object.entries(spread)) { if (isBlockedMember(key)) throw new InterpreterRuntimeError(`Property '${key}' is not available.`, property); -@@ -2004,6 +2129,7 @@ export class Interpreter { +@@ -2004,6 +2197,7 @@ export class Interpreter { const element = asNode(elementValue, "elements"); if (element.type === "SpreadElement") { const spread = yield* self.evaluateExpression(getNode(element, "argument")); -+ self.budget.charge(nativeSteps(spread), element); ++ 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,6 +2230,9 @@ 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")); + } + } diff --git a/dist/namespace.d.ts b/dist/namespace.d.ts index e4640762c1139d5a864f966035446f5d890040f0..220d15700f5e0f2d4dbee3f5500875ccbf3cb86d 100644 --- a/dist/namespace.d.ts diff --git a/patches/codemode-step-budget.md b/patches/codemode-step-budget.md index 746fa0d0e2..85a9dbb479 100644 --- a/patches/codemode-step-budget.md +++ b/patches/codemode-step-budget.md @@ -35,7 +35,7 @@ times less than the runaway. Counted programs: a 100,000-iteration `for` loop is 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 +## 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 @@ -45,50 +45,98 @@ host (tools, program functions, promise and generator methods, `tools.search`) c 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). This stops - a call whose input is already too large before it starts. -- after it returns, for its result. + 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. + 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 `>=`. +- 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 its first one, up -to 32, because copying a result walks every field. Other values (numbers, functions, class -instances) cost nothing. Two kinds of method are charged less, so loops that call them stay -linear: methods whose work is bounded by their result (`slice`, `substring`, `at`, `push`, `pop`, -`concat` and similar) charge only their result, and `indexOf`, `includes` and `lastIndexOf` charge -one step per 4,096 elements or characters they can scan from their start position. A method that -returns its own receiver, such as `Map.set`, is not charged again for it. +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. +data it already paid to build, and the next step after an overrun stops the program. A regular +expression is the exception: its input is small, but a backtracking pattern such as `/^(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, so this is not charged. -Charging adds about 4% to a loop that makes three native calls per iteration (100,000 iterations of -`Math.max`, `slice` and `push`: medians of 2,083-2,098 ms against 2,003-2,004 ms without native -charging, Node 24, 7 runs each). +Hot loops got faster, as primitives now skip the operand walk. Medians of 7 runs in Node 24 +(base is `main`'s patch, previous is the first native-charging version): + +| Loop (100,000-300,000 iterations) | base ms | previous ms | now ms | +| ----------------------------------------- | ------: | ----------: | -----: | +| `s += i % 7` | 1,392 | 1,356 | 1,271 | +| comparisons and `in` | 887 | 881 | 784 | +| `push`, `Math.max` and `slice` every turn | 1,711 | 1,714 | 1,757 | The rates were measured with each workload looped under the default 5,000,000-step budget until it -stopped (CPU seconds in Node 24 and Bun; wall seconds of one request in workerd, where it was the -only work): - -| Workload | Node s | Bun s | workerd s | -| ----------------------------------- | -----: | ----: | --------: | -| pure interpreter loop | 2.35 | 1.02 | 8.52 | -| `split("")` of 1 MB (review PoC) | 2.05 | 3.83 | 1.07 | -| `JSON.stringify` 20k records + scan | 4.87 | 1.24 | 2.79 | -| `JSON.parse` of 1.3 MB | 3.73 | 1.34 | 1.53 | -| `join` of 1M elements | 1.89 | 3.17 | 0.87 | -| `Object.keys` of 100k keys | 3.15 | 2.27 | 2.89 | -| spread of 1M elements | 0.77 | 0.82 | 4.93 | -| `replaceAll` on 1 MB | 5.40 | 3.62 | 9.43 | - -Native costs differ by engine, so no one rate matches all three, but every workload stops within -about 10 s on each runtime. In workerd, where the budget is about 9 s of interpreted work, each -native-heavy loop stops at or before the interpreted one. Ordinary programs fit: filtering and -mapping 20,000 records needs under 2,000,000 steps, grouping 50,000 records under 5,000,000, and a -comparator `sort` of 50,000 objects passes. A comparator `sort` of 100,000 objects needs about -20,000,000 steps, as every comparison is interpreted, so the default stops it; self-host and local -raise the budget with `EXECUTOR_MCP_MAX_STEPS`. +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 | + +Every workload 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. 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. From e158010c2a0369fc9720824dad7e69e1e05848de Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Wed, 18 Nov 2026 19:53:14 -0800 Subject: [PATCH 4/5] Charge codemode stores for their cycle walk and string + for its result Storing a value (assignment, push, unshift, splice, fill, Object.assign) walked it to reject cycles without charging, so attaching one large shared value to every row, or reassigning a group after each push, ran for minutes. The walk is now charged per 2 values visited, and remembers objects that hold no objects so it stops there next time. switch walks are charged the same way. `+` yielding a string was one step however long the result, and reading the rope it made copied it all. It is now charged one step per 4,096 characters of the result. The doc states the stop times seen under load and lists the walks left uncharged. --- apps/local/server/README.md | 34 +- e2e/test-plan.ts | 2 +- e2e/tests/mcp-execute-step-budget.spec.ts | 60 +++- packages/mcp/src/contracts/execute.ts | 7 +- ...encode-ai%2Fcodemode@0.0.0-dev-19272.patch | 317 ++++++++++++++---- patches/codemode-step-budget.md | 65 +++- 6 files changed, 392 insertions(+), 93 deletions(-) diff --git a/apps/local/server/README.md b/apps/local/server/README.md index f08f2be568..a0cb8d6f41 100644 --- a/apps/local/server/README.md +++ b/apps/local/server/README.md @@ -55,23 +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_MCP_MAX_STEPS` | Interpreter steps one execute program may spend: statements and expressions, plus native calls and whole-value operators 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`. | +| 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/e2e/test-plan.ts b/e2e/test-plan.ts index de68d8ae12..e6aa5b7a8c 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -1232,7 +1232,7 @@ export const scenarios = plan({ 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 or through operators, at its step budget with TimeoutExceeded, while other callers stay fast and ordinary programs, including grouping and `in` lookups, complete", + "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"), diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts index 5d3fd3d25c..cfab87a2b0 100644 --- a/e2e/tests/mcp-execute-step-budget.spec.ts +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -8,7 +8,10 @@ * 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. + * 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. + * Joining strings with `+` 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"; @@ -135,6 +138,37 @@ 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;`; + /** 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'] })); @@ -257,6 +291,27 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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); + + // 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 } }); @@ -277,6 +332,9 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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); }).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 e56cdfaf30..345d54c128 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -528,9 +528,10 @@ export const McpLimits = Schema.Struct({ maxToolCalls: Schema.Int.check(Schema.isGreaterThan(0)), /** * Interpreter steps one program may spend: one per statement or expression evaluated, plus steps - * for native calls, and for operators that walk or coerce a whole value, 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). A value a call returns that the program already holds, such as the group + * 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); `+` 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 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 8001b0f707..0d863bbf83 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -196,18 +196,78 @@ index 3d7f507ef37117b1d3ca12a3fa1d9754abca4031..82ffd78ab9661d809225c8e6326b142e const timeoutMs = limits.timeoutMs; const operation = timeoutMs === undefined diff --git a/dist/interpreter/methods.d.ts b/dist/interpreter/methods.d.ts -index db276e322e6eb9108c09dff9b5218bc4e29df616..d5b7dfca9c0020d63c828d776e044061411ec2d1 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 @@ -697,34 +757,50 @@ index 996f9a5090879de5104b8ecb439ee15fecee6f40..55843d5d6d27b370a235d66e1a242ab2 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..a22d1c44e05cbb97427cf86a82d68ee79ea3a41d 100644 +index 314a7c3752745de0b8680531b64cf37bc584a812..a4bf237d4e716d3259b0f617695268cc47c5b289 100644 --- a/dist/interpreter/references.js +++ b/dist/interpreter/references.js -@@ -70,6 +70,42 @@ export const containsOpaqueReference = (value) => { +@@ -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 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; -+ } + 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; + 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)) { @@ -732,18 +808,58 @@ index 314a7c3752745de0b8680531b64cf37bc584a812..a22d1c44e05cbb97427cf86a82d68ee7 + return true; + } + if (current === null || typeof current !== "object" || seen.has(current)) -+ continue; -+ seen.add(current); -+ pending.push(childValues(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. - export const rejectCircularInsertion = (container, value, label, node, seen = new Set()) => { - const pending = [[value].values()]; ++// 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 bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59590d8675 100644 --- a/dist/interpreter/runtime.d.ts @@ -783,7 +899,7 @@ index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59 private invokeConsole; private evaluateCallArguments; diff --git a/dist/interpreter/runtime.js b/dist/interpreter/runtime.js -index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2c3d292d9 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..cb19bd882a1e3847531b19847867e1159bccb5cd 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js @@ -1,11 +1,11 @@ @@ -800,7 +916,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 import { ScopeStack } from "./scope.js"; import { arrayMethods, mapMethods, mapStatics, setMethods } from "../stdlib/collections.js"; import { consoleMethods, formatConsoleMessage } from "../stdlib/console.js"; -@@ -140,6 +140,95 @@ const copyIteratorSymbols = (source, target, consumed) => { +@@ -140,6 +140,96 @@ const copyIteratorSymbols = (source, target, consumed) => { } }; const promiseResolutionNode = { type: "PromiseResolution" }; @@ -814,6 +930,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 +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; @@ -896,7 +1013,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 export class Interpreter { scopes; executeTool; -@@ -147,6 +236,7 @@ export class Interpreter { +@@ -147,6 +237,7 @@ export class Interpreter { toolKeys; logs; promises; @@ -904,12 +1021,13 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 generatorState; generatorAsync = false; runner = { -@@ -154,9 +244,12 @@ export class Interpreter { +@@ -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 = [], budget = new StepBudget(undefined)) { @@ -918,7 +1036,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 this.scopes = new ScopeStack([globalScope]); this.executeTool = executeTool; this.invokeSearch = invokeSearch; -@@ -223,22 +316,28 @@ export class Interpreter { +@@ -223,22 +318,28 @@ export class Interpreter { return value; }).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop()))); } @@ -952,7 +1070,25 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 switch (node.type) { case "ExpressionStatement": return Effect.as(this.evaluateExpression(getNode(node, "expression")), { kind: "none" }); -@@ -510,7 +609,7 @@ export class Interpreter { +@@ -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 @@ -961,7 +1097,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 : (cursor?.close ?? Effect.void); let assignment; if (left.type !== "VariableDeclaration" && -@@ -656,7 +755,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) @@ -970,7 +1106,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 return yield* Effect.failCause(called.cause); } const captured = yield* Effect.exit(Effect.sync(() => { -@@ -665,7 +764,7 @@ export class Interpreter { +@@ -665,7 +766,7 @@ export class Interpreter { })); if (!Exit.isSuccess(captured)) { if (awaiting) @@ -979,7 +1115,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 return yield* Effect.failCause(captured.cause); } return { -@@ -681,7 +780,7 @@ export class Interpreter { +@@ -681,7 +782,7 @@ export class Interpreter { ? new GeneratorMethodReference(iterator.iterator, "return") : iterator.iterator.return; if (close === undefined || close === null) @@ -988,7 +1124,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const self = this; return Effect.gen(function* () { const method = self.requireIteratorMethod(close, "Iterator return", node); -@@ -692,13 +791,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) @@ -1004,7 +1140,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 return yield* Effect.failCause(captured.cause); } if (awaiting) -@@ -720,17 +819,18 @@ export class Interpreter { +@@ -720,17 +821,18 @@ export class Interpreter { return value; throw new InterpreterRuntimeError(`${context} must be a function.`, node).as("TypeError"); } @@ -1027,7 +1163,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } evaluateForInStatement(node, labels) { const left = getNode(node, "left"); -@@ -743,7 +843,9 @@ 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"); @@ -1038,7 +1174,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 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); } -@@ -833,7 +935,10 @@ export class Interpreter { +@@ -833,7 +937,10 @@ export class Interpreter { const self = this; const attempted = Effect.matchCauseEffect(this.evaluateStatement(body), { onFailure: (cause) => { @@ -1050,7 +1186,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 return Effect.failCause(cause); } const caught = caughtErrorValue(Cause.squash(cause)); -@@ -897,6 +1002,7 @@ export class Interpreter { +@@ -897,6 +1004,7 @@ export class Interpreter { for (const propertyValue of getArray(pattern, "properties")) { const property = asNode(propertyValue, "properties"); if (property.type === "RestElement") { @@ -1058,7 +1194,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const rest = Object.create(null); for (const [key, item] of Object.entries(value)) { if (!consumed.has(key) && !isBlockedMember(key)) -@@ -946,6 +1052,7 @@ export class Interpreter { +@@ -946,6 +1054,7 @@ export class Interpreter { for (const propertyValue of getArray(pattern, "properties")) { const property = asNode(propertyValue, "properties"); if (property.type === "RestElement") { @@ -1066,7 +1202,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const rest = Object.create(null); for (const [key, item] of Object.entries(source)) { if (!consumed.has(key) && !isBlockedMember(key)) -@@ -998,6 +1105,8 @@ export class Interpreter { +@@ -998,6 +1107,8 @@ export class Interpreter { if (!step.done) rest.push(step.value); while (!done) { @@ -1075,7 +1211,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const next = yield* cursor.next; done = next.done; if (!done) -@@ -1037,6 +1146,7 @@ export class Interpreter { +@@ -1037,6 +1148,7 @@ export class Interpreter { return undefined; } evaluateExpression(node) { @@ -1083,7 +1219,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 switch (node.type) { case "Literal": { const regex = node.regex; -@@ -1114,7 +1224,11 @@ export class Interpreter { +@@ -1114,7 +1226,11 @@ export class Interpreter { } // Array and Object construct identically with or without new, like JS. if (name === "Array") { @@ -1096,7 +1232,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } if (name === "Object") { return Effect.map(this.evaluateCallArguments(argNodes), (args) => self.constructObject(args, node)); -@@ -1122,6 +1236,8 @@ export class Interpreter { +@@ -1122,6 +1238,8 @@ export class Interpreter { if (valueConstructors.has(name)) { return Effect.gen(function* () { const args = yield* self.evaluateCallArguments(argNodes); @@ -1105,7 +1241,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 switch (name) { case "Date": return yield* self.constructDate(args, node); -@@ -1333,12 +1449,22 @@ export class Interpreter { +@@ -1333,12 +1451,22 @@ export class Interpreter { return boundedData(self.applyBinaryOperator(operator, lhs, rhs, node), "Binary expression result"); }); } @@ -1133,16 +1269,26 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 throw new InterpreterRuntimeError("Binary operators require data values.", node, "InvalidDataValue"); } // Null-prototype data needs explicit primitive coercion; identity and `in` retain raw objects. -@@ -1352,6 +1478,8 @@ export class Interpreter { +@@ -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; -@@ -1429,7 +1557,7 @@ export class Interpreter { +- 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; @@ -1151,7 +1297,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 throw new InterpreterRuntimeError("Unary operators require data values.", node, "InvalidDataValue"); } const operand = value instanceof CodeModeDate -@@ -1561,8 +1689,54 @@ export class Interpreter { +@@ -1561,8 +1696,54 @@ export class Interpreter { return yield* self.invokeCallable(callable, args, node, callee); }); } @@ -1207,7 +1353,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const self = this; return Effect.gen(function* () { if (callable instanceof ToolReference) { -@@ -1592,7 +1766,7 @@ export class Interpreter { +@@ -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) { @@ -1216,7 +1362,13 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } if (callable.namespace === "Object" && objectMethodsPreservingIdentity.has(callable.name)) { if (callable.name === "fromEntries") -@@ -1665,7 +1839,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") { @@ -1225,7 +1377,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } 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"); } -@@ -1683,6 +1857,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")); @@ -1233,7 +1385,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const cursor = yield* self.syncIterator(spread, argNode); if (cursor === undefined) throw new InterpreterRuntimeError("Spread arguments require a synchronous iterable.", argNode).as("TypeError"); -@@ -1701,7 +1876,7 @@ export class Interpreter { +@@ -1701,7 +1883,7 @@ export class Interpreter { }); } invokeFunction(fn, args) { @@ -1242,7 +1394,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 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 +1905,7 @@ export class Interpreter { +@@ -1730,7 +1912,7 @@ export class Interpreter { return run; // The initial yield assigns the promise before the body can self-resolve. const box = {}; @@ -1251,7 +1403,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 box.promise = promise; return promise; }); -@@ -1739,8 +1914,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; @@ -1270,7 +1422,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 if (!asynchronous && state.active) { return Effect.fail(new InterpreterRuntimeError("Generator is already running.", node).as("TypeError")); } -@@ -1749,11 +1933,11 @@ export class Interpreter { +@@ -1749,11 +1940,11 @@ export class Interpreter { state.completed = true; state.pending.push(request); if (state.draining) @@ -1284,7 +1436,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } if (state.completed) { if (kind === "throw") -@@ -1766,11 +1950,19 @@ export class Interpreter { +@@ -1766,11 +1957,19 @@ export class Interpreter { return Effect.fail(new ProgramThrow(value)); return Effect.succeed({ value, done: true }); } @@ -1305,7 +1457,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } if (!state.started) { state.started = true; -@@ -1789,10 +1981,10 @@ export class Interpreter { +@@ -1789,10 +1988,10 @@ export class Interpreter { yield* invocation.completeGeneratorRequests(state, asynchronous); state.completed = true; }); @@ -1319,7 +1471,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 return generator; } completeGeneratorRequests(state, asynchronous) { -@@ -1820,7 +2012,7 @@ export class Interpreter { +@@ -1820,7 +2019,7 @@ export class Interpreter { if (next) return Effect.succeed(next); state.available = Deferred.makeUnsafe(); @@ -1328,7 +1480,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 } dequeueGeneratorRequest(state) { const request = state.pending[state.pendingIndex]; -@@ -1952,6 +2144,7 @@ export class Interpreter { +@@ -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"); } @@ -1336,7 +1488,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 for (const [key, value] of Object.entries(spread)) { if (isBlockedMember(key)) throw new InterpreterRuntimeError(`Property '${key}' is not available.`, property); -@@ -2004,6 +2197,7 @@ export class Interpreter { +@@ -2004,6 +2204,7 @@ export class Interpreter { const element = asNode(elementValue, "elements"); if (element.type === "SpreadElement") { const spread = yield* self.evaluateExpression(getNode(element, "argument")); @@ -1344,7 +1496,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 const cursor = yield* self.syncIterator(spread, element); if (cursor === undefined) throw new InterpreterRuntimeError("Array spread requires a synchronous iterable.", element).as("TypeError"); -@@ -2036,6 +2230,9 @@ export class Interpreter { +@@ -2036,6 +2237,9 @@ export class Interpreter { output += rawValue.cooked; if (index < expressions.length) { const raw = yield* self.evaluateExpression(asNode(expressions[index], "expressions")); @@ -1354,6 +1506,24 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..5502f4f042142565e2e1304a24a9c9b2 output += coerceToString(boundedData(raw, "Template interpolation")); } } +@@ -2312,7 +2516,7 @@ 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); ++ rejectCircularInsertion(target, next, "Array assignment result", node, undefined, this.meterWalk(node)); + target[key] = next; + return; + } +@@ -2337,7 +2541,7 @@ export class Interpreter { + return; + } + const target = reference.target; +- rejectCircularInsertion(target, next, "Object assignment result", node); ++ 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 e4640762c1139d5a864f966035446f5d890040f0..220d15700f5e0f2d4dbee3f5500875ccbf3cb86d 100644 --- a/dist/namespace.d.ts @@ -1451,6 +1621,39 @@ index bc386d2cd78bffa1bd1f76bf98509183f61c57d1..483cc3fd953ab434bf0209462d9a88dd 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 1adc0b7af422b5ca0ffe5b79abc7a14ceccb7627..9489b77bae4a0833ee2ff82811a859b22621775f 100644 --- a/dist/tool-error.d.ts diff --git a/patches/codemode-step-budget.md b/patches/codemode-step-budget.md index 85a9dbb479..22f2e40788 100644 --- a/patches/codemode-step-budget.md +++ b/patches/codemode-step-budget.md @@ -69,6 +69,23 @@ Operators and patterns that read a whole value are charged too: 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 is + `+=`. 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. 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. @@ -84,20 +101,37 @@ their start position; `normalize` and `localeCompare` charge 8 times their recei 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. A regular -expression is the exception: its input is small, but a backtracking pattern such as `/^(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, so this is not charged. - -Hot loops got faster, as primitives now skip the operand walk. Medians of 7 runs in Node 24 -(base is `main`'s patch, previous is the first native-charging version): +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. +- 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,392 | 1,356 | 1,271 | -| comparisons and `in` | 887 | 881 | 784 | -| `push`, `Math.max` and `slice` every turn | 1,711 | 1,714 | 1,757 | +| `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 @@ -129,9 +163,12 @@ to 7 s there. | `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 | -Every workload 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. Ordinary programs fit, with the steps they are charged: grouping 50,000 rows with +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 From 03edd79e190e35f102d38a08770471a591757358 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Wed, 18 Nov 2026 20:38:20 -0800 Subject: [PATCH 5/5] Skip the cycle walk for self-stores and charge templates like string + `g[k] = g[k] || []` stored the group back into its own slot on every row, and the store walked the whole group each time, so grouping 50,000 row objects stopped at the budget. Storing the value a slot already holds cannot change the graph, so it no longer walks. A template literal is charged one step per 4,096 characters of its result, like string `+`; interpolating a megabyte string in a loop ran to the wall clock. --- e2e/tests/mcp-execute-step-budget.spec.ts | 55 ++++++++++++++++++- packages/mcp/src/contracts/execute.ts | 6 +- ...encode-ai%2Fcodemode@0.0.0-dev-19272.patch | 20 +++++-- patches/codemode-step-budget.md | 12 +++- 4 files changed, 79 insertions(+), 14 deletions(-) diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts index cfab87a2b0..205d63cc79 100644 --- a/e2e/tests/mcp-execute-step-budget.spec.ts +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -10,8 +10,9 @@ * 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. - * Joining strings with `+` is charged by the length of the result, so a loop that joins a - * megabyte string stops in seconds. + * 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"; @@ -169,6 +170,32 @@ 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'] })); @@ -299,6 +326,27 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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({ @@ -335,6 +383,9 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget 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 345d54c128..0cc72b6552 100644 --- a/packages/mcp/src/contracts/execute.ts +++ b/packages/mcp/src/contracts/execute.ts @@ -530,9 +530,9 @@ export const McpLimits = Schema.Struct({ * 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); `+` 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 + * 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 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 0d863bbf83..375ee40b51 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -899,7 +899,7 @@ index bd1bc708017c1a30b0c81d37639b55ff12a2e3bc..0ef22693f3e8bbed507161502b837c59 private invokeConsole; private evaluateCallArguments; diff --git a/dist/interpreter/runtime.js b/dist/interpreter/runtime.js -index 8c1b549d3da8c2287e330e63d7370cd3838e57df..cb19bd882a1e3847531b19847867e1159bccb5cd 100644 +index 8c1b549d3da8c2287e330e63d7370cd3838e57df..bc213c7be488a22deac6d42fe4b1f630647bb577 100644 --- a/dist/interpreter/runtime.js +++ b/dist/interpreter/runtime.js @@ -1,11 +1,11 @@ @@ -1496,7 +1496,7 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..cb19bd882a1e3847531b19847867e115 const cursor = yield* self.syncIterator(spread, element); if (cursor === undefined) throw new InterpreterRuntimeError("Array spread requires a synchronous iterable.", element).as("TypeError"); -@@ -2036,6 +2237,9 @@ export class Interpreter { +@@ -2036,9 +2237,14 @@ export class Interpreter { output += rawValue.cooked; if (index < expressions.length) { const raw = yield* self.evaluateExpression(asNode(expressions[index], "expressions")); @@ -1506,21 +1506,29 @@ index 8c1b549d3da8c2287e330e63d7370cd3838e57df..cb19bd882a1e3847531b19847867e115 output += coerceToString(boundedData(raw, "Template interpolation")); } } -@@ -2312,7 +2516,7 @@ export class Interpreter { ++ // 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); -+ rejectCircularInsertion(target, next, "Array assignment result", node, undefined, this.meterWalk(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 +2541,7 @@ export class Interpreter { +@@ -2337,7 +2545,8 @@ export class Interpreter { return; } const target = reference.target; - rejectCircularInsertion(target, next, "Object assignment result", node); -+ rejectCircularInsertion(target, next, "Object assignment result", node, undefined, this.meterWalk(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/patches/codemode-step-budget.md b/patches/codemode-step-budget.md index 22f2e40788..16a1cca4f1 100644 --- a/patches/codemode-step-budget.md +++ b/patches/codemode-step-budget.md @@ -69,8 +69,8 @@ Operators and patterns that read a whole value are charged too: 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 is - `+=`. The engine joins strings as a rope, so the join itself is cheap, but the next read of the +- `+` 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, @@ -82,7 +82,10 @@ Operators and patterns that read a whole value are charged too: 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. Before this, attaching + 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. @@ -117,6 +120,9 @@ These still run work the budget does not see, and are left for a follow-up: 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.