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/chat.mjs
CHANGED
|
@@ -8,9 +8,9 @@ import { ago } from './card.mjs';
|
|
|
8
8
|
import { getMeta } from './db.mjs';
|
|
9
9
|
import { runDream } from './dream.mjs';
|
|
10
10
|
import { openWatchTab, runHerdr } from './herdr.mjs';
|
|
11
|
-
import { appendLive, endLive, getJob, newJob, parseJobId, reportOf, startLive, tell as tellJob } from './jobs.mjs';
|
|
11
|
+
import { appendLive, attemptsSettled, endLive, getJob, newJob, parseJobId, reportOf, runFollowed, startLive, tell as tellJob, treeOf } from './jobs.mjs';
|
|
12
12
|
import { handleEvent } from './hooks.mjs';
|
|
13
|
-
import { contextFor, converse, paramsFor, runJob, runLines, sendToApi, stopReason, textOf, thinkingFor, withMcp, withServers } from './loop.mjs';
|
|
13
|
+
import { contextFor, converse, networkOn, paramsFor, runJob, runLines, sandboxOn, sendToApi, stopReason, TAMPER_REFUSED, tampersMarker, textOf, thinkingFor, withMcp, withServers } from './loop.mjs';
|
|
14
14
|
import { closeMcp, compactInput, gateMcp, isMcpTool, mcpLines, mcpReady, splitMcpName } from './mcp.mjs';
|
|
15
15
|
import { assertOn, MODEL_IDS, MODELS, modelId, usableModels } from './catalog.mjs';
|
|
16
16
|
import { takeLock } from './lock.mjs';
|
|
@@ -22,10 +22,12 @@ import { redact } from './redact.mjs';
|
|
|
22
22
|
import { chooseChatRoute, EFFORTS } from './route.mjs';
|
|
23
23
|
import { verifyCommands } from './scan.mjs';
|
|
24
24
|
import { outcomeLines, runScribe } from './scribe.mjs';
|
|
25
|
-
import { currentProject, describeSaved, findSaved, loadState, lockFile, saveState, savedSessions, sessionLine, shortId, withoutThinking } from './sessions.mjs';
|
|
26
|
-
import { BASH_TOOL, cap, capRedacted, DELEGATE_TOOL, EDITOR_TOOL, INTERRUPTED, runCommand } from './tools.mjs';
|
|
25
|
+
import { currentProject, describeSaved, findSaved, loadState, lockFile, recordTurn, saveState, savedSessions, sessionLine, shortId, withoutThinking } from './sessions.mjs';
|
|
26
|
+
import { ASK_TOOL, BASH_TOOL, cap, capRedacted, DELEGATE_TOOL, EDITOR_TOOL, INTERRUPTED, nextUndoTurn, runCommand, undoLast } from './tools.mjs';
|
|
27
27
|
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
28
|
-
import {
|
|
28
|
+
import { estimateTokens, head } from './text.mjs';
|
|
29
|
+
import { colourEnabled, createRenderer, header, pathFrom, prompt, renderBlock, safeForTerminal, styles, tilde, widthOf } from './tty.mjs';
|
|
30
|
+
import { hasCommit } from './verify.mjs';
|
|
29
31
|
import { sourcesOf, WEB_FETCH_TOOL, WEB_SEARCH_TOOL, WEB_TOOLS } from './web.mjs';
|
|
30
32
|
|
|
31
33
|
/**
|
|
@@ -75,6 +77,7 @@ export function commandList(db, { effortNow = () => null, saved = () => [], runn
|
|
|
75
77
|
{ name: 'mcp', hint: 'the MCP servers: connected or not, their tools, what may run unasked' },
|
|
76
78
|
{ name: 'resume', hint: 'pick a saved session up where it stopped', choices: (given) => (given.length === 0 ? saved().map((s) => ({ name: shortId(s.id), hint: s.hint })) : []) },
|
|
77
79
|
{ name: 'copy', hint: 'the last reply to the clipboard, as it was written; `code` for its last code block', choices: (given) => (given.length === 0 ? ['code'] : []) },
|
|
80
|
+
{ name: 'undo', hint: "put back the files the editor changed in the last turn that changed any; again for the turn before — bash's changes are not tracked" },
|
|
78
81
|
{ name: 'stop', hint: 'stop a job at work in the background', choices: (given) => (given.length === 0 ? running().map((id) => `j${id}`) : []) },
|
|
79
82
|
{ name: 'help', hint: 'the commands and the keys' },
|
|
80
83
|
{ name: 'new', hint: 'start a fresh session' },
|
|
@@ -99,6 +102,31 @@ export function injection(text, model, messages = []) {
|
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
const asText = (text) => ({ role: 'user', content: [{ type: 'text', text: `<sumo>\n${text}\n</sumo>` }] });
|
|
105
|
+
/** Something that happened — a background job's report — as a block of a user turn: labelled as an event, never wrapped as operator text. */
|
|
106
|
+
const eventBlock = (text) => ({ type: 'text', text: `<event>\n${text}\n</event>` });
|
|
107
|
+
|
|
108
|
+
/** Automated turns in a row — a report read, which delegates, which reports — before the reports wait for the user's next message instead. */
|
|
109
|
+
const MAX_WOKEN = 3;
|
|
110
|
+
/** Jobs at work here at once: each report can start a turn, and each turn can delegate again. */
|
|
111
|
+
const MAX_JOBS = 4;
|
|
112
|
+
/** About how much of a job's report the chat's model is given; the rest is a command away, and the screen shows it whole. */
|
|
113
|
+
const REPORT_TOKENS = 1500;
|
|
114
|
+
/** The sections code adds at the end of a report: the checks' verdict, and where a worktree's change went. */
|
|
115
|
+
const ADDED_BY_CODE = /\n## (?:Verified by code|Accepted without verification|Worktree|Attempts)\b/;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A job's report as the chat's model is given it: whole when it fits in REPORT_TOKENS; otherwise the head of what its
|
|
119
|
+
* author wrote, what code added after it whole — the verdict, whether the change was brought back — and a last line
|
|
120
|
+
* that says where the whole report is.
|
|
121
|
+
*/
|
|
122
|
+
function reportForModel(report, id) {
|
|
123
|
+
if (estimateTokens(report) <= REPORT_TOKENS) return report;
|
|
124
|
+
const at = report.search(ADDED_BY_CODE);
|
|
125
|
+
const added = at === -1 ? '' : report.slice(at);
|
|
126
|
+
const last = `The report is cut to fit; the whole of it: sumo job show ${id}`;
|
|
127
|
+
const room = Math.max(0, REPORT_TOKENS * 4 - added.length - last.length - 40);
|
|
128
|
+
return `${head(at === -1 ? report : report.slice(0, at), room).trimEnd()}\n[… cut]${added}\n${last}`;
|
|
129
|
+
}
|
|
102
130
|
|
|
103
131
|
/** One line per tool call, so the user can watch the work: the command, or the edit and its file. */
|
|
104
132
|
export function describeCall(call) {
|
|
@@ -196,6 +224,8 @@ export function jobPrinter(write, s, { width = 98, clean = (text) => text } = {}
|
|
|
196
224
|
export const retryLine = ({ attempt, limit, delayMs, reason }) => `retrying in ${Math.max(1, Math.round(delayMs / 1000))}s (${attempt} of ${limit}) — ${reason}`;
|
|
197
225
|
/** A conversation summarized to make room, in one line: how big it had grown. */
|
|
198
226
|
export const compactLine = ({ before }) => `the conversation was summarized at ${Math.round(before / 1000)}k tokens to make room, and goes on from the summary`;
|
|
227
|
+
/** A session past its budget, in one line: what it has cost, how to start afresh, and how to move the line. */
|
|
228
|
+
export const budgetLine = (cost) => `this session has cost $${cost.toFixed(2)} — /new starts a fresh one; sumo config chat.budget changes when this is said`;
|
|
199
229
|
|
|
200
230
|
/** The session log, one JSON line per message, the shape the scribe reads assistant replies from and the gauge reads usage from. */
|
|
201
231
|
function logLine(file, entry) {
|
|
@@ -206,6 +236,14 @@ function configured(db, key) {
|
|
|
206
236
|
return getMeta(db, `config.${key}`) ?? CONFIG_DEFAULTS[key];
|
|
207
237
|
}
|
|
208
238
|
|
|
239
|
+
/** The chat's own tools, in a fixed order so its cached prefix never moves; the MCP servers' come after them. */
|
|
240
|
+
export const CHAT_TOOLS = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ASK_TOOL, ...WEB_TOOLS];
|
|
241
|
+
/**
|
|
242
|
+
* The chat's system prompt: AGENTS.md. The guides it names are read from wherever the chat was started: their paths are
|
|
243
|
+
* made absolute, or the model goes looking for them.
|
|
244
|
+
*/
|
|
245
|
+
export const chatSystem = () => readFileSync(join(REPO_ROOT, 'AGENTS.md'), 'utf8').trim().replaceAll(/(?<![\w/])guides\//g, `${join(REPO_ROOT, 'guides')}/`);
|
|
246
|
+
|
|
209
247
|
/**
|
|
210
248
|
* One chat session's state and policy, apart from the terminal, so it can be
|
|
211
249
|
* driven by tests with canned responses. `say(text)` is one user turn;
|
|
@@ -219,13 +257,16 @@ function configured(db, key) {
|
|
|
219
257
|
* for each turn's model when the model is `auto`. `approve` puts an MCP call
|
|
220
258
|
* that is not on the allow list to the user — the chat's own, or a job's run
|
|
221
259
|
* here — and answers `once`, `always` or `no`; without it such a call is refused.
|
|
260
|
+
* `ask` puts a question of the model's to the user, with its options, and
|
|
261
|
+
* answers `{ label }` for the one picked or `{ text }` for words of their own —
|
|
262
|
+
* anything else is no answer; without it the model is told nobody is there.
|
|
222
263
|
* With `background`, a delegated job does not hold the turn: `delegate` answers
|
|
223
264
|
* at once, the job runs beside the conversation with a stop of its own, and its
|
|
224
265
|
* report reaches the model later as an automated event — with its next request
|
|
225
266
|
* if it is at work, else in a turn of its own (`wake`), which whoever watches
|
|
226
267
|
* starts when told `job-report`.
|
|
227
268
|
*/
|
|
228
|
-
export function createChat(db, { model, effort, cwd = process.cwd(), send = sendToApi, out = () => {}, activity = () => {}, watch = () => {}, herdr = runHerdr, env = process.env, route = chooseChatRoute, approve = null, background = false, now = () => new Date().toISOString() } = {}) {
|
|
269
|
+
export function createChat(db, { model, effort, cwd = process.cwd(), send = sendToApi, out = () => {}, activity = () => {}, watch = () => {}, herdr = runHerdr, env = process.env, route = chooseChatRoute, approve = null, ask = null, background = false, now = () => new Date().toISOString() } = {}) {
|
|
229
270
|
// A route the chat was started with holds through a resumed session; one it was not, the saved session brings back.
|
|
230
271
|
const given = { model: model !== undefined, effort: effort !== undefined };
|
|
231
272
|
model ??= configured(db, 'chat.model');
|
|
@@ -238,9 +279,8 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
238
279
|
if (modelId(model) === MODEL_IDS.haiku) effort = 'none';
|
|
239
280
|
/** On auto, the route the last turn ran on; null otherwise. */
|
|
240
281
|
let routed = null;
|
|
241
|
-
|
|
242
|
-
const
|
|
243
|
-
const own = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ...WEB_TOOLS];
|
|
282
|
+
const system = chatSystem();
|
|
283
|
+
const own = CHAT_TOOLS;
|
|
244
284
|
/** The MCP servers of this process, once connected: their tools join the request, deferred, and their names the system prompt. */
|
|
245
285
|
let registry = null;
|
|
246
286
|
const connecting = mcpReady().then((r) => {
|
|
@@ -258,8 +298,12 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
258
298
|
let lock = null;
|
|
259
299
|
/** Whether there is anything to save yet: a session nobody has spoken to is not kept. */
|
|
260
300
|
let spoken = false;
|
|
261
|
-
/** Said to the model with the next turn: that the session was picked up again. */
|
|
301
|
+
/** Said to the model with the next turn: that the session was picked up again, or that /undo put files back. */
|
|
262
302
|
let note = null;
|
|
303
|
+
/** The files the model has viewed or written in this session, as they stood then: an edit to one changed on disk since says so. */
|
|
304
|
+
let seen = new Map();
|
|
305
|
+
/** Whether the screen has said what this session cost: once a session, when it first passes `chat.budget`. */
|
|
306
|
+
let costSaid = false;
|
|
263
307
|
/** The jobs running here, each with what the user has said to it that it has not read yet. */
|
|
264
308
|
const jobs = new Map();
|
|
265
309
|
/** The projects a worker is running in: two in one working tree overwrite each other. */
|
|
@@ -274,6 +318,8 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
274
318
|
const stoppedByUser = new Set();
|
|
275
319
|
/** What background jobs reported and the model has not read yet: `{ id, text, quiet }`. */
|
|
276
320
|
const reports = [];
|
|
321
|
+
/** Turns nobody typed since the user last did: past MAX_WOKEN, what the jobs report waits for the user. */
|
|
322
|
+
let woken = 0;
|
|
277
323
|
/** The memory page, once /memory has asked for it: the promise of its address. */
|
|
278
324
|
let page = null;
|
|
279
325
|
const ledger = { kind: 'chat', sessionId: null };
|
|
@@ -307,7 +353,9 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
307
353
|
lock = held ?? takeLock(lockFile(sessionId), busy(sessionId));
|
|
308
354
|
ledger.sessionId = sessionId;
|
|
309
355
|
lastContext = saved?.contextTokens ?? 0;
|
|
356
|
+
costSaid = false;
|
|
310
357
|
project = null;
|
|
358
|
+
seen = new Map();
|
|
311
359
|
spoken = Boolean(saved);
|
|
312
360
|
transcript = join(paths().logs, 'sessions', `${sessionId}.jsonl`);
|
|
313
361
|
const block = handleEvent(db, 'session-start', { session_id: sessionId, cwd, transcript_path: transcript, source }, now());
|
|
@@ -380,19 +428,52 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
380
428
|
|
|
381
429
|
const event = (name, payload) => handleEvent(db, name, { session_id: sessionId, cwd, transcript_path: transcript, ...payload }, now());
|
|
382
430
|
|
|
431
|
+
/** Where this session's turns keep what the editor changed, for /undo. */
|
|
432
|
+
const undoDir = () => join(paths().logs, 'undo', sessionId);
|
|
433
|
+
|
|
383
434
|
/** The project the work is in: the one whose card came in last, else none (tools then run in scratch, wherever the chat was started). */
|
|
384
435
|
function workspace() {
|
|
385
436
|
const slug = currentProject(db, sessionId);
|
|
386
437
|
if (slug && project?.slug !== slug) project = getProject(db, slug);
|
|
387
|
-
return project
|
|
438
|
+
return contextFor(project ?? { path: scratchDir() }, { sandbox: sandboxOn(db), network: networkOn(db) });
|
|
388
439
|
}
|
|
389
440
|
|
|
390
|
-
/**
|
|
441
|
+
/**
|
|
442
|
+
* A question for the user. Memory is read first, and holds it back once if it may hold the answer. Then it waits on
|
|
443
|
+
* the user: a pick is kept as their decision, with the question; what they typed instead is their words, kept as a turn is.
|
|
444
|
+
*/
|
|
445
|
+
async function askUser(input) {
|
|
446
|
+
const line = (v) => safeForTerminal(String(v ?? '')).replace(/\s+/g, ' ').trim();
|
|
447
|
+
const question = safeForTerminal(String(input.question ?? '')).trim();
|
|
448
|
+
const options = (Array.isArray(input.options) ? input.options : []).map((o) => ({ label: line(o?.label ?? o), ...(line(o?.detail) ? { detail: line(o.detail) } : {}) }));
|
|
449
|
+
if (!question || options.length < 2 || options.length > 4 || options.some((o) => !o.label)) return { content: 'not asked: give `question` and 2–4 `options`, each with a `label`.', isError: true };
|
|
450
|
+
const gate = event('pre-tool', { tool_name: ASK_TOOL.name, tool_input: { question } });
|
|
451
|
+
if (gate) return { content: JSON.parse(gate).deny, isError: true };
|
|
452
|
+
if (!ask) return { content: 'not asked: nobody is here to answer. Decide it yourself and say what you assumed, or end your turn on the question.', isError: true };
|
|
453
|
+
const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve('no'), { once: true }));
|
|
454
|
+
const answer = await Promise.race([ask({ question, options, signal: stopper.signal }), stopped]);
|
|
455
|
+
const picked = options.find((o) => o.label === answer?.label);
|
|
456
|
+
if (picked) {
|
|
457
|
+
// A pick is the user's decision in the model's wording: kept beside what they typed, with the question it answers.
|
|
458
|
+
recordTurn(db, { sessionId, text: `${question} → ${picked.label}`, now: now() });
|
|
459
|
+
return { content: `The user chose: ${picked.label}` };
|
|
460
|
+
}
|
|
461
|
+
const text = typeof answer?.text === 'string' ? answer.text.trim() : '';
|
|
462
|
+
if (!text) return { content: 'no answer: the user turned the question away. Do not ask it again; decide it yourself and say what you assumed, or carry on another way.', isError: true };
|
|
463
|
+
const brought = event('prompt', { prompt: text, context_tokens: lastContext });
|
|
464
|
+
noteGuides(brought);
|
|
465
|
+
logLine(transcript, { type: 'user', message: { role: 'user', content: redact(text).text } });
|
|
466
|
+
return { content: `The user answered: ${text}${brought ? `\n<sumo>\n${brought}\n</sumo>` : ''}` };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** The workflow gate, the cheap-model passes, `delegate` and `ask`; anything else is the tool's own business. */
|
|
391
470
|
async function beforeTool(call) {
|
|
392
471
|
if (call.name === DELEGATE_TOOL.name) return delegate(call.input ?? {}, call.id);
|
|
472
|
+
if (call.name === ASK_TOOL.name) return askUser(call.input ?? {});
|
|
393
473
|
if (isMcpTool(call.name)) return gateMcp(call, { approve, signal: stopper.signal });
|
|
394
474
|
if (call.name !== BASH_TOOL.name) return null;
|
|
395
475
|
const command = String(call.input?.command ?? '');
|
|
476
|
+
if (tampersMarker(command)) return { content: TAMPER_REFUSED, isError: true };
|
|
396
477
|
const gate = event('pre-tool', { tool_name: 'bash', tool_input: { command } });
|
|
397
478
|
if (gate) return { content: JSON.parse(gate).deny, isError: true };
|
|
398
479
|
const pass = PASS_RUN.exec(command);
|
|
@@ -408,15 +489,19 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
408
489
|
/**
|
|
409
490
|
* `delegate`: a job is made from the brief, or an open one picked up, and run here on its own route. The chat waits
|
|
410
491
|
* for its report — or, working in the background, is answered at once and reads the report when it comes. Calls in
|
|
411
|
-
* one reply run side by side, but a worker has its project's working tree to itself
|
|
492
|
+
* one reply run side by side, but a worker has its project's working tree to itself — in a git project, a second one
|
|
493
|
+
* is given a worktree of its own. Several `attempts` run side by side too, and are answered for once, together.
|
|
412
494
|
* `callId` is the delegate call's own, so a screen can show the job under the call that started it.
|
|
413
495
|
*/
|
|
414
496
|
async function delegate(input, callId = null) {
|
|
497
|
+
const asked = (input.job === undefined || input.job === null) && [2, 3].includes(input.attempts) ? input.attempts : 1;
|
|
498
|
+
// Counted where a run begins, before anything is awaited: five calls in one reply are judged one after another, not all at once.
|
|
499
|
+
if (jobs.size + asked > MAX_JOBS) throw new UsageError(`${[...jobs.keys()].map((j) => `j${j}`).join(', ')} are at work already — ${MAX_JOBS} at once is the most; wait for a report`);
|
|
415
500
|
const own = background ? new AbortController() : null;
|
|
416
501
|
const signal = own?.signal ?? stopper.signal;
|
|
417
502
|
let claimed = null;
|
|
418
|
-
const claim = (agent, slug) => {
|
|
419
|
-
if (agent !== 'worker') return;
|
|
503
|
+
const claim = (agent, slug, shared) => {
|
|
504
|
+
if (agent !== 'worker' || !shared) return;
|
|
420
505
|
if (workers.has(slug)) throw new UsageError(`a worker is already running in ${slug} — two in one working tree overwrite each other; wait for its report`);
|
|
421
506
|
workers.add(slug);
|
|
422
507
|
claimed = slug;
|
|
@@ -425,19 +510,23 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
425
510
|
if (claimed) workers.delete(claimed);
|
|
426
511
|
};
|
|
427
512
|
let id;
|
|
513
|
+
/** The other attempts made beside the first, when several were asked for. */
|
|
514
|
+
let siblings = [];
|
|
428
515
|
let warnings = [];
|
|
429
516
|
try {
|
|
430
517
|
if (input.job !== undefined && input.job !== null) {
|
|
431
518
|
const job = getJob(db, parseJobId(input.job));
|
|
432
|
-
claim(job.agent, job.project);
|
|
519
|
+
claim(job.agent, job.project, treeOf(db, job.id) === null);
|
|
433
520
|
id = job.id;
|
|
434
521
|
} else {
|
|
435
522
|
const agent = input.agent ?? 'worker';
|
|
436
523
|
const project = getProject(db, input.project ?? '');
|
|
437
524
|
if (!project) throw new UsageError(`no project "${input.project ?? ''}" — the delegate call names one by its slug`);
|
|
438
|
-
claim(agent, project.slug);
|
|
525
|
+
claim(agent, project.slug, !hasCommit(project.path));
|
|
439
526
|
activity(`briefing a ${agent}…`);
|
|
440
|
-
|
|
527
|
+
const made = await newJob(db, { project: project.slug, title: input.title, agent, task: input.task, guide: input.guide, reviews: input.reviews ?? undefined, testsMayChange: Boolean(input.tests_may_change), attempts: input.attempts ?? 1, now: now() });
|
|
528
|
+
({ job: { id }, warnings } = made);
|
|
529
|
+
siblings = (made.jobs ?? []).slice(1).map((job) => job.id);
|
|
441
530
|
}
|
|
442
531
|
} catch (cause) {
|
|
443
532
|
release();
|
|
@@ -445,35 +534,72 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
445
534
|
}
|
|
446
535
|
let begun;
|
|
447
536
|
const started = new Promise((resolve) => (begun = resolve));
|
|
448
|
-
const
|
|
537
|
+
const ids = [id, ...siblings];
|
|
538
|
+
// A job made to follow this one — its retry, or its review — is at work under the same call, and stopped with it.
|
|
539
|
+
const follow = (next) => {
|
|
540
|
+
ids.push(next);
|
|
541
|
+
if (own) stops.set(next, own);
|
|
542
|
+
};
|
|
543
|
+
const work = (siblings.length === 0 ? runFollowedHere(id, warnings, { signal, callId, begun }, follow) : runAttempts(ids, warnings, { signal, callId, begun })).finally(release);
|
|
449
544
|
if (!own) return work;
|
|
450
545
|
|
|
451
546
|
// In the background. A job that cannot start says so as the call's own answer; one that has started answers later.
|
|
452
|
-
stops.
|
|
547
|
+
// Attempts share one stop: stopping one stops them all.
|
|
548
|
+
for (const each of ids) stops.set(each, own);
|
|
453
549
|
const first = await Promise.race([started, work.then(() => null, (cause) => ({ cause }))]);
|
|
454
550
|
if (first?.cause) {
|
|
455
|
-
stops.delete(
|
|
551
|
+
for (const each of ids) stops.delete(each);
|
|
456
552
|
throw first.cause;
|
|
457
553
|
}
|
|
458
554
|
const run = work
|
|
459
555
|
.then((result) => result, (cause) => ({ content: `j${id} stopped on an error: ${cause.message}`, isError: true }))
|
|
460
556
|
.then((result) => {
|
|
461
|
-
stops.delete(
|
|
557
|
+
for (const each of ids) stops.delete(each);
|
|
462
558
|
runs.delete(run);
|
|
463
|
-
const quiet = stoppedByUser.delete(
|
|
464
|
-
reports.push({ id, quiet, text: `An automated event, not a message from the user: j${
|
|
559
|
+
const quiet = ids.filter((each) => stoppedByUser.delete(each)).length > 0;
|
|
560
|
+
reports.push({ id, quiet, text: `An automated event, not a message from the user: ${ids.map((each) => `j${each}`).join(', ')} ${ids.length === 1 ? 'has' : 'have'} reported.\n${result.content}` });
|
|
465
561
|
watch({ type: 'job-report', job: id, result, quiet });
|
|
466
562
|
});
|
|
467
563
|
runs.add(run);
|
|
468
564
|
const job = getJob(db, id);
|
|
565
|
+
const beside = siblings.length === 0 ? '' : `, with ${siblings.map((each) => `j${each}`).join(' and ')} beside it: attempts at the same task, reported on together`;
|
|
469
566
|
return {
|
|
470
567
|
content:
|
|
471
|
-
`j${id} is running in the background (${job.agent} · ${routeOf(job)} — ${job.title}). Its report will reach you later, as an automated event marked as ` +
|
|
568
|
+
`j${id} is running in the background (${job.agent} · ${routeOf(job)} — ${job.title})${beside}. Its report will reach you later, as an automated event marked as ` +
|
|
472
569
|
'not from the user: with your next request if you are still at work, otherwise in a turn of its own. Until then you know nothing of what it found — ' +
|
|
473
570
|
'do not guess its result, and do not do its work yourself. Carry on with what does not depend on it, or end your turn and say it is running.',
|
|
474
571
|
};
|
|
475
572
|
}
|
|
476
573
|
|
|
574
|
+
/** Attempts at one task, run side by side and answered for once, when all have ended: what was kept first, then how each went. */
|
|
575
|
+
async function runAttempts(ids, warnings, options) {
|
|
576
|
+
const ended = await Promise.allSettled(ids.map((each) => runHere(each, [], options)));
|
|
577
|
+
const kept = attemptsSettled(db, ids[0]) ?? ['Not every attempt closed, so none was picked or brought back yet.'];
|
|
578
|
+
const each = ended.map((e, i) => (e.status === 'fulfilled' ? e.value.content : `j${ids[i]} stopped on an error: ${e.reason?.message}`));
|
|
579
|
+
return { content: capRedacted([[...warnings, `Attempts ${ids.map((j) => `j${j}`).join(', ')}:`, ...kept].join('\n'), ...each].join('\n\n')) };
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* One job run here, and what follows it unasked — a retry one step up of a first attempt that failed, a review of
|
|
584
|
+
* risky work done (`runFollowed`, src/jobs.mjs) — answered for once. A retry stands in for the attempt that failed,
|
|
585
|
+
* after the line that says so; a review comes after the work it judged. `onFollow` hears each job made, before it runs.
|
|
586
|
+
*/
|
|
587
|
+
async function runFollowedHere(id, warnings, options, onFollow) {
|
|
588
|
+
const ran = await runFollowed(db, id, (each, said = warnings) => runHere(each, said, options), { signal: options.signal, onFollow, now });
|
|
589
|
+
if (ran.length === 1) return ran[0].result;
|
|
590
|
+
let parts = [];
|
|
591
|
+
let shown = [];
|
|
592
|
+
let job = null;
|
|
593
|
+
for (const step of ran) {
|
|
594
|
+
// The attempt that failed is one line now; its report is a command away.
|
|
595
|
+
if (step.kind === 'retry') [parts, shown, job] = [[], [], null];
|
|
596
|
+
parts.push([step.line, step.result?.content ?? (step.error ? `j${step.id} stopped on an error: ${step.error}` : '')].filter(Boolean).join('\n'));
|
|
597
|
+
shown.push([step.line, step.result?.job?.report].filter(Boolean).join('\n'));
|
|
598
|
+
job ??= step.result?.job ?? null;
|
|
599
|
+
}
|
|
600
|
+
return { content: parts.join('\n\n'), job: job && { ...job, report: shown.join('\n\n') } };
|
|
601
|
+
}
|
|
602
|
+
|
|
477
603
|
/** One job, run here to its end: watched as it works, written down for `sumo job watch`, and answered with how it went and what it reported. */
|
|
478
604
|
async function runHere(id, warnings, { signal, callId, begun }) {
|
|
479
605
|
const inbox = [];
|
|
@@ -505,7 +631,10 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
505
631
|
live.onResult(call, result);
|
|
506
632
|
watch({ type: 'result', call, result, job: id });
|
|
507
633
|
},
|
|
508
|
-
onTurn:
|
|
634
|
+
onTurn: (response, totals) => {
|
|
635
|
+
live.onTurn(response, totals);
|
|
636
|
+
overBudget();
|
|
637
|
+
},
|
|
509
638
|
onThinking: (_delta, snapshot) => watch({ type: 'thinking', job: id, text: snapshot }),
|
|
510
639
|
onRetry: (r) => {
|
|
511
640
|
live.onRetry(r);
|
|
@@ -518,10 +647,10 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
518
647
|
});
|
|
519
648
|
ended = runLines(outcome);
|
|
520
649
|
const report = reportOf(id);
|
|
521
|
-
// Model-written, like any tool output: redacted before it reaches the chat's context. `job` is how it went, for the screen only.
|
|
650
|
+
// Model-written, like any tool output: redacted before it reaches the chat's context, and cut to fit there. `job` is how it went, for the screen only.
|
|
522
651
|
const { job, turns, totals } = outcome;
|
|
523
652
|
return {
|
|
524
|
-
content: capRedacted([...warnings, ...ended, ...(report ? ['', report] : [])].join('\n')),
|
|
653
|
+
content: capRedacted([...warnings, ...ended, ...(report ? ['', reportForModel(report, id)] : [])].join('\n')),
|
|
525
654
|
job: { id, status: job.status, seconds: Math.round((Date.now() - began) / 1000), turns, toolCalls: totals.toolCalls, costUsd: totals.costUsd, model: job.model, effort: job.effort, report: report ? redact(report).text : '' },
|
|
526
655
|
};
|
|
527
656
|
} catch (cause) {
|
|
@@ -554,10 +683,14 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
554
683
|
* Null when nothing is waiting — a job the user stopped waits for the next message instead.
|
|
555
684
|
*/
|
|
556
685
|
async function wake() {
|
|
557
|
-
if (
|
|
686
|
+
if (waiting() === 0) return null;
|
|
687
|
+
woken++;
|
|
558
688
|
return say(reports.splice(0).map((r) => r.text).join('\n\n'), '', [], { automated: true });
|
|
559
689
|
}
|
|
560
690
|
|
|
691
|
+
/** How many reports wait for a turn of their own: none once MAX_WOKEN automated turns have run in a row — they wait for the user's next message. */
|
|
692
|
+
const waiting = () => (woken >= MAX_WOKEN ? 0 : reports.filter((r) => !r.quiet).length);
|
|
693
|
+
|
|
561
694
|
/** The job's tab in Herdr, showing its work: its pane, or why there is none — the job runs here either way. A watcher that never starts in it is said when it gives up. */
|
|
562
695
|
function watchTab(job) {
|
|
563
696
|
try {
|
|
@@ -680,23 +813,29 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
680
813
|
watch({ type: 'route', ...routed });
|
|
681
814
|
}
|
|
682
815
|
settleTail();
|
|
683
|
-
// A resumed session is told so with its first turn
|
|
684
|
-
|
|
685
|
-
const injected = [note, ...(automated ? [] : [...reports.splice(0).map((r) => r.text), event('prompt', { prompt: said, context_tokens: lastContext, guide })])].filter(Boolean).join('\n');
|
|
816
|
+
// A resumed session is told so with its first turn. An automated turn is a report itself: nothing in it is the user's, so nothing of it is kept as theirs.
|
|
817
|
+
const injected = [note, ...(automated ? [] : [event('prompt', { prompt: said, context_tokens: lastContext, guide })])].filter(Boolean).join('\n');
|
|
686
818
|
noteGuides(injected);
|
|
687
819
|
note = null;
|
|
820
|
+
if (!automated) woken = 0;
|
|
688
821
|
const pictures = images.flatMap(({ label, mediaType, data }) => [{ type: 'text', text: label }, { type: 'image', source: { type: 'base64', media_type: mediaType, data } }]);
|
|
689
|
-
|
|
822
|
+
// What background jobs reported while nobody was looking rides in with the message, as blocks of its own: an event in the user's turn,
|
|
823
|
+
// never operator text — a report repeats what a page or a file said, and that is nobody's instruction.
|
|
824
|
+
const events = reports.splice(0).map((r) => eventBlock(r.text));
|
|
825
|
+
const at = params.messages.push(automated ? { role: 'user', content: [eventBlock(text)] } : { role: 'user', content: [...pictures, { type: 'text', text }, ...events] }) - 1;
|
|
690
826
|
spoken = true;
|
|
691
827
|
if (injected) params.messages.push(injection(injected, params.model, params.messages));
|
|
692
828
|
// The log keeps what memory keeps: the user's words without their secrets — and an event as an event.
|
|
693
829
|
logLine(transcript, automated ? { type: 'event', text: redact(text).text } : { type: 'user', message: { role: 'user', content: redact(text).text } });
|
|
694
830
|
|
|
831
|
+
// What the editor changes in this turn is kept for /undo in one folder, however many requests the turn takes.
|
|
832
|
+
const undoFolder = nextUndoTurn(undoDir());
|
|
695
833
|
let continued = false;
|
|
696
834
|
for (;;) {
|
|
697
835
|
const outcome = await converse(db, params, {
|
|
698
836
|
send: sendAs,
|
|
699
|
-
|
|
837
|
+
// A command the sandbox kept from writing is put to the user as an MCP call off the allow list is, for one run outside it.
|
|
838
|
+
ctx: { ...workspace(), seen, undo: undoFolder, approve },
|
|
700
839
|
ledger,
|
|
701
840
|
beforeTool,
|
|
702
841
|
concurrent: (call) => call.name === DELEGATE_TOOL.name,
|
|
@@ -719,6 +858,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
719
858
|
lastContext = totals.contextTokens;
|
|
720
859
|
logLine(transcript, { type: 'assistant', message: { role: 'assistant', model: response.model ?? params.model, usage: response.usage, content: response.content } });
|
|
721
860
|
watch({ type: 'usage', totals });
|
|
861
|
+
overBudget();
|
|
722
862
|
// What the reply rests on, said under it: the pages it cites.
|
|
723
863
|
const sources = sourcesOf(response.content);
|
|
724
864
|
if (sources.length > 0) watch({ type: 'sources', sources });
|
|
@@ -727,7 +867,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
727
867
|
// Pictures the API refused would ride along with every later turn and be refused again: a turn refused before the model saw it keeps only its words.
|
|
728
868
|
// Only a refusal of the request itself (a 4xx); a rate limit, an overload or a dropped connection says nothing against the pictures.
|
|
729
869
|
const rejected = outcome.status >= 400 && outcome.status < 500 && outcome.status !== 429;
|
|
730
|
-
if (outcome.stop === 'error' && rejected && pictures.length > 0 && !params.messages.slice(at + 1).some((m) => m.role === 'assistant')) params.messages[at].content = [{ type: 'text', text }];
|
|
870
|
+
if (outcome.stop === 'error' && rejected && pictures.length > 0 && !params.messages.slice(at + 1).some((m) => m.role === 'assistant')) params.messages[at].content = [{ type: 'text', text }, ...events];
|
|
731
871
|
// Stopped by the user: the turn still ended, so the scribe is woken, but nothing is asked of memory and nothing continues.
|
|
732
872
|
if (outcome.stop === 'interrupted') {
|
|
733
873
|
event('stop', { stop_hook_active: true });
|
|
@@ -748,6 +888,24 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
748
888
|
}
|
|
749
889
|
}
|
|
750
890
|
|
|
891
|
+
/**
|
|
892
|
+
* `/undo`: the files the editor changed in the last turn that changed any, put back as they were before it — one it
|
|
893
|
+
* created is deleted — and the model told so with its next turn. Another walks back one turn more. The shell's changes
|
|
894
|
+
* are not seen, so they are not undone.
|
|
895
|
+
*/
|
|
896
|
+
function undo() {
|
|
897
|
+
const done = undoLast(undoDir());
|
|
898
|
+
if (!done) return 'nothing to undo: the editor has changed no file in this session — changes made through bash are not tracked';
|
|
899
|
+
const { cwd: at } = workspace();
|
|
900
|
+
const names = (list) => {
|
|
901
|
+
const shown = list.map((path) => pathFrom(path, at));
|
|
902
|
+
return shown.length === 1 ? shown[0] : `${shown.slice(0, -1).join(', ')} and ${shown.at(-1)}`;
|
|
903
|
+
};
|
|
904
|
+
const what = [done.restored.length > 0 && `restored ${names(done.restored)}`, done.deleted.length > 0 && `deleted ${names(done.deleted)}, which that turn created`].filter(Boolean).join('; ');
|
|
905
|
+
note = [note, `the user ran /undo: ${what}. View a file again before you edit it.`].filter(Boolean).join('\n');
|
|
906
|
+
return `undid the last turn that changed files: ${what} — changes made through bash are not tracked, so they stay`;
|
|
907
|
+
}
|
|
908
|
+
|
|
751
909
|
/** `! <command>`: the user runs it themselves, where the model would be refused; the model sees what it printed. */
|
|
752
910
|
async function shell(command) {
|
|
753
911
|
stopper = new AbortController();
|
|
@@ -769,7 +927,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
769
927
|
if (!m) return { text: line };
|
|
770
928
|
const [, name, args = ''] = m;
|
|
771
929
|
if (name === 'exit' || name === 'quit') return { control: 'quit' };
|
|
772
|
-
if (name === 'new' || name === 'memory') return { control: name };
|
|
930
|
+
if (name === 'new' || name === 'memory' || name === 'undo') return { control: name };
|
|
773
931
|
if (name === 'model' || name === 'resume' || name === 'mcp' || name === 'copy' || name === 'help' || name === 'stop') return { control: name, args: args.trim() };
|
|
774
932
|
const make = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined;
|
|
775
933
|
return make ? { ...make(args.trim()), said: args.trim() } : { error: `no such command /${name} — one of: ${commands.map((c) => `/${c.name}`).join(' ')}` };
|
|
@@ -801,6 +959,20 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
801
959
|
/** What this session's requests have cost so far, the jobs it delegated included: the ledger's own sum. */
|
|
802
960
|
const spent = () => (sessionId ? db.prepare('SELECT COALESCE(SUM(cost_usd), 0) AS cost FROM model_runs WHERE session_id = ?').get(sessionId).cost : 0);
|
|
803
961
|
|
|
962
|
+
/**
|
|
963
|
+
* The session's cost said to the user the first time it passes `chat.budget` — on the screen, where a retry is said,
|
|
964
|
+
* and never to the model: what to spend is the user's to weigh. Asked after every reply, a delegated job's included.
|
|
965
|
+
*/
|
|
966
|
+
function overBudget() {
|
|
967
|
+
const budget = Number(configured(db, 'chat.budget'));
|
|
968
|
+
if (costSaid || !(budget > 0)) return;
|
|
969
|
+
const cost = spent();
|
|
970
|
+
if (cost < budget) return;
|
|
971
|
+
costSaid = true;
|
|
972
|
+
activity(budgetLine(cost));
|
|
973
|
+
watch({ type: 'budget', text: budgetLine(cost) });
|
|
974
|
+
}
|
|
975
|
+
|
|
804
976
|
function end() {
|
|
805
977
|
event('session-end', {});
|
|
806
978
|
if (lock) rmSync(lock, { force: true });
|
|
@@ -820,11 +992,12 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
|
|
|
820
992
|
/** The jobs at work in the background, by id. */
|
|
821
993
|
running: () => [...stops.keys()],
|
|
822
994
|
/** How many background jobs have reported and wait for a turn to read them. */
|
|
823
|
-
waiting
|
|
995
|
+
waiting,
|
|
824
996
|
where,
|
|
825
997
|
spent,
|
|
826
998
|
tell,
|
|
827
999
|
shell,
|
|
1000
|
+
undo,
|
|
828
1001
|
expand,
|
|
829
1002
|
memoryPage,
|
|
830
1003
|
mcp,
|
|
@@ -868,6 +1041,8 @@ async function screen(db, { model, effort, resume }) {
|
|
|
868
1041
|
watch: (e) => events.emit(e.type, e),
|
|
869
1042
|
// A call waiting on the user's word goes to the screen, which answers it with a key.
|
|
870
1043
|
approve: (ask) => new Promise((resolve) => events.emit('approve', { ...ask, resolve })),
|
|
1044
|
+
// So does a question of the model's: its options are shown, and a pick or what is typed in the box answers it.
|
|
1045
|
+
ask: (question) => new Promise((resolve) => events.emit('ask', { ...question, resolve })),
|
|
871
1046
|
});
|
|
872
1047
|
try {
|
|
873
1048
|
await runUi({ session, events, resume, notify: configured(db, 'chat.notify') !== 'off' }).waitUntilExit();
|
|
@@ -905,7 +1080,7 @@ export async function chat(db, { model, effort, resume = null, send, input = pro
|
|
|
905
1080
|
reply.flush();
|
|
906
1081
|
write(`\n${s.dim(` › ${line}`)}\n`);
|
|
907
1082
|
},
|
|
908
|
-
// Piped, nobody answers a question: an MCP call not on the allow list is refused, and a server that failed is said.
|
|
1083
|
+
// Piped, nobody answers a question: one of the model's is not asked, an MCP call not on the allow list is refused, and a server that failed is said.
|
|
909
1084
|
watch: (e) => {
|
|
910
1085
|
if (e.type === 'retry' && e.job === undefined) return write(`${s.dim(` › ${retryLine(e)}`)}\n`);
|
|
911
1086
|
if (e.type === 'sources') return write(`\n${e.sources.map((page) => s.dim(` ⎿ ${safeForTerminal(`${page.title} — ${page.url}`).replace(/\s+/g, ' ')}`)).join('\n')}\n`);
|
|
@@ -963,8 +1138,8 @@ export async function chat(db, { model, effort, resume = null, send, input = pro
|
|
|
963
1138
|
show('', resumed);
|
|
964
1139
|
continue;
|
|
965
1140
|
}
|
|
966
|
-
if (control === 'model' || control === 'memory' || control === 'mcp') {
|
|
967
|
-
result(control === 'model' ? session.route(args) : control === 'mcp' ? (await session.mcp()).join('\n ') : await session.memoryPage());
|
|
1141
|
+
if (control === 'model' || control === 'memory' || control === 'mcp' || control === 'undo') {
|
|
1142
|
+
result(control === 'model' ? session.route(args) : control === 'undo' ? session.undo() : control === 'mcp' ? (await session.mcp()).join('\n ') : await session.memoryPage());
|
|
968
1143
|
continue;
|
|
969
1144
|
}
|
|
970
1145
|
write('\n');
|