@hexdecimal16/nexo 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # nexo
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@hexdecimal16/nexo)](https://www.npmjs.com/package/@hexdecimal16/nexo)
4
+ [![test](https://github.com/hexdecimal16/nexo/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/hexdecimal16/nexo/actions/workflows/test.yml)
5
+
3
6
  An AI agent engine that runs on your machine. It tracks what each run costs, scores the results, and keeps every run inspectable. Use it from the terminal, from a web run inspector, or from Node code.
4
7
 
5
8
  Requires Node 22.19 or newer.
package/cli/src/common.js CHANGED
@@ -22,8 +22,8 @@ export const USAGE = `Usage:
22
22
  nexo eval accept <case-id> [--expected <text>]
23
23
  nexo eval dismiss <case-id>
24
24
  nexo eval golden revert <case-id>
25
- nexo optimize --target skill:<name>|system_prompt|planner_prompt|judge_rubric [--budget-usd <n>] [--max-rollouts <n>] [--merge] [--seed <n>]
26
- nexo optimize climb --target skill:<name>|system_prompt|planner_prompt [--case <id>] [--guards <0-10>] [--budget-usd <n>] [--seed <n>]
25
+ nexo optimize --target skill:<name>|tool:<name>|system_prompt|planner_prompt|judge_rubric [--budget-usd <n>] [--max-rollouts <n>] [--merge] [--seed <n>]
26
+ nexo optimize climb --target skill:<name>|tool:<name>|system_prompt|planner_prompt [--case <id>] [--guards <0-10>] [--budget-usd <n>] [--seed <n>]
27
27
  nexo optimize list
28
28
  nexo optimize show <id> [--diff]
29
29
  nexo optimize approve <id> promote the best candidate (refused on weak evidence)
@@ -37,6 +37,7 @@ export const USAGE = `Usage:
37
37
  nexo playbook retire <skill> <key>
38
38
  nexo init [--yes] [--s1 <provider/id>] [--s2 <provider/id>] [--skip-test] guided first-time setup with a test call
39
39
  nexo doctor [--json] check Node, config, keys and the data directory (exit 1 on a failure)
40
+ nexo update check npm for a newer version and print the install command
40
41
  nexo ui [--port <n>]
41
42
  `;
42
43
  export const EXIT_SIGINT = 130;
package/cli/src/main.js CHANGED
@@ -15,6 +15,7 @@ import { interactiveCommand, settingsScreenCommand } from "./interactive.js";
15
15
  import { sessionsCommand } from "./sessions.js";
16
16
  import { doctorCommand } from "./doctor.js";
17
17
  import { initCommand } from "./init.js";
18
+ import { updateCommand } from "./update.js";
18
19
  import { nexoVersion } from "./version.js";
19
20
  import { settingsCommand } from "./settings.js";
20
21
  import { escapeControl } from "../../tui/src/index.js";
@@ -235,6 +236,8 @@ export async function runCli(argv, io, options = {}) {
235
236
  return doctorCommand(rest, io, options);
236
237
  case "init":
237
238
  return await initCommand(rest, io, options, stdio);
239
+ case "update":
240
+ return await updateCommand(io, options);
238
241
  case "sessions":
239
242
  return sessionsCommand(rest, io, options);
240
243
  case "settings":
@@ -128,7 +128,7 @@ async function runOptimize(args, io, options) {
128
128
  options: { target: { type: "string" }, "budget-usd": { type: "string" }, "max-rollouts": { type: "string" }, merge: { type: "boolean" }, seed: { type: "string" } },
129
129
  });
130
130
  if (!values.target)
131
- throw usageError("nexo optimize needs --target skill:<name>|system_prompt|planner_prompt|judge_rubric");
131
+ throw usageError("nexo optimize needs --target skill:<name>|tool:<name>|system_prompt|planner_prompt|judge_rubric");
132
132
  const seed = values.seed === undefined ? undefined : Number(values.seed);
133
133
  if (seed !== undefined && !(Number.isInteger(seed) && seed >= 0))
134
134
  throw usageError("--seed must be a non-negative integer");
@@ -171,7 +171,7 @@ export function climbEventLine(event) {
171
171
  async function runClimb(args, io, options) {
172
172
  const { values } = parseArgs({ args, options: { target: { type: "string" }, case: { type: "string" }, guards: { type: "string" }, "budget-usd": { type: "string" }, seed: { type: "string" } } });
173
173
  if (!values.target)
174
- throw usageError("nexo optimize climb needs --target skill:<name>|system_prompt|planner_prompt");
174
+ throw usageError("nexo optimize climb needs --target skill:<name>|tool:<name>|system_prompt|planner_prompt");
175
175
  const guards = values.guards === undefined ? undefined : Number(values.guards);
176
176
  if (guards !== undefined && !(Number.isInteger(guards) && guards >= 0 && guards <= 10))
177
177
  throw usageError("--guards must be an integer from 0 to 10");
@@ -0,0 +1,78 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { fileURLToPath } from "node:url";
3
+ import { NexoError } from "../../telemetry/src/index.js";
4
+ import { nexoVersion } from "./version.js";
5
+ const DEFAULT_PACKAGE = "@hexdecimal16/nexo";
6
+ const REGISTRY = "https://registry.npmjs.org";
7
+ const TIMEOUT_MS = 10_000;
8
+ /** The published package name: read from the installed package.json, or the default when running from the repo. */
9
+ function packageName() {
10
+ for (const relative of ["../../package.json", "../package.json"]) {
11
+ try {
12
+ const json = JSON.parse(readFileSync(fileURLToPath(new URL(relative, import.meta.url)), "utf8"));
13
+ if (typeof json.name === "string" && !json.name.startsWith("@nexo" + "/"))
14
+ return json.name;
15
+ }
16
+ catch {
17
+ // not at this location; try the next one
18
+ }
19
+ }
20
+ return DEFAULT_PACKAGE;
21
+ }
22
+ function parse(version) {
23
+ const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
24
+ return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
25
+ }
26
+ /** Negative when a < b, 0 when equal, positive when a > b; null when either is not x.y.z. */
27
+ export function compareVersions(a, b) {
28
+ const pa = parse(a);
29
+ const pb = parse(b);
30
+ if (!pa || !pb)
31
+ return null;
32
+ for (let i = 0; i < 3; i++) {
33
+ const d = pa[i] - pb[i];
34
+ if (d !== 0)
35
+ return d;
36
+ }
37
+ return 0;
38
+ }
39
+ /**
40
+ * `nexo update`: compares the installed version with the registry's `latest` dist-tag and prints the command to
41
+ * update. It never installs anything. Exit 0 when up to date or when an update is available, 1 when the registry
42
+ * cannot be reached.
43
+ */
44
+ export async function updateCommand(io, options = {}) {
45
+ const name = options.name ?? packageName();
46
+ const current = options.current ?? nexoVersion();
47
+ const doFetch = options.fetch ?? globalThis.fetch;
48
+ const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), ...(options.signal ? [options.signal] : [])]);
49
+ let latest;
50
+ try {
51
+ const response = await doFetch(`${REGISTRY}/${name.replace("/", "%2f")}/latest`, { signal });
52
+ if (!response.ok)
53
+ throw new Error(`the registry answered HTTP ${response.status}`);
54
+ const body = (await response.json());
55
+ if (typeof body.version !== "string")
56
+ throw new Error("the registry reply has no version");
57
+ latest = body.version;
58
+ }
59
+ catch (cause) {
60
+ throw new NexoError({
61
+ code: "PROVIDER_UNAVAILABLE",
62
+ message: `Could not check ${name} on npm: ${cause.message}`,
63
+ hint: `Check your connection, or run: npm view ${name} version`,
64
+ cause,
65
+ });
66
+ }
67
+ const order = compareVersions(current, latest);
68
+ if (order === null) {
69
+ io.stdout.write(`Installed: ${current}, latest: ${latest}\nRun: npm install -g ${name}@latest\n`);
70
+ }
71
+ else if (order >= 0) {
72
+ io.stdout.write(`nexo ${current} is up to date (latest on npm: ${latest}).\n`);
73
+ }
74
+ else {
75
+ io.stdout.write(`Update available: ${current} -> ${latest}\nRun: npm install -g ${name}@latest\n`);
76
+ }
77
+ return 0;
78
+ }
@@ -4,7 +4,7 @@ import { createModelRegistry, DEFAULT_ROUTER_POLICY, ESCALATION_BLOCK_REASONS, e
4
4
  import { createLoadSkillTool, formatSkillIndex, parseSkillFile } from "../../../skills/src/index.js";
5
5
  import { createArtifactSet, enqueueLearnJob, getActiveArtifact, insertRun, isPidAlive, listPlaybookItems, listSpans, newId, recoverCrashedRuns, resolveSourcedArtifact, RunDir, sessionRunDirs, updateRun, } from "../../../store/src/index.js";
6
6
  import { NexoError, toErrorPayload } from "../../../telemetry/src/index.js";
7
- import { createBuiltinTools, createChangeTracker, createPolicy, FileLeases, TOOL_OUTPUT_PREFIX, } from "../../../tools/src/index.js";
7
+ import { builtinToolDescriptions, createBuiltinTools, createChangeTracker, createPolicy, FileLeases, TOOL_OUTPUT_PREFIX, } from "../../../tools/src/index.js";
8
8
  import { realpathSync, statSync } from "node:fs";
9
9
  import { join } from "node:path";
10
10
  import { createApprovalClassifier } from "../approval/classifier.js";
@@ -254,6 +254,8 @@ export async function executeRun(env, input) {
254
254
  };
255
255
  let systemPromptText = DEFAULT_SYSTEM_PROMPT;
256
256
  let plannerPromptText = DEFAULT_PLANNER_PROMPT;
257
+ /** Descriptions that replace a built-in tool's own: the rollout's candidate, else a promoted version. Empty for a normal run. */
258
+ const toolDescriptionOverrides = new Map();
257
259
  /** The `--skill` skill with the body this run uses. */
258
260
  let runSkill = skill;
259
261
  const recorder = new Recorder({ db, runDir, runId, now, redact: deps.redact });
@@ -318,6 +320,16 @@ export async function executeRun(env, input) {
318
320
  runSkill = effective.skill;
319
321
  snapshot(effective.artifact, effective.skill.content, `skills/${skill.name}/SKILL.md`);
320
322
  }
323
+ for (const toolName of Object.keys(builtinToolDescriptions())) {
324
+ const candidate = rolloutCandidate && rolloutCandidate.kind === "tool_description" && rolloutCandidate.name === toolName ? rolloutCandidate : undefined;
325
+ const version = candidate ?? getActiveArtifact(db, "tool_description", toolName);
326
+ // A manual version mirrors the built-in text, so only a promoted or candidate version changes what the agent sees.
327
+ if (!version || (version.origin === "manual" && !candidate))
328
+ continue;
329
+ snapshot(version, version.content, `tools/${toolName}.md`);
330
+ if (version.origin !== "manual")
331
+ toolDescriptionOverrides.set(toolName, version.content);
332
+ }
321
333
  if (skill && startPlaybook?.content.trim()) {
322
334
  members.push({ kind: "playbook", name: skill.name, artifactId: startPlaybook.id, version: startPlaybook.version });
323
335
  runDir.writeArtifact(`playbooks/${skill.name}.md`, startPlaybook.content);
@@ -568,7 +580,9 @@ export async function executeRun(env, input) {
568
580
  baseTools.push(...factory(toolCtx));
569
581
  if (deps.mcp)
570
582
  baseTools.push(...(await deps.mcp.tools()));
571
- const tools = baseTools.map(withToolErrorCodes);
583
+ const tools = baseTools
584
+ .map((t) => (toolDescriptionOverrides.has(t.name) ? { ...t, description: toolDescriptionOverrides.get(t.name) } : t))
585
+ .map(withToolErrorCodes);
572
586
  // Retrieval at run start (spec §8.3); the query embedding is charged to this run like any of its calls.
573
587
  memoryDeps = {
574
588
  env,
@@ -11,6 +11,7 @@ import { createEngineEnv } from "../engine/run.js";
11
11
  import { judgeKey, judgeModels, loadJudgeRubric } from "../scoring/sources.js";
12
12
  import { mineQuietly } from "./mine.js";
13
13
  import { readScratchpad, writeScratchpad } from "./scratchpad-wiki.js";
14
+ import { builtinToolDescriptions } from "../../../tools/src/index.js";
14
15
  import { assertReflectionFits } from "./reflect.js";
15
16
  import { createOptimAdapter } from "./adapter.js";
16
17
  import { closeOptimizerRun, openOptimizerRun } from "./optimizer-run.js";
@@ -32,6 +33,13 @@ export function baseVersion(env, target, nowIso) {
32
33
  return resolveSourcedArtifact(db, "system_prompt", "default", redact(DEFAULT_SYSTEM_PROMPT), nowIso);
33
34
  case "planner_prompt":
34
35
  return resolveSourcedArtifact(db, "planner_prompt", "default", redact(DEFAULT_PLANNER_PROMPT), nowIso);
36
+ case "tool_description": {
37
+ const description = builtinToolDescriptions()[target.name];
38
+ if (description === undefined) {
39
+ throw new NexoError({ code: "INVALID_REQUEST", message: `Unknown built-in tool "${target.name}"`, hint: `Available: ${Object.keys(builtinToolDescriptions()).join(", ")}` });
40
+ }
41
+ return resolveSourcedArtifact(db, "tool_description", target.name, redact(description), nowIso);
42
+ }
35
43
  case "judge_rubric":
36
44
  return loadJudgeRubric(db, config, nowIso, redact).artifact;
37
45
  }
@@ -28,6 +28,8 @@ function describe(target) {
28
28
  return "the agent's system prompt ({{workspace}} is replaced by the workspace path; keep that placeholder)";
29
29
  case "planner_prompt":
30
30
  return "the planner prompt that tells the agent when and how to split work into parallel subagents";
31
+ case "tool_description":
32
+ return `the description of the tool "${target.name}", which the agent reads when it decides whether and how to call the tool (the tool's name and parameters are fixed; describe only what it does, when to use it and its limits)`;
31
33
  case "judge_rubric":
32
34
  return "the rubric an LLM judge grades the agent's work against; each example shows the judge's verdict next to the human's done / not done verdict, which is the ground truth";
33
35
  }
@@ -8,6 +8,8 @@ export function targetComponents(target) {
8
8
  return ["system_prompt"];
9
9
  case "planner_prompt":
10
10
  return ["planner_prompt"];
11
+ case "tool_description":
12
+ return ["description"];
11
13
  case "judge_rubric":
12
14
  return ["rubric"];
13
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hexdecimal16/nexo",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "nexo agent engine",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "repository": {
package/store/src/db.js CHANGED
@@ -4,6 +4,8 @@ import { DatabaseSync } from "node:sqlite";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { NexoError } from "../../telemetry/src/index.js";
6
6
  const MIGRATIONS_DIR = fileURLToPath(new URL("./migrations/", import.meta.url));
7
+ /** A migration whose first line is this marker rebuilds a table that others reference: it runs with foreign keys off and is checked with foreign_key_check before it commits. */
8
+ const FK_OFF_MARKER = "-- nexo:foreign-keys-off";
7
9
  export function openDb(path) {
8
10
  try {
9
11
  if (path !== ":memory:")
@@ -41,14 +43,30 @@ export function migrate(db) {
41
43
  for (const file of files) {
42
44
  const version = Number(file.slice(0, 3));
43
45
  const sql = readFileSync(join(MIGRATIONS_DIR, file), "utf8");
44
- const ran = transaction(db, () => {
45
- const current = Number(db.prepare("SELECT COALESCE(MAX(version), 0) AS v FROM schema_migrations").get()?.v ?? 0);
46
- if (version <= current)
47
- return false;
48
- db.exec(sql);
49
- db.prepare("INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)").run(version, file, new Date().toISOString());
50
- return true;
51
- });
46
+ const fkOff = sql.startsWith(FK_OFF_MARKER);
47
+ // PRAGMA foreign_keys is a no-op inside a transaction, so it is switched outside it.
48
+ if (fkOff)
49
+ db.exec("PRAGMA foreign_keys = OFF");
50
+ let ran;
51
+ try {
52
+ ran = transaction(db, () => {
53
+ const current = Number(db.prepare("SELECT COALESCE(MAX(version), 0) AS v FROM schema_migrations").get()?.v ?? 0);
54
+ if (version <= current)
55
+ return false;
56
+ db.exec(sql);
57
+ if (fkOff) {
58
+ const broken = db.prepare("PRAGMA foreign_key_check").all();
59
+ if (broken.length > 0)
60
+ throw new Error(`Migration ${file} left ${broken.length} foreign key violations`);
61
+ }
62
+ db.prepare("INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)").run(version, file, new Date().toISOString());
63
+ return true;
64
+ });
65
+ }
66
+ finally {
67
+ if (fkOff)
68
+ db.exec("PRAGMA foreign_keys = ON");
69
+ }
52
70
  if (ran)
53
71
  applied.push(version);
54
72
  }
@@ -12,6 +12,7 @@ export const MAX_EVAL_INPUT_CHARS = 20_000;
12
12
  /** Most runs listed when an eval set is too small or a promotion needs more human verdicts. */
13
13
  export const RUNS_TO_REVIEW = 10;
14
14
  const SKILL_TARGET = /^skill:([a-z0-9]+(-[a-z0-9]+)*)$/;
15
+ const TOOL_TARGET = /^tool:([a-z][a-z0-9_]{0,63})$/;
15
16
  /**
16
17
  * Parses an optimization target. `router_policy` is refused: it is a JSON policy of numeric thresholds, not prose.
17
18
  * Throws INVALID_REQUEST.
@@ -20,12 +21,15 @@ export function parseOptimTarget(text) {
20
21
  const skill = SKILL_TARGET.exec(text);
21
22
  if (skill && skill[1].length <= 64)
22
23
  return { key: text, kind: "skill", name: skill[1] };
24
+ const tool = TOOL_TARGET.exec(text);
25
+ if (tool)
26
+ return { key: text, kind: "tool_description", name: tool[1] };
23
27
  if (text === "system_prompt" || text === "planner_prompt" || text === "judge_rubric")
24
28
  return { key: text, kind: text, name: "default" };
25
29
  if (text === "router_policy") {
26
- throw invalidRequest("router_policy is not an optimization target yet: it is a JSON policy of numeric thresholds, not prose, and evaluating one needs S2 escalation rollouts on every case", { target: text }, "Optimize skill:<name>, system_prompt, planner_prompt or judge_rubric; edit router thresholds with router.policy = file:<path>");
30
+ throw invalidRequest("router_policy is not an optimization target yet: it is a JSON policy of numeric thresholds, not prose, and evaluating one needs S2 escalation rollouts on every case", { target: text }, "Optimize skill:<name>, tool:<name>, system_prompt, planner_prompt or judge_rubric; edit router thresholds with router.policy = file:<path>");
27
31
  }
28
- throw invalidRequest(`Unknown optimization target "${text}"`, { target: text }, "Use skill:<name>, system_prompt, planner_prompt or judge_rubric");
32
+ throw invalidRequest(`Unknown optimization target "${text}"`, { target: text }, "Use skill:<name>, tool:<name>, system_prompt, planner_prompt or judge_rubric");
29
33
  }
30
34
  /** The text a case's split is derived from: trimmed, whitespace collapsed, lower-cased. */
31
35
  export function normalizeCaseInput(input) {
@@ -0,0 +1,22 @@
1
+ -- nexo:foreign-keys-off
2
+ -- Tool descriptions become an optimization target (artifact kind 'tool_description', name = the tool's name).
3
+ -- artifacts is rebuilt because SQLite cannot widen a CHECK in place. optim_runs and optim_candidates reference it,
4
+ -- so the runner switches foreign keys off for this file and runs foreign_key_check before it commits.
5
+ CREATE TABLE artifacts_new (
6
+ id TEXT PRIMARY KEY,
7
+ kind TEXT NOT NULL CHECK (kind IN ('system_prompt','skill','playbook','router_policy','judge_rubric','planner_prompt','tool_description')),
8
+ name TEXT NOT NULL,
9
+ version INTEGER NOT NULL,
10
+ content TEXT NOT NULL,
11
+ content_hash TEXT NOT NULL,
12
+ parent_version_id TEXT REFERENCES artifacts(id),
13
+ origin TEXT NOT NULL CHECK (origin IN ('manual','ace','gepa')),
14
+ status TEXT NOT NULL CHECK (status IN ('active','candidate','retired')),
15
+ created_at TEXT NOT NULL,
16
+ UNIQUE (kind, name, version)
17
+ );
18
+ INSERT INTO artifacts_new (id, kind, name, version, content, content_hash, parent_version_id, origin, status, created_at)
19
+ SELECT id, kind, name, version, content, content_hash, parent_version_id, origin, status, created_at FROM artifacts;
20
+ DROP TABLE artifacts;
21
+ ALTER TABLE artifacts_new RENAME TO artifacts;
22
+ CREATE UNIQUE INDEX artifacts_one_active ON artifacts(kind, name) WHERE status = 'active';
@@ -0,0 +1,11 @@
1
+ import { tmpdir } from "node:os";
2
+ import { createBuiltinTools } from "./builtin.js";
3
+ import { FileLeases } from "./leases.js";
4
+ /**
5
+ * The built-in tools' own descriptions by tool name: the text an optimization of `tool:<name>` starts from and a run
6
+ * uses when no promoted or candidate description replaces it. Read from the tools themselves, so there is one copy.
7
+ */
8
+ export function builtinToolDescriptions() {
9
+ const tools = createBuiltinTools({ workspaceRoot: tmpdir(), holder: "descriptions", leases: new FileLeases(), bashTimeoutMs: 1 });
10
+ return Object.fromEntries(tools.map((t) => [t.name, t.description]));
11
+ }
@@ -1,6 +1,7 @@
1
1
  export * from "./builtin.js";
2
2
  export * from "./change-tracker.js";
3
3
  export * from "./confine.js";
4
+ export * from "./descriptions.js";
4
5
  export * from "./leases.js";
5
6
  export * from "./policy.js";
6
7
  export * from "./read-only.js";