sumo-agents 0.7.2 → 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/scribe.mjs CHANGED
@@ -1,19 +1,18 @@
1
- import { spawn } from 'node:child_process';
1
+ import { spawn, spawnSync } from 'node:child_process';
2
2
  import { appendFileSync, closeSync, openSync, readFileSync, rmSync, statSync, writeSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  import { applyOps } from './apply.mjs';
5
5
  import { resolveAnthropicCredential } from './auth.mjs';
6
6
  import { MODELS, offLine, usableModels } from './catalog.mjs';
7
- import { getMeta, openDb, tx } from './db.mjs';
7
+ import { openDb, tx } from './db.mjs';
8
8
  import { judgeNewPairs } from './dream.mjs';
9
- import { extractorVersion, filingState, notePass } from './ledger.mjs';
9
+ import { extractorVersion, filingState, memoryModel, notePass } from './ledger.mjs';
10
10
  import { callModel, failure, logRun } from './model.mjs';
11
11
  import { appendLog } from './logs.mjs';
12
12
  import { paths, REPO_ROOT, SELF } from './paths.mjs';
13
13
  import { aliasesOf, listProjects } from './projects.mjs';
14
14
  import { line } from './render.mjs';
15
15
  import { currentProject, markScribed, sessionJournal, sessionsAwaitingExtraction, turnsAwaitingExtraction } from './sessions.mjs';
16
- import { CONFIG_DEFAULTS } from './setup.mjs';
17
16
  import { ftsQuery, head } from './text.mjs';
18
17
  import { abridge } from './transcript.mjs';
19
18
 
@@ -35,8 +34,6 @@ const ATTEMPTS = 3;
35
34
  const NO_USAGE = { inputTokens: 0, outputTokens: 0, costUsd: 0, cacheReadTokens: null, cacheCreationTokens: null };
36
35
  const added = (x, y) => ({ ...x, inputTokens: x.inputTokens + y.inputTokens, outputTokens: x.outputTokens + y.outputTokens, costUsd: x.costUsd + y.costUsd });
37
36
 
38
- /** The model the pass is set to: `memory.model`, the one setting. */
39
- const memoryModel = (db) => getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model'];
40
37
  /** Whether the user turned the writer off: nothing gets filed, so nothing should wait for it to be. */
41
38
  const writerOff = (db) => memoryModel(db) === 'off';
42
39
 
@@ -47,7 +44,7 @@ const writerOff = (db) => memoryModel(db) === 'off';
47
44
  export function shouldRun(db, { now }) {
48
45
  if (writerOff(db)) return false;
49
46
  if (sessionsAwaitingExtraction(db, { now, limit: 1 }).length === 0) return false;
50
- return !filingState(db, now).paused;
47
+ return !filingState(db).paused;
51
48
  }
52
49
 
53
50
  /**
@@ -70,24 +67,59 @@ export function spawnDetached(args) {
70
67
  child.unref();
71
68
  }
72
69
 
70
+ /** When a process started, as `ps` says it, or '' when there is no such process: with the pid, which run it is. */
71
+ export function startOf(pid) {
72
+ // In one locale and one zone, whoever asks: the words and the hour `ps` gives follow LC_TIME and TZ, and a lock read in another
73
+ // shell's would be taken for another run's.
74
+ return spawnSync('/bin/ps', ['-o', 'lstart=', '-p', String(pid)], { encoding: 'utf8', env: { PATH: '/bin:/usr/bin', LC_ALL: 'C', TZ: 'UTC' } }).stdout?.trim() ?? '';
75
+ }
76
+
77
+ /** What a lock says of the run that holds it: its pid and when it started, so a pid handed out again is not taken for it. */
78
+ let mine = null;
79
+ const ownLine = () => (mine ??= `${process.pid} ${startOf(process.pid)}`);
80
+
81
+ /** A lock taken a moment ago, whose holder cannot be told yet: believed for a while, then taken for one left behind. */
82
+ const young = (file) => Date.now() - statSync(file).mtimeMs <= LOCK_STALE_MS;
83
+
73
84
  /**
74
- * Whether the run that took a lock is still going. Its pid says so; a model call that is retried can outlast any
75
- * fixed age. Only a lock that died before it named a pid is judged by its age.
85
+ * Whether the run that took a lock is still going: its process is alive, and started when the lock says — a pid is
86
+ * handed out again once its process is gone, and a model call that is retried can outlast any fixed age. A lock that
87
+ * names no pid yet is judged by its age, and so is a live pid with no start beside it (written by hand, or by a sumo
88
+ * from before).
76
89
  */
77
90
  function held(file) {
78
- const pid = Number(readFileSync(file, 'utf8'));
79
- if (!Number.isInteger(pid) || pid <= 0) return Date.now() - statSync(file).mtimeMs <= LOCK_STALE_MS;
91
+ const [pidText = '', ...rest] = readFileSync(file, 'utf8').trim().split(' ');
92
+ const pid = Number(pidText);
93
+ if (!pidText || !Number.isInteger(pid) || pid <= 0) return young(file);
80
94
  try {
81
95
  process.kill(pid, 0);
82
- return true;
83
96
  } catch (cause) {
84
- return cause.code === 'EPERM'; // alive, and someone else's
97
+ if (cause.code !== 'EPERM') return false; // EPERM: alive, and someone else's
98
+ }
99
+ const started = rest.join(' ');
100
+ return started ? startOf(pid) === started : young(file);
101
+ }
102
+
103
+ const lockFile = (name) => join(paths().home, `${name}.lock`);
104
+
105
+ /**
106
+ * The run holding a pass's lock, for whoever checks on it: `{ pid, since }` (since: when it was taken, in
107
+ * milliseconds), or null when no live run holds it — a lock left by one that died is taken over by the next.
108
+ */
109
+ export function lockHolder(name) {
110
+ const file = lockFile(name);
111
+ try {
112
+ return held(file) ? { pid: Number(readFileSync(file, 'utf8').split(' ')[0]), since: statSync(file).mtimeMs } : null;
113
+ } catch (cause) {
114
+ if (cause.code === 'ENOENT') return null;
115
+ throw cause;
85
116
  }
86
117
  }
87
118
 
88
119
  /** One writer at a time. A lock whose run has died is taken over. */
89
120
  export async function withLock(name, fn) {
90
- const file = join(paths().home, `${name}.lock`);
121
+ const file = lockFile(name);
122
+ const ours = ownLine();
91
123
  let fd;
92
124
  // Serialize observation, replacement and PID publication so a paused owner cannot lose its fresh lock.
93
125
  const db = openDb();
@@ -106,7 +138,7 @@ export async function withLock(name, fn) {
106
138
  return undefined;
107
139
  }
108
140
  try {
109
- writeSync(acquired, String(process.pid));
141
+ writeSync(acquired, ours);
110
142
  } catch (cause) {
111
143
  closeSync(acquired);
112
144
  rmSync(file);
@@ -117,14 +149,15 @@ export async function withLock(name, fn) {
117
149
  } finally {
118
150
  db.close();
119
151
  }
120
- if (fd === undefined) return { skipped: 'another run is in progress' };
152
+ // `locked`: nothing ran because another run had it — for whoever must say that was not done.
153
+ if (fd === undefined) return { skipped: 'another run is in progress', locked: true };
121
154
  try {
122
155
  return await fn();
123
156
  } finally {
124
157
  closeSync(fd);
125
158
  // Only while it is still ours: a lock taken over meanwhile belongs to the run that took it.
126
159
  try {
127
- if (readFileSync(file, 'utf8') === String(process.pid)) rmSync(file);
160
+ if (readFileSync(file, 'utf8') === ours) rmSync(file);
128
161
  } catch {
129
162
  // Already gone.
130
163
  }
@@ -310,18 +343,21 @@ const EXTRACTS = ['add', 'supersede', 'gotcha', 'checkpoint'];
310
343
  * model that could not answer (server down, bundle past its context, answer cut off) is one failed row of the model
311
344
  * ledger, then one more try on the hosted fallback — when a credential is there to pay for it and a model is on to
312
345
  * take it — and `fallbackUsed` says so; `fallback: false` keeps the call on the model as set, for a pass that has
313
- * paid for as much as it may. `quiet` keeps the model ledger out of it: a dry run writes nothing.
346
+ * paid for as much as it may. `quiet` keeps the local model's calls out of the ledger, for a run that files nothing —
347
+ * never a hosted model's: what was paid for is always in the ledger, the answer that came back noted `said`.
314
348
  */
315
- async function ask(db, { bundle, now, quiet = false, fallback: mayPay = true }) {
349
+ async function ask(db, { bundle, now, quiet = false, fallback: mayPay = true, said = 'read for a run that files nothing' }) {
316
350
  let model = memoryModel(db);
351
+ let paid = false;
317
352
  const log = (result, note) => {
318
- if (!quiet) logRun(db, { kind: 'scribe', model, result, note, now });
353
+ if (!quiet || paid) logRun(db, { kind: 'scribe', model, result, note, now });
319
354
  };
320
355
  const system = readFileSync(join(REPO_ROOT, 'prompts', 'scribe.md'), 'utf8').trim();
321
356
  const schema = opsSchema(db, EXTRACTS);
322
357
  // A writer set to a model the user has since turned off is not called: the refusal is the pass's failure, in the ledger like any other.
323
358
  const off = MODELS.includes(model) ? offLine(db, model, 'set Memory › Filed by to local there') : null;
324
359
  let result = off ? failure(off) : await callModel(db, { system, prompt: bundle.prompt, schema, model, kind: 'scribe' });
360
+ paid = !off && model !== 'local';
325
361
  // A local answer that failed (server down, bundle past its context, answer cut off) is one failed row, then one more try —
326
362
  // when a credential is there to pay for it and a model is on to take it.
327
363
  const fallback = mayPay && !result.ok && model === 'local' && resolveAnthropicCredential() ? fallbackModel(db) : null;
@@ -329,6 +365,7 @@ async function ask(db, { bundle, now, quiet = false, fallback: mayPay = true })
329
365
  log(result, `${result.error} — retrying on ${fallback}`);
330
366
  model = fallback;
331
367
  result = await callModel(db, { system, prompt: bundle.prompt, schema, model, kind: 'scribe' });
368
+ paid = true;
332
369
  }
333
370
  const answered = { usage: result.usage, model, result, fallbackUsed: Boolean(fallback), log };
334
371
  if (!result.ok) {
@@ -341,6 +378,7 @@ async function ask(db, { bundle, now, quiet = false, fallback: mayPay = true })
341
378
  log({ ...result, ok: false }, error);
342
379
  return { ...answered, ok: false, error, ops: null };
343
380
  }
381
+ if (quiet) log(result, said);
344
382
  return { ...answered, ok: true, error: null, ops: result.data.ops };
345
383
  }
346
384
 
@@ -366,16 +404,16 @@ async function fileOps(db, ops, ctx) {
366
404
  * One batch read and filed, with its row of the extraction ledger: written `running` before the model is called, so a
367
405
  * pass that dies leaves the attempt behind it; closed `ok` with the model's answer as it came, or `failed` with why.
368
406
  */
369
- async function readBatch(db, { sessionId, batch, attempts, version, now, fallback }) {
407
+ async function readBatch(db, { sessionId, batch, attempts, version, now, at, fallback }) {
370
408
  const bundle = bundleOf(db, sessionId, batch, { now });
371
409
  const ids = batch.map((u) => u.turn.id);
372
410
  const runId = Number(
373
411
  db
374
412
  .prepare(`INSERT INTO extractions (session_id, extractor_version, status, attempts, first_turn, last_turn, started_at) VALUES (?, ?, 'running', ?, ?, ?, ?)`)
375
- .run(sessionId, version, attempts, ids[0], ids.at(-1), now).lastInsertRowid,
413
+ .run(sessionId, version, attempts, ids[0], ids.at(-1), at()).lastInsertRowid,
376
414
  );
377
415
  const close = (status, { ops = null, error = null } = {}) =>
378
- db.prepare('UPDATE extractions SET status = ?, ops_json = ?, error = ?, ended_at = ? WHERE id = ?').run(status, ops === null ? null : JSON.stringify(ops), error, now, runId);
416
+ db.prepare('UPDATE extractions SET status = ?, ops_json = ?, error = ?, ended_at = ? WHERE id = ?').run(status, ops === null ? null : JSON.stringify(ops), error, at(), runId);
379
417
 
380
418
  const asked = await ask(db, { bundle, now, fallback });
381
419
  if (!asked.ok) {
@@ -402,13 +440,13 @@ async function readBatch(db, { sessionId, batch, attempts, version, now, fallbac
402
440
  }
403
441
 
404
442
  /** A turn that failed three times alone is set aside, in the ledger and counted, so the turns after it are read. */
405
- function quarantine(db, { sessionId, turn, attempts, version, now }) {
443
+ function quarantine(db, { sessionId, turn, attempts, version, at }) {
406
444
  const last = db
407
445
  .prepare(`SELECT error FROM extractions WHERE session_id = ? AND extractor_version = ? AND first_turn = ? AND last_turn = ? AND status = 'failed' ORDER BY id DESC LIMIT 1`)
408
446
  .get(sessionId, version, turn.id, turn.id);
409
447
  tx(db, () => {
410
448
  db.prepare(`INSERT INTO extractions (session_id, extractor_version, status, attempts, first_turn, last_turn, error, started_at, ended_at) VALUES (?, ?, 'quarantined', ?, ?, ?, ?, ?, ?)`)
411
- .run(sessionId, version, attempts, turn.id, turn.id, last?.error ?? null, now, now);
449
+ .run(sessionId, version, attempts, turn.id, turn.id, last?.error ?? null, at(), at());
412
450
  markScribed(db, [turn.id]);
413
451
  });
414
452
  return `t${turn.id} was set aside: read alone it failed ${attempts} times — ${last?.error ?? 'no reason kept'}`;
@@ -422,9 +460,10 @@ function quarantine(db, { sessionId, turn, attempts, version, now }) {
422
460
  async function modelAnswers(db, now, fallback) {
423
461
  const turn = { id: 0, session_id: '', ts: now, text: 'hello', project: null };
424
462
  const bundle = bundleOf(db, '', [{ turn, text: turn.text, replies: [] }], { now, related: false });
425
- const asked = await ask(db, { bundle, now, quiet: true, fallback });
426
- // In the model ledger when it read or cost something; a call that reached no model is the failed row already there.
427
- if (asked.usage.inputTokens + asked.usage.outputTokens > 0) {
463
+ const asked = await ask(db, { bundle, now, quiet: true, fallback, said: 'asked whether the model answers at all: it does' });
464
+ // In the model ledger when it read something (a hosted model's call is there already); a call that reached no model
465
+ // is the failed row already there.
466
+ if (asked.model === 'local' && asked.usage.inputTokens + asked.usage.outputTokens > 0) {
428
467
  logRun(db, { kind: 'scribe', model: asked.model, result: asked.ok ? asked.result : { ...asked.result, ok: false }, note: `asked whether the model answers at all: ${asked.ok ? 'it does' : asked.error}`, now });
429
468
  }
430
469
  return { ok: asked.ok, fallbackUsed: asked.fallbackUsed };
@@ -442,14 +481,17 @@ async function modelAnswers(db, now, fallback) {
442
481
  * against the batch, and three such passes in a row pause filing.
443
482
  */
444
483
  export async function runScribe(db, { now = new Date().toISOString(), auto = false } = {}) {
445
- if (writerOff(db)) return { skipped: 'memory.model is off' };
446
-
447
484
  return withLock('scribe', async () => {
485
+ // The lock is ours: a row still `running` belongs to a pass that died, and is one failed attempt — closed before
486
+ // anything can turn this pass away, or a pass that is off or paused would leave it running for ever.
487
+ db.prepare(`UPDATE extractions SET status = 'failed', error = 'the pass that started it died before it finished', ended_at = ? WHERE status = 'running'`).run(now);
488
+ if (writerOff(db)) return { skipped: 'memory.model is off' };
448
489
  const version = extractorVersion(db);
449
- const state = filingState(db, now);
490
+ const state = filingState(db);
450
491
  if (auto && state.paused) return { skipped: `filing is paused: ${state.reason}`, paused: true };
451
- // The lock is ours: a row still `running` belongs to a pass that died, and is one failed attempt.
452
- db.prepare(`UPDATE extractions SET status = 'failed', error = 'the pass that started it died before it finished', ended_at = ? WHERE status = 'running'`).run(now);
492
+ // Each batch's row says when that batch started and ended, on the pass's clock: a pass long at work is not one that died.
493
+ const began = Date.now();
494
+ const at = () => new Date(Date.parse(now) + Date.now() - began).toISOString();
453
495
 
454
496
  const sessions = sessionsAwaitingExtraction(db, { now, limit: MAX_SESSIONS, includeOpen: !auto });
455
497
  if (sessions.length === 0) return { skipped: 'nothing new was said' };
@@ -465,7 +507,7 @@ export async function runScribe(db, { now = new Date().toISOString(), auto = fal
465
507
  const next = nextBatch(db, session.id, version);
466
508
  if (!next) break;
467
509
  if (next.quarantine) {
468
- pass.notes.push(quarantine(db, { sessionId: session.id, turn: next.batch[0].turn, attempts: next.attempts, version, now }));
510
+ pass.notes.push(quarantine(db, { sessionId: session.id, turn: next.batch[0].turn, attempts: next.attempts, version, at }));
469
511
  continue;
470
512
  }
471
513
  if (!mayPay()) {
@@ -473,7 +515,7 @@ export async function runScribe(db, { now = new Date().toISOString(), auto = fal
473
515
  break reading;
474
516
  }
475
517
  calls++;
476
- const outcome = await readBatch(db, { sessionId: session.id, batch: next.batch, attempts: next.attempts, version, now, fallback: true });
518
+ const outcome = await readBatch(db, { sessionId: session.id, batch: next.batch, attempts: next.attempts, version, now, at, fallback: true });
477
519
  if (outcome.fallbackUsed) paid++;
478
520
  pass.usage = added(pass.usage, outcome.usage);
479
521
  pass.fallbackUsed ||= outcome.fallbackUsed;
@@ -519,9 +561,11 @@ export async function runScribe(db, { now = new Date().toISOString(), auto = fal
519
561
  * memory stands. `{ ok, error, wouldFile, dropped, batches }` — `wouldFile` as `applyOps` gives `filed`, `dropped`
520
562
  * the proposals the rules refused; `batches` what was read, as `{ sessionId, turnIds, ops }`, for whoever then applies it (rebuild.mjs).
521
563
  * The model is shown the conversation and no memory, so the answer is the conversation's alone. Only what the user
522
- * stated is asked of it here: a trap or a checkpoint drawn from a reply cites no turn to be rebuilt from.
564
+ * stated is asked of it here: a trap or a checkpoint drawn from a reply cites no turn to be rebuilt from. `fallback:
565
+ * false` asks the model as set alone, never the hosted fallback: for a run the user did not ask for. A hosted call made
566
+ * is in the ledger of model runs.
523
567
  */
524
- export async function extract(db, { turnIds, now = new Date().toISOString() }) {
568
+ export async function extract(db, { turnIds, now = new Date().toISOString(), fallback = true }) {
525
569
  const out = { ok: true, error: null, wouldFile: [], dropped: [], batches: [], usage: NO_USAGE };
526
570
  if (writerOff(db)) return { ...out, ok: false, error: 'memory.model is off' };
527
571
  const wanted = [...new Set(turnIds)].filter(Number.isInteger);
@@ -536,7 +580,7 @@ export async function extract(db, { turnIds, now = new Date().toISOString() }) {
536
580
  const batch = naturalBatch(rest);
537
581
  rest = rest.slice(batch.length);
538
582
  const bundle = bundleOf(db, sessionId, batch, { now, related: false });
539
- const asked = await ask(db, { bundle, now, quiet: true });
583
+ const asked = await ask(db, { bundle, now, quiet: true, fallback });
540
584
  out.usage = added(out.usage, asked.usage);
541
585
  if (!asked.ok) {
542
586
  out.ok = false;
package/src/sessions.mjs CHANGED
@@ -17,7 +17,7 @@ import { readBackwards } from './transcript.mjs';
17
17
  const JOURNAL_MAX_CHARS = 64_000;
18
18
  const CUT = ' …[cut]';
19
19
  /** A conversation nobody has spoken in for this long is over, as far as reading it for memory goes. */
20
- const FINISHED_AFTER_MS = 30 * 60_000;
20
+ export const FINISHED_AFTER_MS = 30 * 60_000;
21
21
 
22
22
  /**
23
23
  * Where a session starts costing quality. The numbers are absolute tokens, not
@@ -570,8 +570,9 @@ function summaryOf(file) {
570
570
  }
571
571
 
572
572
  /**
573
- * The sessions that can be picked up, newest first — every one still on disk, the heavy ones marked. `except` is the
574
- * session asking, which cannot resume itself.
573
+ * The sessions that can be picked up, newest first — every one still on disk that a conversation in the journal stands
574
+ * behind, the heavy ones marked. Newest is by the journal's own time, the last thing said in it, never by what a file
575
+ * says of itself: a file is not the hand that wrote it. `except` is the session asking, which cannot resume itself.
575
576
  */
576
577
  export function savedSessions(db, { now, except = null } = {}) {
577
578
  let names;
@@ -583,19 +584,20 @@ export function savedSessions(db, { now, except = null } = {}) {
583
584
  }
584
585
  const firstTurn = db.prepare('SELECT text FROM user_turns WHERE session_id = ? ORDER BY id LIMIT 1');
585
586
  const turns = db.prepare('SELECT COUNT(*) AS n FROM user_turns WHERE session_id = ?');
586
- const row = db.prepare('SELECT last_turn_at FROM sessions WHERE id = ?');
587
+ const row = db.prepare('SELECT COALESCE(last_turn_at, started_at) AS at FROM sessions WHERE id = ?');
587
588
  return names
588
589
  .filter((name) => name.endsWith('.state.json'))
589
590
  .map((name) => name.slice(0, -'.state.json'.length))
590
591
  .filter((id) => id !== except)
591
592
  .map((id) => {
592
- const summary = summaryOf(stateFile(id));
593
+ const heard = row.get(id)?.at;
594
+ const summary = heard ? summaryOf(stateFile(id)) : null;
593
595
  if (!summary?.savedAt) return null;
594
596
  const tokens = summary.contextTokens ?? 0;
595
597
  return {
596
598
  id,
597
599
  savedAt: summary.savedAt,
598
- lastTurnAt: row.get(id)?.last_turn_at ?? summary.savedAt,
600
+ lastTurnAt: heard,
599
601
  cwd: summary.cwd ?? null,
600
602
  project: currentProject(db, id),
601
603
  title: firstTurn.get(id)?.text ?? '',
@@ -606,7 +608,7 @@ export function savedSessions(db, { now, except = null } = {}) {
606
608
  };
607
609
  })
608
610
  .filter(Boolean)
609
- .sort((a, b) => (a.savedAt < b.savedAt ? 1 : a.savedAt > b.savedAt ? -1 : 0));
611
+ .sort((a, b) => (a.lastTurnAt < b.lastTurnAt ? 1 : a.lastTurnAt > b.lastTurnAt ? -1 : 0));
610
612
  }
611
613
 
612
614
  /** The saved session `ref` names: `last` for the newest, otherwise the one id that starts with it. Anything else is said. */