From c0933e2fc850f5597f95ddc6e1b6f630dc1798b5 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 22 Mar 2026 19:32:06 -0700 Subject: [PATCH] perf(reply): lazy-load session store writes --- src/auto-reply/reply/agent-runner.ts | 12 +++++++++++- src/config/sessions/store.runtime.ts | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/auto-reply/reply/agent-runner.ts b/src/auto-reply/reply/agent-runner.ts index 2a634016c9e..b8ac5b91384 100644 --- a/src/auto-reply/reply/agent-runner.ts +++ b/src/auto-reply/reply/agent-runner.ts @@ -9,7 +9,6 @@ import { resolveSessionFilePathOptions, resolveSessionTranscriptPath, } from "../../config/sessions/paths.js"; -import { updateSessionStore, updateSessionStoreEntry } from "../../config/sessions/store.js"; import type { SessionEntry } from "../../config/sessions/types.js"; import type { TypingMode } from "../../config/types.js"; import { emitAgentEvent } from "../../infra/agent-events.js"; @@ -70,6 +69,8 @@ let contextTokensRuntimePromise: Promise< let replyMediaPathsRuntimePromise: Promise< typeof import("./reply-media-paths.runtime.js") > | null = null; +let sessionStoreRuntimePromise: Promise | null = + null; function loadPiEmbeddedQueueRuntime() { piEmbeddedQueueRuntimePromise ??= import("../../agents/pi-embedded-queue.runtime.js"); @@ -101,6 +102,11 @@ function loadReplyMediaPathsRuntime() { return replyMediaPathsRuntimePromise; } +function loadSessionStoreRuntime() { + sessionStoreRuntimePromise ??= import("../../config/sessions/store.runtime.js"); + return sessionStoreRuntimePromise; +} + export async function runReplyAgent(params: { commandBody: string; followupRun: FollowupRun; @@ -228,6 +234,7 @@ export async function runReplyAgent(params: { activeSessionEntry.updatedAt = updatedAt; activeSessionStore[sessionKey] = activeSessionEntry; if (storePath) { + const { updateSessionStoreEntry } = await loadSessionStoreRuntime(); await updateSessionStoreEntry({ storePath, sessionKey, @@ -347,6 +354,7 @@ export async function runReplyAgent(params: { nextEntry.sessionFile = nextSessionFile; activeSessionStore[sessionKey] = nextEntry; try { + const { updateSessionStore } = await loadSessionStoreRuntime(); await updateSessionStore(storePath, (store) => { store[sessionKey] = nextEntry; }); @@ -447,6 +455,7 @@ export async function runReplyAgent(params: { activeSessionEntry.updatedAt = updatedAt; activeSessionStore[sessionKey] = activeSessionEntry; if (storePath) { + const { updateSessionStoreEntry } = await loadSessionStoreRuntime(); await updateSessionStoreEntry({ storePath, sessionKey, @@ -503,6 +512,7 @@ export async function runReplyAgent(params: { activeSessionStore[sessionKey] = fallbackStateEntry; } if (sessionKey && storePath) { + const { updateSessionStoreEntry } = await loadSessionStoreRuntime(); await updateSessionStoreEntry({ storePath, sessionKey, diff --git a/src/config/sessions/store.runtime.ts b/src/config/sessions/store.runtime.ts index a409e0b38d9..53741366463 100644 --- a/src/config/sessions/store.runtime.ts +++ b/src/config/sessions/store.runtime.ts @@ -1 +1 @@ -export { updateSessionStore } from "./store.js"; +export { updateSessionStore, updateSessionStoreEntry } from "./store.js";