Skip to content

fix(quickjs): resolve BigInt serialization crashes in REPL results an…#559

Open
Arjun Shenoy (noishey) wants to merge 10 commits into
langchain-ai:mainfrom
noishey:fix/bigint-quickjs-serialization
Open

fix(quickjs): resolve BigInt serialization crashes in REPL results an…#559
Arjun Shenoy (noishey) wants to merge 10 commits into
langchain-ai:mainfrom
noishey:fix/bigint-quickjs-serialization

Conversation

@noishey

Copy link
Copy Markdown

…d logs

Description

Resolves Issue #558: BigInt values crash QuickJS interpreter result serialization.

This PR adds safe BigInt serialization and formatting within the @langchain/quickjs code interpreter. Prior to this fix, the interpreter would throw a TypeError: Do not know how to serialize a BigInt inside standard host-side stringification paths (formatReplResult, setupConsole, extractToolText), and return "[object Object]" for nested objects containing bigints due to Emscripten bridge JSON limitations.

Key Changes

  • Safe JSON Stringifier: Introduced stringifyJson in utils.ts to cleanly stringify nested objects containing bigint primitives by mapping them to strings instead of crashing.
  • Primitive BigInt Support: Updated formatReplResult to print primitive BigInt cell evaluations without surrounding quotes (matching standard number styling).
  • Resilient safeDump VM Bridge:
    • Implemented a custom safeDump(handle) on ReplSession in session.ts.
    • When standard context.dump() encounters serialization limits for objects containing BigInt and falls back to returning "[object Object]", safeDump automatically invokes a custom guest-side stringifier to prefix bigints with a sentinel ("__BIGINT__:").
    • The host cleanly parses and revives these sentinel string patterns back into native host-side bigint values.
    • Avoids relying on context.isEqual, preserving support for the asyncify WASM variant.
  • Direct Safe Stringification in Host-Paths: Replaced standard JSON.stringify inside host console listeners and tool result extractions with stringifyJson.

Verification & Tests

Automated Tests Added

  • Unit Tests (utils.test.ts):
    • Validated that primitive bigint cell results are formatted cleanly without quotes.
    • Validated that nested arrays/objects containing bigint types serialize to formatted JSON.
  • Integration Tests (session.test.ts):
    • Validated that evaluating 9007199254740993n + 7n yields a native host bigint (9007199254741000n).
    • Validated that evaluating objects/arrays containing bigints correctly returns the fully revived host object with native bigint properties.
    • Validated that console logging deep objects containing bigint properties behaves correctly.

Results

All 206 unit/integration tests and type-checks successfully passed.

pnpm --filter @langchain/quickjs typecheck  # Success (0 errors)
pnpm --filter @langchain/quickjs test       # Success (206/206 passed)
pnpm test                                   # Success (1500+ monorepo tests passed)

@changeset-bot

changeset-bot Bot commented May 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f90e3ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@langchain/quickjs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented May 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

deepagents-acp

npm i https://pkg.pr.new/deepagents-acp@559

deepagents

npm i https://pkg.pr.new/deepagents@559

@langchain/sandbox-standard-tests

npm i https://pkg.pr.new/@langchain/sandbox-standard-tests@559

@langchain/daytona

npm i https://pkg.pr.new/@langchain/daytona@559

@langchain/deno

npm i https://pkg.pr.new/@langchain/deno@559

@langchain/modal

npm i https://pkg.pr.new/@langchain/modal@559

@langchain/node-vfs

npm i https://pkg.pr.new/@langchain/node-vfs@559

@langchain/quickjs

npm i https://pkg.pr.new/@langchain/quickjs@559

commit: f90e3ef

@noishey

Copy link
Copy Markdown
Author

Please Review The PR.

@langsmith-fleet langsmith-fleet Bot added bug Something isn't working needs review labels Jun 1, 2026
Comment thread libs/deepagents/tsconfig.json Outdated
Comment on lines +736 to +780
private safeDump(handle: QuickJSHandle): unknown {
const context = this.context!;
const value = context.dump(handle);
if (value === "[object Object]") {
try {
const stringifyFn = context.evalCode(`
(val) => JSON.stringify(val, (k, v) => typeof v === "bigint" ? "__BIGINT__:" + v.toString() : v)
`);
if (stringifyFn.error) {
stringifyFn.error.dispose();
return value;
}
const stringified = context.callFunction(
stringifyFn.value,
context.undefined,
handle,
);
stringifyFn.value.dispose();

if (stringified.error) {
stringified.error.dispose();
return value;
}

const typeofStringified = context.typeof(stringified.value);
if (typeofStringified === "undefined") {
stringified.value.dispose();
return value;
}

const jsonStr = context.getString(stringified.value);
stringified.value.dispose();

return JSON.parse(jsonStr, (_k, v) => {
if (typeof v === "string" && v.startsWith("__BIGINT__:")) {
return BigInt(v.slice(11));
}
return v;
});
} catch {
return value;
}
}
return value;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for? Context values don't get serialized at .dump so we shouldn't need to indirect to bigint handling here?

@noishey Arjun Shenoy (noishey) force-pushed the fix/bigint-quickjs-serialization branch from 2355cfc to f90e3ef Compare June 16, 2026 12:09
@noishey

Copy link
Copy Markdown
Author

Hunter Lovell (@hntrl) please check the merging of this PR #559

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BigInt values crash QuickJS interpreter result serialization

2 participants