@hexdecimal16/nexo 0.4.7 → 0.4.9

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.
Files changed (52) hide show
  1. package/cli/src/areas.js +49 -0
  2. package/cli/src/common.js +1 -1
  3. package/cli/src/home-migrate.js +168 -0
  4. package/cli/src/main.js +6 -0
  5. package/cli/src/optimize.js +2 -5
  6. package/cli/src/ui.js +1 -1
  7. package/core/src/config/schema.js +1 -0
  8. package/core/src/engine/charged-completion.js +9 -4
  9. package/core/src/engine/errors.js +29 -2
  10. package/core/src/engine/prompts.js +1 -1
  11. package/core/src/engine/run.js +11 -6
  12. package/core/src/index.js +1 -0
  13. package/core/src/learn/areas.js +192 -0
  14. package/core/src/learn/job.js +15 -1
  15. package/core/src/learn/wiki.js +26 -18
  16. package/core/src/optimize/climb.js +0 -2
  17. package/core/src/optimize/optimize.js +2 -5
  18. package/core/src/optimize/scratchpad-wiki.js +7 -3
  19. package/optimize/src/engine.js +1 -22
  20. package/optimize/src/index.js +0 -1
  21. package/optimize/src/report.js +0 -2
  22. package/package.json +1 -1
  23. package/skills/bundled/wiki/SKILL.md +23 -13
  24. package/store/src/eval-cases.js +12 -6
  25. package/store/src/paths.js +2 -2
  26. package/tools/src/home-folders.js +36 -0
  27. package/tools/src/index.js +2 -0
  28. package/tools/src/wiki-page.js +78 -0
  29. package/tui/src/questions.js +31 -0
  30. package/web/public/area.html +23 -0
  31. package/web/public/cost.html +1 -1
  32. package/web/public/home.html +23 -0
  33. package/web/public/index.html +1 -1
  34. package/web/public/judge.html +1 -1
  35. package/web/public/learning.html +1 -1
  36. package/web/public/lib/api-types.ts +0 -2
  37. package/web/public/lib/optim-view.ts +1 -1
  38. package/web/public/lib/wikis-view.ts +3 -1
  39. package/web/public/optim.html +1 -1
  40. package/web/public/page.html +24 -0
  41. package/web/public/pages/area.ts +59 -0
  42. package/web/public/pages/home.ts +66 -0
  43. package/web/public/pages/page.ts +62 -0
  44. package/web/public/pages/wikis.ts +43 -6
  45. package/web/public/run.html +1 -1
  46. package/web/public/sessions.html +1 -1
  47. package/web/public/style.css +58 -0
  48. package/web/public/wikis.html +1 -1
  49. package/web/src/api.js +14 -9
  50. package/web/src/home-api.js +156 -0
  51. package/web/src/server.js +16 -0
  52. package/optimize/src/merge.js +0 -73
