feat(daemon,cli): RFC-003 Phase 5 — Integration (hot-reload + validate)
- Kernel: rebuild AgentRegistry on config hot-reload, log agent_registry_reload - Running threads unaffected, new threads use rebuilt registry - nerve validate: check agent name refs in WorkflowSpec source files - nerve validate: verify adapter type is known (KNOWN_AGENT_ADAPTER_IDS) - nerve validate: require extract config when WorkflowSpec agent refs exist - Tests: kernel reload (mock), validate (missing/valid/extract/adapter) Closes #239 Ref: #234
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Kernel AgentRegistry integration — rebuilt on reloadConfig (RFC-003 Phase 5).
|
||||
*/
|
||||
|
||||
import { EventEmitter } from "node:events";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { NerveConfig } from "@uncaged/nerve-core";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mockCreateAgentRegistry = vi.hoisted(() =>
|
||||
vi.fn(() => ({
|
||||
get: vi.fn(),
|
||||
getAgentConfig: vi.fn(),
|
||||
})),
|
||||
);
|
||||
|
||||
const mockChildren: MockChild[] = [];
|
||||
|
||||
type MockChild = EventEmitter & {
|
||||
send: ReturnType<typeof vi.fn>;
|
||||
kill: ReturnType<typeof vi.fn>;
|
||||
pid: number;
|
||||
};
|
||||
|
||||
function makeMockChild(pid = 1): MockChild {
|
||||
const child = new EventEmitter() as MockChild;
|
||||
setImmediate(() => {
|
||||
child.emit("message", { type: "ready" });
|
||||
});
|
||||
child.send = vi.fn((msg: unknown) => {
|
||||
if (msg === null || typeof msg !== "object") return;
|
||||
const m = msg as Record<string, unknown>;
|
||||
if (m.type === "shutdown") {
|
||||
setImmediate(() => child.emit("exit", 0, null));
|
||||
}
|
||||
});
|
||||
child.kill = vi.fn((_signal?: string) => {
|
||||
child.emit("exit", null, _signal ?? "SIGKILL");
|
||||
});
|
||||
child.pid = pid;
|
||||
return child;
|
||||
}
|
||||
|
||||
vi.mock("node:child_process", () => ({
|
||||
fork: vi.fn((_script: string, _args: string[], _opts: unknown) => {
|
||||
const child = makeMockChild(mockChildren.length + 1);
|
||||
mockChildren.push(child);
|
||||
return child;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("../agent-registry.js", () => ({
|
||||
createAgentRegistry: mockCreateAgentRegistry,
|
||||
}));
|
||||
|
||||
const { createKernel } = await import("../kernel.js");
|
||||
const { createLogStore } = await import("@uncaged/nerve-store");
|
||||
|
||||
function makeConfig(agents: NerveConfig["agents"]): NerveConfig {
|
||||
return {
|
||||
senses: {
|
||||
"cpu-usage": {
|
||||
group: "system",
|
||||
throttle: null,
|
||||
timeout: null,
|
||||
gracePeriod: null,
|
||||
retention: 10_000,
|
||||
interval: null,
|
||||
on: [],
|
||||
},
|
||||
},
|
||||
workflows: {},
|
||||
maxRounds: 10,
|
||||
agents,
|
||||
extract: null,
|
||||
api: { port: null, token: null, host: "127.0.0.1" },
|
||||
};
|
||||
}
|
||||
|
||||
describe("kernel — AgentRegistry hot-reload", () => {
|
||||
let nerveRoot: string;
|
||||
|
||||
beforeEach(() => {
|
||||
mockChildren.length = 0;
|
||||
mockCreateAgentRegistry.mockClear();
|
||||
mockCreateAgentRegistry.mockImplementation(() => ({
|
||||
get: vi.fn(),
|
||||
getAgentConfig: vi.fn(),
|
||||
}));
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true });
|
||||
nerveRoot = mkdtempSync(join(tmpdir(), "nerve-kernel-agent-reg-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
rmSync(nerveRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("rebuilds AgentRegistry on reloadConfig", async () => {
|
||||
const logStore = createLogStore(join(nerveRoot, "logs.db"));
|
||||
const a = makeConfig({
|
||||
dev: { type: "echo", model: "auto", timeout: null },
|
||||
});
|
||||
const kernel = createKernel(a, nerveRoot, { logStore });
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(mockCreateAgentRegistry).toHaveBeenCalledTimes(1);
|
||||
expect(mockCreateAgentRegistry.mock.calls[0][0]).toEqual(a.agents);
|
||||
|
||||
const b = makeConfig({
|
||||
dev: { type: "echo", model: "auto", timeout: null },
|
||||
ops: { type: "echo", model: "auto", timeout: null },
|
||||
});
|
||||
kernel.reloadConfig(b);
|
||||
|
||||
expect(mockCreateAgentRegistry).toHaveBeenCalledTimes(2);
|
||||
expect(mockCreateAgentRegistry.mock.calls[1][0]).toEqual(b.agents);
|
||||
|
||||
const reloadLogs = logStore.query({ source: "system", type: "agent_registry_reload" });
|
||||
expect(reloadLogs.length).toBe(1);
|
||||
expect(reloadLogs[0].payload).toBe(JSON.stringify({ agentNames: ["dev", "ops"] }));
|
||||
|
||||
await kernel.stop();
|
||||
await vi.runAllTimersAsync();
|
||||
});
|
||||
|
||||
it("getAgentRegistry returns the registry from the latest reload", async () => {
|
||||
const cfg = makeConfig({});
|
||||
const kernel = createKernel(cfg, nerveRoot);
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
const r1 = kernel.getAgentRegistry();
|
||||
kernel.reloadConfig(makeConfig({ x: { type: "echo", model: "auto", timeout: null } }));
|
||||
const r2 = kernel.getAgentRegistry();
|
||||
|
||||
expect(r1).not.toBe(r2);
|
||||
|
||||
await kernel.stop();
|
||||
await vi.runAllTimersAsync();
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { AgentConfig, AgentFn } from "@uncaged/nerve-core";
|
||||
import { KNOWN_AGENT_ADAPTER_IDS } from "@uncaged/nerve-core";
|
||||
|
||||
import { createEchoAgent } from "./agent-adapters/echo.js";
|
||||
|
||||
@@ -12,7 +13,9 @@ function createAgentFnForConfig(config: AgentConfig): AgentFn {
|
||||
if (config.type === "echo") {
|
||||
return createEchoAgent(config);
|
||||
}
|
||||
throw new Error(`Unknown agent adapter type: "${config.type}"`);
|
||||
throw new Error(
|
||||
`Unknown agent adapter type: "${config.type}" (known: ${KNOWN_AGENT_ADAPTER_IDS.join(", ")})`,
|
||||
);
|
||||
}
|
||||
|
||||
export function createAgentRegistry(agents: Record<string, AgentConfig>): AgentRegistry {
|
||||
|
||||
@@ -19,6 +19,8 @@ import { routeSenseComputeOutput } from "@uncaged/nerve-core";
|
||||
|
||||
import { createLogStore } from "@uncaged/nerve-store";
|
||||
import type { LogStore } from "@uncaged/nerve-store";
|
||||
import { createAgentRegistry } from "./agent-registry.js";
|
||||
import type { AgentRegistry } from "./agent-registry.js";
|
||||
import { createDaemonHandlers } from "./daemon-handlers.js";
|
||||
import { createDaemonIpcServer } from "./daemon-ipc.js";
|
||||
import type { DaemonIpcServer } from "./daemon-ipc.js";
|
||||
@@ -64,6 +66,8 @@ export type Kernel = {
|
||||
triggerSense: (senseName: string) => void;
|
||||
restartGroup: (group: string) => Promise<void>;
|
||||
reloadConfig: (newConfig: NerveConfig) => void;
|
||||
/** Agent adapters rebuilt on config hot-reload; running workflow threads keep bindings from thread start. */
|
||||
getAgentRegistry: () => AgentRegistry;
|
||||
getHealth: () => KernelHealth;
|
||||
/** HTTP/IPC-oriented health (version, uptime seconds, hostname). */
|
||||
getDaemonHealth: () => HealthInfo;
|
||||
@@ -126,6 +130,7 @@ export function createKernel(
|
||||
});
|
||||
|
||||
let config = initialConfig;
|
||||
let agentRegistry = createAgentRegistry(config.agents);
|
||||
|
||||
let _signalIdCounter = 0;
|
||||
function nextSignalId(): number {
|
||||
@@ -305,6 +310,14 @@ export function createKernel(
|
||||
const oldConfig = config;
|
||||
const oldWorkflows = config.workflows;
|
||||
config = newConfig;
|
||||
agentRegistry = createAgentRegistry(newConfig.agents);
|
||||
logStore.append({
|
||||
source: "system",
|
||||
type: "agent_registry_reload",
|
||||
refId: null,
|
||||
payload: JSON.stringify({ agentNames: Object.keys(newConfig.agents).sort() }),
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
scheduler.stop();
|
||||
scheduler = createSenseScheduler(config, bus, triggerFn, {
|
||||
logStore,
|
||||
@@ -477,6 +490,7 @@ export function createKernel(
|
||||
triggerSense,
|
||||
restartGroup: (group) => senseWorkerPool.restartGroup(group),
|
||||
reloadConfig,
|
||||
getAgentRegistry: () => agentRegistry,
|
||||
getHealth,
|
||||
getDaemonHealth,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user