Skip to content

Fix Vite SSR agentContext duplication#891

Merged
threepointone merged 3 commits into
mainfrom
opencode/issue880-20260211135740
Feb 11, 2026
Merged

Fix Vite SSR agentContext duplication#891
threepointone merged 3 commits into
mainfrom
opencode/issue880-20260211135740

Conversation

@ask-bonk

@ask-bonk ask-bonk Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

The fix is committed and ready. Here's a summary of what was done:

Summary

Problem: When @cloudflare/ai-chat and agents are used with Vite SSR, the agentContext AsyncLocalStorage gets duplicated into two separate instances. This happens because @cloudflare/ai-chat imports agentContext via the bare specifier agents/internal_context, while the Agent class in agents imports it via ./internal_context. Vite SSR pre-bundling treats these as separate modules and inlines agents/internal_context into its bundled copy of @cloudflare/ai-chat, creating a second AsyncLocalStorage — so getCurrentAgent().connection returns undefined.

Fix (2 lines changed):

  1. packages/agents/src/index.ts:3 — Added export { agentContext } from "./internal_context" to re-export agentContext from the main agents entry point

  2. packages/ai-chat/src/index.ts:17 — Changed import { agentContext } from "agents/internal_context" to import agentContext from "agents" (alongside Agent, Connection, etc.)

This ensures that when Vite SSR pre-bundles @cloudflare/ai-chat, it sees agentContext coming from the same agents module as everything else — no separate subpath module for Vite to split and duplicate. The ./internal_context subpath export is kept for backward compatibility but is no longer used cross-package.

Verification: Build passes, all checks pass (sherif, exports, formatting, linting, typecheck across 35 projects), and all 458 Workers tests pass.

Closes #880

github run

Re-export agentContext from the main agents entry point and change
@cloudflare/ai-chat to import it from 'agents' instead of the
'agents/internal_context' subpath. This ensures both packages share the
same AsyncLocalStorage instance even when Vite SSR pre-bundles
@cloudflare/ai-chat separately.

Fixes #880
@changeset-bot

changeset-bot Bot commented Feb 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4e5224f

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

This PR includes changesets to release 2 packages
Name Type
agents Patch
@cloudflare/ai-chat 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

@threepointone

Copy link
Copy Markdown
Contributor

/bonk I want to make it so that this isn't a public api, so it should have a scary name "DO NOT USE THIS WILL BREAK"

@pkg-pr-new

pkg-pr-new Bot commented Feb 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@891

commit: 4e5224f

Co-authored-by: threepointone <threepointone@users.noreply.github.com>
@ask-bonk

ask-bonk Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor Author

Everything looks correct. Here's a summary of the changes:

Changes Made

The agentContext export from the agents package has been renamed to __DO_NOT_USE_WILL_BREAK__agentContext to signal that it's an internal API not meant for public consumption.

Files changed:

  1. packages/agents/src/internal_context.ts - The primary AsyncLocalStorage instance is now exported as __DO_NOT_USE_WILL_BREAK__agentContext. The old agentContext name is kept as a deprecated alias for backward compatibility with anyone using the agents/internal_context subpath export.

  2. packages/agents/src/index.ts - Imports the scary name and aliases it locally to agentContext (so all internal code stays clean). The re-export uses the scary name __DO_NOT_USE_WILL_BREAK__agentContext.

  3. packages/ai-chat/src/index.ts - Imports __DO_NOT_USE_WILL_BREAK__agentContext from "agents" and aliases it to agentContext locally.

  4. .changeset/fix-vite-ssr-agent-context-duplication.md - Updated description to mention the rename.

Verification:

  • Build passes for all 4 packages
  • All checks pass (sherif, export checks, oxfmt, oxlint, typecheck across 35 projects)
  • Workers tests pass (the Playwright/React tests fail due to missing Chromium browser, which is a pre-existing CI environment issue)

github run

@threepointone

Copy link
Copy Markdown
Contributor

/bonk I don't want backward compatibility, this was never a public api, and I'm fine breaking anyone that used it.

…h export

Drop the deprecated agentContext re-export from internal_context.ts and
remove the agents/internal_context subpath from package.json exports.
This was never a public API.
@ask-bonk

ask-bonk Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor Author

Done. Here's a summary of the changes:

Changes Made

  1. packages/agents/src/internal_context.ts -- Removed the deprecated agentContext alias. Only __DO_NOT_USE_WILL_BREAK__agentContext remains as the sole export.

  2. packages/agents/package.json -- Removed the ./internal_context subpath export entirely. Anyone importing from agents/internal_context will now get a module-not-found error.

  3. .changeset/fix-vite-ssr-agent-context-duplication.md -- Updated the changeset description to document the removal.

Verification

  • Build passes for all 4 packages
  • All checks pass (sherif, export checks, oxfmt, oxlint, typecheck across 35 projects)
  • Workers tests pass (Playwright/React tests fail due to missing Chromium -- pre-existing CI environment issue)

github run

@threepointone
threepointone merged commit 0723b99 into main Feb 11, 2026
4 checks passed
@threepointone
threepointone deleted the opencode/issue880-20260211135740 branch February 11, 2026 14:25
@github-actions github-actions Bot mentioned this pull request Feb 11, 2026
Muhammad-Bin-Ali pushed a commit that referenced this pull request Feb 11, 2026
* fix: prevent agentContext duplication by Vite SSR pre-bundling

Re-export agentContext from the main agents entry point and change
@cloudflare/ai-chat to import it from 'agents' instead of the
'agents/internal_context' subpath. This ensures both packages share the
same AsyncLocalStorage instance even when Vite SSR pre-bundles
@cloudflare/ai-chat separately.

Fixes #880

* Rename agentContext export to scary name

Co-authored-by: threepointone <threepointone@users.noreply.github.com>

* Remove backward-compat agentContext alias and internal_context subpath export

Drop the deprecated agentContext re-export from internal_context.ts and
remove the agents/internal_context subpath from package.json exports.
This was never a public API.

---------

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: threepointone <threepointone@users.noreply.github.com>
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.

agents/internal_context gets duplicated by Vite SSR pre-bundling, breaking getCurrentAgent()

1 participant