Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,24 @@ export interface NativeInterceptState {
// Symbol would let a second copy re-install on top of the first).
const INTERCEPT_FLAG = Symbol.for("billion-context.native-fetch-intercept");

// #3394: which intercept state routes RIGHT NOW, read by the fetch chain at
// call time. INTERCEPT_FLAG only marks "a chain exists"; this slot carries the
// live instance. A host hot-reimport (DSH 0.2.1-alpha.3 profile recomposition)
// re-applies with a FRESH state — the once-per-process chain must follow that
// new instance instead of staying closed over the first one's dead state.
const ACTIVE_STATE = Symbol.for("billion-context.native-intercept-state");
// #3394: which intercept state routes RIGHT NOW, read by the fetch chain AND
// the Codex Responses WS chain (#3558) at call time. INTERCEPT_FLAG /
// WS_INTERCEPT_FLAG only mark "a chain exists"; this ONE slot carries the live
// instance. A host hot-reimport (DSH 0.2.1-alpha.3 profile recomposition)
// re-applies with a FRESH state — the once-per-process chains must follow that
// new instance instead of staying closed over the first one's dead state. Both
// transports share this single slot so the whole plugin instance moves
// atomically when a new module instance is applied.
export const ACTIVE_STATE = Symbol.for("billion-context.native-intercept-state");

// #3394 hardening: install generation paired with ACTIVE_STATE, bumped by
// every installNativeFetchIntercept call. Object identity alone cannot tell
// apart "a newer module instance" from "a newer activation of the SAME
// instance" (dsh deactivate/reactivate re-applies the module-level state,
// #2082/#2101) — the generation stops an older activation's late dispose from
// clearing a slot a later apply() already rebound.
const ACTIVE_STATE_SEQ = Symbol.for("billion-context.native-intercept-state-seq");
// #3394 hardening: install generation paired with ACTIVE_STATE, bumped by every
// installNativeFetchIntercept / installNativeWebSocketIntercept call (#3558).
// Object identity alone cannot tell apart "a newer module instance" from "a
// newer activation of the SAME instance" (dsh deactivate/reactivate re-applies
// the module-level state, #2082/#2101) — the generation stops an older
// activation's late dispose from clearing a slot a later apply() already
// rebound.
export const ACTIVE_STATE_SEQ = Symbol.for("billion-context.native-intercept-state-seq");

// #2090: the built-in-scheme passthrough opt-in (#1884 escape hatch) is the
// only path where a signed request goes DIRECT — make it LOUD in host logs so
Expand Down Expand Up @@ -642,6 +646,22 @@ export function replaceRequestPayload(e: { request?: unknown }, body: string, ex
}
}

/** Re-point the process-level live-state slot at the activating instance and
* advance its generation, so every transport (fetch + Codex Responses WS)
* follows the newest applied module instance (#3394/#3558). Called at the TOP
* of every install — including the ones that find a chain already present and
* bail — because "which instance is active" is independent of whether THIS
* transport's wrapper got (re)installed. Returns true when the slot was empty
* before: the caller then warns once, since a pre-live-state chain cannot be
* rewritten to read the slot until the host restarts. */
export function bindLiveStateSlot(state: NativeInterceptState): boolean {
const g = globalThis as Record<PropertyKey, unknown>;
const firstBind = g[ACTIVE_STATE] === undefined;
g[ACTIVE_STATE_SEQ] = Number(g[ACTIVE_STATE_SEQ] ?? 0) + 1;
g[ACTIVE_STATE] = state;
return firstBind;
}

