Compare commits

...

11 Commits

Author SHA1 Message Date
xiaoju 3082568b85 refactor(daemon): exhaustive IPC request dispatch
Ensure new DaemonIpcRequest variants require an explicit handler branch.

Made-with: Cursor
2026-04-24 15:11:58 +00:00
xiaoju 830b0aa762 refactor(core): shared daemon IPC request/response types
Move wire protocol types and parseDaemonIpcRequest into @uncaged/nerve-core so CLI and daemon share one definition. Type sendAndReceive message as DaemonIpcRequest. Align workflow trigger CLI with daemon (prompt, maxRounds from --payload JSON).

Made-with: Cursor
2026-04-24 15:10:00 +00:00
xiaoju 777d51cc73 chore: bump version to 0.4.0
小橘 🍊(NEKO Team)
2026-04-24 13:22:30 +00:00
xiaomo 06a957d62a Merge pull request 'chore: add pre-push hook to run tests before push' (#92) from chore/add-pre-push-hook into main 2026-04-24 13:19:10 +00:00
xiaoju b2c379cbfd refactor: reduce cognitive complexity in 3 functions
Extract helpers to bring all functions below biome's complexity threshold (15):
- store/log-store.ts: extract recordToRoundMessage() from parseRoundPayload()
- cli/commands/workflow.ts: extract buildTruncatedSingleRound() from buildThreadCommandOutput()
- daemon/workflow-worker.ts: extract validateRoleResult(), buildInitialLastSignal(),
  initChain(), executeRole() from runThread()

小橘 🍊(NEKO Team)
2026-04-24 12:44:39 +00:00
xiaoju 7cb7112ed6 chore: fix biome lint errors and tune overrides
- Remove duplicate 'prepare' key in package.json
- Allow default exports in rslib.config.ts
- Relax noExplicitAny and noNonNullAssertion in test files
- Auto-fix 17 files (imports, formatting)

小橘 🍊(NEKO Team)
2026-04-24 12:36:57 +00:00
xiaoju 48c81c2e19 chore: add biome lint check to pre-push hook
小橘 🍊(NEKO Team)
2026-04-24 12:32:41 +00:00
xiaoju dd3d4315c4 chore: add pre-push hook to run tests before push
Adds husky with a pre-push hook that runs `pnpm -r test` to catch
test failures before they reach the remote.

小橘 🍊(NEKO Team)
2026-04-24 12:28:47 +00:00
xingyue 788ebc6779 Merge pull request 'fix(test): align tests with type-safety refactor' (#91) from fix/test-failures-after-type-safety-refactor into main 2026-04-24 12:24:50 +00:00
xiaoju 8807b0ac6a fix(test): align tests with type-safety refactor
Update test expectations after workflow reflexes were removed from
YAML config and type signatures were tightened:

- core/config: workflow reflex tests now expect 'not supported' error
- cli/workflow: partitionWorkflowMessage test uses strict typed params
- daemon/crash-recovery: remove triggerPayload from resume-thread assertion
- daemon/daemon-ipc: trigger-workflow sends prompt+maxRounds
- daemon/kernel-workflow: use Sense-driven workflow trigger pattern

Fixes 12 test failures across core, cli, and daemon packages.

Refs #88, #89
2026-04-24 12:23:21 +00:00
xiaomo 5b65afdc4b Merge pull request 'refactor: improve type safety across codebase' (#90) from refactor/type-safety into main 2026-04-24 12:09:36 +00:00
33 changed files with 533 additions and 282 deletions
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
pnpm check
pnpm -r test
+14 -1
View File
@@ -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": {
+2
View File
@@ -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"
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
"engines": {
"node": ">=22.5.0"
},
"version": "0.3.0",
"version": "0.4.0",
"type": "module",
"bin": {
"nerve": "dist/cli.js"
+2 -2
View File
@@ -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,
+12 -7
View File
@@ -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}');
});
});
@@ -509,7 +514,7 @@ describe("triggerWorkflowViaDaemon", () => {
await new Promise<void>((r) => server.listen(sockPath, r));
try {
const result = await triggerWorkflowViaDaemon(sockPath, "my-workflow", {});
const result = await triggerWorkflowViaDaemon(sockPath, "my-workflow", "", 100);
expect(result).toEqual({ ok: true });
} finally {
await new Promise<void>((r) => server.close(() => r()));
@@ -525,7 +530,7 @@ describe("triggerWorkflowViaDaemon", () => {
await new Promise<void>((r) => server.listen(sockPath, r));
try {
const result = await triggerWorkflowViaDaemon(sockPath, "missing", {});
const result = await triggerWorkflowViaDaemon(sockPath, "missing", "", 100);
expect(result).toEqual({ ok: false, error: "unknown workflow" });
} finally {
await new Promise<void>((r) => server.close(() => r()));
@@ -533,7 +538,7 @@ describe("triggerWorkflowViaDaemon", () => {
});
it("rejects when no daemon is listening on the socket", async () => {
await expect(triggerWorkflowViaDaemon(sockPath, "my-workflow", {})).rejects.toThrow(
await expect(triggerWorkflowViaDaemon(sockPath, "my-workflow", "", 100)).rejects.toThrow(
/Cannot connect to daemon/,
);
});
+1 -1
View File
@@ -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 -2
View File
@@ -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, isPlainRecord, parseNerveConfig } from "@uncaged/nerve-core";
import { defineCommand } from "citty";
import { listSensesViaDaemon, triggerSenseViaDaemon } from "../daemon-client.js";
import {
assertSenseDbExists,
defaultPreviewSql,
formatRowsAsAlignedTable,
listTableSqlStatements,
+47 -39
View File
@@ -1,12 +1,13 @@
import { existsSync } from "node:fs";
import { join } from "node:path";
import { isPlainRecord } from "@uncaged/nerve-core";
import type { DaemonIpcTriggerResponse } from "@uncaged/nerve-core";
import { DEFAULT_ENGINE_MAX_ROUNDS, 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";
@@ -218,13 +219,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 = {
@@ -232,6 +227,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.
@@ -257,25 +279,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;
}
@@ -284,9 +288,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 };
@@ -455,10 +457,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;
}
@@ -469,7 +468,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`,
@@ -515,7 +514,8 @@ const workflowTriggerCommand = defineCommand({
},
payload: {
type: "string",
description: "JSON payload to pass as trigger payload (default: {})",
description:
'JSON with optional "prompt" (string) and "maxRounds" (number) for the workflow run (default: {})',
default: "{}",
},
},
@@ -528,15 +528,23 @@ const workflowTriggerCommand = defineCommand({
process.exit(1);
}
let prompt = "";
let maxRounds = DEFAULT_ENGINE_MAX_ROUNDS;
if (isPlainRecord(triggerPayload)) {
const p = triggerPayload;
if (typeof p.prompt === "string") prompt = p.prompt;
if (typeof p.maxRounds === "number") maxRounds = p.maxRounds;
}
if (!isRunning()) {
process.stderr.write("❌ Nerve daemon is not running. Start it with `nerve start`.\n");
process.exit(1);
}
const socketPath = getSocketPath();
let response: { ok: true } | { ok: false; error: string };
let response: DaemonIpcTriggerResponse;
try {
response = await triggerWorkflowViaDaemon(socketPath, args.name, triggerPayload);
response = await triggerWorkflowViaDaemon(socketPath, args.name, prompt, maxRounds);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
process.stderr.write(`❌ Failed to contact daemon: ${msg}\n`);
+28 -19
View File
@@ -8,7 +8,12 @@
import { connect } from "node:net";
import type { Socket } from "node:net";
import type { SenseInfo } from "@uncaged/nerve-core";
import type {
DaemonIpcListSensesResponse,
DaemonIpcRequest,
DaemonIpcTriggerResponse,
SenseInfo,
} from "@uncaged/nerve-core";
import { isPlainRecord } from "@uncaged/nerve-core";
const CONNECT_TIMEOUT_MS = 3_000;
@@ -16,10 +21,6 @@ const RESPONSE_TIMEOUT_MS = 5_000;
export type { SenseInfo };
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 (
@@ -31,7 +32,7 @@ function isSenseInfo(value: unknown): value is SenseInfo {
);
}
function parseDaemonResponse(line: string): TriggerResponse {
function parseDaemonResponse(line: string): DaemonIpcTriggerResponse {
try {
const obj: unknown = JSON.parse(line);
if (isPlainRecord(obj)) {
@@ -45,7 +46,7 @@ function parseDaemonResponse(line: string): TriggerResponse {
return { ok: false, error: `Unexpected daemon response: ${line}` };
}
function parseListSensesResponse(line: string): ListSensesResponse {
function parseListSensesResponse(line: string): DaemonIpcListSensesResponse {
try {
const obj: unknown = JSON.parse(line);
if (isPlainRecord(obj)) {
@@ -67,7 +68,7 @@ function parseListSensesResponse(line: string): ListSensesResponse {
*/
function sendAndReceive<T>(
socketPath: string,
message: object,
message: DaemonIpcRequest,
parseFirstLine: (trimmed: string) => T,
responseTimeoutMs: number = RESPONSE_TIMEOUT_MS,
): Promise<T> {
@@ -132,27 +133,35 @@ function sendAndReceive<T>(
export function triggerWorkflowViaDaemon(
socketPath: string,
workflow: string,
payload: unknown,
): Promise<TriggerResponse> {
return sendAndReceive(
socketPath,
{ type: "trigger-workflow", workflow, payload },
parseDaemonResponse,
);
prompt: string,
maxRounds: number,
): Promise<DaemonIpcTriggerResponse> {
const message: DaemonIpcRequest = {
type: "trigger-workflow",
workflow,
prompt,
maxRounds,
};
return sendAndReceive(socketPath, message, parseDaemonResponse);
}
/**
* Send a trigger-sense message to the running daemon via its Unix socket.
* Resolves with the daemon's response or rejects on connection/timeout errors.
*/
export function triggerSenseViaDaemon(socketPath: string, sense: string): Promise<TriggerResponse> {
return sendAndReceive(socketPath, { type: "trigger-sense", sense }, parseDaemonResponse);
export function triggerSenseViaDaemon(
socketPath: string,
sense: string,
): Promise<DaemonIpcTriggerResponse> {
const message: DaemonIpcRequest = { type: "trigger-sense", sense };
return sendAndReceive(socketPath, message, parseDaemonResponse);
}
/**
* Send a list-senses message to the running daemon via its Unix socket.
* Resolves with the list of registered senses or rejects on connection/timeout errors.
*/
export function listSensesViaDaemon(socketPath: string): Promise<ListSensesResponse> {
return sendAndReceive(socketPath, { type: "list-senses" }, parseListSensesResponse);
export function listSensesViaDaemon(socketPath: string): Promise<DaemonIpcListSensesResponse> {
const message: DaemonIpcRequest = { type: "list-senses" };
return sendAndReceive(socketPath, message, parseListSensesResponse);
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@uncaged/nerve-core",
"version": "0.3.0",
"version": "0.4.0",
"type": "module",
"main": "dist/index.js",
"files": ["dist"],
+6 -6
View File
@@ -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"/);
});
});
});
@@ -0,0 +1,51 @@
import { describe, expect, it } from "vitest";
import { parseDaemonIpcRequest } from "../daemon-ipc-protocol.js";
describe("parseDaemonIpcRequest", () => {
it("parses trigger-workflow", () => {
expect(
parseDaemonIpcRequest(
JSON.stringify({
type: "trigger-workflow",
workflow: "wf",
prompt: "go",
maxRounds: 3,
}),
),
).toEqual({
type: "trigger-workflow",
workflow: "wf",
prompt: "go",
maxRounds: 3,
});
});
it("rejects trigger-workflow with empty workflow", () => {
expect(
parseDaemonIpcRequest(
JSON.stringify({
type: "trigger-workflow",
workflow: "",
prompt: "",
maxRounds: 1,
}),
),
).toBeNull();
});
it("parses trigger-sense and list-senses", () => {
expect(parseDaemonIpcRequest(JSON.stringify({ type: "trigger-sense", sense: "x" }))).toEqual({
type: "trigger-sense",
sense: "x",
});
expect(parseDaemonIpcRequest(JSON.stringify({ type: "list-senses" }))).toEqual({
type: "list-senses",
});
});
it("returns null for invalid JSON or unknown type", () => {
expect(parseDaemonIpcRequest("not json")).toBeNull();
expect(parseDaemonIpcRequest(JSON.stringify({ type: "nope" }))).toBeNull();
});
});
+85
View File
@@ -0,0 +1,85 @@
/**
* Daemon Unix-socket IPC protocol (CLI → daemon).
* Newline-delimited JSON: one request object per line from the client,
* one response object per line from the daemon.
*/
import { isPlainRecord } from "./is-plain-record.js";
import type { SenseInfo } from "./types.js";
/** Client → daemon: start a workflow run. */
export type DaemonIpcTriggerWorkflowRequest = {
type: "trigger-workflow";
workflow: string;
prompt: string;
maxRounds: number;
};
/** Client → daemon: run a sense compute on demand. */
export type DaemonIpcTriggerSenseRequest = {
type: "trigger-sense";
sense: string;
};
/** Client → daemon: list registered senses. */
export type DaemonIpcListSensesRequest = {
type: "list-senses";
};
/** Union of all JSON requests the daemon IPC server accepts. */
export type DaemonIpcRequest =
| DaemonIpcTriggerWorkflowRequest
| DaemonIpcTriggerSenseRequest
| DaemonIpcListSensesRequest;
/** Successful trigger / trigger-sense reply (no body). */
export type DaemonIpcTriggerOkResponse = { ok: true };
export type DaemonIpcErrorResponse = { ok: false; error: string };
/** Replies for trigger-workflow and trigger-sense. */
export type DaemonIpcTriggerResponse = DaemonIpcTriggerOkResponse | DaemonIpcErrorResponse;
/** Reply for list-senses. */
export type DaemonIpcListSensesResponse =
| { ok: true; senses: SenseInfo[] }
| DaemonIpcErrorResponse;
/** Any JSON response the daemon may write on the IPC socket. */
export type DaemonIpcResponse =
| DaemonIpcTriggerOkResponse
| DaemonIpcErrorResponse
| { ok: true; senses: SenseInfo[] };
/**
* Parse a single line of JSON into a {@link DaemonIpcRequest}, or null if invalid.
* Kept in core with the request types so CLI and daemon stay aligned at compile time.
*/
export function parseDaemonIpcRequest(line: string): DaemonIpcRequest | null {
try {
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,
};
}
if (req.type === "trigger-sense") {
if (typeof req.sense !== "string" || req.sense.length === 0) return null;
return { type: "trigger-sense", sense: req.sense };
}
if (req.type === "list-senses") {
return { type: "list-senses" };
}
return null;
} catch {
return null;
}
}
+21 -2
View File
@@ -24,5 +24,24 @@ 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";
export type {
DaemonIpcTriggerWorkflowRequest,
DaemonIpcTriggerSenseRequest,
DaemonIpcListSensesRequest,
DaemonIpcRequest,
DaemonIpcTriggerOkResponse,
DaemonIpcErrorResponse,
DaemonIpcTriggerResponse,
DaemonIpcListSensesResponse,
DaemonIpcResponse,
} from "./daemon-ipc-protocol.js";
export { parseDaemonIpcRequest } from "./daemon-ipc-protocol.js";
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@uncaged/nerve-daemon",
"version": "0.3.0",
"version": "0.4.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -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();
@@ -2,7 +2,7 @@
* Unit + integration tests for daemon-ipc.ts — trigger-sense request type.
*
* Tests cover:
* - parseRequest correctly accepts/rejects trigger-sense messages
* - parseDaemonIpcRequest (core) / server correctly accept or reject trigger-sense messages
* - createDaemonIpcServer routes trigger-sense to opts.triggerSense
* - Error response when triggerSense throws (unknown sense)
* - Success response on valid sense trigger
@@ -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" } },
});
+1 -1
View File
@@ -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);
+17 -65
View File
@@ -2,78 +2,24 @@
* Daemon IPC server — listens on a Unix domain socket so that the CLI
* can send commands (e.g. trigger-workflow, trigger-sense) to the running daemon process.
*
* Protocol: newline-delimited JSON messages.
* Each request: { type: "trigger-workflow"; workflow: string; payload: unknown }
* | { type: "trigger-sense"; sense: string }
* | { type: "list-senses" }
* Each response: { ok: true } | { ok: false; error: string }
* | { ok: true; senses: SenseInfo[] } (for list-senses)
* Protocol: newline-delimited JSON — request/response types and
* `parseDaemonIpcRequest` live in `@uncaged/nerve-core`.
*/
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 { DaemonIpcResponse, SenseInfo } from "@uncaged/nerve-core";
import { parseDaemonIpcRequest } from "@uncaged/nerve-core";
import type { WorkflowManager } from "./workflow-manager.js";
export type { SenseInfo };
/** JSON message sent by the CLI to trigger a workflow. */
export type TriggerWorkflowRequest = {
type: "trigger-workflow";
workflow: string;
prompt: string;
maxRounds: number;
};
/** JSON message sent by the CLI to trigger a sense compute on-demand. */
export type TriggerSenseRequest = {
type: "trigger-sense";
sense: string;
};
/** JSON message sent by the CLI to list registered senses. */
export type ListSensesRequest = {
type: "list-senses";
};
type DaemonRequest = TriggerWorkflowRequest | TriggerSenseRequest | ListSensesRequest;
type DaemonResponse =
| { ok: true }
| { ok: false; error: string }
| { ok: true; senses: SenseInfo[] };
export type DaemonIpcServer = {
close: () => Promise<void>;
};
function parseRequest(line: string): DaemonRequest | null {
try {
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 };
}
if (req.type === "trigger-sense") {
if (typeof req.sense !== "string" || req.sense.length === 0) return null;
return { type: "trigger-sense", sense: req.sense };
}
if (req.type === "list-senses") {
return { type: "list-senses" };
}
return null;
} catch {
return null;
}
}
export type DaemonIpcServerOptions = {
/** Called when a trigger-sense request arrives. Should throw if the sense is unknown. */
triggerSense: (senseName: string) => void;
@@ -97,30 +43,36 @@ export function createDaemonIpcServer(
const trimmed = line.trim();
if (trimmed.length === 0) return;
const req = parseRequest(trimmed);
const req = parseDaemonIpcRequest(trimmed);
if (req === null) {
const resp: DaemonResponse = { ok: false, error: "Invalid request" };
const resp: DaemonIpcResponse = { ok: false, error: "Invalid request" };
socket.write(`${JSON.stringify(resp)}\n`);
return;
}
try {
if (req.type === "trigger-workflow") {
workflowManager.startWorkflow(req.workflow, { prompt: req.prompt, maxRounds: req.maxRounds });
const resp: DaemonResponse = { ok: true };
workflowManager.startWorkflow(req.workflow, {
prompt: req.prompt,
maxRounds: req.maxRounds,
});
const resp: DaemonIpcResponse = { ok: true };
socket.write(`${JSON.stringify(resp)}\n`);
} else if (req.type === "trigger-sense") {
opts.triggerSense(req.sense);
const resp: DaemonResponse = { ok: true };
const resp: DaemonIpcResponse = { ok: true };
socket.write(`${JSON.stringify(resp)}\n`);
} else if (req.type === "list-senses") {
const senses = opts.listSenses();
const resp: DaemonResponse = { ok: true, senses };
const resp: DaemonIpcResponse = { ok: true, senses };
socket.write(`${JSON.stringify(resp)}\n`);
} else {
const _exhaustive: never = req;
void _exhaustive;
}
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
const resp: DaemonResponse = { ok: false, error: msg };
const resp: DaemonIpcResponse = { ok: false, error: msg };
socket.write(`${JSON.stringify(resp)}\n`);
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ export {
export { createKernel } from "./kernel.js";
export type { Kernel, KernelOptions, KernelHealth } from "./kernel.js";
export type { SenseInfo } from "./daemon-ipc.js";
export type { SenseInfo } from "@uncaged/nerve-core";
export { createFileWatcher } from "./file-watcher.js";
export type { FileWatcher, FileChange, FileChangeHandler } from "./file-watcher.js";
+3 -1
View File
@@ -296,7 +296,9 @@ const WORKER_MSG_TYPES = new Set([
"thread-workflow-message",
]);
function parseThreadWorkflowMessageMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
function parseThreadWorkflowMessageMsg(
obj: Record<string, unknown>,
): Result<WorkerToParentMessage> {
if (typeof obj.runId !== "string") {
return err(new Error("Worker 'thread-workflow-message' missing string 'runId' field"));
}
+3 -3
View File
@@ -110,9 +110,9 @@ export function runMigrations(sqlite: DatabaseSync, migrationsDir: string): Resu
const migrationRows = sqlite.prepare("SELECT name FROM _migrations").all();
const applied = new Set<string>(
migrationRows.filter((r): r is { name: string } => isPlainRecord(r) && typeof r.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) {
+9 -3
View File
@@ -14,6 +14,7 @@ import { fileURLToPath } from "node:url";
import type { NerveConfig, WorkflowConfig, WorkflowMessage } 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,
@@ -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 = {
+77 -49
View File
@@ -71,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,
@@ -78,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];
@@ -101,17 +160,7 @@ async function runThread(
return;
}
const lastSignal: ModeratorInput =
lastMsg.role === START
? {
role: START,
content: lastMsg.content,
meta: lastMsg.meta as StartSignal["meta"],
timestamp: lastMsg.timestamp,
}
: { role: lastMsg.role, meta: lastMsg.meta as Record<string, unknown> };
let nextRole = def.moderator(lastSignal, roleRound, maxRounds);
let nextRole = def.moderator(buildInitialLastSignal(lastMsg), roleRound, maxRounds);
if (nextRole === END) {
sendThreadEvent(runId, "completed", null);
@@ -119,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,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@uncaged/nerve-store",
"version": "0.3.0",
"version": "0.4.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
+24 -18
View File
@@ -580,6 +580,29 @@ 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,
@@ -587,24 +610,7 @@ export function createLogStore(dbPath: string): LogStore {
try {
const parsed: unknown = JSON.parse(payload);
if (!isPlainRecord(parsed)) return null;
const obj = parsed;
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;
return recordToRoundMessage(parsed, fallbackTs);
} catch {
return null;
}
+10
View File
@@ -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