fix: hot reload agents

This commit is contained in:
Nicolas Meienberger 2026-04-09 23:35:46 +02:00
parent 8374bc0496
commit 9dbb94fce0
No known key found for this signature in database
2 changed files with 20 additions and 8 deletions

View file

@ -1,11 +1,11 @@
import type { ChildProcess } from "node:child_process"; import type { ChildProcess } from "node:child_process";
import { createAgentManagerRuntime, type AgentManagerRuntime } from "./controller/server"; import type { AgentManagerRuntime } from "./controller/server";
import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process"; import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process";
export type { AgentBackupEventHandlers } from "./controller/server"; export type { AgentBackupEventHandlers } from "./controller/server";
type AgentRuntimeState = { type AgentRuntimeState = {
agentManager: AgentManagerRuntime; agentManager: AgentManagerRuntime | null;
localAgent: ChildProcess | null; localAgent: ChildProcess | null;
}; };
@ -22,7 +22,7 @@ const getAgentRuntimeState = () => {
} }
const runtime = { const runtime = {
agentManager: createAgentManagerRuntime(), agentManager: null,
localAgent: null, localAgent: null,
}; };
@ -32,6 +32,20 @@ const getAgentRuntimeState = () => {
const getAgentManagerRuntime = () => getAgentRuntimeState().agentManager; const getAgentManagerRuntime = () => getAgentRuntimeState().agentManager;
export const startAgentRuntime = async () => {
const runtime = getAgentRuntimeState();
if (runtime.agentManager) {
await runtime.agentManager.stop();
}
const { createAgentManagerRuntime } = await import("./controller/server");
const agentManager = createAgentManagerRuntime();
await agentManager.start();
runtime.agentManager = agentManager;
};
export const spawnLocalAgent = async () => { export const spawnLocalAgent = async () => {
await spawnLocalAgentProcess(getAgentRuntimeState()); await spawnLocalAgentProcess(getAgentRuntimeState());
}; };
@ -40,9 +54,7 @@ export const stopLocalAgent = async () => {
await stopLocalAgentProcess(getAgentRuntimeState()); await stopLocalAgentProcess(getAgentRuntimeState());
}; };
export const agentManager = getAgentManagerRuntime();
export const stopAgentRuntime = async () => { export const stopAgentRuntime = async () => {
await getAgentManagerRuntime().stop(); await getAgentManagerRuntime()?.stop();
await stopLocalAgent(); await stopLocalAgent();
}; };

View file

@ -1,5 +1,5 @@
import { runDbMigrations } from "../../db/db"; import { runDbMigrations } from "../../db/db";
import { agentManager, spawnLocalAgent, stopAgentRuntime } from "../agents/agents-manager"; import { spawnLocalAgent, startAgentRuntime, stopAgentRuntime } from "../agents/agents-manager";
import { runMigrations } from "./migrations"; import { runMigrations } from "./migrations";
import { startup } from "./startup"; import { startup } from "./startup";
@ -9,7 +9,7 @@ const runBootstrap = async () => {
await runDbMigrations(); await runDbMigrations();
await runMigrations(); await runMigrations();
if (process.env.ENABLE_LOCAL_AGENT === "true") { if (process.env.ENABLE_LOCAL_AGENT === "true") {
await agentManager.start(); await startAgentRuntime();
await spawnLocalAgent(); await spawnLocalAgent();
} }
await startup(); await startup();