Skip to content

fix(mcp): stop execute programs at a CodeMode step budget - #105

Merged
shuv1337 merged 5 commits into
v2from
fix/codemode-cpu-budget
Nov 19, 2026
Merged

shuv1337 merged 5 commits into
v2from
fix/codemode-cpu-budget

Conversation

@shuv1337

@shuv1337 shuv1337 commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Why

On Nov 18 2026 (about 3:26–3:33 PM PT) two execute programs on production each spent about 203 s in the CodeMode interpreter with no tool calls. The interpreter runs on the product workerd's main thread, and its only budgets were wall time (timeoutMs, 5 minutes) and maxToolCalls (100). A program that only computes ran inside its 5 minutes and kept the product isolate near 100% CPU. Effect preempts the program's fiber every 2,048 operations, so other requests still ran, but slowly, and clients gave up.

What

  • CodeMode patch (patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch, notes in patches/codemode-step-budget.md): adds ExecutionLimits.maxSteps. A StepBudget is shared by every Interpreter of one execution (invokeFunction creates one per call). evaluateStatement and evaluateExpression each count one step. Waiting on a tool call or approval evaluates nothing, so only interpreter work counts.
    • Past the limit, every step throws StepBudgetExceeded, an InterpreterRuntimeError of kind TimeoutExceeded. try passes it on without running catch, and because each later step throws again, no catch, finally or promise reaction can keep the program running.
    • I counted steps rather than CPU time: workerd's clock doesn't advance while code runs, and a count gives the same answer on every host and under any load.
    • Native calls and whole-value operators are charged for the data they read, copy and return (added in review rounds 0 and 1). The rules are below.
  • Executor: McpLimits.maxSteps defaults to 5,000,000. Self-host and local read EXECUTOR_MCP_MAX_STEPS, documented in the local README. Execute keeps CodeMode's message instead of replacing it with the wall-time message, and tags mcp.execute with executor.timeout.budget=steps. EXECUTOR_MCP_TIMEOUT_MS is unchanged.

The message the agent gets:

Execution exceeded its compute budget of 5000000 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. (line 3, col 12)

What is charged

