sumo-agents 0.7.1 → 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/jobs.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { execFileSync } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
3
2
  import { appendFileSync, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
4
3
  import { join } from 'node:path';
5
4
  import { applyOps } from './apply.mjs';
@@ -13,6 +12,7 @@ import { callModel, failure, logRun } from './model.mjs';
13
12
  import { paths } from './paths.mjs';
14
13
  import { preferences } from './prime.mjs';
15
14
  import { getProject, isScratch } from './projects.mjs';
15
+ import { ownGit } from './sandbox.mjs';
16
16
  import { chooseRoute, RETRY_IMPORTANT_THRESHOLD, statsLines } from './route.mjs';
17
17
  import { verifyCommands } from './scan.mjs';
18
18
  import { filingState } from './ledger.mjs';
@@ -20,8 +20,9 @@ import { runScribe } from './scribe.mjs';
20
20
  import { addCheckpoint, pendingTurns } from './sessions.mjs';
21
21
  import { CONFIG_DEFAULTS } from './setup.mjs';
22
22
  import { clip, estimateTokens, head, STOPWORDS } from './text.mjs';
23
+ import { digest } from './tools.mjs';
23
24
  import { changesSince, diffStat, hasCommit, snapshot, stillThere, takeBaseline, verdictLines, verify, writeChanges } from './verify.mjs';
24
- import { bringBack, dropTree, makeTree, treeDiff } from './worktree.mjs';
25
+ import { bringBack, dropTree, makeTree, sharedSourceDependencies, treeDiff } from './worktree.mjs';
25
26
 
26
27
  const AGENTS = ['scout', 'worker', 'reviewer'];
27
28
  /** Work with a written way of doing it: guides/<name>.md, carried into the brief on request. */
@@ -162,7 +163,7 @@ function whereThingsStand(root) {
162
163
  return null;
163
164
  }
164
165
  };
165
- const git = (...args) => run('git', ['--no-optional-locks', '-c', 'core.quotePath=false', ...args]);
166
+ const git = (...args) => run('git', ['--no-optional-locks', '-c', 'core.quotePath=false', ...ownGit(args)]);
166
167
  const repo = git('rev-parse', '--is-inside-work-tree')?.trim() === 'true';
167
168
  const heading = '## Where things stand as this job begins — no need to look it up again';
168
169
  const lines = [];
@@ -229,7 +230,6 @@ ${REPORT[job.agent]}
229
230
  `;
230
231
  }
231
232
 
232
- const digest = (text) => createHash('sha256').update(text).digest('hex');
233
233
  const stateKey = (id) => `job.${id}.verify`;
234
234
 
235
235
  /**
@@ -380,7 +380,7 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
380
380
 
381
381
  // A rule the user stated a minute ago may not be filed yet, and the brief is built from memory: the conversation
382
382
  // still going on is read now. Not while filing is paused — a job starting is not the user asking for it by hand.
383
- if (pendingTurns(db, 1).length > 0 && !filingState(db, now).paused) await runScribe(db, { now });
383
+ if (pendingTurns(db, 1).length > 0 && !filingState(db).paused) await runScribe(db, { now });
384
384
 
385
385
  const route = routed ?? (await chooseRoute(db, { agent, project, task, title: title.trim(), retryOf, reviews }));
386
386
 
@@ -440,7 +440,7 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
440
440
  // The project's installed dependencies are linked in (makeTree): outside the job's roots, the sandbox lets it read them, never change them.
441
441
  const deps =
442
442
  tree.linked?.length > 0
443
- ? ` The project's installed dependencies — ${tree.linked.join(', ')} — are its own, linked in: the checks run with them, and they are read-only here; if one must be added or changed, say so in the report. A workspace package or an editable install reached through them is the project's copy, not this tree's: a check that loads one tests the project's code, not your change to it.`
443
+ ? ` The project's installed dependencies — ${tree.linked.join(', ')} — are its own, linked in: the checks run with them, and they are read-only here; if one must be added or changed, say so in the report. Verification reports shared workspace packages and editable installs that need an independent installation in this worktree.`
444
444
  : ' If the checks need installed dependencies, install them there.';
445
445
  appendFileSync(fileOf(job.id, 'brief.md'), `\n## Where you work\n${why} Files git ignores — build output and the like — were not copied into it.${deps}\n`, { mode: 0o600 });
446
446
  }
@@ -605,6 +605,11 @@ async function followUp(db, id, { now = new Date().toISOString() } = {}) {
605
605
  const why = `j${id} failed on ${routeOf(job)} after it was told to close near its ${near}: it is not retried one step up, which would start again on a dearer route with a fresh ${near}. To retry it anyway: sumo job retry ${id}`;
606
606
  return { line: said(id, 'Not retried', [why])[0] };
607
607
  }
