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/src/chat.mjs CHANGED
@@ -8,24 +8,26 @@ 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';
17
17
  import { paths, REPO_ROOT } from './paths.mjs';
18
18
  import { UsageError } from './memory.mjs';
19
19
  import { openInBrowser, servePage } from './page.mjs';
20
- import { getProject, scratchDir } from './projects.mjs';
20
+ import { getProject, projectAt, scratchDir } from './projects.mjs';
21
21
  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 { colourEnabled, createRenderer, header, prompt, renderBlock, safeForTerminal, styles, tilde, widthOf } from './tty.mjs';
28
+ import { estimateTokens, head } from './text.mjs';
29
+ import { colourEnabled, createRenderer, header, pathFrom, prompt, renderBlock, safeForTerminal, styles, tilde, toolView, 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
  /**
@@ -74,10 +76,14 @@ export function commandList(db, { effortNow = () => null, saved = () => [], runn
74
76
  { name: 'memory', hint: 'see the memory in the browser: say yes or no, edit, forget' },
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
- { 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'] : []) },
79
+ { name: 'copy', hint: 'the last reply to the clipboard, as it was written; `code` for its last code block; a number for one further back', 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}`) : []) },
82
+ { name: 'context', hint: 'what this session has used: how full the context is, the tokens and the cost, by model' },
83
+ { name: 'export', hint: 'the conversation as a Markdown file: to a path, or into the home\'s exports folder' },
84
+ { name: 'thinking', hint: 'the model\'s thinking under the working line: on or off', choices: (given) => (given.length === 0 ? ['on', 'off'] : []) },
79
85
  { name: 'help', hint: 'the commands and the keys' },
80
- { name: 'new', hint: 'start a fresh session' },
86
+ { name: 'new', hint: 'start a fresh session (/clear does too)' },
81
87
  { name: 'exit', hint: 'leave' },
82
88
  ];
83
89
  }
@@ -99,6 +105,31 @@ export function injection(text, model, messages = []) {
99
105
  }
100
106
 
101
107
  const asText = (text) => ({ role: 'user', content: [{ type: 'text', text: `<sumo>\n${text}\n</sumo>` }] });
108
+ /** Something that happened — a background job's report — as a block of a user turn: labelled as an event, never wrapped as operator text. */
109
+ const eventBlock = (text) => ({ type: 'text', text: `<event>\n${text}\n</event>` });
110
+
111
+ /** Automated turns in a row — a report read, which delegates, which reports — before the reports wait for the user's next message instead. */
112
+ const MAX_WOKEN = 3;
113
+ /** Jobs at work here at once: each report can start a turn, and each turn can delegate again. */
114
+ const MAX_JOBS = 4;
115
+ /** 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. */
116
+ const REPORT_TOKENS = 1500;
117
+ /** The sections code adds at the end of a report: the checks' verdict, and where a worktree's change went. */
118
+ const ADDED_BY_CODE = /\n## (?:Verified by code|Accepted without verification|Worktree|Attempts)\b/;
119
+
120
+ /**
121
+ * 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
122
+ * author wrote, what code added after it whole — the verdict, whether the change was brought back — and a last line
123
+ * that says where the whole report is.
124
+ */
125
+ function reportForModel(report, id) {
126
+ if (estimateTokens(report) <= REPORT_TOKENS) return report;
127
+ const at = report.search(ADDED_BY_CODE);
128
+ const added = at === -1 ? '' : report.slice(at);
129
+ const last = `The report is cut to fit; the whole of it: sumo job show ${id}`;
130
+ const room = Math.max(0, REPORT_TOKENS * 4 - added.length - last.length - 40);
131
+ return `${head(at === -1 ? report : report.slice(0, at), room).trimEnd()}\n[… cut]${added}\n${last}`;
132
+ }
102
133
 
103
134
  /** One line per tool call, so the user can watch the work: the command, or the edit and its file. */
104
135
  export function describeCall(call) {
@@ -170,14 +201,23 @@ export function parseRoute(args, current, db) {
170
201
  * draws a reply, set in under its line. `clean` is done to the words before they are drawn: a wrapped line must not
171
202
  * break a secret apart where the redaction after it would no longer see it whole.
172
203
  */
173
- export function jobPrinter(write, s, { width = 98, clean = (text) => text } = {}) {
204
+ export function jobPrinter(write, s, { width = 98, clean = (text) => text, cwd = null } = {}) {
205
+ // Each call as the chat names it — Bash(npm test), Read(src/cart.js) — a file from the project the job works in.
206
+ const at = () => (typeof cwd === 'function' ? cwd() : cwd);
174
207
  return {
175
208
  onStart: (job) => write(`⏺ ${s.bold(`j${job.id}`)} ${job.agent} · ${routeOf(job)} — ${job.title}\n`),
176
- onTool: (call) => write(` ⏺ ${describeCall(call)}\n`),
209
+ onTool: (call) => {
210
+ const view = toolView(call, null, { cwd: at() });
211
+ // One line, and where more was cut away, the ellipsis that says so.
212
+ const [first, ...rest] = clean(view.detail).split('\n');
213
+ const line = first.length > 160 ? `${first.slice(0, 159)}…` : rest.length > 0 && !first.endsWith('…') ? `${first} …` : first;
214
+ write(` ⏺ ${s.bold(view.title)}(${line})\n`);
215
+ },
177
216
  onRetry: (r) => write(` ${s.dim(retryLine(r))}\n`),
178
217
  onCompact: (c) => write(` ${s.dim(compactLine(c))}\n`),
179
218
  onResult: (call, result) => {
180
- const first = String(result.content ?? '').split('\n').find((l) => l.trim()) ?? '';
219
+ // The line that says how it went, as the chat's screen would show it: what a command printed first, a read's size, an edit's first change.
220
+ const first = toolView(call, { content: String(result.content ?? ''), isError: Boolean(result.isError), edit: result.edit }, { cwd: at() }).lines[0]?.text ?? '';
181
221
  write(` ⎿ ${s.dim(clean(first).slice(0, 160))}\n`);
182
222
  },
183
223
  onTurn: (response) => {
@@ -196,6 +236,28 @@ export function jobPrinter(write, s, { width = 98, clean = (text) => text } = {}
196
236
  export const retryLine = ({ attempt, limit, delayMs, reason }) => `retrying in ${Math.max(1, Math.round(delayMs / 1000))}s (${attempt} of ${limit}) — ${reason}`;
197
237
  /** A conversation summarized to make room, in one line: how big it had grown. */
198
238
  export const compactLine = ({ before }) => `the conversation was summarized at ${Math.round(before / 1000)}k tokens to make room, and goes on from the summary`;
239
+ const kilo = (n) => (n >= 1000 ? `${(n / 1000).toFixed(n >= 10_000 ? 0 : 1)}k` : String(n));
240
+
241
+ /**
242
+ * `/context`: how full the context is — against the size where it is time to start afresh, with the word for its band — and
243
+ * what the session has used, by who asked and on which model. Markdown, drawn as a reply is.
244
+ */
245
+ export function contextReport({ contextTokens = 0, rows = [], turns = 0, calls = 0 }) {
246
+ const full = Math.min(1, contextTokens / CONTEXT_FULL);
247
+ const bar = `${'█'.repeat(Math.round(full * 20))}${'░'.repeat(20 - Math.round(full * 20))}`;
248
+ const band = contextTokens >= CONTEXT_FULL ? 'very large — /new now; the memory brings the thread back' : contextTokens >= CONTEXT_LARGE ? 'large — quality drops from here; finish the piece in hand, then /new' : 'room to spare';
249
+ const cost = rows.reduce((sum, row) => sum + row.cost, 0);
250
+ const lines = [`**Context** ${kilo(contextTokens)} tokens · \`${bar}\` ${Math.round(full * 100)}% of ${kilo(CONTEXT_FULL)} · ${band}`, '', `**This session** ${turns} turn${turns === 1 ? '' : 's'} · ${calls} tool call${calls === 1 ? '' : 's'} · $${cost.toFixed(2)}`];
251
+ if (rows.length === 0) return [...lines, '', 'No request has been made yet.'].join('\n');
252
+ return [...lines, '', '| Who | Model | Requests | In | Cached | Out | Cost |', '|---|---|--:|--:|--:|--:|--:|', ...rows.map((row) => `| ${row.kind} | ${row.model} | ${row.requests} | ${kilo(row.input)} | ${kilo(row.cached)} | ${kilo(row.output)} | $${row.cost.toFixed(2)} |`)].join('\n');
253
+ }
254
+
255
+ /** A session past its budget, in one line: what it has cost, how to start afresh, and how to move the line. */
256
+ 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`;
257
+
258
+ /** Where the context is large enough that quality drops, and where it is time for /new: the bands the session is told about. */
259
+ const CONTEXT_LARGE = 60_000;
260
+ const CONTEXT_FULL = 150_000;
199
261
 
200
262
  /** The session log, one JSON line per message, the shape the scribe reads assistant replies from and the gauge reads usage from. */
201
263
  function logLine(file, entry) {
@@ -206,6 +268,14 @@ function configured(db, key) {
206
268
  return getMeta(db, `config.${key}`) ?? CONFIG_DEFAULTS[key];
207
269
  }
208
270
 
271
+ /** The chat's own tools, in a fixed order so its cached prefix never moves; the MCP servers' come after them. */
272
+ export const CHAT_TOOLS = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ASK_TOOL, ...WEB_TOOLS];
273
+ /**
274
+ * The chat's system prompt: AGENTS.md. The guides it names are read from wherever the chat was started: their paths are
275
+ * made absolute, or the model goes looking for them.
276
+ */
277
+ export const chatSystem = () => readFileSync(join(REPO_ROOT, 'AGENTS.md'), 'utf8').trim().replaceAll(/(?<![\w/])guides\//g, `${join(REPO_ROOT, 'guides')}/`);
278
+
209
279
  /**
210
280
  * One chat session's state and policy, apart from the terminal, so it can be
211
281
  * driven by tests with canned responses. `say(text)` is one user turn;
@@ -219,13 +289,16 @@ function configured(db, key) {
219
289
  * for each turn's model when the model is `auto`. `approve` puts an MCP call
220
290
  * that is not on the allow list to the user — the chat's own, or a job's run
221
291
  * here — and answers `once`, `always` or `no`; without it such a call is refused.
292
+ * `ask` puts a question of the model's to the user, with its options, and
293
+ * answers `{ label }` for the one picked or `{ text }` for words of their own —
294
+ * anything else is no answer; without it the model is told nobody is there.
222
295
  * With `background`, a delegated job does not hold the turn: `delegate` answers
223
296
  * at once, the job runs beside the conversation with a stop of its own, and its
224
297
  * report reaches the model later as an automated event — with its next request
225
298
  * if it is at work, else in a turn of its own (`wake`), which whoever watches
226
299
  * starts when told `job-report`.
227
300
  */
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() } = {}) {
301
+ 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
302
  // A route the chat was started with holds through a resumed session; one it was not, the saved session brings back.
230
303
  const given = { model: model !== undefined, effort: effort !== undefined };
231
304
  model ??= configured(db, 'chat.model');
@@ -238,11 +311,12 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
238
311
  if (modelId(model) === MODEL_IDS.haiku) effort = 'none';
239
312
  /** On auto, the route the last turn ran on; null otherwise. */
240
313
  let routed = null;
241
- // The guides it names are read from wherever the chat was started: their paths are made absolute, or the model goes looking for them.
242
- const system = readFileSync(join(REPO_ROOT, 'AGENTS.md'), 'utf8').trim().replaceAll(/(?<![\w/])guides\//g, `${join(REPO_ROOT, 'guides')}/`);
243
- const own = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ...WEB_TOOLS];
314
+ const system = chatSystem();
315
+ const own = CHAT_TOOLS;
244
316
  /** The MCP servers of this process, once connected: their tools join the request, deferred, and their names the system prompt. */
245
317
  let registry = null;
318
+ /** Whether the servers' tools have joined the requests: once, at the first turn after they connected. */
319
+ let joined = false;
246
320
  const connecting = mcpReady().then((r) => {
247
321
  registry = r;
248
322
  });
@@ -258,8 +332,12 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
258
332
  let lock = null;
259
333
  /** Whether there is anything to save yet: a session nobody has spoken to is not kept. */
260
334
  let spoken = false;
261
- /** Said to the model with the next turn: that the session was picked up again. */
335
+ /** Said to the model with the next turn: that the session was picked up again, or that /undo put files back. */
262
336
  let note = null;
337
+ /** 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. */
338
+ let seen = new Map();
339
+ /** Whether the screen has said what this session cost: once a session, when it first passes `chat.budget`. */
340
+ let costSaid = false;
263
341
  /** The jobs running here, each with what the user has said to it that it has not read yet. */
264
342
  const jobs = new Map();
265
343
  /** The projects a worker is running in: two in one working tree overwrite each other. */
@@ -274,6 +352,8 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
274
352
  const stoppedByUser = new Set();
275
353
  /** What background jobs reported and the model has not read yet: `{ id, text, quiet }`. */
276
354
  const reports = [];
355
+ /** Turns nobody typed since the user last did: past MAX_WOKEN, what the jobs report waits for the user. */
356
+ let woken = 0;
277
357
  /** The memory page, once /memory has asked for it: the promise of its address. */
278
358
  let page = null;
279
359
  const ledger = { kind: 'chat', sessionId: null };
@@ -307,7 +387,9 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
307
387
  lock = held ?? takeLock(lockFile(sessionId), busy(sessionId));
308
388
  ledger.sessionId = sessionId;
309
389
  lastContext = saved?.contextTokens ?? 0;
390
+ costSaid = false;
310
391
  project = null;
392
+ seen = new Map();
311
393
  spoken = Boolean(saved);
312
394
  transcript = join(paths().logs, 'sessions', `${sessionId}.jsonl`);
313
395
  const block = handleEvent(db, 'session-start', { session_id: sessionId, cwd, transcript_path: transcript, source }, now());
@@ -318,6 +400,8 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
318
400
  if (modelId(model) === MODEL_IDS.haiku) effort = 'none';
319
401
  }
320
402
  params = paramsFor({ model: model === 'auto' ? (routed?.model ?? CONFIG_DEFAULTS['chat.model']) : model, effort: model === 'auto' ? routed?.effort : effort, tools: toolsNow(), system: systemNow(), text: saved ? undefined : block });
403
+ // What jobs reported that this session had not read comes back with it; a fresh session has read nothing, and is owed nothing.
404
+ reports.splice(0, reports.length, ...(saved?.pending ?? []));
321
405
  if (saved) {
322
406
  params.messages = withoutThinking(saved.messages);
323
407
  note = `resumed: this session was saved ${ago(saved.savedAt, now())} and picked up again now, in ${cwd}. Carry on where it stopped; memory may have changed since this session's first block — search it before assuming.`;
@@ -342,11 +426,20 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
342
426
  return `resumed ${sessionLine(found, now())} — the first reply pays one uncached turn`;
343
427
  }
344
428
 
429
+ /** The session kept where nothing can say a failure: a save that fails here is tried again after the next turn. */
430
+ function keepQuietly() {
431
+ try {
432
+ persist();
433
+ } catch {
434
+ // Said, if it still fails, by the save after the next turn.
435
+ }
436
+ }
437
+
345
438
  /** The session as it stands, kept after every turn. A save that fails is said as the turn's error: the turn itself went fine. */
346
439
  function persist() {
347
440
  if (!spoken) return;
348
441
  try {
349
- saveState({ id: sessionId, messages: params.messages, model, effort, routed, cwd, contextTokens: lastContext, now: now() });
442
+ saveState({ id: sessionId, messages: params.messages, model, effort, routed, cwd, contextTokens: lastContext, now: now(), pending: reports.map(({ id, quiet, text }) => ({ id, quiet, text })) });
350
443
  } catch (cause) {
351
444
  throw new UsageError(`the session could not be saved: ${cause.message}`);
352
445
  }
@@ -380,19 +473,55 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
380
473
 
381
474
  const event = (name, payload) => handleEvent(db, name, { session_id: sessionId, cwd, transcript_path: transcript, ...payload }, now());
382
475
 
383
- /** 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). */
476
+ /** Where this session's turns keep what the editor changed, for /undo. */
477
+ const undoDir = () => join(paths().logs, 'undo', sessionId);
478
+
479
+ /**
480
+ * The project the work is in: the one whose card came in last; before any has, the one the chat was started in; else none
481
+ * (tools then run in scratch).
482
+ */
384
483
  function workspace() {
385
- const slug = currentProject(db, sessionId);
484
+ const slug = currentProject(db, sessionId) ?? projectAt(db, cwd);
386
485
  if (slug && project?.slug !== slug) project = getProject(db, slug);
387
- return project ? contextFor(project) : contextFor({ path: scratchDir() });
486
+ return contextFor(project ?? { path: scratchDir() }, { sandbox: sandboxOn(db), network: networkOn(db) });
487
+ }
488
+
489
+ /**
490
+ * A question for the user. Memory is read first, and holds it back once if it may hold the answer. Then it waits on
491
+ * the user: a pick is kept as their decision, with the question; what they typed instead is their words, kept as a turn is.
492
+ */
493
+ async function askUser(input) {
494
+ const line = (v) => safeForTerminal(String(v ?? '')).replace(/\s+/g, ' ').trim();
495
+ const question = safeForTerminal(String(input.question ?? '')).trim();
496
+ const options = (Array.isArray(input.options) ? input.options : []).map((o) => ({ label: line(o?.label ?? o), ...(line(o?.detail) ? { detail: line(o.detail) } : {}) }));
497
+ 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 };
498
+ const gate = event('pre-tool', { tool_name: ASK_TOOL.name, tool_input: { question } });
499
+ if (gate) return { content: JSON.parse(gate).deny, isError: true };
500
+ 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 };
501
+ const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve('no'), { once: true }));
502
+ const answer = await Promise.race([ask({ question, options, signal: stopper.signal }), stopped]);
503
+ const picked = options.find((o) => o.label === answer?.label);
504
+ if (picked) {
505
+ // A pick is the user's decision in the model's wording: kept beside what they typed, with the question it answers.
506
+ recordTurn(db, { sessionId, text: `${question} → ${picked.label}`, now: now() });
507
+ return { content: `The user chose: ${picked.label}` };
508
+ }
509
+ const text = typeof answer?.text === 'string' ? answer.text.trim() : '';
510
+ 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 };
511
+ const brought = event('prompt', { prompt: text, context_tokens: lastContext });
512
+ noteGuides(brought);
513
+ logLine(transcript, { type: 'user', message: { role: 'user', content: redact(text).text } });
514
+ return { content: `The user answered: ${text}${brought ? `\n<sumo>\n${brought}\n</sumo>` : ''}` };
388
515
  }
389
516
 
390
- /** The workflow gate, the cheap-model passes and `delegate`; anything else is the tool's own business. */
517
+ /** The workflow gate, the cheap-model passes, `delegate` and `ask`; anything else is the tool's own business. */
391
518
  async function beforeTool(call) {
392
519
  if (call.name === DELEGATE_TOOL.name) return delegate(call.input ?? {}, call.id);
520
+ if (call.name === ASK_TOOL.name) return askUser(call.input ?? {});
393
521
  if (isMcpTool(call.name)) return gateMcp(call, { approve, signal: stopper.signal });
394
522
  if (call.name !== BASH_TOOL.name) return null;
395
523
  const command = String(call.input?.command ?? '');
524
+ if (tampersMarker(command)) return { content: TAMPER_REFUSED, isError: true };
396
525
  const gate = event('pre-tool', { tool_name: 'bash', tool_input: { command } });
397
526
  if (gate) return { content: JSON.parse(gate).deny, isError: true };
398
527
  const pass = PASS_RUN.exec(command);
@@ -408,15 +537,19 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
408
537
  /**
409
538
  * `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
539
  * 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.
540
+ * one reply run side by side, but a worker has its project's working tree to itself — in a git project, a second one
541
+ * is given a worktree of its own. Several `attempts` run side by side too, and are answered for once, together.
412
542
  * `callId` is the delegate call's own, so a screen can show the job under the call that started it.
413
543
  */
414
544
  async function delegate(input, callId = null) {
545
+ const asked = (input.job === undefined || input.job === null) && [2, 3].includes(input.attempts) ? input.attempts : 1;
546
+ // Counted where a run begins, before anything is awaited: five calls in one reply are judged one after another, not all at once.
547
+ 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
548
  const own = background ? new AbortController() : null;
416
549
  const signal = own?.signal ?? stopper.signal;
417
550
  let claimed = null;
418
- const claim = (agent, slug) => {
419
- if (agent !== 'worker') return;
551
+ const claim = (agent, slug, shared) => {
552
+ if (agent !== 'worker' || !shared) return;
420
553
  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
554
  workers.add(slug);
422
555
  claimed = slug;
@@ -425,19 +558,23 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
425
558
  if (claimed) workers.delete(claimed);
426
559
  };
427
560
  let id;
561
+ /** The other attempts made beside the first, when several were asked for. */
562
+ let siblings = [];
428
563
  let warnings = [];
429
564
  try {
430
565
  if (input.job !== undefined && input.job !== null) {
431
566
  const job = getJob(db, parseJobId(input.job));
432
- claim(job.agent, job.project);
567
+ claim(job.agent, job.project, treeOf(db, job.id) === null);
433
568
  id = job.id;
434
569
  } else {
435
570
  const agent = input.agent ?? 'worker';
436
571
  const project = getProject(db, input.project ?? '');
437
572
  if (!project) throw new UsageError(`no project "${input.project ?? ''}" — the delegate call names one by its slug`);
438
- claim(agent, project.slug);
573
+ claim(agent, project.slug, !hasCommit(project.path));
439
574
  activity(`briefing a ${agent}…`);
440
- ({ job: { id }, warnings } = 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), now: now() }));
575
+ 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() });
576
+ ({ job: { id }, warnings } = made);
577
+ siblings = (made.jobs ?? []).slice(1).map((job) => job.id);
441
578
  }
442
579
  } catch (cause) {
443
580
  release();
@@ -445,35 +582,74 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
445
582
  }
446
583
  let begun;
447
584
  const started = new Promise((resolve) => (begun = resolve));
448
- const work = runHere(id, warnings, { signal, callId, begun }).finally(release);
585
+ const ids = [id, ...siblings];
586
+ // A job made to follow this one — its retry, or its review — is at work under the same call, and stopped with it.
587
+ const follow = (next) => {
588
+ ids.push(next);
589
+ if (own) stops.set(next, own);
590
+ };
591
+ const work = (siblings.length === 0 ? runFollowedHere(id, warnings, { signal, callId, begun }, follow) : runAttempts(ids, warnings, { signal, callId, begun })).finally(release);
449
592
  if (!own) return work;
450
593
 
451
594
  // In the background. A job that cannot start says so as the call's own answer; one that has started answers later.
452
- stops.set(id, own);
595
+ // Attempts share one stop: stopping one stops them all.
596
+ for (const each of ids) stops.set(each, own);
453
597
  const first = await Promise.race([started, work.then(() => null, (cause) => ({ cause }))]);
454
598
  if (first?.cause) {
455
- stops.delete(id);
599
+ for (const each of ids) stops.delete(each);
456
600
  throw first.cause;
457
601
  }
458
602
  const run = work
459
603
  .then((result) => result, (cause) => ({ content: `j${id} stopped on an error: ${cause.message}`, isError: true }))
460
604
  .then((result) => {
461
- stops.delete(id);
605
+ for (const each of ids) stops.delete(each);
462
606
  runs.delete(run);
463
- const quiet = stoppedByUser.delete(id);
464
- reports.push({ id, quiet, text: `An automated event, not a message from the user: j${id} has reported.\n${result.content}` });
607
+ const quiet = ids.filter((each) => stoppedByUser.delete(each)).length > 0;
608
+ 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}` });
609
+ // Kept with the session as soon as it comes: a chat closed before the next message still has it when picked up again.
610
+ keepQuietly();
465
611
  watch({ type: 'job-report', job: id, result, quiet });
466
612
  });
467
613
  runs.add(run);
468
614
  const job = getJob(db, id);
615
+ const beside = siblings.length === 0 ? '' : `, with ${siblings.map((each) => `j${each}`).join(' and ')} beside it: attempts at the same task, reported on together`;
469
616
  return {
470
617
  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 ` +
618
+ `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
619
  '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
620
  '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
621
  };
475
622
  }
476
623
 
624
+ /** Attempts at one task, run side by side and answered for once, when all have ended: what was kept first, then how each went. */
625
+ async function runAttempts(ids, warnings, options) {
626
+ const ended = await Promise.allSettled(ids.map((each) => runHere(each, [], options)));
627
+ const kept = attemptsSettled(db, ids[0]) ?? ['Not every attempt closed, so none was picked or brought back yet.'];
628
+ const each = ended.map((e, i) => (e.status === 'fulfilled' ? e.value.content : `j${ids[i]} stopped on an error: ${e.reason?.message}`));
629
+ return { content: capRedacted([[...warnings, `Attempts ${ids.map((j) => `j${j}`).join(', ')}:`, ...kept].join('\n'), ...each].join('\n\n')) };
630
+ }
631
+
632
+ /**
633
+ * One job run here, and what follows it unasked — a retry one step up of a first attempt that failed, a review of
634
+ * risky work done (`runFollowed`, src/jobs.mjs) — answered for once. A retry stands in for the attempt that failed,
635
+ * after the line that says so; a review comes after the work it judged. `onFollow` hears each job made, before it runs.
636
+ */
637
+ async function runFollowedHere(id, warnings, options, onFollow) {
638
+ const ran = await runFollowed(db, id, (each, said = warnings) => runHere(each, said, options), { signal: options.signal, onFollow, now });
639
+ if (ran.length === 1) return ran[0].result;
640
+ let parts = [];
641
+ let shown = [];
642
+ let job = null;
643
+ for (const step of ran) {
644
+ // The attempt that failed is one line now; its report is a command away.
645
+ if (step.kind === 'retry') [parts, shown, job] = [[], [], null];
646
+ parts.push([step.line, step.result?.content ?? (step.error ? `j${step.id} stopped on an error: ${step.error}` : '')].filter(Boolean).join('\n'));
647
+ shown.push([step.line, step.result?.job?.report].filter(Boolean).join('\n'));
648
+ job ??= step.result?.job ?? null;
649
+ }
650
+ return { content: parts.join('\n\n'), job: job && { ...job, report: shown.join('\n\n') } };
651
+ }
652
+
477
653
  /** 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
654
  async function runHere(id, warnings, { signal, callId, begun }) {
479
655
  const inbox = [];
@@ -482,7 +658,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
482
658
  // Its work is written down as it happens, for `sumo job watch` — in a Herdr tab of its own, or any terminal —
483
659
  // and without its secrets, through the same redaction as what the model sees.
484
660
  const record = (t) => appendLive(id, redact(t).text);
485
- const live = jobPrinter(record, styles(false), { clean: (t) => redact(t).text });
661
+ const live = jobPrinter(record, styles(false), { clean: (t) => redact(t).text, cwd: () => getProject(db, getJob(db, id)?.project ?? '')?.path ?? null });
486
662
  let ended = [];
487
663
  try {
488
664
  const outcome = await runJob(db, id, {
@@ -505,7 +681,10 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
505
681
  live.onResult(call, result);
506
682
  watch({ type: 'result', call, result, job: id });
507
683
  },
508
- onTurn: live.onTurn,
684
+ onTurn: (response, totals) => {
685
+ live.onTurn(response, totals);
686
+ overBudget();
687
+ },
509
688
  onThinking: (_delta, snapshot) => watch({ type: 'thinking', job: id, text: snapshot }),
510
689
  onRetry: (r) => {
511
690
  live.onRetry(r);
@@ -518,10 +697,10 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
518
697
  });
519
698
  ended = runLines(outcome);
520
699
  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.
700
+ // 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
701
  const { job, turns, totals } = outcome;
523
702
  return {
524
- content: capRedacted([...warnings, ...ended, ...(report ? ['', report] : [])].join('\n')),
703
+ content: capRedacted([...warnings, ...ended, ...(report ? ['', reportForModel(report, id)] : [])].join('\n')),
525
704
  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
705
  };
527
706
  } catch (cause) {
@@ -554,10 +733,14 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
554
733
  * Null when nothing is waiting — a job the user stopped waits for the next message instead.
555
734
  */
556
735
  async function wake() {
557
- if (!reports.some((r) => !r.quiet)) return null;
736
+ if (waiting() === 0) return null;
737
+ woken++;
558
738
  return say(reports.splice(0).map((r) => r.text).join('\n\n'), '', [], { automated: true });
559
739
  }
560
740
 
741
+ /** 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. */
742
+ const waiting = () => (woken >= MAX_WOKEN ? 0 : reports.filter((r) => !r.quiet).length);
743
+
561
744
  /** 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
745
  function watchTab(job) {
563
746
  try {
@@ -650,12 +833,14 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
650
833
  const checks = declared();
651
834
  if (checks.length === 0 || checks.some((c) => String(call.input?.command ?? '').includes(c))) unchecked.clear();
652
835
  };
653
- // The servers connect while the first turn is typed; the first request waits for them, and Esc does not.
654
- if (!registry) {
836
+ // The servers connect while the first turn is typed; the first request waits for them, and Esc does not. Their tools
837
+ // join the request then — even when something else, /mcp, already waited for them to connect.
838
+ if (!joined) {
655
839
  const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve('stopped'), { once: true }));
656
840
  if ((await Promise.race([connecting, stopped])) === 'stopped') return { stop: 'interrupted', error: null, text: '' };
657
841
  params.tools = toolsNow();
658
842
  params.system[0].text = systemNow();
843
+ joined = true;
659
844
  if (registry.servers().length > 0 || registry.error) watch({ type: 'mcp', servers: registry.servers(), error: registry.error });
660
845
  }
661
846
  // On auto the router names this turn's model first; a router that cannot answer refuses the turn, and nothing of it reaches the conversation.
@@ -680,23 +865,29 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
680
865
  watch({ type: 'route', ...routed });
681
866
  }
682
867
  settleTail();
683
- // A resumed session is told so with its first turn, and what background jobs reported while nobody was looking rides in with a message
684
- // too. An automated turn is that report itself: nothing in it is the user's, so nothing of it is kept as theirs.
685
- const injected = [note, ...(automated ? [] : [...reports.splice(0).map((r) => r.text), event('prompt', { prompt: said, context_tokens: lastContext, guide })])].filter(Boolean).join('\n');
868
+ // 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.
869
+ const injected = [note, ...(automated ? [] : [event('prompt', { prompt: said, context_tokens: lastContext, guide })])].filter(Boolean).join('\n');
686
870
  noteGuides(injected);
687
871
  note = null;
872
+ if (!automated) woken = 0;
688
873
  const pictures = images.flatMap(({ label, mediaType, data }) => [{ type: 'text', text: label }, { type: 'image', source: { type: 'base64', media_type: mediaType, data } }]);
689
- const at = params.messages.push(automated ? asText(text) : { role: 'user', content: [...pictures, { type: 'text', text }] }) - 1;
874
+ // 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,
875
+ // never operator text — a report repeats what a page or a file said, and that is nobody's instruction.
876
+ const events = reports.splice(0).map((r) => eventBlock(r.text));
877
+ const at = params.messages.push(automated ? { role: 'user', content: [eventBlock(text)] } : { role: 'user', content: [...pictures, { type: 'text', text }, ...events] }) - 1;
690
878
  spoken = true;
691
879
  if (injected) params.messages.push(injection(injected, params.model, params.messages));
692
880
  // The log keeps what memory keeps: the user's words without their secrets — and an event as an event.
693
881
  logLine(transcript, automated ? { type: 'event', text: redact(text).text } : { type: 'user', message: { role: 'user', content: redact(text).text } });
694
882
 
883
+ // What the editor changes in this turn is kept for /undo in one folder, however many requests the turn takes.
884
+ const undoFolder = nextUndoTurn(undoDir());
695
885
  let continued = false;
696
886
  for (;;) {
697
887
  const outcome = await converse(db, params, {
698
888
  send: sendAs,
699
- ctx: workspace(),
889
+ // 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.
890
+ ctx: { ...workspace(), seen, undo: undoFolder, approve },
700
891
  ledger,
701
892
  beforeTool,
702
893
  concurrent: (call) => call.name === DELEGATE_TOOL.name,
@@ -719,6 +910,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
719
910
  lastContext = totals.contextTokens;
720
911
  logLine(transcript, { type: 'assistant', message: { role: 'assistant', model: response.model ?? params.model, usage: response.usage, content: response.content } });
721
912
  watch({ type: 'usage', totals });
913
+ overBudget();
722
914
  // What the reply rests on, said under it: the pages it cites.
723
915
  const sources = sourcesOf(response.content);
724
916
  if (sources.length > 0) watch({ type: 'sources', sources });
@@ -727,7 +919,7 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
727
919
  // 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
920
  // Only a refusal of the request itself (a 4xx); a rate limit, an overload or a dropped connection says nothing against the pictures.
729
921
  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 }];
922
+ 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
923
  // Stopped by the user: the turn still ended, so the scribe is woken, but nothing is asked of memory and nothing continues.
732
924
  if (outcome.stop === 'interrupted') {
733
925
  event('stop', { stop_hook_active: true });
@@ -748,6 +940,24 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
748
940
  }
749
941
  }
750
942
 
943
+ /**
944
+ * `/undo`: the files the editor changed in the last turn that changed any, put back as they were before it — one it
945
+ * created is deleted — and the model told so with its next turn. Another walks back one turn more. The shell's changes
946
+ * are not seen, so they are not undone.
947
+ */
948
+ function undo() {
949
+ const done = undoLast(undoDir());
950
+ if (!done) return 'nothing to undo: the editor has changed no file in this session — changes made through bash are not tracked';
951
+ const { cwd: at } = workspace();
952
+ const names = (list) => {
953
+ const shown = list.map((path) => pathFrom(path, at));
954
+ return shown.length === 1 ? shown[0] : `${shown.slice(0, -1).join(', ')} and ${shown.at(-1)}`;
955
+ };
956
+ 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('; ');
957
+ note = [note, `the user ran /undo: ${what}. View a file again before you edit it.`].filter(Boolean).join('\n');
958
+ return `undid the last turn that changed files: ${what} — changes made through bash are not tracked, so they stay`;
959
+ }
960
+
751
961
  /** `! <command>`: the user runs it themselves, where the model would be refused; the model sees what it printed. */
752
962
  async function shell(command) {
753
963
  stopper = new AbortController();
@@ -769,8 +979,9 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
769
979
  if (!m) return { text: line };
770
980
  const [, name, args = ''] = m;
771
981
  if (name === 'exit' || name === 'quit') return { control: 'quit' };
772
- if (name === 'new' || name === 'memory') return { control: name };
773
- if (name === 'model' || name === 'resume' || name === 'mcp' || name === 'copy' || name === 'help' || name === 'stop') return { control: name, args: args.trim() };
982
+ if (name === 'new' || name === 'clear') return { control: 'new' };
983
+ if (name === 'memory' || name === 'undo' || name === 'context') return { control: name };
984
+ if (name === 'model' || name === 'resume' || name === 'mcp' || name === 'copy' || name === 'help' || name === 'stop' || name === 'export' || name === 'thinking') return { control: name, args: args.trim() };
774
985
  const make = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined;
775
986
  return make ? { ...make(args.trim()), said: args.trim() } : { error: `no such command /${name} — one of: ${commands.map((c) => `/${c.name}`).join(' ')}` };
776
987
  }
@@ -798,10 +1009,38 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
798
1009
  return { cwd: at, name: project?.slug ?? basename(at), branch: branchOf(at) };
799
1010
  }
800
1011
 
1012
+ /** What this session's requests have used so far, the jobs it delegated included: by the kind of conversation and the model, dearest first. */
1013
+ const usage = () =>
1014
+ sessionId
1015
+ ? db
1016
+ .prepare(
1017
+ `SELECT kind, model, COUNT(*) AS requests, COALESCE(SUM(input_tokens), 0) AS input, COALESCE(SUM(output_tokens), 0) AS output,
1018
+ COALESCE(SUM(cache_read_tokens), 0) AS cached, COALESCE(SUM(cost_usd), 0) AS cost
1019
+ FROM model_runs WHERE session_id = ? GROUP BY kind, model ORDER BY cost DESC`,
1020
+ )
1021
+ .all(sessionId)
1022
+ : [];
1023
+
801
1024
  /** What this session's requests have cost so far, the jobs it delegated included: the ledger's own sum. */
802
1025
  const spent = () => (sessionId ? db.prepare('SELECT COALESCE(SUM(cost_usd), 0) AS cost FROM model_runs WHERE session_id = ?').get(sessionId).cost : 0);
803
1026
 
1027
+ /**
1028
+ * The session's cost said to the user the first time it passes `chat.budget` — on the screen, where a retry is said,
1029
+ * and never to the model: what to spend is the user's to weigh. Asked after every reply, a delegated job's included.
1030
+ */
1031
+ function overBudget() {
1032
+ const budget = Number(configured(db, 'chat.budget'));
1033
+ if (costSaid || !(budget > 0)) return;
1034
+ const cost = spent();
1035
+ if (cost < budget) return;
1036
+ costSaid = true;
1037
+ activity(budgetLine(cost));
1038
+ watch({ type: 'budget', text: budgetLine(cost) });
1039
+ }
1040
+
804
1041
  function end() {
1042
+ // Kept as it stands, reports not yet read with it, before the session is let go.
1043
+ if (sessionId) keepQuietly();
805
1044
  event('session-end', {});
806
1045
  if (lock) rmSync(lock, { force: true });
807
1046
  lock = null;
@@ -820,11 +1059,13 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
820
1059
  /** The jobs at work in the background, by id. */
821
1060
  running: () => [...stops.keys()],
822
1061
  /** How many background jobs have reported and wait for a turn to read them. */
823
- waiting: () => reports.filter((r) => !r.quiet).length,
1062
+ waiting,
824
1063
  where,
825
1064
  spent,
1065
+ usage,
826
1066
  tell,
827
1067
  shell,
1068
+ undo,
828
1069
  expand,
829
1070
  memoryPage,
830
1071
  mcp,
@@ -837,6 +1078,10 @@ export function createChat(db, { model, effort, cwd = process.cwd(), send = send
837
1078
  get sessionId() {
838
1079
  return sessionId;
839
1080
  },
1081
+ /** Whether anything has been said in this session: one nobody spoke to is not kept, and there is nothing to pick up again. */
1082
+ get spoken() {
1083
+ return spoken;
1084
+ },
840
1085
  get model() {
841
1086
  return model;
842
1087
  },
@@ -868,9 +1113,17 @@ async function screen(db, { model, effort, resume }) {
868
1113
  watch: (e) => events.emit(e.type, e),
869
1114
  // A call waiting on the user's word goes to the screen, which answers it with a key.
870
1115
  approve: (ask) => new Promise((resolve) => events.emit('approve', { ...ask, resolve })),
1116
+ // So does a question of the model's: its options are shown, and a pick or what is typed in the box answers it.
1117
+ ask: (question) => new Promise((resolve) => events.emit('ask', { ...question, resolve })),
871
1118
  });
872
1119
  try {
873
- await runUi({ session, events, resume, notify: configured(db, 'chat.notify') !== 'off' }).waitUntilExit();
1120
+ await runUi({ session, events, resume, notify: configured(db, 'chat.notify') !== 'off', motion: configured(db, 'chat.motion') !== 'off' }).waitUntilExit();
1121
+ // The way back in, said as the screen closes: the command that picks this session up again, and what it cost.
1122
+ if (session.spoken) {
1123
+ const s = styles(colourEnabled());
1124
+ const cost = session.spent();
1125
+ process.stdout.write(`\n${s.dim(` sumo chat --resume ${shortId(session.sessionId)} picks this session up again${cost >= 0.005 ? ` · it cost $${cost.toFixed(2)}` : ''}`)}\n\n`);
1126
+ }
874
1127
  } finally {
875
1128
  // Jobs still at work in the background are stopped with the chat, and let go of what they hold before it closes.
876
1129
  await session.settle();
@@ -905,7 +1158,7 @@ export async function chat(db, { model, effort, resume = null, send, input = pro
905
1158
  reply.flush();
906
1159
  write(`\n${s.dim(` › ${line}`)}\n`);
907
1160
  },
908
- // Piped, nobody answers a question: an MCP call not on the allow list is refused, and a server that failed is said.
1161
+ // 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
1162
  watch: (e) => {
910
1163
  if (e.type === 'retry' && e.job === undefined) return write(`${s.dim(` › ${retryLine(e)}`)}\n`);
911
1164
  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 +1216,19 @@ export async function chat(db, { model, effort, resume = null, send, input = pro
963
1216
  show('', resumed);
964
1217
  continue;
965
1218
  }
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());
1219
+ if (control === 'model' || control === 'memory' || control === 'mcp' || control === 'undo') {
1220
+ result(control === 'model' ? session.route(args) : control === 'undo' ? session.undo() : control === 'mcp' ? (await session.mcp()).join('\n ') : await session.memoryPage());
1221
+ continue;
1222
+ }
1223
+ if (control === 'context') {
1224
+ reply.write(contextReport({ contextTokens: session.contextTokens, rows: session.usage() }));
1225
+ reply.flush();
1226
+ write('\n');
1227
+ continue;
1228
+ }
1229
+ // The rest belong to the screen — a clipboard, a pager, jobs under the box — and are never sent to the model as words.
1230
+ if (control) {
1231
+ result(`/${control} is for the chat on a terminal screen`);
968
1232
  continue;
969
1233
  }
970
1234
  write('\n');