sumo-agents 0.7.1 → 0.8.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/loop.mjs CHANGED
@@ -2,10 +2,11 @@ import { existsSync, readFileSync, rmSync } from 'node:fs';
2
2
  import { join, resolve } from 'node:path';
3
3
  import { ADVISOR_BETA, ADVISOR_SPENT, ADVISOR_SPENT_AT, ADVISOR_STOP_AT, advisorIn, advisorLine, advisorOf, advisorResult, advisorRuns, advisorTool, isAdvisorCall, isAdvisorResult } from './advisor.mjs';
4
4
  import { anthropicClient, authenticatedRequest, FALLBACK_BETA, loadSdk, resolveAnthropicCredential } from './auth.mjs';
5
- import { MODEL_IDS, modelId, MODELS, usableModels } from './catalog.mjs';
5
+ import { JOB_MODELS, laneOf, MODEL_IDS, modelId, offLine, usableModels } from './catalog.mjs';
6
+ import { converseCursor } from './cursor.mjs';
6
7
  import { getMeta, tx } from './db.mjs';
7
8
  import { handleEvent } from './hooks.mjs';
8
- import { brief, getJob, noteOutsideRead, planOf, readState, runLockFile, settle, takeInbox, toldToClose, treeOf } from './jobs.mjs';
9
+ import { ASK_TO_ACCEPT, brief, getJob, noteOutsideRead, planOf, readState, runLockFile, settle, takeInbox, toldToClose, treeOf } from './jobs.mjs';
9
10
  import { isJobTool, jobTools, runJobTool } from './jobtools.mjs';
10
11
  import { takeLock } from './lock.mjs';
11
12
  import { DECLINED, gateMcp, isMcpTool, mcpReady, runMcpTool, TOOL_SEARCH } from './mcp.mjs';
