refactor: centralize main session key resolution

This commit is contained in:
Peter Steinberger
2026-01-09 22:13:16 +01:00
parent c37b77855b
commit 6220106ab2
6 changed files with 18 additions and 15 deletions

View File

@@ -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: {

View File

@@ -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,

View File

@@ -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;

View File

@@ -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 () => {

View File

@@ -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;
}

View File

@@ -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<T = unknown>(
}
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);