sumo-agents 0.4.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.
- package/AGENTS.md +8 -6
- package/CHANGELOG.md +155 -0
- package/README.md +76 -18
- package/bin/sumo.mjs +13 -2
- package/guides/feature.md +2 -1
- package/guides/memory.md +6 -4
- package/guides/projects.md +1 -1
- package/guides/review.md +1 -1
- package/package.json +5 -4
- package/prompts/dream.md +6 -0
- package/spinner.txt +25 -1
- package/src/apply.mjs +146 -18
- package/src/auth.mjs +32 -5
- package/src/card.mjs +39 -2
- package/src/catalog.mjs +6 -3
- package/src/chat.mjs +197 -86
- package/src/cli.mjs +359 -135
- package/src/cost.mjs +67 -8
- package/src/db.mjs +106 -3
- package/src/dream.mjs +445 -19
- package/src/editor.mjs +3 -2
- package/src/export.mjs +105 -7
- package/src/guard.mjs +57 -11
- package/src/guides.mjs +5 -4
- package/src/herdr.mjs +4 -2
- package/src/hooks.mjs +31 -18
- package/src/jobs.mjs +118 -39
- package/src/jobtools.mjs +6 -4
- package/src/keeper.mjs +77 -0
- package/src/local-server.mjs +9 -16
- package/src/lock.mjs +2 -1
- package/src/logs.mjs +165 -0
- package/src/loop.mjs +121 -44
- package/src/mcp.mjs +72 -16
- package/src/memory.mjs +142 -15
- package/src/model.mjs +41 -21
- package/src/paths.mjs +38 -0
- package/src/prime.mjs +19 -31
- package/src/projects.mjs +76 -12
- package/src/redact.mjs +10 -0
- package/src/render.mjs +2 -1
- package/src/route.mjs +16 -22
- package/src/sandbox.mjs +82 -3
- package/src/scan.mjs +35 -1
- package/src/scribe.mjs +10 -22
- package/src/sessions.mjs +46 -11
- package/src/setup.mjs +423 -78
- package/src/text.mjs +100 -1
- package/src/tools.mjs +258 -21
- package/src/tty.mjs +205 -78
- package/src/ui.mjs +869 -293
- package/src/verify.mjs +17 -1
package/src/jobtools.mjs
CHANGED
|
@@ -21,7 +21,7 @@ const BASELINE = tool('baseline', "Before your first edit: run the project's che
|
|
|
21
21
|
const VERIFY = tool('verify', "Run the project's checks now and see the verdict DONE will be given. Takes minutes.");
|
|
22
22
|
const FINISH = tool(
|
|
23
23
|
'finish',
|
|
24
|
-
"Close the job — the last thing you do, or nobody knows it ended. A worker's first DONE is answered with the task beside its change, to
|
|
24
|
+
"Close the job — the last thing you do, or nobody knows it ended. A worker's first DONE is answered with the task as a numbered list beside its change, each number to answer once; the next runs the project's checks and is refused while something new fails.",
|
|
25
25
|
// No way to take the work unverified: that is for the user or the main agent to grant, never the author.
|
|
26
26
|
{ status: { type: 'string', enum: ['DONE', 'FAILED'] }, report: text('the report, in the shape the brief gives') },
|
|
27
27
|
);
|
|
@@ -70,9 +70,11 @@ export async function runJobTool(call, { job, ctx, signal = null }) {
|
|
|
70
70
|
try {
|
|
71
71
|
const args = file ? [...command.args, '--from-file', file] : command.args;
|
|
72
72
|
// The checks give each command its own limit; anything else is quick. They run the project's code — tests the model may have
|
|
73
|
-
// written among it — so
|
|
74
|
-
//
|
|
75
|
-
|
|
73
|
+
// written among it — so each check runs in the sandbox the job's shell is in, handed down to sumo, which itself runs outside
|
|
74
|
+
// it: sumo's memory is read-only to the sandbox, and the verdict is recorded there. A check that fails only for the sandbox
|
|
75
|
+
// fails so at the baseline and at the end alike.
|
|
76
|
+
const env = command.checks ? { ...ctx.env, SUMO_CHECKS_WRAP: JSON.stringify(checksWrap(ctx)) } : ctx.env;
|
|
77
|
+
const run = await runCommand(sumo(args), { cwd: ctx.cwd, env, signal, timeoutMs: command.checks ? null : 60_000 });
|
|
76
78
|
if (run.error) return { content: run.error, isError: true };
|
|
77
79
|
const output = capRedacted(run.output.trim(), undefined, run.omitted);
|
|
78
80
|
return { content: output || '(done)', isError: run.stopped !== null || run.status !== 0 };
|
package/src/keeper.mjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { getMeta, setMeta } from './db.mjs';
|
|
2
|
+
import { runDream, SESSIONS_TO_TIDY, tidyDue } from './dream.mjs';
|
|
3
|
+
import { localServerStatus } from './local-server.mjs';
|
|
4
|
+
import { memoryHealth, memoryHealthLine } from './memory.mjs';
|
|
5
|
+
import { modelStats, outcomeLines, runScribe } from './scribe.mjs';
|
|
6
|
+
import { pendingTurns, sessionsAwaitingDream } from './sessions.mjs';
|
|
7
|
+
import { CONFIG_DEFAULTS, modelFileOf } from './setup.mjs';
|
|
8
|
+
|
|
9
|
+
/** When the last tidy failed; until TIDY_RETRY_MS has gone by, a pass that finds a tidy due files only. */
|
|
10
|
+
const TIDY_FAILED = 'memory.tidy_failed_at';
|
|
11
|
+
const TIDY_RETRY_MS = 6 * 3_600_000;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The memory pass: the one thing that looks after memory, so the user has one name, one status and one setting
|
|
15
|
+
* (`memory.model`) instead of a writer and a tidier to keep track of. Each pass files what was said since the last one;
|
|
16
|
+
* when tidying is due — conversations have finished, or a week has gone by — the same pass then tidies: reads them
|
|
17
|
+
* side by side, raises pairs that say one thing or disagree, fills in search terms. It runs by itself after turns and
|
|
18
|
+
* at session start and end; `sumo memory run` runs it now, `--tidy` tidies now whatever is due. A tidy that failed is
|
|
19
|
+
* not tried again by itself for six hours: a local model that is down must not turn every turn's filing into a failed
|
|
20
|
+
* tidy. Returns what each half did: `{ filed, tidied }`, `tidied` null when it did not tidy.
|
|
21
|
+
*/
|
|
22
|
+
export async function runMemoryPass(db, { now = new Date().toISOString(), tidy = false } = {}) {
|
|
23
|
+
const filed = await runScribe(db, { now });
|
|
24
|
+
const failedAt = getMeta(db, TIDY_FAILED);
|
|
25
|
+
const resting = failedAt && Date.parse(now) - Date.parse(failedAt) < TIDY_RETRY_MS;
|
|
26
|
+
if (!tidy && (resting || !tidyDue(db, now))) return { filed, tidied: null };
|
|
27
|
+
const tidied = await runDream(db, { now, force: tidy, file: false });
|
|
28
|
+
if (tidied.ok === false && !tidied.skipped) setMeta(db, TIDY_FAILED, now);
|
|
29
|
+
else if (failedAt) setMeta(db, TIDY_FAILED, '');
|
|
30
|
+
return { filed, tidied };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A pass as a person reads it: what the filing did, then what the tidying did. A half with nothing to do is left out
|
|
35
|
+
* when the other did something. Fails — exit 1 for a script — when the filing failed, or a tidy asked for failed.
|
|
36
|
+
*/
|
|
37
|
+
export function passLines({ filed, tidied }, { asked = false } = {}) {
|
|
38
|
+
const tidying = tidied ? outcomeLines(tidied).map((l, i) => (i === 0 ? `tidy: ${l}` : l)) : [];
|
|
39
|
+
const lines = [...(tidied && filed.skipped ? [] : outcomeLines(filed)), ...tidying];
|
|
40
|
+
const failed = (filed.ok === false && !filed.skipped) || (asked && tidied?.ok === false && !tidied.skipped);
|
|
41
|
+
return failed ? Object.assign(lines, { exitCode: 1 }) : lines;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `sumo memory status`: the pass as a whole — the model it runs on, what waits, how it last went — then the numbers. */
|
|
45
|
+
/** The ledger's names for the pass's two halves, as the status says them. */
|
|
46
|
+
const HALVES = { scribe: 'filing', dream: 'tidying' };
|
|
47
|
+
/** A status line: its label in a column of its own, so the values stand in one. */
|
|
48
|
+
const row = (label, value) => `${label.padEnd(LABEL_WIDTH)}${value}`;
|
|
49
|
+
const LABEL_WIDTH = 11;
|
|
50
|
+
|
|
51
|
+
export async function memoryStatus(db, now = new Date().toISOString()) {
|
|
52
|
+
const health = memoryHealthLine(memoryHealth(db, now), now);
|
|
53
|
+
const model = getMeta(db, 'config.memory.model') ?? CONFIG_DEFAULTS['memory.model'];
|
|
54
|
+
const server = model === 'local' ? await localServerStatus(db, { file: modelFileOf(db) }) : null;
|
|
55
|
+
const turns = pendingTurns(db, 1000).length;
|
|
56
|
+
const finished = sessionsAwaitingDream(db, now, 1000).length;
|
|
57
|
+
const tidied = db.prepare(`SELECT MAX(ts) AS at FROM model_runs WHERE kind = 'dream' AND ok = 1`).get()?.at ?? null;
|
|
58
|
+
const sweep = getMeta(db, 'dream.last_tidy');
|
|
59
|
+
// Due with fewer than three conversations waiting: the weekly sweep is what is owed.
|
|
60
|
+
const owed = finished < SESSIONS_TO_TIDY && tidyDue(db, now);
|
|
61
|
+
const failures = Number(getMeta(db, 'scribe.failures') ?? 0);
|
|
62
|
+
const failedAt = getMeta(db, TIDY_FAILED);
|
|
63
|
+
const day = (at) => (at ? at.slice(0, 10) : 'never');
|
|
64
|
+
// The ledger holds every request; the pass's own are its filing and its tidying, named so.
|
|
65
|
+
const spent = modelStats(db)
|
|
66
|
+
.filter((l) => /^(scribe|dream): /.test(l))
|
|
67
|
+
.map((l) => l.replace(/^(scribe|dream)/, (kind) => HALVES[kind]));
|
|
68
|
+
return [
|
|
69
|
+
`${health.ok ? 'ok ' : 'warn'} ${health.label}${health.fix ? ` — ${health.fix}` : ''}`,
|
|
70
|
+
row('model', `${model}${server ? ` · ${server}` : ''}`),
|
|
71
|
+
row('waiting', `${turns} turn${turns === 1 ? '' : 's'} to file · ${finished} finished conversation${finished === 1 ? '' : 's'} to read (tidied by itself at ${SESSIONS_TO_TIDY}, and weekly)`),
|
|
72
|
+
row('last tidy', `${day(tidied)} · weekly sweep: ${day(sweep)}${owed ? ' — owed: the next pass sweeps' : ''}`),
|
|
73
|
+
...(failures > 0 ? [row('failing', `filing failed ${failures} time${failures === 1 ? '' : 's'} in a row — ${getMeta(db, 'scribe.last_error')}`)] : []),
|
|
74
|
+
...(failedAt ? [row('failing', `the last tidy, at ${failedAt.slice(0, 16)}; tried again by itself ${Date.parse(now) - Date.parse(failedAt) < TIDY_RETRY_MS ? 'six hours on' : 'with the next pass'}, or now: sumo memory run --tidy`)] : []),
|
|
75
|
+
...(spent.length > 0 ? [row('spent', spent[0]), ...spent.slice(1).map((l) => row('', l))] : []),
|
|
76
|
+
];
|
|
77
|
+
}
|
package/src/local-server.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
import { closeSync
|
|
2
|
+
import { closeSync } from 'node:fs';
|
|
3
3
|
import { createServer } from 'node:net';
|
|
4
|
-
import { basename
|
|
4
|
+
import { basename } from 'node:path';
|
|
5
5
|
import { getMeta, setMeta, tx } from './db.mjs';
|
|
6
|
+
import { alive } from './lock.mjs';
|
|
7
|
+
import { openLog, trimLog } from './logs.mjs';
|
|
6
8
|
import { paths } from './paths.mjs';
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -16,22 +18,13 @@ const HEALTH_TIMEOUT_MS = 60_000;
|
|
|
16
18
|
const HEALTH_POLL_MS = 250;
|
|
17
19
|
const SLEEP_IDLE_SECONDS = 600;
|
|
18
20
|
/** The context the server is given: the largest scribe bundle seen was 24k tokens, dream's 90th percentile 12k. */
|
|
19
|
-
|
|
21
|
+
const LOCAL_CONTEXT_TOKENS = 32_768;
|
|
20
22
|
/** How long a pass may think. Measured on the scribe probe, the 4B model answers right within this; uncapped, a small model can think until the token limit and answer nothing. */
|
|
21
23
|
export const LOCAL_REASONING_BUDGET = 1024;
|
|
22
24
|
|
|
23
25
|
/** Router mode names a model after its file. */
|
|
24
26
|
export const modelIdOf = (file) => basename(file, '.gguf');
|
|
25
27
|
|
|
26
|
-
function alive(pid) {
|
|
27
|
-
try {
|
|
28
|
-
process.kill(pid, 0);
|
|
29
|
-
return true;
|
|
30
|
-
} catch (cause) {
|
|
31
|
-
return cause.code === 'EPERM';
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
28
|
/** Only a process still carrying the port it was started on is ours: a pid can be reused once the server is gone. */
|
|
36
29
|
function ours(server) {
|
|
37
30
|
if (!alive(server.pid)) return false;
|
|
@@ -85,9 +78,7 @@ async function waitForHealth(port, dead) {
|
|
|
85
78
|
|
|
86
79
|
/** Starts the server for `file`, detached, logging to logs/llama-server.log, and records it. */
|
|
87
80
|
function start(db, { llama, file, port }) {
|
|
88
|
-
const
|
|
89
|
-
mkdirSync(logs, { recursive: true, mode: 0o700 });
|
|
90
|
-
const out = openSync(join(logs, 'llama-server.log'), 'a');
|
|
81
|
+
const out = openLog('llama-server.log');
|
|
91
82
|
let dead = null;
|
|
92
83
|
let child;
|
|
93
84
|
try {
|
|
@@ -117,6 +108,8 @@ function start(db, { llama, file, port }) {
|
|
|
117
108
|
export async function ensureLocalServer(db, { llama, file }) {
|
|
118
109
|
const current = recorded(db);
|
|
119
110
|
if (current && current.file === file && current.llama === llama && ours(current)) {
|
|
111
|
+
// The server outlives the sumo that started it, and its log with it: trimmed in place each time it is used again.
|
|
112
|
+
trimLog('llama-server.log');
|
|
120
113
|
if (await healthy(current.port)) return current;
|
|
121
114
|
// Started a moment ago by another sumo and still coming up: wait for it rather than replace it.
|
|
122
115
|
if (Date.now() - current.startedAt < HEALTH_TIMEOUT_MS) {
|
|
@@ -154,7 +147,7 @@ export function stopLocalServer(db, expected) {
|
|
|
154
147
|
db.prepare('DELETE FROM meta WHERE key = ?').run(META_KEY);
|
|
155
148
|
}
|
|
156
149
|
|
|
157
|
-
/** One line for `sumo
|
|
150
|
+
/** One line for `sumo memory status`: where the server is and what it says about the model. */
|
|
158
151
|
export async function localServerStatus(db, { file }) {
|
|
159
152
|
const current = recorded(db);
|
|
160
153
|
if (!current) return 'local server: not running (starts on the first call)';
|
package/src/lock.mjs
CHANGED
|
@@ -35,7 +35,8 @@ function holderOf(file) {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
/** Whether a process is there: signal 0 asks without sending; one that is someone else's (EPERM) is there too. */
|
|
39
|
+
export function alive(pid) {
|
|
39
40
|
try {
|
|
40
41
|
process.kill(pid, 0);
|
|
41
42
|
return true;
|
package/src/logs.mjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { appendFileSync, chmodSync, copyFileSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync, truncateSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { paths } from './paths.mjs';
|
|
4
|
+
import { redact } from './redact.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sumo's own logs, in ~/.sumo-agents/logs: private like everything else in the home, and never growing without end.
|
|
8
|
+
* A log past LOG_ROTATE_BYTES is moved aside to `<name>.1` (one older generation kept) before the next line is
|
|
9
|
+
* written, so the newest lines are always in the file of that name and a log costs at most twice the limit.
|
|
10
|
+
*/
|
|
11
|
+
export const LOG_ROTATE_BYTES = 1024 * 1024;
|
|
12
|
+
/** How many crash files are kept: the newest. */
|
|
13
|
+
const CRASHES_KEPT = 10;
|
|
14
|
+
|
|
15
|
+
/** A log past the limit, moved aside. Nothing there yet, or a file that cannot be moved, is left as it is: a log never stops the work. */
|
|
16
|
+
export function rotate(file, limit = LOG_ROTATE_BYTES) {
|
|
17
|
+
try {
|
|
18
|
+
if (statSync(file).size > limit) renameSync(file, `${file}.1`);
|
|
19
|
+
} catch {
|
|
20
|
+
// Not there yet, or not ours to move.
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The logs this process has made sure are private: looked at once each, on the first line written. */
|
|
25
|
+
const madePrivate = new Set();
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One or more lines added to a log of the home: made private when it is made, moved aside when it has grown too big.
|
|
29
|
+
* A mode is given only to a file being made, so a log from before logs were private is made private here, once.
|
|
30
|
+
*/
|
|
31
|
+
export function appendLog(name, text) {
|
|
32
|
+
const dir = paths().logs;
|
|
33
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
34
|
+
const file = join(dir, name);
|
|
35
|
+
rotate(file);
|
|
36
|
+
if (!madePrivate.has(file)) {
|
|
37
|
+
madePrivate.add(file);
|
|
38
|
+
try {
|
|
39
|
+
if (statSync(file).mode & 0o077) chmodSync(file, 0o600);
|
|
40
|
+
} catch {
|
|
41
|
+
// Not there yet: made private below, when it is made.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
appendFileSync(file, text, { mode: 0o600 });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A log a long-lived process still holds open, kept in bounds where it lies: past the limit its lines are copied aside
|
|
49
|
+
* to `<name>.1` and the file emptied in place, so the writer — appending — carries on at its new start. A rename would
|
|
50
|
+
* leave the writer filling the moved file until it restarts.
|
|
51
|
+
*/
|
|
52
|
+
export function trimLog(name, limit = LOG_ROTATE_BYTES) {
|
|
53
|
+
const file = join(paths().logs, name);
|
|
54
|
+
try {
|
|
55
|
+
if (statSync(file).size <= limit) return;
|
|
56
|
+
copyFileSync(file, `${file}.1`);
|
|
57
|
+
truncateSync(file, 0);
|
|
58
|
+
} catch {
|
|
59
|
+
// Not there, or not ours to trim: a log never stops the work.
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A log opened for a child process to write to (llama-server's), moved aside first if it has grown too big. */
|
|
64
|
+
export function openLog(name) {
|
|
65
|
+
const dir = paths().logs;
|
|
66
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
67
|
+
const file = join(dir, name);
|
|
68
|
+
rotate(file);
|
|
69
|
+
return openSync(file, 'a', 0o600);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const CRASH = /^crash-.+\.log$/;
|
|
73
|
+
|
|
74
|
+
/** The crash files there are, newest first, by their names, which are the times they were written. */
|
|
75
|
+
export function crashFiles() {
|
|
76
|
+
try {
|
|
77
|
+
return readdirSync(paths().logs).filter((name) => CRASH.test(name)).sort().reverse().map((name) => join(paths().logs, name));
|
|
78
|
+
} catch {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The newest crash file written since `since` (a Date), with when and the line that says what failed; null when there is none. */
|
|
84
|
+
export function latestCrash(since) {
|
|
85
|
+
const [file] = crashFiles();
|
|
86
|
+
if (!file) return null;
|
|
87
|
+
const at = statSync(file).mtime;
|
|
88
|
+
if (at < since) return null;
|
|
89
|
+
const said = readFileSync(file, 'utf8').split('\n').find((line) => line.startsWith('error: '))?.slice('error: '.length) ?? 'see the file';
|
|
90
|
+
return { file, at, said };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* What a crash leaves behind: the version, the runtime, the command and the stack, every part redacted — an argument or
|
|
95
|
+
* a message may hold a token — in a file only its owner can read. Returns the file, or null when even that failed: a
|
|
96
|
+
* crash report must never be a second crash.
|
|
97
|
+
*/
|
|
98
|
+
/** The line a crash file names its command with: the command, and only how many words came after it. */
|
|
99
|
+
function commandOf(args) {
|
|
100
|
+
const [command = '(none)', ...rest] = args;
|
|
101
|
+
const name = /^[a-z][a-z-]*$/.test(command) ? command : '(not a command)';
|
|
102
|
+
return `command: ${name}${rest.length > 0 ? ` (${rest.length} more argument${rest.length === 1 ? '' : 's'}, not kept)` : ''}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function writeCrash(error, origin, { version = '', argv = process.argv, now = new Date() } = {}) {
|
|
106
|
+
try {
|
|
107
|
+
const dir = paths().logs;
|
|
108
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
109
|
+
const file = join(dir, `crash-${now.toISOString().replace(/[:.]/g, '-')}.log`);
|
|
110
|
+
const stack = error instanceof Error ? (error.stack ?? `${error.name}: ${error.message}`) : String(error);
|
|
111
|
+
const message = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
|
|
112
|
+
const body = [
|
|
113
|
+
`sumo ${version || 'unknown'} crashed (${origin})`,
|
|
114
|
+
`at: ${now.toISOString()}`,
|
|
115
|
+
`node: ${process.versions.bun ? `bun ${process.versions.bun}` : process.versions.node} · ${process.platform} ${process.arch}`,
|
|
116
|
+
// The command, never what was typed after it: a search, a memory or a message is the user's words, not a bug report's.
|
|
117
|
+
commandOf(argv.slice(2)),
|
|
118
|
+
`error: ${message.split('\n')[0]}`,
|
|
119
|
+
'',
|
|
120
|
+
stack,
|
|
121
|
+
'',
|
|
122
|
+
].join('\n');
|
|
123
|
+
writeFileSync(file, redact(body).text, { mode: 0o600, flag: 'wx' });
|
|
124
|
+
for (const old of crashFiles().slice(CRASHES_KEPT)) rmSync(old, { force: true });
|
|
125
|
+
return file;
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Crashes written down as they happen, the process otherwise left to end as Node ends it: the monitor runs before
|
|
133
|
+
* Node's own handling and changes none of it — the stack still goes to stderr, the exit code is still 1, and the chat
|
|
134
|
+
* screen's own exit hooks still hand the terminal back. A rejection nobody handled reaches it as an uncaught exception.
|
|
135
|
+
*/
|
|
136
|
+
export function watchForCrashes(version) {
|
|
137
|
+
process.on('uncaughtExceptionMonitor', (error, origin) => {
|
|
138
|
+
const file = writeCrash(error, origin, { version });
|
|
139
|
+
if (file) process.stderr.write(`sumo: crashed — written to ${file} (sumo doctor --report bundles it)\n`);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The last `count` lines of a file, redacted; empty when it cannot be read. */
|
|
144
|
+
export function tailOf(file, count = 50) {
|
|
145
|
+
try {
|
|
146
|
+
const lines = readFileSync(file, 'utf8').split('\n');
|
|
147
|
+
if (lines.at(-1) === '') lines.pop();
|
|
148
|
+
return redact(lines.slice(-count).join('\n')).text;
|
|
149
|
+
} catch {
|
|
150
|
+
return '';
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The logs a report shows: the files directly in the logs folder that are not crash files, oldest generation last. */
|
|
155
|
+
export function logFiles() {
|
|
156
|
+
try {
|
|
157
|
+
const dir = paths().logs;
|
|
158
|
+
return readdirSync(dir)
|
|
159
|
+
.filter((name) => !CRASH.test(name) && existsSync(join(dir, name)) && statSync(join(dir, name)).isFile())
|
|
160
|
+
.sort()
|
|
161
|
+
.map((name) => join(dir, name));
|
|
162
|
+
} catch {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
}
|