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.

2.6 KB
Raw Blame
/** A machine-configured one-minute watch, independent of ordinary ten-minute health scans [H5,H17]. */
import { probeRelayCanary } from './relay-canary-probe.js';
import {
  claimRelayCanaryTick,
  observeRelayHealth,
  readRelayHealth,
  pendingRelayCanaryNotices,
  markCanaryNoticeRecorded,
} from './relay-canary-health.js';
import { Refused } from './refused.js';
import { relayCanaryConfig } from './relay-canary-config.js';
import { loadMachineSettings } from './settings.js';

export const RELAY_CANARY_INTERVAL_MS = 60000;

/** Observe exactly one complete probe; optional notices use the ordinary command's board authority. */
export async function runRelayCanary(value, { io = {}, now = Date.now, notify } = {}) {
  const config = relayCanaryConfig(value);
  if (!config) throw new Refused('RELAY_CANARY_CONFIG', 'configure this machine with pullboard relay canary on before running its watch');
  const observation = await probeRelayCanary(config.root, { agentId: config.agentId,
    deadlineMs: config.deadlineMs, io });
  const update = observeRelayHealth(config.root, observation, { now: now() });
  if (notify) for (const notice of pendingRelayCanaryNotices(config.root)) {
    await notify(notice, observation);
    markCanaryNoticeRecorded(config.root, notice.id);
  }
  return { observation, ...update, health: readRelayHealth(config.root, { now: now() }) };
}

/** Start one due local tick; shutdown aborts and awaits the active probe instead of abandoning a write. */
export function startRelayCanaryWatch({ config = () => loadMachineSettings().relay?.canary,
  run = runRelayCanary, now = Date.now, every = setInterval, cancel = clearInterval, onError = () => {}, notify, keepAlive = false } = {}) {
  const lifetime = new AbortController();
  let pending = null;
  /** Read current machine opt-in on every tick and admit at most one local run for its minute. */
  const tick = () => {
    if (pending || lifetime.signal.aborted) return pending;
    pending = Promise.resolve().then(async () => {
      const selected = relayCanaryConfig(config());
      if (!selected?.enabled || !claimRelayCanaryTick(selected.root, now())) return;
      await run(selected, { io: { relaySignal: lifetime.signal }, now, notify });
    }).catch(error => { if (!lifetime.signal.aborted) onError(error); }).finally(() => { pending = null; });
    return pending;
  };
  const timer = every(tick, RELAY_CANARY_INTERVAL_MS);
  if (!keepAlive) timer.unref?.();
  void tick();
  return { tick,
    /** Stop scheduling first, then drain the exact in-flight probe under its shared deadline. */
    async close() {
      cancel(timer); lifetime.abort();
      if (pending) await pending;
    },
  };
}