惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
The Cloudflare Blog
IT之家
IT之家
V
V2EX
雷峰网
雷峰网
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
A
About on SuperTechFans
B
Blog
月光博客
月光博客
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(gateway): reject RPCs from invalidated device-token c...
davidangular · 2026-05-27 · via Recent Commits to openclaw:main

@@ -1,6 +1,77 @@

11

import { describe, expect, it, vi } from "vitest";

22

import type { GatewayServerLiveState } from "./server-live-state.js";

3-

import { createGatewayRequestContext } from "./server-request-context.js";

3+

import {

4+

createGatewayRequestContext,

5+

type GatewayRequestContextParams,

6+

} from "./server-request-context.js";

7+8+

function makeContextParams(

9+

overrides: Partial<GatewayRequestContextParams> = {},

10+

): GatewayRequestContextParams {

11+

const runtimeState: Pick<GatewayServerLiveState, "cronState"> = {

12+

cronState: {

13+

cron: { start: vi.fn(), stop: vi.fn() } as never,

14+

storePath: "/tmp/cron",

15+

cronEnabled: true,

16+

},

17+

};

18+

return {

19+

deps: {} as never,

20+

runtimeState,

21+

getRuntimeConfig: vi.fn(() => ({}) as never),

22+

execApprovalManager: undefined,

23+

pluginApprovalManager: undefined,

24+

loadGatewayModelCatalog: vi.fn(async () => []),

25+

getHealthCache: vi.fn(() => null),

26+

refreshHealthSnapshot: vi.fn(async () => ({}) as never),

27+

logHealth: { error: vi.fn() },

28+

logGateway: { warn: vi.fn(), info: vi.fn(), error: vi.fn() } as never,

29+

incrementPresenceVersion: vi.fn(() => 1),

30+

getHealthVersion: vi.fn(() => 1),

31+

broadcast: vi.fn(),

32+

broadcastToConnIds: vi.fn(),

33+

nodeSendToSession: vi.fn(),

34+

nodeSendToAllSubscribed: vi.fn(),

35+

nodeSubscribe: vi.fn(),

36+

nodeUnsubscribe: vi.fn(),

37+

nodeUnsubscribeAll: vi.fn(),

38+

hasConnectedTalkNode: vi.fn(() => false),

39+

clients: new Set(),

40+

enforceSharedGatewayAuthGenerationForConfigWrite: vi.fn(),

41+

nodeRegistry: {} as never,

42+

agentRunSeq: new Map(),

43+

chatAbortControllers: new Map(),

44+

chatAbortedRuns: new Map(),

45+

chatRunBuffers: new Map(),

46+

chatDeltaSentAt: new Map(),

47+

chatDeltaLastBroadcastLen: new Map(),

48+

chatDeltaLastBroadcastText: new Map(),

49+

agentDeltaSentAt: new Map(),

50+

bufferedAgentEvents: new Map(),

51+

addChatRun: vi.fn(),

52+

removeChatRun: vi.fn(),

53+

subscribeSessionEvents: vi.fn(),

54+

unsubscribeSessionEvents: vi.fn(),

55+

subscribeSessionMessageEvents: vi.fn(),

56+

unsubscribeSessionMessageEvents: vi.fn(),

57+

unsubscribeAllSessionEvents: vi.fn(),

58+

getSessionEventSubscriberConnIds: vi.fn(() => new Set<string>()),

59+

registerToolEventRecipient: vi.fn(),

60+

dedupe: new Map(),

61+

wizardSessions: new Map(),

62+

findRunningWizard: vi.fn(() => null),

63+

purgeWizardSession: vi.fn(),

64+

getRuntimeSnapshot: vi.fn(() => ({}) as never),

65+

startChannel: vi.fn(async () => undefined),

66+

stopChannel: vi.fn(async () => undefined),

67+

markChannelLoggedOut: vi.fn(),

68+

wizardRunner: vi.fn(async () => undefined),

69+

broadcastVoiceWakeChanged: vi.fn(),

70+

broadcastVoiceWakeRoutingChanged: vi.fn(),

71+

unavailableGatewayMethods: new Set(),

72+

...overrides,

73+

};

74+

}

475576

describe("createGatewayRequestContext", () => {

677

it("reads cron state live from runtime state", () => {

@@ -82,4 +153,66 @@ describe("createGatewayRequestContext", () => {

82153

expect(context.cron).toBe(cronB);

83154

expect(context.cronStorePath).toBe("/tmp/cron-b");

84155

});

156+157+

it("invalidateClientsForDevice sets the flag on matching clients without closing the socket", () => {

158+

const target = {

159+

connId: "conn-target",

160+

connect: { device: { id: "device-1" }, role: "primary" },

161+

socket: { close: vi.fn() },

162+

};

163+

const unrelated = {

164+

connId: "conn-unrelated",

165+

connect: { device: { id: "device-2" }, role: "primary" },

166+

socket: { close: vi.fn() },

167+

};

168+

const clients = new Set([target, unrelated]) as never;

169+170+

const context = createGatewayRequestContext(makeContextParams({ clients }));

171+

context.invalidateClientsForDevice?.("device-1", { reason: "device-token-rotated" });

172+173+

expect((target as { invalidated?: boolean }).invalidated).toBe(true);

174+

expect((target as { invalidatedReason?: string }).invalidatedReason).toBe(

175+

"device-token-rotated",

176+

);

177+

expect(target.socket.close).not.toHaveBeenCalled();

178+179+

expect((unrelated as { invalidated?: boolean }).invalidated).toBeUndefined();

180+

expect(unrelated.socket.close).not.toHaveBeenCalled();

181+

});

182+183+

it("disconnectClientsForDevice also marks the invalidated flag before closing", () => {

184+

const target = {

185+

connId: "conn-target",

186+

connect: { device: { id: "device-1" }, role: "primary" },

187+

socket: { close: vi.fn() },

188+

};

189+

const clients = new Set([target]) as never;

190+191+

const context = createGatewayRequestContext(makeContextParams({ clients }));

192+

context.disconnectClientsForDevice?.("device-1");

193+194+

expect((target as { invalidated?: boolean }).invalidated).toBe(true);

195+

expect((target as { invalidatedReason?: string }).invalidatedReason).toBe("device-removed");

196+

expect(target.socket.close).toHaveBeenCalledWith(4001, "device removed");

197+

});

198+199+

it("invalidateClientsForDevice filters by role when provided", () => {

200+

const primary = {

201+

connId: "conn-primary",

202+

connect: { device: { id: "device-1" }, role: "primary" },

203+

socket: { close: vi.fn() },

204+

};

205+

const secondary = {

206+

connId: "conn-secondary",

207+

connect: { device: { id: "device-1" }, role: "secondary" },

208+

socket: { close: vi.fn() },

209+

};

210+

const clients = new Set([primary, secondary]) as never;

211+212+

const context = createGatewayRequestContext(makeContextParams({ clients }));

213+

context.invalidateClientsForDevice?.("device-1", { role: "primary" });

214+215+

expect((primary as { invalidated?: boolean }).invalidated).toBe(true);

216+

expect((secondary as { invalidated?: boolean }).invalidated).toBeUndefined();

217+

});

85218

});