#20471: fix: share relay auth state across bundler chunks via globalThis
size: XS
trusted-contributor
Cluster:
GlobalThis Integration Fixes
Fixes #20457
## What changed
- Modified `src/browser/extension-relay.ts` to store the relay auth and server Maps on `globalThis` using `Symbol.for()` keys
- This ensures that when Rolldown bundles the module into multiple chunks (one for gateway/control-service and one for pi-embedded-helpers), both chunks share the same Map instances
- Previously, each chunk had its own `relayAuthByPort` and `serversByPort` Maps, causing the agent tool to read from an empty Map while the gateway stored auth tokens in its own copy
## AI-assisted contribution
- This fix was generated by an AI agent (OpenClaw cron: gh-issues-fix)
- Testing depth: validated with pnpm build && pnpm tsgo && pnpm format:check && pnpm test -- --run src/browser/extension-relay.test.ts
- The fix addresses the root cause described in the issue by using globalThis with Symbol.for() to share state across bundler chunks
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Replaced module-scoped Maps with globalThis-backed Maps using `Symbol.for()` to share relay authentication state and server instances across Rolldown bundler chunks. This prevents each chunk from maintaining separate Map instances, ensuring the agent tool can access auth tokens stored by the gateway service.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is a well-understood pattern for sharing state across bundler chunks using globalThis and Symbol.for(). The implementation includes proper type guards (instanceof Map checks), maintains backward compatibility by reusing existing Maps if present, and is tested by the existing test suite. The change is minimal, focused, and addresses a specific bundler issue without affecting runtime behavior when the module is loaded normally.
- No files require special attention
<sub>Last reviewed commit: 6815608</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#14746: fix(hooks): use globalThis for handler registry to survive bundler ...
by openperf · 2026-02-12
77.7%
#21277: fix(browser): dedupe concurrent relay init and await shared startup...
by HOYALIM · 2026-02-19
75.1%
#23019: fix(hooks): use globalThis singleton for internal hooks handlers Map
by karmafeast · 2026-02-21
74.9%
#22399: fix(web): use globalThis singleton for active-listener state
by mcinteerj · 2026-02-21
74.2%
#17588: fix(relay): survive WS disconnects and MV3 worker restarts
by Unayung · 2026-02-15
72.1%
#22571: fix(browser): complete extension relay handshake on connect.challenge
by pandego · 2026-02-21
72.0%
#21314: feat: enhance browser relay with custom naming and diagnostic tools
by kelvinCB · 2026-02-19
71.7%
#22478: fix(diagnostics-otel): wire OTLP exporter to emit traffic to config...
by LuffySama-Dev · 2026-02-21
71.3%
#19353: fix(diagnostics-otel): fix cross-chunk module isolation breaking even…
by nez · 2026-02-17
70.2%
#16865: fix(diagnostics-otel): share listeners/transports across module bun...
by leonnardo · 2026-02-15
70.1%