sumo-agents 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +38 -0
- package/LICENSE +21 -0
- package/README.md +111 -2
- package/bin/sumo.mjs +33 -0
- package/guides/delegation.md +33 -0
- package/guides/feature.md +14 -0
- package/guides/fix.md +14 -0
- package/guides/memory.md +23 -0
- package/guides/projects.md +27 -0
- package/guides/review.md +16 -0
- package/guides/workflows.md +26 -0
- package/logo.txt +19 -0
- package/package.json +42 -4
- package/prompts/agent.md +7 -0
- package/prompts/dream.md +13 -0
- package/prompts/scribe.md +21 -0
- package/spinner.txt +90 -0
- package/src/advisor.mjs +59 -0
- package/src/apply.mjs +368 -0
- package/src/auth.mjs +80 -0
- package/src/card.mjs +72 -0
- package/src/catalog.mjs +167 -0
- package/src/chat.mjs +1162 -0
- package/src/cli.mjs +815 -0
- package/src/cost.mjs +128 -0
- package/src/db.mjs +384 -0
- package/src/dream.mjs +97 -0
- package/src/editor.mjs +198 -0
- package/src/export.mjs +132 -0
- package/src/guard.mjs +121 -0
- package/src/guides.mjs +86 -0
- package/src/herdr.mjs +117 -0
- package/src/hooks.mjs +270 -0
- package/src/images.mjs +89 -0
- package/src/jobs.mjs +1184 -0
- package/src/jobtools.mjs +82 -0
- package/src/local-server.mjs +169 -0
- package/src/lock.mjs +45 -0
- package/src/loop.mjs +838 -0
- package/src/mcp.mjs +604 -0
- package/src/memory.mjs +400 -0
- package/src/model.mjs +270 -0
- package/src/page.html +304 -0
- package/src/page.mjs +135 -0
- package/src/paths.mjs +33 -0
- package/src/prime.mjs +177 -0
- package/src/projects.mjs +232 -0
- package/src/recall.mjs +45 -0
- package/src/redact.mjs +173 -0
- package/src/render.mjs +45 -0
- package/src/route.mjs +287 -0
- package/src/sandbox.mjs +173 -0
- package/src/scan.mjs +204 -0
- package/src/scribe.mjs +349 -0
- package/src/sessions.mjs +371 -0
- package/src/setup.mjs +528 -0
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +97 -0
- package/src/tools.mjs +987 -0
- package/src/transcript.mjs +106 -0
- package/src/tty.mjs +529 -0
- package/src/ui.mjs +1363 -0
- package/src/verify.mjs +369 -0
- package/src/web.mjs +49 -0
- package/src/workflows.mjs +270 -0
- package/src/worktree.mjs +128 -0
package/src/scribe.mjs
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { appendFileSync, closeSync, openSync, readFileSync, rmSync, statSync, writeSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { applyOps } from './apply.mjs';
|
|
5
|
+
import { resolveAnthropicCredential } from './auth.mjs';
|
|
6
|
+
import { ago } from './card.mjs';
|
|
7
|
+
import { MODELS, offLine, usableModels } from './catalog.mjs';
|
|
8
|
+
import { getMeta, openDb, setMeta, tx } from './db.mjs';
|
|
9
|
+
import { localServerStatus } from './local-server.mjs';
|
|
10
|
+
import { callModel, failure, logRun } from './model.mjs';
|
|
11
|
+
import { paths, REPO_ROOT, SELF } from './paths.mjs';
|
|
12
|
+
import { aliasesOf, listProjects } from './projects.mjs';
|
|
13
|
+
import { line } from './render.mjs';
|
|
14
|
+
import { currentProject, markScribed, pendingTurns } from './sessions.mjs';
|
|
15
|
+
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
16
|
+
import { ftsQuery, looksDurable } from './text.mjs';
|
|
17
|
+
import { assistantReplies } from './transcript.mjs';
|
|
18
|
+
|
|
19
|
+
const LOCK_STALE_MS = 5 * 60_000;
|
|
20
|
+
const QUIET_MS = 5 * 60_000;
|
|
21
|
+
const TURNS_TO_RUN = 3;
|
|
22
|
+
const MAX_REPLIES_PER_SESSION = 6;
|
|
23
|
+
const MAX_RELATED = 10;
|
|
24
|
+
/**
|
|
25
|
+
* How much of the user's words one run reads, so a batch always fits the local model's 32k context with room to think
|
|
26
|
+
* and answer. Counted in characters, which a pasted log turns into more tokens than prose does — the room is left for
|
|
27
|
+
* that. Thirty stored turns of 4,000 characters each would not fit; what does not fit waits for the next run.
|
|
28
|
+
*/
|
|
29
|
+
const BATCH_CHARS = 40_000;
|
|
30
|
+
|
|
31
|
+
/** The pending turns one run reads: the oldest first, as many as fit, and always at least one. */
|
|
32
|
+
function batchOf(turns) {
|
|
33
|
+
const batch = [];
|
|
34
|
+
let used = 0;
|
|
35
|
+
for (const t of turns) {
|
|
36
|
+
if (batch.length > 0 && used + t.text.length > BATCH_CHARS) break;
|
|
37
|
+
batch.push(t);
|
|
38
|
+
used += t.text.length;
|
|
39
|
+
}
|
|
40
|
+
return batch;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether the writer should run now. It is batched on purpose: each call
|
|
45
|
+
* carries a few thousand tokens of fixed overhead, so one call per message
|
|
46
|
+
* would cost many times more for the same memories.
|
|
47
|
+
*/
|
|
48
|
+
export function shouldRun(db, { event, now }) {
|
|
49
|
+
const pending = pendingTurns(db);
|
|
50
|
+
if (pending.length === 0) return false;
|
|
51
|
+
if (event !== 'stop') return true; // session start and end always sweep up what is left
|
|
52
|
+
if (pending.some((t) => looksDurable(t.text)) || pending.length >= TURNS_TO_RUN) return true;
|
|
53
|
+
const last = getMeta(db, 'scribe.last_run');
|
|
54
|
+
return !last || Date.parse(now) - Date.parse(last) >= QUIET_MS;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Starts `sumo <args>` and lets go of it, so the hook that asked returns at once.
|
|
59
|
+
* SUMO_AGENTS_SPAWN_LOG records the request instead of acting on it, which is
|
|
60
|
+
* how the tests check *that* a run was asked for without racing a real one.
|
|
61
|
+
*/
|
|
62
|
+
export function spawnDetached(args) {
|
|
63
|
+
if (process.env.SUMO_AGENTS_SPAWN_LOG) {
|
|
64
|
+
appendFileSync(process.env.SUMO_AGENTS_SPAWN_LOG, `${args.join(' ')}\n`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const child = spawn(SELF[0], [...SELF.slice(1), ...args], {
|
|
68
|
+
detached: true,
|
|
69
|
+
stdio: 'ignore',
|
|
70
|
+
env: process.env,
|
|
71
|
+
});
|
|
72
|
+
// A run that cannot be started is that run's loss: left unheard, the failure would end the process that asked for it.
|
|
73
|
+
child.on('error', () => {});
|
|
74
|
+
child.unref();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Whether the run that took a lock is still going. Its pid says so; a model call that is retried can outlast any
|
|
79
|
+
* fixed age. Only a lock that died before it named a pid is judged by its age.
|
|
80
|
+
*/
|
|
81
|
+
function held(file) {
|
|
82
|
+
const pid = Number(readFileSync(file, 'utf8'));
|
|
83
|
+
if (!Number.isInteger(pid) || pid <= 0) return Date.now() - statSync(file).mtimeMs <= LOCK_STALE_MS;
|
|
84
|
+
try {
|
|
85
|
+
process.kill(pid, 0);
|
|
86
|
+
return true;
|
|
87
|
+
} catch (cause) {
|
|
88
|
+
return cause.code === 'EPERM'; // alive, and someone else's
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** One writer at a time. A lock whose run has died is taken over. */
|
|
93
|
+
export async function withLock(name, fn) {
|
|
94
|
+
const file = join(paths().home, `${name}.lock`);
|
|
95
|
+
let fd;
|
|
96
|
+
// Serialize observation, replacement and PID publication so a paused owner cannot lose its fresh lock.
|
|
97
|
+
const db = openDb();
|
|
98
|
+
try {
|
|
99
|
+
fd = tx(db, () => {
|
|
100
|
+
try {
|
|
101
|
+
if (!held(file)) rmSync(file);
|
|
102
|
+
} catch (cause) {
|
|
103
|
+
if (cause.code !== 'ENOENT') throw cause;
|
|
104
|
+
}
|
|
105
|
+
let acquired;
|
|
106
|
+
try {
|
|
107
|
+
acquired = openSync(file, 'wx', 0o600);
|
|
108
|
+
} catch (cause) {
|
|
109
|
+
if (cause.code !== 'EEXIST') throw cause;
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
writeSync(acquired, String(process.pid));
|
|
114
|
+
} catch (cause) {
|
|
115
|
+
closeSync(acquired);
|
|
116
|
+
rmSync(file);
|
|
117
|
+
throw cause;
|
|
118
|
+
}
|
|
119
|
+
return acquired;
|
|
120
|
+
});
|
|
121
|
+
} finally {
|
|
122
|
+
db.close();
|
|
123
|
+
}
|
|
124
|
+
if (fd === undefined) return { skipped: 'another run is in progress' };
|
|
125
|
+
try {
|
|
126
|
+
return await fn();
|
|
127
|
+
} finally {
|
|
128
|
+
closeSync(fd);
|
|
129
|
+
// Only while it is still ours: a lock taken over meanwhile belongs to the run that took it.
|
|
130
|
+
try {
|
|
131
|
+
if (readFileSync(file, 'utf8') === String(process.pid)) rmSync(file);
|
|
132
|
+
} catch {
|
|
133
|
+
// Already gone.
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function projectEnum(db) {
|
|
139
|
+
return listProjects(db, { includeArchived: true }).map((p) => p.slug);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function knownProjectsLine(db) {
|
|
143
|
+
const all = listProjects(db);
|
|
144
|
+
if (all.length === 0) return 'Known projects: none';
|
|
145
|
+
return `Known projects: ${all.map((p) => [p.slug, ...aliasesOf(db, p.slug)].join(' / ')).join(' · ')}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The JSON shape the model must answer in. Each operation is a closed branch, avoiding an exponential set of optional fields. */
|
|
149
|
+
export function opsSchema(db, ops) {
|
|
150
|
+
const slugs = projectEnum(db);
|
|
151
|
+
const field = {
|
|
152
|
+
type: { type: 'string', enum: ['preference', 'fact', 'decision'] },
|
|
153
|
+
scope: { type: 'string', enum: ['global', ...slugs.map((s) => `project:${s}`)] },
|
|
154
|
+
topic: { type: 'string', description: 'one lowercase word: git, testing, style, writing, tooling, deploy…' },
|
|
155
|
+
body: { type: 'string', description: 'one plain sentence, written as a standing fact or rule' },
|
|
156
|
+
turn: { type: ['integer', 'null'], description: 'the number from the [tN] tag, or null when this is an inferred pattern' },
|
|
157
|
+
quote: { type: ['string', 'null'], description: 'an exact run of words from that user turn, or null when this is an inferred pattern' },
|
|
158
|
+
// Required like the rest, and may be empty: an optional field is a combination more for the grammar to compile.
|
|
159
|
+
keys: { type: 'array', items: { type: 'string' }, description: 'up to 6 short lowercase search terms it is about that are not words of body, or []' },
|
|
160
|
+
};
|
|
161
|
+
const branch = (op, properties) => {
|
|
162
|
+
const all = { op: { type: 'string', enum: [op] }, ...properties };
|
|
163
|
+
return { type: 'object', properties: all, required: Object.keys(all), additionalProperties: false };
|
|
164
|
+
};
|
|
165
|
+
const branches = {
|
|
166
|
+
add: branch('add', { type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote, keys: field.keys }),
|
|
167
|
+
supersede: branch('supersede', {
|
|
168
|
+
old: { type: 'integer', description: 'the id of the existing memory this replaces' },
|
|
169
|
+
type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote, keys: field.keys,
|
|
170
|
+
}),
|
|
171
|
+
gotcha: branch('gotcha', { scope: field.scope, body: field.body, keys: field.keys }),
|
|
172
|
+
checkpoint: slugs.length > 0
|
|
173
|
+
? branch('checkpoint', { project: { type: 'string', enum: slugs }, done: { type: 'string' }, next: { type: ['string', 'null'] } })
|
|
174
|
+
: null,
|
|
175
|
+
contradiction: branch('contradiction', { ids: { type: 'array', items: { type: 'integer' } }, note: { type: 'string' } }),
|
|
176
|
+
procedure: branch('procedure', { scope: field.scope, title: { type: 'string' }, cue: { type: 'string' }, body: field.body }),
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
type: 'object',
|
|
180
|
+
properties: { ops: { type: 'array', items: { anyOf: ops.map((op) => branches[op]).filter(Boolean) } } },
|
|
181
|
+
required: ['ops'],
|
|
182
|
+
additionalProperties: false,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function relatedMemories(db, texts, scopes) {
|
|
187
|
+
const seen = new Map();
|
|
188
|
+
for (const text of texts) {
|
|
189
|
+
const match = ftsQuery(text);
|
|
190
|
+
if (!match) continue;
|
|
191
|
+
const rows = db
|
|
192
|
+
.prepare(
|
|
193
|
+
`SELECT m.* FROM memories_fts JOIN memories m ON m.id = memories_fts.rowid
|
|
194
|
+
WHERE memories_fts MATCH ? AND m.state = 'active' AND m.scan_key IS NULL
|
|
195
|
+
AND m.scope IN (${scopes.map(() => '?').join(', ')})
|
|
196
|
+
ORDER BY bm25(memories_fts) LIMIT 3`,
|
|
197
|
+
)
|
|
198
|
+
.all(match, ...scopes);
|
|
199
|
+
for (const row of rows) seen.set(row.id, row);
|
|
200
|
+
}
|
|
201
|
+
return [...seen.values()].slice(0, MAX_RELATED);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** What the writer is shown: the user's words, the assistant's replies around them, and nothing else. */
|
|
205
|
+
export function buildBundle(db) {
|
|
206
|
+
const turns = batchOf(pendingTurns(db));
|
|
207
|
+
if (turns.length === 0) return null;
|
|
208
|
+
|
|
209
|
+
const bySession = new Map();
|
|
210
|
+
for (const t of turns) bySession.set(t.session_id, [...(bySession.get(t.session_id) ?? []), t]);
|
|
211
|
+
|
|
212
|
+
const scopes = new Set(['global']);
|
|
213
|
+
const sections = [];
|
|
214
|
+
for (const [sessionId, sessionTurns] of bySession) {
|
|
215
|
+
const project = currentProject(db, sessionId);
|
|
216
|
+
if (project) scopes.add(`project:${project}`);
|
|
217
|
+
const session = db.prepare('SELECT transcript_path FROM sessions WHERE id = ?').get(sessionId);
|
|
218
|
+
const replies = session?.transcript_path ? assistantReplies(session.transcript_path, sessionTurns[0].ts, MAX_REPLIES_PER_SESSION).slice(-MAX_REPLIES_PER_SESSION) : [];
|
|
219
|
+
|
|
220
|
+
const events = [
|
|
221
|
+
...sessionTurns.map((t) => ({ ts: t.ts, text: `[t${t.id}] user: ${t.text}` })),
|
|
222
|
+
...replies.map((r) => ({ ts: r.ts, text: `[assistant]: ${r.text}` })),
|
|
223
|
+
].sort((a, b) => a.ts.localeCompare(b.ts));
|
|
224
|
+
|
|
225
|
+
sections.push(
|
|
226
|
+
`--- session${project ? ` (the conversation is about the project "${project}": statements that are not clearly general belong to project:${project})` : ' (no project in focus: scope is global unless a project is named)'}`,
|
|
227
|
+
...events.map((e) => e.text),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const related = relatedMemories(db, turns.map((t) => t.text), [...scopes]);
|
|
232
|
+
const prompt = [
|
|
233
|
+
knownProjectsLine(db),
|
|
234
|
+
related.length > 0 ? `Existing memories that may be related (use supersede, not add, when one of these is being replaced):\n${related.map(line).join('\n')}` : 'Existing related memories: none',
|
|
235
|
+
...sections,
|
|
236
|
+
].join('\n');
|
|
237
|
+
|
|
238
|
+
return { prompt, turns: new Map(turns.map((t) => [t.id, t])) };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The API model a pass is retried on when the local one could not answer: the cheapest one that is on, or none. */
|
|
242
|
+
const fallbackModel = (db) => usableModels(db)[0] ?? null;
|
|
243
|
+
|
|
244
|
+
/** Shared by the writer and the consolidation pass: ask, validate, record what it cost. */
|
|
245
|
+
export async function askAndApply(db, { kind, promptFile, bundle, ops, sessionId, now }) {
|
|
246
|
+
let model = getMeta(db, `config.${kind}.model`) ?? CONFIG_DEFAULTS[`${kind}.model`];
|
|
247
|
+
const system = readFileSync(join(REPO_ROOT, 'prompts', promptFile), 'utf8').trim();
|
|
248
|
+
const schema = opsSchema(db, ops);
|
|
249
|
+
// 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.
|
|
250
|
+
const off = MODELS.includes(model) ? offLine(db, model, `sumo config ${kind}.model local`) : null;
|
|
251
|
+
let result = off ? failure(off) : await callModel(db, { system, prompt: bundle.prompt, schema, model, kind });
|
|
252
|
+
// A local answer that failed (server down, bundle past its context, answer cut off) is one failed row, then one more try —
|
|
253
|
+
// when a credential is there to pay for it and a model is on to take it.
|
|
254
|
+
const fallback = !result.ok && model === 'local' && resolveAnthropicCredential() ? fallbackModel(db) : null;
|
|
255
|
+
if (fallback) {
|
|
256
|
+
logRun(db, { kind, model, result, note: `${result.error} — retrying on ${fallback}`, now });
|
|
257
|
+
model = fallback;
|
|
258
|
+
result = await callModel(db, { system, prompt: bundle.prompt, schema, model, kind });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (!result.ok) {
|
|
262
|
+
logRun(db, { kind, model, result, note: result.error, now });
|
|
263
|
+
return { ok: false, error: result.error, applied: [], dropped: [], usage: result.usage };
|
|
264
|
+
}
|
|
265
|
+
// An answer in the wrong shape is a failed call, not an empty one: what it was shown must be read again.
|
|
266
|
+
if (!Array.isArray(result.data?.ops)) {
|
|
267
|
+
const error = 'the answer had no list of operations';
|
|
268
|
+
logRun(db, { kind, model, result: { ...result, ok: false }, note: error, now });
|
|
269
|
+
return { ok: false, error, applied: [], dropped: [], usage: result.usage };
|
|
270
|
+
}
|
|
271
|
+
const outcome = applyOps(db, result.data.ops, { source: kind, turns: bundle.turns, sessionId, now });
|
|
272
|
+
logRun(db, { kind, model, result, note: `applied=${outcome.applied.length} dropped=${outcome.dropped.length}`, now });
|
|
273
|
+
for (const reason of outcome.dropped) appendFileSync(join(paths().logs, 'scribe.log'), `${now} dropped — ${reason}\n`);
|
|
274
|
+
return { ok: true, error: null, ...outcome, usage: result.usage };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Whether the user turned the writer off: nothing gets filed, so nothing should wait for it to be. */
|
|
278
|
+
export const writerOff = (db) => (getMeta(db, 'config.scribe.model') ?? CONFIG_DEFAULTS['scribe.model']) === 'off';
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Files what was said since the last run. Turns are marked done only after the
|
|
282
|
+
* answer was applied, so a failed call costs nothing but a retry.
|
|
283
|
+
*/
|
|
284
|
+
export async function runScribe(db, { now = new Date().toISOString() } = {}) {
|
|
285
|
+
if (writerOff(db)) return { skipped: 'scribe.model is off' };
|
|
286
|
+
|
|
287
|
+
return withLock('scribe', async () => {
|
|
288
|
+
const bundle = buildBundle(db);
|
|
289
|
+
if (!bundle) return { skipped: 'nothing new was said' };
|
|
290
|
+
|
|
291
|
+
const sessions = new Set([...bundle.turns.values()].map((t) => t.session_id));
|
|
292
|
+
const outcome = await askAndApply(db, {
|
|
293
|
+
kind: 'scribe', promptFile: 'scribe.md', bundle, ops: ['add', 'supersede', 'gotcha', 'checkpoint'],
|
|
294
|
+
sessionId: sessions.size === 1 ? [...sessions][0] : null, now,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
setMeta(db, 'scribe.last_run', now);
|
|
298
|
+
if (outcome.ok) {
|
|
299
|
+
markScribed(db, [...bundle.turns.keys()]);
|
|
300
|
+
setMeta(db, 'scribe.failures', 0);
|
|
301
|
+
} else {
|
|
302
|
+
setMeta(db, 'scribe.failures', Number(getMeta(db, 'scribe.failures') ?? 0) + 1);
|
|
303
|
+
setMeta(db, 'scribe.last_error', outcome.error);
|
|
304
|
+
}
|
|
305
|
+
return { ...outcome, turns: bundle.turns.size };
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export async function scribeStatus(db) {
|
|
310
|
+
const failures = Number(getMeta(db, 'scribe.failures') ?? 0);
|
|
311
|
+
const last = getMeta(db, 'scribe.last_run');
|
|
312
|
+
const model = getMeta(db, 'config.scribe.model') ?? CONFIG_DEFAULTS['scribe.model'];
|
|
313
|
+
return [
|
|
314
|
+
`model: ${model}`,
|
|
315
|
+
...(model === 'local' ? [await localServerStatus(db, { file: getMeta(db, 'config.model.file') ?? CONFIG_DEFAULTS['model.file'] })] : []),
|
|
316
|
+
`turns waiting: ${pendingTurns(db, 1000).length}`,
|
|
317
|
+
`last run: ${last ? `${last} (${ago(last, new Date().toISOString())})` : 'never'}`,
|
|
318
|
+
failures > 0 ? `failing: ${failures} in a row — ${getMeta(db, 'scribe.last_error')}` : 'healthy',
|
|
319
|
+
];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function modelStats(db) {
|
|
323
|
+
const rows = db
|
|
324
|
+
.prepare(
|
|
325
|
+
`SELECT kind, COUNT(*) AS runs, SUM(ok) AS ok, SUM(input_tokens) AS input, SUM(output_tokens) AS output, SUM(cost_usd) AS cost,
|
|
326
|
+
SUM(cache_read_tokens) AS cache_read, SUM(cache_creation_tokens) AS cache_write
|
|
327
|
+
FROM model_runs GROUP BY kind ORDER BY kind`,
|
|
328
|
+
)
|
|
329
|
+
.all();
|
|
330
|
+
if (rows.length === 0) return ['no cheap-model runs yet'];
|
|
331
|
+
// Token counts run to millions: grouped by thousands, they read at a glance.
|
|
332
|
+
const n = (count) => (count ?? 0).toLocaleString('en-US');
|
|
333
|
+
return rows.map(
|
|
334
|
+
(r) => `${r.kind}: ${r.runs} run${r.runs === 1 ? '' : 's'} (${r.ok} ok) · ${n(r.input)} tokens in · ${n(r.output)} out · $${(r.cost ?? 0).toFixed(4)} · cache ${n(r.cache_read)} read · ${n(r.cache_write)} written`,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** What a pass did, as a person reads it: how much it read and cost, and each thing it filed or dropped. */
|
|
339
|
+
export function outcomeLines(outcome) {
|
|
340
|
+
if (outcome.skipped) return [`nothing to do — ${outcome.skipped}`];
|
|
341
|
+
if (!outcome.ok) return [`failed — ${outcome.error}`];
|
|
342
|
+
const cost = `${outcome.usage.inputTokens.toLocaleString('en-US')} tokens in, ${outcome.usage.outputTokens.toLocaleString('en-US')} out, $${outcome.usage.costUsd.toFixed(4)}`;
|
|
343
|
+
return [
|
|
344
|
+
`read ${outcome.turns ?? outcome.sessions} ${outcome.turns === undefined ? 'sessions' : 'turns'} (${cost})`,
|
|
345
|
+
...outcome.applied.map((l) => ` ${l}`),
|
|
346
|
+
...outcome.dropped.map((l) => ` dropped — ${l}`),
|
|
347
|
+
...(outcome.applied.length + outcome.dropped.length === 0 ? [' nothing worth remembering'] : []),
|
|
348
|
+
];
|
|
349
|
+
}
|