sumo-agents 0.1.0 → 0.4.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.
@@ -0,0 +1,173 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { existsSync, realpathSync, statSync } from 'node:fs';
3
+ import { homedir, tmpdir } from 'node:os';
4
+ import { dirname, join, resolve } from 'node:path';
5
+ import { paths } from './paths.mjs';
6
+
7
+ /**
8
+ * The OS sandbox around the model's shell: macOS Seatbelt, the way Codex and Claude Code wrap their commands. Reads and
9
+ * the network stay open — git over ssh reads ~/.ssh, a smoke test calls out, and the user chose to give the shell their
10
+ * environment — while writes are held to the project, its git directory, the temporary folders, Sumo's own home and
11
+ * the tool caches, and never reach git's hooks or config, which run later outside it. The user can close the network
12
+ * past this machine (`shell.network off`). The guard refuses the commands it knows; this stops the ones it does not,
13
+ * whatever they are spelled as.
14
+ * `sandbox-exec` execs into the shell in place, so the shell keeps its pid and its process group, and a stop still reaches it.
15
+ */
16
+
17
+ /** Only the one in /usr/bin: one found on the PATH could be anything. */
18
+ const SANDBOX_EXEC = '/usr/bin/sandbox-exec';
19
+
20
+ /** The caches under the home a build or an install writes into: each is writable when it exists. */
21
+ const TOOL_CACHES = ['.npm', 'Library/Caches', '.cache', 'go/pkg', '.cargo/registry', '.cargo/git', 'Library/pnpm', '.bun/install/cache', '.gradle/caches', '.m2/repository'];
22
+
23
+ /** What is printed for a write the sandbox refused — Codex looks for the same two — in any case: Node says `EPERM: operation not permitted`. */
24
+ const BLOCKED = /operation not permitted|read-only file system/i;
25
+
26
+ /** Why the model's shell cannot be sandboxed here, or null when it can. */
27
+ export function unsandboxable({ platform = process.platform, exec = SANDBOX_EXEC } = {}) {
28
+ if (platform !== 'darwin') return 'there is a sandbox for it only on macOS so far';
29
+ if (!existsSync(exec)) return `${exec} is missing`;
30
+ return null;
31
+ }
32
+
33
+ const real = (path) => {
34
+ try {
35
+ return realpathSync(path);
36
+ } catch {
37
+ return null;
38
+ }
39
+ };
40
+
41
+ /** Each root's git common directory, asked of git once: a root does not turn into a worktree of another repository halfway through. */
42
+ const commonDirs = new Map();
43
+
44
+ /** The git directory a root's history is written to, when it lies outside the root — a worktree's is in the main checkout. */
45
+ function gitCommonDir(root) {
46
+ if (!commonDirs.has(root)) {
47
+ let dir = null;
48
+ try {
49
+ const said = execFileSync('git', ['rev-parse', '--git-common-dir'], { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5_000 }).trim();
50
+ dir = said ? real(resolve(root, said)) : null;
51
+ } catch {
52
+ // Not in a repository, or no git: there is nothing outside the root to write history to.
53
+ }
54
+ commonDirs.set(root, dir && dir !== root && !dir.startsWith(`${root}/`) ? dir : null);
55
+ }
56
+ return commonDirs.get(root);
57
+ }
58
+
59
+ /** Each root's hooks directory when git's config moves it, asked of git once, like the common directory. */
60
+ const hooksPaths = new Map();
61
+
62
+ /**
63
+ * Where git runs a root's hooks from when its config sets `core.hooksPath` — husky's `.husky/_` — by its real path where it
64
+ * is there, else as git would find it: a relative one from the top of the working tree, where git runs its hooks. Null when unset.
65
+ */
66
+ function hooksPathOf(root) {
67
+ if (!hooksPaths.has(root)) {
68
+ let dir = null;
69
+ try {
70
+ const ask = (...args) => execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5_000 }).trim();
71
+ const said = ask('config', '--type=path', '--get', 'core.hooksPath');
72
+ if (said) {
73
+ const path = resolve(ask('rev-parse', '--show-toplevel') || root, said);
74
+ dir = real(path) ?? path;
75
+ }
76
+ } catch {
77
+ // Not set, not in a repository, or no git: the hooks are in the git directory, kept already.
78
+ }
79
+ hooksPaths.set(root, dir);
80
+ }
81
+ return hooksPaths.get(root);
82
+ }
83
+
84
+ /** The per-user cache folder macOS gives every program — clang's module cache and swift's are in it — asked of getconf once a process; null where there is none. */
85
+ let userCacheDir;
86
+ function darwinUserCache() {
87
+ if (userCacheDir === undefined) {
88
+ try {
89
+ userCacheDir = execFileSync('/usr/bin/getconf', ['DARWIN_USER_CACHE_DIR'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5_000 }).trim() || null;
90
+ } catch {
91
+ userCacheDir = null;
92
+ }
93
+ }
94
+ return userCacheDir;
95
+ }
96
+
97
+ /**
98
+ * The places a sandboxed command may write, by their real paths — the sandbox sees through a link — each once: every
99
+ * root and the git directory it commits to, the temporary folders, Sumo's home (the shell runs `sumo job note` and
100
+ * `sumo job finish`, which write there), the per-user cache macOS gives compilers, and the tool caches that exist. A
101
+ * place that does not exist is left out.
102
+ */
103
+ export function writablePlaces(roots, { home = homedir(), tmp = tmpdir(), sumo = paths().home, userCache = darwinUserCache() } = {}) {
104
+ const places = [];
105
+ for (const root of roots.map(real).filter(Boolean)) places.push(root, gitCommonDir(root));
106
+ places.push(real(tmp), real('/private/tmp'), real(sumo), userCache && real(userCache), ...TOOL_CACHES.map((cache) => real(join(home, cache))));
107
+ return [...new Set(places.filter(Boolean))];
108
+ }
109
+
110
+ const isDirectory = (path) => {
111
+ try {
112
+ return statSync(path).isDirectory();
113
+ } catch {
114
+ return false;
115
+ }
116
+ };
117
+
118
+ /**
119
+ * What stays read-only inside the places a command may write, by real paths: git's hooks and its config, which run or
120
+ * are read later outside the sandbox — a hook on the user's next commit — in each root's git directory and in the
121
+ * common one a worktree commits into, outside it; a root's `.git` that is a file, which says where its git
122
+ * directory is; and the hooks directory git's config names instead, where it does — husky's `.husky/_` is in the project.
123
+ * Only for a repository that is there when the command starts: `git init` makes its own.
124
+ */
125
+ export function readOnlyPlaces(roots) {
126
+ const places = [];
127
+ for (const root of roots.map(real).filter(Boolean)) {
128
+ const dotGit = join(root, '.git');
129
+ const gitDir = real(dotGit);
130
+ if (gitDir && isDirectory(gitDir)) places.push(join(gitDir, 'hooks'), join(gitDir, 'config'));
131
+ else if (gitDir) places.push(gitDir);
132
+ const common = gitCommonDir(root);
133
+ if (common) places.push(join(common, 'hooks'), join(common, 'config'));
134
+ const hooks = hooksPathOf(root);
135
+ if (hooks) places.push(hooks);
136
+ }
137
+ return [...new Set(places)];
138
+ }
139
+
140
+ /** Every directory above a path, up to and without the root of the disk. */
141
+ function ancestors(path) {
142
+ const above = [];
143
+ for (let up = dirname(path); up !== dirname(up); up = dirname(up)) above.push(up);
144
+ return above;
145
+ }
146
+
147
+ /**
148
+ * What a command runs under: sandbox-exec with a profile that allows everything but writing, then writing to each
149
+ * place — given as a parameter, so no path is ever spelled inside the profile — but never to what is kept read-only in
150
+ * them, carved out with require-not as Codex does; and to the devices every shell writes to. No directory above a path
151
+ * kept read-only can be moved or removed: renamed aside, what is under it would be out of the carve-out, and could be
152
+ * written and moved back. With the network off, nothing past this machine is reached; loopback — a dev server, a
153
+ * test's own — and local sockets stay open. The shell and its command follow it.
154
+ */
155
+ export function sandboxWrap(writable, { readOnly = [], network = true } = {}) {
156
+ const places = writable.map((_, i) => `(subpath (param "W${i}"))`).join(' ');
157
+ const kept = readOnly.map((_, i) => `(require-not (literal (param "R${i}"))) (require-not (subpath (param "R${i}")))`).join(' ');
158
+ const above = [...new Set(readOnly.flatMap(ancestors))];
159
+ const rules = [
160
+ '(version 1)',
161
+ '(allow default)',
162
+ '(deny file-write*)',
163
+ `(allow file-write* ${readOnly.length === 0 ? places : `(require-all (require-any ${places}) ${kept})`} (literal "/dev/null") (regex #"^/dev/(tty|fd/|zero|dtracehelper)"))`,
164
+ ];
165
+ if (!network) rules.push('(deny network-outbound (remote ip))', '(allow network-outbound (remote ip "localhost:*"))');
166
+ // Last, so no allowance after it opens the unlink a rename is made of.
167
+ if (above.length > 0) rules.push(`(deny file-write-unlink (require-all (vnode-type DIRECTORY) (require-any ${above.map((_, i) => `(literal (param "A${i}"))`).join(' ')})))`);
168
+ const params = [...writable.map((place, i) => `-DW${i}=${place}`), ...readOnly.map((place, i) => `-DR${i}=${place}`), ...above.map((place, i) => `-DA${i}=${place}`)];
169
+ return [SANDBOX_EXEC, '-p', rules.join('\n'), ...params, '--'];
170
+ }
171
+
172
+ /** Whether what a failed command printed says a write was refused. */
173
+ export const blockedWrite = (output) => BLOCKED.test(output);
package/src/scribe.mjs CHANGED
@@ -8,7 +8,7 @@ import { MODELS, offLine, usableModels } from './catalog.mjs';
8
8
  import { getMeta, openDb, setMeta, tx } from './db.mjs';
