sumo-agents 0.3.0 → 0.5.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.
Files changed (52) hide show
  1. package/AGENTS.md +8 -6
  2. package/CHANGELOG.md +155 -0
  3. package/README.md +82 -19
  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 +289 -89
  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 +113 -11
  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 +9 -4
  26. package/src/hooks.mjs +35 -19
  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 +94 -12
  40. package/src/redact.mjs +10 -0
  41. package/src/render.mjs +2 -1
  42. package/src/route.mjs +39 -25
  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 +49 -13
  47. package/src/setup.mjs +425 -76
  48. package/src/text.mjs +100 -1
  49. package/src/tools.mjs +258 -21
  50. package/src/tty.mjs +481 -105
  51. package/src/ui.mjs +1205 -276
  52. package/src/verify.mjs +17 -1
package/src/jobs.mjs CHANGED
@@ -8,7 +8,7 @@ import { usableModels } from './catalog.mjs';
8
8
  import { getMeta, setMeta, tx } from './db.mjs';
9
9
  import { autoGuides, guideBody, matchGuide, WORKER_GUIDES } from './guides.mjs';
10
10
  import { heldBy } from './lock.mjs';
11
- import { UsageError } from './memory.mjs';
11
+ import { Refusal, UsageError } from './memory.mjs';
12
12
  import { callModel, failure, logRun } from './model.mjs';
13
13
  import { paths } from './paths.mjs';
14
14
  import { preferences } from './prime.mjs';
