sumo-agents 0.7.1 → 0.7.2
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/CHANGELOG.md +36 -0
- package/README.md +4 -2
- package/package.json +1 -1
- package/src/chat.mjs +40 -15
- package/src/jobs.mjs +40 -20
- package/src/loop.mjs +33 -4
- package/src/sessions.mjs +20 -14
- package/src/tools.mjs +78 -49
- package/src/verify.mjs +56 -11
- package/src/worktree.mjs +81 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,42 @@ What each release changed for you. The commit of each version has the full detai
|
|
|
4
4
|
|
|
5
5
|
## Unreleased
|
|
6
6
|
|
|
7
|
+
## 0.7.2 — 2026-11-18
|
|
8
|
+
|
|
9
|
+
- **A new failure can no longer hide in a suite that was already red.** A check failing when a worker began, and
|
|
10
|
+
still failing at the end, was let through whatever else had started failing. Now the failing tests are named
|
|
11
|
+
from the output at baseline and at the end. A test that fails now but did not then refuses DONE. When the
|
|
12
|
+
names can't be read from the output, the worker stays open with both logs kept, for you to look at and
|
|
13
|
+
accept with `sumo job finish <id> --status DONE --accept "<why>"`.
|
|
14
|
+
- **No check run is no longer a pass.** A worker in a project with no check, test, lint or typecheck command
|
|
15
|
+
could say DONE with nothing run. Now it stays open until you accept it with `--accept`. That includes a
|
|
16
|
+
chore in scratch. An accepted attempt is never the one picked from several.
|
|
17
|
+
- **`/undo` never throws away an edit you made after it.** It deleted a file the editor had created even after
|
|
18
|
+
you had written to it. Now every file of the turn is checked against what the editor last wrote before any
|
|
19
|
+
file is touched. A file changed since keeps the whole turn and names the file. An undo cut off halfway picks
|
|
20
|
+
up where it stopped.
|
|
21
|
+
- **Secrets the model repeats stay out of files.** A key the model echoed in its reply was written raw to the
|
|
22
|
+
session's transcript. Replies are now redacted there. A saved session is redacted all the way down, web
|
|
23
|
+
results and tool results inside other blocks included, and keeps no thinking.
|
|
24
|
+
- **A worker's change is kept whole before its worktree goes.** If `changes.diff` could not be written, the tree
|
|
25
|
+
was removed anyway, and a binary file came through as "files differ". Now `change.patch` is written beside it,
|
|
26
|
+
applicable with `git apply`, binary files included. If either can't be written, the worktree stays.
|
|
27
|
+
- **A worker in a worktree is tested on its own code.** A workspace package or editable install linked in from the
|
|
28
|
+
project loads the project's copy, so a check could pass on code the worker never changed. Such links are now
|
|
29
|
+
found (npm, pnpm and Python editable installs). Baseline and verify say which to install in the worktree.
|
|
30
|
+
- **Chat's "edited, nothing checked" is cleared only by a check that ran and passed.** Typing the check's name
|
|
31
|
+
into any command cleared it, before it ran, and in a project with no checks any command did. Now it takes
|
|
32
|
+
the project's own check, run whole without error. In a project with none, it takes a test runner run on
|
|
33
|
+
its own. Files changed by the shell in a git project count too.
|
|
34
|
+
- **A job stops at its time limit, even mid-request.** The limit was checked only between requests, so a slow
|
|
35
|
+
request, retry or tool could run on past it. The request in flight is now cut off at the deadline, and no tool
|
|
36
|
+
starts after it.
|
|
37
|
+
- **The test suite is green whatever your terminal says.** Two screen tests failed under `NO_COLOR=1`.
|
|
38
|
+
- **`probes/paired-eval.mjs` runs the coding eval through other harnesses.** It runs native sumo, Claude Code and
|
|
39
|
+
sumo-harness on the same commit with the same brief and the same hidden tests. The route is pinned and the
|
|
40
|
+
budget split per run. It reports verified DONE, false DONE, time and cost per verified task. Tested offline
|
|
41
|
+
with stand-in CLIs; no paid run has been made.
|
|
42
|
+
|
|
7
43
|
## 0.7.1 — 2026-11-18
|
|
8
44
|
|
|
9
45
|
- **A job's Herdr tab closes when the job is over, however it ended.** Before, only a job that ended done closed
|
package/README.md
CHANGED
|
@@ -84,10 +84,10 @@ are covered in [Install](https://github.com/sumododda/sumo-agents/wiki/Install).
|
|
|
84
84
|
- A worker's first DONE is answered with its task as a numbered checklist beside its change, one answer per
|
|
85
85
|
item; the second runs your checks.
|
|
86
86
|
A failed first attempt is retried once on the next model up, and risky work is reviewed, without asking.
|
|
87
|
-
A second worker on a git project works in a worktree of its own.
|
|
87
|
+
A second worker on a git project works in a worktree of its own. If no check runs, or a failing suite’s results cannot be compared with its baseline, the worker stays open. After inspecting the logs, you can record a manual acceptance with `sumo job finish <id> --status DONE --accept "<reason>"` and the report on stdin.
|
|
88
88
|
- A job's run stops at $10 or 60 minutes (`jobs.budget`, `jobs.minutes`). `sumo cost` shows where the money went,
|
|
89
89
|
and why the cache was written again when it was.
|
|
90
|
-
- `/undo`
|
|
90
|
+
- `/undo` restores the last editor turn. If a file has changed since, Sumo keeps the turn and tells you which file needs inspection.
|
|
91
91
|
- Only your words become memory. Web pages, files and tool output are never filed as yours.
|
|
92
92
|
- The local model, Qwen3-4B through llama.cpp, routes jobs and writes memory for $0 and sleeps when idle.
|
|
93
93
|
- A crash is written, redacted, to `~/.sumo-agents/logs/crash-*.log`. `sumo doctor --report` prints everything a
|
|
@@ -178,6 +178,8 @@ npm run build:bin # optional: a standalone binary in dist/sumo-agents/
|
|
|
178
178
|
|
|
179
179
|
`node probes/coding-eval.mjs --route sonnet/medium` runs real jobs on small tasks and scores them with hidden
|
|
180
180
|
tests; `--hard` runs the harder set, `--no-guides` and `--advisor` change one thing at a time. It costs money.
|
|
181
|
+
`node probes/paired-eval.mjs --route sonnet/medium --budget-usd 2` runs the same tasks through sumo and Claude Code
|
|
182
|
+
for a side-by-side, within that total.
|
|
181
183
|
|
|
182
184
|
A release is a commit on `main` that raises the version in `package.json`. The workflow runs the suite on
|
|
183
185
|
macOS, the platform sumo runs on, with Node 24; when it passes, the publish waits in the `release` environment for the
|
package/package.json
CHANGED
package/src/chat.mjs
CHANGED
|
@@ -28,7 +28,7 @@ import { ASK_TOOL, BASH_TOOL, cap, capRedacted, DELEGATE_TOOL, EDITOR_TOOL, INTE
|
|
|
28
28
|
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
29
29
|
import { closest, estimateTokens, head } from './text.mjs';
|
|
30
30
|
import { colourEnabled, createRenderer, explained, header, pathFrom, prompt, renderBlock, safeForTerminal, styles, tilde, toolView, widthOf, wrap } from './tty.mjs';
|
|
31
|
-
import { hasCommit } from './verify.mjs';
|
|
31
|
+
import { changesSince, hasCommit, snapshot } from './verify.mjs';
|
|
32
32
|
import { isWebTool, sourcesOf, WEB_FETCH_TOOL, WEB_SEARCH_TOOL, WEB_TOOLS } from './web.mjs';
|
|
33
33
|
|
|
34
34
|
/**
|
|
@@ -289,6 +289,11 @@ function configured(db, key) {
|
|
|
289
289
|
return getMeta(db, `config.${key}`) ?? CONFIG_DEFAULTS[key];
|
|
290
290
|
}
|
|
291
291
|
|
|
292
|
+
function isCheck(command) {
|
|
293
|
+
if (/[;&|<>\n`$]/.test(command) || /(?:^|\s)(?:--help|--version|-h|--collect-only)(?:\s|$)/.test(command)) return false;
|
|
294
|
+
return /^(?:node\s+--test|(?:python3?\s+-m\s+)?pytest|python3?\s+-m\s+unittest|go\s+test|cargo\s+test|(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:test|check|lint|typecheck)|make\s+(?:test|check)|test\s+-[efs]\s+\S+)(?:\s|$)/.test(command);
|
|
295
|
+
}
|
|
296
|
+
|
|
292
297
|
/** The chat's own tools, in a fixed order so its cached prefix never moves; the MCP servers' come after them. */
|
|
293
298
|
export const CHAT_TOOLS = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ASK_TOOL, ...WEB_TOOLS];
|
|
294
299
|
/**
|
|
@@ -713,7 +718,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
713
718
|
let ended = [];
|
|
714
719
|
try {
|
|
715
720
|
const outcome = await runJob(db, id, {
|
|
716
|
-
send
|
|
721
|
+
send,
|
|
717
722
|
signal,
|
|
718
723
|
approve,
|
|
719
724
|
inbox: () => inbox.splice(0),
|
|
@@ -894,20 +899,32 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
894
899
|
|
|
895
900
|
async function turn(text, said, images, automated = false, guide = null) {
|
|
896
901
|
stopper = new AbortController();
|
|
897
|
-
// Edits nothing has checked: an editor call that wrote adds its file — one refused, or whose old_str was not found,
|
|
898
|
-
// changed nothing and adds none; one of the project's declared checks running clears them — any command does when
|
|
899
|
-
// the project declares none. The gate at the end of the turn is told what is left. The shell can edit too (`sed -i`,
|
|
900
|
-
// a redirect); that is not seen.
|
|
901
902
|
const unchecked = new Set();
|
|
902
|
-
|
|
903
|
-
|
|
903
|
+
let beforeShell;
|
|
904
|
+
let shellRoot;
|
|
905
|
+
const watchShell = (call) => {
|
|
904
906
|
if (call.name !== BASH_TOOL.name) return;
|
|
905
|
-
const
|
|
906
|
-
if (
|
|
907
|
+
const { cwd } = workspace();
|
|
908
|
+
if (beforeShell === undefined || shellRoot !== cwd) {
|
|
909
|
+
shellRoot = cwd;
|
|
910
|
+
beforeShell = snapshot(cwd);
|
|
911
|
+
}
|
|
907
912
|
};
|
|
908
|
-
|
|
913
|
+
const declared = () => verifyCommands(workspace().cwd).map((c) => c.command);
|
|
914
|
+
// Only completed, foreground checks can cover prior edits. If the project declares none,
|
|
915
|
+
// recognise direct invocations of a test runner or file assertion. Compound commands can mask their exit status.
|
|
909
916
|
const noteResult = (call, result) => {
|
|
910
|
-
if (
|
|
917
|
+
if (result?.isError) return;
|
|
918
|
+
if (call.name === BASH_TOOL.name && (unchecked.size > 0 || beforeShell)) {
|
|
919
|
+
const command = String(call.input?.command ?? '').trim();
|
|
920
|
+
const checks = declared();
|
|
921
|
+
if (checks.includes(command) || (checks.length === 0 && isCheck(command))) {
|
|
922
|
+
unchecked.clear();
|
|
923
|
+
beforeShell = undefined;
|
|
924
|
+
}
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (call.name !== EDITOR_TOOL.name || call.input?.command === 'view' || !call.input?.path) return;
|
|
911
928
|
const { cwd: root } = workspace();
|
|
912
929
|
const path = resolve(root, String(call.input.path));
|
|
913
930
|
if (path.startsWith(`${root}${sep}`)) unchecked.add(path);
|
|
@@ -940,7 +957,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
940
957
|
if (stopper.signal.aborted) return { stop: 'interrupted', error: null, text: '' };
|
|
941
958
|
routed = chosen;
|
|
942
959
|
apply(routed);
|
|
943
|
-
logLine(transcript, { type: 'route', ...routed });
|
|
960
|
+
logLine(transcript, { type: 'route', ...routed, reason: routed.reason && redact(routed.reason).text });
|
|
944
961
|
watch({ type: 'route', ...routed });
|
|
945
962
|
}
|
|
946
963
|
settleTail();
|
|
@@ -981,9 +998,9 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
981
998
|
watch({ type: 'retry', ...r });
|
|
982
999
|
},
|
|
983
1000
|
onTool: (call) => {
|
|
1001
|
+
watchShell(call);
|
|
984
1002
|
activity(describeCall(call));
|
|
985
1003
|
watch({ type: 'tool', call });
|
|
986
|
-
noteCall(call);
|
|
987
1004
|
// A page or an MCP server read here: what this conversation says from now on may be what it was told.
|
|
988
1005
|
if (isWebTool(call) || isMcpTool(call.name)) noteSessionOutside(db, sessionId, call.name);
|
|
989
1006
|
},
|
|
@@ -993,7 +1010,8 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
993
1010
|
},
|
|
994
1011
|
onTurn: (response, totals) => {
|
|
995
1012
|
lastContext = totals.contextTokens;
|
|
996
|
-
|
|
1013
|
+
const content = response.content.filter((b) => b.type === 'text').map((b) => ({ type: 'text', text: redact(b.text).text }));
|
|
1014
|
+
logLine(transcript, { type: 'assistant', message: { role: 'assistant', model: response.model ?? params.model, usage: response.usage, content } });
|
|
997
1015
|
watch({ type: 'usage', totals });
|
|
998
1016
|
overBudget();
|
|
999
1017
|
// What the reply rests on, said under it: the pages it cites.
|
|
@@ -1014,6 +1032,12 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
1014
1032
|
// memory can answer, is told so and continued, once. Only a turn the model ended itself: after an error or a cut-off
|
|
1015
1033
|
// reply the turn is simply over, and the scribe still hears of it.
|
|
1016
1034
|
const asked = outcome.stop === 'end_turn';
|
|
1035
|
+
if (asked && beforeShell) {
|
|
1036
|
+
const { tracked, created, fingerprint } = changesSince(shellRoot, beforeShell);
|
|
1037
|
+
if (fingerprint !== beforeShell.fingerprint) {
|
|
1038
|
+
for (const name of [...tracked.map((file) => file.to ?? file.path), ...created]) unchecked.add(resolve(shellRoot, name));
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1017
1041
|
const edited = [...unchecked].map((path) => relative(workspace().cwd, path));
|
|
1018
1042
|
const checks = declared();
|
|
1019
1043
|
// The reply goes with the event either way, for the journal: also the one a continued turn ends on, which asks nothing more.
|
|
@@ -1034,6 +1058,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
1034
1058
|
function undo() {
|
|
1035
1059
|
const done = undoLast(undoDir());
|
|
1036
1060
|
if (!done) return 'nothing to undo: the editor has changed no file in this session — changes made through bash are not tracked';
|
|
1061
|
+
if (done.blocked) return done.blocked;
|
|
1037
1062
|
const { cwd: at } = workspace();
|
|
1038
1063
|
const names = (list) => {
|
|
1039
1064
|
const shown = list.map((path) => pathFrom(path, at));
|
package/src/jobs.mjs
CHANGED
|
@@ -21,7 +21,7 @@ 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
23
|
import { changesSince, diffStat, hasCommit, snapshot, stillThere, takeBaseline, verdictLines, verify, writeChanges } from './verify.mjs';
|
|
24
|
-
import { bringBack, dropTree, makeTree, treeDiff } from './worktree.mjs';
|
|
24
|
+
import { bringBack, dropTree, makeTree, sharedSourceDependencies, treeDiff } from './worktree.mjs';
|
|
25
25
|
|
|
26
26
|
const AGENTS = ['scout', 'worker', 'reviewer'];
|
|
27
27
|
/** Work with a written way of doing it: guides/<name>.md, carried into the brief on request. */
|
|
@@ -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.
|
|
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
|
}
|
|
@@ -697,9 +697,16 @@ function checkable(db, id, verb) {
|
|
|
697
697
|
return { job, state, root };
|
|
698
698
|
}
|
|
699
699
|
|
|
700
|
+
function sharedSourceReason(state, project) {
|
|
701
|
+
const shared = sharedSourceDependencies(state.tree, project.path);
|
|
702
|
+
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;
|
|
703
|
+
}
|
|
704
|
+
|
|
700
705
|
/** The project's checks as they stand before the work. Taken once; a second call reports what the first found. */
|
|
701
706
|
export function baseline(db, id) {
|
|
702
|
-
const { state, root } = checkable(db, id, 'given a baseline');
|
|
707
|
+
const { job, state, root } = checkable(db, id, 'given a baseline');
|
|
708
|
+
const shared = sharedSourceReason(state, getProject(db, job.project));
|
|
709
|
+
if (shared) return [`no baseline taken: ${shared}`];
|
|
703
710
|
if (!state.baseline) {
|
|
704
711
|
const taken = takeBaseline(root, state.snap, dirOf(id));
|
|
705
712
|
if (taken.refused) return [`no baseline taken: ${taken.refused}.`, 'Every check that fails at the end will count as this job\'s.'];
|
|
@@ -717,8 +724,13 @@ export function baseline(db, id) {
|
|
|
717
724
|
|
|
718
725
|
/** Runs the checks now and records the verdict against the exact tree it judged. */
|
|
719
726
|
export function verifyJob(db, id, now = new Date().toISOString()) {
|
|
720
|
-
const { state, root } = checkable(db, id, 'verified');
|
|
727
|
+
const { job, state, root } = checkable(db, id, 'verified');
|
|
721
728
|
const verdict = verify(root, state, dirOf(id), now);
|
|
729
|
+
const shared = sharedSourceReason(state, getProject(db, job.project));
|
|
730
|
+
if (shared) {
|
|
731
|
+
verdict.ok = false;
|
|
732
|
+
verdict.inconclusive = [...(verdict.inconclusive ?? []), shared];
|
|
733
|
+
}
|
|
722
734
|
writeState(db, id, { ...state, verdict });
|
|
723
735
|
return verdict;
|
|
724
736
|
}
|
|
@@ -742,10 +754,11 @@ export function changes(db, id) {
|
|
|
742
754
|
* whether the tree moved, so it is always run again.
|
|
743
755
|
*/
|
|
744
756
|
function verdictForDone(db, id, state, now) {
|
|
745
|
-
const
|
|
757
|
+
const project = getProject(db, getJob(db, id).project);
|
|
758
|
+
const root = workdir(state, project);
|
|
746
759
|
if (!existsSync(root)) return null;
|
|
747
760
|
const last = state.verdict;
|
|
748
|
-
if (last?.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
|
|
761
|
+
if (last?.version === 2 && !last.inconclusive?.length && !sharedSourceReason(state, project) && last.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
|
|
749
762
|
return verifyJob(db, id, now);
|
|
750
763
|
}
|
|
751
764
|
|
|
@@ -1022,6 +1035,7 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
|
|
|
1022
1035
|
// The checks can take minutes: report a job closed in the meantime before interpreting their verdict.
|
|
1023
1036
|
requireOpen(getJob(db, id), 'finished');
|
|
1024
1037
|
if (graded && accept === undefined && !verdict) throw unchecked(`${getProject(db, job.project).path} no longer exists, so its checks cannot be run`);
|
|
1038
|
+
if (verdict?.inconclusive?.length && !verdict.blocking.length) throw unchecked(verdict.inconclusive.join('\n'));
|
|
1025
1039
|
if (verdict && !verdict.ok) {
|
|
1026
1040
|
throw new Refusal(
|
|
1027
1041
|
[
|
|
@@ -1153,16 +1167,20 @@ function said(id, heading, lines) {
|
|
|
1153
1167
|
return lines;
|
|
1154
1168
|
}
|
|
1155
1169
|
|
|
1156
|
-
|
|
1170
|
+
/* Keep the review text and a complete, applicable patch before removing or merging a tree. */
|
|
1171
|
+
function archiveTree(id, state) {
|
|
1172
|
+
writeChanges(state.tree.path, state.snap, fileOf(id, 'changes.diff'));
|
|
1173
|
+
const patch = treeDiff(state.tree, { binary: true });
|
|
1174
|
+
const file = fileOf(id, 'change.patch');
|
|
1175
|
+
writeFileSync(file + '.tmp', patch, { mode: 0o600, flush: true });
|
|
1176
|
+
renameSync(file + '.tmp', file);
|
|
1177
|
+
return patch;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1157
1180
|
function release(id, project, state) {
|
|
1158
|
-
|
|
1159
|
-
try {
|
|
1160
|
-
writeChanges(state.tree.path, state.snap, kept);
|
|
1161
|
-
} catch {
|
|
1162
|
-
// A tree too broken to diff has nothing to keep.
|
|
1163
|
-
}
|
|
1181
|
+
archiveTree(id, state);
|
|
1164
1182
|
dropTree(project.path, state.tree.top);
|
|
1165
|
-
return
|
|
1183
|
+
return fileOf(id, 'changes.diff');
|
|
1166
1184
|
}
|
|
1167
1185
|
|
|
1168
1186
|
/** The newest other worker at work in the project itself — not in a worktree of its own — right now, or undefined. */
|
|
@@ -1175,25 +1193,27 @@ const inPlace = (db, project, id) => openWorkers(db, project.slug, id).find((o)
|
|
|
1175
1193
|
*/
|
|
1176
1194
|
function applyTree(db, id, project, state) {
|
|
1177
1195
|
const busy = inPlace(db, project, id);
|
|
1196
|
+
const patch = archiveTree(id, state);
|
|
1178
1197
|
if (busy) {
|
|
1179
|
-
const
|
|
1180
|
-
|
|
1181
|
-
const kept = release(id, project, state);
|
|
1198
|
+
const kept = fileOf(id, 'changes.diff');
|
|
1199
|
+
dropTree(project.path, state.tree.top);
|
|
1182
1200
|
if (patch.length === 0) return 'It changed nothing; its worktree is removed.';
|
|
1183
1201
|
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
1202
|
}
|
|
1185
|
-
const back = bringBack(project.path, state.tree, dirOf(id));
|
|
1203
|
+
const back = bringBack(project.path, state.tree, dirOf(id), patch);
|
|
1186
1204
|
if (!back.applied && !back.empty) {
|
|
1187
1205
|
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
1206
|
}
|
|
1189
|
-
|
|
1207
|
+
dropTree(project.path, state.tree.top);
|
|
1190
1208
|
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
1209
|
}
|
|
1192
1210
|
|
|
1193
1211
|
/** Done, judged by the checks, and its tree still the one they judged: an attempt worth keeping. */
|
|
1194
1212
|
function verifiedDone(db, id, state) {
|
|
1195
1213
|
const tree = state?.tree?.path;
|
|
1196
|
-
|
|
1214
|
+
const job = getJob(db, id);
|
|
1215
|
+
if (/^## Accepted without verification$/m.test(reportOf(id)) || sharedSourceReason(state, getProject(db, job.project))) return false;
|
|
1216
|
+
return job.status === 'done' && state.verdict?.version === 2 && state.verdict.ok === true && existsSync(tree ?? '') && unmoved(tree, state.snap, state.verdict.fingerprint);
|
|
1197
1217
|
}
|
|
1198
1218
|
|
|
1199
1219
|
/**
|
package/src/loop.mjs
CHANGED
|
@@ -506,6 +506,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
506
506
|
}
|
|
507
507
|
|
|
508
508
|
while (turns < MAX_TURNS) {
|
|
509
|
+
if (signal?.aborted) return interrupted();
|
|
509
510
|
// A job that has spent its budget sends nothing more: the last round it made is answered, and the run ends there.
|
|
510
511
|
if (overBudget()) {
|
|
511
512
|
stop = 'budget';
|
|
@@ -521,6 +522,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
521
522
|
let response;
|
|
522
523
|
// The same request, sent again while the API or the connection is the trouble; nothing of a failed try is kept.
|
|
523
524
|
for (let attempt = 0; ; ) {
|
|
525
|
+
if (overTime()) return { turns, stop: 'time', error: null, text, totals, toldLast };
|
|
524
526
|
try {
|
|
525
527
|
response = await send(params, { onText, onThinking, onBlock, signal });
|
|
526
528
|
break;
|
|
@@ -578,6 +580,10 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
578
580
|
if (!refused) text = textOf(response.content) || text;
|
|
579
581
|
for (const block of response.content) onBlock(block);
|
|
580
582
|
onTurn?.(response, totals);
|
|
583
|
+
if (overTime()) {
|
|
584
|
+
stop = 'time';
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
581
587
|
// The API paused a long turn of its own accord: the reply so far is kept, and the turn simply goes on.
|
|
582
588
|
if (response.stop_reason === 'pause_turn') continue;
|
|
583
589
|
if (response.stop_reason === 'end_turn') {
|
|
@@ -597,6 +603,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
597
603
|
|
|
598
604
|
totals.toolCalls += calls.length;
|
|
599
605
|
const answer = async (call) => {
|
|
606
|
+
if (overTime()) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: the job reached its time limit', is_error: true };
|
|
600
607
|
if (signal?.aborted) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: interrupted by the user', is_error: true };
|
|
601
608
|
onTool?.(call);
|
|
602
609
|
let out;
|
|
@@ -746,8 +753,6 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
746
753
|
const project = getProject(db, job.project);
|
|
747
754
|
const tree = treeOf(db, id);
|
|
748
755
|
if (tree && !existsSync(tree.path)) throw new UsageError(`j${id}'s worktree, ${tree.path}, no longer exists — abandon it and create it again`);
|
|
749
|
-
// The MCP servers of this process are the job's too: their tools behind the search tool, their names in its prompt.
|
|
750
|
-
const registry = await mcpReady();
|
|
751
756
|
// Whom this job may ask, by the setting and the switches as they are now: the next model up that is on, or nobody.
|
|
752
757
|
const advisor = (getMeta(db, 'config.jobs.advisor') ?? CONFIG_DEFAULTS['jobs.advisor']) === 'on' ? advisorOf(job.model, usableModels(db)) : null;
|
|
753
758
|
// What this job may spend, by the setting as it is now; shared by both runs, so the second goes on from what the first spent.
|
|
@@ -757,7 +762,6 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
757
762
|
// with an answer, say — counts from its own start, as its budget does: nothing of an earlier `sumo job run` is carried over.
|
|
758
763
|
const minutes = jobMinutes(db);
|
|
759
764
|
const clock = minutes === null ? null : { ms: minutes * 60_000, from: Date.parse(now?.() ?? new Date().toISOString()), warned: false };
|
|
760
|
-
const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry.tools(), advisor });
|
|
761
765
|
const lock = tx(db, () => takeRunLock(id));
|
|
762
766
|
const heard = () => [...(inbox?.() ?? []), ...takeInbox(id)];
|
|
763
767
|
// A command the sandbox blocked is put to the user with the job named, as its MCP calls are; its background commands are its own to end.
|
|
@@ -773,7 +777,29 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
773
777
|
const held = handleEvent(db, 'pre-tool', { session_id: job.session_id ?? `job:j${id}`, agent_id: `j${id}`, project: job.project, cwd: ctx.cwd, tool_name: 'bash', tool_input: { command: String(call.input?.command ?? '') } }, now?.());
|
|
774
778
|
return held ? { content: JSON.parse(held).deny, isError: true } : null;
|
|
775
779
|
};
|
|
780
|
+
const deadline = new AbortController();
|
|
781
|
+
let timer;
|
|
782
|
+
// Node timers accept at most a signed 32-bit delay.
|
|
783
|
+
function arm(ms) {
|
|
784
|
+
const delay = Math.min(ms, 2_147_483_647);
|
|
785
|
+
timer = setTimeout(() => ms > delay ? arm(ms - delay) : deadline.abort(), delay);
|
|
786
|
+
timer.unref?.();
|
|
787
|
+
}
|
|
788
|
+
if (clock) {
|
|
789
|
+
arm(clock.ms);
|
|
790
|
+
signal = signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal;
|
|
791
|
+
}
|
|
792
|
+
let cancelSetup;
|
|
776
793
|
try {
|
|
794
|
+
// The registry is shared by other conversations. Stop waiting for it when this job ends.
|
|
795
|
+
const stopped = new Promise((resolve) => {
|
|
796
|
+
cancelSetup = () => resolve(null);
|
|
797
|
+
if (signal?.aborted) cancelSetup();
|
|
798
|
+
else signal?.addEventListener('abort', cancelSetup, { once: true });
|
|
799
|
+
});
|
|
800
|
+
const registry = await Promise.race([mcpReady(), stopped]);
|
|
801
|
+
signal?.removeEventListener('abort', cancelSetup);
|
|
802
|
+
const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry?.tools(), advisor });
|
|
777
803
|
onStart?.(job);
|
|
778
804
|
// A job that outgrows the context is summarized and goes on; its brief is on disk for whatever the summary left out, and what code
|
|
779
805
|
// recorded of its work is said after the summary.
|
|
@@ -804,14 +830,17 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
804
830
|
const totals = Object.fromEntries(Object.keys(more.totals).map((k) => [k, k === 'contextTokens' ? more.totals[k] : outcome.totals[k] + more.totals[k]]));
|
|
805
831
|
outcome = { ...more, turns: outcome.turns + more.turns, text: more.text || outcome.text, totals, toldLast: outcome.toldLast || more.toldLast };
|
|
806
832
|
}
|
|
833
|
+
if (deadline.signal.aborted && signal.reason === deadline.signal.reason) outcome.stop = 'time';
|
|
807
834
|
// Kept for what follows the job: one told here to close near a limit is not retried past it (`followUp`, src/jobs.mjs).
|
|
808
|
-
toldToClose(db, id, budget?.warned ? 'budget' : clock?.warned ? 'time' : outcome.toldLast ? 'turns' : null);
|
|
835
|
+
toldToClose(db, id, budget?.warned ? 'budget' : (clock?.warned || outcome.stop === 'time') ? 'time' : outcome.toldLast ? 'turns' : null);
|
|
809
836
|
// What it left running in the background goes before its worktree is settled: still running, it would write into a tree being
|
|
810
837
|
// brought back and removed, or be there when the change lands in the project.
|
|
811
838
|
for (const group of ctx.background) killGroup(group);
|
|
812
839
|
const settled = await settle(db, id, now?.());
|
|
813
840
|
return { ...outcome, job: getJob(db, id), settled };
|
|
814
841
|
} finally {
|
|
842
|
+
clearTimeout(timer);
|
|
843
|
+
signal?.removeEventListener('abort', cancelSetup);
|
|
815
844
|
for (const group of ctx.background) killGroup(group);
|
|
816
845
|
rmSync(lock, { force: true });
|
|
817
846
|
}
|
package/src/sessions.mjs
CHANGED
|
@@ -489,23 +489,29 @@ const redactValues = (value) => {
|
|
|
489
489
|
return value;
|
|
490
490
|
};
|
|
491
491
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (
|
|
499
|
-
|
|
500
|
-
if (
|
|
501
|
-
if (
|
|
502
|
-
|
|
492
|
+
// Identifiers pair tool calls and results. The API needs encrypted web results and citation indices byte-for-byte.
|
|
493
|
+
const PROTOCOL_FIELDS = new Set(['type', 'name', 'id', 'tool_use_id', 'encrypted_content', 'encrypted_index', 'encrypted_stdout']);
|
|
494
|
+
|
|
495
|
+
/** Keep the API block structure for resume while scrubbing its human-readable contents, including nested web results. */
|
|
496
|
+
function keepBlock(value) {
|
|
497
|
+
if (typeof value === 'string') return redact(value).text;
|
|
498
|
+
if (Array.isArray(value)) return value.map(keepBlock);
|
|
499
|
+
if (!value || typeof value !== 'object') return value;
|
|
500
|
+
if (value.type === 'image') return { type: 'text', text: NO_PICTURE };
|
|
501
|
+
if (value.type === 'base64') return { type: 'text', media_type: 'text/plain', data: '[binary attachment omitted]' };
|
|
502
|
+
const kept = {};
|
|
503
|
+
for (const [key, child] of Object.entries(value)) {
|
|
504
|
+
if (key === 'cache_control') continue;
|
|
505
|
+
// Tool inputs contain arbitrary user data, even under keys that look like protocol fields.
|
|
506
|
+
if (key === 'input') kept[key] = redactValues(child);
|
|
507
|
+
else kept[key] = PROTOCOL_FIELDS.has(key) ? child : keepBlock(child);
|
|
508
|
+
}
|
|
503
509
|
return kept;
|
|
504
510
|
}
|
|
505
511
|
|
|
506
|
-
/**
|
|
512
|
+
/** What the session can replay after resuming. Thinking signatures cannot survive redaction or a changed prefix. */
|
|
507
513
|
function keepable(messages) {
|
|
508
|
-
return messages.map((m) => ({ role: m.role, content:
|
|
514
|
+
return withoutThinking(messages).map((m) => ({ role: m.role, content: keepBlock(m.content) }));
|
|
509
515
|
}
|
|
510
516
|
|
|
511
517
|
/**
|
|
@@ -526,7 +532,7 @@ export function saveState({ id, messages, model, effort, routed, cwd, contextTok
|
|
|
526
532
|
const file = stateFile(id);
|
|
527
533
|
// The summary first, so a listing can read it without the messages behind it; the route's reason last, since it is prose of any length.
|
|
528
534
|
// `pending` is what jobs reported that the model has not read yet: it goes with the session, to be read when it is picked up again.
|
|
529
|
-
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed, pending };
|
|
535
|
+
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed: redactValues(routed), pending: redactValues(pending) };
|
|
530
536
|
writeFileSync(`${file}.tmp`, JSON.stringify(state), { mode: 0o600 });
|
|
531
537
|
renameSync(`${file}.tmp`, file);
|
|
532
538
|
return file;
|
package/src/tools.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
4
|
import { homedir, tmpdir } from 'node:os';
|
|
4
5
|
import { delimiter, dirname, extname, join, relative, resolve, sep } from 'node:path';
|
|
5
6
|
import { CREDENTIAL_NAMES } from './auth.mjs';
|
|
@@ -868,23 +869,12 @@ function undoTurns(sessionDir) {
|
|
|
868
869
|
/** The folder the coming turn's undo goes in — made only when the editor first changes a file in it. */
|
|
869
870
|
export const nextUndoTurn = (sessionDir) => join(sessionDir, String((undoTurns(sessionDir).at(-1) ?? 0) + 1));
|
|
870
871
|
|
|
871
|
-
const
|
|
872
|
-
try {
|
|
873
|
-
return JSON.parse(readFileSync(join(turnDir, MANIFEST), 'utf8'));
|
|
874
|
-
} catch {
|
|
875
|
-
return null;
|
|
876
|
-
}
|
|
877
|
-
};
|
|
872
|
+
const digest = (data) => createHash('sha256').update(data).digest('hex');
|
|
878
873
|
|
|
879
|
-
/**
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
* UNDO_TURNS, and of the undo of sessions long untouched.
|
|
884
|
-
*/
|
|
885
|
-
function keepForUndo(turnDir, path, before) {
|
|
886
|
-
const manifest = readManifest(turnDir) ?? [];
|
|
887
|
-
if (manifest.some((entry) => entry.path === path)) return;
|
|
874
|
+
/** Keep the original bytes once, and the expected file identity and content after the last editor write. */
|
|
875
|
+
function keepForUndo(turnDir, path, before, written) {
|
|
876
|
+
const file = join(turnDir, MANIFEST);
|
|
877
|
+
const manifest = existsSync(turnDir) ? JSON.parse(readFileSync(file, 'utf8')) : [];
|
|
888
878
|
if (manifest.length === 0) {
|
|
889
879
|
const sessionDir = dirname(turnDir);
|
|
890
880
|
mkdirSync(turnDir, { recursive: true, mode: 0o700 });
|
|
@@ -895,39 +885,78 @@ function keepForUndo(turnDir, path, before) {
|
|
|
895
885
|
if (other !== sessionDir && Date.now() - statSync(other).mtimeMs > UNDO_KEEP_MS) rmSync(other, { recursive: true, force: true });
|
|
896
886
|
}
|
|
897
887
|
}
|
|
898
|
-
const
|
|
899
|
-
|
|
900
|
-
manifest.
|
|
901
|
-
|
|
888
|
+
const target = realpathSync.native(path);
|
|
889
|
+
const stat = statSync(target);
|
|
890
|
+
let entry = manifest.find((item) => item.path === path || item.target === target);
|
|
891
|
+
if (!entry) {
|
|
892
|
+
const kept = before === null ? null : String(manifest.length);
|
|
893
|
+
if (kept !== null) writeFileSync(join(turnDir, kept), before, { mode: 0o600, flag: 'wx' });
|
|
894
|
+
entry = { path, target, kept, original: before === null ? null : digest(before), dev: stat.dev, ino: stat.ino, nlink: stat.nlink };
|
|
895
|
+
manifest.push(entry);
|
|
896
|
+
} else if (before === null || entry.after !== digest(before) || entry.target !== target || entry.dev !== stat.dev || entry.ino !== stat.ino || entry.nlink !== stat.nlink) {
|
|
897
|
+
entry.conflict = true; // A later editor write cannot claim an intervening change as its own.
|
|
898
|
+
}
|
|
899
|
+
entry.after = digest(written);
|
|
900
|
+
writeFileSync(file + '.tmp', JSON.stringify(manifest), { mode: 0o600 });
|
|
901
|
+
renameSync(file + '.tmp', file);
|
|
902
902
|
}
|
|
903
903
|
|
|
904
|
-
/**
|
|
905
|
-
* /undo: every file the session's last kept turn changed, put back as it was before that turn — a file it created is
|
|
906
|
-
* deleted — and the turn let go, so the next /undo reaches the one before. Null when there is none to undo.
|
|
907
|
-
*/
|
|
904
|
+
/** Check every destination and backup before touching the first file. A conflict keeps the whole turn for inspection or retry. */
|
|
908
905
|
export function undoLast(sessionDir) {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
906
|
+
const n = undoTurns(sessionDir).at(-1);
|
|
907
|
+
if (n === undefined) return null;
|
|
908
|
+
const turnDir = join(sessionDir, String(n));
|
|
909
|
+
const cursor = join(turnDir, 'restoring');
|
|
910
|
+
let manifest, start, plan;
|
|
911
|
+
try {
|
|
912
|
+
manifest = JSON.parse(readFileSync(join(turnDir, MANIFEST), 'utf8'));
|
|
913
|
+
if (!Array.isArray(manifest) || !manifest.length) throw new Error('the undo record is incomplete');
|
|
914
|
+
// The cursor is written before each mutation. After an interruption, recognise a completed
|
|
915
|
+
// write or unlink at the cursor, and leave already restored files alone on the next /undo.
|
|
916
|
+
const resuming = existsSync(cursor);
|
|
917
|
+
start = resuming ? JSON.parse(readFileSync(cursor, 'utf8')) : 0;
|
|
918
|
+
if (!Number.isInteger(start) || start < 0 || start >= manifest.length) throw new Error('invalid undo progress');
|
|
919
|
+
plan = manifest.slice(start).map(({ path, target, kept, original, after, dev, ino, nlink, conflict }, offset) => {
|
|
920
|
+
if (!after || !target) throw new Error(`the saved record for ${path} has no verifiable after-state`);
|
|
921
|
+
if (conflict) throw new Error(`${path} was changed outside the editor during this turn`);
|
|
922
|
+
const atCursor = resuming && offset === 0;
|
|
923
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
924
|
+
if (atCursor && kept === null && original === null && !stat && !lstatSync(target, { throwIfNoEntry: false })) return null;
|
|
925
|
+
if (!stat?.isFile() || realpathSync.native(path) !== target || stat.dev !== dev || stat.ino !== ino || stat.nlink !== nlink) throw new Error(`${path} was removed or replaced`);
|
|
926
|
+
const content = digest(readFileSync(target));
|
|
927
|
+
if (atCursor && kept !== null && content === original) return null;
|
|
928
|
+
if (content !== after) throw new Error(`${path} changed after the editor wrote it`);
|
|
929
|
+
const remove = kept === null && original === null;
|
|
930
|
+
if (remove) accessSync(dirname(target), constants.W_OK | constants.X_OK);
|
|
931
|
+
else if (kept !== String(start + offset)) throw new Error(`invalid backup for ${path}`);
|
|
932
|
+
else accessSync(target, constants.W_OK);
|
|
933
|
+
const bytes = remove ? null : readFileSync(join(turnDir, kept));
|
|
934
|
+
if (bytes !== null && digest(bytes) !== original) throw new Error(`the backup for ${path} has changed`);
|
|
935
|
+
return { path, target, stat, after, bytes };
|
|
936
|
+
});
|
|
937
|
+
} catch (cause) {
|
|
938
|
+
return { blocked: `undo stopped before changing any files: ${cause.message}. The saved turn is at ${turnDir}.` };
|
|
939
|
+
}
|
|
940
|
+
try {
|
|
941
|
+
for (const [offset, entry] of plan.entries()) {
|
|
942
|
+
if (!entry) continue;
|
|
943
|
+
const { path, target, stat, after, bytes } = entry;
|
|
944
|
+
writeFileSync(cursor + '.tmp', String(start + offset), { mode: 0o600 });
|
|
945
|
+
renameSync(cursor + '.tmp', cursor);
|
|
946
|
+
const current = statSync(path);
|
|
947
|
+
if (realpathSync.native(path) !== target || current.dev !== stat.dev || current.ino !== stat.ino || current.nlink !== stat.nlink || digest(readFileSync(target)) !== after) {
|
|
948
|
+
throw new Error(`${path} changed while undo was running`);
|
|
925
949
|
}
|
|
950
|
+
if (bytes === null) rmSync(target);
|
|
951
|
+
else writeTo(target, bytes);
|
|
926
952
|
}
|
|
927
953
|
rmSync(turnDir, { recursive: true, force: true });
|
|
928
|
-
|
|
954
|
+
} catch (cause) {
|
|
955
|
+
return { blocked: `undo stopped while restoring files: ${cause.message}. Progress is saved in ${turnDir}. Retry /undo to resume.` };
|
|
929
956
|
}
|
|
930
|
-
|
|
957
|
+
const done = { restored: [], deleted: [] };
|
|
958
|
+
for (const { path, kept } of manifest) (kept === null ? done.deleted : done.restored).push(path);
|
|
959
|
+
return done;
|
|
931
960
|
}
|
|
932
961
|
|
|
933
962
|
/**
|
|
@@ -935,16 +964,16 @@ export function undoLast(sessionDir) {
|
|
|
935
964
|
* chat's turn folder), a change on disk since the model saw it is said first, and a parse the change broke — one that held
|
|
936
965
|
* before it — is said after. `created`: the file was not there, so it parsed before as far as anyone is concerned.
|
|
937
966
|
*/
|
|
938
|
-
function change(path, ctx,
|
|
967
|
+
function change(path, ctx, written, { created = false } = {}) {
|
|
939
968
|
const stale = created ? null : changedSince(path, ctx);
|
|
940
969
|
const before = ctx.undo && !created ? readFileSync(path) : null;
|
|
941
970
|
const broken = created ? null : parseError(path, ctx);
|
|
942
|
-
|
|
971
|
+
writeTo(path, written, { create: created });
|
|
943
972
|
// Kept once the write has gone through: a change refused — a create of a file already there — has nothing to undo. A keep
|
|
944
973
|
// that fails is said, not thrown: the change is made, and a model told it failed would make it twice.
|
|
945
974
|
let unkept = null;
|
|
946
975
|
try {
|
|
947
|
-
if (ctx.undo) keepForUndo(ctx.undo, path, before);
|
|
976
|
+
if (ctx.undo) keepForUndo(ctx.undo, path, before, written);
|
|
948
977
|
} catch (cause) {
|
|
949
978
|
unkept = `(this change could not be kept for /undo: ${cause.message})`;
|
|
950
979
|
}
|
|
@@ -1160,7 +1189,7 @@ export function runEditor(input, ctx) {
|
|
|
1160
1189
|
return result(`${path} already exists (${lines} line${lines === 1 ? '' : 's'}): change the lines that change with str_replace or insert — a file is never rewritten whole`, true);
|
|
1161
1190
|
}
|
|
1162
1191
|
mkdirSync(dirname(path), { recursive: true });
|
|
1163
|
-
const said = change(path, ctx,
|
|
1192
|
+
const said = change(path, ctx, String(input.file_text ?? ''), { created: true });
|
|
1164
1193
|
return result(said(`created ${path}`));
|
|
1165
1194
|
}
|
|
1166
1195
|
case 'str_replace': {
|
|
@@ -1178,7 +1207,7 @@ export function runEditor(input, ctx) {
|
|
|
1178
1207
|
if (text.includes('\r\n')) replacement = replacement.replace(/\r?\n/g, '\r\n');
|
|
1179
1208
|
if (where.reindent) replacement = where.reindent(replacement);
|
|
1180
1209
|
}
|
|
1181
|
-
const said = change(path, ctx,
|
|
1210
|
+
const said = change(path, ctx, `${text.slice(0, where.start)}${replacement}${text.slice(where.end)}`);
|
|
1182
1211
|
return { ...result(said(`edited ${path}${where.how ? ` (old_str matched ${where.how})` : ''}`)), edit: changeOf(text, where, replacement) };
|
|
1183
1212
|
}
|
|
1184
1213
|
case 'insert': {
|
|
@@ -1194,7 +1223,7 @@ export function runEditor(input, ctx) {
|
|
|
1194
1223
|
// A file ending in a newline splits to a last empty piece, which is not a line to insert after.
|
|
1195
1224
|
const at = Math.max(0, Math.min(lines.at(-1) === '' ? lines.length - 1 : lines.length, line));
|
|
1196
1225
|
lines.splice(at, 0, text.replace(/\n$/, ''));
|
|
1197
|
-
const said = change(path, ctx,
|
|
1226
|
+
const said = change(path, ctx, lines.join('\n'));
|
|
1198
1227
|
return { ...result(said(`inserted into ${path} after line ${at}`)), edit: { line: at + 1, old: '', new: redact(text.replace(/\n$/, '')).text } };
|
|
1199
1228
|
}
|
|
1200
1229
|
default:
|
package/src/verify.mjs
CHANGED
|
@@ -129,14 +129,15 @@ function timeoutMs() {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/** The last lines of a check's log, read from its end rather than whole. */
|
|
132
|
-
function
|
|
132
|
+
function checkOutput(fd, failed) {
|
|
133
133
|
const size = fstatSync(fd).size;
|
|
134
134
|
const start = Math.max(0, size - OUTPUT_TAIL_BYTES);
|
|
135
135
|
const bytes = Buffer.alloc(size - start);
|
|
136
136
|
const lines = bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, start)).toString('utf8').trimEnd().split('\n');
|
|
137
137
|
// Read from inside a line, the first piece is half of one.
|
|
138
138
|
if (start > 0 && lines.length > 1) lines.shift();
|
|
139
|
-
|
|
139
|
+
const failures = failed && start === 0 ? namedFailures(lines) : [];
|
|
140
|
+
return { output: lines.slice(-OUTPUT_TAIL_LINES).join('\n'), failures };
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
/** The sandbox a job's process hands down for its checks: the wrap's words, or null — none given, or one that is not a list of words. */
|
|
@@ -175,10 +176,10 @@ function runChecks(root, commands, dir, prefix) {
|
|
|
175
176
|
const timedOut = run.error?.code === 'ETIMEDOUT';
|
|
176
177
|
// Whatever it left running goes with it, so nothing writes to the file once it is read.
|
|
177
178
|
killGroup(run.pid);
|
|
178
|
-
const output =
|
|
179
|
+
const { output, failures } = checkOutput(into, run.status !== 0);
|
|
179
180
|
ftruncateSync(into);
|
|
180
181
|
writeSync(into, `$ ${command}\n${output}\n${timedOut ? '\n(stopped: it ran past the time limit)\n' : ''}`, 0);
|
|
181
|
-
return { name, command, ok: run.status === 0, status: run.status, timedOut, ms: Date.now() - started, file };
|
|
182
|
+
return { name, command, ok: run.status === 0, failures, status: run.status, timedOut, ms: Date.now() - started, file };
|
|
182
183
|
} finally {
|
|
183
184
|
closeSync(into);
|
|
184
185
|
}
|
|
@@ -190,6 +191,33 @@ const FAILURE_LINE = /\b(?:fail(?:s|ed|ing|ures?)?|errors?|not ok)\b|[✗✕✖]
|
|
|
190
191
|
/** A time a runner prints, which differs from one run to the next when nothing else does. */
|
|
191
192
|
const TIMING = /\b\d+(?:\.\d+)?\s?m?s\b/g;
|
|
192
193
|
|
|
194
|
+
/** Named cases printed by common test reporters. A wrapper's exit message or a failure count alone identifies no test. */
|
|
195
|
+
function namedFailures(lines) {
|
|
196
|
+
const names = [];
|
|
197
|
+
for (const raw of lines) {
|
|
198
|
+
const line = raw.replace(/\x1b\[[0-9;]*m/g, '').trim();
|
|
199
|
+
const match = /^(?:not ok(?: \d+)? - |--- FAIL: |(?:FAIL|FAILED) +|[✗✕✖×●] +)(.+)$/.exec(line)
|
|
200
|
+
?? /^test (.+?) \.\.\. FAILED$/.exec(line);
|
|
201
|
+
if (match) {
|
|
202
|
+
const name = match[1].replace(/\s*\(\d+(?:\.\d+)?\s?m?s\)$/, '').trim();
|
|
203
|
+
if (name) names.push(createHash('sha256').update(name).digest('hex'));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return names;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A new failure must not be forgiven just because the same command was red before. Count duplicate case names as well. */
|
|
210
|
+
function explainedBy(current, previous) {
|
|
211
|
+
if (!previous?.failures?.length || !current.failures.length || current.timedOut || previous.timedOut || previous.status !== current.status) return false;
|
|
212
|
+
const counts = new Map();
|
|
213
|
+
for (const name of previous.failures) counts.set(name, (counts.get(name) ?? 0) + 1);
|
|
214
|
+
for (const name of current.failures) {
|
|
215
|
+
if (!counts.get(name)) return false;
|
|
216
|
+
counts.set(name, counts.get(name) - 1);
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
|
|
193
221
|
/** 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. */
|
|
194
222
|
const endedAs = (run) =>
|
|
195
223
|
`${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')}`;
|
|
@@ -224,7 +252,12 @@ export function takeBaseline(root, snap, dir) {
|
|
|
224
252
|
}
|
|
225
253
|
const checks = runChecks(root, verifyCommands(root), dir, 'baseline');
|
|
226
254
|
const again = runAgain(root, checks.filter((c) => !c.ok), dir);
|
|
227
|
-
return { checks: checks.map((
|
|
255
|
+
return { checks: checks.map((check) => {
|
|
256
|
+
const repeat = again.get(check);
|
|
257
|
+
if (!repeat || endedAs(repeat) === endedAs(check)) return check;
|
|
258
|
+
const { status, failures, timedOut } = repeat;
|
|
259
|
+
return { ...check, flaky: true, again: repeat.file, repeat: { status, failures, timedOut } };
|
|
260
|
+
}) };
|
|
228
261
|
}
|
|
229
262
|
|
|
230
263
|
/**
|
|
@@ -269,6 +302,7 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
269
302
|
const blocking = [];
|
|
270
303
|
const flags = [];
|
|
271
304
|
const notes = [];
|
|
305
|
+
const inconclusive = [];
|
|
272
306
|
const beforeChecks = snap && stillThere(root, snap) ? changesSince(root, snap).fingerprint : null;
|
|
273
307
|
|
|
274
308
|
// The checks that judged the start judge the end: one no longer run, or rewritten, is not part of the change.
|
|
@@ -286,11 +320,15 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
286
320
|
const before = new Map((baseline ?? []).map((c) => [c.command, c]));
|
|
287
321
|
const ran = runChecks(root, commands, dir, 'verify');
|
|
288
322
|
// 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.
|
|
289
|
-
//
|
|
323
|
+
// A baseline failure can explain only failures actually observed in its recorded runs.
|
|
290
324
|
const again = runAgain(root, ran.filter((c) => !c.ok && before.get(c.command)?.ok), dir);
|
|
291
325
|
const checks = ran.map((c) => {
|
|
292
326
|
const was = before.get(c.command);
|
|
293
|
-
|
|
327
|
+
let state = c.ok ? 'pass' : 'fail';
|
|
328
|
+
if (!c.ok && again.get(c)?.ok) state = 'flaky';
|
|
329
|
+
if (!c.ok && was && !was.ok && (explainedBy(c, was) || explainedBy(c, was.repeat))) {
|
|
330
|
+
state = was.flaky ? 'flaky' : 'already-failing';
|
|
331
|
+
}
|
|
294
332
|
if (state === 'flaky') {
|
|
295
333
|
flags.push(
|
|
296
334
|
was.flaky
|
|
@@ -298,10 +336,16 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
298
336
|
: `\`${c.command}\` failed, then passed when run again — flaky, so DONE is not refused on it; read ${c.file}`,
|
|
299
337
|
);
|
|
300
338
|
}
|
|
339
|
+
if (state === 'fail' && was && !was.ok && !c.timedOut && (!c.failures.length || !was.failures?.length)) {
|
|
340
|
+
state = 'inconclusive';
|
|
341
|
+
inconclusive.push(`\`${c.command}\` failed, but its failing cases could not be compared; see ${was.file} and ${c.file}`);
|
|
342
|
+
}
|
|
301
343
|
if (state === 'fail') {
|
|
302
344
|
const why = c.timedOut ? 'ran past the time limit' : 'fails';
|
|
303
345
|
blocking.push(
|
|
304
|
-
was
|
|
346
|
+
was && !was.ok
|
|
347
|
+
? `\`${c.command}\` ${why} with a new failure or one the baseline cannot identify; compare ${was.file} with ${c.file}`
|
|
348
|
+
: was
|
|
305
349
|
? `\`${c.command}\` ${why}, and it passed when this job began — read ${c.file}`
|
|
306
350
|
: `\`${c.command}\` ${why}, and no baseline was taken before the work, so the failure counts as this job's — read ${c.file}`,
|
|
307
351
|
);
|
|
@@ -310,7 +354,7 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
310
354
|
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`);
|
|
311
355
|
return { name: c.name, command: c.command, state, file: c.file };
|
|
312
356
|
});
|
|
313
|
-
if (checks.length === 0)
|
|
357
|
+
if (checks.length === 0) inconclusive.push('this project declares no check, test, lint or typecheck command — nothing was run');
|
|
314
358
|
|
|
315
359
|
let fingerprint = null;
|
|
316
360
|
if (snap && !stillThere(root, snap)) {
|
|
@@ -338,7 +382,7 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
338
382
|
notes.push('not a git repository — what changed, and whether existing tests were touched, could not be checked');
|
|
339
383
|
}
|
|
340
384
|
|
|
341
|
-
return { at: now, ok: blocking.length === 0, fingerprint, checks, blocking, flags, notes };
|
|
385
|
+
return { version: 2, at: now, ok: blocking.length === 0 && inconclusive.length === 0, fingerprint, checks, blocking, inconclusive, flags, notes };
|
|
342
386
|
}
|
|
343
387
|
|
|
344
388
|
/** 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. */
|
|
@@ -371,9 +415,10 @@ export function writeChanges(root, snap, file) {
|
|
|
371
415
|
|
|
372
416
|
/** One line per check, for a status line or a report. */
|
|
373
417
|
export function verdictLines(verdict) {
|
|
374
|
-
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing', flaky: 'was flaky' };
|
|
418
|
+
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing', flaky: 'was flaky', inconclusive: 'was inconclusive' };
|
|
375
419
|
return [
|
|
376
420
|
...verdict.checks.map((c) => `\`${c.command}\` ${words[c.state]}`),
|
|
421
|
+
...(verdict.inconclusive ?? []).map((reason) => `unverified: ${reason}`),
|
|
377
422
|
...verdict.blocking.map((b) => `blocking: ${b}`),
|
|
378
423
|
...verdict.flags.map((f) => `look at: ${f}`),
|
|
379
424
|
...verdict.notes.map((n) => `note: ${n}`),
|
package/src/worktree.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
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';
|
|
2
|
+
import { copyFileSync, cpSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, sep } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
4
5
|
import { DIFF } from './verify.mjs';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -74,6 +75,83 @@ export function makeTree(root, top) {
|
|
|
74
75
|
return { top, path, prefix, base: git(top, ['rev-parse', 'HEAD']), linked };
|
|
75
76
|
}
|
|
76
77
|
|
|
78
|
+
const within = (root, path) => {
|
|
79
|
+
const rel = relative(root, path);
|
|
80
|
+
return rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Inspect package-manager links, including scoped packages and pnpm's virtual store. */
|
|
84
|
+
function nodeLoadsOriginal(modules, repository, candidate) {
|
|
85
|
+
const installed = realpathSync(modules);
|
|
86
|
+
const queue = [modules];
|
|
87
|
+
const seen = new Set();
|
|
88
|
+
while (queue.length) {
|
|
89
|
+
const dir = queue.pop();
|
|
90
|
+
const real = realpathSync(dir);
|
|
91
|
+
if (seen.has(real)) continue;
|
|
92
|
+
seen.add(real);
|
|
93
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
94
|
+
const path = join(dir, entry.name);
|
|
95
|
+
if (entry.name === '.pnpm' && entry.isDirectory()) {
|
|
96
|
+
for (const name of readdirSync(path)) if (isDir(join(path, name, 'node_modules'))) queue.push(join(path, name, 'node_modules'));
|
|
97
|
+
} else if (entry.name.startsWith('@') && entry.isDirectory()) queue.push(path);
|
|
98
|
+
else if (entry.isSymbolicLink()) {
|
|
99
|
+
const target = realpathSync(path);
|
|
100
|
+
if (within(repository, target) && !within(installed, target) && !within(candidate, target)) return true;
|
|
101
|
+
} else if (entry.isDirectory() && isDir(join(path, 'node_modules'))) queue.push(join(path, 'node_modules'));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Common editable-install formats: PEP 610/660 metadata, path-based .pth files and legacy egg links. */
|
|
108
|
+
function pythonLoadsOriginal(env, repository, candidate, shared) {
|
|
109
|
+
function loadsOriginal(path) {
|
|
110
|
+
const target = realpathSync(path);
|
|
111
|
+
return within(repository, target) && !within(candidate, target);
|
|
112
|
+
}
|
|
113
|
+
const lib = join(env, 'lib');
|
|
114
|
+
const sites = [join(env, 'Lib', 'site-packages')];
|
|
115
|
+
if (isDir(lib)) for (const name of readdirSync(lib)) sites.push(join(lib, name, 'site-packages'));
|
|
116
|
+
for (const site of sites.filter(isDir)) {
|
|
117
|
+
for (const name of readdirSync(site)) {
|
|
118
|
+
const file = join(site, name);
|
|
119
|
+
if (shared && (name.endsWith('.egg-link') || name.startsWith('__editable__'))) return true;
|
|
120
|
+
if (name.endsWith('.egg-link') || name.endsWith('.pth')) {
|
|
121
|
+
for (const line of readFileSync(file, 'utf8').split(/\r?\n/)) {
|
|
122
|
+
const path = line.trim();
|
|
123
|
+
if (isAbsolute(path) && taken(path) && loadsOriginal(path)) return true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (name.endsWith('.dist-info') && taken(join(file, 'direct_url.json'))) {
|
|
127
|
+
const metadata = JSON.parse(readFileSync(join(file, 'direct_url.json'), 'utf8'));
|
|
128
|
+
if (metadata.dir_info?.editable && (shared || (metadata.url?.startsWith('file:') && loadsOriginal(fileURLToPath(metadata.url))))) return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** An isolated job cannot be graded using source reached through the original checkout's installed dependencies. */
|
|
136
|
+
export function sharedSourceDependencies(tree, project) {
|
|
137
|
+
if (!tree) return [];
|
|
138
|
+
const shared = [];
|
|
139
|
+
for (const name of DEPENDENCIES) {
|
|
140
|
+
const dependency = join(tree.path, name);
|
|
141
|
+
if (!taken(dependency)) continue;
|
|
142
|
+
try {
|
|
143
|
+
const original = join(project, name);
|
|
144
|
+
const sharedInstall = taken(original) && realpathSync(dependency) === realpathSync(original);
|
|
145
|
+
const candidate = realpathSync(tree.top);
|
|
146
|
+
const repository = realpathSync(git(project, ['rev-parse', '--show-toplevel']));
|
|
147
|
+
if (name === 'node_modules' ? nodeLoadsOriginal(dependency, repository, candidate) : pythonLoadsOriginal(dependency, repository, candidate, sharedInstall)) shared.push(name);
|
|
148
|
+
} catch {
|
|
149
|
+
shared.push(name); // Unreadable installation or link: its source cannot be verified.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return shared;
|
|
153
|
+
}
|
|
154
|
+
|
|
77
155
|
/** 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
156
|
export function treeDiff({ top, prefix, base }, { binary = false } = {}) {
|
|
79
157
|
git(top, ['add', '-A']);
|
|
@@ -91,8 +169,7 @@ export function treeDiff({ top, prefix, base }, { binary = false } = {}) {
|
|
|
91
169
|
* unless a file it touches has edits not staged in the project: git refuses a three-way merge over those, whatever the trial
|
|
92
170
|
* said. `scratch` is a folder of sumo's own for that copy. Not applied, `why` is what git said.
|
|
93
171
|
*/
|
|
94
|
-
export function bringBack(root, tree, scratch) {
|
|
95
|
-
const patch = treeDiff(tree, { binary: true });
|
|
172
|
+
export function bringBack(root, tree, scratch, patch = treeDiff(tree, { binary: true })) {
|
|
96
173
|
if (patch.length === 0) return { applied: false, empty: true };
|
|
97
174
|
const at = git(root, ['rev-parse', '--show-toplevel']);
|
|
98
175
|
const plain = fed(at, ['apply', '--whitespace=nowarn'], patch);
|