@zenera/cli 1.1.22 → 1.1.24

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 (73) hide show
  1. package/README.md +72 -2
  2. package/dist/archive.d.ts +139 -0
  3. package/dist/archive.js +376 -0
  4. package/dist/ask.d.ts +51 -0
  5. package/dist/ask.js +143 -0
  6. package/dist/batch.d.ts +77 -0
  7. package/dist/batch.js +338 -0
  8. package/dist/command.d.ts +6 -0
  9. package/dist/commands/export.d.ts +3 -0
  10. package/dist/commands/export.js +255 -0
  11. package/dist/commands/import.d.ts +3 -0
  12. package/dist/commands/import.js +173 -0
  13. package/dist/commands/index.d.ts +5 -1
  14. package/dist/commands/index.js +13 -1
  15. package/dist/commands/inspect.d.ts +11 -0
  16. package/dist/commands/inspect.js +388 -89
  17. package/dist/commands/list.js +128 -19
  18. package/dist/commands/memory.js +453 -16
  19. package/dist/commands/models.js +14 -2
  20. package/dist/commands/run.js +139 -34
  21. package/dist/commands/sandbox.js +72 -1
  22. package/dist/engine.d.ts +19 -0
  23. package/dist/engine.js +60 -8
  24. package/dist/liveness.js +6 -3
  25. package/dist/main.js +5 -2
  26. package/dist/podman.d.ts +16 -0
  27. package/dist/podman.js +115 -2
  28. package/dist/progress.d.ts +33 -0
  29. package/dist/progress.js +402 -0
  30. package/dist/request.d.ts +45 -0
  31. package/dist/request.js +300 -0
  32. package/dist/sandbox.js +10 -0
  33. package/dist/scaffold.d.ts +8 -0
  34. package/dist/scaffold.js +4 -1
  35. package/dist/session.d.ts +26 -1
  36. package/dist/session.js +42 -3
  37. package/dist/term.d.ts +3 -0
  38. package/dist/term.js +17 -1
  39. package/dist/trace.d.ts +84 -0
  40. package/dist/trace.js +686 -0
  41. package/dist/validate.js +93 -4
  42. package/package.json +12 -6
  43. package/templates/editor/.github/copilot-instructions.md +11 -6
  44. package/templates/editor/.github/prompts/finetune.prompt.md +251 -0
  45. package/templates/editor/.github/prompts/spec-sync-project.prompt.md +25 -2
  46. package/templates/editor/.github/skills/zen-cli/SKILL.md +14 -3
  47. package/templates/editor/.github/skills/zen-cli/references/check.md +13 -5
  48. package/templates/editor/.github/skills/zen-cli/references/export.md +107 -0
  49. package/templates/editor/.github/skills/zen-cli/references/inspect.md +262 -21
  50. package/templates/editor/.github/skills/zen-cli/references/memory.md +123 -15
  51. package/templates/editor/.github/skills/zen-cli/references/models.md +1 -1
  52. package/templates/editor/.github/skills/zen-cli/references/projects.md +12 -5
  53. package/templates/editor/.github/skills/zen-cli/references/rag.md +6 -0
  54. package/templates/editor/.github/skills/zen-cli/references/run.md +201 -4
  55. package/templates/editor/.github/skills/zen-finetune/SKILL.md +1000 -0
  56. package/templates/editor/.github/skills/zen-finetune/scripts/collect.sh +432 -0
  57. package/templates/editor/.github/skills/zen-finetune/scripts/sample.sh +199 -0
  58. package/templates/editor/.github/skills/zen-inspect/SKILL.md +396 -0
  59. package/templates/editor/.github/skills/zen-memory/SKILL.md +74 -28
  60. package/templates/editor/.github/skills/zen-memory-warmup/SKILL.md +370 -408
  61. package/templates/editor/.github/skills/zen-sandbox/SKILL.md +19 -0
  62. package/templates/editor/.github/skills/zen-sandbox-capacity/SKILL.md +334 -0
  63. package/templates/editor/.github/skills/zen-sandbox-capacity/references/loadtest.md +181 -0
  64. package/templates/editor/.github/skills/zen-sandbox-capacity/scripts/preflight_sandbox.sh +457 -0
  65. package/templates/editor/.github/skills/zen-spec-sync/SKILL.md +6 -5
  66. package/templates/editor/.vscode/agents.schema.json +1 -1
  67. package/templates/editor/.vscode/settings.json +1 -0
  68. package/templates/project/SPECIFICATION.md.tmpl +2 -0
  69. package/templates/project/agents/files-instructions.md +13 -0
  70. package/templates/project/agents/fork-instructions.md +27 -0
  71. package/templates/project/agents/memory-instructions.md +80 -7
  72. package/templates/project/gitignore +3 -0
  73. package/templates/project/scripts/_setup.sh +49 -7