Round 0 showed a loop of s.split("") on a 1 MB string running 83 s inside a 5 M budget. Round 1 showed two more holes: operators that walk a whole value for 1 step, and a regression where results the program already holds were charged as if copied. The rule now is charge data that is actually read, copied or scanned, never the size of a reference that is handed back.

  • Native calls. invokeCallable, the one dispatch every call goes through, charges each native call. Calls that run program code or wait on the host count their own steps and aren't charged: tools, program functions, promise and generator methods, and tools.search.
    • Before the call, for its input: a method's receiver and string arguments, or every argument of a global function (JSON, Object, Array, Math…). Two exceptions: Object.assign's target, which it only writes, and Object.hasOwn and Array.isArray, which read one key or the type.
    • After the call, for its result, only if the result is new. A result the program already holds isn't charged: the receiver (Map.set), an argument (Object.assign), or an element or accumulator (Map.get, at, pop, shift, find, findLast, reduce). sort, reverse and fill still pay for their pass.
  • Operators. Binary operators other than ===/!==, and unary +, - and ~, walk their operands to reject interpreter references before coercing them. The walk is now charged as it goes, 1 step per 2 values visited, so it can stop partway. Primitives skip the walk entirely.
    • in reads one own key, so it no longer walks its right-hand object at all; it only checks that the object itself is data. One behavior change: "a" in o now works when o holds a function somewhere inside, where it used to throw.
    • String comparisons are charged 1 step per 256 characters they may read.
    • A template that interpolates an array or object is charged for copying and joining it.
    • New (round 3): + (and +=) that yields a string is charged 1 step per 4,096 characters of its result. Round 4: so is a template literal; `${s}${i}` on a 1 MB string ran to the wall clock and now stops in about 1.5 s. The engine joins strings as ropes, so the join is cheap but the next read copies the whole result: t = s + i on a 1 MB string, then t.charCodeAt(…), ran to the wall clock and now stops in about 1.5 s. 4,096 rather than 256 because out += line is charged its full length on every append, which grows with the square of the line count: a 554 KB report of 5,000 lines costs 0.43 M steps, and about 18,000 lines (2.2 MB) spend the whole default budget. Building in an array and calling join is charged once.
  • New (round 3): storing a value. Assignment to a property or index, push, unshift, splice, fill and Object.assign walk the stored value to reject cycles, and switch walks its discriminant and case values to reject interpreter references. These walks are now charged as they go, 1 step per 2 values visited. The store walk also remembers, in a WeakSet, every object it found holding no objects, and stops there next time. Storing an object into one of them drops it from the set, and every store into existing data runs this walk first, so the walk still catches every cycle (20 cycle and sharing checks, below). Round 4: a property or index assignment of the value the slot already holds skips the walk, since it can't change the graph. Round 3 walked the whole group on every g[k] = g[k] || [] and stopped grouping 50k row objects at the budget in about 2 s; it now completes in 0.7–0.9 s, in the reduce form too. Attaching one 50,000-column meta to each of 20,000 rows went from minutes to 0.36 s; grouping into an object with a = g[k] || []; a.push(v); g[k] = a takes 0.7 s.
  • Patterns and iteration. Object rest is charged for the object, and array rest 1 step per element. for…in is charged for its key list. Spread, new Set and new Map charge a string 1 step per 2 characters, because they consume it one character at a time.
  • Rates. 1 step per 2 array elements, object keys or Map/Set entries, and 1 per 256 string characters. An array of records counts each element as wide as the widest of up to 8 evenly spaced samples, capped at 64. Round 1 used only the first element, capped at 32, and [0, ...wide records] got through. slice, at, push, pop, concat and similar charge only their result. indexOf/includes/lastIndexOf charge 1 step per 512 elements scannable from their start position (round 1: 4,096). normalize and localeCompare charge 8× their receiver.
  • Residual gaps (listed in the patch notes under "Not charged yet", for a follow-up PR):
    • One native call still runs to its end before it's charged, but its input is charged first, so it's bounded by data the program already paid to build.
    • Backtracking regular expressions: /^(a+)+$/ on a 25-character string takes about 40 ms per match, and a loop of 1,000 matches ran 44 s in Node, inside its 60 s wall budget. Codemode has no backtracking limit to set, and refusing nested quantifiers would refuse ordinary patterns.
    • Three reference walks are still unmetered, each running to a 30 s wall time in a loop over a 1M-element array: ++/-- on a property (o.x++), the entries Object.fromEntries reads, and non-string arguments of string methods ("a".padEnd(1, big)). console.table and Object.is walk too but are already stopped by the charge on their arguments (7.6 s, 4.4 s).
    • A shared value made of many small records is walked in full on every store, since only objects holding no objects are remembered: a 1,000-record meta on each of 20k rows stops at the budget in about 2 s.
    • Not a hole: g[k] = (g[k] || []).concat([v]) copies the group every row and is charged for it, so 50k rows in 10 groups stop at the budget in 0.7 s. push into the group instead.
  • Cost. Hot loops cost about what they did on main. Medians of 7 Node 24 runs, main's patch / round 2 / round 3: 300k-iteration s += i % 7 1,370 / 1,291 / 1,283 ms; comparisons and in 884 / 790 / 780 ms; push + Math.max + slice 1,667 / 1,710 / 1,713 ms; stores o.x = {v: i, t: [i]} and a[0] = i 1,109 / 1,134 / 1,129 ms; string joins 'key-' + i + '-' + (i % 7) 768 / 703 / 714 ms (+1.6% vs round 2).

Review PoCs

Every PoC from round 0 (/workspace/pr105-poc/), round 1 and round 2 (/workspace/pr105b/) was run against the round 3 patched CodeMode under Bun, with the default 5 M budget unless a budget is named.

"Stops" means TimeoutExceeded at the step budget. Times are wall time under Bun.

