@hexdecimal16/nexo 0.4.8 → 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.
package/cli/src/areas.js CHANGED
@@ -10,7 +10,7 @@ export function areasCommand(args, io, options) {
10
10
  throw usageError(AREAS_USAGE);
11
11
  const paths = resolveNexoPaths(options.env ?? process.env);
12
12
  ensureNexoDirs(paths);
13
- const registry = readRegistry(paths.wikis);
13
+ const registry = readRegistry(paths.homeRoot);
14
14
  if (sub === "list") {
15
15
  if (registry.areas.length === 0) {
16
16
  io.stdout.write("No areas yet.\n");
@@ -34,7 +34,7 @@ export function areasCommand(args, io, options) {
34
34
  if ((sub !== "approve" && sub !== "reject") || !id)
35
35
  throw usageError(AREAS_USAGE);
36
36
  if (sub === "approve") {
37
- writeRegistry(paths.wikis, approveArea(registry, id));
37
+ writeRegistry(paths.homeRoot, approveArea(registry, id));
38
38
  io.stdout.write(`Approved area ${id}\n`);
39
39
  return 0;
40
40
  }
@@ -43,7 +43,7 @@ export function areasCommand(args, io, options) {
43
43
  throw new NexoError({ code: "NOT_FOUND", message: `No area "${id}"`, context: { id } });
44
44
  if (area.status !== "proposed")
45
45
  throw new NexoError({ code: "CONFLICT", message: `Area "${id}" is active; only proposed areas can be rejected`, context: { id } });
46
- writeRegistry(paths.wikis, { ...registry, areas: registry.areas.filter((a) => a.id !== id) });
46
+ writeRegistry(paths.homeRoot, { ...registry, areas: registry.areas.filter((a) => a.id !== id) });
47
47
  io.stdout.write(`Rejected area ${id}\n`);
48
48
  return 0;
49
49
  }
@@ -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,4 @@
1
+ import { homeCommand } from "./home-migrate.js";
1
2
  import { areasCommand } from "./areas.js";
2
3
  import { parseArgs } from "node:util";
3
4
  import { judgeFamilyWarnings } from "../../core/src/index.js";
@@ -258,6 +259,8 @@ export async function runCli(argv, io, options = {}) {
258
259
  return await initCommand(rest, io, options, stdio);
259
260
  case "update":
260
261
  return await updateCommand(io, options);
262
+ case "home":
263
+ return homeCommand(rest, io, options);
261
264
  case "areas":
262
265
  return areasCommand(rest, io, options);
263
266
  case "sessions":
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
  }
@@ -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 folders of HTML pages 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. 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.`;
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)];
@@ -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 } : {}),
@@ -1,7 +1,8 @@
1
1
  import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
2
- import { join } from "node:path";
2
+ import { isAbsolute, join, relative, sep } from "node:path";
3
3
  import { dataBlock } from "../../../scorers/src/index.js";
4
4
  import { NexoError } from "../../../telemetry/src/index.js";
5
+ import { listHomeFolders } from "../../../tools/src/index.js";
5
6
  import { Type } from "typebox";
6
7
  import { structuredLearnCall } from "./caller.js";
7
8
  /** Life areas live in the wikis directory as data. Code never names an area; the user approves every new one. */
@@ -94,13 +95,24 @@ export function askUserAnswers(spans) {
94
95
  })
95
96
  .slice(-5);
96
97
  }
97
- function listWikis(wikisDir) {
98
- try {
99
- return readdirSync(wikisDir).filter((f) => !f.startsWith(".") && statSync(join(wikisDir, f)).isDirectory());
100
- }
101
- catch {
102
- return [];
98
+ /** HTML wiki pages (relative to the wikis dir) that the run's write and edit tool calls changed. */
99
+ export function wikiPagesTouched(spans, wikisDir) {
100
+ const out = [];
101
+ for (const s of spans) {
102
+ if (s.kind !== "tool_call" || s.status !== "ok" || (s.name !== "write" && s.name !== "edit"))
103
+ continue;
104
+ const path = (s.attrs ?? {}).args?.path;
105
+ if (typeof path !== "string" || !isAbsolute(path))
106
+ continue;
107
+ const rel = relative(wikisDir, path);
108
+ if (!rel || rel.startsWith("..") || isAbsolute(rel) || !rel.endsWith(".html"))
109
+ continue;
110
+ out.push(rel.split(sep).join("/"));
103
111
  }
112
+ return out;
113
+ }
114
+ function listWikis(wikisDir) {
115
+ return listHomeFolders(wikisDir).map((f) => f.name);
104
116
  }
105
117
  export const AssignmentSchema = Type.Object({
106
118
  areaId: Type.String({ minLength: 1, maxLength: 64 }),
@@ -131,7 +143,7 @@ export function assignPrompt(input) {
131
143
  /** After a scored run: files it into an approved area (or the unsorted queue), appends a feed line and, for verified runs, the user's decisions. Does nothing until an area is active. */
132
144
  export async function assignRun(deps, input) {
133
145
  const none = { areaId: null, feed: false, decisions: 0, proposed: null, verified: false };
134
- const wikisDir = deps.env.deps.paths?.wikis;
146
+ const wikisDir = deps.env.deps.paths?.homeRoot;
135
147
  if (!wikisDir || !existsSync(wikisDir))
136
148
  return none;
137
149
  if (!input.output || input.output.trim().length < 50)
@@ -162,7 +174,8 @@ export async function assignRun(deps, input) {
162
174
  }
163
175
  }
164
176
  const areaId = known?.id ?? null;
165
- const base = { at: now, runId: input.run.id, sessionId: input.run.sessionId ?? null, areaId, wiki };
177
+ const pages = [...new Set([...wikiPagesTouched(input.spans, wikisDir), ...(input.extraPages ?? [])])].filter((x) => x.endsWith(".html")).slice(0, 5);
178
+ const base = { at: now, runId: input.run.id, sessionId: input.run.sessionId ?? null, areaId, wiki, pages };
166
179
  let fed = false;
167
180
  if (reply.feed.trim()) {
168
181
  appendFileSync(join(wikisDir, FEED_FILE), `${JSON.stringify({ ...base, text: redact(reply.feed.trim()), verified })}\n`);
@@ -90,9 +90,9 @@ export async function runLearnJob(env, job, options) {
90
90
  output = "";
91
91
  }
92
92
  const feedback = latestFeedback(db, run.id);
93
- const assignAreas = async () => {
93
+ const assignAreas = async (extraPages = []) => {
94
94
  try {
95
- await assignRun(deps, { run, output, spans, scores, cwd: run.sessionId ? getSessionRow(db, run.sessionId)?.cwd : undefined });
95
+ await assignRun(deps, { run, output, spans, scores, extraPages, cwd: run.sessionId ? getSessionRow(db, run.sessionId)?.cwd : undefined });
96
96
  }
97
97
  catch (err) {
98
98
  if (options.signal.aborted)
@@ -194,13 +194,14 @@ export async function runLearnJob(env, job, options) {
194
194
  throw error;
195
195
  }
196
196
  recorder.finish(writeSpan, { status: "ok", attrs: { result: curation } });
197
+ let curated = [];
197
198
  try {
198
- await curateWikiKnowledge(deps, run, output, spans);
199
+ curated = (await curateWikiKnowledge(deps, run, output, spans)).pages;
199
200
  }
200
201
  catch (err) {
201
202
  logger.warn("wiki curation pass failed; continuing without it", { run_id: run.id, error: err });
202
203
  }
203
- await assignAreas();
204
+ await assignAreas(curated);
204
205
  return { lessons: reflection.lessons.length, curation, costUsd, costUnknown };
205
206
  }
206
207
  catch (error) {
@@ -1,7 +1,7 @@
1
1
  import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
2
- import { dirname, join } from "node:path";
2
+ import { dirname, join, relative, sep } from "node:path";
3
3
  import { dataBlock } from "../../../scorers/src/index.js";
4
- import { resolveConfined, wrapWikiHtml } from "../../../tools/src/index.js";
4
+ import { findHomeFolder, listHomeFolders, resolveConfined, UNSORTED_AREA, wrapWikiHtml } from "../../../tools/src/index.js";
5
5
  import { Type } from "typebox";
6
6
  import { structuredLearnCall } from "./caller.js";
7
7
  function titleFromPath(path) {
@@ -42,18 +42,18 @@ export function wikiCuratorPrompt(input) {
42
42
  * Curates durable subject knowledge into persistent markdown wikis after a run.
43
43
  */
44
44
  export async function curateWikiKnowledge(deps, run, output, spans) {
45
- const wikisDir = deps.env.deps.paths?.wikis;
45
+ const wikisDir = deps.env.deps.paths?.homeRoot;
46
46
  if (!wikisDir)
47
- return { pagesCreated: 0, wikisTouched: [] };
47
+ return { pagesCreated: 0, wikisTouched: [], pages: [] };
48
48
  if (!existsSync(wikisDir))
49
49
  mkdirSync(wikisDir, { recursive: true });
50
50
  // Skip if the run itself explicitly loaded the wiki skill (it managed its own wiki edits)
51
51
  const usedWikiSkill = spans.some((s) => s.kind === "skill_load" && s.status === "ok" && s.attrs.args?.name === "wiki");
52
52
  if (usedWikiSkill)
53
- return { pagesCreated: 0, wikisTouched: [] };
53
+ return { pagesCreated: 0, wikisTouched: [], pages: [] };
54
54
  // Only evaluate runs that have non-trivial instruction/output
55
55
  if (!output || output.trim().length < 50)
56
- return { pagesCreated: 0, wikisTouched: [] };
56
+ return { pagesCreated: 0, wikisTouched: [], pages: [] };
57
57
  let existingWikis = [];
58
58
  try {
59
59
  existingWikis = readdirSync(wikisDir).filter((f) => {
@@ -70,6 +70,7 @@ export async function curateWikiKnowledge(deps, run, output, spans) {
70
70
  catch {
71
71
  existingWikis = [];
72
72
  }
73
+ existingWikis = listHomeFolders(wikisDir).map((f) => f.name);
73
74
  const prompt = wikiCuratorPrompt({
74
75
  instruction: run.instruction,
75
76
  output,
@@ -81,13 +82,14 @@ export async function curateWikiKnowledge(deps, run, output, spans) {
81
82
  tier: "curator",
82
83
  });
83
84
  if (curation.pages.length === 0)
84
- return { pagesCreated: 0, wikisTouched: [] };
85
+ return { pagesCreated: 0, wikisTouched: [], pages: [] };
85
86
  let pagesCreated = 0;
87
+ const createdPages = [];
86
88
  const wikisTouched = new Set();
87
89
  const today = deps.env.now().toISOString().slice(0, 10);
88
90
  for (const p of curation.pages) {
89
91
  try {
90
- const wikiDir = resolveConfined(wikisDir, p.wikiName);
92
+ const wikiDir = findHomeFolder(wikisDir, p.wikiName)?.dir ?? resolveConfined(wikisDir, `${UNSORTED_AREA}/${p.wikiName}`);
91
93
  if (!existsSync(wikiDir)) {
92
94
  mkdirSync(wikiDir, { recursive: true });
93
95
  mkdirSync(join(wikiDir, "concepts"), { recursive: true });
@@ -106,6 +108,7 @@ export async function curateWikiKnowledge(deps, run, output, spans) {
106
108
  const pageHtml = wrapWikiHtml({ title: titleFromPath(pagePath), summary: p.summary, updated: today, sources: [`run:${run.id}`], body: p.content });
107
109
  writeFileSync(targetFile, pageHtml);
108
110
  pagesCreated++;
111
+ createdPages.push(relative(wikisDir, targetFile).split(sep).join("/"));
109
112
  wikisTouched.add(p.wikiName);
110
113
  const logFile = join(wikiDir, "log.md");
111
114
  if (existsSync(logFile)) {
@@ -117,5 +120,5 @@ export async function curateWikiKnowledge(deps, run, output, spans) {
117
120
  deps.env.deps.logger.warn("failed to write curated wiki page", { wiki: p.wikiName, path: p.path, error: err });
118
121
  }
119
122
  }
120
- return { pagesCreated, wikisTouched: [...wikisTouched] };
123
+ return { pagesCreated, wikisTouched: [...wikisTouched], pages: createdPages };
121
124
  }
@@ -244,7 +244,7 @@ export async function startOptimization(baseEnv, options) {
244
244
  ctx.reserveUsd = cases.holdout.length * Math.min(config.budgets.perRunUsd, 2 * dearest);
245
245
  ctx.phase = "loop";
246
246
  options.onEvent?.({ type: "phase", phase: "loop" });
247
- const wikisDir = env.deps.paths?.wikis;
247
+ const wikisDir = env.deps.paths?.homeRoot;
248
248
  const seededPad = wikisDir ? readScratchpad(wikisDir, target.key) : undefined;
249
249
  const result = await runGepa({
250
250
  adapter,
@@ -1,7 +1,11 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
3
  import { emptyScratchpad, SCRATCHPAD_ENTRIES, scratchpadEntry } from "../../../optimize/src/index.js";
4
- import { resolveConfined } from "../../../tools/src/index.js";
4
+ import { findHomeFolder, resolveConfined, UNSORTED_AREA } from "../../../tools/src/index.js";
5
+ /** The scratchpad wiki folder: wherever it already lives under an area, else <root>/unsorted/optimization. */
6
+ function scratchpadDir(root) {
7
+ return findHomeFolder(root, SCRATCHPAD_WIKI)?.dir ?? resolveConfined(root, `${UNSORTED_AREA}/${SCRATCHPAD_WIKI}`);
8
+ }
5
9
  /** The wiki that holds what optimizations learned: one page per target, readable and editable in the Wikis UI. */
6
10
  export const SCRATCHPAD_WIKI = "optimization";
7
11
  /** The page of `targetKey` inside the wiki, e.g. `analyses/optim-skill-web-research.md`. */
@@ -28,7 +32,7 @@ export function parseScratchpad(markdown) {
28
32
  /** The saved scratchpad of a target, or undefined when there is no page, it has no entries or it cannot be read. */
29
33
  export function readScratchpad(wikisDir, targetKey) {
30
34
  try {
31
- const file = resolveConfined(resolveConfined(wikisDir, SCRATCHPAD_WIKI), scratchpadPagePath(targetKey));
35
+ const file = resolveConfined(scratchpadDir(wikisDir), scratchpadPagePath(targetKey));
32
36
  if (!existsSync(file))
33
37
  return undefined;
34
38
  const pad = parseScratchpad(readFileSync(file, "utf8"));
@@ -40,7 +44,7 @@ export function readScratchpad(wikisDir, targetKey) {
40
44
  }
41
45
  /** Writes the target's scratchpad page (creating the wiki on first use) and logs it; returns the page file. */
42
46
  export function writeScratchpad(wikisDir, targetKey, pad, info) {
43
- const wikiDir = resolveConfined(wikisDir, SCRATCHPAD_WIKI);
47
+ const wikiDir = scratchpadDir(wikisDir);
44
48
  const { today } = info;
45
49
  if (!existsSync(wikiDir)) {
46
50
  for (const d of ["concepts", "entities", "sources", "analyses", "raw"])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hexdecimal16/nexo",
3
- "version": "0.4.8",
3
+ "version": "0.4.9",
4
4
  "description": "nexo agent engine",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "repository": {
@@ -9,9 +9,9 @@ metadata:
9
9
 
10
10
  A wiki is a folder of interlinked HTML pages that you build and keep current for the user. Knowledge is compiled once, when a source arrives, instead of being rediscovered on every question. You write and maintain the pages. The user brings sources and asks questions.
11
11
 
12
- The wikis directory is named in your system prompt. Each wiki is one folder in it, named for its topic in lowercase with hyphens: `<wikis>/<name>/`. Reach it with absolute paths in `read`, `write`, `edit` and `bash`. Never put wiki pages in the run workspace and never touch other nexo files.
12
+ The wikis directory is named in your system prompt. Each wiki is one folder one level under its area, named for its topic in lowercase with hyphens: `<wikis>/<area>/<name>/` (for example `<wikis>/builds/nexo/`). Reach it with absolute paths in `read`, `write`, `edit` and `bash`. Never put wiki pages in the run workspace and never touch other nexo files.
13
13
 
14
- If the user does not name a wiki, list the wikis directory and pick the one that fits the topic. If none fits and the task produced durable subject knowledge worth keeping, create a new wiki without asking and tell the user its name and why. Do not create a wiki for a one-off question or for knowledge about how you should behave (that belongs to memories and playbooks).
14
+ If the user does not name a wiki, list the area folders (`ls <wikis>/*/`) and pick the wiki that fits the topic. If none fits and the task produced durable subject knowledge worth keeping, create a new wiki without asking and tell the user its name and why. Do not create a wiki for a one-off question or for knowledge about how you should behave (that belongs to memories and playbooks).
15
15
 
16
16
  The wiki holds knowledge about a subject. Lessons about how you should behave next time belong to nexo's memories and playbooks, which you do not edit. Do not copy the same fact into both.
17
17
 
@@ -34,7 +34,7 @@ The wiki holds knowledge about a subject. Lessons about how you should behave ne
34
34
 
35
35
  `<wikis>/areas.json` lists the user's life areas (for example work, builds, research). Each area has a scope note, aliases and the wikis it holds. The user approves every area.
36
36
 
37
- - Before creating a wiki, read `areas.json` and pick the area whose scope fits. Put the new wiki's name in that area's `wikis` list.
37
+ - Before creating a wiki, read `areas.json` and pick the area whose scope fits. Create the wiki at `<wikis>/<area>/<name>/` and put its name in that area's `wikis` list. If no area fits, use `<wikis>/unsorted/<name>/`.
38
38
  - Never add, rename, merge or approve an area. If nothing fits, say so in your answer; the post-run step queues it and proposes a new area once three tasks need it.
39
39
  - Do not write `feed.jsonl` or `decisions.jsonl`; the post-run step writes them after the run is scored.
40
40
 
@@ -13,11 +13,11 @@ export function resolveNexoPaths(env = process.env) {
13
13
  skills: join(home, "skills"),
14
14
  config: join(home, "config.json"),
15
15
  credentials: join(home, "credentials.json"),
16
- wikis: join(home, "wikis"),
16
+ homeRoot: join(home, "home"),
17
17
  promptHistory: join(home, "history.jsonl"),
18
18
  };
19
19
  }
20
20
  export function ensureNexoDirs(paths) {
21
- for (const dir of [paths.home, paths.runs, paths.sessions, paths.optim, paths.logs, paths.skills, paths.wikis])
21
+ for (const dir of [paths.home, paths.runs, paths.sessions, paths.optim, paths.logs, paths.skills, paths.homeRoot])
22
22
  mkdirSync(dir, { recursive: true });
23
23
  }
@@ -0,0 +1,36 @@
1
+ import { existsSync, readdirSync, statSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ /** Where a new folder goes before it has an area. */
4
+ export const UNSORTED_AREA = "unsorted";
5
+ function isDir(path) {
6
+ try {
7
+ return statSync(path).isDirectory();
8
+ }
9
+ catch {
10
+ return false;
11
+ }
12
+ }
13
+ /** Every <area>/<name> folder under the home root, sorted by area then name. */
14
+ export function listHomeFolders(root) {
15
+ if (!existsSync(root))
16
+ return [];
17
+ const out = [];
18
+ for (const area of readdirSync(root).sort()) {
19
+ if (area.startsWith("."))
20
+ continue;
21
+ const areaDir = join(root, area);
22
+ if (!isDir(areaDir))
23
+ continue;
24
+ for (const name of readdirSync(areaDir).sort()) {
25
+ if (name.startsWith("."))
26
+ continue;
27
+ const dir = join(areaDir, name);
28
+ if (isDir(dir))
29
+ out.push({ area, name, dir });
30
+ }
31
+ }
32
+ return out;
33
+ }
34
+ export function findHomeFolder(root, name) {
35
+ return listHomeFolders(root).find((f) => f.name === name);
36
+ }
@@ -9,3 +9,4 @@ export * from "./read-only.js";
9
9
  export * from "./session-rules.js";
10
10
  export * from "./web-search.js";
11
11
  export * from "./wiki-page.js";
12
+ export * from "./home-folders.js";
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html" aria-current="page">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html" aria-current="page">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <p class="muted"><a href="/home.html">&larr; Home</a></p>
18
18
  <h1 id="area-title">Area</h1>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Cost</h1>
18
18
  <form id="cost-form" class="filters">
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html" aria-current="page">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html" aria-current="page">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Home</h1>
18
18
  <div id="error"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Runs</h1>
18
18
  <div id="session-filter"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Judge calibration</h1>
18
18
  <div id="error"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Learning</h1>
18
18
  <div id="error"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Optimization</h1>
18
18
  <div id="error"></div>
@@ -0,0 +1,24 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="nexo-token" content="{{NEXO_TOKEN}}">
7
+ <title>nexo · page</title>
8
+ <link rel="stylesheet" href="/style.css">
9
+ <script type="module" src="/lib/theme.ts"></script>
10
+ <script type="module" src="/pages/companion.ts"></script>
11
+ <script type="module" src="/pages/page.ts"></script>
12
+ </head>
13
+ <body>
14
+ <a class="skip" href="#main">Skip to content</a>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
+ <main id="main" tabindex="-1">
17
+ <p class="muted" id="page-crumbs"><a href="/home.html">Home</a></p>
18
+ <h1 id="page-title">Page</h1>
19
+ <p class="muted" id="page-meta"></p>
20
+ <div id="error"></div>
21
+ <div id="page-body" class="wiki-prose wiki-html"></div>
22
+ </main>
23
+ </body>
24
+ </html>
@@ -3,9 +3,9 @@ import { formatTime } from "../lib/format.ts";
3
3
  import { html, type SafeHtml } from "../lib/html.ts";
4
4
  import { asErrorBody, el, getJson } from "./api.ts";
5
5
 
6
- interface HomeWiki { name: string; summary?: string; pageCount: number }
6
+ interface HomeWiki { name: string; summary?: string; pageCount: number; path: string }
7
7
  interface HomeArea { id: string; name: string; scope: string; wikis: HomeWiki[]; recent: number }
8
- interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean }
8
+ interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean; pages?: string[] }
9
9
  interface HomeDecision { at: string; choice: string; reason: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string }
10
10
  interface HomeSession { id: string; title: string; cwd: string; updatedAt: string; areaId: string | null }
11
11
  interface Home { areas: HomeArea[]; feed: HomeFeedItem[]; decisions: HomeDecision[]; sessions: HomeSession[] }
@@ -14,8 +14,13 @@ const box = el<HTMLDivElement>("#area");
14
14
  const title = el<HTMLHeadingElement>("#area-title");
15
15
  const errorBox = el<HTMLDivElement>("#error");
16
16
 
17
- function list(items: SafeHtml[], empty: string): SafeHtml {
18
- return items.length > 0 ? html`<ul class="home-list">${items}</ul>` : html`<p class="muted">${empty}</p>`;
17
+ const empty = (text: string): SafeHtml => html`<p class="muted">${text}</p>`;
18
+ const stat = (n: number, label: string): SafeHtml => html`<div class="area-stat"><b>${String(n)}</b><span class="muted">${label}</span></div>`;
19
+
20
+ function pageLinks(f: { pages?: string[]; wiki: string | null; areaId: string | null }): SafeHtml {
21
+ const pages = (f.pages ?? []).filter((p) => p.endsWith(".html") && p.includes("/") && !p.includes(".."));
22
+ if (pages.length > 0) return html`${pages.map((p) => html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(p)}">${p.slice(p.lastIndexOf("/") + 1, -5)}</a> `)}`;
23
+ return f.wiki && f.areaId ? html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(`${f.areaId}/${f.wiki}/index.html`)}">${f.wiki}</a>` : html``;
19
24
  }
20
25
 
21
26
  function render(d: Home, id: string): void {
@@ -27,18 +32,23 @@ function render(d: Home, id: string): void {
27
32
  }
28
33
  title.textContent = a.name;
29
34
  document.title = `nexo · ${a.name}`;
30
- const wikis = a.wikis.map((w) => html`<li><a href="/wikis.html#${encodeURIComponent(w.name)}/index">${w.name}</a> <span class="muted">${String(w.pageCount)} pages${w.summary ? ` · ${w.summary}` : ""}</span></li>`);
31
- const feed = d.feed.filter((f) => f.areaId === a.id).slice(0, 30).map((f) => {
35
+ const feed = d.feed.filter((f) => f.areaId === a.id).slice(0, 30);
36
+ const decisions = d.decisions.filter((x) => x.areaId === a.id);
37
+ const sessions = d.sessions.filter((s) => s.areaId === a.id);
38
+ const pages = a.wikis.reduce((n, w) => n + w.pageCount, 0);
39
+ const wikis = a.wikis.map((w) => html`<a class="area-card" href="/page.html?path=${encodeURIComponent(`${w.path}/index.html`)}"><strong>${w.name}</strong><span class="muted">${String(w.pageCount)} pages</span>${w.summary ? html`<span class="area-card-text">${w.summary}</span>` : ""}</a>`);
40
+ const timeline = feed.map((f) => {
32
41
  const link = f.sessionId ? html`<a href="/?session=${encodeURIComponent(f.sessionId)}">session</a>` : html`<a href="/run.html?id=${encodeURIComponent(f.runId)}">run</a>`;
33
- return html`<li><span class="muted mono">${formatTime(f.at)}</span> ${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`}${f.wiki ? html` <span class="muted">· ${f.wiki}</span>` : ""} ${link}</li>`;
42
+ return html`<li><time>${formatTime(f.at)}${f.wiki ? ` · ${f.wiki}` : ""}</time>${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} ${pageLinks(f)} ${link}</li>`;
34
43
  });
35
- const decisions = d.decisions.filter((x) => x.areaId === a.id).map((x) => html`<li><span class="muted mono">${formatTime(x.at)}</span> <strong>${x.choice}</strong>${x.reason ? html` <span class="muted">because ${x.reason}</span>` : ""}</li>`);
36
- const sessions = d.sessions.filter((s) => s.areaId === a.id).map((s) => html`<li><a href="/?session=${encodeURIComponent(s.id)}">${s.title || s.id}</a> <span class="muted">${s.cwd} · ${formatTime(s.updatedAt)}</span></li>`);
44
+ const decisionCards = decisions.map((x) => html`<div class="area-decision"><strong>${x.choice}</strong>${x.reason ? html`<span class="muted">Because ${x.reason}</span>` : ""}<time class="muted">${formatTime(x.at)}</time></div>`);
45
+ const rows = sessions.map((s) => html`<tr><td><a href="/?session=${encodeURIComponent(s.id)}">${s.title || s.id}</a></td><td class="mono muted">${s.cwd}</td><td class="muted">${formatTime(s.updatedAt)}</td></tr>`);
37
46
  box.innerHTML = html`<p class="home-note">${a.scope}</p>
38
- <section><h2>Wikis</h2>${list(wikis, "No wikis in this area yet.")}</section>
39
- <section><h2>Recent</h2>${list(feed, "Nothing filed here yet. Finished tasks appear here after they are scored.")}</section>
40
- <section><h2>Decisions</h2>${list(decisions, "No decisions recorded here yet.")}</section>
41
- <section><h2>Sessions</h2>${list(sessions, "No linked sessions yet.")}</section>`.value;
47
+ <div class="area-stats">${stat(a.wikis.length, "wikis")}${stat(pages, "pages")}${stat(a.recent, "filed this week")}${stat(decisions.length, "decisions")}</div>
48
+ <section><h2>Wikis</h2>${wikis.length > 0 ? html`<div class="area-grid">${wikis}</div>` : empty("No wikis in this area yet.")}</section>
49
+ <section><h2>Recent</h2>${timeline.length > 0 ? html`<ol class="area-timeline">${timeline}</ol>` : empty("Nothing filed here yet. Finished tasks appear here after they are scored.")}</section>
50
+ <section><h2>Decisions</h2>${decisionCards.length > 0 ? decisionCards : empty("No decisions recorded here yet.")}</section>
51
+ <section><h2>Sessions</h2>${rows.length > 0 ? html`<table class="area-table"><thead><tr><th>Session</th><th>Folder</th><th>Updated</th></tr></thead><tbody>${rows}</tbody></table>` : empty("No linked sessions yet.")}</section>`.value;
42
52
  }
43
53
 
44
54
  const id = new URLSearchParams(location.search).get("id") ?? "";
@@ -3,9 +3,9 @@ import { formatTime } from "../lib/format.ts";
3
3
  import { html, type SafeHtml } from "../lib/html.ts";
4
4
  import { asErrorBody, el, getJson } from "./api.ts";
5
5
 
6
- interface HomeWiki { name: string; summary?: string; pageCount: number }
6
+ interface HomeWiki { name: string; summary?: string; pageCount: number; path: string }
7
7
  interface HomeArea { id: string; name: string; scope: string; wikis: HomeWiki[]; recent: number }
8
- interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean }
8
+ interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean; pages?: string[] }
9
9
  interface HomeDecision { at: string; choice: string; reason: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string }
10
10
  interface HomeSession { id: string; title: string; cwd: string; updatedAt: string; areaId: string | null }
11
11
  interface Home { areas: HomeArea[]; proposed: { id: string; name: string }[]; unsorted: number; feed: HomeFeedItem[]; decisions: HomeDecision[]; sessions: HomeSession[] }
@@ -18,9 +18,15 @@ let data: Home | null = null;
18
18
  const hit = (q: string, ...parts: (string | null | undefined)[]) => !q || parts.some((p) => (p ?? "").toLowerCase().includes(q));
19
19
  const areaName = (id: string | null) => data?.areas.find((a) => a.id === id)?.name ?? "unsorted";
20
20
 
21
+ function pageLinks(f: { pages?: string[]; wiki: string | null; areaId: string | null }): SafeHtml {
22
+ const pages = (f.pages ?? []).filter((p) => p.endsWith(".html") && p.includes("/") && !p.includes(".."));
23
+ if (pages.length > 0) return html`${pages.map((p) => html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(p)}">${p.slice(p.lastIndexOf("/") + 1, -5)}</a> `)}`;
24
+ return f.wiki && f.areaId ? html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(`${f.areaId}/${f.wiki}/index.html`)}">${f.wiki}</a>` : html``;
25
+ }
26
+
21
27
  function feedItem(f: HomeFeedItem): SafeHtml {
22
28
  const link = f.sessionId ? html`<a href="/?session=${encodeURIComponent(f.sessionId)}">session</a>` : html`<a href="/run.html?id=${encodeURIComponent(f.runId)}">run</a>`;
23
- return html`<li><span class="muted mono">${formatTime(f.at)}</span> ${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} <span class="muted">· ${areaName(f.areaId)}${f.wiki ? ` · ${f.wiki}` : ""}</span> ${link}</li>`;
29
+ return html`<li><span class="muted mono">${formatTime(f.at)}</span> ${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} <span class="muted">· ${areaName(f.areaId)}${f.wiki ? ` · ${f.wiki}` : ""}</span> ${pageLinks(f)} ${link}</li>`;
24
30
  }