@@ -0,0 +1,49 @@
1
+ import { approveArea, readRegistry, writeRegistry } from "../../core/src/index.js";
2
+ import { ensureNexoDirs, resolveNexoPaths } from "../../store/src/index.js";
3
+ import { NexoError } from "../../telemetry/src/index.js";
4
+ import { usageError } from "./common.js";
5
+ const AREAS_USAGE = "Usage: nexo areas [list] | nexo areas approve <id> | nexo areas reject <id>";
6
+ /** `nexo areas`: lists life areas and the unsorted queue; approve activates a proposed area, reject removes one. */
7
+ export function areasCommand(args, io, options) {
8
+ const [sub = "list", id, ...extra] = args;
9
+ if (extra.length > 0)
10
+ throw usageError(AREAS_USAGE);
11
+ const paths = resolveNexoPaths(options.env ?? process.env);
12
+ ensureNexoDirs(paths);
13
+ const registry = readRegistry(paths.homeRoot);
14
+ if (sub === "list") {
15
+ if (registry.areas.length === 0) {
16
+ io.stdout.write("No areas yet.\n");
17
+ return 0;
18
+ }
19
+ for (const status of ["active", "proposed"]) {
20
+ const areas = registry.areas.filter((a) => a.status === status);
21
+ if (areas.length === 0)
22
+ continue;
23
+ io.stdout.write(status === "active" ? "Active:\n" : "Proposed (approve with: nexo areas approve <id>):\n");
24
+ for (const a of areas)
25
+ io.stdout.write(` ${a.id.padEnd(10)} ${a.name}: ${a.scope}${a.wikis.length > 0 ? ` [wikis: ${a.wikis.join(", ")}]` : ""}\n`);
26
+ }
27
+ const labels = new Map();
28
+ for (const u of registry.unsorted)
29
+ labels.set(u.label, (labels.get(u.label) ?? 0) + 1);
30
+ if (labels.size > 0)
31
+ io.stdout.write(`Unsorted: ${[...labels].map(([l, n]) => `${l} (${n})`).join(", ")}\n`);
32
+ return 0;
33
+ }
34
+ if ((sub !== "approve" && sub !== "reject") || !id)
35
+ throw usageError(AREAS_USAGE);
36
+ if (sub === "approve") {
37
+ writeRegistry(paths.homeRoot, approveArea(registry, id));
38
+ io.stdout.write(`Approved area ${id}\n`);
39
+ return 0;
40
+ }
41
+ const area = registry.areas.find((a) => a.id === id);
42
+ if (!area)
43
+ throw new NexoError({ code: "NOT_FOUND", message: `No area "${id}"`, context: { id } });
44
+ if (area.status !== "proposed")
45
+ throw new NexoError({ code: "CONFLICT", message: `Area "${id}" is active; only proposed areas can be rejected`, context: { id } });
46
+ writeRegistry(paths.homeRoot, { ...registry, areas: registry.areas.filter((a) => a.id !== id) });
47
+ io.stdout.write(`Rejected area ${id}\n`);
48
+ return 0;
49
+ }
package/cli/src/common.js CHANGED
@@ -30,7 +30,7 @@ export const USAGE = `Usage:
30
30
  nexo eval accept <case-id> [--expected <text>]
31
31
  nexo eval dismiss <case-id>
32
32
  nexo eval golden revert <case-id>
33
- nexo optimize --target skill:<name>|tool:<name>|system_prompt|planner_prompt|judge_rubric [--budget-usd <n>] [--max-rollouts <n>] [--merge] [--seed <n>]
33
+ nexo optimize --target skill:<name>|tool:<name>|system_prompt|planner_prompt|judge_rubric [--budget-usd <n>] [--max-rollouts <n>] [--seed <n>]
34
34
  nexo optimize climb --target skill:<name>|tool:<name>|system_prompt|planner_prompt [--case <id>] [--guards <0-10>] [--budget-usd <n>] [--seed <n>]
35
35
  nexo optimize list
36
36
  nexo optimize show <id> [--diff]
@@ -0,0 +1,168 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
2
+ import { dirname, join, relative, resolve, sep } from "node:path";
3
+ import { parseArgs } from "node:util";
4
+ import { resolveNexoPaths } from "../../store/src/index.js";
5
+ import { NexoError } from "../../telemetry/src/index.js";
6
+ import { UNSORTED_AREA } from "../../tools/src/index.js";
7
+ import { usageError } from "./common.js";
8
+ const META_FILES = ["areas.json", "feed.jsonl", "decisions.jsonl"];
9
+ const MANIFEST = ".migrated-from-wikis.json";
10
+ const HOME_USAGE = "Usage: nexo home migrate [--apply | --undo]";
11
+ function isDir(p) {
12
+ try {
13
+ return statSync(p).isDirectory();
14
+ }
15
+ catch {
16
+ return false;
17
+ }
18
+ }
19
+ /** Where each folder of the old wikis root goes: <home>/<area>/<name>, or <home>/unsorted/<name> when no active area lists it. */
20
+ export function planHomeMigration(oldRoot, newRoot) {
21
+ const plan = { folders: [], files: [], conflicts: [] };
22
+ if (!isDir(oldRoot))
23
+ return plan;
24
+ let areas = [];
25
+ try {
26
+ const parsed = JSON.parse(readFileSync(join(oldRoot, "areas.json"), "utf8"));
27
+ if (Array.isArray(parsed.areas))
28
+ areas = parsed.areas;
29
+ }
30
+ catch {
31
+ areas = [];
32
+ }
33
+ const owner = new Map();
34
+ for (const a of areas) {
35
+ if (a.status !== "active" || typeof a.id !== "string" || !/^[a-z0-9][a-z0-9-]*$/.test(a.id) || !Array.isArray(a.wikis))
36
+ continue;
37
+ for (const w of a.wikis)
38
+ if (typeof w === "string" && !owner.has(w))
39
+ owner.set(w, a.id);
40
+ }
41
+ for (const name of readdirSync(oldRoot).sort()) {
42
+ if (name.startsWith("."))
43
+ continue;
44
+ const from = join(oldRoot, name);
45
+ if (!isDir(from))
46
+ continue;
47
+ const to = join(newRoot, owner.get(name) ?? UNSORTED_AREA, name);
48
+ if (existsSync(to))
49
+ plan.conflicts.push(to);
50
+ else
51
+ plan.folders.push({ from, to });
52
+ }
53
+ for (const f of META_FILES) {
54
+ const from = join(oldRoot, f);
55
+ if (!existsSync(from))
56
+ continue;
57
+ const to = join(newRoot, f);
58
+ if (existsSync(to))
59
+ plan.conflicts.push(to);
60
+ else
61
+ plan.files.push({ from, to });
62
+ }
63
+ return plan;
64
+ }
65
+ function htmlFiles(dir, out = []) {
66
+ for (const f of readdirSync(dir)) {
67
+ if (f === ".git")
68
+ continue;
69
+ const p = join(dir, f);
70
+ if (isDir(p))
71
+ htmlFiles(p, out);
72
+ else if (f.endsWith(".html"))
73
+ out.push(p);
74
+ }
75
+ return out;
76
+ }
77
+ const within = (p, root) => p === root || p.startsWith(root + sep);
78
+ /** After folders moved from `from` to `to`, rewrites relative links into another moved folder so they still resolve. Returns links changed. */
79
+ export function rewriteMovedLinks(moves) {
80
+ let changed = 0;
81
+ for (const m of moves) {
82
+ if (!isDir(m.to))
83
+ continue;
84
+ for (const file of htmlFiles(m.to)) {
85
+ const oldFile = join(m.from, relative(m.to, file));
86
+ const text = readFileSync(file, "utf8");
87
+ let n = 0;
88
+ const next = text.replace(/href="([^"#?:]+\.html)([#?][^"]*)?"/g, (whole, href, tail) => {
89
+ if (href.startsWith("/"))
90
+ return whole;
91
+ const target = resolve(dirname(oldFile), href);
92
+ const owner = moves.find((x) => within(target, x.from));
93
+ if (!owner)
94
+ return whole;
95
+ const rel = relative(dirname(file), join(owner.to, relative(owner.from, target))).split(sep).join("/");
96
+ if (rel === href)
97
+ return whole;
98
+ n++;
99
+ return `href="${rel}${tail ?? ""}"`;
100
+ });
101
+ if (n > 0) {
102
+ writeFileSync(file, next);
103
+ changed += n;
104
+ }
105
+ }
106
+ }
107
+ return changed;
108
+ }
109
+ function moveAll(moves, done) {
110
+ for (const m of moves) {
111
+ mkdirSync(dirname(m.to), { recursive: true });
112
+ renameSync(m.from, m.to);
113
+ done.push(m);
114
+ }
115
+ }
116
+ /** `nexo home migrate`: moves ~/.nexo/wikis/<name> to ~/.nexo/home/<area>/<name>. Dry run by default; --apply moves, --undo reverses. */
117
+ export function homeCommand(args, io, options) {
118
+ const [sub, ...rest] = args;
119
+ if (sub !== "migrate")
120
+ throw usageError(HOME_USAGE);
121
+ const { values } = parseArgs({ args: rest, options: { apply: { type: "boolean" }, undo: { type: "boolean" } } });
122
+ if (values.apply && values.undo)
123
+ throw usageError(HOME_USAGE);
124
+ const paths = resolveNexoPaths(options.env ?? process.env);
125
+ const oldRoot = join(paths.home, "wikis");
126
+ const newRoot = paths.homeRoot;
127
+ const manifestFile = join(newRoot, MANIFEST);
128
+ if (values.undo) {
129
+ if (!existsSync(manifestFile))
130
+ throw new NexoError({ code: "NOT_FOUND", message: `No migration manifest at ${manifestFile}`, context: { manifest: manifestFile } });
131
+ const manifest = JSON.parse(readFileSync(manifestFile, "utf8"));
132
+ const back = [...manifest.moves].reverse().map((m) => ({ from: m.to, to: m.from }));
133
+ for (const m of back)
134
+ if (existsSync(m.to))
135
+ throw new NexoError({ code: "CONFLICT", message: `${m.to} already exists; undo would overwrite it`, context: { path: m.to } });
136
+ moveAll(back, []);
137
+ const links = rewriteMovedLinks(manifest.folders.map((m) => ({ from: m.to, to: m.from })));
138
+ rmSync(manifestFile);
139
+ io.stdout.write(`Moved ${back.length} entries back to ${oldRoot} and restored ${links} links.\n`);
140
+ return 0;
141
+ }
142
+ const plan = planHomeMigration(oldRoot, newRoot);
143
+ if (plan.folders.length + plan.files.length + plan.conflicts.length === 0) {
144
+ io.stdout.write(`Nothing to migrate in ${oldRoot}.\n`);
145
+ return 0;
146
+ }
147
+ for (const m of [...plan.folders, ...plan.files])
148
+ io.stdout.write(` ${relative(paths.home, m.from)} -> ${relative(paths.home, m.to)}\n`);
149
+ if (plan.conflicts.length > 0) {
150
+ throw new NexoError({ code: "CONFLICT", message: `Already exists: ${plan.conflicts.join(", ")}`, hint: "Move or remove those paths, then run again", context: { conflicts: plan.conflicts } });
151
+ }
152
+ if (!values.apply) {
153
+ io.stdout.write("Dry run: nothing moved. Run `nexo home migrate --apply` to move, and `--undo` to reverse it.\n");
154
+ return 0;
155
+ }
156
+ mkdirSync(newRoot, { recursive: true });
157
+ const done = [];
158
+ try {
159
+ moveAll([...plan.folders, ...plan.files], done);
160
+ }
161
+ finally {
162
+ const moved = new Set(done);
163
+ writeFileSync(manifestFile, `${JSON.stringify({ at: new Date().toISOString(), moves: done, folders: plan.folders.filter((m) => moved.has(m)) }, null, 2)}\n`);
164
+ }
165
+ const links = rewriteMovedLinks(plan.folders);
166
+ io.stdout.write(`Moved ${done.length} entries into ${newRoot} and rewrote ${links} links. Undo with: nexo home migrate --undo\n`);
167
+ return 0;
168
+ }
package/cli/src/main.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { homeCommand } from "./home-migrate.js";
2
+ import { areasCommand } from "./areas.js";
1
3
  import { parseArgs } from "node:util";
2
4
  import { judgeFamilyWarnings } from "../../core/src/index.js";
3
5
  import { EFFORTS } from "../../router/src/index.js";
@@ -257,6 +259,10 @@ export async function runCli(argv, io, options = {}) {
257
259
  return await initCommand(rest, io, options, stdio);
258
260
  case "update":
259
261
  return await updateCommand(io, options);
262
+ case "home":
263
+ return homeCommand(rest, io, options);
264
+ case "areas":
265
+ return areasCommand(rest, io, options);
260
266
  case "sessions":
261
267
  return sessionsCommand(rest, io, options);
262
268
  // Read-only listing of the same file the TUI's Up key uses.
@@ -46,7 +46,7 @@ export function reportLines(db, run, report, minJudgeKappa, options = {}) {
46
46
  for (const c of h.cases)
47
47
  lines.push(` ${c.outcome.padEnd(4)} ${q(c.before.quality)} → ${q(c.after.quality)} ${escapeControl(c.input.replace(/\s+/g, " ").slice(0, 80))}`);
48
48
  if (!report.climb)
49
- lines.push(` ${report.iterations} iterations, ${report.lineage.length - 1} candidates, ${report.merges} merges${report.mergeNote ? ` (${escapeControl(report.mergeNote)})` : ""}`);
49
+ lines.push(` ${report.iterations} iterations, ${report.lineage.length - 1} candidates`);
50
50
  const e = report.evidence;
51
51
  lines.push(e.humanScored
52
52
  ? " evidence: human verdicts"
@@ -96,8 +96,6 @@ export function optimizeEventLine(event) {
96
96
  return ` minibatch ${q(event.parentQuality)} → ${q(event.childQuality)} ${event.accepted ? "✓ accepted" : "✗ not better"}${event.prediction ? ` · predicted fixes ${event.prediction.fixesHit}/${event.prediction.fixesPredicted}, risks ${event.prediction.atRiskHit}/${event.prediction.atRiskPredicted}${event.prediction.unpredictedRegressions > 0 ? `, ${event.prediction.unpredictedRegressions} unpredicted` : ""}` : ""}`;
97
97
  case "pareto":
98
98
  return ` pareto mean ${q(event.meanQuality)} at ${usd(event.meanCostUsd)}${event.newMember ? " · new Pareto member" : ""}`;
99
- case "merge":
100
- return ` merge of ${event.parents.join(" and ")} ${event.accepted ? "kept" : "dropped"}`;
101
99
  case "reflection":
102
100
  return event.problem ? ` reflection unusable: ${escapeControl(event.problem)}` : null;
103
101
  default:
@@ -125,7 +123,7 @@ function rolloutNotice(config) {
125
123
  async function runOptimize(args, io, options) {
126
124
  const { values } = parseArgs({
127
125
  args,
128
- options: { target: { type: "string" }, "budget-usd": { type: "string" }, "max-rollouts": { type: "string" }, merge: { type: "boolean" }, seed: { type: "string" } },
126
+ options: { target: { type: "string" }, "budget-usd": { type: "string" }, "max-rollouts": { type: "string" }, seed: { type: "string" } },
129
127
  });
130
128
  if (!values.target)
131
129
  throw usageError("nexo optimize needs --target skill:<name>|tool:<name>|system_prompt|planner_prompt|judge_rubric");
@@ -141,7 +139,6 @@ async function runOptimize(args, io, options) {
141
139
  target: values.target,
142
140
  budgetUsd: positiveNumber("--budget-usd", values["budget-usd"], false),
143
141
  maxRollouts: positiveNumber("--max-rollouts", values["max-rollouts"], true),
144
- merge: values.merge ?? false,
145
142
  ...(seed !== undefined ? { seed } : {}),
146
143
  signal: options.signal,
147
144
  onEvent: (event) => {
package/cli/src/ui.js CHANGED
@@ -85,7 +85,7 @@ export async function runUi(options) {
85
85
  judge,
86
86
  learning: { enabled: config?.learning.enabled ?? false },
87
87
  optimize: { minJudgeKappa: config?.optimize.minJudgeKappa ?? 0.6 },
88
- wikisDir: paths.wikis,
88
+ wikisDir: paths.homeRoot,
89
89
  companion: config ? companionModel(options, paths) : null,
90
90
  });
91
91
  }
@@ -44,6 +44,7 @@ export const ConfigSchema = Type.Object({
44
44
  maxConcurrent: Type.Optional(Type.Integer({ minimum: 1 })),
45
45
  maxRetries: Type.Optional(Type.Integer({ minimum: 0, maximum: 10 })),
46
46
  retryBaseDelayMs: Type.Optional(Type.Integer({ minimum: 0 })),
47
+ retryWindowMs: Type.Optional(Type.Integer({ minimum: 0 })),
47
48
  rateLimitBaseDelayMs: Type.Optional(Type.Integer({ minimum: 0 })),
48
49
  }, { additionalProperties: false })),
49
50
  router: Type.Object({ policy: ArtifactSource("router_policy"), maxEscalations: Type.Integer({ minimum: 0 }) }, { additionalProperties: false }),
@@ -1,7 +1,7 @@
1
1
  import { normalizeContext } from "@earendil-works/pi-ai";
2
2
  import { NexoError, toErrorPayload } from "../../../telemetry/src/index.js";
3
3
  import { estimateCallCostUsd } from "../cost/pricing.js";
4
- import { classifyProviderError, providerRetryDelayMs } from "./errors.js";
4
+ import { classifyProviderError, providerRetryDelayMs, withinRetryLimits } from "./errors.js";
5
5
  import { assistantText } from "./messages.js";
6
6
  /**
7
7
  * One LLM completion outside the agent loop (judge, self-check, reflector, curator). Each attempt reserves its estimate
@@ -15,7 +15,8 @@ export async function runChargedCompletion(payer, request, signal) {
15
15
  const { config, redact } = env.deps;
16
16
  const { model, span } = request;
17
17
  const providerCfg = config.providers[model.provider] ?? {};
18
- const maxRetries = request.maxRetries ?? providerCfg.maxRetries ?? 3;
18
+ const retryCfg = request.maxRetries !== undefined ? { ...providerCfg, maxRetries: request.maxRetries } : providerCfg;
19
+ let waitedMs = 0;
19
20
  // The reply is capped at the output tokens the estimate assumes, so a verbose reply cannot cost more than was reserved.
20
21
  const outputTokens = request.outputTokens ?? config.budgets.estimateOutputTokens;
21
22
  const maxTokens = model.maxTokens > 0 ? Math.min(outputTokens, model.maxTokens) : outputTokens;
@@ -72,8 +73,12 @@ export async function runChargedCompletion(payer, request, signal) {
72
73
  if (!failed)
73
74
  return { text, costUsd, costUnknown };
74
75
  const error = classifyProviderError(msg.errorMessage ?? "Provider error");
75
- if (!error.retryable || attempt > maxRetries)
76
+ if (!error.retryable)
76
77
  throw error;
77
- await env.sleep(providerRetryDelayMs(attempt, msg.errorMessage ?? "", providerCfg), signal);
78
+ const delay = providerRetryDelayMs(attempt, msg.errorMessage ?? "", providerCfg);
79
+ if (!withinRetryLimits(attempt, waitedMs + delay, retryCfg))
80
+ throw error;
81
+ waitedMs += delay;
82
+ await env.sleep(delay, signal);
78
83
  }
79
84
  }
@@ -1,3 +1,4 @@
1
+ import { isRetryableAssistantError } from "@earendil-works/pi-ai";
1
2
  import { NexoError } from "../../../telemetry/src/index.js";
2
3
  /** Maps a provider error message (pi encodes failures as text) to a NexoError code and hint. */
3
4
  export function classifyProviderError(message) {
@@ -36,6 +37,32 @@ export function retryAfterMs(errorMessage) {
36
37
  return match?.[1] ? Math.round(Number(match[1]) * 1000) : undefined;
37
38
  }
38
39
  const RATE_LIMIT_MAX_DELAY_MS = 60_000;
40
+ /** The longest single backoff for a non-rate-limit error before jitter. */
41
+ const RETRY_MAX_DELAY_MS = 60_000;
42
+ /** Backoff time a streak of consecutive provider failures may spend before the call gives up, when maxRetries is unset. */
43
+ export const DEFAULT_RETRY_WINDOW_MS = 30 * 60_000;
44
+ /** A hard cap on retries within the window, so zero-delay configs (tests) cannot loop forever. */
45
+ const MAX_RETRY_ATTEMPTS = 200;
46
+ // Transient failures pi's classifier does not know: Kiro's capacity message and the kiro-p idle stall.
47
+ const EXTRA_TRANSIENT = /high traffic|try again|no output for \d+\s*s/i;
48
+ const NOT_TRANSIENT = /quota|billing|usage limit/i;
49
+ /** Whether a failed model call is worth retrying: pi's transient patterns plus the kiro-cli ones above. */
50
+ export function isRetryableProviderMessage(message) {
51
+ if (message.stopReason !== "error" || !message.errorMessage)
52
+ return false;
53
+ if (isRetryableAssistantError(message))
54
+ return true;
55
+ return EXTRA_TRANSIENT.test(message.errorMessage) && !NOT_TRANSIENT.test(message.errorMessage);
56
+ }
57
+ /**
58
+ * Whether retry `attempt` (1-based, per failure streak) may run after `waitedMs` of backoff including its own delay.
59
+ * An explicit `maxRetries` is a plain count; otherwise retries continue until `retryWindowMs` (default 30 min) of backoff.
60
+ */
61
+ export function withinRetryLimits(attempt, waitedMs, cfg) {
62
+ if (cfg.maxRetries !== undefined)
63
+ return attempt <= cfg.maxRetries;
64
+ return attempt <= MAX_RETRY_ATTEMPTS && waitedMs <= (cfg.retryWindowMs ?? DEFAULT_RETRY_WINDOW_MS);
65
+ }
39
66
  /**
40
67
  * Backoff before retry `attempt` (1-based, counted per streak of consecutive failures) of a retryable provider error.
41
68
  * A rate limit waits `rateLimitBaseDelayMs` (default 5 s) doubling per attempt up to a minute, ±20% so parallel callers
@@ -48,9 +75,9 @@ export function providerRetryDelayMs(attempt, errorMessage, cfg, random = Math.r
48
75
  const base = Math.min(RATE_LIMIT_MAX_DELAY_MS, (cfg.rateLimitBaseDelayMs ?? 5000) * 2 ** (attempt - 1));
49
76
  return Math.max(Math.round(base * (0.8 + 0.4 * random())), retryAfterMs(errorMessage) ?? 0);
50
77
  }
51
- /** Backoff before retry `attempt` (1-based) of a retryable provider error: full jitter on an exponential base, at least the provider's retry-after. */
78
+ /** Backoff before retry `attempt` (1-based) of a retryable provider error: full jitter on an exponential base capped at a minute, at least the provider's retry-after. */
52
79
  export function retryDelayMs(attempt, baseDelayMs, errorMessage, random = Math.random) {
53
- return Math.max(random() * baseDelayMs * 2 ** (attempt - 1), retryAfterMs(errorMessage) ?? 0);
80
+ return Math.max(random() * Math.min(RETRY_MAX_DELAY_MS, baseDelayMs * 2 ** (attempt - 1)), retryAfterMs(errorMessage) ?? 0);
54
81
  }
55
82
  /** The text the model sees for a failed tool call: code, retryability, message and hint. */
56
83
  export function toolErrorText(error) {
@@ -17,7 +17,7 @@ export const DEFAULT_PLANNER_PROMPT = `Parallel work:
17
17
  export const WEB_SEARCH_PROMPT = `Web research: your training data has a cutoff, so use web_search for anything that may have changed or that you are not sure of: current versions, APIs and docs, error messages, prices, news and other recent facts. Search with specific terms, read the best one to three hits with web_fetch, and cite the URLs you relied on in your answer. Do not search for things the workspace or a skill already answers.`;
