Extract Persona scheduler control source

This commit is contained in:
Meta Vens 2026-07-15 23:33:37 +01:00
commit 128858dd1c
7 changed files with 105 additions and 61 deletions

View file

@ -1,8 +1,7 @@
import "dotenv/config";
import { spawn } from "node:child_process";
import { spawn, spawnSync } from "node:child_process";
import { appendFile, mkdir, readFile, stat, writeFile } from "node:fs/promises";
import { createRequire } from "node:module";
import { homedir } from "node:os";
import { dirname, resolve } from "node:path";
@ -80,6 +79,10 @@ import {
type PersonaChannelOption as RepoFaceChannelOption,
type PersonaChannelPlan as RepoFaceChannelPlan,
} from "../apps/persona-scheduler/dist/turn-routing.js";
import {
readAgentSwarmPause,
readSwarmControlState,
} from "../apps/persona-scheduler/dist/control-source.js";
async function main(): Promise<void> {
const config = loadConfig();
@ -304,27 +307,6 @@ async function main(): Promise<void> {
);
}
async function readAgentSwarmPause(): Promise<{ paused: boolean; path: string; reason?: string }> {
const path = resolve(process.cwd(), "state", "agent-swarm-paused.json");
try {
const parsed = JSON.parse(stripLeadingBom(await readFile(path, "utf8"))) as Record<string, unknown>;
return {
paused: parsed.paused !== false,
path,
reason: typeof parsed.reason === "string" ? parsed.reason : undefined,
};
} catch (error) {
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
return { paused: false, path };
}
return {
paused: true,
path,
reason: "Pause file exists but could not be parsed; failing closed.",
};
}
}
interface ParticipantSpec extends SchedulerParticipantSpec {
identity?: RepoDiscordIdentity;
}
@ -4438,44 +4420,6 @@ function projectCharacterDescription(description: string | undefined): string |
.join(" ");
}
async function readSwarmControlState(): Promise<{ globalHeat: number } | null> {
const controlStorePath = resolve(
process.env.VOIDBOT_SWARM_CONTROL_STORE || ".voidbot/private/swarm-controls.cc",
);
try {
const packageJson = resolve(process.cwd(), "..", "CultLib", "packages", "cultmesh-ts", "package.json");
const requireCult = createRequire(packageJson);
const { CultMesh } = requireCult("./dist/index.js");
const { defineDocumentType } = createRequire(
resolve(process.cwd(), "..", "CultLib", "packages", "cultcache-ts", "package.json"),
)("./dist/index.js");
const definition = defineDocumentType({
type: "voidbot.swarm_control_state",
schemaName: "voidbot.swarm_control_state",
schemaId: "voidbot.swarm_control_state.v1",
schemaVersion: "voidbot.swarm_control_state.v1",
global: false,
name: () => "voidbot-swarm",
schema: {
parse(value: unknown) {
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new Error("VoidBot swarm control state must be an object.");
}
return value;
},
},
});
const node = await CultMesh.createNode(controlStorePath, { documents: [definition] });
const value = node.get(definition, "voidbot-swarm") as { globalHeat?: unknown } | undefined;
const globalHeat = Number(value?.globalHeat);
return Number.isFinite(globalHeat) && globalHeat >= 0.05 && globalHeat <= 2
? { globalHeat }
: null;
} catch {
return null;
}
}
function mergeStrings(values: string[], value: string): string[] {
return Array.from(new Set([...values, value]));
}

View file

@ -54,6 +54,10 @@ import {
newestPendingMentionChannel,
personaChannelSpeedMultiplier,
} from "../apps/persona-scheduler/dist/turn-routing.js";
import {
readAgentSwarmPause,
readSwarmControlState,
} from "../apps/persona-scheduler/dist/control-source.js";
function participant(identityId: string, nextTurnAt: number): InitiativeParticipant {
return {
@ -411,6 +415,22 @@ assert.equal(newestPendingMentionChannel([
{ identityId: "nibu", channelId: "newer", queuedAt: "2026-07-15T21:00:00.000Z" },
] as RepoFacePendingMention[]), "newer", "routing follows the newest pending room obligation");
const controlDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-controls-"));
try {
const pausePath = join(controlDirectory, "pause.json");
assert.equal((await readAgentSwarmPause({ path: pausePath })).paused, false, "an absent pause witness leaves the resident scheduler running");
await writeFile(pausePath, "{broken", "utf8");
const malformedPause = await readAgentSwarmPause({ path: pausePath });
assert.equal(malformedPause.paused, true, "a present malformed pause witness fails closed");
assert.match(malformedPause.reason ?? "", /could not be parsed/, "the pause source explains its fail-closed observation");
await writeFile(pausePath, JSON.stringify({ paused: false, reason: "operator resumed" }), "utf8");
assert.deepEqual(await readAgentSwarmPause({ path: pausePath }), { paused: false, path: pausePath, reason: "operator resumed" }, "the pause source returns operator facts without touching scheduler state");
assert.deepEqual(await readSwarmControlState({ loadControl: () => ({ globalHeat: 1.75 }) }), { globalHeat: 1.75 }, "typed CultMesh heat is projected through the control source");
assert.equal(await readSwarmControlState({ loadControl: () => ({ globalHeat: 4 }) }), null, "out-of-contract heat fails closed before the engine sees it");
} finally {
await rm(controlDirectory, { recursive: true, force: true });
}
const stateDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-scheduler-"));
try {
const statePath = join(stateDirectory, "scheduler.json");