sumo-agents 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/settings.mjs CHANGED
@@ -3,11 +3,12 @@ import { homedir } from 'node:os';
3
3
  import { join } from 'node:path';
4
4
  import { Box, render, Text, useApp, useInput, usePaste, useWindowSize } from 'ink';
5
5
  import { createElement as h, useReducer } from 'react';
6
- import { discoverModels, discoverySummary, MODEL_IDS, MODELS, modelStates, setModel } from './catalog.mjs';
6
+ import { ALL_MODELS, cursorSummary, discoverCursor, discoverModels, discoverySummary, JOB_MODELS, laneOf, modelId, MODELS, modelStates, setModel } from './catalog.mjs';
7
+ import { refreshScorecard, scorecardSummary } from './scorecard.mjs';
7
8
  import { getMeta, openDb } from './db.mjs';
8
9
  import { addServer, allowedBy, CLAUDE_CODE_TOKEN, connect, readConfig, removeServer, setAllow } from './mcp.mjs';
9
10
  import { redact } from './redact.mjs';
10
- import { CHAT_EFFORTS, CONFIG_DEFAULTS, config, fetchModel } from './setup.mjs';
11
+ import { CHAT_EFFORTS, CONFIG_DEFAULTS, config, fetchModel, modelUrlOrRefuse } from './setup.mjs';
11
12
 
12
13
  /**
13
14
  * The settings page: every setting Sumo has, on one page, changed with the arrow keys and saved the moment it changes —
@@ -22,7 +23,12 @@ import { CHAT_EFFORTS, CONFIG_DEFAULTS, config, fetchModel } from './setup.mjs';
22
23
  */
23
24
 
24
25
  const ACCENT = '#d77757';
25
- const TONES = { acc: { color: ACCENT }, b: { bold: true }, d: { dimColor: true }, g: { color: 'green' }, r: { color: 'red' }, y: { color: 'yellow' }, gr: { color: 'gray' }, v: { inverse: true } };
26
+ const TONES = { acc: { color: ACCENT }, b: { bold: true }, d: { dimColor: true }, g: { color: 'green' }, r: { color: 'red' }, y: { color: 'yellow' }, gr: { color: 'gray' } };
27
+ /**
28
+ * Reverse video, whatever the colour — the cursor in a field (`v`): NO_COLOR asks for no colour, and chalk, told so,
29
+ * drops every style with it. This is not colour, and is kept, as the chat's box keeps its cursor.
30
+ */
31
+ const reverse = (text) => `\x1b[7m${text}\x1b[27m`;
26
32
  const VALUE_WIDTH = 18;
27
33
  /** A value typed here with no `${NAME}` in it would be stored, and a header is where a token goes. */
28
34
  const NAMED = /\$\{[A-Za-z_][A-Za-z0-9_]*\}/;