25
31
 
26
32
  function list(items: SafeHtml[], empty: string): SafeHtml {
@@ -0,0 +1,62 @@
1
+ import { renderError } from "../lib/common-view.ts";
2
+ import { formatTime } from "../lib/format.ts";
3
+ import { asErrorBody, el, getJson } from "./api.ts";
4
+
5
+ interface HomePage { path: string; title: string; summary?: string; updated?: string; content: string; modifiedAt: string }
6
+
7
+ const crumbs = el<HTMLParagraphElement>("#page-crumbs");
8
+ const titleEl = el<HTMLHeadingElement>("#page-title");
9
+ const meta = el<HTMLParagraphElement>("#page-meta");
10
+ const body = el<HTMLDivElement>("#page-body");
11
+ const errorBox = el<HTMLDivElement>("#error");
12
+ const DROP = "script,style,iframe,object,embed,form,link,meta,base,title";
13
+
14
+ function link(text: string, href: string): HTMLAnchorElement {
15
+ const a = document.createElement("a");
16
+ a.href = href;
17
+ a.textContent = text;
18
+ return a;
19
+ }
20
+
21
+ /** Shows the page body inline: active content removed, links to other pages kept inside page.html. */
22
+ function mount(page: HomePage): void {
23
+ const doc = new DOMParser().parseFromString(page.content, "text/html");
24
+ const root = doc.querySelector("main") ?? doc.body;
25
+ root.querySelectorAll(DROP).forEach((n) => n.remove());
26
+ root.querySelectorAll(":scope > nav, :scope > p.meta, :scope > h1").forEach((n) => n.remove());
27
+ root.querySelectorAll("*").forEach((node) => {
28
+ for (const attr of Array.from(node.attributes)) {
29
+ const name = attr.name.toLowerCase();
30
+ if (name.startsWith("on") || name === "style") node.removeAttribute(attr.name);
31
+ else if ((name === "href" || name === "src" || name === "xlink:href") && /^\s*(javascript|data):/i.test(attr.value)) node.removeAttribute(attr.name);
32
+ }
33
+ });
34
+ const base = page.path.slice(0, page.path.lastIndexOf("/") + 1);
35
+ root.querySelectorAll<HTMLAnchorElement>("a[href]").forEach((a) => {
36
+ const href = a.getAttribute("href") ?? "";
37
+ if (/^(https?:|mailto:)/i.test(href)) {
38
+ a.target = "_blank";
39
+ a.rel = "noopener noreferrer";
40
+ return;
41
+ }
42
+ if (href.startsWith("#")) return;
43
+ const target = decodeURIComponent(new URL(href, `https://home.invalid/${base}`).pathname.slice(1));
44
+ if (target.endsWith(".html")) a.setAttribute("href", `/page.html?path=${encodeURIComponent(target)}`);
45
+ else a.removeAttribute("href");
46
+ });
47
+ body.replaceChildren(...Array.from(root.childNodes).map((n) => document.importNode(n, true)));
48
+ }
49
+
50
+ const path = new URLSearchParams(location.search).get("path") ?? "";
51
+ const [area = "", folder = ""] = path.split("/");
52
+ crumbs.replaceChildren(link("Home", "/home.html"), " / ", link(area, `/area.html?id=${encodeURIComponent(area)}`), " / ", link(folder, `/page.html?path=${encodeURIComponent(`${area}/${folder}/index.html`)}`));
53
+ getJson<HomePage>(`/api/home/page?path=${encodeURIComponent(path)}`)
54
+ .then((page) => {
55
+ titleEl.textContent = page.title;
56
+ document.title = `nexo · ${page.title}`;
57
+ meta.textContent = `Updated ${page.updated || formatTime(page.modifiedAt)}`;
58
+ mount(page);
59
+ })
60
+ .catch((error) => {
61
+ errorBox.innerHTML = renderError(asErrorBody(error)).value;
62
+ });
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <nav class="run-nav" aria-label="Run sections"><a href="#sec-timeline">Timeline</a><a href="#sec-spans">Spans</a><a href="#sec-scores">Scores</a><a href="#sec-memories">Memories</a><a href="#sec-subagents">Subagents</a><a href="#sec-files">Files</a><a href="#sec-review">Review</a></nav>
18
18
  <div id="error"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Sessions</h1>
18
18
  <p class="muted">Each chat message is a turn. Open a session to see its turns and review them.</p>
@@ -378,3 +378,30 @@ section[id] { scroll-margin-top: 96px; }
378
378
  .home-note { border-left: 4px solid var(--pen); padding: 6px 12px; background: var(--bg); }
379
379
  a.home-area { display: block; color: inherit; text-decoration: none; }
380
380
  a.home-area:hover, a.home-area:focus-visible { border-color: var(--pen); }
381
+
382
+ /* Area page (pages/area.ts) */
383
+ .area-stats { display: flex; flex-wrap: wrap; gap: 12px; margin: 14px 0 6px; }
384
+ .area-stat { border: 1px solid var(--line); border-radius: 6px; padding: 10px 16px; min-width: 120px; }
385
+ .area-stat b { display: block; font-size: 22px; line-height: 28px; }
386
+ .area-stat span { font-size: 12px; }
387
+ .area-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
388
+ a.area-card { display: block; border: 1px solid var(--line); border-radius: 6px; padding: 12px 14px; color: inherit; text-decoration: none; }
389
+ a.area-card:hover, a.area-card:focus-visible { border-color: var(--pen); }
390
+ .area-card .muted { display: block; font-size: 12px; margin-top: 2px; }
391
+ .area-card-text { display: block; font-size: 13px; margin-top: 6px; }
392
+ .area-timeline { list-style: none; margin: 8px 0 8px 6px; padding: 0; border-left: 2px solid var(--line); }
393
+ .area-timeline li { position: relative; padding: 2px 0 14px 18px; }
394
+ .area-timeline li::before { content: ""; position: absolute; left: -7px; top: 6px; width: 12px; height: 12px; border-radius: 50%; background: var(--pen); }
395
+ .area-timeline time { display: block; font-size: 12px; color: var(--muted); }
396
+ .area-decision { border: 1px solid var(--line); border-left: 4px solid var(--ok); border-radius: 4px; padding: 10px 14px; margin: 8px 0; }
397
+ .area-decision span, .area-decision time { display: block; font-size: 13px; margin-top: 2px; }
398
+ .area-table { border-collapse: collapse; width: 100%; font-size: 13px; }
399
+ .area-table th, .area-table td { border-bottom: 1px solid var(--line); padding: 6px 10px; text-align: left; }
400
+
401
+ /* Converted wiki pages: top-level bullets as cards, clearer sections */
402
+ .wiki-html h2 { border-top: 1px solid var(--line); padding-top: 16px; margin-top: 24px; }
403
+ .wiki-html > ul { list-style: none; padding-left: 0; }
404
+ .wiki-html > ul > li { border: 1px solid var(--line); border-left: 3px solid var(--pen); border-radius: 4px; padding: 8px 12px; margin: 8px 0; background: var(--bg); }
405
+ .wiki-html > ul > li > ul { margin-top: 6px; }
406
+ .wiki-html tbody tr:nth-child(even) td { background: var(--bg); }
407
+ .feed-page { font-size: 12px; border: 1px solid var(--line); border-radius: 3px; padding: 0 5px; text-decoration: none; }
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html" aria-current="page">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Wikis</h1>
18
18
  <div id="error"></div>
package/web/src/api.js CHANGED
@@ -574,7 +574,7 @@ function parseSummary(content) {
574
574
  }
575
575
  /** `GET /api/wikis`: lists all wiki directories under the wikis path. */
576
576
  export function listWikisResponse(ctx) {
577
- const dir = ctx.wikisDir ?? resolveNexoPaths().wikis;
577
+ const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
578
578
  if (!existsSync(dir))
579
579
  return { wikis: [] };
580
580
  const wikis = [];
@@ -622,7 +622,7 @@ export function listWikisResponse(ctx) {
622
622
  }
623
623
  /** `GET /api/wikis/{name}/pages`: lists all markdown pages in a wiki with summary metadata. */
624
624
  export function listWikiPagesResponse(ctx, wikiName) {
625
- const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
625
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
626
626
  let wikiDir;
627
627
  try {
628
628
  wikiDir = resolveConfined(root, wikiName);
@@ -676,7 +676,7 @@ export function listWikiPagesResponse(ctx, wikiName) {
676
676
  export function wikiPageResponse(ctx, wikiName, relPath) {
677
677
  if (relPath.includes("\0"))
678
678
  throw invalidRequest("Path contains a NUL byte");
679
- const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
679
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
680
680
  let wikiDir;
681
681
  try {
682
682
  wikiDir = resolveConfined(root, wikiName);
@@ -1,7 +1,8 @@
1
- import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
- import { join } from "node:path";
1
+ import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
2
+ import { join, relative, sep } from "node:path";
3
3
  import { listSessionRows, resolveNexoPaths } from "../../store/src/index.js";
4
- import { parseWikiMeta } from "../../tools/src/index.js";
4
+ import { NexoError } from "../../telemetry/src/index.js";
5
+ import { listHomeFolders, parseWikiMeta, resolveConfined } from "../../tools/src/index.js";
5
6
  const FEED_LIMIT = 50;
6
7
  const DECISION_LIMIT = 50;
7
8
  /** Reads a JSON-lines file newest first; unparseable lines are skipped. */
@@ -46,7 +47,7 @@ function countPages(dir) {
46
47
  }
47
48
  /** `GET /api/home`: approved areas with their wikis, the feed, decisions and recent sessions, read fresh on every call. */
48
49
  export function homeResponse(ctx) {
49
- const dir = ctx.wikisDir ?? resolveNexoPaths().wikis;
50
+ const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
50
51
  let raw = {};
51
52
  try {
52
53
  raw = JSON.parse(readFileSync(join(dir, "areas.json"), "utf8"));
@@ -58,22 +59,23 @@ export function homeResponse(ctx) {
58
59
  const active = all.filter((a) => a.status === "active");
59
60
  const feed = readJsonl(join(dir, "feed.jsonl"), FEED_LIMIT).map((f) => ({ ...f, text: ctx.redact(String(f.text ?? "")) }));
60
61
  const decisions = readJsonl(join(dir, "decisions.jsonl"), DECISION_LIMIT).map((d) => ({ ...d, choice: ctx.redact(String(d.choice ?? "")), reason: ctx.redact(String(d.reason ?? "")) }));
62
+ const folders = new Map(listHomeFolders(dir).map((f) => [f.name, f.dir]));
61
63
  const weekAgo = ctx.now().getTime() - 7 * 86_400_000;
62
64
  const areas = active.map((a) => ({
63
65
  id: a.id,
64
66
  name: a.name,
65
67
  scope: a.scope,
66
68
  wikis: (a.wikis ?? [])
67
- .filter((w) => /^[a-z0-9-]+$/.test(w) && existsSync(join(dir, w)))
69
+ .filter((w) => folders.has(w))
68
70
  .map((w) => {
69
71
  let summary;
70
72
  try {
71
- summary = parseWikiMeta(readFileSync(join(dir, w, "overview.html"), "utf8")).summary;
73
+ summary = parseWikiMeta(readFileSync(join(folders.get(w), "overview.html"), "utf8")).summary;
72
74
  }
73
75
  catch {
74
76
  summary = undefined;
75
77
  }
76
- return { name: w, summary, pageCount: countPages(join(dir, w)) };
78
+ return { name: w, summary, pageCount: countPages(folders.get(w)), path: relative(dir, folders.get(w)).split(sep).join("/") };
77
79
  }),
78
80
  recent: feed.filter((f) => f.areaId === a.id && Date.parse(f.at) >= weekAgo).length,
79
81
  }));
@@ -97,3 +99,58 @@ export function homeResponse(ctx) {
97
99
  sessions,
98
100
  };
99
101
  }
102
+ const SEGMENT = /^[a-z0-9][a-z0-9-]{0,63}$/;
103
+ function confined(root, rel) {
104
+ try {
105
+ return resolveConfined(root, rel);
106
+ }
107
+ catch {
108
+ throw new NexoError({ code: "FORBIDDEN", message: `Path "${rel}" is outside the home folder`, context: { path: rel } });
109
+ }
110
+ }
111
+ function titleOf(content, fallback) {
112
+ const m = /<title>([^<]*)<\/title>/i.exec(content.slice(0, 4096));
113
+ return m?.[1]?.trim() || fallback;
114
+ }
115
+ /** `GET /api/home/folder?area=&name=`: the HTML pages of one folder, paths relative to the home root. */
116
+ export function homeFolderResponse(ctx, area, name) {
117
+ if (!SEGMENT.test(area) || !SEGMENT.test(name))
118
+ throw new NexoError({ code: "INVALID_REQUEST", message: "area and name must be lowercase slugs", context: { area, name } });
119
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
120
+ const dir = confined(root, `${area}/${name}`);
121
+ if (!existsSync(dir) || !statSync(dir).isDirectory())
122
+ throw new NexoError({ code: "NOT_FOUND", message: `No folder ${area}/${name}`, context: { area, name } });
123
+ const realRoot = realpathSync(root);
124
+ const pages = [];
125
+ const walk = (d) => {
126
+ for (const f of readdirSync(d)) {
127
+ if (f.startsWith(".") || f === "raw")
128
+ continue;
129
+ const p = join(d, f);
130
+ const st = statSync(p);
131
+ if (st.isDirectory())
132
+ walk(p);
133
+ else if (f.endsWith(".html")) {
134
+ const content = readFileSync(p, "utf8");
135
+ pages.push({ path: relative(realRoot, p).split(sep).join("/"), title: ctx.redact(titleOf(content, f.slice(0, -5))), summary: parseWikiMeta(content).summary, modifiedAt: st.mtime.toISOString() });
136
+ }
137
+ }
138
+ };
139
+ walk(dir);
140
+ pages.sort((a, b) => a.path.localeCompare(b.path));
141
+ return { area, name, pages };
142
+ }
143
+ /** `GET /api/home/page?path=<area>/<folder>/<page>.html`: one HTML page, redacted. */
144
+ export function homePageResponse(ctx, relPath) {
145
+ const segs = relPath.split("/");
146
+ if (!/^[A-Za-z0-9._/-]+\.html$/.test(relPath) || segs.length < 3 || segs.includes("..") || segs.includes(".")) {
147
+ throw new NexoError({ code: "INVALID_REQUEST", message: "path must be <area>/<folder>/<page>.html", context: { path: relPath } });
148
+ }
149
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
150
+ const file = confined(root, relPath);
151
+ if (!existsSync(file) || !statSync(file).isFile())
152
+ throw new NexoError({ code: "NOT_FOUND", message: `No page ${relPath}`, context: { path: relPath } });
153
+ const content = ctx.redact(readFileSync(file, "utf8"));
154
+ const meta = parseWikiMeta(content);
155
+ return { path: relPath, title: titleOf(content, (segs[segs.length - 1] ?? relPath).replace(/[.]html$/, "")), summary: meta.summary, updated: meta.updated, content, modifiedAt: statSync(file).mtime.toISOString() };
156
+ }
package/web/src/server.js CHANGED
@@ -1,4 +1,4 @@
1
- import { homeResponse } from "./home-api.js";
1
+ import { homeFolderResponse, homePageResponse, homeResponse } from "./home-api.js";
2
2
  import { randomBytes, timingSafeEqual } from "node:crypto";
3
3
  import { createServer } from "node:http";
4
4
  import { fileURLToPath } from "node:url";
@@ -261,6 +261,16 @@ export async function startWebServer(options) {
261
261
  assertWritable();
262
262
  return sendJson(res, 200, reviewEvalCaseResponse(ctx, m[1], m[2]));
263
263
  }
264
+ if (path === "/api/home/folder") {
265
+ if (method !== "GET")
266
+ throw methodNotAllowed(method, path, "GET");
267
+ return sendJson(res, 200, homeFolderResponse(ctx, url.searchParams.get("area") ?? "", url.searchParams.get("name") ?? ""));
268
+ }
269
+ if (path === "/api/home/page") {
270
+ if (method !== "GET")
271
+ throw methodNotAllowed(method, path, "GET");
272
+ return sendJson(res, 200, homePageResponse(ctx, url.searchParams.get("path") ?? ""));
273
+ }
264
274
  if (path === "/api/home") {
265
275
  if (method !== "GET")
266
276
  throw methodNotAllowed(method, path, "GET");