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/CHANGELOG.md +124 -0
- package/README.md +46 -21
- package/package.json +2 -1
- package/prompts/scribe.md +1 -1
- package/src/apply.mjs +16 -12
- package/src/auth.mjs +2 -2
- package/src/card.mjs +3 -2
- package/src/catalog.mjs +100 -13
- package/src/chat.mjs +111 -31
- package/src/check.mjs +17 -15
- package/src/cli.mjs +21 -11
- package/src/cost.mjs +2 -4
- package/src/cursor.mjs +405 -0
- package/src/db.mjs +24 -13
- package/src/dream.mjs +2 -2
- package/src/jobs.mjs +72 -34
- package/src/ledger.mjs +4 -3
- package/src/loop.mjs +157 -77
- package/src/mcp.mjs +7 -9
- package/src/memeval.mjs +2 -3
- package/src/memory.mjs +5 -4
- package/src/model.mjs +5 -0
- package/src/page.mjs +1 -0
- package/src/prime.mjs +11 -7
- package/src/rebuild.mjs +8 -3
- package/src/redact.mjs +13 -1
- package/src/route.mjs +263 -56
- package/src/sandbox.mjs +86 -20
- package/src/scan.mjs +4 -2
- package/src/scorecard.mjs +419 -0
- package/src/scribe.mjs +82 -38
- package/src/sessions.mjs +29 -21
- package/src/settings.mjs +114 -37
- package/src/setup.mjs +33 -14
- package/src/spool.mjs +68 -43
- package/src/tools.mjs +220 -98
- package/src/ui.mjs +14 -5
- package/src/verify.mjs +116 -15
- package/src/worktree.mjs +93 -9
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 {
|
|
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
|
|
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
|
|
75
|
-
*
|
|
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
|
|
79
|
-
|
|
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
|
-
|
|
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 =
|
|
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,
|
|
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
|
-
|
|
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') ===
|
|
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
|
|
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),
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
|
427
|
-
|
|
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
|
|
490
|
+
const state = filingState(db);
|
|
450
491
|
if (auto && state.paused) return { skipped: `filing is paused: ${state.reason}`, paused: true };
|
|
451
|
-
//
|
|
452
|
-
|
|
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,
|
|
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
|
|
@@ -489,23 +489,29 @@ const redactValues = (value) => {
|
|
|
489
489
|
return value;
|
|
490
490
|
};
|
|
491
491
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (
|
|
499
|
-
|
|
500
|
-
if (
|
|
501
|
-
if (
|
|
502
|
-
|
|
492
|
+
// Identifiers pair tool calls and results. The API needs encrypted web results and citation indices byte-for-byte.
|
|
493
|
+
const PROTOCOL_FIELDS = new Set(['type', 'name', 'id', 'tool_use_id', 'encrypted_content', 'encrypted_index', 'encrypted_stdout']);
|
|
494
|
+
|
|
495
|
+
/** Keep the API block structure for resume while scrubbing its human-readable contents, including nested web results. */
|
|
496
|
+
function keepBlock(value) {
|
|
497
|
+
if (typeof value === 'string') return redact(value).text;
|
|
498
|
+
if (Array.isArray(value)) return value.map(keepBlock);
|
|
499
|
+
if (!value || typeof value !== 'object') return value;
|
|
500
|
+
if (value.type === 'image') return { type: 'text', text: NO_PICTURE };
|
|
501
|
+
if (value.type === 'base64') return { type: 'text', media_type: 'text/plain', data: '[binary attachment omitted]' };
|
|
502
|
+
const kept = {};
|
|
503
|
+
for (const [key, child] of Object.entries(value)) {
|
|
504
|
+
if (key === 'cache_control') continue;
|
|
505
|
+
// Tool inputs contain arbitrary user data, even under keys that look like protocol fields.
|
|
506
|
+
if (key === 'input') kept[key] = redactValues(child);
|
|
507
|
+
else kept[key] = PROTOCOL_FIELDS.has(key) ? child : keepBlock(child);
|
|
508
|
+
}
|
|
503
509
|
return kept;
|
|
504
510
|
}
|
|
505
511
|
|
|
506
|
-
/**
|
|
512
|
+
/** What the session can replay after resuming. Thinking signatures cannot survive redaction or a changed prefix. */
|
|
507
513
|
function keepable(messages) {
|
|
508
|
-
return messages.map((m) => ({ role: m.role, content:
|
|
514
|
+
return withoutThinking(messages).map((m) => ({ role: m.role, content: keepBlock(m.content) }));
|
|
509
515
|
}
|
|
510
516
|
|
|
511
517
|
/**
|
|
@@ -526,7 +532,7 @@ export function saveState({ id, messages, model, effort, routed, cwd, contextTok
|
|
|
526
532
|
const file = stateFile(id);
|
|
527
533
|
// The summary first, so a listing can read it without the messages behind it; the route's reason last, since it is prose of any length.
|
|
528
534
|
// `pending` is what jobs reported that the model has not read yet: it goes with the session, to be read when it is picked up again.
|
|
529
|
-
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed, pending };
|
|
535
|
+
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed: redactValues(routed), pending: redactValues(pending) };
|
|
530
536
|
writeFileSync(`${file}.tmp`, JSON.stringify(state), { mode: 0o600 });
|
|
531
537
|
renameSync(`${file}.tmp`, file);
|
|
532
538
|
return file;
|
|
@@ -564,8 +570,9 @@ function summaryOf(file) {
|
|
|
564
570
|
}
|
|
565
571
|
|
|
566
572
|
/**
|
|
567
|
-
* The sessions that can be picked up, newest first — every one still on disk
|
|
568
|
-
*
|
|
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.
|
|
569
576
|
*/
|
|
570
577
|
export function savedSessions(db, { now, except = null } = {}) {
|
|
571
578
|
let names;
|
|
@@ -577,19 +584,20 @@ export function savedSessions(db, { now, except = null } = {}) {
|
|
|
577
584
|
}
|
|
578
585
|
const firstTurn = db.prepare('SELECT text FROM user_turns WHERE session_id = ? ORDER BY id LIMIT 1');
|
|
579
586
|
const turns = db.prepare('SELECT COUNT(*) AS n FROM user_turns WHERE session_id = ?');
|
|
580
|
-
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 = ?');
|
|
581
588
|
return names
|
|
582
589
|
.filter((name) => name.endsWith('.state.json'))
|
|
583
590
|
.map((name) => name.slice(0, -'.state.json'.length))
|
|
584
591
|
.filter((id) => id !== except)
|
|
585
592
|
.map((id) => {
|
|
586
|
-
const
|
|
593
|
+
const heard = row.get(id)?.at;
|
|
594
|
+
const summary = heard ? summaryOf(stateFile(id)) : null;
|
|
587
595
|
if (!summary?.savedAt) return null;
|
|
588
596
|
const tokens = summary.contextTokens ?? 0;
|
|
589
597
|
return {
|
|
590
598
|
id,
|
|
591
599
|
savedAt: summary.savedAt,
|
|
592
|
-
lastTurnAt:
|
|
600
|
+
lastTurnAt: heard,
|
|
593
601
|
cwd: summary.cwd ?? null,
|
|
594
602
|
project: currentProject(db, id),
|
|
595
603
|
title: firstTurn.get(id)?.text ?? '',
|
|
@@ -600,7 +608,7 @@ export function savedSessions(db, { now, except = null } = {}) {
|
|
|
600
608
|
};
|
|
601
609
|
})
|
|
602
610
|
.filter(Boolean)
|
|
603
|
-
.sort((a, b) => (a.
|
|
611
|
+
.sort((a, b) => (a.lastTurnAt < b.lastTurnAt ? 1 : a.lastTurnAt > b.lastTurnAt ? -1 : 0));
|
|
604
612
|
}
|
|
605
613
|
|
|
606
614
|
/** The saved session `ref` names: `last` for the newest, otherwise the one id that starts with it. Anything else is said. */
|