From 82c3eafc1c767831eeccf1bc835e004ac619771d Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Tue, 5 May 2026 21:29:31 +0200 Subject: [PATCH] fix: agent runtime duplication --- app/server/modules/agents/agents-manager.ts | 25 +++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/server/modules/agents/agents-manager.ts b/app/server/modules/agents/agents-manager.ts index 690ef7e1..9e63d9bd 100644 --- a/app/server/modules/agents/agents-manager.ts +++ b/app/server/modules/agents/agents-manager.ts @@ -5,13 +5,19 @@ import { config } from "../../core/config"; import { createAgentManagerRuntime, type AgentManagerEvent } from "./controller/server"; import { LOCAL_AGENT_ID } from "./constants"; import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process"; -import type { BackupExecutionProgress, BackupExecutionResult } from "./helpers/runtime-state"; -import { createAgentRuntimeState } from "./helpers/runtime-state"; +import { + createAgentRuntimeState, + type AgentRuntimeState, + type BackupExecutionProgress, + type BackupExecutionResult, +} from "./helpers/runtime-state"; import { getDevAgentRuntimeState } from "./helpers/runtime-state.dev"; export type { BackupExecutionProgress, BackupExecutionResult } from "./helpers/runtime-state"; export type { ProcessWithAgentRuntime } from "./helpers/runtime-state.dev"; -const productionRuntimeState = createAgentRuntimeState(); +type ProcessWithProductionAgentRuntime = NodeJS.Process & { + __zerobyteProductionAgentRuntime?: AgentRuntimeState; +}; type AgentRunBackupRequest = { scheduleId: number; @@ -20,7 +26,18 @@ type AgentRunBackupRequest = { onProgress: (progress: BackupExecutionProgress) => void; }; -const getAgentRuntimeState = () => (config.__prod__ ? productionRuntimeState : getDevAgentRuntimeState()); +const getProductionAgentRuntimeState = () => { + // Nitro production builds can bundle startup plugins and API handlers into separate chunks. + // Keep the live controller on process so both chunks see the same agent sessions. + const runtimeProcess = process as ProcessWithProductionAgentRuntime; + if (!runtimeProcess.__zerobyteProductionAgentRuntime) { + runtimeProcess.__zerobyteProductionAgentRuntime = createAgentRuntimeState(); + } + + return runtimeProcess.__zerobyteProductionAgentRuntime; +}; + +const getAgentRuntimeState = () => (config.__prod__ ? getProductionAgentRuntimeState() : getDevAgentRuntimeState()); const getAgentManagerRuntime = () => getAgentRuntimeState().agentManager; const getActiveBackupsByScheduleId = () => getAgentRuntimeState().activeBackupsByScheduleId; const getActiveBackupScheduleIdsByJobId = () => getAgentRuntimeState().activeBackupScheduleIdsByJobId;