sumo-agents 0.0.0-stage → 0.3.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 +38 -0
- package/LICENSE +21 -0
- package/README.md +111 -2
- package/bin/sumo.mjs +33 -0
- package/guides/delegation.md +33 -0
- package/guides/feature.md +14 -0
- package/guides/fix.md +14 -0
- package/guides/memory.md +23 -0
- package/guides/projects.md +27 -0
- package/guides/review.md +16 -0
- package/guides/workflows.md +26 -0
- package/logo.txt +19 -0
- package/package.json +42 -4
- package/prompts/agent.md +7 -0
- package/prompts/dream.md +13 -0
- package/prompts/scribe.md +21 -0
- package/spinner.txt +90 -0
- package/src/advisor.mjs +59 -0
- package/src/apply.mjs +368 -0
- package/src/auth.mjs +80 -0
- package/src/card.mjs +72 -0
- package/src/catalog.mjs +167 -0
- package/src/chat.mjs +1162 -0
- package/src/cli.mjs +815 -0
- package/src/cost.mjs +128 -0
- package/src/db.mjs +384 -0
- package/src/dream.mjs +97 -0
- package/src/editor.mjs +198 -0
- package/src/export.mjs +132 -0
- package/src/guard.mjs +121 -0
- package/src/guides.mjs +86 -0
- package/src/herdr.mjs +117 -0
- package/src/hooks.mjs +270 -0
- package/src/images.mjs +89 -0
- package/src/jobs.mjs +1184 -0
- package/src/jobtools.mjs +82 -0
- package/src/local-server.mjs +169 -0
- package/src/lock.mjs +45 -0
- package/src/loop.mjs +838 -0
- package/src/mcp.mjs +604 -0
- package/src/memory.mjs +400 -0
- package/src/model.mjs +270 -0
- package/src/page.html +304 -0
- package/src/page.mjs +135 -0
- package/src/paths.mjs +33 -0
- package/src/prime.mjs +177 -0
- package/src/projects.mjs +232 -0
- package/src/recall.mjs +45 -0
- package/src/redact.mjs +173 -0
- package/src/render.mjs +45 -0
- package/src/route.mjs +287 -0
- package/src/sandbox.mjs +173 -0
- package/src/scan.mjs +204 -0
- package/src/scribe.mjs +349 -0
- package/src/sessions.mjs +371 -0
- package/src/setup.mjs +528 -0
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +97 -0
- package/src/tools.mjs +987 -0
- package/src/transcript.mjs +106 -0
- package/src/tty.mjs +529 -0
- package/src/ui.mjs +1363 -0
- package/src/verify.mjs +369 -0
- package/src/web.mjs +49 -0
- package/src/workflows.mjs +270 -0
- package/src/worktree.mjs +128 -0
package/src/verify.mjs
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { isSecretPath } from './guard.mjs';
|
|
6
|
+
import { UsageError } from './memory.mjs';
|
|
7
|
+
import { secretShape } from './redact.mjs';
|
|
8
|
+
import { verifyCommands } from './scan.mjs';
|
|
9
|
+
import { killGroup } from './tools.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A job's work, judged by running things rather than by reading the report.
|
|
13
|
+
*
|
|
14
|
+
* One discipline, kept in one place: whoever did the work does not grade it.
|
|
15
|
+
* The project's own checks are run before the work and after it, so a failure
|
|
16
|
+
* that was already there is never blamed on the job and a new one is never
|
|
17
|
+
* waved through; and the tests that judge a change are not part of the change.
|
|
18
|
+
*
|
|
19
|
+
* Everything degrades rather than blocks when it cannot know: a project that is
|
|
20
|
+
* not in git has no "what changed", a project with no commands has nothing to
|
|
21
|
+
* run, and both are said in the verdict instead of being guessed at.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const DEFAULT_TIMEOUT_MS = 540_000; // under the ten minutes a harness gives one shell command
|
|
25
|
+
const OUTPUT_TAIL_LINES = 200;
|
|
26
|
+
/** How much of a log's end is read to find those lines: a check can print far more than is ever kept. */
|
|
27
|
+
const OUTPUT_TAIL_BYTES = 1024 * 1024;
|
|
28
|
+
const MAX_SCANNED_FILE_BYTES = 200_000;
|
|
29
|
+
|
|
30
|
+
/** Paths whose job is to judge other code. Fixtures beside tests count: editing one can weaken the test that reads it. */
|
|
31
|
+
const TEST_PATH = /(^|\/)(tests?|__tests__|specs?)\/|[._](test|spec)\.[a-z]+$|(^|\/)test_[^/]+\.py$/i;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The settings of a checker — the type checker, the linter, the test runner. Changing one can loosen what judges the
|
|
35
|
+
* change (`"strict": false`, a test path ignored) as surely as editing a test can. Flagged for a person, not blocking:
|
|
36
|
+
* a new path alias or a new lint rule are honest reasons to touch one.
|
|
37
|
+
*/
|
|
38
|
+
const CHECKER_CONFIG =
|
|
39
|
+
/(^|\/)(tsconfig[^/]*\.json|jsconfig\.json|\.eslintrc[^/]*|eslint\.config\.[cm]?[jt]s|(jest|vitest|karma|playwright)\.config\.[cm]?[jt]s|pytest\.ini|tox\.ini|setup\.cfg|conftest\.py|\.flake8|mypy\.ini|ruff\.toml|\.golangci\.ya?ml|phpunit\.xml(\.dist)?|\.rubocop\.yml)$/;
|
|
40
|
+
|
|
41
|
+
/** Ways of telling a checker to look away. Reported, never blocking: each has honest uses, and a person should see them. */
|
|
42
|
+
const LOOKS_AWAY =
|
|
43
|
+
/eslint-disable|@ts-ignore|@ts-nocheck|@ts-expect-error|#\s*noqa|#\s*type:\s*ignore|pylint:\s*disable|\/\/\s*nolint|#\[ignore\]|\b(?:it|test|describe)\.(?:skip|only)\b|\bx(?:it|describe)\(|@pytest\.mark\.skip|@unittest\.skip|\bt\.Skip\(|@Disabled\b|@Ignore\b/;
|
|
44
|
+
|
|
45
|
+
function git(root, args) {
|
|
46
|
+
try {
|
|
47
|
+
return execFileSync('git', ['-C', root, '-c', 'core.quotePath=false', ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
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
|
+
export const DIFF = ['diff', '--no-ext-diff', '--no-color', '--no-textconv', '--src-prefix=a/', '--dst-prefix=b/'];
|
|
55
|
+
|
|
56
|
+
const lines = (text) => (text ?? '').split('\n').filter(Boolean);
|
|
57
|
+
|
|
58
|
+
/** A repository with at least one commit — the least there must be for "what changed" to mean anything. */
|
|
59
|
+
export const hasCommit = (root) => Boolean(git(root, ['rev-parse', 'HEAD'])?.trim());
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether the recorded start can still be compared against. Nothing points at
|
|
63
|
+
* a snapshot commit, so git may collect it from a job left open for weeks —
|
|
64
|
+
* and a diff against a missing commit looks exactly like "nothing changed".
|
|
65
|
+
*/
|
|
66
|
+
/** The content hash of each file that is there, asked of git in one call; a file that is gone has none. */
|
|
67
|
+
function hashesOf(root, files) {
|
|
68
|
+
// An untracked repository inside the project is listed as `dir/`: it has no content hash, and asking for one fails the call for every file.
|
|
69
|
+
const there = files.filter((f) => !f.endsWith('/') && existsSync(join(root, f)));
|
|
70
|
+
if (there.length === 0) return new Map();
|
|
71
|
+
try {
|
|
72
|
+
const out = execFileSync('git', ['-C', root, 'hash-object', '--stdin-paths'], { input: `${there.join('\n')}\n`, encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024 });
|
|
73
|
+
return new Map(out.split('\n').slice(0, there.length).map((hash, i) => [there[i], hash]));
|
|
74
|
+
} catch {
|
|
75
|
+
// One path git will not hash (a directory, an odd name) must not hide the others: they are asked one at a time.
|
|
76
|
+
return new Map(there.map((f) => [f, git(root, ['hash-object', '--', f])?.trim()]).filter(([, hash]) => hash));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const stillThere = (root, snap) => git(root, ['cat-file', '-e', `${snap.base}^{commit}`]) !== null;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where the work starts from. `git stash create` writes the tree as it is —
|
|
84
|
+
* uncommitted edits included — into a commit nobody points at, touching neither
|
|
85
|
+
* the index nor the files, so "what this job changed" never includes what the
|
|
86
|
+
* user had already changed.
|
|
87
|
+
*/
|
|
88
|
+
export function snapshot(root) {
|
|
89
|
+
const head = git(root, ['rev-parse', 'HEAD'])?.trim();
|
|
90
|
+
if (!head) return null; // not a repository, or one with no commit to compare against
|
|
91
|
+
const stashed = git(root, ['stash', 'create'])?.trim();
|
|
92
|
+
const dirty = lines(git(root, ['status', '--porcelain', '--', '.'])).some((l) => !l.startsWith('??'));
|
|
93
|
+
const untracked = lines(git(root, ['ls-files', '--others', '--exclude-standard', '--', '.']));
|
|
94
|
+
const snap = {
|
|
95
|
+
base: stashed || head,
|
|
96
|
+
// A dirty tree that could not be captured (no git identity, say) leaves earlier edits mixed into the job's.
|
|
97
|
+
mixed: dirty && !stashed,
|
|
98
|
+
untracked,
|
|
99
|
+
// git says nothing about a file it does not track, so what each one held is written down here.
|
|
100
|
+
hashes: Object.fromEntries(hashesOf(root, untracked)),
|
|
101
|
+
};
|
|
102
|
+
// The tree before any work, so "has anything changed yet?" is a comparison and not a guess.
|
|
103
|
+
return { ...snap, fingerprint: changesSince(root, snap).fingerprint };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** What differs from the snapshot now, and a fingerprint of it, so a verdict can be tied to the exact tree it judged. */
|
|
107
|
+
export function changesSince(root, snap) {
|
|
108
|
+
// A diff git could not give — the start is gone, or the diff is too large to read — is not "nothing changed".
|
|
109
|
+
const diff = git(root, [...DIFF, snap.base, '--', '.']);
|
|
110
|
+
if (diff === null) throw new UsageError(`what changed in ${root} since ${snap.base.slice(0, 12)} could not be read from git — the recorded start is gone, or the diff is too large`);
|
|
111
|
+
const tracked = lines(git(root, [...DIFF, '--name-status', snap.base, '--', '.'])).map((l) => {
|
|
112
|
+
const [status, ...paths] = l.split('\t');
|
|
113
|
+
return { status: status[0], path: paths[0], to: paths[1] ?? null };
|
|
114
|
+
});
|
|
115
|
+
const before = new Set(snap.untracked);
|
|
116
|
+
const created = lines(git(root, ['ls-files', '--others', '--exclude-standard', '--', '.'])).filter((f) => !before.has(f));
|
|
117
|
+
// A file that was there before the work but never added to git: its content is compared with what was written down, since git will not.
|
|
118
|
+
const was = Object.entries(snap.hashes ?? {});
|
|
119
|
+
const now = hashesOf(root, [...created, ...was.map(([file]) => file)]);
|
|
120
|
+
const outside = was.filter(([file, hash]) => now.get(file) !== hash).map(([file]) => ({ status: now.has(file) ? 'M' : 'D', path: file, to: null, untracked: true }));
|
|
121
|
+
const hash = createHash('sha256').update(diff);
|
|
122
|
+
for (const file of [...created, ...outside.map((c) => c.path)]) hash.update(`\0${file}:${now.get(file) ?? ''}`);
|
|
123
|
+
return { tracked: [...tracked, ...outside], created, fingerprint: hash.digest('hex') };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function timeoutMs() {
|
|
127
|
+
const asked = Number(process.env.SUMO_AGENTS_CHECK_TIMEOUT_MS);
|
|
128
|
+
return Number.isFinite(asked) && asked > 0 ? asked : DEFAULT_TIMEOUT_MS;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The last lines of a check's log, read from its end rather than whole. */
|
|
132
|
+
function tailOf(file) {
|
|
133
|
+
const fd = openSync(file, 'r');
|
|
134
|
+
try {
|
|
135
|
+
const size = fstatSync(fd).size;
|
|
136
|
+
const start = Math.max(0, size - OUTPUT_TAIL_BYTES);
|
|
137
|
+
const bytes = Buffer.alloc(size - start);
|
|
138
|
+
const lines = bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, start)).toString('utf8').trimEnd().split('\n');
|
|
139
|
+
// Read from inside a line, the first piece is half of one.
|
|
140
|
+
if (start > 0 && lines.length > 1) lines.shift();
|
|
141
|
+
return lines.slice(-OUTPUT_TAIL_LINES).join('\n');
|
|
142
|
+
} finally {
|
|
143
|
+
closeSync(fd);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Runs each command where the project lives and keeps the end of what it printed, which is where runners put the failures. */
|
|
148
|
+
function runChecks(root, commands, dir, prefix) {
|
|
149
|
+
const named = new Map();
|
|
150
|
+
return commands.map(({ name, command }) => {
|
|
151
|
+
const started = Date.now();
|
|
152
|
+
// In a group of its own, the time limit reaches everything the check started. That takes it out of this process's group,
|
|
153
|
+
// which is what a stop kills — so a watcher in the check's group ends it once this process is gone.
|
|
154
|
+
const watcher = `(while kill -0 ${process.pid} 2>/dev/null; do sleep 1; done; kill -9 0) </dev/null >/dev/null 2>&1 &\n`;
|
|
155
|
+
// Two checks of one name each keep their own output.
|
|
156
|
+
const nth = (named.get(name) ?? 0) + 1;
|
|
157
|
+
named.set(name, nth);
|
|
158
|
+
const file = join(dir, `${prefix}-${name}${nth > 1 ? `-${nth}` : ''}.txt`);
|
|
159
|
+
// Printed into the file, not a pipe: a pipe stays open while anything the check left running holds it, and would be waited on to the time limit.
|
|
160
|
+
const into = openSync(file, 'w', 0o600);
|
|
161
|
+
let run;
|
|
162
|
+
try {
|
|
163
|
+
run = spawnSync(`${watcher}${command}`, { cwd: root, shell: true, detached: true, stdio: ['ignore', into, into], timeout: timeoutMs(), killSignal: 'SIGKILL' });
|
|
164
|
+
} finally {
|
|
165
|
+
closeSync(into);
|
|
166
|
+
}
|
|
167
|
+
const timedOut = run.error?.code === 'ETIMEDOUT';
|
|
168
|
+
// Whatever it left running goes with it, so nothing writes to the file once it is read.
|
|
169
|
+
killGroup(run.pid);
|
|
170
|
+
const output = tailOf(file);
|
|
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, status: run.status, timedOut, ms: Date.now() - started, file };
|
|
173
|
+
});
|
|
174
|
+
}
|
|
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
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The checks as they stand before the work. Refused once the tree has moved:
|
|
203
|
+
* a baseline taken after the first edit would call the job's own breakage
|
|
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.
|
|
207
|
+
*/
|
|
208
|
+
export function takeBaseline(root, snap, dir) {
|
|
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' };
|
|
210
|
+
if (snap && changesSince(root, snap).fingerprint !== snap.fingerprint) {
|
|
211
|
+
return { refused: 'files have already changed since this job began, so a baseline now would include your own edits' };
|
|
212
|
+
}
|
|
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)) };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Every line the job added, read once: a checker told to look away is flagged
|
|
220
|
+
* for a person; a credential shape only a real key has blocks; an assignment
|
|
221
|
+
* to password= or a long random string is flagged, since a fixture has those too.
|
|
222
|
+
*/
|
|
223
|
+
function scanAdded(root, snap, changes) {
|
|
224
|
+
const found = { away: [], key: [], maybe: [] };
|
|
225
|
+
const look = (where, l) => {
|
|
226
|
+
if (LOOKS_AWAY.test(l)) found.away.push(where);
|
|
227
|
+
const shape = secretShape(l);
|
|
228
|
+
if (shape) found[shape].push(where);
|
|
229
|
+
};
|
|
230
|
+
let file = null;
|
|
231
|
+
let at = 0;
|
|
232
|
+
for (const l of (git(root, [...DIFF, '-U0', snap.base, '--', '.']) ?? '').split('\n')) {
|
|
233
|
+
if (l.startsWith('+++ ')) file = l.slice(6);
|
|
234
|
+
else if (l.startsWith('@@')) at = Number(/\+(\d+)/.exec(l)?.[1] ?? 0) - 1;
|
|
235
|
+
else if (l.startsWith('+')) look(`${file}:${++at}`, l);
|
|
236
|
+
}
|
|
237
|
+
// A new file is read whole, and so is one that git never tracked: there is no diff to take its added lines from.
|
|
238
|
+
for (const created of [...changes.created, ...changes.tracked.filter((c) => c.untracked && c.status === 'M').map((c) => c.path)]) {
|
|
239
|
+
try {
|
|
240
|
+
const full = join(root, created);
|
|
241
|
+
if (statSync(full).size > MAX_SCANNED_FILE_BYTES) continue;
|
|
242
|
+
readFileSync(full, 'utf8').split('\n').forEach((l, i) => look(`${created}:${i + 1}`, l));
|
|
243
|
+
} catch {
|
|
244
|
+
// unreadable or binary: nothing to say about it
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return found;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const some = (list) => `${list.slice(0, 8).join(', ')}${list.length > 8 ? ` and ${list.length - 8} more` : ''}`;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The verdict. `blocking` is what stops a job being called done; `flags` are
|
|
254
|
+
* for a person to look at; `notes` say what could not be known.
|
|
255
|
+
*/
|
|
256
|
+
export function verify(root, { snap, baseline, testsMayChange, declared }, dir, now = new Date().toISOString()) {
|
|
257
|
+
const blocking = [];
|
|
258
|
+
const flags = [];
|
|
259
|
+
const notes = [];
|
|
260
|
+
const beforeChecks = snap && stillThere(root, snap) ? changesSince(root, snap).fingerprint : null;
|
|
261
|
+
|
|
262
|
+
// The checks that judged the start judge the end: one no longer run, or rewritten, is not part of the change.
|
|
263
|
+
const commands = verifyCommands(root);
|
|
264
|
+
const current = new Map(commands.map((c) => [c.command, c]));
|
|
265
|
+
const started = declared ?? baseline ?? [];
|
|
266
|
+
const dropped = started.filter((c) => !current.has(c.command)).map((c) => `\`${c.command}\``);
|
|
267
|
+
if (dropped.length > 0) blocking.push(`checks that were run when this job began are no longer run: ${dropped.join(', ')} — they judge this change and are not part of it`);
|
|
268
|
+
const rewritten = started.filter((c) => c.definition !== undefined && current.has(c.command) && current.get(c.command).definition !== c.definition).map((c) => `\`${c.command}\``);
|
|
269
|
+
if (rewritten.length > 0 && !testsMayChange) {
|
|
270
|
+
blocking.push(`checks that were already here were changed: ${rewritten.join(', ')} — they judge this change and are not part of it. If one is genuinely wrong, say why: sumo job ask`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// By command, not name: `cargo test` and `pytest` are both "test", and each has a start of its own.
|
|
274
|
+
const before = new Map((baseline ?? []).map((c) => [c.command, c]));
|
|
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) => {
|
|
280
|
+
const was = before.get(c.command);
|
|
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
|
+
}
|
|
289
|
+
if (state === 'fail') {
|
|
290
|
+
const why = c.timedOut ? 'ran past the time limit' : 'fails';
|
|
291
|
+
blocking.push(
|
|
292
|
+
was
|
|
293
|
+
? `\`${c.command}\` ${why}, and it passed when this job began — read ${c.file}`
|
|
294
|
+
: `\`${c.command}\` ${why}, and no baseline was taken before the work, so the failure counts as this job's — read ${c.file}`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
// A check that already failed cannot tell a new failure from the old one, so a person looks.
|
|
298
|
+
if (state === 'already-failing') flags.push(`\`${c.command}\` was already failing when this job began; compare ${was.file} with ${c.file} to see nothing new broke`);
|
|
299
|
+
return { name: c.name, command: c.command, state, file: c.file };
|
|
300
|
+
});
|
|
301
|
+
if (checks.length === 0) notes.push('this project declares no check, test, lint or typecheck command — nothing was run');
|
|
302
|
+
|
|
303
|
+
let fingerprint = null;
|
|
304
|
+
if (snap && !stillThere(root, snap)) {
|
|
305
|
+
notes.push('the recorded start of this job is no longer in the repository — what changed, and whether existing tests were touched, could not be checked');
|
|
306
|
+
} else if (snap) {
|
|
307
|
+
const changes = changesSince(root, snap);
|
|
308
|
+
fingerprint = changes.fingerprint;
|
|
309
|
+
if (beforeChecks !== null && beforeChecks !== fingerprint) {
|
|
310
|
+
blocking.push('files changed while the checks ran — run verification again on the resulting tree');
|
|
311
|
+
}
|
|
312
|
+
if (snap.mixed) notes.push('the tree had uncommitted edits that could not be set aside when this job began; "what changed" may include them');
|
|
313
|
+
const judges = changes.tracked.filter((c) => c.status !== 'A' && TEST_PATH.test(c.path)).map((c) => c.path);
|
|
314
|
+
if (judges.length > 0 && !testsMayChange) {
|
|
315
|
+
blocking.push(`tests that were already here were changed: ${judges.join(', ')} — they judge this change and are not part of it. If one is genuinely wrong, say why: sumo job ask`);
|
|
316
|
+
}
|
|
317
|
+
const secretFiles = [...changes.tracked.filter((c) => c.status !== 'D').map((c) => c.to ?? c.path), ...changes.created].filter(isSecretPath);
|
|
318
|
+
if (secretFiles.length > 0) blocking.push(`a secret file was added or changed: ${some(secretFiles)} — it belongs in the environment, never in the change`);
|
|
319
|
+
const added = scanAdded(root, snap, changes);
|
|
320
|
+
if (added.key.length > 0) blocking.push(`a key, token or private key was added: ${some(added.key)} — remove it and read it from the environment`);
|
|
321
|
+
if (added.away.length > 0) flags.push(`added lines tell a checker to look away (skip, ignore, disable): ${some(added.away)}`);
|
|
322
|
+
const settings = changes.tracked.filter((c) => c.status !== 'A' && CHECKER_CONFIG.test(c.path)).map((c) => c.path);
|
|
323
|
+
if (settings.length > 0) flags.push(`a checker's settings were changed: ${some(settings)} — see that nothing it checked was loosened`);
|
|
324
|
+
if (added.maybe.length > 0) flags.push(`added lines look like credentials (password=, token=, or a long random string): ${some(added.maybe)}`);
|
|
325
|
+
} else {
|
|
326
|
+
notes.push('not a git repository — what changed, and whether existing tests were touched, could not be checked');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return { at: now, ok: blocking.length === 0, fingerprint, checks, blocking, flags, notes };
|
|
330
|
+
}
|
|
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
|
+
|
|
335
|
+
/** The whole change as one file — what a reviewer reads instead of re-deriving it with git. */
|
|
336
|
+
export function writeChanges(root, snap, file) {
|
|
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`);
|
|
338
|
+
const changes = changesSince(root, snap);
|
|
339
|
+
const outside = changes.tracked.filter((c) => c.untracked);
|
|
340
|
+
const parts = [
|
|
341
|
+
`# Changes since ${snap.base.slice(0, 12)} in ${root}`,
|
|
342
|
+
snap.mixed ? '\n(uncommitted edits that were already there when the work began may be mixed in)' : '',
|
|
343
|
+
'\n## Files',
|
|
344
|
+
git(root, [...DIFF, '--stat', snap.base, '--', '.'])?.trimEnd() || '(no tracked file changed)',
|
|
345
|
+
...changes.created.map((f) => ` new file: ${f}`),
|
|
346
|
+
...outside.map((c) => ` ${c.status === 'D' ? 'removed' : 'changed'} (it was never in git): ${c.path}`),
|
|
347
|
+
'\n## Diff',
|
|
348
|
+
git(root, [...DIFF, '-U10', snap.base, '--', '.'])?.trimEnd() ?? '',
|
|
349
|
+
];
|
|
350
|
+
// What git never tracked has no earlier side to show: a new file, and one changed outside git, are given whole.
|
|
351
|
+
for (const created of [...changes.created, ...outside.filter((c) => c.status === 'M').map((c) => c.path)]) {
|
|
352
|
+
// `--no-index` exits 1 when the files differ, which is every time; the diff is on stdout either way.
|
|
353
|
+
const run = spawnSync('git', ['-C', root, '-c', 'core.quotePath=false', ...DIFF, '--no-index', '--', '/dev/null', created], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
|
|
354
|
+
parts.push(run.stdout?.trimEnd() ?? '');
|
|
355
|
+
}
|
|
356
|
+
writeFileSync(file, `${parts.filter(Boolean).join('\n')}\n`, { mode: 0o600 });
|
|
357
|
+
return { file, files: changes.tracked.length + changes.created.length };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** One line per check, for a status line or a report. */
|
|
361
|
+
export function verdictLines(verdict) {
|
|
362
|
+
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing', flaky: 'was flaky' };
|
|
363
|
+
return [
|
|
364
|
+
...verdict.checks.map((c) => `\`${c.command}\` ${words[c.state]}`),
|
|
365
|
+
...verdict.blocking.map((b) => `blocking: ${b}`),
|
|
366
|
+
...verdict.flags.map((f) => `look at: ${f}`),
|
|
367
|
+
...verdict.notes.map((n) => `note: ${n}`),
|
|
368
|
+
];
|
|
369
|
+
}
|
package/src/web.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web, as the API's own tools: a search and a page fetch that run on Anthropic's side, inside the request. Nothing is
|
|
3
|
+
* run here — the call and what came back are blocks of the reply, and go back with the conversation exactly as they came:
|
|
4
|
+
* a result's encrypted content is what the API reads it from on the next turn.
|
|
5
|
+
*
|
|
6
|
+
* Called directly. Left to their default the current versions are run from code the model writes to filter what comes
|
|
7
|
+
* back, and that code is refused (`too_many_requests`) on a Claude Code token; called directly they work on every model
|
|
8
|
+
* here, haiku included, so one definition serves them all and a conversation can change models with searches in it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** The most of one page that reaches the context: a large documentation page whole, a book's worth cut. A PDF is not counted this way and comes whole. */
|
|
12
|
+
const PAGE_TOKENS = 20_000;
|
|
13
|
+
|
|
14
|
+
export const WEB_SEARCH_TOOL = { type: 'web_search_20260318', name: 'web_search', allowed_callers: ['direct'] };
|
|
15
|
+
export const WEB_FETCH_TOOL = { type: 'web_fetch_20260318', name: 'web_fetch', allowed_callers: ['direct'], max_content_tokens: PAGE_TOKENS };
|
|
16
|
+
export const WEB_TOOLS = [WEB_SEARCH_TOOL, WEB_FETCH_TOOL];
|
|
17
|
+
|
|
18
|
+
/** US dollars for one search, on top of the tokens of what it found; a fetch costs its tokens only. */
|
|
19
|
+
export const SEARCH_USD = 0.01;
|
|
20
|
+
|
|
21
|
+
const isWebName = (name) => name === WEB_SEARCH_TOOL.name || name === WEB_FETCH_TOOL.name;
|
|
22
|
+
export const isWebCall = (block) => block.type === 'server_tool_use' && isWebName(block.name);
|
|
23
|
+
export const isWebResult = (block) => block.type === 'web_search_tool_result' || block.type === 'web_fetch_tool_result';
|
|
24
|
+
/** A call by its name alone, for the screen: a search or a fetch, wherever it ran. */
|
|
25
|
+
export const isWebTool = (call) => isWebName(call.name);
|
|
26
|
+
|
|
27
|
+
/** What a search or a fetch came back with, said as any tool's result is: the pages found, or the page read and its size; a failure by its code. */
|
|
28
|
+
export function webResult(block) {
|
|
29
|
+
const got = block.content;
|
|
30
|
+
if (block.type === 'web_search_tool_result') {
|
|
31
|
+
// Found, it is a list; failed, one object with the code.
|
|
32
|
+
if (!Array.isArray(got)) return { content: `search failed: ${got?.error_code ?? 'no reason given'}`, isError: true };
|
|
33
|
+
return { content: got.length === 0 ? 'nothing found' : got.map((r) => `${r.title} — ${r.url}`).join('\n'), isError: false };
|
|
34
|
+
}
|
|
35
|
+
if (got?.type !== 'web_fetch_result') return { content: `fetch failed: ${got?.error_code ?? 'no reason given'}`, isError: true };
|
|
36
|
+
const source = got.content?.source;
|
|
37
|
+
const size = source?.type === 'text' ? `${source.data.length} characters` : (source?.media_type ?? 'a document');
|
|
38
|
+
return { content: `${got.content?.title ?? got.url} — ${size}`, isError: false };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The pages a reply cites, each once, in the order it cites them. */
|
|
42
|
+
export function sourcesOf(content) {
|
|
43
|
+
const pages = new Map();
|
|
44
|
+
for (const block of content) {
|
|
45
|
+
if (block.type !== 'text') continue;
|
|
46
|
+
for (const cite of block.citations ?? []) if (cite.url && !pages.has(cite.url)) pages.set(cite.url, { title: cite.title ?? cite.url, url: cite.url });
|
|
47
|
+
}
|
|
48
|
+
return [...pages.values()];
|
|
49
|
+
}
|