@@ -1,13 +1,15 @@
1
- import { ALL_AGENTS, buildMemoryReport, hostPath, memoryDir, MemoryIndex, MemoryStore, readProjectConfig, renderMemoryHtml, } from '@zenera/neo';
1
+ import { ALL_AGENTS, buildMemoryReport, createEmbedder, GREP_FIELDS, grepMemory, hostPath, memoryDir, MemoryError, MemoryIndex, MemoryStore, MergeConflicts, mergeMemories, readProjectConfig, renderMemoryHtml, renderRecollection, } from '@zenera/neo';
2
2
  import { spawn } from 'node:child_process';
3
3
  import { existsSync } from 'node:fs';
4
4
  import { writeFile } from 'node:fs/promises';
5
- import { basename, isAbsolute, join, resolve } from 'node:path';
5
+ import { basename, isAbsolute, join, relative, resolve } from 'node:path';
6
6
  import { parse } from "../args.js";
7
+ import { loadProjectEnv } from "../env.js";
8
+ import { KeyStore } from "../keys.js";
7
9
  import { project as resolveProject } from "../resolve.js";
8
- import { ago, bold, bytes, confirm, count, cyan, dim, green, isInteractive, json, note, table, usageError, write, writeAll, yellow, } from "../term.js";
9
- const USAGE = 'zen memory [stats|ls|show|export|forget] [args] [options]';
10
- const SUBCOMMANDS = ['stats', 'ls', 'show', 'export', 'forget'];
10
+ import { ago, bold, bytes, CliError, confirm, count, cyan, dim, EXIT, green, isInteractive, json, note, table, usageError, write, writeAll, yellow, } from "../term.js";
11
+ const USAGE = 'zen memory [stats|ls|search|grep|show|export|merge|forget] [args] [options]';
12
+ const SUBCOMMANDS = ['stats', 'ls', 'search', 'grep', 'show', 'export', 'merge', 'forget'];
11
13
  /** Enough to see the shape of it; the rest is a number. */
12
14
  const LISTED = 30;
13
15
  // ---------------------------------------------------------------------------
@@ -24,9 +26,11 @@ const LISTED = 30;
24
26
  // an agent inside a run, and the mask exists to keep agents apart rather than
25
27
  // to keep secrets from the person who owns the files.
26
28
  //
27
- // Nothing here contacts a model. The store is opened without an embedder, so
28
- // inspection is free and works offline; the price is that `ls` filters by text
29
- // rather than by meaning, which is the right trade for a debugging tool.
29
+ // `search` is the one subcommand that contacts a model, and it has to: the
30
+ // question it answers is what an agent's recall returns, and a recall ranked
31
+ // any other way is a different answer wearing the same shape. Everything else
32
+ // is offline, so inspecting a graph whose credentials have gone missing —
33
+ // which is exactly when someone is debugging — still works.
30
34
  // ---------------------------------------------------------------------------
