Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2c379cbfd | |||
| 7cb7112ed6 | |||
| 48c81c2e19 | |||
| dd3d4315c4 | |||
| 8807b0ac6a | |||
| 5b65afdc4b | |||
| f5cb72db50 | |||
| e433e7c2a9 |
Executable
+3
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
pnpm check
|
||||
pnpm -r test
|
||||
+14
-1
@@ -19,7 +19,7 @@
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"include": ["tsup.config.ts"],
|
||||
"include": ["tsup.config.ts", "*/rslib.config.ts"],
|
||||
"linter": {
|
||||
"rules": {
|
||||
"style": {
|
||||
@@ -27,6 +27,19 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"include": ["**/__tests__/**"],
|
||||
"linter": {
|
||||
"rules": {
|
||||
"suspicious": {
|
||||
"noExplicitAny": "off"
|
||||
},
|
||||
"style": {
|
||||
"noNonNullAssertion": "off"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"linter": {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"node": ">=22.5.0"
|
||||
},
|
||||
"scripts": {
|
||||
"prepare": "husky",
|
||||
"build": "pnpm -r run build",
|
||||
"check": "biome check .",
|
||||
"format": "biome format --write ."
|
||||
@@ -12,6 +13,7 @@
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^1.9.0",
|
||||
"@rslib/core": "^0.21.3",
|
||||
"husky": "^9.1.7",
|
||||
"typescript": "^5.5.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ describe("logsCommand negative offset", () => {
|
||||
|
||||
it("exits with code 1 and writes to stderr when offset is negative", async () => {
|
||||
await expect(
|
||||
logsCommand.run!({
|
||||
logsCommand.run?.({
|
||||
args: { n: "50", offset: "-5", follow: false },
|
||||
rawArgs: [],
|
||||
cmd: logsCommand as never,
|
||||
@@ -247,7 +247,7 @@ describe("logsCommand negative offset", () => {
|
||||
|
||||
it("exits with code 1 for offset=-1", async () => {
|
||||
await expect(
|
||||
logsCommand.run!({
|
||||
logsCommand.run?.({
|
||||
args: { n: "10", offset: "-1", follow: false },
|
||||
rawArgs: [],
|
||||
cmd: logsCommand as never,
|
||||
|
||||
@@ -15,6 +15,7 @@ import { join } from "node:path";
|
||||
import { createLogStore } from "@uncaged/nerve-store";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
||||
import {
|
||||
DEFAULT_THREAD_BUDGET_CHARS,
|
||||
buildInspectOutput,
|
||||
@@ -28,7 +29,6 @@ import {
|
||||
statusIcon,
|
||||
} from "../commands/workflow.js";
|
||||
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
||||
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test helpers
|
||||
@@ -342,9 +342,14 @@ describe("partitionWorkflowMessage", () => {
|
||||
expect(p.meta).toEqual({ items: [1, 2] });
|
||||
});
|
||||
|
||||
it("uses fallback role and stringifies non-string content", () => {
|
||||
const p = partitionWorkflowMessage({ content: { n: 1 } });
|
||||
expect(p.roleStr).toBe("?");
|
||||
it("passes through role and content as-is", () => {
|
||||
const p = partitionWorkflowMessage({
|
||||
role: "unknown",
|
||||
content: '{"n":1}',
|
||||
meta: null,
|
||||
timestamp: 0,
|
||||
});
|
||||
expect(p.roleStr).toBe("unknown");
|
||||
expect(p.contentBody).toBe('{"n":1}');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -85,7 +85,7 @@ export function buildLogFooter(slice: LogSlice, nArg: number, logPath: string):
|
||||
let footer = `\n📄 ${rangeStr} | ${logPath}\n`;
|
||||
|
||||
if (slice.nextOffset !== null) {
|
||||
footer += `⏩ Earlier lines available. Fetch previous page:\n`;
|
||||
footer += "⏩ Earlier lines available. Fetch previous page:\n";
|
||||
footer += ` nerve logs --offset ${slice.nextOffset} -n ${nArg}\n`;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
|
||||
import { type SenseInfo, parseNerveConfig } from "@uncaged/nerve-core";
|
||||
import { type SenseInfo, isPlainRecord, parseNerveConfig } from "@uncaged/nerve-core";
|
||||
import { defineCommand } from "citty";
|
||||
|
||||
import { listSensesViaDaemon, triggerSenseViaDaemon } from "../daemon-client.js";
|
||||
import {
|
||||
assertSenseDbExists,
|
||||
defaultPreviewSql,
|
||||
formatRowsAsAlignedTable,
|
||||
listTableSqlStatements,
|
||||
@@ -240,7 +239,8 @@ const senseQueryCommand = defineCommand({
|
||||
}
|
||||
}
|
||||
|
||||
const rows = db.prepare(sql).all() as Record<string, unknown>[];
|
||||
const rawRows: unknown[] = db.prepare(sql).all();
|
||||
const rows: Record<string, unknown>[] = rawRows.filter(isPlainRecord);
|
||||
|
||||
if (args.json) {
|
||||
process.stdout.write(`${JSON.stringify(rows, null, 2)}\n`);
|
||||
|
||||
@@ -74,9 +74,11 @@ async function runDaemon(nerveRoot: string): Promise<void> {
|
||||
|
||||
const bootstrapPath = daemonBootstrapScript();
|
||||
|
||||
// After `open`, file-backed WriteStream has a numeric OS fd for spawn stdio; `@types/node` omits `fd` on this WriteStream alias.
|
||||
const logFd = (logStream as unknown as { fd: number }).fd;
|
||||
const child = spawn(process.execPath, [bootstrapPath], {
|
||||
detached: true,
|
||||
stdio: ["ignore", (logStream as any).fd, (logStream as any).fd],
|
||||
stdio: ["ignore", logFd, logFd],
|
||||
env: { ...process.env, NERVE_ROOT: nerveRoot },
|
||||
cwd: nerveRoot,
|
||||
});
|
||||
|
||||
@@ -47,7 +47,11 @@ export const statusCommand = defineCommand({
|
||||
return;
|
||||
}
|
||||
|
||||
const pid = readPidFile() as number;
|
||||
const pid = readPidFile();
|
||||
if (pid === null) {
|
||||
process.stdout.write("😴 Nerve daemon is not running.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const configPath = join(getNerveRoot(), "nerve.yaml");
|
||||
let senseList: string[] = [];
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { isPlainRecord } from "@uncaged/nerve-core";
|
||||
import { defineCommand } from "citty";
|
||||
import { stringify } from "yaml";
|
||||
|
||||
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
||||
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
||||
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
||||
import { loadDaemonModule } from "../workspace-daemon.js";
|
||||
import { getNerveRoot, getSocketPath, isRunning } from "../workspace.js";
|
||||
|
||||
@@ -203,7 +204,9 @@ export function partitionWorkflowMessage(msg: {
|
||||
const contentBody = msg.content;
|
||||
const meta: Record<string, unknown> =
|
||||
msg.meta !== null && msg.meta !== undefined && typeof msg.meta === "object"
|
||||
? (msg.meta as Record<string, unknown>)
|
||||
? isPlainRecord(msg.meta)
|
||||
? msg.meta
|
||||
: (msg.meta as Record<string, unknown>)
|
||||
: {};
|
||||
return { roleStr, contentBody, meta };
|
||||
}
|
||||
@@ -215,13 +218,7 @@ export function formatThreadRoundBlock(row: ThreadRoundRow): string {
|
||||
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
||||
const yamlBlock =
|
||||
Object.keys(meta).length === 0 ? "{}\n" : `${stringify(meta, { lineWidth: 100 })}\n`;
|
||||
return (
|
||||
`[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n` +
|
||||
`---\n` +
|
||||
yamlBlock +
|
||||
`---\n` +
|
||||
`${contentBody}\n\n`
|
||||
);
|
||||
return `[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n---\n${yamlBlock}---\n${contentBody}\n\n`;
|
||||
}
|
||||
|
||||
export type ThreadCommandOutput = {
|
||||
@@ -229,6 +226,33 @@ export type ThreadCommandOutput = {
|
||||
paginationHint: string | null;
|
||||
};
|
||||
|
||||
function buildTruncatedSingleRound(
|
||||
row: ThreadRoundRow,
|
||||
remaining: number,
|
||||
prefixLines: string[],
|
||||
runId: string,
|
||||
budgetFlag: string,
|
||||
): ThreadCommandOutput {
|
||||
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
||||
const yamlBlock =
|
||||
Object.keys(meta).length === 0 ? "{}\n" : `${stringify(meta, { lineWidth: 100 })}\n`;
|
||||
const header = `[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n---\n${yamlBlock}---\n`;
|
||||
const maxBody = Math.max(0, remaining - header.length - "[truncated]\n".length);
|
||||
const truncated =
|
||||
maxBody > 0 && contentBody.length > maxBody
|
||||
? `${contentBody.slice(0, maxBody)}\n[truncated]\n`
|
||||
: `${contentBody}\n[truncated]\n`;
|
||||
const single = `${header + truncated}\n`;
|
||||
const hintRound = row.round;
|
||||
return {
|
||||
lines: [...prefixLines, single],
|
||||
paginationHint:
|
||||
hintRound > 1
|
||||
? `\n⏩ Older rounds exist. Fetch with:\n nerve workflow thread ${runId} --before ${String(hintRound)}${budgetFlag}\n`
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build stdout lines for `nerve workflow thread`: newest-first selection from
|
||||
* `descRows` until `budgetChars` (including `prefixLines`), then chronological order.
|
||||
@@ -254,25 +278,7 @@ export function buildThreadCommandOutput(
|
||||
continue;
|
||||
}
|
||||
if (picked.length === 0) {
|
||||
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
||||
const yamlBlock =
|
||||
Object.keys(meta).length === 0 ? "{}\n" : `${stringify(meta, { lineWidth: 100 })}\n`;
|
||||
const header =
|
||||
`[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n` + `---\n` + yamlBlock + `---\n`;
|
||||
const maxBody = Math.max(0, remaining - header.length - `[truncated]\n`.length);
|
||||
const truncated =
|
||||
maxBody > 0 && contentBody.length > maxBody
|
||||
? `${contentBody.slice(0, maxBody)}\n[truncated]\n`
|
||||
: `${contentBody}\n[truncated]\n`;
|
||||
const single = header + truncated + "\n";
|
||||
const hintRound = row.round;
|
||||
return {
|
||||
lines: [...prefixLines, single],
|
||||
paginationHint:
|
||||
hintRound > 1
|
||||
? `\n⏩ Older rounds exist. Fetch with:\n nerve workflow thread ${runId} --before ${String(hintRound)}${budgetFlag}\n`
|
||||
: null,
|
||||
};
|
||||
return buildTruncatedSingleRound(row, remaining, prefixLines, runId, budgetFlag);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -281,9 +287,7 @@ export function buildThreadCommandOutput(
|
||||
const shownMinRound = picked.length === 0 ? null : Math.min(...picked.map((r) => r.round));
|
||||
let paginationHint: string | null = null;
|
||||
if (shownMinRound !== null && shownMinRound > 1) {
|
||||
paginationHint =
|
||||
`\n⏩ Older rounds not shown. Fetch with:\n` +
|
||||
` nerve workflow thread ${runId} --before ${String(shownMinRound)}${budgetFlag}\n`;
|
||||
paginationHint = `\n⏩ Older rounds not shown. Fetch with:\n nerve workflow thread ${runId} --before ${String(shownMinRound)}${budgetFlag}\n`;
|
||||
}
|
||||
|
||||
return { lines: [...prefixLines, ...blocksAsc], paginationHint };
|
||||
@@ -452,10 +456,7 @@ const workflowThreadCommand = defineCommand({
|
||||
const totalRoleRounds = store.getThreadRoundCount(args.runId);
|
||||
if (totalRoleRounds === 0) {
|
||||
process.stdout.write(
|
||||
`🧵 Workflow thread: ${run.runId}\n` +
|
||||
` workflow: ${run.workflow}\n` +
|
||||
` status: ${run.status}\n\n` +
|
||||
`📭 No role rounds recorded for this run.\n`,
|
||||
`🧵 Workflow thread: ${run.runId}\n workflow: ${run.workflow}\n status: ${run.status}\n\n📭 No role rounds recorded for this run.\n`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -466,7 +467,7 @@ const workflowThreadCommand = defineCommand({
|
||||
});
|
||||
|
||||
const prefixLines = [
|
||||
`🧵 Role rounds (workflow thread)\n`,
|
||||
"🧵 Role rounds (workflow thread)\n",
|
||||
` runId: ${run.runId}\n`,
|
||||
` workflow: ${run.workflow}\n`,
|
||||
` status: ${run.status}\n`,
|
||||
|
||||
@@ -9,6 +9,7 @@ import { connect } from "node:net";
|
||||
import type { Socket } from "node:net";
|
||||
|
||||
import type { SenseInfo } from "@uncaged/nerve-core";
|
||||
import { isPlainRecord } from "@uncaged/nerve-core";
|
||||
|
||||
const CONNECT_TIMEOUT_MS = 3_000;
|
||||
const RESPONSE_TIMEOUT_MS = 5_000;
|
||||
@@ -19,11 +20,22 @@ type TriggerResponse = { ok: true } | { ok: false; error: string };
|
||||
|
||||
type ListSensesResponse = { ok: true; senses: SenseInfo[] } | { ok: false; error: string };
|
||||
|
||||
function isSenseInfo(value: unknown): value is SenseInfo {
|
||||
if (!isPlainRecord(value)) return false;
|
||||
return (
|
||||
typeof value.name === "string" &&
|
||||
typeof value.group === "string" &&
|
||||
(value.throttle === null || typeof value.throttle === "number") &&
|
||||
(value.timeout === null || typeof value.timeout === "number") &&
|
||||
(value.lastSignalTs === null || typeof value.lastSignalTs === "number")
|
||||
);
|
||||
}
|
||||
|
||||
function parseDaemonResponse(line: string): TriggerResponse {
|
||||
try {
|
||||
const obj = JSON.parse(line) as unknown;
|
||||
if (obj !== null && typeof obj === "object") {
|
||||
const r = obj as Record<string, unknown>;
|
||||
const obj: unknown = JSON.parse(line);
|
||||
if (isPlainRecord(obj)) {
|
||||
const r = obj;
|
||||
if (r.ok === true) return { ok: true };
|
||||
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
||||
}
|
||||
@@ -35,12 +47,13 @@ function parseDaemonResponse(line: string): TriggerResponse {
|
||||
|
||||
function parseListSensesResponse(line: string): ListSensesResponse {
|
||||
try {
|
||||
const obj = JSON.parse(line) as unknown;
|
||||
if (obj !== null && typeof obj === "object") {
|
||||
const r = obj as Record<string, unknown>;
|
||||
const obj: unknown = JSON.parse(line);
|
||||
if (isPlainRecord(obj)) {
|
||||
const r = obj;
|
||||
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
||||
if (r.ok === true && Array.isArray(r.senses))
|
||||
return { ok: true, senses: r.senses as SenseInfo[] };
|
||||
if (r.ok === true && Array.isArray(r.senses) && r.senses.every(isSenseInfo)) {
|
||||
return { ok: true, senses: r.senses };
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
|
||||
@@ -46,5 +46,6 @@ export type DaemonModule = {
|
||||
export async function loadDaemonModule(nerveRoot: string): Promise<DaemonModule> {
|
||||
const entry = assertWorkspaceDaemonInstalled(nerveRoot);
|
||||
const url = pathToFileURL(entry).href;
|
||||
// Dynamic import return type is module-specific; narrow at this workspace boundary.
|
||||
return import(url) as Promise<DaemonModule>;
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ reflexes:
|
||||
expect(result.error.message).toMatch(/disk.*not found in senses/);
|
||||
});
|
||||
|
||||
it("returns error when workflow reflex references a non-existent workflow", () => {
|
||||
it("returns error when reflex uses unsupported workflow field", () => {
|
||||
const yaml = `
|
||||
senses:
|
||||
cpu:
|
||||
@@ -206,10 +206,10 @@ reflexes:
|
||||
const result = parseNerveConfig(yaml);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toMatch(/missing_wf.*not found in workflows/);
|
||||
expect(result.error.message).toMatch(/workflow.*not supported/);
|
||||
});
|
||||
|
||||
it("returns error when workflow reflex references non-existent workflow (with workflows defined)", () => {
|
||||
it("returns error when reflex uses unsupported workflow field (with workflows defined)", () => {
|
||||
const yaml = `
|
||||
senses:
|
||||
cpu:
|
||||
@@ -226,7 +226,7 @@ workflows:
|
||||
const result = parseNerveConfig(yaml);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toMatch(/unknown.*not found in workflows/);
|
||||
expect(result.error.message).toMatch(/workflow.*not supported/);
|
||||
});
|
||||
|
||||
it("returns error for invalid throttle format", () => {
|
||||
@@ -354,7 +354,7 @@ reflexes:
|
||||
const result = parseNerveConfig(yaml);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toMatch(/cannot have both/);
|
||||
expect(result.error.message).toMatch(/workflow.*not supported/);
|
||||
});
|
||||
|
||||
it("returns error when reflex has neither sense nor workflow", () => {
|
||||
@@ -368,7 +368,7 @@ reflexes:
|
||||
const result = parseNerveConfig(yaml);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toMatch(/must have either/);
|
||||
expect(result.error.message).toMatch(/must include "sense"/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+15
-14
@@ -1,5 +1,6 @@
|
||||
import { parse } from "yaml";
|
||||
|
||||
import { isPlainRecord } from "./is-plain-record.js";
|
||||
import type { Result } from "./result.js";
|
||||
import { err, ok } from "./result.js";
|
||||
import type { NerveConfig, ReflexConfig, SenseConfig, WorkflowConfig } from "./types.js";
|
||||
@@ -40,11 +41,11 @@ function parseDurationField(field: unknown, label: string): Result<number | null
|
||||
}
|
||||
|
||||
function validateSenseConfig(name: string, raw: unknown): Result<SenseConfig> {
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
if (!isPlainRecord(raw)) {
|
||||
return err(new Error(`senses.${name}: must be an object`));
|
||||
}
|
||||
|
||||
const obj = raw as Record<string, unknown>;
|
||||
const obj = raw;
|
||||
|
||||
if (typeof obj.group !== "string" || obj.group.trim() === "") {
|
||||
return err(new Error(`senses.${name}.group: required string`));
|
||||
@@ -77,10 +78,10 @@ function validateSenseConfig(name: string, raw: unknown): Result<SenseConfig> {
|
||||
|
||||
function parseOnField(index: number, obj: Record<string, unknown>): Result<string[] | null> {
|
||||
if (obj.on === undefined || obj.on === null) return ok(null);
|
||||
if (!Array.isArray(obj.on) || !obj.on.every((item) => typeof item === "string")) {
|
||||
if (!Array.isArray(obj.on) || !obj.on.every((item): item is string => typeof item === "string")) {
|
||||
return err(new Error(`reflexes[${index}].on: must be an array of strings`));
|
||||
}
|
||||
return ok(obj.on as string[]);
|
||||
return ok(obj.on);
|
||||
}
|
||||
|
||||
function parseSenseReflex(
|
||||
@@ -118,11 +119,11 @@ function validateReflexConfig(
|
||||
raw: unknown,
|
||||
senseNames: Set<string>,
|
||||
): Result<ReflexConfig> {
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
if (!isPlainRecord(raw)) {
|
||||
return err(new Error(`reflexes[${index}]: must be an object`));
|
||||
}
|
||||
|
||||
const obj = raw as Record<string, unknown>;
|
||||
const obj = raw;
|
||||
const hasSense = obj.sense !== undefined;
|
||||
const hasWorkflowKey = Object.hasOwn(obj, "workflow");
|
||||
|
||||
@@ -158,11 +159,11 @@ function parseEngineMaxRounds(obj: Record<string, unknown>): Result<number> {
|
||||
}
|
||||
|
||||
function validateWorkflowConfig(name: string, raw: unknown): Result<WorkflowConfig> {
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
if (!isPlainRecord(raw)) {
|
||||
return err(new Error(`workflows.${name}: must be an object`));
|
||||
}
|
||||
|
||||
const obj = raw as Record<string, unknown>;
|
||||
const obj = raw;
|
||||
|
||||
if (
|
||||
typeof obj.concurrency !== "number" ||
|
||||
@@ -209,11 +210,11 @@ function validateWorkflowConfig(name: string, raw: unknown): Result<WorkflowConf
|
||||
function parseSenses(
|
||||
obj: Record<string, unknown>,
|
||||
): Result<{ senses: Record<string, SenseConfig>; senseNames: Set<string> }> {
|
||||
if (obj.senses === null || typeof obj.senses !== "object" || Array.isArray(obj.senses)) {
|
||||
if (!isPlainRecord(obj.senses)) {
|
||||
return err(new Error("senses: required object"));
|
||||
}
|
||||
|
||||
const sensesRaw = obj.senses as Record<string, unknown>;
|
||||
const sensesRaw = obj.senses;
|
||||
const senses: Record<string, SenseConfig> = {};
|
||||
const senseNames = new Set(Object.keys(sensesRaw));
|
||||
|
||||
@@ -249,11 +250,11 @@ function parseWorkflows(
|
||||
): Result<Record<string, WorkflowConfig> | null> {
|
||||
if (obj.workflows === undefined || obj.workflows === null) return ok(null);
|
||||
|
||||
if (typeof obj.workflows !== "object" || Array.isArray(obj.workflows)) {
|
||||
if (!isPlainRecord(obj.workflows)) {
|
||||
return err(new Error("workflows: must be an object if provided"));
|
||||
}
|
||||
|
||||
const workflowsRaw = obj.workflows as Record<string, unknown>;
|
||||
const workflowsRaw = obj.workflows;
|
||||
const workflows: Record<string, WorkflowConfig> = {};
|
||||
|
||||
for (const [name, wfRaw] of Object.entries(workflowsRaw)) {
|
||||
@@ -275,11 +276,11 @@ export function parseNerveConfig(raw: string): Result<NerveConfig> {
|
||||
return err(new Error(`YAML parse error: ${message}`));
|
||||
}
|
||||
|
||||
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
if (!isPlainRecord(parsed)) {
|
||||
return err(new Error("Config must be a YAML object"));
|
||||
}
|
||||
|
||||
const obj = parsed as Record<string, unknown>;
|
||||
const obj = parsed;
|
||||
|
||||
const sensesResult = parseSenses(obj);
|
||||
if (!sensesResult.ok) return sensesResult;
|
||||
|
||||
@@ -22,6 +22,13 @@ export { START, END, DEFAULT_ENGINE_MAX_ROUNDS } from "./types.js";
|
||||
export type { Result } from "./result.js";
|
||||
export { ok, err } from "./result.js";
|
||||
export { parseNerveConfig } from "./config.js";
|
||||
export { isPlainRecord } from "./is-plain-record.js";
|
||||
|
||||
export type { ParsedSenseWorkflowDirective, SenseComputeRoute } from "./sense-workflow-directive.js";
|
||||
export { parseSenseWorkflowDirective, routeSenseComputeOutput } from "./sense-workflow-directive.js";
|
||||
export type {
|
||||
ParsedSenseWorkflowDirective,
|
||||
SenseComputeRoute,
|
||||
} from "./sense-workflow-directive.js";
|
||||
export {
|
||||
parseSenseWorkflowDirective,
|
||||
routeSenseComputeOutput,
|
||||
} from "./sense-workflow-directive.js";
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Narrows `unknown` to a plain JSON-style object (not null, not array).
|
||||
* Use after `JSON.parse` / YAML / IPC when validating structure field-by-field.
|
||||
*/
|
||||
export function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isPlainRecord } from "./is-plain-record.js";
|
||||
import type { Result } from "./result.js";
|
||||
import { err, ok } from "./result.js";
|
||||
|
||||
@@ -54,10 +55,10 @@ function stripWorkflowKey(payload: Record<string, unknown>): Record<string, unkn
|
||||
* - `workflow: "name|n|prompt"` → launch workflow; no Signal is emitted to the bus
|
||||
*/
|
||||
export function routeSenseComputeOutput(payload: unknown): SenseComputeRoute {
|
||||
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
||||
if (!isPlainRecord(payload)) {
|
||||
return { kind: "signal", payload };
|
||||
}
|
||||
const obj = payload as Record<string, unknown>;
|
||||
const obj = payload;
|
||||
if (!Object.hasOwn(obj, "workflow")) {
|
||||
return { kind: "signal", payload };
|
||||
}
|
||||
|
||||
@@ -235,7 +235,6 @@ describe("WorkflowManager — crash recovery (Phase 3)", () => {
|
||||
expect(resumeCalls[0][0]).toMatchObject({
|
||||
type: "resume-thread",
|
||||
runId: "run-started-1",
|
||||
triggerPayload: { trigger: "initial" },
|
||||
});
|
||||
expect(Array.isArray((resumeCalls[0][0] as Record<string, unknown>).messages)).toBe(true);
|
||||
|
||||
@@ -318,8 +317,8 @@ describe("WorkflowManager — crash recovery (Phase 3)", () => {
|
||||
});
|
||||
const mgr = createWorkflowManager("/nerve-root", config, logStore);
|
||||
|
||||
const payload = { prompt: "build-docker for myrepo", maxRounds: 10 };
|
||||
mgr.startWorkflow("my-wf", payload);
|
||||
const launch = { prompt: "build-docker for myrepo", maxRounds: 10 };
|
||||
mgr.startWorkflow("my-wf", launch);
|
||||
|
||||
const startedCall = logStore.upsertWorkflowRun.mock.calls.find(
|
||||
(args: any[]) => (args[0] as { type: string }).type === "started",
|
||||
@@ -328,7 +327,7 @@ describe("WorkflowManager — crash recovery (Phase 3)", () => {
|
||||
const logEntry = startedCall?.[0] as { payload: string | null };
|
||||
expect(logEntry.payload).not.toBeNull();
|
||||
const parsed = JSON.parse(logEntry.payload as string) as Record<string, unknown>;
|
||||
expect(parsed.triggerPayload).toMatchObject(payload);
|
||||
expect(parsed).toMatchObject({ prompt: "build-docker for myrepo", maxRounds: 10 });
|
||||
|
||||
const stopPromise = mgr.stop();
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
@@ -152,12 +152,16 @@ describe("daemon-ipc — trigger-sense", () => {
|
||||
const resp = await sendRaw(sockPath, {
|
||||
type: "trigger-workflow",
|
||||
workflow: "my-workflow",
|
||||
payload: {},
|
||||
prompt: "test prompt",
|
||||
maxRounds: 10,
|
||||
});
|
||||
|
||||
expect(resp).toEqual({ ok: true });
|
||||
expect(triggerSense).not.toHaveBeenCalled();
|
||||
expect(wfManager.startWorkflow).toHaveBeenCalledWith("my-workflow", {});
|
||||
expect(wfManager.startWorkflow).toHaveBeenCalledWith("my-workflow", {
|
||||
prompt: "test prompt",
|
||||
maxRounds: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it("responds ok:false for completely unknown request type", async () => {
|
||||
|
||||
@@ -304,7 +304,7 @@ describe("Kernel — workflow hot reload via file-watcher (Phase 3)", () => {
|
||||
senses: {},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
kernel.reloadConfig(newConfig);
|
||||
|
||||
|
||||
@@ -181,7 +181,7 @@ describe("kernel — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
});
|
||||
|
||||
expect(kernel.groups.has("network")).toBe(true);
|
||||
@@ -198,7 +198,7 @@ describe("kernel — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
const kernel = createKernel(config, "/tmp/nerve-test");
|
||||
|
||||
@@ -213,7 +213,7 @@ describe("kernel — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
});
|
||||
|
||||
expect(kernel.groups.has("network")).toBe(false);
|
||||
@@ -236,7 +236,7 @@ describe("kernel — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
});
|
||||
|
||||
expect(kernel.getHealth().activeSenses).toBe(2);
|
||||
|
||||
@@ -116,14 +116,14 @@ describe("kernel + workflowManager integration", () => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("sense signal triggers workflow via reflex", () => {
|
||||
it("calls workflowManager.startWorkflow when a sense signal fires on a workflow reflex", async () => {
|
||||
describe("sense compute triggers workflow via return value", () => {
|
||||
it("calls workflowManager.startWorkflow when a sense compute returns a workflow launch", async () => {
|
||||
const logStore = makeLogStore();
|
||||
const config = makeConfig({
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "my-workflow", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "my-workflow": { concurrency: 2, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -132,14 +132,20 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
// Emit a signal from "cpu-usage" on the bus
|
||||
const { createSignalBus } = await import("../signal-bus.js");
|
||||
void createSignalBus; // ensure import resolves
|
||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: { value: 80 }, ts: Date.now() });
|
||||
// Simulate a sense worker sending a signal with workflow launch payload
|
||||
// The kernel's handleWorkerMessage processes "signal" type messages
|
||||
// and uses routeSenseComputeOutput to detect workflow launches
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
// Simulate the worker sending a signal message with workflow field
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "my-workflow|10|run this workflow" },
|
||||
});
|
||||
}
|
||||
|
||||
// The workflow worker should be spawned (one for the sense group, one for workflow)
|
||||
// The sense group worker is mockChildren[0]; the workflow worker is mockChildren[1]
|
||||
// We need to check that a start-thread message was sent to the workflow worker
|
||||
// A workflow worker should be spawned and a start-thread message sent
|
||||
const workflowWorker = mockChildren.find((c) =>
|
||||
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
||||
(args: unknown[]) =>
|
||||
@@ -155,13 +161,13 @@ describe("kernel + workflowManager integration", () => {
|
||||
await stopPromise;
|
||||
});
|
||||
|
||||
it("passes the signal payload as triggerPayload to the workflow", async () => {
|
||||
it("passes prompt and maxRounds from the workflow field to the workflow", async () => {
|
||||
const logStore = makeLogStore();
|
||||
const config = makeConfig({
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "alert-workflow", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "alert-workflow": { concurrency: 1, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -170,8 +176,15 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
const payload = { level: "critical", value: 99 };
|
||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload, ts: Date.now() });
|
||||
// Simulate sense worker returning a workflow launch
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "alert-workflow|5|handle critical alert" },
|
||||
});
|
||||
}
|
||||
|
||||
// Find the start-thread call and verify triggerPayload
|
||||
const startThreadCall = mockChildren
|
||||
@@ -187,7 +200,8 @@ describe("kernel + workflowManager integration", () => {
|
||||
expect(startThreadCall?.[0]).toMatchObject({
|
||||
type: "start-thread",
|
||||
workflow: "alert-workflow",
|
||||
triggerPayload: payload,
|
||||
prompt: "handle critical alert",
|
||||
maxRounds: 5,
|
||||
});
|
||||
|
||||
const stopPromise = kernel.stop();
|
||||
@@ -202,7 +216,7 @@ describe("kernel + workflowManager integration", () => {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
"disk-io": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "my-workflow", on: ["disk-io"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "my-workflow": { concurrency: 1, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -211,10 +225,17 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
// Emit signal from cpu-usage — NOT in the workflow's "on" list
|
||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: 50, ts: Date.now() });
|
||||
// Emit a regular signal (no workflow field) — should NOT trigger any workflow
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: 50,
|
||||
});
|
||||
}
|
||||
|
||||
// No workflow worker should have been spawned (only the sense group worker)
|
||||
// No workflow should have been started
|
||||
const workflowWorkerSpawned = mockChildren.some((c) =>
|
||||
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
||||
(args: unknown[]) =>
|
||||
@@ -232,13 +253,13 @@ describe("kernel + workflowManager integration", () => {
|
||||
});
|
||||
|
||||
describe("workflow events are logged", () => {
|
||||
it("logs a 'started' event when workflow thread is triggered", async () => {
|
||||
it("logs a 'started' event when workflow thread is triggered via sense compute", async () => {
|
||||
const logStore = makeLogStore();
|
||||
const config = makeConfig({
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "log-test-workflow", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "log-test-workflow": { concurrency: 2, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -247,7 +268,15 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: null, ts: Date.now() });
|
||||
// Simulate sense compute returning a workflow launch
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "log-test-workflow|10|test prompt" },
|
||||
});
|
||||
}
|
||||
|
||||
expect(logStore.upsertWorkflowRun).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: "workflow", type: "started" }),
|
||||
@@ -261,7 +290,7 @@ describe("kernel + workflowManager integration", () => {
|
||||
});
|
||||
|
||||
describe("reloadConfig handles workflow changes", () => {
|
||||
it("new workflow reflexes are active after reloadConfig", async () => {
|
||||
it("new workflows are available after reloadConfig", async () => {
|
||||
const logStore = makeLogStore();
|
||||
const initialConfig = makeConfig({
|
||||
senses: {
|
||||
@@ -269,7 +298,7 @@ describe("kernel + workflowManager integration", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
});
|
||||
|
||||
const kernel = createKernel(initialConfig, "/tmp/nerve-test", {
|
||||
@@ -277,19 +306,26 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
// Reload with a workflow reflex added
|
||||
// Reload with a workflow added
|
||||
const newConfig: NerveConfig = {
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "new-workflow", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "new-workflow": { concurrency: 1, overflow: "drop" } },
|
||||
maxRounds: 10,
|
||||
};
|
||||
kernel.reloadConfig(newConfig);
|
||||
|
||||
// Now emit a signal — should trigger the new workflow
|
||||
kernel.bus.emit({ id: 2, senseId: "cpu-usage", payload: "reload-test", ts: Date.now() });
|
||||
// Simulate sense compute returning a workflow launch for the new workflow
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "new-workflow|10|reload test" },
|
||||
});
|
||||
}
|
||||
|
||||
const startThreadCall = mockChildren
|
||||
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
||||
@@ -308,13 +344,13 @@ describe("kernel + workflowManager integration", () => {
|
||||
await stopPromise;
|
||||
});
|
||||
|
||||
it("old workflow reflexes are removed after reloadConfig", async () => {
|
||||
it("old workflows are removed after reloadConfig", async () => {
|
||||
const logStore = makeLogStore();
|
||||
const initialConfig = makeConfig({
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "old-workflow", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "old-workflow": { concurrency: 1, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -323,14 +359,14 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
// Reload with the workflow reflex removed
|
||||
// Reload with the workflow removed
|
||||
const newConfig: NerveConfig = {
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
kernel.reloadConfig(newConfig);
|
||||
|
||||
@@ -339,8 +375,15 @@ describe("kernel + workflowManager integration", () => {
|
||||
(c.send as ReturnType<typeof vi.fn>).mockClear();
|
||||
}
|
||||
|
||||
// Emit a signal — old-workflow should NOT be triggered
|
||||
kernel.bus.emit({ id: 3, senseId: "cpu-usage", payload: "after-reload", ts: Date.now() });
|
||||
// Simulate sense compute trying to launch the old workflow — it should still not start
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "old-workflow|10|should not work" },
|
||||
});
|
||||
}
|
||||
|
||||
const startThreadCall = mockChildren
|
||||
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
||||
@@ -366,7 +409,7 @@ describe("kernel + workflowManager integration", () => {
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "shutdown-test", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "shutdown-test": { concurrency: 1, overflow: "drop" } },
|
||||
});
|
||||
|
||||
@@ -375,8 +418,15 @@ describe("kernel + workflowManager integration", () => {
|
||||
logStore,
|
||||
});
|
||||
|
||||
// Trigger a workflow so a worker is spawned
|
||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: null, ts: Date.now() });
|
||||
// Trigger a workflow via sense compute return value
|
||||
const workerPool = mockChildren[0];
|
||||
if (workerPool) {
|
||||
workerPool.emit("message", {
|
||||
type: "signal",
|
||||
sense: "cpu-usage",
|
||||
payload: { workflow: "shutdown-test|10|test" },
|
||||
});
|
||||
}
|
||||
|
||||
const stopPromise = kernel.stop();
|
||||
await vi.runAllTimersAsync();
|
||||
@@ -408,7 +458,7 @@ describe("kernel + workflowManager integration", () => {
|
||||
senses: {
|
||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||
},
|
||||
reflexes: [{ kind: "workflow", workflow: "health-wf", on: ["cpu-usage"] } as any],
|
||||
reflexes: [],
|
||||
workflows: { "health-wf": { concurrency: 2, overflow: "drop" } },
|
||||
});
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ describe("kernel — groupForSense mapping", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
const kernel = createKernel(config, "/tmp/nerve-test");
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ describe("LogStore + ReflexScheduler integration", () => {
|
||||
},
|
||||
reflexes: [{ kind: "sense", sense: "cpu-usage", interval: null, on: ["cpu-usage"] }],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
const bus = createSignalBus();
|
||||
const triggered: string[] = [];
|
||||
@@ -58,7 +58,7 @@ describe("LogStore + ReflexScheduler integration", () => {
|
||||
},
|
||||
reflexes: [{ kind: "sense", sense: "cpu-usage", interval: 1000, on: null }],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
const bus = createSignalBus();
|
||||
const ref: { scheduler: ReturnType<typeof createReflexScheduler> | null } = { scheduler: null };
|
||||
@@ -89,7 +89,7 @@ describe("LogStore + ReflexScheduler integration", () => {
|
||||
},
|
||||
reflexes: [{ kind: "sense", sense: "cpu-usage", interval: null, on: ["cpu-usage"] }],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
const bus = createSignalBus();
|
||||
const triggered: string[] = [];
|
||||
|
||||
@@ -137,7 +137,7 @@ describe("phase6 — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
|
||||
kernel.reloadConfig(newConfig);
|
||||
@@ -157,7 +157,7 @@ describe("phase6 — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
kernel = createKernel(config, "/tmp/nerve-phase6-test", {
|
||||
workerScript: MOCK_WORKER,
|
||||
@@ -172,7 +172,7 @@ describe("phase6 — reloadConfig", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
|
||||
kernel.reloadConfig(newConfig);
|
||||
@@ -203,7 +203,7 @@ describe("phase6 — error isolation", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
|
||||
kernel = createKernel(config, "/tmp/nerve-phase6-test", {
|
||||
@@ -307,7 +307,7 @@ describe("phase6 — getHealth", () => {
|
||||
},
|
||||
reflexes: [],
|
||||
workflows: null,
|
||||
maxRounds: 10,
|
||||
maxRounds: 10,
|
||||
};
|
||||
kernel.reloadConfig(newConfig);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import { rmSync } from "node:fs";
|
||||
import { type Server, type Socket, createServer } from "node:net";
|
||||
|
||||
import type { SenseInfo } from "@uncaged/nerve-core";
|
||||
import { isPlainRecord } from "@uncaged/nerve-core";
|
||||
|
||||
import type { WorkflowManager } from "./workflow-manager.js";
|
||||
|
||||
@@ -51,14 +52,19 @@ export type DaemonIpcServer = {
|
||||
|
||||
function parseRequest(line: string): DaemonRequest | null {
|
||||
try {
|
||||
const obj = JSON.parse(line) as unknown;
|
||||
if (obj === null || typeof obj !== "object") return null;
|
||||
const req = obj as Record<string, unknown>;
|
||||
const obj: unknown = JSON.parse(line);
|
||||
if (!isPlainRecord(obj)) return null;
|
||||
const req = obj;
|
||||
if (req.type === "trigger-workflow") {
|
||||
if (typeof req.workflow !== "string" || req.workflow.length === 0) return null;
|
||||
if (typeof req.prompt !== "string") return null;
|
||||
if (typeof req.maxRounds !== "number") return null;
|
||||
return { type: "trigger-workflow", workflow: req.workflow, prompt: req.prompt, maxRounds: req.maxRounds as number };
|
||||
return {
|
||||
type: "trigger-workflow",
|
||||
workflow: req.workflow,
|
||||
prompt: req.prompt,
|
||||
maxRounds: req.maxRounds,
|
||||
};
|
||||
}
|
||||
if (req.type === "trigger-sense") {
|
||||
if (typeof req.sense !== "string" || req.sense.length === 0) return null;
|
||||
@@ -105,7 +111,10 @@ export function createDaemonIpcServer(
|
||||
|
||||
try {
|
||||
if (req.type === "trigger-workflow") {
|
||||
workflowManager.startWorkflow(req.workflow, { prompt: req.prompt, maxRounds: req.maxRounds });
|
||||
workflowManager.startWorkflow(req.workflow, {
|
||||
prompt: req.prompt,
|
||||
maxRounds: req.maxRounds,
|
||||
});
|
||||
const resp: DaemonResponse = { ok: true };
|
||||
socket.write(`${JSON.stringify(resp)}\n`);
|
||||
} else if (req.type === "trigger-sense") {
|
||||
|
||||
+96
-43
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import type { Result } from "@uncaged/nerve-core";
|
||||
import { err, ok } from "@uncaged/nerve-core";
|
||||
import { err, isPlainRecord, ok } from "@uncaged/nerve-core";
|
||||
|
||||
/** Parent → Worker: trigger one compute cycle for a sense */
|
||||
export type ComputeMessage = {
|
||||
@@ -148,76 +148,115 @@ function validateResumeThreadMsg(obj: Record<string, unknown>): string | null {
|
||||
|
||||
/** Validate and parse an unknown IPC message received from the parent process. */
|
||||
export function parseParentMessage(raw: unknown): Result<ParentToWorkerMessage> {
|
||||
if (raw === null || typeof raw !== "object") {
|
||||
if (!isPlainRecord(raw)) {
|
||||
return err(new Error("IPC message is not an object"));
|
||||
}
|
||||
const obj = raw as Record<string, unknown>;
|
||||
const obj = raw;
|
||||
if (typeof obj.type !== "string") {
|
||||
return err(new Error("IPC message missing string 'type' field"));
|
||||
}
|
||||
if (!PARENT_MSG_TYPES.has(obj.type)) {
|
||||
return err(new Error(`Unknown IPC message type: "${obj.type}"`));
|
||||
}
|
||||
if (obj.type === "compute") {
|
||||
if (typeof obj.sense !== "string") {
|
||||
return err(new Error("IPC 'compute' message missing string 'sense' field"));
|
||||
}
|
||||
return ok({ type: "compute", sense: obj.sense });
|
||||
}
|
||||
if (obj.type === "shutdown") {
|
||||
return ok({ type: "shutdown" });
|
||||
}
|
||||
if (obj.type === "health-request") {
|
||||
return ok({ type: "health-request" });
|
||||
}
|
||||
if (obj.type === "start-thread") {
|
||||
const errMsg = validateStartThreadMsg(obj);
|
||||
if (errMsg !== null) return err(new Error(errMsg));
|
||||
// Field types are validated above; `Record<string, unknown>` values stay `unknown` to TypeScript.
|
||||
return ok({
|
||||
type: "start-thread",
|
||||
runId: obj.runId,
|
||||
workflow: obj.workflow,
|
||||
prompt: obj.prompt,
|
||||
maxRounds: obj.maxRounds,
|
||||
} as StartThreadMessage);
|
||||
}
|
||||
if (obj.type === "resume-thread") {
|
||||
const errMsg = validateResumeThreadMsg(obj);
|
||||
if (errMsg !== null) return err(new Error(errMsg));
|
||||
// Elements are validated as plain objects by the kernel; trust the wire shape here.
|
||||
return ok({
|
||||
type: "resume-thread",
|
||||
runId: obj.runId,
|
||||
messages: obj.messages as ResumeThreadMessage["messages"],
|
||||
maxRounds: obj.maxRounds,
|
||||
} as ResumeThreadMessage);
|
||||
}
|
||||
return ok(raw as ParentToWorkerMessage);
|
||||
return err(new Error(`Unhandled IPC message type: "${obj.type}"`));
|
||||
}
|
||||
|
||||
function parseSignalMsg(obj: Record<string, unknown>, raw: unknown): Result<WorkerToParentMessage> {
|
||||
function parseSignalMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||
if (typeof obj.sense !== "string") {
|
||||
return err(new Error("Worker 'signal' message missing string 'sense' field"));
|
||||
}
|
||||
if (!("payload" in obj)) {
|
||||
return err(new Error("Worker 'signal' message missing 'payload' field"));
|
||||
}
|
||||
return ok(raw as SignalMessage);
|
||||
return ok({
|
||||
type: "signal",
|
||||
sense: obj.sense,
|
||||
payload: obj.payload,
|
||||
});
|
||||
}
|
||||
|
||||
function parseErrorMsg(obj: Record<string, unknown>, raw: unknown): Result<WorkerToParentMessage> {
|
||||
function parseErrorMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||
if (typeof obj.sense !== "string") {
|
||||
return err(new Error("Worker 'error' message missing string 'sense' field"));
|
||||
}
|
||||
if (typeof obj.error !== "string") {
|
||||
return err(new Error("Worker 'error' message missing string 'error' field"));
|
||||
}
|
||||
return ok(raw as ErrorMessage);
|
||||
return ok({
|
||||
type: "error",
|
||||
sense: obj.sense,
|
||||
error: obj.error,
|
||||
});
|
||||
}
|
||||
|
||||
function parseHealthResponseMsg(
|
||||
obj: Record<string, unknown>,
|
||||
raw: unknown,
|
||||
): Result<WorkerToParentMessage> {
|
||||
function parseHealthResponseMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||
if (!Array.isArray(obj.senses)) {
|
||||
return err(new Error("Worker 'health-response' message missing 'senses' array"));
|
||||
}
|
||||
if (typeof obj.inFlightCount !== "number") {
|
||||
return err(new Error("Worker 'health-response' message missing 'inFlightCount' number"));
|
||||
}
|
||||
return ok(raw as HealthResponseMessage);
|
||||
return ok({
|
||||
type: "health-response",
|
||||
// Kernel only sends string[] today; keep accepting any array elements without filtering.
|
||||
senses: obj.senses as string[],
|
||||
inFlightCount: obj.inFlightCount,
|
||||
});
|
||||
}
|
||||
|
||||
const THREAD_EVENT_TYPES = new Set<string>([
|
||||
"queued",
|
||||
"started",
|
||||
"step_complete",
|
||||
"completed",
|
||||
"failed",
|
||||
]);
|
||||
function isThreadEventType(value: string): value is ThreadEventType {
|
||||
switch (value) {
|
||||
case "queued":
|
||||
case "started":
|
||||
case "step_complete":
|
||||
case "completed":
|
||||
case "failed":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function parseThreadEventMsg(
|
||||
obj: Record<string, unknown>,
|
||||
raw: unknown,
|
||||
): Result<WorkerToParentMessage> {
|
||||
function parseThreadEventMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||
if (typeof obj.runId !== "string") {
|
||||
return err(new Error("Worker 'thread-event' message missing string 'runId' field"));
|
||||
}
|
||||
if (typeof obj.eventType !== "string" || !THREAD_EVENT_TYPES.has(obj.eventType)) {
|
||||
if (typeof obj.eventType !== "string" || !isThreadEventType(obj.eventType)) {
|
||||
return err(
|
||||
new Error(`Worker 'thread-event' message has invalid 'eventType': "${obj.eventType}"`),
|
||||
);
|
||||
@@ -225,20 +264,26 @@ function parseThreadEventMsg(
|
||||
if (!("payload" in obj)) {
|
||||
return err(new Error("Worker 'thread-event' message missing 'payload' field"));
|
||||
}
|
||||
return ok(raw as ThreadEventMessage);
|
||||
return ok({
|
||||
type: "thread-event",
|
||||
runId: obj.runId,
|
||||
eventType: obj.eventType,
|
||||
payload: obj.payload,
|
||||
});
|
||||
}
|
||||
|
||||
function parseWorkflowErrorMsg(
|
||||
obj: Record<string, unknown>,
|
||||
raw: unknown,
|
||||
): Result<WorkerToParentMessage> {
|
||||
function parseWorkflowErrorMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||
if (typeof obj.runId !== "string") {
|
||||
return err(new Error("Worker 'workflow-error' message missing string 'runId' field"));
|
||||
}
|
||||
if (typeof obj.error !== "string") {
|
||||
return err(new Error("Worker 'workflow-error' message missing string 'error' field"));
|
||||
}
|
||||
return ok(raw as WorkflowErrorMessage);
|
||||
return ok({
|
||||
type: "workflow-error",
|
||||
runId: obj.runId,
|
||||
error: obj.error,
|
||||
});
|
||||
}
|
||||
|
||||
const WORKER_MSG_TYPES = new Set([
|
||||
@@ -253,15 +298,14 @@ const WORKER_MSG_TYPES = new Set([
|
||||
|
||||
function parseThreadWorkflowMessageMsg(
|
||||
obj: Record<string, unknown>,
|
||||
raw: unknown,
|
||||
): Result<WorkerToParentMessage> {
|
||||
if (typeof obj.runId !== "string") {
|
||||
return err(new Error("Worker 'thread-workflow-message' missing string 'runId' field"));
|
||||
}
|
||||
if (obj.message === null || typeof obj.message !== "object") {
|
||||
if (!isPlainRecord(obj.message)) {
|
||||
return err(new Error("Worker 'thread-workflow-message' missing object 'message' field"));
|
||||
}
|
||||
const msg = obj.message as Record<string, unknown>;
|
||||
const msg = obj.message;
|
||||
if (typeof msg.role !== "string") {
|
||||
return err(new Error("Worker 'thread-workflow-message' message missing string 'role' field"));
|
||||
}
|
||||
@@ -275,26 +319,35 @@ function parseThreadWorkflowMessageMsg(
|
||||
new Error("Worker 'thread-workflow-message' message missing number 'timestamp' field"),
|
||||
);
|
||||
}
|
||||
return ok(raw as ThreadWorkflowMessageMessage);
|
||||
return ok({
|
||||
type: "thread-workflow-message",
|
||||
runId: obj.runId,
|
||||
message: {
|
||||
role: msg.role,
|
||||
content: msg.content,
|
||||
meta: "meta" in msg ? msg.meta : undefined,
|
||||
timestamp: msg.timestamp,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Validate and parse an unknown IPC message received from a worker process. */
|
||||
export function parseWorkerMessage(raw: unknown): Result<WorkerToParentMessage> {
|
||||
if (raw === null || typeof raw !== "object") {
|
||||
if (!isPlainRecord(raw)) {
|
||||
return err(new Error("Worker IPC message is not an object"));
|
||||
}
|
||||
const obj = raw as Record<string, unknown>;
|
||||
const obj = raw;
|
||||
if (typeof obj.type !== "string") {
|
||||
return err(new Error("Worker IPC message missing string 'type' field"));
|
||||
}
|
||||
if (!WORKER_MSG_TYPES.has(obj.type)) {
|
||||
return err(new Error(`Unknown worker IPC message type: "${obj.type}"`));
|
||||
}
|
||||
if (obj.type === "signal") return parseSignalMsg(obj, raw);
|
||||
if (obj.type === "error") return parseErrorMsg(obj, raw);
|
||||
if (obj.type === "health-response") return parseHealthResponseMsg(obj, raw);
|
||||
if (obj.type === "thread-event") return parseThreadEventMsg(obj, raw);
|
||||
if (obj.type === "workflow-error") return parseWorkflowErrorMsg(obj, raw);
|
||||
if (obj.type === "thread-workflow-message") return parseThreadWorkflowMessageMsg(obj, raw);
|
||||
if (obj.type === "signal") return parseSignalMsg(obj);
|
||||
if (obj.type === "error") return parseErrorMsg(obj);
|
||||
if (obj.type === "health-response") return parseHealthResponseMsg(obj);
|
||||
if (obj.type === "thread-event") return parseThreadEventMsg(obj);
|
||||
if (obj.type === "workflow-error") return parseWorkflowErrorMsg(obj);
|
||||
if (obj.type === "thread-workflow-message") return parseThreadWorkflowMessageMsg(obj);
|
||||
return ok({ type: "ready" });
|
||||
}
|
||||
|
||||
@@ -100,7 +100,8 @@ export function createKernel(
|
||||
}
|
||||
|
||||
let stopped = false;
|
||||
let scheduler: ReflexScheduler = null as unknown as ReflexScheduler;
|
||||
/** Assigned before workers start; `handleWorkerMessage` only runs after this is set. */
|
||||
let scheduler!: ReflexScheduler;
|
||||
|
||||
let readyResolve: (() => void) | undefined;
|
||||
const ready = new Promise<void>((resolve) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { drizzle } from "drizzle-orm/node-sqlite";
|
||||
import type { NodeSQLiteDatabase } from "drizzle-orm/node-sqlite";
|
||||
|
||||
import type { Result } from "@uncaged/nerve-core";
|
||||
import { err, ok } from "@uncaged/nerve-core";
|
||||
import { err, isPlainRecord, ok } from "@uncaged/nerve-core";
|
||||
|
||||
import type { BlobStore } from "@uncaged/nerve-store";
|
||||
|
||||
@@ -108,10 +108,11 @@ export function runMigrations(sqlite: DatabaseSync, migrationsDir: string): Resu
|
||||
const filesResult = listMigrationFiles(migrationsDir);
|
||||
if (!filesResult.ok) return filesResult;
|
||||
|
||||
const migrationRows = sqlite.prepare("SELECT name FROM _migrations").all();
|
||||
const applied = new Set<string>(
|
||||
(sqlite.prepare("SELECT name FROM _migrations").all() as Array<{ name: string }>).map(
|
||||
(r) => r.name,
|
||||
),
|
||||
migrationRows
|
||||
.filter((r): r is { name: string } => isPlainRecord(r) && typeof r.name === "string")
|
||||
.map((r) => r.name),
|
||||
);
|
||||
|
||||
for (const file of filesResult.value) {
|
||||
@@ -145,6 +146,7 @@ export function openSenseDb(
|
||||
const migResult = runMigrations(sqlite, migrationsDir);
|
||||
if (!migResult.ok) return migResult;
|
||||
|
||||
// Drizzle infers a schema-specific DB type; senses are schema-agnostic at this layer.
|
||||
const db = drizzle({ client: sqlite }) as DrizzleDB;
|
||||
return ok({ sqlite, db });
|
||||
}
|
||||
@@ -162,6 +164,7 @@ export function openPeerDb(dbPath: string): Result<DrizzleDB> {
|
||||
return err(new Error(`Failed to open peer database "${dbPath}" (readonly): ${msg}`));
|
||||
}
|
||||
|
||||
// Same schema-agnostic Drizzle wrapper as openSenseDb.
|
||||
return ok(drizzle({ client: sqlite }) as DrizzleDB);
|
||||
}
|
||||
|
||||
@@ -180,18 +183,13 @@ export async function loadComputeFn(senseIndexPath: string): Promise<Result<Comp
|
||||
return err(new Error(`Failed to import sense module "${senseIndexPath}": ${msg}`));
|
||||
}
|
||||
|
||||
if (
|
||||
mod === null ||
|
||||
typeof mod !== "object" ||
|
||||
!("compute" in mod) ||
|
||||
typeof (mod as Record<string, unknown>).compute !== "function"
|
||||
) {
|
||||
if (!isPlainRecord(mod) || !("compute" in mod) || typeof mod.compute !== "function") {
|
||||
return err(
|
||||
new Error(`Sense module "${senseIndexPath}" must export a named "compute" function`),
|
||||
);
|
||||
}
|
||||
|
||||
return ok((mod as { compute: ComputeFn }).compute);
|
||||
return ok(mod.compute as ComputeFn);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,7 +230,9 @@ export async function executeCompute(
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
if (controller.signal.aborted) {
|
||||
return err(new Error(`compute("${runtime.name}") timed out after ${timeoutMs as number}ms`));
|
||||
return err(
|
||||
new Error(`compute("${runtime.name}") timed out after ${String(timeoutMs ?? "?")}ms`),
|
||||
);
|
||||
}
|
||||
return err(new Error(`compute("${runtime.name}") threw: ${msg}`));
|
||||
} finally {
|
||||
|
||||
@@ -12,8 +12,9 @@ import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import type { NerveConfig, WorkflowConfig, WorkflowMessage } from "@uncaged/nerve-core";
|
||||
import { START } from "@uncaged/nerve-core";
|
||||
import { START, isPlainRecord } from "@uncaged/nerve-core";
|
||||
|
||||
import type { LogStore, WorkflowRunStatus } from "@uncaged/nerve-store";
|
||||
import type {
|
||||
ResumeThreadMessage,
|
||||
ShutdownMessage,
|
||||
@@ -21,7 +22,6 @@ import type {
|
||||
ThreadEventMessage,
|
||||
} from "./ipc.js";
|
||||
import { parseWorkerMessage } from "./ipc.js";
|
||||
import type { LogStore, WorkflowRunStatus } from "@uncaged/nerve-store";
|
||||
import {
|
||||
formatCapturedStderrTail,
|
||||
formatChildExitSummary,
|
||||
@@ -91,8 +91,8 @@ function readLaunchFromTriggerPayload(
|
||||
raw: unknown,
|
||||
engineDefaultMaxRounds: number,
|
||||
): { prompt: string; maxRounds: number } {
|
||||
if (raw !== null && typeof raw === "object" && !Array.isArray(raw)) {
|
||||
const o = raw as Record<string, unknown>;
|
||||
if (isPlainRecord(raw)) {
|
||||
const o = raw;
|
||||
if (typeof o.prompt === "string" && typeof o.maxRounds === "number") {
|
||||
return { prompt: o.prompt, maxRounds: o.maxRounds };
|
||||
}
|
||||
@@ -307,7 +307,10 @@ export function createWorkflowManager(
|
||||
|
||||
function recoverQueuedRun(workflowName: string, runId: string, state: WorkflowState): void {
|
||||
if (state.queue.some((q) => q.runId === runId)) return;
|
||||
const launch = readLaunchFromTriggerPayload(logStore.getTriggerPayload(runId), config.maxRounds);
|
||||
const launch = readLaunchFromTriggerPayload(
|
||||
logStore.getTriggerPayload(runId),
|
||||
config.maxRounds,
|
||||
);
|
||||
state.queue.push({ runId, prompt: launch.prompt, maxRounds: launch.maxRounds });
|
||||
process.stderr.write(
|
||||
`[workflow-manager] crash-recovery: re-queued thread "${runId}" for "${workflowName}"\n`,
|
||||
@@ -322,7 +325,10 @@ export function createWorkflowManager(
|
||||
): void {
|
||||
if (state.active.has(runId)) return;
|
||||
const rawMessages = logStore.getThreadMessages(runId);
|
||||
const launch = readLaunchFromTriggerPayload(logStore.getTriggerPayload(runId), config.maxRounds);
|
||||
const launch = readLaunchFromTriggerPayload(
|
||||
logStore.getTriggerPayload(runId),
|
||||
config.maxRounds,
|
||||
);
|
||||
const messages = ensureThreadMessagesWithStart(rawMessages, launch.prompt, launch.maxRounds);
|
||||
state.active.add(runId);
|
||||
const msg: ResumeThreadMessage = {
|
||||
|
||||
@@ -12,8 +12,14 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
import type { RoleMeta, WorkflowDefinition, WorkflowMessage } from "@uncaged/nerve-core";
|
||||
import { END, START } from "@uncaged/nerve-core";
|
||||
import type {
|
||||
Moderator,
|
||||
RoleMeta,
|
||||
StartSignal,
|
||||
WorkflowDefinition,
|
||||
WorkflowMessage,
|
||||
} from "@uncaged/nerve-core";
|
||||
import { END, START, isPlainRecord } from "@uncaged/nerve-core";
|
||||
|
||||
import type {
|
||||
ThreadEventType,
|
||||
@@ -23,6 +29,8 @@ import type {
|
||||
import { parseParentMessage } from "./ipc.js";
|
||||
import { ignoreSessionBroadcastSignals } from "./worker-fork-support.js";
|
||||
|
||||
type ModeratorInput = Parameters<Moderator<RoleMeta>>[0];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// IPC helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -63,6 +71,79 @@ function sendWorkflowMessage(runId: string, message: WorkflowMessage): void {
|
||||
// Thread loop (signal-driven automaton, issue #80)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function validateRoleResult(
|
||||
result: { content: string; meta: Record<string, unknown> },
|
||||
roleName: string,
|
||||
runId: string,
|
||||
): boolean {
|
||||
if (typeof result.content !== "string") {
|
||||
sendWorkflowError(runId, `Role "${roleName}" returned non-string content`);
|
||||
return false;
|
||||
}
|
||||
if (result.meta === null || typeof result.meta !== "object" || Array.isArray(result.meta)) {
|
||||
sendWorkflowError(runId, `Role "${roleName}" returned invalid meta (must be a plain object)`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function buildInitialLastSignal(lastMsg: WorkflowMessage): ModeratorInput {
|
||||
if (lastMsg.role === START) {
|
||||
return {
|
||||
role: START,
|
||||
content: lastMsg.content,
|
||||
meta: lastMsg.meta as StartSignal["meta"],
|
||||
timestamp: lastMsg.timestamp,
|
||||
};
|
||||
}
|
||||
return { role: lastMsg.role, meta: lastMsg.meta as Record<string, unknown> };
|
||||
}
|
||||
|
||||
function initChain(
|
||||
runId: string,
|
||||
resumeMessages: WorkflowMessage[],
|
||||
freshPrompt: string | null,
|
||||
maxRounds: number,
|
||||
): WorkflowMessage[] {
|
||||
if (resumeMessages.length > 0) {
|
||||
return [...resumeMessages];
|
||||
}
|
||||
const prompt = freshPrompt ?? "";
|
||||
const startMsg: WorkflowMessage = {
|
||||
role: START,
|
||||
content: prompt,
|
||||
meta: { maxRounds },
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
sendWorkflowMessage(runId, startMsg);
|
||||
return [startMsg];
|
||||
}
|
||||
|
||||
async function executeRole(
|
||||
def: WorkflowDefinition<RoleMeta>,
|
||||
nextRole: string,
|
||||
chain: WorkflowMessage[],
|
||||
runId: string,
|
||||
): Promise<{ content: string; meta: Record<string, unknown> } | null> {
|
||||
const role = def.roles[nextRole];
|
||||
if (!role) {
|
||||
sendWorkflowError(runId, `Unknown role: ${nextRole}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
let result: { content: string; meta: Record<string, unknown> };
|
||||
try {
|
||||
result = await role(chain);
|
||||
} catch (e: unknown) {
|
||||
const errMsg = e instanceof Error ? e.message : String(e);
|
||||
sendThreadEvent(runId, "failed", { error: errMsg });
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!validateRoleResult(result, nextRole, runId)) return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
async function runThread(
|
||||
def: WorkflowDefinition<RoleMeta>,
|
||||
runId: string,
|
||||
@@ -70,21 +151,7 @@ async function runThread(
|
||||
resumeMessages: WorkflowMessage[] = [],
|
||||
freshPrompt: string | null = null,
|
||||
): Promise<void> {
|
||||
let chain: WorkflowMessage[];
|
||||
|
||||
if (resumeMessages.length > 0) {
|
||||
chain = [...resumeMessages];
|
||||
} else {
|
||||
const prompt = freshPrompt ?? "";
|
||||
const startMsg: WorkflowMessage = {
|
||||
role: START,
|
||||
content: prompt,
|
||||
meta: { maxRounds },
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
chain = [startMsg];
|
||||
sendWorkflowMessage(runId, startMsg);
|
||||
}
|
||||
const chain = initChain(runId, resumeMessages, freshPrompt, maxRounds);
|
||||
|
||||
let roleRound = chain.filter((m) => m.role !== START).length;
|
||||
const lastMsg = chain[chain.length - 1];
|
||||
@@ -93,21 +160,7 @@ async function runThread(
|
||||
return;
|
||||
}
|
||||
|
||||
const lastSignal =
|
||||
lastMsg.role === START
|
||||
? {
|
||||
role: START,
|
||||
content: lastMsg.content,
|
||||
meta: lastMsg.meta as { maxRounds: number },
|
||||
timestamp: lastMsg.timestamp,
|
||||
}
|
||||
: { role: lastMsg.role, meta: lastMsg.meta as Record<string, unknown> };
|
||||
|
||||
let nextRole = def.moderator(
|
||||
lastSignal as Parameters<typeof def.moderator>[0],
|
||||
roleRound,
|
||||
maxRounds,
|
||||
);
|
||||
let nextRole = def.moderator(buildInitialLastSignal(lastMsg), roleRound, maxRounds);
|
||||
|
||||
if (nextRole === END) {
|
||||
sendThreadEvent(runId, "completed", null);
|
||||
@@ -115,29 +168,8 @@ async function runThread(
|
||||
}
|
||||
|
||||
while (roleRound < maxRounds) {
|
||||
const role = def.roles[nextRole];
|
||||
if (!role) {
|
||||
sendWorkflowError(runId, `Unknown role: ${nextRole}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let result: { content: string; meta: Record<string, unknown> };
|
||||
try {
|
||||
result = await role(chain);
|
||||
} catch (e: unknown) {
|
||||
const errMsg = e instanceof Error ? e.message : String(e);
|
||||
sendThreadEvent(runId, "failed", { error: errMsg });
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof result.content !== "string") {
|
||||
sendWorkflowError(runId, `Role "${nextRole}" returned non-string content`);
|
||||
return;
|
||||
}
|
||||
if (result.meta === null || typeof result.meta !== "object" || Array.isArray(result.meta)) {
|
||||
sendWorkflowError(runId, `Role "${nextRole}" returned invalid meta (must be a plain object)`);
|
||||
return;
|
||||
}
|
||||
const result = await executeRole(def, nextRole, chain, runId);
|
||||
if (result === null) return;
|
||||
|
||||
const message: WorkflowMessage = {
|
||||
role: nextRole,
|
||||
@@ -150,8 +182,8 @@ async function runThread(
|
||||
|
||||
roleRound += 1;
|
||||
|
||||
const signal = { role: nextRole, meta: result.meta };
|
||||
nextRole = def.moderator(signal as Parameters<typeof def.moderator>[0], roleRound, maxRounds);
|
||||
const signal: ModeratorInput = { role: nextRole, meta: result.meta };
|
||||
nextRole = def.moderator(signal, roleRound, maxRounds);
|
||||
|
||||
if (nextRole === END) {
|
||||
sendThreadEvent(runId, "completed", null);
|
||||
@@ -166,6 +198,17 @@ async function runThread(
|
||||
// Workflow definition loader
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function isWorkflowDefinitionShape(def: unknown): def is WorkflowDefinition<RoleMeta> {
|
||||
if (!isPlainRecord(def)) return false;
|
||||
return (
|
||||
typeof def.moderator === "function" &&
|
||||
typeof def.roles === "object" &&
|
||||
def.roles !== null &&
|
||||
!Array.isArray(def.roles) &&
|
||||
typeof def.name === "string"
|
||||
);
|
||||
}
|
||||
|
||||
async function loadWorkflowDefinition(
|
||||
nerveRoot: string,
|
||||
workflowName: string,
|
||||
@@ -186,19 +229,13 @@ async function loadWorkflowDefinition(
|
||||
const mod = await import(indexPath);
|
||||
const def: unknown = mod.default ?? mod;
|
||||
|
||||
if (
|
||||
def === null ||
|
||||
typeof def !== "object" ||
|
||||
typeof (def as WorkflowDefinition<RoleMeta>).moderator !== "function" ||
|
||||
typeof (def as WorkflowDefinition<RoleMeta>).roles !== "object" ||
|
||||
typeof (def as WorkflowDefinition<RoleMeta>).name !== "string"
|
||||
) {
|
||||
if (!isWorkflowDefinitionShape(def)) {
|
||||
throw new Error(
|
||||
`Workflow "${workflowName}" must export a WorkflowDefinition with "name", "roles", and "moderator".`,
|
||||
);
|
||||
}
|
||||
|
||||
return def as WorkflowDefinition<RoleMeta>;
|
||||
return def;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -253,7 +290,7 @@ function handleMessage(
|
||||
|
||||
const previous = inFlight.get(runId) ?? Promise.resolve();
|
||||
const next = previous
|
||||
.then(() => runThread(def, runId, maxRounds, messages as WorkflowMessage[], null))
|
||||
.then(() => runThread(def, runId, maxRounds, messages, null))
|
||||
.catch((e: unknown) => {
|
||||
const errMsg = e instanceof Error ? e.message : String(e);
|
||||
sendWorkflowError(runId, errMsg);
|
||||
|
||||
@@ -11,6 +11,8 @@ import { mkdirSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { DatabaseSync, type StatementSync } from "node:sqlite";
|
||||
|
||||
import { isPlainRecord } from "@uncaged/nerve-core";
|
||||
|
||||
import {
|
||||
DEFAULT_LOG_RETENTION_MS,
|
||||
LOG_ARCHIVE_META_KEY,
|
||||
@@ -68,11 +70,15 @@ const VALID_WORKFLOW_STATUSES = new Set<string>([
|
||||
"interrupted",
|
||||
]);
|
||||
|
||||
function isWorkflowRunStatus(value: string): value is WorkflowRunStatus {
|
||||
return VALID_WORKFLOW_STATUSES.has(value);
|
||||
}
|
||||
|
||||
function validateWorkflowRunStatus(status: string): WorkflowRunStatus {
|
||||
if (!VALID_WORKFLOW_STATUSES.has(status)) {
|
||||
if (!isWorkflowRunStatus(status)) {
|
||||
throw new Error(`Invalid workflow run status from DB: "${status}"`);
|
||||
}
|
||||
return status as WorkflowRunStatus;
|
||||
return status;
|
||||
}
|
||||
|
||||
/** One row in the workflow_runs materialized table. */
|
||||
@@ -508,10 +514,9 @@ export function createLogStore(dbPath: string): LogStore {
|
||||
const row = getTriggerPayloadStmt.get(runId) as { payload: string | null } | undefined;
|
||||
if (row === undefined || row.payload === null) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(row.payload) as unknown;
|
||||
if (parsed !== null && typeof parsed === "object") {
|
||||
const obj = parsed as Record<string, unknown>;
|
||||
return obj.triggerPayload ?? null;
|
||||
const parsed: unknown = JSON.parse(row.payload);
|
||||
if (isPlainRecord(parsed)) {
|
||||
return parsed.triggerPayload ?? null;
|
||||
}
|
||||
} catch {
|
||||
// malformed
|
||||
@@ -525,12 +530,8 @@ export function createLogStore(dbPath: string): LogStore {
|
||||
for (const row of rows) {
|
||||
if (row.payload === null) continue;
|
||||
try {
|
||||
const parsed = JSON.parse(row.payload) as unknown;
|
||||
if (
|
||||
parsed !== null &&
|
||||
typeof parsed === "object" &&
|
||||
typeof (parsed as Record<string, unknown>).type === "string"
|
||||
) {
|
||||
const parsed: unknown = JSON.parse(row.payload);
|
||||
if (isPlainRecord(parsed) && typeof parsed.type === "string") {
|
||||
result.push(parsed as { type: string; [key: string]: unknown });
|
||||
}
|
||||
} catch {
|
||||
@@ -544,9 +545,9 @@ export function createLogStore(dbPath: string): LogStore {
|
||||
payload: string,
|
||||
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
||||
try {
|
||||
const parsed = JSON.parse(payload) as unknown;
|
||||
if (parsed === null || typeof parsed !== "object") return null;
|
||||
const obj = parsed as Record<string, unknown>;
|
||||
const parsed: unknown = JSON.parse(payload);
|
||||
if (!isPlainRecord(parsed)) return null;
|
||||
const obj = parsed;
|
||||
if (typeof obj.role !== "string" || typeof obj.content !== "string") return null;
|
||||
return {
|
||||
role: obj.role,
|
||||
@@ -579,31 +580,37 @@ export function createLogStore(dbPath: string): LogStore {
|
||||
return Number(c);
|
||||
}
|
||||
|
||||
function recordToRoundMessage(
|
||||
obj: Record<string, unknown>,
|
||||
fallbackTs: number,
|
||||
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
||||
if (typeof obj.role === "string" && typeof obj.content === "string") {
|
||||
return {
|
||||
role: obj.role,
|
||||
content: obj.content,
|
||||
meta: obj.meta,
|
||||
timestamp: typeof obj.timestamp === "number" ? obj.timestamp : 0,
|
||||
};
|
||||
}
|
||||
if (typeof obj.type === "string") {
|
||||
return {
|
||||
role: typeof obj.role === "string" ? obj.role : obj.type,
|
||||
content: typeof obj.content === "string" ? obj.content : JSON.stringify(obj),
|
||||
meta: obj,
|
||||
timestamp: fallbackTs,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseRoundPayload(
|
||||
payload: string,
|
||||
fallbackTs: number,
|
||||
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
||||
try {
|
||||
const parsed = JSON.parse(payload) as unknown;
|
||||
if (parsed === null || typeof parsed !== "object") return null;
|
||||
const obj = parsed as Record<string, unknown>;
|
||||
if (typeof obj.role === "string" && typeof obj.content === "string") {
|
||||
return {
|
||||
role: obj.role,
|
||||
content: obj.content,
|
||||
meta: obj.meta,
|
||||
timestamp: typeof obj.timestamp === "number" ? obj.timestamp : 0,
|
||||
};
|
||||
}
|
||||
if (typeof obj.type === "string") {
|
||||
return {
|
||||
role: typeof obj.role === "string" ? obj.role : obj.type,
|
||||
content: typeof obj.content === "string" ? obj.content : JSON.stringify(obj),
|
||||
meta: obj,
|
||||
timestamp: fallbackTs,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
const parsed: unknown = JSON.parse(payload);
|
||||
if (!isPlainRecord(parsed)) return null;
|
||||
return recordToRoundMessage(parsed, fallbackTs);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
Generated
+10
@@ -14,6 +14,9 @@ importers:
|
||||
'@rslib/core':
|
||||
specifier: ^0.21.3
|
||||
version: 0.21.3(typescript@5.9.3)
|
||||
husky:
|
||||
specifier: ^9.1.7
|
||||
version: 9.1.7
|
||||
typescript:
|
||||
specifier: ^5.5.0
|
||||
version: 5.9.3
|
||||
@@ -1010,6 +1013,11 @@ packages:
|
||||
resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==}
|
||||
engines: {node: '>= 14'}
|
||||
|
||||
husky@9.1.7:
|
||||
resolution: {integrity: sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==}
|
||||
engines: {node: '>=18'}
|
||||
hasBin: true
|
||||
|
||||
iconv-lite@0.6.3:
|
||||
resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
@@ -2258,6 +2266,8 @@ snapshots:
|
||||
- supports-color
|
||||
optional: true
|
||||
|
||||
husky@9.1.7: {}
|
||||
|
||||
iconv-lite@0.6.3:
|
||||
dependencies:
|
||||
safer-buffer: 2.1.2
|
||||
|
||||
Reference in New Issue
Block a user