sumo-agents 0.1.0 → 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 +3 -2
- package/README.md +97 -611
- package/bin/sumo.mjs +3 -1
- package/guides/delegation.md +8 -7
- package/package.json +3 -2
- package/prompts/agent.md +3 -3
- package/prompts/dream.md +1 -0
- package/prompts/scribe.md +1 -0
- package/src/apply.mjs +79 -19
- package/src/card.mjs +11 -6
- package/src/chat.mjs +215 -40
- package/src/cli.mjs +120 -31
- package/src/cost.mjs +128 -0
- package/src/db.mjs +40 -1
- package/src/export.mjs +1 -1
- package/src/hooks.mjs +8 -1
- package/src/jobs.mjs +606 -59
- package/src/jobtools.mjs +10 -7
- package/src/loop.mjs +358 -53
- package/src/mcp.mjs +44 -14
- package/src/memory.mjs +39 -4
- package/src/model.mjs +6 -5
- package/src/page.mjs +3 -1
- package/src/paths.mjs +9 -2
- package/src/prime.mjs +14 -5
- package/src/render.mjs +1 -0
- package/src/route.mjs +87 -9
- package/src/sandbox.mjs +173 -0
- package/src/scribe.mjs +7 -5
- package/src/sessions.mjs +9 -6
- package/src/setup.mjs +72 -7
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +8 -3
- package/src/tools.mjs +562 -31
- package/src/tty.mjs +5 -4
- package/src/ui.mjs +74 -18
- package/src/verify.mjs +51 -8
- package/src/worktree.mjs +128 -0
package/src/tty.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join, sep } from 'node:path';
|
|
4
4
|
import { isAdvisorTool } from './advisor.mjs';
|
|
5
|
-
import { compactInput, isMcpTool, splitMcpName } from './mcp.mjs';
|
|
5
|
+
import { compactInput, isMcpTool, splitMcpName, unlabeled } from './mcp.mjs';
|
|
6
6
|
import { REPO_ROOT } from './paths.mjs';
|
|
7
7
|
import { elapsed } from './text.mjs';
|
|
8
8
|
import { BASH_TOOL, DELEGATE_TOOL, EDITOR_TOOL } from './tools.mjs';
|
|
@@ -382,8 +382,8 @@ function diffView(old, now, line, full) {
|
|
|
382
382
|
export function toolView(call, result = null, { full = false, cwd = null } = {}) {
|
|
383
383
|
const input = call.input ?? {};
|
|
384
384
|
// Folded, the empty lines of what came back are not shown: three of five lines spent on nothing say nothing.
|
|
385
|
-
const output = () => {
|
|
386
|
-
const lines = toned(
|
|
385
|
+
const output = (content = result.content) => {
|
|
386
|
+
const lines = toned(content, result.isError ? 'error' : 'plain');
|
|
387
387
|
if (full) return lines;
|
|
388
388
|
const said = lines.filter((line) => line.text.trim() !== '');
|
|
389
389
|
return said.length === 0 ? [{ text: '(no output)', tone: 'dim' }] : ends(said, false);
|
|
@@ -421,7 +421,8 @@ export function toolView(call, result = null, { full = false, cwd = null } = {})
|
|
|
421
421
|
const { server, tool } = splitMcpName(call.name);
|
|
422
422
|
const detail = `${tool}(${compactInput(input)})`;
|
|
423
423
|
if (!result) return { title: server, detail, lines: [] };
|
|
424
|
-
|
|
424
|
+
// The line that tells the model whose data this is: here the title says it.
|
|
425
|
+
return { title: server, detail, lines: output(unlabeled(result.content)) };
|
|
425
426
|
}
|
|
426
427
|
if (isWebTool(call)) {
|
|
427
428
|
// The web: what was searched for or which page was read, and under it the pages found or the page and its size.
|
package/src/ui.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { createElement as h, useEffect, useReducer, useRef } from 'react';
|
|
|
6
6
|
import { compactLine, retryLine, routeLine, routeOf } from './chat.mjs';
|
|
7
7
|
import { liveFile } from './jobs.mjs';
|
|
8
8
|
import { stopReason } from './loop.mjs';
|
|
9
|
-
import { compactInput } from './mcp.mjs';
|
|
9
|
+
import { compactInput, INPUT_SHOWN } from './mcp.mjs';
|
|
10
10
|
import { paths } from './paths.mjs';
|
|
11
11
|
import { redact } from './redact.mjs';
|
|
12
12
|
import { elapsed } from './text.mjs';
|
|
@@ -304,11 +304,46 @@ function JobOpen({ id, job, rows }) {
|
|
|
304
304
|
);
|
|
305
305
|
}
|
|
306
306
|
|
|
307
|
-
/**
|
|
307
|
+
/** The most of a call's input the question shows in full; past it, the cut is said. A recipient or a body hidden past the cut is one the user never approved. */
|
|
308
|
+
const ASK_INPUT_MAX = 4_000;
|
|
309
|
+
|
|
310
|
+
/** A call waiting on the user's word: who asks, what would run with what — the whole input, not its first line — and the keys that answer. */
|
|
308
311
|
function Ask({ ask, aside = false }) {
|
|
309
312
|
const who = ask.job ? `j${ask.job}: ` : '';
|
|
313
|
+
// A command the sandbox blocked is asked as itself: the command, to run once outside the sandbox — yes or no, never always.
|
|
314
|
+
const command = ask.unsandboxed ? safeForTerminal(String(ask.input?.command ?? '')) : null;
|
|
315
|
+
const whole = command ?? JSON.stringify(ask.input ?? {});
|
|
316
|
+
// Short enough for the line, it is on the line; longer, it is laid out under it, every field whole, up to the cut.
|
|
317
|
+
const short = whole.length <= INPUT_SHOWN && !whole.includes('\n');
|
|
318
|
+
const laid = short ? [] : command !== null ? command.split('\n') : JSON.stringify(ask.input ?? {}, null, 2).split('\n');
|
|
319
|
+
const kept = [];
|
|
320
|
+
let size = 0;
|
|
321
|
+
for (const line of laid) {
|
|
322
|
+
if (size + line.length > ASK_INPUT_MAX) break;
|
|
323
|
+
kept.push(line);
|
|
324
|
+
size += line.length + 1;
|
|
325
|
+
}
|
|
326
|
+
const cut = laid.length - kept.length;
|
|
310
327
|
// Esc stops the turn the call is part of; for a job at work in the background there is no such turn, and Esc refuses the call.
|
|
311
|
-
return h(
|
|
328
|
+
return h(
|
|
329
|
+
Box,
|
|
330
|
+
{ marginTop: 1, flexDirection: 'column' },
|
|
331
|
+
h(Text, { color: 'yellow' }, command !== null ? `⚠ ${who}run this once outside the sandbox?${short ? ` ${whole}` : ''}` : `⚠ ${who}allow ${ask.server} · ${ask.tool}(${short ? whole : '…'})?`),
|
|
332
|
+
...kept.map((line, i) => h(Text, { key: i, color: 'yellow', wrap: 'truncate-end' }, ` ${line}`)),
|
|
333
|
+
cut > 0 ? h(Text, { color: 'yellow', dimColor: true }, ` … ${cut} more line${cut === 1 ? '' : 's'} of input not shown — n, and ask the model for less`) : null,
|
|
334
|
+
h(Text, { dimColor: true }, ` ${command !== null ? 'y yes · n no' : 'y once · a always · n no'} · esc ${aside ? 'no' : 'stop'}`),
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** A question of the model's: its options, the one the arrows are on, and the keys that answer — a pick, or words typed in the box. */
|
|
339
|
+
function Question({ ask }) {
|
|
340
|
+
return h(
|
|
341
|
+
Box,
|
|
342
|
+
{ marginTop: 1, flexDirection: 'column' },
|
|
343
|
+
h(Text, { color: 'yellow' }, `? ${ask.question}`),
|
|
344
|
+
...ask.options.map((option, i) => h(Text, { key: i, color: i === ask.at ? ACCENT : undefined }, ` ${i === ask.at ? '❯' : ' '} ${option.label}`, option.detail ? h(Text, { dimColor: true }, ` — ${option.detail}`) : null)),
|
|
345
|
+
h(Text, { dimColor: true }, ' ↑↓ choose · enter picks · or type your own answer · esc stop'),
|
|
346
|
+
);
|
|
312
347
|
}
|
|
313
348
|
|
|
314
349
|
/** One line of a reply; the first of a block carries the mark. */
|
|
@@ -434,14 +469,14 @@ function Status({ session, leaving, notice, jobs, aside = [], pick = null, open
|
|
|
434
469
|
const said = leaving || notice;
|
|
435
470
|
const placed = place && place.length <= room - (said ? hint.length + 2 : 0) ? place : '';
|
|
436
471
|
const hinted = room - (placed ? placed.length + 2 : 0) >= 12 ? hint : '';
|
|
437
|
-
// With jobs at work the keys are what is needed, and they are long:
|
|
472
|
+
// With jobs at work the keys are what is needed, and they are long: they follow the jobs, from the left, and give way in a
|
|
473
|
+
// narrow window; the route stands at the right, over the place, so nothing under the jobs reads as one more of them.
|
|
438
474
|
if (jobs.length > 0) {
|
|
439
|
-
const beside = place && place.length <= room ? place : '';
|
|
440
475
|
return h(
|
|
441
476
|
Box,
|
|
442
477
|
{ paddingX: 2, flexDirection: 'column' },
|
|
443
|
-
h(Box, { justifyContent: 'space-between', gap: 2 }, h(Box, { flexShrink:
|
|
444
|
-
h(
|
|
478
|
+
h(Box, { justifyContent: 'space-between', gap: 2 }, h(Box, { flexShrink: 1 }, h(Text, { dimColor: true, wrap: 'truncate' }, hint)), h(Box, { flexShrink: 0 }, h(Text, { dimColor: true }, route))),
|
|
479
|
+
place && place.length <= columns - 4 ? h(Box, { justifyContent: 'flex-end' }, h(Text, { dimColor: true }, place)) : null,
|
|
445
480
|
);
|
|
446
481
|
}
|
|
447
482
|
return h(
|
|
@@ -759,9 +794,9 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
759
794
|
record({ kind: 'user', text: line });
|
|
760
795
|
return record({ kind: 'reply', raw: helpText(session.commands), own: true });
|
|
761
796
|
}
|
|
762
|
-
if (control === 'model' || control === 'memory' || control === 'mcp') {
|
|
797
|
+
if (control === 'model' || control === 'memory' || control === 'mcp' || control === 'undo') {
|
|
763
798
|
record({ kind: 'user', text: line });
|
|
764
|
-
const text = control === 'model' ? session.route(args) : control === 'mcp' ? (await session.mcp()).join('\n') : await session.memoryPage();
|
|
799
|
+
const text = control === 'model' ? session.route(args) : control === 'undo' ? session.undo() : control === 'mcp' ? (await session.mcp()).join('\n') : await session.memoryPage();
|
|
765
800
|
return record({ kind: 'note', text });
|
|
766
801
|
}
|
|
767
802
|
record({ kind: 'user', text: line });
|
|
@@ -1001,6 +1036,8 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1001
1036
|
// A request being sent again, or a job's conversation summarized to make room: said where the work shows, with the job's id.
|
|
1002
1037
|
const onRetry = (r) => record({ kind: 'note', text: `${r.job ? `j${r.job}: ` : ''}${retryLine(r)}` });
|
|
1003
1038
|
const onCompact = (c) => record({ kind: 'note', text: `${c.job ? `j${c.job}: ` : ''}${compactLine(c)}` });
|
|
1039
|
+
// The session has passed what it may cost before it is said: said once, where the retries are.
|
|
1040
|
+
const onBudget = ({ text }) => record({ kind: 'note', text });
|
|
1004
1041
|
const onTool = ({ call, job }) => {
|
|
1005
1042
|
const at = state.jobs.get(job);
|
|
1006
1043
|
if (at) at.calls++;
|
|
@@ -1089,7 +1126,15 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1089
1126
|
const onApprove = (ask) => {
|
|
1090
1127
|
state.asks.push(ask);
|
|
1091
1128
|
ask.signal?.addEventListener('abort', () => settle(ask, 'no'), { once: true });
|
|
1092
|
-
alert(`${state.where.name}: ${ask.server} · ${ask.tool} waits for your answer`);
|
|
1129
|
+
alert(`${state.where.name}: ${ask.unsandboxed ? 'a command waits to run outside the sandbox' : `${ask.server} · ${ask.tool} waits for your answer`}`);
|
|
1130
|
+
redraw();
|
|
1131
|
+
};
|
|
1132
|
+
// A question of the model's: shown with its options, the first one picked, until a pick or a typed answer settles it.
|
|
1133
|
+
const onAsk = (ask) => {
|
|
1134
|
+
const standing = { ...ask, at: 0 };
|
|
1135
|
+
state.asks.push(standing);
|
|
1136
|
+
ask.signal?.addEventListener('abort', () => settle(standing, 'no'), { once: true });
|
|
1137
|
+
alert(`${state.where.name}: a question waits for your answer`);
|
|
1093
1138
|
redraw();
|
|
1094
1139
|
};
|
|
1095
1140
|
// An MCP server that is not there is the user's to see; the model is simply not offered its tools.
|
|
@@ -1097,9 +1142,9 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1097
1142
|
if (error) record({ kind: 'error', text: `mcp: ${error}` });
|
|
1098
1143
|
for (const server of servers.filter((s) => !s.ok)) record({ kind: 'error', text: `mcp ${server.name}: ${server.error} — sumo mcp shows every server` });
|
|
1099
1144
|
};
|
|
1100
|
-
events.on('text', onText).on('thinking', onThinking).on('retry', onRetry).on('compact', onCompact).on('tool', onTool).on('output', onOutput).on('heard', onHeard).on('guide', onGuide).on('sources', onSources).on('check', onCheck).on('result', onResult).on('usage', onUsage).on('job', onJob).on('tab-failed', onTabFailed).on('job-end', onJobEnd).on('job-report', onJobReport).on('approve', onApprove).on('mcp', onMcp);
|
|
1145
|
+
events.on('text', onText).on('thinking', onThinking).on('retry', onRetry).on('compact', onCompact).on('budget', onBudget).on('tool', onTool).on('output', onOutput).on('heard', onHeard).on('guide', onGuide).on('sources', onSources).on('check', onCheck).on('result', onResult).on('usage', onUsage).on('job', onJob).on('tab-failed', onTabFailed).on('job-end', onJobEnd).on('job-report', onJobReport).on('approve', onApprove).on('ask', onAsk).on('mcp', onMcp);
|
|
1101
1146
|
return () => {
|
|
1102
|
-
events.off('text', onText).off('thinking', onThinking).off('retry', onRetry).off('compact', onCompact).off('tool', onTool).off('output', onOutput).off('heard', onHeard).off('guide', onGuide).off('sources', onSources).off('check', onCheck).off('result', onResult).off('usage', onUsage).off('job', onJob).off('tab-failed', onTabFailed).off('job-end', onJobEnd).off('job-report', onJobReport).off('approve', onApprove).off('mcp', onMcp);
|
|
1147
|
+
events.off('text', onText).off('thinking', onThinking).off('retry', onRetry).off('compact', onCompact).off('budget', onBudget).off('tool', onTool).off('output', onOutput).off('heard', onHeard).off('guide', onGuide).off('sources', onSources).off('check', onCheck).off('result', onResult).off('usage', onUsage).off('job', onJob).off('tab-failed', onTabFailed).off('job-end', onJobEnd).off('job-report', onJobReport).off('approve', onApprove).off('ask', onAsk).off('mcp', onMcp);
|
|
1103
1148
|
clearTimeout(state.leaving);
|
|
1104
1149
|
clearTimeout(state.notice?.timer);
|
|
1105
1150
|
clearTimeout(state.drawSoon);
|
|
@@ -1145,11 +1190,21 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1145
1190
|
return;
|
|
1146
1191
|
}
|
|
1147
1192
|
for (const [one, oneKey] of keysOf(input, key)) {
|
|
1148
|
-
// A question
|
|
1149
|
-
|
|
1150
|
-
|
|
1193
|
+
// A question of the model's: on an empty box the arrows move the pick and Enter takes it. Anything else is the box's,
|
|
1194
|
+
// and Enter there sends what was typed as the answer; Esc and Ctrl-C go on to stop the turn.
|
|
1195
|
+
const asked = state.asks[0]?.options ? state.asks[0] : null;
|
|
1196
|
+
if (asked && !state.ed.text && (oneKey.upArrow || oneKey.downArrow || (oneKey.return && !oneKey.shift && !oneKey.meta))) {
|
|
1197
|
+
if (oneKey.return) settle(asked, { label: asked.options[asked.at].label });
|
|
1198
|
+
else asked.at = Math.max(0, Math.min(asked.options.length - 1, asked.at + (oneKey.downArrow ? 1 : -1)));
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
// A call standing takes the key: an answer, or Esc and Ctrl-C, which go on to stop the turn; nothing lands in the box.
|
|
1202
|
+
if (!asked && state.asks.length > 0 && !oneKey.escape && !(oneKey.ctrl && (one === 'c' || one === 'd'))) {
|
|
1203
|
+
// A run outside the sandbox is this one run or none: there is no always for it.
|
|
1204
|
+
const unsandboxed = state.asks[0].unsandboxed;
|
|
1205
|
+
const answer = (unsandboxed ? { y: 'once', n: 'no' } : { y: 'once', a: 'always', n: 'no' })[one];
|
|
1151
1206
|
if (answer) settle(state.asks[0], answer);
|
|
1152
|
-
else notify('y allows it once · a always · n refuses it · esc stops the turn');
|
|
1207
|
+
else notify(unsandboxed ? 'y runs it once outside the sandbox · n refuses it · esc stops the turn' : 'y allows it once · a always · n refuses it · esc stops the turn');
|
|
1153
1208
|
continue;
|
|
1154
1209
|
}
|
|
1155
1210
|
// A question from a job in the background: Esc refuses that one call, and stops nothing.
|
|
@@ -1224,7 +1279,8 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1224
1279
|
// The first step is onto the row that is not on the screen already: a job from the chat, the chat from a job's record.
|
|
1225
1280
|
if (oneKey.downArrow && pressed.state === state.ed && listed.length > 0) state.pick = state.open === null ? listed[1] : MAIN;
|
|
1226
1281
|
state.ed = pressed.state;
|
|
1227
|
-
if (pressed.submit !== undefined)
|
|
1282
|
+
if (pressed.submit !== undefined && asked) settle(asked, { text: pressed.submit });
|
|
1283
|
+
else if (pressed.submit !== undefined) submit(pressed.submit);
|
|
1228
1284
|
}
|
|
1229
1285
|
redraw();
|
|
1230
1286
|
});
|
|
@@ -1263,7 +1319,7 @@ function App({ session, events, messages, cwd, block, opening, logo, clipboard,
|
|
|
1263
1319
|
),
|
|
1264
1320
|
// The silence that is said is the model's: while a command, a job or a question is in hand, the wait is not on it.
|
|
1265
1321
|
state.working ? h(Working, { messages, first: state.working, tokens: state.tokens, thought: state.thought, quietAfterMs, heard: state.talking && state.running.size === 0 && state.jobs.size === 0 && state.asks.length === 0 ? state.heard : 0 }) : null,
|
|
1266
|
-
state.asks.length > 0 ? h(Ask, { ask: state.asks[0], aside: session.running().includes(state.asks[0].job) }) : null,
|
|
1322
|
+
state.asks.length > 0 ? (state.asks[0].options ? h(Question, { ask: state.asks[0] }) : h(Ask, { ask: state.asks[0], aside: session.running().includes(state.asks[0].job) })) : null,
|
|
1267
1323
|
// The record of the job that is open, under everything the turn is doing: its end, as it grows.
|
|
1268
1324
|
state.open !== null ? h(JobOpen, { id: state.open, job: state.jobs.get(state.open), rows }) : null,
|
|
1269
1325
|
// The logo is the empty room's: once something has been said, the room is the conversation's.
|
package/src/verify.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
|
-
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { isSecretPath } from './guard.mjs';
|
|
6
6
|
import { UsageError } from './memory.mjs';
|
|
@@ -51,7 +51,7 @@ function git(root, args) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/** A diff as git itself writes it, whatever the user has configured: no external tool, no colour, the usual prefixes — it is parsed, hashed and handed to a reviewer. */
|
|
54
|
-
const DIFF = ['diff', '--no-ext-diff', '--no-color', '--no-textconv', '--src-prefix=a/', '--dst-prefix=b/'];
|
|
54
|
+
export const DIFF = ['diff', '--no-ext-diff', '--no-color', '--no-textconv', '--src-prefix=a/', '--dst-prefix=b/'];
|
|
55
55
|
|
|
56
56
|
const lines = (text) => (text ?? '').split('\n').filter(Boolean);
|
|
57
57
|
|
|
@@ -169,21 +169,50 @@ function runChecks(root, commands, dir, prefix) {
|
|
|
169
169
|
killGroup(run.pid);
|
|
170
170
|
const output = tailOf(file);
|
|
171
171
|
writeFileSync(file, `$ ${command}\n${output}\n${timedOut ? '\n(stopped: it ran past the time limit)\n' : ''}`, { mode: 0o600 });
|
|
172
|
-
return { name, command, ok: run.status === 0, timedOut, ms: Date.now() - started, file };
|
|
172
|
+
return { name, command, ok: run.status === 0, status: run.status, timedOut, ms: Date.now() - started, file };
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/** A line of a check's log that names a failure: runners print the failing test, or the error, on it. */
|
|
177
|
+
const FAILURE_LINE = /\b(?:fail(?:s|ed|ing|ures?)?|errors?|not ok)\b|[✗✕✖]/i;
|
|
178
|
+
/** A time a runner prints, which differs from one run to the next when nothing else does. */
|
|
179
|
+
const TIMING = /\b\d+(?:\.\d+)?\s?m?s\b/g;
|
|
180
|
+
|
|
181
|
+
/** How a run of a check ended: its exit status and the lines that name a failure, without their times. Two runs that agree on it failed the same way. */
|
|
182
|
+
const endedAs = (run) =>
|
|
183
|
+
`${run.status}\n${[...new Set(readFileSync(run.file, 'utf8').split('\n').filter((l) => FAILURE_LINE.test(l)).map((l) => l.replace(TIMING, '').trim()))].sort().join('\n')}`;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The checks that failed, run once more, each beside the run it repeats — its log too, `<log>-again.txt`. One that ran past
|
|
187
|
+
* its time limit is not run again: that would take as long once more, past the ten minutes a harness gives the command that asked for it.
|
|
188
|
+
*/
|
|
189
|
+
function runAgain(root, failed, dir) {
|
|
190
|
+
const again = failed.filter((c) => !c.timedOut);
|
|
191
|
+
const runs = runChecks(root, again, dir, 'again');
|
|
192
|
+
return new Map(
|
|
193
|
+
again.map((c, i) => {
|
|
194
|
+
const file = c.file.replace(/\.txt$/, '-again.txt');
|
|
195
|
+
renameSync(runs[i].file, file);
|
|
196
|
+
return [c, { ...runs[i], file }];
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
176
201
|
/**
|
|
177
202
|
* The checks as they stand before the work. Refused once the tree has moved:
|
|
178
203
|
* a baseline taken after the first edit would call the job's own breakage
|
|
179
|
-
* "already there", which is the one thing it exists to prevent.
|
|
204
|
+
* "already there", which is the one thing it exists to prevent. A check that
|
|
205
|
+
* fails is run once more, and two runs that disagree — on the exit status, or
|
|
206
|
+
* on what they say failed — record it as flaky, with the second run's log.
|
|
180
207
|
*/
|
|
181
208
|
export function takeBaseline(root, snap, dir) {
|
|
182
209
|
if (snap && !stillThere(root, snap)) return { refused: 'the recorded start of this job is no longer in the repository, so there is no telling what has changed since' };
|
|
183
210
|
if (snap && changesSince(root, snap).fingerprint !== snap.fingerprint) {
|
|
184
211
|
return { refused: 'files have already changed since this job began, so a baseline now would include your own edits' };
|
|
185
212
|
}
|
|
186
|
-
|
|
213
|
+
const checks = runChecks(root, verifyCommands(root), dir, 'baseline');
|
|
214
|
+
const again = runAgain(root, checks.filter((c) => !c.ok), dir);
|
|
215
|
+
return { checks: checks.map((c) => (again.has(c) && endedAs(again.get(c)) !== endedAs(c) ? { ...c, flaky: true, again: again.get(c).file } : c)) };
|
|
187
216
|
}
|
|
188
217
|
|
|
189
218
|
/**
|
|
@@ -243,9 +272,20 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
243
272
|
|
|
244
273
|
// By command, not name: `cargo test` and `pytest` are both "test", and each has a start of its own.
|
|
245
274
|
const before = new Map((baseline ?? []).map((c) => [c.command, c]));
|
|
246
|
-
const
|
|
275
|
+
const ran = runChecks(root, commands, dir, 'verify');
|
|
276
|
+
// One that passed when the job began and fails now is run once more before it is held against the job: passing then, it is flaky.
|
|
277
|
+
// One recorded flaky at the start is not run again: whatever it says, it does not refuse DONE on its own.
|
|
278
|
+
const again = runAgain(root, ran.filter((c) => !c.ok && before.get(c.command)?.ok), dir);
|
|
279
|
+
const checks = ran.map((c) => {
|
|
247
280
|
const was = before.get(c.command);
|
|
248
|
-
const state = c.ok ? 'pass' : was && !was.ok ? 'already-failing' : 'fail';
|
|
281
|
+
const state = c.ok ? 'pass' : was?.flaky || again.get(c)?.ok ? 'flaky' : was && !was.ok ? 'already-failing' : 'fail';
|
|
282
|
+
if (state === 'flaky') {
|
|
283
|
+
flags.push(
|
|
284
|
+
was.flaky
|
|
285
|
+
? `\`${c.command}\` failed, and it was flaky when this job began — DONE is not refused on it; compare ${was.file} with ${c.file}`
|
|
286
|
+
: `\`${c.command}\` failed, then passed when run again — flaky, so DONE is not refused on it; read ${c.file}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
249
289
|
if (state === 'fail') {
|
|
250
290
|
const why = c.timedOut ? 'ran past the time limit' : 'fails';
|
|
251
291
|
blocking.push(
|
|
@@ -289,6 +329,9 @@ export function verify(root, { snap, baseline, testsMayChange, declared }, dir,
|
|
|
289
329
|
return { at: now, ok: blocking.length === 0, fingerprint, checks, blocking, flags, notes };
|
|
290
330
|
}
|
|
291
331
|
|
|
332
|
+
/** git's own summary of what changed in tracked files since the snapshot: a line a file, at most `count` of them, then its totals. Empty when git cannot say. */
|
|
333
|
+
export const diffStat = (root, snap, count) => git(root, [...DIFF, '--stat', `--stat-count=${count}`, snap.base, '--', '.'])?.trimEnd() ?? '';
|
|
334
|
+
|
|
292
335
|
/** The whole change as one file — what a reviewer reads instead of re-deriving it with git. */
|
|
293
336
|
export function writeChanges(root, snap, file) {
|
|
294
337
|
if (!stillThere(root, snap)) throw new UsageError(`the recorded start of that work (${snap.base.slice(0, 12)}) is no longer in the repository — there is no change to hand over`);
|
|
@@ -316,7 +359,7 @@ export function writeChanges(root, snap, file) {
|
|
|
316
359
|
|
|
317
360
|
/** One line per check, for a status line or a report. */
|
|
318
361
|
export function verdictLines(verdict) {
|
|
319
|
-
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing' };
|
|
362
|
+
const words = { pass: 'passed', fail: 'FAILED', 'already-failing': 'was already failing', flaky: 'was flaky' };
|
|
320
363
|
return [
|
|
321
364
|
...verdict.checks.map((c) => `\`${c.command}\` ${words[c.state]}`),
|
|
322
365
|
...verdict.blocking.map((b) => `blocking: ${b}`),
|
package/src/worktree.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { copyFileSync, cpSync, lstatSync, mkdirSync, rmSync, statSync, symlinkSync } from 'node:fs';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { DIFF } from './verify.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A worker's own git worktree, for when another worker already has the project's tree to itself. It is made from the
|
|
8
|
+
* project as it stands — uncommitted edits and untracked files included — without touching the user's files, index or
|
|
9
|
+
* branches, and what the work changed is brought back to the project at the end. Nothing here knows about jobs.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Sumo's own git calls: no hook of the user's runs, nothing is signed, and a commit has an author where git has no identity. */
|
|
13
|
+
const OWN = ['-c', 'core.hooksPath=/dev/null', '-c', 'commit.gpgsign=false', '-c', 'user.name=sumo', '-c', 'user.email=sumo@localhost'];
|
|
14
|
+
|
|
15
|
+
const git = (root, args) => execFileSync('git', ['-C', root, ...OWN, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 256 * 1024 * 1024 }).trim();
|
|
16
|
+
/** How a git command that reads a patch on stdin went: whether it succeeded, and the first lines of what it said, on one line. */
|
|
17
|
+
const fed = (root, args, patch, env = {}) => {
|
|
18
|
+
const run = spawnSync('git', ['-C', root, ...OWN, ...args], { input: patch, env: { ...process.env, ...env }, stdio: ['pipe', 'ignore', 'pipe'], encoding: 'utf8' });
|
|
19
|
+
return { ok: run.status === 0, said: (run.stderr ?? '').split('\n').map((l) => l.trim()).filter(Boolean).slice(0, 3).join('; ') };
|
|
20
|
+
};
|
|
21
|
+
/** The paths a NUL-separated git listing names. */
|
|
22
|
+
const listed = (out) => out.split('\0').filter(Boolean);
|
|
23
|
+
|
|
24
|
+
/** The folders at a project's top level its installed dependencies live in — git ignores them, so a tree is made without them. */
|
|
25
|
+
const DEPENDENCIES = ['node_modules', '.venv', 'venv'];
|
|
26
|
+
|
|
27
|
+
const isDir = (path) => {
|
|
28
|
+
try {
|
|
29
|
+
return statSync(path).isDirectory();
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** Whether anything is at a path, a link to nothing included. */
|
|
35
|
+
const taken = (path) => {
|
|
36
|
+
try {
|
|
37
|
+
return Boolean(lstatSync(path));
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Makes the tree at `top`: `git stash create` captures the tracked edits in a commit nobody points at (none: the head),
|
|
45
|
+
* the tree is checked out from it detached, and what git does not track is copied in and committed there, on the tree's
|
|
46
|
+
* own head — so the change brought back is the work's, and nothing the user already had. A project in a subdirectory of
|
|
47
|
+
* its repository works in the same subdirectory of the tree: that is `path`. The project's installed dependencies that
|
|
48
|
+
* exist are linked in, not copied, so its checks run there: `linked` names them.
|
|
49
|
+
*/
|
|
50
|
+
export function makeTree(root, top) {
|
|
51
|
+
const prefix = git(root, ['rev-parse', '--show-prefix']).replace(/\/$/, '');
|
|
52
|
+
const start = git(root, ['stash', 'create']) || git(root, ['rev-parse', 'HEAD']);
|
|
53
|
+
git(root, ['worktree', 'add', '--detach', top, start]);
|
|
54
|
+
const path = join(top, prefix);
|
|
55
|
+
// An untracked repository inside the project is listed as `dir/`: it is not part of this one, and is left out.
|
|
56
|
+
const untracked = git(root, ['ls-files', '--others', '--exclude-standard', '-z']).split('\0').filter((f) => f && !f.endsWith('/'));
|
|
57
|
+
for (const file of untracked) {
|
|
58
|
+
mkdirSync(dirname(join(path, file)), { recursive: true });
|
|
59
|
+
cpSync(join(root, file), join(path, file), { verbatimSymlinks: true });
|
|
60
|
+
}
|
|
61
|
+
// A link to the project's own folder: the sandbox follows it out of the job's roots, so the job reads the user's packages and cannot change them.
|
|
62
|
+
const linked = DEPENDENCIES.filter((name) => isDir(join(root, name)) && !taken(join(path, name)));
|
|
63
|
+
for (const name of linked) symlinkSync(join(root, name), join(path, name));
|
|
64
|
+
if (untracked.length > 0 || linked.length > 0) {
|
|
65
|
+
git(top, ['add', '-A']);
|
|
66
|
+
// `node_modules/` in a .gitignore does not match a link of that name, and info/exclude is the user's own, shared by every
|
|
67
|
+
// worktree: the links are committed on the tree's head instead, and marked in its own index as never changing — so whatever
|
|
68
|
+
// the job does to one, an install that puts a folder in its place included, no `git add -A` stages it and no diff shows it.
|
|
69
|
+
const links = linked.map((name) => join(prefix, name));
|
|
70
|
+
if (links.length > 0) git(top, ['add', '-f', '--', ...links]);
|
|
71
|
+
git(top, ['commit', '-q', '--no-verify', '-m', 'what the project held outside git, as it was']);
|
|
72
|
+
if (links.length > 0) git(top, ['update-index', '--skip-worktree', '--', ...links]);
|
|
73
|
+
}
|
|
74
|
+
return { top, path, prefix, base: git(top, ['rev-parse', 'HEAD']), linked };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Everything changed in the tree since it was made, new files included, as git itself writes a diff: with `binary`, whole enough to apply. */
|
|
78
|
+
export function treeDiff({ top, prefix, base }, { binary = false } = {}) {
|
|
79
|
+
git(top, ['add', '-A']);
|
|
80
|
+
return execFileSync('git', ['-C', top, ...DIFF, '--cached', ...(binary ? ['--binary'] : []), base, '--', prefix || '.'], {
|
|
81
|
+
encoding: binary ? 'buffer' : 'utf8',
|
|
82
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
83
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Brings the tree's change back to the project. Straight onto the files first: it applies whole or not at all, touches no
|
|
89
|
+
* index, and the user's uncommitted edits are the very start the work was made from. Where the project has moved since, a
|
|
90
|
+
* three-way merge — tried first on a copy of the index, since one that conflicts leaves its markers in the user's files —
|
|
91
|
+
* unless a file it touches has edits not staged in the project: git refuses a three-way merge over those, whatever the trial
|
|
92
|
+
* said. `scratch` is a folder of sumo's own for that copy. Not applied, `why` is what git said.
|
|
93
|
+
*/
|
|
94
|
+
export function bringBack(root, tree, scratch) {
|
|
95
|
+
const patch = treeDiff(tree, { binary: true });
|
|
96
|
+
if (patch.length === 0) return { applied: false, empty: true };
|
|
97
|
+
const at = git(root, ['rev-parse', '--show-toplevel']);
|
|
98
|
+
const plain = fed(at, ['apply', '--whitespace=nowarn'], patch);
|
|
99
|
+
if (plain.ok) return { applied: true, merged: false };
|
|
100
|
+
const unstaged = new Set(listed(git(at, ['diff', '--name-only', '-z'])));
|
|
101
|
+
const edited = listed(git(tree.top, ['diff', '--cached', '--name-only', '--no-renames', '-z', tree.base, '--', tree.prefix || '.'])).filter((f) => unstaged.has(f));
|
|
102
|
+
if (edited.length > 0) {
|
|
103
|
+
const named = `${edited.slice(0, 5).join(', ')}${edited.length > 5 ? ` and ${edited.length - 5} more` : ''}`;
|
|
104
|
+
return { applied: false, why: `${plain.said} — and ${named} ${edited.length === 1 ? 'has' : 'have'} edits in ${at} not staged, so a three-way merge could not be tried` };
|
|
105
|
+
}
|
|
106
|
+
const index = join(scratch, 'index.try');
|
|
107
|
+
try {
|
|
108
|
+
copyFileSync(git(at, ['rev-parse', '--path-format=absolute', '--git-path', 'index']), index);
|
|
109
|
+
const trial = fed(at, ['apply', '--whitespace=nowarn', '--3way', '--cached'], patch, { GIT_INDEX_FILE: index });
|
|
110
|
+
if (!trial.ok) return { applied: false, why: trial.said };
|
|
111
|
+
const merged = fed(at, ['apply', '--whitespace=nowarn', '--3way'], patch);
|
|
112
|
+
return merged.ok ? { applied: true, merged: true } : { applied: false, why: merged.said };
|
|
113
|
+
} finally {
|
|
114
|
+
rmSync(index, { force: true });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Removes the tree, and git's record of it; a tree git will not let go of is deleted all the same. Neither way follows a
|
|
120
|
+
* link: the project's dependencies linked in (`makeTree`) stay as they are.
|
|
121
|
+
*/
|
|
122
|
+
export function dropTree(root, top) {
|
|
123
|
+
try {
|
|
124
|
+
git(root, ['worktree', 'remove', '--force', top]);
|
|
125
|
+
} catch {
|
|
126
|
+
rmSync(top, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
}
|