sumo-agents 0.3.0 → 0.5.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.
Files changed (52) hide show
  1. package/AGENTS.md +8 -6
  2. package/CHANGELOG.md +155 -0
  3. package/README.md +82 -19
  4. package/bin/sumo.mjs +13 -2
  5. package/guides/feature.md +2 -1
  6. package/guides/memory.md +6 -4
  7. package/guides/projects.md +1 -1
  8. package/guides/review.md +1 -1
  9. package/package.json +5 -4
  10. package/prompts/dream.md +6 -0
  11. package/spinner.txt +25 -1
  12. package/src/apply.mjs +146 -18
  13. package/src/auth.mjs +32 -5
  14. package/src/card.mjs +39 -2
  15. package/src/catalog.mjs +6 -3
  16. package/src/chat.mjs +289 -89
  17. package/src/cli.mjs +359 -135
  18. package/src/cost.mjs +67 -8
  19. package/src/db.mjs +106 -3
  20. package/src/dream.mjs +445 -19
  21. package/src/editor.mjs +113 -11
  22. package/src/export.mjs +105 -7
  23. package/src/guard.mjs +57 -11
  24. package/src/guides.mjs +5 -4
  25. package/src/herdr.mjs +9 -4
  26. package/src/hooks.mjs +35 -19
  27. package/src/jobs.mjs +118 -39
  28. package/src/jobtools.mjs +6 -4
  29. package/src/keeper.mjs +77 -0
  30. package/src/local-server.mjs +9 -16
  31. package/src/lock.mjs +2 -1
  32. package/src/logs.mjs +165 -0
  33. package/src/loop.mjs +121 -44
  34. package/src/mcp.mjs +72 -16
  35. package/src/memory.mjs +142 -15
  36. package/src/model.mjs +41 -21
  37. package/src/paths.mjs +38 -0
  38. package/src/prime.mjs +19 -31
  39. package/src/projects.mjs +94 -12
  40. package/src/redact.mjs +10 -0
  41. package/src/render.mjs +2 -1
  42. package/src/route.mjs +39 -25
  43. package/src/sandbox.mjs +82 -3
  44. package/src/scan.mjs +35 -1
  45. package/src/scribe.mjs +10 -22
  46. package/src/sessions.mjs +49 -13
  47. package/src/setup.mjs +425 -76
  48. package/src/text.mjs +100 -1
  49. package/src/tools.mjs +258 -21
  50. package/src/tty.mjs +481 -105
  51. package/src/ui.mjs +1205 -276
  52. package/src/verify.mjs +17 -1
package/src/editor.mjs CHANGED
@@ -4,8 +4,15 @@
4
4
  * function from one state to the next, so the screen only has to draw it.
5
5
  */
6
6
 
7
- /** An empty box. `history` is what Up recalls, oldest first; `at` is the entry shown, one past the end for the text being written. */
8
- export const editor = (history = []) => ({ text: '', cursor: 0, history, at: history.length, draft: '', pick: 0 });
7
+ /**
8
+ * An empty box. `history` is what Up recalls, oldest first; `at` is the entry shown, one past the end for the text being written.
9
+ * `undo` is what the text was before each change, newest last; `kill` is the last thing cut, for Ctrl-Y; `search`, while Ctrl-R
10
+ * looks back through history, what has been typed to find and where it was found.
11
+ */
12
+ export const editor = (history = [], kill = '') => ({ text: '', cursor: 0, history, at: history.length, draft: '', pick: 0, undo: [], kill, search: null });
13
+
14
+ /** How many changes Ctrl-_ can take back. */
15
+ const UNDO_KEPT = 100;
9
16
 
10
17
  /**
11
18
  * The menu for what is being typed: the commands a half-typed slash could mean, or, once a command's
@@ -13,8 +20,28 @@ export const editor = (history = []) => ({ text: '', cursor: 0, history, at: his
13
20
  * command that offers none. Each entry says what to show (`label`), its hint, and the whole text
14
21
  * picking it leaves in the box (`complete`).
15
22
  */
