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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
/** Health mutation extracted without changing its transaction or notice order [A6]. */
import { ENGINE_VERSION } from './machine.js';
import {
itemById,
listAgents,
} from './board-query.js';
import { logEvent } from './board-event-log.js';
import { Refused } from './refused.js';
import { normalizedHealthFindings } from './health-receipts.js';
import { healthFindingIdentity } from './health-receipts.js';
import { healthTargets } from './health-receipts.js';
import { deliverHealthNotices } from './health-receipts.js';
import { HEALTH_SENTINEL_ENGINE } from './health-receipts.js';
import { COORDINATOR } from './config.js';
import { PERSON } from './board-query.js';
import { CPU_SENTINEL_ENGINE } from './cpu-sentinel-engine.js';
import { hasPendingCpuNotices } from './health-receipts.js';
/** Record one current health snapshot atomically, notifying each new or resolved episode once [A6]. */
export function applyHealthFindings(board, { agentId, observedRevision, findings }, { atomic, insertShout }) {
return atomic(board, () => {
const minimum = hasPendingCpuNotices(board) || (Array.isArray(findings) && findings.some(finding => finding?.cause === 'AGENT_CPU')) ? CPU_SENTINEL_ENGINE : HEALTH_SENTINEL_ENGINE;
if ((board.executionEngineVersion ?? ENGINE_VERSION) < minimum) {
throw new Refused('ENGINE_VERSION', `sentinel receipts require engine ${minimum}; upgrade Pullboard before scanning`);
}
if (!board.db.prepare('SELECT 1 FROM agent WHERE agent_id = ?').get(agentId)) {
throw new Refused('NOT_JOINED', 'join this board before recording a health scan: pullboard join <lane>');
}
const revision = board.db.prepare('SELECT COALESCE(MAX(event_id), 0) AS revision FROM event').get().revision;
if (!Number.isSafeInteger(observedRevision) || observedRevision !== revision) {
throw new Refused('HEALTH_SCAN_STALE', 'the board changed while health was scanning; run pullboard resume again');
}
const active = normalizedHealthFindings(findings);
const last = board.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 before = new Map(previous.map(finding => [healthFindingIdentity(finding), finding]));
const after = new Map(active.map(finding => [healthFindingIdentity(finding), finding]));
const added = active.filter(finding => !before.has(healthFindingIdentity(finding)));
const resolved = previous.filter(finding => !after.has(healthFindingIdentity(finding)));
const pending = last ? JSON.parse(last.event_detail).pendingCpu ?? [] : [];
if (!added.length && !resolved.length && !pending.length) return { added: [], resolved: [] };
const agents = listAgents(board);
const lanes = [...new Set(agents.map(agent => agent.agent_lane))];
const engine = board.executionEngineVersion ?? ENGINE_VERSION;
/** Deliver through ordinary feed/inbox records without registering a synthetic worktree seat. */
const log = (finding, cleared) => {
const item = finding.item === null ? null : itemById(board, finding.item);
logEvent(board, 'sentinel', cleared ? 'health-resolved' : 'health-failure', finding.item, {
key: finding.key, cause: `refusal:${finding.cause}`, since: finding.since, subject: finding.subject,
owner: finding.owner, commit: item?.item_commit ?? null, engine: HEALTH_SENTINEL_ENGINE,
});
};
const deferred = deliverHealthNotices({ resolved, added, pending, engine, log, coordinator: COORDINATOR, shout: (to, text) => insertShout(board, { from: 'sentinel', to, text, lanes }),
targets: finding => healthTargets(finding, { engine, coordinator: COORDINATOR, person: PERSON, lanes, agents }) });
logEvent(board, agentId, 'health-scan', null, { active, ...(deferred === undefined ? {} : { pendingCpu: deferred }), engine: HEALTH_SENTINEL_ENGINE });
return { added: added.map(finding => finding.key), resolved: resolved.map(finding => finding.key) };
});
}