Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0ce1d995c | |||
| 0a4a2330dc | |||
| d3088c623b | |||
| a7e6caf6e7 | |||
| d4dcd9722f | |||
| 3082568b85 | |||
| 830b0aa762 | |||
| 777d51cc73 | |||
| 06a957d62a | |||
| b2c379cbfd | |||
| 7cb7112ed6 | |||
| 48c81c2e19 | |||
| dd3d4315c4 | |||
| 788ebc6779 | |||
| 8807b0ac6a | |||
| 5b65afdc4b | |||
| f5cb72db50 | |||
| e433e7c2a9 |
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
pnpm check
|
||||||
|
pnpm -r test
|
||||||
@@ -7,28 +7,31 @@ Nerve is a lightweight daemon that continuously observes external state through
|
|||||||
## Core Concepts
|
## Core Concepts
|
||||||
|
|
||||||
```
|
```
|
||||||
External World → Sense → Signal → Reflex → Workflow → Log
|
External World → Sense ─┬→ Signal → Reflex → Sense (scheduled compute)
|
||||||
↑ ↑
|
│
|
||||||
"what to observe" "what to do"
|
└→ Workflow (Sense return with workflow directive) → Log
|
||||||
```
|
```
|
||||||
|
|
||||||
| Concept | Metaphor | Role |
|
| Concept | Metaphor | Role |
|
||||||
|---------|----------|------|
|
|---------|----------|------|
|
||||||
| **Sense** | 👁️ Perception | A `compute()` function that samples or derives data. Each sense has its own SQLite database. |
|
| **Sense** | 👁️ Perception | A `compute()` function that samples or derives data. Each sense has its own SQLite database. |
|
||||||
| **Reflex** | ⚡ Reaction | Declarative trigger — interval-based, event-driven, or both. Connects senses to actions. |
|
| **Reflex** | ⚡ Reaction | Declarative rules that **only schedule Sense computes** (interval and/or `on` signal names). Reflex YAML cannot reference workflows. |
|
||||||
| **Signal** | 📡 Notification | Emitted when a sense returns non-null. Other reflexes can listen for signals. |
|
| **Signal** | 📡 Notification | Emitted when a sense returns a non-null value that is routed as a normal signal (see Sense → Workflow below). Other reflexes can listen via `on`. |
|
||||||
| **Workflow** | 🔧 Action | Stateful multi-step execution with Roles (actors) and a Moderator (coordinator). |
|
| **Workflow** | 🔧 Action | Stateful multi-step execution with Roles and a Moderator. Started from a Sense return value or from CLI/daemon IPC—not from reflex YAML. |
|
||||||
| **Log** | 📝 Record | Immutable audit trail. Queryable by senses, but **cannot** trigger reflexes (prevents feedback loops). |
|
| **Log** | 📝 Record | Immutable audit trail. Queryable by senses, but **cannot** trigger reflexes (prevents feedback loops). |
|
||||||
|
|
||||||
Three extension points, fully orthogonal — a Sense doesn't know when it runs, a Reflex doesn't know what it computes, a Workflow doesn't know why it was triggered.
|
**Sense → Workflow:** if `compute()` returns a plain object with a string field `workflow` in the form `name|maxRounds|prompt` (only the first two `|` delimit name and rounds; the rest is the prompt), the engine starts that workflow and **does not** emit a Signal for that return. `workflow: null` or `""` means “emit a signal” and strip the key from the payload. Invalid `workflow` strings are treated like a normal signal (directive stripped). See `@uncaged/nerve-core` `routeSenseComputeOutput` / `parseSenseWorkflowDirective`.
|
||||||
|
|
||||||
|
Three extension points for **what / when / multi-step action** — reflexes never replace Sense-driven workflow launches.
|
||||||
|
|
||||||
## Packages
|
## Packages
|
||||||
|
|
||||||
| Package | Description |
|
| Package | Description |
|
||||||
|---------|-------------|
|
|---------|-------------|
|
||||||
| [`@uncaged/nerve-core`](./packages/core) | Shared types and config parser |
|
| [`@uncaged/nerve-core`](./packages/core) | Shared types, config parser, Sense→workflow routing, daemon IPC protocol |
|
||||||
| [`@uncaged/nerve-daemon`](./packages/daemon) | The observation engine — kernel, sense runtime, reflex scheduler, workflow manager |
|
| [`@uncaged/nerve-store`](./packages/store) | Append-only log SQLite, JSONL archive, CAS blob store, workflow run rows |
|
||||||
| [`@uncaged/nerve-cli`](./packages/cli) | CLI tool (`nerve`) — init, start, stop, logs, query |
|
| [`@uncaged/nerve-daemon`](./packages/daemon) | Kernel, workers, signal bus, reflex scheduler, workflow manager, file watcher, IPC |
|
||||||
|
| [`@uncaged/nerve-cli`](./packages/cli) | CLI (`nerve`) — init, validate, daemon, dev, logs, sense, store, workflow |
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
@@ -70,15 +73,17 @@ nerve logs # view logs
|
|||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
`nerve.yaml` declares senses, reflexes, and workflows:
|
`nerve.yaml` declares senses, reflexes (sense-only), optional workflows (concurrency), and optional engine `max_rounds`:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
max_rounds: 100 # default moderator cap (e.g. CLI workflow trigger)
|
||||||
|
|
||||||
senses:
|
senses:
|
||||||
cpu-usage:
|
cpu-usage:
|
||||||
group: system # senses in the same group share a worker process
|
group: system # senses in the same group share a worker process
|
||||||
throttle: 10s # min interval between computes
|
throttle: 10s # min interval between computes
|
||||||
timeout: 30s # max compute duration
|
timeout: 30s # max compute duration
|
||||||
gracePeriod: 5s # wait before first compute after startup
|
grace_period: 5s # wait before first compute after startup
|
||||||
|
|
||||||
reflexes:
|
reflexes:
|
||||||
- kind: sense
|
- kind: sense
|
||||||
@@ -86,10 +91,6 @@ reflexes:
|
|||||||
interval: 30s # periodic trigger
|
interval: 30s # periodic trigger
|
||||||
on: [disk-pressure] # also trigger on signals from other senses
|
on: [disk-pressure] # also trigger on signals from other senses
|
||||||
|
|
||||||
- kind: workflow
|
|
||||||
workflow: cleanup
|
|
||||||
on: [disk-pressure] # start a workflow when signal fires
|
|
||||||
|
|
||||||
workflows:
|
workflows:
|
||||||
cleanup:
|
cleanup:
|
||||||
concurrency: 1
|
concurrency: 1
|
||||||
@@ -97,43 +98,66 @@ workflows:
|
|||||||
code-review:
|
code-review:
|
||||||
concurrency: 3
|
concurrency: 3
|
||||||
overflow: queue
|
overflow: queue
|
||||||
maxQueue: 20
|
max_queue: 20
|
||||||
|
```
|
||||||
|
|
||||||
|
YAML must **not** include `workflow:` under `reflexes` — the parser rejects it. Declare workflows under `workflows:` and start them from Sense `compute()` or `nerve workflow trigger`.
|
||||||
|
|
||||||
|
**Example — Sense starts a workflow** (`senses/disk-pressure/compute.ts`):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export async function compute() {
|
||||||
|
const full = await diskNearlyFull();
|
||||||
|
if (!full) return null;
|
||||||
|
return {
|
||||||
|
path: "/data",
|
||||||
|
workflow: "cleanup|10|Disk partition nearly full", // name|maxRounds|prompt
|
||||||
|
};
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
```
|
```
|
||||||
┌─────────────────────────────────────────────────────────┐
|
┌────────────────────────────────────────────────────────────────────────┐
|
||||||
│ Kernel │
|
│ Kernel │
|
||||||
│ │
|
│ │
|
||||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
│ ┌──────────────┐ watches nerve.yaml / senses / workflows │
|
||||||
│ │ Worker │ │ Worker │ │ Worker │ (1 per │
|
│ │ File Watcher ├──────────────────────────────────────────┐ │
|
||||||
│ │ (group A)│ │ (group B)│ │ (group C)│ group) │
|
│ └──────────────┘ │ │
|
||||||
│ │ sense-1 │ │ sense-3 │ │ sense-5 │ │
|
│ ┌──────────────┐ CLI ↔ newline JSON (trigger-workflow, │ │
|
||||||
│ │ sense-2 │ │ sense-4 │ │ │ │
|
│ │ Daemon IPC │ trigger-sense, list-senses) │ │
|
||||||
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
|
│ └──────┬───────┘ ▼ │
|
||||||
│ │ │ │ │
|
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
||||||
│ └──────────────┼──────────────┘ │
|
│ │ │ Worker │ │ Worker │ │ Worker │ (1 per│
|
||||||
│ ▼ │
|
│ │ │ (group A)│ │ (group B)│ │ (group C)│ group) │
|
||||||
│ ┌──────────────┐ │
|
│ │ │ sense-1 │ │ sense-3 │ │ sense-5 │ │
|
||||||
│ │ Signal Bus │ │
|
│ │ │ sense-2 │ │ sense-4 │ │ │ │
|
||||||
│ └──────┬───────┘ │
|
│ │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
|
||||||
│ ▼ │
|
│ │ │ │ │ │
|
||||||
│ ┌──────────────────┐ │
|
│ │ └──────────────┼──────────────┘ │
|
||||||
│ │ Reflex Scheduler │ │
|
│ │ ▼ │
|
||||||
│ └────────┬─────────┘ │
|
│ │ ┌──────────────┐ │
|
||||||
│ ▼ │
|
│ │ │ Signal Bus │ │
|
||||||
│ ┌───────────────────┐ │
|
│ │ └──────┬───────┘ │
|
||||||
│ │ Workflow Manager │──→ Log Store (SQLite) │
|
│ │ ▼ │
|
||||||
│ └───────────────────┘ │
|
│ │ ┌──────────────────┐ │
|
||||||
└─────────────────────────────────────────────────────────┘
|
│ │ │ Reflex Scheduler│ │
|
||||||
|
│ │ └────────┬─────────┘ │
|
||||||
|
│ │ ▼ │
|
||||||
|
│ │ ┌───────────────────┐ │
|
||||||
|
│ └───────────────────►│ Workflow Manager │──→ @uncaged/nerve-store │
|
||||||
|
│ └───────────────────┘ (logs.db, …) │
|
||||||
|
└────────────────────────────────────────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
- **Worker processes** — one per sense group, forked by the kernel. Isolated compute execution.
|
- **Worker pool** — one child process per sense group; isolation between groups.
|
||||||
- **Signal Bus** — in-memory pub/sub for signal distribution.
|
- **Signal Bus** — in-memory pub/sub for signal distribution.
|
||||||
- **Reflex Scheduler** — interval timers + signal subscriptions, with throttle/coalesce.
|
- **Reflex Scheduler** — interval timers + signal subscriptions, with throttle/coalesce.
|
||||||
- **Workflow Manager** — concurrency control (drop/queue), thread lifecycle tracking.
|
- **Workflow Manager** — concurrency (drop/queue), per-workflow workers, crash recovery.
|
||||||
- **Log Store** — WAL-mode SQLite via `node:sqlite`, with archival and retention policies.
|
- **File watcher** — hot reload for config, sense modules, and workflow modules.
|
||||||
|
- **Daemon IPC** — Unix domain socket; used by the CLI when the daemon is running.
|
||||||
|
- **Log / blob storage** — implemented in `@uncaged/nerve-store` (WAL SQLite, JSONL archive, CAS blobs).
|
||||||
|
|
||||||
## Tech Stack
|
## Tech Stack
|
||||||
|
|
||||||
|
|||||||
+14
-1
@@ -19,7 +19,7 @@
|
|||||||
},
|
},
|
||||||
"overrides": [
|
"overrides": [
|
||||||
{
|
{
|
||||||
"include": ["tsup.config.ts"],
|
"include": ["tsup.config.ts", "*/rslib.config.ts"],
|
||||||
"linter": {
|
"linter": {
|
||||||
"rules": {
|
"rules": {
|
||||||
"style": {
|
"style": {
|
||||||
@@ -27,6 +27,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": ["**/__tests__/**"],
|
||||||
|
"linter": {
|
||||||
|
"rules": {
|
||||||
|
"suspicious": {
|
||||||
|
"noExplicitAny": "off"
|
||||||
|
},
|
||||||
|
"style": {
|
||||||
|
"noNonNullAssertion": "off"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"linter": {
|
"linter": {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"node": ">=22.5.0"
|
"node": ">=22.5.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"prepare": "husky",
|
||||||
"build": "pnpm -r run build",
|
"build": "pnpm -r run build",
|
||||||
"check": "biome check .",
|
"check": "biome check .",
|
||||||
"format": "biome format --write ."
|
"format": "biome format --write ."
|
||||||
@@ -12,6 +13,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "^1.9.0",
|
"@biomejs/biome": "^1.9.0",
|
||||||
"@rslib/core": "^0.21.3",
|
"@rslib/core": "^0.21.3",
|
||||||
|
"husky": "^9.1.7",
|
||||||
"typescript": "^5.5.0"
|
"typescript": "^5.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-14
@@ -21,41 +21,59 @@ nerve init # Initialize a nerve workspace (installs deps, scaff
|
|||||||
nerve validate # Validate nerve.yaml configuration
|
nerve validate # Validate nerve.yaml configuration
|
||||||
```
|
```
|
||||||
|
|
||||||
### Daemon Management
|
### Daemon management
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nerve daemon start # Start the daemon (background)
|
nerve daemon start # Start the daemon (background)
|
||||||
nerve daemon stop # Stop the daemon
|
nerve daemon stop # Stop the daemon
|
||||||
nerve daemon status # Check daemon health
|
nerve daemon status # Show pid, uptime, sense names from nerve.yaml (process must exist)
|
||||||
nerve daemon restart # Restart the daemon
|
nerve daemon restart # Stop then start
|
||||||
nerve daemon logs # Tail daemon logs
|
nerve daemon logs # Tail daemon process logs (file under workspace logs/)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Development
|
### Development
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nerve dev # Run in foreground mode (no daemon, Ctrl+C to stop)
|
nerve dev # Foreground kernel with file watcher + IPC (Ctrl+C stops)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Querying
|
### Querying & status
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nerve logs # View structured logs
|
nerve logs # Tail or page the daemon text log file (path in footer; default ~/.uncaged-nerve/logs/nerve.log)
|
||||||
nerve sense query <name> # Query a sense's SQLite database
|
nerve status # Short daemon health summary (aliases daemon status)
|
||||||
nerve sense schema <name> # Show a sense's database schema
|
```
|
||||||
nerve status # Daemon health summary
|
|
||||||
|
Structured rows in `data/logs.db` are surfaced via **`nerve workflow inspect`** / **`nerve workflow list`** (and `LogStore` in code), not via `nerve logs`.
|
||||||
|
|
||||||
|
### Sense
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nerve sense list # List senses (live fields from daemon IPC when running)
|
||||||
|
nerve sense trigger <name> # IPC trigger-sense — queue a compute for that sense
|
||||||
|
nerve sense query <name> # Read-only SQL on data/senses/<name>.db (optional SQL args)
|
||||||
|
nerve sense schema <name> # Print CREATE TABLE statements for that sense DB
|
||||||
|
```
|
||||||
|
|
||||||
|
### Store maintenance
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nerve store archive # Move old log rows to JSONL under data/archive/logs/… (optional --vacuum)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Workflows
|
### Workflows
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nerve workflow list # List workflow runs
|
nerve workflow list # Queued/started runs (add --all for terminal states; --workflow, --limit, --offset)
|
||||||
nerve workflow show <runId> # Show workflow run details
|
nerve workflow inspect <runId> # Run metadata + paginated workflow log lines
|
||||||
|
nerve workflow thread <runId> # Role rounds from persisted messages (--before, --budget)
|
||||||
|
nerve workflow trigger <name> # IPC trigger-workflow (daemon must be running)
|
||||||
|
# Optional JSON: --payload '{"prompt":"…","maxRounds":50}'
|
||||||
```
|
```
|
||||||
|
|
||||||
### Top-level Aliases
|
`nerve workflow trigger` sends a `trigger-workflow` line on the daemon Unix socket (same protocol as `@uncaged/nerve-core` / `parseDaemonIpcRequest`). It does not read `nerve.yaml` workflow definitions beyond what the running daemon already loaded.
|
||||||
|
|
||||||
For convenience, these aliases are available:
|
### Top-level aliases
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nerve start → nerve daemon start
|
nerve start → nerve daemon start
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=22.5.0"
|
"node": ">=22.5.0"
|
||||||
},
|
},
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"bin": {
|
"bin": {
|
||||||
"nerve": "dist/cli.js"
|
"nerve": "dist/cli.js"
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ describe("logsCommand negative offset", () => {
|
|||||||
|
|
||||||
it("exits with code 1 and writes to stderr when offset is negative", async () => {
|
it("exits with code 1 and writes to stderr when offset is negative", async () => {
|
||||||
await expect(
|
await expect(
|
||||||
logsCommand.run!({
|
logsCommand.run?.({
|
||||||
args: { n: "50", offset: "-5", follow: false },
|
args: { n: "50", offset: "-5", follow: false },
|
||||||
rawArgs: [],
|
rawArgs: [],
|
||||||
cmd: logsCommand as never,
|
cmd: logsCommand as never,
|
||||||
@@ -247,7 +247,7 @@ describe("logsCommand negative offset", () => {
|
|||||||
|
|
||||||
it("exits with code 1 for offset=-1", async () => {
|
it("exits with code 1 for offset=-1", async () => {
|
||||||
await expect(
|
await expect(
|
||||||
logsCommand.run!({
|
logsCommand.run?.({
|
||||||
args: { n: "10", offset: "-1", follow: false },
|
args: { n: "10", offset: "-1", follow: false },
|
||||||
rawArgs: [],
|
rawArgs: [],
|
||||||
cmd: logsCommand as never,
|
cmd: logsCommand as never,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import { join } from "node:path";
|
|||||||
import { createLogStore } from "@uncaged/nerve-store";
|
import { createLogStore } from "@uncaged/nerve-store";
|
||||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
||||||
import {
|
import {
|
||||||
DEFAULT_THREAD_BUDGET_CHARS,
|
DEFAULT_THREAD_BUDGET_CHARS,
|
||||||
buildInspectOutput,
|
buildInspectOutput,
|
||||||
@@ -28,7 +29,6 @@ import {
|
|||||||
statusIcon,
|
statusIcon,
|
||||||
} from "../commands/workflow.js";
|
} from "../commands/workflow.js";
|
||||||
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
||||||
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Test helpers
|
// Test helpers
|
||||||
@@ -342,9 +342,14 @@ describe("partitionWorkflowMessage", () => {
|
|||||||
expect(p.meta).toEqual({ items: [1, 2] });
|
expect(p.meta).toEqual({ items: [1, 2] });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses fallback role and stringifies non-string content", () => {
|
it("passes through role and content as-is", () => {
|
||||||
const p = partitionWorkflowMessage({ content: { n: 1 } });
|
const p = partitionWorkflowMessage({
|
||||||
expect(p.roleStr).toBe("?");
|
role: "unknown",
|
||||||
|
content: '{"n":1}',
|
||||||
|
meta: null,
|
||||||
|
timestamp: 0,
|
||||||
|
});
|
||||||
|
expect(p.roleStr).toBe("unknown");
|
||||||
expect(p.contentBody).toBe('{"n":1}');
|
expect(p.contentBody).toBe('{"n":1}');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -509,7 +514,7 @@ describe("triggerWorkflowViaDaemon", () => {
|
|||||||
await new Promise<void>((r) => server.listen(sockPath, r));
|
await new Promise<void>((r) => server.listen(sockPath, r));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await triggerWorkflowViaDaemon(sockPath, "my-workflow", {});
|
const result = await triggerWorkflowViaDaemon(sockPath, "my-workflow", "", 100);
|
||||||
expect(result).toEqual({ ok: true });
|
expect(result).toEqual({ ok: true });
|
||||||
} finally {
|
} finally {
|
||||||
await new Promise<void>((r) => server.close(() => r()));
|
await new Promise<void>((r) => server.close(() => r()));
|
||||||
@@ -525,7 +530,7 @@ describe("triggerWorkflowViaDaemon", () => {
|
|||||||
await new Promise<void>((r) => server.listen(sockPath, r));
|
await new Promise<void>((r) => server.listen(sockPath, r));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await triggerWorkflowViaDaemon(sockPath, "missing", {});
|
const result = await triggerWorkflowViaDaemon(sockPath, "missing", "", 100);
|
||||||
expect(result).toEqual({ ok: false, error: "unknown workflow" });
|
expect(result).toEqual({ ok: false, error: "unknown workflow" });
|
||||||
} finally {
|
} finally {
|
||||||
await new Promise<void>((r) => server.close(() => r()));
|
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 () => {
|
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/,
|
/Cannot connect to daemon/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ export function buildLogFooter(slice: LogSlice, nArg: number, logPath: string):
|
|||||||
let footer = `\n📄 ${rangeStr} | ${logPath}\n`;
|
let footer = `\n📄 ${rangeStr} | ${logPath}\n`;
|
||||||
|
|
||||||
if (slice.nextOffset !== null) {
|
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`;
|
footer += ` nerve logs --offset ${slice.nextOffset} -n ${nArg}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import { join } from "node:path";
|
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 { defineCommand } from "citty";
|
||||||
|
|
||||||
import { listSensesViaDaemon, triggerSenseViaDaemon } from "../daemon-client.js";
|
import { listSensesViaDaemon, triggerSenseViaDaemon } from "../daemon-client.js";
|
||||||
import {
|
import {
|
||||||
assertSenseDbExists,
|
|
||||||
defaultPreviewSql,
|
defaultPreviewSql,
|
||||||
formatRowsAsAlignedTable,
|
formatRowsAsAlignedTable,
|
||||||
listTableSqlStatements,
|
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) {
|
if (args.json) {
|
||||||
process.stdout.write(`${JSON.stringify(rows, null, 2)}\n`);
|
process.stdout.write(`${JSON.stringify(rows, null, 2)}\n`);
|
||||||
|
|||||||
@@ -74,9 +74,11 @@ async function runDaemon(nerveRoot: string): Promise<void> {
|
|||||||
|
|
||||||
const bootstrapPath = daemonBootstrapScript();
|
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], {
|
const child = spawn(process.execPath, [bootstrapPath], {
|
||||||
detached: true,
|
detached: true,
|
||||||
stdio: ["ignore", (logStream as any).fd, (logStream as any).fd],
|
stdio: ["ignore", logFd, logFd],
|
||||||
env: { ...process.env, NERVE_ROOT: nerveRoot },
|
env: { ...process.env, NERVE_ROOT: nerveRoot },
|
||||||
cwd: nerveRoot,
|
cwd: nerveRoot,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -47,7 +47,11 @@ export const statusCommand = defineCommand({
|
|||||||
return;
|
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");
|
const configPath = join(getNerveRoot(), "nerve.yaml");
|
||||||
let senseList: string[] = [];
|
let senseList: string[] = [];
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import type { DaemonIpcTriggerResponse } from "@uncaged/nerve-core";
|
||||||
|
import { DEFAULT_ENGINE_MAX_ROUNDS, isPlainRecord } from "@uncaged/nerve-core";
|
||||||
import { defineCommand } from "citty";
|
import { defineCommand } from "citty";
|
||||||
import { stringify } from "yaml";
|
import { stringify } from "yaml";
|
||||||
|
|
||||||
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
|
||||||
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
import type { LogStore, ThreadRoundRow, WorkflowRun } from "@uncaged/nerve-store";
|
||||||
|
import { triggerWorkflowViaDaemon } from "../daemon-client.js";
|
||||||
import { loadDaemonModule } from "../workspace-daemon.js";
|
import { loadDaemonModule } from "../workspace-daemon.js";
|
||||||
import { getNerveRoot, getSocketPath, isRunning } from "../workspace.js";
|
import { getNerveRoot, getSocketPath, isRunning } from "../workspace.js";
|
||||||
|
|
||||||
@@ -203,7 +205,9 @@ export function partitionWorkflowMessage(msg: {
|
|||||||
const contentBody = msg.content;
|
const contentBody = msg.content;
|
||||||
const meta: Record<string, unknown> =
|
const meta: Record<string, unknown> =
|
||||||
msg.meta !== null && msg.meta !== undefined && typeof msg.meta === "object"
|
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 };
|
return { roleStr, contentBody, meta };
|
||||||
}
|
}
|
||||||
@@ -215,13 +219,7 @@ export function formatThreadRoundBlock(row: ThreadRoundRow): string {
|
|||||||
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
||||||
const yamlBlock =
|
const yamlBlock =
|
||||||
Object.keys(meta).length === 0 ? "{}\n" : `${stringify(meta, { lineWidth: 100 })}\n`;
|
Object.keys(meta).length === 0 ? "{}\n" : `${stringify(meta, { lineWidth: 100 })}\n`;
|
||||||
return (
|
return `[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n---\n${yamlBlock}---\n${contentBody}\n\n`;
|
||||||
`[#${row.round} ${roleStr}] ${formatTs(row.ts)}\n` +
|
|
||||||
`---\n` +
|
|
||||||
yamlBlock +
|
|
||||||
`---\n` +
|
|
||||||
`${contentBody}\n\n`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ThreadCommandOutput = {
|
export type ThreadCommandOutput = {
|
||||||
@@ -229,6 +227,33 @@ export type ThreadCommandOutput = {
|
|||||||
paginationHint: string | null;
|
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
|
* Build stdout lines for `nerve workflow thread`: newest-first selection from
|
||||||
* `descRows` until `budgetChars` (including `prefixLines`), then chronological order.
|
* `descRows` until `budgetChars` (including `prefixLines`), then chronological order.
|
||||||
@@ -254,25 +279,7 @@ export function buildThreadCommandOutput(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (picked.length === 0) {
|
if (picked.length === 0) {
|
||||||
const { roleStr, contentBody, meta } = partitionWorkflowMessage(row.message);
|
return buildTruncatedSingleRound(row, remaining, prefixLines, runId, budgetFlag);
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -281,9 +288,7 @@ export function buildThreadCommandOutput(
|
|||||||
const shownMinRound = picked.length === 0 ? null : Math.min(...picked.map((r) => r.round));
|
const shownMinRound = picked.length === 0 ? null : Math.min(...picked.map((r) => r.round));
|
||||||
let paginationHint: string | null = null;
|
let paginationHint: string | null = null;
|
||||||
if (shownMinRound !== null && shownMinRound > 1) {
|
if (shownMinRound !== null && shownMinRound > 1) {
|
||||||
paginationHint =
|
paginationHint = `\n⏩ Older rounds not shown. Fetch with:\n nerve workflow thread ${runId} --before ${String(shownMinRound)}${budgetFlag}\n`;
|
||||||
`\n⏩ Older rounds not shown. Fetch with:\n` +
|
|
||||||
` nerve workflow thread ${runId} --before ${String(shownMinRound)}${budgetFlag}\n`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { lines: [...prefixLines, ...blocksAsc], paginationHint };
|
return { lines: [...prefixLines, ...blocksAsc], paginationHint };
|
||||||
@@ -452,10 +457,7 @@ const workflowThreadCommand = defineCommand({
|
|||||||
const totalRoleRounds = store.getThreadRoundCount(args.runId);
|
const totalRoleRounds = store.getThreadRoundCount(args.runId);
|
||||||
if (totalRoleRounds === 0) {
|
if (totalRoleRounds === 0) {
|
||||||
process.stdout.write(
|
process.stdout.write(
|
||||||
`🧵 Workflow thread: ${run.runId}\n` +
|
`🧵 Workflow thread: ${run.runId}\n workflow: ${run.workflow}\n status: ${run.status}\n\n📭 No role rounds recorded for this run.\n`,
|
||||||
` workflow: ${run.workflow}\n` +
|
|
||||||
` status: ${run.status}\n\n` +
|
|
||||||
`📭 No role rounds recorded for this run.\n`,
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -466,7 +468,7 @@ const workflowThreadCommand = defineCommand({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const prefixLines = [
|
const prefixLines = [
|
||||||
`🧵 Role rounds (workflow thread)\n`,
|
"🧵 Role rounds (workflow thread)\n",
|
||||||
` runId: ${run.runId}\n`,
|
` runId: ${run.runId}\n`,
|
||||||
` workflow: ${run.workflow}\n`,
|
` workflow: ${run.workflow}\n`,
|
||||||
` status: ${run.status}\n`,
|
` status: ${run.status}\n`,
|
||||||
@@ -512,7 +514,8 @@ const workflowTriggerCommand = defineCommand({
|
|||||||
},
|
},
|
||||||
payload: {
|
payload: {
|
||||||
type: "string",
|
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: "{}",
|
default: "{}",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -525,15 +528,23 @@ const workflowTriggerCommand = defineCommand({
|
|||||||
process.exit(1);
|
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()) {
|
if (!isRunning()) {
|
||||||
process.stderr.write("❌ Nerve daemon is not running. Start it with `nerve start`.\n");
|
process.stderr.write("❌ Nerve daemon is not running. Start it with `nerve start`.\n");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const socketPath = getSocketPath();
|
const socketPath = getSocketPath();
|
||||||
let response: { ok: true } | { ok: false; error: string };
|
let response: DaemonIpcTriggerResponse;
|
||||||
try {
|
try {
|
||||||
response = await triggerWorkflowViaDaemon(socketPath, args.name, triggerPayload);
|
response = await triggerWorkflowViaDaemon(socketPath, args.name, prompt, maxRounds);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
process.stderr.write(`❌ Failed to contact daemon: ${msg}\n`);
|
process.stderr.write(`❌ Failed to contact daemon: ${msg}\n`);
|
||||||
|
|||||||
@@ -8,22 +8,35 @@
|
|||||||
import { connect } from "node:net";
|
import { connect } from "node:net";
|
||||||
import type { Socket } 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;
|
const CONNECT_TIMEOUT_MS = 3_000;
|
||||||
const RESPONSE_TIMEOUT_MS = 5_000;
|
const RESPONSE_TIMEOUT_MS = 5_000;
|
||||||
|
|
||||||
export type { SenseInfo };
|
export type { SenseInfo };
|
||||||
|
|
||||||
type TriggerResponse = { ok: true } | { 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")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
type ListSensesResponse = { ok: true; senses: SenseInfo[] } | { ok: false; error: string };
|
function parseDaemonResponse(line: string): DaemonIpcTriggerResponse {
|
||||||
|
|
||||||
function parseDaemonResponse(line: string): TriggerResponse {
|
|
||||||
try {
|
try {
|
||||||
const obj = JSON.parse(line) as unknown;
|
const obj: unknown = JSON.parse(line);
|
||||||
if (obj !== null && typeof obj === "object") {
|
if (isPlainRecord(obj)) {
|
||||||
const r = obj as Record<string, unknown>;
|
const r = obj;
|
||||||
if (r.ok === true) return { ok: true };
|
if (r.ok === true) return { ok: true };
|
||||||
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
||||||
}
|
}
|
||||||
@@ -33,14 +46,15 @@ function parseDaemonResponse(line: string): TriggerResponse {
|
|||||||
return { ok: false, error: `Unexpected daemon response: ${line}` };
|
return { ok: false, error: `Unexpected daemon response: ${line}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseListSensesResponse(line: string): ListSensesResponse {
|
function parseListSensesResponse(line: string): DaemonIpcListSensesResponse {
|
||||||
try {
|
try {
|
||||||
const obj = JSON.parse(line) as unknown;
|
const obj: unknown = JSON.parse(line);
|
||||||
if (obj !== null && typeof obj === "object") {
|
if (isPlainRecord(obj)) {
|
||||||
const r = obj as Record<string, unknown>;
|
const r = obj;
|
||||||
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
if (r.ok === false && typeof r.error === "string") return { ok: false, error: r.error };
|
||||||
if (r.ok === true && Array.isArray(r.senses))
|
if (r.ok === true && Array.isArray(r.senses) && r.senses.every(isSenseInfo)) {
|
||||||
return { ok: true, senses: r.senses as SenseInfo[] };
|
return { ok: true, senses: r.senses };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// fall through
|
// fall through
|
||||||
@@ -54,7 +68,7 @@ function parseListSensesResponse(line: string): ListSensesResponse {
|
|||||||
*/
|
*/
|
||||||
function sendAndReceive<T>(
|
function sendAndReceive<T>(
|
||||||
socketPath: string,
|
socketPath: string,
|
||||||
message: object,
|
message: DaemonIpcRequest,
|
||||||
parseFirstLine: (trimmed: string) => T,
|
parseFirstLine: (trimmed: string) => T,
|
||||||
responseTimeoutMs: number = RESPONSE_TIMEOUT_MS,
|
responseTimeoutMs: number = RESPONSE_TIMEOUT_MS,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
@@ -119,27 +133,35 @@ function sendAndReceive<T>(
|
|||||||
export function triggerWorkflowViaDaemon(
|
export function triggerWorkflowViaDaemon(
|
||||||
socketPath: string,
|
socketPath: string,
|
||||||
workflow: string,
|
workflow: string,
|
||||||
payload: unknown,
|
prompt: string,
|
||||||
): Promise<TriggerResponse> {
|
maxRounds: number,
|
||||||
return sendAndReceive(
|
): Promise<DaemonIpcTriggerResponse> {
|
||||||
socketPath,
|
const message: DaemonIpcRequest = {
|
||||||
{ type: "trigger-workflow", workflow, payload },
|
type: "trigger-workflow",
|
||||||
parseDaemonResponse,
|
workflow,
|
||||||
);
|
prompt,
|
||||||
|
maxRounds,
|
||||||
|
};
|
||||||
|
return sendAndReceive(socketPath, message, parseDaemonResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a trigger-sense message to the running daemon via its Unix socket.
|
* 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.
|
* Resolves with the daemon's response or rejects on connection/timeout errors.
|
||||||
*/
|
*/
|
||||||
export function triggerSenseViaDaemon(socketPath: string, sense: string): Promise<TriggerResponse> {
|
export function triggerSenseViaDaemon(
|
||||||
return sendAndReceive(socketPath, { type: "trigger-sense", sense }, parseDaemonResponse);
|
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.
|
* 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.
|
* Resolves with the list of registered senses or rejects on connection/timeout errors.
|
||||||
*/
|
*/
|
||||||
export function listSensesViaDaemon(socketPath: string): Promise<ListSensesResponse> {
|
export function listSensesViaDaemon(socketPath: string): Promise<DaemonIpcListSensesResponse> {
|
||||||
return sendAndReceive(socketPath, { type: "list-senses" }, parseListSensesResponse);
|
const message: DaemonIpcRequest = { type: "list-senses" };
|
||||||
|
return sendAndReceive(socketPath, message, parseListSensesResponse);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,5 +46,6 @@ export type DaemonModule = {
|
|||||||
export async function loadDaemonModule(nerveRoot: string): Promise<DaemonModule> {
|
export async function loadDaemonModule(nerveRoot: string): Promise<DaemonModule> {
|
||||||
const entry = assertWorkspaceDaemonInstalled(nerveRoot);
|
const entry = assertWorkspaceDaemonInstalled(nerveRoot);
|
||||||
const url = pathToFileURL(entry).href;
|
const url = pathToFileURL(entry).href;
|
||||||
|
// Dynamic import return type is module-specific; narrow at this workspace boundary.
|
||||||
return import(url) as Promise<DaemonModule>;
|
return import(url) as Promise<DaemonModule>;
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-3
@@ -4,9 +4,12 @@ Shared types and configuration parser for the [nerve](../../README.md) observati
|
|||||||
|
|
||||||
## What's Inside
|
## What's Inside
|
||||||
|
|
||||||
- **Type definitions** — `Signal`, `SenseConfig`, `ReflexConfig`, `WorkflowConfig`, `NerveConfig`, and all related types
|
- **Type definitions** — `Signal`, `SenseConfig`, `SenseInfo`, `SenseReflexConfig`, `ReflexConfig` (sense-only), `WorkflowConfig`, `NerveConfig`, and related types
|
||||||
- **Config parser** — `parseNerveConfig(yaml)` validates and parses `nerve.yaml` into a typed `NerveConfig`
|
- **Config parser** — `parseNerveConfig(yaml)` validates and parses `nerve.yaml` into `NerveConfig` (rejects reflex entries that declare a `workflow` key; reflexes only schedule senses)
|
||||||
- **Result type** — `Result<T>` with `ok()` / `err()` helpers for explicit error handling (no thrown exceptions)
|
- **Sense → workflow routing** — `parseSenseWorkflowDirective`, `routeSenseComputeOutput`, and types `ParsedSenseWorkflowDirective`, `SenseComputeRoute`
|
||||||
|
- **Daemon IPC protocol** — request/response types (`DaemonIpcRequest`, `DaemonIpcResponse`, …) and `parseDaemonIpcRequest` for newline-delimited JSON on the CLI ↔ daemon socket
|
||||||
|
- **Workflow automaton types** — `START` / `END` sentinel constants, `WorkflowMessage`, `StartSignal`, `RoleSignal`, `Moderator`, `WorkflowDefinition`, `Role`, `SenseResult`, plus `DEFAULT_ENGINE_MAX_ROUNDS`
|
||||||
|
- **Result type** — `Result<T>` with `ok()` / `err()` helpers for explicit error handling (no thrown exceptions for parse paths)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -20,6 +23,29 @@ if (result.ok) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Sense return → signal vs workflow
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { parseSenseWorkflowDirective, routeSenseComputeOutput } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
|
const directive = parseSenseWorkflowDirective("my-workflow|8|Hello from sense");
|
||||||
|
if (directive.ok) {
|
||||||
|
console.log(directive.value.workflowName, directive.value.maxRounds, directive.value.prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
const route = routeSenseComputeOutput({
|
||||||
|
metric: 42,
|
||||||
|
workflow: "my-workflow|8|Run now",
|
||||||
|
});
|
||||||
|
if (route.kind === "launch") {
|
||||||
|
// engine starts workflow; no Signal to the bus for this return
|
||||||
|
console.log(route.launch);
|
||||||
|
} else {
|
||||||
|
// normal signal with payload
|
||||||
|
console.log(route.payload);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Duration Format
|
## Duration Format
|
||||||
|
|
||||||
Config fields like `throttle`, `timeout`, and `interval` accept human-readable durations:
|
Config fields like `throttle`, `timeout`, and `interval` accept human-readable durations:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@uncaged/nerve-core",
|
"name": "@uncaged/nerve-core",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"files": ["dist"],
|
"files": ["dist"],
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ reflexes:
|
|||||||
expect(result.error.message).toMatch(/disk.*not found in senses/);
|
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 = `
|
const yaml = `
|
||||||
senses:
|
senses:
|
||||||
cpu:
|
cpu:
|
||||||
@@ -206,10 +206,10 @@ reflexes:
|
|||||||
const result = parseNerveConfig(yaml);
|
const result = parseNerveConfig(yaml);
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
if (result.ok) return;
|
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 = `
|
const yaml = `
|
||||||
senses:
|
senses:
|
||||||
cpu:
|
cpu:
|
||||||
@@ -226,7 +226,7 @@ workflows:
|
|||||||
const result = parseNerveConfig(yaml);
|
const result = parseNerveConfig(yaml);
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
if (result.ok) return;
|
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", () => {
|
it("returns error for invalid throttle format", () => {
|
||||||
@@ -354,7 +354,7 @@ reflexes:
|
|||||||
const result = parseNerveConfig(yaml);
|
const result = parseNerveConfig(yaml);
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
if (result.ok) return;
|
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", () => {
|
it("returns error when reflex has neither sense nor workflow", () => {
|
||||||
@@ -368,7 +368,7 @@ reflexes:
|
|||||||
const result = parseNerveConfig(yaml);
|
const result = parseNerveConfig(yaml);
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
if (result.ok) return;
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
+15
-14
@@ -1,5 +1,6 @@
|
|||||||
import { parse } from "yaml";
|
import { parse } from "yaml";
|
||||||
|
|
||||||
|
import { isPlainRecord } from "./is-plain-record.js";
|
||||||
import type { Result } from "./result.js";
|
import type { Result } from "./result.js";
|
||||||
import { err, ok } from "./result.js";
|
import { err, ok } from "./result.js";
|
||||||
import type { NerveConfig, ReflexConfig, SenseConfig, WorkflowConfig } from "./types.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> {
|
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`));
|
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() === "") {
|
if (typeof obj.group !== "string" || obj.group.trim() === "") {
|
||||||
return err(new Error(`senses.${name}.group: required string`));
|
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> {
|
function parseOnField(index: number, obj: Record<string, unknown>): Result<string[] | null> {
|
||||||
if (obj.on === undefined || obj.on === null) return ok(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 err(new Error(`reflexes[${index}].on: must be an array of strings`));
|
||||||
}
|
}
|
||||||
return ok(obj.on as string[]);
|
return ok(obj.on);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseSenseReflex(
|
function parseSenseReflex(
|
||||||
@@ -118,11 +119,11 @@ function validateReflexConfig(
|
|||||||
raw: unknown,
|
raw: unknown,
|
||||||
senseNames: Set<string>,
|
senseNames: Set<string>,
|
||||||
): Result<ReflexConfig> {
|
): Result<ReflexConfig> {
|
||||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
if (!isPlainRecord(raw)) {
|
||||||
return err(new Error(`reflexes[${index}]: must be an object`));
|
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 hasSense = obj.sense !== undefined;
|
||||||
const hasWorkflowKey = Object.hasOwn(obj, "workflow");
|
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> {
|
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`));
|
return err(new Error(`workflows.${name}: must be an object`));
|
||||||
}
|
}
|
||||||
|
|
||||||
const obj = raw as Record<string, unknown>;
|
const obj = raw;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof obj.concurrency !== "number" ||
|
typeof obj.concurrency !== "number" ||
|
||||||
@@ -209,11 +210,11 @@ function validateWorkflowConfig(name: string, raw: unknown): Result<WorkflowConf
|
|||||||
function parseSenses(
|
function parseSenses(
|
||||||
obj: Record<string, unknown>,
|
obj: Record<string, unknown>,
|
||||||
): Result<{ senses: Record<string, SenseConfig>; senseNames: Set<string> }> {
|
): 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"));
|
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 senses: Record<string, SenseConfig> = {};
|
||||||
const senseNames = new Set(Object.keys(sensesRaw));
|
const senseNames = new Set(Object.keys(sensesRaw));
|
||||||
|
|
||||||
@@ -249,11 +250,11 @@ function parseWorkflows(
|
|||||||
): Result<Record<string, WorkflowConfig> | null> {
|
): Result<Record<string, WorkflowConfig> | null> {
|
||||||
if (obj.workflows === undefined || obj.workflows === null) return ok(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"));
|
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> = {};
|
const workflows: Record<string, WorkflowConfig> = {};
|
||||||
|
|
||||||
for (const [name, wfRaw] of Object.entries(workflowsRaw)) {
|
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}`));
|
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"));
|
return err(new Error("Config must be a YAML object"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const obj = parsed as Record<string, unknown>;
|
const obj = parsed;
|
||||||
|
|
||||||
const sensesResult = parseSenses(obj);
|
const sensesResult = parseSenses(obj);
|
||||||
if (!sensesResult.ok) return sensesResult;
|
if (!sensesResult.ok) return sensesResult;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,26 @@ export { START, END, DEFAULT_ENGINE_MAX_ROUNDS } from "./types.js";
|
|||||||
export type { Result } from "./result.js";
|
export type { Result } from "./result.js";
|
||||||
export { ok, err } from "./result.js";
|
export { ok, err } from "./result.js";
|
||||||
export { parseNerveConfig } from "./config.js";
|
export { parseNerveConfig } from "./config.js";
|
||||||
|
export { isPlainRecord } from "./is-plain-record.js";
|
||||||
|
|
||||||
export type { ParsedSenseWorkflowDirective, SenseComputeRoute } from "./sense-workflow-directive.js";
|
export type {
|
||||||
export { parseSenseWorkflowDirective, routeSenseComputeOutput } from "./sense-workflow-directive.js";
|
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";
|
||||||
|
|||||||
@@ -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 type { Result } from "./result.js";
|
||||||
import { err, ok } 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
|
* - `workflow: "name|n|prompt"` → launch workflow; no Signal is emitted to the bus
|
||||||
*/
|
*/
|
||||||
export function routeSenseComputeOutput(payload: unknown): SenseComputeRoute {
|
export function routeSenseComputeOutput(payload: unknown): SenseComputeRoute {
|
||||||
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
if (!isPlainRecord(payload)) {
|
||||||
return { kind: "signal", payload };
|
return { kind: "signal", payload };
|
||||||
}
|
}
|
||||||
const obj = payload as Record<string, unknown>;
|
const obj = payload;
|
||||||
if (!Object.hasOwn(obj, "workflow")) {
|
if (!Object.hasOwn(obj, "workflow")) {
|
||||||
return { kind: "signal", payload };
|
return { kind: "signal", payload };
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-18
@@ -4,18 +4,33 @@ The observation engine runtime for [nerve](../../README.md) — runs senses, rou
|
|||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
| Module | Responsibility |
|
| Module | Source (indicative) | Responsibility |
|
||||||
|--------|---------------|
|
|--------|---------------------|----------------|
|
||||||
| **Kernel** | Top-level orchestrator — spawns workers, wires up signal bus, scheduler, and workflow manager. Supports hot reload and graceful shutdown. |
|
| **Kernel** | `kernel.ts` | Orchestrator — worker pool, signal bus, reflex scheduler, workflow manager, optional file watcher and daemon IPC, config reload hooks |
|
||||||
| **Sense Runtime** | Per-sense SQLite database (via `node:sqlite` + Drizzle ORM), migration runner, peer DB read access. |
|
| **Worker pool** | `worker-pool.ts` | Fork and supervise one child process per sense group; restart/shutdown; crash cleanup hooks for scheduler state |
|
||||||
| **Sense Worker** | Forked child process — one per sense group. Runs compute functions in isolation. |
|
| **Kernel sense groups** | `kernel-sense-groups.ts` | Derive sense groups from config; list senses per group for scheduling |
|
||||||
| **Signal Bus** | In-memory pub/sub. Sense computes emit signals; reflexes and workflows subscribe. |
|
| **Sense runtime** | sense worker + Drizzle | Per-sense SQLite (`node:sqlite`), migrations, peer DB reads |
|
||||||
| **Reflex Scheduler** | Drives compute triggers — interval timers, signal-based events, throttle/coalesce logic. |
|
| **Sense worker** | `sense-worker.ts` (fork target) | Child process entry — runs `compute()` per sense in a group |
|
||||||
| **Workflow Manager** | Concurrency control (drop/queue), thread lifecycle, worker process management (RFC-002). |
|
| **Signal bus** | `signal-bus.ts` | In-memory pub/sub for sense signals |
|
||||||
| **Log Store** | Structured log storage in WAL-mode SQLite. Supports retention policies, archival to JSONL, and workflow run tracking. |
|
| **Reflex scheduler** | `reflex-scheduler.ts` | Interval + `on` subscriptions, throttle/coalesce |
|
||||||
| **Blob Store** | Binary artifact storage for workflow outputs. |
|
| **Workflow manager** | `workflow-manager.ts` | One worker per workflow name, concurrency (drop/queue), queue caps |
|
||||||
| **File Watcher** | Watches `nerve.yaml` and sense files for hot reload. |
|
| **Workflow worker** | `workflow-worker.ts` | Child process — runs RFC-002 threads (`start-thread`, `resume-thread` IPC) |
|
||||||
| **Daemon IPC** | Unix socket server for CLI ↔ daemon communication. |
|
| **IPC (parent ↔ workers)** | `ipc.ts` | Typed messages for sense and workflow workers (includes `resume-thread` for recovery) |
|
||||||
|
| **Log / workflow persistence** | via `@uncaged/nerve-store` | Structured logs, `workflow_runs`, thread messages (used for recovery) |
|
||||||
|
| **Blob store** | `@uncaged/nerve-store` | CAS under `data/blobs/` — sense workers construct `createBlobStore(join(nerveRoot, "data", "blobs"))` for artifact writes |
|
||||||
|
| **File watcher** | `file-watcher.ts` | Watches workspace paths for config / sense / workflow file changes |
|
||||||
|
| **Kernel file watch** | `kernel-file-watch.ts` | Maps watcher events to `reloadConfig`, sense group restart, workflow `drainAndRespawn` |
|
||||||
|
| **Daemon IPC** | `daemon-ipc.ts` | Unix socket server — parses `@uncaged/nerve-core` `DaemonIpcRequest`, dispatches trigger-workflow / trigger-sense / list-senses |
|
||||||
|
|
||||||
|
## Crash recovery (workflow workers)
|
||||||
|
|
||||||
|
If a workflow worker exits unexpectedly while threads are active:
|
||||||
|
|
||||||
|
- In-flight runs are marked **`crashed`** in the log store; the manager respawns a fresh worker.
|
||||||
|
- Runs still in **`started`** state can be **`resume-thread`**’d: the manager rebuilds the message chain from persisted workflow log rows and sends `resume-thread` to the new worker.
|
||||||
|
- **Crash-loop backoff:** repeated crashes for the same workflow name are counted in a sliding window (`60s`); after **`5`** crashes in that window, the manager **stops respawning** that worker and logs the condition (avoids tight crash loops).
|
||||||
|
|
||||||
|
Hot reload (`drainAndRespawn`) uses a controlled drain: in-flight runs may be marked **`interrupted`** when the old worker is torn down after a timeout — that path is distinct from unexpected crash recovery.
|
||||||
|
|
||||||
## Key Design Decisions
|
## Key Design Decisions
|
||||||
|
|
||||||
@@ -26,24 +41,44 @@ The observation engine runtime for [nerve](../../README.md) — runs senses, rou
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
The daemon is typically started via the CLI (`nerve daemon start`), but can be used programmatically:
|
The daemon is typically started via the CLI (`nerve daemon start` / `nerve dev`), but you can embed the kernel:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import { parseNerveConfig } from "@uncaged/nerve-core";
|
||||||
import { createKernel } from "@uncaged/nerve-daemon";
|
import { createKernel } from "@uncaged/nerve-daemon";
|
||||||
|
|
||||||
const kernel = await createKernel(nerveRoot);
|
const nerveRoot = "/path/to/workspace";
|
||||||
|
const yamlPath = join(nerveRoot, "nerve.yaml");
|
||||||
|
const parsed = parseNerveConfig(readFileSync(yamlPath, "utf8"));
|
||||||
|
if (!parsed.ok) {
|
||||||
|
throw parsed.error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const kernel = createKernel(parsed.value, nerveRoot, {
|
||||||
|
enableFileWatcher: true,
|
||||||
|
ipcSocketPath: join(nerveRoot, "nerve.sock"),
|
||||||
|
});
|
||||||
|
|
||||||
await kernel.ready;
|
await kernel.ready;
|
||||||
|
|
||||||
// Trigger a sense manually
|
|
||||||
kernel.triggerSense("cpu-usage");
|
kernel.triggerSense("cpu-usage");
|
||||||
|
|
||||||
// Check health
|
|
||||||
const health = kernel.getHealth();
|
const health = kernel.getHealth();
|
||||||
|
|
||||||
// Graceful shutdown
|
|
||||||
await kernel.stop();
|
await kernel.stop();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`createKernel(config, nerveRoot, options?)` — `config` is a parsed `NerveConfig`; `nerveRoot` is the workspace root (contains `nerve.yaml`, `data/`, etc.). Optional `KernelOptions`:
|
||||||
|
|
||||||
|
| Field | Meaning |
|
||||||
|
|-------|---------|
|
||||||
|
| `workerScript` | Override path to the sense worker entry script (defaults to the package’s resolved worker) |
|
||||||
|
| `enableFileWatcher` | Watch config / senses / workflows for hot reload |
|
||||||
|
| `logStore` | Inject a `LogStore` instance (defaults to `createLogStore(join(nerveRoot, "data", "logs.db"))`) |
|
||||||
|
| `ipcSocketPath` | When non-null, listen for daemon IPC on this Unix socket path |
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@uncaged/nerve-daemon",
|
"name": "@uncaged/nerve-daemon",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -235,7 +235,6 @@ describe("WorkflowManager — crash recovery (Phase 3)", () => {
|
|||||||
expect(resumeCalls[0][0]).toMatchObject({
|
expect(resumeCalls[0][0]).toMatchObject({
|
||||||
type: "resume-thread",
|
type: "resume-thread",
|
||||||
runId: "run-started-1",
|
runId: "run-started-1",
|
||||||
triggerPayload: { trigger: "initial" },
|
|
||||||
});
|
});
|
||||||
expect(Array.isArray((resumeCalls[0][0] as Record<string, unknown>).messages)).toBe(true);
|
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 mgr = createWorkflowManager("/nerve-root", config, logStore);
|
||||||
|
|
||||||
const payload = { prompt: "build-docker for myrepo", maxRounds: 10 };
|
const launch = { prompt: "build-docker for myrepo", maxRounds: 10 };
|
||||||
mgr.startWorkflow("my-wf", payload);
|
mgr.startWorkflow("my-wf", launch);
|
||||||
|
|
||||||
const startedCall = logStore.upsertWorkflowRun.mock.calls.find(
|
const startedCall = logStore.upsertWorkflowRun.mock.calls.find(
|
||||||
(args: any[]) => (args[0] as { type: string }).type === "started",
|
(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 };
|
const logEntry = startedCall?.[0] as { payload: string | null };
|
||||||
expect(logEntry.payload).not.toBeNull();
|
expect(logEntry.payload).not.toBeNull();
|
||||||
const parsed = JSON.parse(logEntry.payload as string) as Record<string, unknown>;
|
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();
|
const stopPromise = mgr.stop();
|
||||||
await vi.runAllTimersAsync();
|
await vi.runAllTimersAsync();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* Unit + integration tests for daemon-ipc.ts — trigger-sense request type.
|
* Unit + integration tests for daemon-ipc.ts — trigger-sense request type.
|
||||||
*
|
*
|
||||||
* Tests cover:
|
* 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
|
* - createDaemonIpcServer routes trigger-sense to opts.triggerSense
|
||||||
* - Error response when triggerSense throws (unknown sense)
|
* - Error response when triggerSense throws (unknown sense)
|
||||||
* - Success response on valid sense trigger
|
* - Success response on valid sense trigger
|
||||||
@@ -152,12 +152,16 @@ describe("daemon-ipc — trigger-sense", () => {
|
|||||||
const resp = await sendRaw(sockPath, {
|
const resp = await sendRaw(sockPath, {
|
||||||
type: "trigger-workflow",
|
type: "trigger-workflow",
|
||||||
workflow: "my-workflow",
|
workflow: "my-workflow",
|
||||||
payload: {},
|
prompt: "test prompt",
|
||||||
|
maxRounds: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(resp).toEqual({ ok: true });
|
expect(resp).toEqual({ ok: true });
|
||||||
expect(triggerSense).not.toHaveBeenCalled();
|
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 () => {
|
it("responds ok:false for completely unknown request type", async () => {
|
||||||
|
|||||||
@@ -116,14 +116,14 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sense signal triggers workflow via reflex", () => {
|
describe("sense compute triggers workflow via return value", () => {
|
||||||
it("calls workflowManager.startWorkflow when a sense signal fires on a workflow reflex", async () => {
|
it("calls workflowManager.startWorkflow when a sense compute returns a workflow launch", async () => {
|
||||||
const logStore = makeLogStore();
|
const logStore = makeLogStore();
|
||||||
const config = makeConfig({
|
const config = makeConfig({
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "my-workflow": { concurrency: 2, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -132,14 +132,20 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Emit a signal from "cpu-usage" on the bus
|
// Simulate a sense worker sending a signal with workflow launch payload
|
||||||
const { createSignalBus } = await import("../signal-bus.js");
|
// The kernel's handleWorkerMessage processes "signal" type messages
|
||||||
void createSignalBus; // ensure import resolves
|
// and uses routeSenseComputeOutput to detect workflow launches
|
||||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: { value: 80 }, ts: Date.now() });
|
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)
|
// A workflow worker should be spawned and a start-thread message sent
|
||||||
// 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
|
|
||||||
const workflowWorker = mockChildren.find((c) =>
|
const workflowWorker = mockChildren.find((c) =>
|
||||||
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
||||||
(args: unknown[]) =>
|
(args: unknown[]) =>
|
||||||
@@ -155,13 +161,13 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
await stopPromise;
|
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 logStore = makeLogStore();
|
||||||
const config = makeConfig({
|
const config = makeConfig({
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "alert-workflow": { concurrency: 1, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -170,8 +176,15 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
const payload = { level: "critical", value: 99 };
|
// Simulate sense worker returning a workflow launch
|
||||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload, ts: Date.now() });
|
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
|
// Find the start-thread call and verify triggerPayload
|
||||||
const startThreadCall = mockChildren
|
const startThreadCall = mockChildren
|
||||||
@@ -187,7 +200,8 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
expect(startThreadCall?.[0]).toMatchObject({
|
expect(startThreadCall?.[0]).toMatchObject({
|
||||||
type: "start-thread",
|
type: "start-thread",
|
||||||
workflow: "alert-workflow",
|
workflow: "alert-workflow",
|
||||||
triggerPayload: payload,
|
prompt: "handle critical alert",
|
||||||
|
maxRounds: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
const stopPromise = kernel.stop();
|
const stopPromise = kernel.stop();
|
||||||
@@ -202,7 +216,7 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||||
"disk-io": { 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" } },
|
workflows: { "my-workflow": { concurrency: 1, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -211,10 +225,17 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Emit signal from cpu-usage — NOT in the workflow's "on" list
|
// Emit a regular signal (no workflow field) — should NOT trigger any workflow
|
||||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: 50, ts: Date.now() });
|
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) =>
|
const workflowWorkerSpawned = mockChildren.some((c) =>
|
||||||
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
(c.send as ReturnType<typeof vi.fn>).mock.calls.some(
|
||||||
(args: unknown[]) =>
|
(args: unknown[]) =>
|
||||||
@@ -232,13 +253,13 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("workflow events are logged", () => {
|
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 logStore = makeLogStore();
|
||||||
const config = makeConfig({
|
const config = makeConfig({
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "log-test-workflow": { concurrency: 2, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -247,7 +268,15 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
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(logStore.upsertWorkflowRun).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({ source: "workflow", type: "started" }),
|
expect.objectContaining({ source: "workflow", type: "started" }),
|
||||||
@@ -261,7 +290,7 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("reloadConfig handles workflow changes", () => {
|
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 logStore = makeLogStore();
|
||||||
const initialConfig = makeConfig({
|
const initialConfig = makeConfig({
|
||||||
senses: {
|
senses: {
|
||||||
@@ -277,19 +306,26 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reload with a workflow reflex added
|
// Reload with a workflow added
|
||||||
const newConfig: NerveConfig = {
|
const newConfig: NerveConfig = {
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "new-workflow": { concurrency: 1, overflow: "drop" } },
|
||||||
maxRounds: 10,
|
maxRounds: 10,
|
||||||
};
|
};
|
||||||
kernel.reloadConfig(newConfig);
|
kernel.reloadConfig(newConfig);
|
||||||
|
|
||||||
// Now emit a signal — should trigger the new workflow
|
// Simulate sense compute returning a workflow launch for the new workflow
|
||||||
kernel.bus.emit({ id: 2, senseId: "cpu-usage", payload: "reload-test", ts: Date.now() });
|
const workerPool = mockChildren[0];
|
||||||
|
if (workerPool) {
|
||||||
|
workerPool.emit("message", {
|
||||||
|
type: "signal",
|
||||||
|
sense: "cpu-usage",
|
||||||
|
payload: { workflow: "new-workflow|10|reload test" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const startThreadCall = mockChildren
|
const startThreadCall = mockChildren
|
||||||
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
||||||
@@ -308,13 +344,13 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
await stopPromise;
|
await stopPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("old workflow reflexes are removed after reloadConfig", async () => {
|
it("old workflows are removed after reloadConfig", async () => {
|
||||||
const logStore = makeLogStore();
|
const logStore = makeLogStore();
|
||||||
const initialConfig = makeConfig({
|
const initialConfig = makeConfig({
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "old-workflow": { concurrency: 1, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -323,7 +359,7 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reload with the workflow reflex removed
|
// Reload with the workflow removed
|
||||||
const newConfig: NerveConfig = {
|
const newConfig: NerveConfig = {
|
||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
||||||
@@ -339,8 +375,15 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
(c.send as ReturnType<typeof vi.fn>).mockClear();
|
(c.send as ReturnType<typeof vi.fn>).mockClear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emit a signal — old-workflow should NOT be triggered
|
// Simulate sense compute trying to launch the old workflow — it should still not start
|
||||||
kernel.bus.emit({ id: 3, senseId: "cpu-usage", payload: "after-reload", ts: Date.now() });
|
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
|
const startThreadCall = mockChildren
|
||||||
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
.flatMap((c) => (c.send as ReturnType<typeof vi.fn>).mock.calls as [unknown][])
|
||||||
@@ -366,7 +409,7 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "shutdown-test": { concurrency: 1, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -375,8 +418,15 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
logStore,
|
logStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Trigger a workflow so a worker is spawned
|
// Trigger a workflow via sense compute return value
|
||||||
kernel.bus.emit({ id: 1, senseId: "cpu-usage", payload: null, ts: Date.now() });
|
const workerPool = mockChildren[0];
|
||||||
|
if (workerPool) {
|
||||||
|
workerPool.emit("message", {
|
||||||
|
type: "signal",
|
||||||
|
sense: "cpu-usage",
|
||||||
|
payload: { workflow: "shutdown-test|10|test" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const stopPromise = kernel.stop();
|
const stopPromise = kernel.stop();
|
||||||
await vi.runAllTimersAsync();
|
await vi.runAllTimersAsync();
|
||||||
@@ -408,7 +458,7 @@ describe("kernel + workflowManager integration", () => {
|
|||||||
senses: {
|
senses: {
|
||||||
"cpu-usage": { group: "system", throttle: null, timeout: null, gracePeriod: null },
|
"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" } },
|
workflows: { "health-wf": { concurrency: 2, overflow: "drop" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,77 +2,24 @@
|
|||||||
* Daemon IPC server — listens on a Unix domain socket so that the CLI
|
* 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.
|
* can send commands (e.g. trigger-workflow, trigger-sense) to the running daemon process.
|
||||||
*
|
*
|
||||||
* Protocol: newline-delimited JSON messages.
|
* Protocol: newline-delimited JSON — request/response types and
|
||||||
* Each request: { type: "trigger-workflow"; workflow: string; payload: unknown }
|
* `parseDaemonIpcRequest` live in `@uncaged/nerve-core`.
|
||||||
* | { type: "trigger-sense"; sense: string }
|
|
||||||
* | { type: "list-senses" }
|
|
||||||
* Each response: { ok: true } | { ok: false; error: string }
|
|
||||||
* | { ok: true; senses: SenseInfo[] } (for list-senses)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { rmSync } from "node:fs";
|
import { rmSync } from "node:fs";
|
||||||
import { type Server, type Socket, createServer } from "node:net";
|
import { type Server, type Socket, createServer } from "node:net";
|
||||||
|
|
||||||
import type { SenseInfo } 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";
|
import type { WorkflowManager } from "./workflow-manager.js";
|
||||||
|
|
||||||
export type { SenseInfo };
|
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 = {
|
export type DaemonIpcServer = {
|
||||||
close: () => Promise<void>;
|
close: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
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>;
|
|
||||||
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 };
|
|
||||||
}
|
|
||||||
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 = {
|
export type DaemonIpcServerOptions = {
|
||||||
/** Called when a trigger-sense request arrives. Should throw if the sense is unknown. */
|
/** Called when a trigger-sense request arrives. Should throw if the sense is unknown. */
|
||||||
triggerSense: (senseName: string) => void;
|
triggerSense: (senseName: string) => void;
|
||||||
@@ -96,30 +43,36 @@ export function createDaemonIpcServer(
|
|||||||
const trimmed = line.trim();
|
const trimmed = line.trim();
|
||||||
if (trimmed.length === 0) return;
|
if (trimmed.length === 0) return;
|
||||||
|
|
||||||
const req = parseRequest(trimmed);
|
const req = parseDaemonIpcRequest(trimmed);
|
||||||
if (req === null) {
|
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`);
|
socket.write(`${JSON.stringify(resp)}\n`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (req.type === "trigger-workflow") {
|
if (req.type === "trigger-workflow") {
|
||||||
workflowManager.startWorkflow(req.workflow, { prompt: req.prompt, maxRounds: req.maxRounds });
|
workflowManager.startWorkflow(req.workflow, {
|
||||||
const resp: DaemonResponse = { ok: true };
|
prompt: req.prompt,
|
||||||
|
maxRounds: req.maxRounds,
|
||||||
|
});
|
||||||
|
const resp: DaemonIpcResponse = { ok: true };
|
||||||
socket.write(`${JSON.stringify(resp)}\n`);
|
socket.write(`${JSON.stringify(resp)}\n`);
|
||||||
} else if (req.type === "trigger-sense") {
|
} else if (req.type === "trigger-sense") {
|
||||||
opts.triggerSense(req.sense);
|
opts.triggerSense(req.sense);
|
||||||
const resp: DaemonResponse = { ok: true };
|
const resp: DaemonIpcResponse = { ok: true };
|
||||||
socket.write(`${JSON.stringify(resp)}\n`);
|
socket.write(`${JSON.stringify(resp)}\n`);
|
||||||
} else if (req.type === "list-senses") {
|
} else if (req.type === "list-senses") {
|
||||||
const senses = opts.listSenses();
|
const senses = opts.listSenses();
|
||||||
const resp: DaemonResponse = { ok: true, senses };
|
const resp: DaemonIpcResponse = { ok: true, senses };
|
||||||
socket.write(`${JSON.stringify(resp)}\n`);
|
socket.write(`${JSON.stringify(resp)}\n`);
|
||||||
|
} else {
|
||||||
|
const _exhaustive: never = req;
|
||||||
|
void _exhaustive;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const msg = err instanceof Error ? err.message : String(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`);
|
socket.write(`${JSON.stringify(resp)}\n`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export {
|
|||||||
export { createKernel } from "./kernel.js";
|
export { createKernel } from "./kernel.js";
|
||||||
export type { Kernel, KernelOptions, KernelHealth } 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 { createFileWatcher } from "./file-watcher.js";
|
||||||
export type { FileWatcher, FileChange, FileChangeHandler } from "./file-watcher.js";
|
export type { FileWatcher, FileChange, FileChangeHandler } from "./file-watcher.js";
|
||||||
|
|||||||
+96
-43
@@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import type { Result } from "@uncaged/nerve-core";
|
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 */
|
/** Parent → Worker: trigger one compute cycle for a sense */
|
||||||
export type ComputeMessage = {
|
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. */
|
/** Validate and parse an unknown IPC message received from the parent process. */
|
||||||
export function parseParentMessage(raw: unknown): Result<ParentToWorkerMessage> {
|
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"));
|
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") {
|
if (typeof obj.type !== "string") {
|
||||||
return err(new Error("IPC message missing string 'type' field"));
|
return err(new Error("IPC message missing string 'type' field"));
|
||||||
}
|
}
|
||||||
if (!PARENT_MSG_TYPES.has(obj.type)) {
|
if (!PARENT_MSG_TYPES.has(obj.type)) {
|
||||||
return err(new Error(`Unknown IPC message type: "${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") {
|
if (obj.type === "start-thread") {
|
||||||
const errMsg = validateStartThreadMsg(obj);
|
const errMsg = validateStartThreadMsg(obj);
|
||||||
if (errMsg !== null) return err(new Error(errMsg));
|
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") {
|
if (obj.type === "resume-thread") {
|
||||||
const errMsg = validateResumeThreadMsg(obj);
|
const errMsg = validateResumeThreadMsg(obj);
|
||||||
if (errMsg !== null) return err(new Error(errMsg));
|
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") {
|
if (typeof obj.sense !== "string") {
|
||||||
return err(new Error("Worker 'signal' message missing string 'sense' field"));
|
return err(new Error("Worker 'signal' message missing string 'sense' field"));
|
||||||
}
|
}
|
||||||
if (!("payload" in obj)) {
|
if (!("payload" in obj)) {
|
||||||
return err(new Error("Worker 'signal' message missing 'payload' field"));
|
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") {
|
if (typeof obj.sense !== "string") {
|
||||||
return err(new Error("Worker 'error' message missing string 'sense' field"));
|
return err(new Error("Worker 'error' message missing string 'sense' field"));
|
||||||
}
|
}
|
||||||
if (typeof obj.error !== "string") {
|
if (typeof obj.error !== "string") {
|
||||||
return err(new Error("Worker 'error' message missing string 'error' field"));
|
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(
|
function parseHealthResponseMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||||
obj: Record<string, unknown>,
|
|
||||||
raw: unknown,
|
|
||||||
): Result<WorkerToParentMessage> {
|
|
||||||
if (!Array.isArray(obj.senses)) {
|
if (!Array.isArray(obj.senses)) {
|
||||||
return err(new Error("Worker 'health-response' message missing 'senses' array"));
|
return err(new Error("Worker 'health-response' message missing 'senses' array"));
|
||||||
}
|
}
|
||||||
if (typeof obj.inFlightCount !== "number") {
|
if (typeof obj.inFlightCount !== "number") {
|
||||||
return err(new Error("Worker 'health-response' message missing '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>([
|
function isThreadEventType(value: string): value is ThreadEventType {
|
||||||
"queued",
|
switch (value) {
|
||||||
"started",
|
case "queued":
|
||||||
"step_complete",
|
case "started":
|
||||||
"completed",
|
case "step_complete":
|
||||||
"failed",
|
case "completed":
|
||||||
]);
|
case "failed":
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function parseThreadEventMsg(
|
function parseThreadEventMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||||
obj: Record<string, unknown>,
|
|
||||||
raw: unknown,
|
|
||||||
): Result<WorkerToParentMessage> {
|
|
||||||
if (typeof obj.runId !== "string") {
|
if (typeof obj.runId !== "string") {
|
||||||
return err(new Error("Worker 'thread-event' message missing string 'runId' field"));
|
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(
|
return err(
|
||||||
new Error(`Worker 'thread-event' message has invalid 'eventType': "${obj.eventType}"`),
|
new Error(`Worker 'thread-event' message has invalid 'eventType': "${obj.eventType}"`),
|
||||||
);
|
);
|
||||||
@@ -225,20 +264,26 @@ function parseThreadEventMsg(
|
|||||||
if (!("payload" in obj)) {
|
if (!("payload" in obj)) {
|
||||||
return err(new Error("Worker 'thread-event' message missing 'payload' field"));
|
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(
|
function parseWorkflowErrorMsg(obj: Record<string, unknown>): Result<WorkerToParentMessage> {
|
||||||
obj: Record<string, unknown>,
|
|
||||||
raw: unknown,
|
|
||||||
): Result<WorkerToParentMessage> {
|
|
||||||
if (typeof obj.runId !== "string") {
|
if (typeof obj.runId !== "string") {
|
||||||
return err(new Error("Worker 'workflow-error' message missing string 'runId' field"));
|
return err(new Error("Worker 'workflow-error' message missing string 'runId' field"));
|
||||||
}
|
}
|
||||||
if (typeof obj.error !== "string") {
|
if (typeof obj.error !== "string") {
|
||||||
return err(new Error("Worker 'workflow-error' message missing string 'error' field"));
|
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([
|
const WORKER_MSG_TYPES = new Set([
|
||||||
@@ -253,15 +298,14 @@ const WORKER_MSG_TYPES = new Set([
|
|||||||
|
|
||||||
function parseThreadWorkflowMessageMsg(
|
function parseThreadWorkflowMessageMsg(
|
||||||
obj: Record<string, unknown>,
|
obj: Record<string, unknown>,
|
||||||
raw: unknown,
|
|
||||||
): Result<WorkerToParentMessage> {
|
): Result<WorkerToParentMessage> {
|
||||||
if (typeof obj.runId !== "string") {
|
if (typeof obj.runId !== "string") {
|
||||||
return err(new Error("Worker 'thread-workflow-message' missing string 'runId' field"));
|
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"));
|
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") {
|
if (typeof msg.role !== "string") {
|
||||||
return err(new Error("Worker 'thread-workflow-message' message missing string 'role' field"));
|
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"),
|
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. */
|
/** Validate and parse an unknown IPC message received from a worker process. */
|
||||||
export function parseWorkerMessage(raw: unknown): Result<WorkerToParentMessage> {
|
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"));
|
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") {
|
if (typeof obj.type !== "string") {
|
||||||
return err(new Error("Worker IPC message missing string 'type' field"));
|
return err(new Error("Worker IPC message missing string 'type' field"));
|
||||||
}
|
}
|
||||||
if (!WORKER_MSG_TYPES.has(obj.type)) {
|
if (!WORKER_MSG_TYPES.has(obj.type)) {
|
||||||
return err(new Error(`Unknown worker IPC message type: "${obj.type}"`));
|
return err(new Error(`Unknown worker IPC message type: "${obj.type}"`));
|
||||||
}
|
}
|
||||||
if (obj.type === "signal") return parseSignalMsg(obj, raw);
|
if (obj.type === "signal") return parseSignalMsg(obj);
|
||||||
if (obj.type === "error") return parseErrorMsg(obj, raw);
|
if (obj.type === "error") return parseErrorMsg(obj);
|
||||||
if (obj.type === "health-response") return parseHealthResponseMsg(obj, raw);
|
if (obj.type === "health-response") return parseHealthResponseMsg(obj);
|
||||||
if (obj.type === "thread-event") return parseThreadEventMsg(obj, raw);
|
if (obj.type === "thread-event") return parseThreadEventMsg(obj);
|
||||||
if (obj.type === "workflow-error") return parseWorkflowErrorMsg(obj, raw);
|
if (obj.type === "workflow-error") return parseWorkflowErrorMsg(obj);
|
||||||
if (obj.type === "thread-workflow-message") return parseThreadWorkflowMessageMsg(obj, raw);
|
if (obj.type === "thread-workflow-message") return parseThreadWorkflowMessageMsg(obj);
|
||||||
return ok({ type: "ready" });
|
return ok({ type: "ready" });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,7 +100,8 @@ export function createKernel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let stopped = false;
|
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;
|
let readyResolve: (() => void) | undefined;
|
||||||
const ready = new Promise<void>((resolve) => {
|
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 { NodeSQLiteDatabase } from "drizzle-orm/node-sqlite";
|
||||||
|
|
||||||
import type { Result } from "@uncaged/nerve-core";
|
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";
|
import type { BlobStore } from "@uncaged/nerve-store";
|
||||||
|
|
||||||
@@ -108,10 +108,11 @@ export function runMigrations(sqlite: DatabaseSync, migrationsDir: string): Resu
|
|||||||
const filesResult = listMigrationFiles(migrationsDir);
|
const filesResult = listMigrationFiles(migrationsDir);
|
||||||
if (!filesResult.ok) return filesResult;
|
if (!filesResult.ok) return filesResult;
|
||||||
|
|
||||||
|
const migrationRows = sqlite.prepare("SELECT name FROM _migrations").all();
|
||||||
const applied = new Set<string>(
|
const applied = new Set<string>(
|
||||||
(sqlite.prepare("SELECT name FROM _migrations").all() as Array<{ name: string }>).map(
|
migrationRows
|
||||||
(r) => r.name,
|
.filter((r): r is { name: string } => isPlainRecord(r) && typeof r.name === "string")
|
||||||
),
|
.map((r) => r.name),
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of filesResult.value) {
|
for (const file of filesResult.value) {
|
||||||
@@ -145,6 +146,7 @@ export function openSenseDb(
|
|||||||
const migResult = runMigrations(sqlite, migrationsDir);
|
const migResult = runMigrations(sqlite, migrationsDir);
|
||||||
if (!migResult.ok) return migResult;
|
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;
|
const db = drizzle({ client: sqlite }) as DrizzleDB;
|
||||||
return ok({ sqlite, db });
|
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}`));
|
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);
|
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}`));
|
return err(new Error(`Failed to import sense module "${senseIndexPath}": ${msg}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (!isPlainRecord(mod) || !("compute" in mod) || typeof mod.compute !== "function") {
|
||||||
mod === null ||
|
|
||||||
typeof mod !== "object" ||
|
|
||||||
!("compute" in mod) ||
|
|
||||||
typeof (mod as Record<string, unknown>).compute !== "function"
|
|
||||||
) {
|
|
||||||
return err(
|
return err(
|
||||||
new Error(`Sense module "${senseIndexPath}" must export a named "compute" function`),
|
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) {
|
} catch (e) {
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
if (controller.signal.aborted) {
|
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}`));
|
return err(new Error(`compute("${runtime.name}") threw: ${msg}`));
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ import { dirname, join } from "node:path";
|
|||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
import type { NerveConfig, WorkflowConfig, WorkflowMessage } from "@uncaged/nerve-core";
|
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 {
|
import type {
|
||||||
ResumeThreadMessage,
|
ResumeThreadMessage,
|
||||||
ShutdownMessage,
|
ShutdownMessage,
|
||||||
@@ -21,7 +22,6 @@ import type {
|
|||||||
ThreadEventMessage,
|
ThreadEventMessage,
|
||||||
} from "./ipc.js";
|
} from "./ipc.js";
|
||||||
import { parseWorkerMessage } from "./ipc.js";
|
import { parseWorkerMessage } from "./ipc.js";
|
||||||
import type { LogStore, WorkflowRunStatus } from "@uncaged/nerve-store";
|
|
||||||
import {
|
import {
|
||||||
formatCapturedStderrTail,
|
formatCapturedStderrTail,
|
||||||
formatChildExitSummary,
|
formatChildExitSummary,
|
||||||
@@ -91,8 +91,8 @@ function readLaunchFromTriggerPayload(
|
|||||||
raw: unknown,
|
raw: unknown,
|
||||||
engineDefaultMaxRounds: number,
|
engineDefaultMaxRounds: number,
|
||||||
): { prompt: string; maxRounds: number } {
|
): { prompt: string; maxRounds: number } {
|
||||||
if (raw !== null && typeof raw === "object" && !Array.isArray(raw)) {
|
if (isPlainRecord(raw)) {
|
||||||
const o = raw as Record<string, unknown>;
|
const o = raw;
|
||||||
if (typeof o.prompt === "string" && typeof o.maxRounds === "number") {
|
if (typeof o.prompt === "string" && typeof o.maxRounds === "number") {
|
||||||
return { prompt: o.prompt, maxRounds: o.maxRounds };
|
return { prompt: o.prompt, maxRounds: o.maxRounds };
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,10 @@ export function createWorkflowManager(
|
|||||||
|
|
||||||
function recoverQueuedRun(workflowName: string, runId: string, state: WorkflowState): void {
|
function recoverQueuedRun(workflowName: string, runId: string, state: WorkflowState): void {
|
||||||
if (state.queue.some((q) => q.runId === runId)) return;
|
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 });
|
state.queue.push({ runId, prompt: launch.prompt, maxRounds: launch.maxRounds });
|
||||||
process.stderr.write(
|
process.stderr.write(
|
||||||
`[workflow-manager] crash-recovery: re-queued thread "${runId}" for "${workflowName}"\n`,
|
`[workflow-manager] crash-recovery: re-queued thread "${runId}" for "${workflowName}"\n`,
|
||||||
@@ -322,7 +325,10 @@ export function createWorkflowManager(
|
|||||||
): void {
|
): void {
|
||||||
if (state.active.has(runId)) return;
|
if (state.active.has(runId)) return;
|
||||||
const rawMessages = logStore.getThreadMessages(runId);
|
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);
|
const messages = ensureThreadMessagesWithStart(rawMessages, launch.prompt, launch.maxRounds);
|
||||||
state.active.add(runId);
|
state.active.add(runId);
|
||||||
const msg: ResumeThreadMessage = {
|
const msg: ResumeThreadMessage = {
|
||||||
|
|||||||
@@ -12,8 +12,14 @@
|
|||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import { join, resolve } from "node:path";
|
import { join, resolve } from "node:path";
|
||||||
|
|
||||||
import type { RoleMeta, WorkflowDefinition, WorkflowMessage } from "@uncaged/nerve-core";
|
import type {
|
||||||
import { END, START } from "@uncaged/nerve-core";
|
Moderator,
|
||||||
|
RoleMeta,
|
||||||
|
StartSignal,
|
||||||
|
WorkflowDefinition,
|
||||||
|
WorkflowMessage,
|
||||||
|
} from "@uncaged/nerve-core";
|
||||||
|
import { END, START, isPlainRecord } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
ThreadEventType,
|
ThreadEventType,
|
||||||
@@ -23,6 +29,8 @@ import type {
|
|||||||
import { parseParentMessage } from "./ipc.js";
|
import { parseParentMessage } from "./ipc.js";
|
||||||
import { ignoreSessionBroadcastSignals } from "./worker-fork-support.js";
|
import { ignoreSessionBroadcastSignals } from "./worker-fork-support.js";
|
||||||
|
|
||||||
|
type ModeratorInput = Parameters<Moderator<RoleMeta>>[0];
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// IPC helpers
|
// IPC helpers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -63,18 +71,43 @@ function sendWorkflowMessage(runId: string, message: WorkflowMessage): void {
|
|||||||
// Thread loop (signal-driven automaton, issue #80)
|
// Thread loop (signal-driven automaton, issue #80)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async function runThread(
|
function validateRoleResult(
|
||||||
def: WorkflowDefinition<RoleMeta>,
|
result: { content: string; meta: Record<string, unknown> },
|
||||||
|
roleName: string,
|
||||||
runId: string,
|
runId: string,
|
||||||
maxRounds: number,
|
): boolean {
|
||||||
resumeMessages: WorkflowMessage[] = [],
|
if (typeof result.content !== "string") {
|
||||||
freshPrompt: string | null = null,
|
sendWorkflowError(runId, `Role "${roleName}" returned non-string content`);
|
||||||
): Promise<void> {
|
return false;
|
||||||
let chain: WorkflowMessage[];
|
}
|
||||||
|
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) {
|
if (resumeMessages.length > 0) {
|
||||||
chain = [...resumeMessages];
|
return [...resumeMessages];
|
||||||
} else {
|
}
|
||||||
const prompt = freshPrompt ?? "";
|
const prompt = freshPrompt ?? "";
|
||||||
const startMsg: WorkflowMessage = {
|
const startMsg: WorkflowMessage = {
|
||||||
role: START,
|
role: START,
|
||||||
@@ -82,43 +115,20 @@ async function runThread(
|
|||||||
meta: { maxRounds },
|
meta: { maxRounds },
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
};
|
};
|
||||||
chain = [startMsg];
|
|
||||||
sendWorkflowMessage(runId, startMsg);
|
sendWorkflowMessage(runId, startMsg);
|
||||||
}
|
return [startMsg];
|
||||||
|
}
|
||||||
|
|
||||||
let roleRound = chain.filter((m) => m.role !== START).length;
|
async function executeRole(
|
||||||
const lastMsg = chain[chain.length - 1];
|
def: WorkflowDefinition<RoleMeta>,
|
||||||
if (lastMsg === undefined) {
|
nextRole: string,
|
||||||
sendWorkflowError(runId, "empty workflow message chain");
|
chain: WorkflowMessage[],
|
||||||
return;
|
runId: string,
|
||||||
}
|
): Promise<{ content: string; meta: Record<string, unknown> } | null> {
|
||||||
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (nextRole === END) {
|
|
||||||
sendThreadEvent(runId, "completed", null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (roleRound < maxRounds) {
|
|
||||||
const role = def.roles[nextRole];
|
const role = def.roles[nextRole];
|
||||||
if (!role) {
|
if (!role) {
|
||||||
sendWorkflowError(runId, `Unknown role: ${nextRole}`);
|
sendWorkflowError(runId, `Unknown role: ${nextRole}`);
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: { content: string; meta: Record<string, unknown> };
|
let result: { content: string; meta: Record<string, unknown> };
|
||||||
@@ -127,18 +137,40 @@ async function runThread(
|
|||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
const errMsg = e instanceof Error ? e.message : String(e);
|
const errMsg = e instanceof Error ? e.message : String(e);
|
||||||
sendThreadEvent(runId, "failed", { error: errMsg });
|
sendThreadEvent(runId, "failed", { error: errMsg });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validateRoleResult(result, nextRole, runId)) return null;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runThread(
|
||||||
|
def: WorkflowDefinition<RoleMeta>,
|
||||||
|
runId: string,
|
||||||
|
maxRounds: number,
|
||||||
|
resumeMessages: WorkflowMessage[] = [],
|
||||||
|
freshPrompt: string | null = null,
|
||||||
|
): Promise<void> {
|
||||||
|
const chain = initChain(runId, resumeMessages, freshPrompt, maxRounds);
|
||||||
|
|
||||||
|
let roleRound = chain.filter((m) => m.role !== START).length;
|
||||||
|
const lastMsg = chain[chain.length - 1];
|
||||||
|
if (lastMsg === undefined) {
|
||||||
|
sendWorkflowError(runId, "empty workflow message chain");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof result.content !== "string") {
|
let nextRole = def.moderator(buildInitialLastSignal(lastMsg), roleRound, maxRounds);
|
||||||
sendWorkflowError(runId, `Role "${nextRole}" returned non-string content`);
|
|
||||||
return;
|
if (nextRole === END) {
|
||||||
}
|
sendThreadEvent(runId, "completed", null);
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (roleRound < maxRounds) {
|
||||||
|
const result = await executeRole(def, nextRole, chain, runId);
|
||||||
|
if (result === null) return;
|
||||||
|
|
||||||
const message: WorkflowMessage = {
|
const message: WorkflowMessage = {
|
||||||
role: nextRole,
|
role: nextRole,
|
||||||
content: result.content,
|
content: result.content,
|
||||||
@@ -150,8 +182,8 @@ async function runThread(
|
|||||||
|
|
||||||
roleRound += 1;
|
roleRound += 1;
|
||||||
|
|
||||||
const signal = { role: nextRole, meta: result.meta };
|
const signal: ModeratorInput = { role: nextRole, meta: result.meta };
|
||||||
nextRole = def.moderator(signal as Parameters<typeof def.moderator>[0], roleRound, maxRounds);
|
nextRole = def.moderator(signal, roleRound, maxRounds);
|
||||||
|
|
||||||
if (nextRole === END) {
|
if (nextRole === END) {
|
||||||
sendThreadEvent(runId, "completed", null);
|
sendThreadEvent(runId, "completed", null);
|
||||||
@@ -166,6 +198,17 @@ async function runThread(
|
|||||||
// Workflow definition loader
|
// 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(
|
async function loadWorkflowDefinition(
|
||||||
nerveRoot: string,
|
nerveRoot: string,
|
||||||
workflowName: string,
|
workflowName: string,
|
||||||
@@ -186,19 +229,13 @@ async function loadWorkflowDefinition(
|
|||||||
const mod = await import(indexPath);
|
const mod = await import(indexPath);
|
||||||
const def: unknown = mod.default ?? mod;
|
const def: unknown = mod.default ?? mod;
|
||||||
|
|
||||||
if (
|
if (!isWorkflowDefinitionShape(def)) {
|
||||||
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"
|
|
||||||
) {
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Workflow "${workflowName}" must export a WorkflowDefinition with "name", "roles", and "moderator".`,
|
`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 previous = inFlight.get(runId) ?? Promise.resolve();
|
||||||
const next = previous
|
const next = previous
|
||||||
.then(() => runThread(def, runId, maxRounds, messages as WorkflowMessage[], null))
|
.then(() => runThread(def, runId, maxRounds, messages, null))
|
||||||
.catch((e: unknown) => {
|
.catch((e: unknown) => {
|
||||||
const errMsg = e instanceof Error ? e.message : String(e);
|
const errMsg = e instanceof Error ? e.message : String(e);
|
||||||
sendWorkflowError(runId, errMsg);
|
sendWorkflowError(runId, errMsg);
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# @uncaged/nerve-store
|
||||||
|
|
||||||
|
Persistent storage for the [nerve](../../README.md) daemon — append-only structured logs, optional JSONL cold archive, and content-addressable blobs.
|
||||||
|
|
||||||
|
## LogStore (`createLogStore`, `log-store.ts`)
|
||||||
|
|
||||||
|
- **Append-only log table** — rows with `source`, `type`, `refId`, `payload`, `ts` (string payloads for ad hoc fields)
|
||||||
|
- **SQLite WAL** — `DatabaseSync` from `node:sqlite`
|
||||||
|
- **Workflow run tracking** — materialized `workflow_runs` table plus helpers to list active runs, upsert status transitions, and read **thread messages** / **role rounds** for CLI and crash recovery
|
||||||
|
- **Meta key-value** — small `meta` table (e.g. archive watermarks)
|
||||||
|
|
||||||
|
Public exports include `LogStore`, `LogEntry`, `LogQuery`, `WorkflowRun`, `WorkflowRunStatus`, `ThreadRoundRow`, `GetThreadRoundsParams`, and archive-related types re-exported from `log-archive`.
|
||||||
|
|
||||||
|
## WorkflowRunStatus
|
||||||
|
|
||||||
|
Runs progress through a small state machine. Typical paths:
|
||||||
|
|
||||||
|
1. **`queued`** → **`started`** when a worker picks up the thread
|
||||||
|
2. **`started`** → **`completed`** | **`failed`** | **`crashed`** | **`interrupted`** | **`dropped`**
|
||||||
|
|
||||||
|
Semantics in the daemon/store layer:
|
||||||
|
|
||||||
|
- **`completed` / `failed`** — normal terminal outcomes from the workflow worker
|
||||||
|
- **`crashed`** — worker exited unexpectedly; manager may respawn and **`resume-thread`** eligible **`started`** runs
|
||||||
|
- **`interrupted`** — e.g. hot-reload drain killed an in-flight thread after timeout
|
||||||
|
- **`dropped`** — concurrency **`overflow: drop`** rejected a new run, or **`overflow: queue`** evicted an queued item when the queue was full
|
||||||
|
|
||||||
|
## LogArchive (`log-archive.ts`)
|
||||||
|
|
||||||
|
- **`archiveLogs`** / helpers — export eligible UTC days of old rows to **`data/archive/logs/YYYY-MM-DD.jsonl`**, delete archived rows from SQLite, optional **`VACUUM`**
|
||||||
|
- Used by **`nerve store archive`** in `@uncaged/nerve-cli`
|
||||||
|
|
||||||
|
## BlobStore (`createBlobStore`, `blob-store.ts`)
|
||||||
|
|
||||||
|
- **Content-addressable storage** — `write` returns lowercase **sha256** hex; files live under **`data/blobs/<2-hex>/<62-hex>`**
|
||||||
|
- **`read` / `exists`** — path must match digest on disk (tamper detection)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm add @uncaged/nerve-store
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Node.js ≥ 22.5 (same as the rest of the stack).
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@uncaged/nerve-store",
|
"name": "@uncaged/nerve-store",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import { mkdirSync, writeFileSync } from "node:fs";
|
|||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
import { DatabaseSync, type StatementSync } from "node:sqlite";
|
import { DatabaseSync, type StatementSync } from "node:sqlite";
|
||||||
|
|
||||||
|
import { isPlainRecord } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_LOG_RETENTION_MS,
|
DEFAULT_LOG_RETENTION_MS,
|
||||||
LOG_ARCHIVE_META_KEY,
|
LOG_ARCHIVE_META_KEY,
|
||||||
@@ -68,11 +70,15 @@ const VALID_WORKFLOW_STATUSES = new Set<string>([
|
|||||||
"interrupted",
|
"interrupted",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
function isWorkflowRunStatus(value: string): value is WorkflowRunStatus {
|
||||||
|
return VALID_WORKFLOW_STATUSES.has(value);
|
||||||
|
}
|
||||||
|
|
||||||
function validateWorkflowRunStatus(status: string): WorkflowRunStatus {
|
function validateWorkflowRunStatus(status: string): WorkflowRunStatus {
|
||||||
if (!VALID_WORKFLOW_STATUSES.has(status)) {
|
if (!isWorkflowRunStatus(status)) {
|
||||||
throw new Error(`Invalid workflow run status from DB: "${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. */
|
/** 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;
|
const row = getTriggerPayloadStmt.get(runId) as { payload: string | null } | undefined;
|
||||||
if (row === undefined || row.payload === null) return null;
|
if (row === undefined || row.payload === null) return null;
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(row.payload) as unknown;
|
const parsed: unknown = JSON.parse(row.payload);
|
||||||
if (parsed !== null && typeof parsed === "object") {
|
if (isPlainRecord(parsed)) {
|
||||||
const obj = parsed as Record<string, unknown>;
|
return parsed.triggerPayload ?? null;
|
||||||
return obj.triggerPayload ?? null;
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// malformed
|
// malformed
|
||||||
@@ -525,12 +530,8 @@ export function createLogStore(dbPath: string): LogStore {
|
|||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
if (row.payload === null) continue;
|
if (row.payload === null) continue;
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(row.payload) as unknown;
|
const parsed: unknown = JSON.parse(row.payload);
|
||||||
if (
|
if (isPlainRecord(parsed) && typeof parsed.type === "string") {
|
||||||
parsed !== null &&
|
|
||||||
typeof parsed === "object" &&
|
|
||||||
typeof (parsed as Record<string, unknown>).type === "string"
|
|
||||||
) {
|
|
||||||
result.push(parsed as { type: string; [key: string]: unknown });
|
result.push(parsed as { type: string; [key: string]: unknown });
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -544,9 +545,9 @@ export function createLogStore(dbPath: string): LogStore {
|
|||||||
payload: string,
|
payload: string,
|
||||||
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(payload) as unknown;
|
const parsed: unknown = JSON.parse(payload);
|
||||||
if (parsed === null || typeof parsed !== "object") return null;
|
if (!isPlainRecord(parsed)) return null;
|
||||||
const obj = parsed as Record<string, unknown>;
|
const obj = parsed;
|
||||||
if (typeof obj.role !== "string" || typeof obj.content !== "string") return null;
|
if (typeof obj.role !== "string" || typeof obj.content !== "string") return null;
|
||||||
return {
|
return {
|
||||||
role: obj.role,
|
role: obj.role,
|
||||||
@@ -579,14 +580,10 @@ export function createLogStore(dbPath: string): LogStore {
|
|||||||
return Number(c);
|
return Number(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseRoundPayload(
|
function recordToRoundMessage(
|
||||||
payload: string,
|
obj: Record<string, unknown>,
|
||||||
fallbackTs: number,
|
fallbackTs: number,
|
||||||
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
): { 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") {
|
if (typeof obj.role === "string" && typeof obj.content === "string") {
|
||||||
return {
|
return {
|
||||||
role: obj.role,
|
role: obj.role,
|
||||||
@@ -604,6 +601,16 @@ export function createLogStore(dbPath: string): LogStore {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseRoundPayload(
|
||||||
|
payload: string,
|
||||||
|
fallbackTs: number,
|
||||||
|
): { role: string; content: string; meta: unknown; timestamp: number } | null {
|
||||||
|
try {
|
||||||
|
const parsed: unknown = JSON.parse(payload);
|
||||||
|
if (!isPlainRecord(parsed)) return null;
|
||||||
|
return recordToRoundMessage(parsed, fallbackTs);
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"name": "@uncaged/nerve-workflow-utils",
|
||||||
|
"version": "0.4.0",
|
||||||
|
"type": "module",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"files": ["dist"],
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"prepublishOnly": "bash ../../scripts/prepublish-check.sh",
|
||||||
|
"build": "rslib build",
|
||||||
|
"test": "vitest run"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-core": "workspace:*",
|
||||||
|
"zod": "^4.3.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@rslib/core": "^0.21.3",
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"vitest": "^4.1.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { defineConfig } from "@rslib/core";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
lib: [
|
||||||
|
{
|
||||||
|
format: "esm",
|
||||||
|
dts: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
source: {
|
||||||
|
entry: {
|
||||||
|
index: "src/index.ts",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
target: "node",
|
||||||
|
cleanDistPath: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { llmExtract } from "../llm-extract.js";
|
||||||
|
|
||||||
|
describe("llmExtract", () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses tool call arguments and validates with the zod schema", async () => {
|
||||||
|
const schema = z
|
||||||
|
.object({
|
||||||
|
name: z.string(),
|
||||||
|
description: z.string(),
|
||||||
|
})
|
||||||
|
.describe("Extract sense metadata from plan");
|
||||||
|
|
||||||
|
const fetchMock = vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () =>
|
||||||
|
JSON.stringify({
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
tool_calls: [
|
||||||
|
{
|
||||||
|
function: {
|
||||||
|
name: "extract",
|
||||||
|
arguments: JSON.stringify({ name: "cpu-usage", description: "CPU load" }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const result = await llmExtract({
|
||||||
|
text: "some plan",
|
||||||
|
schema,
|
||||||
|
provider: {
|
||||||
|
baseUrl: "https://example.com/v1",
|
||||||
|
apiKey: "k",
|
||||||
|
model: "m",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.ok).toBe(true);
|
||||||
|
if (!result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.value).toEqual({ name: "cpu-usage", description: "CPU load" });
|
||||||
|
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||||
|
const [, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
||||||
|
expect(init.method).toBe("POST");
|
||||||
|
expect(init.headers).toMatchObject({
|
||||||
|
Authorization: "Bearer k",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
});
|
||||||
|
const body = JSON.parse(init.body as string) as {
|
||||||
|
model: string;
|
||||||
|
tool_choice: { function: { name: string } };
|
||||||
|
};
|
||||||
|
expect(body.model).toBe("m");
|
||||||
|
expect(body.tool_choice.function.name).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns schema_validation_failed when arguments do not match the schema", async () => {
|
||||||
|
const schema = z.object({ n: z.number() });
|
||||||
|
|
||||||
|
vi.stubGlobal(
|
||||||
|
"fetch",
|
||||||
|
vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () =>
|
||||||
|
JSON.stringify({
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
tool_calls: [
|
||||||
|
{ function: { name: "extract", arguments: JSON.stringify({ n: "oops" }) } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await llmExtract({
|
||||||
|
text: "x",
|
||||||
|
schema,
|
||||||
|
provider: { baseUrl: "https://example.com", apiKey: "k", model: "m" },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.error.kind).toBe("schema_validation_failed");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { spawnSafe } from "../spawn-safe.js";
|
||||||
|
|
||||||
|
describe("spawnSafe", () => {
|
||||||
|
it("passes argv literally without shell interpretation (injection-safe)", async () => {
|
||||||
|
const injection = "$(echo BAD)";
|
||||||
|
const result = await spawnSafe(
|
||||||
|
process.execPath,
|
||||||
|
["-e", "process.stdout.write(process.argv[1] ?? '')", injection],
|
||||||
|
{ cwd: null, env: null, timeoutMs: 10_000 },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.ok).toBe(true);
|
||||||
|
if (!result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.value.stdout).toBe(injection);
|
||||||
|
expect(result.value.exitCode).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns err on non-zero exit", async () => {
|
||||||
|
const result = await spawnSafe(process.execPath, ["-e", "process.exit(7)"], {
|
||||||
|
cwd: null,
|
||||||
|
env: null,
|
||||||
|
timeoutMs: 10_000,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.error.kind).toBe("non_zero_exit");
|
||||||
|
if (result.error.kind !== "non_zero_exit") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.error.exitCode).toBe(7);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import { resolve } from "node:path";
|
||||||
|
import { type Result, err, ok } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
|
export type ReadNerveYamlOptions = {
|
||||||
|
nerveRoot: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type NerveYamlError = {
|
||||||
|
code: "PATH_TRAVERSAL" | "READ_FAILED";
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads `nerve.yaml` from a Nerve data directory (typically `~/.uncaged-nerve`).
|
||||||
|
* Returns Result to avoid throwing on expected failures (missing file, bad perms).
|
||||||
|
* Validates that the resolved path stays within nerveRoot to prevent path traversal.
|
||||||
|
*/
|
||||||
|
export function readNerveYaml(options: ReadNerveYamlOptions): Result<string, NerveYamlError> {
|
||||||
|
const root = resolve(options.nerveRoot);
|
||||||
|
const target = resolve(root, "nerve.yaml");
|
||||||
|
|
||||||
|
if (!target.startsWith(root)) {
|
||||||
|
return err({
|
||||||
|
code: "PATH_TRAVERSAL",
|
||||||
|
message: `Resolved path "${target}" escapes nerveRoot "${root}"`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return ok(readFileSync(target, "utf-8"));
|
||||||
|
} catch (e) {
|
||||||
|
return err({
|
||||||
|
code: "READ_FAILED",
|
||||||
|
message: e instanceof Error ? e.message : String(e),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared context for workflow agents: how Nerve fits together and common CLI verbs.
|
||||||
|
*/
|
||||||
|
export const nerveAgentContext = `
|
||||||
|
Nerve observes the world through **Senses** (each has its own SQLite DB and a \`compute()\` function).
|
||||||
|
**Reflexes** (YAML) schedule sense runs or start **Workflows** on intervals or signals.
|
||||||
|
The \`nerve\` CLI manages config, triggers, and queries; keep paths and commands aligned with the host nerve.yaml and senses directory.
|
||||||
|
`.trim();
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import { type Result, ok } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
|
import { type SpawnEnv, type SpawnError, spawnSafe } from "./spawn-safe.js";
|
||||||
|
|
||||||
|
export type CursorAgentMode = "plan" | "ask" | "default";
|
||||||
|
|
||||||
|
export type CursorAgentOptions = {
|
||||||
|
prompt: string;
|
||||||
|
mode: CursorAgentMode;
|
||||||
|
cwd: string;
|
||||||
|
env: SpawnEnv | null;
|
||||||
|
timeoutMs: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes `cursor-agent` with the prompt passed as a single argv slot (`shell: false`).
|
||||||
|
*/
|
||||||
|
export async function cursorAgent(
|
||||||
|
options: CursorAgentOptions,
|
||||||
|
): Promise<Result<string, SpawnError>> {
|
||||||
|
const args: string[] = [
|
||||||
|
"-p",
|
||||||
|
options.prompt,
|
||||||
|
"--model",
|
||||||
|
"auto",
|
||||||
|
"--output-format",
|
||||||
|
"text",
|
||||||
|
"--trust",
|
||||||
|
"--force",
|
||||||
|
];
|
||||||
|
if (options.mode === "plan") {
|
||||||
|
args.push("--mode=plan");
|
||||||
|
} else if (options.mode === "ask") {
|
||||||
|
args.push("--mode=ask");
|
||||||
|
}
|
||||||
|
|
||||||
|
const run = await spawnSafe("cursor-agent", args, {
|
||||||
|
cwd: options.cwd,
|
||||||
|
env: options.env,
|
||||||
|
timeoutMs: options.timeoutMs,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!run.ok) {
|
||||||
|
return run;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok(run.value.stdout);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
export { cursorAgent, type CursorAgentMode, type CursorAgentOptions } from "./cursor-agent.js";
|
||||||
|
export {
|
||||||
|
nerveAgentContext,
|
||||||
|
readNerveYaml,
|
||||||
|
type NerveYamlError,
|
||||||
|
type ReadNerveYamlOptions,
|
||||||
|
} from "./context.js";
|
||||||
|
export {
|
||||||
|
llmExtract,
|
||||||
|
type LlmError,
|
||||||
|
type LlmExtractOptions,
|
||||||
|
type LlmProvider,
|
||||||
|
} from "./llm-extract.js";
|
||||||
|
export {
|
||||||
|
nerveCommandEnv,
|
||||||
|
spawnSafe,
|
||||||
|
type SpawnEnv,
|
||||||
|
type SpawnError,
|
||||||
|
type SpawnResult,
|
||||||
|
type SpawnSafeOptions,
|
||||||
|
} from "./spawn-safe.js";
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
import { type Result, err, ok } from "@uncaged/nerve-core";
|
||||||
|
import { toJSONSchema, type z } from "zod";
|
||||||
|
|
||||||
|
export type LlmProvider = {
|
||||||
|
baseUrl: string;
|
||||||
|
apiKey: string;
|
||||||
|
model: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LlmExtractOptions<T> = {
|
||||||
|
text: string;
|
||||||
|
schema: z.ZodType<T>;
|
||||||
|
provider: LlmProvider;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LlmError =
|
||||||
|
| { kind: "http_error"; status: number; body: string }
|
||||||
|
| { kind: "invalid_response_json"; message: string }
|
||||||
|
| { kind: "no_tool_call"; preview: string }
|
||||||
|
| { kind: "tool_arguments_invalid_json"; message: string }
|
||||||
|
| { kind: "schema_validation_failed"; message: string }
|
||||||
|
| { kind: "network_error"; message: string };
|
||||||
|
|
||||||
|
function chatCompletionsUrl(baseUrl: string): string {
|
||||||
|
const trimmed = baseUrl.replace(/\/+$/, "");
|
||||||
|
return `${trimmed}/chat/completions`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stripJsonSchemaMeta(json: Record<string, unknown>): Record<string, unknown> {
|
||||||
|
const { $schema: _drop, ...rest } = json;
|
||||||
|
return rest;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readToolName(parametersSchema: Record<string, unknown>): string {
|
||||||
|
const title = parametersSchema.title;
|
||||||
|
if (typeof title === "string" && title.trim().length > 0) {
|
||||||
|
return title.trim();
|
||||||
|
}
|
||||||
|
return "extract";
|
||||||
|
}
|
||||||
|
|
||||||
|
function readToolArgumentsJson(parsed: unknown, previewSource: string): Result<string, LlmError> {
|
||||||
|
if (!isRecord(parsed)) {
|
||||||
|
return err({ kind: "invalid_response_json", message: "Top-level JSON is not an object" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const choices = parsed.choices;
|
||||||
|
if (!Array.isArray(choices) || choices.length === 0) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const first = choices[0];
|
||||||
|
if (!isRecord(first)) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageObj = first.message;
|
||||||
|
if (!isRecord(messageObj)) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const toolCalls = messageObj.tool_calls;
|
||||||
|
if (!Array.isArray(toolCalls) || toolCalls.length === 0) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const call0 = toolCalls[0];
|
||||||
|
if (!isRecord(call0)) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn = call0.function;
|
||||||
|
if (!isRecord(fn)) {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const argsRaw = fn.arguments;
|
||||||
|
if (typeof argsRaw !== "string") {
|
||||||
|
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok(argsRaw);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls an OpenAI-compatible chat completions API with `tool_choice` forced to a single function
|
||||||
|
* derived from a Zod v4 schema (`toJSONSchema`). Uses `fetch()` only (no shell).
|
||||||
|
*/
|
||||||
|
export async function llmExtract<T>(options: LlmExtractOptions<T>): Promise<Result<T, LlmError>> {
|
||||||
|
const rawJsonSchema = toJSONSchema(options.schema) as Record<string, unknown>;
|
||||||
|
const parameters = stripJsonSchemaMeta(rawJsonSchema);
|
||||||
|
const toolName = readToolName(parameters);
|
||||||
|
const toolDescription =
|
||||||
|
typeof options.schema.description === "string" && options.schema.description.trim().length > 0
|
||||||
|
? options.schema.description.trim()
|
||||||
|
: "Extract structured data from the input text.";
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
model: options.provider.model,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "system" as const,
|
||||||
|
content: "Extract the requested information from the provided text. Be precise.",
|
||||||
|
},
|
||||||
|
{ role: "user" as const, content: options.text },
|
||||||
|
],
|
||||||
|
tools: [
|
||||||
|
{
|
||||||
|
type: "function" as const,
|
||||||
|
function: {
|
||||||
|
name: toolName,
|
||||||
|
description: toolDescription,
|
||||||
|
parameters,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
tool_choice: { type: "function" as const, function: { name: toolName } },
|
||||||
|
};
|
||||||
|
|
||||||
|
let response: Response;
|
||||||
|
try {
|
||||||
|
response = await fetch(chatCompletionsUrl(options.provider.baseUrl), {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${options.provider.apiKey}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
} catch (cause) {
|
||||||
|
const message = cause instanceof Error ? cause.message : String(cause);
|
||||||
|
return err({ kind: "network_error", message });
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseText = await response.text();
|
||||||
|
if (!response.ok) {
|
||||||
|
return err({ kind: "http_error", status: response.status, body: responseText.slice(0, 4000) });
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed: unknown;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(responseText) as unknown;
|
||||||
|
} catch (cause) {
|
||||||
|
const message = cause instanceof Error ? cause.message : String(cause);
|
||||||
|
return err({ kind: "invalid_response_json", message });
|
||||||
|
}
|
||||||
|
|
||||||
|
const argsJson = readToolArgumentsJson(parsed, responseText);
|
||||||
|
if (!argsJson.ok) {
|
||||||
|
return argsJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
let argsParsed: unknown;
|
||||||
|
try {
|
||||||
|
argsParsed = JSON.parse(argsJson.value) as unknown;
|
||||||
|
} catch (cause) {
|
||||||
|
const message = cause instanceof Error ? cause.message : String(cause);
|
||||||
|
return err({ kind: "tool_arguments_invalid_json", message });
|
||||||
|
}
|
||||||
|
|
||||||
|
const validated = options.schema.safeParse(argsParsed);
|
||||||
|
if (!validated.success) {
|
||||||
|
return err({
|
||||||
|
kind: "schema_validation_failed",
|
||||||
|
message: validated.error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok(validated.data);
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
import { spawn } from "node:child_process";
|
||||||
|
import { homedir } from "node:os";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import { type Result, err, ok } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
|
/** Compatible with `process.env` for `child_process.spawn`. */
|
||||||
|
export type SpawnEnv = Record<string, string | undefined>;
|
||||||
|
|
||||||
|
export type SpawnResult = {
|
||||||
|
stdout: string;
|
||||||
|
stderr: string;
|
||||||
|
exitCode: number;
|
||||||
|
/** OS signal name (e.g. `"SIGTERM"`) when terminated by signal; otherwise `null`. */
|
||||||
|
signal: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SpawnError =
|
||||||
|
| {
|
||||||
|
kind: "non_zero_exit";
|
||||||
|
stdout: string;
|
||||||
|
stderr: string;
|
||||||
|
exitCode: number;
|
||||||
|
signal: string | null;
|
||||||
|
}
|
||||||
|
| { kind: "timeout"; stdout: string; stderr: string }
|
||||||
|
| { kind: "spawn_failed"; message: string };
|
||||||
|
|
||||||
|
export type SpawnSafeOptions = {
|
||||||
|
cwd: string | null;
|
||||||
|
/** When null, merges {@link nerveCommandEnv} over `process.env`. When set, merges over that default. */
|
||||||
|
env: SpawnEnv | null;
|
||||||
|
timeoutMs: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const DEFAULT_TIMEOUT_MS = 300_000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PATH and PNPM_HOME for running `pnpm` and `nerve` from workflow roles.
|
||||||
|
* Uses the pnpm store home only (no npm user bin); binaries must resolve via PATH.
|
||||||
|
*/
|
||||||
|
export function nerveCommandEnv(): SpawnEnv {
|
||||||
|
const home = homedir();
|
||||||
|
const pnpmHome = join(home, ".local/share/pnpm");
|
||||||
|
return {
|
||||||
|
...process.env,
|
||||||
|
PNPM_HOME: pnpmHome,
|
||||||
|
PATH: `${pnpmHome}:${process.env.PATH ?? ""}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeEnv(user: SpawnEnv | null): SpawnEnv {
|
||||||
|
const base = nerveCommandEnv();
|
||||||
|
if (user === null) {
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
return { ...base, ...user };
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveTimeout(timeoutMs: number | null): number {
|
||||||
|
if (timeoutMs === null) {
|
||||||
|
return DEFAULT_TIMEOUT_MS;
|
||||||
|
}
|
||||||
|
return timeoutMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spawn a process with `shell: false` (argv only), default {@link nerveCommandEnv}, and optional timeout.
|
||||||
|
* Returns `ok` only when the process exits with code 0.
|
||||||
|
*/
|
||||||
|
export function spawnSafe(
|
||||||
|
command: string,
|
||||||
|
args: ReadonlyArray<string>,
|
||||||
|
options: SpawnSafeOptions,
|
||||||
|
): Promise<Result<SpawnResult, SpawnError>> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const cwd = options.cwd === null ? process.cwd() : options.cwd;
|
||||||
|
const env = mergeEnv(options.env);
|
||||||
|
const timeoutMs = resolveTimeout(options.timeoutMs);
|
||||||
|
|
||||||
|
const child = spawn(command, args, {
|
||||||
|
cwd,
|
||||||
|
env,
|
||||||
|
shell: false,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
|
||||||
|
let stdout = "";
|
||||||
|
let stderr = "";
|
||||||
|
let settled = false;
|
||||||
|
|
||||||
|
const finish = (outcome: Result<SpawnResult, SpawnError>) => {
|
||||||
|
if (settled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
settled = true;
|
||||||
|
clearTimeout(timer);
|
||||||
|
resolve(outcome);
|
||||||
|
};
|
||||||
|
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
child.kill("SIGTERM");
|
||||||
|
finish(err({ kind: "timeout", stdout, stderr }));
|
||||||
|
}, timeoutMs);
|
||||||
|
|
||||||
|
child.stdout?.on("data", (chunk: Buffer | string) => {
|
||||||
|
stdout += typeof chunk === "string" ? chunk : chunk.toString("utf-8");
|
||||||
|
});
|
||||||
|
child.stderr?.on("data", (chunk: Buffer | string) => {
|
||||||
|
stderr += typeof chunk === "string" ? chunk : chunk.toString("utf-8");
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on("error", (cause: Error) => {
|
||||||
|
finish(err({ kind: "spawn_failed", message: cause.message }));
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on("close", (code, signal) => {
|
||||||
|
const exitCode = code ?? 1;
|
||||||
|
const sig = signal === undefined || signal === null ? null : String(signal);
|
||||||
|
const result: SpawnResult = {
|
||||||
|
stdout: stdout.trimEnd(),
|
||||||
|
stderr: stderr.trimEnd(),
|
||||||
|
exitCode,
|
||||||
|
signal: sig,
|
||||||
|
};
|
||||||
|
if (exitCode !== 0) {
|
||||||
|
finish(
|
||||||
|
err({
|
||||||
|
kind: "non_zero_exit",
|
||||||
|
stdout: result.stdout,
|
||||||
|
stderr: result.stderr,
|
||||||
|
exitCode,
|
||||||
|
signal: sig,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finish(ok(result));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src",
|
||||||
|
"composite": false
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
Generated
+37
-2
@@ -14,6 +14,9 @@ importers:
|
|||||||
'@rslib/core':
|
'@rslib/core':
|
||||||
specifier: ^0.21.3
|
specifier: ^0.21.3
|
||||||
version: 0.21.3(typescript@5.9.3)
|
version: 0.21.3(typescript@5.9.3)
|
||||||
|
husky:
|
||||||
|
specifier: ^9.1.7
|
||||||
|
version: 9.1.7
|
||||||
typescript:
|
typescript:
|
||||||
specifier: ^5.5.0
|
specifier: ^5.5.0
|
||||||
version: 5.9.3
|
version: 5.9.3
|
||||||
@@ -66,7 +69,7 @@ importers:
|
|||||||
version: link:../store
|
version: link:../store
|
||||||
drizzle-orm:
|
drizzle-orm:
|
||||||
specifier: 1.0.0-beta.23-c10d10c
|
specifier: 1.0.0-beta.23-c10d10c
|
||||||
version: 1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)
|
version: 1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)(zod@4.3.6)
|
||||||
yaml:
|
yaml:
|
||||||
specifier: ^2.8.3
|
specifier: ^2.8.3
|
||||||
version: 2.8.3
|
version: 2.8.3
|
||||||
@@ -97,6 +100,25 @@ importers:
|
|||||||
specifier: ^4.1.5
|
specifier: ^4.1.5
|
||||||
version: 4.1.5(@types/node@22.19.17)(vite@8.0.9(@types/node@22.19.17)(esbuild@0.27.7)(yaml@2.8.3))
|
version: 4.1.5(@types/node@22.19.17)(vite@8.0.9(@types/node@22.19.17)(esbuild@0.27.7)(yaml@2.8.3))
|
||||||
|
|
||||||
|
packages/workflow-utils:
|
||||||
|
dependencies:
|
||||||
|
'@uncaged/nerve-core':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../core
|
||||||
|
zod:
|
||||||
|
specifier: ^4.3.6
|
||||||
|
version: 4.3.6
|
||||||
|
devDependencies:
|
||||||
|
'@rslib/core':
|
||||||
|
specifier: ^0.21.3
|
||||||
|
version: 0.21.3(typescript@5.9.3)
|
||||||
|
'@types/node':
|
||||||
|
specifier: ^22.0.0
|
||||||
|
version: 22.19.17
|
||||||
|
vitest:
|
||||||
|
specifier: ^4.1.5
|
||||||
|
version: 4.1.5(@types/node@22.19.17)(vite@8.0.9(@types/node@22.19.17)(esbuild@0.27.7)(yaml@2.8.3))
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
'@ast-grep/napi-darwin-arm64@0.37.0':
|
'@ast-grep/napi-darwin-arm64@0.37.0':
|
||||||
@@ -1010,6 +1032,11 @@ packages:
|
|||||||
resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==}
|
resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==}
|
||||||
engines: {node: '>= 14'}
|
engines: {node: '>= 14'}
|
||||||
|
|
||||||
|
husky@9.1.7:
|
||||||
|
resolution: {integrity: sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
iconv-lite@0.6.3:
|
iconv-lite@0.6.3:
|
||||||
resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
|
resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
|
||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
@@ -1464,6 +1491,9 @@ packages:
|
|||||||
engines: {node: '>= 14.6'}
|
engines: {node: '>= 14.6'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
|
zod@4.3.6:
|
||||||
|
resolution: {integrity: sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==}
|
||||||
|
|
||||||
snapshots:
|
snapshots:
|
||||||
|
|
||||||
'@ast-grep/napi-darwin-arm64@0.37.0':
|
'@ast-grep/napi-darwin-arm64@0.37.0':
|
||||||
@@ -2161,13 +2191,14 @@ snapshots:
|
|||||||
|
|
||||||
detect-libc@2.1.2: {}
|
detect-libc@2.1.2: {}
|
||||||
|
|
||||||
drizzle-orm@1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1):
|
drizzle-orm@1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)(zod@4.3.6):
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
'@types/better-sqlite3': 7.6.13
|
'@types/better-sqlite3': 7.6.13
|
||||||
'@types/mssql': 9.1.11(@azure/core-client@1.10.1)
|
'@types/mssql': 9.1.11(@azure/core-client@1.10.1)
|
||||||
better-sqlite3: 11.10.0
|
better-sqlite3: 11.10.0
|
||||||
mssql: 11.0.1(@azure/core-client@1.10.1)
|
mssql: 11.0.1(@azure/core-client@1.10.1)
|
||||||
sql.js: 1.14.1
|
sql.js: 1.14.1
|
||||||
|
zod: 4.3.6
|
||||||
|
|
||||||
ecdsa-sig-formatter@1.0.11:
|
ecdsa-sig-formatter@1.0.11:
|
||||||
dependencies:
|
dependencies:
|
||||||
@@ -2258,6 +2289,8 @@ snapshots:
|
|||||||
- supports-color
|
- supports-color
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
husky@9.1.7: {}
|
||||||
|
|
||||||
iconv-lite@0.6.3:
|
iconv-lite@0.6.3:
|
||||||
dependencies:
|
dependencies:
|
||||||
safer-buffer: 2.1.2
|
safer-buffer: 2.1.2
|
||||||
@@ -2762,3 +2795,5 @@ snapshots:
|
|||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
yaml@2.8.3: {}
|
yaml@2.8.3: {}
|
||||||
|
|
||||||
|
zod@4.3.6: {}
|
||||||
|
|||||||
Reference in New Issue
Block a user