sumo-agents 0.1.0 → 0.4.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/AGENTS.md +3 -2
- package/README.md +102 -611
- package/bin/sumo.mjs +3 -1
- package/guides/delegation.md +8 -7
- package/package.json +3 -2
- package/prompts/agent.md +3 -3
- package/prompts/dream.md +1 -0
- package/prompts/scribe.md +1 -0
- package/src/apply.mjs +79 -19
- package/src/card.mjs +11 -6
- package/src/chat.mjs +318 -54
- package/src/cli.mjs +120 -31
- package/src/cost.mjs +128 -0
- package/src/db.mjs +40 -1
- package/src/editor.mjs +110 -9
- package/src/export.mjs +1 -1
- package/src/herdr.mjs +5 -2
- package/src/hooks.mjs +13 -3
- package/src/jobs.mjs +606 -59
- package/src/jobtools.mjs +10 -7
- package/src/loop.mjs +358 -53
- package/src/mcp.mjs +44 -14
- package/src/memory.mjs +39 -4
- package/src/model.mjs +6 -5
- package/src/page.mjs +3 -1
- package/src/paths.mjs +9 -2
- package/src/prime.mjs +14 -5
- package/src/projects.mjs +18 -0
- package/src/render.mjs +1 -0
- package/src/route.mjs +110 -12
- package/src/sandbox.mjs +173 -0
- package/src/scribe.mjs +7 -5
- package/src/sessions.mjs +12 -8
- package/src/setup.mjs +76 -7
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +8 -3
- package/src/tools.mjs +562 -31
- package/src/tty.mjs +293 -43
- package/src/ui.mjs +507 -98
- package/src/verify.mjs +51 -8
- package/src/worktree.mjs +128 -0
package/src/verify.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
|
-
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { isSecretPath } from './guard.mjs';
|
|
6
6
|
import { UsageError } from './memory.mjs';
|
|
@@ -51,7 +51,7 @@ function git(root, args) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/** A diff as git itself writes it, whatever the user has configured: no external tool, no colour, the usual prefixes — it is parsed, hashed and handed to a reviewer. */
|
|
54
|
-
const DIFF = ['diff', '--no-ext-diff', '--no-color', '--no-textconv', '--src-prefix=a/', '--dst-prefix=b/'];
|
|
54
|
+
export const DIFF = ['diff', '--no-ext-diff', '--no-color', '--no-textconv', '--src-prefix=a/', '--dst-prefix=b/'];
|
|
55
55
|
|
|
56
56
|
const lines = (text) => (text ?? '').split('\n').filter(Boolean);
|
|
57
57
|
|
|
@@ -169,21 +169,50 @@ function runChecks(root, commands, dir, prefix) {
|
|
|
169
169
|
killGroup(run.pid);
|
|
170
170
|
const output = tailOf(file);
|
|
171
171
|
writeFileSync(file, `$ ${command}\n${output}\n${timedOut ? '\n(stopped: it ran past the time limit)\n' : ''}`, { mode: 0o600 });
|
|
172
|
-
return { name, command, ok: run.status === 0, timedOut, ms: Date.now() - started, file };
|
|
172
|
+
return { name, command, ok: run.status === 0, status: run.status, timedOut, ms: Date.now() - started, file };
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/** A line of a check's log that names a failure: runners print the failing test, or the error, on it. */
|
|
177
|
+
const FAILURE_LINE = /\b(?:fail(?:s|ed|ing|ures?)?|errors?|not ok)\b|[✗✕✖]/i;
|
|
178
|
+
/** A time a runner prints, which differs from one run to the next when nothing else does. */
|
|
179
|
+
const TIMING = /\b\d+(?:\.\d+)?\s?m?s\b/g;
|
|
180
|
+
|
|
181
|
+
/** How a run of a check ended: its exit status and the lines that name a failure, without their times. Two runs that agree on it failed the same way. */
|
|
182
|
+
const endedAs = (run) =>
|
|
183
|
+
`${run.status}\n${[...new Set(readFileSync(run.file, 'utf8').split('\n').filter((l) => FAILURE_LINE.test(l)).map((l) => l.replace(TIMING, '').trim()))].sort().join('\n')}`;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The checks that failed, run once more, each beside the run it repeats — its log too, `<log>-again.txt`. One that ran past
|
|
187
|
+
* its time limit is not run again: that would take as long once more, past the ten minutes a harness gives the command that asked for it.
|
|
188
|
+
*/
|
|
189
|
+
function runAgain(root, failed, dir) {
|
|
190
|
+
const again = failed.filter((c) => !c.timedOut);
|
|
191
|
+
const runs = runChecks(root, again, dir, 'again');
|
|
192
|
+
return new Map(
|
|
193
|
+
again.map((c, i) => {
|
|
194
|
+
const file = c.file.replace(/\.txt$/, '-again.txt');
|
|
195
|
+
renameSync(runs[i].file, file);
|
|
196
|
+
return [c, { ...runs[i], file }];
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
176
201
|
/**
|
|
177
202
|
* The checks as they stand before the work. Refused once the tree has moved:
|
|
178
203
|
* a baseline taken after the first edit would call the job's own breakage
|
|
179
|
-
* "already there", which is the one thing it exists to prevent.
|
|
204
|
+
* "already there", which is the one thing it exists to prevent. A check that
|
|
205
|
+
* fails is run once more, and two runs that disagree — on the exit status, or
|
|
206
|
+
* on what they say failed — record it as flaky, with the second run's log.
|
|
180
207
|
*/
|
|
181
208
|
export function takeBaseline(root, snap, dir) {
|
|
182
209
|
if (snap && !stillThere(root, snap)) return { refused: 'the recorded start of this job is no longer in the repository, so there is no telling what has changed since' };
|
|
183
210
|
if (snap && changesSince(root, snap).fingerprint !== snap.fingerprint) {
|
|
184
211
|
return { refused: 'files have already changed since this job began, so a baseline now would include your own edits' };
|
|
185
212
|
}
|
|
186
|
-
|
|
213
|
+
const checks = runChecks(root, verifyCommands(root), dir, 'baseline');
|
|
214
|
+
const again = runAgain(root, checks.filter((c) => !c.ok), dir);
|
|
215
|
+
return { checks: checks.map((c) => (again.has(c) && endedAs(again.get(c)) !== endedAs(c) ? { ...c, flaky: true, again: again.get(c).file } : c)) };
|
|
187
216
|
}
|
|
188
217
|
|
|
189
218
|
/**
|
|
@@ -243,9 +272,20 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
243
272
|
|
|
244
273
|
// By command, not name: `cargo test` and `pytest` are both "test", and each has a start of its own.
|
|
245
274
|
const before = new Map((baseline ?? []).map((c) => [c.command, c]));
|
|
246
|
-
const
|
|
275
|
+
const ran = runChecks(root, commands, dir, 'verify');
|
|
276
|
+
// One that passed when the job began and fails now is run once more before it is held against the job: passing then, it is flaky.
|
|
277
|
+
// One recorded flaky at the start is not run again: whatever it says, it does not refuse DONE on its own.
|
|
278
|
+
const again = runAgain(root, ran.filter((c) => !c.ok && before.get(c.command)?.ok), dir);
|
|
279
|
+
const checks = ran.map((c) => {
|
|
247
280
|
const was = before.get(c.command);
|
|
248
|
-
const state = c.ok ? 'pass' : was && !was.ok ? 'already-failing' : 'fail';
|
|
281
|
+
const state = c.ok ? 'pass' : was?.flaky || again.get(c)?.ok ? 'flaky' : was && !was.ok ? 'already-failing' : 'fail';
|
|
282
|
+
if (state === 'flaky') {
|
|
283
|
+
flags.push(
|
|
284
|
+
was.flaky
|
|
285
|
+
? `\`${c.command}\` failed, and it was flaky when this job began — DONE is not refused on it; compare ${was.file} with ${c.file}`
|
|
286
|
+
: `\`${c.command}\` failed, then passed when run again — flaky, so DONE is not refused on it; read ${c.file}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
249
289
|
if (state === 'fail') {
|
|
250
290
|
const why = c.timedOut ? 'ran past the time limit' : 'fails';
|
|
251
291
|
blocking.push(
|
|
@@ -289,6 +329,9 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
289
329
|
return { at: now, ok: blocking.length === 0, fingerprint, checks, blocking, flags, notes };
|
|
290
330
|
}
|
|
291
331
|
|
|
332
|
+
/** git's own summary of what changed in tracked files since the snapshot: a line a file, at most `count` of them, then its totals. Empty when git cannot say. */
|
|
333
|
+
export const diffStat = (root, snap, count) => git(root, [...DIFF, '--stat', `--stat-count=${count}`, snap.base, '--', '.'])?.trimEnd() ?? '';
|
|
334
|
+
|
|
292
335
|
/** The whole change as one file — what a reviewer reads instead of re-deriving it with git. */
|
|
293
336
|
export function writeChanges(root, snap, file) {
|
|
294
337
|
if (!stillThere(root, snap)) throw new UsageError(`the recorded start of that work (${snap.base.slice(0, 12)}) is no longer in the repository — there is no change to hand over`);
|
|
@@ -316,7 +359,7 @@ export function writeChanges(root, snap, file) {
|
|
|
316
359
|
|
|
317
360
|
/** One line per check, for a status line or a report. */
|
|
318
361
|
export function verdictLines(verdict) {
|
|
319
|
-
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing' };
|
|
362
|
+
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing', flaky: 'was flaky' };
|
|
320
363
|
return [
|
|
321
364
|
...verdict.checks.map((c) => `\`${c.command}\` ${words[c.state]}`),
|
|
322
365
|
...verdict.blocking.map((b) => `blocking: ${b}`),
|
package/src/worktree.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { copyFileSync, cpSync, lstatSync, mkdirSync, rmSync, statSync, symlinkSync } from 'node:fs';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { DIFF } from './verify.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A worker's own git worktree, for when another worker already has the project's tree to itself. It is made from the
|
|
8
|
+
* project as it stands — uncommitted edits and untracked files included — without touching the user's files, index or
|
|
9
|
+
* branches, and what the work changed is brought back to the project at the end. Nothing here knows about jobs.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Sumo's own git calls: no hook of the user's runs, nothing is signed, and a commit has an author where git has no identity. */
|
|
13
|
+
const OWN = ['-c', 'core.hooksPath=/dev/null', '-c', 'commit.gpgsign=false', '-c', 'user.name=sumo', '-c', 'user.email=sumo@localhost'];
|
|
14
|
+
|
|
15
|
+
const git = (root, args) => execFileSync('git', ['-C', root, ...OWN, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 256 * 1024 * 1024 }).trim();
|
|
16
|
+
/** How a git command that reads a patch on stdin went: whether it succeeded, and the first lines of what it said, on one line. */
|
|
17
|
+
const fed = (root, args, patch, env = {}) => {
|
|
18
|
+
const run = spawnSync('git', ['-C', root, ...OWN, ...args], { input: patch, env: { ...process.env, ...env }, stdio: ['pipe', 'ignore', 'pipe'], encoding: 'utf8' });
|
|
19
|
+
return { ok: run.status === 0, said: (run.stderr ?? '').split('\n').map((l) => l.trim()).filter(Boolean).slice(0, 3).join('; ') };
|
|
20
|
+
};
|
|
21
|
+
/** The paths a NUL-separated git listing names. */
|
|
22
|
+
const listed = (out) => out.split('\0').filter(Boolean);
|
|
23
|
+
|
|
24
|
+
/** The folders at a project's top level its installed dependencies live in — git ignores them, so a tree is made without them. */
|
|
25
|
+
const DEPENDENCIES = ['node_modules', '.venv', 'venv'];
|
|
26
|
+
|
|
27
|
+
const isDir = (path) => {
|
|
28
|
+
try {
|
|
29
|
+
return statSync(path).isDirectory();
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** Whether anything is at a path, a link to nothing included. */
|
|
35
|
+
const taken = (path) => {
|
|
36
|
+
try {
|
|
37
|
+
return Boolean(lstatSync(path));
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Makes the tree at `top`: `git stash create` captures the tracked edits in a commit nobody points at (none: the head),
|
|
45
|
+
* the tree is checked out from it detached, and what git does not track is copied in and committed there, on the tree's
|
|
46
|
+
* own head — so the change brought back is the work's, and nothing the user already had. A project in a subdirectory of
|
|
47
|
+
* its repository works in the same subdirectory of the tree: that is `path`. The project's installed dependencies that
|
|
48
|
+
* exist are linked in, not copied, so its checks run there: `linked` names them.
|
|
49
|
+
*/
|
|
50
|
+
export function makeTree(root, top) {
|
|
51
|
+
const prefix = git(root, ['rev-parse', '--show-prefix']).replace(/\/$/, '');
|
|
52
|
+
const start = git(root, ['stash', 'create']) || git(root, ['rev-parse', 'HEAD']);
|
|
53
|
+
git(root, ['worktree', 'add', '--detach', top, start]);
|
|
54
|
+
const path = join(top, prefix);
|
|
55
|
+
// An untracked repository inside the project is listed as `dir/`: it is not part of this one, and is left out.
|
|
56
|
+
const untracked = git(root, ['ls-files', '--others', '--exclude-standard', '-z']).split('\0').filter((f) => f && !f.endsWith('/'));
|
|
57
|
+
for (const file of untracked) {
|
|
58
|
+
mkdirSync(dirname(join(path, file)), { recursive: true });
|
|
59
|
+
cpSync(join(root, file), join(path, file), { verbatimSymlinks: true });
|
|
60
|
+
}
|
|
61
|
+
// A link to the project's own folder: the sandbox follows it out of the job's roots, so the job reads the user's packages and cannot change them.
|
|
62
|
+
const linked = DEPENDENCIES.filter((name) => isDir(join(root, name)) && !taken(join(path, name)));
|
|
63
|
+
for (const name of linked) symlinkSync(join(root, name), join(path, name));
|
|
64
|
+
if (untracked.length > 0 || linked.length > 0) {
|
|
65
|
+
git(top, ['add', '-A']);
|
|
66
|
+
// `node_modules/` in a .gitignore does not match a link of that name, and info/exclude is the user's own, shared by every
|
|
67
|
+
// worktree: the links are committed on the tree's head instead, and marked in its own index as never changing — so whatever
|
|
68
|
+
// the job does to one, an install that puts a folder in its place included, no `git add -A` stages it and no diff shows it.
|
|
69
|
+
const links = linked.map((name) => join(prefix, name));
|
|
70
|
+
if (links.length > 0) git(top, ['add', '-f', '--', ...links]);
|
|
71
|
+
git(top, ['commit', '-q', '--no-verify', '-m', 'what the project held outside git, as it was']);
|
|
72
|
+
if (links.length > 0) git(top, ['update-index', '--skip-worktree', '--', ...links]);
|
|
73
|
+
}
|
|
74
|
+
return { top, path, prefix, base: git(top, ['rev-parse', 'HEAD']), linked };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Everything changed in the tree since it was made, new files included, as git itself writes a diff: with `binary`, whole enough to apply. */
|
|
78
|
+
export function treeDiff({ top, prefix, base }, { binary = false } = {}) {
|
|
79
|
+
git(top, ['add', '-A']);
|
|
80
|
+
return execFileSync('git', ['-C', top, ...DIFF, '--cached', ...(binary ? ['--binary'] : []), base, '--', prefix || '.'], {
|
|
81
|
+
encoding: binary ? 'buffer' : 'utf8',
|
|
82
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
83
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Brings the tree's change back to the project. Straight onto the files first: it applies whole or not at all, touches no
|
|
89
|
+
* index, and the user's uncommitted edits are the very start the work was made from. Where the project has moved since, a
|
|
90
|
+
* three-way merge — tried first on a copy of the index, since one that conflicts leaves its markers in the user's files —
|
|
91
|
+
* unless a file it touches has edits not staged in the project: git refuses a three-way merge over those, whatever the trial
|
|
92
|
+
* said. `scratch` is a folder of sumo's own for that copy. Not applied, `why` is what git said.
|
|
93
|
+
*/
|
|
94
|
+
export function bringBack(root, tree, scratch) {
|
|
95
|
+
const patch = treeDiff(tree, { binary: true });
|
|
96
|
+
if (patch.length === 0) return { applied: false, empty: true };
|
|
97
|
+
const at = git(root, ['rev-parse', '--show-toplevel']);
|
|
98
|
+
const plain = fed(at, ['apply', '--whitespace=nowarn'], patch);
|
|
99
|
+
if (plain.ok) return { applied: true, merged: false };
|
|
100
|
+
const unstaged = new Set(listed(git(at, ['diff', '--name-only', '-z'])));
|
|
101
|
+
const edited = listed(git(tree.top, ['diff', '--cached', '--name-only', '--no-renames', '-z', tree.base, '--', tree.prefix || '.'])).filter((f) => unstaged.has(f));
|
|
102
|
+
if (edited.length > 0) {
|
|
103
|
+
const named = `${edited.slice(0, 5).join(', ')}${edited.length > 5 ? ` and ${edited.length - 5} more` : ''}`;
|
|
104
|
+
return { applied: false, why: `${plain.said} — and ${named} ${edited.length === 1 ? 'has' : 'have'} edits in ${at} not staged, so a three-way merge could not be tried` };
|
|
105
|
+
}
|
|
106
|
+
const index = join(scratch, 'index.try');
|
|
107
|
+
try {
|
|
108
|
+
copyFileSync(git(at, ['rev-parse', '--path-format=absolute', '--git-path', 'index']), index);
|
|
109
|
+
const trial = fed(at, ['apply', '--whitespace=nowarn', '--3way', '--cached'], patch, { GIT_INDEX_FILE: index });
|
|
110
|
+
if (!trial.ok) return { applied: false, why: trial.said };
|
|
111
|
+
const merged = fed(at, ['apply', '--whitespace=nowarn', '--3way'], patch);
|
|
112
|
+
return merged.ok ? { applied: true, merged: true } : { applied: false, why: merged.said };
|
|
113
|
+
} finally {
|
|
114
|
+
rmSync(index, { force: true });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Removes the tree, and git's record of it; a tree git will not let go of is deleted all the same. Neither way follows a
|
|
120
|
+
* link: the project's dependencies linked in (`makeTree`) stay as they are.
|
|
121
|
+
*/
|
|
122
|
+
export function dropTree(root, top) {
|
|
123
|
+
try {
|
|
124
|
+
git(root, ['worktree', 'remove', '--force', top]);
|
|
125
|
+
} catch {
|
|
126
|
+
rmSync(top, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
}
|