sumo-agents 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +3 -2
- package/README.md +102 -611
- package/bin/sumo.mjs +3 -1
- package/guides/delegation.md +8 -7
- package/package.json +3 -2
- package/prompts/agent.md +3 -3
- package/prompts/dream.md +1 -0
- package/prompts/scribe.md +1 -0
- package/src/apply.mjs +79 -19
- package/src/card.mjs +11 -6
- package/src/chat.mjs +318 -54
- package/src/cli.mjs +120 -31
- package/src/cost.mjs +128 -0
- package/src/db.mjs +40 -1
- package/src/editor.mjs +110 -9
- package/src/export.mjs +1 -1
- package/src/herdr.mjs +5 -2
- package/src/hooks.mjs +13 -3
- package/src/jobs.mjs +606 -59
- package/src/jobtools.mjs +10 -7
- package/src/loop.mjs +358 -53
- package/src/mcp.mjs +44 -14
- package/src/memory.mjs +39 -4
- package/src/model.mjs +6 -5
- package/src/page.mjs +3 -1
- package/src/paths.mjs +9 -2
- package/src/prime.mjs +14 -5
- package/src/projects.mjs +18 -0
- package/src/render.mjs +1 -0
- package/src/route.mjs +110 -12
- package/src/sandbox.mjs +173 -0
- package/src/scribe.mjs +7 -5
- package/src/sessions.mjs +12 -8
- package/src/setup.mjs +76 -7
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +8 -3
- package/src/tools.mjs +562 -31
- package/src/tty.mjs +293 -43
- package/src/ui.mjs +507 -98
- package/src/verify.mjs +51 -8
- package/src/worktree.mjs +128 -0
package/src/jobs.mjs
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
1
2
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { appendFileSync, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
4
|
import { join } from 'node:path';
|
|
4
5
|
import { applyOps } from './apply.mjs';
|
|
5
6
|
import { card } from './card.mjs';
|
|
7
|
+
import { usableModels } from './catalog.mjs';
|
|
6
8
|
import { getMeta, setMeta, tx } from './db.mjs';
|
|
7
9
|
import { autoGuides, guideBody, matchGuide, WORKER_GUIDES } from './guides.mjs';
|
|
10
|
+
import { heldBy } from './lock.mjs';
|
|
8
11
|
import { UsageError } from './memory.mjs';
|
|
12
|
+
import { callModel, failure, logRun } from './model.mjs';
|
|
9
13
|
import { paths } from './paths.mjs';
|
|
10
14
|
import { preferences } from './prime.mjs';
|
|
11
15
|
import { getProject, isScratch } from './projects.mjs';
|
|
12
|
-
import { chooseRoute, statsLines } from './route.mjs';
|
|
16
|
+
import { chooseRoute, RETRY_IMPORTANT_THRESHOLD, statsLines } from './route.mjs';
|
|
13
17
|
import { verifyCommands } from './scan.mjs';
|
|
14
18
|
import { runScribe } from './scribe.mjs';
|
|
15
19
|
import { addCheckpoint, pendingTurns } from './sessions.mjs';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
20
|
+
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
21
|
+
import { clip, estimateTokens, head, STOPWORDS } from './text.mjs';
|
|
22
|
+
import { changesSince, diffStat, hasCommit, snapshot, stillThere, takeBaseline, verdictLines, verify, writeChanges } from './verify.mjs';
|
|
23
|
+
import { bringBack, dropTree, makeTree, treeDiff } from './worktree.mjs';
|
|
18
24
|
|
|
19
25
|
const AGENTS = ['scout', 'worker', 'reviewer'];
|
|
20
26
|
/** Work with a written way of doing it: guides/<name>.md, carried into the brief on request. */
|
|
@@ -22,9 +28,9 @@ const GUIDES = ['fix', 'feature', 'review'];
|
|
|
22
28
|
const BRIEF_CARD_BUDGET = 400;
|
|
23
29
|
/** The user's own rules — global preferences and decisions — ride with every brief, the way the memory block opens every chat. */
|
|
24
30
|
const BRIEF_RULES_BUDGET = 400;
|
|
31
|
+
/** Where the project stands as a job begins — branch, what is uncommitted, the top level, the toolchain — heading included. */
|
|
32
|
+
const BRIEF_SITUATION_BUDGET = 150;
|
|
25
33
|
const MAX_LEARNED = 5;
|
|
26
|
-
/** A failed job can always be retried; a done one only once review found enough wrong with it. */
|
|
27
|
-
const RETRY_IMPORTANT_THRESHOLD = 3;
|
|
28
34
|
|
|
29
35
|
export function parseJobId(raw) {
|
|
30
36
|
const match = /^j?(\d+)$/.exec(String(raw ?? '').trim());
|
|
@@ -48,6 +54,16 @@ const readOr = (file) => {
|
|
|
48
54
|
}
|
|
49
55
|
};
|
|
50
56
|
|
|
57
|
+
/** The lock a job's run holds while it runs (src/loop.mjs): it names the process. */
|
|
58
|
+
export const runLockFile = (id) => fileOf(id, 'run.lock');
|
|
59
|
+
/** Whether a job is being run right now: its run lock held by a live process. One a stop left open — budget, time, Esc — is not. */
|
|
60
|
+
const atWork = (id) => heldBy(runLockFile(id)) !== null;
|
|
61
|
+
/** How long a worker made and not yet run still counts as at work: the time between `sumo job new` and its run. */
|
|
62
|
+
const FRESH_MS = 10 * 60_000;
|
|
63
|
+
/** The open workers in a project, but one, the newest first. */
|
|
64
|
+
const openWorkers = (db, slug, id) => db.prepare(`SELECT * FROM jobs WHERE project = ? AND agent = 'worker' AND status IN ('running', 'needs_input') AND id <> ? ORDER BY id DESC`).all(slug, id);
|
|
65
|
+
/** A meta key taken off, so what it marked can happen again. */
|
|
66
|
+
const unmark = (db, key) => db.prepare('DELETE FROM meta WHERE key = ?').run(key);
|
|
51
67
|
function setStatus(db, id, status, now) {
|
|
52
68
|
db.prepare('UPDATE jobs SET status = ?, updated_at = ? WHERE id = ?').run(status, now, id);
|
|
53
69
|
}
|
|
@@ -109,13 +125,90 @@ function workerRules(job, { testsMayChange }) {
|
|
|
109
125
|
- A question you can settle yourself: decide, carry on, and list it under Decisions. Stop and ask only for something destructive or irreversible, security-sensitive, outside this project, or a task so unclear that every path is a guess.
|
|
110
126
|
- You do not start other jobs. Review comes after you, from someone who did not write the code.
|
|
111
127
|
- At each major milestone (a phase of the task done) \`note\` one line — \`<what now works>; next: <step>\`. Nothing else in between.
|
|
128
|
+
- A task of several steps: \`note\` the plan first, as a checklist — one \`- [ ] <step>\` a line — and at each milestone note the list again, whole, with what is done ticked \`- [x]\`; the latest list is put back in front of you as you work.
|
|
112
129
|
`;
|
|
113
130
|
}
|
|
114
131
|
|
|
115
|
-
|
|
132
|
+
/** The toolchains a project's own files call for, and the command that says each one's version. */
|
|
133
|
+
const TOOLCHAINS = [
|
|
134
|
+
{ name: 'node', files: ['package.json'], command: 'node', args: ['-v'] },
|
|
135
|
+
{ name: 'go', files: ['go.mod'], command: 'go', args: ['version'] },
|
|
136
|
+
{ name: 'python', files: ['pyproject.toml', 'requirements.txt'], command: 'python3', args: ['--version'] },
|
|
137
|
+
{ name: 'cargo', files: ['Cargo.toml'], command: 'cargo', args: ['--version'] },
|
|
138
|
+
];
|
|
139
|
+
const LOOK_TIMEOUT_MS = 3000;
|
|
140
|
+
const STATUS_LINES = 15;
|
|
141
|
+
const TOP_LEVEL_NAMES = 30;
|
|
142
|
+
|
|
143
|
+
/** As many of `items` as fit in `room` tokens, `max` at most, as `say(shown, left)` puts them: whole items are left out to fit, and counted. */
|
|
144
|
+
function fitted(items, max, room, say) {
|
|
145
|
+
let shown = items.slice(0, max);
|
|
146
|
+
while (shown.length > 0 && estimateTokens(say(shown, items.length - shown.length)) > room) shown = shown.slice(0, -1);
|
|
147
|
+
return say(shown, items.length - shown.length);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Where the project stands as a job begins — its branch, what is uncommitted, what is at its top level, the toolchain
|
|
152
|
+
* its files call for — so the job's first turns are not spent finding out. Each command is given three seconds and
|
|
153
|
+
* left out if it fails; outside git there are no git lines. What is uncommitted matters most, so the top level takes
|
|
154
|
+
* at most half the room. Empty when there is nothing to say.
|
|
155
|
+
*/
|
|
156
|
+
function whereThingsStand(root) {
|
|
157
|
+
const run = (command, args) => {
|
|
158
|
+
try {
|
|
159
|
+
return execFileSync(command, args, { cwd: root, encoding: 'utf8', timeout: LOOK_TIMEOUT_MS, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const git = (...args) => run('git', ['--no-optional-locks', '-c', 'core.quotePath=false', ...args]);
|
|
165
|
+
const repo = git('rev-parse', '--is-inside-work-tree')?.trim() === 'true';
|
|
166
|
+
const heading = '## Where things stand as this job begins — no need to look it up again';
|
|
167
|
+
const lines = [];
|
|
168
|
+
if (repo) {
|
|
169
|
+
const branch = git('branch', '--show-current')?.trim();
|
|
170
|
+
const head = branch ? null : git('rev-parse', '--short', 'HEAD')?.trim();
|
|
171
|
+
if (branch || head) lines.push(`Branch: ${branch || `none — detached at ${head}`}`);
|
|
172
|
+
}
|
|
173
|
+
const versions = TOOLCHAINS.filter((t) => t.files.some((f) => existsSync(join(root, f)))).flatMap((t) => {
|
|
174
|
+
const version = /\d+(?:\.\d+)+/.exec(run(t.command, t.args) ?? '')?.[0];
|
|
175
|
+
return version ? [`${t.name} ${version}`] : [];
|
|
176
|
+
});
|
|
177
|
+
if (versions.length > 0) lines.push(`Toolchain: ${versions.join(' · ')}`);
|
|
178
|
+
|
|
179
|
+
const cost = (text) => estimateTokens(text) + 1;
|
|
180
|
+
let room = BRIEF_SITUATION_BUDGET - cost(heading) - lines.reduce((sum, line) => sum + cost(line), 0);
|
|
181
|
+
const status = repo ? git('status', '--short', '--', '.') : null;
|
|
182
|
+
let names = null;
|
|
183
|
+
try {
|
|
184
|
+
names = readdirSync(root).filter((name) => !name.startsWith('.')).sort((a, b) => a.localeCompare(b));
|
|
185
|
+
} catch {
|
|
186
|
+
// A folder that cannot be read has no top level to show.
|
|
187
|
+
}
|
|
188
|
+
if (names) {
|
|
189
|
+
const isDir = (name) => {
|
|
190
|
+
try {
|
|
191
|
+
return statSync(join(root, name)).isDirectory();
|
|
192
|
+
} catch {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const marked = names.map((name, i) => (i < TOP_LEVEL_NAMES && isDir(name) ? `${name}/` : name));
|
|
197
|
+
const line = fitted(marked, TOP_LEVEL_NAMES, (status === null ? room : room / 2) - 1, (shown, left) => `Top level: ${[...shown, ...(left > 0 ? [`… ${left} more`] : [])].join(' ') || 'nothing'}`);
|
|
198
|
+
lines.push(line);
|
|
199
|
+
room -= cost(line);
|
|
200
|
+
}
|
|
201
|
+
if (status !== null) {
|
|
202
|
+
const changed = status.split('\n').filter((l) => l.trim());
|
|
203
|
+
lines.push(changed.length === 0 ? 'git status --short: clean' : fitted(changed, STATUS_LINES, room - 1, (shown, left) => ['git status --short:', ...shown, ...(left > 0 ? [`… ${left} more`] : [])].join('\n')));
|
|
204
|
+
}
|
|
205
|
+
return lines.length > 0 ? [heading, ...lines].join('\n') : '';
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function renderBrief({ job, project, known, rules, situation, task, guide, change, options = {} }) {
|
|
116
209
|
const closing =
|
|
117
210
|
job.agent === 'worker'
|
|
118
|
-
? `\`finish\` with DONE (or FAILED). DONE is not taken on your word: the project's checks
|
|
211
|
+
? `\`finish\` with DONE (or FAILED). DONE is not taken on your word: the first is answered with the task beside your change, to look over once more; the next has the project's checks run and compared with the baseline, and is refused while something new fails. See the verdict first with \`verify\` (allow it ten minutes). The report:`
|
|
119
212
|
: `\`finish\` with DONE (or FAILED), and this report:`;
|
|
120
213
|
return `# Job j${job.id} — ${job.title}
|
|
121
214
|
|
|
@@ -124,7 +217,7 @@ Work only inside: ${project.path} (bash starts there; give files by absolute p
|
|
|
124
217
|
|
|
125
218
|
## What is already known about this project
|
|
126
219
|
${known}
|
|
127
|
-
${rules ? `\n## The user's standing rules — they hold here too\n${rules}\n` : ''}
|
|
220
|
+
${rules ? `\n## The user's standing rules — they hold here too\n${rules}\n` : ''}${situation ? `\n${situation}\n` : ''}
|
|
128
221
|
## The task
|
|
129
222
|
${task.trim()}
|
|
130
223
|
${change ? `\n## The change to judge\n${change}\n` : ''}${guide ? `\n## How this kind of work is done here\n${guide}\n` : ''}
|
|
@@ -143,7 +236,7 @@ const stateKey = (id) => `job.${id}.verify`;
|
|
|
143
236
|
* The job's shell can reach its folder, so the file is checked against the digest sumo kept when it wrote it:
|
|
144
237
|
* a record changed since — a baseline made to fail, a verdict made to pass — reads as none.
|
|
145
238
|
*/
|
|
146
|
-
const readState = (db, id) => {
|
|
239
|
+
export const readState = (db, id) => {
|
|
147
240
|
try {
|
|
148
241
|
const text = readFileSync(fileOf(id, 'verify.json'), 'utf8');
|
|
149
242
|
const kept = getMeta(db, stateKey(id));
|
|
@@ -188,11 +281,19 @@ function defaultGuide(db, { agent, title, task }) {
|
|
|
188
281
|
function changeToJudge(db, { project, reviews }) {
|
|
189
282
|
const lines = [];
|
|
190
283
|
let snap;
|
|
284
|
+
let root = project.path;
|
|
285
|
+
let kept = null;
|
|
191
286
|
if (reviews !== undefined) {
|
|
192
287
|
const target = getJob(db, reviews);
|
|
193
288
|
if (target.project !== project.slug) throw new UsageError(`j${target.id} belongs to ${target.project}, not ${project.slug}`);
|
|
194
|
-
|
|
289
|
+
const state = readState(db, target.id);
|
|
290
|
+
snap = state?.snap;
|
|
291
|
+
// A change still in the worktree it was made in is judged there.
|
|
292
|
+
if (state?.tree && existsSync(state.tree.path)) root = state.tree.path;
|
|
195
293
|
if (!snap) throw new UsageError(`j${target.id} recorded no starting point (not a worker job, or not a git repository) — there is no change to hand over`);
|
|
294
|
+
// One whose worktree is gone is judged by what it kept as it went (`changes`), not by the project against the tree's start:
|
|
295
|
+
// the project holds that change and more — what git ignores, what was never added — none of it the job's.
|
|
296
|
+
if (state.tree && !existsSync(state.tree.path)) kept = changes(db, target.id);
|
|
196
297
|
lines.push(`What was asked: ${fileOf(target.id, 'brief.md')} (read "## The task")`);
|
|
197
298
|
if (existsSync(fileOf(target.id, 'report.md'))) {
|
|
198
299
|
lines.push(`What its author says they did: ${fileOf(target.id, 'report.md')} — claims, not facts. A stated reason never makes a finding smaller.`);
|
|
@@ -201,9 +302,13 @@ function changeToJudge(db, { project, reviews }) {
|
|
|
201
302
|
if (!hasCommit(project.path)) throw new UsageError(`${project.path} is not a git repository with a commit — there is no change to hand over`);
|
|
202
303
|
snap = { base: 'HEAD', mixed: false, untracked: [] };
|
|
203
304
|
}
|
|
204
|
-
|
|
305
|
+
if (kept) {
|
|
306
|
+
if (kept.files === 0) throw new UsageError('nothing has changed — there is nothing to review');
|
|
307
|
+
return { snap, lines, root, kept };
|
|
308
|
+
}
|
|
309
|
+
const { tracked, created } = changesSince(root, snap);
|
|
205
310
|
if (tracked.length + created.length === 0) throw new UsageError('nothing has changed — there is nothing to review');
|
|
206
|
-
return { snap, lines };
|
|
311
|
+
return { snap, lines, root };
|
|
207
312
|
}
|
|
208
313
|
|
|
209
314
|
const reviewsFile = (id) => fileOf(id, 'reviews.json');
|
|
@@ -219,14 +324,47 @@ function reviewTargetOf(id) {
|
|
|
219
324
|
}
|
|
220
325
|
}
|
|
221
326
|
|
|
327
|
+
/** How lately a scout's answer still counts for the next scout, how much of its title the new one must share, and how many are offered. */
|
|
328
|
+
const ASKED_DAYS = 14;
|
|
329
|
+
const ASKED_ALIKE = 0.6;
|
|
330
|
+
const ASKED_SHOWN = 2;
|
|
331
|
+
|
|
332
|
+
/** The words of a title that say what it is about: lowercased, without stop words or words under three letters. */
|
|
333
|
+
const titleWords = (title) => new Set((title.toLowerCase().match(/[\p{L}\p{N}_]+/gu) ?? []).filter((w) => w.length >= 3 && !STOPWORDS.has(w)));
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Earlier scouts on the project, closed DONE in the last two weeks with a report, whose title has most of its words in
|
|
337
|
+
* this one's: a line for each of the two closest — the newer first of two as close — saying where to read the answer.
|
|
338
|
+
*/
|
|
339
|
+
function askedBefore(db, slug, title, now) {
|
|
340
|
+
const mine = titleWords(title);
|
|
341
|
+
const since = new Date(Date.parse(now) - ASKED_DAYS * 86_400_000).toISOString();
|
|
342
|
+
const rows = db.prepare(`SELECT id, title, updated_at FROM jobs WHERE project = ? AND agent = 'scout' AND status = 'done' AND updated_at >= ?`).all(slug, since);
|
|
343
|
+
return rows
|
|
344
|
+
.filter((row) => existsSync(fileOf(row.id, 'report.md')))
|
|
345
|
+
.map((row) => {
|
|
346
|
+
const theirs = titleWords(row.title);
|
|
347
|
+
return { row, share: theirs.size === 0 ? 0 : [...theirs].filter((w) => mine.has(w)).length / theirs.size };
|
|
348
|
+
})
|
|
349
|
+
.filter(({ share }) => share >= ASKED_ALIKE)
|
|
350
|
+
.sort((a, b) => b.share - a.share || b.row.updated_at.localeCompare(a.row.updated_at))
|
|
351
|
+
.slice(0, ASKED_SHOWN)
|
|
352
|
+
.map(({ row }) => `A scout answered a similar question on ${row.updated_at.slice(0, 10)} — j${row.id} "${row.title}": read it with \`sumo job show ${row.id}\` and reuse what still holds.`);
|
|
353
|
+
}
|
|
354
|
+
|
|
222
355
|
/**
|
|
223
356
|
* Creates a job and its brief. The brief carries what memory knows about the
|
|
224
357
|
* project, so the sub-agent starts with its context instead of spending turns
|
|
225
358
|
* finding it — and the main agent's conversation never has to hold it. Its
|
|
226
|
-
* model and effort are the local router's answer
|
|
227
|
-
* asked before the row is written: no answer, no job.
|
|
359
|
+
* model and effort are the local router's answer under the route policy
|
|
360
|
+
* (`chooseRoute`, src/route.mjs), asked before the row is written: no answer, no job.
|
|
361
|
+
* A worker that finds another one open in its git project is given a worktree of its
|
|
362
|
+
* own (src/worktree.mjs), as is each of several `attempts` (`ownTree`, the route they
|
|
363
|
+
* share given once). A scout is pointed at what earlier scouts answered to much the
|
|
364
|
+
* same question (`askedBefore`).
|
|
228
365
|
*/
|
|
229
|
-
export async function newJob(db, { project: nameOrAlias, title, agent = 'worker', task, guide: guideName, reviews, testsMayChange = false, retryOf, start, now = new Date().toISOString() }) {
|
|
366
|
+
export async function newJob(db, { project: nameOrAlias, title, agent = 'worker', task, guide: guideName, reviews, testsMayChange = false, retryOf, start, attempts = 1, ownTree = false, route: routed, now = new Date().toISOString() }) {
|
|
367
|
+
if (attempts !== 1) return newAttempts(db, { project: nameOrAlias, title, agent, task, guide: guideName, testsMayChange, attempts, now });
|
|
230
368
|
if (!AGENTS.includes(agent)) throw new UsageError(`--agent is one of: ${AGENTS.join(', ')}`);
|
|
231
369
|
if (!title?.trim()) throw new UsageError('a job needs a --title');
|
|
232
370
|
if (!task?.trim()) throw new UsageError('describe the task on stdin — see guides/delegation.md for the five headings');
|
|
@@ -240,11 +378,11 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
|
|
|
240
378
|
// A rule the user stated a minute ago may not be filed yet, and the brief is built from memory.
|
|
241
379
|
if (pendingTurns(db, 1).length > 0) await runScribe(db, { now });
|
|
242
380
|
|
|
243
|
-
const route = await chooseRoute(db, { agent, project, task, title: title.trim(), retryOf });
|
|
381
|
+
const route = routed ?? (await chooseRoute(db, { agent, project, task, title: title.trim(), retryOf, reviews }));
|
|
244
382
|
|
|
245
383
|
// A job's own stand-in session (`job:j3`, see loop.mjs) is no one's conversation: it is never the session a new job belongs to.
|
|
246
384
|
const session = db.prepare(`SELECT id FROM sessions WHERE id NOT LIKE 'job:%' ORDER BY COALESCE(last_turn_at, started_at) DESC LIMIT 1`).get();
|
|
247
|
-
const job = tx(db, () => {
|
|
385
|
+
const { job, other } = tx(db, () => {
|
|
248
386
|
// A database put back from a backup has an older counter than the job folders on disk: the next id must not be one
|
|
249
387
|
// whose folder is still there, or the new job reads another job's notes and report as its own.
|
|
250
388
|
const onDisk = existsSync(paths().jobs) ? Math.max(0, ...readdirSync(paths().jobs).filter((name) => /^\d+$/.test(name)).map(Number)) : 0;
|
|
@@ -256,12 +394,19 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
|
|
|
256
394
|
}
|
|
257
395
|
const { lastInsertRowid } = db
|
|
258
396
|
.prepare(
|
|
259
|
-
`INSERT INTO jobs (project, title, agent, status, session_id, created_at, updated_at, model, effort, route_reason, retry_of)
|
|
260
|
-
VALUES (?, ?, ?, 'running', ?, ?, ?, ?, ?, ?, ?)`,
|
|
397
|
+
`INSERT INTO jobs (project, title, agent, status, session_id, created_at, updated_at, model, effort, route_reason, retry_of, risky)
|
|
398
|
+
VALUES (?, ?, ?, 'running', ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
261
399
|
)
|
|
262
|
-
.run(project.slug, title.trim(), agent, session?.id ?? null, now, now, route.model, route.effort, route.reason, retryOf ?? null);
|
|
263
|
-
|
|
400
|
+
.run(project.slug, title.trim(), agent, session?.id ?? null, now, now, route.model, route.effort, route.reason, retryOf ?? null, route.risky ? 1 : 0);
|
|
401
|
+
const id = Number(lastInsertRowid);
|
|
402
|
+
// Asked where the job is written: of two workers made at once, the second always sees the first. Only a worker at work counts —
|
|
403
|
+
// its run lock held — or one made in the last ten minutes, maybe not run yet: a job a stop left open is worked by nobody.
|
|
404
|
+
const since = new Date(Date.parse(now) - FRESH_MS).toISOString();
|
|
405
|
+
return { job: getJob(db, id), other: agent === 'worker' ? openWorkers(db, project.slug, id).find((o) => o.created_at >= since || atWork(o.id)) : undefined };
|
|
264
406
|
});
|
|
407
|
+
// Two workers in one working tree overwrite each other: in a git project the second is given a worktree of its own.
|
|
408
|
+
const own = agent === 'worker' && (ownTree || other !== undefined) && hasCommit(project.path);
|
|
409
|
+
let tree = null;
|
|
265
410
|
|
|
266
411
|
// The job's files. If any of them cannot be written the job is taken back: an open job with no brief is worse than no job.
|
|
267
412
|
try {
|
|
@@ -269,19 +414,39 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
|
|
|
269
414
|
const rules = preferences(db, BRIEF_RULES_BUDGET).slice(1).join('\n');
|
|
270
415
|
mkdirSync(dirOf(job.id), { recursive: true, mode: 0o700 });
|
|
271
416
|
if (agent === 'reviewer' && reviews !== undefined) recordReviewTarget(job.id, reviews);
|
|
417
|
+
if (own) tree = makeTree(project.path, join(dirOf(job.id), 'tree'));
|
|
418
|
+
const where = tree?.path ?? project.path;
|
|
272
419
|
|
|
273
420
|
let change = null;
|
|
274
421
|
if (judged) {
|
|
275
|
-
const
|
|
422
|
+
const own = fileOf(job.id, 'changes.diff');
|
|
423
|
+
if (judged.kept) copyFileSync(judged.kept.file, own);
|
|
424
|
+
const { file, files } = judged.kept ? { file: own, files: judged.kept.files } : writeChanges(judged.root, judged.snap, own);
|
|
276
425
|
change = [...judged.lines, `The whole change, ${files} file${files === 1 ? '' : 's'}: ${file} — read it once; do not re-derive it with git.`].join('\n');
|
|
277
426
|
}
|
|
278
427
|
// Where a worker starts from, and the checks that judge it there, so what it changed — and only that — can be told apart later.
|
|
279
|
-
// A retry is handed the first attempt's start instead.
|
|
280
|
-
|
|
281
|
-
|
|
428
|
+
// A retry is handed the first attempt's start instead — one in the project, never in a worktree made just now.
|
|
429
|
+
const begin = start && !tree ? start : { snap: snapshot(where), declared: verifyCommands(where), baseline: null };
|
|
430
|
+
if (agent === 'worker') writeState(db, job.id, { ...begin, testsMayChange, verdict: null, ...(tree ? { tree } : {}) });
|
|
431
|
+
writeFileSync(fileOf(job.id, 'brief.md'), renderBrief({ job, project: { ...project, path: where }, known, rules, situation: whereThingsStand(where), task, guide, change, options: { testsMayChange } }), { mode: 0o600 });
|
|
432
|
+
if (tree) {
|
|
433
|
+
const why = ownTree
|
|
434
|
+
? 'Several attempts are made at this task, each in a git worktree of its own; the one to keep is picked once all have closed.'
|
|
435
|
+
: `j${other.id} "${other.title}" is another worker open in ${project.path}, so this job has a git worktree of its own; when it is done, its change is brought back to the project.`;
|
|
436
|
+
// The project's installed dependencies are linked in (makeTree): outside the job's roots, the sandbox lets it read them, never change them.
|
|
437
|
+
const deps =
|
|
438
|
+
tree.linked?.length > 0
|
|
439
|
+
? ` The project's installed dependencies — ${tree.linked.join(', ')} — are its own, linked in: the checks run with them, and they are read-only here; if one must be added or changed, say so in the report. A workspace package or an editable install reached through them is the project's copy, not this tree's: a check that loads one tests the project's code, not your change to it.`
|
|
440
|
+
: ' If the checks need installed dependencies, install them there.';
|
|
441
|
+
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 });
|
|
442
|
+
}
|
|
443
|
+
// A question a scout answered lately is not asked from nothing again: the brief points at those answers, and the job goes ahead.
|
|
444
|
+
const asked = agent === 'scout' ? askedBefore(db, project.slug, title.trim(), now) : [];
|
|
445
|
+
if (asked.length > 0) appendFileSync(fileOf(job.id, 'brief.md'), `\n## Asked before\n${asked.join('\n')}\n`, { mode: 0o600 });
|
|
282
446
|
} catch (cause) {
|
|
283
447
|
db.prepare('DELETE FROM jobs WHERE id = ?').run(job.id);
|
|
284
448
|
try {
|
|
449
|
+
if (own) dropTree(project.path, join(dirOf(job.id), 'tree'));
|
|
285
450
|
rmSync(dirOf(job.id), { recursive: true, force: true });
|
|
286
451
|
} catch {
|
|
287
452
|
// Nothing was written there.
|
|
@@ -293,18 +458,50 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
|
|
|
293
458
|
if (agent !== 'reviewer' && !/^#{1,3}\s*(the\s+)?check\b|^check\s*:/im.test(task)) {
|
|
294
459
|
warnings.push('note: the task names no check that proves the work — add a "## Check" with a command, or say why there is none');
|
|
295
460
|
}
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
if (other) warnings.push(`note: j${other.id} "${other.title}" is already a running worker in ${project.slug} — two workers in one working tree overwrite each other. Let it finish, or: sumo job abandon ${other.id}`);
|
|
299
|
-
}
|
|
461
|
+
if (other && tree) warnings.push(`note: j${other.id} "${other.title}" is already a worker in ${project.slug} — this one works in a worktree of its own, ${tree.top}, and its change is brought back when it is done`);
|
|
462
|
+
else if (other) warnings.push(`note: j${other.id} "${other.title}" is already a running worker in ${project.slug} — two workers in one working tree overwrite each other. Let it finish, or: sumo job abandon ${other.id}`);
|
|
300
463
|
return { job, warnings };
|
|
301
464
|
}
|
|
302
465
|
|
|
466
|
+
/**
|
|
467
|
+
* Several attempts at one hard task, when asked for: the same brief made into sibling worker jobs, routed once, each in a
|
|
468
|
+
* worktree of its own. Each is run and verified like any job; once all have closed, one verified attempt is brought back
|
|
469
|
+
* (`settle`). It costs as many runs as there are attempts, so it is never the default.
|
|
470
|
+
*/
|
|
471
|
+
async function newAttempts(db, { attempts, title, ...options }) {
|
|
472
|
+
if (![2, 3].includes(attempts)) throw new UsageError('--attempts is 2 or 3');
|
|
473
|
+
if (options.agent !== 'worker') throw new UsageError('--attempts goes with a worker');
|
|
474
|
+
if (!title?.trim()) throw new UsageError('a job needs a --title');
|
|
475
|
+
const project = getProject(db, options.project);
|
|
476
|
+
if (!hasCommit(project.path)) throw new UsageError(`${project.path} is not a git repository with a commit — each attempt needs a worktree of its own`);
|
|
477
|
+
const made = [];
|
|
478
|
+
try {
|
|
479
|
+
for (let n = 1; n <= attempts; n++) {
|
|
480
|
+
const first = made[0]?.job;
|
|
481
|
+
const route = first && { model: first.model, effort: first.effort, reason: first.route_reason, risky: first.risky === 1 };
|
|
482
|
+
made.push(await newJob(db, { ...options, title: `${title.trim()} (attempt ${n} of ${attempts})`, ownTree: true, route }));
|
|
483
|
+
}
|
|
484
|
+
} catch (cause) {
|
|
485
|
+
// All of them, or none.
|
|
486
|
+
for (const { job } of made) {
|
|
487
|
+
abandon(db, job.id);
|
|
488
|
+
await settle(db, job.id);
|
|
489
|
+
}
|
|
490
|
+
throw cause;
|
|
491
|
+
}
|
|
492
|
+
const ids = made.map(({ job }) => job.id);
|
|
493
|
+
for (const id of ids) setMeta(db, attemptsKey(id), JSON.stringify(ids));
|
|
494
|
+
return { job: made[0].job, jobs: made.map(({ job }) => job), warnings: made[0].warnings };
|
|
495
|
+
}
|
|
496
|
+
|
|
303
497
|
/**
|
|
304
498
|
* Retries a job: same project, same title (marked as a retry), same task, with
|
|
305
|
-
* what the previous attempt found carried into the new brief
|
|
306
|
-
*
|
|
307
|
-
*
|
|
499
|
+
* what the previous attempt found carried into the new brief — and, when a review
|
|
500
|
+
* judged it, that review's findings as the reviewer wrote them. The router is
|
|
501
|
+
* asked again for its route, like any new job, and the route goes at least one
|
|
502
|
+
* step above the attempt being retried. Only a failed job, or a done
|
|
503
|
+
* one a review found enough wrong with, is eligible. A done one that worked in a worktree of its own had its change
|
|
504
|
+
* brought back when it closed: its retry starts from the project as it is now, with that change already in it.
|
|
308
505
|
*/
|
|
309
506
|
export async function retry(db, id, { now = new Date().toISOString() } = {}) {
|
|
310
507
|
const old = getJob(db, id);
|
|
@@ -324,17 +521,31 @@ export async function retry(db, id, { now = new Date().toISOString() } = {}) {
|
|
|
324
521
|
const testsMayChange = Boolean(state?.testsMayChange);
|
|
325
522
|
// A worker's retry is judged from where the first attempt began, so what that attempt left in the tree is still answered for.
|
|
326
523
|
// Its baseline comes too: taken again now, it would call the first attempt's breakage "already there".
|
|
327
|
-
|
|
524
|
+
// A first attempt made in a worktree of its own left nothing in the project to answer for.
|
|
525
|
+
const start = !state?.tree && state?.snap && stillThere(getProject(db, old.project).path, state.snap) ? { snap: state.snap, declared: state.declared, baseline: state.baseline } : undefined;
|
|
328
526
|
const { job, warnings } = await newJob(db, { project: old.project, title: `${old.title} (retry of j${id})`, agent: old.agent, task, reviews, testsMayChange, retryOf: id, start, now });
|
|
329
527
|
|
|
330
528
|
const oldNotes = readOr(fileOf(id, 'notes.md')).trim();
|
|
331
529
|
const oldReport = readOr(fileOf(id, 'report.md')).trim();
|
|
332
|
-
|
|
530
|
+
// What a reviewer found is what the retry is for: given in the reviewer's own words, the head of it when it is long.
|
|
531
|
+
const review = reviewOf(db, old);
|
|
532
|
+
const findings = review === null ? '' : findingsSection(reportOf(review)).trim();
|
|
533
|
+
const judged = findings && `### What its review, j${review}, found\n${findings.length > REVIEW_FINDINGS_CHARS ? `${head(findings, REVIEW_FINDINGS_CHARS)}\n[… the rest of the review: sumo job show ${review}]` : findings}`;
|
|
534
|
+
const found = [oldNotes && `### Notes\n${oldNotes}`, oldReport && `### Report\n${oldReport}`, judged].filter(Boolean).join('\n\n');
|
|
333
535
|
if (found) appendFileSync(fileOf(job.id, 'brief.md'), `\n## What the previous attempt found\n${found}\n`, { mode: 0o600 });
|
|
334
536
|
|
|
335
537
|
return { job, warnings };
|
|
336
538
|
}
|
|
337
539
|
|
|
540
|
+
/** How much of a review's findings a retry's brief carries: enough for every finding of a usual review, not a flood. */
|
|
541
|
+
const REVIEW_FINDINGS_CHARS = 1500;
|
|
542
|
+
|
|
543
|
+
/** The newest reviewer job that judged this one with `--reviews` and closed DONE: its id, or null when none did. */
|
|
544
|
+
function reviewOf(db, job) {
|
|
545
|
+
const reviewers = db.prepare(`SELECT id FROM jobs WHERE agent = 'reviewer' AND status = 'done' AND project = ? AND id > ? ORDER BY id DESC`).all(job.project, job.id);
|
|
546
|
+
return reviewers.find((r) => reviewTargetOf(r.id) === job.id)?.id ?? null;
|
|
547
|
+
}
|
|
548
|
+
|
|
338
549
|
/** The task text a brief was built from — everything between "## The task" and the next section renderBrief adds. */
|
|
339
550
|
function taskFromBrief(brief) {
|
|
340
551
|
const marker = '## The task\n';
|
|
@@ -347,6 +558,104 @@ function taskFromBrief(brief) {
|
|
|
347
558
|
return brief.slice(from, stops.length > 0 ? Math.min(...stops) : brief.length).trim();
|
|
348
559
|
}
|
|
349
560
|
|
|
561
|
+
const setting = (db, key) => getMeta(db, `config.${key}`) ?? CONFIG_DEFAULTS[key];
|
|
562
|
+
const routeOf = (job) => `${job.model}/${job.effort}`;
|
|
563
|
+
|
|
564
|
+
const closingKey = (id) => `job.${id}.told-to-close`;
|
|
565
|
+
/** The limits sumo itself tells a run to close near (src/loop.mjs), as a report names them. */
|
|
566
|
+
const CLOSED_NEAR = { budget: 'budget', time: 'time limit', turns: 'turn limit' };
|
|
567
|
+
/** What sumo told a job's last run to close near — `budget`, `time` or `turns` — kept for what follows it; null when it told it nothing. */
|
|
568
|
+
export function toldToClose(db, id, near) {
|
|
569
|
+
if (near) setMeta(db, closingKey(id), near);
|
|
570
|
+
else unmark(db, closingKey(id));
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/** The brief of a review nobody asked for: the change to judge comes with it, as with any `--reviews`. */
|
|
574
|
+
const reviewTask = (job, risky) => `## Goal
|
|
575
|
+
Judge the change j${job.id} made for "${job.title}" against what it was asked: does it do all of it, correctly, and can it be trusted as it stands? ${
|
|
576
|
+
risky ? 'The router called this work risky — it touches security, credentials, money, migrations or concurrency.' : 'Every worker that finishes is reviewed here (jobs.review is all).'
|
|
577
|
+
} It passed the project's checks; this review is for what the checks cannot see.
|
|
578
|
+
## Non-goals
|
|
579
|
+
Rewriting the change, or judging code it did not touch.
|
|
580
|
+
## Check
|
|
581
|
+
none: a review changes nothing — read the change, what was asked, and the code around it.
|
|
582
|
+
`;
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* What follows a worker's run without being asked, once the worker has closed. A first attempt that FAILED is retried
|
|
586
|
+
* once, on a route one step up (`retry`; `jobs.escalate`) — a retry is never retried again this way, nor one sumo told to
|
|
587
|
+
* close near its budget, time or turns, and one of several `attempts` is settled with its siblings instead. One that closed
|
|
588
|
+
* DONE, verified by the checks — in git, so there is a change to hand over — is reviewed when the
|
|
589
|
+
* router called its work risky, or always (`jobs.review`), by a reviewer at least as strong as it (`--reviews`). The job
|
|
590
|
+
* made, with a line that says why and on what route; only the line when it could not be made; null when nothing follows.
|
|
591
|
+
*/
|
|
592
|
+
async function followUp(db, id, { now = new Date().toISOString() } = {}) {
|
|
593
|
+
const job = getJob(db, id);
|
|
594
|
+
if (job.agent !== 'worker' || attemptsOf(db, id) !== null) return null;
|
|
595
|
+
if (job.status === 'failed') {
|
|
596
|
+
if (job.retry_of !== null || setting(db, 'jobs.escalate') !== 'on') return null;
|
|
597
|
+
// Told by sumo to close near one of its limits, it closed where it stood: a run one step up with that limit fresh would only buy
|
|
598
|
+
// the limit off at a dearer price, and that is the user's call.
|
|
599
|
+
const near = CLOSED_NEAR[getMeta(db, closingKey(id))];
|
|
600
|
+
if (near) {
|
|
601
|
+
const why = `j${id} failed on ${routeOf(job)} after it was told to close near its ${near}: it is not retried one step up, which would start again on a dearer route with a fresh ${near}. To retry it anyway: sumo job retry ${id}`;
|
|
602
|
+
return { line: said(id, 'Not retried', [why])[0] };
|
|
603
|
+
}
|
|
604
|
+
try {
|
|
605
|
+
const made = await retry(db, id, { now });
|
|
606
|
+
return { ...made, kind: 'retry', line: `j${id} failed on ${routeOf(job)} and was retried on ${routeOf(made.job)}, as j${made.job.id} — j${id}'s own report: sumo job show ${id}` };
|
|
607
|
+
} catch (cause) {
|
|
608
|
+
return { line: `j${id} failed on ${routeOf(job)}; a retry one step up could not be made: ${cause.message}` };
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const review = setting(db, 'jobs.review');
|
|
612
|
+
const risky = job.risky === 1;
|
|
613
|
+
if (job.status !== 'done' || !(review === 'all' || (review === 'risky' && risky))) return null;
|
|
614
|
+
// Work taken with --accept was never verified: a review is added to the checks, it does not stand in for them.
|
|
615
|
+
const state = readState(db, id);
|
|
616
|
+
if (state?.verdict?.ok !== true || /^## Accepted without verification$/m.test(reportOf(id))) return null;
|
|
617
|
+
// Outside git nothing recorded where the work began, so there is no change to hand a reviewer: none is asked for.
|
|
618
|
+
if (!state.snap) return null;
|
|
619
|
+
const why = risky ? `The router called j${id}'s work risky` : 'jobs.review is all';
|
|
620
|
+
try {
|
|
621
|
+
const made = await newJob(db, { project: job.project, title: `${job.title} (review of j${id})`, agent: 'reviewer', task: reviewTask(job, risky), reviews: id, now });
|
|
622
|
+
return { ...made, kind: 'review', line: `${why}, so j${made.job.id} reviewed j${id} on ${routeOf(made.job)}:` };
|
|
623
|
+
} catch (cause) {
|
|
624
|
+
return { line: `${why}, but a review could not be made: ${cause.message}` };
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Runs a job with `run`, then what follows it unasked (`followUp`), each with `run` too, until nothing does — at most a
|
|
630
|
+
* retry of a first attempt that failed, and a review of the work once it is done. The chat and `sumo job run` both come
|
|
631
|
+
* through here, each with its own way of running one job: `run(id)` for the first, `run(id, warnings)` for each made
|
|
632
|
+
* here. `onFollow` hears each job made, before it runs; nothing more is made once `signal` is aborted. Each step comes
|
|
633
|
+
* back in order as `{ id, kind, line, result }` — `error` in place of `result` when a follow-up's run threw, and only a
|
|
634
|
+
* `line` when one could not be made. A throw from the first run is the caller's.
|
|
635
|
+
*/
|
|
636
|
+
export async function runFollowed(db, id, run, { signal = null, onFollow = null, now = () => new Date().toISOString() } = {}) {
|
|
637
|
+
const ran = [{ id, kind: null, line: null, result: await run(id) }];
|
|
638
|
+
for (let at = id; !signal?.aborted; ) {
|
|
639
|
+
const next = await followUp(db, at, { now: now() });
|
|
640
|
+
if (!next) break;
|
|
641
|
+
if (!next.job) {
|
|
642
|
+
ran.push({ id: null, kind: null, line: next.line, result: null });
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
onFollow?.(next.job.id);
|
|
646
|
+
const step = { id: next.job.id, kind: next.kind, line: next.line, result: null };
|
|
647
|
+
ran.push(step);
|
|
648
|
+
try {
|
|
649
|
+
step.result = await run(next.job.id, next.warnings);
|
|
650
|
+
} catch (cause) {
|
|
651
|
+
step.error = cause.message;
|
|
652
|
+
break;
|
|
653
|
+
}
|
|
654
|
+
at = next.job.id;
|
|
655
|
+
}
|
|
656
|
+
return ran;
|
|
657
|
+
}
|
|
658
|
+
|
|
350
659
|
/** `jobs, done, failed, reviewed, avg important` — one line per model/effort this project has finished. */
|
|
351
660
|
export function stats(db, { project } = {}) {
|
|
352
661
|
return statsLines(db, { project: project === undefined ? undefined : getProject(db, project).slug });
|
|
@@ -360,7 +669,7 @@ export function brief(db, id) {
|
|
|
360
669
|
const notes = readOr(fileOf(id, 'notes.md')).trim();
|
|
361
670
|
const red = (readState(db, id)?.baseline ?? []).filter((c) => !c.ok);
|
|
362
671
|
if (red.length > 0) {
|
|
363
|
-
parts.push(`## Already failing before this job began — not yours to fix, and not to be made worse\n${red.map((c) => `- \`${c.command}\` — ${c.file}`).join('\n')}`);
|
|
672
|
+
parts.push(`## Already failing before this job began — not yours to fix, and not to be made worse\n${red.map((c) => `- \`${c.command}\` — ${c.flaky ? 'flaky: run twice, it did not fail the same way — ' : ''}${c.file}`).join('\n')}`);
|
|
364
673
|
}
|
|
365
674
|
if (qa) parts.push(`## Questions and answers so far\n${qa}`);
|
|
366
675
|
if (notes) parts.push(`## Progress so far (from an earlier run of this job — continue from here, do not start over)\n${notes}`);
|
|
@@ -368,35 +677,44 @@ export function brief(db, id) {
|
|
|
368
677
|
return parts.join('\n\n');
|
|
369
678
|
}
|
|
370
679
|
|
|
371
|
-
/**
|
|
680
|
+
/** Where a worker job works: the worktree it was given, or the project itself. */
|
|
681
|
+
const workdir = (state, project) => state?.tree?.path ?? project.path;
|
|
682
|
+
/** The worktree a worker job was given — `{ top, path, prefix, base }` — or null when it works in the project. */
|
|
683
|
+
export const treeOf = (db, id) => readState(db, id)?.tree ?? null;
|
|
684
|
+
|
|
685
|
+
/** A worker job's recorded state and where it works — or a plain reason why there is nothing to check. */
|
|
372
686
|
function checkable(db, id, verb) {
|
|
373
687
|
const job = getJob(db, id);
|
|
374
688
|
requireOpen(job, verb);
|
|
375
689
|
const state = readState(db, id);
|
|
376
690
|
if (job.agent !== 'worker' || !state) throw new UsageError(`j${id} is not a worker job with a recorded start — there is nothing to ${verb === 'verified' ? 'verify' : 'take a baseline of'}`);
|
|
377
|
-
const
|
|
378
|
-
if (!existsSync(
|
|
379
|
-
return { job, state,
|
|
691
|
+
const root = workdir(state, getProject(db, job.project));
|
|
692
|
+
if (!existsSync(root)) throw new UsageError(`${root} no longer exists`);
|
|
693
|
+
return { job, state, root };
|
|
380
694
|
}
|
|
381
695
|
|
|
382
696
|
/** The project's checks as they stand before the work. Taken once; a second call reports what the first found. */
|
|
383
697
|
export function baseline(db, id) {
|
|
384
|
-
const { state,
|
|
698
|
+
const { state, root } = checkable(db, id, 'given a baseline');
|
|
385
699
|
if (!state.baseline) {
|
|
386
|
-
const taken = takeBaseline(
|
|
700
|
+
const taken = takeBaseline(root, state.snap, dirOf(id));
|
|
387
701
|
if (taken.refused) return [`no baseline taken: ${taken.refused}.`, 'Every check that fails at the end will count as this job\'s.'];
|
|
388
702
|
// A verdict given before there was a baseline judged against none: it no longer says what DONE would.
|
|
389
703
|
writeState(db, id, { ...state, baseline: taken.checks, verdict: null });
|
|
390
704
|
state.baseline = taken.checks;
|
|
391
705
|
}
|
|
392
706
|
if (state.baseline.length === 0) return ['this project declares no check, test, lint or typecheck command — there is nothing to take a baseline of'];
|
|
393
|
-
|
|
707
|
+
const failing = (c) =>
|
|
708
|
+
c.flaky
|
|
709
|
+
? `is FLAKY — it failed, and run again it did not fail the same way; recorded so a failure of it is not blamed on you (${c.file}, ${c.again})`
|
|
710
|
+
: `ALREADY FAILS — not yours to fix, and recorded so it is not blamed on you (${c.file})`;
|
|
711
|
+
return state.baseline.map((c) => `\`${c.command}\` ${c.ok ? 'passes' : failing(c)}`);
|
|
394
712
|
}
|
|
395
713
|
|
|
396
714
|
/** Runs the checks now and records the verdict against the exact tree it judged. */
|
|
397
715
|
export function verifyJob(db, id, now = new Date().toISOString()) {
|
|
398
|
-
const { state,
|
|
399
|
-
const verdict = verify(
|
|
716
|
+
const { state, root } = checkable(db, id, 'verified');
|
|
717
|
+
const verdict = verify(root, state, dirOf(id), now);
|
|
400
718
|
writeState(db, id, { ...state, verdict });
|
|
401
719
|
return verdict;
|
|
402
720
|
}
|
|
@@ -404,9 +722,14 @@ export function verifyJob(db, id, now = new Date().toISOString()) {
|
|
|
404
722
|
/** Everything a job changed, as one file a reviewer can read. */
|
|
405
723
|
export function changes(db, id) {
|
|
406
724
|
const job = getJob(db, id);
|
|
407
|
-
const
|
|
408
|
-
if (!snap) throw new UsageError(`j${id} recorded no starting point (not a worker job, or not a git repository)`);
|
|
409
|
-
|
|
725
|
+
const state = readState(db, id);
|
|
726
|
+
if (!state?.snap) throw new UsageError(`j${id} recorded no starting point (not a worker job, or not a git repository)`);
|
|
727
|
+
const root = workdir(state, getProject(db, job.project));
|
|
728
|
+
// A worktree that is gone wrote down its change as it went: that is kept, not overwritten from a project that has moved on.
|
|
729
|
+
if (state.tree && !existsSync(root) && existsSync(fileOf(id, 'changes.diff'))) {
|
|
730
|
+
return { file: fileOf(id, 'changes.diff'), files: (readOr(fileOf(id, 'changes.diff')).match(/^diff --git /gm) ?? []).length };
|
|
731
|
+
}
|
|
732
|
+
return writeChanges(root, state.snap, fileOf(id, 'changes.diff'));
|
|
410
733
|
}
|
|
411
734
|
|
|
412
735
|
/**
|
|
@@ -415,10 +738,10 @@ export function changes(db, id) {
|
|
|
415
738
|
* whether the tree moved, so it is always run again.
|
|
416
739
|
*/
|
|
417
740
|
function verdictForDone(db, id, state, now) {
|
|
418
|
-
const
|
|
419
|
-
if (!existsSync(
|
|
741
|
+
const root = workdir(state, getProject(db, getJob(db, id).project));
|
|
742
|
+
if (!existsSync(root)) return null;
|
|
420
743
|
const last = state.verdict;
|
|
421
|
-
if (last?.fingerprint && state.snap && unmoved(
|
|
744
|
+
if (last?.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
|
|
422
745
|
return verifyJob(db, id, now);
|
|
423
746
|
}
|
|
424
747
|
|
|
@@ -435,6 +758,23 @@ function append(id, name, heading, text, now) {
|
|
|
435
758
|
appendFileSync(fileOf(id, name), `### ${heading} — ${now.slice(0, 16).replace('T', ' ')}\n${text.trim()}\n\n`, { mode: 0o600 });
|
|
436
759
|
}
|
|
437
760
|
|
|
761
|
+
/** A line of a checklist, ticked or not: `- [ ] a step`, `- [x] a step`. */
|
|
762
|
+
const STEP = /^\s*[-*]\s+\[[ xX]\]\s+\S/;
|
|
763
|
+
/** The most of a plan put back in front of a job: it is a reminder, not the notes again. */
|
|
764
|
+
const PLAN_STEPS = 20;
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* A job's plan: the checklist in the latest of its notes that holds one. A worker notes its steps as `- [ ] …` and notes the
|
|
768
|
+
* list again, ticked `- [x] …`, as it goes, so the last list is where it stands. Null while no note holds one.
|
|
769
|
+
*/
|
|
770
|
+
export function planOf(id) {
|
|
771
|
+
for (const entry of readOr(fileOf(id, 'notes.md')).split(/^### /m).reverse()) {
|
|
772
|
+
const steps = entry.split('\n').filter((l) => STEP.test(l)).map((l) => l.trim());
|
|
773
|
+
if (steps.length > 0) return [...steps.slice(0, PLAN_STEPS), ...(steps.length > PLAN_STEPS ? [`… ${steps.length - PLAN_STEPS} more steps`] : [])].join('\n');
|
|
774
|
+
}
|
|
775
|
+
return null;
|
|
776
|
+
}
|
|
777
|
+
|
|
438
778
|
export function note(db, id, text, now = new Date().toISOString()) {
|
|
439
779
|
const job = getJob(db, id);
|
|
440
780
|
requireOpen(job, 'noted on');
|
|
@@ -509,14 +849,28 @@ export function answer(db, id, text, now = new Date().toISOString()) {
|
|
|
509
849
|
setOpenStatus(db, id, 'running', now);
|
|
510
850
|
}
|
|
511
851
|
|
|
512
|
-
/**
|
|
852
|
+
/**
|
|
853
|
+
* The traps under "## Learned" in a report, minus the ways of saying "nothing". A trap is a bullet with the lines it wraps
|
|
854
|
+
* onto, or — where nothing is listed — the first paragraph; what stands after them is the report going on, not a trap.
|
|
855
|
+
*/
|
|
513
856
|
function learnedLines(report) {
|
|
514
857
|
const section = /^##\s*Learned\s*\n([\s\S]*?)(?=^##\s|(?![\s\S]))/im.exec(report)?.[1] ?? '';
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
858
|
+
const BULLET = /^\s*(?:[-*]|\d+[.)])\s+/;
|
|
859
|
+
const listed = section.split('\n').some((l) => BULLET.test(l));
|
|
860
|
+
const traps = [];
|
|
861
|
+
let open = false;
|
|
862
|
+
for (const raw of section.split('\n')) {
|
|
863
|
+
const text = raw.replace(BULLET, '').trim();
|
|
864
|
+
if (!text) {
|
|
865
|
+
// A blank line ends a trap; with nothing listed, it ends the one paragraph there is.
|
|
866
|
+
if (!listed && traps.length > 0) break;
|
|
867
|
+
open = false;
|
|
868
|
+
} else if (BULLET.test(raw) || (!listed && !open)) {
|
|
869
|
+
traps.push(text);
|
|
870
|
+
open = true;
|
|
871
|
+
} else if (open) traps[traps.length - 1] += ` ${text}`;
|
|
872
|
+
}
|
|
873
|
+
return traps.filter((l) => l.length > 12 && !/^(none|nothing|n\/a)\b/i.test(l)).slice(0, MAX_LEARNED);
|
|
520
874
|
}
|
|
521
875
|
|
|
522
876
|
/**
|
|
@@ -526,8 +880,56 @@ function learnedLines(report) {
|
|
|
526
880
|
* a wrapped line is indented, so it is never counted twice.
|
|
527
881
|
*/
|
|
528
882
|
function findingsCount(report) {
|
|
529
|
-
|
|
530
|
-
|
|
883
|
+
return (findingsSection(report).match(/^(?:\d+\.\s|\S+\.\w+:\d+)/gm) ?? []).length;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/** What a review lists under "## Findings", as it was written; empty when it has no such section. */
|
|
887
|
+
function findingsSection(report) {
|
|
888
|
+
return /^##\s*Findings\s*\n([\s\S]*?)(?=^##\s|(?![\s\S]))/im.exec(report)?.[1] ?? '';
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const checkedFile = (id) => fileOf(id, 'checked');
|
|
892
|
+
/** How many changed files, and how many new ones, the check names before it counts the rest. */
|
|
893
|
+
const CHECK_LISTED = 30;
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* What a worker's first DONE is answered with instead of being taken: the task as it was given, what has changed since
|
|
897
|
+
* the job began, and what to look for — the first thing a reviewer does, done by the author while it can still fix
|
|
898
|
+
* what it finds. Nothing is run here; the checks come with the next DONE.
|
|
899
|
+
*/
|
|
900
|
+
function checkAgainstTask(db, job, state) {
|
|
901
|
+
// Where the job works: its worktree when it was given one, the project otherwise.
|
|
902
|
+
const root = workdir(state, getProject(db, job.project));
|
|
903
|
+
let change = ['not a git repository — what changed cannot be shown; look over it yourself'];
|
|
904
|
+
if (state.snap) {
|
|
905
|
+
try {
|
|
906
|
+
const { created } = changesSince(root, state.snap);
|
|
907
|
+
change = [
|
|
908
|
+
diffStat(root, state.snap, CHECK_LISTED) || '(no tracked file changed)',
|
|
909
|
+
...created.slice(0, CHECK_LISTED).map((f) => ` new file: ${f}`),
|
|
910
|
+
...(created.length > CHECK_LISTED ? [` … ${created.length - CHECK_LISTED} more new files`] : []),
|
|
911
|
+
];
|
|
912
|
+
} catch {
|
|
913
|
+
change = ['what changed could not be read from git — look over it yourself'];
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
return [
|
|
917
|
+
`j${job.id} is not closed yet — before DONE counts, check the work against the task. Nothing was run.`,
|
|
918
|
+
'',
|
|
919
|
+
'## The task',
|
|
920
|
+
taskFromBrief(readOr(fileOf(job.id, 'brief.md'))) || '(the brief has no "## The task" section)',
|
|
921
|
+
'',
|
|
922
|
+
'## What changed since the job began',
|
|
923
|
+
...change,
|
|
924
|
+
'',
|
|
925
|
+
'## Check each, and fix what is not so',
|
|
926
|
+
'- Every requirement in the task is met.',
|
|
927
|
+
'- Debug output and scratch files are removed.',
|
|
928
|
+
'- Tests were changed only if the task allows it.',
|
|
929
|
+
'- Every claim in the report is backed by a tool result from this run.',
|
|
930
|
+
'',
|
|
931
|
+
'Then call `finish` again with the same report, or a corrected one, to go on to verification. This is asked once.',
|
|
932
|
+
].join('\n');
|
|
531
933
|
}
|
|
532
934
|
|
|
533
935
|
/**
|
|
@@ -548,6 +950,13 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
|
|
|
548
950
|
const state = graded ? readState(db, id) : null;
|
|
549
951
|
const unchecked = (why) => new UsageError(`j${id} cannot be verified — ${why}. To take the work as it stands: sumo job finish ${id} --status DONE --accept "<why>"`);
|
|
550
952
|
if (graded && accept === undefined && !state) throw unchecked(`the record of where it began (${fileOf(id, 'verify.json')}) cannot be read, or was changed since sumo wrote it`);
|
|
953
|
+
// Before the checks are run, the author looks once at the task beside its change: the first DONE is answered with that, not taken.
|
|
954
|
+
// Kept in the job's folder, so a run started again goes on to the checks instead of being asked again.
|
|
955
|
+
if (graded && accept === undefined && !existsSync(checkedFile(id))) {
|
|
956
|
+
const asked = checkAgainstTask(db, job, state);
|
|
957
|
+
writeFileSync(checkedFile(id), `${now}\n`, { mode: 0o600 });
|
|
958
|
+
throw new UsageError(asked);
|
|
959
|
+
}
|
|
551
960
|
const verdict = state && accept === undefined ? verdictForDone(db, id, state, now) : null;
|
|
552
961
|
// The checks can take minutes: report a job closed in the meantime before interpreting their verdict.
|
|
553
962
|
requireOpen(getJob(db, id), 'finished');
|
|
@@ -635,3 +1044,141 @@ export function show(db, id) {
|
|
|
635
1044
|
if (report) out.push('', report);
|
|
636
1045
|
return out.join('\n');
|
|
637
1046
|
}
|
|
1047
|
+
|
|
1048
|
+
const settledKey = (id) => `job.${id}.settled`;
|
|
1049
|
+
const attemptsKey = (id) => `job.${id}.attempts`;
|
|
1050
|
+
const pickedKey = (first) => `job.${first}.picked`;
|
|
1051
|
+
/** True for the one caller that gets there first, whoever else tries at the same moment. */
|
|
1052
|
+
const firstTo = (db, key, now) => tx(db, () => getMeta(db, key) === null && (setMeta(db, key, now), true));
|
|
1053
|
+
|
|
1054
|
+
/** The jobs one `--attempts` made, in order; null for a job that is not one of them. */
|
|
1055
|
+
export function attemptsOf(db, id) {
|
|
1056
|
+
const ids = getMeta(db, attemptsKey(id));
|
|
1057
|
+
return ids === null ? null : JSON.parse(ids);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/** What the last of a set of attempts to close settled for them all, in lines; null until then. */
|
|
1061
|
+
export function attemptsSettled(db, first) {
|
|
1062
|
+
try {
|
|
1063
|
+
return JSON.parse(getMeta(db, pickedKey(first))).lines ?? null;
|
|
1064
|
+
} catch {
|
|
1065
|
+
return null;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
/** Added to a job's report under a heading of its own, and handed back to be said. */
|
|
1070
|
+
function said(id, heading, lines) {
|
|
1071
|
+
appendFileSync(fileOf(id, 'report.md'), `\n## ${heading}\n${lines.join('\n')}\n`, { mode: 0o600 });
|
|
1072
|
+
return lines;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/** The tree goes; what it changed stays beside the job as changes.diff, for a retry, a review or a person. */
|
|
1076
|
+
function release(id, project, state) {
|
|
1077
|
+
const kept = fileOf(id, 'changes.diff');
|
|
1078
|
+
try {
|
|
1079
|
+
writeChanges(state.tree.path, state.snap, kept);
|
|
1080
|
+
} catch {
|
|
1081
|
+
// A tree too broken to diff has nothing to keep.
|
|
1082
|
+
}
|
|
1083
|
+
dropTree(project.path, state.tree.top);
|
|
1084
|
+
return kept;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/** The newest other worker at work in the project itself — not in a worktree of its own — right now, or undefined. */
|
|
1088
|
+
const inPlace = (db, project, id) => openWorkers(db, project.slug, id).find((o) => !readState(db, o.id)?.tree && atWork(o.id));
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* The tree's change, brought back to the project: the tree goes once it is there, and stays, said, where it would not go.
|
|
1092
|
+
* Never under a worker at work in the project: its start, its baseline and its review would all count the change as its
|
|
1093
|
+
* own. Then the tree goes, and the change is kept whole as a patch, with the command that applies it once that one is done.
|
|
1094
|
+
*/
|
|
1095
|
+
function applyTree(db, id, project, state) {
|
|
1096
|
+
const busy = inPlace(db, project, id);
|
|
1097
|
+
if (busy) {
|
|
1098
|
+
const patch = treeDiff(state.tree, { binary: true });
|
|
1099
|
+
if (patch.length > 0) writeFileSync(fileOf(id, 'change.patch'), patch, { mode: 0o600 });
|
|
1100
|
+
const kept = release(id, project, state);
|
|
1101
|
+
if (patch.length === 0) return 'It changed nothing; its worktree is removed.';
|
|
1102
|
+
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')}`;
|
|
1103
|
+
}
|
|
1104
|
+
const back = bringBack(project.path, state.tree, dirOf(id));
|
|
1105
|
+
if (!back.applied && !back.empty) {
|
|
1106
|
+
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})` : ''}`;
|
|
1107
|
+
}
|
|
1108
|
+
release(id, project, state);
|
|
1109
|
+
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.`;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/** Done, judged by the checks, and its tree still the one they judged: an attempt worth keeping. */
|
|
1113
|
+
function verifiedDone(db, id, state) {
|
|
1114
|
+
const tree = state?.tree?.path;
|
|
1115
|
+
return getJob(db, id).status === 'done' && state.verdict?.ok === true && existsSync(tree ?? '') && unmoved(tree, state.snap, state.verdict.fingerprint);
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
/**
|
|
1119
|
+
* What becomes of a closed worker's own worktree. Done, its change is brought back to the project and the tree removed —
|
|
1120
|
+
* or, where it does not apply, kept and said; failed or abandoned, the tree goes and what it changed stays as changes.diff.
|
|
1121
|
+
* One of several attempts keeps its tree only when verified done, and the last of them to close settles them all. Once per
|
|
1122
|
+
* job, by whoever gets there first; what happened is added to the report and handed back. Never throws: the job is closed either
|
|
1123
|
+
* way. One that fails part-way is not marked settled, so it can be tried again; where the tree is, and that its change was not
|
|
1124
|
+
* brought back, is said.
|
|
1125
|
+
*/
|
|
1126
|
+
export async function settle(db, id, now = new Date().toISOString()) {
|
|
1127
|
+
const job = getJob(db, id);
|
|
1128
|
+
const state = readState(db, id);
|
|
1129
|
+
if (!state?.tree || job.status === 'running' || job.status === 'needs_input' || !firstTo(db, settledKey(id), now)) return [];
|
|
1130
|
+
const project = getProject(db, job.project);
|
|
1131
|
+
const group = attemptsOf(db, id);
|
|
1132
|
+
let picking = false;
|
|
1133
|
+
try {
|
|
1134
|
+
if (!group) return said(id, 'Worktree', [job.status === 'done' ? applyTree(db, id, project, state) : `Its worktree is removed; what it changed is kept in ${release(id, project, state)}.`]);
|
|
1135
|
+
// Settled already, by another attempt that closed at the same moment.
|
|
1136
|
+
if (getMeta(db, pickedKey(group[0])) !== null) return [];
|
|
1137
|
+
const own = verifiedDone(db, id, state) ? [] : said(id, 'Worktree', [`Not verified done, so not a candidate: its worktree is removed; what it changed is kept in ${release(id, project, state)}.`]);
|
|
1138
|
+
const open = group.some((m) => ['running', 'needs_input'].includes(getJob(db, m).status));
|
|
1139
|
+
if (open || !firstTo(db, pickedKey(group[0]), now)) return own;
|
|
1140
|
+
picking = true;
|
|
1141
|
+
const lines = await settleAttempts(db, group, project, now);
|
|
1142
|
+
setMeta(db, pickedKey(group[0]), JSON.stringify({ lines }));
|
|
1143
|
+
return [...own, ...said(group[0], 'Attempts', lines)];
|
|
1144
|
+
} catch (cause) {
|
|
1145
|
+
unmark(db, settledKey(id));
|
|
1146
|
+
if (picking) unmark(db, pickedKey(group[0]));
|
|
1147
|
+
const where = existsSync(state.tree.top)
|
|
1148
|
+
? `the tree is still at ${state.tree.top} — bring it over by hand, then: git -C ${project.path} worktree remove --force ${state.tree.top}`
|
|
1149
|
+
: `what it changed is kept in ${fileOf(id, 'changes.diff')}`;
|
|
1150
|
+
return said(id, 'Worktree', [`Its worktree could not be settled: ${cause.message.trim()}. Its change was NOT brought back; ${where}`]);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/** A set of attempts, all closed: of the verified ones, one is picked and brought back, and every other tree goes. */
|
|
1155
|
+
async function settleAttempts(db, group, project, now) {
|
|
1156
|
+
const states = new Map(group.map((m) => [m, readState(db, m)]));
|
|
1157
|
+
const candidates = group.filter((m) => states.get(m)?.tree && verifiedDone(db, m, states.get(m)));
|
|
1158
|
+
const pick = candidates.length === 0 ? null : candidates.length === 1 ? { id: candidates[0], reason: 'the only one verified done' } : await pickAttempt(db, group[0], candidates.map((m) => ({ id: m, tree: states.get(m).tree })), now);
|
|
1159
|
+
for (const m of group) if (m !== pick?.id && existsSync(states.get(m)?.tree?.top ?? '')) release(m, project, states.get(m));
|
|
1160
|
+
const lines = [`${group.map((m) => `j${m} ${getJob(db, m).status}`).join(', ')}; verified done: ${candidates.map((m) => `j${m}`).join(', ') || 'none'}.`];
|
|
1161
|
+
if (!pick) return [...lines, 'None was verified done, so nothing was brought back.'];
|
|
1162
|
+
return [...lines, `Picked j${pick.id}: ${pick.reason}`, applyTree(db, pick.id, project, states.get(pick.id))];
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const PICK_DIFF_CHARS = 20_000;
|
|
1166
|
+
const PICK_SCHEMA = { type: 'object', properties: { pick: { type: 'integer' }, reason: { type: 'string' } }, required: ['pick', 'reason'], additionalProperties: false };
|
|
1167
|
+
const PICK_SYSTEM =
|
|
1168
|
+
"Several attempts were made at one coding task, and each passed the project's checks. Pick the one to keep: the one that does all the task asks, " +
|
|
1169
|
+
'correctly, with the least that was not asked for. Answer with its number and one sentence saying why.';
|
|
1170
|
+
|
|
1171
|
+
/** Which of several verified attempts to keep: one structured answer from sonnet, given the task and each change; the smallest change when none comes. */
|
|
1172
|
+
async function pickAttempt(db, first, candidates, now) {
|
|
1173
|
+
const diffs = candidates.map(({ tree }) => treeDiff(tree));
|
|
1174
|
+
const shown = (d) => (d.length > PICK_DIFF_CHARS ? `${head(d, PICK_DIFF_CHARS)}\n[… ${d.length - PICK_DIFF_CHARS} more characters not shown]` : d);
|
|
1175
|
+
const prompt = [`## The task\n${taskFromBrief(readOr(fileOf(first, 'brief.md'))) ?? ''}`, ...diffs.map((d, i) => `## Attempt ${i + 1}\n${shown(d)}`)].join('\n\n');
|
|
1176
|
+
const result = usableModels(db).includes('sonnet') ? await callModel(db, { system: PICK_SYSTEM, prompt, schema: PICK_SCHEMA, model: 'sonnet', kind: 'pick' }) : failure('sonnet is switched off');
|
|
1177
|
+
const n = result.data?.pick;
|
|
1178
|
+
const ok = result.ok && Number.isInteger(n) && n >= 1 && n <= candidates.length;
|
|
1179
|
+
const why = ok ? `picked attempt ${n}` : (result.error ?? `there is no attempt ${n}`);
|
|
1180
|
+
logRun(db, { kind: 'pick', model: 'sonnet', result: { ...result, ok }, note: why, now, jobId: first });
|
|
1181
|
+
if (ok) return { id: candidates[n - 1].id, reason: String(result.data.reason ?? '').trim() || 'no reason was given' };
|
|
1182
|
+
const smallest = diffs.reduce((at, d, i) => (d.length < diffs[at].length ? i : at), 0);
|
|
1183
|
+
return { id: candidates[smallest].id, reason: `the smallest change — no pick came back (${why})` };
|
|
1184
|
+
}
|