@hexdecimal16/nexo 0.2.1 → 0.3.0
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 +3 -0
- package/cli/src/common.js +3 -2
- package/cli/src/main.js +3 -0
- package/cli/src/optimize.js +2 -2
- package/cli/src/update.js +78 -0
- package/core/src/engine/run.js +16 -2
- package/core/src/optimize/optimize.js +8 -0
- package/core/src/providers/claude-p.js +2 -18
- package/core/src/providers/kiro-p.js +395 -147
- package/core/src/providers/process-runner.js +91 -0
- package/optimize/src/proposer.js +2 -0
- package/optimize/src/target.js +2 -0
- package/package.json +1 -1
- package/store/src/db.js +26 -8
- package/store/src/eval-cases.js +6 -2
- package/store/src/migrations/009_tool_description_artifacts.sql +22 -0
- package/tools/src/descriptions.js +11 -0
- package/tools/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# nexo
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@hexdecimal16/nexo)
|
|
4
|
+
[](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":
|
package/cli/src/optimize.js
CHANGED
|
@@ -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
|
+
}
|
package/core/src/engine/run.js
CHANGED
|
@@ -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
|
|
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
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
2
|
import { tmpdir } from "node:os";
|
|
4
3
|
import { join } from "node:path";
|
|
5
|
-
import { createInterface } from "node:readline";
|
|
6
4
|
import { calculateCost, createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
7
5
|
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
6
|
+
import { spawnGroup } from "./process-runner.js";
|
|
8
7
|
/** Provider id: models are referenced as `claude-p/<model>`, e.g. `claude-p/opus` or `claude-p/claude-sonnet-5-5`. */
|
|
9
8
|
export const CLAUDE_P_PROVIDER = "claude-p";
|
|
10
9
|
const CLAUDE_P_API = "claude-p";
|
|
@@ -15,22 +14,7 @@ const ALIASES = ["opus", "sonnet", "haiku", "fable"];
|
|
|
15
14
|
/** `claude --effort` values; pi's `minimal` maps to `low`. */
|
|
16
15
|
const EFFORTS = new Set(["low", "medium", "high", "xhigh", "max"]);
|
|
17
16
|
function spawnRunner(command) {
|
|
18
|
-
return (
|
|
19
|
-
const child = spawn(command, args, { cwd, stdio: ["pipe", "pipe", "pipe"], ...(signal ? { signal } : {}) });
|
|
20
|
-
let stderr = "";
|
|
21
|
-
child.stderr.setEncoding("utf8");
|
|
22
|
-
child.stderr.on("data", (chunk) => {
|
|
23
|
-
if (stderr.length < 16_384)
|
|
24
|
-
stderr += chunk;
|
|
25
|
-
});
|
|
26
|
-
const exit = new Promise((resolve) => {
|
|
27
|
-
child.on("error", (error) => resolve({ code: null, stderr: stderr || error.message }));
|
|
28
|
-
child.on("close", (code) => resolve({ code, stderr }));
|
|
29
|
-
});
|
|
30
|
-
child.stdin.on("error", () => { });
|
|
31
|
-
child.stdin.end(stdin);
|
|
32
|
-
return { lines: createInterface({ input: child.stdout, crlfDelay: Infinity }), exit };
|
|
33
|
-
};
|
|
17
|
+
return (request) => spawnGroup(command, request);
|
|
34
18
|
}
|
|
35
19
|
/** The Claude models from pi-ai's Anthropic catalog, re-homed under `claude-p`, plus the CLI's family aliases. */
|
|
36
20
|
function claudePModels() {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { rmSync } from "node:fs";
|
|
2
4
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
5
|
import { tmpdir } from "node:os";
|
|
4
6
|
import { join } from "node:path";
|
|
@@ -11,25 +13,86 @@ export const KIRO_P_PROVIDER = "kiro-p";
|
|
|
11
13
|
const KIRO_P_API = "kiro-p";
|
|
12
14
|
/** Environment variable naming the Kiro CLI executable; `kiro-cli` on the PATH otherwise. */
|
|
13
15
|
export const KIRO_P_COMMAND_ENV = "NEXO_KIRO_P_COMMAND";
|
|
14
|
-
/**
|
|
16
|
+
/** Environment variable overriding how long a turn may stay silent before it is cancelled (milliseconds). */
|
|
17
|
+
export const KIRO_P_IDLE_ENV = "NEXO_KIRO_P_IDLE_MS";
|
|
18
|
+
const IDLE_MS_DEFAULT = 120_000;
|
|
19
|
+
/** How long a cancelled turn gets to settle before the whole process is considered stuck and killed. */
|
|
20
|
+
const CANCEL_GRACE_MS = 5_000;
|
|
21
|
+
/** `effortLevel` values; pi's `minimal` maps to `low`. */
|
|
15
22
|
const EFFORTS = new Set(["low", "medium", "high", "xhigh", "max"]);
|
|
23
|
+
const isWindows = process.platform === "win32";
|
|
24
|
+
const liveGroups = new Set();
|
|
25
|
+
const liveDirs = new Set();
|
|
26
|
+
let exitHandlerRegistered = false;
|
|
27
|
+
function registerExitHandler() {
|
|
28
|
+
if (exitHandlerRegistered)
|
|
29
|
+
return;
|
|
30
|
+
exitHandlerRegistered = true;
|
|
31
|
+
process.on("exit", () => {
|
|
32
|
+
for (const pgid of liveGroups) {
|
|
33
|
+
try {
|
|
34
|
+
process.kill(-pgid, "SIGKILL");
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// the process group already exited
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const dir of liveDirs)
|
|
41
|
+
rmSync(dir, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function killGroup(child) {
|
|
45
|
+
try {
|
|
46
|
+
if (!isWindows && child.pid !== undefined)
|
|
47
|
+
process.kill(-child.pid, "SIGKILL");
|
|
48
|
+
else
|
|
49
|
+
child.kill("SIGKILL");
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// the process group already exited
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Spawns the server in its own process group; the pipes are unref'd so an idle server never keeps the host alive. */
|
|
16
56
|
function spawnRunner(command) {
|
|
17
|
-
return ({ args,
|
|
18
|
-
|
|
57
|
+
return ({ args, cwd }) => {
|
|
58
|
+
registerExitHandler();
|
|
59
|
+
const child = spawn(command, args, { cwd, stdio: ["pipe", "pipe", "pipe"], detached: !isWindows });
|
|
60
|
+
const pgid = child.pid;
|
|
61
|
+
if (pgid !== undefined && !isWindows)
|
|
62
|
+
liveGroups.add(pgid);
|
|
19
63
|
let stderr = "";
|
|
20
64
|
child.stderr.setEncoding("utf8");
|
|
21
65
|
child.stderr.on("data", (chunk) => {
|
|
22
66
|
if (stderr.length < 16_384)
|
|
23
67
|
stderr += chunk;
|
|
24
|
-
onStderr?.(chunk);
|
|
25
68
|
});
|
|
69
|
+
child.stdin.on("error", () => { });
|
|
26
70
|
const exit = new Promise((resolve) => {
|
|
27
|
-
child.on("error", (error) =>
|
|
28
|
-
|
|
71
|
+
child.on("error", (error) => {
|
|
72
|
+
if (pgid !== undefined)
|
|
73
|
+
liveGroups.delete(pgid);
|
|
74
|
+
resolve({ code: null, stderr: stderr || error.message });
|
|
75
|
+
});
|
|
76
|
+
child.on("close", (code) => {
|
|
77
|
+
if (pgid !== undefined)
|
|
78
|
+
liveGroups.delete(pgid);
|
|
79
|
+
resolve({ code, stderr });
|
|
80
|
+
});
|
|
29
81
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
82
|
+
return {
|
|
83
|
+
write: (line) => {
|
|
84
|
+
child.stdin.write(`${line}\n`);
|
|
85
|
+
},
|
|
86
|
+
lines: createInterface({ input: child.stdout, crlfDelay: Infinity }),
|
|
87
|
+
exit,
|
|
88
|
+
kill: () => killGroup(child),
|
|
89
|
+
hold: (on) => {
|
|
90
|
+
const method = on ? "ref" : "unref";
|
|
91
|
+
child[method]();
|
|
92
|
+
for (const s of [child.stdin, child.stdout, child.stderr])
|
|
93
|
+
s[method]?.();
|
|
94
|
+
},
|
|
95
|
+
};
|
|
33
96
|
};
|
|
34
97
|
}
|
|
35
98
|
/** Kiro's model ids (`kiro-cli chat --list-models`) with their context windows. Kiro bills in credits, so cost stays zero. */
|
|
@@ -176,9 +239,163 @@ function effortFlag(reasoning) {
|
|
|
176
239
|
function emptyUsage() {
|
|
177
240
|
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
178
241
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
242
|
+
/**
|
|
243
|
+
* One long-lived `kiro-cli acp --agent-engine=v3` server. Requests are matched to responses by id, session updates are
|
|
244
|
+
* routed by session id, and every pending request fails at once when the process goes away.
|
|
245
|
+
*/
|
|
246
|
+
class AcpConnection {
|
|
247
|
+
closed = false;
|
|
248
|
+
dir;
|
|
249
|
+
nextId = 0;
|
|
250
|
+
pending = new Map();
|
|
251
|
+
listeners = new Map();
|
|
252
|
+
agents = new Map();
|
|
253
|
+
proc;
|
|
254
|
+
onClose;
|
|
255
|
+
constructor(proc, dir, onClose) {
|
|
256
|
+
this.proc = proc;
|
|
257
|
+
this.dir = dir;
|
|
258
|
+
this.onClose = onClose;
|
|
259
|
+
}
|
|
260
|
+
/** Starts the server and completes the `initialize` handshake. */
|
|
261
|
+
static async open(run, onClose) {
|
|
262
|
+
registerExitHandler();
|
|
263
|
+
// The agents live in a throwaway workspace: Kiro finds them under ./.kiro/agents, and no project files are in reach.
|
|
264
|
+
const dir = await mkdtemp(join(tmpdir(), "nexo-kiro-p-"));
|
|
265
|
+
liveDirs.add(dir);
|
|
266
|
+
await mkdir(join(dir, ".kiro", "agents"), { recursive: true });
|
|
267
|
+
const proc = run({ args: ["acp", "--agent-engine=v3"], cwd: dir });
|
|
268
|
+
const conn = new AcpConnection(proc, dir, onClose);
|
|
269
|
+
void conn.pump();
|
|
270
|
+
void proc.exit.then((exit) => conn.shutdown(new Error(exit.stderr.trim().split("\n").at(-1) || `kiro-cli exited with code ${exit.code ?? "null"}`)));
|
|
271
|
+
try {
|
|
272
|
+
await conn.request("initialize", { protocolVersion: 1, clientCapabilities: {}, clientInfo: { name: "nexo", version: "0" } });
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
conn.shutdown(error instanceof Error ? error : new Error(String(error)));
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
return conn;
|
|
279
|
+
}
|
|
280
|
+
async pump() {
|
|
281
|
+
try {
|
|
282
|
+
for await (const line of this.proc.lines)
|
|
283
|
+
this.handle(line);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// the stream was torn down with the process
|
|
287
|
+
}
|
|
288
|
+
this.shutdown(new Error("kiro-cli closed its output"));
|
|
289
|
+
}
|
|
290
|
+
handle(line) {
|
|
291
|
+
if (!line.trim())
|
|
292
|
+
return;
|
|
293
|
+
let m;
|
|
294
|
+
try {
|
|
295
|
+
m = JSON.parse(line);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (m.id !== undefined && m.method) {
|
|
301
|
+
// Agent-to-client request: refuse permissions (the agents deny everything anyway) and error on the rest, so the agent never waits.
|
|
302
|
+
if (m.method === "session/request_permission") {
|
|
303
|
+
const options = (m.params?.options ?? []);
|
|
304
|
+
const pick = options.find((o) => o.kind === "reject_once") ?? options.find((o) => o.kind === "reject_always");
|
|
305
|
+
this.send({ id: m.id, result: { outcome: pick ? { outcome: "selected", optionId: pick.optionId } : { outcome: "cancelled" } } });
|
|
306
|
+
}
|
|
307
|
+
else
|
|
308
|
+
this.send({ id: m.id, error: { code: -32601, message: "Method not found" } });
|
|
309
|
+
}
|
|
310
|
+
else if (m.id !== undefined) {
|
|
311
|
+
const p = this.pending.get(Number(m.id));
|
|
312
|
+
if (!p)
|
|
313
|
+
return;
|
|
314
|
+
this.settle(Number(m.id));
|
|
315
|
+
if (m.error)
|
|
316
|
+
p.reject(new Error(m.error.message ?? `error ${m.error.code ?? ""}`));
|
|
317
|
+
else
|
|
318
|
+
p.resolve(m.result);
|
|
319
|
+
}
|
|
320
|
+
else if (m.method === "session/update") {
|
|
321
|
+
const sessionId = m.params?.sessionId;
|
|
322
|
+
if (typeof sessionId === "string")
|
|
323
|
+
this.listeners.get(sessionId)?.(m.params?.update);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
send(message) {
|
|
327
|
+
try {
|
|
328
|
+
this.proc.write(JSON.stringify({ jsonrpc: "2.0", ...message }));
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
// a dead process is reported through exit
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
settle(id) {
|
|
335
|
+
this.pending.delete(id);
|
|
336
|
+
// Idle server: let the host exit (the exit handler kills the server); a request in flight keeps the host alive.
|
|
337
|
+
if (this.pending.size === 0 && !this.closed)
|
|
338
|
+
this.proc.hold?.(false);
|
|
339
|
+
}
|
|
340
|
+
request(method, params) {
|
|
341
|
+
if (this.closed)
|
|
342
|
+
return Promise.reject(new Error("kiro-cli is not running"));
|
|
343
|
+
const id = ++this.nextId;
|
|
344
|
+
return new Promise((resolve, reject) => {
|
|
345
|
+
if (this.pending.size === 0)
|
|
346
|
+
this.proc.hold?.(true);
|
|
347
|
+
this.pending.set(id, { resolve, reject });
|
|
348
|
+
this.send({ id, method, params });
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
notify(method, params) {
|
|
352
|
+
this.send({ method, params });
|
|
353
|
+
}
|
|
354
|
+
listen(sessionId, fn) {
|
|
355
|
+
this.listeners.set(sessionId, fn);
|
|
356
|
+
}
|
|
357
|
+
unlisten(sessionId) {
|
|
358
|
+
this.listeners.delete(sessionId);
|
|
359
|
+
}
|
|
360
|
+
/** Writes the agent file for `name` once; later sessions select it with `modeId`. */
|
|
361
|
+
ensureAgent(name, prompt) {
|
|
362
|
+
let written = this.agents.get(name);
|
|
363
|
+
if (!written) {
|
|
364
|
+
const agent = {
|
|
365
|
+
name,
|
|
366
|
+
description: "nexo model turn",
|
|
367
|
+
prompt,
|
|
368
|
+
tools: [],
|
|
369
|
+
// `tools: []` alone still leaves Kiro's skill tool in the request, which keeps the model in tool-calling mode and
|
|
370
|
+
// makes it call the actions below natively ("Tool X is not available"). Denying every capability empties the
|
|
371
|
+
// request's tool list, so the model cannot emit a tool call at all.
|
|
372
|
+
permissions: { rules: [{ capability: "all", effect: "deny" }] },
|
|
373
|
+
mcpServers: {},
|
|
374
|
+
includeMcpJson: false,
|
|
375
|
+
includePowers: false,
|
|
376
|
+
};
|
|
377
|
+
written = writeFile(join(this.dir, ".kiro", "agents", `${name}.json`), JSON.stringify(agent), "utf8");
|
|
378
|
+
this.agents.set(name, written);
|
|
379
|
+
}
|
|
380
|
+
return written;
|
|
381
|
+
}
|
|
382
|
+
/** Fails everything in flight, stops the process and removes the workspace. Safe to call more than once. */
|
|
383
|
+
shutdown(reason) {
|
|
384
|
+
if (this.closed)
|
|
385
|
+
return;
|
|
386
|
+
this.closed = true;
|
|
387
|
+
this.onClose();
|
|
388
|
+
for (const p of this.pending.values())
|
|
389
|
+
p.reject(reason);
|
|
390
|
+
this.pending.clear();
|
|
391
|
+
this.listeners.clear();
|
|
392
|
+
this.proc.kill();
|
|
393
|
+
liveDirs.delete(this.dir);
|
|
394
|
+
void rm(this.dir, { recursive: true, force: true }).catch(() => { });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const RETRY_NOTE = "Your previous reply was not a single valid JSON object in the required format. Reply again with only that JSON object.";
|
|
398
|
+
function streamKiroP(getConn, model, context, options) {
|
|
182
399
|
const stream = createAssistantMessageEventStream();
|
|
183
400
|
const partial = {
|
|
184
401
|
role: "assistant",
|
|
@@ -211,143 +428,161 @@ function streamKiroP(run, model, context, options) {
|
|
|
211
428
|
const tools = getCurrentTools(context.messages);
|
|
212
429
|
const useTools = tools.length > 0 && options?.toolChoice !== "none";
|
|
213
430
|
const systemPrompt = [getCurrentSystemPrompt(context.messages), useTools ? kiroToolsSection(tools) : ""].filter((s) => s.length > 0).join("\n\n");
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
431
|
+
const agentPrompt = systemPrompt || "You are a helpful assistant.";
|
|
432
|
+
const agentName = `nexo-${createHash("sha256").update(agentPrompt).digest("hex").slice(0, 12)}`;
|
|
433
|
+
const effort = effortFlag(options?.reasoning);
|
|
434
|
+
const idleMs = Number(process.env[KIRO_P_IDLE_ENV]) || IDLE_MS_DEFAULT;
|
|
435
|
+
/** A fresh session on the shared server, running the agent that carries this turn's system prompt. */
|
|
436
|
+
const startSession = async () => {
|
|
437
|
+
const conn = await getConn();
|
|
438
|
+
await conn.ensureAgent(agentName, agentPrompt);
|
|
439
|
+
// Kiro notices a new agent file a moment after it is written, and a session created sooner silently runs its
|
|
440
|
+
// default agent (which has tools). Retry a few times until the requested agent is the session's mode.
|
|
441
|
+
let mode;
|
|
442
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
443
|
+
if (attempt > 0)
|
|
444
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
445
|
+
const res = (await conn.request("session/new", {
|
|
446
|
+
cwd: conn.dir,
|
|
447
|
+
mcpServers: [],
|
|
448
|
+
_meta: { kiro: { modeId: agentName, modelId: model.id, ...(effort ? { effortLevel: effort } : {}) } },
|
|
449
|
+
}));
|
|
450
|
+
mode = res.modes?.currentModeId;
|
|
451
|
+
if (res.sessionId && mode === agentName)
|
|
452
|
+
return { conn, id: res.sessionId };
|
|
453
|
+
}
|
|
454
|
+
// Still not applied: refuse to run rather than let the model see Kiro's default tools.
|
|
455
|
+
throw new Error(`stopped: agent "${agentName}" not applied (mode ${mode ?? "none"})`);
|
|
456
|
+
};
|
|
457
|
+
/** One prompt on a session; plain turns stream their text and thinking into `partial` as it arrives. */
|
|
458
|
+
const runOnce = async (session, text) => {
|
|
459
|
+
const { conn, id } = session;
|
|
460
|
+
const outcome = { text: "" };
|
|
461
|
+
// A native tool call means the model was offered tools after all; Kiro would answer "not available" and loop.
|
|
462
|
+
let nativeCall;
|
|
463
|
+
let stalled = false;
|
|
464
|
+
let open;
|
|
465
|
+
let idle;
|
|
466
|
+
let killTimer;
|
|
467
|
+
const cancel = () => conn.notify("session/cancel", { sessionId: id });
|
|
468
|
+
const arm = () => {
|
|
469
|
+
clearTimeout(idle);
|
|
470
|
+
idle = setTimeout(() => {
|
|
471
|
+
stalled = true;
|
|
472
|
+
cancel();
|
|
473
|
+
killTimer = setTimeout(() => conn.shutdown(new Error("kiro-cli did not answer the cancel")), CANCEL_GRACE_MS);
|
|
474
|
+
}, idleMs);
|
|
231
475
|
};
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
249
|
-
const proc = run({
|
|
250
|
-
args,
|
|
251
|
-
stdin,
|
|
252
|
-
cwd,
|
|
253
|
-
signal: kill.signal,
|
|
254
|
-
onStderr: (chunk) => {
|
|
255
|
-
const m = /\[warn\] (agent "[^"]*"[^\n]*)/.exec(chunk);
|
|
256
|
-
if (m && !fellBack) {
|
|
257
|
-
fellBack = m[1];
|
|
258
|
-
kill.abort();
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
});
|
|
262
|
-
const outcome = { finalText: "" };
|
|
263
|
-
let open;
|
|
264
|
-
const close = () => {
|
|
265
|
-
if (!open)
|
|
266
|
-
return;
|
|
267
|
-
const block = partial.content[open.index];
|
|
268
|
-
if (block?.type === "text")
|
|
269
|
-
stream.push({ type: "text_end", contentIndex: open.index, content: block.text, partial: snapshot() });
|
|
270
|
-
else if (block?.type === "thinking")
|
|
271
|
-
stream.push({ type: "thinking_end", contentIndex: open.index, content: block.thinking, partial: snapshot() });
|
|
272
|
-
open = undefined;
|
|
273
|
-
};
|
|
274
|
-
try {
|
|
275
|
-
for await (const line of proc.lines) {
|
|
276
|
-
if (!line.trim())
|
|
277
|
-
continue;
|
|
278
|
-
let event;
|
|
279
|
-
try {
|
|
280
|
-
event = JSON.parse(line);
|
|
281
|
-
}
|
|
282
|
-
catch {
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
const data = event.data ?? {};
|
|
286
|
-
if (event.type === "runFinished") {
|
|
287
|
-
outcome.finalText = data.finalText ?? "";
|
|
288
|
-
outcome.stopReason = data.stopReason;
|
|
289
|
-
if (data.status !== "success")
|
|
290
|
-
outcome.error = `run ${data.status ?? "failed"}${data.stopReason ? ` (${data.stopReason})` : ""}`;
|
|
291
|
-
}
|
|
292
|
-
else if (event.type === "runError")
|
|
293
|
-
outcome.error = data.message ?? "run failed";
|
|
294
|
-
else if (event.type === "sessionUpdate" && data.update?.sessionUpdate === "tool_call") {
|
|
295
|
-
if (!nativeCall) {
|
|
296
|
-
nativeCall = data.update.title ?? "tool";
|
|
297
|
-
kill.abort();
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
else if (event.type === "sessionUpdate" && !useTools) {
|
|
301
|
-
const kind = data.update?.sessionUpdate === "agent_message_chunk" ? "text" : data.update?.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
302
|
-
const delta = data.update?.content?.type === "text" ? data.update.content.text : undefined;
|
|
303
|
-
if (!kind || !delta)
|
|
304
|
-
continue;
|
|
305
|
-
if (open?.kind !== kind) {
|
|
306
|
-
close();
|
|
307
|
-
open = { kind, index: partial.content.length };
|
|
308
|
-
partial.content.push(kind === "text" ? { type: "text", text: "" } : { type: "thinking", thinking: "" });
|
|
309
|
-
stream.push({ type: kind === "text" ? "text_start" : "thinking_start", contentIndex: open.index, partial: snapshot() });
|
|
310
|
-
}
|
|
311
|
-
const block = partial.content[open.index];
|
|
312
|
-
if (block.type === "text")
|
|
313
|
-
block.text += delta;
|
|
314
|
-
else if (block.type === "thinking")
|
|
315
|
-
block.thinking += delta;
|
|
316
|
-
stream.push({ type: kind === "text" ? "text_delta" : "thinking_delta", contentIndex: open.index, delta, partial: snapshot() });
|
|
317
|
-
}
|
|
476
|
+
const close = () => {
|
|
477
|
+
if (!open)
|
|
478
|
+
return;
|
|
479
|
+
const block = partial.content[open.index];
|
|
480
|
+
if (block?.type === "text")
|
|
481
|
+
stream.push({ type: "text_end", contentIndex: open.index, content: block.text, partial: snapshot() });
|
|
482
|
+
else if (block?.type === "thinking")
|
|
483
|
+
stream.push({ type: "thinking_end", contentIndex: open.index, content: block.thinking, partial: snapshot() });
|
|
484
|
+
open = undefined;
|
|
485
|
+
};
|
|
486
|
+
conn.listen(id, (u) => {
|
|
487
|
+
arm();
|
|
488
|
+
if (u.sessionUpdate === "tool_call") {
|
|
489
|
+
if (!nativeCall) {
|
|
490
|
+
nativeCall = u.title ?? "tool";
|
|
491
|
+
cancel();
|
|
318
492
|
}
|
|
319
|
-
|
|
320
|
-
const exit = await proc.exit;
|
|
321
|
-
if (fellBack)
|
|
322
|
-
outcome.error = `stopped: ${fellBack}`;
|
|
323
|
-
else if (nativeCall)
|
|
324
|
-
outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
|
|
325
|
-
else if (!outcome.error && outcome.stopReason === undefined)
|
|
326
|
-
outcome.error = exit.stderr.trim().split("\n").at(-1) || `exited with code ${exit.code ?? "null"} without a result`;
|
|
493
|
+
return;
|
|
327
494
|
}
|
|
328
|
-
|
|
329
|
-
|
|
495
|
+
const kind = u.sessionUpdate === "agent_message_chunk" ? "text" : u.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
496
|
+
const delta = u.content?.type === "text" ? u.content.text : undefined;
|
|
497
|
+
if (!kind || !delta)
|
|
498
|
+
return;
|
|
499
|
+
if (kind === "text")
|
|
500
|
+
outcome.text += delta;
|
|
501
|
+
if (useTools)
|
|
502
|
+
return;
|
|
503
|
+
if (open?.kind !== kind) {
|
|
504
|
+
close();
|
|
505
|
+
open = { kind, index: partial.content.length };
|
|
506
|
+
partial.content.push(kind === "text" ? { type: "text", text: "" } : { type: "thinking", thinking: "" });
|
|
507
|
+
stream.push({ type: kind === "text" ? "text_start" : "thinking_start", contentIndex: open.index, partial: snapshot() });
|
|
330
508
|
}
|
|
331
|
-
|
|
509
|
+
const block = partial.content[open.index];
|
|
510
|
+
if (block.type === "text")
|
|
511
|
+
block.text += delta;
|
|
512
|
+
else if (block.type === "thinking")
|
|
513
|
+
block.thinking += delta;
|
|
514
|
+
stream.push({ type: kind === "text" ? "text_delta" : "thinking_delta", contentIndex: open.index, delta, partial: snapshot() });
|
|
515
|
+
});
|
|
516
|
+
let abort;
|
|
517
|
+
const aborted = new Promise((resolve) => {
|
|
518
|
+
abort = () => resolve("aborted");
|
|
519
|
+
});
|
|
520
|
+
const onAbort = () => {
|
|
521
|
+
cancel();
|
|
522
|
+
abort();
|
|
332
523
|
};
|
|
333
|
-
|
|
524
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
525
|
+
arm();
|
|
526
|
+
try {
|
|
527
|
+
const settled = await Promise.race([
|
|
528
|
+
conn.request("session/prompt", { sessionId: id, prompt: [{ type: "text", text }] }).then((r) => ({ r: r }), (e) => ({ e })),
|
|
529
|
+
aborted,
|
|
530
|
+
]);
|
|
531
|
+
close();
|
|
532
|
+
if (settled === "aborted")
|
|
533
|
+
outcome.aborted = true;
|
|
534
|
+
else if (stalled)
|
|
535
|
+
outcome.error = `no output for ${Math.round(idleMs / 1000)}s`;
|
|
536
|
+
else if (nativeCall)
|
|
537
|
+
outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
|
|
538
|
+
else if ("e" in settled) {
|
|
539
|
+
outcome.error = settled.e.message;
|
|
540
|
+
outcome.lost = conn.closed;
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
outcome.stopReason = settled.r?.stopReason;
|
|
544
|
+
if (outcome.stopReason === "cancelled")
|
|
545
|
+
outcome.error = "run cancelled";
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
finally {
|
|
549
|
+
clearTimeout(idle);
|
|
550
|
+
clearTimeout(killTimer);
|
|
551
|
+
conn.unlisten(id);
|
|
552
|
+
signal?.removeEventListener("abort", onAbort);
|
|
553
|
+
}
|
|
554
|
+
return outcome;
|
|
555
|
+
};
|
|
556
|
+
try {
|
|
557
|
+
stream.push({ type: "start", partial: snapshot() });
|
|
558
|
+
const prompt = renderTranscript(context.messages);
|
|
559
|
+
let session = await startSession();
|
|
334
560
|
if (signal?.aborted)
|
|
335
561
|
return fail("aborted", "Request was aborted");
|
|
562
|
+
let outcome = await runOnce(session, prompt);
|
|
563
|
+
// The shared server died under this turn before anything streamed: start a fresh one and run it again, once.
|
|
564
|
+
if (outcome.lost && !signal?.aborted && partial.content.length === 0) {
|
|
565
|
+
session = await startSession();
|
|
566
|
+
outcome = await runOnce(session, prompt);
|
|
567
|
+
}
|
|
568
|
+
if (outcome.aborted || signal?.aborted)
|
|
569
|
+
return fail("aborted", "Request was aborted");
|
|
336
570
|
if (outcome.error)
|
|
337
571
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
338
572
|
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
339
573
|
if (useTools) {
|
|
340
|
-
let reply = parseReply(outcome.
|
|
574
|
+
let reply = parseReply(outcome.text, tools);
|
|
341
575
|
if (!reply) {
|
|
342
|
-
|
|
343
|
-
|
|
576
|
+
// Same session, so the model still has the transcript: only the correction is sent.
|
|
577
|
+
outcome = await runOnce(session, RETRY_NOTE);
|
|
578
|
+
if (outcome.aborted || signal?.aborted)
|
|
344
579
|
return fail("aborted", "Request was aborted");
|
|
345
580
|
if (outcome.error)
|
|
346
581
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
347
|
-
reply = parseReply(outcome.
|
|
582
|
+
reply = parseReply(outcome.text, tools);
|
|
348
583
|
}
|
|
349
584
|
// Still not the JSON format: keep what the model said as a plain answer rather than dropping it.
|
|
350
|
-
const { text, calls } = reply ?? { text: outcome.
|
|
585
|
+
const { text, calls } = reply ?? { text: outcome.text, calls: [] };
|
|
351
586
|
if (text)
|
|
352
587
|
pushText(text);
|
|
353
588
|
for (const call of calls) {
|
|
@@ -360,8 +595,8 @@ function streamKiroP(run, model, context, options) {
|
|
|
360
595
|
}
|
|
361
596
|
stopReason = calls.length > 0 ? "toolUse" : "stop";
|
|
362
597
|
}
|
|
363
|
-
else if (!partial.content.some((b) => b.type === "text") && outcome.
|
|
364
|
-
pushText(outcome.
|
|
598
|
+
else if (!partial.content.some((b) => b.type === "text") && outcome.text) {
|
|
599
|
+
pushText(outcome.text);
|
|
365
600
|
}
|
|
366
601
|
const message = { ...snapshot(), stopReason };
|
|
367
602
|
stream.push({ type: "done", reason: stopReason, message });
|
|
@@ -372,30 +607,43 @@ function streamKiroP(run, model, context, options) {
|
|
|
372
607
|
return fail("aborted", "Request was aborted");
|
|
373
608
|
fail("error", `kiro-cli failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
374
609
|
}
|
|
375
|
-
finally {
|
|
376
|
-
if (dir)
|
|
377
|
-
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
378
|
-
}
|
|
379
610
|
});
|
|
380
611
|
return stream;
|
|
381
612
|
}
|
|
382
613
|
/**
|
|
383
|
-
* Runs models through
|
|
384
|
-
* CLI's own login.
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
614
|
+
* Runs models through one long-lived Kiro CLI ACP server (`kiro-cli acp --agent-engine=v3`), authenticated by the
|
|
615
|
+
* CLI's own login. The server starts on the first call and is shared by every call after it, which removes the
|
|
616
|
+
* per-call CLI start-up. Each turn is a fresh session whose agent carries the system prompt (one agent file per
|
|
617
|
+
* prompt hash), no tools and a policy denying every capability, so Kiro's request carries no tool at all; a native
|
|
618
|
+
* tool call cancels the turn. Nexo tools are described in the prompt and the reply parsed as `{ text, tool_calls }`
|
|
619
|
+
* (one correction retry when it does not parse), so tool turns arrive whole rather than streamed. A turn silent for
|
|
620
|
+
* `$NEXO_KIRO_P_IDLE_MS` (120s) is cancelled; a server that dies is replaced on the next call, and a turn it dropped
|
|
621
|
+
* is re-run once. Kiro bills in credits and reports no token counts, so usage is empty.
|
|
389
622
|
*/
|
|
390
623
|
export function kiroPProvider(options = {}) {
|
|
391
624
|
const run = options.run ?? spawnRunner(options.command ?? process.env[KIRO_P_COMMAND_ENV] ?? "kiro-cli");
|
|
625
|
+
let current;
|
|
626
|
+
const getConn = () => {
|
|
627
|
+
if (current)
|
|
628
|
+
return current;
|
|
629
|
+
const opened = AcpConnection.open(run, () => {
|
|
630
|
+
if (current === opened)
|
|
631
|
+
current = undefined;
|
|
632
|
+
});
|
|
633
|
+
current = opened;
|
|
634
|
+
opened.catch(() => {
|
|
635
|
+
if (current === opened)
|
|
636
|
+
current = undefined;
|
|
637
|
+
});
|
|
638
|
+
return opened;
|
|
639
|
+
};
|
|
392
640
|
const api = {
|
|
393
|
-
stream: (model, context, opts) => streamKiroP(
|
|
394
|
-
streamSimple: (model, context, opts) => streamKiroP(
|
|
641
|
+
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
642
|
+
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
395
643
|
};
|
|
396
644
|
return createProvider({
|
|
397
645
|
id: KIRO_P_PROVIDER,
|
|
398
|
-
name: "Kiro CLI (kiro-cli
|
|
646
|
+
name: "Kiro CLI (kiro-cli acp)",
|
|
399
647
|
auth: {
|
|
400
648
|
apiKey: {
|
|
401
649
|
name: "Kiro CLI login",
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
/** How long a process group gets between SIGTERM and SIGKILL after an abort. */
|
|
4
|
+
const KILL_GRACE_MS = 2_000;
|
|
5
|
+
const isWindows = process.platform === "win32";
|
|
6
|
+
const liveGroups = new Set();
|
|
7
|
+
let exitHandlerRegistered = false;
|
|
8
|
+
function killLiveGroups() {
|
|
9
|
+
for (const pgid of liveGroups) {
|
|
10
|
+
try {
|
|
11
|
+
process.kill(-pgid, "SIGKILL");
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// the process group already exited
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
liveGroups.clear();
|
|
18
|
+
}
|
|
19
|
+
function signalGroup(child, sig) {
|
|
20
|
+
try {
|
|
21
|
+
if (!isWindows && child.pid !== undefined)
|
|
22
|
+
process.kill(-child.pid, sig);
|
|
23
|
+
else
|
|
24
|
+
child.kill(sig);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// the process group already exited
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Spawns `command` in its own process group. On abort the whole group gets SIGTERM (SIGKILL after a grace period), and
|
|
32
|
+
* `lines` and `exit` settle at once instead of waiting for the pipes to close: the CLIs we run start helpers of their
|
|
33
|
+
* own, and one that outlives the signal would otherwise hold the pipes open and keep the caller waiting. The groups
|
|
34
|
+
* are also killed when the host exits.
|
|
35
|
+
*/
|
|
36
|
+
export function spawnGroup(command, request) {
|
|
37
|
+
const { args, stdin, cwd, signal, onStderr } = request;
|
|
38
|
+
const child = spawn(command, args, { cwd, stdio: ["pipe", "pipe", "pipe"], detached: !isWindows });
|
|
39
|
+
const pgid = child.pid;
|
|
40
|
+
if (pgid !== undefined && !isWindows) {
|
|
41
|
+
if (!exitHandlerRegistered) {
|
|
42
|
+
exitHandlerRegistered = true;
|
|
43
|
+
process.on("exit", killLiveGroups);
|
|
44
|
+
}
|
|
45
|
+
liveGroups.add(pgid);
|
|
46
|
+
}
|
|
47
|
+
let stderr = "";
|
|
48
|
+
child.stderr.setEncoding("utf8");
|
|
49
|
+
child.stderr.on("data", (chunk) => {
|
|
50
|
+
if (stderr.length < 16_384)
|
|
51
|
+
stderr += chunk;
|
|
52
|
+
onStderr?.(chunk);
|
|
53
|
+
});
|
|
54
|
+
const lines = createInterface({ input: child.stdout, crlfDelay: Infinity });
|
|
55
|
+
let finish;
|
|
56
|
+
const exit = new Promise((resolve) => {
|
|
57
|
+
finish = resolve;
|
|
58
|
+
});
|
|
59
|
+
let killTimer;
|
|
60
|
+
const cleanup = () => {
|
|
61
|
+
if (pgid !== undefined)
|
|
62
|
+
liveGroups.delete(pgid);
|
|
63
|
+
if (killTimer)
|
|
64
|
+
clearTimeout(killTimer);
|
|
65
|
+
signal?.removeEventListener("abort", onAbort);
|
|
66
|
+
};
|
|
67
|
+
const onAbort = () => {
|
|
68
|
+
signalGroup(child, "SIGTERM");
|
|
69
|
+
killTimer = setTimeout(() => signalGroup(child, "SIGKILL"), KILL_GRACE_MS);
|
|
70
|
+
killTimer.unref();
|
|
71
|
+
child.stdout.destroy();
|
|
72
|
+
child.stderr.destroy();
|
|
73
|
+
lines.close();
|
|
74
|
+
finish({ code: null, stderr });
|
|
75
|
+
};
|
|
76
|
+
child.on("error", (error) => {
|
|
77
|
+
cleanup();
|
|
78
|
+
finish({ code: null, stderr: stderr || error.message });
|
|
79
|
+
});
|
|
80
|
+
child.on("close", (code) => {
|
|
81
|
+
cleanup();
|
|
82
|
+
finish({ code, stderr });
|
|
83
|
+
});
|
|
84
|
+
child.stdin.on("error", () => { });
|
|
85
|
+
child.stdin.end(stdin);
|
|
86
|
+
if (signal?.aborted)
|
|
87
|
+
onAbort();
|
|
88
|
+
else
|
|
89
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
90
|
+
return { lines, exit };
|
|
91
|
+
}
|
package/optimize/src/proposer.js
CHANGED
|
@@ -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
|
}
|
package/optimize/src/target.js
CHANGED
package/package.json
CHANGED
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
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
}
|
package/store/src/eval-cases.js
CHANGED
|
@@ -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
|
+
}
|
package/tools/src/index.js
CHANGED