Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(core): namespace CPU episodes by observing host [A6,H16]

Corey Olsona94e77e39626
diff --git a/src/sentinel-cpu.js b/src/sentinel-cpu.js
index 67e2a0f7..bce4a65f 100644
--- a/src/sentinel-cpu.js
+++ b/src/sentinel-cpu.js
@@ -5,6 +5,7 @@ import { realpathSync } from 'node:fs';
import { basename } from 'node:path';
import { relative } from 'node:path';
import { sep } from 'node:path';
+import { hostname } from 'node:os';
/** Read bounded native utility output without arguments or environment supplied by a process. */
function native(command, args) {
@@ -30,17 +31,22 @@ function canonical(path) {
try { return realpathSync(path); } catch { return null; /* A disappearing process/worktree has no attributable cwd. */ }
}
/** Report only native cwd ownership revalidated against the same PID birth after the cwd observation. */
-export function agentCpuFindings(db, { platform = process.platform, run = native, readLink = readlinkSync } = {}) {
+export function agentCpuFindings(db, { platform = process.platform, run = native, readLink = readlinkSync, host = hostname() } = {}) {
if (!['darwin', 'linux'].includes(platform)) return [];
+ const prefix = `AGENT_CPU:${encodeURIComponent(host)}:`;
+ const last = db.prepare("SELECT event_detail FROM event WHERE event_kind='health-scan' ORDER BY event_id DESC LIMIT 1").get();
+ const previous = last ? JSON.parse(last.event_detail).active : [];
+ const foreign = previous.filter(finding => finding.cause === 'AGENT_CPU' && !finding.key.startsWith(prefix))
+ .map(finding => ({ ...finding, owner: { who: finding.owner } }));
const agents = db.prepare('SELECT agent_id, agent_path FROM agent WHERE agent_id != ?').all('coordinator')
.map(agent => ({ ...agent, path: canonical(agent.agent_path) })).filter(agent => agent.path)
.sort((left, right) => right.path.length - left.path.length);
- if (!agents.length) return [];
+ if (!agents.length) return foreign;
const format = ['pid=', 'time=', 'lstart=', 'comm='].flatMap(field => ['-o', field]);
let candidates, confirmed, directories = new Map();
try {
candidates = rows(run('/bin/ps', ['-e', ...format]));
- if (!candidates.length) return [];
+ if (!candidates.length) return foreign;
if (platform === 'darwin') {
const output = run('/usr/sbin/lsof', ['-b', '-a', '-d', 'cwd', '-p', candidates.map(row => row.pid).join(','), '-Fpn']);
let pid;
@@ -56,17 +62,16 @@ export function agentCpuFindings(db, { platform = process.platform, run = native
}
confirmed = new Set(rows(run('/bin/ps', ['-p', candidates.map(row => row.pid).join(','), ...format])).map(row => `${row.pid}:${row.since}`));
} catch { return null; /* Unavailable observations cannot establish that a previously seen process resolved. */ }
- return candidates.flatMap(row => {
+ return [...foreign, ...candidates.flatMap(row => {
const cwd = directories.get(row.pid);
if (!cwd || !confirmed.has(`${row.pid}:${row.since}`)) return [];
const agent = agents.find(agent => { const child = relative(agent.path, cwd); return !child || child !== '..' && !child.startsWith('..' + sep) && !child.startsWith(sep); });
if (!agent) return [];
- const key = `AGENT_CPU:${row.pid}:${row.since}`;
+ const key = `${prefix}${row.pid}:${row.since}`;
// Durable prior alerts survive temporary observation gaps and observer restarts; PID birth prevents reuse collisions.
- const last = db.prepare("SELECT event_detail FROM event WHERE event_kind='health-scan' ORDER BY event_id DESC LIMIT 1").get();
- const active = last ? JSON.parse(last.event_detail).active.some(finding => finding.key === key) : false;
+ const active = previous.some(finding => finding.key === key);
if (!active && db.prepare("SELECT 1 FROM event WHERE event_kind='health-failure' AND json_extract(event_detail,'$.key')=? LIMIT 1").get(key)) return [];
return [{ key, cause: 'AGENT_CPU', item: null, subject: `pid ${row.pid}`, title: row.name, since: row.since,
why: `${row.cpu.toFixed(2)} CPU minutes used in agent worktree ${agent.path}`, next: 'inspect the owning agent process before deciding whether to stop it', owner: { who: agent.agent_id } }];
- });
+ })];
}
diff --git a/test/sentinel-cpu.test.js b/test/sentinel-cpu.test.js
index d8a85069..9ae04311 100644
--- a/test/sentinel-cpu.test.js
+++ b/test/sentinel-cpu.test.js
@@ -19,7 +19,7 @@ test('the sentinel names an agent process past 20 CPU minutes once [A6,H16]', as
store.register(board, { lane: 'coordinator', path: root });
const seat = store.register(board, { lane: 'core', path: work });
t.after(() => { store.closeBoard(board); rmSync(directory, { recursive: true }); });
- let birth = 'Mon Oct 19 12:00:00 2026', cpu = '21:30.00', cwd = work, present = true, unavailable = false;
+ let birth = 'Mon Oct 19 12:00:00 2026', cpu = '21:30.00', cwd = work, present = true, unavailable = false, host = 'host-a';
const calls = [];
const run = (command, args) => {
calls.push([command, args]);
@@ -28,7 +28,7 @@ test('the sentinel names an agent process past 20 CPU minutes once [A6,H16]', as
assert.equal(command, '/usr/sbin/lsof');
return `p4321\nn${cwd}\n`;
};
- const observe = () => agentCpuFindings(board.db, { platform: 'darwin', run });
+ const observe = () => agentCpuFindings(board.db, { platform: 'darwin', run, host });
const scan = () => {
const observation = scanHealthSentinel(file, { root, config: {}, now: new Date(), cpu: observe });
if (observation) store.recordHealthFindings(board, { agentId: 'coordinator', ...observation });
@@ -53,6 +53,14 @@ test('the sentinel names an agent process past 20 CPU minutes once [A6,H16]', as
cpu = '1-00:00:01';
assert.equal(observe()[0].why.includes('1440.02 CPU minutes'), true, 'day-form CPU time is parsed');
assert.equal(agentCpuFindings(board.db, { platform: 'linux', run, readLink: path => { assert.equal(path, '/proc/4321/cwd'); return work; } })[0].cause, 'AGENT_CPU');
+ host = 'host-a'; scan();
+ const beforeHost = notices().length;
+ host = 'host-b'; scan();
+ assert.equal(notices().length, beforeHost + 1, 'equal PID and birth on another host is a different process');
+ const resolved = store.allShouts(board).filter(row => row.shout_text.startsWith('Resolved:') && row.shout_text.includes('AGENT_CPU')).length;
+ host = 'host-a'; scan(); host = 'host-b'; scan();
+ assert.equal(notices().length, beforeHost + 1, 'alternating hosts do not repeat each other’s alert');
+ assert.equal(store.allShouts(board).filter(row => row.shout_text.startsWith('Resolved:') && row.shout_text.includes('AGENT_CPU')).length, resolved, 'one host cannot resolve another host’s observation');
assert.ok(calls.some(([command, args]) => command === '/bin/ps' && args.includes('time=')));
assert.ok(calls.some(([command]) => command === '/usr/sbin/lsof'));
});