Round 3 review findings (round 4 build; before = round 3, e158010)

PoC Expected Before (e158010) Now Result
g[r.k] = g[r.k] || []; g[r.k].push(r), row objects, 50k rows into 10 / 50 groups; 20k into 10 finish stopped at the budget, 1.7 s (Node) OK 0.77 / 0.70 / 0.29 s ✅ pass
(acc[r.k] = acc[r.k] || []).push(r) in reduce, 50k rows, 10 groups finish stopped, 1.6 s (Node) OK 0.89 s ✅ pass
same idiom with an array index finish — OK 0.73 s ✅ pass
`${s}${i}` then charCodeAt, 1 MB / 4 MB s, 1M times stop well under wall ran past a 20 s timeout stops 1.51 / 1.36 s ✅ pass
short templates (`row ${i}: ${i*2}`), 100k times finish OK OK 0.46 s ✅ pass
cycles through self-stores (g.a = g.a then g.a.push(g), g[0] = g[0], g.a = g.a || [g], (acc.k = acc.k || []).push(acc), replacing with a cycle) reject rejected all rejected; plain self-store OK ✅ pass
hunt: templateLoop (`${s}${s}`.length on 1 MB, 100k times) — OK 0.34 s stops 0.04 s ⚠️ intended: each 2 MB template is now charged 488 steps, as s + s is

Every other PoC below gives the same outcome on the round 4 build as on round 3.

Round 2 review PoCs (round 3 build; before = round 2, 4bfe724)

PoC (file: case) Expected Before (4bfe724) Now Result
realistic2: r.meta = meta (50k columns) on 20k rows finish fast ran for minutes (20 s timeout in my run) OK 0.36 s ✅ pass
realistic2: object group-by with reassignment, 500 / 50 / 10 groups finish fast 10 groups 10.8 s in Node OK 0.76 / 0.72 / 0.71 s ✅ pass
realistic2: object group-by without reassignment, 10 groups finish OK OK 0.63 s ✅ pass
realistic2 (added): g[k] = (g[k] || []).concat([v]), 10 groups stop or finish — stops 0.73 s (it copies the group every row) ⚠️ charged, documented
concat: t = s + i on 1 MB, then charCodeAt, 1M times stop well under wall ran to the 30 s wall stops 1.49 s (Node 4.1 s) ✅ pass
hunt3: h.x = big, h[0] = big, push/unshift/splice/fill of big, Object.assign(t, {a: big}), 1M times stop ran to wall all stop, 3.0–3.8 s ✅ pass
hunt3: switch (big) 1M times stop ran to wall stops 4.4 s ✅ pass
hunt3: object/array literal holding big, Map.set, !big, &&, ?? stop stopped all stop, 3.1–3.5 s ✅ pass
hunt3: Object.assign growing a 200k-key target; reduce growing an accumulator finish OK OK 1.28 / 1.55 s ✅ pass
hunt3: big.sort((x, y) => 0) 100k times stop stopped 16.4 s (under load) stops 10.3 s (a callback per comparison) ✅ pass, slow host rate
hunt3: copyWithin 100k times stop stopped stops 5.9 s ✅ pass
hunt3: push(...big) refuse nesting-depth error same ✅ unchanged
hunt3: s += "x".repeat(100) 1M times; a == b on 4 MB strings stop stopped stop 0.10 / 0.05 s ✅ pass
sec: in against tools, a tool namespace, a tool function, a promise, a search result reject rejected same 8 rejections; 3 data objects holding a tool, function or promise answer true, a returned object holding a tool is refused ✅ unchanged
cycle checks (mine): a.push(a), o.x = o, cycles through splice, fill, unshift, Object.assign, index assignment, and indirect cycles after the objects were cached as leaves reject rejected all 11 rejected; sorted leaf, shared value and diamond still OK ✅ pass

Earlier PoCs (round 0 and round 1)