/** Install the global fetch patch. The first call wraps globalThis.fetch and
* returns true; later calls return false (a process installs one chain) BUT
* always rebind the live-state slot, so a host hot-reimport that re-applies
Expand All @@ -652,16 +672,12 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
// reads g[ACTIVE_STATE] per request, so the newest applied instance wins
// even though the chain itself was wrapped only once.
const hadChain = g[INTERCEPT_FLAG] === true;
const hadSlot = g[ACTIVE_STATE] !== undefined;
// Bumped before the bind so every activation owns a distinct generation
// even when it re-applies the SAME state object (#2082/#2101).
g[ACTIVE_STATE_SEQ] = Number(g[ACTIVE_STATE_SEQ] ?? 0) + 1;
g[ACTIVE_STATE] = initialState;
const firstBind = bindLiveStateSlot(initialState);
if (hadChain) {
// A chain from an EARLIER build already wraps fetch and cannot be
// rewritten to read this slot — until the host restarts, requests route
// through that old closure. Say so once instead of failing silent.
if (!hadSlot) logHostEvent("warn", "[native-client]", "native fetch chain predates live-state routing; restart the host once for model requests to follow this plugin instance (#3394)");
if (firstBind) logHostEvent("warn", "[native-client]", "native fetch chain predates live-state routing; restart the host once for model requests to follow this plugin instance (#3394)");
return false;
}
// Shared across every re-armed chain link (a re-arm replaces the chain
Expand Down
37 changes: 31 additions & 6 deletions src/agent/native-ws-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// matching the fetch lane's give-up semantics. Explicit `sse` remains the
// documented escape hatch either way.

import { noteRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js";
import { ACTIVE_STATE, ACTIVE_STATE_SEQ, bindLiveStateSlot, noteRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js";
import { logHostEvent } from "./host-log.js";

// @types/node exposes Event/EventTarget globally but keeps the web-platform
Expand Down Expand Up @@ -327,9 +327,25 @@ let ourGet: (() => unknown) | undefined;
* lifecycle-bound to the same guard as installNativeFetchIntercept: off mode
* patches nothing. Third-party replacements are ADOPTED as the downstream
* constructor (authority is kept, like the fetch lane) instead of silently
* evicting routing; non-constructor values are refused with a warning. */
export function installNativeWebSocketIntercept(state: NativeInterceptState): boolean {
if (nativeWebSocketInterceptInstalled()) return false;
* evicting routing; non-constructor values are refused with a warning. Every
* activation rebinds the SHARED live-state slot (#3558), so the wrapper's
* per-connection read follows the newest applied module instance across a hot
* reimport — mirroring the fetch lane exactly. */
export function installNativeWebSocketIntercept(initialState: NativeInterceptState): boolean {
// #3558: rebind the shared live-state slot on EVERY activation (the same
// slot the fetch chain reads), then resolve it per connection below — so a
// hot-reimport that swaps in a fresh state takes over WS routing without a
// restart, instead of the once-per-process wrapper staying closed over the
// first instance's dead state.
const hadChain = nativeWebSocketInterceptInstalled();
const firstBind = bindLiveStateSlot(initialState);
if (hadChain) {
// A wrapper from an EARLIER build already wraps globalThis.WebSocket and
// cannot be rewritten to read this slot — until the host restarts, Codex
// Responses traffic routes through that old closure. Say so once.
if (firstBind) logHostEvent("warn", "[native-client]", "native WebSocket chain predates live-state routing; restart the host once for Codex Responses traffic to follow this plugin instance (#3558)");
return false;
}
const initial = (globalThis as GlobalRecord).WebSocket;
if (typeof initial !== "function") return false;

Expand Down Expand Up @@ -368,7 +384,12 @@ export function installNativeWebSocketIntercept(state: NativeInterceptState): bo
function routed(this: unknown, url: string | URL, ...rest: unknown[]): unknown {
const urlStr = typeof url === "string" ? url : url instanceof URL ? url.href : String(url);
const target = codexResponsesWsTarget(urlStr);
if (target === undefined) return new RoutedWebSocket(url, ...rest);
// #3558: resolve the ACTIVE state per connection (mirrors the fetch
// lane's per-request read, #3394) instead of the install-time closure —
// a hot-reimport swapped in a fresh instance. Empty slot = no active
// plugin → pass through direct, never hit a disposed context.
const state = (globalThis as GlobalRecord)[ACTIVE_STATE] as NativeInterceptState | undefined;
if (target === undefined || state === undefined) return new RoutedWebSocket(url, ...rest);
if (state.takeoverGate !== undefined && state.takeoverGate(target) === false) {
state.onDispatch?.(target, "refused");
return new RoutedWebSocket(url, ...rest);
Expand Down Expand Up @@ -457,7 +478,9 @@ export function installNativeWebSocketIntercept(state: NativeInterceptState): bo
}

/** Test hook: restore the pre-install globalThis.WebSocket (identity-compared
* accessor, mirroring the fetch lane's #1410 discipline) and clear the flag. */
* accessor, mirroring the fetch lane's #1410 discipline), clear the flag, and
* drop the shared live-state slot (#3558) so a suite that installed only the WS
* wrapper leaves no stale active state for a later suite. */
export function _resetForTest(): void {
if (preInstallDesc !== undefined && ourGet !== undefined) {
const d = preInstallDesc;
Expand All @@ -470,4 +493,6 @@ export function _resetForTest(): void {
}
}
delete (globalThis as GlobalRecord)[WS_INTERCEPT_FLAG];
delete (globalThis as GlobalRecord)[ACTIVE_STATE];
delete (globalThis as GlobalRecord)[ACTIVE_STATE_SEQ];
}
67 changes: 66 additions & 1 deletion tests/native-ws-intercept.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "node:test";
import assert from "node:assert/strict";
import type { NativeInterceptState } from "../src/agent/native-intercept.ts";
import { liveStateSlotEffect, type NativeInterceptState } from "../src/agent/native-intercept.ts";
import { _resetForTest, codexResponsesWsTarget, installNativeWebSocketIntercept, nativeWebSocketInterceptInstalled, rewriteCodexResponsesWsUrl } from "../src/agent/native-ws-intercept.ts";

// Same @types/node quirk as in src: the listener callback type is not global.
Expand Down Expand Up @@ -253,6 +253,71 @@ test("instanceof holds against both the wrapper and the underlying constructor",
});
});

// ——— Live-state routing across hot-reimport (#3558) ————————————————

test("install: a hot-reimport rebinds WS routing to the new instance's state (#3558)", async () => {
await withPatchedGlobal(async () => {
// Two distinct module instances: A booted first, B is the FRESH state a
// host hot-reimport re-applies in the SAME process with no reset between.
const dispatchedA: Array<[string, string]> = [];
const dispatchedB: Array<[string, string]> = [];
const stateA: NativeInterceptState = {
origin: "http://127.0.0.1:40901",
ready: Promise.resolve("http://127.0.0.1:40901"),
onDispatch: (url, action) => dispatchedA.push([url, action]),
};
const stateB: NativeInterceptState = {
origin: "http://127.0.0.1:40902",
ready: Promise.resolve("http://127.0.0.1:40902"),
onDispatch: (url, action) => dispatchedB.push([url, action]),
};
assert.equal(installNativeWebSocketIntercept(stateA), true);
// The hot-reimport path: install again with B's state. One wrapper per
// process (returns false) but the shared live slot must rebind to B.
assert.equal(installNativeWebSocketIntercept(stateB), false);
const ws = new (WsCtor<FakeWs>())(UPSTREAM_WS, {});
assert.ok(ws instanceof FakeWs);
// Routing must follow B — the newest applied instance: B's origin rewrite
// AND B's dispatch evidence. Pre-#3558 the wrapper closed over A → 40901.
assert.equal(ws.url, "ws://127.0.0.1:40902/bili/https://chatgpt.com/backend-api/codex/responses");
assert.deepEqual(dispatchedB, [["ws://127.0.0.1:40902/bili/https://chatgpt.com/backend-api/codex/responses", "rewrite"]]);
assert.deepEqual(dispatchedA, []);
});
});

test("install: a same-instance re-activation outranks an older activation's late dispose (#3558)", async () => {
await withPatchedGlobal(async () => {
// Deactivate/reactivate (#2082/#2101) re-applies the SAME module-level
// state object under a fresh context. If activation 1's effect cleanup
// fires LATE — after activation 2's apply() already rebound the slot —
// object identity cannot tell the activations apart; only the install
// generation can. A wrong clear silently drops WS routing to direct.
const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) };
assert.equal(installNativeWebSocketIntercept(state), true);
// Activation 1's effect runs post-commit: capture its disposer.
const disposeActivation1 = liveStateSlotEffect(state)();
// Re-activation: same state object, fresh context, no reset in between.
assert.equal(installNativeWebSocketIntercept(state), false);
disposeActivation1();
const ws = new (WsCtor<FakeWs>())(UPSTREAM_WS, {});
// The late dispose must be a no-op: routing still rides the rebound slot.
assert.equal(ws.url, REWRITTEN);
});
});

test("install: the latest activation's own teardown clears the slot to clean direct (#3558)", async () => {
await withPatchedGlobal(async () => {
const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) };
assert.equal(installNativeWebSocketIntercept(state), true);
// The activation's own teardown fires: generation + identity both match
// the current slot, so the clear lands.
liveStateSlotEffect(state)()();
const ws = new (WsCtor<FakeWs>())(UPSTREAM_WS, {});
// Empty slot → the model WS shape passes through direct, unrewritten.
assert.equal(ws.url, UPSTREAM_WS);
});
});

// ——— Deferred shell (origin still settling) ——————————————————————————————————

test("deferred: window mirrors the WebSocket API, then attaches to the routed connection", async () => {
Expand Down
Loading