|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { WebSocket } from "ws"; |
| 3 | +import { |
| 4 | + GATEWAY_CLIENT_IDS, |
| 5 | + GATEWAY_CLIENT_MODES, |
| 6 | +} from "../../packages/gateway-protocol/src/client-info.js"; |
| 7 | +import { |
| 8 | + type WorkerConnectParams, |
| 9 | + WORKER_PROTOCOL_FEATURES, |
| 10 | + WORKER_RPC_SET_VERSION, |
| 11 | +} from "../../packages/gateway-protocol/src/schema/worker-admission.js"; |
| 12 | +import type { |
| 13 | + WorkerInferenceEventFrame, |
| 14 | + WorkerInferenceTerminalFrame, |
| 15 | +} from "../../packages/gateway-protocol/src/schema/worker-inference.js"; |
| 16 | +import { WorkerConnectionFrameDispatcher } from "./worker-connection-frames.js"; |
| 17 | +import { createWorkerConnection, type WorkerConnectionState } from "./worker-connection.js"; |
| 18 | + |
| 19 | +const FRAME_CONNECT_PARAMS: WorkerConnectParams = { |
| 20 | + minProtocol: 1, |
| 21 | + maxProtocol: 1, |
| 22 | + client: { |
| 23 | + id: GATEWAY_CLIENT_IDS.WORKER, |
| 24 | + version: "listener-isolation-test", |
| 25 | + platform: process.platform, |
| 26 | + mode: GATEWAY_CLIENT_MODES.WORKER, |
| 27 | + }, |
| 28 | + role: "worker", |
| 29 | + admission: { |
| 30 | + environmentId: "listener-isolation-test", |
| 31 | + credential: "listener-isolation-credential", |
| 32 | + ownerEpoch: 1, |
| 33 | + rpcSetVersion: WORKER_RPC_SET_VERSION, |
| 34 | + handshake: { |
| 35 | + bundleHash: "a".repeat(64), |
| 36 | + openclawVersion: "listener-isolation-test", |
| 37 | + protocolFeatures: [...WORKER_PROTOCOL_FEATURES], |
| 38 | + }, |
| 39 | + sessionId: "session-1", |
| 40 | + runId: "run-1", |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +function createIdleConnection() { |
| 45 | + return createWorkerConnection({ |
| 46 | + socketPath: "ws://127.0.0.1:1", |
| 47 | + connectParams: { |
| 48 | + minProtocol: 1, |
| 49 | + maxProtocol: 1, |
| 50 | + client: { |
| 51 | + id: GATEWAY_CLIENT_IDS.WORKER, |
| 52 | + version: "listener-isolation-test", |
| 53 | + platform: process.platform, |
| 54 | + mode: GATEWAY_CLIENT_MODES.WORKER, |
| 55 | + }, |
| 56 | + role: "worker", |
| 57 | + admission: { |
| 58 | + environmentId: "listener-isolation-test", |
| 59 | + credential: "listener-isolation-credential", |
| 60 | + ownerEpoch: 1, |
| 61 | + rpcSetVersion: WORKER_RPC_SET_VERSION, |
| 62 | + handshake: { |
| 63 | + bundleHash: "a".repeat(64), |
| 64 | + openclawVersion: "listener-isolation-test", |
| 65 | + protocolFeatures: [...WORKER_PROTOCOL_FEATURES], |
| 66 | + }, |
| 67 | + sessionId: null, |
| 68 | + runId: null, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +function createFrameDispatcher() { |
| 75 | + return new WorkerConnectionFrameDispatcher({ |
| 76 | + connectParams: () => FRAME_CONNECT_PARAMS, |
| 77 | + requestTimeoutMs: 1_000, |
| 78 | + isReady: () => false, |
| 79 | + socket: () => undefined, |
| 80 | + isTerminal: () => false, |
| 81 | + terminalError: () => new Error("not terminal"), |
| 82 | + interruptReadySocket: () => undefined, |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +function inferenceEventFrame(seq: number): WorkerInferenceEventFrame { |
| 87 | + return { |
| 88 | + type: "event", |
| 89 | + event: "worker.inference.event", |
| 90 | + payload: { |
| 91 | + runEpoch: 1, |
| 92 | + sessionId: "session-1", |
| 93 | + runId: "run-1", |
| 94 | + turnId: "turn-1", |
| 95 | + seq, |
| 96 | + event: { type: "text_delta", contentIndex: 0, delta: `chunk-${seq}` }, |
| 97 | + }, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +function inferenceTerminalFrame(seq: number): WorkerInferenceTerminalFrame { |
| 102 | + return { |
| 103 | + type: "event", |
| 104 | + event: "worker.inference.terminal", |
| 105 | + payload: { |
| 106 | + runEpoch: 1, |
| 107 | + sessionId: "session-1", |
| 108 | + runId: "run-1", |
| 109 | + turnId: "turn-1", |
| 110 | + seq, |
| 111 | + outcome: { |
| 112 | + type: "error", |
| 113 | + reason: "provider-error", |
| 114 | + message: `failure-${seq}`, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }; |
| 118 | +} |
| 119 | + |
| 120 | +function installThrowingThenHealthyListeners(connection: ReturnType<typeof createIdleConnection>) { |
| 121 | + let throwingCalls = 0; |
| 122 | + const observed: WorkerConnectionState["kind"][] = []; |
| 123 | + connection.onStateChange(() => { |
| 124 | + throwingCalls += 1; |
| 125 | + throw new Error("induced observer failure"); |
| 126 | + }); |
| 127 | + connection.onStateChange((state) => { |
| 128 | + observed.push(state.kind); |
| 129 | + }); |
| 130 | + return { observed, throwingCalls: () => throwingCalls }; |
| 131 | +} |
| 132 | + |
| 133 | +describe("WorkerConnection state listener isolation", () => { |
| 134 | + it("settles stop and reaches later listeners when an earlier listener throws", async () => { |
| 135 | + const connection = createIdleConnection(); |
| 136 | + const listeners = installThrowingThenHealthyListeners(connection); |
| 137 | + const exit = connection.waitForExit(); |
| 138 | + |
| 139 | + await expect(connection.stop()).resolves.toBeUndefined(); |
| 140 | + await expect(exit).resolves.toEqual({ kind: "stopped" }); |
| 141 | + await expect(connection.stop()).resolves.toBeUndefined(); |
| 142 | + |
| 143 | + expect(connection.state).toEqual({ kind: "stopped" }); |
| 144 | + expect(listeners.throwingCalls()).toBe(1); |
| 145 | + expect(listeners.observed).toEqual(["stopped"]); |
| 146 | + }); |
| 147 | + |
| 148 | + it("settles fencing and reaches later listeners when an earlier listener throws", async () => { |
| 149 | + const connection = createIdleConnection(); |
| 150 | + const listeners = installThrowingThenHealthyListeners(connection); |
| 151 | + |
| 152 | + expect(() => connection.fence("owner-epoch-mismatch")).not.toThrow(); |
| 153 | + await expect(connection.waitForExit()).resolves.toEqual({ |
| 154 | + kind: "fenced", |
| 155 | + reason: "owner-epoch-mismatch", |
| 156 | + }); |
| 157 | + |
| 158 | + expect(connection.state).toEqual({ kind: "fenced", reason: "owner-epoch-mismatch" }); |
| 159 | + expect(listeners.throwingCalls()).toBe(1); |
| 160 | + expect(listeners.observed).toEqual(["fenced"]); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +describe("WorkerConnection inference listener isolation", () => { |
| 165 | + it("continues event delivery and processes later frames after an observer throws", () => { |
| 166 | + const dispatcher = createFrameDispatcher(); |
| 167 | + const observed: number[] = []; |
| 168 | + dispatcher.onInferenceEvent(() => { |
| 169 | + throw new Error("induced event observer failure"); |
| 170 | + }); |
| 171 | + dispatcher.onInferenceEvent((frame) => { |
| 172 | + observed.push(frame.payload.seq); |
| 173 | + }); |
| 174 | + |
| 175 | + expect(() => |
| 176 | + dispatcher.dispatchReadyFrame(inferenceEventFrame(1), {} as WebSocket), |
| 177 | + ).not.toThrow(); |
| 178 | + expect(() => |
| 179 | + dispatcher.dispatchReadyFrame(inferenceEventFrame(2), {} as WebSocket), |
| 180 | + ).not.toThrow(); |
| 181 | + |
| 182 | + expect(observed).toEqual([1, 2]); |
| 183 | + }); |
| 184 | + |
| 185 | + it("continues terminal delivery and processes later frames after an observer throws", () => { |
| 186 | + const dispatcher = createFrameDispatcher(); |
| 187 | + const observed: number[] = []; |
| 188 | + dispatcher.onInferenceTerminal(() => { |
| 189 | + throw new Error("induced terminal observer failure"); |
| 190 | + }); |
| 191 | + dispatcher.onInferenceTerminal((frame) => { |
| 192 | + observed.push(frame.payload.seq); |
| 193 | + }); |
| 194 | + |
| 195 | + expect(() => |
| 196 | + dispatcher.dispatchReadyFrame(inferenceTerminalFrame(1), {} as WebSocket), |
| 197 | + ).not.toThrow(); |
| 198 | + expect(() => |
| 199 | + dispatcher.dispatchReadyFrame(inferenceTerminalFrame(2), {} as WebSocket), |
| 200 | + ).not.toThrow(); |
| 201 | + |
| 202 | + expect(observed).toEqual([1, 2]); |
| 203 | + }); |
| 204 | +}); |
0 commit comments