9
9
  import { localServerStatus } from './local-server.mjs';
10
10
  import { callModel, failure, logRun } from './model.mjs';
11
- import { ENTRY, paths, REPO_ROOT } from './paths.mjs';
11
+ import { paths, REPO_ROOT, SELF } from './paths.mjs';
12
12
  import { aliasesOf, listProjects } from './projects.mjs';
13
13
  import { line } from './render.mjs';
14
14
  import { currentProject, markScribed, pendingTurns } from './sessions.mjs';
@@ -64,7 +64,7 @@ export function spawnDetached(args) {
64
64
  appendFileSync(process.env.SUMO_AGENTS_SPAWN_LOG, `${args.join(' ')}\n`);
65
65
  return;
66
66
  }
67
- const child = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', ENTRY, ...args], {
67
+ const child = spawn(SELF[0], [...SELF.slice(1), ...args], {
68
68
  detached: true,
69
69
  stdio: 'ignore',
70
70
  env: process.env,
@@ -155,18 +155,20 @@ export function opsSchema(db, ops) {
155
155
  body: { type: 'string', description: 'one plain sentence, written as a standing fact or rule' },
156
156
  turn: { type: ['integer', 'null'], description: 'the number from the [tN] tag, or null when this is an inferred pattern' },
157
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 []' },
158
160
  };
159
161
  const branch = (op, properties) => {
160
162
  const all = { op: { type: 'string', enum: [op] }, ...properties };
161
163
  return { type: 'object', properties: all, required: Object.keys(all), additionalProperties: false };
162
164
  };
163
165
  const branches = {
164
- add: branch('add', { type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote }),
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 }),
165
167
  supersede: branch('supersede', {
166
168
  old: { type: 'integer', description: 'the id of the existing memory this replaces' },
167
- type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote,
169
+ type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote, keys: field.keys,
168
170
  }),
169
- gotcha: branch('gotcha', { scope: field.scope, body: field.body }),
171
+ gotcha: branch('gotcha', { scope: field.scope, body: field.body, keys: field.keys }),
170
172
  checkpoint: slugs.length > 0
171
173
  ? branch('checkpoint', { project: { type: 'string', enum: slugs }, done: { type: 'string' }, next: { type: ['string', 'null'] } })
172
174
  : null,
package/src/sessions.mjs CHANGED
@@ -14,10 +14,12 @@ const TURN_MAX_CHARS = 4000;
14
14
  * Where a session starts costing quality. The numbers are absolute tokens, not
15
15
  * a share of the window: what degrades an answer is how much context is in
16
16
  * play, and a million-token window degrades at the same sizes a small one does.
17
+ * It is said early because every request pays for all of it again: a chat averages
18
+ * some 65k tokens a request, and it is most of what is spent.
17
19
  */
18
20
  const BANDS = [
19
21
  { band: 'act', from: 150_000 },
20
- { band: 'watch', from: 80_000 },
22
+ { band: 'watch', from: 60_000 },
21
23
  ];
22
24
 
23
25
  /** Hooks can arrive in any order and a session can outlive a restart, so every write starts by making sure the row exists. */
@@ -155,12 +157,13 @@ const inK = (tokens) => `${Math.round(tokens / 1000)}k`;
155
157
 
156
158
  /**
157
159
  * What the session is told when it crosses a band. One line, ending with the
158
- * hint, so a `/compact` can be pasted exactly as it stands.
160
+ * hint, so a `/compact` can be pasted exactly as it stands. No figure: a count
161
+ * of its own tokens is what makes a model cut its work short; the screen shows it.
159
162
  */
160
- export function contextNudge(band, tokens, focus) {
163
+ export function contextNudge(band, focus) {
161
164
  const hint = `hint: focus on ${focus}; keep decisions and open job ids in a note; drop tool output and file contents.`;
162
- if (band === 'act') return `context: ${inK(tokens)} tokens — start fresh now: note where you are (sumo job note; the end of the turn records "Left off"), then tell the user to type /new. ${hint}`;
163
- return `context: ${inK(tokens)} tokens — quality drops from here. Finish the piece in hand, then tell the user to type /new: the memory block brings the thread back. ${hint}`;
165
+ if (band === 'act') return `context: very large — start fresh now: note where you are (sumo job note; the end of the turn records "Left off"), then tell the user to type /new. ${hint}`;
166
+ return `context: large — quality drops from here. Finish the piece in hand, then tell the user to type /new: the memory block brings the thread back. ${hint}`;
164
167
  }
165
168
 
166
169
  /**
@@ -172,7 +175,7 @@ export function taskEndedNudge(db) {
172
175
  const path = db.prepare(`SELECT transcript_path FROM sessions WHERE id NOT LIKE 'job:%' ORDER BY COALESCE(last_turn_at, started_at) DESC LIMIT 1`).get()?.transcript_path ?? null;
173
176
  const use = contextUse(path);
174
177
  if (!use || !contextBand(use.tokens)) return null;
175
- return `this session is at ${inK(use.tokens)} tokens and the task just ended — a good moment for /new; the memory block brings the thread back.`;
178
+ return 'this session has grown large and the task just ended — a good moment for /new; the memory block brings the thread back.';
176
179
  }
177
180
 
178
181
  /**
@@ -232,11 +235,12 @@ export function withoutThinking(messages) {
232
235
  }
233
236
 
234
237
  /** Writes the session's state whole, then moves it into place: a crash while writing leaves the last good one. */
235
- export function saveState({ id, messages, model, effort, routed, cwd, contextTokens, now }) {
238
+ export function saveState({ id, messages, model, effort, routed, cwd, contextTokens, now, pending = [] }) {
236
239
  mkdirSync(sessionsDir(), { recursive: true, mode: 0o700 });
237
240
  const file = stateFile(id);
238
241
  // The summary first, so a listing can read it without the messages behind it; the route's reason last, since it is prose of any length.
239
- const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed };
242
+ // `pending` is what jobs reported that the model has not read yet: it goes with the session, to be read when it is picked up again.
243
+ const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed, pending };
240
244
  writeFileSync(`${file}.tmp`, JSON.stringify(state), { mode: 0o600 });
241
245
  renameSync(`${file}.tmp`, file);
242
246
  return file;
package/src/setup.mjs CHANGED
@@ -8,12 +8,12 @@ import { basename, delimiter, isAbsolute, join } from 'node:path';
8
8
  import { createInterface } from 'node:readline/promises';
9
9
  import { pipeline } from 'node:stream/promises';
10
10
  import { Readable, Transform } from 'node:stream';
11
- import { resolveAnthropicCredential } from './auth.mjs';
12
- import { assertOn, discoverModels, discoverySummary, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
11
+ import { anthropicClient, authenticatedRequest, resolveAnthropicCredential } from './auth.mjs';
12
+ import { assertOn, describeApiError, discoverModels, discoverySummary, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
13
13
  import { getMeta, openDb, SCHEMA_VERSION, schemaVersion, setMeta } from './db.mjs';
14
14
  import { stopLocalServer } from './local-server.mjs';
15
15
  import { UsageError } from './memory.mjs';
16
- import { ENTRY, paths, REPO_ROOT } from './paths.mjs';
16
+ import { COMPILED, ENTRY, paths, REPO_ROOT } from './paths.mjs';
17
17
 
18
18
  const NODE_REQUIREMENT = '22.19+ or 24.6+';
19
19
  // route.mjs's EFFORTS, spelled out: importing it would close a cycle (route → model → setup). A test keeps them equal.
@@ -25,10 +25,19 @@ export const CONFIG_DEFAULTS = {
25
25
  'scribe.model': 'local',
26
26
  'dream.model': 'local',
27
27
  'chat.model': 'opus',
28
- 'chat.effort': 'high',
28
+ 'chat.effort': 'medium',
29
29
  'chat.notify': 'on',
30
+ 'chat.motion': 'on',
31
+ 'chat.budget': '5',
30
32
  'guides.auto': 'on',
31
33
  'jobs.advisor': 'off',
34
+ 'jobs.budget': '10',
35
+ 'jobs.floor': 'haiku',
36
+ 'jobs.escalate': 'on',
37
+ 'jobs.review': 'risky',
38
+ 'shell.sandbox': 'on',
39
+ 'jobs.minutes': '60',
40
+ 'shell.network': 'on',
32
41
  'projects.home': '~',
33
42
  'scratch.days': '14',
34
43
  'model.source': 'https://huggingface.co',
@@ -44,7 +53,8 @@ export const CONFIG_DEFAULTS = {
44
53
  function launcherScript() {
45
54
  // Single quotes keep a `$`, backtick or `"` in either path literal; a `'` is closed, escaped and reopened.
46
55
  const quote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
47
- return `#!/bin/sh\nexport NODE_USE_SYSTEM_CA=1\nexec ${quote(process.execPath)} --disable-warning=ExperimentalWarning ${quote(ENTRY)} "$@"\n`;
56
+ const self = COMPILED ? quote(ENTRY) : `${quote(process.execPath)} --disable-warning=ExperimentalWarning ${quote(ENTRY)}`;
57
+ return `#!/bin/sh\nexport NODE_USE_SYSTEM_CA=1\nexec ${self} "$@"\n`;
48
58
  }
49
59
 
50
60
  /** A directory already on PATH, inside the home folder, that a command can be linked into without sudo. */
@@ -183,6 +193,10 @@ function cappedAt(limit) {
183
193
  /** A model source is a web address; anything else is refused before it is stored, since every later setup would fail on it. */
184
194
  function sourceOrRefuse(value) {
185
195
  if (!/^https?:\/\/[^\s/]/i.test(value)) throw new UsageError(`"${redactUrl(value)}" is not a web address — a model source looks like https://<host>[/<path>]`);
196
+ // The router model is run on every job: fetched in clear text it is anyone's on the way to replace. Loopback, a mirror on this machine, is the exception.
197
+ if (/^http:/i.test(value) && !/^http:\/\/(?:[^@/]*@)?(?:127\.0\.0\.1|localhost|\[::1\])(?::\d+)?(?:\/|$)/i.test(value)) {
198
+ throw new UsageError(`"${redactUrl(value)}" is plain http: the model would cross the network in clear text. Use https:// (http:// is taken only for 127.0.0.1 and localhost)`);
199
+ }
186
200
  return value;
187
201
  }
188
202
 
@@ -298,7 +312,7 @@ function commandOnPath(name) {
298
312
  }
299
313
 
300
314
  /** Each check is [ok, label, how to fix]. `warn` checks never fail the run. */
301
- export function doctor() {
315
+ export async function doctor() {
302
316
  const p = paths();
303
317
  const checks = [];
304
318
  const check = (ok, label, fix, warn = false) => checks.push({ ok, label, fix, warn });
@@ -310,7 +324,9 @@ export function doctor() {
310
324
 
311
325
  const [major, minor] = process.versions.node.split('.').map(Number);
312
326
  const nodeOk = (major === 22 && minor >= 19) || (major === 24 && minor >= 6) || major > 24;
313
- check(nodeOk, `node ${process.versions.node}`, `needs Node ${NODE_REQUIREMENT}`);
327
+ // The binary Bun builds carries its own runtime, whatever Node version that says it is.
328
+ if (process.versions.bun) check(true, `bun ${process.versions.bun} (a built binary: no Node needed)`, '');
329
+ else check(nodeOk, `node ${process.versions.node}`, `needs Node ${NODE_REQUIREMENT}`);
314
330
 
315
331
  let db = null;
316
332
  // Read here, inside the one guarded open: a database that will not open is a failed check, not the end of the report.
@@ -369,6 +385,9 @@ export function doctor() {
369
385
  'ANTHROPIC_API_KEY is set or CLAUDE_CODE_OAUTH_TOKEN is set (chat, jobs, and the passes\' fallback)',
370
386
  'export ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN in the shell that starts sessions and hooks',
371
387
  );
388
+ // What every request of each role pays for before its first word: the first number of the ledger, and never a failed check.
389
+ const chatModel = stored.configured.find(([key]) => key === 'chat.model')?.[1];
390
+ checks.push(await promptCheck(MODELS.includes(chatModel) ? chatModel : CONFIG_DEFAULTS['chat.model']));
372
391
 
373
392
  // Not a warning: every job is routed by it, and without an answer no job can be created.
374
393
  const standIn = process.env.SUMO_AGENTS_MODEL_CMD;
@@ -385,6 +404,37 @@ export function doctor() {
385
404
  return checks;
386
405
  }
387
406
 
407
+ /** How long doctor waits for a count: it is one request, and a doctor that hangs tells nobody anything. */
408
+ const COUNT_TIMEOUT_MS = 15_000;
409
+
410
+ /**
411
+ * The fixed prompt of each role — its tools and its system prompt, with a one-word turn after them, as the endpoint takes
412
+ * no empty conversation — counted by the API's free count_tokens, every role on the chat's model so the numbers compare.
413
+ * The MCP servers' tools are left out: connecting them is not doctor's to do, and they go deferred, outside the prefix.
414
+ * Without a credential, or on any error, it is a warning that says why nothing was counted.
415
+ */
416
+ async function promptCheck(model) {
417
+ const skipped = (why) => ({ ok: false, warn: true, label: 'prompt: not counted', fix: why });
418
+ const credential = resolveAnthropicCredential();
419
+ if (!credential) return skipped('no Anthropic credential to count with');
420
+ try {
421
+ // Loaded here: the loop and the chat import this module, and only doctor needs them from it.
422
+ const { jobParams, paramsFor } = await import('./loop.mjs');
423
+ const { CHAT_TOOLS, chatSystem } = await import('./chat.mjs');
424
+ const requests = [
425
+ ['chat', paramsFor({ model, tools: CHAT_TOOLS, system: chatSystem(), text: 'hi' })],
426
+ ...['worker', 'scout', 'reviewer'].map((agent) => [agent, jobParams({ job: { agent, model }, text: 'hi' })]),
427
+ ];
428
+ const client = anthropicClient({ timeout: COUNT_TIMEOUT_MS, maxRetries: 0 }, credential);
429
+ // Sent as each role sends it, less what only bounds a reply.
430
+ const counts = await Promise.all(requests.map(([, { max_tokens: _bound, ...params }]) => client.beta.messages.countTokens(authenticatedRequest(params, credential))));
431
+ const said = requests.map(([role], i) => `${role} ${counts[i].input_tokens.toLocaleString('en-US')}`).join(' · ');
432
+ return { ok: true, warn: false, label: `prompt: ${said} tokens on ${model}, without MCP tools`, fix: '' };
433
+ } catch (cause) {
434
+ return skipped(describeApiError(cause));
435
+ }
436
+ }
437
+
388
438
  /** A checkout says its commit; an installed package says its version. */
389
439
  function codeVersion() {
390
440
  try {
@@ -418,6 +468,10 @@ export function config(key, value) {
418
468
  if (value !== undefined) {
419
469
  // A budget that is not a number would be no budget at all: every session's block would carry every preference.
420
470
  if (key === 'prime.budget') positiveInteger(value, 'prime.budget needs a positive number');
471
+ // A cap that is not a number of dollars would be read as the default without a word; 0 is a choice, no cap at all.
472
+ if (key === 'jobs.budget' && !/^\d+(\.\d+)?$/.test(value)) throw new UsageError('jobs.budget needs a number of dollars, 0 or more: what one job may spend before it is stopped, 0 for no cap');
473
+ // The same for a limit in time: whole minutes, and 0 for none.
474
+ if (key === 'jobs.minutes' && !/^\d+$/.test(value)) throw new UsageError('jobs.minutes needs a whole number of minutes, 0 or more: how long one job may run before it is stopped, 0 for no limit');
421
475
  // Zero or a word would let go of every chore at the next session start, or of none ever.
422
476
  if (key === 'scratch.days') positiveInteger(value, 'scratch.days needs a positive number: the days a chore nothing is written in is kept');
423
477
  // Where a new project is made: a place that is not there, or that moves with the shell's directory, is no answer to "where".
@@ -428,8 +482,18 @@ export function config(key, value) {
428
482
  // Sent with every chat turn: a word the API does not take would fail each one, not this command.
429
483
  if (key === 'chat.effort' && !CHAT_EFFORTS.includes(value)) throw new UsageError(`no such effort "${value}" — one of: ${CHAT_EFFORTS.join(', ')}`);
430
484
  if (key === 'chat.notify' && value !== 'on' && value !== 'off') throw new UsageError('chat.notify is on or off: the bell and the notification when a long turn ends behind another window');
485
+ if (key === 'chat.motion' && value !== 'on' && value !== 'off') throw new UsageError('chat.motion is on or off: the colours flowing through the working line — off, it stands still');
486
+ // A word would be no budget at all, and the line it holds back would never be said.
487
+ if (key === 'chat.budget' && !/^\d+(\.\d+)?$/.test(value)) throw new UsageError('chat.budget is a sum in US dollars, like 5 or 2.50 (0 for never): what a chat session costs before the screen says so, once');
431
488
  if (key === 'guides.auto' && value !== 'on' && value !== 'off') throw new UsageError('guides.auto is on or off: a guide riding in with the message that asks for its kind of work, and a turn held for an edit nothing checked');
432
489
  if (key === 'jobs.advisor' && value !== 'on' && value !== 'off') throw new UsageError('jobs.advisor is on or off: a job asking the next model up for advice, from inside its own request');
490
+ if (key === 'jobs.escalate' && value !== 'on' && value !== 'off') throw new UsageError('jobs.escalate is on or off: a worker\'s first attempt that fails retried once, one step up, without being asked');
491
+ if (key === 'jobs.floor' && !['haiku', 'sonnet', 'opus', 'fable'].includes(value)) throw new UsageError('jobs.floor is haiku, sonnet, opus or fable: the smallest model a job may run on — haiku holds nothing back');
492
+ if (key === 'jobs.review' && !['risky', 'all', 'off'].includes(value)) {
493
+ throw new UsageError('jobs.review is risky, all or off: which workers that finish DONE, verified, are reviewed without being asked — the ones the router called risky, every one, or none');
494
+ }
495
+ if (key === 'shell.sandbox' && value !== 'on' && value !== 'off') throw new UsageError('shell.sandbox is on or off: the model\'s shell writing only in the project, the temporary folders and the tool caches, on macOS');
496
+ if (key === 'shell.network' && value !== 'on' && value !== 'off') throw new UsageError('shell.network is on or off: the sandboxed shell reaching past this machine — off, it reaches only loopback and local sockets');
433
497
  // Stored for every later setup: one that is not a URL would fail each of them.
434
498
  if (key === 'model.source') sourceOrRefuse(value);
435
499
  // The file name decides where the bytes land and what the server is asked for: a name, never a path.
@@ -446,6 +510,11 @@ export function config(key, value) {
446
510
  assertOn(db, value);
447
511
  }
448
512
  setMeta(db, `config.${key}`, value);
513
+ // Read once a process, so a shell that edits the database cannot switch them off under itself: a chat already open keeps
514
+ // what it read, for the jobs it starts too.
515
+ if (key === 'shell.sandbox' || key === 'shell.network') {
516
+ return [shown(key), 'takes effect in chats and `sumo job run`s started from now on — a chat already open keeps its setting, for the jobs it starts too, until it is started again'];
517
+ }
449
518
  }
450
519
  return [shown(key)];
451
520
  } finally {
package/src/sqlite.mjs ADDED
@@ -0,0 +1,66 @@
1
+ const PREFIXES = [':', '$', '@'];
2
+
3
+ /**
4
+ * The class every sumo database is opened with. Under Node it is node:sqlite's DatabaseSync itself. The binary
5
+ * `npm run build:bin` makes runs on Bun, which has no node:sqlite: there it is bun:sqlite's Database made to answer
6
+ * the calls sumo makes the way DatabaseSync does. Node never loads bun:sqlite, nor Bun node:sqlite.
7
+ */
8
+ export const DatabaseSync = process.versions.bun
9
+ ? bunDatabaseSync((await import('bun:sqlite')).Database)
10
+ : (await import('node:sqlite')).DatabaseSync;
11
+
12
+ /**
13
+ * The values for one statement, as bun:sqlite takes them. node:sqlite binds a named value given bare ({ id }) to
14
+ * :id, $id or @id, and bun:sqlite only one spelled with its prefix; node:sqlite leaves a placeholder it was given no
15
+ * value for NULL, and bun:sqlite refuses the statement.
16
+ */
17
+ export function bunArgs(args, count) {
18
+ const [first] = args;
19
+ if (args.length === 1 && first !== null && typeof first === 'object' && !ArrayBuffer.isView(first)) {
20
+ const named = {};
21
+ for (const [key, value] of Object.entries(first)) {
22
+ for (const name of PREFIXES.includes(key[0]) ? [key] : PREFIXES.map((p) => `${p}${key}`)) named[name] = value;
23
+ }
24
+ return [named];
25
+ }
26
+ return args.length < count ? [...args, ...Array(count - args.length).fill(null)] : args;
27
+ }
28
+
29
+ /** DatabaseSync's surface over bun:sqlite's Database, for what sumo uses: prepare().get/all/run/iterate, exec, close. */
30
+ export function bunDatabaseSync(Database) {
31
+ const statement = (db, stmt) => {
32
+ const bind = (args) => bunArgs(args, stmt.paramsCount);
33
+ return {
34
+ // No row is undefined from node:sqlite, null from bun:sqlite.
35
+ get: (...args) => stmt.get(...bind(args)) ?? undefined,
36
+ all: (...args) => stmt.all(...bind(args)),
37
+ run: (...args) => {
38
+ const { lastInsertRowid } = stmt.run(...bind(args));
39
+ // bun:sqlite counts the rows its triggers wrote too (an index kept in step); node:sqlite, as SQLite's changes(), the statement's own.
40
+ return { changes: db.query('SELECT changes() AS n').get().n, lastInsertRowid };
41
+ },
42
+ iterate: (...args) => stmt.iterate(...bind(args)),
43
+ };
44
+ };
45
+ return class DatabaseSync {
46
+ #db;
47
+
48
+ constructor(path, { readOnly = false } = {}) {
49
+ this.#db = new Database(path, readOnly ? { readonly: true } : { readwrite: true, create: true });
50
+ // node:sqlite turns foreign keys on for every connection it opens; SQLite's own default is off.
51
+ this.#db.exec('PRAGMA foreign_keys = ON');
52
+ }
53
+
54
+ prepare(sql) {
55
+ return statement(this.#db, this.#db.prepare(sql));
56
+ }
57
+
58
+ exec(sql) {
59
+ this.#db.exec(sql);
60
+ }
61
+
62
+ close() {
63
+ this.#db.close();
64
+ }
65
+ };
66
+ }
package/src/text.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const STOPWORDS = new Set(
1
+ export const STOPWORDS = new Set(
2
2
  `a an and are as at be but by do does for from has have how i if in into is it its me my no not of
3
3
  on or our so that the their them then there these this to us was we what when where which who why
4
4
  will with you your`.split(/\s+/),
@@ -78,15 +78,20 @@ export function clip(text, max) {
78
78
  }
79
79
 
80
80
  /** Words that mark a sentence as a standing instruction, which is worth filing right away rather than in the next batch. */
81
- const DURABLE = /\b(always|never|from now on|going forward|remember|don'?t ever|i (prefer|like|want|hate|use)|we (use|moved|switched|decided)|make sure|by default|every time|stop (doing|using))\b/i;
81
+ const DURABLE = /\b(always|never|from now on|going forward|remember|don'?t ever|i (prefer|like|want(?! to\b)|hate|use)|we (use|moved|switched|decided)|make sure|by default|every time|stop (doing|using))\b/i;
82
82
 
83
83
  export const looksDurable = (text) => DURABLE.test(text);
84
84
 
85
85
  /**
86
86
  * A clause that asks for work or an answer — describe this, fix that, delegate, a question — rather than stating
87
87
  * something. Only verbs that are almost never a standing rule: "write tests first" or "keep answers short" are rules.
88
+ * The words people open an order with (ok, lets, so) do not hide it, and "I want to …" is the next piece of work.
88
89
  */
89
- const REQUEST = /(?:^|[.,:;!?)]\s+|\n\s*)(?:(?:please|now|then|also|and|so)\s+)?(?:describe|explain|delegate|fix|find|investigate|look\s+(?:at|into|for)|show\s+me|tell\s+me|give\s+me|summari[sz]e|debug|continue|(?:can|could|would|will)\s+you)\b|\?\s*$/im;
90
+ const OPENERS = /(?:(?:please|pls|now|then|also|and|so+|ok|okay|yes|yeah|yep|na+h|just|lets|let's|let\s+us)[\s,]+)*/.source;
91
+ const VERBS = /describe|explain|delegate|fix|find|investigate|look\s+(?:at|into|for)|show\s+me|tell\s+me|give\s+me|summari[sz]e|debug|continue|research|install|spawn|implement|proceed|resume|redeploy|restart|rerun|retry|go\s+ahead|carry\s+on|try\s+again|(?:can|could|would|will)\s+you|i\s+(?:want|need|would\s+like|'d\s+like)\s+to/.source;
92
+ /** What marks an order as one for now, whatever its verb: "not … yet" holds something for the moment, "let me know" asks for an answer. */
93
+ const FOR_NOW = /\b(?:not|n't|dont|don't)\b[^.!?\n]*\byet\b|\b(?:lmk|let\s+me\s+know)\b/.source;
94
+ const REQUEST = new RegExp(`(?:^|[.,:;!?)]\\s+|\\n\\s*)${OPENERS}(?:${VERBS})\\b|\\?\\s*$|${FOR_NOW}`, 'im');
90
95
 
91
96
  /** Words that ask for work or an answer and state nothing that lasts: nothing in them is a memory. */
92
97
  export const asksForWork = (text) => REQUEST.test(String(text ?? '')) && !looksDurable(String(text ?? ''));