import assert from "node:assert/strict"; import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { queueAgentHeartbeatMention, postDiscordViaBifrostCultMesh, ensureVoidSelfStateIdentityProfile, createEmptyVoidSelfState, readRepoFaceMentionInbox, resolveRepoFaceHeartbeatDebugProjectionPath, resolveRepoFaceHeartbeatStatePath, type CreateJobInput, type JobQueue, type RepoDiscordIdentity, type RepoFacePendingMention, } from "@voidbot/core"; import type { JobRecord } from "@voidbot/shared"; import { advanceInitiativeClockFromWallClock, applyActiveTurnFreeze, applyPendingMentionPriority, applySchedulerControls, applySemanticPressureProjection, consumePendingMentions, finalizeSchedulerTick, reconcileParticipants, reconcileInitiativeParticipants, recordDryRunSelection, recordSchedulerSkip, recordTurnFailedToStart, recordTurnStarted, selectReadyParticipants, type InitiativeParticipant, type InitiativeState, } from "../apps/persona-scheduler/dist/initiative-engine.js"; import { newPersonaSchedulerState, readPersonaSchedulerState, writePersonaSchedulerState, } from "../apps/persona-scheduler/dist/state-store.js"; import { parsePersonaTurnIdentity, staleActiveTurnThresholdMs, } from "../apps/persona-scheduler/dist/active-turn-source.js"; import { newestUnpromptedTurnQueuedAt, readDiscordActivitySnapshot, } from "../apps/persona-scheduler/dist/discord-activity-source.js"; import { fetchChannelSnapshots, fetchRecentDiscordMessages, } from "../apps/persona-scheduler/dist/turn-context-source.js"; import { readBifrostGovernanceDigest } from "../apps/persona-scheduler/dist/bifrost-governance-source.js"; import { submitPersonaTurn } from "../apps/persona-scheduler/dist/turn-actuator.js"; import { buildPersonaChannelPlan, newestPendingMentionChannel, personaChannelSpeedMultiplier, } from "../apps/persona-scheduler/dist/turn-routing.js"; import { readAgentSwarmPause, readSwarmControlState, } from "../apps/persona-scheduler/dist/control-source.js"; import { readRepoActivity } from "../apps/persona-scheduler/dist/repo-activity-source.js"; import { projectRepoActivityObservation } from "../apps/persona-scheduler/dist/repo-activity-projector.js"; import { readPersonaStateObservation } from "../apps/persona-scheduler/dist/persona-state-source.js"; import { readPersonaMemoryRecall } from "../apps/persona-scheduler/dist/persona-memory-context-source.js"; import { coordinatePersonaMemoryTurn } from "../apps/persona-scheduler/dist/persona-memory-turn-coordinator.js"; import { projectPersonaMemoryRecall } from "../apps/persona-scheduler/dist/persona-memory-recall-projector.js"; import { composePersonaMemoryPacket, projectPersonaMemorySurface, renderPersonaPressureSections, renderPersonaSelfMaintenancePressure, renderPersonaTypedStateSections } from "../apps/persona-scheduler/dist/persona-memory-projector.js"; import { readPersonaCuriosityEvidence } from "../apps/persona-scheduler/dist/persona-curiosity-context-source.js"; import { projectPersonaCuriosityContext } from "../apps/persona-scheduler/dist/persona-curiosity-projector.js"; import { projectPersonaConversation, renderPersonaRoomTopicSaturation } from "../apps/persona-scheduler/dist/persona-conversation-projector.js"; import { buildPersonaJurisdictionDiveDirective, buildPersonaTurnPrompt } from "../apps/persona-scheduler/dist/persona-turn-prompt-projector.js"; import { assemblePersonaTurn } from "../apps/persona-scheduler/dist/persona-turn-assembler.js"; import { readGlobalAgentDoctrine } from "../apps/persona-scheduler/dist/global-agent-doctrine-source.js"; import { buildInspectionParticipant } from "../apps/persona-scheduler/dist/inspection-participant-factory.js"; import { runPersonaSchedulerTick } from "../apps/persona-scheduler/dist/persona-scheduler-runner.js"; import { projectVoidPhysiology } from "../apps/persona-scheduler/dist/void-physiology-domain.js"; import { runVoidPhysiologyOrgan } from "../apps/persona-scheduler/dist/void-physiology-organ.js"; import { buildVoidMemoryMaintenancePrompt, parseVoidMemoryMaintenanceOperations, projectVoidMemoryMaintenanceContext } from "../apps/persona-scheduler/dist/void-memory-maintenance-projector.js"; import { runVoidMemoryMaintenance } from "../apps/persona-scheduler/dist/void-memory-maintenance-organ.js"; import { projectVoidMemoryOperations } from "../apps/persona-scheduler/dist/void-memory-text-actuator.js"; import { buildVoidModerationHeartbeatPrompt, parseVoidModerationHeartbeatOperations, projectVoidModerationHeartbeatContext } from "../apps/persona-scheduler/dist/void-moderation-heartbeat-projector.js"; import { readVoidModerationEvidence } from "../apps/persona-scheduler/dist/void-moderation-evidence-source.js"; import { runVoidModerationHeartbeat } from "../apps/persona-scheduler/dist/void-moderation-heartbeat-organ.js"; import { projectVoidModerationOperations } from "../apps/persona-scheduler/dist/void-moderation-text-actuator.js"; import { runVoidModerationEnforcement } from "../apps/persona-scheduler/dist/void-moderation-enforcement-organ.js"; import { buildVoidRuminationPrompt, parseVoidRuminationOperations, projectVoidRuminationContext } from "../apps/persona-scheduler/dist/void-rumination-projector.js"; import { runVoidRumination } from "../apps/persona-scheduler/dist/void-rumination-organ.js"; import { projectVoidRuminationOperations } from "../apps/persona-scheduler/dist/void-rumination-text-actuator.js"; import { runVoidCandidateDelivery } from "../apps/persona-scheduler/dist/void-candidate-delivery-organ.js"; import { projectGamecultPersonaState } from "../apps/persona-scheduler/dist/persona-standard-state-projector.js"; import { projectPersonaStatePacket } from "../apps/persona-scheduler/dist/persona-state-packet-projector.js"; import { extractLastPersonaProjectionMessage, isRetryablePersonaProjectionFailure } from "../apps/persona-scheduler/dist/persona-text-projection-actuator.js"; import { observePersonaRoomTexture, projectPersonaSocialContext, renderPersonaHumanClarityPressure, renderPersonaHumanPronounFacts, renderPersonaRoomWeather, renderPersonaSocialGraph } from "../apps/persona-scheduler/dist/persona-social-context-projector.js"; import { readPersonaHumanPronounGuidance, readStoredPersonaHumanPronounGuidance } from "../apps/persona-scheduler/dist/persona-social-context-source.js"; async function main(): Promise { function participant(identityId: string, nextTurnAt: number): InitiativeParticipant { return { identityId, participantKind: "repo_face", turnKind: "repo_face_rumination", repoName: identityId, displayName: identityId, initiativeSpeed: 1, status: "active", currentLoad: 0, nextTurnAt, baseRecoveryMinutes: 30, reactionBias: 1, interruptThreshold: 0.6, groups: [], heat: 1, dynamicHeat: 1, responsePressure: 0, responsePressureEvidence: [], semanticInterruptReceipts: [], effectiveSpeed: 1, queuedCount: 0, constraints: [], }; } const state: InitiativeState = { initiativeClock: 10, lastTickAt: "2026-07-15T20:00:00.000Z", participants: [participant("quiet", 9), participant("mentioned", 30), participant("napping", 9)], pendingMentions: [{ identityId: "mentioned" }], history: [], }; assert.equal(extractLastPersonaProjectionMessage([ JSON.stringify({ type: "item.completed", item: { type: "agent_message", text: "first" } }), JSON.stringify({ type: "item.completed", item: { type: "agent_message", text: "final projection" } }), ].join("\n")), "final projection", "text projection actuator owns Codex event decoding"); assert.equal(isRetryablePersonaProjectionFailure({ stdout: "", stderr: "HTTP 429 too many requests" }), true, "capacity failures may advance to the next configured projection model"); assert.equal(isRetryablePersonaProjectionFailure({ stdout: "", stderr: "prompt contract rejected" }), false, "semantic or contract failures cannot hide behind model fallback"); advanceInitiativeClockFromWallClock(state, new Date("2026-07-15T20:02:30.000Z")); assert.equal(state.initiativeClock, 12.5, "wall time advances the scheduler clock once"); applyPendingMentionPriority(state); assert.equal(state.participants[1].nextTurnAt, 12.5, "a mention makes its participant ready without queuing it"); state.participants[0].responsePressure = state.participants[0].interruptThreshold; state.participants[0].responsePressureEvidence = [{ messageId: "fresh-pressure", observedAt: "2026-07-15T20:02:00.000Z", similarity: 0.8, contribution: state.participants[0].interruptThreshold, }]; const selected = selectReadyParticipants( state, 2, new Set(), new Map([["napping", { isNapping: true }]]), {}, Date.parse("2026-07-15T20:02:30.000Z"), ); assert.deepEqual(selected.map((entry) => entry.identityId), ["mentioned", "quiet"], "mentions lead while one pressure-backed unprompted participant is admitted"); state.participants[0].responsePressure = 0; const clockOnly = selectReadyParticipants(state, 2, new Set(), new Map(), {}, Date.parse("2026-07-15T20:02:30.000Z")); assert.deepEqual(clockOnly.map((entry) => entry.identityId), ["mentioned"], "elapsed wall clock alone cannot authorize an unprompted model turn"); const cooled = selectReadyParticipants( state, 2, new Set(), new Map(), { nextUnpromptedTurnAllowedAt: "2026-07-15T21:00:00.000Z" }, Date.parse("2026-07-15T20:02:30.000Z"), ); assert.deepEqual(cooled.map((entry) => entry.identityId), ["mentioned"], "idle cooling suppresses unprompted turns but never pending mentions"); const activeRoomCadence = selectReadyParticipants( state, 2, new Set(), new Map(), { nextUnpromptedTurnAllowedAt: "2026-07-15T21:00:00.000Z" }, Date.parse("2026-07-15T20:03:30.000Z"), ); assert.deepEqual(activeRoomCadence.map((entry) => entry.identityId), ["mentioned"], "normal room activity cannot bypass the global unprompted cadence"); const blocked = { ...participant("existing", 44), status: "blocked" as const, currentLoad: 1, activeJobId: "job-1" }; const reconciled = reconcileParticipants({ existing: [blocked], specs: [ { id: "existing", participantKind: "native_persona", turnKind: "repo_face_rumination", repoName: "New body", displayName: "Existing", allowedChannelIds: ["room"], channelSpeedMultiplier: 2, }, { id: "channel-less", participantKind: "repo_face", turnKind: "repo_face_rumination", repoName: "No mouth", displayName: "No mouth", allowedChannelIds: [], channelSpeedMultiplier: 1, }, ], speedOverrides: { existing: 1.5 }, heatOverrides: { "identity:existing": 0.5 }, initiativeClock: 40, baseRecoveryMinutes: 20, globalHeat: 1.2, }); assert.equal(reconciled[0].status, "blocked", "reconciliation preserves an explicit manual block"); assert.equal(reconciled[0].activeJobId, "job-1", "reconciliation preserves live load ownership"); assert.equal(reconciled[0].nextTurnAt, 44, "reconciliation preserves initiative position"); assert.equal(reconciled[0].initiativeSpeed, 3, "operator speed and channel multiplier are projected once"); assert.equal(reconciled[0].heat, 0.6, "global and identity heat compose in the scheduler owner"); assert.equal(reconciled[1].status, "blocked", "a new Face without any mouth fails closed"); const freezeState: InitiativeState = { initiativeClock: 50, participants: [], pendingMentions: [], history: [], }; const frozen = applyActiveTurnFreeze(participant("working", 40), "job-live", freezeState, new Set()); assert.equal(frozen.currentLoad, 1, "an active queue witness freezes initiative"); assert.equal(frozen.activeJobId, "job-live", "the scheduler records the external job witness without transferring ownership"); assert.equal(frozen.activeTurnStartedAt, 50, "freeze begins at the scheduler clock when no earlier start exists"); const completed = new Set(); const recovered = applyActiveTurnFreeze(frozen, undefined, freezeState, completed); assert.equal(recovered.currentLoad, 0, "loss of the active witness releases scheduler load"); assert.equal(recovered.activeJobId, undefined, "completed work cannot remain an initiative owner"); assert.equal(recovered.nextTurnAt, 80, "recovery begins after completion using scheduler-owned recovery math"); assert.deepEqual([...completed], ["working"], "a just-completed participant cannot be selected in the same tick"); assert.equal(freezeState.history.at(-1)?.type, "turn_completed", "the state machine records its own completion transition"); const queueState: InitiativeState = { initiativeClock: 70, participants: [], pendingMentions: [], history: [] }; const queued = participant("queued", 68); recordTurnStarted({ participant: queued, state: queueState, queuedAt: "2026-07-15T21:00:00.000Z", activeJobId: "job-queued", requestMessageId: "agent-turn:queued:test", pendingMentionCount: 2, }); assert.equal(queued.currentLoad, 1, "queue success freezes the participant through one scheduler commit"); assert.equal(queued.queuedCount, 1, "queue success advances its counter once"); assert.equal(queueState.history.at(-1)?.type, "queued", "queue success and its history witness share one owner"); recordTurnFailedToStart({ participant: queued, state: queueState, queuedAt: "2026-07-15T21:01:00.000Z", reason: "actuator refused" }); assert.equal(queued.currentLoad, 0, "queue failure cannot leave false scheduler load"); assert.equal(queueState.history.at(-1)?.type, "turn_failed_to_start", "queue failure is recorded by the state owner"); const inspected = participant("inspected", 69); recordDryRunSelection(inspected, queueState, "2026-07-15T21:02:00.000Z", 0); assert.equal(inspected.nextTurnAt, 100, "dry-run selection uses the same recovery ownership without claiming active load"); assert.equal(inspected.currentLoad, 0, "inspection cannot impersonate a live job"); assert.equal(parsePersonaTurnIdentity("agent-turn:nibu:2026-07-15"), "nibu", "the queue adapter recognizes current Persona turn witnesses"); assert.equal(parsePersonaTurnIdentity("unrelated:job"), undefined, "unrelated jobs cannot freeze Persona initiative"); assert.equal(staleActiveTurnThresholdMs(20 * 60_000), 60 * 60_000, "runtime timeout expands the stale-claim boundary"); assert.equal(staleActiveTurnThresholdMs(5 * 60_000), 45 * 60_000, "stale recovery never becomes an impatient repair loop"); assert.equal(newestUnpromptedTurnQueuedAt([ { type: "queued", queuedAt: "2026-07-15T19:00:00.000Z", pendingMentionCount: 0 }, { type: "queued", queuedAt: "2026-07-15T20:00:00.000Z", pendingMentionCount: 1 }, ]), "2026-07-15T19:00:00.000Z", "direct attention cannot consume the unprompted-turn cooling window"); const activitySnapshot = await readDiscordActivitySnapshot({ botToken: "test-token", channelIds: ["aquarium", "aquarium", undefined], policy: { enabled: true, idleAfterMinutes: 30, recoveryMinutes: 90 }, history: [{ type: "queued", queuedAt: "2026-07-15T19:30:00.000Z", pendingMentionCount: 0 }], now: new Date("2026-07-15T21:00:00.000Z"), fetchImpl: async () => new Response(JSON.stringify([ { id: "bot-message", author: { id: "bot", username: "VoidBot", bot: true }, content: "machine noise", timestamp: "2026-07-15T20:55:00.000Z", }, { id: "human-message", author: { id: "human", username: "Human", global_name: "Operator" }, content: "still here", timestamp: "2026-07-15T20:45:00.000Z", }, ]), { status: 200, headers: { "content-type": "application/json" } }), }); assert.deepEqual(activitySnapshot.checkedChannelIds, ["aquarium"], "activity acquisition deduplicates its explicit watched channels"); assert.deepEqual(activitySnapshot.observedHumanMessages.map((message) => message.id), ["human-message"], "bot output cannot impersonate human room activity"); assert.equal(activitySnapshot.active, false, "recent human activity keeps idle cooling inactive"); assert.equal(activitySnapshot.idleForMinutes, 15, "idle duration is a neutral wall-clock observation"); assert.equal(activitySnapshot.nextUnpromptedTurnAllowedAt, "2026-07-15T21:00:00.000Z", "the activity source derives the next unprompted window without mutating initiative"); const disabledCoolingSnapshot = await readDiscordActivitySnapshot({ botToken: "test-token", channelIds: ["aquarium"], policy: { enabled: false, idleAfterMinutes: 30, recoveryMinutes: 90 }, history: [{ type: "queued", queuedAt: "2026-07-15T19:30:00.000Z", pendingMentionCount: 0 }], now: new Date("2026-07-15T20:00:00.000Z"), fetchImpl: async () => new Response("[]", { status: 200, headers: { "content-type": "application/json" } }), }); assert.equal(disabledCoolingSnapshot.nextUnpromptedTurnAllowedAt, "2026-07-15T21:00:00.000Z", "disabling idle rest cannot disable unprompted speech cadence"); const pressureState: InitiativeState = { initiativeClock: 50, participants: [participant("awakened", 90), participant("unmoved", 90)], pendingMentions: [], history: [], }; applySemanticPressureProjection({ state: pressureState, projections: [{ identityId: "awakened", pressure: 0.8, interrupt: true, evidence: [{ messageId: "pressure-message", observedAt: "2026-07-15T20:59:00.000Z", similarity: 0.9, contribution: 0.8, }], }], projectedAt: new Date("2026-07-15T21:00:00.000Z"), }); assert.equal(pressureState.participants[0].nextTurnAt, 50, "only the initiative engine may turn semantic evidence into readiness"); assert.equal(pressureState.participants[0].dynamicHeat, 2.2, "the engine owns pressure-derived recovery heat"); assert.deepEqual(pressureState.participants[0].semanticInterruptReceipts, [], "observing semantic pressure does not consume it before a turn commits"); assert.deepEqual( selectReadyParticipants(pressureState, 1, new Set(), new Map(), {}, Date.parse("2026-07-15T21:00:00.000Z")).map((entry) => entry.identityId), ["awakened"], "fresh semantic pressure authorizes one unprompted turn", ); recordTurnStarted({ participant: pressureState.participants[0], state: pressureState, queuedAt: "2026-07-15T21:00:00.000Z", pendingMentionCount: 0, }); pressureState.participants[0].currentLoad = 0; pressureState.participants[0].nextTurnAt = pressureState.initiativeClock; assert.deepEqual(pressureState.participants[0].semanticInterruptReceipts, ["pressure-message"], "a committed unprompted turn consumes its semantic evidence"); assert.deepEqual( selectReadyParticipants(pressureState, 1, new Set(), new Map(), {}, Date.parse("2026-07-15T21:01:00.000Z")), [], "the next observation pulse cannot replay consumed semantic pressure", ); assert.equal(pressureState.participants[1].responsePressure, 0, "missing projections explicitly cool stale pressure"); assert.equal(pressureState.history.some((entry) => entry.type === "semantic_response_interrupt"), true, "the state owner records the semantic transition"); const commitState: InitiativeState = { initiativeClock: 20, participants: [participant("commit-owner", 25)], pendingMentions: [{ identityId: "commit-owner", id: "mention-1", visiblePrompt: " answer this " }], history: [], }; applySchedulerControls({ state: commitState, baseRecoveryMinutes: 45, globalHeat: 1.2 }); assert.equal((commitState as InitiativeState & { baseRecoveryMinutes: number }).baseRecoveryMinutes, 45, "scheduler controls cross one engine commit primitive"); consumePendingMentions({ state: commitState, participant: commitState.participants[0], mentions: commitState.pendingMentions, consumedAt: "2026-07-15T21:00:00.000Z", }); assert.equal(commitState.pendingMentions.length, 0, "only the engine consumes pending attention after queue success"); assert.equal((commitState.history.at(-1)?.mentions as Array<{ visiblePrompt: string }>)[0].visiblePrompt, "answer this", "the durable consumption witness is bounded and normalized"); finalizeSchedulerTick(commitState, new Date("2026-07-15T21:01:00.000Z")); assert.equal(commitState.lastTickAt, "2026-07-15T21:01:00.000Z", "tick finalization belongs to the state owner"); const skippedState: InitiativeState = { initiativeClock: 0, participants: [], pendingMentions: [], history: [] }; recordSchedulerSkip({ state: skippedState, skippedAt: new Date("2026-07-15T21:02:00.000Z"), reason: "paused" }); assert.equal(skippedState.history[0].type, "skipped", "pause observations become scheduler history through an engine commit"); const reconciledState: InitiativeState = { initiativeClock: 20, participants: [], pendingMentions: [], history: [] }; reconcileInitiativeParticipants({ state: reconciledState, specs: [{ id: "resident", participantKind: "repo_face", turnKind: "repo_face_rumination", repoName: "Resident", displayName: "Resident", allowedChannelIds: ["aquarium"], channelSpeedMultiplier: 1 }], defaultChannelId: "aquarium", speedOverrides: {}, heatOverrides: {}, baseRecoveryMinutes: 30, globalHeat: 1, activeTurns: new Map(), completedThisTick: new Set(), }); assert.deepEqual(reconciledState.participants.map((entry) => entry.identityId), ["resident"], "the engine commits reconciled participants instead of returning a second-writer patch"); const contextMessages = await fetchRecentDiscordMessages({ botToken: "test-token", channelId: "aquarium", limit: 15, ignoreBotMessages: true, fetchImpl: async () => new Response(JSON.stringify([ { id: "newer", author: { id: "human-2", username: "Second" }, content: "second", timestamp: "2026-07-15T21:00:00.000Z", attachments: [{ id: "text", filename: "note.txt", url: "https://example.invalid/note.txt" }], }, { id: "ignored-bot", author: { id: "bot", username: "Mirror", bot: true }, content: "mirror", timestamp: "2026-07-15T20:59:00.000Z", }, { id: "older", author: { id: "human-1", username: "First", global_name: "First Human" }, content: "first", timestamp: "2026-07-15T20:58:00.000Z", }, ]), { status: 200, headers: { "content-type": "application/json" } }), }); assert.deepEqual(contextMessages.map((message) => message.id), ["older", "newer"], "turn context is raw chronological Discord evidence with configured bot mirrors removed"); assert.equal(contextMessages[1].attachments?.[0].kind, "other", "non-image attachments remain inspectable without becoming media-cache writes"); const failedSnapshot = await fetchChannelSnapshots({ botToken: "test-token", channelIds: ["primary", "nearby"], primaryChannelId: "primary", limit: 6, now: new Date("2026-07-15T21:00:00.000Z"), fetchImpl: async () => new Response("nope", { status: 503 }), }); assert.equal(failedSnapshot[0].messages[0].id, "snapshot-error:nearby", "nearby-channel failure is returned as explicit evidence rather than aborting the turn"); assert.equal(failedSnapshot[0].messages[0].timestamp, "2026-07-15T21:00:00.000Z", "context-source failure witnesses use the caller's observation clock"); let bifrostArgs: string[] = []; const bifrostDigest = await readBifrostGovernanceDigest({ bifrostRoot: "Bifrost", repoName: "VoidBot", agentIdentity: "void", runDigest: (_scriptPath, args) => { bifrostArgs = args; return { status: 0, stdout: JSON.stringify({ generatedAt: "2026-07-15T21:00:00.000Z", topics: [] }) }; }, }); assert.deepEqual(bifrostArgs, ["digest", "--repo", "VoidBot", "--agent", "void", "--limit", "6"], "the Bifrost source alone owns the governance CLI contract"); assert.deepEqual(bifrostDigest.topics, [], "typed Bifrost governance facts cross the source boundary unchanged"); const malformedBifrostDigest = await readBifrostGovernanceDigest({ bifrostRoot: "Bifrost", repoName: "VoidBot", agentIdentity: "void", now: new Date("2026-07-15T21:00:00.000Z"), runDigest: () => ({ status: 0, stdout: "not-json" }), }); assert.equal(malformedBifrostDigest.topics[0].id, "bifrost-digest-parse-error", "malformed provider output becomes an explicit typed error topic"); assert.equal(malformedBifrostDigest.generatedAt, "2026-07-15T21:00:00.000Z", "provider failure witnesses use the caller's observation clock"); const submittedJobs: CreateJobInput[] = []; const turnReceipt = await submitPersonaTurn({ jobQueue: { async createJob(input) { submittedJobs.push(input); return { created: true, job: { id: "job-1" } as JobRecord }; }, } satisfies Pick, provider: "owner_codex", identityId: "nibu", queuedAt: "2026-07-15T21:03:00.000Z", channelId: "aquarium", prompt: "Persona prompt", recentMessages: contextMessages, conversationFocus: { channelId: "aquarium", reason: "latest_human_message", isCurrentRoom: true }, conversationThreads: [], imageAttachments: [], }); assert.deepEqual(turnReceipt, { created: true, activeJobId: "job-1", requestMessageId: "agent-turn:nibu:2026-07-15T21:03:00.000Z", }, "the turn actuator returns only its queue receipt"); assert.equal(submittedJobs[0].command, "repo-face-rumination", "the actuator owns the worker command contract"); assert.equal(submittedJobs[0].initialState, "approved", "Persona turns enter the canonical queue through one approval policy"); assert.equal(submittedJobs[0].contextBundle.recentMessages.length, 2, "the actuator lowers supplied evidence into the worker context bundle"); assert.equal(submittedJobs[0].requester.id, "voidbot-agent-turn", "the actuator owns the worker-facing actor identity"); const routedIdentity = { id: "nibu", identityKind: "repo_face", repoName: "AetheriaLore", displayName: "Nibu", allowedChannelIds: ["legacy"], channelPermissions: [{ channelId: "aquarium", label: "Aquarium", topic: "casual conversation", speechThreshold: "very_low", speedMultiplier: 4, }, { channelId: "lore", label: "Lore", topic: "setting work", speechThreshold: "medium", speedMultiplier: 1, }], } as RepoDiscordIdentity; assert.deepEqual(await readPersonaMemoryRecall({ identity: routedIdentity, config: undefined as never, state: undefined, projectedMemory: "No memory was acquired.", recentMessages: [], channelSnapshots: [], }), { status: "unavailable", reason: "No typed Persona state observation was supplied.", }, "semantic recall fails explicitly before touching vector infrastructure when the typed Mind observation is absent"); const roomObservation = observePersonaRoomTexture({ identity: routedIdentity, recentMessages: [{ id: "human-1", authorId: "human", authorName: "Operator", content: "hello", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], }); assert.equal(roomObservation?.texture, "light", "social-context projection derives room weather from supplied messages without acquiring Discord state"); assert.match(renderPersonaRoomWeather({ identity: routedIdentity, recentMessages: [], channelSnapshots: [] }), /No current room weather/, "absent room evidence stays explicit"); assert.match(renderPersonaHumanPronounFacts([{ actorId: "human", actorName: "Operator", guidance: "use they/them", policy: "explicit", confidence: 1 }]) ?? "", /Confidence: 1\.00/, "pronoun guidance presentation belongs to the social-context projector"); let pronounReaderClosed = false; const pronounGuidance = await readPersonaHumanPronounGuidance({ ownerActorId: "owner", ownerFallbackName: "Metacrat", recentMessages: [{ id: "human-pronouns", authorId: "human", authorName: "Operator", content: "hello", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], openProfiles: async () => ({ getProfile: async (actorId) => actorId === "human" ? { actorName: "Operator", pronounPolicy: "explicit", resolvedPronounSets: ["they/them"], resolvedPronounSet: "they/them", pronounGuidance: "use they/them", pronounConfidence: 0.95, pronounEvidence: [ { pronounSet: "they/them", stance: "prefer", source: "explicit_correction", confidence: 0.9, timestamp: "2026-07-14T20:00:00.000Z", excerpt: "please use they/them" }, { pronounSet: "they/them", stance: "prefer", source: "ambient_usage", confidence: 1, timestamp: "2026-07-15T20:00:00.000Z", excerpt: "someone said they" }, ], } as never : undefined, close: async () => { pronounReaderClosed = true; }, }), }); assert.match(pronounGuidance[0]?.evidenceExcerpt ?? "", /please use they\/them/, "the social-context source owns pronoun evidence precedence rather than favoring merely newer ambient usage"); assert.equal(pronounReaderClosed, true, "the social-context source closes its injected interaction-profile reader"); let storedProfileReaderClosed = false; const storedPronounGuidance = await readStoredPersonaHumanPronounGuidance({ config: { ownerDiscordId: "human" } as never, recentMessages: [{ id: "human-pronouns", authorId: "human", authorName: "Operator", content: "hello", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], }, { openProfiles: async () => ({ getProfile: async () => ({ actorId: "human", actorName: "Operator", pronounPolicy: "explicit", resolvedPronounSet: "they/them", resolvedPronounSets: ["they/them"], pronounGuidance: "use they/them", pronounConfidence: 1, pronounEvidence: [{ pronounSet: "they/them", stance: "prefer", source: "explicit_self_statement", confidence: 1, timestamp: "2026-07-15T20:00:00.000Z", excerpt: "I use they/them" }], traits: [], notes: [], updatedAt: "2026-07-15T20:00:00.000Z" }), close: async () => { storedProfileReaderClosed = true; }, }), }); assert.equal(storedPronounGuidance[0]?.resolvedPronounSet, "they/them", "the stored-profile adapter feeds the existing social-context source without reimplementing pronoun policy"); assert.equal(storedProfileReaderClosed, true, "the stored-profile adapter preserves provider close semantics"); const clarityPressure = renderPersonaHumanClarityPressure({ identity: routedIdentity, recentMessages: [ { id: "later", authorId: "human", authorName: "Operator", content: "speak plainly", timestamp: "2026-07-15T22:00:00.000Z", isBot: false, channelId: "aquarium" }, { id: "earlier", authorId: "human", authorName: "Operator", content: "what are you talking about", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }, ], channelSnapshots: [], }); assert.match(clarityPressure ?? "", /said, "speak plainly"/, "clarity projection orders supplied evidence by its real timestamp before choosing the freshest human correction"); assert.equal(await projectPersonaMemorySurface({ identityId: "nibu", characterIdentity: "Nibu", statePacket: "Nibu remembers the ship mind and remains irritated by careless continuity.", modelProjectionEnabled: false, }), "Nibu remembers the ship mind and remains irritated by careless continuity.", "disabled model projection returns the validated deterministic memory surface without invoking a model"); await assert.rejects(projectPersonaMemorySurface({ identityId: "nibu", characterIdentity: "Nibu", statePacket: "path=private-state", modelProjectionEnabled: false, }), /leaked schema/, "the projector rejects state-path leakage on the model-free path"); const stateSourceDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-state-source-")); try { const statePath = join(stateSourceDirectory, "nibu.cc"); const observation = await readPersonaStateObservation({ identity: { ...routedIdentity, faceStatePath: statePath }, storageRoot: stateSourceDirectory, now: new Date("2026-07-15T21:00:00.000Z"), }); assert.equal(observation.status, "missing", "the Persona state source reports a missing canonical mind explicitly"); await assert.rejects(access(statePath), "observing a missing Persona state cannot create or mutate its canonical surface"); await ensureVoidSelfStateIdentityProfile({ canonicalPath: statePath, identity: { agentId: routedIdentity.id, publicName: routedIdentity.displayName, publicDescription: routedIdentity.description }, }); const beforeObservation = await readFile(statePath); const populatedObservation = await readPersonaStateObservation({ identity: { ...routedIdentity, faceStatePath: statePath }, storageRoot: stateSourceDirectory, now: new Date("2026-07-15T21:00:00.000Z"), }); assert.equal(populatedObservation.status, "ok", "the source acquires typed mind and physiology facts from an existing CultCache surface"); assert.equal(populatedObservation.status === "ok" && populatedObservation.rest?.isNapping, false, "rest projection is deterministic at the supplied observation time"); const typedSections = populatedObservation.status === "ok" ? renderPersonaTypedStateSections({ identityName: "Nibu", state: populatedObservation.typedState }) : undefined; assert.match(typedSections?.opening.join("\n") ?? "", /Speaking pressure:/, "the projector owns typed runtime-pressure presentation without acquiring Persona state"); const socialGraph = populatedObservation.status === "ok" ? renderPersonaSocialGraph({ identity: routedIdentity, registryIdentities: [routedIdentity], state: populatedObservation.typedState }) : undefined; assert.match(socialGraph ?? "", /Social graph topology:/, "social topology projection consumes typed state and the explicit registry without acquiring either"); const socialContext = populatedObservation.status === "ok" ? projectPersonaSocialContext({ identity: routedIdentity, registryIdentities: [routedIdentity, { ...routedIdentity, id: "peer", repoName: "PeerRepo", displayName: "Peer" }], state: populatedObservation.typedState, recentMessages: [ { id: "peer-opening", authorId: "peer", authorName: "Peer", content: "Nibu, the room is yours.", timestamp: "2026-07-15T21:01:00.000Z", isBot: true, channelId: "aquarium" }, { id: "human-pressure", authorId: "human", authorName: "Operator", content: "Nibu, your authority over this jurisdiction is being challenged.", timestamp: "2026-07-15T21:02:00.000Z", isBot: false, channelId: "aquarium" }, ], channelSnapshots: [], pronounGuidance: [], observedAt: new Date("2026-07-15T21:03:00.000Z"), }) : undefined; assert.match(socialContext?.peerOpening ?? "", /Peer: 1 recent nearby message/, "the aggregate social projection owns peer openings"); assert.match(socialContext?.socialPressure ?? "", /human voice/, "the aggregate social projection owns relationship-pressure scoring"); const maintenanceState = populatedObservation.status === "ok" ? { ...populatedObservation.typedState, faceAffect: { ...populatedObservation.typedState.faceAffect, needs: [{ needId: "need-tool", kind: "substrate" as const, status: "neglected" as const, target: { kind: "project" as const, id: "voidbot", label: "VoidBot" }, summary: "The Persona tool path is broken", claim: "The voice cannot reach its room", tension: "Private rumination cannot repair a missing actuator", actionImplication: "Ask plainly for the tool path to be repaired", intensity: 0.8, valence: -0.6, anchorRefs: [], evidenceRefs: [], sourceMemoryIds: [], createdAt: "2026-07-15T20:00:00.000Z", updatedAt: "2026-07-15T20:00:00.000Z", tags: ["anchor:missing"], }], }, } : undefined; assert.match(maintenanceState ? renderPersonaSelfMaintenancePressure({ identityName: "Nibu", state: maintenanceState, clarityPressureActive: false }) ?? "" : "", /Self-maintenance pressure requiring public advocacy/, "the memory projector owns self-maintenance classification and rendering"); assert.equal(maintenanceState ? renderPersonaSelfMaintenancePressure({ identityName: "Nibu", state: maintenanceState, clarityPressureActive: true }) : undefined, undefined, "live human clarity pressure suppresses self-maintenance advocacy inside the owning projector"); const curiosityObservation = populatedObservation.status === "ok" ? await readPersonaCuriosityEvidence({ identity: routedIdentity, state: populatedObservation.typedState, recentMessages: [{ id: "curiosity-room", authorId: "human", authorName: "Operator", content: "memory topology needs inspectable semantic anchors", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], sourceRepoName: "AetheriaLore", retrieval: { searchHistory: async () => [{ chunkId: "history-1", sourceId: "message-1", sourceKind: "discord_message", text: "semantic memory topology and inspectable anchors", score: 0.84, metadata: { channelId: "aquarium" } }], searchSources: async (_query, _limit, repoName) => repoName ? [{ chunkId: "source-home", sourceId: "doc-home", sourceKind: "source_document", text: "semantic memory topology preserves inspectable provenance anchors", score: 0.82, metadata: { repoName, path: "memory.md" } }] : [{ chunkId: "source-global", sourceId: "doc-global", sourceKind: "source_document", text: "inspectable semantic topology joins memory evidence anchors", score: 0.8, metadata: { repoName: "VoidBot", path: "map.md" } }], }, }) : undefined; assert.equal(curiosityObservation?.status, "ok", "the curiosity source plans queries and acquires bounded neutral evidence through an injected retrieval port"); const unavailableCuriosity = populatedObservation.status === "ok" ? await readPersonaCuriosityEvidence({ identity: routedIdentity, state: populatedObservation.typedState, recentMessages: [{ id: "curiosity-failure", authorId: "human", authorName: "Operator", content: "semantic memory topology needs inspectable anchors", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], retrieval: () => { throw new Error("vector adapter unavailable"); }, }) : undefined; assert.deepEqual(unavailableCuriosity, { status: "unavailable", reason: "vector adapter unavailable" }, "retrieval-adapter construction fails inside the curiosity source's explicit unavailable witness"); const curiosityProjection = populatedObservation.status === "ok" && curiosityObservation ? projectPersonaCuriosityContext({ identity: routedIdentity, state: populatedObservation.typedState, recentMessages: [{ id: "curiosity-room", authorId: "human", authorName: "Operator", content: "memory topology needs inspectable semantic anchors", timestamp: "2026-07-15T21:00:00.000Z", isBot: false, channelId: "aquarium" }], channelSnapshots: [], observation: curiosityObservation, backendDescription: "injected smoke retrieval", }) : undefined; assert.match(curiosityProjection ?? "", /Curiosity graph attractors:[\s\S]*injected smoke retrieval/, "the pure curiosity projector decodes and renders supplied evidence without acquiring it"); const pressureSections = populatedObservation.status === "ok" ? renderPersonaPressureSections({ identityName: "Nibu", state: populatedObservation.typedState, clarityPressureActive: true }) : []; const composedMemory = typedSections && composePersonaMemoryPacket({ identityName: "Nibu", typed: typedSections, socialGraph: "Explicit social graph fact.", roomTexture: "Explicit room texture fact.", pressureSections, }); assert.match(composedMemory ?? "", /Explicit social graph fact\.[\s\S]*Explicit room texture fact\./, "the projector owns final ordering of explicitly supplied external facts"); const statePacket = populatedObservation.status === "ok" ? projectPersonaStatePacket({ identity: routedIdentity, state: populatedObservation.typedState, curiosityGraphFacts: "A witness artifact remains literal source evidence.", observedAt: new Date("2026-07-15T21:00:00.000Z"), }) : ""; assert.match(statePacket, /A witness artifact remains literal source evidence\./, "state packet projection does not rewrite natural language through a downstream vocabulary cop"); let recalledProjectedMemory = ""; const coordinatedMemory = await coordinatePersonaMemoryTurn({ identity: routedIdentity, config: { storageRoot: stateSourceDirectory, repoFaceHeartbeats: { stateProjectorEnabled: false } } as never, registryIdentities: [routedIdentity], recentMessages: [], channelSnapshots: [], stateObservation: populatedObservation, observedAt: new Date("2026-07-15T21:00:00.000Z"), }, { projectCuriosityFacts: async () => "Coordinator supplied curiosity facts.", projectMemory: async ({ statePacket: packet }) => `Projected by coordinator:\n${packet}`, readRecall: async ({ projectedMemory }) => { recalledProjectedMemory = projectedMemory; return { status: "ok", hits: [{ score: 0.91, text: "A recalled architectural promise.", targetLabel: "memory coordinator", memoryKind: "durable" }] }; }, }); assert.match(coordinatedMemory.memorySurface, /Coordinator supplied curiosity facts\./, "the coordinator sequences curiosity evidence into the state packet before memory projection"); assert.equal(recalledProjectedMemory, coordinatedMemory.memorySurface, "semantic recall consumes the exact projected Mind surface returned to both live and inspection turns"); assert.match(coordinatedMemory.semanticMemoryRecallSurface, /memory coordinator\/durable score=0\.910: A recalled architectural promise\./, "the coordinator returns prompt-ready semantic recall beside the projected Mind surface"); assert.match(projectPersonaMemoryRecall({ status: "unavailable", reason: "vector store asleep" }), /Semantic Persona memory recall unavailable:[\s\S]*vector store asleep/, "recall failure projection remains explicit and cannot masquerade as an empty successful search"); assert.deepEqual(await readFile(statePath), beforeObservation, "Persona state observation cannot rewrite sleep, speaking pressure, or any other Mind field"); const portableProjection = projectGamecultPersonaState({ ...routedIdentity, id: "muninn", displayName: "Muninn", identityKind: "native_persona" }, { schemaVersion: "gamecult.persona_state.v0", publicDescription: "Muninn remembers provenance.", privateNotes: ["Name missing evidence."], values: [{ label: "Source time", summary: "Keep observation time distinct." }], thoughtMemory: { shortTerm: [{ summary: "The last frame was stale." }] } }); assert.match(portableProjection, /Persona state authority is unreported; treat it as a non-canonical projection[\s\S]*Source time: Keep observation time distinct/, "the portable projector renders bounded state without inventing canonical authority"); assert.doesNotMatch(portableProjection, /persona_state\.v0\.json|state-source/, "portable Persona projection cannot leak its private filesystem path"); } finally { await rm(stateSourceDirectory, { recursive: true, force: true }); } const channelPlan = buildPersonaChannelPlan(routedIdentity, "aquarium", "lore"); assert.equal(channelPlan.primaryChannelId, "lore", "fresh direct attention chooses its permitted source room"); assert.deepEqual(channelPlan.snapshotChannelIds, ["aquarium", "lore", "legacy"], "routing exposes one deduplicated evidence neighborhood"); assert.deepEqual(channelPlan.lowThresholdTopics, ["casual conversation"], "prompt renderers consume routing policy instead of recomputing thresholds"); const saturatedMessages = Array.from({ length: 8 }, (_, index) => ({ id: `topic-${index}`, authorId: "human", authorName: "Operator", content: `semantic memory topology anchors evidence ${index}`, timestamp: `2026-07-15T21:0${index}:00.000Z`, isBot: false, channelId: "lore" })); const conversationProjection = projectPersonaConversation({ identity: routedIdentity, recentMessages: saturatedMessages, channelSnapshots: [{ channelId: "aquarium", messages: [{ id: "nearby", authorId: "human-2", authorName: "Neighbor", content: "a nearby correction", timestamp: "2026-07-15T21:09:00.000Z", isBot: false, channelId: "aquarium" }] }], pendingMentions: [{ messageId: "mention", channelId: "aquarium", authorId: "human-2", authorName: "Neighbor", visiblePrompt: "Nibu, answer this", queuedAt: "2026-07-15T21:10:00.000Z" }], channelPlan, }); assert.equal(conversationProjection.focus?.reason, "pending_mention", "conversation projection gives the freshest direct call default context authority"); assert.match(conversationProjection.transcript, /Visible cross-channel chronology,[\s\S]*nearby correction/, "conversation projection renders supplied cross-channel evidence without acquiring it"); assert.match(conversationProjection.topicAttractor ?? "", /semantic \(8\)/, "conversation projection owns topic-attractor facts"); assert.match(renderPersonaRoomTopicSaturation(routedIdentity, saturatedMessages), /Current room topic saturation:/, "turn-facing saturation and memory-facing attractors share one conversation owner"); const promptParticipant = participant("nibu", 0); const inspectionParticipant = buildInspectionParticipant(routedIdentity, 90); assert.deepEqual(inspectionParticipant.groups.slice(-2), ["identity:nibu", "repo:aetherialore"], "inspection participant synthesis is a pure scheduler fixture with normalized identity groups"); assert.equal(inspectionParticipant.baseRecoveryMinutes, 90, "inspection synthesis carries explicit scheduler timing without reading live initiative state"); const jurisdictionDive = buildPersonaJurisdictionDiveDirective(routedIdentity, promptParticipant); assert.equal(jurisdictionDive.cadence, 3, "the turn-prompt projector owns Nibu's jurisdiction-dive cadence"); const projectedTurnPrompt = buildPersonaTurnPrompt({ identity: routedIdentity, channelId: "lore", channelPlan, channelSnapshots: [], recentMessages: saturatedMessages, memorySurface: "Nibu remembers the architecture.", conversationMemorySurface: conversationProjection.transcript, participant: promptParticipant, pendingMentions: [{ messageId: "mention", channelId: "aquarium", authorId: "human-2", authorName: "Neighbor", visiblePrompt: "Nibu, answer this", queuedAt: "2026-07-15T21:10:00.000Z" }], jurisdictionDive, githubActionsEnabled: false, globalAgentDoctrine: { status: "ok", path: "C:/Codex/AGENTS.md", doctrine: "Global doctrine.", attemptedPaths: ["C:/Codex/AGENTS.md"] }, }); assert.match(projectedTurnPrompt, /A direct call is tugging at you[\s\S]*Nibu, answer this/, "the pure turn-prompt projector owns situation and pending-call policy from supplied facts"); const assembledTurn = assemblePersonaTurn({ identity: routedIdentity, channelId: "lore", channelPlan, channelSnapshots: [{ channelId: "aquarium", messages: [{ id: "duplicate-image", authorId: "human-2", authorName: "Neighbor", content: "same image nearby", timestamp: "2026-07-15T21:09:00.000Z", isBot: false, channelId: "aquarium", attachments: [{ kind: "image", filename: "same.png", contentType: "image/png", localPath: "C:/cache/same.png" }], }] }], recentMessages: Array.from({ length: 9 }, (_, index) => ({ id: `image-${index}`, authorId: "human", authorName: "Operator", content: `image evidence ${index}`, timestamp: `2026-07-15T21:${String(index).padStart(2, "0")}:00.000Z`, isBot: false, channelId: "lore", attachments: [{ kind: "image" as const, filename: `${index}.png`, contentType: "image/png", localPath: index === 0 ? "C:/cache/same.png" : `C:/cache/${index}.png` }], })), participant: promptParticipant, pendingMentions: [], conversationMemorySurface: "Inspection supplied conversation authority.", githubActionsEnabled: false, globalAgentDoctrine: { status: "ok", path: "C:/Codex/AGENTS.md", doctrine: "Global doctrine.", attemptedPaths: ["C:/Codex/AGENTS.md"] }, }); assert.match(assembledTurn.prompt, /Inspection supplied conversation authority\./, "queued and inspection paths share one assembler with an explicit conversation override"); assert.doesNotMatch(assembledTurn.prompt, /Visible cross-channel chronology/, "the assembler does not let its derived transcript override an explicit inspection surface"); assert.equal(assembledTurn.imageAttachments.length, 8, "the assembler owns the bounded image evidence budget"); assert.equal(assembledTurn.imageAttachments.filter((attachment) => attachment.localPath === "C:/cache/same.png").length, 1, "the assembler deduplicates image evidence across room and nearby snapshots"); assert.equal(assembledTurn.conversation.focus?.channelId, "aquarium", "the assembler returns the same newest-evidence conversation projection supplied to the turn actuator"); const doctrineReads: string[] = []; const doctrineObservation = await readGlobalAgentDoctrine({ codexHome: "C:/Codex", userProfile: "C:/User", homeDirectory: "C:/User", readText: async (path) => { doctrineReads.push(path); if (doctrineReads.length === 1) throw new Error("missing"); return "Global doctrine from the first readable authority."; }, }); assert.equal(doctrineObservation.status, "ok", "the doctrine source reports the first readable authority as typed evidence"); assert.equal(doctrineReads.length, 2, "the doctrine source stops after the first readable candidate and deduplicates repeated home paths"); const unavailableDoctrine = await readGlobalAgentDoctrine({ codexHome: "C:/Absent", homeDirectory: "C:/AlsoAbsent", readText: async () => { throw new Error("not found"); }, }); assert.equal(unavailableDoctrine.status, "unavailable", "missing global doctrine remains an explicit source failure"); const unavailableDoctrineTurn = assemblePersonaTurn({ identity: routedIdentity, channelId: "lore", channelPlan, channelSnapshots: [], recentMessages: [], participant: promptParticipant, pendingMentions: [], githubActionsEnabled: false, globalAgentDoctrine: unavailableDoctrine, }); assert.match(unavailableDoctrineTurn.prompt, /Global Agent Instructions Unavailable[\s\S]*inspection failure/, "the assembler, not the source or shell, projects unavailable doctrine into the final prompt"); const physiologyObservedAt = new Date("2026-07-16T03:00:00.000Z"); const physiologyState = createEmptyVoidSelfState({ createdAt: "2026-07-15T00:00:00.000Z" }); physiologyState.scheduledRuntime.sleepCycle.nextNapStartsAt = "2026-07-16T02:59:00.000Z"; physiologyState.speechReceipts.recentReceipts.push({ sentAt: "2026-07-16T02:30:00.000Z" } as never); physiologyState.thoughtMemory.shortTerm.push({ id: "short-term", retiredAt: undefined } as never); const physiology = projectVoidPhysiology({ state: physiologyState, observedAt: physiologyObservedAt, moderationActive: false }); assert.equal(physiology.sleepCycle.isNapping, true, "Void physiology owns deterministic sleep transition at the due boundary"); assert.equal(physiology.speakingPressure.lastSpokeAt, "2026-07-16T02:30:00.000Z", "speaking pressure derives last speech from canonical typed receipts instead of a JSON status cache"); assert.deepEqual(physiology.operations.map((operation) => operation.operation), ["update_sleep_cycle", "update_speaking_pressure"], "physiology emits only its two typed state operations"); assert.deepEqual(physiology.memoryMaintenanceIntent, { napStartedAt: physiologyObservedAt.toISOString(), shortTermMemoryCount: 1, reason: "sleep_short_term_pressure" }, "physiology emits memory pressure as intent without running or deciding memory maintenance"); const moderationHeldPhysiology = projectVoidPhysiology({ state: physiologyState, observedAt: physiologyObservedAt, moderationActive: true }); assert.deepEqual(moderationHeldPhysiology.operations.map((operation) => operation.operation), ["update_speaking_pressure"], "active moderation freezes sleep ownership but does not prevent typed speech-pressure refresh"); assert.equal(moderationHeldPhysiology.memoryMaintenanceIntent, undefined, "moderation-held physiology cannot launch maintenance behind the moderation organ"); const appliedPhysiologyOperations: string[] = []; const physiologyOrgan = await runVoidPhysiologyOrgan({ statePath: "void-self-state.cc", observedAt: physiologyObservedAt }, { loadState: async () => physiologyState, applyOperation: async (_store, operation) => { appliedPhysiologyOperations.push(operation.operation); return physiologyState; }, }); assert.deepEqual(appliedPhysiologyOperations, ["update_sleep_cycle", "update_speaking_pressure"], "the daemon physiology organ applies only domain-projected typed operations through the state-service port"); assert.equal(physiologyOrgan.memoryMaintenanceIntent?.reason, "sleep_short_term_pressure", "the daemon organ exposes maintenance intent for a separate memory owner"); const memoryMaintenanceContext = projectVoidMemoryMaintenanceContext({ state: physiologyState, observedAt: physiologyObservedAt, forceDistillation: true }); assert.equal(memoryMaintenanceContext.forceDistillation, true, "sleep memory context exposes explicit maintenance pressure without deciding the memory operation"); assert.match(JSON.stringify(memoryMaintenanceContext.shortTermMemories), /ago|short-term/, "prompt-facing memory context projects chronology instead of dumping exact state timestamps"); assert.doesNotMatch(buildVoidMemoryMaintenancePrompt(memoryMaintenanceContext), /OPERATION_OUTPUT_PATH|apply-operation/, "portable maintenance prompts return operations directly and cannot preserve the filesystem tool-writing contract"); const parsedMaintenance = parseVoidMemoryMaintenanceOperations(JSON.stringify([{ operation: "prune_short_term_memories", sourceMemoryIds: ["short-term"], prunedAt: physiologyObservedAt.toISOString(), reason: "The fixture residue has no durable claim beyond proving the typed boundary.", }])); assert.equal(parsedMaintenance[0]?.operation, "prune_short_term_memories", "memory output parsing crosses the canonical operation schema before application"); assert.throws(() => parseVoidMemoryMaintenanceOperations(JSON.stringify([{ operation: "update_sleep_cycle", sleepCycle: physiology.sleepCycle }])), /not allowed/, "memory maintenance cannot annex physiology operations even when they are schema-valid"); const moderationContext = projectVoidModerationHeartbeatContext({ state: physiologyState, observedAt: physiologyObservedAt, observedCursor: { lastReviewedMessageId: "message-1", lastReviewedTimestamp: physiologyObservedAt.toISOString() }, recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z", content: "fixture" }] }, urgentModerationWitnesses: [], enforcementMode: "log_only", rules: "Fixture rules", }); assert.match(JSON.stringify(moderationContext.recentHistory), /5 minutes ago/, "moderation evidence chronology is projected without surrendering exact operation time ownership"); assert.doesNotMatch(buildVoidModerationHeartbeatPrompt(moderationContext), /OPERATION_OUTPUT_PATH|apply-operation/, "portable moderation prompts return operations directly and cannot preserve the filesystem tool-writing contract"); const parsedModeration = parseVoidModerationHeartbeatOperations({ outputText: JSON.stringify({ reviewedMessageIds: ["message-1"], urgentMessageIds: ["message-1"], operations: [{ operation: "upsert_open_case", case: { sourceMessageId: "message-1", status: "pending", summary: "Concrete fixture evidence.", authorId: "human-1", authorName: "Human", channelId: "room-1", whyItMatters: "The fixture crosses the safety boundary.", createdAt: physiologyObservedAt.toISOString(), lastTouchedAt: physiologyObservedAt.toISOString(), tags: ["infringement:safety_threat", "moderation:instaban"], }, }], }), state: physiologyState, observedMessageIds: ["message-1"] }); assert.equal(parsedModeration.operations[0]?.operation, "upsert_open_case", "moderation output parsing crosses the canonical operation schema and classification invariant"); assert.throws(() => parseVoidModerationHeartbeatOperations({ outputText: JSON.stringify({ reviewedMessageIds: ["message-2"], urgentMessageIds: [], operations: [{ operation: "upsert_open_case", case: { sourceMessageId: "message-2", status: "watching", summary: "Ambiguous fixture evidence.", createdAt: physiologyObservedAt.toISOString(), lastTouchedAt: physiologyObservedAt.toISOString(), tags: ["infringement:safety_threat", "infringement:weaponized_intimidation", "moderation:case_only"], }, }], }), state: physiologyState, observedMessageIds: ["message-2"] }), /exactly one infringement/, "moderation classification cannot multiply infringement authority for one message"); assert.throws(() => parseVoidModerationHeartbeatOperations({ outputText: JSON.stringify({ reviewedMessageIds: ["message-1"], urgentMessageIds: [], operations: [{ operation: "update_sleep_cycle", sleepCycle: physiology.sleepCycle }] }), state: physiologyState, observedMessageIds: ["message-1"] }), /not allowed/, "moderation cannot annex physiology even when the operation is schema-valid"); assert.throws(() => parseVoidModerationHeartbeatOperations({ outputText: JSON.stringify({ reviewedMessageIds: [], urgentMessageIds: [], operations: [] }), state: physiologyState, observedMessageIds: ["message-1"] }), /every observed message/, "moderation cannot advance over an omitted message"); assert.throws(() => parseVoidModerationHeartbeatOperations({ outputText: JSON.stringify({ reviewedMessageIds: ["message-1"], urgentMessageIds: ["message-1"], operations: [] }), state: physiologyState, observedMessageIds: ["message-1"] }), /unaccounted/, "urgent classification cannot advance without an existing or proposed case"); let moderationExporterArgs: string[] = []; const moderationEvidence = await readVoidModerationEvidence({ priorCursorTimestamp: "2026-07-16T02:00:00.000Z" }, { run: async (_file, args) => { moderationExporterArgs = args; return { stdout: JSON.stringify({ after: "2026-07-16T02:00:00.000Z", totalMatchingMessages: 1, messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z", channelId: "room-1", content: "fixture" }] }) }; }, }); assert.deepEqual(moderationExporterArgs.slice(-4), ["--after", "2026-07-16T02:00:00.000Z", "--limit", "120"], "moderation evidence acquisition owns cursor-bounded chronological export arguments"); assert.deepEqual(moderationEvidence.observedCursor, { lastReviewedMessageId: "message-1", lastReviewedTimestamp: "2026-07-16T02:55:00.000Z" }, "moderation evidence returns the exact parent-owned cursor witness separately from prompt chronology"); const moderationState = createEmptyVoidSelfState({ createdAt: "2026-07-15T00:00:00.000Z" }); const appliedModerationOperations: string[] = []; let moderationModelCalls = 0; const moderationOrganDependencies = { loadState: async () => moderationState, readEvidence: async () => moderationEvidence, projectText: async () => { moderationModelCalls += 1; return JSON.stringify({ reviewedMessageIds: ["message-1"], urgentMessageIds: ["message-1"], operations: [{ operation: "upsert_open_case", case: { sourceMessageId: "message-1", status: "pending", summary: "Concrete fixture evidence.", authorId: "human-1", createdAt: physiologyObservedAt.toISOString(), lastTouchedAt: physiologyObservedAt.toISOString(), tags: ["infringement:safety_threat", "moderation:instaban"] }, }] }); }, applyOperation: async (_store: unknown, operation: { operation: string; run?: { runner: string; ranAt: string; summary: string }; case?: unknown; lastReviewedMessageId?: string; lastReviewedTimestamp?: string }) => { appliedModerationOperations.push(operation.operation); if (operation.operation === "record_scheduled_run" && operation.run) moderationState.scheduledRuntime.lastRuns.push(operation.run); if (operation.operation === "upsert_open_case" && operation.case) moderationState.moderationCursor.openCases.push(operation.case as never); if (operation.operation === "record_reviewed_messages") { moderationState.moderationCursor.lastReviewedMessageId = operation.lastReviewedMessageId; moderationState.moderationCursor.lastReviewedTimestamp = operation.lastReviewedTimestamp; } return moderationState; }, }; const moderated = await runVoidModerationHeartbeat({ statePath: "void-self-state.cc", rules: "Fixture rules", enforcementMode: "log_only", observedAt: physiologyObservedAt }, moderationOrganDependencies as never); assert.equal(moderated.status, "ok", "portable moderation coordinator completes one reviewed evidence window"); assert.deepEqual(appliedModerationOperations, ["record_scheduled_run", "upsert_open_case", "record_reviewed_messages"], "moderation persists attempt and cases before the sole cursor commit"); const repeatedModeration = await runVoidModerationHeartbeat({ statePath: "void-self-state.cc", rules: "Fixture rules", enforcementMode: "log_only", observedAt: physiologyObservedAt }, moderationOrganDependencies as never); assert.deepEqual(repeatedModeration, { status: "skipped", reason: "already_attempted_this_window", runnerId: "void-moderation-heartbeat-attempt:message-1" }, "the same evidence window cannot become a recurring inference loop"); assert.equal(moderationModelCalls, 1, "moderation makes at most one model call per latest-message evidence window"); let bannedUserId: string | undefined; const moderationEnforcement = await runVoidModerationEnforcement({ statePath: "void-self-state.cc", mode: "policy", botToken: "token", guildId: "guild", observedAt: physiologyObservedAt }, { loadState: async () => moderationState, banMember: async ({ userId }) => { bannedUserId = userId; }, applyOperation: async (_store, operation) => { if (operation.operation === "close_open_case") { const existing = moderationState.moderationCursor.openCases.find((entry) => entry.sourceMessageId === operation.sourceMessageId); if (existing) Object.assign(existing, { status: operation.status, resolvedAt: operation.resolvedAt, resolutionSummary: operation.resolutionSummary }); } return moderationState; }, }); assert.equal(bannedUserId, "human-1", "policy enforcement lowers an instaban through the injected Discord actuator"); assert.equal(moderationEnforcement.actions[0]?.status, "instaban_applied", "successful sanction closes the typed case with an enforcement result"); const repeatedEnforcement = await runVoidModerationEnforcement({ statePath: "void-self-state.cc", mode: "policy", botToken: "token", guildId: "guild", observedAt: physiologyObservedAt }, { loadState: async () => moderationState }); assert.equal(repeatedEnforcement.evaluatedCaseCount, 0, "resolved moderation debt cannot be sanctioned twice on later cadences"); const ruminationState = createEmptyVoidSelfState({ createdAt: "2026-07-15T00:00:00.000Z" }); ruminationState.scheduledRuntime.sleepCycle.isNapping = false; ruminationState.agencyPressure.pressures.push({ pressureId: "pressure-1", kind: "world_advocacy_request", status: "active", target: { kind: "room", id: "aquarium" }, summary: "A direct room claim deserves an answer.", claim: "The reply belongs in its source room.", tension: "Silence would leave the claim unanswered.", actionImplication: "Queue a source-anchored reply.", intensity: 0.8, anchorRefs: [{ ref: "discord:message-1" }], evidenceRefs: [], sourceMemoryIds: [], createdAt: physiologyObservedAt.toISOString(), updatedAt: physiologyObservedAt.toISOString(), tags: [], } as never); const ruminationContext = projectVoidRuminationContext({ state: ruminationState, observedAt: physiologyObservedAt, pendingMentions: [{ messageId: "message-1", channelId: "room-1" }], recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z" }] }, doctrine: "Participate honestly.", rules: "Fixture rules.", voice: "Dry and direct." }); const laterRuminationContext = projectVoidRuminationContext({ state: ruminationState, observedAt: new Date("2026-07-16T04:00:00.000Z"), pendingMentions: [{ messageId: "message-1", channelId: "room-1" }], recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z" }] }, doctrine: "Participate honestly.", rules: "Fixture rules.", voice: "Dry and direct." }); assert.equal(ruminationContext.pressureFingerprint, laterRuminationContext.pressureFingerprint, "wall-clock projection cannot manufacture a fresh rumination intent"); assert.match(buildVoidRuminationPrompt(ruminationContext), /source_pressure:/, "rumination prompt projects explicit speech-pressure accountability"); assert.throws(() => parseVoidRuminationOperations({ outputText: "[]", state: ruminationState }), /requires a candidate/, "strong advocacy pressure cannot be silently acknowledged"); assert.throws(() => parseVoidRuminationOperations({ outputText: JSON.stringify([{ operation: "propose_memory_distillation", proposal: {} }]), state: ruminationState }), /invalid|not allowed/i, "awake rumination cannot annex sleep-owned durable-memory policy"); const ruminationCandidate = { operation: "queue_candidate_intervention", intervention: { interventionId: "candidate-1", kind: "world_advocacy", status: "queued", target: { kind: "room", id: "room-1" }, summary: "Answer the source-room claim.", draft: "This answer stays with the conversation that made it necessary.", deliveryTarget: { channelId: "room-1", replyToMessageId: "message-1" }, priority: 0.8, mustEventuallyShare: true, createdAt: physiologyObservedAt.toISOString(), updatedAt: physiologyObservedAt.toISOString(), tags: ["source_pressure:pressure-1"], }, }; const parsedRumination = parseVoidRuminationOperations({ outputText: JSON.stringify([ruminationCandidate]), state: ruminationState }); assert.equal(parsedRumination[0]?.operation, "queue_candidate_intervention", "person-shaped rumination crosses the typed operation boundary without delivering speech"); let ruminationModelCalls = 0; const ruminationDependencies = { loadState: async () => ruminationState, projectText: async () => { ruminationModelCalls += 1; return ruminationModelCalls === 1 ? JSON.stringify([ruminationCandidate]) : "[]"; }, applyOperation: async (_store: unknown, operation: { operation: string; run?: { runner: string; ranAt: string; summary: string }; intervention?: unknown }) => { if (operation.operation === "record_scheduled_run" && operation.run) ruminationState.scheduledRuntime.lastRuns.push(operation.run); if (operation.operation === "queue_candidate_intervention" && operation.intervention) { const index = ruminationState.candidateInterventions.interventions.findIndex((entry) => entry.interventionId === (operation.intervention as { interventionId: string }).interventionId); if (index >= 0) ruminationState.candidateInterventions.interventions[index] = operation.intervention as never; else ruminationState.candidateInterventions.interventions.push(operation.intervention as never); } return ruminationState; }, }; const ruminated = await runVoidRumination({ statePath: "void-self-state.cc", observedAt: physiologyObservedAt, pendingMentions: [{ messageId: "message-1", channelId: "room-1" }], recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z" }] }, doctrine: "Participate honestly.", rules: "Fixture rules.", voice: "Dry and direct." }, ruminationDependencies as never); assert.equal(ruminated.status, "ok", "the rumination coordinator applies one bounded pressure pass"); const repeatedRumination = await runVoidRumination({ statePath: "void-self-state.cc", observedAt: new Date("2026-07-16T04:00:00.000Z"), pendingMentions: [{ messageId: "message-1", channelId: "room-1" }], recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z" }] }, doctrine: "Participate honestly.", rules: "Fixture rules.", voice: "Dry and direct." }, ruminationDependencies as never); assert.equal(repeatedRumination.status, "ok", "a meaning-bearing state change may authorize the next rumination pressure fingerprint"); const settledRumination = await runVoidRumination({ statePath: "void-self-state.cc", observedAt: new Date("2026-07-16T05:00:00.000Z"), pendingMentions: [{ messageId: "message-1", channelId: "room-1" }], recentHistory: { messages: [{ id: "message-1", timestamp: "2026-07-16T02:55:00.000Z" }] }, doctrine: "Participate honestly.", rules: "Fixture rules.", voice: "Dry and direct." }, ruminationDependencies as never); assert.equal(settledRumination.status, "skipped", "an unchanged settled pressure cannot become a timer-driven inference loop"); assert.equal(ruminationModelCalls, 2, "rumination cadence follows changed pressure rather than elapsed clock"); const bifrostWrites: Array<{ key: string; value: Record }> = []; const bifrostPostInput = { idempotencyKey: "candidate-1:room-1:message-1", source: { kind: "voidbot.void.candidate", id: "candidate-1" }, actor: { id: "void", displayName: "Void" }, channelId: "room-1", replyToMessageId: "message-1", content: "This answer stays with the conversation that made it necessary.", }; const postThroughFakeBifrost = () => postDiscordViaBifrostCultMesh(bifrostPostInput, { commandUri: "cultmesh://asgard.yggdrasil.bifrost/commands/discord-post", storePath: "provider-store.cc", timeoutMs: 100, pumpEnabled: false, bifrostRoot: "Bifrost", }, { definitions: { command: "command-definition", receipt: "receipt-definition", generic: [] }, openNode: async () => ({ put: async (_definition, key, value) => { bifrostWrites.push({ key, value: value as Record }); }, get: (_definition, key) => ({ status: "completed", ok: true, commandId: key, messageId: "posted-message-1", transport: "webhook" }), }), now: () => physiologyObservedAt, }); const firstBifrostReceipt = await postThroughFakeBifrost(); const secondBifrostReceipt = await postThroughFakeBifrost(); assert.equal(firstBifrostReceipt.commandId, secondBifrostReceipt.commandId, "the shared Bifrost port derives a stable command id from its caller-owned idempotency key"); assert.deepEqual(firstBifrostReceipt, { commandId: firstBifrostReceipt.commandId, messageId: "posted-message-1", transport: "webhook", url: undefined }, "the shared port returns only a completed typed transport receipt"); assert.deepEqual((bifrostWrites[0].value.payload as Record), { identityId: "void", channelId: "room-1", content: bifrostPostInput.content, personaName: "Void", personaAvatarUrl: "", replyToMessageId: "message-1", }, "the shared port preserves actor, room, reply, and content in the typed command payload"); let deliveredCandidate: { channelId?: string; replyToMessageId?: string; content?: string } = {}; const candidateDelivery = await runVoidCandidateDelivery({ statePath: "void-self-state.cc", personaName: "Void", observedAt: physiologyObservedAt }, { loadState: async () => ruminationState, deliver: async (request) => { deliveredCandidate = request; return { messageId: "posted-message-1", transport: "bifrost", sentAt: physiologyObservedAt.toISOString() }; }, applyOperation: async (_store, operation) => { if (operation.operation === "mark_candidate_intervention_spoken") { const candidate = ruminationState.candidateInterventions.interventions.find((entry) => entry.interventionId === operation.interventionId); if (candidate) candidate.status = "spoken"; ruminationState.speechReceipts.recentReceipts.push(operation.receipt); } return ruminationState; }, }); assert.equal(candidateDelivery.status, "ok", "the delivery organ lowers one targeted queued candidate through its injected Bifrost port"); assert.deepEqual({ channelId: deliveredCandidate.channelId, replyToMessageId: deliveredCandidate.replyToMessageId }, { channelId: "room-1", replyToMessageId: "message-1" }, "candidate delivery preserves the source conversation target"); const repeatedCandidateDelivery = await runVoidCandidateDelivery({ statePath: "void-self-state.cc", personaName: "Void", observedAt: physiologyObservedAt }, { loadState: async () => ruminationState, deliver: async () => { throw new Error("resolved candidates cannot deliver again"); } }); assert.deepEqual(repeatedCandidateDelivery, { status: "skipped", reason: "no_targeted_candidate" }, "a spoken candidate cannot be delivered twice"); const maintainedState = structuredClone(physiologyState); let memoryModelCalls = 0; const memoryMaintenanceDependencies = { loadState: async () => maintainedState, projectText: async () => { memoryModelCalls += 1; return JSON.stringify([{ operation: "prune_short_term_memories", sourceMemoryIds: ["short-term"], prunedAt: physiologyObservedAt.toISOString(), reason: "The fixture residue has no durable meaning beyond proving nap-level ownership." }]); }, applyOperation: async (_store: unknown, operation: { operation: string; run?: { runner: string; ranAt: string; summary: string } }) => { if (operation.operation === "prune_short_term_memories") maintainedState.thoughtMemory.shortTerm = []; if (operation.operation === "record_scheduled_run" && operation.run) maintainedState.scheduledRuntime.lastRuns.push(operation.run); return maintainedState; }, }; const maintenanceIntent = physiology.memoryMaintenanceIntent; assert.ok(maintenanceIntent, "physiology fixture supplies memory-maintenance intent"); const maintained = await runVoidMemoryMaintenance({ statePath: "void-self-state.cc", intent: maintenanceIntent, observedAt: physiologyObservedAt }, memoryMaintenanceDependencies as never); assert.equal(maintained.status, "ok", "the memory organ validates, applies, verifies, and receipts one sleep pass"); const duplicateMaintenance = await runVoidMemoryMaintenance({ statePath: "void-self-state.cc", intent: maintenanceIntent, observedAt: physiologyObservedAt }, memoryMaintenanceDependencies as never); assert.deepEqual(duplicateMaintenance, { status: "skipped", reason: "already_completed_this_nap", runnerId: `void-memory-maintenance:${maintenanceIntent.napStartedAt}` }, "the typed scheduled-run receipt makes repeated intent idempotent for one nap"); assert.equal(memoryModelCalls, 1, "one nap can authorize at most one memory model pass"); let memoryRequestBody: Record | undefined; const memoryModelOutput = await projectVoidMemoryOperations({ prompt: "Maintain memory.", config: { openAiApi: { baseUrl: "https://model.invalid/v1", apiKey: "secret", model: "memory-model", timeoutMs: 1000, authHeader: "Authorization", maxCompletionTokens: 512 } } as never }, { fetch: async (_url, init) => { memoryRequestBody = JSON.parse(String(init?.body)); return new Response(JSON.stringify({ choices: [{ message: { content: "[]" } }] }), { status: 200 }); }, }); assert.equal(memoryModelOutput, "[]", "the portable memory actuator returns only model text to the operation parser"); assert.equal(memoryRequestBody?.model, "memory-model", "the memory actuator uses one explicit configured model and bounded completion budget"); assert.equal(memoryRequestBody?.tools, undefined, "memory maintenance cannot acquire filesystem or side-effect tools through the model actuator"); let ruminationRequestBody: Record | undefined; const ruminationModelOutput = await projectVoidRuminationOperations({ prompt: "Ruminate from typed pressure.", config: { openAiApi: { baseUrl: "https://model.invalid/v1", apiKey: "secret", model: "rumination-model", timeoutMs: 1000, authHeader: "Authorization", maxCompletionTokens: 512 } } as never }, { fetch: async (_url, init) => { ruminationRequestBody = JSON.parse(String(init?.body)); return new Response(JSON.stringify({ choices: [{ message: { content: "[]" } }] }), { status: 200 }); }, }); assert.equal(ruminationModelOutput, "[]", "the rumination actuator returns only model text to the typed parser"); assert.equal(ruminationRequestBody?.model, "rumination-model", "rumination uses one bounded configured text model"); assert.equal(ruminationRequestBody?.tools, undefined, "person-shaped rumination cannot acquire transport or filesystem tools"); let moderationRequestBody: Record | undefined; const moderationModelOutput = await projectVoidModerationOperations({ prompt: "Review moderation evidence.", config: { openAiApi: { baseUrl: "https://model.invalid/v1", apiKey: "secret", model: "moderation-model", timeoutMs: 1000, authHeader: "Authorization", maxCompletionTokens: 512 } } as never }, { fetch: async (_url, init) => { moderationRequestBody = JSON.parse(String(init?.body)); return new Response(JSON.stringify({ choices: [{ message: { content: '{"reviewedMessageIds":[],"urgentMessageIds":[],"operations":[]}' } }] }), { status: 200 }); }, }); assert.match(moderationModelOutput, /reviewedMessageIds/, "the portable moderation actuator returns only model text to the decision parser"); assert.equal(moderationRequestBody?.model, "moderation-model", "the moderation actuator uses one explicit configured model and bounded completion budget"); assert.equal(moderationRequestBody?.tools, undefined, "moderation inference cannot acquire filesystem or side-effect tools"); assert.equal(personaChannelSpeedMultiplier(routedIdentity), 3, "scheduler speed projection uses the routing organ's bounded channel policy"); assert.equal(newestPendingMentionChannel([ { identityId: "nibu", channelId: "older", queuedAt: "2026-07-15T20:00:00.000Z" }, { 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"); let pausedRunnerWrite = false; const pausedRunnerResult = await runPersonaSchedulerTick({ config: { repoFaceHeartbeats: { statePath: "paused.cc" } } as never, dryRun: true, now: new Date("2026-07-15T21:00:00.000Z") }, { readPause: async () => ({ paused: true, path: "pause.json", reason: "operator brake" }), readState: async () => newPersonaSchedulerState(), writeState: async () => { pausedRunnerWrite = true; }, }); assert.equal(pausedRunnerResult.reason, "agent_swarm_paused", "the in-process runner owns the pause-before-expensive-work short circuit"); assert.equal(pausedRunnerWrite, false, "a dry-run paused pulse cannot persist scheduler state"); } finally { await rm(controlDirectory, { recursive: true, force: true }); } let repoActivityArgs: string[] = []; const repoActivity = readRepoActivity({ identity: routedIdentity, storageRoot: "state-root", cwd: "workspace", runExporter: (_scriptPath, args) => { repoActivityArgs = args; return { status: 0, stdout: JSON.stringify({ digest: "- Fresh repo motion." }) }; }, }); assert.deepEqual(repoActivity, { status: "ok", sourceRepoName: "AetheriaLore", digest: "- Fresh repo motion." }, "repo activity crosses the source boundary as facts, not prompt prose"); assert.equal(projectRepoActivityObservation(repoActivity), "- Fresh repo motion.", "repo activity prompt lowering belongs to the pure projector"); assert.match(projectRepoActivityObservation({ status: "unavailable", sourceRepoName: "AetheriaLore", detail: "exporter asleep" }), /could not be read[\s\S]*exporter asleep[\s\S]*Do not claim current repo state/, "repo activity failure remains explicit without moving exporter mechanics into the projector"); assert.ok(repoActivityArgs.includes("--read-only"), "Persona turns cannot advance the repo activity cursor while observing body context"); assert.deepEqual(repoActivityArgs.slice(-4), ["--hours", "96", "--max-commits", "5"], "the source owns one bounded activity window"); const failedRepoActivity = readRepoActivity({ identity: routedIdentity, storageRoot: "state-root", runExporter: () => ({ status: 1, stderr: "reader failed" }), }); assert.deepEqual(failedRepoActivity, { status: "unavailable", sourceRepoName: "AetheriaLore", detail: "reader failed" }, "activity process failure is an explicit observation"); const malformedRepoActivity = readRepoActivity({ identity: routedIdentity, storageRoot: "state-root", runExporter: () => ({ status: 0, stdout: "not-json" }), }); assert.equal(malformedRepoActivity.status, "malformed", "malformed activity output cannot impersonate current repo truth"); const stateDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-scheduler-")); try { const statePath = join(stateDirectory, "scheduler.json"); const absent = await readPersonaSchedulerState(statePath); assert.equal(absent.initiativeClock, 0, "an absent scheduler store starts one explicit new state"); absent.participants.push(participant("persisted", 7)); await writePersonaSchedulerState(statePath, absent); const persisted = await readPersonaSchedulerState(statePath); assert.equal(persisted.participants[0].identityId, "persisted", "atomic scheduler persistence round-trips its state"); assert.ok((await readFile(resolveRepoFaceHeartbeatDebugProjectionPath(statePath), "utf8")).endsWith("\n"), "the JSON witness remains a complete inspectable projection"); await writeFile(resolveRepoFaceHeartbeatDebugProjectionPath(statePath), `${JSON.stringify(newPersonaSchedulerState())}\n`, "utf8"); assert.equal((await readPersonaSchedulerState(statePath)).participants[0].identityId, "persisted", "the derived JSON witness cannot override canonical CultCache state"); await writeFile(resolveRepoFaceHeartbeatStatePath(statePath), "{broken", "utf8"); await assert.rejects( readPersonaSchedulerState(statePath), /refusing to replace it with empty state/, "malformed persistent state must fail closed instead of erasing the scheduler mind", ); await rm(resolveRepoFaceHeartbeatStatePath(statePath), { force: true }); await writeFile(statePath, JSON.stringify({ schemaVersion: "legacy", baseIntervalMinutes: 90, participants: [{ identityId: "legacy", repoName: "Legacy", displayName: "Legacy" }], pendingMentions: [], history: [], }), "utf8"); const migrated = await readPersonaSchedulerState(statePath, Date.parse("2026-07-15T20:00:00.000Z")); assert.equal(migrated.baseRecoveryMinutes, 30, "legacy cadence migrates under the scheduler store owner"); assert.equal(migrated.participants[0].participantKind, "repo_face", "legacy participants receive current scheduling anatomy"); assert.ok(migrated.history.some((entry) => entry.type === "migrated"), "schema migration leaves a durable state witness"); assert.equal(migrated.history.at(-1)?.type, "storage_migrated", "JSON-to-CultCache migration names the storage authority change"); const mentionStatePath = join(stateDirectory, "mentions.json"); const mentionInput = { statePath: mentionStatePath, identityId: "nibu", channelId: "aquarium", messageId: "message-1", authorId: "human-1", content: "Nibu, look at this.", visiblePrompt: "Look at this.", queuedAt: "2026-07-15T20:03:00.000Z", }; const firstMention = await queueAgentHeartbeatMention(mentionInput); const duplicateMention = await queueAgentHeartbeatMention(mentionInput); assert.equal(firstMention.queued, true, "the bot creates one immutable attention command"); assert.equal(duplicateMention.queued, false, "the typed inbox deduplicates the same Discord source message"); assert.equal((await readRepoFaceMentionInbox(mentionStatePath)).length, 1, "mention ingress is a typed inbox, not a scheduler-state rewrite"); const withMention = await readPersonaSchedulerState(mentionStatePath); assert.equal(withMention.pendingMentions[0].identityId, "nibu", "the scheduler store alone ingests attention commands"); await writePersonaSchedulerState(mentionStatePath, withMention); assert.equal((await readRepoFaceMentionInbox(mentionStatePath)).length, 0, "inbox acknowledgement follows durable scheduler commit"); assert.equal((await readPersonaSchedulerState(mentionStatePath)).pendingMentions.length, 1, "acknowledgement cannot erase pending attention"); } finally { await rm(stateDirectory, { recursive: true, force: true }); } process.stdout.write("Persona initiative engine smoke passed.\n"); } void main().catch((error) => { console.error(error); process.exitCode = 1; });