@@ -40,7 +40,7 @@ export function parseJobId(raw) {
40
40
 
41
41
  export function getJob(db, id) {
42
42
  const job = db.prepare('SELECT * FROM jobs WHERE id = ?').get(id);
43
- if (!job) throw new UsageError(`no job j${id}`);
43
+ if (!job) throw new Refusal(`no job j${id}`);
44
44
  return job;
45
45
  }
46
46
 
@@ -77,7 +77,7 @@ function setOpenStatus(db, id, status, now) {
77
77
  }
78
78
 
79
79
  function requireOpen(job, verb) {
80
- if (job.status !== 'running' && job.status !== 'needs_input') throw new UsageError(`j${job.id} is ${job.status} — it cannot be ${verb}`);
80
+ if (job.status !== 'running' && job.status !== 'needs_input') throw new Refusal(`j${job.id} is ${job.status} — it cannot be ${verb}`);
81
81
  }
82
82
 
83
83
  const ROLE = {
@@ -208,7 +208,7 @@ function whereThingsStand(root) {
208
208
  function renderBrief({ job, project, known, rules, situation, task, guide, change, options = {} }) {
209
209
  const closing =
210
210
  job.agent === 'worker'
211
- ? `\`finish\` with DONE (or FAILED). DONE is not taken on your word: the first is answered with the task beside your change, to look over once more; the next has the project's checks run and compared with the baseline, and is refused while something new fails. See the verdict first with \`verify\` (allow it ten minutes). The report:`
211
+ ? `\`finish\` with DONE (or FAILED). DONE is not taken on your word: the first is answered with the task as a numbered list beside your change, each number to answer once; the next has the project's checks run and compared with the baseline, and is refused while something new fails. See the verdict first with \`verify\` (allow it ten minutes). The report:`
212
212
  : `\`finish\` with DONE (or FAILED), and this report:`;
213
213
  return `# Job j${job.id} — ${job.title}
214
214
 
@@ -256,7 +256,7 @@ const writeState = (db, id, state) => {
256
256
  };
257
257
 
258
258
  /** The written way of doing one kind of work, without its title — the brief has its own heading for it. */
259
- export function guideFor(name) {
259
+ function guideFor(name) {
260
260
  if (!GUIDES.includes(name)) throw new UsageError(`--guide is one of: ${[...GUIDES, 'none'].join(', ')}`);
261
261
  const text = guideBody(name);
262
262
  if (!text) throw new UsageError(`guides/${name}.md is missing or empty`);
@@ -265,11 +265,11 @@ export function guideFor(name) {
265
265
 
266
266
  /**
267
267
  * The guide a job carries when none is named: a reviewer always review; a worker the one its title and goal ask
268
- * for, the way the chat is handed one by the user's words — unless the switch is off. A scout changes nothing.
268
+ * for, the way the chat is handed one by the user's words — unless the coding eval switched guides off. A scout changes nothing.
269
269
  */
270
- function defaultGuide(db, { agent, title, task }) {
270
+ function defaultGuide({ agent, title, task }) {
271
271
  if (agent === 'reviewer') return 'review';
272
- if (agent !== 'worker' || !autoGuides(db)) return null;
272
+ if (agent !== 'worker' || !autoGuides()) return null;
273
273
  return matchGuide(`${title}\n${task.split(/\n(?=## )/)[0]}`, WORKER_GUIDES);
274
274
  }
275
275
 
@@ -285,29 +285,31 @@ function changeToJudge(db, { project, reviews }) {
285
285
  let kept = null;
286
286
  if (reviews !== undefined) {
287
287
  const target = getJob(db, reviews);
288
- if (target.project !== project.slug) throw new UsageError(`j${target.id} belongs to ${target.project}, not ${project.slug}`);
288
+ if (target.project !== project.slug) throw new Refusal(`j${target.id} belongs to ${target.project}, not ${project.slug}`);
289
289
  const state = readState(db, target.id);
290
290
  snap = state?.snap;
291
291
  // A change still in the worktree it was made in is judged there.
292
292
  if (state?.tree && existsSync(state.tree.path)) root = state.tree.path;
293
- if (!snap) throw new UsageError(`j${target.id} recorded no starting point (not a worker job, or not a git repository) — there is no change to hand over`);
293
+ if (!snap) throw new Refusal(`j${target.id} recorded no starting point (not a worker job, or not a git repository) — there is no change to hand over`);
294
294
  // One whose worktree is gone is judged by what it kept as it went (`changes`), not by the project against the tree's start:
295
295
  // the project holds that change and more — what git ignores, what was never added — none of it the job's.
296
296
  if (state.tree && !existsSync(state.tree.path)) kept = changes(db, target.id);
297
297
  lines.push(`What was asked: ${fileOf(target.id, 'brief.md')} (read "## The task")`);
298
+ // The task as the author was asked to answer it: the reviewer gives a verdict on each number under "Asked vs built".
299
+ lines.push('Asked, by number — a verdict on each under "Asked vs built", with what in the change shows it:', ...checklistOf(taskFromBrief(readOr(fileOf(target.id, 'brief.md')))).map((item, i) => ` ${i + 1}. ${item}`));
298
300
  if (existsSync(fileOf(target.id, 'report.md'))) {
299
301
  lines.push(`What its author says they did: ${fileOf(target.id, 'report.md')} — claims, not facts. A stated reason never makes a finding smaller.`);
300
302
  }
301
303
  } else {
302
- if (!hasCommit(project.path)) throw new UsageError(`${project.path} is not a git repository with a commit — there is no change to hand over`);
304
+ if (!hasCommit(project.path)) throw new Refusal(`${project.path} is not a git repository with a commit — there is no change to hand over`);
303
305
  snap = { base: 'HEAD', mixed: false, untracked: [] };
304
306
  }
305
307
  if (kept) {
306
- if (kept.files === 0) throw new UsageError('nothing has changed — there is nothing to review');
308
+ if (kept.files === 0) throw new Refusal('nothing has changed — there is nothing to review');
307
309
  return { snap, lines, root, kept };
308
310
  }
309
311
  const { tracked, created } = changesSince(root, snap);
310
- if (tracked.length + created.length === 0) throw new UsageError('nothing has changed — there is nothing to review');
312
+ if (tracked.length + created.length === 0) throw new Refusal('nothing has changed — there is nothing to review');
311
313
  return { snap, lines, root };
312
314
  }
313
315
 
@@ -371,7 +373,7 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
371
373
  if (reviews !== undefined && agent !== 'reviewer') throw new UsageError('--reviews goes with --agent reviewer');
372
374
  const project = getProject(db, nameOrAlias);
373
375
  // Everything that can refuse the job is settled before the job exists.
374
- const chosen = guideName === 'none' ? null : (guideName ?? defaultGuide(db, { agent, title, task }));
376
+ const chosen = guideName === 'none' ? null : (guideName ?? defaultGuide({ agent, title, task }));
375
377
  const guide = chosen ? guideFor(chosen) : null;
376
378
  const judged = agent === 'reviewer' ? changeToJudge(db, { project, reviews }) : null;
377
379
 
@@ -473,7 +475,7 @@ async function newAttempts(db, { attempts, title, ...options }) {
473
475
  if (options.agent !== 'worker') throw new UsageError('--attempts goes with a worker');
474
476
  if (!title?.trim()) throw new UsageError('a job needs a --title');
475
477
  const project = getProject(db, options.project);
476
- if (!hasCommit(project.path)) throw new UsageError(`${project.path} is not a git repository with a commit — each attempt needs a worktree of its own`);
478
+ if (!hasCommit(project.path)) throw new Refusal(`${project.path} is not a git repository with a commit — each attempt needs a worktree of its own`);
477
479
  const made = [];
478
480
  try {
479
481
  for (let n = 1; n <= attempts; n++) {
@@ -507,12 +509,12 @@ export async function retry(db, id, { now = new Date().toISOString() } = {}) {
507
509
  const old = getJob(db, id);
508
510
  const eligible = old.status === 'failed' || (old.status === 'done' && (old.important ?? 0) >= RETRY_IMPORTANT_THRESHOLD);
509
511
  if (!eligible) {
510
- throw new UsageError(
512
+ throw new Refusal(
511
513
  `j${id} is ${old.status}${old.status === 'done' ? ` (important: ${old.important ?? 0})` : ''} — only a failed job, or a done one reviewed with important >= ${RETRY_IMPORTANT_THRESHOLD}, can be retried`,
512
514
  );
513
515
  }
514
516
  const task = taskFromBrief(readOr(fileOf(id, 'brief.md')));
515
- if (!task) throw new UsageError(`j${id}'s brief has no "## The task" section to retry from`);
517
+ if (!task) throw new Refusal(`j${id}'s brief has no "## The task" section to retry from`);
516
518
 
517
519
  // What the original was allowed and pointed at travels with the task: a retry of a review judges the same job,
518
520
  // and a retry of a worker keeps the permission the first attempt was given.
@@ -583,7 +585,7 @@ none: a review changes nothing — read the change, what was asked, and the code
583
585
 
584
586
  /**
585
587
  * What follows a worker's run without being asked, once the worker has closed. A first attempt that FAILED is retried
586
- * once, on a route one step up (`retry`; `jobs.escalate`) — a retry is never retried again this way, nor one sumo told to
588
+ * once, on a route one step up (`retry`) — a retry is never retried again this way, nor one sumo told to
587
589
  * close near its budget, time or turns, and one of several `attempts` is settled with its siblings instead. One that closed
588
590
  * DONE, verified by the checks — in git, so there is a change to hand over — is reviewed when the
589
591
  * router called its work risky, or always (`jobs.review`), by a reviewer at least as strong as it (`--reviews`). The job
@@ -593,7 +595,7 @@ async function followUp(db, id, { now = new Date().toISOString() } = {}) {
593
595
  const job = getJob(db, id);
594
596
  if (job.agent !== 'worker' || attemptsOf(db, id) !== null) return null;
595
597
  if (job.status === 'failed') {
596
- if (job.retry_of !== null || setting(db, 'jobs.escalate') !== 'on') return null;
598
+ if (job.retry_of !== null) return null;
597
599
  // Told by sumo to close near one of its limits, it closed where it stood: a run one step up with that limit fresh would only buy
598
600
  // the limit off at a dearer price, and that is the user's call.
599
601
  const near = CLOSED_NEAR[getMeta(db, closingKey(id))];
@@ -687,9 +689,9 @@ function checkable(db, id, verb) {
687
689
  const job = getJob(db, id);
688
690
  requireOpen(job, verb);
689
691
  const state = readState(db, id);
690
- if (job.agent !== 'worker' || !state) throw new UsageError(`j${id} is not a worker job with a recorded start — there is nothing to ${verb === 'verified' ? 'verify' : 'take a baseline of'}`);
692
+ if (job.agent !== 'worker' || !state) throw new Refusal(`j${id} is not a worker job with a recorded start — there is nothing to ${verb === 'verified' ? 'verify' : 'take a baseline of'}`);
691
693
  const root = workdir(state, getProject(db, job.project));
692
- if (!existsSync(root)) throw new UsageError(`${root} no longer exists`);
694
+ if (!existsSync(root)) throw new Refusal(`${root} no longer exists`);
693
695
  return { job, state, root };
694
696
  }
695
697
 
@@ -723,7 +725,7 @@ export function verifyJob(db, id, now = new Date().toISOString()) {
723
725
  export function changes(db, id) {
724
726
  const job = getJob(db, id);
725
727
  const state = readState(db, id);
726
- if (!state?.snap) throw new UsageError(`j${id} recorded no starting point (not a worker job, or not a git repository)`);
728
+ if (!state?.snap) throw new Refusal(`j${id} recorded no starting point (not a worker job, or not a git repository)`);
727
729
  const root = workdir(state, getProject(db, job.project));
728
730
  // A worktree that is gone wrote down its change as it went: that is kept, not overwritten from a project that has moved on.
729
731
  if (state.tree && !existsSync(root) && existsSync(fileOf(id, 'changes.diff'))) {
@@ -820,6 +822,24 @@ export function startLive(id) {
820
822
  export const appendLive = (id, text) => appendFileSync(liveFile(id), text, { mode: 0o600 });
821
823
  export const endLive = (id, lines) => writeFileSync(endFile(id), `${lines.join('\n')}\n`, { mode: 0o600 });
822
824
 
825
+ /**
826
+ * The tools a job used that read the world outside the machine — a web search, a fetch, an MCP server — one name a
827
+ * line, each once. What such a run says it learned may be what a page told it, so it is not filed as a fact (see finish).
828
+ */
829
+ const outsideFile = (id) => fileOf(id, 'outside');
830
+ export function noteOutsideRead(id, tool) {
831
+ const name = String(tool).replace(/\s+/g, ' ');
832
+ if (outsideReads(id).includes(name)) return;
833
+ appendFileSync(outsideFile(id), `${name}\n`, { mode: 0o600 });
834
+ }
835
+ export function outsideReads(id) {
836
+ try {
837
+ return readFileSync(outsideFile(id), 'utf8').split('\n').filter(Boolean);
838
+ } catch {
839
+ return [];
840
+ }
841
+ }
842
+
823
843
  /** What was told to the job since it last looked, taken off the disk so it is read once. A file that is not a message is passed over. */
824
844
  export function takeInbox(id) {
825
845
  let names;
@@ -843,7 +863,7 @@ export function takeInbox(id) {
843
863
 
844
864
  export function answer(db, id, text, now = new Date().toISOString()) {
845
865
  const job = getJob(db, id);
846
- if (job.status !== 'needs_input') throw new UsageError(`j${id} is ${job.status} — it is not waiting for an answer`);
866
+ if (job.status !== 'needs_input') throw new Refusal(`j${id} is ${job.status} — it is not waiting for an answer`);
847
867
  if (!text.trim()) throw new UsageError('the answer is empty — put it on stdin');
848
868
  append(id, 'qa.md', 'Answer', text, now);
849
869
  setOpenStatus(db, id, 'running', now);
@@ -892,10 +912,48 @@ const checkedFile = (id) => fileOf(id, 'checked');
892
912
  /** How many changed files, and how many new ones, the check names before it counts the rest. */
893
913
  const CHECK_LISTED = 30;
894
914
 
915
+ /** The most lines of one section of the task that become items of their own; past it, the rest is one item. */
916
+ const ITEMS_PER_SECTION = 12;
917
+
918
+ /** The lines under one `## heading` of a task, bullets and numbers stripped, until the next heading. */
919
+ function sectionLines(task, heading) {
920
+ const lines = String(task ?? '').split('\n');
921
+ const at = lines.findIndex((l) => l.trim().toLowerCase() === `## ${heading}`.toLowerCase());
922
+ if (at === -1) return [];
923
+ const body = [];
924
+ for (const line of lines.slice(at + 1)) {
925
+ if (/^##\s/.test(line)) break;
926
+ const said = line.replace(/^\s*(?:[-*•]|\d+[.)])\s*/, '').trim();
927
+ if (said && !/^none\.?$/i.test(said)) body.push(said);
928
+ }
929
+ return body;
930
+ }
931
+
932
+ /**
933
+ * The task as a numbered list, one line each — what was asked, what must not change, what must not be attempted, and the
934
+ * check that proves it — for an author or a reviewer to answer by number. A checklist drawn from the task is answered;
935
+ * "check your work" is not: measured on long tasks, an itemized list caught what a general self-check let through. A task
936
+ * without those headings is one item: the whole of it.
937
+ */
938
+ export function checklistOf(task) {
939
+ const items = [];
940
+ const take = (heading, say) => {
941
+ const lines = sectionLines(task, heading);
942
+ for (const line of lines.slice(0, ITEMS_PER_SECTION)) items.push(say(line));
943
+ if (lines.length > ITEMS_PER_SECTION) items.push(`and the ${lines.length - ITEMS_PER_SECTION} more lines under ${heading}`);
944
+ };
945
+ take('Goal', (l) => `Done: ${l}`);
946
+ take('Must not change', (l) => `Unchanged: ${l}`);
947
+ take('Non-goals', (l) => `Not attempted: ${l}`);
948
+ take('Check', (l) => `Ran, and it passed: ${l}`);
949
+ if (items.length === 0) items.push('Done: everything the task asks, as it asks it.');
950
+ return items;
951
+ }
952
+
895
953
  /**
896
954
  * What a worker's first DONE is answered with instead of being taken: the task as it was given, what has changed since
897
- * the job began, and what to look for — the first thing a reviewer does, done by the author while it can still fix
898
- * what it finds. Nothing is run here; the checks come with the next DONE.
955
+ * the job began, and the task as a list to answer by number — the first thing a reviewer does, done by the author while
956
+ * it can still fix what it finds. Nothing is run here; the checks come with the next DONE.
899
957
  */
900
958
  function checkAgainstTask(db, job, state) {
901
959
  // Where the job works: its worktree when it was given one, the project otherwise.
@@ -913,20 +971,21 @@ function checkAgainstTask(db, job, state) {
913
971
  change = ['what changed could not be read from git — look over it yourself'];
914
972
  }
915
973
  }
974
+ const task = taskFromBrief(readOr(fileOf(job.id, 'brief.md')));
975
+ // Asked to hunt for the bug, an author judges its own work closest (arXiv 2602.06948: told to find what breaks it,
976
+ // not whether it works); and nothing that was in the tree before the job is the job's to undo.
977
+ const items = [...checklistOf(task), 'Debug output and scratch files are removed.', 'Tests were changed only if the task allows it.', 'Every claim in the report is backed by a tool result from this run.', 'The input most likely to break this change is named, and was run.', 'No change you did not make was reverted or overwritten.'];
916
978
  return [
917
979
  `j${job.id} is not closed yet — before DONE counts, check the work against the task. Nothing was run.`,
918
980
  '',
919
981
  '## The task',
920
- taskFromBrief(readOr(fileOf(job.id, 'brief.md'))) || '(the brief has no "## The task" section)',
982
+ task || '(the brief has no "## The task" section)',
921
983
  '',
922
984
  '## What changed since the job began',
923
985
  ...change,
924
986
  '',
925
- '## Check each, and fix what is not so',
926
- '- Every requirement in the task is met.',
927
- '- Debug output and scratch files are removed.',
928
- '- Tests were changed only if the task allows it.',
929
- '- Every claim in the report is backed by a tool result from this run.',
987
+ '## Answer each by number — what in the change or a tool result shows it — and fix what is not so',
988
+ ...items.map((item, i) => `${i + 1}. ${item}`),
930
989
  '',
931
990
  'Then call `finish` again with the same report, or a corrected one, to go on to verification. This is asked once.',
932
991
  ].join('\n');
@@ -948,21 +1007,21 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
948
1007
  // A DONE that cannot be checked is refused, not waved through; --accept is the way to take it anyway, and it is written down.
949
1008
  const graded = job.agent === 'worker' && status === 'DONE';
950
1009
  const state = graded ? readState(db, id) : null;
951
- const unchecked = (why) => new UsageError(`j${id} cannot be verified — ${why}. To take the work as it stands: sumo job finish ${id} --status DONE --accept "<why>"`);
1010
+ const unchecked = (why) => new Refusal(`j${id} cannot be verified — ${why}. To take the work as it stands: sumo job finish ${id} --status DONE --accept "<why>"`);
952
1011
  if (graded && accept === undefined && !state) throw unchecked(`the record of where it began (${fileOf(id, 'verify.json')}) cannot be read, or was changed since sumo wrote it`);
953
1012
  // Before the checks are run, the author looks once at the task beside its change: the first DONE is answered with that, not taken.
954
1013
  // Kept in the job's folder, so a run started again goes on to the checks instead of being asked again.
955
1014
  if (graded && accept === undefined && !existsSync(checkedFile(id))) {
956
1015
  const asked = checkAgainstTask(db, job, state);
957
1016
  writeFileSync(checkedFile(id), `${now}\n`, { mode: 0o600 });
958
- throw new UsageError(asked);
1017
+ throw new Refusal(asked);
959
1018
  }
960
1019
  const verdict = state && accept === undefined ? verdictForDone(db, id, state, now) : null;
961
1020
  // The checks can take minutes: report a job closed in the meantime before interpreting their verdict.
962
1021
  requireOpen(getJob(db, id), 'finished');
963
1022
  if (graded && accept === undefined && !verdict) throw unchecked(`${getProject(db, job.project).path} no longer exists, so its checks cannot be run`);
964
1023
  if (verdict && !verdict.ok) {
965
- throw new UsageError(
1024
+ throw new Refusal(
966
1025
  [
967
1026
  `j${id} is not done — the project's checks were run, and:`,
968
1027
  ...verdictLines(verdict).map((l) => ` ${l}`),
@@ -994,6 +1053,16 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
994
1053
  const kept = !isScratch(getProject(db, job.project));
995
1054
  const ops = kept ? learnedLines(report).map((body) => ({ op: 'gotcha', scope: `project:${job.project}`, body: clip(body, 300) })) : [];
996
1055
  const learned = applyOps(db, ops, { source: 'worker', turns: new Map(), now });
1056
+ // A run that read a page or an MCP server may be passing on what it was told: what it learned waits for the user to
1057
+ // confirm (arXiv 2503.03704 — memory written from injected content is read later as trusted). Exactly what this
1058
+ // finish wrote is held: another job finishing in the same project at the same moment keeps its own.
1059
+ const outside = outsideReads(id);
1060
+ if (outside.length > 0 && learned.written.length > 0) {
1061
+ const held = db
1062
+ .prepare(`UPDATE memories SET state = 'unconfirmed', provenance = 'inferred', held_for = 'outside' WHERE id IN (${learned.written.map(() => '?').join(', ')}) AND type = 'gotcha' AND state = 'active' RETURNING id`)
1063
+ .all(...learned.written);
1064
+ if (held.length > 0) learned.applied = learned.applied.map((said) => `${said} — held for the user to confirm: this run read ${outside.join(', ')}`);
1065
+ }
997
1066
 
998
1067
  const summary = /^##\s*Summary\s*\n+([^\n]+)/im.exec(report)?.[1]?.trim();
999
1068
  if (kept) {
@@ -1020,11 +1089,21 @@ export function listJobs(db, { all = false } = {}) {
1020
1089
  const where = all ? '' : `WHERE status IN ('running', 'needs_input')`;
1021
1090
  const rows = db.prepare(`SELECT * FROM jobs ${where} ORDER BY id DESC LIMIT ${LISTED}`).all();
1022
1091
  const total = db.prepare(`SELECT COUNT(*) AS n FROM jobs ${where}`).get().n;
1023
- return Object.assign(rows, { more: total - rows.length });
1092
+ return Object.assign(rows.map((row) => ({ ...row, shown: shownStatus(db, row) })), { more: total - rows.length });
1093
+ }
1094
+
1095
+ /**
1096
+ * A job's state as a person reads it; the stored status is unchanged. Open, it is running only while a run holds it,
1097
+ * not started if no run ever sent a request for it, and stopped if a stop — budget, time, Esc — left it open.
1098
+ */
1099
+ function shownStatus(db, job) {
1100
+ if (job.status !== 'running') return job.status;
1101
+ if (atWork(job.id)) return 'running';
1102
+ return db.prepare('SELECT 1 FROM model_runs WHERE job_id = ? LIMIT 1').get(job.id) ? 'stopped' : 'not started';
1024
1103
  }
1025
1104
 
1026
- export function jobLine(job) {
1027
- return `j${job.id} [${job.agent}·${job.project}·${job.status}] ${job.title}`;
1105
+ export function jobLine(job, status = job.status) {
1106
+ return `j${job.id} [${job.agent}·${job.project}·${status}] ${job.title}`;
1028
1107
  }
1029
1108
 
1030
1109
  /** The report a job closed with, as its sub-agent wrote it; empty while it has none. */
@@ -1033,7 +1112,7 @@ export const reportOf = (id) => readOr(fileOf(id, 'report.md')).trim();
1033
1112
  /** What the main agent needs to decide its next move — not the whole report. */
1034
1113
  export function show(db, id) {
1035
1114
  const job = getJob(db, id);
1036
- const out = [jobLine(job), `files: ${dirOf(id)}`];
1115
+ const out = [jobLine(job, shownStatus(db, job)), `files: ${dirOf(id)}`];
1037
1116
  if (job.model && job.effort) out.push(`route: ${job.model}/${job.effort} — ${job.route_reason}`);
1038
1117
  if (job.important !== null && job.important !== undefined) out.push(`important: ${job.important}`);
1039
1118
  const qa = readOr(fileOf(id, 'qa.md')).trim();
@@ -1052,7 +1131,7 @@ const pickedKey = (first) => `job.${first}.picked`;
1052
1131
  const firstTo = (db, key, now) => tx(db, () => getMeta(db, key) === null && (setMeta(db, key, now), true));
1053
1132
 
1054
1133
  /** The jobs one `--attempts` made, in order; null for a job that is not one of them. */
1055
- export function attemptsOf(db, id) {
1134
+ function attemptsOf(db, id) {
1056
1135
  const ids = getMeta(db, attemptsKey(id));
1057
1136
  return ids === null ? null : JSON.parse(ids);
1058
1137
  }
package/src/jobtools.mjs CHANGED
@@ -21,7 +21,7 @@ const BASELINE = tool('baseline', "Before your first edit: run the project's che
21
21
  const VERIFY = tool('verify', "Run the project's checks now and see the verdict DONE will be given. Takes minutes.");
22
22
  const FINISH = tool(
23
23
  'finish',
24
- "Close the job — the last thing you do, or nobody knows it ended. A worker's first DONE is answered with the task beside its change, to look over once; the next runs the project's checks and is refused while something new fails.",
24
+ "Close the job — the last thing you do, or nobody knows it ended. A worker's first DONE is answered with the task as a numbered list beside its change, each number to answer once; the next runs the project's checks and is refused while something new fails.",
25
25
  // No way to take the work unverified: that is for the user or the main agent to grant, never the author.
26
26
  { status: { type: 'string', enum: ['DONE', 'FAILED'] }, report: text('the report, in the shape the brief gives') },
27
27
  );
@@ -70,9 +70,11 @@ export async function runJobTool(call, { job, ctx, signal = null }) {
70
70
  try {
71
71
  const args = file ? [...command.args, '--from-file', file] : command.args;
72
72
  // The checks give each command its own limit; anything else is quick. They run the project's code — tests the model may have
73
- // written among it — so they run in the sandbox its shell is in, writing only where it may: Sumo's home is one of those places,
74
- // so the records are still written. A check that fails only for the sandbox fails so at the baseline and at the end alike.
75
- const run = await runCommand(sumo(args), { cwd: ctx.cwd, env: ctx.env, signal, timeoutMs: command.checks ? null : 60_000, wrap: command.checks ? checksWrap(ctx) : null });
73
+ // written among it — so each check runs in the sandbox the job's shell is in, handed down to sumo, which itself runs outside
74
+ // it: sumo's memory is read-only to the sandbox, and the verdict is recorded there. A check that fails only for the sandbox
75
+ // fails so at the baseline and at the end alike.
76
+ const env = command.checks ? { ...ctx.env, SUMO_CHECKS_WRAP: JSON.stringify(checksWrap(ctx)) } : ctx.env;
77
+ const run = await runCommand(sumo(args), { cwd: ctx.cwd, env, signal, timeoutMs: command.checks ? null : 60_000 });
76
78
  if (run.error) return { content: run.error, isError: true };
77
79
  const output = capRedacted(run.output.trim(), undefined, run.omitted);
78
80
  return { content: output || '(done)', isError: run.stopped !== null || run.status !== 0 };
package/src/keeper.mjs ADDED
@@ -0,0 +1,77 @@
1
+ import { getMeta, setMeta } from './db.mjs';
2
+ import { runDream, SESSIONS_TO_TIDY, tidyDue } from './dream.mjs';
3
+ import { localServerStatus } from './local-server.mjs';
4
+ import { memoryHealth, memoryHealthLine } from './memory.mjs';
5
+ import { modelStats, outcomeLines, runScribe } from './scribe.mjs';
6
+ import { pendingTurns, sessionsAwaitingDream } from './sessions.mjs';
7
+ import { CONFIG_DEFAULTS, modelFileOf } from './setup.mjs';
8
+
9
+ /** When the last tidy failed; until TIDY_RETRY_MS has gone by, a pass that finds a tidy due files only. */
10
+ const TIDY_FAILED = 'memory.tidy_failed_at';
11
+ const TIDY_RETRY_MS = 6 * 3_600_000;
12
+
13
+ /**
14
+ * The memory pass: the one thing that looks after memory, so the user has one name, one status and one setting
15
+ * (`memory.model`) instead of a writer and a tidier to keep track of. Each pass files what was said since the last one;
16
+ * when tidying is due — conversations have finished, or a week has gone by — the same pass then tidies: reads them
17
+ * side by side, raises pairs that say one thing or disagree, fills in search terms. It runs by itself after turns and
18
+ * at session start and end; `sumo memory run` runs it now, `--tidy` tidies now whatever is due. A tidy that failed is
19
+ * not tried again by itself for six hours: a local model that is down must not turn every turn's filing into a failed
20
+ * tidy. Returns what each half did: `{ filed, tidied }`, `tidied` null when it did not tidy.
21
+ */
22
+ export async function runMemoryPass(db, { now = new Date().toISOString(), tidy = false } = {}) {
23
+ const filed = await runScribe(db, { now });
24
+ const failedAt = getMeta(db, TIDY_FAILED);
25
+ const resting = failedAt && Date.parse(now) - Date.parse(failedAt) < TIDY_RETRY_MS;
26
+ if (!tidy && (resting || !tidyDue(db, now))) return { filed, tidied: null };
27
+ const tidied = await runDream(db, { now, force: tidy, file: false });
28
+ if (tidied.ok === false && !tidied.skipped) setMeta(db, TIDY_FAILED, now);
29
+ else if (failedAt) setMeta(db, TIDY_FAILED, '');
30
+ return { filed, tidied };
31
+ }
32
+
33
+ /**
34
+ * A pass as a person reads it: what the filing did, then what the tidying did. A half with nothing to do is left out
35
+ * when the other did something. Fails — exit 1 for a script — when the filing failed, or a tidy asked for failed.
36
+ */
37
+ export function passLines({ filed, tidied }, { asked = false } = {}) {
38
+ const tidying = tidied ? outcomeLines(tidied).map((l, i) => (i === 0 ? `tidy: ${l}` : l)) : [];
39
+ const lines = [...(tidied && filed.skipped ? [] : outcomeLines(filed)), ...tidying];
40
+ const failed = (filed.ok === false && !filed.skipped) || (asked && tidied?.ok === false && !tidied.skipped);
41
+ return failed ? Object.assign(lines, { exitCode: 1 }) : lines;
42
+ }
43
+
44
+ /** `sumo memory status`: the pass as a whole — the model it runs on, what waits, how it last went — then the numbers. */
45
+ /** The ledger's names for the pass's two halves, as the status says them. */
46
+ const HALVES = { scribe: 'filing', dream: 'tidying' };
47
+ /** A status line: its label in a column of its own, so the values stand in one. */
48
+ const row = (label, value) => `${label.padEnd(LABEL_WIDTH)}${value}`;
49
+ const LABEL_WIDTH = 11;
50
+
51
+ export async function memoryStatus(db, now = new Date().toISOString()) {
52
+ const health = memoryHealthLine(memoryHealth(db, now), now);
53
+ const model = getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model'];
54
+ const server = model === 'local' ? await localServerStatus(db, { file: modelFileOf(db) }) : null;
55
+ const turns = pendingTurns(db, 1000).length;
56
+ const finished = sessionsAwaitingDream(db, now, 1000).length;
57
+ const tidied = db.prepare(`SELECT MAX(ts) AS at FROM model_runs WHERE kind = 'dream' AND ok = 1`).get()?.at ?? null;
58
+ const sweep = getMeta(db, 'dream.last_tidy');
59
+ // Due with fewer than three conversations waiting: the weekly sweep is what is owed.
60
+ const owed = finished < SESSIONS_TO_TIDY && tidyDue(db, now);
61
+ const failures = Number(getMeta(db, 'scribe.failures') ?? 0);
62
+ const failedAt = getMeta(db, TIDY_FAILED);
63
+ const day = (at) => (at ? at.slice(0, 10) : 'never');
64
+ // The ledger holds every request; the pass's own are its filing and its tidying, named so.
65
+ const spent = modelStats(db)
66
+ .filter((l) => /^(scribe|dream): /.test(l))
67
+ .map((l) => l.replace(/^(scribe|dream)/, (kind) => HALVES[kind]));
68
+ return [
69
+ `${health.ok ? 'ok ' : 'warn'} ${health.label}${health.fix ? ` — ${health.fix}` : ''}`,
70
+ row('model', `${model}${server ? ` · ${server}` : ''}`),
71
+ row('waiting', `${turns} turn${turns === 1 ? '' : 's'} to file · ${finished} finished conversation${finished === 1 ? '' : 's'} to read (tidied by itself at ${SESSIONS_TO_TIDY}, and weekly)`),
72
+ row('last tidy', `${day(tidied)} · weekly sweep: ${day(sweep)}${owed ? ' — owed: the next pass sweeps' : ''}`),
73
+ ...(failures > 0 ? [row('failing', `filing failed ${failures} time${failures === 1 ? '' : 's'} in a row — ${getMeta(db, 'scribe.last_error')}`)] : []),
74
+ ...(failedAt ? [row('failing', `the last tidy, at ${failedAt.slice(0, 16)}; tried again by itself ${Date.parse(now) - Date.parse(failedAt) < TIDY_RETRY_MS ? 'six hours on' : 'with the next pass'}, or now: sumo memory run --tidy`)] : []),
75
+ ...(spent.length > 0 ? [row('spent', spent[0]), ...spent.slice(1).map((l) => row('', l))] : []),
76
+ ];
77
+ }
@@ -1,8 +1,10 @@
1
1
  import { spawn, spawnSync } from 'node:child_process';
2
- import { closeSync, mkdirSync, openSync } from 'node:fs';
2
+ import { closeSync } from 'node:fs';
3
3
  import { createServer } from 'node:net';
4
- import { basename, join } from 'node:path';
4
+ import { basename } from 'node:path';
5
5
  import { getMeta, setMeta, tx } from './db.mjs';
6
+ import { alive } from './lock.mjs';
7
+ import { openLog, trimLog } from './logs.mjs';
6
8
  import { paths } from './paths.mjs';
7
9
 
8
10
  /**
@@ -16,22 +18,13 @@ const HEALTH_TIMEOUT_MS = 60_000;
16
18
  const HEALTH_POLL_MS = 250;
17
19
  const SLEEP_IDLE_SECONDS = 600;
18
20
  /** The context the server is given: the largest scribe bundle seen was 24k tokens, dream's 90th percentile 12k. */
19
- export const LOCAL_CONTEXT_TOKENS = 32_768;
21
+ const LOCAL_CONTEXT_TOKENS = 32_768;
20
22
  /** How long a pass may think. Measured on the scribe probe, the 4B model answers right within this; uncapped, a small model can think until the token limit and answer nothing. */
21
23
  export const LOCAL_REASONING_BUDGET = 1024;
22
24
 
23
25
  /** Router mode names a model after its file. */
24
26
  export const modelIdOf = (file) => basename(file, '.gguf');
25
27
 
26
- function alive(pid) {
27
- try {
28
- process.kill(pid, 0);
29
- return true;
30
- } catch (cause) {
31
- return cause.code === 'EPERM';
32
- }
33
- }
34
-
35
28
  /** Only a process still carrying the port it was started on is ours: a pid can be reused once the server is gone. */
36
29
  function ours(server) {
37
30
  if (!alive(server.pid)) return false;
@@ -85,9 +78,7 @@ async function waitForHealth(port, dead) {
85
78
 
86
79
  /** Starts the server for `file`, detached, logging to logs/llama-server.log, and records it. */
87
80
  function start(db, { llama, file, port }) {
88
- const logs = paths().logs;
89
- mkdirSync(logs, { recursive: true, mode: 0o700 });
90
- const out = openSync(join(logs, 'llama-server.log'), 'a');
81
+ const out = openLog('llama-server.log');
91
82
  let dead = null;
92
83
  let child;
93
84
  try {
@@ -117,6 +108,8 @@ function start(db, { llama, file, port }) {
117
108
  export async function ensureLocalServer(db, { llama, file }) {
118
109
  const current = recorded(db);
119
110
  if (current && current.file === file && current.llama === llama && ours(current)) {
111
+ // The server outlives the sumo that started it, and its log with it: trimmed in place each time it is used again.
112
+ trimLog('llama-server.log');
120
113
  if (await healthy(current.port)) return current;
121
114
  // Started a moment ago by another sumo and still coming up: wait for it rather than replace it.
122
115
  if (Date.now() - current.startedAt < HEALTH_TIMEOUT_MS) {
@@ -154,7 +147,7 @@ export function stopLocalServer(db, expected) {
154
147
  db.prepare('DELETE FROM meta WHERE key = ?').run(META_KEY);
155
148
  }
156
149
 
157
- /** One line for `sumo scribe status`: where the server is and what it says about the model. */
150
+ /** One line for `sumo memory status`: where the server is and what it says about the model. */
158
151
  export async function localServerStatus(db, { file }) {
159
152
  const current = recorded(db);
160
153
  if (!current) return 'local server: not running (starts on the first call)';
package/src/lock.mjs CHANGED
@@ -35,7 +35,8 @@ function holderOf(file) {
35
35
  }
36
36
  }
37
37
 
38
- function alive(pid) {
38
+ /** Whether a process is there: signal 0 asks without sending; one that is someone else's (EPERM) is there too. */
39
+ export function alive(pid) {
39
40
  try {
40
41
  process.kill(pid, 0);
41
42
  return true;