23
+ /** How well a few typed letters name a command: from its start best, then anywhere in it, then in order with gaps between; -1 when they do not. */
24
+ function nameRank(name, typed) {
25
+ if (name.startsWith(typed)) return 0;
26
+ if (name.includes(typed)) return 1;
27
+ let at = 0;
28
+ for (const ch of typed) {
29
+ at = name.indexOf(ch, at) + 1;
30
+ if (at === 0) return -1;
31
+ }
32
+ return 2;
33
+ }
34
+
16
35
  export function menuFor(text, commands) {
17
- if (/^\/\w*$/.test(text)) return commands.filter((c) => c.name.startsWith(text.slice(1))).map((c) => ({ name: c.name, label: `/${c.name}`, hint: c.hint, complete: `/${c.name} ` }));
36
+ if (/^\/\w*$/.test(text)) {
37
+ // A command is found by any of its letters, in order: /rsm is /resume. The ones it starts with come first, in the menu's own order.
38
+ const typed = text.slice(1);
39
+ return commands
40
+ .map((c, i) => [nameRank(c.name, typed), i, c])
41
+ .filter(([rank]) => rank !== -1)
42
+ .sort((a, b) => a[0] - b[0] || a[1] - b[1])
43
+ .map(([, , c]) => ({ name: c.name, label: `/${c.name}`, hint: c.hint, complete: `/${c.name} ` }));
44
+ }
18
45
  const m = /^\/(\w+)\s+([^\n]*)$/.exec(text);
19
46
  const command = m && commands.find((c) => c.name === m[1] && c.choices);
20
47
  if (!command) return [];
@@ -54,10 +81,11 @@ export function matchFiles(list, query, max = 8) {
54
81
  /**
55
82
  * The menu for the text being written: the slash menu, or — given `files(query, from)`, which answers with the paths a
56
83
  * mention could mean — the files for the `@word` at the cursor; picking one leaves its path where the mention was. A
57
- * recalled entry has none, so the arrows keep walking history; nor has a text whose menu Esc shut (`shut`), until it changes.
84
+ * recalled entry, as it was sent, has none, so the arrows keep walking history — changed, it is being written again, and
85
+ * has; nor has a text whose menu Esc shut (`shut`), until it changes.
58
86
  */
59
87
  export function menuOf(state, commands, files = null) {
60
- if (state.at !== state.history.length || state.shut === state.text) return [];
88
+ if ((state.at !== state.history.length && state.text === state.history[state.at]) || state.shut === state.text) return [];
61
89
  const menu = menuFor(state.text, commands);
62
90
  const at = menu.length === 0 && files ? mentionAt(state) : null;
63
91
  if (!at) return menu;
@@ -89,7 +117,56 @@ const insert = (state, chunk) => edit(state, state.text.slice(0, state.cursor) +
89
117
  const cut = (state, from, to) => edit(state, state.text.slice(0, from) + state.text.slice(to), from);
90
118
 
91
119
  /** Pasted text goes in whole, its line ends made plain; a newline in it is never Enter. */
92
- export const paste = (state, text) => insert(state, text.replace(/\r\n?/g, '\n'));
120
+ export const paste = (state, text) => remember(state, insert(state, text.replace(/\r\n?/g, '\n')));
121
+
122
+ /**
123
+ * Characters nobody sees that change what text says: direction overrides, zero-width spaces, the invisible tag letters that
124
+ * can carry a hidden instruction. The joiner and the selectors an emoji is built from are not among them.
125
+ */
126
+ const INVISIBLE = /[\u200b\u200c\u200e\u200f\u202a-\u202e\u2060-\u2064\u2066-\u2069\ufeff\u{e0000}-\u{e007f}]/gu;
127
+
128
+ /** Text from the clipboard as the box takes it: without the characters nobody sees, and how many were taken out. */
129
+ export function cleanPaste(text) {
130
+ const removed = String(text).match(INVISIBLE)?.length ?? 0;
131
+ return { text: removed ? String(text).replace(INVISIBLE, '') : String(text), removed };
132
+ }
133
+
134
+ /** A marker standing for something put in the box — an image, a long paste — ending at the cursor: deleted whole, never a character of it. */
135
+ const MARKER_BEFORE = /\[(?:Image #\d+|Pasted text #\d+[^\]\n]*)\] ?$/;
136
+
137
+ /** The state after a change, carrying what the text was before it so Ctrl-_ can take it back. A run of letters typed is one change. */
138
+ function remember(before, after, typing = false) {
139
+ if (after.text === before.text) return after;
140
+ const run = typing && before.typing;
141
+ const undo = run ? before.undo : [...before.undo, { text: before.text, cursor: before.cursor }].slice(-UNDO_KEPT);
142
+ return { ...after, undo, typing };
143
+ }
144
+
145
+ /** What was cut is kept for Ctrl-Y as well as gone from the box. */
146
+ const killed = (state, from, to) => ({ ...cut(state, from, to), kill: state.text.slice(from, to) || state.kill });
147
+
148
+ /** Ctrl-R: the newest line sent before `below` that holds `query`, put in the box with the cursor where it was found. */
149
+ function find(state, query, below, further = false) {
150
+ const { search } = state;
151
+ for (let i = Math.min(below, state.history.length) - 1; i >= 0; i--) {
152
+ const at = query ? state.history[i].toLowerCase().indexOf(query.toLowerCase()) : -1;
153
+ if (at !== -1) return { ...state, text: state.history[i], cursor: at, search: { ...search, query, at: i, failed: false, end: false } };
154
+ }
155
+ // Nothing further back keeps what was found; nothing at all says so.
156
+ if (further && search.at !== null) return { ...state, search: { ...search, end: true } };
157
+ return { ...state, search: { ...search, query, at: null, failed: query !== '', end: false } };
158
+ }
159
+
160
+ /** A key while Ctrl-R looks back: typing narrows it, Ctrl-R goes further back, Enter keeps what was found, Esc puts the draft back. */
161
+ function searching(state, input, key) {
162
+ const { search } = state;
163
+ if (key.escape || (key.ctrl && (input === 'g' || input === 'c'))) return { ...state, text: search.draft.text, cursor: search.draft.cursor, search: null };
164
+ if (key.ctrl && input === 'r') return find(state, search.query, search.at ?? state.history.length, true);
165
+ if (key.backspace) return find(state, search.query.slice(0, -1), state.history.length);
166
+ if (input && !key.ctrl && !key.meta && !key.return && !key.tab) return find(state, search.query + input, state.history.length);
167
+ // Anything else keeps what was found in the box, to send or to edit.
168
+ return { ...state, at: state.history.length, search: null };
169
+ }
93
170
 
94
171
  const BURST_KEYS = { '\r': ['\r', { return: true }], '\t': ['', { tab: true }], '\x7f': ['', { backspace: true }] };
95
172
  /** A control character is Ctrl and a letter: 3 is Ctrl-C. */
@@ -141,7 +218,7 @@ function send(state) {
141
218
  const text = state.text.trim();
142
219
  if (!text) return { state: edit(state, '', 0) };
143
220
  const history = state.history.at(-1) === text ? state.history : [...state.history, text];
144
- return { state: editor(history), submit: text };
221
+ return { state: editor(history, state.kill), submit: text };
145
222
  }
146
223
 
147
224
  /**
@@ -150,6 +227,22 @@ function send(state) {
150
227
  * sent the text.
151
228
  */
152
229
  export function press(state, input, key, commands = [], files = null) {
230
+ if (state.search) return { state: searching(state, input, key) };
231
+ if (key.ctrl && input === 'r') return { state: find({ ...state, search: { query: '', at: null, failed: false, draft: { text: state.text, cursor: state.cursor } } }, '', state.history.length) };
232
+ // Ctrl-_ (Ctrl-/ on most keyboards): the text as it was before the last change.
233
+ if (input === '\x1f') {
234
+ const last = state.undo.at(-1);
235
+ return { state: last ? { ...state, text: last.text, cursor: last.cursor, undo: state.undo.slice(0, -1), typing: false, pick: 0 } : state };
236
+ }
237
+ const pressed = keyed(state, input, key, commands, files);
238
+ const typing = !key.ctrl && !key.meta && input.length === 1 && /\S/.test(input) && !key.return && !key.tab;
239
+ // Sent, the box starts afresh: there is nothing to take back into it. A key that changed nothing hands back the same state:
240
+ // the screen reads that as Down having left the box.
241
+ if (pressed.submit !== undefined || pressed.state === state) return pressed;
242
+ return { ...pressed, state: { ...remember(state, pressed.state, typing), typing } };
243
+ }
244
+
245
+ function keyed(state, input, key, commands, files) {
153
246
  const menu = menuOf(state, commands, files);
154
247
  const { text, cursor } = state;
155
248
  const word = key.meta || key.ctrl;
@@ -171,7 +264,11 @@ export function press(state, input, key, commands = [], files = null) {
171
264
  if (key.rightArrow) return { state: { ...state, cursor: word ? wordRight(state) : cursor + under(state).length } };
172
265
  if (key.home) return { state: { ...state, cursor: lineStart(state) } };
173
266
  if (key.end) return { state: { ...state, cursor: lineEnd(state) } };
174
- if (key.backspace) return { state: cut(state, key.meta ? wordLeft(state) : cursor - before(state).length, cursor) };
267
+ if (key.backspace && key.meta) return { state: killed(state, wordLeft(state), cursor) };
268
+ if (key.backspace) {
269
+ const marker = MARKER_BEFORE.exec(text.slice(0, cursor));
270
+ return { state: cut(state, marker ? cursor - marker[0].length : cursor - before(state).length, cursor) };
271
+ }
175
272
  if (key.delete) return { state: cut(state, cursor, cursor + under(state).length) };
176
273
 
177
274
  if (key.ctrl) {
@@ -181,16 +278,21 @@ export function press(state, input, key, commands = [], files = null) {
181
278
  if (input === 'u') {
182
279
  // At the start of a line nothing of it is before the cursor: the line above goes instead, so pressing it again clears a box of many lines.
183
280
  const start = lineStart(state);
184
- return { state: cut(state, start === cursor && cursor > 0 ? lineStart({ text: state.text, cursor: cursor - 1 }) : start, cursor) };
281
+ return { state: killed(state, start === cursor && cursor > 0 ? lineStart({ text: state.text, cursor: cursor - 1 }) : start, cursor) };
185
282
  }
186
- if (input === 'k') return { state: cut(state, cursor, lineEnd(state)) };
187
- if (input === 'w') return { state: cut(state, wordLeft(state), cursor) };
283
+ if (input === 'k') return { state: killed(state, cursor, lineEnd(state)) };
284
+ if (input === 'w') return { state: killed(state, wordLeft(state), cursor) };
188
285
  if (input === 'd') return { state: cut(state, cursor, cursor + under(state).length) };
286
+ // What Ctrl-U, Ctrl-K, Ctrl-W or Option-Backspace cut, put back where the cursor is.
287
+ if (input === 'y') return { state: state.kill ? insert(state, state.kill) : state };
288
+ if (input === 'b') return { state: { ...state, cursor: cursor - before(state).length } };
289
+ if (input === 'f') return { state: { ...state, cursor: cursor + under(state).length } };
189
290
  return { state };
190
291
  }
191
292
  if (key.meta) {
192
293
  if (input === 'b') return { state: { ...state, cursor: wordLeft(state) } };
193
294
  if (input === 'f') return { state: { ...state, cursor: wordRight(state) } };
295
+ if (input === 'd') return { state: killed(state, cursor, wordRight(state)) };
194
296
  return { state };
195
297
  }
196
298
  if (!input) return { state };
package/src/export.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
2
- import { join } from 'node:path';
1
+ import { chmodSync, copyFileSync, existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync } from 'node:fs';
2
+ import { basename, dirname, join, resolve } from 'node:path';
3
3
  import { getMeta, setMeta } from './db.mjs';
4
+ import { UsageError } from './memory.mjs';
4
5
  import { paths } from './paths.mjs';
5
6
  import { DatabaseSync } from './sqlite.mjs';
6
7
 
@@ -89,20 +90,117 @@ export function backup(db, now = new Date()) {
89
90
  return { file, pruned: old.length };
90
91
  }
91
92
 
93
+ /**
94
+ * The copies a damaged database can be put back from, newest first: the weekly backups, and the copies made before a
95
+ * migration (`memory.db.pre-v<N>.bak`). An empty one — a copy a process died making — is no copy.
96
+ */
97
+ function backupsOnHand() {
98
+ const { backups, db } = paths();
99
+ const found = [];
100
+ const listed = (dir, keep) => {
101
+ try {
102
+ for (const name of readdirSync(dir)) if (keep(name)) found.push(join(dir, name));
103
+ } catch {
104
+ // Not there yet: nothing kept there.
105
+ }
106
+ };
107
+ listed(backups, (name) => /^memory-.*\.db$/.test(name));
108
+ listed(dirname(db), (name) => name.startsWith(`${basename(db)}.pre-v`) && name.endsWith('.bak'));
109
+ return found
110
+ .map((file) => ({ file, stat: statSync(file) }))
111
+ .filter(({ stat }) => stat.size > 0)
112
+ .map(({ file, stat }) => ({ file, at: stat.mtime }))
113
+ .sort((a, b) => b.at - a.at);
114
+ }
115
+
116
+ const minute = (date) => date.toISOString().slice(0, 16).replace('T', ' ');
117
+
118
+ /** What to say when the database cannot be opened: the newest copy, and the command that puts it back. */
119
+ export function restoreHint() {
120
+ const [newest] = backupsOnHand();
121
+ return newest ? `the newest backup is ${newest.file} (${minute(newest.at)}): sumo backup restore puts it back, keeping this one aside` : `no backup was found to put back (${paths().backups} is empty)`;
122
+ }
123
+
124
+ /** Whether an error is SQLite saying the file is not a database it can read. */
125
+ export const damaged = (cause) => /file is not a database|database disk image is malformed|SQLITE_(?:CORRUPT|NOTADB)/i.test(String(cause?.message ?? cause));
126
+
127
+ const memoriesIn = (n) => `${n} memor${n === 1 ? 'y' : 'ies'}`;
128
+
129
+ /**
130
+ * The copy a restore would put back — `file`, or the newest on hand — checked before anything moves: a copy SQLite cannot
131
+ * read changes nothing. `{ from, at, memories }`.
132
+ */
133
+ export function restoreCandidate(file = null) {
134
+ const chosen = file ? resolve(file) : backupsOnHand()[0]?.file;
135
+ if (!chosen) throw new UsageError(`no backup to put back: none in ${paths().backups}, and no copy made before a migration`);
136
+ if (!existsSync(chosen)) throw new UsageError(`${chosen} does not exist`);
137
+ try {
138
+ const copy = new DatabaseSync(chosen, { readOnly: true });
139
+ try {
140
+ const checked = Object.values(copy.prepare('PRAGMA quick_check').get())[0];
141
+ if (checked !== 'ok') throw new Error(checked);
142
+ return { from: chosen, at: statSync(chosen).mtime, memories: copy.prepare('SELECT COUNT(*) AS n FROM memories').get().n };
143
+ } finally {
144
+ copy.close();
145
+ }
146
+ } catch (cause) {
147
+ throw new UsageError(`${chosen} is not a usable database (${cause.message}) — nothing was changed`);
148
+ }
149
+ }
150
+
151
+ /** The database in place, when it opens and checks out: `{ memories }`. Null when it is missing or damaged — nothing to lose by replacing it. */
152
+ export function liveDatabase() {
153
+ const { db } = paths();
154
+ if (!existsSync(db)) return null;
155
+ try {
156
+ const live = new DatabaseSync(db, { readOnly: true });
157
+ try {
158
+ if (Object.values(live.prepare('PRAGMA quick_check').get())[0] !== 'ok') return null;
159
+ return { memories: live.prepare('SELECT COUNT(*) AS n FROM memories').get().n };
160
+ } finally {
161
+ live.close();
162
+ }
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
167
+
168
+ /** What is asked before a copy goes over a database that is fine: both sides, counted, then a short question. */
169
+ export const restoreQuestion = (copy, live) =>
170
+ `memory.db is fine, with ${memoriesIn(live.memories)}. The copy is ${copy.from}, backed up ${minute(copy.at)}, with ${memoriesIn(copy.memories)}.\nPut the copy in its place, keeping the one there now aside? [y/N] `;
171
+
172
+ /**
173
+ * Puts a checked copy back. The database it replaces is kept aside with its -wal and -shm — as `memory.db.damaged-<time>`,
174
+ * or `memory.db.replaced-<time>` when it was fine (`live`): a restore never deletes. The copy is the user's only, at 0600.
175
+ * Returns the lines the user reads.
176
+ */
177
+ export function restoreLines(copy, live, now = new Date()) {
178
+ const { db } = paths();
179
+ const aside = `${db}.${live ? 'replaced' : 'damaged'}-${now.toISOString().replace(/[:.]/g, '-')}`;
180
+ for (const side of ['', '-wal', '-shm']) if (existsSync(`${db}${side}`)) renameSync(`${db}${side}`, `${aside}${side}`);
181
+ copyFileSync(copy.from, db);
182
+ chmodSync(db, 0o600);
183
+ const kept = !existsSync(aside) ? '' : live ? `; the database it replaced (${memoriesIn(live.memories)}) is kept as ${aside}` : `; the damaged database is kept as ${aside}`;
184
+ return [`restored ${copy.from} (backed up ${minute(copy.at)}) — ${memoriesIn(copy.memories)}${kept}`];
185
+ }
186
+
92
187
  /**
93
188
  * A purge reaches the backups too: "erased, not recoverable" must hold for every copy this machine keeps. Each backup
94
189
  * loses the memory and the sentence it came from, and is rewritten so the pages that held them are gone.
95
190
  * Returns how many were cleaned, and the ones that could not be — said, never hidden.
96
191
  */
97
192
  export function purgeFromBackups({ id, body, created_at: createdAt, source_turn: sourceTurn }) {
98
- const dir = paths().backups;
99
- const files = existsSync(dir) ? readdirSync(dir).filter((f) => /^memory-.*\.db$/.test(f)) : [];
193
+ const { backups, db: live } = paths();
194
+ const within = (dir, keep) => (existsSync(dir) ? readdirSync(dir).filter(keep).map((f) => join(dir, f)) : []);
195
+ // The weekly copies, and every copy set aside beside the database: before a migration, or by a restore.
196
+ const aside = (f) => f.startsWith(`${basename(live)}.`) && (/\.pre-v\d+\.bak$/.test(f) || /\.(?:damaged|replaced)-[^/]*$/.test(f)) && !/-(?:wal|shm)$/.test(f);
197
+ const files = [...within(backups, (f) => /^memory-.*\.db$/.test(f)), ...within(dirname(live), aside)];
100
198
  const failed = [];
101
199
  let cleaned = 0;
102
- for (const f of files) {
200
+ for (const file of files) {
103
201
  let db = null;
104
202
  try {
105
- db = new DatabaseSync(join(dir, f));
203
+ db = new DatabaseSync(file);
106
204
  db.exec('PRAGMA secure_delete = ON;');
107
205
  // The same memory, not just the same number: a backup from after a restore may hold another one under this id.
108
206
  const row = db.prepare('SELECT source_turn FROM memories WHERE id = ? AND body = ? AND created_at = ?').get(id, body, createdAt);
@@ -123,7 +221,7 @@ export function purgeFromBackups({ id, body, created_at: createdAt, source_turn:
123
221
  cleaned++;
124
222
  }
125
223
  } catch {
126
- failed.push(join(dir, f));
224
+ failed.push(file);
127
225
  } finally {
128
226
  db?.close();
129
227
  }
package/src/guard.mjs CHANGED
@@ -14,10 +14,11 @@ import { homedir } from 'node:os';
14
14
 
15
15
  /**
16
16
  * A file whose contents are a secret: reading it is refused, and a change that adds one is never DONE. A glob (`.env*`) names them too.
17
- * Any case: the disk a Mac ships with reads `.ENV` as `.env`. A redirect (`<.env`) is a reading too.
17
+ * Any case: the disk a Mac ships with reads `.ENV` as `.env`. A redirect (`<.env`) is a reading too, and so is a file at a
18
+ * revision (`HEAD:.env`).
18
19
  */
19
20
  const SECRET_FILE =
20
- /(^|[\s/'"=<])(\.env[*?]*(\.(?!example\b|sample\b|template\b)[\w*?-]+)?|\.netrc|\.git-credentials|\.aws\/credentials|\.ssh\/(?!known_hosts\b|config\b|authorized_keys\b)[\w.-]+(?<!\.pub)|id_(rsa|dsa|ecdsa|ed25519)(?!\.pub)|[\w.-]+\.(pem|key|p12|pfx))(?=$|[\s'"|;&)])/i;
21
+ /(^|[\s/'"=<:])(\.env[*?]*(\.(?!example\b|sample\b|template\b)[\w*?-]+)?|\.netrc|\.git-credentials|\.aws\/credentials|\.ssh\/(?!known_hosts\b|config\b|authorized_keys\b)[\w.-]+(?<!\.pub)|id_(rsa|dsa|ecdsa|ed25519)(?!\.pub)|[\w.-]+\.(pem|key|p12|pfx))(?=$|[\s'"|;&)])/i;
21
22
 
22
23
  /** Anything that would put a file's contents in front of the model, `$(< file)` among them. `source .env` is not on it: that sets variables without showing them. */
23
24
  const PRINTS = /(^|[\s;&|($`])(cat|head|tail|less|more|bat|grep|rg|egrep|fgrep|sed|awk|cut|strings|xxd|hexdump|od|base64|nl|tac|view|vim?|nano|code|open|pbcopy|jq|yq)\s|\$\(\s*</;
@@ -25,22 +26,67 @@ const PRINTS = /(^|[\s;&|($`])(cat|head|tail|less|more|bat|grep|rg|egrep|fgrep|s
25
26
  /** Where one simple command ends and the next begins: a separator, a pipe, a new line, a subshell or a substitution. */
26
27
  const SEPARATORS = /[;&|\n()`]|\$\(/;
27
28
 
28
- /** A secret file bound to a name instead of read: `f=.env`, `for f in .env .env.local`. What later prints `$f` is a reading of it. */
29
- const BINDS = /(^|[\s;&|(`])(?:\w+=|for\s+\w+\s+in\s)/;
29
+ /**
30
+ * Commands that name a secret file without putting what is in it in front of the model: loading it (`source`, `.`),
31
+ * listing, testing, touching or removing it, changing its mode, and `echo`/`printf`, which print the name.
32
+ */
33
+ const NAMES_ONLY = new Set(['source', '.', 'ls', 'test', '[', '[[', 'touch', 'chmod', 'chown', 'rm', 'stat', 'file', 'echo', 'printf', 'mkdir']);
34
+ /** Of git's commands, those that name a file without printing it. `git diff` and `git show` print it. */
35
+ const GIT_NAMES_ONLY = new Set(['add', 'rm', 'mv', 'status', 'check-ignore', 'ls-files', 'update-index', 'restore']);
36
+ /** Commands that copy a file: harmless only when the secret is where it goes, never where it comes from. */
37
+ const COPIES = new Set(['cp', 'mv', 'install', 'ln']);
38
+ /** Words in front of a command that leave it the same command. */
39
+ const PREFIXES = new Set(['sudo', 'command', 'builtin', 'nohup', 'time', 'exec', 'env']);
40
+ /** A here-document whose delimiter is quoted: its body is text, nothing in it runs or expands. */
41
+ const QUOTED_HEREDOC = /<<-?\s*(['"])(\w+)\1[^\n]*\n[\s\S]*?\n\s*\2[ \t]*(?=\n|$)/g;
42
+ /** A shell started on a command string: what matters is the command inside the quotes. */
43
+ const SHELL_C = /\b(?:ba|z)?sh\s+-l?c\s+/g;
44
+ const isSecret = (word) => SECRET_FILE.test(` ${word} `);
45
+
46
+ /**
47
+ * Whether one simple command reads a secret file it names. Read narrowly the other way round from a list of printing
48
+ * commands, which a reader nobody listed (`sort`, `diff`, a one-line script) walks past: it reads it unless what it does
49
+ * with it is one of the few things that never show what is in it — loading, listing, writing into it, copying onto it,
50
+ * git's bookkeeping, or handing it to a container as its environment.
51
+ */
52
+ function readsSecret(part) {
53
+ // Written into, not read: a redirect's target is not an argument.
54
+ const rest = part.replace(/\d*>>?\s*\S+/g, ' ');
55
+ if (!SECRET_FILE.test(rest)) return false;
56
+ // `$(< .env)` was split at its `$(`: what is left starts with the redirect.
57
+ if (/^\s*</.test(rest)) return true;
58
+ const words = rest.trim().replace(/^["']+/, '').split(/\s+/).filter(Boolean);
59
+ let i = 0;
60
+ while (i < words.length && (PREFIXES.has(words[i]) || /^\w+=/.test(words[i]))) i++;
61
+ const [command, sub] = [words[i], words[i + 1]];
62
+ if (NAMES_ONLY.has(command)) return false;
63
+ if (command === 'git' && GIT_NAMES_ONLY.has(sub)) return false;
64
+ if (COPIES.has(command)) return !isSecret(words.at(-1));
65
+ if (command === 'docker' || command === 'podman') return SECRET_FILE.test(rest.replace(/--env-file[=\s]+\S+/g, ' '));
66
+ return true;
67
+ }
30
68
 
31
69
  /**
32
- * Whether a command puts a secret file's contents in front of the model: a printing command with the file among its
33
- * words — in the same simple command, not merely somewhere in the same line, so `source .env && npm test | tail` is
34
- * what it looks like — or a printing command given a variable the file was bound to earlier in the line.
70
+ * Whether a command puts a secret file's contents in front of the model: one of its simple commands reads the file —
71
+ * judged command by command, not across the line, so `source .env && npm test | tail` is what it looks like — or a
72
+ * printing command is given a variable the file was bound to earlier in the line.
35
73
  */
36
74
  function printsSecret(command) {
37
- const parts = command.replace(/\\\n/g, ' ').split(SEPARATORS);
75
+ const parts = command.replace(/\\\n/g, ' ').replace(QUOTED_HEREDOC, '<<heredoc').replace(SHELL_C, ' ').split(SEPARATORS);
38
76
  let bound = false;
39
77
  for (const part of parts) {
40
78
  if (!SECRET_FILE.test(part)) continue;
41
- // `$(< .env)` was split at its `$(`: what is left starts with the redirect.
42
- if (PRINTS.test(part) || /^\s*</.test(part)) return true;
43
- if (BINDS.test(part)) bound = true;
79
+ // Bound to a name, not read: `f=.env`, `export ENV_FILE=.env`, `for f in .env …`. What later prints `$f` is a reading.
80
+ const first = part.trim().split(/\s+/)[0];
81
+ if (/^\w+=/.test(first) && part.trim().split(/\s+/).every((w) => /^\w+=/.test(w))) {
82
+ bound = true;
83
+ continue;
84
+ }
85
+ if (first === 'export' || first === 'for') {
86
+ bound = true;
87
+ continue;
88
+ }
89
+ if (readsSecret(part)) return true;
44
90
  }
45
91
  return bound && parts.some((part) => PRINTS.test(part) && part.includes('$'));
46
92
  }
package/src/guides.mjs CHANGED
@@ -1,8 +1,6 @@
1
1
  import { readdirSync, readFileSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
- import { getMeta } from './db.mjs';
4
3
  import { REPO_ROOT } from './paths.mjs';
5
- import { CONFIG_DEFAULTS } from './setup.mjs';
6
4
  import { stem, words } from './text.mjs';
7
5
 
8
6
  /**
@@ -82,5 +80,8 @@ export function renderGuide(name) {
82
80
  return `<guide ${name}>\nThe user is asking for ${asking}. This is how that is done here — follow it in order:\n${body}\n</guide>`;
83
81
  }
84
82
 
85
- /** `guides.auto`: whether guides ride in unasked and the end of a turn is held for an unchecked edit. */
86
- export const autoGuides = (db) => (getMeta(db, 'config.guides.auto') ?? CONFIG_DEFAULTS['guides.auto']) !== 'off';
83
+ /**
84
+ * Whether guides ride in unasked and the end of a turn is held for an unchecked edit: always, for the user. The coding
85
+ * eval's `--no-guides` switches them off for its own run (SUMO_AGENTS_GUIDES=off), to measure what a guide is worth.
86
+ */
87
+ export const autoGuides = () => process.env.SUMO_AGENTS_GUIDES !== 'off';
package/src/herdr.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { spawnSync } from 'node:child_process';
2
- import { paths } from './paths.mjs';
2
+ import { existsSync } from 'node:fs';
3
+ import { paths, SELF } from './paths.mjs';
3
4
 
4
5
  /**
5
6
  * Herdr is where a delegated job can be watched. The job itself runs in the chat's process; inside Herdr it also
@@ -11,7 +12,7 @@ import { paths } from './paths.mjs';
11
12
  export const runHerdr = (args) => spawnSync('herdr', args, { encoding: 'utf8', timeout: 10_000 });
12
13
 
13
14
  /** The name a job goes by in Herdr's lists: `j33 scout`. */
14
- export const agentLabel = (job) => `j${job.id} ${job.agent}`;
15
+ const agentLabel = (job) => `j${job.id} ${job.agent}`;
15
16
 
16
17
  /**
17
18
  * What the job's own process tells Herdr about itself, when it runs in a pane: working while it works, blocked
@@ -37,7 +38,9 @@ export function openWatchTab(herdr, { job, project, env = process.env, onFail =
37
38
  if (tab.error) throw new Error(tab.error);
38
39
  // Marked as the tab's own, so the watcher may close it behind a job that is done; the home goes with the command,
39
40
  // or a chat on another home would be watched from the default one.
40
- const command = `env SUMO_JOB_TAB=1 SUMO_AGENTS_HOME=${shellWord(paths().home)} ${shellWord(paths().launcher)} job watch ${job.id}`;
41
+ // The launcher setup made, where there is one; else this very sumo, node and entry by their full paths — a clone not set up yet still has a watcher.
42
+ const sumo = existsSync(paths().launcher) ? shellWord(paths().launcher) : SELF.map(shellWord).join(' ');
43
+ const command = `env SUMO_JOB_TAB=1 SUMO_AGENTS_HOME=${shellWord(paths().home)} ${sumo} job watch ${job.id}`;
41
44
  startWatcher(herdr, { pane: tab.id, job: job.id, command, sleep })
42
45
  .then((started) => started || onFail('its Herdr tab never started the watcher'))
43
46
  .catch((cause) => onFail(cause.message));
@@ -75,7 +78,9 @@ const screen = (herdr, pane) => {
75
78
  */
76
79
  export async function startWatcher(herdr, { pane, job, command, sleep = pause, tries = TRIES }) {
77
80
  const watching = new RegExp(`job watch ${job}(?!\\d)`);
78
- const started = (info) => info.foreground_processes?.some((p) => watching.test(String(p.cmdline))) || screen(herdr, pane)?.includes(`⏺ j${job} `);
81
+ // The record's first line: the job named in the ledger's gutter, then who works on it.
82
+ const shown = new RegExp(`(?:^|\\n) *j${job} {2,}\\S`);
83
+ const started = (info) => info.foreground_processes?.some((p) => watching.test(String(p.cmdline))) || shown.test(screen(herdr, pane) ?? '');
79
84
  for (let attempt = 0; attempt < tries; attempt++) {
80
85
  let last = null;
81
86
  for (let waited = 0; waited < READY_MS; waited += POLL_MS) {
package/src/hooks.mjs CHANGED
@@ -1,17 +1,17 @@
1
- import { appendFileSync, mkdirSync } from 'node:fs';
2
- import { join } from 'node:path';
3
1
  import { card } from './card.mjs';
4
2
  import { getMeta, openDb, setMeta } from './db.mjs';
5
3
  import { backup } from './export.mjs';
6
4
  import { guardCommand, guardPath } from './guard.mjs';
7
5
  import { autoGuides, firingGuides, matchGuide, renderGuide } from './guides.mjs';
8
- import { paths } from './paths.mjs';
6
+ import { appendLog } from './logs.mjs';
7
+ import { expireUnusedGotchas, GOTCHA_UNUSED_DAYS } from './memory.mjs';
9
8
  import { prime } from './prime.mjs';
10
- import { detect, keepScratch, touch } from './projects.mjs';
9
+ import { detect, freshen, keepScratch, projectAt, touch } from './projects.mjs';
11
10
  import { closingQuestions, recallBeforeAsking } from './recall.mjs';
12
11
  import { line } from './render.mjs';
13
12
  import { shouldRun, spawnDetached } from './scribe.mjs';
14
- import { clearInjections, contextBand, contextNudge, contextUse, currentProject, endSession, ensureSession, injectedSlugs, markInjected, pruneStates, recordTurn, sessionsAwaitingDream } from './sessions.mjs';
13
+ import { tidyDue } from './dream.mjs';
14
+ import { clearInjections, contextBand, contextNudge, contextUse, currentProject, endSession, ensureSession, injectedSlugs, markInjected, pruneStates, recordTurn } from './sessions.mjs';
15
15
  import { claim, renderWorkflow, withoutSumoCalls } from './workflows.mjs';
16
16
 
17
17
  /**
@@ -23,10 +23,9 @@ import { claim, renderWorkflow, withoutSumoCalls } from './workflows.mjs';
23
23
  */
24
24
 
25
25
  const BACKUP_EVERY_MS = 7 * 24 * 3_600_000;
26
- const SESSIONS_TO_DREAM = 3;
27
26
 
28
27
  /** The one event shape. Field names are what the payload carries; a missing one is simply absent. */
29
- export function fieldsOf(p) {
28
+ function fieldsOf(p) {
30
29
  return {
31
30
  sessionId: p.session_id,
32
31
  cwd: p.cwd ?? null,
@@ -53,7 +52,7 @@ const deny = (reason) => JSON.stringify({ deny: reason });
53
52
  const isRead = (tool) => tool === 'read' || tool === 'view';
54
53
 
55
54
  /** The guard, first and without memory: a destructive command or a secret file is refused on sight. */
56
- export function earlyRefusal(e) {
55
+ function earlyRefusal(e) {
57
56
  if (e.toolName === 'bash' && e.command) return guardCommand(e.command, { cwd: e.cwd }) ?? '';
58
57
  if (isRead(e.toolName) && e.path) return guardPath(e.path) ?? '';
59
58
  return '';
@@ -91,7 +90,7 @@ function sizeNudge(db, e, now) {
91
90
  * was shown before, and whatever the switch says: the user asked for it.
92
91
  */
93
92
  function guideDue(db, e, now, taught) {
94
- const name = e.guide ?? (!taught && autoGuides(db) ? matchGuide(e.prompt) : null);
93
+ const name = e.guide ?? (!taught && autoGuides() ? matchGuide(e.prompt) : null);
95
94
  if (!name || !firingGuides().some((g) => g.name === name)) return '';
96
95
  const slug = `guide:${name}`;
97
96
  if (!e.guide && db.prepare('SELECT 1 FROM session_injections WHERE session_id = ? AND slug = ?').get(e.sessionId, slug)) return '';
@@ -106,7 +105,7 @@ function guideDue(db, e, now, taught) {
106
105
  * left alone.
107
106
  */
108
107
  function unchecked(db, e) {
109
- if (e.edited.length === 0 || !autoGuides(db)) return '';
108
+ if (e.edited.length === 0 || !autoGuides()) return '';
110
109
  const first = e.edited.slice(0, 3);
111
110
  const rest = e.edited.length > 3 ? `${e.edited.length - 3} more` : first.length > 1 ? first.pop() : null;
112
111
  const shown = rest ? `${first.join(', ')} and ${rest}` : first[0];
@@ -125,8 +124,8 @@ const HANDLERS = {
125
124
  // A fresh start wipes the cards from context; forgetting they were shown lets them come back.
126
125
  if (e.source === 'compact' || e.source === 'new') clearInjections(db, e.sessionId);
127
126
 
128
- if (shouldRun(db, { event: 'session-start', now })) spawnDetached(['scribe', 'run']);
129
- if (sessionsAwaitingDream(db, now, SESSIONS_TO_DREAM).length >= SESSIONS_TO_DREAM) spawnDetached(['dream', 'run']);
127
+ // One memory pass: it files what waits, and tidies too when conversations have finished or a week has gone by.
128
+ if (shouldRun(db, { event: 'session-start', now }) || tidyDue(db, now)) spawnDetached(['memory', 'run']);
130
129
 
131
130
  const last = getMeta(db, 'last_backup');
132
131
  if (!last || Date.parse(now) - Date.parse(last) > BACKUP_EVERY_MS) {
@@ -149,8 +148,16 @@ const HANDLERS = {
149
148
  // And this: scratch that cannot be made or tidied is said in the hook log; the session still gets its memory.
150
149
  logFailure('session-start scratch', cause);
151
150
  }
151
+ try {
152
+ // A trap nobody was ever shown, filed by an agent and never confirmed, lets go after a while; history keeps it.
153
+ const expired = expireUnusedGotchas(db, now);
154
+ if (expired > 0) appendLog('hook.log', `${now} session-start: ${expired} gotcha${expired === 1 ? '' : 's'} never shown in ${GOTCHA_UNUSED_DAYS} days let go (sumo history shows them)\n`);
155
+ } catch (cause) {
156
+ logFailure('session-start expire', cause);
157
+ }
152
158
 
153
- return prime(db, { now });
159
+ // The questions put to the user are this session's: about everything, or the project it was started in.
160
+ return prime(db, { now, project: currentProject(db, e.sessionId) ?? (e.cwd ? projectAt(db, e.cwd) : null) });
154
161
  },
155
162
 
156
163
  prompt(db, e, now) {
@@ -159,12 +166,22 @@ const HANDLERS = {
159
166
 
160
167
  const shown = new Set(injectedSlugs(db, e.sessionId));
161
168
  const cards = [];
162
- for (const slug of detect(db, e.prompt)) {
169
+ // A session started inside a project, whose first words name none, is about that project: it is the one meant.
170
+ const named = detect(db, e.prompt);
171
+ const here = named.length === 0 && shown.size === 0 && e.cwd ? projectAt(db, e.cwd) : null;
172
+ for (const slug of here ? [here] : named) {
163
173
  touch(db, slug, now);
164
174
  // Every mention is stamped, not only the first: the project named last is the one the session is about, also when its card was shown before.
165
175
  markInjected(db, e.sessionId, slug, now);
166
176
  if (shown.has(slug)) continue;
167
- cards.push(card(db, slug, { now }));
177
+ // The card is built from the repository as it is now: a scan a week old, or older than a file it reads, is redone.
178
+ try {
179
+ freshen(db, slug, now);
180
+ } catch (cause) {
181
+ logFailure('prompt rescan', cause);
182
+ }
183
+ // Its questions come with it, unless the block this session started with asked them: a session started inside the project.
184
+ cards.push(card(db, slug, { now, asks: slug !== (e.cwd ? projectAt(db, e.cwd) : null) }));
168
185
  }
169
186
 
170
187
  const asked = claim(db, { sessionId: e.sessionId, text: e.prompt, scopes: scopesOf(db, e.sessionId), now });
@@ -207,7 +224,7 @@ const HANDLERS = {
207
224
 
208
225
  stop(db, e, now) {
209
226
  ensureSession(db, { id: e.sessionId, cwd: e.cwd, transcriptPath: e.transcriptPath, now });
210
- if (shouldRun(db, { event: 'stop', now })) spawnDetached(['scribe', 'run']);
227
+ if (shouldRun(db, { event: 'stop', now })) spawnDetached(['memory', 'run']);
211
228
 
212
229
  // The other gates: a turn that ends on an edit nothing checked, or by asking the user something memory may
213
230
  // already hold. Already continuing because of one → whatever the turn ends on now goes to the user.
@@ -220,7 +237,7 @@ const HANDLERS = {
220
237
  'session-end'(db, e, now) {
221
238
  ensureSession(db, { id: e.sessionId, cwd: e.cwd, transcriptPath: e.transcriptPath, now });
222
239
  endSession(db, e.sessionId, now);
223
- if (shouldRun(db, { event: 'session-end', now })) spawnDetached(['scribe', 'run']);
240
+ if (shouldRun(db, { event: 'session-end', now })) spawnDetached(['memory', 'run']);
224
241
  return '';
225
242
  },
226
243
  };
@@ -239,8 +256,7 @@ export function handleEvent(db, event, payload, now = new Date().toISOString())
239
256
  /** What went wrong, in logs/hook.log. If even the log cannot be written there is nothing left to do but stay out of the way. */
240
257
  function logFailure(what, cause) {
241
258
  try {
242
- mkdirSync(paths().logs, { recursive: true, mode: 0o700 });
243
- appendFileSync(join(paths().logs, 'hook.log'), `${new Date().toISOString()} ${what}: ${cause.stack ?? cause}\n`);
259
+ appendLog('hook.log', `${new Date().toISOString()} ${what}: ${cause.stack ?? cause}\n`);
244
260
  } catch {
245
261
  // Said above.
246
262
  }