From 6220106ab2a0c3afc0c5ca231b3585a4456bde94 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 9 Jan 2026 22:13:16 +0100 Subject: [PATCH] refactor: centralize main session key resolution --- src/config/sessions.ts | 5 +++++ src/gateway/server-bridge.ts | 4 ++-- src/gateway/server-methods/system.ts | 5 ++--- src/gateway/server.hooks.test.ts | 5 ++--- src/gateway/server.ts | 7 ++++--- src/gateway/test-helpers.ts | 7 +++---- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/config/sessions.ts b/src/config/sessions.ts index 704b6d74e..2ffb655ae 100644 --- a/src/config/sessions.ts +++ b/src/config/sessions.ts @@ -19,6 +19,7 @@ import { isCacheEnabled, resolveCacheTtlMs, } from "./cache-utils.js"; +import { loadConfig } from "./config.js"; import { resolveStateDir } from "./paths.js"; // ============================================================================ @@ -232,6 +233,10 @@ export function resolveMainSessionKey(cfg?: { return buildAgentMainSessionKey({ agentId, mainKey }); } +export function resolveMainSessionKeyFromConfig(): string { + return resolveMainSessionKey(loadConfig()); +} + export { resolveAgentIdFromSessionKey }; export function resolveAgentMainSessionKey(params: { diff --git a/src/gateway/server-bridge.ts b/src/gateway/server-bridge.ts index 85147d62e..b939a3892 100644 --- a/src/gateway/server-bridge.ts +++ b/src/gateway/server-bridge.ts @@ -23,7 +23,7 @@ import { import { buildConfigSchema } from "../config/schema.js"; import { loadSessionStore, - resolveMainSessionKey, + resolveMainSessionKeyFromConfig, type SessionEntry, saveSessionStore, } from "../config/sessions.js"; @@ -472,7 +472,7 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) { }; } - const mainKey = resolveMainSessionKey(loadConfig()); + const mainKey = resolveMainSessionKeyFromConfig(); if (key === mainKey) { return { ok: false, diff --git a/src/gateway/server-methods/system.ts b/src/gateway/server-methods/system.ts index 36e0290c9..1f7783e42 100644 --- a/src/gateway/server-methods/system.ts +++ b/src/gateway/server-methods/system.ts @@ -1,5 +1,4 @@ -import { loadConfig } from "../../config/config.js"; -import { resolveMainSessionKey } from "../../config/sessions.js"; +import { resolveMainSessionKeyFromConfig } from "../../config/sessions.js"; import { getLastHeartbeatEvent } from "../../infra/heartbeat-events.js"; import { setHeartbeatsEnabled } from "../../infra/heartbeat-runner.js"; import { @@ -47,7 +46,7 @@ export const systemHandlers: GatewayRequestHandlers = { ); return; } - const sessionKey = resolveMainSessionKey(loadConfig()); + const sessionKey = resolveMainSessionKeyFromConfig(); const instanceId = typeof params.instanceId === "string" ? params.instanceId : undefined; const host = typeof params.host === "string" ? params.host : undefined; diff --git a/src/gateway/server.hooks.test.ts b/src/gateway/server.hooks.test.ts index 4ed2e59ae..07b863062 100644 --- a/src/gateway/server.hooks.test.ts +++ b/src/gateway/server.hooks.test.ts @@ -1,6 +1,5 @@ import { describe, expect, test } from "vitest"; -import { loadConfig } from "../config/config.js"; -import { resolveMainSessionKey } from "../config/sessions.js"; +import { resolveMainSessionKeyFromConfig } from "../config/sessions.js"; import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js"; import { cronIsolatedRun, @@ -13,7 +12,7 @@ import { installGatewayTestHooks(); -const resolveMainKey = () => resolveMainSessionKey(loadConfig()); +const resolveMainKey = () => resolveMainSessionKeyFromConfig(); describe("gateway server hooks", () => { test("hooks wake requires auth", async () => { diff --git a/src/gateway/server.ts b/src/gateway/server.ts index e124adb2b..0d1e8ebc2 100644 --- a/src/gateway/server.ts +++ b/src/gateway/server.ts @@ -41,6 +41,7 @@ import { import { loadSessionStore, resolveMainSessionKey, + resolveMainSessionKeyFromConfig, resolveStorePath, } from "../config/sessions.js"; import { runCronIsolatedAgentTurn } from "../cron/isolated-agent.js"; @@ -488,7 +489,7 @@ export async function startGatewayServer( text: string; mode: "now" | "next-heartbeat"; }) => { - const sessionKey = resolveMainSessionKey(loadConfig()); + const sessionKey = resolveMainSessionKeyFromConfig(); enqueueSystemEvent(value.text, { sessionKey }); if (value.mode === "now") { requestHeartbeatNow({ reason: "hook:wake" }); @@ -510,7 +511,7 @@ export async function startGatewayServer( const sessionKey = value.sessionKey.trim() ? value.sessionKey.trim() : `hook:${randomUUID()}`; - const mainSessionKey = resolveMainSessionKey(loadConfig()); + const mainSessionKey = resolveMainSessionKeyFromConfig(); const jobId = randomUUID(); const now = Date.now(); const job: CronJob = { @@ -1828,7 +1829,7 @@ export async function startGatewayServer( const summary = summarizeRestartSentinel(payload); if (!sessionKey) { - const mainSessionKey = resolveMainSessionKey(loadConfig()); + const mainSessionKey = resolveMainSessionKeyFromConfig(); enqueueSystemEvent(message, { sessionKey: mainSessionKey }); return; } diff --git a/src/gateway/test-helpers.ts b/src/gateway/test-helpers.ts index 08a5003ed..3ba386345 100644 --- a/src/gateway/test-helpers.ts +++ b/src/gateway/test-helpers.ts @@ -4,8 +4,7 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, expect, vi } from "vitest"; import { WebSocket } from "ws"; -import { loadConfig } from "../config/config.js"; -import { resolveMainSessionKey } from "../config/sessions.js"; +import { resolveMainSessionKeyFromConfig } from "../config/sessions.js"; import { resetAgentRunContextForTest } from "../infra/agent-events.js"; import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js"; import { rawDataToString } from "../infra/ws.js"; @@ -375,7 +374,7 @@ export function installGatewayTestHooks() { embeddedRunMock.abortCalls = []; embeddedRunMock.waitCalls = []; embeddedRunMock.waitResults.clear(); - drainSystemEvents(resolveMainSessionKey(loadConfig())); + drainSystemEvents(resolveMainSessionKeyFromConfig()); resetAgentRunContextForTest(); const mod = await import("./server.js"); mod.__resetModelCatalogCacheForTest(); @@ -555,7 +554,7 @@ export async function rpcReq( } export async function waitForSystemEvent(timeoutMs = 2000) { - const sessionKey = resolveMainSessionKey(loadConfig()); + const sessionKey = resolveMainSessionKeyFromConfig(); const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { const events = peekSystemEvents(sessionKey);