sumo-agents 0.4.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/AGENTS.md +8 -6
  2. package/CHANGELOG.md +187 -0
  3. package/README.md +76 -18
  4. package/bin/sumo.mjs +13 -2
  5. package/guides/feature.md +2 -1
  6. package/guides/memory.md +6 -4
  7. package/guides/projects.md +1 -1
  8. package/guides/review.md +1 -1
  9. package/package.json +5 -4
  10. package/prompts/dream.md +6 -0
  11. package/spinner.txt +25 -1
  12. package/src/apply.mjs +146 -18
  13. package/src/auth.mjs +32 -5
  14. package/src/card.mjs +39 -2
  15. package/src/catalog.mjs +6 -3
  16. package/src/chat.mjs +186 -75
  17. package/src/cli.mjs +359 -135
  18. package/src/cost.mjs +67 -8
  19. package/src/db.mjs +106 -3
  20. package/src/dream.mjs +445 -19
  21. package/src/editor.mjs +3 -2
  22. package/src/export.mjs +105 -7
  23. package/src/guard.mjs +57 -11
  24. package/src/guides.mjs +5 -4
  25. package/src/herdr.mjs +1 -1
  26. package/src/hooks.mjs +31 -18
  27. package/src/jobs.mjs +118 -39
  28. package/src/jobtools.mjs +6 -4
  29. package/src/keeper.mjs +77 -0
  30. package/src/local-server.mjs +9 -16
  31. package/src/lock.mjs +2 -1
  32. package/src/logs.mjs +165 -0
  33. package/src/loop.mjs +121 -44
  34. package/src/mcp.mjs +72 -16
  35. package/src/memory.mjs +142 -15
  36. package/src/model.mjs +41 -21
  37. package/src/paths.mjs +38 -0
  38. package/src/prime.mjs +19 -31
  39. package/src/projects.mjs +76 -12
  40. package/src/redact.mjs +10 -0
  41. package/src/render.mjs +2 -1
  42. package/src/route.mjs +16 -22
  43. package/src/sandbox.mjs +82 -3
  44. package/src/scan.mjs +35 -1
  45. package/src/scribe.mjs +10 -22
  46. package/src/sessions.mjs +46 -11
  47. package/src/setup.mjs +423 -78
  48. package/src/text.mjs +100 -1
  49. package/src/tools.mjs +258 -21
  50. package/src/tty.mjs +210 -44
  51. package/src/ui.mjs +816 -226
  52. package/src/verify.mjs +17 -1
package/src/memory.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { getMeta, setMeta, tx } from './db.mjs';
3
3
  import { redact } from './redact.mjs';
4
- import { clip, ftsQuery, overlap, sharedStems, stem, words } from './text.mjs';
4
+ import { cjkTerms, clip, containing, ftsQuery, overlap, sharedStems, stem, words } from './text.mjs';
5
5
  import { matchWithin } from './workflows.mjs';
6
6
 
7
7
  /** How much of a search that found nothing is kept as the record of it. */
8
8
  const MISS_KEPT = 200;
9
9
 
10
- export const TYPES = ['preference', 'fact', 'decision', 'gotcha', 'procedure'];
10
+ const TYPES = ['preference', 'fact', 'decision', 'gotcha', 'procedure'];
11
11
 
12
12
  const IMPORTANCE = { preference: 0.8, procedure: 0.8, decision: 0.7, gotcha: 0.6, fact: 0.5 };
13
13
  const PROVENANCE_WEIGHT = { stated: 1, observed: 0.9, scanned: 0.7, inferred: 0.5 };
@@ -26,13 +26,23 @@ const NEVER_GATED = ['', 'ls', 'cd src', 'git status', 'cat README.md'];
26
26
  const RUNAWAY = ['a'.repeat(40), 'word '.repeat(30), ' '.repeat(60), '1'.repeat(40), 'gh pr create '.repeat(8)].map((text) => `${text}!`);
27
27
 
28
28
  /** A mistake in how `sumo` was called (exit 2), as opposed to something going wrong (exit 1). */
29
- export class UsageError extends Error {}
29
+ export class UsageError extends Error {
30
+ exit = 2;
31
+ }
32
+ /** Understood, and not done: what it names is not there, not in a state for it, or not the asker's to do (exit 1). */
33
+ export class Refusal extends UsageError {
34
+ exit = 1;
35
+ }
36
+ /** Not done for want of an Anthropic credential in this shell (exit 4, as other tools say "log in first"). */
37
+ export class NeedsCredential extends UsageError {
38
+ exit = 4;
39
+ }
30
40
 
31
41
  /**
32
42
  * A gate is a regular expression over a shell command: the commands a workflow has to come before.
33
43
  * It is written once, by whoever teaches the workflow, and shown to the user — stated, not guessed.
34
44
  */
