Compare commits
No commits in common. "master" and "feat/266-knowledge-extraction-workflow" have entirely different histories.
master
...
feat/266-k
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,4 +5,3 @@ nerve.pid
|
|||||||
nerve.sock
|
nerve.sock
|
||||||
false/
|
false/
|
||||||
*.db
|
*.db
|
||||||
dist/
|
|
||||||
|
|||||||
22
nerve.yaml
22
nerve.yaml
@ -5,11 +5,31 @@ extract:
|
|||||||
model: qwen-plus
|
model: qwen-plus
|
||||||
|
|
||||||
senses:
|
senses:
|
||||||
|
linux-system-health:
|
||||||
|
group: system
|
||||||
|
interval: 30s
|
||||||
|
throttle: 10s
|
||||||
|
timeout: 15s
|
||||||
hermes-gateway-health:
|
hermes-gateway-health:
|
||||||
group: system
|
group: system
|
||||||
interval: 2m
|
interval: 2m
|
||||||
throttle: 30s
|
throttle: 30s
|
||||||
timeout: 30s
|
timeout: 30s
|
||||||
|
hermes-session-message-stats:
|
||||||
|
group: hermes
|
||||||
|
interval: 15m
|
||||||
|
throttle: 30s
|
||||||
|
timeout: 60s
|
||||||
|
worker-process-metrics:
|
||||||
|
group: system
|
||||||
|
interval: 1m
|
||||||
|
throttle: 15s
|
||||||
|
timeout: 5s
|
||||||
|
git-workspace-status:
|
||||||
|
group: workspace
|
||||||
|
interval: 2m
|
||||||
|
throttle: 30s
|
||||||
|
timeout: 15s
|
||||||
|
|
||||||
workflows:
|
workflows:
|
||||||
develop-sense:
|
develop-sense:
|
||||||
@ -21,6 +41,6 @@ workflows:
|
|||||||
solve-issue:
|
solve-issue:
|
||||||
concurrency: 1
|
concurrency: 1
|
||||||
overflow: queue
|
overflow: queue
|
||||||
extract-knowledge:
|
knowledge-extraction:
|
||||||
concurrency: 1
|
concurrency: 1
|
||||||
overflow: queue
|
overflow: queue
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "node scripts/build.mjs"
|
"build": "pnpm -r build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uncaged/nerve-adapter-cursor": "link:../repos/nerve/packages/adapter-cursor",
|
"@uncaged/nerve-adapter-cursor": "link:../repos/nerve/packages/adapter-cursor",
|
||||||
@ -19,10 +19,7 @@
|
|||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.0.0",
|
"drizzle-kit": "latest"
|
||||||
"drizzle-kit": "latest",
|
|
||||||
"esbuild": "^0.27.0",
|
|
||||||
"typescript": "^5.7.0"
|
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"onlyBuiltDependencies": [
|
"onlyBuiltDependencies": [
|
||||||
|
|||||||
11
pnpm-lock.yaml
generated
11
pnpm-lock.yaml
generated
@ -48,18 +48,9 @@ importers:
|
|||||||
specifier: ^4.3.6
|
specifier: ^4.3.6
|
||||||
version: 4.3.6
|
version: 4.3.6
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@types/node':
|
|
||||||
specifier: ^22.0.0
|
|
||||||
version: 22.19.17
|
|
||||||
drizzle-kit:
|
drizzle-kit:
|
||||||
specifier: latest
|
specifier: latest
|
||||||
version: 0.31.10
|
version: 0.31.10
|
||||||
esbuild:
|
|
||||||
specifier: ^0.27.0
|
|
||||||
version: 0.27.7
|
|
||||||
typescript:
|
|
||||||
specifier: ^5.7.0
|
|
||||||
version: 5.9.3
|
|
||||||
|
|
||||||
senses/git-workspace-status:
|
senses/git-workspace-status:
|
||||||
devDependencies:
|
devDependencies:
|
||||||
@ -183,7 +174,7 @@ importers:
|
|||||||
specifier: ^5.7.0
|
specifier: ^5.7.0
|
||||||
version: 5.9.3
|
version: 5.9.3
|
||||||
|
|
||||||
workflows/extract-knowledge:
|
workflows/knowledge-extraction:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@uncaged/nerve-adapter-cursor':
|
'@uncaged/nerve-adapter-cursor':
|
||||||
specifier: link:../../../repos/nerve/packages/adapter-cursor
|
specifier: link:../../../repos/nerve/packages/adapter-cursor
|
||||||
|
|||||||
3
pnpm-workspace.yaml
Normal file
3
pnpm-workspace.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
packages:
|
||||||
|
- "workflows/*"
|
||||||
|
- "senses/*"
|
||||||
@ -1,46 +0,0 @@
|
|||||||
import * as esbuild from "esbuild";
|
|
||||||
import fs from "node:fs";
|
|
||||||
import path from "node:path";
|
|
||||||
import { fileURLToPath } from "node:url";
|
|
||||||
|
|
||||||
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
||||||
const dist = path.join(root, "dist");
|
|
||||||
|
|
||||||
const opts = {
|
|
||||||
bundle: true,
|
|
||||||
platform: "node",
|
|
||||||
format: "esm",
|
|
||||||
packages: "external",
|
|
||||||
};
|
|
||||||
|
|
||||||
function listDirs(dir) {
|
|
||||||
if (!fs.existsSync(dir)) return [];
|
|
||||||
return fs
|
|
||||||
.readdirSync(dir)
|
|
||||||
.filter((name) => !name.startsWith(".") && !name.startsWith("_"))
|
|
||||||
.map((name) => ({ name, full: path.join(dir, name) }))
|
|
||||||
.filter(({ full }) => fs.statSync(full).isDirectory());
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
// Clean dist/
|
|
||||||
fs.rmSync(dist, { recursive: true, force: true });
|
|
||||||
|
|
||||||
for (const { name, full } of listDirs(path.join(root, "senses"))) {
|
|
||||||
const entry = path.join(full, "src", "index.ts");
|
|
||||||
if (!fs.existsSync(entry)) continue;
|
|
||||||
const outfile = path.join(dist, "senses", name, "index.js");
|
|
||||||
fs.mkdirSync(path.dirname(outfile), { recursive: true });
|
|
||||||
await esbuild.build({ ...opts, entryPoints: [entry], outfile });
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const { name, full } of listDirs(path.join(root, "workflows"))) {
|
|
||||||
const entry = path.join(full, "index.ts");
|
|
||||||
if (!fs.existsSync(entry)) continue;
|
|
||||||
const outfile = path.join(dist, "workflows", name, "index.js");
|
|
||||||
fs.mkdirSync(path.dirname(outfile), { recursive: true });
|
|
||||||
await esbuild.build({ ...opts, entryPoints: [entry], outfile });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await main();
|
|
||||||
85
senses/git-workspace-status/index.js
Normal file
85
senses/git-workspace-status/index.js
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// src/index.ts
|
||||||
|
import { execFileSync } from "node:child_process";
|
||||||
|
import { resolve } from "node:path";
|
||||||
|
|
||||||
|
// src/schema.ts
|
||||||
|
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||||
|
var snapshots = sqliteTable("snapshots", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
branch: text("branch").notNull(),
|
||||||
|
headShort: text("head_short").notNull(),
|
||||||
|
porcelainLines: integer("porcelain_lines").notNull(),
|
||||||
|
hasUpstream: integer("has_upstream").notNull(),
|
||||||
|
aheadCount: integer("ahead_count").notNull(),
|
||||||
|
behindCount: integer("behind_count").notNull(),
|
||||||
|
/** Empty string when the snapshot succeeded; otherwise a short error summary. */
|
||||||
|
gitError: text("git_error").notNull()
|
||||||
|
});
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var GIT_TIMEOUT_MS = 15e3;
|
||||||
|
function workspaceRoot() {
|
||||||
|
const raw = process.env.GIT_WORKSPACE_ROOT;
|
||||||
|
return raw ? resolve(raw) : resolve(process.cwd());
|
||||||
|
}
|
||||||
|
function gitErrorMessage(err) {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
const m = err.message.trim();
|
||||||
|
return m.length > 200 ? `${m.slice(0, 197)}...` : m;
|
||||||
|
}
|
||||||
|
return String(err);
|
||||||
|
}
|
||||||
|
function runGit(cwd, args) {
|
||||||
|
return execFileSync("git", args, {
|
||||||
|
cwd,
|
||||||
|
encoding: "utf8",
|
||||||
|
timeout: GIT_TIMEOUT_MS,
|
||||||
|
maxBuffer: 2 * 1024 * 1024
|
||||||
|
}).trimEnd();
|
||||||
|
}
|
||||||
|
function countPorcelainLines(output) {
|
||||||
|
if (!output) return 0;
|
||||||
|
return output.split("\n").filter((line) => line.length > 0).length;
|
||||||
|
}
|
||||||
|
async function compute() {
|
||||||
|
const root = workspaceRoot();
|
||||||
|
const ts = Date.now();
|
||||||
|
let branch = "";
|
||||||
|
let headShort = "";
|
||||||
|
let porcelainLines = 0;
|
||||||
|
let hasUpstream = 0;
|
||||||
|
let aheadCount = 0;
|
||||||
|
let behindCount = 0;
|
||||||
|
let gitError = "";
|
||||||
|
try {
|
||||||
|
const inside = runGit(root, ["rev-parse", "--is-inside-work-tree"]).trim();
|
||||||
|
if (inside !== "true") {
|
||||||
|
gitError = "not a git work tree";
|
||||||
|
return { signal: { ts, branch, headShort, porcelainLines, hasUpstream, aheadCount, behindCount, gitError }, workflow: null };
|
||||||
|
}
|
||||||
|
branch = runGit(root, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
||||||
|
headShort = runGit(root, ["rev-parse", "--short", "HEAD"]);
|
||||||
|
porcelainLines = countPorcelainLines(runGit(root, ["status", "--porcelain"]));
|
||||||
|
try {
|
||||||
|
runGit(root, ["rev-parse", "--abbrev-ref", "@{upstream}"]);
|
||||||
|
hasUpstream = 1;
|
||||||
|
const lb = runGit(root, ["rev-list", "--left-right", "--count", "HEAD...@{upstream}"]);
|
||||||
|
const parts = lb.split(/[\t\s]+/).filter(Boolean);
|
||||||
|
if (parts.length >= 2) {
|
||||||
|
aheadCount = Number.parseInt(parts[0], 10) || 0;
|
||||||
|
behindCount = Number.parseInt(parts[1], 10) || 0;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
hasUpstream = 0;
|
||||||
|
aheadCount = 0;
|
||||||
|
behindCount = 0;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
gitError = gitErrorMessage(e);
|
||||||
|
}
|
||||||
|
return { signal: { ts, branch, headShort, porcelainLines, hasUpstream, aheadCount, behindCount, gitError }, workflow: null };
|
||||||
|
}
|
||||||
|
export {
|
||||||
|
compute,
|
||||||
|
snapshots as table
|
||||||
|
};
|
||||||
13
senses/git-workspace-status/migrations/0001_init.sql
Normal file
13
senses/git-workspace-status/migrations/0001_init.sql
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
-- Migration: 0001_init
|
||||||
|
-- Creates the snapshots table for git-workspace-status sense.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS snapshots (
|
||||||
|
ts INTEGER PRIMARY KEY,
|
||||||
|
branch TEXT NOT NULL,
|
||||||
|
head_short TEXT NOT NULL,
|
||||||
|
porcelain_lines INTEGER NOT NULL,
|
||||||
|
has_upstream INTEGER NOT NULL,
|
||||||
|
ahead_count INTEGER NOT NULL,
|
||||||
|
behind_count INTEGER NOT NULL,
|
||||||
|
git_error TEXT NOT NULL
|
||||||
|
);
|
||||||
14
senses/git-workspace-status/package.json
Normal file
14
senses/git-workspace-status/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "sense-git-workspace-status",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=index.js --packages=external"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
76
senses/git-workspace-status/src/index.ts
Normal file
76
senses/git-workspace-status/src/index.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import { execFileSync } from "node:child_process";
|
||||||
|
import { resolve } from "node:path";
|
||||||
|
export { snapshots as table } from "./schema.ts";
|
||||||
|
|
||||||
|
const GIT_TIMEOUT_MS = 15_000;
|
||||||
|
|
||||||
|
function workspaceRoot(): string {
|
||||||
|
const raw = process.env.GIT_WORKSPACE_ROOT;
|
||||||
|
return raw ? resolve(raw) : resolve(process.cwd());
|
||||||
|
}
|
||||||
|
|
||||||
|
function gitErrorMessage(err: unknown): string {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
const m = err.message.trim();
|
||||||
|
return m.length > 200 ? `${m.slice(0, 197)}...` : m;
|
||||||
|
}
|
||||||
|
return String(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
function runGit(cwd: string, args: string[]): string {
|
||||||
|
return execFileSync("git", args, {
|
||||||
|
cwd,
|
||||||
|
encoding: "utf8",
|
||||||
|
timeout: GIT_TIMEOUT_MS,
|
||||||
|
maxBuffer: 2 * 1024 * 1024,
|
||||||
|
}).trimEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
function countPorcelainLines(output: string): number {
|
||||||
|
if (!output) return 0;
|
||||||
|
return output.split("\n").filter((line) => line.length > 0).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compute() {
|
||||||
|
const root = workspaceRoot();
|
||||||
|
const ts = Date.now();
|
||||||
|
|
||||||
|
let branch = "";
|
||||||
|
let headShort = "";
|
||||||
|
let porcelainLines = 0;
|
||||||
|
let hasUpstream = 0;
|
||||||
|
let aheadCount = 0;
|
||||||
|
let behindCount = 0;
|
||||||
|
let gitError = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const inside = runGit(root, ["rev-parse", "--is-inside-work-tree"]).trim();
|
||||||
|
if (inside !== "true") {
|
||||||
|
gitError = "not a git work tree";
|
||||||
|
return { signal: { ts, branch, headShort, porcelainLines, hasUpstream, aheadCount, behindCount, gitError }, workflow: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
branch = runGit(root, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
||||||
|
headShort = runGit(root, ["rev-parse", "--short", "HEAD"]);
|
||||||
|
porcelainLines = countPorcelainLines(runGit(root, ["status", "--porcelain"]));
|
||||||
|
|
||||||
|
try {
|
||||||
|
runGit(root, ["rev-parse", "--abbrev-ref", "@{upstream}"]);
|
||||||
|
hasUpstream = 1;
|
||||||
|
const lb = runGit(root, ["rev-list", "--left-right", "--count", "HEAD...@{upstream}"]);
|
||||||
|
const parts = lb.split(/[\t\s]+/).filter(Boolean);
|
||||||
|
if (parts.length >= 2) {
|
||||||
|
aheadCount = Number.parseInt(parts[0], 10) || 0;
|
||||||
|
behindCount = Number.parseInt(parts[1], 10) || 0;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
hasUpstream = 0;
|
||||||
|
aheadCount = 0;
|
||||||
|
behindCount = 0;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
gitError = gitErrorMessage(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { signal: { ts, branch, headShort, porcelainLines, hasUpstream, aheadCount, behindCount, gitError }, workflow: null };
|
||||||
|
}
|
||||||
13
senses/git-workspace-status/src/schema.ts
Normal file
13
senses/git-workspace-status/src/schema.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||||
|
|
||||||
|
export const snapshots = sqliteTable("snapshots", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
branch: text("branch").notNull(),
|
||||||
|
headShort: text("head_short").notNull(),
|
||||||
|
porcelainLines: integer("porcelain_lines").notNull(),
|
||||||
|
hasUpstream: integer("has_upstream").notNull(),
|
||||||
|
aheadCount: integer("ahead_count").notNull(),
|
||||||
|
behindCount: integer("behind_count").notNull(),
|
||||||
|
/** Empty string when the snapshot succeeded; otherwise a short error summary. */
|
||||||
|
gitError: text("git_error").notNull(),
|
||||||
|
});
|
||||||
361
senses/hermes-gateway-health/index.js
Normal file
361
senses/hermes-gateway-health/index.js
Normal file
@ -0,0 +1,361 @@
|
|||||||
|
// src/index.ts
|
||||||
|
import { execFile } from "node:child_process";
|
||||||
|
|
||||||
|
// src/schema.ts
|
||||||
|
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||||
|
var hermesGatewayHealth = sqliteTable("hermes_gateway_health", {
|
||||||
|
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||||
|
ts: integer("ts").notNull(),
|
||||||
|
alive: integer("alive").notNull(),
|
||||||
|
mainPid: integer("main_pid").notNull(),
|
||||||
|
rssBytes: integer("rss_bytes").notNull(),
|
||||||
|
cpuPercent: real("cpu_percent").notNull(),
|
||||||
|
uptimeSec: integer("uptime_sec").notNull(),
|
||||||
|
activeSessions: integer("active_sessions").notNull(),
|
||||||
|
childProcessCount: integer("child_process_count").notNull(),
|
||||||
|
httpOk: integer("http_ok").notNull(),
|
||||||
|
httpStatusCode: integer("http_status_code").notNull(),
|
||||||
|
httpLatencyMs: integer("http_latency_ms").notNull(),
|
||||||
|
httpError: text("http_error").notNull()
|
||||||
|
});
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var EXEC_TIMEOUT_MS = 25e3;
|
||||||
|
var HTTP_TIMEOUT_MS = Math.min(23e3, EXEC_TIMEOUT_MS - 2e3);
|
||||||
|
var HTTP_ERROR_MAX_LEN = 256;
|
||||||
|
function gatewayProbeUrl() {
|
||||||
|
const u = process.env.HERMES_GATEWAY_HEALTH_URL ?? process.env.NERVE_HERMES_GATEWAY_URL ?? "";
|
||||||
|
return String(u).trim();
|
||||||
|
}
|
||||||
|
function truncateHttpError(err) {
|
||||||
|
const raw = err && typeof err === "object" && "code" in err && err.code ? String(err.code) : String(err?.message ?? err ?? "error");
|
||||||
|
const s = raw.trim() || "error";
|
||||||
|
return s.length > HTTP_ERROR_MAX_LEN ? s.slice(0, HTTP_ERROR_MAX_LEN) : s;
|
||||||
|
}
|
||||||
|
async function probeGatewayHttp(url) {
|
||||||
|
if (!url) {
|
||||||
|
return {
|
||||||
|
httpOk: 0,
|
||||||
|
httpStatusCode: 0,
|
||||||
|
httpLatencyMs: 0,
|
||||||
|
httpError: "missing_url"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const t0 = Date.now();
|
||||||
|
try {
|
||||||
|
const signal = AbortSignal.timeout(HTTP_TIMEOUT_MS);
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method: "GET",
|
||||||
|
signal,
|
||||||
|
redirect: "follow"
|
||||||
|
});
|
||||||
|
const httpLatencyMs = Date.now() - t0;
|
||||||
|
const code = res.status;
|
||||||
|
const ok = code >= 200 && code < 400;
|
||||||
|
return {
|
||||||
|
httpOk: ok ? 1 : 0,
|
||||||
|
httpStatusCode: code,
|
||||||
|
httpLatencyMs,
|
||||||
|
httpError: ok ? "" : truncateHttpError({ message: `HTTP ${code}` })
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
const httpLatencyMs = Date.now() - t0;
|
||||||
|
return {
|
||||||
|
httpOk: 0,
|
||||||
|
httpStatusCode: 0,
|
||||||
|
httpLatencyMs,
|
||||||
|
httpError: truncateHttpError(err)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function etimeToSeconds(etime) {
|
||||||
|
let s = String(etime).trim();
|
||||||
|
if (!s) return 0;
|
||||||
|
let days = 0;
|
||||||
|
if (s.includes("-")) {
|
||||||
|
const idx = s.indexOf("-");
|
||||||
|
const d = Number.parseInt(s.slice(0, idx), 10);
|
||||||
|
days = Number.isFinite(d) ? d : 0;
|
||||||
|
s = s.slice(idx + 1);
|
||||||
|
}
|
||||||
|
const parts = s.split(":").map((x) => Number.parseInt(String(x).trim(), 10));
|
||||||
|
if (parts.some((n) => !Number.isFinite(n))) return 0;
|
||||||
|
if (parts.length === 3) {
|
||||||
|
return Math.trunc(days * 86400 + parts[0] * 3600 + parts[1] * 60 + parts[2]);
|
||||||
|
}
|
||||||
|
if (parts.length === 2) {
|
||||||
|
return Math.trunc(days * 86400 + parts[0] * 60 + parts[1]);
|
||||||
|
}
|
||||||
|
if (parts.length === 1) {
|
||||||
|
return Math.trunc(days * 86400 + parts[0]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
function execFileUtf8(file, args, opts = {}) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
execFile(
|
||||||
|
file,
|
||||||
|
args,
|
||||||
|
{
|
||||||
|
encoding: "utf8",
|
||||||
|
maxBuffer: 8 * 1024 * 1024,
|
||||||
|
timeout: EXEC_TIMEOUT_MS,
|
||||||
|
...opts
|
||||||
|
},
|
||||||
|
(err, stdout, stderr) => {
|
||||||
|
const exitCode = err && typeof err.status === "number" ? err.status : err ? -1 : 0;
|
||||||
|
resolve({
|
||||||
|
exitCode,
|
||||||
|
errCode: err?.code,
|
||||||
|
stdout: String(stdout ?? ""),
|
||||||
|
stderr: String(stderr ?? "")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function parseMainPidFromStatus(text2) {
|
||||||
|
const m = text2.match(/Main PID:\s*(\d+)/i);
|
||||||
|
return m ? Math.trunc(Number.parseInt(m[1], 10)) || 0 : 0;
|
||||||
|
}
|
||||||
|
function parseActiveLineFromStatus(text2) {
|
||||||
|
for (const line of text2.split("\n")) {
|
||||||
|
if (/^\s*Active:/i.test(line)) {
|
||||||
|
const m = line.match(/Active:\s*(\S+)\s*\(([^)]*)\)/i);
|
||||||
|
if (m) {
|
||||||
|
return {
|
||||||
|
active: m[1].toLowerCase() === "active",
|
||||||
|
subRunning: m[2].toLowerCase().includes("running")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { active: false, subRunning: false };
|
||||||
|
}
|
||||||
|
function parseSystemctlShow(text2) {
|
||||||
|
let mainPid = 0;
|
||||||
|
let active = false;
|
||||||
|
let subRunning = false;
|
||||||
|
for (const line of text2.split("\n")) {
|
||||||
|
const t = line.trim();
|
||||||
|
if (t.startsWith("MainPID=")) {
|
||||||
|
mainPid = Math.trunc(Number.parseInt(t.slice("MainPID=".length), 10)) || 0;
|
||||||
|
} else if (t.startsWith("ActiveState=")) {
|
||||||
|
active = t.slice("ActiveState=".length).trim().toLowerCase() === "active";
|
||||||
|
} else if (t.startsWith("SubState=")) {
|
||||||
|
subRunning = t.slice("SubState=".length).trim().toLowerCase() === "running";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { mainPid, active, subRunning };
|
||||||
|
}
|
||||||
|
async function readSystemdState() {
|
||||||
|
const status = await execFileUtf8("systemctl", [
|
||||||
|
"--user",
|
||||||
|
"--no-pager",
|
||||||
|
"status",
|
||||||
|
"hermes-gateway"
|
||||||
|
]);
|
||||||
|
const combined = `${status.stdout}
|
||||||
|
${status.stderr}`.trim();
|
||||||
|
let mainPid = parseMainPidFromStatus(combined);
|
||||||
|
let { active, subRunning } = parseActiveLineFromStatus(combined);
|
||||||
|
const needShow = mainPid <= 0 || !active || !subRunning;
|
||||||
|
if (needShow) {
|
||||||
|
const show = await execFileUtf8("systemctl", [
|
||||||
|
"--user",
|
||||||
|
"--no-pager",
|
||||||
|
"show",
|
||||||
|
"hermes-gateway",
|
||||||
|
"-p",
|
||||||
|
"MainPID",
|
||||||
|
"-p",
|
||||||
|
"ActiveState",
|
||||||
|
"-p",
|
||||||
|
"SubState"
|
||||||
|
]);
|
||||||
|
const showText = `${show.stdout}
|
||||||
|
${show.stderr}`;
|
||||||
|
const s = parseSystemctlShow(showText);
|
||||||
|
if (mainPid <= 0 && s.mainPid > 0) mainPid = s.mainPid;
|
||||||
|
if (!active) active = s.active;
|
||||||
|
if (!subRunning) subRunning = s.subRunning;
|
||||||
|
}
|
||||||
|
return { mainPid, systemdActiveRunning: active && subRunning };
|
||||||
|
}
|
||||||
|
async function processExists(mainPid) {
|
||||||
|
if (mainPid <= 0) return false;
|
||||||
|
const r = await execFileUtf8("ps", ["-p", String(mainPid), "-o", "pid="]);
|
||||||
|
if (r.errCode === "ENOENT") return false;
|
||||||
|
return r.stdout.trim().length > 0;
|
||||||
|
}
|
||||||
|
async function readPsMetrics(mainPid) {
|
||||||
|
if (mainPid <= 0) {
|
||||||
|
return { rssBytes: 0, cpuPercent: 0, uptimeSec: 0 };
|
||||||
|
}
|
||||||
|
let r = await execFileUtf8("ps", [
|
||||||
|
"-p",
|
||||||
|
String(mainPid),
|
||||||
|
"-o",
|
||||||
|
"rss=,%cpu=,etimes="
|
||||||
|
]);
|
||||||
|
let line = r.stdout.trim().replace(/\s+/g, " ");
|
||||||
|
if (r.errCode === "ENOENT" || !line) {
|
||||||
|
return { rssBytes: 0, cpuPercent: 0, uptimeSec: 0 };
|
||||||
|
}
|
||||||
|
let parts = line.split(" ").filter(Boolean);
|
||||||
|
if (parts.length < 3) {
|
||||||
|
r = await execFileUtf8("ps", [
|
||||||
|
"-p",
|
||||||
|
String(mainPid),
|
||||||
|
"-o",
|
||||||
|
"rss=,%cpu=,etime="
|
||||||
|
]);
|
||||||
|
line = r.stdout.trim().replace(/\s+/g, " ");
|
||||||
|
parts = line.split(" ").filter(Boolean);
|
||||||
|
if (parts.length < 3) {
|
||||||
|
return { rssBytes: 0, cpuPercent: 0, uptimeSec: 0 };
|
||||||
|
}
|
||||||
|
const rssKiB2 = Number(parts[0]);
|
||||||
|
const cpu2 = Number(parts[1]);
|
||||||
|
const uptimeSec2 = etimeToSeconds(parts.slice(2).join(" "));
|
||||||
|
const rssBytes2 = Number.isFinite(rssKiB2) ? Math.trunc(rssKiB2 * 1024) : 0;
|
||||||
|
const cpuPercent2 = Number.isFinite(cpu2) ? Math.round(cpu2 * 100) / 100 : 0;
|
||||||
|
return { rssBytes: rssBytes2, cpuPercent: cpuPercent2, uptimeSec: uptimeSec2 };
|
||||||
|
}
|
||||||
|
const rssKiB = Number(parts[0]);
|
||||||
|
const cpu = Number(parts[1]);
|
||||||
|
const etimes = Number(parts[2]);
|
||||||
|
const rssBytes = Number.isFinite(rssKiB) ? Math.trunc(rssKiB * 1024) : 0;
|
||||||
|
const cpuPercent = Number.isFinite(cpu) ? Math.round(cpu * 100) / 100 : 0;
|
||||||
|
const uptimeSec = Number.isFinite(etimes) ? Math.trunc(etimes) : 0;
|
||||||
|
return { rssBytes, cpuPercent, uptimeSec };
|
||||||
|
}
|
||||||
|
function parseActiveSessionsFromHermesStats(text2) {
|
||||||
|
const src = String(text2);
|
||||||
|
const patterns = [
|
||||||
|
/^\s*Active\s+sessions?:\s*(\d+)/gim,
|
||||||
|
/^\s*active\s+sessions?:\s*(\d+)/gim,
|
||||||
|
/^\s*Total\s+sessions?:\s*(\d+)/gim
|
||||||
|
];
|
||||||
|
for (const re of patterns) {
|
||||||
|
re.lastIndex = 0;
|
||||||
|
const m = re.exec(src);
|
||||||
|
if (m) {
|
||||||
|
const n = Math.trunc(Number.parseInt(m[1], 10));
|
||||||
|
return Number.isFinite(n) ? n : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
async function readActiveSessions() {
|
||||||
|
try {
|
||||||
|
const r = await execFileUtf8("hermes", ["sessions", "stats"]);
|
||||||
|
if (r.errCode === "ENOENT") return 0;
|
||||||
|
return parseActiveSessionsFromHermesStats(`${r.stdout}
|
||||||
|
${r.stderr}`);
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function countDirectChildren(mainPid) {
|
||||||
|
if (mainPid <= 0) return 0;
|
||||||
|
try {
|
||||||
|
const r = await execFileUtf8("ps", [
|
||||||
|
"--no-headers",
|
||||||
|
"-o",
|
||||||
|
"pid",
|
||||||
|
"--ppid",
|
||||||
|
String(mainPid)
|
||||||
|
]);
|
||||||
|
if (r.errCode === "ENOENT") return 0;
|
||||||
|
const lines = r.stdout.split("\n").map((l) => l.trim()).filter(Boolean);
|
||||||
|
return lines.length;
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function compute() {
|
||||||
|
const ts = Date.now();
|
||||||
|
let mainPid = 0;
|
||||||
|
let systemdActiveRunning = false;
|
||||||
|
try {
|
||||||
|
const st = await readSystemdState();
|
||||||
|
mainPid = st.mainPid;
|
||||||
|
systemdActiveRunning = st.systemdActiveRunning;
|
||||||
|
} catch {
|
||||||
|
mainPid = 0;
|
||||||
|
systemdActiveRunning = false;
|
||||||
|
}
|
||||||
|
let psOk = false;
|
||||||
|
try {
|
||||||
|
psOk = await processExists(mainPid);
|
||||||
|
} catch {
|
||||||
|
psOk = false;
|
||||||
|
}
|
||||||
|
let rssBytes = 0;
|
||||||
|
let cpuPercent = 0;
|
||||||
|
let uptimeSec = 0;
|
||||||
|
if (psOk) {
|
||||||
|
try {
|
||||||
|
const m = await readPsMetrics(mainPid);
|
||||||
|
rssBytes = m.rssBytes;
|
||||||
|
cpuPercent = m.cpuPercent;
|
||||||
|
uptimeSec = m.uptimeSec;
|
||||||
|
} catch {
|
||||||
|
rssBytes = 0;
|
||||||
|
cpuPercent = 0;
|
||||||
|
uptimeSec = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const alive = systemdActiveRunning && mainPid > 0 && psOk ? 1 : 0;
|
||||||
|
let activeSessions = 0;
|
||||||
|
try {
|
||||||
|
activeSessions = await readActiveSessions();
|
||||||
|
} catch {
|
||||||
|
activeSessions = 0;
|
||||||
|
}
|
||||||
|
let childProcessCount = 0;
|
||||||
|
if (alive && mainPid > 0) {
|
||||||
|
try {
|
||||||
|
childProcessCount = await countDirectChildren(mainPid);
|
||||||
|
} catch {
|
||||||
|
childProcessCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let httpOk = 0;
|
||||||
|
let httpStatusCode = 0;
|
||||||
|
let httpLatencyMs = 0;
|
||||||
|
let httpError = "";
|
||||||
|
try {
|
||||||
|
const h = await probeGatewayHttp(gatewayProbeUrl());
|
||||||
|
httpOk = h.httpOk;
|
||||||
|
httpStatusCode = h.httpStatusCode;
|
||||||
|
httpLatencyMs = h.httpLatencyMs;
|
||||||
|
httpError = h.httpError;
|
||||||
|
} catch {
|
||||||
|
httpOk = 0;
|
||||||
|
httpStatusCode = 0;
|
||||||
|
httpLatencyMs = 0;
|
||||||
|
httpError = "probe_failed";
|
||||||
|
}
|
||||||
|
const storedMainPid = mainPid > 0 ? mainPid : 0;
|
||||||
|
const row = {
|
||||||
|
ts,
|
||||||
|
alive,
|
||||||
|
mainPid: storedMainPid,
|
||||||
|
rssBytes: alive ? rssBytes : 0,
|
||||||
|
cpuPercent: alive ? cpuPercent : 0,
|
||||||
|
uptimeSec: alive ? uptimeSec : 0,
|
||||||
|
activeSessions,
|
||||||
|
childProcessCount: alive ? childProcessCount : 0,
|
||||||
|
httpOk,
|
||||||
|
httpStatusCode,
|
||||||
|
httpLatencyMs,
|
||||||
|
httpError
|
||||||
|
};
|
||||||
|
return { signal: row, workflow: null };
|
||||||
|
}
|
||||||
|
export {
|
||||||
|
compute,
|
||||||
|
hermesGatewayHealth as table
|
||||||
|
};
|
||||||
14
senses/hermes-gateway-health/migrations/0001_init.sql
Normal file
14
senses/hermes-gateway-health/migrations/0001_init.sql
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
-- Migration: 0001_init
|
||||||
|
-- Creates the hermes_gateway_health table for hermes-gateway-health sense.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS hermes_gateway_health (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
ts INTEGER NOT NULL,
|
||||||
|
alive INTEGER NOT NULL,
|
||||||
|
main_pid INTEGER NOT NULL,
|
||||||
|
rss_bytes INTEGER NOT NULL,
|
||||||
|
cpu_percent REAL NOT NULL,
|
||||||
|
uptime_sec INTEGER NOT NULL,
|
||||||
|
active_sessions INTEGER NOT NULL,
|
||||||
|
child_process_count INTEGER NOT NULL
|
||||||
|
);
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
-- Migration: 0002_add_http_probe
|
||||||
|
-- HTTP reachability columns for hermes-gateway-health sense.
|
||||||
|
|
||||||
|
ALTER TABLE hermes_gateway_health ADD COLUMN http_ok INTEGER NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE hermes_gateway_health ADD COLUMN http_status_code INTEGER NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE hermes_gateway_health ADD COLUMN http_latency_ms INTEGER NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE hermes_gateway_health ADD COLUMN http_error TEXT NOT NULL DEFAULT '';
|
||||||
14
senses/hermes-gateway-health/package.json
Normal file
14
senses/hermes-gateway-health/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "sense-hermes-gateway-health",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=index.js --packages=external"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
import { execFile } from "node:child_process";
|
import { execFile } from "node:child_process";
|
||||||
|
export { hermesGatewayHealth as table } from "./schema.ts";
|
||||||
|
|
||||||
/** Keep subprocess deadlines slightly under typical sense timeout (30s). */
|
/** Keep subprocess deadlines slightly under typical sense timeout (30s). */
|
||||||
const EXEC_TIMEOUT_MS = 25_000;
|
const EXEC_TIMEOUT_MS = 25_000;
|
||||||
|
|
||||||
@ -7,22 +9,6 @@ const HTTP_TIMEOUT_MS = Math.min(23_000, EXEC_TIMEOUT_MS - 2000);
|
|||||||
|
|
||||||
const HTTP_ERROR_MAX_LEN = 256;
|
const HTTP_ERROR_MAX_LEN = 256;
|
||||||
|
|
||||||
/** How many consecutive failures before triggering a restart. */
|
|
||||||
const FAILURE_THRESHOLD = 3;
|
|
||||||
|
|
||||||
type SenseState = {
|
|
||||||
consecutiveFailures: number;
|
|
||||||
lastRestartTs: number;
|
|
||||||
/** Minimum ms between restart attempts to avoid restart loops. */
|
|
||||||
restartCooldownMs: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const initialState: SenseState = {
|
|
||||||
consecutiveFailures: 0,
|
|
||||||
lastRestartTs: 0,
|
|
||||||
restartCooldownMs: 300_000, // 5 minutes
|
|
||||||
};
|
|
||||||
|
|
||||||
function gatewayProbeUrl(): string {
|
function gatewayProbeUrl(): string {
|
||||||
const u =
|
const u =
|
||||||
process.env.HERMES_GATEWAY_HEALTH_URL ??
|
process.env.HERMES_GATEWAY_HEALTH_URL ??
|
||||||
@ -40,13 +26,17 @@ function truncateHttpError(err: unknown): string {
|
|||||||
return s.length > HTTP_ERROR_MAX_LEN ? s.slice(0, HTTP_ERROR_MAX_LEN) : s;
|
return s.length > HTTP_ERROR_MAX_LEN ? s.slice(0, HTTP_ERROR_MAX_LEN) : s;
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpProbeResult = {
|
interface HttpProbeResult {
|
||||||
httpOk: number;
|
httpOk: number;
|
||||||
httpStatusCode: number;
|
httpStatusCode: number;
|
||||||
httpLatencyMs: number;
|
httpLatencyMs: number;
|
||||||
httpError: string;
|
httpError: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET the gateway URL; success = HTTP 200–399.
|
||||||
|
* URL must be set via HERMES_GATEWAY_HEALTH_URL or NERVE_HERMES_GATEWAY_URL.
|
||||||
|
*/
|
||||||
async function probeGatewayHttp(url: string): Promise<HttpProbeResult> {
|
async function probeGatewayHttp(url: string): Promise<HttpProbeResult> {
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return {
|
return {
|
||||||
@ -84,6 +74,10 @@ async function probeGatewayHttp(url: string): Promise<HttpProbeResult> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When `ps` lacks `etimes` (wall-clock seconds since start), parse `etime`
|
||||||
|
* ([[dd-]hh:]mm:ss) into seconds. See ps(1) `etime` field description.
|
||||||
|
*/
|
||||||
function etimeToSeconds(etime: string): number {
|
function etimeToSeconds(etime: string): number {
|
||||||
let s = String(etime).trim();
|
let s = String(etime).trim();
|
||||||
if (!s) return 0;
|
if (!s) return 0;
|
||||||
@ -108,12 +102,12 @@ function etimeToSeconds(etime: string): number {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExecResult = {
|
interface ExecResult {
|
||||||
exitCode: number;
|
exitCode: number;
|
||||||
errCode: string | undefined;
|
errCode: string | undefined;
|
||||||
stdout: string;
|
stdout: string;
|
||||||
stderr: string;
|
stderr: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
function execFileUtf8(file: string, args: string[], opts: Record<string, unknown> = {}): Promise<ExecResult> {
|
function execFileUtf8(file: string, args: string[], opts: Record<string, unknown> = {}): Promise<ExecResult> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@ -222,11 +216,11 @@ async function processExists(mainPid: number): Promise<boolean> {
|
|||||||
return r.stdout.trim().length > 0;
|
return r.stdout.trim().length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
type PsMetrics = {
|
interface PsMetrics {
|
||||||
rssBytes: number;
|
rssBytes: number;
|
||||||
cpuPercent: number;
|
cpuPercent: number;
|
||||||
uptimeSec: number;
|
uptimeSec: number;
|
||||||
};
|
}
|
||||||
|
|
||||||
async function readPsMetrics(mainPid: number): Promise<PsMetrics> {
|
async function readPsMetrics(mainPid: number): Promise<PsMetrics> {
|
||||||
if (mainPid <= 0) {
|
if (mainPid <= 0) {
|
||||||
@ -271,12 +265,61 @@ async function readPsMetrics(mainPid: number): Promise<PsMetrics> {
|
|||||||
return { rssBytes, cpuPercent, uptimeSec };
|
return { rssBytes, cpuPercent, uptimeSec };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function compute(prevState: SenseState) {
|
function parseActiveSessionsFromHermesStats(text: string): number {
|
||||||
const now = Date.now();
|
const src = String(text);
|
||||||
|
const patterns = [
|
||||||
|
/^\s*Active\s+sessions?:\s*(\d+)/gim,
|
||||||
|
/^\s*active\s+sessions?:\s*(\d+)/gim,
|
||||||
|
/^\s*Total\s+sessions?:\s*(\d+)/gim,
|
||||||
|
];
|
||||||
|
for (const re of patterns) {
|
||||||
|
re.lastIndex = 0;
|
||||||
|
const m = re.exec(src);
|
||||||
|
if (m) {
|
||||||
|
const n = Math.trunc(Number.parseInt(m[1], 10));
|
||||||
|
return Number.isFinite(n) ? n : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readActiveSessions(): Promise<number> {
|
||||||
|
try {
|
||||||
|
const r = await execFileUtf8("hermes", ["sessions", "stats"]);
|
||||||
|
if (r.errCode === "ENOENT") return 0;
|
||||||
|
return parseActiveSessionsFromHermesStats(`${r.stdout}\n${r.stderr}`);
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function countDirectChildren(mainPid: number): Promise<number> {
|
||||||
|
if (mainPid <= 0) return 0;
|
||||||
|
try {
|
||||||
|
const r = await execFileUtf8("ps", [
|
||||||
|
"--no-headers",
|
||||||
|
"-o",
|
||||||
|
"pid",
|
||||||
|
"--ppid",
|
||||||
|
String(mainPid),
|
||||||
|
]);
|
||||||
|
if (r.errCode === "ENOENT") return 0;
|
||||||
|
const lines = r.stdout
|
||||||
|
.split("\n")
|
||||||
|
.map((l) => l.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
return lines.length;
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compute() {
|
||||||
|
const ts = Date.now();
|
||||||
|
|
||||||
// --- probe gateway ---
|
|
||||||
let mainPid = 0;
|
let mainPid = 0;
|
||||||
let systemdActiveRunning = false;
|
let systemdActiveRunning = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const st = await readSystemdState();
|
const st = await readSystemdState();
|
||||||
mainPid = st.mainPid;
|
mainPid = st.mainPid;
|
||||||
@ -311,6 +354,22 @@ export async function compute(prevState: SenseState) {
|
|||||||
|
|
||||||
const alive = systemdActiveRunning && mainPid > 0 && psOk ? 1 : 0;
|
const alive = systemdActiveRunning && mainPid > 0 && psOk ? 1 : 0;
|
||||||
|
|
||||||
|
let activeSessions = 0;
|
||||||
|
try {
|
||||||
|
activeSessions = await readActiveSessions();
|
||||||
|
} catch {
|
||||||
|
activeSessions = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let childProcessCount = 0;
|
||||||
|
if (alive && mainPid > 0) {
|
||||||
|
try {
|
||||||
|
childProcessCount = await countDirectChildren(mainPid);
|
||||||
|
} catch {
|
||||||
|
childProcessCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let httpOk = 0;
|
let httpOk = 0;
|
||||||
let httpStatusCode = 0;
|
let httpStatusCode = 0;
|
||||||
let httpLatencyMs = 0;
|
let httpLatencyMs = 0;
|
||||||
@ -328,42 +387,22 @@ export async function compute(prevState: SenseState) {
|
|||||||
httpError = "probe_failed";
|
httpError = "probe_failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- decide health ---
|
const storedMainPid = mainPid > 0 ? mainPid : 0;
|
||||||
const healthy = alive === 1 && httpOk === 1;
|
|
||||||
|
|
||||||
// --- state machine: track consecutive failures ---
|
const row = {
|
||||||
const consecutiveFailures = healthy ? 0 : prevState.consecutiveFailures + 1;
|
ts,
|
||||||
const lastRestartTs = prevState.lastRestartTs;
|
|
||||||
const cooldown = prevState.restartCooldownMs;
|
|
||||||
const cooldownElapsed = now - lastRestartTs >= cooldown;
|
|
||||||
|
|
||||||
// --- trigger restart? ---
|
|
||||||
const shouldRestart =
|
|
||||||
consecutiveFailures >= FAILURE_THRESHOLD && cooldownElapsed;
|
|
||||||
|
|
||||||
const nextState: SenseState = {
|
|
||||||
consecutiveFailures,
|
|
||||||
lastRestartTs: shouldRestart ? now : lastRestartTs,
|
|
||||||
restartCooldownMs: cooldown,
|
|
||||||
};
|
|
||||||
|
|
||||||
const signal = {
|
|
||||||
ts: now,
|
|
||||||
alive,
|
alive,
|
||||||
mainPid: mainPid > 0 ? mainPid : 0,
|
mainPid: storedMainPid,
|
||||||
rssBytes: alive ? rssBytes : 0,
|
rssBytes: alive ? rssBytes : 0,
|
||||||
cpuPercent: alive ? cpuPercent : 0,
|
cpuPercent: alive ? cpuPercent : 0,
|
||||||
uptimeSec: alive ? uptimeSec : 0,
|
uptimeSec: alive ? uptimeSec : 0,
|
||||||
|
activeSessions,
|
||||||
|
childProcessCount: alive ? childProcessCount : 0,
|
||||||
httpOk,
|
httpOk,
|
||||||
httpStatusCode,
|
httpStatusCode,
|
||||||
httpLatencyMs,
|
httpLatencyMs,
|
||||||
httpError,
|
httpError,
|
||||||
consecutiveFailures,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const trigger = shouldRestart
|
return { signal: row, workflow: null };
|
||||||
? { command: "systemctl --user restart hermes-gateway" }
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return { state: nextState, signal, trigger };
|
|
||||||
}
|
}
|
||||||
|
|||||||
17
senses/hermes-gateway-health/src/schema.ts
Normal file
17
senses/hermes-gateway-health/src/schema.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||||
|
|
||||||
|
export const hermesGatewayHealth = sqliteTable("hermes_gateway_health", {
|
||||||
|
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||||
|
ts: integer("ts").notNull(),
|
||||||
|
alive: integer("alive").notNull(),
|
||||||
|
mainPid: integer("main_pid").notNull(),
|
||||||
|
rssBytes: integer("rss_bytes").notNull(),
|
||||||
|
cpuPercent: real("cpu_percent").notNull(),
|
||||||
|
uptimeSec: integer("uptime_sec").notNull(),
|
||||||
|
activeSessions: integer("active_sessions").notNull(),
|
||||||
|
childProcessCount: integer("child_process_count").notNull(),
|
||||||
|
httpOk: integer("http_ok").notNull(),
|
||||||
|
httpStatusCode: integer("http_status_code").notNull(),
|
||||||
|
httpLatencyMs: integer("http_latency_ms").notNull(),
|
||||||
|
httpError: text("http_error").notNull(),
|
||||||
|
});
|
||||||
110
senses/hermes-session-message-stats/index.js
Normal file
110
senses/hermes-session-message-stats/index.js
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
// src/index.ts
|
||||||
|
import { createReadStream } from "node:fs";
|
||||||
|
import { readdir } from "node:fs/promises";
|
||||||
|
import { homedir } from "node:os";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import { createInterface } from "node:readline";
|
||||||
|
|
||||||
|
// src/schema.ts
|
||||||
|
import { integer, sqliteTable } from "drizzle-orm/sqlite-core";
|
||||||
|
var hermesSessionMessageStats = sqliteTable("hermes_session_message_stats", {
|
||||||
|
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||||
|
ts: integer("ts").notNull(),
|
||||||
|
totalUserMessages: integer("total_user_messages").notNull(),
|
||||||
|
totalAssistantMessages: integer("total_assistant_messages").notNull(),
|
||||||
|
totalToolMessages: integer("total_tool_messages").notNull(),
|
||||||
|
totalMessages: integer("total_messages").notNull(),
|
||||||
|
activeSessions: integer("active_sessions").notNull(),
|
||||||
|
measurementWindowSeconds: integer("measurement_window_seconds").notNull()
|
||||||
|
});
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var MEASUREMENT_WINDOW_MS = 9e5;
|
||||||
|
var MEASUREMENT_WINDOW_SECONDS = 900;
|
||||||
|
async function aggregateJsonlFile(filePath, cutoffMs, nowMs) {
|
||||||
|
let user = 0;
|
||||||
|
let assistant = 0;
|
||||||
|
let tool = 0;
|
||||||
|
let fileHadActivity = false;
|
||||||
|
const input = createReadStream(filePath, { encoding: "utf8" });
|
||||||
|
const rl = createInterface({ input, crlfDelay: Infinity });
|
||||||
|
try {
|
||||||
|
for await (const line of rl) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed) continue;
|
||||||
|
let obj;
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(trimmed);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (typeof obj !== "object" || obj === null || typeof obj.role !== "string" || typeof obj.timestamp !== "string") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const record = obj;
|
||||||
|
const t = Date.parse(record.timestamp);
|
||||||
|
if (!Number.isFinite(t) || t < cutoffMs || t > nowMs) continue;
|
||||||
|
const roleNorm = record.role.trim().toLowerCase();
|
||||||
|
if (roleNorm === "user") {
|
||||||
|
user++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
} else if (roleNorm === "assistant") {
|
||||||
|
assistant++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
} else if (roleNorm === "tool") {
|
||||||
|
tool++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
rl.close();
|
||||||
|
}
|
||||||
|
return { user, assistant, tool, fileHadActivity };
|
||||||
|
}
|
||||||
|
async function compute() {
|
||||||
|
const nowMs = Date.now();
|
||||||
|
const cutoffMs = nowMs - MEASUREMENT_WINDOW_MS;
|
||||||
|
const ts = nowMs;
|
||||||
|
let totalUserMessages = 0;
|
||||||
|
let totalAssistantMessages = 0;
|
||||||
|
let totalToolMessages = 0;
|
||||||
|
let activeSessions = 0;
|
||||||
|
const sessionsDir = join(homedir(), ".hermes", "sessions");
|
||||||
|
let files = [];
|
||||||
|
try {
|
||||||
|
const entries = await readdir(sessionsDir, { withFileTypes: true });
|
||||||
|
files = entries.filter((e) => e.isFile() && e.name.endsWith(".jsonl")).map((e) => join(sessionsDir, e.name));
|
||||||
|
} catch (err) {
|
||||||
|
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
|
||||||
|
files = [];
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const filePath of files) {
|
||||||
|
const { user, assistant, tool, fileHadActivity } = await aggregateJsonlFile(
|
||||||
|
filePath,
|
||||||
|
cutoffMs,
|
||||||
|
nowMs
|
||||||
|
);
|
||||||
|
totalUserMessages += user;
|
||||||
|
totalAssistantMessages += assistant;
|
||||||
|
totalToolMessages += tool;
|
||||||
|
if (fileHadActivity) activeSessions++;
|
||||||
|
}
|
||||||
|
const totalMessages = totalUserMessages + totalAssistantMessages + totalToolMessages;
|
||||||
|
const row = {
|
||||||
|
ts,
|
||||||
|
totalUserMessages,
|
||||||
|
totalAssistantMessages,
|
||||||
|
totalToolMessages,
|
||||||
|
totalMessages,
|
||||||
|
activeSessions,
|
||||||
|
measurementWindowSeconds: MEASUREMENT_WINDOW_SECONDS
|
||||||
|
};
|
||||||
|
return { signal: row, workflow: null };
|
||||||
|
}
|
||||||
|
export {
|
||||||
|
compute,
|
||||||
|
hermesSessionMessageStats as table
|
||||||
|
};
|
||||||
13
senses/hermes-session-message-stats/migrations/0001_init.sql
Normal file
13
senses/hermes-session-message-stats/migrations/0001_init.sql
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
-- Migration: 0001_init
|
||||||
|
-- Creates the hermes_session_message_stats table for hermes-session-message-stats sense.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS hermes_session_message_stats (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
ts INTEGER NOT NULL,
|
||||||
|
total_user_messages INTEGER NOT NULL,
|
||||||
|
total_assistant_messages INTEGER NOT NULL,
|
||||||
|
total_tool_messages INTEGER NOT NULL,
|
||||||
|
total_messages INTEGER NOT NULL,
|
||||||
|
active_sessions INTEGER NOT NULL,
|
||||||
|
measurement_window_seconds INTEGER NOT NULL
|
||||||
|
);
|
||||||
14
senses/hermes-session-message-stats/package.json
Normal file
14
senses/hermes-session-message-stats/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "sense-hermes-session-message-stats",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=index.js --packages=external"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
117
senses/hermes-session-message-stats/src/index.ts
Normal file
117
senses/hermes-session-message-stats/src/index.ts
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import { createReadStream } from "node:fs";
|
||||||
|
import { readdir } from "node:fs/promises";
|
||||||
|
import { homedir } from "node:os";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import { createInterface } from "node:readline";
|
||||||
|
export { hermesSessionMessageStats as table } from "./schema.ts";
|
||||||
|
|
||||||
|
const MEASUREMENT_WINDOW_MS = 900_000;
|
||||||
|
const MEASUREMENT_WINDOW_SECONDS = 900;
|
||||||
|
|
||||||
|
interface MessageCounts {
|
||||||
|
user: number;
|
||||||
|
assistant: number;
|
||||||
|
tool: number;
|
||||||
|
fileHadActivity: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function aggregateJsonlFile(filePath: string, cutoffMs: number, nowMs: number): Promise<MessageCounts> {
|
||||||
|
let user = 0;
|
||||||
|
let assistant = 0;
|
||||||
|
let tool = 0;
|
||||||
|
let fileHadActivity = false;
|
||||||
|
|
||||||
|
const input = createReadStream(filePath, { encoding: "utf8" });
|
||||||
|
const rl = createInterface({ input, crlfDelay: Infinity });
|
||||||
|
try {
|
||||||
|
for await (const line of rl) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed) continue;
|
||||||
|
let obj: unknown;
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(trimmed);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
typeof obj !== "object" || obj === null ||
|
||||||
|
typeof (obj as Record<string, unknown>).role !== "string" ||
|
||||||
|
typeof (obj as Record<string, unknown>).timestamp !== "string"
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const record = obj as { role: string; timestamp: string };
|
||||||
|
const t = Date.parse(record.timestamp);
|
||||||
|
if (!Number.isFinite(t) || t < cutoffMs || t > nowMs) continue;
|
||||||
|
|
||||||
|
const roleNorm = record.role.trim().toLowerCase();
|
||||||
|
if (roleNorm === "user") {
|
||||||
|
user++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
} else if (roleNorm === "assistant") {
|
||||||
|
assistant++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
} else if (roleNorm === "tool") {
|
||||||
|
tool++;
|
||||||
|
fileHadActivity = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
rl.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return { user, assistant, tool, fileHadActivity };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compute() {
|
||||||
|
const nowMs = Date.now();
|
||||||
|
const cutoffMs = nowMs - MEASUREMENT_WINDOW_MS;
|
||||||
|
const ts = nowMs;
|
||||||
|
|
||||||
|
let totalUserMessages = 0;
|
||||||
|
let totalAssistantMessages = 0;
|
||||||
|
let totalToolMessages = 0;
|
||||||
|
let activeSessions = 0;
|
||||||
|
|
||||||
|
const sessionsDir = join(homedir(), ".hermes", "sessions");
|
||||||
|
let files: string[] = [];
|
||||||
|
try {
|
||||||
|
const entries = await readdir(sessionsDir, { withFileTypes: true });
|
||||||
|
files = entries
|
||||||
|
.filter((e) => e.isFile() && e.name.endsWith(".jsonl"))
|
||||||
|
.map((e) => join(sessionsDir, e.name));
|
||||||
|
} catch (err) {
|
||||||
|
if (err && typeof err === "object" && "code" in err && (err as NodeJS.ErrnoException).code === "ENOENT") {
|
||||||
|
files = [];
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const filePath of files) {
|
||||||
|
const { user, assistant, tool, fileHadActivity } = await aggregateJsonlFile(
|
||||||
|
filePath,
|
||||||
|
cutoffMs,
|
||||||
|
nowMs,
|
||||||
|
);
|
||||||
|
totalUserMessages += user;
|
||||||
|
totalAssistantMessages += assistant;
|
||||||
|
totalToolMessages += tool;
|
||||||
|
if (fileHadActivity) activeSessions++;
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalMessages =
|
||||||
|
totalUserMessages + totalAssistantMessages + totalToolMessages;
|
||||||
|
|
||||||
|
const row = {
|
||||||
|
ts,
|
||||||
|
totalUserMessages,
|
||||||
|
totalAssistantMessages,
|
||||||
|
totalToolMessages,
|
||||||
|
totalMessages,
|
||||||
|
activeSessions,
|
||||||
|
measurementWindowSeconds: MEASUREMENT_WINDOW_SECONDS,
|
||||||
|
};
|
||||||
|
|
||||||
|
return { signal: row, workflow: null };
|
||||||
|
}
|
||||||
12
senses/hermes-session-message-stats/src/schema.ts
Normal file
12
senses/hermes-session-message-stats/src/schema.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { integer, sqliteTable } from "drizzle-orm/sqlite-core";
|
||||||
|
|
||||||
|
export const hermesSessionMessageStats = sqliteTable("hermes_session_message_stats", {
|
||||||
|
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||||
|
ts: integer("ts").notNull(),
|
||||||
|
totalUserMessages: integer("total_user_messages").notNull(),
|
||||||
|
totalAssistantMessages: integer("total_assistant_messages").notNull(),
|
||||||
|
totalToolMessages: integer("total_tool_messages").notNull(),
|
||||||
|
totalMessages: integer("total_messages").notNull(),
|
||||||
|
activeSessions: integer("active_sessions").notNull(),
|
||||||
|
measurementWindowSeconds: integer("measurement_window_seconds").notNull(),
|
||||||
|
});
|
||||||
107
senses/linux-system-health/index.js
Normal file
107
senses/linux-system-health/index.js
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
// src/index.ts
|
||||||
|
import { loadavg, totalmem, freemem, uptime } from "node:os";
|
||||||
|
import { execSync } from "node:child_process";
|
||||||
|
import { readFile } from "node:fs/promises";
|
||||||
|
|
||||||
|
// src/schema.ts
|
||||||
|
import { integer, real, sqliteTable } from "drizzle-orm/sqlite-core";
|
||||||
|
var snapshots = sqliteTable("snapshots", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
cpuLoad1m: real("cpu_load_1m").notNull(),
|
||||||
|
cpuLoad5m: real("cpu_load_5m").notNull(),
|
||||||
|
cpuLoad15m: real("cpu_load_15m").notNull(),
|
||||||
|
memTotalMB: integer("mem_total_mb").notNull(),
|
||||||
|
memUsedMB: integer("mem_used_mb").notNull(),
|
||||||
|
memUsedPct: real("mem_used_pct").notNull(),
|
||||||
|
diskTotalGB: real("disk_total_gb").notNull(),
|
||||||
|
diskUsedGB: real("disk_used_gb").notNull(),
|
||||||
|
diskUsedPct: real("disk_used_pct").notNull(),
|
||||||
|
uptimeSec: integer("uptime_sec").notNull(),
|
||||||
|
// TCP socket stats (merged from linux-tcp-socket-stats)
|
||||||
|
socketsUsed: integer("sockets_used"),
|
||||||
|
tcpInuse: integer("tcp_inuse"),
|
||||||
|
tcpOrphan: integer("tcp_orphan"),
|
||||||
|
tcpTw: integer("tcp_tw"),
|
||||||
|
tcpAlloc: integer("tcp_alloc"),
|
||||||
|
tcpMemPages: integer("tcp_mem_pages")
|
||||||
|
});
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var SOCKSTAT_PATH = "/proc/net/sockstat";
|
||||||
|
function parseSockstat(content) {
|
||||||
|
let socketsUsed = 0, tcpInuse = 0, tcpOrphan = 0, tcpTw = 0, tcpAlloc = 0, tcpMemPages = 0;
|
||||||
|
for (const line of content.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (trimmed.startsWith("sockets:")) {
|
||||||
|
const parts = trimmed.split(/\s+/);
|
||||||
|
const idx = parts.indexOf("used");
|
||||||
|
if (idx !== -1 && idx + 1 < parts.length) {
|
||||||
|
socketsUsed = Number.parseInt(parts[idx + 1], 10) || 0;
|
||||||
|
}
|
||||||
|
} else if (trimmed.startsWith("TCP:")) {
|
||||||
|
const parts = trimmed.split(/\s+/);
|
||||||
|
const map = {};
|
||||||
|
for (let i = 1; i + 1 < parts.length; i += 2) {
|
||||||
|
map[parts[i]] = Number.parseInt(parts[i + 1], 10) || 0;
|
||||||
|
}
|
||||||
|
tcpInuse = map.inuse ?? 0;
|
||||||
|
tcpOrphan = map.orphan ?? 0;
|
||||||
|
tcpTw = map.tw ?? 0;
|
||||||
|
tcpAlloc = map.alloc ?? 0;
|
||||||
|
tcpMemPages = map.mem ?? 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { socketsUsed, tcpInuse, tcpOrphan, tcpTw, tcpAlloc, tcpMemPages };
|
||||||
|
}
|
||||||
|
async function compute() {
|
||||||
|
const [load1, load5, load15] = loadavg();
|
||||||
|
const memTotal = totalmem();
|
||||||
|
const memFree = freemem();
|
||||||
|
const memUsed = memTotal - memFree;
|
||||||
|
const memTotalMB = Math.round(memTotal / 1024 / 1024);
|
||||||
|
const memUsedMB = Math.round(memUsed / 1024 / 1024);
|
||||||
|
const memUsedPct = Math.round(memUsed / memTotal * 1e4) / 100;
|
||||||
|
let diskTotalGB = 0, diskUsedGB = 0, diskUsedPct = 0;
|
||||||
|
try {
|
||||||
|
const df = execSync("df -B1 / | tail -1", { encoding: "utf-8" }).trim();
|
||||||
|
const parts = df.split(/\s+/);
|
||||||
|
const total = Number(parts[1]);
|
||||||
|
const used = Number(parts[2]);
|
||||||
|
diskTotalGB = Math.round(total / 1024 / 1024 / 1024 * 100) / 100;
|
||||||
|
diskUsedGB = Math.round(used / 1024 / 1024 / 1024 * 100) / 100;
|
||||||
|
diskUsedPct = total > 0 ? Math.round(used / total * 1e4) / 100 : 0;
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
let tcp = { socketsUsed: 0, tcpInuse: 0, tcpOrphan: 0, tcpTw: 0, tcpAlloc: 0, tcpMemPages: 0 };
|
||||||
|
try {
|
||||||
|
const content = await readFile(SOCKSTAT_PATH, "utf8");
|
||||||
|
tcp = parseSockstat(content);
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
const ts = Date.now();
|
||||||
|
const uptimeSec = Math.round(uptime());
|
||||||
|
const data = {
|
||||||
|
ts,
|
||||||
|
cpuLoad1m: load1,
|
||||||
|
cpuLoad5m: load5,
|
||||||
|
cpuLoad15m: load15,
|
||||||
|
memTotalMB,
|
||||||
|
memUsedMB,
|
||||||
|
memUsedPct,
|
||||||
|
diskTotalGB,
|
||||||
|
diskUsedGB,
|
||||||
|
diskUsedPct,
|
||||||
|
uptimeSec,
|
||||||
|
socketsUsed: tcp.socketsUsed,
|
||||||
|
tcpInuse: tcp.tcpInuse,
|
||||||
|
tcpOrphan: tcp.tcpOrphan,
|
||||||
|
tcpTw: tcp.tcpTw,
|
||||||
|
tcpAlloc: tcp.tcpAlloc,
|
||||||
|
tcpMemPages: tcp.tcpMemPages
|
||||||
|
};
|
||||||
|
return { signal: data, workflow: null };
|
||||||
|
}
|
||||||
|
export {
|
||||||
|
compute,
|
||||||
|
snapshots as table
|
||||||
|
};
|
||||||
16
senses/linux-system-health/migrations/0001_init.sql
Normal file
16
senses/linux-system-health/migrations/0001_init.sql
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
-- Migration: 0001_init
|
||||||
|
-- Creates the snapshots table for linux-system-health sense.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS snapshots (
|
||||||
|
ts INTEGER PRIMARY KEY,
|
||||||
|
cpu_load_1m REAL NOT NULL,
|
||||||
|
cpu_load_5m REAL NOT NULL,
|
||||||
|
cpu_load_15m REAL NOT NULL,
|
||||||
|
mem_total_mb INTEGER NOT NULL,
|
||||||
|
mem_used_mb INTEGER NOT NULL,
|
||||||
|
mem_used_pct REAL NOT NULL,
|
||||||
|
disk_total_gb REAL NOT NULL,
|
||||||
|
disk_used_gb REAL NOT NULL,
|
||||||
|
disk_used_pct REAL NOT NULL,
|
||||||
|
uptime_sec INTEGER NOT NULL
|
||||||
|
);
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
ALTER TABLE snapshots ADD COLUMN sockets_used INTEGER;
|
||||||
|
ALTER TABLE snapshots ADD COLUMN tcp_inuse INTEGER;
|
||||||
|
ALTER TABLE snapshots ADD COLUMN tcp_orphan INTEGER;
|
||||||
|
ALTER TABLE snapshots ADD COLUMN tcp_tw INTEGER;
|
||||||
|
ALTER TABLE snapshots ADD COLUMN tcp_alloc INTEGER;
|
||||||
|
ALTER TABLE snapshots ADD COLUMN tcp_mem_pages INTEGER;
|
||||||
14
senses/linux-system-health/package.json
Normal file
14
senses/linux-system-health/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "sense-linux-system-health",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=index.js --packages=external"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
88
senses/linux-system-health/src/index.ts
Normal file
88
senses/linux-system-health/src/index.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { loadavg, totalmem, freemem, uptime } from "node:os";
|
||||||
|
import { execSync } from "node:child_process";
|
||||||
|
import { readFile } from "node:fs/promises";
|
||||||
|
export { snapshots as table } from "./schema.ts";
|
||||||
|
|
||||||
|
const SOCKSTAT_PATH = "/proc/net/sockstat";
|
||||||
|
|
||||||
|
interface SockstatResult {
|
||||||
|
socketsUsed: number;
|
||||||
|
tcpInuse: number;
|
||||||
|
tcpOrphan: number;
|
||||||
|
tcpTw: number;
|
||||||
|
tcpAlloc: number;
|
||||||
|
tcpMemPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseSockstat(content: string): SockstatResult {
|
||||||
|
let socketsUsed = 0, tcpInuse = 0, tcpOrphan = 0, tcpTw = 0, tcpAlloc = 0, tcpMemPages = 0;
|
||||||
|
|
||||||
|
for (const line of content.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (trimmed.startsWith("sockets:")) {
|
||||||
|
const parts = trimmed.split(/\s+/);
|
||||||
|
const idx = parts.indexOf("used");
|
||||||
|
if (idx !== -1 && idx + 1 < parts.length) {
|
||||||
|
socketsUsed = Number.parseInt(parts[idx + 1], 10) || 0;
|
||||||
|
}
|
||||||
|
} else if (trimmed.startsWith("TCP:")) {
|
||||||
|
const parts = trimmed.split(/\s+/);
|
||||||
|
const map: Record<string, number> = {};
|
||||||
|
for (let i = 1; i + 1 < parts.length; i += 2) {
|
||||||
|
map[parts[i]] = Number.parseInt(parts[i + 1], 10) || 0;
|
||||||
|
}
|
||||||
|
tcpInuse = map.inuse ?? 0;
|
||||||
|
tcpOrphan = map.orphan ?? 0;
|
||||||
|
tcpTw = map.tw ?? 0;
|
||||||
|
tcpAlloc = map.alloc ?? 0;
|
||||||
|
tcpMemPages = map.mem ?? 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { socketsUsed, tcpInuse, tcpOrphan, tcpTw, tcpAlloc, tcpMemPages };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compute() {
|
||||||
|
const [load1, load5, load15] = loadavg();
|
||||||
|
|
||||||
|
const memTotal = totalmem();
|
||||||
|
const memFree = freemem();
|
||||||
|
const memUsed = memTotal - memFree;
|
||||||
|
const memTotalMB = Math.round(memTotal / 1024 / 1024);
|
||||||
|
const memUsedMB = Math.round(memUsed / 1024 / 1024);
|
||||||
|
const memUsedPct = Math.round((memUsed / memTotal) * 10000) / 100;
|
||||||
|
|
||||||
|
let diskTotalGB = 0, diskUsedGB = 0, diskUsedPct = 0;
|
||||||
|
try {
|
||||||
|
const df = execSync("df -B1 / | tail -1", { encoding: "utf-8" }).trim();
|
||||||
|
const parts = df.split(/\s+/);
|
||||||
|
const total = Number(parts[1]);
|
||||||
|
const used = Number(parts[2]);
|
||||||
|
diskTotalGB = Math.round(total / 1024 / 1024 / 1024 * 100) / 100;
|
||||||
|
diskUsedGB = Math.round(used / 1024 / 1024 / 1024 * 100) / 100;
|
||||||
|
diskUsedPct = total > 0 ? Math.round((used / total) * 10000) / 100 : 0;
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
let tcp: SockstatResult = { socketsUsed: 0, tcpInuse: 0, tcpOrphan: 0, tcpTw: 0, tcpAlloc: 0, tcpMemPages: 0 };
|
||||||
|
try {
|
||||||
|
const content = await readFile(SOCKSTAT_PATH, "utf8");
|
||||||
|
tcp = parseSockstat(content);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
const ts = Date.now();
|
||||||
|
const uptimeSec = Math.round(uptime());
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
ts, cpuLoad1m: load1, cpuLoad5m: load5, cpuLoad15m: load15,
|
||||||
|
memTotalMB, memUsedMB, memUsedPct,
|
||||||
|
diskTotalGB, diskUsedGB, diskUsedPct,
|
||||||
|
uptimeSec,
|
||||||
|
socketsUsed: tcp.socketsUsed,
|
||||||
|
tcpInuse: tcp.tcpInuse,
|
||||||
|
tcpOrphan: tcp.tcpOrphan,
|
||||||
|
tcpTw: tcp.tcpTw,
|
||||||
|
tcpAlloc: tcp.tcpAlloc,
|
||||||
|
tcpMemPages: tcp.tcpMemPages,
|
||||||
|
};
|
||||||
|
return { signal: data, workflow: null };
|
||||||
|
}
|
||||||
22
senses/linux-system-health/src/schema.ts
Normal file
22
senses/linux-system-health/src/schema.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||||
|
|
||||||
|
export const snapshots = sqliteTable("snapshots", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
cpuLoad1m: real("cpu_load_1m").notNull(),
|
||||||
|
cpuLoad5m: real("cpu_load_5m").notNull(),
|
||||||
|
cpuLoad15m: real("cpu_load_15m").notNull(),
|
||||||
|
memTotalMB: integer("mem_total_mb").notNull(),
|
||||||
|
memUsedMB: integer("mem_used_mb").notNull(),
|
||||||
|
memUsedPct: real("mem_used_pct").notNull(),
|
||||||
|
diskTotalGB: real("disk_total_gb").notNull(),
|
||||||
|
diskUsedGB: real("disk_used_gb").notNull(),
|
||||||
|
diskUsedPct: real("disk_used_pct").notNull(),
|
||||||
|
uptimeSec: integer("uptime_sec").notNull(),
|
||||||
|
// TCP socket stats (merged from linux-tcp-socket-stats)
|
||||||
|
socketsUsed: integer("sockets_used"),
|
||||||
|
tcpInuse: integer("tcp_inuse"),
|
||||||
|
tcpOrphan: integer("tcp_orphan"),
|
||||||
|
tcpTw: integer("tcp_tw"),
|
||||||
|
tcpAlloc: integer("tcp_alloc"),
|
||||||
|
tcpMemPages: integer("tcp_mem_pages"),
|
||||||
|
});
|
||||||
37
senses/worker-process-metrics/index.js
Normal file
37
senses/worker-process-metrics/index.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// src/schema.ts
|
||||||
|
import { integer, real, sqliteTable } from "drizzle-orm/sqlite-core";
|
||||||
|
var workerProcessMetrics = sqliteTable("worker_process_metrics", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
pid: integer("pid").notNull(),
|
||||||
|
uptimeSec: real("uptime_sec").notNull(),
|
||||||
|
heapUsedMB: real("heap_used_mb").notNull(),
|
||||||
|
rssMB: real("rss_mb").notNull(),
|
||||||
|
externalMB: real("external_mb").notNull()
|
||||||
|
});
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
function round2(n) {
|
||||||
|
return Math.round(n * 100) / 100;
|
||||||
|
}
|
||||||
|
async function compute() {
|
||||||
|
const ts = Date.now();
|
||||||
|
const pid = process.pid;
|
||||||
|
const uptimeSec = process.uptime();
|
||||||
|
const m = process.memoryUsage();
|
||||||
|
const heapUsedMB = round2(m.heapUsed / 1024 / 1024);
|
||||||
|
const rssMB = round2(m.rss / 1024 / 1024);
|
||||||
|
const externalMB = round2(m.external / 1024 / 1024);
|
||||||
|
const row = {
|
||||||
|
ts,
|
||||||
|
pid,
|
||||||
|
uptimeSec,
|
||||||
|
heapUsedMB,
|
||||||
|
rssMB,
|
||||||
|
externalMB
|
||||||
|
};
|
||||||
|
return { signal: row, workflow: null };
|
||||||
|
}
|
||||||
|
export {
|
||||||
|
compute,
|
||||||
|
workerProcessMetrics as table
|
||||||
|
};
|
||||||
11
senses/worker-process-metrics/migrations/0001_init.sql
Normal file
11
senses/worker-process-metrics/migrations/0001_init.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
-- Migration: 0001_init
|
||||||
|
-- Creates the worker_process_metrics table for worker-process-metrics sense.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS worker_process_metrics (
|
||||||
|
ts INTEGER PRIMARY KEY,
|
||||||
|
pid INTEGER NOT NULL,
|
||||||
|
uptime_sec REAL NOT NULL,
|
||||||
|
heap_used_mb REAL NOT NULL,
|
||||||
|
rss_mb REAL NOT NULL,
|
||||||
|
external_mb REAL NOT NULL
|
||||||
|
);
|
||||||
14
senses/worker-process-metrics/package.json
Normal file
14
senses/worker-process-metrics/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "sense-worker-process-metrics",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=index.js --packages=external"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
26
senses/worker-process-metrics/src/index.ts
Normal file
26
senses/worker-process-metrics/src/index.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
export { workerProcessMetrics as table } from "./schema.ts";
|
||||||
|
|
||||||
|
function round2(n: number): number {
|
||||||
|
return Math.round(n * 100) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compute() {
|
||||||
|
const ts = Date.now();
|
||||||
|
const pid = process.pid;
|
||||||
|
const uptimeSec = process.uptime();
|
||||||
|
const m = process.memoryUsage();
|
||||||
|
const heapUsedMB = round2(m.heapUsed / 1024 / 1024);
|
||||||
|
const rssMB = round2(m.rss / 1024 / 1024);
|
||||||
|
const externalMB = round2(m.external / 1024 / 1024);
|
||||||
|
|
||||||
|
const row = {
|
||||||
|
ts,
|
||||||
|
pid,
|
||||||
|
uptimeSec,
|
||||||
|
heapUsedMB,
|
||||||
|
rssMB,
|
||||||
|
externalMB,
|
||||||
|
};
|
||||||
|
|
||||||
|
return { signal: row, workflow: null };
|
||||||
|
}
|
||||||
10
senses/worker-process-metrics/src/schema.ts
Normal file
10
senses/worker-process-metrics/src/schema.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { integer, real, sqliteTable } from "drizzle-orm/sqlite-core";
|
||||||
|
|
||||||
|
export const workerProcessMetrics = sqliteTable("worker_process_metrics", {
|
||||||
|
ts: integer("ts").primaryKey(),
|
||||||
|
pid: integer("pid").notNull(),
|
||||||
|
uptimeSec: real("uptime_sec").notNull(),
|
||||||
|
heapUsedMB: real("heap_used_mb").notNull(),
|
||||||
|
rssMB: real("rss_mb").notNull(),
|
||||||
|
externalMB: real("external_mb").notNull(),
|
||||||
|
});
|
||||||
1
workflows/develop-sense/.gitignore
vendored
Normal file
1
workflows/develop-sense/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist/
|
||||||
22
workflows/develop-sense/package.json
Normal file
22
workflows/develop-sense/package.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "generate-sense-workflow",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild index.ts --bundle --platform=node --format=esm --outdir=dist --packages=external"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-adapter-cursor": "latest",
|
||||||
|
"@uncaged/nerve-adapter-hermes": "latest",
|
||||||
|
"@uncaged/nerve-core": "latest",
|
||||||
|
"@uncaged/nerve-workflow-meta": "link:../../../repos/nerve/packages/workflow-meta",
|
||||||
|
"@uncaged/nerve-workflow-utils": "latest",
|
||||||
|
"zod": "^4.3.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
workflows/develop-sense/tsconfig.json
Normal file
14
workflows/develop-sense/tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"lib": ["ES2022"],
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noEmit": false,
|
||||||
|
"declaration": false,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["./**/*.ts", "../_shared/**/*.ts"]
|
||||||
|
}
|
||||||
1
workflows/develop-workflow/.gitignore
vendored
Normal file
1
workflows/develop-workflow/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist/
|
||||||
22
workflows/develop-workflow/package.json
Normal file
22
workflows/develop-workflow/package.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "generate-workflow-workflow",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild index.ts --bundle --platform=node --format=esm --outdir=dist --packages=external"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-adapter-cursor": "latest",
|
||||||
|
"@uncaged/nerve-adapter-hermes": "latest",
|
||||||
|
"@uncaged/nerve-core": "latest",
|
||||||
|
"@uncaged/nerve-workflow-meta": "link:../../../repos/nerve/packages/workflow-meta",
|
||||||
|
"@uncaged/nerve-workflow-utils": "latest",
|
||||||
|
"zod": "^4.3.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,13 +7,7 @@
|
|||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"types": ["node"]
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["./**/*.ts", "../_shared/**/*.ts"]
|
||||||
"senses/**/*.ts",
|
|
||||||
"workflows/**/*.ts",
|
|
||||||
"scripts/**/*.ts",
|
|
||||||
"workflows/_shared/**/*.ts"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import type { StartStep } from "@uncaged/nerve-core";
|
|
||||||
|
|
||||||
type StartMetaWithWorkdir = StartStep["meta"] & { workdir?: string | null };
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve the target repo working directory.
|
|
||||||
* Priority: start.meta.workdir → prompt second line (if absolute path) → cwd.
|
|
||||||
*/
|
|
||||||
export function resolveWorkdir(start: StartStep): string {
|
|
||||||
const m = start.meta as StartMetaWithWorkdir;
|
|
||||||
if (m.workdir) return m.workdir;
|
|
||||||
|
|
||||||
// Allow prompt to carry workdir on the second line: "seed\n/abs/path"
|
|
||||||
const lines = start.content.split(/\r?\n/);
|
|
||||||
if (lines.length >= 2) {
|
|
||||||
const candidate = lines[1]!.trim();
|
|
||||||
if (candidate.startsWith("/")) return candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return process.cwd();
|
|
||||||
}
|
|
||||||
@ -1,102 +0,0 @@
|
|||||||
import type { AgentFn, Role, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core";
|
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole, nerveCommandEnv, spawnSafe } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
import { resolveWorkdir } from "../lib/workdir.js";
|
|
||||||
|
|
||||||
import type { QuestionerMeta } from "./questioner.js";
|
|
||||||
|
|
||||||
export const answererMetaSchema = z.object({
|
|
||||||
results: z.array(
|
|
||||||
z.object({
|
|
||||||
id: z.string(),
|
|
||||||
found: z.boolean(),
|
|
||||||
source: z.string(),
|
|
||||||
note: z.string(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
has_unanswered: z.boolean(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export type AnswererMeta = z.infer<typeof answererMetaSchema>;
|
|
||||||
|
|
||||||
export type CreateAnswererRoleDeps = {
|
|
||||||
extract: LlmExtractorConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
function lastQuestionerMeta(messages: WorkflowMessage[]): QuestionerMeta | undefined {
|
|
||||||
for (let i = messages.length - 1; i >= 0; i--) {
|
|
||||||
if (messages[i].role === "questioner") {
|
|
||||||
return messages[i].meta as QuestionerMeta;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function answererPrompt(ctx: ThreadContext): Promise<string> {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const cwd = resolveWorkdir(ctx.start);
|
|
||||||
const qm = lastQuestionerMeta(messages);
|
|
||||||
if (!qm || qm.questions.length === 0) {
|
|
||||||
throw new Error("answerer: prompt invoked without questioner questions — wrapped role should short-circuit");
|
|
||||||
}
|
|
||||||
|
|
||||||
const blocks: string[] = [];
|
|
||||||
for (const q of qm.questions) {
|
|
||||||
if ((ctx.start.meta as Record<string, unknown>).dryRun) {
|
|
||||||
blocks.push(`### ${q.id}\n[dryRun] skipped nerve knowledge query\n`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const res = await spawnSafe(
|
|
||||||
"nerve",
|
|
||||||
["knowledge", "query", q.question],
|
|
||||||
{
|
|
||||||
cwd,
|
|
||||||
env: nerveCommandEnv(),
|
|
||||||
timeoutMs: 120_000,
|
|
||||||
dryRun: false,
|
|
||||||
abortSignal: null,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (res.ok) {
|
|
||||||
blocks.push(`### ${q.id} (${q.domain})\nQuestion: ${q.question}\n---\n${res.value.stdout}\n`);
|
|
||||||
} else {
|
|
||||||
const err = res.error;
|
|
||||||
const detail =
|
|
||||||
err.kind === "non_zero_exit"
|
|
||||||
? `exit ${err.exitCode}\n${err.stderr}`
|
|
||||||
: err.kind === "timeout"
|
|
||||||
? `timeout\n${err.stderr}`
|
|
||||||
: err.kind === "spawn_failed"
|
|
||||||
? err.message
|
|
||||||
: "aborted";
|
|
||||||
blocks.push(`### ${q.id}\nnerve knowledge query failed: ${detail}\n`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
"You are the **answerer**. You MUST NOT read repository source code — only the CLI retrieval excerpts below.",
|
|
||||||
"For each question id, decide whether the knowledge base already answers it.",
|
|
||||||
"Set found=true only when the excerpt supports a confident answer; otherwise found=false.",
|
|
||||||
"Set has_unanswered=true if any question remains unanswered by the knowledge base.",
|
|
||||||
"",
|
|
||||||
...blocks,
|
|
||||||
].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createAnswererRole(adapter: AgentFn, { extract }: CreateAnswererRoleDeps): Role<AnswererMeta> {
|
|
||||||
const inner = createRole(adapter, answererPrompt, answererMetaSchema, extract);
|
|
||||||
|
|
||||||
return async (ctx: ThreadContext) => {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const qm = lastQuestionerMeta(messages);
|
|
||||||
if (!qm || qm.questions.length === 0) {
|
|
||||||
return {
|
|
||||||
content: "answerer: no questions from questioner; skipping CLI lookup.",
|
|
||||||
meta: { results: [], has_unanswered: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return inner(ctx);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,108 +0,0 @@
|
|||||||
import { readFile } from "node:fs/promises";
|
|
||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
import type { AgentFn, Role, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core";
|
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
import { resolveQueueForQuestioner } from "../lib/knowledge-queue.js";
|
|
||||||
import { resolveWorkdir } from "../lib/workdir.js";
|
|
||||||
|
|
||||||
const questionerExtractSchema = z.object({
|
|
||||||
questions: z
|
|
||||||
.array(
|
|
||||||
z.object({
|
|
||||||
id: z.string(),
|
|
||||||
question: z.string(),
|
|
||||||
domain: z.string(),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.length(5),
|
|
||||||
});
|
|
||||||
|
|
||||||
export type QuestionerMeta = {
|
|
||||||
/** Empty when no .knowledge cards and no work to do. */
|
|
||||||
card: string;
|
|
||||||
questions: { id: string; question: string; domain: string }[];
|
|
||||||
remaining_queue: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateQuestionerRoleDeps = {
|
|
||||||
extract: LlmExtractorConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
function questionerSystem(): string {
|
|
||||||
return `You are the **questioner** in an extract-knowledge workflow.
|
|
||||||
|
|
||||||
Read the given markdown knowledge card. Propose exactly **five** technical questions that are **not** already answered or covered by that card.
|
|
||||||
|
|
||||||
Rules:
|
|
||||||
- Questions must be concrete and technical.
|
|
||||||
- Each question needs a stable string id (e.g. q1, q2, q3, q4, q5), a short domain label (e.g. routing, storage), and the question text.
|
|
||||||
- Do not assume access to other files or tools — reason only from the card content shown.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function questionerUser(card: string, cardBody: string, remainingHint: string[]): string {
|
|
||||||
return `Current card path: ${card}
|
|
||||||
|
|
||||||
Remaining queue after this card (paths, may be empty): ${JSON.stringify(remainingHint)}
|
|
||||||
|
|
||||||
--- Card content ---
|
|
||||||
|
|
||||||
${cardBody}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function questionerPrompt(ctx: ThreadContext): Promise<string> {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const cwd = resolveWorkdir(ctx.start);
|
|
||||||
const queue = await resolveQueueForQuestioner(ctx.start, messages, cwd);
|
|
||||||
if (queue.length === 0) {
|
|
||||||
throw new Error(
|
|
||||||
"questioner: prompt invoked with empty queue — wrapped role should short-circuit before LLM",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const card = queue[0]!;
|
|
||||||
const remaining_queue = queue.slice(1);
|
|
||||||
let cardBody: string;
|
|
||||||
try {
|
|
||||||
cardBody = await readFile(join(cwd, card), "utf8");
|
|
||||||
} catch (e) {
|
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
|
||||||
throw new Error(`questioner: failed to read ${card}: ${msg}`);
|
|
||||||
}
|
|
||||||
return `${questionerSystem()}\n\n${questionerUser(card, cardBody, remaining_queue)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createQuestionerRole(adapter: AgentFn, { extract }: CreateQuestionerRoleDeps): Role<QuestionerMeta> {
|
|
||||||
const inner = createRole(adapter, questionerPrompt, questionerExtractSchema, extract);
|
|
||||||
|
|
||||||
return async (ctx: ThreadContext) => {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const cwd = resolveWorkdir(ctx.start);
|
|
||||||
const queue = await resolveQueueForQuestioner(ctx.start, messages, cwd);
|
|
||||||
if (queue.length === 0) {
|
|
||||||
return {
|
|
||||||
content:
|
|
||||||
"questioner: no `.knowledge` markdown files found and no seed path in the trigger prompt; queue is empty.",
|
|
||||||
meta: {
|
|
||||||
card: "",
|
|
||||||
questions: [],
|
|
||||||
remaining_queue: [],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const card = queue[0]!;
|
|
||||||
const remaining_queue = queue.slice(1);
|
|
||||||
const r = await inner(ctx);
|
|
||||||
return {
|
|
||||||
content: r.content,
|
|
||||||
meta: {
|
|
||||||
card,
|
|
||||||
questions: r.meta.questions,
|
|
||||||
remaining_queue,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
3
workflows/knowledge-extraction/.gitignore
vendored
Normal file
3
workflows/knowledge-extraction/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
false/
|
||||||
@ -1,6 +1,5 @@
|
|||||||
import type { AgentFn, WorkflowDefinition } from "@uncaged/nerve-core";
|
import type { AgentFn, WorkflowDefinition } from "@uncaged/nerve-core";
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
import { createLlmAdapter } from "@uncaged/nerve-workflow-utils";
|
|
||||||
|
|
||||||
import { moderator } from "./moderator.js";
|
import { moderator } from "./moderator.js";
|
||||||
import type { WorkflowMeta } from "./moderator.js";
|
import type { WorkflowMeta } from "./moderator.js";
|
||||||
@ -20,12 +19,11 @@ export function createKnowledgeExtractionWorkflow({
|
|||||||
extract,
|
extract,
|
||||||
}: CreateKnowledgeExtractionDeps): WorkflowDefinition<WorkflowMeta> {
|
}: CreateKnowledgeExtractionDeps): WorkflowDefinition<WorkflowMeta> {
|
||||||
const a = (role: keyof WorkflowMeta) => adapters?.[role] ?? defaultAdapter;
|
const a = (role: keyof WorkflowMeta) => adapters?.[role] ?? defaultAdapter;
|
||||||
const llmAdapter = createLlmAdapter(extract.provider);
|
|
||||||
return {
|
return {
|
||||||
name: "extract-knowledge",
|
name: "knowledge-extraction",
|
||||||
roles: {
|
roles: {
|
||||||
questioner: createQuestionerRole(adapters?.questioner ?? llmAdapter, { extract }),
|
questioner: createQuestionerRole({ extract }),
|
||||||
answerer: createAnswererRole(adapters?.answerer ?? llmAdapter, { extract }),
|
answerer: createAnswererRole({ extract }),
|
||||||
explorer: createExplorerRole(a("explorer"), { extract }),
|
explorer: createExplorerRole(a("explorer"), { extract }),
|
||||||
},
|
},
|
||||||
moderator,
|
moderator,
|
||||||
@ -20,7 +20,7 @@ const workflow = createKnowledgeExtractionWorkflow({
|
|||||||
adapters: {
|
adapters: {
|
||||||
explorer: createCursorAdapter({
|
explorer: createCursorAdapter({
|
||||||
type: "cursor",
|
type: "cursor",
|
||||||
model: "claude-sonnet-4",
|
model: "auto",
|
||||||
timeout: CURSOR_TIMEOUT_MS,
|
timeout: CURSOR_TIMEOUT_MS,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
8
workflows/knowledge-extraction/lib/workdir.ts
Normal file
8
workflows/knowledge-extraction/lib/workdir.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import type { StartStep } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
|
type StartMetaWithWorkdir = StartStep["meta"] & { workdir?: string | null };
|
||||||
|
|
||||||
|
export function resolveWorkdir(start: StartStep): string {
|
||||||
|
const m = start.meta as StartMetaWithWorkdir;
|
||||||
|
return m.workdir ?? process.cwd();
|
||||||
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import { END } from "@uncaged/nerve-core";
|
import { END } from "@uncaged/nerve-core";
|
||||||
import type { Moderator, ThreadContext } from "@uncaged/nerve-core";
|
import type { Moderator, ModeratorContext } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
import type { AnswererMeta } from "./roles/answerer.js";
|
import type { AnswererMeta } from "./roles/answerer.js";
|
||||||
import type { ExplorerMeta } from "./roles/explorer.js";
|
import type { ExplorerMeta } from "./roles/explorer.js";
|
||||||
@ -11,7 +11,7 @@ export type WorkflowMeta = {
|
|||||||
explorer: ExplorerMeta;
|
explorer: ExplorerMeta;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Steps = ThreadContext<WorkflowMeta>["steps"];
|
type Steps = ModeratorContext<WorkflowMeta>["steps"];
|
||||||
|
|
||||||
function lastQuestionerRemaining(steps: Steps): QuestionerMeta | undefined {
|
function lastQuestionerRemaining(steps: Steps): QuestionerMeta | undefined {
|
||||||
for (let i = steps.length - 1; i >= 0; i--) {
|
for (let i = steps.length - 1; i >= 0; i--) {
|
||||||
21
workflows/knowledge-extraction/package.json
Normal file
21
workflows/knowledge-extraction/package.json
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "knowledge-extraction-workflow",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild index.ts --bundle --platform=node --format=esm --outdir=dist --packages=external"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-adapter-cursor": "latest",
|
||||||
|
"@uncaged/nerve-adapter-hermes": "latest",
|
||||||
|
"@uncaged/nerve-core": "latest",
|
||||||
|
"@uncaged/nerve-workflow-utils": "latest",
|
||||||
|
"zod": "^4.3.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
104
workflows/knowledge-extraction/roles/answerer.ts
Normal file
104
workflows/knowledge-extraction/roles/answerer.ts
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
import type { Role, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { llmExtract, nerveCommandEnv, spawnSafe } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { resolveWorkdir } from "../lib/workdir.js";
|
||||||
|
|
||||||
|
import type { QuestionerMeta } from "./questioner.js";
|
||||||
|
|
||||||
|
export const answererMetaSchema = z.object({
|
||||||
|
results: z.array(
|
||||||
|
z.object({
|
||||||
|
id: z.string(),
|
||||||
|
found: z.boolean(),
|
||||||
|
source: z.string(),
|
||||||
|
note: z.string(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
has_unanswered: z.boolean(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AnswererMeta = z.infer<typeof answererMetaSchema>;
|
||||||
|
|
||||||
|
export type CreateAnswererRoleDeps = {
|
||||||
|
extract: LlmExtractorConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
function lastQuestionerMeta(messages: WorkflowMessage[]): QuestionerMeta | undefined {
|
||||||
|
for (let i = messages.length - 1; i >= 0; i--) {
|
||||||
|
if (messages[i].role === "questioner") {
|
||||||
|
return messages[i].meta as QuestionerMeta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createAnswererRole(deps: CreateAnswererRoleDeps): Role<AnswererMeta> {
|
||||||
|
const { extract } = deps;
|
||||||
|
|
||||||
|
return async (start: StartStep, messages: WorkflowMessage[]) => {
|
||||||
|
const cwd = resolveWorkdir(start);
|
||||||
|
const qm = lastQuestionerMeta(messages);
|
||||||
|
if (!qm || qm.questions.length === 0) {
|
||||||
|
return {
|
||||||
|
content: "answerer: no questions from questioner; skipping CLI lookup.",
|
||||||
|
meta: { results: [], has_unanswered: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const blocks: string[] = [];
|
||||||
|
for (const q of qm.questions) {
|
||||||
|
if (start.meta.dryRun) {
|
||||||
|
blocks.push(`### ${q.id}\n[dryRun] skipped nerve knowledge query\n`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const res = await spawnSafe(
|
||||||
|
"nerve",
|
||||||
|
["knowledge", "query", q.question],
|
||||||
|
{
|
||||||
|
cwd,
|
||||||
|
env: nerveCommandEnv(),
|
||||||
|
timeoutMs: 120_000,
|
||||||
|
dryRun: false,
|
||||||
|
abortSignal: null,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (res.ok) {
|
||||||
|
blocks.push(`### ${q.id} (${q.domain})\nQuestion: ${q.question}\n---\n${res.value.stdout}\n`);
|
||||||
|
} else {
|
||||||
|
const err = res.error;
|
||||||
|
const detail =
|
||||||
|
err.kind === "non_zero_exit"
|
||||||
|
? `exit ${err.exitCode}\n${err.stderr}`
|
||||||
|
: err.kind === "timeout"
|
||||||
|
? `timeout\n${err.stderr}`
|
||||||
|
: err.kind === "spawn_failed"
|
||||||
|
? err.message
|
||||||
|
: "aborted";
|
||||||
|
blocks.push(`### ${q.id}\nnerve knowledge query failed: ${detail}\n`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bundle = [
|
||||||
|
"You are the **answerer**. You MUST NOT read repository source code — only the CLI retrieval excerpts below.",
|
||||||
|
"For each question id, decide whether the knowledge base already answers it.",
|
||||||
|
"Set found=true only when the excerpt supports a confident answer; otherwise found=false.",
|
||||||
|
"Set has_unanswered=true if any question remains unanswered by the knowledge base.",
|
||||||
|
"",
|
||||||
|
...blocks,
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
const metaR = await llmExtract({
|
||||||
|
text: bundle,
|
||||||
|
schema: answererMetaSchema,
|
||||||
|
provider: extract.provider,
|
||||||
|
dryRun: start.meta.dryRun,
|
||||||
|
});
|
||||||
|
if (!metaR.ok) {
|
||||||
|
throw new Error(`answerer llmExtract: ${JSON.stringify(metaR.error)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { content: bundle, meta: metaR.value };
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core";
|
import type { AgentFn, Role, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@ -33,17 +33,16 @@ function lastMeta<M>(messages: WorkflowMessage[], role: string): M | undefined {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function explorerPrompt(ctx: ThreadContext): string {
|
export function explorerPrompt(start: StartStep, messages: WorkflowMessage[]): string {
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
const threadId = start.meta.threadId;
|
||||||
const threadId = ctx.start.meta.threadId;
|
|
||||||
const qm = lastMeta<QuestionerMeta>(messages, "questioner");
|
const qm = lastMeta<QuestionerMeta>(messages, "questioner");
|
||||||
const am = lastMeta<AnswererMeta>(messages, "answerer");
|
const am = lastMeta<AnswererMeta>(messages, "answerer");
|
||||||
const cwd = resolveWorkdir(ctx.start);
|
const cwd = resolveWorkdir(start);
|
||||||
|
|
||||||
const unanswered =
|
const unanswered =
|
||||||
am?.results.filter((r) => !r.found).map((r) => r.id) ?? [];
|
am?.results.filter((r) => !r.found).map((r) => r.id) ?? [];
|
||||||
|
|
||||||
return `You are the **explorer** in an extract-knowledge workflow.
|
return `You are the **explorer** in a knowledge-extraction workflow.
|
||||||
|
|
||||||
## Context
|
## Context
|
||||||
|
|
||||||
@ -86,7 +85,7 @@ export function createExplorerRole(
|
|||||||
): Role<ExplorerMeta> {
|
): Role<ExplorerMeta> {
|
||||||
return createRole(
|
return createRole(
|
||||||
adapter,
|
adapter,
|
||||||
async (ctx: ThreadContext) => explorerPrompt(ctx),
|
async (innerStart: StartStep, msgs: WorkflowMessage[]) => explorerPrompt(innerStart, msgs),
|
||||||
explorerMetaSchema,
|
explorerMetaSchema,
|
||||||
extract,
|
extract,
|
||||||
);
|
);
|
||||||
106
workflows/knowledge-extraction/roles/questioner.ts
Normal file
106
workflows/knowledge-extraction/roles/questioner.ts
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
import { readFile } from "node:fs/promises";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import type { Role, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createLlmRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { resolveQueueForQuestioner } from "../lib/knowledge-queue.js";
|
||||||
|
import { resolveWorkdir } from "../lib/workdir.js";
|
||||||
|
|
||||||
|
const questionerExtractSchema = z.object({
|
||||||
|
questions: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
id: z.string(),
|
||||||
|
question: z.string(),
|
||||||
|
domain: z.string(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.length(3),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type QuestionerMeta = {
|
||||||
|
/** Empty when no .knowledge cards and no work to do. */
|
||||||
|
card: string;
|
||||||
|
questions: { id: string; question: string; domain: string }[];
|
||||||
|
remaining_queue: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CreateQuestionerRoleDeps = {
|
||||||
|
extract: LlmExtractorConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
function questionerSystem(): string {
|
||||||
|
return `You are the **questioner** in a knowledge-extraction workflow.
|
||||||
|
|
||||||
|
Read the given markdown knowledge card. Propose exactly **three** technical questions that are **not** already answered or covered by that card.
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- Questions must be concrete and technical.
|
||||||
|
- Each question needs a stable string id (e.g. q1, q2, q3), a short domain label (e.g. routing, storage), and the question text.
|
||||||
|
- Do not assume access to other files or tools — reason only from the card content shown.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function questionerUser(card: string, cardBody: string, remainingHint: string[]): string {
|
||||||
|
return `Current card path: ${card}
|
||||||
|
|
||||||
|
Remaining queue after this card (paths, may be empty): ${JSON.stringify(remainingHint)}
|
||||||
|
|
||||||
|
--- Card content ---
|
||||||
|
|
||||||
|
${cardBody}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createQuestionerRole(adapterExtract: CreateQuestionerRoleDeps): Role<QuestionerMeta> {
|
||||||
|
const { extract } = adapterExtract;
|
||||||
|
|
||||||
|
return async (start: StartStep, messages: WorkflowMessage[]) => {
|
||||||
|
const cwd = resolveWorkdir(start);
|
||||||
|
const queue = await resolveQueueForQuestioner(start, messages, cwd);
|
||||||
|
if (queue.length === 0) {
|
||||||
|
return {
|
||||||
|
content:
|
||||||
|
"questioner: no `.knowledge` markdown files found and no seed path in the trigger prompt; queue is empty.",
|
||||||
|
meta: {
|
||||||
|
card: "",
|
||||||
|
questions: [],
|
||||||
|
remaining_queue: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const card = queue[0]!;
|
||||||
|
const remaining_queue = queue.slice(1);
|
||||||
|
let cardBody: string;
|
||||||
|
try {
|
||||||
|
cardBody = await readFile(join(cwd, card), "utf8");
|
||||||
|
} catch (e) {
|
||||||
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
|
throw new Error(`questioner: failed to read ${card}: ${msg}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inner = createLlmRole({
|
||||||
|
provider: extract.provider,
|
||||||
|
prompt: async () => [
|
||||||
|
{ role: "system", content: questionerSystem() },
|
||||||
|
{ role: "user", content: questionerUser(card, cardBody, remaining_queue) },
|
||||||
|
],
|
||||||
|
extract: {
|
||||||
|
schema: questionerExtractSchema,
|
||||||
|
provider: extract.provider,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const r = await inner(start, messages);
|
||||||
|
return {
|
||||||
|
content: r.content,
|
||||||
|
meta: {
|
||||||
|
card,
|
||||||
|
questions: r.meta.questions,
|
||||||
|
remaining_queue,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
13
workflows/knowledge-extraction/tsconfig.json
Normal file
13
workflows/knowledge-extraction/tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"lib": ["ES2022"],
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["./**/*.ts"]
|
||||||
|
}
|
||||||
1
workflows/solve-issue/.gitignore
vendored
Normal file
1
workflows/solve-issue/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist/
|
||||||
@ -3,14 +3,14 @@ import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|||||||
|
|
||||||
import { moderator } from "./moderator.js";
|
import { moderator } from "./moderator.js";
|
||||||
import type { WorkflowMeta } from "./moderator.js";
|
import type { WorkflowMeta } from "./moderator.js";
|
||||||
import { createCommitterRole } from "./roles/committer.js";
|
import { createCommitterRole } from "./roles/committer/index.js";
|
||||||
import { createImplementRole } from "./roles/implement.js";
|
import { createImplementRole } from "./roles/implement/index.js";
|
||||||
import { createPlanRole } from "./roles/plan.js";
|
import { createPlanRole } from "./roles/plan/index.js";
|
||||||
import { createPrepareRole } from "./roles/prepare.js";
|
import { createPrepareRole } from "./roles/prepare/index.js";
|
||||||
import { createPublishRole } from "./roles/publish.js";
|
import { createPublishRole } from "./roles/publish/index.js";
|
||||||
import { createReadIssueRole } from "./roles/read-issue.js";
|
import { createReadIssueRole } from "./roles/read-issue/index.js";
|
||||||
import { createReviewRole } from "./roles/review.js";
|
import { createReviewRole } from "./roles/review/index.js";
|
||||||
import { createTestRole } from "./roles/test.js";
|
import { createTestRole } from "./roles/test/index.js";
|
||||||
|
|
||||||
export type CreateSolveIssueDeps = {
|
export type CreateSolveIssueDeps = {
|
||||||
defaultAdapter: AgentFn;
|
defaultAdapter: AgentFn;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import type { RoleStep, WorkflowMessage } from "@uncaged/nerve-core";
|
import type { WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
|
||||||
type SolveIssueParse = {
|
type SolveIssueParse = {
|
||||||
host: string;
|
host: string;
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import { END } from "@uncaged/nerve-core";
|
import { END } from "@uncaged/nerve-core";
|
||||||
import type { Moderator } from "@uncaged/nerve-core";
|
import type { Moderator } from "@uncaged/nerve-core";
|
||||||
import type { ReadIssueMeta } from "./roles/read-issue.js";
|
import type { ReadIssueMeta } from "./roles/read-issue/index.js";
|
||||||
import type { PrepareMeta } from "./roles/prepare.js";
|
import type { PrepareMeta } from "./roles/prepare/index.js";
|
||||||
import type { PlanMeta } from "./roles/plan.js";
|
import type { PlanMeta } from "./roles/plan/index.js";
|
||||||
import type { ImplementMeta } from "./roles/implement.js";
|
import type { ImplementMeta } from "./roles/implement/index.js";
|
||||||
import type { CommitterMeta } from "./roles/committer.js";
|
import type { CommitterMeta } from "./roles/committer/index.js";
|
||||||
import type { ReviewMeta } from "./roles/review.js";
|
import type { ReviewMeta } from "./roles/review/index.js";
|
||||||
import type { TestMeta } from "./roles/test.js";
|
import type { TestMeta } from "./roles/test/index.js";
|
||||||
import type { PublishMeta } from "./roles/publish.js";
|
import type { PublishMeta } from "./roles/publish/index.js";
|
||||||
|
|
||||||
export type WorkflowMeta = {
|
export type WorkflowMeta = {
|
||||||
"read-issue": ReadIssueMeta;
|
"read-issue": ReadIssueMeta;
|
||||||
|
|||||||
21
workflows/solve-issue/package.json
Normal file
21
workflows/solve-issue/package.json
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "solve-issue-workflow",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"build": "esbuild index.ts --bundle --platform=node --format=esm --outdir=dist --packages=external"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-adapter-cursor": "latest",
|
||||||
|
"@uncaged/nerve-adapter-hermes": "latest",
|
||||||
|
"@uncaged/nerve-core": "latest",
|
||||||
|
"@uncaged/nerve-workflow-utils": "latest",
|
||||||
|
"zod": "^4.3.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"esbuild": "^0.27.0",
|
||||||
|
"typescript": "^5.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
30
workflows/solve-issue/roles/committer/index.ts
Normal file
30
workflows/solve-issue/roles/committer/index.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import type { AgentFn, Role, StartStep } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole, decorateRole, withDryRun, onFail } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { committerPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const committerMetaSchema = z.object({
|
||||||
|
committed: z
|
||||||
|
.boolean()
|
||||||
|
.describe("true if branch created, changes committed, and pushed successfully"),
|
||||||
|
});
|
||||||
|
export type CommitterMeta = z.infer<typeof committerMetaSchema>;
|
||||||
|
|
||||||
|
export function createCommitterRole(
|
||||||
|
adapter: AgentFn,
|
||||||
|
extract: LlmExtractorConfig,
|
||||||
|
): Role<CommitterMeta> {
|
||||||
|
const inner = createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) => committerPrompt({ threadId: start.meta.threadId }),
|
||||||
|
committerMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
|
||||||
|
return decorateRole(inner, [
|
||||||
|
withDryRun({ label: "committer", meta: { committed: true } as CommitterMeta }),
|
||||||
|
onFail({ label: "committer", meta: { committed: false } as CommitterMeta }),
|
||||||
|
]) as Role<CommitterMeta>;
|
||||||
|
}
|
||||||
@ -1,9 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext } from "@uncaged/nerve-core";
|
export function committerPrompt({ threadId }: { threadId: string }): string {
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole, decorateRole, withDryRun, onFail } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function committerPrompt({ threadId }: { threadId: string }): string {
|
|
||||||
return `You are the committer agent. The **implement** step finished with a passing build; your job is to branch, commit, and push.
|
return `You are the committer agent. The **implement** step finished with a passing build; your job is to branch, commit, and push.
|
||||||
|
|
||||||
1. Read the workflow thread: \`nerve thread show ${threadId}\` — understand what was planned, implemented, and reviewed.
|
1. Read the workflow thread: \`nerve thread show ${threadId}\` — understand what was planned, implemented, and reviewed.
|
||||||
@ -31,27 +26,3 @@ or
|
|||||||
{ "committed": false }
|
{ "committed": false }
|
||||||
\`\`\``;
|
\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const committerMetaSchema = z.object({
|
|
||||||
committed: z
|
|
||||||
.boolean()
|
|
||||||
.describe("true if branch created, changes committed, and pushed successfully"),
|
|
||||||
});
|
|
||||||
export type CommitterMeta = z.infer<typeof committerMetaSchema>;
|
|
||||||
|
|
||||||
export function createCommitterRole(
|
|
||||||
adapter: AgentFn,
|
|
||||||
extract: LlmExtractorConfig,
|
|
||||||
): Role<CommitterMeta> {
|
|
||||||
const inner = createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) => committerPrompt({ threadId: ctx.start.meta.threadId }),
|
|
||||||
committerMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
|
|
||||||
return decorateRole(inner, [
|
|
||||||
withDryRun({ label: "committer", meta: { committed: true } as CommitterMeta }),
|
|
||||||
onFail({ label: "committer", meta: { committed: false } as CommitterMeta }),
|
|
||||||
]) as Role<CommitterMeta>;
|
|
||||||
}
|
|
||||||
@ -1,86 +0,0 @@
|
|||||||
import type { AgentFn, Role, RoleResult, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core";
|
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
import { resolveRepoCwd } from "../lib/repo-context.js";
|
|
||||||
|
|
||||||
function buildImplementPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
|
||||||
return `You are the **implement** agent. You apply code changes for the issue.
|
|
||||||
|
|
||||||
Read workflow context (plan, reviewer/test feedback): \`nerve thread show ${threadId}\`
|
|
||||||
|
|
||||||
Read Nerve workspace conventions: \`cat ${nerveRoot}/CONVENTIONS.md\`
|
|
||||||
|
|
||||||
Your cwd is the target repository.
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
1. Implement the planned changes; address reviewer/tester feedback from the thread if any.
|
|
||||||
2. Run the project **build** (\`pnpm build\`, \`npm run build\`, etc.) and fix issues until build passes.
|
|
||||||
3. Multi-step: if you cannot finish this round, explain why and set **done** to false.
|
|
||||||
|
|
||||||
Do **not** run \`git checkout -b\`, \`git add\`, \`git commit\`, or \`git push\`. **Never** create commits on any branch — branching and commits are handled by the **committer** step after you finish.
|
|
||||||
|
|
||||||
Then close with JSON:
|
|
||||||
\`\`\`json
|
|
||||||
{ "done": true }
|
|
||||||
\`\`\`
|
|
||||||
or \`{ "done": false }\` matching whether implementation is complete.
|
|
||||||
|
|
||||||
**done=true** only when changes are complete **and** build passes in this round.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const implementMetaSchema = z.object({
|
|
||||||
done: z.boolean().describe("true when changes are complete and build passes this round"),
|
|
||||||
});
|
|
||||||
export type ImplementMeta = z.infer<typeof implementMetaSchema>;
|
|
||||||
|
|
||||||
export type CreateImplementRoleDeps = {
|
|
||||||
extract: LlmExtractorConfig;
|
|
||||||
nerveRoot: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function createImplementRole(
|
|
||||||
adapter: AgentFn,
|
|
||||||
{ extract, nerveRoot }: CreateImplementRoleDeps,
|
|
||||||
): Role<ImplementMeta> {
|
|
||||||
return async (ctx: ThreadContext): Promise<RoleResult<ImplementMeta>> => {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const cwd = resolveRepoCwd(messages);
|
|
||||||
if (cwd === null) {
|
|
||||||
return {
|
|
||||||
content: "implement cannot run: missing repo path in thread markers",
|
|
||||||
meta: { done: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const innerRole = createRole(
|
|
||||||
adapter,
|
|
||||||
async (innerCtx: ThreadContext) =>
|
|
||||||
buildImplementPrompt({
|
|
||||||
threadId: innerCtx.start.meta.threadId,
|
|
||||||
nerveRoot,
|
|
||||||
}),
|
|
||||||
implementMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
|
|
||||||
const innerCtx: ThreadContext = {
|
|
||||||
...ctx,
|
|
||||||
start: {
|
|
||||||
...ctx.start,
|
|
||||||
meta: { ...ctx.start.meta, workdir: cwd },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
return await innerRole(innerCtx);
|
|
||||||
} catch (e) {
|
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
|
||||||
return {
|
|
||||||
content: `implement failed: ${msg}`,
|
|
||||||
meta: { done: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
58
workflows/solve-issue/roles/implement/index.ts
Normal file
58
workflows/solve-issue/roles/implement/index.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import type { AgentFn, Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { resolveRepoCwd } from "../../lib/repo-context.js";
|
||||||
|
import { buildImplementPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const implementMetaSchema = z.object({
|
||||||
|
done: z.boolean().describe("true when changes are complete and build passes this round"),
|
||||||
|
});
|
||||||
|
export type ImplementMeta = z.infer<typeof implementMetaSchema>;
|
||||||
|
|
||||||
|
export type CreateImplementRoleDeps = {
|
||||||
|
extract: LlmExtractorConfig;
|
||||||
|
nerveRoot: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createImplementRole(
|
||||||
|
adapter: AgentFn,
|
||||||
|
{ extract, nerveRoot }: CreateImplementRoleDeps,
|
||||||
|
): Role<ImplementMeta> {
|
||||||
|
return async (start: StartStep, messages: WorkflowMessage[]): Promise<RoleResult<ImplementMeta>> => {
|
||||||
|
const cwd = resolveRepoCwd(messages);
|
||||||
|
if (cwd === null) {
|
||||||
|
return {
|
||||||
|
content: "implement cannot run: missing repo path in thread markers",
|
||||||
|
meta: { done: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const innerRole = createRole(
|
||||||
|
adapter,
|
||||||
|
async (innerStart: StartStep) =>
|
||||||
|
buildImplementPrompt({
|
||||||
|
threadId: innerStart.meta.threadId,
|
||||||
|
nerveRoot,
|
||||||
|
}),
|
||||||
|
implementMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
|
||||||
|
const innerStart = {
|
||||||
|
...start,
|
||||||
|
meta: { ...start.meta, workdir: cwd },
|
||||||
|
} as StartStep;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await innerRole(innerStart, messages);
|
||||||
|
} catch (e) {
|
||||||
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
|
return {
|
||||||
|
content: `implement failed: ${msg}`,
|
||||||
|
meta: { done: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
25
workflows/solve-issue/roles/implement/prompt.ts
Normal file
25
workflows/solve-issue/roles/implement/prompt.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
export function buildImplementPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
||||||
|
return `You are the **implement** agent. You apply code changes for the issue.
|
||||||
|
|
||||||
|
Read workflow context (plan, reviewer/test feedback): \`nerve thread show ${threadId}\`
|
||||||
|
|
||||||
|
Read Nerve workspace conventions: \`cat ${nerveRoot}/CONVENTIONS.md\`
|
||||||
|
|
||||||
|
Your cwd is the target repository.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
1. Implement the planned changes; address reviewer/tester feedback from the thread if any.
|
||||||
|
2. Run the project **build** (\`pnpm build\`, \`npm run build\`, etc.) and fix issues until build passes.
|
||||||
|
3. Multi-step: if you cannot finish this round, explain why and set **done** to false.
|
||||||
|
|
||||||
|
Do **not** run \`git checkout -b\`, \`git add\`, \`git commit\`, or \`git push\`. **Never** create commits on any branch — branching and commits are handled by the **committer** step after you finish.
|
||||||
|
|
||||||
|
Then close with JSON:
|
||||||
|
\`\`\`json
|
||||||
|
{ "done": true }
|
||||||
|
\`\`\`
|
||||||
|
or \`{ "done": false }\` matching whether implementation is complete.
|
||||||
|
|
||||||
|
**done=true** only when changes are complete **and** build passes in this round.`;
|
||||||
|
}
|
||||||
@ -1,88 +0,0 @@
|
|||||||
import type { AgentFn, Role, RoleResult, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core";
|
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
import { resolveRepoCwd } from "../lib/repo-context.js";
|
|
||||||
|
|
||||||
function buildPlanPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
|
||||||
return `You are the **plan** agent (analysis only — ask mode). You produce an implementation plan for fixing the issue.
|
|
||||||
|
|
||||||
Read workflow context: \`nerve thread show ${threadId}\`
|
|
||||||
|
|
||||||
Read Nerve workspace conventions (coding rules for agents): \`cat ${nerveRoot}/CONVENTIONS.md\`
|
|
||||||
|
|
||||||
In the **target repository** (your cwd), skim relevant files and read \`CONVENTIONS.md\` **if it exists** there.
|
|
||||||
|
|
||||||
## Output
|
|
||||||
|
|
||||||
Write an implementation plan in **markdown** with:
|
|
||||||
|
|
||||||
1. Problem understanding
|
|
||||||
2. Change strategy
|
|
||||||
3. Target files (paths)
|
|
||||||
4. **Test commands** to run (explicit shell commands, e.g. \`pnpm test\`, \`pnpm vitest run\`)
|
|
||||||
5. Risks
|
|
||||||
|
|
||||||
End your reply with a JSON code block (meta signal):
|
|
||||||
\`\`\`json
|
|
||||||
{ "ready": true }
|
|
||||||
\`\`\`
|
|
||||||
Use \`{ "ready": false }\` if the plan cannot be made actionable.
|
|
||||||
|
|
||||||
**ready=true** only when the plan is clear and actionable.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const planMetaSchema = z.object({
|
|
||||||
ready: z.boolean().describe("true if plan is clear and actionable"),
|
|
||||||
});
|
|
||||||
export type PlanMeta = z.infer<typeof planMetaSchema>;
|
|
||||||
|
|
||||||
export type CreatePlanRoleDeps = {
|
|
||||||
extract: LlmExtractorConfig;
|
|
||||||
nerveRoot: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function createPlanRole(
|
|
||||||
adapter: AgentFn,
|
|
||||||
{ extract, nerveRoot }: CreatePlanRoleDeps,
|
|
||||||
): Role<PlanMeta> {
|
|
||||||
return async (ctx: ThreadContext): Promise<RoleResult<PlanMeta>> => {
|
|
||||||
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
||||||
const cwd = resolveRepoCwd(messages);
|
|
||||||
if (cwd === null) {
|
|
||||||
return {
|
|
||||||
content: "plan cannot run: missing ---SOLVE_ISSUE_REPO--- or ---SOLVE_ISSUE_PARSE--- in thread",
|
|
||||||
meta: { ready: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const innerRole = createRole(
|
|
||||||
adapter,
|
|
||||||
async (innerCtx: ThreadContext) =>
|
|
||||||
buildPlanPrompt({
|
|
||||||
threadId: innerCtx.start.meta.threadId,
|
|
||||||
nerveRoot,
|
|
||||||
}),
|
|
||||||
planMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
|
|
||||||
const innerCtx: ThreadContext = {
|
|
||||||
...ctx,
|
|
||||||
start: {
|
|
||||||
...ctx.start,
|
|
||||||
meta: { ...ctx.start.meta, workdir: cwd },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
return await innerRole(innerCtx);
|
|
||||||
} catch (e) {
|
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
|
||||||
return {
|
|
||||||
content: `plan failed: ${msg}`,
|
|
||||||
meta: { ready: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
58
workflows/solve-issue/roles/plan/index.ts
Normal file
58
workflows/solve-issue/roles/plan/index.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import type { AgentFn, Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { resolveRepoCwd } from "../../lib/repo-context.js";
|
||||||
|
import { buildPlanPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const planMetaSchema = z.object({
|
||||||
|
ready: z.boolean().describe("true if plan is clear and actionable"),
|
||||||
|
});
|
||||||
|
export type PlanMeta = z.infer<typeof planMetaSchema>;
|
||||||
|
|
||||||
|
export type CreatePlanRoleDeps = {
|
||||||
|
extract: LlmExtractorConfig;
|
||||||
|
nerveRoot: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createPlanRole(
|
||||||
|
adapter: AgentFn,
|
||||||
|
{ extract, nerveRoot }: CreatePlanRoleDeps,
|
||||||
|
): Role<PlanMeta> {
|
||||||
|
return async (start: StartStep, messages: WorkflowMessage[]): Promise<RoleResult<PlanMeta>> => {
|
||||||
|
const cwd = resolveRepoCwd(messages);
|
||||||
|
if (cwd === null) {
|
||||||
|
return {
|
||||||
|
content: "plan cannot run: missing ---SOLVE_ISSUE_REPO--- or ---SOLVE_ISSUE_PARSE--- in thread",
|
||||||
|
meta: { ready: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const innerRole = createRole(
|
||||||
|
adapter,
|
||||||
|
async (innerStart: StartStep) =>
|
||||||
|
buildPlanPrompt({
|
||||||
|
threadId: innerStart.meta.threadId,
|
||||||
|
nerveRoot,
|
||||||
|
}),
|
||||||
|
planMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
|
||||||
|
const innerStart = {
|
||||||
|
...start,
|
||||||
|
meta: { ...start.meta, workdir: cwd },
|
||||||
|
} as StartStep;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await innerRole(innerStart, messages);
|
||||||
|
} catch (e) {
|
||||||
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
|
return {
|
||||||
|
content: `plan failed: ${msg}`,
|
||||||
|
meta: { ready: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
27
workflows/solve-issue/roles/plan/prompt.ts
Normal file
27
workflows/solve-issue/roles/plan/prompt.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
export function buildPlanPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
||||||
|
return `You are the **plan** agent (analysis only — ask mode). You produce an implementation plan for fixing the issue.
|
||||||
|
|
||||||
|
Read workflow context: \`nerve thread show ${threadId}\`
|
||||||
|
|
||||||
|
Read Nerve workspace conventions (coding rules for agents): \`cat ${nerveRoot}/CONVENTIONS.md\`
|
||||||
|
|
||||||
|
In the **target repository** (your cwd), skim relevant files and read \`CONVENTIONS.md\` **if it exists** there.
|
||||||
|
|
||||||
|
## Output
|
||||||
|
|
||||||
|
Write an implementation plan in **markdown** with:
|
||||||
|
|
||||||
|
1. Problem understanding
|
||||||
|
2. Change strategy
|
||||||
|
3. Target files (paths)
|
||||||
|
4. **Test commands** to run (explicit shell commands, e.g. \`pnpm test\`, \`pnpm vitest run\`)
|
||||||
|
5. Risks
|
||||||
|
|
||||||
|
End your reply with a JSON code block (meta signal):
|
||||||
|
\`\`\`json
|
||||||
|
{ "ready": true }
|
||||||
|
\`\`\`
|
||||||
|
Use \`{ "ready": false }\` if the plan cannot be made actionable.
|
||||||
|
|
||||||
|
**ready=true** only when the plan is clear and actionable.`;
|
||||||
|
}
|
||||||
20
workflows/solve-issue/roles/prepare/index.ts
Normal file
20
workflows/solve-issue/roles/prepare/index.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import type { AgentFn, Role, StartStep } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { preparePrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const prepareMetaSchema = z.object({
|
||||||
|
ready: z.boolean().describe("true if repo is ready and baseline build ok"),
|
||||||
|
});
|
||||||
|
export type PrepareMeta = z.infer<typeof prepareMetaSchema>;
|
||||||
|
|
||||||
|
export function createPrepareRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<PrepareMeta> {
|
||||||
|
return createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) => preparePrompt({ threadId: start.meta.threadId }),
|
||||||
|
prepareMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,9 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext } from "@uncaged/nerve-core";
|
export function preparePrompt({ threadId }: { threadId: string }): string {
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function preparePrompt({ threadId }: { threadId: string }): string {
|
|
||||||
return `You are the **prepare** agent. You ensure the target repository is ready for work.
|
return `You are the **prepare** agent. You ensure the target repository is ready for work.
|
||||||
|
|
||||||
Read prior messages / thread for issue markers: \`nerve thread show ${threadId}\`
|
Read prior messages / thread for issue markers: \`nerve thread show ${threadId}\`
|
||||||
@ -57,17 +52,3 @@ or \`{ "ready": false }\` if the repo is invalid, or install/build baseline fail
|
|||||||
|
|
||||||
**ready=true** only when the repo exists at \`path\`, is clean, dependencies installed, and baseline build succeeded (or no build script).`;
|
**ready=true** only when the repo exists at \`path\`, is clean, dependencies installed, and baseline build succeeded (or no build script).`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const prepareMetaSchema = z.object({
|
|
||||||
ready: z.boolean().describe("true if repo is ready and baseline build ok"),
|
|
||||||
});
|
|
||||||
export type PrepareMeta = z.infer<typeof prepareMetaSchema>;
|
|
||||||
|
|
||||||
export function createPrepareRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<PrepareMeta> {
|
|
||||||
return createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) => preparePrompt({ threadId: ctx.start.meta.threadId }),
|
|
||||||
prepareMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
66
workflows/solve-issue/roles/publish/index.ts
Normal file
66
workflows/solve-issue/roles/publish/index.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { mkdirSync, writeFileSync } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import type { AgentFn, Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole, isDryRun } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { buildPublishPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const publishMetaSchema = z.object({
|
||||||
|
success: z.boolean().describe("true if git push and tea pr create both succeeded"),
|
||||||
|
});
|
||||||
|
export type PublishMeta = z.infer<typeof publishMetaSchema>;
|
||||||
|
|
||||||
|
export type CreatePublishRoleDeps = {
|
||||||
|
extract: LlmExtractorConfig;
|
||||||
|
nerveRoot: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function logPath(nerveRoot: string): string {
|
||||||
|
return join(nerveRoot, "logs", `solve-issue-publish-${Date.now()}.log`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createPublishRole(
|
||||||
|
adapter: AgentFn,
|
||||||
|
{ extract, nerveRoot }: CreatePublishRoleDeps,
|
||||||
|
): Role<PublishMeta> {
|
||||||
|
const innerRole = createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) =>
|
||||||
|
buildPublishPrompt({ threadId: start.meta.threadId, nerveRoot }),
|
||||||
|
publishMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
|
||||||
|
return async (start: StartStep, messages: WorkflowMessage[]): Promise<RoleResult<PublishMeta>> => {
|
||||||
|
const file = logPath(nerveRoot);
|
||||||
|
mkdirSync(join(file, ".."), { recursive: true });
|
||||||
|
|
||||||
|
if (isDryRun(start)) {
|
||||||
|
const msg = "[dry-run] publish skipped (no git push / PR)";
|
||||||
|
writeFileSync(file, `${msg}\n`, "utf-8");
|
||||||
|
return {
|
||||||
|
content: `[dry-run] publish skipped — log: ${file}`,
|
||||||
|
meta: { success: true },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const innerStart = {
|
||||||
|
...start,
|
||||||
|
meta: { ...start.meta, workdir: nerveRoot },
|
||||||
|
} as StartStep;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await innerRole(innerStart, messages);
|
||||||
|
} catch (e) {
|
||||||
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
|
const body = `publish failed: ${msg}\n`;
|
||||||
|
writeFileSync(file, body, "utf-8");
|
||||||
|
return {
|
||||||
|
content: `publish failed: ${msg}\nLog: ${file}`,
|
||||||
|
meta: { success: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,11 +1,4 @@
|
|||||||
import { mkdirSync, writeFileSync } from "node:fs";
|
export function buildPublishPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
||||||
import { join } from "node:path";
|
|
||||||
import type { AgentFn, Role, RoleResult, ThreadContext } from "@uncaged/nerve-core";
|
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole, isDryRun } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function buildPublishPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
|
||||||
return `You are the **publish** agent (Hermes). Test has passed. Open a pull request for the current branch using the **tea** CLI.
|
return `You are the **publish** agent (Hermes). Test has passed. Open a pull request for the current branch using the **tea** CLI.
|
||||||
|
|
||||||
## Context
|
## Context
|
||||||
@ -47,64 +40,3 @@ or
|
|||||||
{ "success": false }
|
{ "success": false }
|
||||||
\`\`\``;
|
\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const publishMetaSchema = z.object({
|
|
||||||
success: z.boolean().describe("true if git push and tea pr create both succeeded"),
|
|
||||||
});
|
|
||||||
export type PublishMeta = z.infer<typeof publishMetaSchema>;
|
|
||||||
|
|
||||||
export type CreatePublishRoleDeps = {
|
|
||||||
extract: LlmExtractorConfig;
|
|
||||||
nerveRoot: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function logPath(nerveRoot: string): string {
|
|
||||||
return join(nerveRoot, "logs", `solve-issue-publish-${Date.now()}.log`);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createPublishRole(
|
|
||||||
adapter: AgentFn,
|
|
||||||
{ extract, nerveRoot }: CreatePublishRoleDeps,
|
|
||||||
): Role<PublishMeta> {
|
|
||||||
const innerRole = createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) =>
|
|
||||||
buildPublishPrompt({ threadId: ctx.start.meta.threadId, nerveRoot }),
|
|
||||||
publishMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
|
|
||||||
return async (ctx: ThreadContext): Promise<RoleResult<PublishMeta>> => {
|
|
||||||
const file = logPath(nerveRoot);
|
|
||||||
mkdirSync(join(file, ".."), { recursive: true });
|
|
||||||
|
|
||||||
if (isDryRun(ctx.start)) {
|
|
||||||
const msg = "[dry-run] publish skipped (no git push / PR)";
|
|
||||||
writeFileSync(file, `${msg}\n`, "utf-8");
|
|
||||||
return {
|
|
||||||
content: `[dry-run] publish skipped — log: ${file}`,
|
|
||||||
meta: { success: true },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const innerCtx: ThreadContext = {
|
|
||||||
...ctx,
|
|
||||||
start: {
|
|
||||||
...ctx.start,
|
|
||||||
meta: { ...ctx.start.meta, workdir: nerveRoot },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
return await innerRole(innerCtx);
|
|
||||||
} catch (e) {
|
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
|
||||||
const body = `publish failed: ${msg}\n`;
|
|
||||||
writeFileSync(file, body, "utf-8");
|
|
||||||
return {
|
|
||||||
content: `publish failed: ${msg}\nLog: ${file}`,
|
|
||||||
meta: { success: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
20
workflows/solve-issue/roles/read-issue/index.ts
Normal file
20
workflows/solve-issue/roles/read-issue/index.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import type { AgentFn, Role, StartStep } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { readIssuePrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const readIssueMetaSchema = z.object({
|
||||||
|
ready: z.boolean().describe("true if issue content was fetched and markers are present"),
|
||||||
|
});
|
||||||
|
export type ReadIssueMeta = z.infer<typeof readIssueMetaSchema>;
|
||||||
|
|
||||||
|
export function createReadIssueRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<ReadIssueMeta> {
|
||||||
|
return createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) => readIssuePrompt({ threadId: start.meta.threadId }),
|
||||||
|
readIssueMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,9 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext } from "@uncaged/nerve-core";
|
export function readIssuePrompt({ threadId }: { threadId: string }): string {
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function readIssuePrompt({ threadId }: { threadId: string }): string {
|
|
||||||
return `You are the **read-issue** agent. You fetch Gitea issue content via the \`tea\` CLI.
|
return `You are the **read-issue** agent. You fetch Gitea issue content via the \`tea\` CLI.
|
||||||
|
|
||||||
Read the workflow thread start prompt for the issue URL (same run): \`nerve thread show ${threadId}\`
|
Read the workflow thread start prompt for the issue URL (same run): \`nerve thread show ${threadId}\`
|
||||||
@ -37,17 +32,3 @@ Use \`{ "ready": false }\` if you could not fetch or parse the issue.
|
|||||||
|
|
||||||
**ready=true** only if the issue was fetched successfully and the marker block is correct.`;
|
**ready=true** only if the issue was fetched successfully and the marker block is correct.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const readIssueMetaSchema = z.object({
|
|
||||||
ready: z.boolean().describe("true if issue content was fetched and markers are present"),
|
|
||||||
});
|
|
||||||
export type ReadIssueMeta = z.infer<typeof readIssueMetaSchema>;
|
|
||||||
|
|
||||||
export function createReadIssueRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<ReadIssueMeta> {
|
|
||||||
return createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) => readIssuePrompt({ threadId: ctx.start.meta.threadId }),
|
|
||||||
readIssueMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
25
workflows/solve-issue/roles/review/index.ts
Normal file
25
workflows/solve-issue/roles/review/index.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import type { AgentFn, Role, StartStep } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { reviewPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const reviewMetaSchema = z.object({
|
||||||
|
approved: z.boolean().describe("true if diff is clean and ready for tests"),
|
||||||
|
});
|
||||||
|
export type ReviewMeta = z.infer<typeof reviewMetaSchema>;
|
||||||
|
|
||||||
|
export function createReviewRole(
|
||||||
|
adapter: AgentFn,
|
||||||
|
extract: LlmExtractorConfig,
|
||||||
|
nerveRoot: string,
|
||||||
|
): Role<ReviewMeta> {
|
||||||
|
return createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) =>
|
||||||
|
reviewPrompt({ threadId: start.meta.threadId, nerveRoot }),
|
||||||
|
reviewMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,9 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext } from "@uncaged/nerve-core";
|
export function reviewPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function reviewPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string {
|
|
||||||
return `You are a **code reviewer** (Hermes). You run after implement and before test.
|
return `You are a **code reviewer** (Hermes). You run after implement and before test.
|
||||||
|
|
||||||
Read Nerve workspace conventions: \`cat ${nerveRoot}/CONVENTIONS.md\`
|
Read Nerve workspace conventions: \`cat ${nerveRoot}/CONVENTIONS.md\`
|
||||||
@ -38,22 +33,3 @@ or
|
|||||||
{ "approved": false }
|
{ "approved": false }
|
||||||
\`\`\``;
|
\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const reviewMetaSchema = z.object({
|
|
||||||
approved: z.boolean().describe("true if diff is clean and ready for tests"),
|
|
||||||
});
|
|
||||||
export type ReviewMeta = z.infer<typeof reviewMetaSchema>;
|
|
||||||
|
|
||||||
export function createReviewRole(
|
|
||||||
adapter: AgentFn,
|
|
||||||
extract: LlmExtractorConfig,
|
|
||||||
nerveRoot: string,
|
|
||||||
): Role<ReviewMeta> {
|
|
||||||
return createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) =>
|
|
||||||
reviewPrompt({ threadId: ctx.start.meta.threadId, nerveRoot }),
|
|
||||||
reviewMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
20
workflows/solve-issue/roles/test/index.ts
Normal file
20
workflows/solve-issue/roles/test/index.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import type { AgentFn, Role, StartStep } from "@uncaged/nerve-core";
|
||||||
|
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { createRole } from "@uncaged/nerve-workflow-utils";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { testPrompt } from "./prompt.js";
|
||||||
|
|
||||||
|
export const testMetaSchema = z.object({
|
||||||
|
passed: z.boolean().describe("true if all test commands passed"),
|
||||||
|
});
|
||||||
|
export type TestMeta = z.infer<typeof testMetaSchema>;
|
||||||
|
|
||||||
|
export function createTestRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<TestMeta> {
|
||||||
|
return createRole(
|
||||||
|
adapter,
|
||||||
|
async (start: StartStep) => testPrompt({ threadId: start.meta.threadId }),
|
||||||
|
testMetaSchema,
|
||||||
|
extract,
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,9 +1,4 @@
|
|||||||
import type { AgentFn, Role, ThreadContext } from "@uncaged/nerve-core";
|
export function testPrompt({ threadId }: { threadId: string }): string {
|
||||||
import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { createRole } from "@uncaged/nerve-workflow-utils";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
function testPrompt({ threadId }: { threadId: string }): string {
|
|
||||||
return `You are the **test** agent (Hermes). You execute automated tests for the change.
|
return `You are the **test** agent (Hermes). You execute automated tests for the change.
|
||||||
|
|
||||||
Read workflow context: \`nerve thread show ${threadId}\`
|
Read workflow context: \`nerve thread show ${threadId}\`
|
||||||
@ -24,17 +19,3 @@ or \`{ "passed": false }\`
|
|||||||
|
|
||||||
**passed=true** only if every executed command exited 0 (or skip was justified with no failing command).`;
|
**passed=true** only if every executed command exited 0 (or skip was justified with no failing command).`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const testMetaSchema = z.object({
|
|
||||||
passed: z.boolean().describe("true if all test commands passed"),
|
|
||||||
});
|
|
||||||
export type TestMeta = z.infer<typeof testMetaSchema>;
|
|
||||||
|
|
||||||
export function createTestRole(adapter: AgentFn, extract: LlmExtractorConfig): Role<TestMeta> {
|
|
||||||
return createRole(
|
|
||||||
adapter,
|
|
||||||
async (ctx: ThreadContext) => testPrompt({ threadId: ctx.start.meta.threadId }),
|
|
||||||
testMetaSchema,
|
|
||||||
extract,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
13
workflows/solve-issue/tsconfig.json
Normal file
13
workflows/solve-issue/tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"lib": ["ES2022"],
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["./**/*.ts"]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user