18
18
  /** The line that points the agent at the wikis directory; the wiki skill holds the rules for using it. */
19
19
  export function wikiPrompt(wikisDir) {
20
- return `Wikis: the user keeps persistent knowledge bases as markdown folders under ${wikisDir}. When a task produces durable subject knowledge worth keeping (research findings, a source you read, a topic the user keeps returning to), load the wiki skill with load_skill and file it in the wiki that fits, creating a new wiki when none fits. Do not create or edit wikis for one-off questions, small talk or routine code changes. Load the wiki skill first whenever the task is about a wiki.`;
20
+ return `Wikis: the user keeps persistent knowledge bases as folders of HTML pages under ${wikisDir}, one level under their area (${wikisDir}/<area>/<folder>/). When a task produces durable subject knowledge worth keeping (research findings, a source you read, a topic the user keeps returning to), load the wiki skill with load_skill and file it in the wiki that fits, creating a new wiki when none fits. Do not create or edit wikis for one-off questions, small talk or routine code changes. Load the wiki skill first whenever the task is about a wiki. The user's approved life areas are listed in ${wikisDir}/areas.json; file work in a wiki that belongs to the matching area, and never add or rename an area yourself, because new areas need the user's approval.`;
21
21
  }
22
22
  export function renderSystemPrompt(parts) {
23
23
  const sections = [parts.systemPrompt.replaceAll("{{workspace}}", parts.workspace)];
@@ -1,5 +1,5 @@
1
1
  import { Agent } from "@earendil-works/pi-agent-core";
2
- import { isContextOverflow, isRetryableAssistantError, normalizeContext, } from "@earendil-works/pi-ai";
2
+ import { isContextOverflow, normalizeContext, } from "@earendil-works/pi-ai";
3
3
  import { createModelRegistry, DEFAULT_ROUTER_POLICY, ESCALATION_BLOCK_REASONS, endOfRunSignals, escalationBlock, isModelAllowed, resolveModel, TriggerMonitor, } from "../../../router/src/index.js";
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";
@@ -12,7 +12,7 @@ import { budgetExhaustedError, checkAdmission, recordSpend, runBudgetLimit } fro
12
12
  import { budgetLine, RunBudget } from "../cost/budget.js";
13
13
  import { estimateCallCostUsd, priceUsage } from "../cost/pricing.js";
14
14
  import { clearOldToolResults } from "./context-trim.js";
15
- import { classifyProviderError, contextOverflowError, providerRetryDelayMs, toolErrorText, withToolErrorCodes } from "./errors.js";
15
+ import { classifyProviderError, contextOverflowError, isRetryableProviderMessage, providerRetryDelayMs, toolErrorText, withToolErrorCodes, withinRetryLimits } from "./errors.js";
16
16
  import { assistantText, contentText, estimatePromptChars, foreignToolCallsAsText, lastAssistant, recordedContent, withBudgetLine } from "./messages.js";
17
17
  import { DEFAULT_PLANNER_PROMPT, DEFAULT_SYSTEM_PROMPT, renderSystemPrompt } from "./prompts.js";
18
18
  import { loadProjectInstructions } from "../session/project-instructions.js";
@@ -538,7 +538,7 @@ export async function executeRun(env, input) {
538
538
  bashTimeoutMs: config.tools.bashTimeoutMs,
539
539
  nexoHome: deps.paths.home,
540
540
  ...(sharedWorkspace ? {} : { runWorkspace: runDir.workspaceDir }),
541
- ...(rollout ? {} : { wikiRoots: [deps.paths.wikis] }),
541
+ ...(rollout ? {} : { wikiRoots: [deps.paths.homeRoot] }),
542
542
  toolOutputsDir: runDir.toolOutputsDir,
543
543
  // Large-output pointers in a session's history name files saved by earlier turns' runs.
544
544
  earlierToolOutputsDirs: sessionId
@@ -640,7 +640,7 @@ export async function executeRun(env, input) {
640
640
  plannerPrompt: tools.some((t) => t.name === "spawn") ? plannerPromptText : null,
641
641
  webSearch: tools.some((t) => t.name === "web_search"),
642
642
  workspace,
643
- ...(rollout ? {} : { wikisDir: deps.paths.wikis }),
643
+ ...(rollout ? {} : { wikisDir: deps.paths.homeRoot }),
644
644
  skillIndex: formatSkillIndex(deps.skills),
645
645
  ...(rollout ? {} : { environment: environmentBlock({ ...(request.projectDir ? { projectDir: request.projectDir } : {}), now: new Date(), workspaceShared: sharedWorkspace !== null, ...(process.env.SHELL ? { shell: process.env.SHELL } : {}) }) }),
646
646
  ...(request.projectDir && !rollout ? { projectInstructions: loadProjectInstructions(request.projectDir, config.instructions).block } : {}),
@@ -992,20 +992,25 @@ export async function executeRun(env, input) {
992
992
  await runAgent.prompt(text);
993
993
  // Retries are counted per streak of consecutive failures: a successful call since the last retry starts over.
994
994
  let attempt = 0;
995
+ let waitedMs = 0;
995
996
  let succeededCalls = completedCalls;
996
997
  for (;;) {
997
998
  const last = lastAssistant(runAgent.state.messages);
998
- if (!last || last.stopReason !== "error" || isContextOverflow(last) || !isRetryableAssistantError(last))
999
+ if (!last || last.stopReason !== "error" || isContextOverflow(last) || !isRetryableProviderMessage(last))
999
1000
  break;
1000
1001
  if (completedCalls !== succeededCalls) {
1001
1002
  attempt = 0;
1003
+ waitedMs = 0;
1002
1004
  succeededCalls = completedCalls;
1003
1005
  }
1004
1006
  attempt++;
1005
1007
  const providerCfg = config.providers[active.model.provider] ?? {};
1006
- if (attempt > (providerCfg.maxRetries ?? 3) || input.signal?.aborted || budgetExceeded)
1008
+ if (input.signal?.aborted || budgetExceeded)
1007
1009
  break;
1008
1010
  const delay = providerRetryDelayMs(attempt, last.errorMessage ?? "", providerCfg);
1011
+ if (!withinRetryLimits(attempt, waitedMs + delay, providerCfg))
1012
+ break;
1013
+ waitedMs += delay;
1009
1014
  logger.warn("retrying provider error", { attempt, delay_ms: Math.round(delay), error_message: last.errorMessage });
1010
1015
  await env.sleep(delay, input.signal);
1011
1016
  runAgent.state.messages = runAgent.state.messages.slice(0, -1);
package/core/src/index.js CHANGED
@@ -34,3 +34,4 @@ export * from "./settings/credentials.js";
34
34
  export * from "./settings/doctor.js";
35
35
  export * from "./settings/settings.js";
36
36
  import "./engine/spawn-tool.js";
37
+ export * from "./learn/areas.js";