Repository navigation
fix(codemode): give errors a stack - #64939
Conversation
There was a problem hiding this comment.
The change looks sound. I ran the same probe programs on the base and on this branch. On the base, e.stack is undefined, so e.stack.split("\n") throws inside the catch. With this change:
- Errors made with
new Errorget named program frames, innermost first. - Callbacks show as
<anonymous>. - Built-in failures and caught tool failures get a single
at <program>:line:colline. - Assigning
e.stackkeeps the new value, and it survivesstructuredClone. - What the host sees for uncaught or returned errors is unchanged.
The targeted test files pass, including in round-trip mode, and tsc --noEmit is clean.
Public API: unchanged. stack exists only inside the sandboxed program; no SDK, server, plugin, config or CLI surface changes.
There was a problem hiding this comment.
The new commit looks sound. It cuts frame names at 200 characters and caps the whole stack at the program's longest string, so a long function name can no longer push the stack past the normal string limit. My earlier review had no open points. The targeted test files pass (172/172, including the new ones), tsc --noEmit is clean, and ordinary stack output is unchanged in my probes. Public API: unchanged.
|
Removed the size caps from this PR at the maintainer's request. |
There was a problem hiding this comment.
This revert takes away the cap on stack length that the previous commit added. Without it, a program can build an error stack far past the sandbox's string limit and use a lot of host memory doing it. Details are in the inline comment.
The targeted test files pass (169/169) and tsc --noEmit is clean. Public API: unchanged.
| export const createErrorValue = (prototype: Obj, message: string | undefined, trace = ""): ErrorObj => { | ||
| const value = new ErrorObj(prototype) | ||
| if (message !== undefined) define(value, "message", message, hidden) | ||
| define(value, "stack", value.toString() + trace, hidden) |
There was a problem hiding this comment.
Without the cap from the reverted commit, stack can grow past the sandbox's string limit. With a function name of 2²⁴ characters (const o = { ["m".repeat(2 ** 24)](n) { … } }) recursing 12 deep, new Error().stack.length is 167,772,418, about 10× MAX_STRING_LENGTH. Every other string operation rejects that length (s + s throws RangeError: Invalid string length). Keeping 10 such stacks (forcing each with an indexOf call) took the host process to about 1.8 GB of memory in about 1 s. Could the cap on frame names or on the whole stack come back, or is there a reason the revert is safe?
20fad77 to
0740f37
Compare
|
Pushed: the |
There was a problem hiding this comment.
The new commit, which fills in the stack's first line (name: message) on first read, works as described. A subclass that sets this.name in its constructor gets its own name in the stack. A name changed before the first read shows up, and the stack stays the same after that. Assigning stack, defineProperty, Object.freeze, structuredClone and snapshot round trips (unread and already read) all behave correctly. The targeted test files pass, including in round-trip mode, and tsc --noEmit is clean.
Public API: unchanged. The snapshot codec's new flag for unread stacks is internal.
The earlier point about capping the length of stack is not addressed yet; that thread stays open.
Ported from anomalyco/opencode#64939 User decisions: - psychoscript holds only the language, nothing tool-specific
Summary
Errors in Code Mode had no
stack(a documented gap), so common error handling likee.stack.split("\n")threw a TypeError inside thecatch, andconsole.log(e.stack)printedundefined. Now every Error has an own, non-enumerablestackstring in V8's format.new ErrorError: boom\n at inner (/path/s.js:1:27)\n at outer (/path/s.js:2:21)\n at …Error: boom\n at /path/s.js:3:1undefinedError: boom\n at inner (<program>:1:27)\n at outer (<program>:2:21)\n at <program>:3:1null.xTypeError: Cannot read properties of null (reading 'x')\n at …TypeError: null is not an object …\n at …undefinedTypeError: Cannot read properties of null (reading 'x').\n at <program>:4:7undefinedError: <tool message>\n at <program>:5:7this.namein its constructorMyErr: xMyErr: xMyErr: xe.name = "Late"before the first readLate: yLate: yLate: ye.namechanged after the first readObject.keys(e)/JSON.stringify(e)stackdescriptor{ get, set }{ writable, configurable }{ writable, configurable }, non-enumerablenameError: …)Error: …)new Error(...)(and every error constructor, called with or withoutnew) records up to 10 program frames, innermost first. Built-in frames are skipped, and top-level code shows as a bareat <program>:line:col. This only walks the VM's frame array and does a binary search per frame. It runs when an error is constructed, not on every throw.name: messageheader is formatted the first timestackis read, using the error'snameandmessageat that moment (theError.prototype.toStringrules: name defaults toError, and an empty side is dropped). After that the value is fixed, and assigninge.stack = …replaces it. So a subclass that setsthis.namein its constructor gets its own name in the header, which matches Node and Bun. The header readsnameandmessagethe same way display does, without running a program getter (one reads asundefined). Bun does the same; Node runs the getter.stackhas one line at the expression that raised them. Host and tool failures have no site of their own, so theirs points at theawaitortryframe that caught them.stackis an own data slot that formats itself on first read (StackSlot). A snapshot keeps a stack nobody has read yet unformatted, so a name set after resuming still shows up in it. A stack that has already been read is saved as a plain string.structuredClonereads it. Locations are 1-based line and column in the submitted source, with<program>as the file name, so no host or internal path appears. A host Error's ownstackstill never crosses: a converted error gets only its header line.interpreter-support.md: the "Errors have no stack" note is gone, the new behavior is checked off with the Node/Bun differences above, andError.captureStackTrace,Error.stackTraceLimitandat asyncframes are listed as open gaps.Testing
bun test test/errors.test.ts test/codemode.test.ts test/extensions.test.ts test/snapshot.test.ts. New tests cover: frame names and order, the single-frame stack for built-in failures, the 10-frame limit, non-enumerability withJSON.stringifyandObject.keysunchanged, a caught tool failure, a subclass name in the header,name/messagechanged before the first read, the value staying fixed after it, assignment before and after the first read, and snapshot round trips both before and after the first read. I checked that the unread-stack round trip fails if the codec formats the stack early.packages/codemodesuite: 1870/1870 passed.bun typecheckinpackages/codemode, andbun run checkat the repo root.Visual