@volter/supercode-health 0.1.6 → 0.1.7
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/lib/mail.mjs +65 -11
- package/lib/probe.mjs +1 -1
- package/package.json +1 -1
package/lib/mail.mjs
CHANGED
|
@@ -1,20 +1,74 @@
|
|
|
1
|
-
// Alarm mail, filed in each subscriber's mailbox
|
|
2
|
-
// `sc:<machine>:operator:machine-health`, and delivered by the mailbox
|
|
3
|
-
// D138 step 6). Filing is a local write:
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
|
|
1
|
+
// Alarm mail, filed in each subscriber's mailbox by this machine's daemon (`harness.v1.mail.file`, its one filing,
|
|
2
|
+
// sdk/teams/mail/send.mjs), from `sc:<machine>:operator:machine-health`, and delivered by the mailbox
|
|
3
|
+
// (docs/adr/0008-agent-mailbox.md, decision 17; D138 step 6). Filing is a local write: one call on the daemon's local
|
|
4
|
+
// socket, no network, and nothing for the probe to retry. The mailbox resolves an agent address
|
|
5
|
+
// (`sc:<machine>:agent:<name>`) to the agent's main session when it delivers, retries within its bounds, and records what
|
|
6
|
+
// it could not deliver (`supercode message waiting`, `supercode message expired`). This package has no dependencies, so
|
|
7
|
+
// the call is made here with the daemon's own framing (sdk/volter-teams/channel.mjs).
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
import { connect } from 'node:net';
|
|
10
|
+
import { homedir } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
|
|
13
|
+
/** Where this OS user's daemon serves its local door (sdk/volter-teams/home.mjs `machineSocketPath`). */
|
|
14
|
+
export function daemonSocket(env = process.env) {
|
|
15
|
+
const home = env.SUPERCODE_HOME || (env.XDG_CONFIG_HOME ? join(env.XDG_CONFIG_HOME, 'supercode') : join(homedir(), '.config', 'supercode'));
|
|
16
|
+
const inside = join(env.SUPERCODE_TEAMS_HOME ?? join(home, 'teams'), 'machine.sock');
|
|
17
|
+
const hash = createHash('sha256').update(inside).digest('hex').slice(0, 24);
|
|
18
|
+
if (process.platform === 'win32') return `\\\\.\\pipe\\supercode-${hash}`;
|
|
19
|
+
if (Buffer.byteLength(inside) <= 100) return inside;
|
|
20
|
+
return join('/tmp', `supercode-machine-${typeof process.getuid === 'function' ? process.getuid() : 'user'}`, `${hash}.sock`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** One `harness.v1` call to this machine's daemon: a hello each way, the `node` stream, one request, its answer. */
|
|
24
|
+
export function daemonCall(method, params, { env = process.env, timeoutMs = 15_000 } = {}) {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const socket = connect(daemonSocket(env));
|
|
27
|
+
let buffer = Buffer.alloc(0), phase = 'hello', text = '';
|
|
28
|
+
const done = (error, value) => { clearTimeout(timer); socket.destroy(); error ? reject(error) : resolve(value); };
|
|
29
|
+
const timer = setTimeout(() => done(new Error(`this machine's daemon did not answer ${method} within ${timeoutMs / 1000} s`)), timeoutMs);
|
|
30
|
+
const send = (value) => { const bytes = Buffer.from(JSON.stringify(value)); const head = Buffer.alloc(4); head.writeUInt32BE(bytes.length, 0); socket.write(Buffer.concat([head, bytes])); };
|
|
31
|
+
socket.on('error', (error) => done(new Error(`no machine daemon is answering (${error.message}); run \`supercode teams machine start\``)));
|
|
32
|
+
socket.on('close', () => done(new Error('the daemon\'s socket closed before it answered')));
|
|
33
|
+
socket.on('connect', () => send({ p: 'supercode-machine/1', nv: 3 }));
|
|
34
|
+
socket.on('data', (chunk) => {
|
|
35
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
36
|
+
while (buffer.length >= 4 && buffer.length >= 4 + buffer.readUInt32BE(0)) {
|
|
37
|
+
const frame = JSON.parse(buffer.subarray(4, 4 + buffer.readUInt32BE(0)).toString('utf8'));
|
|
38
|
+
buffer = buffer.subarray(4 + buffer.readUInt32BE(0));
|
|
39
|
+
if (phase === 'hello') {
|
|
40
|
+
if (frame.error) return done(new Error(frame.error));
|
|
41
|
+
phase = 'open';
|
|
42
|
+
send({ ch: 1, t: 'open', name: 'node', params: {} });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (frame.t === 'ping') { send({ ch: 0, t: 'pong' }); continue; }
|
|
46
|
+
if (frame.ch !== 1) continue;
|
|
47
|
+
if (frame.t === 'close') return done(new Error(`the daemon refused: ${frame.reason ?? 'closed'}`));
|
|
48
|
+
if (frame.t === 'opened') { send({ ch: 1, t: 'data', d: `${JSON.stringify({ id: 1, method, params })}\n` }); continue; }
|
|
49
|
+
if (frame.t !== 'data') continue;
|
|
50
|
+
text += frame.d;
|
|
51
|
+
for (let end; (end = text.indexOf('\n')) >= 0;) {
|
|
52
|
+
const line = text.slice(0, end); text = text.slice(end + 1);
|
|
53
|
+
let message = null; try { message = JSON.parse(line); } catch { continue; }
|
|
54
|
+
if (message?.id !== 1) continue;
|
|
55
|
+
return message.error ? done(new Error(message.error.message ?? 'the daemon refused')) : done(null, message.result);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
7
61
|
|
|
8
62
|
/** File one alarm mail for each address. Answers what was filed and the first failure, if any. */
|
|
9
|
-
export function fileAlarm({ machine, env = process.env, to, key, subject, body }) {
|
|
63
|
+
export async function fileAlarm({ machine, env = process.env, to, key, subject, body }) {
|
|
10
64
|
const filed = [];
|
|
11
65
|
let error = null;
|
|
12
66
|
for (const address of to) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
67
|
+
let answer = null;
|
|
68
|
+
try { answer = await daemonCall('harness.v1.mail.file', { from: `sc:${machine}:operator:machine-health`, from_name: 'Machine health', to: address, id: `${key}-${address}`, subject, body, wake: true }, { env }); }
|
|
69
|
+
catch (failure) { error ??= `mail to ${address} was not filed: ${failure.message}`; continue; }
|
|
16
70
|
if (answer?.code === 0) filed.push({ to: address, id: answer.message_id });
|
|
17
|
-
else error ??= `mail to ${address} was not filed: ${answer?.text
|
|
71
|
+
else error ??= `mail to ${address} was not filed: ${answer?.text ?? 'the daemon answered nothing readable'}`;
|
|
18
72
|
}
|
|
19
73
|
return { filed, error };
|
|
20
74
|
}
|
package/lib/probe.mjs
CHANGED
|
@@ -184,7 +184,7 @@ export class Probe {
|
|
|
184
184
|
// The reading is written first; the mail is filed after it, and what the filing did is written beside it.
|
|
185
185
|
writeAtomic(this.f.latest, snapshot);
|
|
186
186
|
if (mail && to.length) {
|
|
187
|
-
const result = fileAlarm({ machine: this.machine, env: this.env, to, key: `health-${this.machine}-${now}`, subject: mail.subject, body: mail.body });
|
|
187
|
+
const result = await fileAlarm({ machine: this.machine, env: this.env, to, key: `health-${this.machine}-${now}`, subject: mail.subject, body: mail.body });
|
|
188
188
|
this.lastMail = { at: new Date(now).toISOString(), filed: result.filed.length, to: result.filed.map((f) => f.to), error: result.error };
|
|
189
189
|
if (result.error) this.log(result.error);
|
|
190
190
|
snapshot.mail = this.lastMail;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volter/supercode-health",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Optional machine pack: a cheap, deterministic probe of a machine's vitals that mails the machine's maintainer when a threshold is crossed. Each feature sits behind its own permission barrier.",
|
|
6
6
|
"license": "MIT",
|