refactor: centralize main session key resolution
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
|||||||
isCacheEnabled,
|
isCacheEnabled,
|
||||||
resolveCacheTtlMs,
|
resolveCacheTtlMs,
|
||||||
} from "./cache-utils.js";
|
} from "./cache-utils.js";
|
||||||
|
import { loadConfig } from "./config.js";
|
||||||
import { resolveStateDir } from "./paths.js";
|
import { resolveStateDir } from "./paths.js";
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -232,6 +233,10 @@ export function resolveMainSessionKey(cfg?: {
|
|||||||
return buildAgentMainSessionKey({ agentId, mainKey });
|
return buildAgentMainSessionKey({ agentId, mainKey });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveMainSessionKeyFromConfig(): string {
|
||||||
|
return resolveMainSessionKey(loadConfig());
|
||||||
|
}
|
||||||
|
|
||||||
export { resolveAgentIdFromSessionKey };
|
export { resolveAgentIdFromSessionKey };
|
||||||
|
|
||||||
export function resolveAgentMainSessionKey(params: {
|
export function resolveAgentMainSessionKey(params: {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
import { buildConfigSchema } from "../config/schema.js";
|
import { buildConfigSchema } from "../config/schema.js";
|
||||||
import {
|
import {
|
||||||
loadSessionStore,
|
loadSessionStore,
|
||||||
resolveMainSessionKey,
|
resolveMainSessionKeyFromConfig,
|
||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
saveSessionStore,
|
saveSessionStore,
|
||||||
} from "../config/sessions.js";
|
} from "../config/sessions.js";
|
||||||
@@ -472,7 +472,7 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const mainKey = resolveMainSessionKey(loadConfig());
|
const mainKey = resolveMainSessionKeyFromConfig();
|
||||||
if (key === mainKey) {
|
if (key === mainKey) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { loadConfig } from "../../config/config.js";
|
import { resolveMainSessionKeyFromConfig } from "../../config/sessions.js";
|
||||||
import { resolveMainSessionKey } from "../../config/sessions.js";
|
|
||||||
import { getLastHeartbeatEvent } from "../../infra/heartbeat-events.js";
|
import { getLastHeartbeatEvent } from "../../infra/heartbeat-events.js";
|
||||||
import { setHeartbeatsEnabled } from "../../infra/heartbeat-runner.js";
|
import { setHeartbeatsEnabled } from "../../infra/heartbeat-runner.js";
|
||||||
import {
|
import {
|
||||||
@@ -47,7 +46,7 @@ export const systemHandlers: GatewayRequestHandlers = {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const sessionKey = resolveMainSessionKey(loadConfig());
|
const sessionKey = resolveMainSessionKeyFromConfig();
|
||||||
const instanceId =
|
const instanceId =
|
||||||
typeof params.instanceId === "string" ? params.instanceId : undefined;
|
typeof params.instanceId === "string" ? params.instanceId : undefined;
|
||||||
const host = typeof params.host === "string" ? params.host : undefined;
|
const host = typeof params.host === "string" ? params.host : undefined;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
import { loadConfig } from "../config/config.js";
|
import { resolveMainSessionKeyFromConfig } from "../config/sessions.js";
|
||||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
|
||||||
import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
|
import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
|
||||||
import {
|
import {
|
||||||
cronIsolatedRun,
|
cronIsolatedRun,
|
||||||
@@ -13,7 +12,7 @@ import {
|
|||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks();
|
||||||
|
|
||||||
const resolveMainKey = () => resolveMainSessionKey(loadConfig());
|
const resolveMainKey = () => resolveMainSessionKeyFromConfig();
|
||||||
|
|
||||||
describe("gateway server hooks", () => {
|
describe("gateway server hooks", () => {
|
||||||
test("hooks wake requires auth", async () => {
|
test("hooks wake requires auth", async () => {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
loadSessionStore,
|
loadSessionStore,
|
||||||
resolveMainSessionKey,
|
resolveMainSessionKey,
|
||||||
|
resolveMainSessionKeyFromConfig,
|
||||||
resolveStorePath,
|
resolveStorePath,
|
||||||
} from "../config/sessions.js";
|
} from "../config/sessions.js";
|
||||||
import { runCronIsolatedAgentTurn } from "../cron/isolated-agent.js";
|
import { runCronIsolatedAgentTurn } from "../cron/isolated-agent.js";
|
||||||
@@ -488,7 +489,7 @@ export async function startGatewayServer(
|
|||||||
text: string;
|
text: string;
|
||||||
mode: "now" | "next-heartbeat";
|
mode: "now" | "next-heartbeat";
|
||||||
}) => {
|
}) => {
|
||||||
const sessionKey = resolveMainSessionKey(loadConfig());
|
const sessionKey = resolveMainSessionKeyFromConfig();
|
||||||
enqueueSystemEvent(value.text, { sessionKey });
|
enqueueSystemEvent(value.text, { sessionKey });
|
||||||
if (value.mode === "now") {
|
if (value.mode === "now") {
|
||||||
requestHeartbeatNow({ reason: "hook:wake" });
|
requestHeartbeatNow({ reason: "hook:wake" });
|
||||||
@@ -510,7 +511,7 @@ export async function startGatewayServer(
|
|||||||
const sessionKey = value.sessionKey.trim()
|
const sessionKey = value.sessionKey.trim()
|
||||||
? value.sessionKey.trim()
|
? value.sessionKey.trim()
|
||||||
: `hook:${randomUUID()}`;
|
: `hook:${randomUUID()}`;
|
||||||
const mainSessionKey = resolveMainSessionKey(loadConfig());
|
const mainSessionKey = resolveMainSessionKeyFromConfig();
|
||||||
const jobId = randomUUID();
|
const jobId = randomUUID();
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const job: CronJob = {
|
const job: CronJob = {
|
||||||
@@ -1828,7 +1829,7 @@ export async function startGatewayServer(
|
|||||||
const summary = summarizeRestartSentinel(payload);
|
const summary = summarizeRestartSentinel(payload);
|
||||||
|
|
||||||
if (!sessionKey) {
|
if (!sessionKey) {
|
||||||
const mainSessionKey = resolveMainSessionKey(loadConfig());
|
const mainSessionKey = resolveMainSessionKeyFromConfig();
|
||||||
enqueueSystemEvent(message, { sessionKey: mainSessionKey });
|
enqueueSystemEvent(message, { sessionKey: mainSessionKey });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeEach, expect, vi } from "vitest";
|
import { afterEach, beforeEach, expect, vi } from "vitest";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
import { loadConfig } from "../config/config.js";
|
import { resolveMainSessionKeyFromConfig } from "../config/sessions.js";
|
||||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
|
||||||
import { resetAgentRunContextForTest } from "../infra/agent-events.js";
|
import { resetAgentRunContextForTest } from "../infra/agent-events.js";
|
||||||
import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
|
import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
|
||||||
import { rawDataToString } from "../infra/ws.js";
|
import { rawDataToString } from "../infra/ws.js";
|
||||||
@@ -375,7 +374,7 @@ export function installGatewayTestHooks() {
|
|||||||
embeddedRunMock.abortCalls = [];
|
embeddedRunMock.abortCalls = [];
|
||||||
embeddedRunMock.waitCalls = [];
|
embeddedRunMock.waitCalls = [];
|
||||||
embeddedRunMock.waitResults.clear();
|
embeddedRunMock.waitResults.clear();
|
||||||
drainSystemEvents(resolveMainSessionKey(loadConfig()));
|
drainSystemEvents(resolveMainSessionKeyFromConfig());
|
||||||
resetAgentRunContextForTest();
|
resetAgentRunContextForTest();
|
||||||
const mod = await import("./server.js");
|
const mod = await import("./server.js");
|
||||||
mod.__resetModelCatalogCacheForTest();
|
mod.__resetModelCatalogCacheForTest();
|
||||||
@@ -555,7 +554,7 @@ export async function rpcReq<T = unknown>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function waitForSystemEvent(timeoutMs = 2000) {
|
export async function waitForSystemEvent(timeoutMs = 2000) {
|
||||||
const sessionKey = resolveMainSessionKey(loadConfig());
|
const sessionKey = resolveMainSessionKeyFromConfig();
|
||||||
const deadline = Date.now() + timeoutMs;
|
const deadline = Date.now() + timeoutMs;
|
||||||
while (Date.now() < deadline) {
|
while (Date.now() < deadline) {
|
||||||
const events = peekSystemEvents(sessionKey);
|
const events = peekSystemEvents(sessionKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user