Skip to content

fix(ai): replace static async_hooks require in eval context - #279

Open
gilfoyle-sre[bot] wants to merge 11 commits into
mainfrom
fix/static-async-hooks-import
Open

fix(ai): replace static async_hooks require in eval context#279
gilfoyle-sre[bot] wants to merge 11 commits into
mainfrom
fix/static-async-hooks-import

Conversation

@gilfoyle-sre

@gilfoyle-sre gilfoyle-sre Bot commented Mar 5, 2026

Copy link
Copy Markdown

Summary

  • replace dynamic createRequire loading of async_hooks in packages/ai/src/evals/context/manager.ts with a static AsyncLocalStorage import
  • remove fallback context-manager path that existed only for dynamic Node/bundler detection
  • clean up packages/ai/tsup.config.ts externals by removing shim-only entries (async_hooks, module, node:module) and keeping node:async_hooks

Why

@axiomhq/console hit Dynamic require of "async_hooks" is not supported on SDK 0.48.0. The eval context manager was emitting runtime require logic. Static import avoids that code path entirely.

Validation

  • cd packages/ai && pnpm build
  • cd packages/ai && pnpm test
  • verified built output uses static import form (no createRequire, no require("async_hooks")) in dist/chunk-Z24VQAZC.js

Note

Medium Risk
Touches async-context propagation used by evals and changes fallback behavior to throw on concurrent async runs, which could surface new runtime errors in environments without AsyncLocalStorage. Otherwise the change is localized and covered by new unit tests.

Overview
Avoids dynamic require/createRequire patterns for AsyncLocalStorage by reworking eval context manager loading to prefer process.getBuiltinModule, then legacy require/mainModule.require, then a global AsyncLocalStorage, and finally a fallback manager.

Strengthens the fallback context manager to correctly restore context for promise-like returns and to fail fast on concurrent/nested async runs to prevent context leakage, and adds Vitest coverage for the new resolution paths and fallback edge cases.

Updates tsup config to keep node:async_hooks external (dropping shim-only externals) and sets removeNodeProtocol: false to preserve node: builtin specifiers in the ESM build.

Written by Cursor Bugbot for commit d4ed30c. This will update automatically on new commits. Configure here.

@pkg-pr-new

pkg-pr-new Bot commented Mar 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/axiom@279

commit: d4ed30c

@gilfoyle-sre
gilfoyle-sre Bot force-pushed the fix/static-async-hooks-import branch from c5326aa to e9b4bee Compare March 5, 2026 23:49
Comment thread packages/ai/src/evals/context/manager.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

return globalAsyncLocalStorage as AsyncLocalStorageLikeConstructor;
}

return undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AsyncLocalStorage unavailable on Node 20 ESM after removing createRequire

Medium Severity

On Node.js 20 running ESM, process.getBuiltinModule is not available (added in v22.3.0, not backported to Node 20 LTS). In ESM mode, globalThis.require and process.mainModule.require are also unavailable. globalThis.AsyncLocalStorage is not a global in Node.js. All four strategies in getAsyncLocalStorageConstructor fail, silently falling back to the in-memory manager. The old code used createRequire from node:module which worked in ESM on all Node versions. Since the context manager is reachable from the main SDK entry points (via storage.tsapp-scope.ts / context.ts), this affects all users on Node 20 ESM, not just eval users — context propagation across await boundaries will silently break.

Additional Locations (1)

Fix in Cursor Fix in Web

@gilfoyle-sre gilfoyle-sre Bot changed the title fix ai async_hooks dynamic require in eval context fix(ai): replace static async_hooks require in eval context Mar 6, 2026
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