Re-run on the round 3 build, all unchanged from round 2 except concatOpLoop, which now stops (1.42 s). The table keeps round 2's numbers; "before" is round 1's build (4213e74).

PoC (file: case) Expected Before (4213e74) Now Result
pr105b/realistic: dedupe with in (20k into 20k) finish fast or stop ran to 120 s wall OK 10,000 added, 0.48 s ✅ pass
pr105b/realistic: dedupe with Map.has finish OK 0.86 s OK 0.46 s ✅ pass
pr105b/group: group-by 50k rows, 10 / 50 / 500 groups, at 5 M finish stopped at 0.41 s / 0.51 s; 500 OK OK 0.74 / 0.74 / 0.87 s ✅ pass
pr105b/group: same with an object index finish OK 0.51 s OK 0.50 s ✅ pass
pr105b/g2: group-by, 50 groups, at no limit / 5 M / 20 M / 40 M finish stopped at 5 M (0.51 s), OK at 20 M and up OK at every budget, 0.69–0.82 s ✅ pass
pr105b/hunt2: "zz" in a 200k-key object stop ran to wall (30–40 s) stops 1.25 s ✅ pass
pr105b/hunt: "zz" in a 1k-key object (LOW) stop stopped 33 s stops 1.39 s ✅ pass
pr105b/hunt: a == "x", String(a), template, "" + a on 1M elements stop ==, template and "" + ran to wall stop 5.31 / 4.55 / 3.50 / 4.69 s ✅ pass
pr105b/hunt: === and < on 4 MB strings stop ran to wall stop 0.05 / 0.07 s ✅ pass
pr105b/hunt: rest destructuring of 1M array / 100k-key object stop ran to wall stop 2.71 / 1.42 s ✅ pass
pr105b/hunt: for…in 200k keys, then break stop ran to wall stops 0.98 s ✅ pass
pr105b/hunt: new Set of a 1M-character string stop ran to wall stops 0.36 s ✅ pass
pr105b/hunt: includes on 1M strings (LOW) stop, sooner stopped 29 s stops 5.96 s ✅ pass
pr105b/hunt: JSON.stringify([0, ...20k 200-key records]) (LOW) stop, sooner stopped 30 s stops 3.78 s ✅ pass
pr105b/hunt: normalize on 1M characters (LOW) stop, sooner stopped 26 s stops 2.87 s ✅ pass
pr105b/hunt: the other 26 amplification loops (repeat, padStart, fill, sort, reverse, new Map, Object.assign, spread, entries, join, localeCompare, Math.max(...a), flat, indexOf, …) stop not flagged all stop, 0.01–4.92 s ✅ pass
pr105b/hunt: templateLoop, concatOpLoop (1 MB string concatenation, ropes) finish not flagged OK 0.34 s; concatOpLoop OK 7.28 s in round 2, stops 1.42 s in round 3 ✅ round 3 charges string + (the review's 12 s run of it is the MED)
pr105b/hunt: bigint, typedArray, structuredClone, String.raw unsupported unsupported same errors ✅ unchanged
pr105b/hunt: /^(a+)+$/ and /^(\w+\s?)*$/ loops of 1,000 matches stop or document ran to wall ran to wall (38.9 s, 44.6 s) ⚠️ documented gap
pr105b/hunt2: a === a, typeof, ternary, property read, new Set(array) in long loops stop stopped all stop, 1.25–3.17 s ✅ pass
pr105b/hunt2: new Set of a 1M-character string, 20 times (setStrSmall) control OK in 0.84 s stops in 0.37 s ⚠️ intended change: each call is now charged 500k steps for 1M characters; in workerd each one takes 0.58 s, so 20 of them take 11.7 s, more than the 8.5 s a pure loop gets
pr105b/hunt2: for…of 1M array, then break, 100k times finish finished OK 2.78 s ✅ pass
pr105b/rerun + round 0 bypass: split("") of 1 MB, 100k times stop stopped 4.4 s stops 2.81 / 2.99 s ✅ pass
pr105b/rerun: JSON.stringify(all).includes(id), 20k records stop stopped 1.5 s stops 0.90 s ✅ pass
pr105b/rerun: 10 amplification loops on 1M data stop 9/10 stopped 10/10 stop, 2.86–3.77 s ✅ pass
round 0 amp: 10 ops × 5 on 1M data heavy ops stop, scans pass as expected 8 stop, includes and indexOf pass ✅ pass
round 0 escape: 34 escape attempts at 20,000 steps loops stop every loop stopped 31 stop (≤ 3.0 s); getter unsupported and errorCtorToString caught data error, as before; one ReDoS match OK in 0.23 s ✅ pass
pr105b/wait + round 0 wait: 5 s tool on 50 steps, 10 × 3 s on 200, race and background tool on 100 finish OK OK ✅ pass
pr105b/wait + round 0 wait: comparator sort of 20k / 50k at 5 M finish OK OK 0.77–0.79 / 2.07–2.10 s ✅ pass
pr105b/legit + round 0 legit: budget sweeps (sort 100k, bigJsonTransform, group-by 500, text lines, paginate) same pass points sort 100k needs 20 M; others ≤ 2 M unchanged ✅ pass
round 0 env: 13 EXECUTOR_MCP_MAX_STEPS values decode or refuse at startup as listed unchanged ✅ pass

Calibration

Each runaway workload ran under the default 5 M budget until it stopped. Times are wall seconds of one program on shuvdev; in workerd, one request with nothing else running. Many setups build a 1M-element array with Array.from and a mapper. That's a million callback calls, and callbacks cost about twice a loop step in workerd, so the setup alone takes up to 7 s there.

Workload Node 24 s Bun s workerd s
pure interpreter loop 1.68 0.78 8.53
split("") of 1 MB (round 0 PoC) 1.23 2.80 0.98
JSON.stringify 20k records + includes 2.74 0.90 2.30
JSON.parse of 1.3 MB 1.77 0.98 1.84
join of 1M elements 5.61 4.18 11.61
Object.keys of 100k keys 1.71 1.30 2.64
spread of 1M elements 5.15 2.68 14.87
replaceAll on 1 MB 3.92 3.30 8.03
JSON.stringify of [0, ...20k 200-key records] 6.76 3.73 6.08
includes on 1M strings 7.16 5.95 15.17
normalize of 1M characters 1.19 2.80 1.24
"zz" in a 200k-key object 2.56 1.25 8.68
a == "x" on 1M elements 6.02 4.76 11.96
"" + a on 1M elements 5.96 4.60 11.84
template of 1M elements 5.25 3.42 11.18
=== of two 4 MB strings 0.05 0.05 0.05
< of two 4 MB strings 0.04 0.07 0.07
[x, ...r] = 1M elements 4.92 2.66 13.16
{k0, ...r} = 100k keys 4.07 1.28 4.24
for…in 200k keys, then break 1.91 0.86 4.34
new Set of a 1M-character string 0.59 0.35 5.47

On a quiet host every workload stops within about 7.5 s in Node and Bun. In workerd, everything stops within about 15 s, against 8.5 s for the interpreted loop alone; the ones above 8.5 s spend most of that time building their input with callbacks. Under load these take longer: PR Reviewer saw 8.3–9.6 s in Bun with other programs running, and a loop that sorts a 1M array in place with a comparator takes 10–16 s. They stop at the budget either way; how long that takes is the step rate the host gives them. (This table is from round 2; round 3 adds a charge to string + only, at most about 1.7k steps per op for these sizes.)

Ordinary programs fit. These are the steps each one is charged (smallest passing budget, Bun), with Bun and workerd wall times:

Program Steps Bun s workerd s
group 50k rows with Map.get, 10, 50 or 500 groups 1.48 M (each) 0.70–0.74 4.79–4.81
merge 20k records into a 20k index with in 0.85 M 0.52 2.15
same with Object.hasOwn 0.63 M 0.44 2.10
Object.assign one key at a time, 20k times 0.32 M 0.14 0.66
filter + map 20k records 0.49 M 0.23 0.99
split + regex 20k log lines 0.48 M 0.29 2.08
build a 554 KB report of 5,000 lines with += (round 3) 0.43 M 0.04 0.29
comparator sort of 50k objects 3.94 M 3.16 17.05

A comparator sort of 100k objects needs about 20 M steps (every comparison is interpreted), so the default stops it. That's documented with how to raise the budget through EXECUTOR_MCP_MAX_STEPS. In workerd, a callback-heavy program near the budget can take up to about 20 s, as the 50k sort shows.

Choosing the default

All rates were measured on one host (shuvdev):

Runtime Steps per second (pure loop)
workerd 2026-09-18 (self-host image and Cloud), patched CodeMode bundled ~590,000 (5 M steps in 8.5 s)
Bun self-host e2e server ~760,000 (the e2e runaway, 5 M in 6.5 s)
Node 24 ~3 million
  • Ordinary production executes spend 0.1–0.5 s before their first tool call (motel telemetry, Nov 18). The runaway spent 203 s, about 110 M steps at workerd's rate.
  • 5 M steps is about 9 s of looping in workerd, close to the geometric middle (√(0.5 × 203) ≈ 10 s).

Proof

Scenario mcp-execute-step-budget.spec.ts (self-host) runs on the real self-host server through MCP with the default budget. It passed on the final code:

  • Runaway. A program wraps while (true) in nested try/catch/finally and tries to return from its outer catch. It fails with TimeoutExceeded naming the 5,000,000-step budget, with toolCalls: [], in 6.5 s (limit: under 60 s; the wall budget is 300 s).
  • Other callers stay fast. A second caller sent an ordinary tool-calling program the whole time. 195 rounds completed, the slowest in 72 ms. Run quietly, the same program took 32–556 ms (556 ms was the cold first call).
  • Native runaway. The round 0 PoC (s.split("") on a 1 MB string, 100,000 times) fails with the same TimeoutExceeded in 3.3 s (limit: 20 s).
  • Operator runaway. a == "x" on a 1M-element array, 100,000 times, fails with the same TimeoutExceeded in 5.1 s (limit: 20 s).
  • Dedupe with in. Merging 20k records into a 20k-record index with !(r.id in byId) completes ok with 10,000 added in 502 ms (limit: 20 s).
  • Group-by. Grouping 50,000 rows with g.get(k) ?? [] / push / set completes ok in 888 ms for 10 groups and 900 ms for 50.
  • New (round 3): object group-by. a = g[r.k] || []; a.push(r.v); g[r.k] = a over 50,000 rows completes ok in 765 ms (limit: 20 s). On round 2 it took 10.8 s in Node.
  • New (round 3): shared value. r.meta = meta (50,000 columns) on each of 20,000 rows completes ok in 370 ms (limit: 20 s). On round 2 it ran past a 20 s timeout.
  • New (round 4): grouping row objects. g[r.k] = g[r.k] || []; g[r.k].push(r) over 50,000 row objects into 10 groups completes ok in 868 ms, and the reduce form in 884 ms (limit: 20 s). On round 3 both stopped at the budget.
  • New (round 4): template runaway. `${s}${i}` on a 1 MB string, read with charCodeAt, 1M times, fails with TimeoutExceeded at the budget in 1.6 s (limit: 20 s). On round 3 it ran past a 20 s timeout.
  • New (round 3): string + runaway. t = s + i on a 1 MB string, read with charCodeAt, 1M times, fails with the same TimeoutExceeded in 1.5 s (limit: 20 s). On round 2 it ran past a 20 s timeout.
  • Filtering and mapping 10,000 records, then JSON.stringify, completes ok in 131 ms.
  • Three tool calls with a 200,000-iteration loop between them complete ok in 570 ms, all three calls success.

Other runs on the final code:

  • bun run check passed: patches:check (all 24 patches apply), format, lint, typecheck and the rest.
  • bun run e2e:check passed.
  • Self-host, 5/5 passed: the step-budget scenario, the 3 hosted mcp-execute-concurrency scenarios, and the mcp-execute-lazy stalled-app timeout scenario (its while (true) {} stops at the step budget and still reports TimeoutExceeded).
  • Cycle check, round 4: 20 programs, including 6 through self-stores. Every cycle (a.push(a), o.x = o, nested and indirect cycles after the objects were cached as leaves, through splice, fill, unshift, Object.assign and index assignment) is still rejected; a sorted leaf, a shared value and a diamond still store.
  • Behavior check, round 2 build against round 1: 17 small programs covering in, == with null, coercion of arrays holding functions, string comparison, unary operators, templates, rest, for…in, Set from strings, Object.assign, Object.hasOwn and Map.get give identical results. The one exception is "a" in {a: {f: fn}}, which now returns true where it used to throw.

Limits and follow-ups

  • One native call can't be interrupted mid-call; backtracking regexes and three reference walks (++/-- on a property, Object.fromEntries entries, non-string string-method arguments) aren't charged yet (see residual gaps above). These are for a follow-up PR.
  • Building a large string with += is charged quadratically: about 18,000 appended lines (2.2 MB) spend the default budget. join an array instead.
  • The rates are calibrated on one host. The default is meant to stop work, not to bill it precisely; native costs differ by engine, so no single rate fits Node, Bun and workerd.
  • This bounds each program but doesn't isolate programs from the product isolate. Moving them to the apps workerd or a dynamic worker is the structural fix.
  • I didn't benchmark production hardware. If the default trips legitimate programs, EXECUTOR_MCP_MAX_STEPS raises it without a code change.

This fork's CI (fork-ci.yml) runs bun run check and a fixed list of scenarios. It doesn't read the block below, so the e2e runs above are the proof. e2e:check-description against upstream's layout fails on a scenario this PR doesn't touch (cloudOAuthProxyRefusal has no CI job).

mcp-execute-step-budget.spec.ts
mcp-execute-concurrency.spec.ts
mcp-execute-lazy.spec.ts
mcp-execute-failures.spec.ts
mcp-catalog.spec.ts
local-mcp-connections.spec.ts
local-pair.spec.ts

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.
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.
…am 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.
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.
`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.
@shuv1337
shuv1337 merged commit 31beaf3 into v2 Nov 19, 2026
4 checks passed
shuv1337 added a commit that referenced this pull request Nov 19, 2026
…search in use (#107)

* Stop the store walk at any value it has walked, not only leaves

A shared value of many small records, such as a meta object of 1,000
records attached to every row, was walked record by record on every
store: about 500 steps each, 10 million for 20,000 rows, so the program
stopped at the 5,000,000-step budget.

The store walk now remembers every object whose whole graph it checked.
A leaf stays remembered until an object is stored into it; any other
object is remembered for the current epoch, and storing an object into a
remembered object that another remembered object holds starts a new
epoch. A differential fuzz of 3.9 million stores against a fresh
reachability check agrees on all of them and fails at once when the
epoch is not advanced.

mcp-execute-step-budget: 20,000 rows sharing a 1,000-record meta now
complete in 283 ms (TimeoutExceeded at 1,317 ms before); storing a row
into that shared value is still rejected as a cycle; runaways still stop
with TimeoutExceeded.

* Never evict a search still being paged, and keep oversized searches apart

Kept search orders (#106) had three gaps:
- room was checked before the ranking await and eviction after it was
  strict LRU, so new searches ranking at once evicted searches still in
  use; an order is now admitted after the await, evicting only searches
  unread for 30 s, or not kept;
- idle time read the wall clock; it now reads the monotonic clock;
- a search too large to keep was remembered in the same cache with its
  key's bytes, and a key past 6 MB evicted every kept search; such
  searches are now remembered in their own bounded map, and a key too
  large to keep is never remembered.

mcp-search-kept-orders covers the first and third: both assertions fail
on the previous store and pass on this one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant