sumo-agents 0.6.1 → 0.7.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 +54 -0
- package/README.md +47 -34
- package/guides/memory.md +21 -14
- package/package.json +2 -2
- package/prompts/dream.md +3 -16
- package/prompts/scribe.md +20 -15
- package/src/apply.mjs +364 -230
- package/src/card.mjs +16 -11
- package/src/catalog.mjs +3 -3
- package/src/chat.mjs +30 -9
- package/src/check.mjs +371 -0
- package/src/cli.mjs +218 -144
- package/src/db.mjs +147 -2
- package/src/dream.mjs +97 -189
- package/src/export.mjs +43 -18
- package/src/hooks.mjs +115 -22
- package/src/jobs.mjs +4 -2
- package/src/keeper.mjs +14 -51
- package/src/ledger.mjs +126 -0
- package/src/logs.mjs +10 -5
- package/src/loop.mjs +4 -4
- package/src/mcp.mjs +129 -51
- package/src/memeval.mjs +167 -0
- package/src/memory.mjs +261 -130
- package/src/page.mjs +3 -3
- package/src/paths.mjs +1 -0
- package/src/prime.mjs +82 -13
- package/src/rebuild.mjs +162 -0
- package/src/render.mjs +22 -0
- package/src/route.mjs +1 -1
- package/src/sandbox.mjs +5 -2
- package/src/scan.mjs +16 -4
- package/src/scribe.mjs +361 -113
- package/src/search.mjs +48 -0
- package/src/sessions.mjs +304 -53
- package/src/settings.mjs +868 -0
- package/src/setup.mjs +56 -64
- package/src/spool.mjs +308 -0
- package/src/text.mjs +118 -6
- package/src/tools.mjs +18 -7
- package/src/transcript.mjs +3 -47
- package/src/tty.mjs +1 -1
- package/src/ui.mjs +44 -16
- package/src/verify.mjs +23 -27
package/src/prime.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { homedir } from 'node:os';
|
|
2
2
|
import { ago, askLines } from './card.mjs';
|
|
3
|
-
import { getMeta } from './db.mjs';
|
|
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';
|
|
7
|
+
import { lastHeard } from './sessions.mjs';
|
|
8
|
+
import { spoolWaitingSince } from './spool.mjs';
|
|
6
9
|
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
7
10
|
import { clip, estimateTokens } from './text.mjs';
|
|
8
11
|
|
|
@@ -10,11 +13,15 @@ const LINE_MAX = 120;
|
|
|
10
13
|
const NAME_MAX = 60;
|
|
11
14
|
const MAX_PROJECTS = 12;
|
|
12
15
|
const MAX_LEFT_OFF = 3;
|
|
16
|
+
/** Where a project was left is news for two weeks; after that it is its card's last line, and search's. */
|
|
17
|
+
const LEFT_OFF_DAYS = 14;
|
|
13
18
|
const MAX_CONFIRM = 3;
|
|
14
19
|
/** The block's ceiling, in tokens: what does not fit is found by search. */
|
|
15
20
|
const PRIME_BUDGET = 800;
|
|
16
21
|
const MAX_NOTICES = 2;
|
|
17
22
|
const SCRIBE_FAILURES_TO_WARN = 3;
|
|
23
|
+
/** An event waiting in the spool this long is said in the block: the next thing said should have replayed it. */
|
|
24
|
+
const SPOOL_WAIT_SAID_MS = 3_600_000;
|
|
18
25
|
/** A preference never used, and said more days ago than this, is the first to leave the block when room is short. */
|
|
19
26
|
const UNUSED_DAYS = 60;
|
|
20
27
|
|
|
@@ -36,7 +43,7 @@ export function prime(db, { budget, project = null, now = new Date().toISOString
|
|
|
36
43
|
|
|
37
44
|
// The small, time-sensitive sections are fixed first; preferences get whatever room is left.
|
|
38
45
|
// Preserve the compact layout normally; when it cannot fit, keep time-sensitive lines first.
|
|
39
|
-
const sections = [workflows(db), projectsLine(db), leftOff(db, now), jobs(db, now), toConfirm(db, { project, now }), warnings(db)];
|
|
46
|
+
const sections = [workflows(db), projectsLine(db), leftOff(db, now), jobs(db, now), toConfirm(db, { project, now }), warnings(db, now)];
|
|
40
47
|
// Where the work with no project goes is said every time, and is not something remembered.
|
|
41
48
|
const remembered = sections.flat().length;
|
|
42
49
|
sections.splice(2, 0, scratchLine());
|
|
@@ -49,15 +56,18 @@ export function prime(db, { budget, project = null, now = new Date().toISOString
|
|
|
49
56
|
const block = lines.join('\n');
|
|
50
57
|
if (estimateTokens(block) <= limit) return block;
|
|
51
58
|
|
|
52
|
-
|
|
59
|
+
// What is left out for room is counted where the block says more is behind search: never a silent cut.
|
|
60
|
+
const more = (left) => `More memory${left > 0 ? ` (${left} line${left === 1 ? '' : 's'} left out for room)` : ''}: sumo search · sumo project list · sumo job list`;
|
|
53
61
|
const wrap = (body) => [open, ...body, close].join('\n');
|
|
62
|
+
const wanted = [...sections.slice().reverse().flat(), ...prefs];
|
|
54
63
|
// A budget too small for even the envelope carries a bounded pointer instead.
|
|
55
|
-
if (estimateTokens(wrap([more])) > limit) return clip('Memory: sumo search', limit * 4);
|
|
64
|
+
if (estimateTokens(wrap([more(wanted.length)])) > limit) return clip('Memory: sumo search', limit * 4);
|
|
56
65
|
const kept = [];
|
|
57
|
-
for
|
|
58
|
-
|
|
66
|
+
// Room for the count is set aside at its longest, so saying it can never push the block over.
|
|
67
|
+
for (const text of wanted) {
|
|
68
|
+
if (estimateTokens(wrap([...kept, text, more(wanted.length)])) <= limit) kept.push(text);
|
|
59
69
|
}
|
|
60
|
-
return wrap([...kept, more]);
|
|
70
|
+
return wrap([...kept, more(wanted.length - kept.length)]);
|
|
61
71
|
}
|
|
62
72
|
|
|
63
73
|
/**
|
|
@@ -127,14 +137,16 @@ function scratchLine() {
|
|
|
127
137
|
}
|
|
128
138
|
|
|
129
139
|
function leftOff(db, now) {
|
|
140
|
+
const since = new Date(Date.parse(now) - LEFT_OFF_DAYS * 86_400_000).toISOString();
|
|
130
141
|
const rows = db
|
|
131
142
|
.prepare(
|
|
132
143
|
`SELECT c.* FROM checkpoints c
|
|
133
144
|
JOIN (SELECT project, MAX(id) AS id FROM checkpoints GROUP BY project) latest ON latest.id = c.id
|
|
134
145
|
JOIN projects p ON p.slug = c.project AND p.status = 'active'
|
|
146
|
+
WHERE c.ts >= ?
|
|
135
147
|
ORDER BY c.ts DESC LIMIT ?`,
|
|
136
148
|
)
|
|
137
|
-
.all(MAX_LEFT_OFF);
|
|
149
|
+
.all(since, MAX_LEFT_OFF);
|
|
138
150
|
return rows.map((c) => clip(`Left off: ${c.project} — ${c.done}${c.next_step ? ` → next: ${c.next_step}` : ''} (${ago(c.ts, now)})`, LINE_MAX * 2));
|
|
139
151
|
}
|
|
140
152
|
|
|
@@ -144,22 +156,79 @@ function jobs(db, now) {
|
|
|
144
156
|
}
|
|
145
157
|
|
|
146
158
|
/**
|
|
147
|
-
* What to put to the user at the start:
|
|
148
|
-
*
|
|
159
|
+
* What to put to the user at the start: memories held for them (their conversation read the web or an MCP server, or
|
|
160
|
+
* the model's shell saved what they never typed), and notices — about everything and about this session's project, each held memory for two weeks. One about
|
|
161
|
+
* another project is asked with that project's card, when it comes up.
|
|
149
162
|
*/
|
|
150
163
|
function toConfirm(db, { project = null, now }) {
|
|
151
|
-
return askLines(db, { scopes: ['global', ...(project ? [`project:${project}`] : [])], now,
|
|
164
|
+
return askLines(db, { scopes: ['global', ...(project ? [`project:${project}`] : [])], now, held: MAX_CONFIRM, notices: MAX_NOTICES });
|
|
152
165
|
}
|
|
153
166
|
|
|
154
|
-
|
|
167
|
+
/** Where it is kept that a sumo command was run from inside Claude Code, and when: the evidence that it is in use. */
|
|
168
|
+
const USED_CLAUDE = 'used.claude';
|
|
169
|
+
/** The stamp is renewed this often at most: a command pays one read for it, and a write an hour. */
|
|
170
|
+
const USE_STAMPED_EVERY_MS = 3_600_000;
|
|
171
|
+
/** Claude Code used this much later than it was last heard is Claude Code not being heard: a prompt comes before a command. */
|
|
172
|
+
const UNHEARD_AFTER_MS = 86_400_000;
|
|
173
|
+
/** A use this old is a harness the user left, not one that is failing now. */
|
|
174
|
+
const LEFT_DAYS = 60;
|
|
175
|
+
const DAY_MS = 86_400_000;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Notes that a sumo command is being run from inside Claude Code (`CLAUDECODE=1`, which it sets in its shell) — never
|
|
179
|
+
* from sumo's own chat or a job started there. Called by the command line for the commands that open memory, never by
|
|
180
|
+
* a hook: a hook running is the hearing itself, not evidence of use without it.
|
|
181
|
+
*/
|
|
182
|
+
export function noteUsedFrom(db, env = process.env, now = new Date().toISOString()) {
|
|
183
|
+
if (env.CLAUDECODE !== '1' || env.SUMO_TOOL || env.SUMO_JOB) return;
|
|
184
|
+
const last = getMeta(db, USED_CLAUDE);
|
|
185
|
+
if (!last || Date.parse(now) - Date.parse(last) >= USE_STAMPED_EVERY_MS) setMeta(db, USED_CLAUDE, now);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Whether memory is still hearing the user, judged on evidence of use and never on silence alone. Capture once stopped
|
|
190
|
+
* for five weeks — the hook lines had gone from Claude Code — while sumo commands went on being run from it: that is
|
|
191
|
+
* the failure, and `deaf` is 'claude' when a command was run from Claude Code more than a day after the last turn
|
|
192
|
+
* heard from it (or with none ever heard). Silence by itself is a holiday: back after eight days, nothing has been
|
|
193
|
+
* missed. sumo's own chat records in process and cannot go deaf, so its silence, however long, is only said. `fix`
|
|
194
|
+
* says what to do, `heard` when each harness was last heard, `used` when a command last ran from Claude Code.
|
|
195
|
+
*/
|
|
196
|
+
export function heartbeat(db, now = new Date().toISOString()) {
|
|
197
|
+
const heard = { claude: lastHeard(db, 'claude'), sumo: lastHeard(db, 'sumo') };
|
|
198
|
+
const lastAt = lastHeard(db);
|
|
199
|
+
const used = getMeta(db, USED_CLAUDE);
|
|
200
|
+
const inUse = used && Date.parse(now) - Date.parse(used) <= LEFT_DAYS * DAY_MS;
|
|
201
|
+
if (inUse && (!heard.claude || Date.parse(used) - Date.parse(heard.claude) > UNHEARD_AFTER_MS)) {
|
|
202
|
+
const days = heard.claude ? Math.floor((Date.parse(now) - Date.parse(heard.claude)) / DAY_MS) : null;
|
|
203
|
+
const since = days === null ? 'no turn has ever come from it' : `no turn has come from it for ${days} day${days === 1 ? '' : 's'}`;
|
|
204
|
+
const fix = `Claude Code is not being heard: sumo commands are run from it and ${since}, so what is said there is not kept. Its hook lines are missing or failing — sumo help hooks prints them, and logs/hook.log says what failed`;
|
|
205
|
+
return { heard, lastAt, used, deaf: 'claude', fix };
|
|
206
|
+
}
|
|
207
|
+
return { heard, lastAt, used, deaf: null, fix: '' };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function warnings(db, now) {
|
|
155
211
|
const out = [];
|
|
156
212
|
// AGENTS.md says a background writer files what the user says; with it off, nothing does unless the model is told.
|
|
157
213
|
if ((getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model']) === 'off') {
|
|
158
214
|
out.push('The memory pass is off (memory.model): save what the user states that should last, as it is said, with sumo add.');
|
|
159
215
|
}
|
|
160
|
-
|
|
216
|
+
// 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);
|
|
218
|
+
const failures = filing.failures;
|
|
219
|
+
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.');
|
|
161
220
|
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.`);
|
|
221
|
+
// 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
|
+
const spool = spoolWaitingSince();
|
|
223
|
+
const waited = spool.since === null ? 0 : Date.parse(now) - spool.since;
|
|
224
|
+
if (waited > SPOOL_WAIT_SAID_MS) {
|
|
225
|
+
const hours = Math.floor(waited / 3_600_000);
|
|
226
|
+
out.push(`Warning: ${spool.waiting} thing${spool.waiting === 1 ? '' : 's'} said in a session ${spool.waiting === 1 ? 'is' : 'are'} still waiting to be written to memory, the oldest for ${hours} hour${hours === 1 ? '' : 's'} — the database could not take them. Tell the user; sumo memory check says where they wait and why.`);
|
|
227
|
+
}
|
|
162
228
|
const backup = getMeta(db, 'backup.failed');
|
|
163
229
|
if (backup) out.push(`Warning: the weekly backup could not be written (${backup}). Tell the user; sumo backup tries again.`);
|
|
230
|
+
// A harness in use and not heard is said where every session starts: the one place it cannot hide.
|
|
231
|
+
const beat = heartbeat(db, now);
|
|
232
|
+
if (beat.deaf) out.push(`Warning: ${beat.fix}. Tell the user.`);
|
|
164
233
|
return out;
|
|
165
234
|
}
|
package/src/rebuild.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { activeScopes, applyOps, tooWide } from './apply.mjs';
|
|
2
|
+
import { tx } from './db.mjs';
|
|
3
|
+
import { extractorVersion } from './ledger.mjs';
|
|
4
|
+
import * as memory from './memory.mjs';
|
|
5
|
+
import { line } from './render.mjs';
|
|
6
|
+
import { extract, labelsFor, withLock } from './scribe.mjs';
|
|
7
|
+
import { markScribed } from './sessions.mjs';
|
|
8
|
+
import { clip } from './text.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Derived memory is disposable: it can be made again from the journal. `dryExtract` asks what the extractor as it is
|
|
12
|
+
* now would file from some turns; `rebuild` puts that beside what memory holds from the same turns, and — unless it
|
|
13
|
+
* is only asked to show the difference — files what is missing and lets go what the extractor no longer finds there.
|
|
14
|
+
* What the user said to remember is never part of it.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The current extractor run over journal turns, and the apply rules over its answer, writing nothing: what it would
|
|
19
|
+
* file from them today. `turnIds` are `user_turns` ids, of one conversation or several.
|
|
20
|
+
*
|
|
21
|
+
* Returns `{ ok, error, wouldFile, dropped }`. `wouldFile` is every statement that passed the rules, as
|
|
22
|
+
* `{ turnId, type, scope, body, quote, existing }` — `existing` the id of a memory already holding that statement
|
|
23
|
+
* from that turn (in whatever state: active, forgotten, superseded), or null when it would be new. `dropped` is each
|
|
24
|
+
* proposal the rules refused, with its reason. `ok` is false when the model could not be asked for some of the turns;
|
|
25
|
+
* what the rest gave is still returned.
|
|
26
|
+
*/
|
|
27
|
+
export async function dryExtract(db, { turnIds, now = new Date().toISOString() }) {
|
|
28
|
+
const read = await extract(db, { turnIds, now });
|
|
29
|
+
return { ok: read.ok, error: read.error, wouldFile: read.wouldFile, dropped: read.dropped };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The memories an extractor derived from these turns that nothing it would file today stands behind: active, drawn
|
|
34
|
+
* from one of the turns read, and not among `supported`. Never a primary row (`derived_by IS NULL` — what the user
|
|
35
|
+
* said to remember, confirmed or edited), never one they pinned or once confirmed, and only statements: a trap or a
|
|
36
|
+
* workflow cites no turn to be read again. `gone` are those to let go; `kept` those that replaced another memory
|
|
37
|
+
* (`replaces`, its id) and so stay: each carries a change the user stated, and letting it go would leave them with
|
|
38
|
+
* neither what they said first — superseded, and staying so — nor what they said after.
|
|
39
|
+
*/
|
|
40
|
+
function unsupported(db, turnIds, supported) {
|
|
41
|
+
const rows = [];
|
|
42
|
+
const replaced = db.prepare('SELECT id FROM memories WHERE superseded_by = ? ORDER BY id LIMIT 1');
|
|
43
|
+
const from = db.prepare(
|
|
44
|
+
`SELECT m.* FROM memories m
|
|
45
|
+
WHERE m.source_turn = ? AND m.state = 'active' AND m.derived_by IS NOT NULL AND m.pinned = 0
|
|
46
|
+
AND m.type IN ('preference', 'fact', 'decision')
|
|
47
|
+
AND NOT EXISTS (SELECT 1 FROM memory_events e WHERE e.memory_id = m.id AND e.op = 'confirmed')
|
|
48
|
+
ORDER BY m.id`,
|
|
49
|
+
);
|
|
50
|
+
for (const id of turnIds) rows.push(...from.all(id).filter((m) => !supported.has(m.id)).map((m) => ({ ...m, replaces: replaced.get(m.id)?.id ?? null })));
|
|
51
|
+
return { gone: rows.filter((m) => m.replaces === null), kept: rows.filter((m) => m.replaces !== null) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const count = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* `sumo memory rebuild`: the journal read again with the extractor as it is now, and the difference from the memories
|
|
58
|
+
* derived so far — what it would add, and which derived memories it would no longer file. With `dryRun` that is all.
|
|
59
|
+
* Otherwise the additions are filed (derived by this extractor, their pass in the ledger, so the conversations count
|
|
60
|
+
* as read) and the unsupported derived memories are let go the way `sumo forget` does, history kept — except one that
|
|
61
|
+
* 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
|
+
* 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.
|
|
64
|
+
*/
|
|
65
|
+
export async function rebuild(db, { dryRun = false, since, limit, yes = false, now = new Date().toISOString() } = {}) {
|
|
66
|
+
const version = extractorVersion(db);
|
|
67
|
+
const sessions = db
|
|
68
|
+
.prepare(
|
|
69
|
+
`SELECT s.id FROM sessions s
|
|
70
|
+
WHERE EXISTS (SELECT 1 FROM user_turns t WHERE t.session_id = s.id AND (?1 IS NULL OR t.ts >= ?1))
|
|
71
|
+
ORDER BY COALESCE(s.last_turn_at, s.started_at) DESC, s.id LIMIT ?2`,
|
|
72
|
+
)
|
|
73
|
+
.all(since ?? null, limit ?? -1)
|
|
74
|
+
.map((s) => s.id);
|
|
75
|
+
if (sessions.length === 0) return ['nothing to do — the journal holds no conversation to read'];
|
|
76
|
+
const turns = db.prepare('SELECT id FROM user_turns WHERE session_id = ? AND (?2 IS NULL OR ts >= ?2) ORDER BY id');
|
|
77
|
+
const turnIds = sessions.flatMap((id) => turns.all(id, since ?? null).map((t) => t.id));
|
|
78
|
+
|
|
79
|
+
const go = async () => {
|
|
80
|
+
const read = await extract(db, { turnIds, now });
|
|
81
|
+
const readTurns = read.batches.flatMap((b) => b.turnIds);
|
|
82
|
+
const adds = read.wouldFile.filter((f) => f.existing === null);
|
|
83
|
+
// What an addition does to a memory it looks like, as far as can be said before the pair judge is asked — which a
|
|
84
|
+
// dry run does not do. An older statement than that memory never replaces it.
|
|
85
|
+
const replacing = (f) =>
|
|
86
|
+
f.replaces ? ` — replaces m${f.replaces}`
|
|
87
|
+
: f.mayReplace ? ` — replaces m${f.mayReplace} if the pair judge agrees`
|
|
88
|
+
: f.mayBeOlder ? ` — said before m${f.mayBeOlder}: not filed if the pair judge calls the two one matter, else filed beside it`
|
|
89
|
+
: '';
|
|
90
|
+
const { gone, kept } = unsupported(db, readTurns, new Set(read.wouldFile.map((f) => f.existing).filter((id) => id !== null)));
|
|
91
|
+
const cost = `${read.usage.inputTokens.toLocaleString('en-US')} tokens in, ${read.usage.outputTokens.toLocaleString('en-US')} out, $${read.usage.costUsd.toFixed(4)}`;
|
|
92
|
+
const lines = [
|
|
93
|
+
`read ${count(new Set(read.batches.map((b) => b.sessionId)).size, 'conversation')}, ${count(readTurns.length, 'turn')}, with extractor ${version} (${cost})`,
|
|
94
|
+
`would add: ${adds.length}`,
|
|
95
|
+
...adds.map((f) => ` + [${f.type}·${f.scope}] ${f.body} — t${f.turnId} "${clip(f.quote, 80)}"${replacing(f)}`),
|
|
96
|
+
`derived, no longer supported: ${gone.length}`,
|
|
97
|
+
...gone.map((m) => ` - ${line(m)} — derived by ${m.derived_by} from t${m.source_turn}`),
|
|
98
|
+
...kept.map((m) => ` kept: replaces m${m.replaces} — ${line(m)}`),
|
|
99
|
+
...(read.dropped.length > 0 ? [`refused by the rules: ${read.dropped.length}`, ...read.dropped.map((reason) => ` dropped — ${reason}`)] : []),
|
|
100
|
+
...(read.ok ? [] : [`some conversations could not be read and are left as they are — ${read.error}`]),
|
|
101
|
+
];
|
|
102
|
+
const done = (more) => (read.ok ? [...lines, ...more] : Object.assign([...lines, ...more], { exitCode: 1 }));
|
|
103
|
+
if (dryRun) return done(['nothing was changed (--dry-run)']);
|
|
104
|
+
if (read.batches.length === 0) return done(['nothing was changed']);
|
|
105
|
+
|
|
106
|
+
// Look-alikes are judged before anything is written (a model call each batch that has one), then every change
|
|
107
|
+
// is made in one transaction, counted, and kept or undone whole.
|
|
108
|
+
for (const batch of read.batches) {
|
|
109
|
+
batch.ctx = { source: 'scribe', turns: batch.turns, sessionId: batch.sessionId, now, derivedBy: version };
|
|
110
|
+
batch.labels = (await labelsFor(db, batch.ops, batch.ctx)).labels;
|
|
111
|
+
}
|
|
112
|
+
const changed = tx(db, () => {
|
|
113
|
+
const before = activeScopes(db);
|
|
114
|
+
db.exec('SAVEPOINT rebuild');
|
|
115
|
+
const event = db.prepare(`INSERT INTO memory_events (ts, memory_id, op, actor, run_id, reason) VALUES (?, ?, 'rebuilt', 'sumo', ?, 'rebuilt')`);
|
|
116
|
+
const runOf = new Map();
|
|
117
|
+
const supported = new Set();
|
|
118
|
+
const applied = [];
|
|
119
|
+
const older = [];
|
|
120
|
+
for (const batch of read.batches) {
|
|
121
|
+
const runId = Number(
|
|
122
|
+
db
|
|
123
|
+
.prepare(`INSERT INTO extractions (session_id, extractor_version, status, attempts, first_turn, last_turn, ops_json, started_at, ended_at) VALUES (?, ?, 'ok', 1, ?, ?, ?, ?, ?)`)
|
|
124
|
+
.run(batch.sessionId, version, batch.turnIds[0], batch.turnIds.at(-1), JSON.stringify(batch.ops), now, now).lastInsertRowid,
|
|
125
|
+
);
|
|
126
|
+
for (const id of batch.turnIds) runOf.set(id, runId);
|
|
127
|
+
const outcome = applyOps(db, batch.ops, { ...batch.ctx, runId, labels: batch.labels });
|
|
128
|
+
for (const id of outcome.written) {
|
|
129
|
+
event.run(now, id, runId);
|
|
130
|
+
supported.add(id);
|
|
131
|
+
}
|
|
132
|
+
for (const f of outcome.filed) if (f.existing !== null) supported.add(f.existing);
|
|
133
|
+
applied.push(...outcome.applied);
|
|
134
|
+
// Said out loud: a statement not filed because what memory holds was said after it.
|
|
135
|
+
older.push(...outcome.dropped.filter((reason) => / said before m\d+/.test(reason)));
|
|
136
|
+
markScribed(db, batch.turnIds);
|
|
137
|
+
}
|
|
138
|
+
// Read again as memory now stands: a statement judged the same as one of these in other words stands behind it,
|
|
139
|
+
// and one a new statement replaced is no longer active.
|
|
140
|
+
const retired = unsupported(db, readTurns, supported).gone;
|
|
141
|
+
for (const m of retired) memory.forget(db, m.id, { now, actor: 'sumo', runId: runOf.get(m.source_turn), reason: 'rebuilt' });
|
|
142
|
+
const wide = tooWide(db, before);
|
|
143
|
+
if (wide && !yes) db.exec('ROLLBACK TO rebuild');
|
|
144
|
+
db.exec('RELEASE rebuild');
|
|
145
|
+
return { applied, older, retired, refused: wide && !yes ? wide : null };
|
|
146
|
+
});
|
|
147
|
+
if (changed.refused) {
|
|
148
|
+
return Object.assign([...lines, `refused — ${changed.refused.replace(/^this pass/, 'this rebuild')}`, 'nothing was changed: sumo memory rebuild --yes makes the change anyway'], { exitCode: 1 });
|
|
149
|
+
}
|
|
150
|
+
return done([
|
|
151
|
+
...changed.applied.map((l) => ` ${l}`),
|
|
152
|
+
...changed.older.map((l) => ` not filed — ${l.replace(/^\w+: /, '')}`),
|
|
153
|
+
...changed.retired.map((m) => ` let go m${m.id} (sumo history ${m.id} still shows it)`),
|
|
154
|
+
`rebuilt: ${count(changed.applied.length, 'statement')} filed, ${count(changed.retired.length, 'memory', 'memories')} let go`,
|
|
155
|
+
]);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
if (dryRun) return go();
|
|
159
|
+
// The filing pass and a rebuild write the same ledger and the same memories: one at a time.
|
|
160
|
+
const out = await withLock('scribe', go);
|
|
161
|
+
return out.skipped ? Object.assign([`nothing was changed — ${out.skipped}`], { exitCode: 1 }) : out;
|
|
162
|
+
}
|
package/src/render.mjs
CHANGED
|
@@ -44,3 +44,25 @@ export function historyLine(m) {
|
|
|
44
44
|
const span = `${m.valid_from.slice(0, 10)} → ${m.invalid_at ? m.invalid_at.slice(0, 10) : ' '}`;
|
|
45
45
|
return `m${m.id} ${span} ${m.state.padEnd(11)} ${clip(m.type === 'procedure' ? m.title : m.body, LINE_BODY_MAX)}`;
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
/** A memory as a search prints it: its line, and the day it was said. */
|
|
49
|
+
export const foundLine = (m) => `${line(m)} · ${m.valid_from.slice(0, 10)}`;
|
|
50
|
+
|
|
51
|
+
const SAID_BY = { turn: 'you said', reply: 'sumo said' };
|
|
52
|
+
const AROUND_MAX = 110;
|
|
53
|
+
const saidTags = (e) => `[${SAID_BY[e.kind]}${e.project ? `·${e.project}` : ''}]`;
|
|
54
|
+
|
|
55
|
+
/** A turn or a reply as a search prints it: `t481 [you said·simba] …the part around the words… · 2026-11-02`. */
|
|
56
|
+
export const saidLine = (hit) => `${hit.ref} ${saidTags(hit)} ${hit.excerpt} · ${hit.ts.slice(0, 10)}`;
|
|
57
|
+
|
|
58
|
+
/** One turn or reply whole, then what was said around it in its conversation, the one asked for marked. */
|
|
59
|
+
export function saidDetail({ entry, around }) {
|
|
60
|
+
const when = (e) => `${e.ts.slice(0, 10)} ${e.ts.slice(11, 16)}`;
|
|
61
|
+
const cut = entry.full_len > entry.text.length ? ` · the first ${entry.text.length} of ${entry.full_len} characters, the rest was not kept` : '';
|
|
62
|
+
const out = [`${entry.ref} ${saidTags(entry)} ${when(entry)} · conversation ${entry.session_id.slice(0, 8)}${cut}`, '', entry.text];
|
|
63
|
+
if (around.length > 1) {
|
|
64
|
+
out.push('', 'around it (sumo show <id> opens one whole):');
|
|
65
|
+
for (const e of around) out.push(`${e === entry ? '>' : ' '} ${e.ref} ${saidTags(e)} ${when(e)} ${clip(e.text, AROUND_MAX)}`);
|
|
66
|
+
}
|
|
67
|
+
return out.join('\n');
|
|
68
|
+
}
|
package/src/route.mjs
CHANGED
|
@@ -188,7 +188,7 @@ const askRouter = (db, { agent, project, task, title, retryOf }) => askFor(db, a
|
|
|
188
188
|
|
|
189
189
|
async function askFor(db, system, prompt, models = usableModels(db)) {
|
|
190
190
|
const usable = models;
|
|
191
|
-
if (usable.length === 0) throw new UsageError('no model is on —
|
|
191
|
+
if (usable.length === 0) throw new UsageError('no model is on — turn one on under API models, on the settings page (sumo config)');
|
|
192
192
|
const result = await callLocalModel(db, { system, prompt, schema: routerSchema(usable) });
|
|
193
193
|
if (!result.ok) return { ok: false, error: result.error };
|
|
194
194
|
const { model, effort, reason, risky } = result.data ?? {};
|
package/src/sandbox.mjs
CHANGED
|
@@ -136,8 +136,11 @@ const whyOf = (lists) => [...Object.entries(lists)].flatMap(([why, names]) => na
|
|
|
136
136
|
const GUARDED_WHY = whyOf(GUARDED);
|
|
137
137
|
const HOME_WHY = [...GUARDED_WHY, ...whyOf(HOME_GUARDED)];
|
|
138
138
|
|
|
139
|
-
/**
|
|
140
|
-
|
|
139
|
+
/**
|
|
140
|
+
* What sumo keeps for itself, which only sumo writes: its memory and settings, its MCP servers, its own program and model —
|
|
141
|
+
* and the spool, whose lines are replayed into the journal as what the user said.
|
|
142
|
+
*/
|
|
143
|
+
const SUMO_PRIVATE = ['memory.db', 'memory.db-wal', 'memory.db-shm', 'mcp.json', 'bin', 'models', 'eval.env', 'spool'];
|
|
141
144
|
|
|
142
145
|
/** The directories on the PATH, absolute: a program put in one runs whenever its name is typed. */
|
|
143
146
|
const pathDirs = (env = process.env) => [...new Set(String(env.PATH ?? '').split(':').filter((d) => d.startsWith('/')).map((d) => resolve(d)))];
|
package/src/scan.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { execFileSync } from 'node:child_process';
|
|
2
|
-
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import { closeSync, constants, existsSync, fstatSync, openSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -15,6 +15,20 @@ const ABOUT_MAX = 200;
|
|
|
15
15
|
/** More than any README, Makefile or package.json a scan takes a pointer from. */
|
|
16
16
|
const READ_MAX_BYTES = 1024 * 1024;
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* A plain file's text, judged and read through one descriptor, so what is read is what was judged — or null when it
|
|
20
|
+
* is not a plain file (a FIFO is opened without waiting on a writer) or is over `maxBytes`. Throws when it cannot be opened.
|
|
21
|
+
*/
|
|
22
|
+
export function readPlainFile(path, maxBytes = Infinity) {
|
|
23
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
24
|
+
try {
|
|
25
|
+
const stat = fstatSync(fd);
|
|
26
|
+
return stat.isFile() && stat.size <= maxBytes ? readFileSync(fd, 'utf8') : null;
|
|
27
|
+
} finally {
|
|
28
|
+
closeSync(fd);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
18
32
|
/**
|
|
19
33
|
* A file of the repository's own, as text — or null. A link that leads out of the repository (a README that is
|
|
20
34
|
* really ~/.netrc) is not the repository's to say, and what is not a plain file of sane size (a FIFO, /dev/zero)
|
|
@@ -25,9 +39,7 @@ const read = (root, name) => {
|
|
|
25
39
|
const real = realpathSync(join(root, name));
|
|
26
40
|
const home = realpathSync(root);
|
|
27
41
|
if (!real.startsWith(`${home}/`)) return null;
|
|
28
|
-
|
|
29
|
-
if (!stat.isFile() || stat.size > READ_MAX_BYTES) return null;
|
|
30
|
-
return readFileSync(real, 'utf8');
|
|
42
|
+
return readPlainFile(real, READ_MAX_BYTES);
|
|
31
43
|
} catch {
|
|
32
44
|
return null;
|
|
33
45
|
}
|