From 510cdf1d2c9e1aeb4999b0498115ab4cbc85eee8 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 15 Jul 2026 02:44:25 +0100 Subject: [PATCH] Fix live operator source watcher --- scripts/voidbot-swarm-operator-view.cjs | 2 +- scripts/voidbot-swarm-operator-view.test.cjs | 25 +++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/voidbot-swarm-operator-view.cjs b/scripts/voidbot-swarm-operator-view.cjs index 218a2fd..b665ab3 100644 --- a/scripts/voidbot-swarm-operator-view.cjs +++ b/scripts/voidbot-swarm-operator-view.cjs @@ -129,7 +129,7 @@ async function readSwarmOperatorInputs(paths) { } function watchSwarmOperatorInputs(paths, onChange, { debounceMs = 75 } = {}) { - const watched = [...new Set(Object.values(paths).map(pathModule.resolve))]; + const watched = [...new Set(Object.values(paths).map((value) => pathModule.resolve(value)))]; const byDirectory = new Map(); for (const path of watched) { const directory = pathModule.dirname(path); diff --git a/scripts/voidbot-swarm-operator-view.test.cjs b/scripts/voidbot-swarm-operator-view.test.cjs index 15b7717..4e7cf6d 100644 --- a/scripts/voidbot-swarm-operator-view.test.cjs +++ b/scripts/voidbot-swarm-operator-view.test.cjs @@ -5,7 +5,7 @@ const fs = require("node:fs/promises"); const os = require("node:os"); const path = require("node:path"); const test = require("node:test"); -const { operatorViewPublication, projectSwarmOperatorView, readSwarmOperatorInputs } = require("./voidbot-swarm-operator-view.cjs"); +const { operatorViewPublication, projectSwarmOperatorView, readSwarmOperatorInputs, watchSwarmOperatorInputs } = require("./voidbot-swarm-operator-view.cjs"); test("projects bounded operator state and lets control own heat", () => { const view = projectSwarmOperatorView({ @@ -70,3 +70,26 @@ test("reads source files independently and reports a missing owner", async () => await fs.rm(root, { recursive: true, force: true }); } }); + +test("watches atomic source replacement without polling", async () => { + const root = await fs.mkdtemp(path.join(os.tmpdir(), "voidbot-operator-watch-")); + const target = path.join(root, "heartbeat.json"); + await fs.writeFile(target, "{}"); + let stop = () => {}; + try { + const changed = new Promise((resolve, reject) => { + const timeout = setTimeout(() => reject(new Error("source watcher did not observe replacement")), 2000); + stop = watchSwarmOperatorInputs({ heartbeat: target }, () => { + clearTimeout(timeout); + resolve(); + }, { debounceMs: 10 }); + }); + const replacement = path.join(root, "heartbeat.next.json"); + await fs.writeFile(replacement, '{"state":"ready"}'); + await fs.rename(replacement, target); + await changed; + } finally { + stop(); + await fs.rm(root, { recursive: true, force: true }); + } +});