31
35
  export const memory = {
32
36
  summary: 'What the agents remember, and getting rid of it.',
@@ -35,8 +39,11 @@ export const memory = {
35
39
  details: [
36
40
  ' stats Size, vocabulary, and whether it is embedded.',
37
41
  ' ls Nodes, newest first. Changes nothing.',
42
+ ' search <text> Recall it, the way an agent does. Ranked.',
43
+ ' grep <pattern> Every node containing it, with the matching lines.',
38
44
  ' show <id> One node in full, with what it links to.',
39
45
  ' export [file] The whole graph as one HTML page.',
46
+ ' merge <dir...> Fold other memories into this one.',
40
47
  ' forget <id...> Remove nodes, their vectors and their files.',
41
48
  '',
42
49
  ' --project <name|dir> Which project. Defaults to the one you are in.',
@@ -45,10 +52,22 @@ export const memory = {
45
52
  ' --audience <name> Only nodes committed under this label.',
46
53
  ' --files Only nodes that remember a file.',
47
54
  ' --stale Only nodes something has superseded.',
48
- ' --limit <n> Rows to list. Default 30.',
55
+ ' --all For `search` and `grep`: superseded nodes too, marked.',
56
+ ' --regex Read the pattern as a regular expression, per line.',
57
+ ' --case-sensitive Match case exactly. Off by default.',
58
+ ' --in <text|metadata|file> Where to look. Repeatable. All three by default.',
59
+ ' --ids-only Print bare ids, for piping into `zen memory show`.',
60
+ ' --limit <n> Rows to list, or seeds to rank. Default 30, search 10.',
61
+ ' --hops <n> How far `search` follows links from a seed. Default 2.',
62
+ ' --nodes <n> Cap on the subgraph `search` returns. Default 25.',
63
+ ' --min-score <n> Drop seeds scoring below this. Default 0.15.',
64
+ ' --embedding <ref> Rank `search` with this model, not the project’s.',
49
65
  ' --out <file> Where `export` writes. Default memory.html.',
50
66
  ' --open Open the exported page.',
51
- ' --yes Do not ask before removing.',
67
+ ' --dry-run Say what `merge` would do, and stop.',
68
+ ' --no-dedupe Keep memories `merge` would otherwise fold together.',
69
+ ' --force Let `merge` pick a winner where two copies disagree.',
70
+ ' --yes Do not ask before removing or merging.',
52
71
  '',
53
72
  'Everything here reads the graph *unmasked* — every audience, including',
54
73
  'what no agent can see. That is the point: when a mask is the thing',
@@ -57,6 +76,34 @@ export const memory = {
57
76
  '`export` is the one to reach for. It writes a single self-contained',
58
77
  'page: the node list on the left, the graph in the middle, and whatever',
59
78
  'you click on the right, file contents and all.',
79
+ '',
80
+ '`search` is recall itself, run from here. It embeds the query with the',
81
+ 'project’s own model and prints the block a model would have been given,',
82
+ 'scores and all — so *why did it recall that?* has an answer, and so does',
83
+ '*why did it not?*:',
84
+ '',
85
+ ' zen memory search ‘how do we deploy’ --audience reviewer',
86
+ '',
87
+ 'Passing `--audience` recalls as an agent that sees only that label,',
88
+ 'which is how a memory that is present but invisible gives itself away.',
89
+ '',
90
+ '`grep` is the counterpart. Recall ranks, so it answers “what is closest”',
91
+ 'and can never answer “is this in here at all”; `grep` reads every node',
92
+ 'exactly — text, metadata and the bytes of remembered files — and the',
93
+ 'count it reports is the true one even when the list was cut:',
94
+ '',
95
+ ' zen memory grep ‘staging.example.com’ --in metadata --in file',
96
+ '',
97
+ 'Nothing that only reads takes the directory lock, so every subcommand',
98
+ 'here but `merge` and `forget` works on a memory a run is writing, and on',
99
+ 'the read-only /memory mount inside a sandbox.',
100
+ '',
101
+ '`merge` is for warming a memory in parallel. The lock is per directory,',
102
+ 'so N runs write N memories; this folds them back into one:',
103
+ '',
104
+ ' zen memory merge <batch-dir>/*/memory',
105
+ '',
106
+ 'which is where `zen run batch` leaves them.',
60
107
  ],
61
108
  run: async (ctx) => {
62
109
  const { values, positionals } = parse(ctx.args, {
@@ -66,27 +113,53 @@ export const memory = {
66
113
  audience: { type: 'string' },
67
114
  files: { type: 'boolean' },
68
115
  stale: { type: 'boolean' },
116
+ all: { type: 'boolean' },
117
+ regex: { type: 'boolean' },
118
+ 'case-sensitive': { type: 'boolean' },
119
+ in: { type: 'string', multiple: true },
120
+ 'ids-only': { type: 'boolean' },
69
121
  limit: { type: 'string' },
122
+ hops: { type: 'string' },
123
+ nodes: { type: 'string' },
124
+ 'min-score': { type: 'string' },
125
+ embedding: { type: 'string' },
70
126
  out: { type: 'string' },
71
127
  open: { type: 'boolean' },
72
128
  yes: { type: 'boolean' },
129
+ force: { type: 'boolean' },
130
+ 'dry-run': { type: 'boolean' },
131
+ 'no-dedupe': { type: 'boolean' },
73
132
  }, USAGE);
74
133
  const what = positionals[0] ?? 'stats';
75
134
  if (!SUBCOMMANDS.includes(what)) {
76
135
  throw usageError(`unknown subcommand: ${what}`, USAGE);
77
136
  }
78
137
  const rest = positionals.slice(1);
79
- const opened = await open(ctx.cwd, values.project, values.dir);
138
+ // `merge` is the one subcommand that may write a memory into existence;
139
+ // every other one is an inspector and a missing manifest is a mistake.
140
+ // And only the two that write take the lock: the moments a reader is
141
+ // most wanted are while a run is writing and against the read-only
142
+ // /memory mount, in both of which claiming it fails.
143
+ const opened = await open(ctx.cwd, values.project, values.dir, {
144
+ create: what === 'merge',
145
+ lock: what === 'merge' || what === 'forget',
146
+ });
80
147
  try {
81
148
  switch (what) {
82
149
  case 'stats':
83
150
  return stats(opened, ctx.json);
84
151
  case 'ls':
85
152
  return list(opened, values, ctx.json);
153
+ case 'search':
154
+ return await search(opened, rest, values, ctx.json);
155
+ case 'grep':
156
+ return await grep(opened, rest, values, ctx.json);
86
157
  case 'show':
87
158
  return show(opened, rest, ctx.json);
88
159
  case 'export':
89
160
  return await write_(opened, rest, values, ctx.cwd, ctx.json);
161
+ case 'merge':
162
+ return await merge(opened, rest, values, ctx.cwd, ctx.json);
90
163
  default:
91
164
  return await forget(opened, rest, values, ctx.json);
92
165
  }
@@ -105,14 +178,23 @@ export const memory = {
105
178
  * A named directory is opened as it stands, project or no project: a graph
106
179
  * copied out of a running session is the one most worth looking at, and it
107
180
  * should not have to be given an `agents.yaml` first.
181
+ *
182
+ * `create` drops the requirement that anything be there yet, for `merge`,
183
+ * which has somewhere to put what it reads even when the destination is a name
184
+ * nothing has written to.
185
+ *
186
+ * `lock` is off for a read that must not fail on a busy or read-only memory.
187
+ * The lock exists to stop two writers losing each other's edges; a reader that
188
+ * took it would only be refusing itself.
108
189
  */
109
- async function open(cwd, want, at) {
190
+ async function open(cwd, want, at, opts = {}) {
191
+ const { create = false, lock = true } = opts;
110
192
  if (at) {
111
193
  const dir = resolve(cwd, at);
112
- if (!existsSync(join(dir, 'manifest.json'))) {
194
+ if (!create && !existsSync(join(dir, 'manifest.json'))) {
113
195
  throw usageError(`${dir} is not a memory`, 'no manifest.json in it');
114
196
  }
115
- return { store: await MemoryStore.open(dir), dir, project: basename(dir) };
197
+ return { store: await MemoryStore.open(dir, { lock }), dir, project: basename(dir) };
116
198
  }
117
199
  const project = await resolveProject({ cwd, project: want });
118
200
  const { config } = readProjectConfig(project.dir);
@@ -120,10 +202,15 @@ async function open(cwd, want, at) {
120
202
  if (!dir) {
121
203
  throw usageError(`${project.name} has no memory`, 'give an agent `memory: true` in agents.yaml, or add a `memory:` block');
122
204
  }
123
- if (!existsSync(join(dir, 'manifest.json'))) {
205
+ if (!create && !existsSync(join(dir, 'manifest.json'))) {
124
206
  throw usageError(`${project.name} has a memory configured but nothing in it yet`, 'it is written the first time an agent commits something');
125
207
  }
126
- return { store: await MemoryStore.open(dir), dir, project: project.name };
208
+ return {
209
+ store: await MemoryStore.open(dir, { lock }),
210
+ dir,
211
+ project: project.name,
212
+ at: { root: project.dir, config },
213
+ };
127
214
  }
128
215
  // ---------------------------------------------------------------------------
129
216
  // stats
@@ -245,6 +332,269 @@ function list(o, flags, asJson) {
245
332
  }
246
333
  }
247
334
  // ---------------------------------------------------------------------------
335
+ // search
336
+ // ---------------------------------------------------------------------------
337
+ /**
338
+ * Recall, run from outside a run. `ls` and `grep` read the graph; this asks it
339
+ * the question an agent asks, through the same ranker, the same traversal and
340
+ * the same renderer — so what it prints is the block a model would have been
341
+ * handed, scores and all.
342
+ *
343
+ * That is the only way to answer *why did it recall that*, and the harder *why
344
+ * did it not*: a memory that is present but ranked sixth looks exactly like a
345
+ * memory that was never written, until the score is on the screen.
346
+ */
347
+ async function search(o, rest, flags, asJson) {
348
+ // Joined rather than taken singly. A query is prose, and this is the one
349
+ // subcommand whose whole argument is prose, so quoting it is the step
350
+ // everybody forgets.
351
+ const text = rest.join(' ').trim();
352
+ if (!text) {
353
+ throw usageError('search takes something to recall', 'zen memory search <text>');
354
+ }
355
+ // Recall either includes what has been superseded or does not; there is no
356
+ // "only", because a correction alone is not the subgraph anyone wanted.
357
+ if (flags.stale) {
358
+ throw usageError('search has no --stale', 'use --all to recall superseded nodes too');
359
+ }
360
+ const query = {
361
+ text,
362
+ kinds: flags.kind ? [flags.kind] : undefined,
363
+ limit: whole(flags.limit, '--limit'),
364
+ // Zero hops is a real request — the seeds and nothing they dragged in.
365
+ maxHops: whole(flags.hops, '--hops', 0),
366
+ maxNodes: whole(flags.nodes, '--nodes'),
367
+ minScore: threshold(flags['min-score']),
368
+ };
369
+ const ranker = await vectoriser(o, flags.embedding);
370
+ const sees = flags.audience ? [flags.audience] : audiences(o);
371
+ let rec;
372
+ try {
373
+ const index = new MemoryIndex({ store: o.store, embedder: ranker.embedder });
374
+ rec = await index.search(query, sees, { stale: flags.all });
375
+ }
376
+ catch (err) {
377
+ if (err instanceof MemoryError) {
378
+ throw new CliError(err.message, EXIT.usage, err.hint);
379
+ }
380
+ throw err;
381
+ }
382
+ if (asJson) {
383
+ json({
384
+ query,
385
+ sees,
386
+ // Named field by field: the ranker carries the embedder itself,
387
+ // which is a client object and no part of an answer.
388
+ ranking: { by: ranker.by, model: ranker.model, reason: ranker.reason },
389
+ seeds: rec.seeds,
390
+ truncated: rec.truncated,
391
+ nodes: rec.nodes.map((n) => ({
392
+ id: n.node.id,
393
+ kind: n.node.kind,
394
+ score: n.score,
395
+ seed: n.seed,
396
+ via: n.via,
397
+ text: n.node.text,
398
+ file: n.node.file ? hostPath(o.dir, n.node.file) : undefined,
399
+ })),
400
+ edges: rec.edges,
401
+ // Verbatim, because it is the thing that went into a prompt.
402
+ block: renderRecollection(rec, { tagged: false }),
403
+ });
404
+ return;
405
+ }
406
+ if (flags['ids-only']) {
407
+ writeAll(rec.nodes.map((n) => n.node.id));
408
+ return;
409
+ }
410
+ if (!rec.nodes.length) {
411
+ note(`nothing recalled for ${text}`);
412
+ ranking(ranker);
413
+ return;
414
+ }
415
+ write(renderRecollection(rec, { tagged: false }));
416
+ write();
417
+ ranking(ranker);
418
+ }
419
+ /**
420
+ * The embedder the project ranks with. Reproducing a recall means reproducing
421
+ * its vectors, so the ref is resolved through the same alias table a run uses
422
+ * and the keyring is materialised the same way.
423
+ *
424
+ * What cannot be built falls back to term overlap rather than failing, because
425
+ * a graph whose credentials have gone missing is one worth looking at. The
426
+ * fallback is a different ranking, though, so it is reported rather than passed
427
+ * off as recall.
428
+ */
429
+ async function vectoriser(o, override) {
430
+ // A memory with no vectors cannot be ranked by meaning whatever we build:
431
+ // embedding a query against an empty block finds nothing, which would read
432
+ // as "nothing is remembered" rather than "there is nothing to rank with".
433
+ if (!o.store.embedding) {
434
+ return { by: 'term overlap', reason: 'this memory holds no vectors' };
435
+ }
436
+ const config = o.at?.config;
437
+ const declared = override ?? config?.memory?.embedding ?? config?.embedding ?? o.store.embedding.model;
438
+ // The project's own file first, then the keyring — the order `zen run`
439
+ // uses, so a query embeds here exactly when it would embed there.
440
+ if (o.at) {
441
+ loadProjectEnv(o.at.root);
442
+ }
443
+ (await KeyStore.open()).materialize();
444
+ try {
445
+ const ref = config?.embeddings?.[declared] ?? declared;
446
+ const embedder = createEmbedder(ref);
447
+ return { embedder, by: 'meaning', model: embedder.id };
448
+ }
449
+ catch (err) {
450
+ return { by: 'term overlap', reason: err instanceof Error ? err.message : String(err) };
451
+ }
452
+ }
453
+ /**
454
+ * Which ranking that was. A lexical fallback is not what an agent sees, and a
455
+ * block that looked like recall without being it would send someone chasing a
456
+ * difference that exists only in this command.
457
+ */
458
+ function ranking(r) {
459
+ if (r.by === 'meaning') {
460
+ note(dim(`ranked by meaning · ${r.model}`));
461
+ return;
462
+ }
463
+ note(yellow('ranked by term overlap — this is not the order an agent sees'));
464
+ if (r.reason) {
465
+ note(dim(` ${r.reason}`));
466
+ }
467
+ }
468
+ /**
469
+ * Every label the graph uses. `recall` masks against a list rather than taking
470
+ * `undefined` for "no mask", so reading unmasked has to be spelled out — and
471
+ * spelling it out from the graph is exact, since a label nothing carries can
472
+ * hide nothing.
473
+ */
474
+ function audiences(o) {
475
+ return [...new Set(o.store.graph.nodes().flatMap((n) => n.audience))];
476
+ }
477
+ /** Absent stays absent, so the engine's own default is the one on display. */
478
+ function whole(value, flag, min = 1) {
479
+ if (value === undefined) {
480
+ return undefined;
481
+ }
482
+ const n = Number(value);
483
+ if (!Number.isInteger(n) || n < min) {
484
+ throw usageError(`${flag} wants a whole number ${min} or more, got ${value}`, USAGE);
485
+ }
486
+ return n;
487
+ }
488
+ function threshold(value) {
489
+ if (value === undefined) {
490
+ return undefined;
491
+ }
492
+ const n = Number(value);
493
+ if (!Number.isFinite(n) || n < 0 || n > 1) {
494
+ throw usageError(`--min-score wants a number from 0 to 1, got ${value}`, USAGE);
495
+ }
496
+ return n;
497
+ }
498
+ // ---------------------------------------------------------------------------
499
+ // grep
500
+ // ---------------------------------------------------------------------------
501
+ /**
502
+ * The exhaustive read, and the counterpart to recall rather than a variant of
503
+ * it. Recall ranks, and a ranking returns the top of a list, so it cannot tell
504
+ * "there is nothing" from "nothing was close enough" — which is the question
505
+ * anyone actually has when they come to a memory looking for a name, a path or
506
+ * a command they think an agent wrote down.
507
+ */
508
+ async function grep(o, rest, flags, asJson) {
509
+ const pattern = rest[0];
510
+ if (!pattern || rest.length > 1) {
511
+ throw usageError('grep takes exactly one pattern', 'zen memory grep <pattern>');
512
+ }
513
+ const limit = Number(flags.limit ?? LISTED);
514
+ if (!Number.isInteger(limit) || limit < 1) {
515
+ throw usageError(`--limit wants a positive whole number, got ${flags.limit}`, USAGE);
516
+ }
517
+ for (const field of flags.in ?? []) {
518
+ if (!GREP_FIELDS.includes(field)) {
519
+ throw usageError(`--in wants one of ${GREP_FIELDS.join(', ')}, got ${field}`, USAGE);
520
+ }
521
+ }
522
+ let res;
523
+ try {
524
+ res = await grepMemory(o.store, pattern, {
525
+ // Unmasked, like everything else here: this is the person who owns
526
+ // the files, not an agent inside a run.
527
+ kinds: flags.kind ? [flags.kind] : undefined,
528
+ audience: flags.audience,
529
+ files: flags.files,
530
+ in: flags.in,
531
+ stale: flags.stale ? 'only' : flags.all ? 'include' : 'exclude',
532
+ regex: flags.regex,
533
+ caseSensitive: flags['case-sensitive'],
534
+ limit,
535
+ });
536
+ }
537
+ catch (err) {
538
+ if (err instanceof MemoryError) {
539
+ throw new CliError(err.message, EXIT.usage, err.hint);
540
+ }
541
+ throw err;
542
+ }
543
+ if (asJson) {
544
+ json({
545
+ found: res.found,
546
+ truncated: res.truncated,
547
+ matches: res.matches.map((m) => ({
548
+ id: m.node.id,
549
+ kind: m.node.kind,
550
+ stale: m.stale,
551
+ file: m.node.file ? hostPath(o.dir, m.node.file) : undefined,
552
+ hits: m.hits,
553
+ more: m.more,
554
+ })),
555
+ unsearched: res.skipped,
556
+ });
557
+ return;
558
+ }
559
+ if (flags['ids-only']) {
560
+ writeAll(res.matches.map((m) => m.node.id));
561
+ return;
562
+ }
563
+ if (!res.found) {
564
+ note(`nothing matches ${pattern}`);
565
+ unsearched(res.skipped);
566
+ return;
567
+ }
568
+ for (const m of res.matches) {
569
+ write(cyan(m.node.id) +
570
+ ' ' +
571
+ m.node.kind +
572
+ (m.stale ? ' ' + yellow('superseded') : '') +
573
+ (m.node.file ? ' ' + dim(m.node.file.path) : ''));
574
+ writeAll(table(m.hits.map((h) => [` ${dim(h.where + ':' + h.line)}`, clip(h.text, 120)])));
575
+ if (m.more) {
576
+ write(dim(` … ${count(m.more, 'more line')} in this node`));
577
+ }
578
+ write();
579
+ }
580
+ if (res.truncated) {
581
+ note(dim(`${res.found - res.matches.length} more nodes — raise --limit`));
582
+ }
583
+ unsearched(res.skipped);
584
+ }
585
+ /**
586
+ * Named rather than counted. A file that was not read is a hole in an answer
587
+ * whose whole value is that it has none, so the ids have to be printable.
588
+ */
589
+ function unsearched(skipped) {
590
+ if (!skipped.length) {
591
+ return;
592
+ }
593
+ write();
594
+ note(yellow(`${count(skipped.length, 'file')} not searched`));
595
+ writeAll(table(skipped.map((s) => [` ${cyan(s.id)}`, dim(s.reason), s.path])));
596
+ }
597
+ // ---------------------------------------------------------------------------
248
598
  // show
249
599
  // ---------------------------------------------------------------------------
250
600
  function show(o, ids, asJson) {
@@ -336,6 +686,88 @@ function reveal(target) {
336
686
  spawn(command, [target], { stdio: 'ignore', detached: true }).unref();
337
687
  }
338
688
  // ---------------------------------------------------------------------------
689
+ // merge
690
+ // ---------------------------------------------------------------------------
691
+ /**
692
+ * Warming a memory is parallel work, and the lock is per directory, so N runs
693
+ * produce N memories rather than one. This is the other half of that: it folds
694
+ * them back together, offline and without a model.
695
+ *
696
+ * Divergence refuses rather than guesses. Two copies of the same memory that
697
+ * disagree are a question about which piece of work was right, and answering
698
+ * it silently is how a merge loses the answer.
699
+ */
700
+ async function merge(o, dirs, flags, cwd, asJson) {
701
+ if (!dirs.length) {
702
+ throw usageError('merge takes at least one memory directory', 'zen memory merge <dir...> [--dir <into>]');
703
+ }
704
+ const dryRun = flags['dry-run'] === true;
705
+ const held = o.store.graph.order;
706
+ if (held && !dryRun && !flags.yes) {
707
+ if (!isInteractive()) {
708
+ throw usageError(`${o.project} already remembers ${count(held, 'node')}`, 'pass --yes, or --dry-run to see what would change first');
709
+ }
710
+ note(`${count(dirs.length, 'memory', 'memories')} into ${bold(o.dir)} ` +
711
+ dim(`· ${count(held, 'node')} already there`));
712
+ if (!(await confirm('Merge?'))) {
713
+ note('nothing merged');
714
+ return;
715
+ }
716
+ }
717
+ let report;
718
+ try {
719
+ report = await mergeMemories(o.store, dirs.map((d) => resolve(cwd, d)), { dedupe: !flags['no-dedupe'], force: flags.force, dryRun });
720
+ }
721
+ catch (err) {
722
+ if (err instanceof MergeConflicts) {
723
+ throw diverged(err, cwd, asJson);
724
+ }
725
+ throw err;
726
+ }
727
+ if (asJson) {
728
+ json(report);
729
+ return;
730
+ }
731
+ writeAll(table([
732
+ [dim('memory'), dim('nodes'), dim('new'), dim('shared'), dim('folded'), dim('files')],
733
+ ...report.sources.map((s) => [
734
+ near(s.dir, cwd),
735
+ String(s.nodes),
736
+ s.added ? green(String(s.added)) : '0',
737
+ String(s.shared),
738
+ s.twins ? yellow(String(s.twins)) : '0',
739
+ String(s.files),
740
+ ]),
741
+ ]));
742
+ write();
743
+ const summary = [
744
+ count(report.added, 'node'),
745
+ `${count(report.edges, 'edge')}`,
746
+ `${report.twins} folded`,
747
+ ].join(', ');
748
+ note(dryRun ? `${yellow('would add')} ${summary}` : `${green('merged')} ${summary}`);
749
+ }
750
+ /**
751
+ * A refusal with the ids to look at. It is deliberately not a summary: the
752
+ * point of stopping is that a person decides, and they cannot decide from a
753
+ * count.
754
+ */
755
+ function diverged(err, cwd, asJson) {
756
+ if (asJson) {
757
+ json({ error: err.message, conflicts: err.conflicts });
758
+ }
759
+ else {
760
+ writeAll(table(err.conflicts.map((c) => [
761
+ cyan(c.id),
762
+ dim(`r${c.mine} ↔ r${c.theirs}`),
763
+ clip(c.text, 48),
764
+ dim(near(c.dir, cwd)),
765
+ ])));
766
+ write();
767
+ }
768
+ return new CliError(err.message, EXIT.failed, err.hint);
769
+ }
770
+ // ---------------------------------------------------------------------------
339
771
  // forget
340
772
  // ---------------------------------------------------------------------------
341
773
  /**
@@ -386,4 +818,9 @@ function clip(text, n) {
386
818
  const one = text.replace(/\s+/g, ' ').trim();
387
819
  return one.length > n ? one.slice(0, n - 1) + '…' : one;
388
820
  }
821
+ /** A path as it was probably typed: relative when it is below here, absolute when it is not. */
822
+ function near(dir, cwd) {
823
+ const rel = relative(cwd, dir);
824
+ return rel && !rel.startsWith('..') ? rel : dir;
825
+ }
389
826
  //# sourceMappingURL=memory.js.map
@@ -393,6 +393,18 @@ function verdict(probe) {
393
393
  return dim('no answer');
394
394
  }
395
395
  }
396
+ /**
397
+ * What the provider said, in the verdict's own colour. Grey is for the half of
398
+ * the output nobody needs to read, and the one line explaining a refusal is not
399
+ * in that half.
400
+ */
401
+ function reason(probe) {
402
+ const detail = probe.check.detail ?? '';
403
+ if (detail === '' || probe.check.state === 'live') {
404
+ return dim(detail);
405
+ }
406
+ return probe.check.state === 'dead' ? red(detail) : yellow(detail);
407
+ }
396
408
  /**
397
409
  * Which project and region a Vertex call was actually made in. A publisher
398
410
  * model is only missing *somewhere*, and the somewhere is the part nobody
@@ -429,11 +441,11 @@ const test = async (ctx, args) => {
429
441
  cyan(p.ref),
430
442
  verdict(p),
431
443
  dim(p.dimensions ? `${p.dimensions} dims` : ''),
432
- dim(p.check.detail ?? ''),
444
+ reason(p),
433
445
  ])));
434
446
  for (const p of probes) {
435
447
  if (p.check.fix) {
436
- note(dim(`${p.ref}: ${p.check.fix}`));
448
+ note(`${yellow('fix')} ${bold(p.ref)} ${p.check.fix}`);
437
449
  }
438
450
  }
439
451
  // A Vertex refusal is about a model *in a project*, and the project is