perf(agents): lazy-load session store updates

This commit is contained in:
Vincent Koc
2026-04-13 21:07:50 +01:00
parent dd27aa945e
commit 99755fcb2f
5 changed files with 13 additions and 5 deletions

View File

@@ -48,7 +48,7 @@ vi.mock("./command/run-context.js", () => ({
}),
}));
vi.mock("./command/session-store.js", () => ({
vi.mock("./command/session-store.runtime.js", () => ({
updateSessionStoreAfterAgentRun: (...args: unknown[]) =>
state.updateSessionStoreAfterAgentRunMock(...args),
}));

View File

@@ -51,7 +51,6 @@ import {
prependInternalEventContext,
} from "./command/attempt-execution.shared.js";
import { resolveAgentRunContext } from "./command/run-context.js";
import { updateSessionStoreAfterAgentRun } from "./command/session-store.js";
import { resolveSession } from "./command/session.js";
import type { AgentCommandIngressOpts, AgentCommandOpts } from "./command/types.js";
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "./defaults.js";
@@ -79,10 +78,12 @@ import { ensureAgentWorkspace } from "./workspace.js";
const log = createSubsystemLogger("agents/agent-command");
type AttemptExecutionRuntime = typeof import("./command/attempt-execution.runtime.js");
type DeliveryRuntime = typeof import("./command/delivery.runtime.js");
type SessionStoreRuntime = typeof import("./command/session-store.runtime.js");
type TranscriptResolveRuntime = typeof import("../config/sessions/transcript-resolve.runtime.js");
let attemptExecutionRuntimePromise: Promise<AttemptExecutionRuntime> | undefined;
let deliveryRuntimePromise: Promise<DeliveryRuntime> | undefined;
let sessionStoreRuntimePromise: Promise<SessionStoreRuntime> | undefined;
let transcriptResolveRuntimePromise: Promise<TranscriptResolveRuntime> | undefined;
function loadAttemptExecutionRuntime(): Promise<AttemptExecutionRuntime> {
@@ -95,6 +96,11 @@ function loadDeliveryRuntime(): Promise<DeliveryRuntime> {
return deliveryRuntimePromise;
}
function loadSessionStoreRuntime(): Promise<SessionStoreRuntime> {
sessionStoreRuntimePromise ??= import("./command/session-store.runtime.js");
return sessionStoreRuntimePromise;
}
function loadTranscriptResolveRuntime(): Promise<TranscriptResolveRuntime> {
transcriptResolveRuntimePromise ??= import("../config/sessions/transcript-resolve.runtime.js");
return transcriptResolveRuntimePromise;
@@ -1022,6 +1028,7 @@ async function agentCommandInternal(
// Update token+model fields in the session store.
if (sessionStore && sessionKey) {
const { updateSessionStoreAfterAgentRun } = await loadSessionStoreRuntime();
await updateSessionStoreAfterAgentRun({
cfg,
contextTokensOverride: agentCfg?.contextTokens,

View File

@@ -0,0 +1 @@
export { updateSessionStoreAfterAgentRun } from "./session-store.js";

View File

@@ -15,7 +15,7 @@ import {
withSharedAgentCommandTempHome,
} from "./agent-runtime-config.test-support.js";
vi.mock("../agents/command/session-store.js", () => {
vi.mock("../agents/command/session-store.runtime.js", () => {
return {
updateSessionStoreAfterAgentRun: vi.fn(async () => undefined),
};

View File

@@ -7,7 +7,7 @@ import "../cron/isolated-agent.mocks.js";
import { __testing as acpManagerTesting } from "../acp/control-plane/manager.js";
import { resolveAgentDir, resolveSessionAgentId } from "../agents/agent-scope.js";
import * as authProfilesModule from "../agents/auth-profiles.js";
import * as sessionStoreModule from "../agents/command/session-store.js";
import * as sessionStoreModule from "../agents/command/session-store.runtime.js";
import { resolveSession } from "../agents/command/session.js";
import { loadModelCatalog } from "../agents/model-catalog.js";
import * as modelSelectionModule from "../agents/model-selection.js";
@@ -51,7 +51,7 @@ vi.mock("../agents/auth-profiles/store.js", () => {
};
});
vi.mock("../agents/command/session-store.js", () => {
vi.mock("../agents/command/session-store.runtime.js", () => {
return {
updateSessionStoreAfterAgentRun: vi.fn(async () => undefined),
};