@@ -169,7 +170,7 @@ export const thinkingFor = (_model) => ({ type: 'adaptive', display: 'summarized
169
170
  * fallback. A request that goes on from one answered that way carries the `fallback` block as it came, and is priced on
170
171
  * the model that answered.
171
172
  */
172
- const fallsBack = (model) => modelId(model) !== MODEL_IDS.haiku;
173
+ const fallsBack = (model) => laneOf(model) === 'claude' && modelId(model) !== MODEL_IDS.haiku;
173
174
 
174
175
  /** A request's fallback set as its model takes it: on, with its beta, or off. The request itself is changed. */
175
176
  export function withFallback(params, model) {
@@ -249,7 +250,7 @@ export const grantsItself = (command) => SELF_GRANT.test(command.replace(/\\\n/g
249
250
  /** Whether a command, as written, clears or sets the marker that says it is the model's. */
250
251
  export const tampersMarker = (command) => TAMPERS.test(command.replace(/\\\n/g, ' '));
251
252
  export const TAMPER_REFUSED = 'Refused: the marker that says this shell is the model\'s is not yours to clear or set. What it holds back, the user does themselves.';
252
- const SELF_GRANT_REFUSED = "Refused: work is never taken unverified on its author's word. Finish FAILED with what blocks verification, or ask.";
253
+ const SELF_GRANT_REFUSED = `Refused: work is never taken unverified on its author's word. ${ASK_TO_ACCEPT}`;
253
254
 
254
255
  /** How many requests apart a job is shown its own plan again, and how. */
255
256
  const PLAN_EVERY = 10;
@@ -395,6 +396,94 @@ function within(promise, ms, signal) {
395
396
  });
396
397
  }
397
398
 
399
+ /** What an inbox hands over, as blocks: the user's words — or, as `{ event }`, something that happened and is nobody's words: said as it is. */
400
+ export const heardFrom = (inbox) => (inbox?.() ?? []).map((said) => ({ type: 'text', text: typeof said === 'string' ? `The user says, while you work: ${said}` : said.event }));
401
+
402
+ /**
403
+ * What a conversation's tool results are watched for, on Claude (`converse`) and on Cursor (src/cursor.mjs) alike. `mark` takes a
404
+ * round of calls and their results (tool_result blocks), adds to a result what the model is told of it — the same call again and
405
+ * again, a round gone round, a file changed too often, an error come back — and says whether to stop: going round, or declined
406
+ * too often. `circles` puts the last calls to the local model every so many requests; `notes` are said after the results — near
407
+ * the budget, near the time limit, near the turn limit (at that request, or with `past`, at the first one past it); `reminder` is
408
+ * the caller's plan; `outOfTurns`, the turn limit.
409
+ */
410
+ export function roundWatch({ ctx = null, budget = null, clock = null, circling = null, remind = null, now }) {
411
+ /** The last call made and answered, and how many times in a row it has been exactly that. */
412
+ const repeat = { key: null, count: 0 };
413
+ /** The last calls made, by name and input, to find a round in; and how many times the editor changed each file. */
414
+ const trail = [];
415
+ const edited = new Map();
416
+ /** How many times each error has come back from a failed command, and the last calls made, as the local model is shown them. */
417
+ const errors = new Map();
418
+ const recent = [];
419
+ let declined = 0;
420
+ let toldLast = false;
421
+ // How long a job has run, from the start of its run.
422
+ const ranMs = () => Date.parse(now()) - clock.from;
423
+ return {
424
+ get toldLast() {
425
+ return toldLast;
426
+ },
427
+ mark(calls, results) {
428
+ // A call made again and again with the same answer is going nowhere: the model is told so, and then stopped. So is a round of
429
+ // a few calls gone round with the same answers. A file changed again and again is said once, and the run goes on; so is the
430
+ // same error back from failed commands, try after try, whatever else the commands did.
431
+ let looping = false;
432
+ for (const [i, call] of calls.entries()) {
433
+ const key = sameAs(call, results[i]);
434
+ const error = call.name === BASH_TOOL.name && results[i].is_error ? errorOf(results[i].content) : null;
435
+ repeat.count = key === repeat.key ? repeat.count + 1 : 1;
436
+ repeat.key = key;
437
+ if (repeat.count >= REPEAT_STOP_AT) looping = true;
438
+ else if (repeat.count >= REPEAT_NUDGE_AT) results[i] = { ...results[i], content: `${results[i].content}${repeated(repeat.count)}` };
439
+ trail.push(key);
440
+ if (trail.length > TRAIL_LENGTH) trail.shift();
441
+ const round = roundOf(trail);
442
+ if (round && round.rounds >= REPEAT_STOP_AT) looping = true;
443
+ else if (round && round.rounds >= REPEAT_NUDGE_AT) results[i] = { ...results[i], content: `${results[i].content}${roundAgain(round)}` };
444
+ if (call.name === EDITOR_TOOL.name && CHANGES.includes(call.input?.command) && !results[i].is_error) {
445
+ const path = resolve(ctx?.cwd ?? '', String(call.input.path));
446
+ const count = (edited.get(path) ?? 0) + 1;
447
+ edited.set(path, count);
448
+ if (count === EDITS_NOTE_AFTER + 1) results[i] = { ...results[i], content: `${results[i].content}${editedOften(path, count)}` };
449
+ }
450
+ if (error) {
451
+ const count = (errors.get(error.key) ?? 0) + 1;
452
+ errors.set(error.key, count);
453
+ if (count === ERROR_NOTE_AT) results[i] = { ...results[i], content: `${results[i].content}${errorAgain(count, error.line)}` };
454
+ }
455
+ if (circling) {
456
+ recent.push(`${call.name} ${JSON.stringify(call.input ?? {})}`.slice(0, circling.chars));
457
+ if (recent.length > circling.calls) recent.shift();
458
+ }
459
+ }
460
+ // A model the user has said no to, again and again: each no is final, and three of them end the conversation.
461
+ declined += results.filter((r) => r.content === DECLINED).length;
462
+ return { looping, declined: declined >= DECLINE_STOP_AT };
463
+ },
464
+ // Every so many requests a job's last calls are put to the local model — free, and never for longer than its cap; a yes rides on the last answer, with why.
465
+ async circles(turns, results, signal) {
466
+ if (circling && turns % circling.every === 0 && recent.length > 0) {
467
+ const why = await within(Promise.resolve().then(() => circling.ask([...recent])), circling.ms, signal);
468
+ if (typeof why === 'string' && why.trim()) results[results.length - 1] = { ...results.at(-1), content: `${results.at(-1).content}${inCircles(recent.length, why.trim())}` };
469
+ }
470
+ },
471
+ notes(turns, spent, { past = false } = {}) {
472
+ const low = budget !== null && !budget.warned && spent() >= budget.usd * BUDGET_WARN_AT ? [{ type: 'text', text: BUDGET_LOW(spent(), budget.usd) }] : [];
473
+ if (low.length > 0) budget.warned = true;
474
+ const late = clock !== null && !clock.warned && ranMs() >= clock.ms * TIME_WARN_AT ? [{ type: 'text', text: TIME_LOW(ranMs(), clock.ms) }] : [];
475
+ if (late.length > 0) clock.warned = true;
476
+ const at = MAX_TURNS - CLOSING_TURNS;
477
+ const last = !toldLast && (past ? turns >= at : turns === at) ? [{ type: 'text', text: LAST_TURN }] : [];
478
+ if (last.length > 0) toldLast = true;
479
+ return [...low, ...late, ...last];
480
+ },
481
+ // What the caller keeps in the model's view — a job's plan, from runJob — goes last with the results, when it has something to say.
482
+ reminder: (turns) => remind?.(turns),
483
+ outOfTurns: (turns) => turns >= MAX_TURNS,
484
+ };
485
+ }
486
+
398
487
  /**
399
488
  * Runs one conversation from its current messages until the model stops
400
489
  * calling tools. `send` is the transport (canned in tests); `beforeTool` may
@@ -430,21 +519,12 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
430
519
  let toldLast = false;
431
520
  // The ledger names the model that answered: with a fallback that is not always the one asked for.
432
521
  const log = (result, note, model = params.model) => logRun(db, { kind: ledger.kind, model, result, note, now: now(), jobId: ledger.jobId ?? null, sessionId: ledger.sessionId ?? null });
433
- // What the inbox hands over is the user's words — or, as `{ event }`, something that happened and is nobody's words: said as it is.
434
- const heard = () => (inbox?.() ?? []).map((said) => ({ type: 'text', text: typeof said === 'string' ? `The user says, while you work: ${said}` : said.event }));
522
+ const heard = () => heardFrom(inbox);
435
523
  const interrupted = () => {
436
524
  log({ ok: false, usage: usageOf(params.model, null) }, `turn ${turns}: interrupted`);
437
525
  return { turns, stop: 'interrupted', error: null, text, totals, toldLast };
438
526
  };
439
- /** The last call made and answered, and how many times in a row it has been exactly that. */
440
- const repeat = { key: null, count: 0 };
441
- /** The last calls made, by name and input, to find a round in; and how many times the editor changed each file. */
442
- const trail = [];
443
- const edited = new Map();
444
- /** How many times each error has come back from a failed command, and the last calls made, as the local model is shown them. */
445
- const errors = new Map();
446
- const recent = [];
447
- let declined = 0;
527
+ const watch = roundWatch({ ctx, budget, clock, circling, remind, now });
448
528
  // What a job has spent, over this run and the one before it.
449
529
  const costSoFar = () => (budget?.spent ?? 0) + totals.costUsd;
450
530
  const overBudget = () => budget !== null && costSoFar() >= budget.usd;
@@ -506,6 +586,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
506
586
  }
507
587
 
508
588
  while (turns < MAX_TURNS) {
589
+ if (signal?.aborted) return interrupted();
509
590
  // A job that has spent its budget sends nothing more: the last round it made is answered, and the run ends there.
510
591
  if (overBudget()) {
511
592
  stop = 'budget';
@@ -521,6 +602,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
521
602
  let response;
522
603
  // The same request, sent again while the API or the connection is the trouble; nothing of a failed try is kept.
523
604
  for (let attempt = 0; ; ) {
605
+ if (overTime()) return { turns, stop: 'time', error: null, text, totals, toldLast };
524
606
  try {
525
607
  response = await send(params, { onText, onThinking, onBlock, signal });
526
608
  break;
@@ -578,6 +660,10 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
578
660
  if (!refused) text = textOf(response.content) || text;
579
661
  for (const block of response.content) onBlock(block);
580
662
  onTurn?.(response, totals);
663
+ if (overTime()) {
664
+ stop = 'time';
665
+ break;
666
+ }
581
667
  // The API paused a long turn of its own accord: the reply so far is kept, and the turn simply goes on.
582
668
  if (response.stop_reason === 'pause_turn') continue;
583
669
  if (response.stop_reason === 'end_turn') {
@@ -597,6 +683,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
597
683
 
598
684
  totals.toolCalls += calls.length;
599
685
  const answer = async (call) => {
686
+ if (overTime()) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: the job reached its time limit', is_error: true };
600
687
  if (signal?.aborted) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: interrupted by the user', is_error: true };
601
688
  onTool?.(call);
602
689
  let out;
@@ -619,63 +706,22 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
619
706
  stop = 'interrupted';
620
707
  break;
621
708
  }
622
- // A call made again and again with the same answer is going nowhere: the model is told so, and then stopped. So is a round of
623
- // a few calls gone round with the same answers. A file changed again and again is said once, and the run goes on; so is the
624
- // same error back from failed commands, try after try, whatever else the commands did.
625
- let looping = false;
626
- for (const [i, call] of calls.entries()) {
627
- const key = sameAs(call, results[i]);
628
- const error = call.name === BASH_TOOL.name && results[i].is_error ? errorOf(results[i].content) : null;
629
- repeat.count = key === repeat.key ? repeat.count + 1 : 1;
630
- repeat.key = key;
631
- if (repeat.count >= REPEAT_STOP_AT) looping = true;
632
- else if (repeat.count >= REPEAT_NUDGE_AT) results[i] = { ...results[i], content: `${results[i].content}${repeated(repeat.count)}` };
633
- trail.push(key);
634
- if (trail.length > TRAIL_LENGTH) trail.shift();
635
- const round = roundOf(trail);
636
- if (round && round.rounds >= REPEAT_STOP_AT) looping = true;
637
- else if (round && round.rounds >= REPEAT_NUDGE_AT) results[i] = { ...results[i], content: `${results[i].content}${roundAgain(round)}` };
638
- if (call.name === EDITOR_TOOL.name && CHANGES.includes(call.input?.command) && !results[i].is_error) {
639
- const path = resolve(ctx?.cwd ?? '', String(call.input.path));
640
- const count = (edited.get(path) ?? 0) + 1;
641
- edited.set(path, count);
642
- if (count === EDITS_NOTE_AFTER + 1) results[i] = { ...results[i], content: `${results[i].content}${editedOften(path, count)}` };
643
- }
644
- if (error) {
645
- const count = (errors.get(error.key) ?? 0) + 1;
646
- errors.set(error.key, count);
647
- if (count === ERROR_NOTE_AT) results[i] = { ...results[i], content: `${results[i].content}${errorAgain(count, error.line)}` };
648
- }
649
- if (circling) {
650
- recent.push(`${call.name} ${JSON.stringify(call.input ?? {})}`.slice(0, circling.chars));
651
- if (recent.length > circling.calls) recent.shift();
652
- }
653
- }
654
709
  // The advisor asked again and again is the same, and its tool cannot be taken away: the model is told so once, and then stopped.
655
- // So is a model the user has said no to, again and again: each no is final, and three of them end the conversation.
656
- declined += results.filter((r) => r.content === DECLINED).length;
657
- if (looping || advisor.asked >= ADVISOR_STOP_AT || declined >= DECLINE_STOP_AT) {
710
+ const marked = watch.mark(calls, results);
711
+ if (marked.looping || advisor.asked >= ADVISOR_STOP_AT || marked.declined) {
658
712
  params.messages.push({ role: 'user', content: results });
659
- stop = looping ? 'loop' : declined >= DECLINE_STOP_AT ? 'declined' : 'advisor';
713
+ stop = marked.looping ? 'loop' : marked.declined ? 'declined' : 'advisor';
660
714
  break;
661
715
  }
662
- // Every so many requests a job's last calls are put to the local model — free, and never for longer than its cap; a yes rides on the last answer, with why.
663
- if (circling && turns % circling.every === 0 && recent.length > 0) {
664
- const why = await within(Promise.resolve().then(() => circling.ask([...recent])), circling.ms, signal);
665
- if (typeof why === 'string' && why.trim()) results[results.length - 1] = { ...results.at(-1), content: `${results.at(-1).content}${inCircles(recent.length, why.trim())}` };
666
- }
716
+ await watch.circles(turns, results, signal);
667
717
  const enough = advisor.asked >= ADVISOR_SPENT_AT && !advisor.told ? [{ type: 'text', text: ADVISOR_SPENT }] : [];
668
718
  if (enough.length > 0) advisor.told = true;
669
- const low = budget !== null && !budget.warned && costSoFar() >= budget.usd * BUDGET_WARN_AT ? [{ type: 'text', text: BUDGET_LOW(costSoFar(), budget.usd) }] : [];
670
- if (low.length > 0) budget.warned = true;
671
- const late = clock !== null && !clock.warned && ranMs() >= clock.ms * TIME_WARN_AT ? [{ type: 'text', text: TIME_LOW(ranMs(), clock.ms) }] : [];
672
- if (late.length > 0) clock.warned = true;
673
- const last = turns === MAX_TURNS - CLOSING_TURNS ? [{ type: 'text', text: LAST_TURN }] : [];
674
- if (last.length > 0) toldLast = true;
675
- params.messages.push({ role: 'user', content: [...results, ...heard(), ...enough, ...low, ...late, ...last] });
719
+ const notes = watch.notes(turns, costSoFar);
720
+ if (watch.toldLast) toldLast = true;
721
+ params.messages.push({ role: 'user', content: [...results, ...heard(), ...enough, ...notes] });
676
722
 
677
723
  // What the caller keeps in the model's view — a job's plan, from runJob — goes last with these results, when it has something to say.
678
- const reminder = remind?.(turns);
724
+ const reminder = watch.reminder(turns);
679
725
  if (reminder) params.messages.at(-1).content.push({ type: 'text', text: reminder });
680
726
 
681
727
  // Past the band where the work must either stop or shed its past: the conversation so far is summarized server-side, here, at the
@@ -736,18 +782,15 @@ export const networkOn = (db) => shellSwitch(db, 'shell.network') !== 'off';
736
782
  * was given one, settled before the run returns (`settle`, src/jobs.mjs): what
737
783
  * became of it is in `settled`, and in the report.
738
784
  */
739
- export async function runJob(db, id, { send = sendToApi, now, signal = null, onStart = null, onTool = null, onResult = null, onTurn = null, onThinking = null, onRetry = null, onCompact = null, inbox = null, approve = null } = {}) {
785
+ export async function runJob(db, id, { send = sendToApi, cursorSdk = null, now, signal = null, onStart = null, onTool = null, onResult = null, onTurn = null, onThinking = null, onRetry = null, onCompact = null, inbox = null, approve = null } = {}) {
740
786
  const job = getJob(db, id);
741
787
  if (job.status !== 'running' && job.status !== 'needs_input') throw new UsageError(`j${id} is ${job.status} — only a running job can be run`);
742
788
  if (!job.model) throw new UsageError(`j${id} has no route — it was created before routing existed; create it again`);
743
- if (MODELS.includes(job.model) && !usableModels(db).includes(job.model)) {
744
- throw new UsageError(`j${id} is routed to ${job.model}, which is off — turn it on under API models, on the settings page (sumo config), or abandon it and create it again`);
745
- }
789
+ const off = JOB_MODELS.includes(job.model) ? offLine(db, job.model, 'abandon it and create it again') : null;
790
+ if (off) throw new UsageError(`j${id} cannot run: ${off}`);
746
791
  const project = getProject(db, job.project);
747
792
  const tree = treeOf(db, id);
748
793
  if (tree && !existsSync(tree.path)) throw new UsageError(`j${id}'s worktree, ${tree.path}, no longer exists — abandon it and create it again`);
749
- // The MCP servers of this process are the job's too: their tools behind the search tool, their names in its prompt.
750
- const registry = await mcpReady();
751
794
  // Whom this job may ask, by the setting and the switches as they are now: the next model up that is on, or nobody.
752
795
  const advisor = (getMeta(db, 'config.jobs.advisor') ?? CONFIG_DEFAULTS['jobs.advisor']) === 'on' ? advisorOf(job.model, usableModels(db)) : null;
753
796
  // What this job may spend, by the setting as it is now; shared by both runs, so the second goes on from what the first spent.
@@ -757,23 +800,45 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
757
800
  // with an answer, say — counts from its own start, as its budget does: nothing of an earlier `sumo job run` is carried over.
758
801
  const minutes = jobMinutes(db);
759
802
  const clock = minutes === null ? null : { ms: minutes * 60_000, from: Date.parse(now?.() ?? new Date().toISOString()), warned: false };
760
- const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry.tools(), advisor });
761
803
  const lock = tx(db, () => takeRunLock(id));
762
804
  const heard = () => [...(inbox?.() ?? []), ...takeInbox(id)];
763
805
  // A command the sandbox blocked is put to the user with the job named, as its MCP calls are; its background commands are its own to end.
764
806
  const ctx = { ...contextFor(project, { jobId: id, sandbox: sandboxOn(db), network: networkOn(db), tree: tree?.path }), approve: approve && ((ask) => approve({ ...ask, job: id })), opened: new Set(), background: new Set() };
765
807
  // The workflow gate holds a job's commands as it holds the chat's: once per session for this job, which has a context of its own,
766
808
  // and with the job's own project in scope whether or not its card came up in the session.
767
- const gate = (call) => {
768
- if (isJobTool(call)) return runJobTool(call, { job, ctx, signal });
769
- if (isMcpTool(call.name)) return gateMcp(call, { approve, signal, job: id });
809
+ // A call on Cursor is handed its run's own signal, which also ends when that run does; on Claude, the job's.
810
+ const gate = (call, { signal: until = signal } = {}) => {
811
+ if (isJobTool(call)) return runJobTool(call, { job, ctx, signal: until });
812
+ if (isMcpTool(call.name)) return gateMcp(call, { approve, signal: until, job: id });
770
813
  if (call.name !== BASH_TOOL.name) return null;
771
814
  if (grantsItself(String(call.input?.command ?? ''))) return { content: SELF_GRANT_REFUSED, isError: true };
772
815
  // A job created where no session had begun is still gated, under a session of its own: it holds no turns, only what was shown.
773
816
  const held = handleEvent(db, 'pre-tool', { session_id: job.session_id ?? `job:j${id}`, agent_id: `j${id}`, project: job.project, cwd: ctx.cwd, tool_name: 'bash', tool_input: { command: String(call.input?.command ?? '') } }, now?.());
774
817
  return held ? { content: JSON.parse(held).deny, isError: true } : null;
775
818
  };
819
+ const deadline = new AbortController();
820
+ let timer;
821
+ // Node timers accept at most a signed 32-bit delay.
822
+ function arm(ms) {
823
+ const delay = Math.min(ms, 2_147_483_647);
824
+ timer = setTimeout(() => ms > delay ? arm(ms - delay) : deadline.abort(), delay);
825
+ timer.unref?.();
826
+ }
827
+ if (clock) {
828
+ arm(clock.ms);
829
+ signal = signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal;
830
+ }
831
+ let cancelSetup;
776
832
  try {
833
+ // The registry is shared by other conversations. Stop waiting for it when this job ends.
834
+ const stopped = new Promise((resolve) => {
835
+ cancelSetup = () => resolve(null);
836
+ if (signal?.aborted) cancelSetup();
837
+ else signal?.addEventListener('abort', cancelSetup, { once: true });
838
+ });
839
+ const registry = await Promise.race([mcpReady(), stopped]);
840
+ signal?.removeEventListener('abort', cancelSetup);
841
+ const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry?.tools(), advisor });
777
842
  onStart?.(job);
778
843
  // A job that outgrows the context is summarized and goes on; its brief is on disk for whatever the summary left out, and what code
779
844
  // recorded of its work is said after the summary.
@@ -790,12 +855,24 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
790
855
  // a page told the job.
791
856
  const told = (call) => {
792
857
  if (isWebTool(call) || isMcpTool(call.name)) {
793
- noteOutsideRead(id, call.name);
858
+ noteOutsideRead(db, id, call.name);
794
859
  noteSessionOutside(db, job.session_id, call.name);
795
860
  }
796
861
  onTool?.(call);
797
862
  };
798
- const run = () => converse(db, params, { send, now, signal, onTool: told, onResult, onTurn, onThinking, onRetry, onCompact, compact, budget, clock, circling, remind, inbox: heard, beforeTool: gate, ctx, ledger: { kind: job.agent, jobId: id, sessionId: job.session_id } });
863
+ const ledger = { kind: job.agent, jobId: id, sessionId: job.session_id };
864
+ // On a Cursor model the loop is Cursor's (src/cursor.mjs): Sumo's tools, gate and caps the same, and its web search unseen —
865
+ // so a job given it (a scout, as on Claude) is taken to have read the web before it starts, and what it learns is held for
866
+ // the user, as above. A worker or a reviewer is given none, and is not marked.
867
+ const onCursor = laneOf(job.model) === 'cursor';
868
+ if (onCursor && params.tools.some(isWebTool)) {
869
+ noteOutsideRead(db, id, 'web_search');
870
+ noteSessionOutside(db, job.session_id, 'web_search');
871
+ }
872
+ const run = () =>
873
+ onCursor
874
+ ? converseCursor(db, params, { ctx, ledger, beforeTool: gate, onTool: told, onResult, signal, budget, clock, circling, remind, inbox: heard, now, sdk: cursorSdk })
875
+ : converse(db, params, { send, now, signal, onTool: told, onResult, onTurn, onThinking, onRetry, onCompact, compact, budget, clock, circling, remind, inbox: heard, beforeTool: gate, ctx, ledger });
799
876
  let outcome = await run();
800
877
  if (outcome.stop === 'end_turn' && getJob(db, id).status === 'running') {
801
878
  params.messages.push({ role: 'user', content: [{ type: 'text', text: UNCLOSED }] });
@@ -804,14 +881,17 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
804
881
  const totals = Object.fromEntries(Object.keys(more.totals).map((k) => [k, k === 'contextTokens' ? more.totals[k] : outcome.totals[k] + more.totals[k]]));
805
882
  outcome = { ...more, turns: outcome.turns + more.turns, text: more.text || outcome.text, totals, toldLast: outcome.toldLast || more.toldLast };
806
883
  }
884
+ if (deadline.signal.aborted && signal.reason === deadline.signal.reason) outcome.stop = 'time';
807
885
  // Kept for what follows the job: one told here to close near a limit is not retried past it (`followUp`, src/jobs.mjs).
808
- toldToClose(db, id, budget?.warned ? 'budget' : clock?.warned ? 'time' : outcome.toldLast ? 'turns' : null);
886
+ toldToClose(db, id, budget?.warned ? 'budget' : (clock?.warned || outcome.stop === 'time') ? 'time' : outcome.toldLast ? 'turns' : null);
809
887
  // What it left running in the background goes before its worktree is settled: still running, it would write into a tree being
810
888
  // brought back and removed, or be there when the change lands in the project.
811
889
  for (const group of ctx.background) killGroup(group);
812
890
  const settled = await settle(db, id, now?.());
813
891
  return { ...outcome, job: getJob(db, id), settled };
814
892
  } finally {
893
+ clearTimeout(timer);
894
+ signal?.removeEventListener('abort', cancelSetup);
815
895
  for (const group of ctx.background) killGroup(group);
816
896
  rmSync(lock, { force: true });
817
897
  }
package/src/mcp.mjs CHANGED
@@ -2,6 +2,7 @@ import { spawn, spawnSync } from 'node:child_process';
2
2
  import { existsSync, readFileSync, renameSync } from 'node:fs';
3
3
  import { homedir } from 'node:os';
4
4
  import { delimiter, dirname, join } from 'node:path';
5
+ import { CREDENTIAL_NAMES } from './auth.mjs';
5
6
  import { getMeta, openDb, setMeta, tx } from './db.mjs';
6
7
  import { UsageError } from './memory.mjs';
7
8
  import { paths } from './paths.mjs';
@@ -162,17 +163,14 @@ export function readConfig() {
162
163
  }));
163
164
  }
164
165
 
165
- /** The servers replaced whole, in one transaction: a run that dies half-way leaves the list as it was. */
166
- export function writeConfig(config) {
167
- withServers((db) => {
168
- db.exec('DELETE FROM mcp_servers');
169
- putServers(db, config?.mcpServers ?? {});
170
- });
171
- }
172
-
173
- /** `${NAME}` in a configured value, filled from the environment. One nothing fills is refused: a server must not start with a blank where its token should be. */
166
+ /**
167
+ * `${NAME}` in a configured value, filled from the environment. One nothing fills is refused: a server must not start
168
+ * with a blank where its token should be. Nor is sumo's own credential filled in: a server never gets it unnamed, and
169
+ * naming it is no way round.
170
+ */
174
171
  export function expand(value, env = process.env) {
175
172
  return String(value).replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_, name) => {
173
+ if (CREDENTIAL_NAMES.includes(name)) throw new UsageError(`\${${name}} is sumo's own credential: it is never handed to an MCP server — export the server's token under a name of its own`);
176
174
  if (env[name] === undefined) throw new UsageError(`\${${name}} is not set in the environment`);
177
175
  return env[name];
178
176
  });
package/src/memeval.mjs CHANGED
@@ -1,9 +1,8 @@
1
1
  import { getMeta, setMeta } from './db.mjs';
2
- import { extractorVersion } from './ledger.mjs';
2
+ import { extractorVersion, memoryModel } from './ledger.mjs';
3
3
  import { UsageError } from './memory.mjs';
4
4
  import { dryExtract } from './rebuild.mjs';
5
5
  import { searchAll } from './search.mjs';
6
- import { CONFIG_DEFAULTS } from './setup.mjs';
7
6
  import { clip, saysAs } from './text.mjs';
8
7
 
9
8
  /**
@@ -91,7 +90,7 @@ const again = (memory, filed) =>
91
90
  async function replayExtractor(db, { limit, now, extract }) {
92
91
  const { negatives, positives } = cases(db, limit);
93
92
  const out = { ran: false, failed: false, error: null, negatives: { cases: negatives.length, kept: 0, refiled: [] }, positives: { cases: positives.length, kept: 0, missed: [] } };
94
- if ((getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model']) === 'off') return { ...out, failed: true, error: 'the memory pass is off (memory.model) — Memory › Filed by, on the settings page (sumo config), turns it on' };
93
+ if (memoryModel(db) === 'off') return { ...out, failed: true, error: 'the memory pass is off (memory.model) — Memory › Filed by, on the settings page (sumo config), turns it on' };
95
94
  if (negatives.length + positives.length === 0) return { ...out, error: 'no memory with the turn it came from to replay yet' };
96
95
  const read = new Map();
97
96
  const filedFrom = async (turn) => {
package/src/memory.mjs CHANGED
@@ -150,7 +150,7 @@ function noteEvent(db, id, op, { actor = 'sumo', runId = null, reason = null, no
150
150
  * Stores one memory. Returns it with any active memories that look like the
151
151
  * same statement, so the caller can supersede instead of piling up duplicates.
152
152
  * `derivedBy` is the extractor version that drew it from the journal (absent: primary, the user's to keep);
153
- * `runId` is the extractions row of the pass that filed it.
153
+ * `runId` is the extractions row of the pass that filed it; `edit` says the memory it supersedes is this one reworded.
154
154
  */
155
155
  export function add(db, input) {
156
156
  if (!TYPES.includes(input.type)) {
@@ -210,7 +210,8 @@ export function add(db, input) {
210
210
  );
211
211
  const id = Number(lastInsertRowid);
212
212
  noteEvent(db, id, 'added', { actor: writer, runId, now });
213
- if (replaced) markSuperseded(db, replaced.id, id, now, { actor: writer, runId });
213
+ // An edit is the user keeping the memory in other words: its history says so, and it is never counted as thrown out.
214
+ if (replaced) markSuperseded(db, replaced.id, id, now, { actor: writer, runId, op: input.edit ? 'edited' : 'superseded' });
214
215
 
215
216
  return { memory: get(db, id), similar, redacted };
216
217
  });
@@ -230,14 +231,14 @@ function noteRetired(db, id) {
230
231
  /** Whether a scanned fact with this text was let go by someone other than the scanner. */
231
232
  export const scanRetired = (db, scope, scanKey, body) => getMeta(db, retiredKey(scope, scanKey)) === digestOf(body);
232
233
 
233
- function markSuperseded(db, oldId, newId, now, { actor, runId } = {}) {
234
+ function markSuperseded(db, oldId, newId, now, { actor, runId, op = 'superseded' } = {}) {
234
235
  noteRetired(db, oldId);
235
236
  db.prepare(`UPDATE memories SET state = 'superseded', superseded_by = ?, invalid_at = ? WHERE id = ?`).run(
236
237
  newId,
237
238
  now,
238
239
  oldId,
239
240
  );
240
- noteEvent(db, oldId, 'superseded', { actor, runId, reason: `by m${newId}`, now });
241
+ noteEvent(db, oldId, op, { actor, runId, reason: `by m${newId}`, now });
241
242
  }
242
243
 
243
244
  /** Links two memories that already exist: `oldId` stops being true because of `newId`. `actor` and `runId` say who asked, and in which pass. */
package/src/model.mjs CHANGED
@@ -35,6 +35,11 @@ const PRICES = {
35
35
  // Where a declined reply is run again (`fallbacks`): the API names the model by the category; the ledger must price what it answered on.
36
36
  'claude-opus-4-8': { input: 5, output: 25, cacheRead: 0.1 },
37
37
  'claude-sonnet-5': { input: 2, output: 10, cacheRead: 0.1 },
38
+ // Cursor's own models, at the standard variants Sumo asks for (cursor.com/docs/models, Nov 18 2026). Paid plans draw them from
39
+ // a weekly pool first: these are what the same tokens cost past it, so a job on them is never priced below what it could cost.
40
+ // Cursor lists no cache-write rate; a write is priced as the others are.
41
+ 'composer-2.5': { input: 0.5, output: 2.5, cacheRead: 0.4 },
42
+ 'grok-4.7': { input: 2, output: 6, cacheRead: 0.25 },
38
43
  };
39
44
  /** What writing to the cache costs relative to input: the five-minute cache every request here writes. */
40
45
  export const CACHE_WRITE = 1.25;
package/src/page.mjs CHANGED
@@ -38,6 +38,7 @@ function edit(db, id, body) {
38
38
  body: String(body ?? ''),
39
39
  writtenBy: 'user',
40
40
  supersedes: id,
41
+ edit: true,
41
42
  });
42
43
  return { memory: saved, redacted };
43
44
  }
package/src/prime.mjs CHANGED
@@ -3,10 +3,9 @@ import { ago, askLines } from './card.mjs';
3
3
  import { getMeta, setMeta } from './db.mjs';
4
4
  import { paths } from './paths.mjs';
5
5
  import { isScratch, listProjects, SCRATCH_DAYS } from './projects.mjs';
6
- import { filingState } from './ledger.mjs';
6
+ import { filingState, memoryModel } from './ledger.mjs';
7
7
  import { lastHeard } from './sessions.mjs';
8
8
  import { spoolWaitingSince } from './spool.mjs';
9
- import { CONFIG_DEFAULTS } from './setup.mjs';
10
9
  import { clip, estimateTokens } from './text.mjs';
11
10
 
12
11
  const LINE_MAX = 120;
@@ -43,7 +42,7 @@ export function prime(db, { budget, project = null, now = new Date().toISOString
43
42
 
44
43
  // The small, time-sensitive sections are fixed first; preferences get whatever room is left.
45
44
  // Preserve the compact layout normally; when it cannot fit, keep time-sensitive lines first.
46
- const sections = [workflows(db), projectsLine(db), leftOff(db, now), jobs(db, now), toConfirm(db, { project, now }), warnings(db, now)];
45
+ const sections = [workflows(db), projectsLine(db, project), leftOff(db, now), jobs(db, now), toConfirm(db, { project, now }), warnings(db, now)];
47
46
  // Where the work with no project goes is said every time, and is not something remembered.
48
47
  const remembered = sections.flat().length;
49
48
  sections.splice(2, 0, scratchLine());
@@ -124,12 +123,15 @@ function workflows(db) {
124
123
  return [`Workflows — before doing what one covers, run sumo show <id> and follow it: ${rows.map((m) => `m${m.id} "${clip(m.title, NAME_MAX)}"${m.cue ? ` — when: ${clip(m.cue, NAME_MAX)}` : ''}`).join(' · ')}`];
125
124
  }
126
125
 
127
- function projectsLine(db) {
126
+ function projectsLine(db, here = null) {
128
127
  const all = listProjects(db).filter((p) => !isScratch(p));
129
128
  if (all.length === 0) return [];
130
129
  const shown = all.slice(0, MAX_PROJECTS).map((p) => `${p.slug} (${tilde(p.path)})`);
131
130
  const more = all.length > MAX_PROJECTS ? ` · +${all.length - MAX_PROJECTS} more: sumo project list` : '';
132
- return [`Projects: ${shown.join(' · ')}${more}`];
131
+ // The project the session started in is said as the one it works in: told only the list and where work with no
132
+ // project goes, a model once took a turn that named none for scratch's, in a chat started inside the project.
133
+ const at = here ? all.find((p) => p.slug === here) : null;
134
+ return [`Projects: ${shown.join(' · ')}${more}`, ...(at ? [`Working in: ${at.slug} (${tilde(at.path)}) — this session started there; the work is there unless the user names another`] : [])];
133
135
  }
134
136
 
135
137
  function scratchLine() {
@@ -210,14 +212,16 @@ export function heartbeat(db, now = new Date().toISOString()) {
210
212
  function warnings(db, now) {
211
213
  const out = [];
212
214
  // AGENTS.md says a background writer files what the user says; with it off, nothing does unless the model is told.
213
- if ((getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model']) === 'off') {
215
+ if (memoryModel(db) === 'off') {
214
216
  out.push('The memory pass is off (memory.model): save what the user states that should last, as it is said, with sumo add.');
215
217
  }
216
218
  // As the ledger counts them, which is what the status says: failures kept under an extractor since changed are not this one's.
217
- const filing = filingState(db, now);
219
+ const filing = filingState(db);
218
220
  const failures = filing.failures;
219
221
  if (filing.fallbackUsed) out.push('Warning: the memory pass last ran on the hosted fallback — the local model could not answer, and each conversation read that way is paid for. Tell the user; sumo memory status says how the local model is.');
220
222
  if (failures >= SCRIBE_FAILURES_TO_WARN) out.push(`Warning: the background memory writer has failed ${failures} times in a row — nothing new is being remembered. Tell the user; sumo memory status shows why.`);
223
+ // Paused for another reason — most of what it filed thrown out — nothing new is filed either, and it is said as plainly.
224
+ else if (filing.paused) out.push(`Warning: the memory pass has stopped filing by itself — ${filing.reason}. Tell the user.`);
221
225
  // What waits in the spool is replayed by the next event that gets through: still there an hour on, it is not getting through.
222
226
  const spool = spoolWaitingSince();
223
227
  const waited = spool.since === null ? 0 : Date.parse(now) - spool.since;
package/src/rebuild.mjs CHANGED
@@ -52,6 +52,8 @@ function unsupported(db, turnIds, supported) {
52
52
  }
53
53
 
54
54
  const count = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
55
+ /** The conversations a rebuild from the model's shell reads unless `--limit` says: each is a model call nobody asked for. */
56
+ const SHELL_LIMIT = 5;
55
57
 
56
58
  /**
57
59
  * `sumo memory rebuild`: the journal read again with the extractor as it is now, and the difference from the memories
@@ -60,10 +62,13 @@ const count = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
60
62
  * as read) and the unsupported derived memories are let go the way `sumo forget` does, history kept — except one that
61
63
  * replaced another memory, which is shown as kept; each change is written to the memory's history with the reason `rebuilt`. A change wider than a pass may make (`tooWide`) is
62
64
  * refused whole unless `yes`. `since` (an ISO time) and `limit` (the newest N conversations) bound what is read: every
63
- * batch is a model call. Returns the lines to print; `exitCode` 1 when something could not be read or was refused.
65
+ * batch is a model call. `fromShell`, a run the model's shell started (only ever a dry one): the newest five unless
66
+ * `limit` says, and never on the hosted fallback. Returns the lines to print; `exitCode` 1 when something could not be
67
+ * read or was refused.
64
68
  */
65
- export async function rebuild(db, { dryRun = false, since, limit, yes = false, now = new Date().toISOString() } = {}) {
69
+ export async function rebuild(db, { dryRun = false, since, limit: asked, yes = false, fromShell = false, now = new Date().toISOString() } = {}) {
66
70
  const version = extractorVersion(db);
71
+ const limit = asked ?? (fromShell ? SHELL_LIMIT : undefined);
67
72
  const sessions = db
68
73
  .prepare(
69
74
  `SELECT s.id FROM sessions s
@@ -77,7 +82,7 @@ export async function rebuild(db, { dryRun = false, since, limit, yes = false, n
77
82
  const turnIds = sessions.flatMap((id) => turns.all(id, since ?? null).map((t) => t.id));
78
83
 
79
84
  const go = async () => {
80
- const read = await extract(db, { turnIds, now });
85
+ const read = await extract(db, { turnIds, now, fallback: !fromShell });
81
86
  const readTurns = read.batches.flatMap((b) => b.turnIds);
82
87
  const adds = read.wouldFile.filter((f) => f.existing === null);
83
88
  // What an addition does to a memory it looks like, as far as can be said before the pair judge is asked — which a
package/src/redact.mjs CHANGED
@@ -6,6 +6,8 @@
6
6
  * plus a digit: without that it eats long file paths and 40-char git SHAs,
7
7
  * which memories legitimately contain.
8
8
  */
9
+ import { CREDENTIAL_NAMES } from './auth.mjs';
10
+
9
11
  const PATTERNS = [
10
12
  /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
11
13
  /\bsk-[A-Za-z0-9_-]{20,}/g,
@@ -149,8 +151,18 @@ function redactReadable(text) {
149
151
  return MARK;
150
152
  };
151
153
 
154
+ // The credentials Sumo itself runs on, by their values, whatever their shape: a Cursor key has none the patterns know,
155
+ // and a file the model may read (~/.zshrc) can hold one in a form no assignment rule sees.
156
+ let out = text;
157
+ for (const name of CREDENTIAL_NAMES) {
158
+ const value = process.env[name]?.trim();
159
+ if (!value || value.length < 16 || !out.includes(value)) continue;
160
+ const parts = out.split(value);
161
+ count += parts.length - 1;
162
+ out = parts.join(MARK);
163
+ }
152
164
  // The URL first: a vendor token used as its password goes once, with the URL around it kept.
153
- let out = text.replace(URL_PASSWORD, (_m, head) => {
165
+ out = out.replace(URL_PASSWORD, (_m, head) => {
154
166
  count++;
155
167
  return `${head}${MARK}@`;
156
168
  });