refactor: migrate from clientSessionKey to clientId identity

This commit is contained in:
Mohamed Boudra
2026-02-21 15:33:29 +07:00
parent e906bffbb9
commit c3c8103563
56 changed files with 3289 additions and 941 deletions
+13 -13
View File
@@ -134,7 +134,7 @@ function makeDeps(
}
return value;
},
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
};
}
@@ -169,7 +169,7 @@ describe("HostRuntimeController", () => {
measureLatency: async () => {
throw new Error("probe unavailable");
},
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
};
const controller = new HostRuntimeController({
host,
@@ -188,7 +188,7 @@ describe("HostRuntimeController", () => {
expect(controller.getSnapshot().agentDirectoryStatus).toBe("initial_loading");
});
it("passes resolved client session key into created active clients", async () => {
it("passes resolved client id into created active clients", async () => {
const host = makeHost({
connections: [
{
@@ -198,23 +198,23 @@ describe("HostRuntimeController", () => {
},
],
});
const seenSessionKeys: string[] = [];
const seenClientIds: string[] = [];
const fakeClient = new FakeDaemonClient();
const controller = new HostRuntimeController({
host,
deps: {
createClient: ({ clientSessionKey }) => {
seenSessionKeys.push(clientSessionKey);
createClient: ({ clientId }) => {
seenClientIds.push(clientId);
return fakeClient as unknown as DaemonClient;
},
measureLatency: async () => 10,
getClientSessionKey: async () => "clsk_runtime_stable",
getClientId: async () => "cid_runtime_stable",
},
});
await controller.start({ autoProbe: false });
expect(seenSessionKeys).toEqual(["clsk_runtime_stable"]);
expect(seenClientIds).toEqual(["cid_runtime_stable"]);
expect(controller.getSnapshot().connectionStatus).toBe("online");
});
@@ -535,7 +535,7 @@ describe("HostRuntimeController", () => {
return client as unknown as DaemonClient;
},
measureLatency: async () => 10,
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
};
const controller = new HostRuntimeController({
host,
@@ -621,7 +621,7 @@ describe("HostRuntimeController", () => {
}
throw new Error("unexpected probe call");
},
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
},
});
@@ -685,7 +685,7 @@ describe("HostRuntimeController", () => {
}
return 10;
},
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
},
});
@@ -730,7 +730,7 @@ describe("HostRuntimeStore", () => {
deps: {
createClient: () => fakeClient as unknown as DaemonClient,
measureLatency: async () => 5,
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
},
});
@@ -772,7 +772,7 @@ describe("HostRuntimeStore", () => {
measureLatency: async () => {
throw new Error("probe unavailable");
},
getClientSessionKey: async () => "clsk_test_runtime",
getClientId: async () => "cid_test_runtime",
},
});
+20 -20
View File
@@ -10,7 +10,7 @@ import {
buildRelayWebSocketUrl,
} from "@/utils/daemon-endpoints";
import { measureConnectionLatency } from "@/utils/test-daemon-connection";
import { getOrCreateClientSessionKey } from "@/utils/client-session-key";
import { getOrCreateClientId } from "@/utils/client-id";
import {
selectBestConnection,
type ConnectionCandidate,
@@ -118,14 +118,14 @@ export type HostRuntimeControllerDeps = {
createClient: (input: {
host: HostProfile;
connection: HostConnection;
clientSessionKey: string;
clientId: string;
runtimeGeneration: number;
}) => DaemonClient;
measureLatency: (input: {
host: HostProfile;
connection: HostConnection;
}) => Promise<number>;
getClientSessionKey: () => Promise<string>;
getClientId: () => Promise<string>;
};
export type HostRuntimeStartOptions = {
@@ -374,11 +374,12 @@ function selectInitialConnectionId(host: HostProfile): string | null {
function createDefaultDeps(): HostRuntimeControllerDeps {
return {
createClient: ({ host, connection, clientSessionKey, runtimeGeneration }) => {
createClient: ({ host, connection, clientId, runtimeGeneration }) => {
const tauriTransportFactory = createTauriWebSocketTransportFactory();
const base = {
suppressSendErrors: true,
clientSessionKey,
clientId,
clientType: "mobile" as const,
runtimeGeneration,
...(tauriTransportFactory
? { transportFactory: tauriTransportFactory }
@@ -387,7 +388,7 @@ function createDefaultDeps(): HostRuntimeControllerDeps {
if (connection.type === "direct") {
return new DaemonClient({
...base,
url: buildDaemonWebSocketUrl(connection.endpoint, { clientSessionKey }),
url: buildDaemonWebSocketUrl(connection.endpoint),
});
}
return new DaemonClient({
@@ -395,7 +396,6 @@ function createDefaultDeps(): HostRuntimeControllerDeps {
url: buildRelayWebSocketUrl({
endpoint: connection.relayEndpoint,
serverId: host.serverId,
clientSessionKey,
}),
e2ee: {
enabled: true,
@@ -405,7 +405,7 @@ function createDefaultDeps(): HostRuntimeControllerDeps {
},
measureLatency: ({ host, connection }) =>
measureConnectionLatency(connection, { serverId: host.serverId }),
getClientSessionKey: () => getOrCreateClientSessionKey(),
getClientId: () => getOrCreateClientId(),
};
}
@@ -421,8 +421,8 @@ export class HostRuntimeController {
private started = false;
private switchCandidateConnectionId: string | null = null;
private switchCandidateHitCount = 0;
private clientSessionKeyPromise: Promise<string> | null = null;
private clientSessionKeyHash: string | null = null;
private clientIdPromise: Promise<string> | null = null;
private clientIdHash: string | null = null;
private switchRequestVersion = 0;
private probeRequestVersion = 0;
@@ -740,7 +740,7 @@ export class HostRuntimeController {
: toReasonCode(reason);
console.info("[HostRuntimeTransition]", {
serverId: this.host.serverId,
clientSessionKeyHash: this.clientSessionKeyHash,
clientIdHash: this.clientIdHash,
from: input.from,
to: input.to,
event: event.type,
@@ -782,9 +782,9 @@ export class HostRuntimeController {
}
const requestVersion = ++this.switchRequestVersion;
let clientSessionKey: string;
let clientId: string;
try {
clientSessionKey = await this.resolveClientSessionKey();
clientId = await this.resolveClientId();
} catch (error) {
if (!this.isCurrentSwitchRequest(requestVersion)) {
return;
@@ -792,7 +792,7 @@ export class HostRuntimeController {
const message = toErrorMessage(error);
this.applyConnectionEvent({
type: "connect_failed",
message: `Failed to resolve client session key: ${message}`,
message: `Failed to resolve client id: ${message}`,
});
this.updateSnapshot({
...toSnapshotConnectionPatch(this.connectionMachineState),
@@ -827,7 +827,7 @@ export class HostRuntimeController {
const client = this.deps.createClient({
host: this.host,
connection,
clientSessionKey,
clientId,
runtimeGeneration: nextGeneration,
});
if (!this.isCurrentSwitchRequest(requestVersion)) {
@@ -910,14 +910,14 @@ export class HostRuntimeController {
}
}
private resolveClientSessionKey(): Promise<string> {
if (!this.clientSessionKeyPromise) {
this.clientSessionKeyPromise = this.deps.getClientSessionKey().then((value) => {
this.clientSessionKeyHash = hashForLog(value);
private resolveClientId(): Promise<string> {
if (!this.clientIdPromise) {
this.clientIdPromise = this.deps.getClientId().then((value) => {
this.clientIdHash = hashForLog(value);
return value;
});
}
return this.clientSessionKeyPromise;
return this.clientIdPromise;
}
}