fix: agent runtime duplication

This commit is contained in:
Nicolas Meienberger 2026-05-05 21:29:31 +02:00
parent 35a52554de
commit 82c3eafc1c
No known key found for this signature in database

View file

@ -5,13 +5,19 @@ import { config } from "../../core/config";
import { createAgentManagerRuntime, type AgentManagerEvent } from "./controller/server"; import { createAgentManagerRuntime, type AgentManagerEvent } from "./controller/server";
import { LOCAL_AGENT_ID } from "./constants"; import { LOCAL_AGENT_ID } from "./constants";
import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process"; import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process";
import type { BackupExecutionProgress, BackupExecutionResult } from "./helpers/runtime-state"; import {
import { createAgentRuntimeState } from "./helpers/runtime-state"; createAgentRuntimeState,
type AgentRuntimeState,
type BackupExecutionProgress,
type BackupExecutionResult,
} from "./helpers/runtime-state";
import { getDevAgentRuntimeState } from "./helpers/runtime-state.dev"; import { getDevAgentRuntimeState } from "./helpers/runtime-state.dev";
export type { BackupExecutionProgress, BackupExecutionResult } from "./helpers/runtime-state"; export type { BackupExecutionProgress, BackupExecutionResult } from "./helpers/runtime-state";
export type { ProcessWithAgentRuntime } from "./helpers/runtime-state.dev"; export type { ProcessWithAgentRuntime } from "./helpers/runtime-state.dev";
const productionRuntimeState = createAgentRuntimeState(); type ProcessWithProductionAgentRuntime = NodeJS.Process & {
__zerobyteProductionAgentRuntime?: AgentRuntimeState;
};
type AgentRunBackupRequest = { type AgentRunBackupRequest = {
scheduleId: number; scheduleId: number;
@ -20,7 +26,18 @@ type AgentRunBackupRequest = {
onProgress: (progress: BackupExecutionProgress) => void; 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 getAgentManagerRuntime = () => getAgentRuntimeState().agentManager;
const getActiveBackupsByScheduleId = () => getAgentRuntimeState().activeBackupsByScheduleId; const getActiveBackupsByScheduleId = () => getAgentRuntimeState().activeBackupsByScheduleId;
const getActiveBackupScheduleIdsByJobId = () => getAgentRuntimeState().activeBackupScheduleIdsByJobId; const getActiveBackupScheduleIdsByJobId = () => getAgentRuntimeState().activeBackupScheduleIdsByJobId;