608
+ // Its DONE could not be verified here — no check, or none that could be compared: a dearer route would meet the same wall.
609
+ if (existsSync(unverifiableFile(id))) {
610
+ const why = `j${id} failed on ${routeOf(job)} after its DONE could not be verified here: it is not retried one step up, which would meet the same wall. To retry it anyway: sumo job retry ${id}`;
611
+ return { line: said(id, 'Not retried', [why])[0] };
612
+ }
608
613
  try {
609
614
  const made = await retry(db, id, { now });
610
615
  return { ...made, kind: 'retry', line: `j${id} failed on ${routeOf(job)} and was retried on ${routeOf(made.job)}, as j${made.job.id} — j${id}'s own report: sumo job show ${id}` };
@@ -697,9 +702,16 @@ function checkable(db, id, verb) {
697
702
  return { job, state, root };
698
703
  }
699
704
 
705
+ function sharedSourceReason(state, project) {
706
+ const shared = sharedSourceDependencies(state.tree, project.path);
707
+ return shared.length ? `shared dependencies (${shared.join(', ')}) may load editable or workspace source from the original checkout. Install those dependencies independently in the worktree and verify again` : null;
708
+ }
709
+
700
710
  /** The project's checks as they stand before the work. Taken once; a second call reports what the first found. */
701
711
  export function baseline(db, id) {
702
- const { state, root } = checkable(db, id, 'given a baseline');
712
+ const { job, state, root } = checkable(db, id, 'given a baseline');
713
+ const shared = sharedSourceReason(state, getProject(db, job.project));
714
+ if (shared) return [`no baseline taken: ${shared}`];
703
715
  if (!state.baseline) {
704
716
  const taken = takeBaseline(root, state.snap, dirOf(id));
705
717
  if (taken.refused) return [`no baseline taken: ${taken.refused}.`, 'Every check that fails at the end will count as this job\'s.'];
@@ -717,8 +729,13 @@ export function baseline(db, id) {
717
729
 
718
730
  /** Runs the checks now and records the verdict against the exact tree it judged. */
719
731
  export function verifyJob(db, id, now = new Date().toISOString()) {
720
- const { state, root } = checkable(db, id, 'verified');
732
+ const { job, state, root } = checkable(db, id, 'verified');
721
733
  const verdict = verify(root, state, dirOf(id), now);
734
+ const shared = sharedSourceReason(state, getProject(db, job.project));
735
+ if (shared) {
736
+ verdict.ok = false;
737
+ verdict.inconclusive = [...(verdict.inconclusive ?? []), shared];
738
+ }
722
739
  writeState(db, id, { ...state, verdict });
723
740
  return verdict;
724
741
  }
@@ -742,10 +759,11 @@ export function changes(db, id) {
742
759
  * whether the tree moved, so it is always run again.
743
760
  */
744
761
  function verdictForDone(db, id, state, now) {
745
- const root = workdir(state, getProject(db, getJob(db, id).project));
762
+ const project = getProject(db, getJob(db, id).project);
763
+ const root = workdir(state, project);
746
764
  if (!existsSync(root)) return null;
747
765
  const last = state.verdict;
748
- if (last?.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
766
+ if (last?.version === 2 && !last.inconclusive?.length && !sharedSourceReason(state, project) && last.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
749
767
  return verifyJob(db, id, now);
750
768
  }
751
769
 
@@ -825,21 +843,26 @@ export const appendLive = (id, text) => appendFileSync(liveFile(id), text, { mod
825
843
  export const endLive = (id, lines) => writeFileSync(endFile(id), `${lines.join('\n')}\n`, { mode: 0o600 });
826
844
 
827
845
  /**
828
- * The tools a job used that read the world outside the machine — a web search, a fetch, an MCP server — one name a
829
- * 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).
846
+ * The tools a job used that read the world outside the machine — a web search, a fetch, an MCP server — each once. What
847
+ * such a run says it learned may be what a page told it, so it is not filed as a fact (see finish). Kept in the database,
848
+ * which the job's shell cannot write: a mark in the job's own folder could be wiped by the job it marks. A job from before
849
+ * wrote a file there, read beside it — a mark only adds.
830
850
  */
851
+ const outsideKey = (id) => `job.${id}.outside`;
831
852
  const outsideFile = (id) => fileOf(id, 'outside');
832
- export function noteOutsideRead(id, tool) {
853
+ export function noteOutsideRead(db, id, tool) {
833
854
  const name = String(tool).replace(/\s+/g, ' ');
834
- if (outsideReads(id).includes(name)) return;
835
- appendFileSync(outsideFile(id), `${name}\n`, { mode: 0o600 });
855
+ const kept = outsideReads(db, id);
856
+ if (!kept.includes(name)) setMeta(db, outsideKey(id), [...kept, name].join('\n'));
836
857
  }
837
- export function outsideReads(id) {
858
+ export function outsideReads(db, id) {
859
+ let before = [];
838
860
  try {
839
- return readFileSync(outsideFile(id), 'utf8').split('\n').filter(Boolean);
861
+ before = readFileSync(outsideFile(id), 'utf8').split('\n').filter(Boolean);
840
862
  } catch {
841
- return [];
863
+ // None: a job of this version keeps its mark in the database alone.
842
864
  }
865
+ return [...new Set([...(getMeta(db, outsideKey(id)) ?? '').split('\n').filter(Boolean), ...before])];
843
866
  }
844
867
 
845
868
  /** 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. */
@@ -911,6 +934,8 @@ function findingsSection(report) {
911
934
  }
912
935
 
913
936
  const checkedFile = (id) => fileOf(id, 'checked');
937
+ /** Written when a DONE was refused as unverifiable: a FAILED the job closes on after it is not the work failing, and is not retried. */
938
+ const unverifiableFile = (id) => fileOf(id, 'unverifiable');
914
939
  /** How many changed files, and how many new ones, the check names before it counts the rest. */
915
940
  const CHECK_LISTED = 30;
916
941
 
@@ -998,6 +1023,9 @@ function checkAgainstTask(db, job, state) {
998
1023
  * as gotchas, only in this project, marked as observed, through the same
999
1024
  * validator as every other model-written memory.
1000
1025
  */
1026
+ /** What a job is told when work cannot be checked here: the user takes it or not, and it has not failed. */
1027
+ export const ASK_TO_ACCEPT = 'Ask the user to accept it — the ask tool — saying what blocks verification: work that cannot be checked here has not failed, so do not finish FAILED for it.';
1028
+
1001
1029
  export function finish(db, id, { status, report, accept }, now = new Date().toISOString()) {
1002
1030
  const job = getJob(db, id);
1003
1031
  requireOpen(job, 'finished');
@@ -1009,7 +1037,10 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
1009
1037
  // A DONE that cannot be checked is refused, not waved through; --accept is the way to take it anyway, and it is written down.
1010
1038
  const graded = job.agent === 'worker' && status === 'DONE';
1011
1039
  const state = graded ? readState(db, id) : null;
1012
- 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>"`);
1040
+ const unchecked = (why) => {
1041
+ writeFileSync(unverifiableFile(id), `${now}\n`, { mode: 0o600 });
1042
+ return new Refusal(`j${id} cannot be verified — ${why}. To take the work as it stands: sumo job finish ${id} --status DONE --accept "<why>" — the user's to run: a job asks the user to accept it — the ask tool — and does not finish FAILED for this`);
1043
+ };
1013
1044
  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`);
1014
1045
  // Before the checks are run, the author looks once at the task beside its change: the first DONE is answered with that, not taken.
1015
1046
  // Kept in the job's folder, so a run started again goes on to the checks instead of being asked again.
@@ -1022,6 +1053,7 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
1022
1053
  // The checks can take minutes: report a job closed in the meantime before interpreting their verdict.
1023
1054
  requireOpen(getJob(db, id), 'finished');
1024
1055
  if (graded && accept === undefined && !verdict) throw unchecked(`${getProject(db, job.project).path} no longer exists, so its checks cannot be run`);
1056
+ if (verdict?.inconclusive?.length && !verdict.blocking.length) throw unchecked(verdict.inconclusive.join('\n'));
1025
1057
  if (verdict && !verdict.ok) {
1026
1058
  throw new Refusal(
1027
1059
  [
@@ -1058,7 +1090,7 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
1058
1090
  // 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
1059
1091
  // confirm (arXiv 2503.03704 — memory written from injected content is read later as trusted). Exactly what this
1060
1092
  // finish wrote is held: another job finishing in the same project at the same moment keeps its own.
1061
- const outside = outsideReads(id);
1093
+ const outside = outsideReads(db, id);
1062
1094
  if (outside.length > 0 && learned.written.length > 0) {
1063
1095
  const held = db
1064
1096
  .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`)
@@ -1153,16 +1185,20 @@ function said(id, heading, lines) {
1153
1185
  return lines;
1154
1186
  }
1155
1187
 
1156
- /** The tree goes; what it changed stays beside the job as changes.diff, for a retry, a review or a person. */
1188
+ /* Keep the review text and a complete, applicable patch before removing or merging a tree. */
1189
+ function archiveTree(id, state) {
1190
+ writeChanges(state.tree.path, state.snap, fileOf(id, 'changes.diff'));
1191
+ const patch = treeDiff(state.tree, { binary: true });
1192
+ const file = fileOf(id, 'change.patch');
1193
+ writeFileSync(file + '.tmp', patch, { mode: 0o600, flush: true });
1194
+ renameSync(file + '.tmp', file);
1195
+ return patch;
1196
+ }
1197
+
1157
1198
  function release(id, project, state) {
1158
- const kept = fileOf(id, 'changes.diff');
1159
- try {
1160
- writeChanges(state.tree.path, state.snap, kept);
1161
- } catch {
1162
- // A tree too broken to diff has nothing to keep.
1163
- }
1199
+ archiveTree(id, state);
1164
1200
  dropTree(project.path, state.tree.top);
1165
- return kept;
1201
+ return fileOf(id, 'changes.diff');
1166
1202
  }
1167
1203
 
1168
1204
  /** The newest other worker at work in the project itself — not in a worktree of its own — right now, or undefined. */
@@ -1175,25 +1211,27 @@ const inPlace = (db, project, id) => openWorkers(db, project.slug, id).find((o)
1175
1211
  */
1176
1212
  function applyTree(db, id, project, state) {
1177
1213
  const busy = inPlace(db, project, id);
1214
+ const patch = archiveTree(id, state);
1178
1215
  if (busy) {
1179
- const patch = treeDiff(state.tree, { binary: true });
1180
- if (patch.length > 0) writeFileSync(fileOf(id, 'change.patch'), patch, { mode: 0o600 });
1181
- const kept = release(id, project, state);
1216
+ const kept = fileOf(id, 'changes.diff');
1217
+ dropTree(project.path, state.tree.top);
1182
1218
  if (patch.length === 0) return 'It changed nothing; its worktree is removed.';
1183
1219
  return `The change was NOT applied: j${busy.id} "${busy.title}" is still working in ${project.path}, and the change would land under it — its start, its checks and its review would count it as its own. Its worktree is removed; what it changed is kept in ${kept}, and whole as a patch: once j${busy.id} is done, git -C ${project.path} apply ${fileOf(id, 'change.patch')}`;
1184
1220
  }
1185
- const back = bringBack(project.path, state.tree, dirOf(id));
1221
+ const back = bringBack(project.path, state.tree, dirOf(id), patch);
1186
1222
  if (!back.applied && !back.empty) {
1187
1223
  return `The change was NOT applied: it does not apply to ${project.path} as that stands now. It is kept in ${state.tree.top} — bring it over by hand, then: git -C ${project.path} worktree remove --force ${state.tree.top}${back.why ? ` (git said: ${back.why})` : ''}`;
1188
1224
  }
1189
- release(id, project, state);
1225
+ dropTree(project.path, state.tree.top);
1190
1226
  return back.empty ? 'It changed nothing; its worktree is removed.' : `The change was applied to ${project.path}${back.merged ? ' by a three-way merge, and staged' : ''}; its worktree is removed.`;
1191
1227
  }
1192
1228
 
1193
1229
  /** Done, judged by the checks, and its tree still the one they judged: an attempt worth keeping. */
1194
1230
  function verifiedDone(db, id, state) {
1195
1231
  const tree = state?.tree?.path;
1196
- return getJob(db, id).status === 'done' && state.verdict?.ok === true && existsSync(tree ?? '') && unmoved(tree, state.snap, state.verdict.fingerprint);
1232
+ const job = getJob(db, id);
1233
+ if (/^## Accepted without verification$/m.test(reportOf(id)) || sharedSourceReason(state, getProject(db, job.project))) return false;
1234
+ return job.status === 'done' && state.verdict?.version === 2 && state.verdict.ok === true && existsSync(tree ?? '') && unmoved(tree, state.snap, state.verdict.fingerprint);
1197
1235
  }
1198
1236
 
1199
1237
  /**
package/src/ledger.mjs CHANGED
@@ -27,8 +27,8 @@ const FAILURES_VERSION = 'scribe.failures_version';
27
27
  const LAST_ERROR = 'scribe.last_error';
28
28
  const FALLBACK_USED = 'scribe.fallback_used';
29
29
 
30
- /** The model the memory pass is set to: `memory.model`, the one setting. */
31
- const memoryModel = (db) => getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model'];
30
+ /** The model the memory pass is set to: `memory.model`, the one setting — 'off' when the pass is turned off. */
31
+ export const memoryModel = (db) => getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model'];
32
32
 
33
33
  /**
34
34
  * The extractor's version: a short hash of what decides what gets filed — the prompt the model reads, the model
@@ -43,7 +43,8 @@ export function extractorVersion(db) {
43
43
  /**
44
44
  * Of the memories this extractor derived, the share the user then threw out within 30 days — forgot, rejected, or
45
45
  * replaced by hand — read from `memory_events`; null under ten derived. A memory a rebuild retired is not the user's
46
- * verdict, and one the pass itself replaced with a newer statement was right when it was filed.
46
+ * verdict, one the pass itself replaced with a newer statement was right when it was filed, and one the user reworded
47
+ * on the page (`edited`) they kept.
47
48
  */
48
49
  function junkRate(db, version) {
49
50
  const derived = db