35
- export function checkGate(raw) {
45
+ function checkGate(raw) {
36
46
  const pattern = String(raw ?? '').trim();
37
47
  const example = 'a gate is a regular expression over the shell command, e.g. gh(-axi)? pr create';
38
48
  if (!pattern) throw new UsageError(example);
@@ -53,7 +63,7 @@ export function checkGate(raw) {
53
63
 
54
64
  export function setGate(db, id, raw) {
55
65
  const row = get(db, id);
56
- if (row.type !== 'procedure') throw new UsageError(`m${id} is not a workflow — only a workflow can gate a command`);
66
+ if (row.type !== 'procedure') throw new Refusal(`m${id} is not a workflow — only a workflow can gate a command`);
57
67
  const gate = raw === null ? null : checkGate(raw);
58
68
  db.prepare('UPDATE memories SET gate = ? WHERE id = ?').run(gate, id);
59
69
  return get(db, id);
@@ -73,7 +83,7 @@ export function resolveProject(db, nameOrAlias) {
73
83
  if (!row) {
74
84
  const known = db.prepare(`SELECT slug FROM projects WHERE status = 'active' ORDER BY slug LIMIT 9`).all().map((r) => r.slug);
75
85
  const listed = known.length === 0 ? '' : ` (known: ${known.slice(0, 8).join(', ')}${known.length > 8 ? ', …' : ''})`;
76
- throw new UsageError(`unknown project "${nameOrAlias}"${listed} — register it first: sumo project add <path>`);
86
+ throw new Refusal(`unknown project "${nameOrAlias}"${listed} — register it first: sumo project add <path>`);
77
87
  }
78
88
  return row.slug;
79
89
  }
@@ -84,13 +94,13 @@ function scopeFor(db, project) {
84
94
 
85
95
  export function get(db, id) {
86
96
  const row = db.prepare('SELECT * FROM memories WHERE id = ?').get(id);
87
- if (!row) throw new UsageError(`no memory m${id}`);
97
+ if (!row) throw new Refusal(`no memory m${id}`);
88
98
  return row;
89
99
  }
90
100
 
91
101
  function requireActive(db, id, verb) {
92
102
  const row = get(db, id);
93
- if (row.state !== 'active') throw new UsageError(`m${id} is ${row.state} — only an active memory can be ${verb}`);
103
+ if (row.state !== 'active') throw new Refusal(`m${id} is ${row.state} — only an active memory can be ${verb}`);
94
104
  return row;
95
105
  }
96
106
 
@@ -255,6 +265,30 @@ export function markHits(db, ids, now) {
255
265
  db.prepare(`UPDATE memories SET hits = hits + 1, last_hit_at = ? WHERE id IN (${ids.map(() => '?').join(', ')})`).run(now, ...ids);
256
266
  }
257
267
 
268
+ /**
269
+ * What the index found, with the memories holding a Chinese, Japanese or Korean word of the search: the index keeps a
270
+ * run of those characters as one word, so a word inside it is looked for in the text itself, under the same `filters`.
271
+ * One found that way ranks by the share of those words it holds, the best the index found counting as all of them.
272
+ * With no such word, the index alone answers, unchanged.
273
+ */
274
+ function withCjk(db, terms, found, filters, params) {
275
+ if (terms.length === 0) return found;
276
+ const fields = ['title', 'cue', 'body', 'topic', 'keys'];
277
+ const holds = terms.flatMap(() => fields.map((f) => `m.${f} LIKE ? ESCAPE '\\'`)).join(' OR ');
278
+ const rows = db
279
+ .prepare(`SELECT m.* FROM memories m WHERE (${holds})${filters.map((w) => ` AND ${w}`).join('')}`)
280
+ .all(...terms.flatMap((t) => fields.map(() => containing(t))), ...params);
281
+ const best = found.length > 0 ? found.reduce((min, row) => Math.min(min, row.rank), 0) : -1;
282
+ const byId = new Map(found.map((row) => [row.id, row]));
283
+ for (const row of rows) {
284
+ const text = fields.map((f) => row[f] ?? '').join(' ').toLowerCase();
285
+ const rank = (best * terms.filter((t) => text.includes(t)).length) / terms.length;
286
+ const known = byId.get(row.id);
287
+ if (!known || rank < known.rank) byId.set(row.id, { ...row, rank });
288
+ }
289
+ return [...byId.values()];
290
+ }
291
+
258
292
  /**
259
293
  * Ranked search. Scope is `global` plus the named project; other projects stay
260
294
  * invisible unless `everywhere` is set — only their hit counts come back, so
@@ -268,7 +302,8 @@ export function search(db, query, opts = {}) {
268
302
  const match = ftsQuery(query);
269
303
  if (!match) throw new UsageError('nothing to search for');
270
304
  if (opts.type && !TYPES.includes(opts.type)) {
271
- throw new UsageError(`unknown type "${opts.type}" — one of: ${TYPES.join(', ')}`);
305
+ // Said as the user knows them: a procedure is a workflow everywhere they look.
306
+ throw new UsageError(`unknown type "${opts.type}" — one of: ${TYPES.map((t) => (t === 'procedure' ? 'workflow' : t)).join(', ')}`);
272
307
  }
273
308
 
274
309
  const projectScope = opts.project ? scopeFor(db, opts.project) : null;
@@ -292,12 +327,13 @@ export function search(db, query, opts = {}) {
292
327
  params.push(opts.before);
293
328
  }
294
329
 
295
- const rows = db
330
+ const found = db
296
331
  .prepare(
297
332
  `SELECT m.*, bm25(memories_fts) AS rank FROM memories_fts JOIN memories m ON m.id = memories_fts.rowid
298
333
  WHERE ${where.join(' AND ')}`,
299
334
  )
300
335
  .all(...params);
336
+ const rows = withCjk(db, cjkTerms(query), found, where.slice(1), params.slice(1));
301
337
 
302
338
  const inScope = (row) => opts.everywhere || row.scope === 'global' || row.scope === projectScope;
303
339
  // OR-ed words match on any one of them. Fine for words a caller picked; a caller searching with
@@ -313,8 +349,10 @@ export function search(db, query, opts = {}) {
313
349
 
314
350
  const elsewhere = {};
315
351
  for (const row of rows) if (!inScope(row)) elsewhere[row.scope] = (elsewhere[row.scope] ?? 0) + 1;
352
+ // Matches past the limit, counted, so a list cut short says so.
353
+ const more = candidates.length - results.length;
316
354
 
317
- if (opts.record === false) return { results, elsewhere };
355
+ if (opts.record === false) return { results, elsewhere, more };
318
356
  if (results.length > 0) {
319
357
  markHits(db, results.map((r) => r.id), now);
320
358
  } else {
@@ -328,7 +366,7 @@ export function search(db, query, opts = {}) {
328
366
  );
329
367
  }
330
368
 
331
- return { results, elsewhere };
369
+ return { results, elsewhere, more };
332
370
  }
333
371
 
334
372
  /** Every memory connected to `id` by supersession, oldest first. */
@@ -385,16 +423,105 @@ export function forget(db, id, { purge = false, now = new Date().toISOString() }
385
423
  }
386
424
 
387
425
  /** The user said yes to a guess: from here on it carries the weight of something they stated. */
388
- export function confirm(db, id) {
426
+ export function confirm(db, id, { byUser = true } = {}) {
389
427
  const row = get(db, id);
390
- if (row.state !== 'unconfirmed') throw new UsageError(`m${id} is ${row.state} — only an unconfirmed memory can be confirmed`);
428
+ if (row.state !== 'unconfirmed') throw new Refusal(`m${id} is ${row.state} — only an unconfirmed memory can be confirmed`);
429
+ // Held because what it says came from a page or an MCP server the model read: the model may not vouch for it.
430
+ if (row.held_for === 'outside' && !byUser) throw new Refusal(`m${id} came from a conversation that read the web or an MCP server: only the user confirms it — on the memory page (/memory), or with sumo confirm in their own terminal`);
391
431
  db.prepare(`UPDATE memories SET state = 'active', provenance = 'stated' WHERE id = ?`).run(id);
392
432
  return get(db, id);
393
433
  }
394
434
 
395
435
  export function reject(db, id, now = new Date().toISOString()) {
396
436
  const row = get(db, id);
397
- if (row.state !== 'unconfirmed') throw new UsageError(`m${id} is ${row.state} — only an unconfirmed memory can be rejected`);
437
+ if (row.state !== 'unconfirmed') throw new Refusal(`m${id} is ${row.state} — only an unconfirmed memory can be rejected`);
398
438
  db.prepare(`UPDATE memories SET state = 'rejected', invalid_at = ? WHERE id = ?`).run(now, id);
399
439
  return get(db, id);
400
440
  }
441
+
442
+ /** How long a gotcha an agent filed may go unshown before it is let go. */
443
+ export const GOTCHA_UNUSED_DAYS = 45;
444
+ const DAY_MS = 86_400_000;
445
+
446
+ /**
447
+ * Lets go of the traps agents filed that nobody was ever shown: observed, unpinned, no hits, older than
448
+ * GOTCHA_UNUSED_DAYS. Let go the way `sumo forget` does — state `invalid`, the row and its history kept. What the user
449
+ * said, pinned, or was shown is never touched. Returns how many went.
450
+ */
451
+ export function expireUnusedGotchas(db, now = new Date().toISOString()) {
452
+ const before = new Date(Date.parse(now) - GOTCHA_UNUSED_DAYS * DAY_MS).toISOString();
453
+ const unused = db
454
+ .prepare(`SELECT id FROM memories WHERE type = 'gotcha' AND state = 'active' AND provenance = 'observed' AND pinned = 0 AND hits = 0 AND created_at < ?`)
455
+ .all(before);
456
+ tx(db, () => {
457
+ for (const { id } of unused) forget(db, id, { now });
458
+ });
459
+ return unused.length;
460
+ }
461
+
462
+ /** The writers whose memories start as a model's guess; one confirmed by the user becomes `stated` and keeps no quote. */
463
+ const GUESSING_WRITERS = ['scribe', 'dream', 'worker'];
464
+ /** How old a project's scan may be before memory health names it. */
465
+ const SCAN_STALE_DAYS = 7;
466
+
467
+ /**
468
+ * How memory is doing, as plain values for whoever says it — doctor, the memory status:
469
+ * - `lastTurnAt`: when the user was last heard (null: never);
470
+ * - `lastPass`: the last memory pass, `{ at, ok, error }` (null: never ran);
471
+ * - `waiting`, `oldestWaitingDays`: guesses waiting for the user, and the age of the oldest;
472
+ * - `guesses`: of the guesses settled, how many the user confirmed and rejected, and the share confirmed;
473
+ * - `scans`: active projects whose scan is more than a week old, oldest first, with its age in days.
474
+ */
475
+ export function memoryHealth(db, now = new Date().toISOString()) {
476
+ const daysSince = (at) => Math.floor((Date.parse(now) - Date.parse(at)) / DAY_MS);
477
+ const lastTurnAt = db.prepare('SELECT MAX(ts) AS at FROM user_turns').get().at ?? null;
478
+ const pass = db.prepare(`SELECT ts, ok, note FROM model_runs WHERE kind IN ('scribe', 'dream') ORDER BY ts DESC, id DESC LIMIT 1`).get();
479
+ const lastPass = pass ? { at: pass.ts, ok: pass.ok === 1, error: pass.ok === 1 ? null : pass.note } : null;
480
+ const waitingRow = db.prepare(`SELECT COUNT(*) AS n, MIN(created_at) AS oldest FROM memories WHERE state = 'unconfirmed'`).get();
481
+ const writers = GUESSING_WRITERS.map(() => '?').join(', ');
482
+ const confirmed = db.prepare(`SELECT COUNT(*) AS n FROM memories WHERE provenance = 'stated' AND source_quote IS NULL AND written_by IN (${writers})`).get(...GUESSING_WRITERS).n;
483
+ const rejected = db.prepare(`SELECT COUNT(*) AS n FROM memories WHERE state = 'rejected'`).get().n;
484
+ const scans = db
485
+ .prepare(`SELECT slug, scanned_at FROM projects WHERE status = 'active' AND scanned_at IS NOT NULL ORDER BY scanned_at`)
486
+ .all()
487
+ .map((p) => ({ slug: p.slug, days: daysSince(p.scanned_at) }))
488
+ .filter((p) => p.days > SCAN_STALE_DAYS);
489
+ return {
490
+ lastTurnAt,
491
+ lastPass,
492
+ waiting: waitingRow.n,
493
+ oldestWaitingDays: waitingRow.oldest ? daysSince(waitingRow.oldest) : null,
494
+ guesses: { confirmed, rejected, rate: confirmed + rejected > 0 ? confirmed / (confirmed + rejected) : null },
495
+ scans,
496
+ };
497
+ }
498
+
499
+ /** How long ago, in the largest whole unit: `3d ago`, `5h ago`, `just now`. */
500
+ function sinceSaid(at, now) {
501
+ const hours = (Date.parse(now) - Date.parse(at)) / 3_600_000;
502
+ if (hours >= 24) return `${Math.floor(hours / 24)}d ago`;
503
+ return hours >= 1 ? `${Math.floor(hours)}h ago` : 'just now';
504
+ }
505
+ /** A scan this old is a warning, not a note. */
506
+ const SCAN_WARN_DAYS = 30;
507
+
508
+ /**
509
+ * Memory health said in one line, for doctor and the memory status: `{ ok, label, fix }`. Not ok — a warning — when the
510
+ * last pass failed or a project's scan is a month old; `fix` says what to do about each.
511
+ */
512
+ export function memoryHealthLine(health, now = new Date().toISOString()) {
513
+ const parts = [
514
+ health.lastTurnAt ? `last heard ${sinceSaid(health.lastTurnAt, now)}` : 'nothing heard yet',
515
+ health.lastPass ? `last pass ${sinceSaid(health.lastPass.at, now)}${health.lastPass.ok ? '' : ', failed'}` : 'no memory pass yet',
516
+ health.waiting > 0 ? `${health.waiting} guess${health.waiting === 1 ? '' : 'es'} waiting, the oldest ${health.oldestWaitingDays}d` : 'no guesses waiting',
517
+ ];
518
+ if (health.guesses.rate !== null) parts.push(`${Math.round(health.guesses.rate * 100)}% of guesses confirmed`);
519
+ if (health.scans.length > 0) {
520
+ const named = health.scans.slice(0, 3).map((p) => `${p.slug}, ${p.days}d`).join('; ');
521
+ parts.push(`${health.scans.length} project scan${health.scans.length === 1 ? '' : 's'} over ${SCAN_STALE_DAYS} days old (${named}${health.scans.length > 3 ? '; …' : ''})`);
522
+ }
523
+ const failed = health.lastPass && !health.lastPass.ok;
524
+ const old = health.scans.filter((p) => p.days > SCAN_WARN_DAYS);
525
+ const fix = [failed ? `the last pass failed: ${health.lastPass.error}` : null, old.length > 0 ? `rescan: sumo project rescan ${old[0].slug}${old.length > 1 ? ` (and ${old.length - 1} more)` : ''}` : null].filter(Boolean).join('; ');
526
+ return { ok: !failed && old.length === 0, label: `memory: ${parts.join(' · ')}`, fix };
527
+ }
package/src/model.mjs CHANGED
@@ -1,13 +1,13 @@
1
- import { jsonSchemaOutputFormat } from '@anthropic-ai/sdk/helpers/json-schema';
2
1
  import { spawnSync } from 'node:child_process';
3
- import { accessSync, appendFileSync, constants, existsSync, mkdirSync } from 'node:fs';
2
+ import { accessSync, constants, existsSync } from 'node:fs';
4
3
  import { join } from 'node:path';
5
- import { anthropicClient, authenticatedRequest, resolveAnthropicCredential } from './auth.mjs';
4
+ import { anthropicClient, authenticatedRequest, loadSdk, resolveAnthropicCredential } from './auth.mjs';
6
5
  import { describeApiError, modelId } from './catalog.mjs';
7
6
  import { getMeta } from './db.mjs';
8
7
  import { ensureLocalServer, LOCAL_REASONING_BUDGET, modelIdOf } from './local-server.mjs';
8
+ import { appendLog } from './logs.mjs';
9
9
  import { paths } from './paths.mjs';
10
- import { CONFIG_DEFAULTS } from './setup.mjs';
10
+ import { modelFileOf } from './setup.mjs';
11
11
  import { SEARCH_USD } from './web.mjs';
12
12
 
13
13
  const TIMEOUT_MS = 180_000;
@@ -18,26 +18,38 @@ const MAX_RETRIES = 1;
18
18
  const MAX_ANSWER_TOKENS = 400;
19
19
  // A memory pass on the local model thinks first, up to the server's reasoning budget, then lists its operations.
20
20
  const MAX_LOCAL_PASS_TOKENS = LOCAL_REASONING_BUDGET + 3072;
21
- // A scribe pass answers with a list of operations. Every model but haiku thinks first, and the thinking counts toward this
21
+ // A scribe pass answers with a list of operations. Every model thinks first, and the thinking counts toward this
22
22
  // limit too: 8k holds both, ends well inside the call's three minutes, and is far below the 21,333 the SDK sends without
23
23
  // streaming. An answer longer than this is the model rambling, not remembering.
24
24
  const MAX_API_ANSWER_TOKENS = 8192;
25
- /** What the SDK's beta parse() sent with a schema'd request, kept for the OAuth path that goes through the beta API. */
26
- const STRUCTURED_OUTPUTS_BETA = 'structured-outputs-2025-12-15';
27
25
 
28
- /** US dollars per million tokens: input, output, and what a cache read costs relative to input. */
29
- export const PRICES = {
30
- 'claude-haiku-4-5-20251001': { input: 1, output: 5, cacheRead: 0.1 },
31
- 'claude-sonnet-5-5': { input: 2, output: 10, cacheRead: 0.1 },
26
+ /**
27
+ * US dollars per million tokens: input, output, and what a cache read costs relative to input. Haiku has two rate cards,
28
+ * by the size of the prompt — everything read, from the cache or not — `over` is the dearer one past that many tokens.
29
+ */
30
+ const PRICES = {
31
+ 'claude-haiku-5-5': { input: 0.1, output: 0.5, cacheRead: 0.1, over: { tokens: 100_000, input: 0.5, output: 2.5, cacheRead: 0.1 } },
32
+ 'claude-sonnet-5-5': { input: 2, output: 10, cacheRead: 0.05 },
32
33
  'claude-opus-5-5': { input: 4, output: 20, cacheRead: 0.05 },
33
34
  'claude-fable-5-1': { input: 10, output: 50, cacheRead: 0.025 },
35
+ // Where a declined reply is run again (`fallbacks`): the API names the model by the category; the ledger must price what it answered on.
36
+ 'claude-opus-4-8': { input: 5, output: 25, cacheRead: 0.1 },
37
+ 'claude-sonnet-5': { input: 2, output: 10, cacheRead: 0.1 },
34
38
  };
35
39
  /** What writing to the cache costs relative to input: the five-minute cache every request here writes. */
36
40
  export const CACHE_WRITE = 1.25;
37
41
 
42
+ /** The rate card a response is priced on: the model's, or its dearer one when the prompt was over that size; null for a model the list does not know. */
43
+ export function priceOf(model, usage = {}) {
44
+ const price = PRICES[modelId(model)];
45
+ if (!price) return null;
46
+ const prompt = (usage.input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0) + (usage.cache_creation_input_tokens ?? 0);
47
+ return price.over && prompt > price.over.tokens ? price.over : price;
48
+ }
49
+
38
50
  /** What one response cost, from its usage and the price list; 0 for a model the list does not know. */
39
51
  export function costOf(model, usage) {
40
- const price = PRICES[modelId(model)];
52
+ const price = priceOf(model, usage);
41
53
  if (!price) return 0;
42
54
  const plain = usage.input_tokens ?? 0;
43
55
  const read = usage.cache_read_input_tokens ?? 0;
@@ -48,6 +60,16 @@ export function costOf(model, usage) {
48
60
  return (plain * price.input + read * price.input * price.cacheRead + written * price.input * CACHE_WRITE + out * price.output) / 1_000_000 + searches;
49
61
  }
50
62
 
63
+ /**
64
+ * The answer's shape as the API takes it: what the SDK's jsonSchemaOutputFormat gives with its transform off, less the
65
+ * parse() that only the SDK's own parse() calls — written here, so a pass that never calls the API never loads the SDK.
66
+ * Sent as written: the helper's own rewrite moves every enum into a description, and the API then holds the answer to none of them.
67
+ */
68
+ function outputFormat(schema) {
69
+ if (schema.type !== 'object') throw new Error(`JSON schema must be an object, but got ${schema.type}`);
70
+ return { type: 'json_schema', schema: { ...schema } };
71
+ }
72
+
51
73
  /** The request one cheap-model call sends: a frozen system prompt, the bundle as the one user turn, the answer shape enforced by the API. */
52
74
  export function requestFor({ system, prompt, schema, model }) {
53
75
  return {
@@ -55,8 +77,7 @@ export function requestFor({ system, prompt, schema, model }) {
55
77
  max_tokens: MAX_API_ANSWER_TOKENS,
56
78
  system,
57
79
  messages: [{ role: 'user', content: prompt }],
58
- // Sent as written. The helper's own rewrite moves every enum into a description, and the API then holds the answer to none of them.
59
- output_config: { format: jsonSchemaOutputFormat(schema, { transform: false }) },
80
+ output_config: { format: outputFormat(schema) },
60
81
  };
61
82
  }
62
83
 
@@ -127,13 +148,13 @@ export async function callModel(db, { system, prompt, schema, model, kind }) {
127
148
  try {
128
149
  const credential = resolveAnthropicCredential();
129
150
  if (!credential) return failure('no Anthropic credential — export ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN in the shell that runs sumo');
151
+ await loadSdk();
130
152
  const client = anthropicClient({ timeout: TIMEOUT_MS, maxRetries: MAX_RETRIES }, credential);
131
153
  const request = authenticatedRequest(requestFor({ system, prompt, schema, model }), credential);
132
154
  // Not the SDK's parse(): it throws on an answer that is not JSON before the stop reason or the usage can be read,
133
155
  // so a cut-off or refused answer would be misnamed and what it spent never logged. The answer is parsed below instead.
134
- const response = credential?.type === 'oauth'
135
- ? await client.beta.messages.create({ ...request, betas: [...request.betas, STRUCTURED_OUTPUTS_BETA] })
136
- : await client.messages.create(request);
156
+ // A Claude Code login goes through the beta API, which carries its betas; structured output itself needs none.
157
+ const response = credential?.type === 'oauth' ? await client.beta.messages.create(request) : await client.messages.create(request);
137
158
  const usage = usageOf(model, response.usage);
138
159
  if (response.stop_reason === 'refusal') return { ...failure(`the model declined to answer (${response.stop_details?.category ?? 'no category'})`), usage };
139
160
  if (response.stop_reason === 'max_tokens') return { ...failure(`the answer was cut off at ${MAX_API_ANSWER_TOKENS} tokens`), usage };
@@ -168,7 +189,7 @@ function describeLocal(status, body) {
168
189
  */
169
190
  export async function callLocalModel(db, { system, prompt, schema, kind = 'local', think = false, maxTokens = MAX_ANSWER_TOKENS, log = true, timeoutMs = TIMEOUT_MS }) {
170
191
  const now = new Date().toISOString();
171
- const modelFile = getMeta(db, 'config.model.file') ?? CONFIG_DEFAULTS['model.file'];
192
+ const modelFile = modelFileOf(db);
172
193
  const modelLabel = `local:${modelFile}`;
173
194
  const record = (result) => {
174
195
  if (log) logRun(db, { kind: 'local', model: modelLabel, result, note: result.ok ? 'ok' : result.error, now });
@@ -262,9 +283,8 @@ export function logRun(db, { kind, model, result, note, now, jobId = null, sessi
262
283
  now, kind, model, result.usage.inputTokens, result.usage.outputTokens, result.usage.costUsd, result.ok ? 1 : 0, note,
263
284
  result.usage.cacheReadTokens ?? null, result.usage.cacheCreationTokens ?? null, jobId, sessionId,
264
285
  );
265
- mkdirSync(paths().logs, { recursive: true, mode: 0o700 });
266
- appendFileSync(
267
- join(paths().logs, 'scribe.log'),
286
+ appendLog(
287
+ 'scribe.log',
268
288
  `${now} ${kind} ${model} ${result.ok ? 'ok' : 'FAILED'} in=${result.usage.inputTokens} out=${result.usage.outputTokens} $${result.usage.costUsd.toFixed(4)} ${note}\n`,
269
289
  );
270
290
  }
package/src/paths.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { readFileSync, statSync } from 'node:fs';
1
2
  import { homedir } from 'node:os';
2
3
  import { dirname, join, resolve } from 'node:path';
3
4
  import { fileURLToPath } from 'node:url';
@@ -9,6 +10,15 @@ import { fileURLToPath } from 'node:url';
9
10
  export const COMPILED = import.meta.url.startsWith('file:///$bunfs/');
10
11
  export const REPO_ROOT = COMPILED ? dirname(process.execPath) : resolve(dirname(fileURLToPath(import.meta.url)), '..');
11
12
  export const ENTRY = COMPILED ? process.execPath : join(REPO_ROOT, 'bin', 'sumo.mjs');
13
+ /** This sumo's version, from its package.json; empty where there is none to read. Read here, with nothing else loaded, so a crash while loading still says it. */
14
+ export const VERSION = (() => {
15
+ try {
16
+ return String(JSON.parse(readFileSync(join(REPO_ROOT, 'package.json'), 'utf8')).version ?? '');
17
+ } catch {
18
+ return '';
19
+ }
20
+ })();
21
+
12
22
  /** The words that start this sumo again: the binary alone, or this node on the entry. */
13
23
  export const SELF = COMPILED ? [ENTRY] : [process.execPath, '--disable-warning=ExperimentalWarning', ENTRY];
14
24
 
@@ -31,3 +41,31 @@ export function paths() {
31
41
  scratch: join(home, 'scratch'),
32
42
  };
33
43
  }
44
+
45
+ /**
46
+ * Why the home cannot be used, said for a person with what to do next — or null when a failure is about something
47
+ * else: a file where its folder should be (the home itself, or a folder above it), or a place sumo may not write.
48
+ * Without a cause only the first is looked for: doctor asks that before anything reads from the home.
49
+ */
50
+ export function homeProblem(cause = null, home = paths().home) {
51
+ let at = home;
52
+ for (;;) {
53
+ let found = null;
54
+ try {
55
+ found = statSync(at);
56
+ } catch {
57
+ // Not there yet, or under a file: the folder above says which.
58
+ }
59
+ if (found && !found.isDirectory()) {
60
+ return `${at} is a file, not a folder — ${process.env.SUMO_AGENTS_HOME ? 'SUMO_AGENTS_HOME must name a folder' : 'sumo keeps its home there: move the file aside'}`;
61
+ }
62
+ if (found || dirname(at) === at) break;
63
+ at = dirname(at);
64
+ }
65
+ if (!['EACCES', 'EROFS'].includes(cause?.code) && !/readonly database/.test(cause?.message ?? '')) return null;
66
+ // In the model's shell the words stay SQLite's and Node's: the sandbox reads them to tell a write it blocked, and asks the user.
67
+ if (process.env.SUMO_TOOL || process.env.SUMO_JOB) return null;
68
+ return at === home
69
+ ? `the home ${home} is not writable — check its owner and mode, or sumo doctor --fix`
70
+ : `the home ${home} cannot be made: ${at} is not writable — check its owner and mode`;
71
+ }
package/src/prime.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { homedir } from 'node:os';
2
- import { ago } from './card.mjs';
2
+ import { ago, askLines } from './card.mjs';
3
3
  import { getMeta } from './db.mjs';
4
4
  import { paths } from './paths.mjs';
5
- import { isScratch, listProjects, scratchDays } from './projects.mjs';
5
+ import { isScratch, listProjects, SCRATCH_DAYS } from './projects.mjs';
6
6
  import { CONFIG_DEFAULTS } from './setup.mjs';
7
7
  import { clip, estimateTokens } from './text.mjs';
8
8
 
@@ -11,6 +11,8 @@ const NAME_MAX = 60;
11
11
  const MAX_PROJECTS = 12;
12
12
  const MAX_LEFT_OFF = 3;
13
13
  const MAX_CONFIRM = 3;
14
+ /** The block's ceiling, in tokens: what does not fit is found by search. */
15
+ const PRIME_BUDGET = 800;
14
16
  const MAX_NOTICES = 2;
15
17
  const SCRIBE_FAILURES_TO_WARN = 3;
16
18
  /** A preference never used, and said more days ago than this, is the first to leave the block when room is short. */
@@ -25,20 +27,19 @@ const tilde = (path) => (path === homedir() || path.startsWith(`${homedir()}/`)
25
27
  * they are the problem". Everything that does not fit is reachable by search,
26
28
  * and the block says so rather than silently pretending to be complete.
27
29
  */
28
- export function prime(db, { budget, now = new Date().toISOString() } = {}) {
29
- const asked = budget ?? Number(getMeta(db, 'config.prime.budget') ?? CONFIG_DEFAULTS['prime.budget']);
30
+ export function prime(db, { budget, project = null, now = new Date().toISOString() } = {}) {
30
31
  // A budget that is not a number would hold nothing back: the default stands in for it.
31
- const limit = Number.isInteger(asked) && asked > 0 ? asked : Number(CONFIG_DEFAULTS['prime.budget']);
32
+ const limit = Number.isInteger(budget) && budget > 0 ? budget : PRIME_BUDGET;
32
33
  const machine = getMeta(db, 'machine');
33
34
  const open = `<sumo-memory${machine ? ` machine="${machine}"` : ''}>`;
34
35
  const close = '</sumo-memory>';
35
36
 
36
37
  // The small, time-sensitive sections are fixed first; preferences get whatever room is left.
37
38
  // Preserve the compact layout normally; when it cannot fit, keep time-sensitive lines first.
38
- const sections = [workflows(db), projectsLine(db), leftOff(db, now), jobs(db, now), toConfirm(db), warnings(db)];
39
+ const sections = [workflows(db), projectsLine(db), leftOff(db, now), jobs(db, now), toConfirm(db, { project, now }), warnings(db)];
39
40
  // Where the work with no project goes is said every time, and is not something remembered.
40
41
  const remembered = sections.flat().length;
41
- sections.splice(2, 0, scratchLine(db));
42
+ sections.splice(2, 0, scratchLine());
42
43
  const fixed = sections.flat();
43
44
  const spent = estimateTokens([open, ...fixed, close].join('\n'));
44
45
  const prefs = preferences(db, limit - spent, now);
@@ -121,8 +122,8 @@ function projectsLine(db) {
121
122
  return [`Projects: ${shown.join(' · ')}${more}`];
122
123
  }
123
124
 
124
- function scratchLine(db) {
125
- return [`Scratch: ${tilde(paths().scratch)} — work that belongs to no project goes here, a folder per chore; one idle ${scratchDays(db)} days is deleted`];
125
+ function scratchLine() {
126
+ return [`Scratch: ${tilde(paths().scratch)} — work that belongs to no project goes here, a folder per chore; one idle ${SCRATCH_DAYS} days is deleted`];
126
127
  }
127
128
 
128
129
  function leftOff(db, now) {
@@ -142,35 +143,22 @@ function jobs(db, now) {
142
143
  return rows.map((j) => `Job j${j.id} ${j.project} "${clip(j.title, 60)}" ${j.status.toUpperCase()} (${ago(j.updated_at, now)}) → sumo job show ${j.id}`);
143
144
  }
144
145
 
145
- function toConfirm(db) {
146
- const guesses = db.prepare(`SELECT * FROM memories WHERE state = 'unconfirmed' ORDER BY id LIMIT ?`).all(MAX_CONFIRM);
147
- const out = guesses.map(
148
- (m) => `Ask the user (then sumo confirm|reject m${m.id}): is this right? "${clip(m.type === 'procedure' ? `workflow "${m.title}"` : m.body, LINE_MAX)}"`,
149
- );
150
- // A notice stands only while both memories are still true; superseding or forgetting either settles it.
151
- const notices = db
152
- .prepare(
153
- `SELECT n.* FROM notices n
154
- JOIN memories a ON a.id = n.memory_a AND a.state = 'active'
155
- JOIN memories b ON b.id = n.memory_b AND b.state = 'active'
156
- ORDER BY n.id LIMIT ?`,
157
- )
158
- .all(MAX_NOTICES);
159
- for (const n of notices) {
160
- const verb = n.kind === 'lookalike' ? 'look alike' : 'disagree';
161
- out.push(`Ask the user: m${n.memory_a} and m${n.memory_b} ${verb} — ${clip(n.note, LINE_MAX)} (sumo show both; settle with sumo supersede or sumo forget)`);
162
- }
163
- return out;
146
+ /**
147
+ * What to put to the user at the start: guesses and notices about everything and about this session's project, each
148
+ * guess for two weeks. One about another project is asked with that project's card, when it comes up.
149
+ */
150
+ function toConfirm(db, { project = null, now }) {
151
+ return askLines(db, { scopes: ['global', ...(project ? [`project:${project}`] : [])], now, guesses: MAX_CONFIRM, notices: MAX_NOTICES });
164
152
  }
165
153
 
166
154
  function warnings(db) {
167
155
  const out = [];
168
156
  // AGENTS.md says a background writer files what the user says; with it off, nothing does unless the model is told.
169
- if ((getMeta(db, 'config.scribe.model') ?? CONFIG_DEFAULTS['scribe.model']) === 'off') {
170
- out.push('The memory writer is off (scribe.model): save what the user states that should last, as it is said, with sumo add.');
157
+ if ((getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model']) === 'off') {
158
+ out.push('The memory pass is off (memory.model): save what the user states that should last, as it is said, with sumo add.');
171
159
  }
172
160
  const failures = Number(getMeta(db, 'scribe.failures') ?? 0);
173
- if (failures >= SCRIBE_FAILURES_TO_WARN) out.push(`Warning: the background memory writer has failed ${failures} times in a row — nothing new is being remembered. Tell the user; sumo scribe status shows why.`);
161
+ if (failures >= SCRIBE_FAILURES_TO_WARN) out.push(`Warning: the background memory writer has failed ${failures} times in a row — nothing new is being remembered. Tell the user; sumo memory status shows why.`);
174
162
  const backup = getMeta(db, 'backup.failed');
175
163
  if (backup) out.push(`Warning: the weekly backup could not be written (${backup}). Tell the user; sumo backup tries again.`);
176
164
  return out;