@@ -95,7 +101,7 @@ export function claudeCodeServers(cwd = process.cwd(), env = process.env) {
95
101
  const out = [];
96
102
  for (const [name, s] of Object.entries(found)) {
97
103
  if (!s || typeof s !== 'object') continue;
98
- const literal = (values) => Object.values(values ?? {}).some((v) => !NAMED.test(String(v)) && redact(String(v)) !== String(v));
104
+ const literal = (values) => Object.values(values ?? {}).some((v) => !NAMED.test(String(v)) && redact(String(v)).count > 0);
99
105
  if (s.url) {
100
106
  const headers = s.headers && Object.keys(s.headers).length > 0 ? s.headers : null;
101
107
  const why = s.type === 'sse' ? 'an SSE server — Sumo speaks stdio and streamable HTTP' : headers && Object.values(headers).some((v) => !NAMED.test(String(v))) ? "its header holds a token in Claude Code's file — add it here with ${NAME}" : null;
@@ -135,17 +141,17 @@ export function keyName(input, key) {
135
141
  }
136
142
 
137
143
  /**
138
- * The page. `live` is what the chat lends it: `route(args)` switches the chat's model and effort as the page changes
144
+ * The page. `live` is what the chat lends it: `route(args)` switches the chat's model and effort as the page changes, `current()` is the route it is on,
139
145
  * them, `servers()` says how its MCP servers connected, `left()` is the status line's left side. Without it (`sumo
140
- * config`) a change is for the next chat. `toolsOf`, `download`, `discover` and `claude` are the slow or outside parts,
146
+ * config`) a change is for the next chat. `toolsOf`, `download`, `discover`, `cursor`, `score` and `claude` are the slow or outside parts,
141
147
  * passed in so a test can stand in for them.
142
148
  */
143
- export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNames, download = fetchModel, discover = discoverModels, claude = () => claudeCodeServers(cwd), onChange = () => {} } = {}) {
149
+ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNames, download = fetchModel, discover = discoverModels, cursor = discoverCursor, score = refreshScorecard, claude = () => claudeCodeServers(cwd), onChange = () => {} } = {}) {
144
150
  const db = openDb();
145
151
  const values = () => Object.fromEntries(Object.keys(CONFIG_DEFAULTS).map((k) => [k, getMeta(db, `config.${k}`) ?? CONFIG_DEFAULTS[k]]));
146
- const switches = () => Object.fromEntries(MODELS.map((m) => [m, modelStates(db)[m].on]));
152
+ const switches = () => Object.fromEntries(ALL_MODELS.map((m) => [m, modelStates(db)[m].on]));
147
153
  const serversNow = () => Object.entries(readConfig().mcpServers).map(([name, spec]) => ({ name, spec }));
148
- const S = { v: values(), on: switches(), servers: [], view: 'page', f: 0, tf: 0, sv: 0, edit: null, msg: null, ask: null, add: null, tools: null, busy: 0, scroll: {}, log: [], closed: false };
154
+ const S = { v: values(), on: switches(), servers: [], view: 'page', f: 0, tf: 0, sv: 0, edit: null, msg: null, ask: null, add: null, tools: null, busy: 0, scroll: {}, log: [], closed: false, fetching: null };
149
155
  try {
150
156
  S.servers = serversNow();
151
157
  } catch (cause) {
@@ -174,14 +180,14 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
174
180
 
175
181
  const sections = () => [
176
182
  ['Chat', [
177
- { id: 'chat.model', label: 'Model', type: 'choice', opts: () => ['auto', ...MODELS], disabled: (o) => o !== 'auto' && !S.on[o], when: live.route ? 'this chat too' : 'from the next chat', hint: 'the model chats run on — auto: the router picks one each turn' },
183
+ { id: 'chat.model', label: 'Model', type: 'choice', opts: () => ['auto', ...ALL_MODELS], disabled: (o) => o !== 'auto' && !S.on[o], when: live.route ? 'this chat too' : 'from the next chat', hint: 'the model chats run on — auto: the router picks one each turn' },
178
184
  { id: 'chat.effort', label: 'Effort', type: 'choice', opts: () => CHAT_EFFORTS, when: live.route ? 'this chat too' : 'from the next chat', hint: 'how much the model thinks before it acts' },
179
185
  { id: 'chat.budget', label: 'Spend notice', type: 'money', step: 1, unit: 'a session', zero: 'never', when: live.route ? 'this chat too' : '', hint: 'what a chat may cost before the screen says so, once — ←→ by $1 or type it, 0 for never' },
180
186
  { id: 'chat.notify', label: 'Notify', type: 'toggle', when: 'from the next chat', hint: 'the bell and a notification when a long turn ends behind another window' },
181
187
  { id: 'chat.screen', label: 'Screen', type: 'choice', opts: () => ['full', 'still', 'plain'], when: 'from the next chat', hint: 'full: colours flow through the working line · still: nothing moves · plain: for a screen reader' },
182
188
  ]],
183
189
  ['Jobs', [
184
- { id: 'jobs.floor', label: 'Smallest model', type: 'choice', opts: () => MODELS, disabled: (o) => !S.on[o], when: 'jobs from now on', hint: 'the smallest model a job may run on — haiku holds nothing back' },
190
+ { id: 'jobs.floor', label: 'Smallest model', type: 'choice', opts: () => JOB_MODELS, disabled: (o) => !S.on[o], when: 'jobs from now on', hint: 'the smallest model a job may run on — haiku holds nothing back' },
185
191
  { id: 'jobs.budget', label: 'Budget', type: 'money', step: 1, unit: 'a job', zero: 'no cap', when: 'jobs from now on', hint: 'what one job may spend before it is stopped — 0 for no cap' },
186
192
  { id: 'jobs.minutes', label: 'Time limit', type: 'minutes', step: 15, zero: 'no limit', when: 'jobs from now on', hint: 'how long one job may run before it is stopped — ←→ by 15 or type it, 0 for no limit' },
187
193
  { id: 'jobs.review', label: 'Review', type: 'choice', opts: () => ['risky', 'all', 'off'], when: 'jobs from now on', hint: 'which workers that finish DONE are reviewed unasked: the risky ones, every one, or none' },
@@ -197,8 +203,8 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
197
203
  ]],
198
204
  ['Projects', [{ id: 'projects.home', label: 'New ones go in', type: 'path', action: 'enter: change', when: 'at once', hint: 'the folder a new project is made in' }]],
199
205
  ['API models', [
200
- ...MODELS.map((m) => ({ id: `model:${m}`, label: cap(m), type: 'model', m, hint: 'on: offered for chats, jobs and memory · off: never used' })),
201
- { id: 'discover', label: 'Discover', type: 'action', text: 'ask the API which this login can use', action: 'enter', hint: 'sets the switches from the answer — one you turned off stays off' },
206
+ ...ALL_MODELS.map((m) => ({ id: `model:${m}`, label: cap(m), type: 'model', m, hint: laneOf(m) === 'cursor' ? 'on: offered for chats and jobs, run through Cursor with CURSOR_API_KEY · off: never used' : 'on: offered for chats, jobs and memory · off: never used' })),
207
+ { id: 'discover', label: 'Discover', type: 'action', text: 'ask Anthropic and Cursor which this login can use', action: 'enter', hint: 'sets the switches from the answer — one you turned off stays off — and fetches the scorecard routing reads' },
202
208
  ]],
203
209
  ['MCP servers', [
204
210
  ...S.servers.map((s, i) => ({ id: `server:${s.name}`, label: s.name, type: 'server', s, i, action: 'enter: tools', hint: `${s.spec.url ?? [s.spec.command, ...(s.spec.args ?? [])].join(' ')} — enter: its tools, and which run without asking` })),
@@ -285,7 +291,7 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
285
291
  case 'model': {
286
292
  const t = tog(S.on[r.m]);
287
293
  t[0][0] = t[0][0].padEnd(plain ? 3 : 5);
288
- return [...t, [` ${MODEL_IDS[r.m]}`, 'd']];
294
+ return [...t, [` ${modelId(r.m)}`, 'd']];
289
295
  }
290
296
  case 'action':
291
297
  return r.text ? [[r.text, 'd']] : [];
@@ -422,6 +428,7 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
422
428
  function set(r, value) {
423
429
  const old = S.v[r.id];
424
430
  if (old === value) return;
431
+ if (r.id === 'model.url') return fetchFirst(r, old, value);
425
432
  try {
426
433
  config(r.id, value);
427
434
  } catch (cause) {
@@ -430,19 +437,46 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
430
437
  S.v[r.id] = value;
431
438
  S.msg = { ok: `saved · ${r.label.toLowerCase()}: ${show(r, old)} → ${show(r, value)}${r.when ? ` · ${r.when}` : ''}` };
432
439
  if (r.id === 'shell.sandbox' && value === 'off') S.msg = { warn: "saved · sandbox off from the next chat — the model's shell can write anywhere you can" };
440
+ // Only what changed moves in the chat: a model picked there with /model stays when the effort changes, and the
441
+ // effort it is on when the model does (`/model <model>` keeps it). A chat on auto has the router pick its effort.
433
442
  if ((r.id === 'chat.model' || r.id === 'chat.effort') && live.route) {
434
443
  try {
435
- live.route(S.v['chat.model'] === 'auto' ? 'auto' : `${S.v['chat.model']} ${S.v['chat.effort']}`);
444
+ const now = live.current();
445
+ if (r.id === 'chat.model') live.route(value);
446
+ else if (now.model === 'auto') S.msg = { ok: `saved · ${r.label.toLowerCase()}: ${show(r, old)} → ${show(r, value)} · this chat is on auto, which picks the effort each turn` };
447
+ else live.route(`${now.model} ${value}`);
436
448
  } catch (cause) {
437
449
  S.msg = { warn: `saved for new chats; this one did not switch: ${cause.message}` };
438
450
  }
439
451
  }
440
- if (r.id === 'model.url') {
441
- S.msg = { info: `saved · downloading ${show(r, value)}… — the old model is used until it is here` };
442
- later(download(), (said, cause) => {
443
- S.msg = cause ? { err: cause.message } : /not|fail|could/i.test(String(said)) ? { warn: String(said).replace(/^model\s+/, '') } : { ok: String(said ?? 'downloaded').replace(/^model\s+/, '') };
444
- });
452
+ }
453
+ /**
454
+ * A new local model, downloaded first and stored only once it is here (`download` stores it then, and stops the
455
+ * server that ran the old one): until then, and for good if it never comes, the model in use stays in use.
456
+ */
457
+ function fetchFirst(r, old, value) {
458
+ try {
459
+ modelUrlOrRefuse(value);
460
+ } catch (cause) {
461
+ return err(cause.message);
445
462
  }
463
+ S.msg = { info: `downloading ${show(r, value)}… — the model in use stays until it is here` };
464
+ const done = Promise.resolve(download(value)).then(
465
+ (got) => {
466
+ if (got?.ok) S.v[r.id] = value;
467
+ S.fetching = null;
468
+ return got;
469
+ },
470
+ (cause) => {
471
+ S.fetching = null;
472
+ throw cause;
473
+ },
474
+ );
475
+ S.fetching = { from: show(r, old), to: show(r, value), done };
476
+ later(done, (got, cause) => {
477
+ const said = String(cause?.message ?? got?.line ?? '').replace(/^model\s+/, '');
478
+ S.msg = got?.ok ? { ok: `saved · ${r.label.toLowerCase()}: ${show(r, old)} → ${show(r, value)} · ${said}` } : { err: said || 'the download failed' };
479
+ });
446
480
  }
447
481
  function change(r, d) {
448
482
  if (r.type === 'choice') {
@@ -471,7 +505,8 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
471
505
  if (S.on[m]) {
472
506
  const users = [['chat.model', "the chat's model"], ['jobs.floor', "the jobs' smallest model"], ['memory.model', 'the model that files memory']].filter(([k]) => S.v[k] === m);
473
507
  if (users.length > 0) return err(`${m} is ${users[0][1]} — pick another there first`);
474
- if (MODELS.filter((x) => S.on[x]).length === 1) return err('one model stays on: chats and jobs need one');
508
+ // A Claude model stays on: the chat and the memory pass run on nothing else. Cursor's are for jobs, and may all be off.
509
+ if (laneOf(m) === 'claude' && MODELS.filter((x) => S.on[x]).length === 1) return err('one model stays on: chats and jobs need one');
475
510
  }
476
511
  setModel(db, m, !S.on[m]);
477
512
  S.on = switches();
@@ -512,10 +547,18 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
512
547
  return openTools(r);
513
548
  }
514
549
  if (r.id === 'discover') {
515
- S.msg = { info: 'asking the API which models this login can use…' };
516
- later(discover(db), (found, cause) => {
550
+ S.msg = { info: 'asking Anthropic and Cursor which models this login can use, and fetching the scorecard…' };
551
+ // Each is asked on its own: Cursor needs only its key, and the scorecard no login at all. One that fails is said, never thrown.
552
+ const settled = (work) => Promise.resolve().then(work).catch((cause) => ({ ok: false, error: cause.message }));
553
+ later(Promise.all([discover(db), settled(() => cursor(db)), settled(() => score(db))]), (all, cause) => {
517
554
  S.on = switches();
518
- S.msg = cause ? { err: cause.message } : found.ok ? { ok: discoverySummary(found) } : { err: found.error };
555
+ if (cause) {
556
+ S.msg = { err: cause.message };
557
+ return;
558
+ }
559
+ const [found, fromCursor, card] = all;
560
+ const said = `${found.ok ? discoverySummary(found) : found.error} — ${cursorSummary(fromCursor)} — ${scorecardSummary(card)}`;
561
+ S.msg = found.ok && card.ok ? { ok: said } : { err: said };
519
562
  });
520
563
  }
521
564
  if (r.id === 'add') {
@@ -547,7 +590,15 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
547
590
  const stem = full.slice(at + 1);
548
591
  let names = [];
549
592
  try {
550
- names = readdirSync(dir).filter((n) => n.startsWith(stem) && (stem.startsWith('.') || !n.startsWith('.')) && statSync(join(dir, n)).isDirectory());
593
+ // Each name on its own: a link that leads nowhere is no folder, and no reason the others are not.
594
+ const isFolder = (n) => {
595
+ try {
596
+ return statSync(join(dir, n)).isDirectory();
597
+ } catch {
598
+ return false;
599
+ }
600
+ };
601
+ names = readdirSync(dir).filter((n) => n.startsWith(stem) && (stem.startsWith('.') || !n.startsWith('.')) && isFolder(n));
551
602
  } catch {
552
603
  names = [];
553
604
  }
@@ -583,7 +634,7 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
583
634
  if (a.login === 1) {
584
635
  const pairs = wordsOf(cred).map((p) => /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(p));
585
636
  if (pairs.length === 0 || pairs.some((p) => !p)) throw new Error('variables as NAME=${NAME}, one or more');
586
- if (pairs.some((p) => !NAMED.test(p[2]) && redact(p[2]) !== p[2])) throw new Error('a token typed here would be stored — write ${NAME} and keep the token in your shell');
637
+ if (pairs.some((p) => !NAMED.test(p[2]) && redact(p[2]).count > 0)) throw new Error('a token typed here would be stored — write ${NAME} and keep the token in your shell');
587
638
  return { command, args, env: Object.fromEntries(pairs.map((p) => [p[1], p[2]])) };
588
639
  }
589
640
  return { command, args };
@@ -633,8 +684,13 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
633
684
  }
634
685
  function toggleTool(s, t) {
635
686
  const on = allowedBy(s.spec, t);
636
- setAllow(s.name, t, !on, S.tools.names);
637
- S.servers = serversNow();
687
+ // Changed elsewhere meanwhile — removed by another sumo, say — is said, like a remove or an add that fails.
688
+ try {
689
+ setAllow(s.name, t, !on, S.tools.names);
690
+ S.servers = serversNow();
691
+ } catch (cause) {
692
+ return err(cause.message);
693
+ }
638
694
  if (!S.log.includes(`${s.name} allow list`)) S.log.push(`${s.name} allow list`);
639
695
  S.msg = { ok: `${t} ${on ? 'asks first again' : 'runs without asking'}${live.route ? ' · from the next chat' : ''}` };
640
696
  }
@@ -692,8 +748,12 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
692
748
  } else if (k === 'a' || k === 'A') {
693
749
  if (!S.tools?.names?.length) return err('no tools to switch: it has not connected');
694
750
  const every = s.spec.allow === 'all' || S.tools.names.every((t) => allowedBy(s.spec, t));
695
- setAllow(s.name, 'all', !every);
696
- S.servers = serversNow();
751
+ try {
752
+ setAllow(s.name, 'all', !every);
753
+ S.servers = serversNow();
754
+ } catch (cause) {
755
+ return err(cause.message);
756
+ }
697
757
  if (!S.log.includes(`${s.name} allow list`)) S.log.push(`${s.name} allow list`);
698
758
  S.msg = every ? { ok: `every ${s.name} tool asks first again` } : { warn: `every ${s.name} tool runs without asking — new ones it adds too` };
699
759
  } else if (['left', 'right', 'enter', 'space'].includes(k) && r?.type === 'tool') toggleTool(s, r.t);
@@ -823,9 +883,14 @@ export function settingsPage({ live = {}, cwd = process.cwd(), toolsOf = toolNam
823
883
  for (const [, rows] of sections()) {
824
884
  for (const r of rows) if (r.id in snap.v && snap.v[r.id] !== S.v[r.id]) out.push(`${r.label.toLowerCase()} ${show(r, snap.v[r.id])} → ${show(r, S.v[r.id])}`);
825
885
  }
826
- for (const m of MODELS) if (snap.on[m] !== S.on[m]) out.push(`${m} ${S.on[m] ? 'on' : 'off'}`);
886
+ for (const m of ALL_MODELS) if (snap.on[m] !== S.on[m]) out.push(`${m} ${S.on[m] ? 'on' : 'off'}`);
887
+ if (S.fetching) out.push(`local model ${S.fetching.from} → ${S.fetching.to} once it has downloaded — the one in use stays until then`);
827
888
  return [...out, ...S.log];
828
889
  },
890
+ /** A local model still downloading — what it is, and the promise of how it went — or null. */
891
+ get fetching() {
892
+ return S.fetching;
893
+ },
829
894
  get closed() {
830
895
  return S.closed;
831
896
  },
@@ -837,11 +902,15 @@ const toneOf = (cls) => Object.assign({}, ...String(cls ?? '').split(' ').filter
837
902
  /** The page's lines on an Ink screen. */
838
903
  export function SettingsView({ page, columns, rows, plain = false }) {
839
904
  const lines = page.lines({ columns, rows, plain });
840
- return h(Box, { flexDirection: 'column' }, ...lines.map((l, i) => h(Text, { key: i, wrap: 'truncate' }, ...(l.length === 0 ? [' '] : l.map(([t, c], j) => h(Text, { key: j, ...toneOf(c) }, t))))));
905
+ const drawn = (t, c) => (String(c ?? '').split(' ').includes('v') ? reverse(t) : t);
906
+ return h(Box, { flexDirection: 'column' }, ...lines.map((l, i) => h(Text, { key: i, wrap: 'truncate' }, ...(l.length === 0 ? [' '] : l.map(([t, c], j) => h(Text, { key: j, ...toneOf(c) }, drawn(t, c)))))));
841
907
  }
842
908
 
843
- /** `sumo config` at a terminal: the page on its own, until Esc; then what changed, said once. */
844
- export async function runSettings({ stdin = process.stdin, stdout = process.stdout, plain = false } = {}) {
909
+ /**
910
+ * `sumo config` at a terminal: the page on its own, until Esc; then what changed, said once. A local model still
911
+ * downloading is said and waited for — Ctrl-C stops it, and the model in use stays. `options` are the page's.
912
+ */
913
+ export async function runSettings({ stdin = process.stdin, stdout = process.stdout, plain = false, ...options } = {}) {
845
914
  let wake = () => {};
846
915
  function App() {
847
916
  const { exit } = useApp();
@@ -859,10 +928,18 @@ export async function runSettings({ stdin = process.stdin, stdout = process.stdo
859
928
  // One row short of the window: Ink draws a frame that fills it as a full screen, wiping what was above.
860
929
  return h(SettingsView, { page, columns, rows: Math.max(8, rows - 1), plain });
861
930
  }
862
- const page = settingsPage({ onChange: () => wake() });
931
+ const page = settingsPage({ ...options, onChange: () => wake() });
863
932
  const app = render(h(App), { stdin, stdout, exitOnCtrlC: false, interactive: Boolean(stdout.isTTY) });
864
933
  await app.waitUntilExit();
865
- const changed = page.summary();
866
- return changed.length > 0 ? changed.map((c) => `saved: ${c}`) : ['nothing changed'];
934
+ const pending = page.fetching;
935
+ let failed = null;
936
+ if (pending) {
937
+ stdout.write(`still downloading ${pending.to} — the model in use stays until it is here; Ctrl-C stops it and changes nothing\n`);
938
+ const got = await pending.done.catch((cause) => ({ ok: false, line: cause.message }));
939
+ if (!got?.ok) failed = `not saved: local model ${pending.to} — ${String(got?.line ?? '').replace(/^model\s+/, '')}`;
940
+ }
941
+ const changed = page.summary().map((c) => `saved: ${c}`);
942
+ if (failed) changed.push(failed);
943
+ return changed.length > 0 ? changed : ['nothing changed'];
867
944
  }
868
945
 
package/src/setup.mjs CHANGED
@@ -8,8 +8,9 @@ import { basename, delimiter, dirname, isAbsolute, join } from 'node:path';
8
8
  import { pipeline } from 'node:stream/promises';
9
9
  import { Readable, Transform } from 'node:stream';
10
10
  import { anthropicClient, authenticatedRequest, FALLBACK_BETA, resolveAnthropicCredential } from './auth.mjs';
11
- import { assertOn, describeApiError, discoverModels, discoverySummary, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
11
+ import { ALL_MODELS, assertOn, describeApiError, discoverModels, discoverySummary, JOB_MODELS, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
12
12
  import { getMeta, openDb, SCHEMA_VERSION, schemaVersion, setMeta } from './db.mjs';
13
+ import { refreshScorecard, savedScorecard, scorecardSummary } from './scorecard.mjs';
13
14
  import { damaged, restoreHint } from './export.mjs';
14
15
  import { stopLocalServer } from './local-server.mjs';
15
16
  import { latestCrash, crashFiles, logFiles, tailOf } from './logs.mjs';
@@ -149,8 +150,7 @@ function withSource(url, source) {
149
150
  * Streams to `<file>.part` then renames, so a run that dies never leaves a file that looks finished.
150
151
  * Never throws: a model that could not be fetched is not a reason for `sumo setup` to fail.
151
152
  */
152
- async function ensureModel(db) {
153
- const url = modelUrlOf(db);
153
+ async function ensureModel(db, url = modelUrlOf(db)) {
154
154
  const dest = join(paths().models, modelFileOfUrl(url));
155
155
  const shown = redactUrl(url);
156
156
  // Unique per run: two setups at once must not interleave into one file, and a dead run must not leave a
@@ -224,7 +224,7 @@ function sourceOrRefuse(value) {
224
224
  }
225
225
 
226
226
  /** A model's address is a web address of a .gguf file: anything else would fail every later setup. */
227
- function modelUrlOrRefuse(value) {
227
+ export function modelUrlOrRefuse(value) {
228
228
  sourceOrRefuse(value);
229
229
  let path;
230
230
  try {
@@ -237,15 +237,22 @@ function modelUrlOrRefuse(value) {
237
237
  }
238
238
 
239
239
  /**
240
- * The local model fetched again for the address the settings page now holds, and the server that ran the old one
241
- * stopped, so the next local call starts on the new file. Never throws: what happened is the line returned.
240
+ * A local model set on the settings page: downloaded first, and only once its file is here made the one in use — the
241
+ * address stored, and the server that ran the old one stopped, so the next local call starts on the new file. One that
242
+ * does not come, or a download stopped half-way, changes nothing: the model in use stays. `line` says what happened;
243
+ * only an address that is no model's address throws.
242
244
  */
243
- export async function fetchModel() {
245
+ export async function fetchModel(url) {
246
+ modelUrlOrRefuse(url);
244
247
  const db = openDb();
245
248
  try {
246
- const line = await ensureModel(db);
247
- stopLocalServer(db);
248
- return line;
249
+ const line = await ensureModel(db, url);
250
+ const ok = existsSync(join(paths().models, modelFileOfUrl(url)));
251
+ if (ok) {
252
+ setMeta(db, 'config.model.url', url);
253
+ stopLocalServer(db);
254
+ }
255
+ return { ok, line };
249
256
  } finally {
250
257
  db.close();
251
258
  }
@@ -281,6 +288,7 @@ export async function setup({ binDir, link = true, modelSource, noModel = false
281
288
  : await ensureModel(db);
282
289
  }
283
290
  const modelsLine = await checkModels(db);
291
+ const scorecardLine = await checkScorecard(db);
284
292
  db.close();
285
293
 
286
294
  // Written aside and renamed in, so a hook running the launcher mid-setup never reads half a script.
@@ -292,7 +300,7 @@ export async function setup({ binDir, link = true, modelSource, noModel = false
292
300
  const lines = [`home ${p.home}`, `database ${p.db}`, `launcher ${p.launcher}`];
293
301
  // What was skipped is said too: the next step depends on it.
294
302
  lines.push(modelLine ?? `model not fetched (--no-model) — the router needs it: sumo setup`);
295
- lines.push(modelsLine);
303
+ lines.push(modelsLine, scorecardLine);
296
304
  if (!llama) lines.push('llama-server not found — brew install llama.cpp');
297
305
  if (link) {
298
306
  const dir = binDir ?? pickBinDir();
@@ -321,6 +329,16 @@ async function checkModels(db) {
321
329
  return result.ok ? `models ${discoverySummary(result)}` : `models not checked — ${result.error}`;
322
330
  }
323
331
 
332
+ /**
333
+ * The benchmarks routing reads, fetched the first time only, as the models are: Discover, on the settings page, fetches them again.
334
+ * Never throws: a source that cannot be reached is not a reason for `sumo setup` to fail.
335
+ */
336
+ async function checkScorecard(db) {
337
+ const card = savedScorecard(db);
338
+ if (card) return `scorecard fetched ${card.at.slice(0, 10)} — API models › Discover, on the settings page (sumo config), fetches it again`;
339
+ return scorecardSummary(await refreshScorecard(db));
340
+ }
341
+
324
342
  function commandOnPath(name) {
325
343
  for (const dir of (process.env.PATH ?? '').split(delimiter).filter(Boolean)) {
326
344
  const candidate = join(dir, name);
@@ -438,7 +456,8 @@ export async function doctor() {
438
456
  readConfig();
439
457
  const db = openDb();
440
458
  try {
441
- unread = getMeta(db, 'mcp.import_failed');
459
+ // Said while the file is there: deleted, as the advice says, there is nothing left to bring in.
460
+ unread = existsSync(join(p.home, 'mcp.json')) ? getMeta(db, 'mcp.import_failed') : null;
442
461
  } finally {
443
462
  db.close();
444
463
  }
@@ -734,7 +753,7 @@ export function config(key, value) {
734
753
 
735
754
  if (key === 'jobs.advisor' && value !== 'on' && value !== 'off') throw new UsageError('jobs.advisor is on or off: a job asking the next model up for advice, from inside its own request');
736
755
 
737
- if (key === 'jobs.floor' && !['haiku', 'sonnet', 'opus', 'fable'].includes(value)) throw new UsageError('jobs.floor is haiku, sonnet, opus or fable: the smallest model a job may run on — haiku holds nothing back');
756
+ if (key === 'jobs.floor' && !JOB_MODELS.includes(value)) throw new UsageError(`jobs.floor is one of ${JOB_MODELS.join(', ')}: the smallest model a job may run on — haiku holds nothing back`);
738
757
  if (key === 'jobs.review' && !['risky', 'all', 'off'].includes(value)) {
739
758
  throw new UsageError('jobs.review is risky, all or off: which workers that finish DONE, verified, are reviewed without being asked — the ones the router called risky, every one, or none');
740
759
  }
@@ -744,7 +763,7 @@ export function config(key, value) {
744
763
  if (key === 'model.url') modelUrlOrRefuse(value);
745
764
  // A model is one Sumo knows and one that is on; the memory pass also takes `local` and `off`, the chat `auto`.
746
765
  if (key === 'chat.model' && value !== 'auto') {
747
- knownModel(value, ['auto']);
766
+ knownModel(value, ['auto'], ALL_MODELS);
748
767
  assertOn(db, value);
749
768
  }
750
769
  if (key === 'memory.model' && value !== 'local' && value !== 'off') {
package/src/spool.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { appendFileSync, chmodSync, closeSync, fchmodSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
1
+ import { appendFileSync, chmodSync, closeSync, fchmodSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { alive } from './lock.mjs';
4
4
  import { paths } from './paths.mjs';
@@ -19,6 +19,8 @@ import { paths } from './paths.mjs';
19
19
  const WAITING = 'events.jsonl';
20
20
  const BAD = 'events.bad.jsonl';
21
21
  const DRAINING = /^events\.(\d+)\.\d+\.draining$/;
22
+ /** What was set aside, in the hand of a process trying it again or purging it: `events.bad.<pid>.<n>.retrying`. */
23
+ const RETRYING = /^events\.bad\.(\d+)\.\d+\.retrying$/;
22
24
  /** How many times a line may fail to replay before it is set aside. */
23
25
  const TRIES = 3;
24
26
  /** Failures of one line closer together than this are one failure: the hooks of one busy minute are not three tries. */
@@ -72,17 +74,22 @@ const names = () => {
72
74
  }
73
75
  };
74
76
 
77
+ /** The two files a process takes into its own hand by a rename before it reads them: what waits, and what was set aside. */
78
+ const WAITS = { file: WAITING, left: DRAINING, taken: () => `events.${process.pid}.${Date.now()}${claims++}.draining` };
79
+ const ASIDE = { file: BAD, left: RETRYING, taken: () => `events.bad.${process.pid}.${Date.now()}${claims++}.retrying` };
80
+
75
81
  /**
76
- * The files this process may drain now, each taken by a rename only one process can win: what is waiting, and what a
77
- * drain that died left in its hand (its process is gone) — replaying a line twice inserts nothing twice.
82
+ * The files of a kind this process may take now, each by a rename only one process can win: the file itself, and what
83
+ * a process that died left in its hand (its process is gone) — replaying a line twice inserts nothing twice. A file is
84
+ * never read and written back where a writer may be adding to it: a line added meanwhile would be written over.
78
85
  */
79
- function claim() {
86
+ function claim(kind = WAITS) {
80
87
  const dir = paths().spool;
81
88
  const mine = [];
82
89
  for (const name of names()) {
83
- const left = DRAINING.exec(name);
84
- if (name !== WAITING && !(left && (Number(left[1]) === process.pid || !alive(Number(left[1])) || olderThan(join(dir, name), LEFT_AFTER_MS)))) continue;
85
- const taken = join(dir, `events.${process.pid}.${Date.now()}${claims++}.draining`);
90
+ const left = kind.left.exec(name);
91
+ if (name !== kind.file && !(left && (Number(left[1]) === process.pid || !alive(Number(left[1])) || olderThan(join(dir, name), LEFT_AFTER_MS)))) continue;
92
+ const taken = join(dir, kind.taken());
86
93
  try {
87
94
  renameSync(join(dir, name), taken);
88
95
  mine.push(taken);
@@ -195,33 +202,50 @@ export function drainSpool(replay, { within = (fn) => fn(), now = Date.now() } =
195
202
  return done;
196
203
  }
197
204
 
205
+ /**
206
+ * Each line of a file this process took, in order, then the file let go — read again until nothing new comes: a
207
+ * writer that had it open when it was taken may still add its line. What is left with no line end, when nothing more
208
+ * comes, is a line cut off as it was written, handed on as it is.
209
+ */
210
+ function eachLine(file, fn) {
211
+ for (let read = 0; ; ) {
212
+ const text = readFileSync(file, 'utf8');
213
+ const end = text.lastIndexOf('\n') + 1;
214
+ if (end > read) {
215
+ for (const raw of text.slice(read, end).split('\n')) if (raw.trim()) fn(raw);
216
+ read = end;
217
+ continue;
218
+ }
219
+ if (statSync(file).size > Buffer.byteLength(text)) continue;
220
+ if (text.slice(end).trim()) fn(text.slice(end));
221
+ rmSync(file, { force: true });
222
+ return;
223
+ }
224
+ }
225
+
198
226
  /**
199
227
  * `sumo memory check --fix`: every line set aside tried once more, each as its own piece of work — a turn set aside
200
- * while the database was busy goes in now. What replays leaves `events.bad.jsonl`; what still cannot — a line that
201
- * was never an event — stays as it was. Returns `{ replayed, left }`.
228
+ * while the database was busy goes in now. What was set aside is taken by a rename first, so a line a hook sets aside
229
+ * meanwhile starts a file of its own; what still cannot replay — a line that was never an event — goes back as it was.
230
+ * Returns `{ replayed, left }`.
202
231
  */
203
232
  export function retrySetAside(replay, { within = (fn) => fn() } = {}) {
204
- const file = join(paths().spool, BAD);
205
- let lines;
206
- try {
207
- lines = readFileSync(file, 'utf8').split('\n').filter((l) => l.trim());
208
- } catch (cause) {
209
- if (cause.code === 'ENOENT') return { replayed: 0, left: 0 };
210
- throw cause;
233
+ const done = { replayed: 0, left: 0 };
234
+ for (const file of claim(ASIDE)) {
235
+ eachLine(file, (raw) => {
236
+ let entry = null;
237
+ try {
238
+ entry = JSON.parse(JSON.parse(raw).line);
239
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) throw new Error('not an event');
240
+ within(() => replay(entry));
241
+ done.replayed++;
242
+ } catch {
243
+ addLine(BAD, raw);
244
+ done.left++;
245
+ }
246
+ });
211
247
  }
212
- const left = lines.filter((raw) => {
213
- try {
214
- const entry = JSON.parse(JSON.parse(raw).line);
215
- if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return true;
216
- within(() => replay(entry));
217
- return false;
218
- } catch {
219
- return true;
220
- }
221
- });
222
- if (left.length === 0) rmSync(file, { force: true });
223
- else if (left.length < lines.length) writeFileSync(file, `${left.join('\n')}\n`, { mode: 0o600 });
224
- return { replayed: lines.length - left.length, left: left.length };
248
+ return done;
225
249
  }
226
250
 
227
251
  /** Every string a line of the spool holds: its own, and those of a line kept inside it (`events.bad.jsonl` keeps the line it set aside). */
@@ -237,20 +261,19 @@ function saidIn(raw) {
237
261
  }
238
262
 
239
263
  /**
240
- * A purge reaches the spool too: every line — waiting, in a drain's hand, or set aside — whose words `says` holds is
241
- * taken out, the rest left as they were. Returns how many lines went.
264
+ * A purge reaches the spool too: every line — waiting, set aside, or left by a process that died — whose words `says`
265
+ * holds is taken out, the rest put back as they were, each file taken by a rename first so a line a hook adds meanwhile
266
+ * is never written over. A file in a living process's hand is that process's. Returns how many lines went.
242
267
  */
243
268
  export function purgeSpool(says) {
244
269
  let gone = 0;
245
- for (const name of names()) {
246
- if (name !== WAITING && name !== BAD && !DRAINING.test(name)) continue;
247
- const file = join(paths().spool, name);
248
- const lines = readFileSync(file, 'utf8').split('\n');
249
- const kept = lines.filter((l) => !l.trim() || !saidIn(l).some(says));
250
- if (kept.length === lines.length) continue;
251
- gone += lines.length - kept.length;
252
- if (kept.some((l) => l.trim())) writeFileSync(file, kept.join('\n'), { mode: 0o600 });
253
- else rmSync(file, { force: true });
270
+ for (const kind of [WAITS, ASIDE]) {
271
+ for (const file of claim(kind)) {
272
+ eachLine(file, (raw) => {
273
+ if (saidIn(raw).some(says)) gone++;
274
+ else addLine(kind.file, raw);
275
+ });
276
+ }
254
277
  }
255
278
  return gone;
256
279
  }
@@ -299,10 +322,12 @@ const linesIn = (name) => {
299
322
 
300
323
  /**
301
324
  * How the spool stands, for whoever checks memory's health: `waiting`, the events not yet in the journal (what waits,
302
- * and what a drain has in its hand or died holding), and `bad`, the lines set aside in `events.bad.jsonl` because
303
- * they were not events or failed to replay three times, a minute or more apart. Both are 0 when there has never been a spool.
325
+ * and what a drain has in its hand or died holding), and `bad`, the lines set aside in `events.bad.jsonl` (or in the
326
+ * hand of a retry) because they were not events or failed to replay three times, a minute or more apart. Both are 0
327
+ * when there has never been a spool.
304
328
  */
305
329
  export function spoolStatus() {
306
330
  const all = names();
307
- return { waiting: all.filter((n) => n === WAITING || DRAINING.test(n)).reduce((n, name) => n + linesIn(name), 0), bad: all.includes(BAD) ? linesIn(BAD) : 0 };
331
+ const count = (kind) => all.filter((n) => n === kind.file || kind.left.test(n)).reduce((n, name) => n + linesIn(name), 0);
332
+ return { waiting: count(WAITS), bad: count(ASIDE) };
308
333
  }