Skip to content

Commit 0aa40fb

Browse files
fix(gateway): align generation guards with current base
1 parent 9b83710 commit 0aa40fb

14 files changed

Lines changed: 83 additions & 60 deletions

‎src/gateway/node-registry.test.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from "@openclaw/normalization-core/number-coercion";
99
import { afterEach, describe, expect, it, vi } from "vitest";
1010
import {
11-
getActiveNodeContext,
1211
getCurrentActiveNodeContext,
1312
setActiveNodeContext,
1413
} from "../infra/active-node-context.js";
@@ -351,7 +350,7 @@ describe("gateway/node-registry", () => {
351350
currentPairingGeneration = "generation-b";
352351
await expect(registry.listCurrentConnected()).resolves.toEqual([]);
353352
expect(registry.getActiveNode()).toBeUndefined();
354-
expect(getActiveNodeContext()).toBeNull();
353+
expect(getCurrentActiveNodeContext()).toBeNull();
355354
expect(client.invalidated).toBe(true);
356355
expect(onPairingInvalidated).toHaveBeenCalledWith({
357356
nodeId: "node-generation",
@@ -647,10 +646,10 @@ describe("gateway/node-registry", () => {
647646
});
648647

649648
expect(registry.getActiveNode()?.nodeId).toBe("node-2");
650-
expect(getActiveNodeContext()).toEqual({ nodeId: "node-2" });
649+
expect(getCurrentActiveNodeContext()).toEqual({ nodeId: "node-2" });
651650
expect(registry.unregister("conn-2")).toBe("node-2");
652651
expect(registry.getActiveNode()?.nodeId).toBe("node-1");
653-
expect(getActiveNodeContext()).toEqual({ nodeId: "node-1" });
652+
expect(getCurrentActiveNodeContext()).toEqual({ nodeId: "node-1" });
654653
});
655654

656655
it("recomputes active context when a same-id connection replaces reported presence", () => {
@@ -672,9 +671,9 @@ describe("gateway/node-registry", () => {
672671
);
673672

674673
expect(registry.getActiveNode()).toBeUndefined();
675-
expect(getActiveNodeContext()).toBeNull();
674+
expect(getCurrentActiveNodeContext()).toBeNull();
676675
expect(registry.unregister("conn-old")).toBeNull();
677-
expect(getActiveNodeContext()).toBeNull();
676+
expect(getCurrentActiveNodeContext()).toBeNull();
678677
});
679678

680679
it("rejects presence updates from stale node connections", () => {
@@ -742,7 +741,7 @@ describe("gateway/node-registry", () => {
742741
expect(registry.get("node-1")?.lastActiveAtMs).toBeUndefined();
743742
expect(registry.get("node-1")?.presenceUpdatedAtMs).toBeUndefined();
744743
expect(registry.getActiveNode()).toBeUndefined();
745-
expect(getActiveNodeContext()).toBeNull();
744+
expect(getCurrentActiveNodeContext()).toBeNull();
746745
});
747746

748747
it("checks node websocket connectivity with ping/pong", async () => {
@@ -1754,7 +1753,10 @@ describe("gateway/node-registry", () => {
17541753
const client = makeClient("conn-1", "node-1", [], {
17551754
declaredCommands: ["device.info"],
17561755
});
1757-
registry.register(client, { pairingGeneration: "generation-a" });
1756+
registry.register(client, {
1757+
pairingIdentity: "identity-a",
1758+
pairingGeneration: "generation-a",
1759+
});
17581760

17591761
const updated = registry.updateSurface(
17601762
"node-1",

‎src/gateway/node-registry.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ export type NodeEventTransport = {
168168
checkConnectivity?: (timeoutMs: number) => Promise<NodeConnectivityResult>;
169169
};
170170

171-
export type NodePairingStateSnapshot = {
171+
type NodePairingStateSnapshot = {
172172
identity: string;
173173
generation?: string;
174174
};
175175

176-
export type NodePairingBinding = {
176+
type NodePairingBinding = {
177177
identity?: string;
178178
generation?: string;
179179
};

‎src/gateway/server-methods.authorization.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe("gateway method authorization", () => {
104104
signedAt: 1,
105105
nonce: "nonce",
106106
},
107-
client: { id: "test-node", version: "1", platform: "test", mode: "node" },
107+
client: { id: "node-host", version: "1", platform: "test", mode: "node" },
108108
minProtocol: 1,
109109
maxProtocol: 1,
110110
},

‎src/gateway/server-methods/node-pairing-generation.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { PairedDevice } from "../../infra/device-pairing.js";
33
import {
4-
captureAuthenticatedNodePairingGeneration,
54
captureAuthenticatedNodePairingState,
65
captureNodePairingGeneration,
76
isNodePairingGenerationCurrent,
@@ -89,21 +88,21 @@ describe("node pairing generation", () => {
8988
);
9089

9190
await expect(
92-
captureAuthenticatedNodePairingGeneration({
91+
captureAuthenticatedNodePairingState({
9392
nodeId: original.deviceId,
9493
publicKey: original.publicKey,
9594
token: original.tokens!.node!.token,
9695
}),
97-
).resolves.toMatchObject({ nodeId: original.deviceId });
96+
).resolves.toMatchObject({ generation: { nodeId: original.deviceId } });
9897
await expect(
99-
captureAuthenticatedNodePairingGeneration({
98+
captureAuthenticatedNodePairingState({
10099
nodeId: original.deviceId,
101100
publicKey: original.publicKey,
102101
token: original.tokens!.node!.token,
103102
}),
104103
).resolves.toBeNull();
105104
await expect(
106-
captureAuthenticatedNodePairingGeneration({
105+
captureAuthenticatedNodePairingState({
107106
nodeId: original.deviceId,
108107
publicKey: original.publicKey,
109108
token: original.tokens!.node!.token,

‎src/gateway/server-methods/node-pairing-generation.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
export type {
1111
NodePairingGeneration,
1212
NodePairingIdentity,
13-
NodePairingState,
1413
} from "../../infra/device-pairing.js";
1514

1615
/** Captures the persistent authenticated pairing and optional approved surface. */
@@ -47,16 +46,6 @@ export async function captureAuthenticatedNodePairingState(params: {
4746
return resolveNodePairingState(device);
4847
}
4948

50-
/** Binds approved node work to the exact device key and node token used for authentication. */
51-
export async function captureAuthenticatedNodePairingGeneration(params: {
52-
nodeId: string;
53-
publicKey: string;
54-
token: string;
55-
baseDir?: string;
56-
}): Promise<NodePairingGeneration | null> {
57-
return (await captureAuthenticatedNodePairingState(params))?.generation ?? null;
58-
}
59-
6049
/** Revalidates that asynchronous work still belongs to the admitted pairing. */
6150
export async function isNodePairingGenerationCurrent(
6251
generation: NodePairingGeneration,

‎src/gateway/server-methods/nodes-pending.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ vi.mock("./node-pairing-generation.js", () => ({
3131
isNodePairingGenerationCurrent: mocks.isNodePairingGenerationCurrent,
3232
}));
3333

34+
vi.mock("./nodes-wake-state.js", () => ({
35+
captureNodeWakeLifecycle: mocks.captureNodeWakeLifecycle,
36+
isNodeWakeLifecycleCurrent: mocks.isNodeWakeLifecycleCurrent,
37+
releaseNodeWakeLifecycle: mocks.releaseNodeWakeLifecycle,
38+
}));
39+
3440
vi.mock("./nodes.js", () => ({
3541
NODE_WAKE_RECONNECT_WAIT_MS: 3_000,
3642
NODE_WAKE_RECONNECT_RETRY_WAIT_MS: 12_000,
37-
captureNodeWakeLifecycle: mocks.captureNodeWakeLifecycle,
38-
isNodeWakeLifecycleCurrent: mocks.isNodeWakeLifecycleCurrent,
3943
maybeWakeNodeWithApns: mocks.maybeWakeNodeWithApns,
4044
maybeSendNodeWakeNudge: mocks.maybeSendNodeWakeNudge,
41-
releaseNodeWakeLifecycle: mocks.releaseNodeWakeLifecycle,
4245
waitForNodeReconnect: mocks.waitForNodeReconnect,
4346
}));
4447

‎src/gateway/server-node-session-runtime.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ describe("gateway node session runtime", () => {
116116
currentPairingGeneration = "generation-b";
117117
runtime.broadcastVoiceWakeChanged(["retired"]);
118118
runtime.broadcastVoiceWakeRoutingChanged({ ...routing, updatedAtMs: 2 });
119-
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(4));
120-
await expect(send.mock.results[2]?.value).resolves.toBe(false);
121-
await expect(send.mock.results[3]?.value).resolves.toBe(false);
119+
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(2));
122120

123121
expect(frames.map((frame) => JSON.parse(frame))).toEqual([
124122
{ type: "event", event: "voicewake.changed", payload: { triggers: ["openclaw"] } },

‎src/gateway/server.roles-allowlist-update.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ describe("gateway node command allowlist", () => {
502502
path: path.join(os.tmpdir(), `openclaw-node-allowed-${Date.now()}-${Math.random()}.sqlite`),
503503
});
504504

505-
systemClient = await connectNodeClientWithPairing({
505+
systemClient = await connectNodeClientWithNodePairing({
506506
port,
507507
commands: ["system.run"],
508508
instanceId: "node-system-run",
@@ -521,7 +521,7 @@ describe("gateway node command allowlist", () => {
521521
await systemClient.stopAndWait();
522522
await waitForConnectedCount(0);
523523

524-
emptyClient = await connectNodeClientWithPairing({
524+
emptyClient = await connectNodeClientWithNodePairing({
525525
port,
526526
commands: [],
527527
instanceId: "node-empty",
@@ -611,7 +611,9 @@ describe("gateway node command allowlist", () => {
611611
const nodeId = await findConnectedNodeIdByDisplayName(displayName);
612612

613613
await expectPendingPairingCommands(nodeId, ["canvas.snapshot", "system.run"]);
614-
await expectCanvasSnapshotDenied(nodeId, "pending-node-canvas");
614+
const denied = await invokeCanvasSnapshot(nodeId, "pending-node-canvas");
615+
expect(denied.ok).toBe(false);
616+
expect(denied.error?.details).toMatchObject({ code: "PAIRING_CHANGED" });
615617
} finally {
616618
await nodeClient?.stopAndWait();
617619
}

‎src/infra/active-node-context.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ export function setActiveNodeContext(
2525
activeNodeContext = next ? { ...next, ...options } : null;
2626
}
2727

28-
/** Returns a defensive snapshot for local state inspection. */
29-
export function getActiveNodeContext(): ActiveNodeContext | null {
30-
if (!activeNodeContext) {
31-
return null;
32-
}
33-
return snapshotActiveNodeContext(activeNodeContext);
34-
}
35-
3628
/** Revalidates the published node before projecting it into an agent prompt. */
3729
export function getCurrentActiveNodeContext(): ActiveNodeContext | null {
3830
if (!activeNodeContext) {

‎src/infra/device-pairing.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,11 @@ describe("device pairing tokens", () => {
920920
{ nodeId: "node-1", platform: "darwin", commands: ["system.run"] },
921921
baseDir,
922922
);
923-
await approveNodePairing(nodePairing.request.requestId, { callerScopes: [] }, baseDir);
923+
await approveNodePairing(
924+
nodePairing.request.requestId,
925+
{ callerScopes: ["operator.pairing", "operator.admin"] },
926+
baseDir,
927+
);
924928
const original = resolveNodePairingGeneration(await getPairedDevice("node-1", baseDir));
925929
expect(original).not.toBeNull();
926930
if (!original) {
@@ -2119,7 +2123,11 @@ describe("device pairing tokens", () => {
21192123
const baseDir = await makeDevicePairingDir();
21202124
await setupPairedNodeDevice(baseDir);
21212125
const nodePairing = await requestNodePairing({ nodeId: "node-1" }, baseDir);
2122-
await approveNodePairing(nodePairing.request.requestId, { callerScopes: [] }, baseDir);
2126+
await approveNodePairing(
2127+
nodePairing.request.requestId,
2128+
{ callerScopes: ["operator.pairing"] },
2129+
baseDir,
2130+
);
21232131
await registerApnsRegistration({
21242132
nodeId: "node-1",
21252133
transport: "direct",
@@ -2173,7 +2181,11 @@ describe("device pairing tokens", () => {
21732181
{ nodeId: "node-1", platform: "darwin", commands: ["system.run", "system.which"] },
21742182
baseDir,
21752183
);
2176-
await approveNodePairing(nodePairing.request.requestId, { callerScopes: [] }, baseDir);
2184+
await approveNodePairing(
2185+
nodePairing.request.requestId,
2186+
{ callerScopes: ["operator.pairing", "operator.admin"] },
2187+
baseDir,
2188+
);
21772189
const previousGeneration = resolveNodePairingGeneration(
21782190
await getPairedDevice("node-1", baseDir),
21792191
);

0 commit comments

Comments
 (0)