Move Void physiology into daemon
This commit is contained in:
parent
1d76da4003
commit
15a7c58e1b
9 changed files with 197 additions and 2 deletions
|
|
@ -1,13 +1,17 @@
|
|||
import "dotenv/config";
|
||||
|
||||
import { loadConfig } from "@voidbot/config";
|
||||
import { resolve } from "node:path";
|
||||
import { runPersonaSchedulerTick } from "./persona-scheduler-runner.js";
|
||||
import { runVoidPhysiologyOrgan } from "./void-physiology-organ.js";
|
||||
|
||||
const config = loadConfig();
|
||||
const intervalMs = Math.max(1, config.repoFaceHeartbeats.intervalMinutes) * 60_000;
|
||||
let stopping = false;
|
||||
let activeTick: Promise<void> | undefined;
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
let lastPhysiologyAt = 0;
|
||||
const physiologyIntervalMs = Math.max(5, Number(process.env.VOIDBOT_MOOD_INTERVAL_MINUTES) || 5) * 60_000;
|
||||
|
||||
function runTick(): Promise<void> {
|
||||
if (activeTick) {
|
||||
|
|
@ -16,7 +20,18 @@ function runTick(): Promise<void> {
|
|||
}
|
||||
const startedAt = new Date().toISOString();
|
||||
activeTick = runPersonaSchedulerTick({ config })
|
||||
.then((result) => { console.log(`Persona scheduler tick completed (started ${startedAt}). ${JSON.stringify(result)}`); })
|
||||
.then(async (result) => {
|
||||
console.log(`Persona scheduler tick completed (started ${startedAt}). ${JSON.stringify(result)}`);
|
||||
if (Date.now() - lastPhysiologyAt >= physiologyIntervalMs) {
|
||||
try {
|
||||
const physiology = await runVoidPhysiologyOrgan({ statePath: resolve(config.storageRoot, "private", "void-self-state.cc"), statusDirectory: resolve(config.storageRoot, "status") });
|
||||
lastPhysiologyAt = Date.now();
|
||||
console.log(`Void physiology pulse completed. ${JSON.stringify(physiology)}`);
|
||||
} catch (error) {
|
||||
console.error(`Void physiology pulse failed. ${error instanceof Error ? error.stack ?? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => { console.error(`Persona scheduler tick failed (started ${startedAt}). ${error instanceof Error ? error.stack ?? error.message : String(error)}`); })
|
||||
.finally(() => { activeTick = undefined; });
|
||||
return activeTick;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
import { stat } from "node:fs/promises";
|
||||
|
||||
export async function readVoidModerationActivity(input: {
|
||||
lockPath: string;
|
||||
observedAt?: Date;
|
||||
recentWithinMs?: number;
|
||||
}): Promise<{ active: boolean; lockPath: string; ageMs?: number }> {
|
||||
try {
|
||||
const info = await stat(input.lockPath);
|
||||
const ageMs = (input.observedAt ?? new Date()).getTime() - info.mtimeMs;
|
||||
return { active: ageMs >= 0 && ageMs < (input.recentWithinMs ?? 20 * 60_000), lockPath: input.lockPath, ageMs };
|
||||
} catch {
|
||||
return { active: false, lockPath: input.lockPath };
|
||||
}
|
||||
}
|
||||
107
apps/persona-scheduler/src/void-physiology-domain.ts
Normal file
107
apps/persona-scheduler/src/void-physiology-domain.ts
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import type { VoidSelfStateOperation, VoidSelfStateTypedProjection } from "@voidbot/core";
|
||||
|
||||
type SleepCycle = VoidSelfStateTypedProjection["scheduledRuntime"]["sleepCycle"];
|
||||
type SpeakingPressure = VoidSelfStateTypedProjection["scheduledRuntime"]["speakingPressure"];
|
||||
|
||||
export interface VoidMemoryMaintenanceIntent {
|
||||
napStartedAt: string;
|
||||
shortTermMemoryCount: number;
|
||||
reason: "sleep_short_term_pressure";
|
||||
}
|
||||
|
||||
export interface VoidPhysiologyProjection {
|
||||
sleepCycle: SleepCycle;
|
||||
speakingPressure: SpeakingPressure;
|
||||
operations: VoidSelfStateOperation[];
|
||||
memoryMaintenanceIntent?: VoidMemoryMaintenanceIntent;
|
||||
moderationActive: boolean;
|
||||
}
|
||||
|
||||
export function projectVoidPhysiology(input: {
|
||||
state: VoidSelfStateTypedProjection;
|
||||
observedAt: Date;
|
||||
moderationActive: boolean;
|
||||
}): VoidPhysiologyProjection {
|
||||
const sleepCycle = input.moderationActive
|
||||
? input.state.scheduledRuntime.sleepCycle
|
||||
: projectVoidSleepCycle(input.state.scheduledRuntime.sleepCycle, input.observedAt);
|
||||
const speakingPressure = projectVoidSpeakingPressure({ state: input.state, sleepCycle, observedAt: input.observedAt });
|
||||
const operations: VoidSelfStateOperation[] = [
|
||||
...(!input.moderationActive ? [{ operation: "update_sleep_cycle" as const, sleepCycle }] : []),
|
||||
{ operation: "update_speaking_pressure" as const, speakingPressure },
|
||||
];
|
||||
const activeShortTerm = input.state.thoughtMemory.shortTerm.filter((memory) => !memory.retiredAt).length;
|
||||
const memoryMaintenanceIntent = !input.moderationActive && sleepCycle.isNapping && sleepCycle.currentNapStartedAt && activeShortTerm > 0
|
||||
? { napStartedAt: sleepCycle.currentNapStartedAt, shortTermMemoryCount: activeShortTerm, reason: "sleep_short_term_pressure" as const }
|
||||
: undefined;
|
||||
return { sleepCycle, speakingPressure, operations, memoryMaintenanceIntent, moderationActive: input.moderationActive };
|
||||
}
|
||||
|
||||
export function projectVoidSleepCycle(previous: SleepCycle, now: Date): SleepCycle {
|
||||
const napIntervalMs = 4 * 60 * 60 * 1000;
|
||||
const napDurationMs = 60 * 60 * 1000;
|
||||
const nowMs = now.getTime();
|
||||
let isNapping = previous.isNapping;
|
||||
let currentNapStartedAt = previous.currentNapStartedAt;
|
||||
let currentNapEndsAt = previous.currentNapEndsAt;
|
||||
let nextNapStartsAt = previous.nextNapStartsAt;
|
||||
let nextNapMs = nextNapStartsAt ? Date.parse(nextNapStartsAt) : Number.NaN;
|
||||
const napEndMs = currentNapEndsAt ? Date.parse(currentNapEndsAt) : Number.NaN;
|
||||
if (!Number.isFinite(nextNapMs)) {
|
||||
nextNapMs = nowMs + napIntervalMs;
|
||||
nextNapStartsAt = new Date(nextNapMs).toISOString();
|
||||
}
|
||||
if (isNapping && Number.isFinite(napEndMs) && nowMs >= napEndMs) {
|
||||
isNapping = false;
|
||||
currentNapStartedAt = undefined;
|
||||
currentNapEndsAt = undefined;
|
||||
nextNapMs = nowMs + napIntervalMs;
|
||||
nextNapStartsAt = new Date(nextNapMs).toISOString();
|
||||
}
|
||||
if (!isNapping && nowMs >= nextNapMs) {
|
||||
isNapping = true;
|
||||
currentNapStartedAt = now.toISOString();
|
||||
currentNapEndsAt = new Date(nowMs + napDurationMs).toISOString();
|
||||
nextNapStartsAt = new Date(nowMs + napIntervalMs).toISOString();
|
||||
}
|
||||
return { isNapping, currentNapStartedAt, currentNapEndsAt, nextNapStartsAt, activeDreamThemes: previous.activeDreamThemes.filter((value) => value.trim()).slice(0, 4) };
|
||||
}
|
||||
|
||||
export function projectVoidSpeakingPressure(input: {
|
||||
state: VoidSelfStateTypedProjection;
|
||||
sleepCycle: SleepCycle;
|
||||
observedAt: Date;
|
||||
}): SpeakingPressure {
|
||||
const previous = input.state.scheduledRuntime.speakingPressure;
|
||||
const latestTypedSpeech = input.state.speechReceipts.recentReceipts
|
||||
.map((receipt) => receipt.sentAt)
|
||||
.filter((value) => Number.isFinite(Date.parse(value)))
|
||||
.sort((left, right) => Date.parse(right) - Date.parse(left))[0];
|
||||
const lastSpokeAt = latestTypedSpeech ?? previous.lastSpokeAt;
|
||||
const hoursSinceSpeech = lastSpokeAt ? hoursBetween(input.observedAt, new Date(lastSpokeAt)) : 24;
|
||||
const recentSpeechDamping = clamp(Math.exp(-hoursSinceSpeech / 3.5));
|
||||
const candidatePressure = input.state.candidateInterventions.interventions.filter((entry) => entry.status === "queued" || entry.status === "deferred").reduce((sum, entry) => sum + entry.priority * (entry.mustEventuallyShare ? 0.28 : 0.18), 0);
|
||||
const agencyPressure = input.state.agencyPressure.pressures.filter((entry) => entry.status === "active" || entry.status === "ready_to_act").reduce((sum, entry) => sum + entry.intensity * (entry.kind === "self_advocacy_request" || entry.kind === "world_advocacy_request" ? 0.3 : 0.14) * (entry.status === "ready_to_act" ? 1.25 : 1.08), 0);
|
||||
const thoughtPressure = input.state.thoughtMemory.incubation.filter((entry) => entry.status === "active" || entry.status === "ready_to_share").sort((left, right) => (right.desireToSpeak ?? 0) - (left.desireToSpeak ?? 0)).slice(0, 4).reduce((sum, entry) => sum + (entry.desireToSpeak ?? 0) * 0.12, 0);
|
||||
const needPressure = input.state.faceAffect.needs.filter((entry) => entry.status === "active" || entry.status === "neglected").reduce((sum, entry) => sum + entry.intensity * (["substrate", "agency", "status"].includes(entry.kind) ? 0.18 : 0.12) * (entry.status === "neglected" ? 1.28 : 1), 0);
|
||||
const statusPressure = input.state.faceAffect.statusReads.filter((entry) => !entry.retiredAt).reduce((sum, entry) => sum + entry.intensity * (["neglected", "bypassed", "blocked", "ignored", "threatened"].includes(entry.status) ? 0.14 : 0.08), 0);
|
||||
const moodPressure = input.state.faceAffect.moodDimensions.reduce((sum, entry) => sum + entry.value * (["anger", "annoyance", "irritation", "envy", "pride", "smugness", "playfulness", "anxiety", "commandForce"].includes(entry.name) ? 0.06 : 0.025), 0);
|
||||
const silencePressure = clamp(hoursSinceSpeech / 6);
|
||||
const sleepiness = input.sleepCycle.isNapping ? 1 : 0;
|
||||
const target = clamp(0.18 + silencePressure * 0.32 + candidatePressure + thoughtPressure + agencyPressure + needPressure + statusPressure + moodPressure - recentSpeechDamping * 0.38 - sleepiness * 0.26);
|
||||
return {
|
||||
needToSpeak: round3(clamp(previous.needToSpeak + (target - previous.needToSpeak) * 0.42)),
|
||||
confessionPressure: round3(clamp((previous.confessionPressure ?? 0.25) * 0.68 + silencePressure * 0.12 + sleepiness * 0.1 + moodPressure * 0.4)),
|
||||
noveltyPressure: round3(clamp((previous.noveltyPressure ?? 0.35) * 0.6 + thoughtPressure * 0.72 + candidatePressure * 0.3 + agencyPressure * 0.44 + needPressure * 0.38 + statusPressure * 0.3)),
|
||||
recentSpeechDamping: round3(recentSpeechDamping),
|
||||
lastSpokeAt,
|
||||
};
|
||||
}
|
||||
|
||||
function hoursBetween(later: Date, earlier: Date): number {
|
||||
const difference = later.getTime() - earlier.getTime();
|
||||
return Number.isFinite(difference) ? Math.max(0, difference / 3_600_000) : 24;
|
||||
}
|
||||
|
||||
function round3(value: number): number { return Math.round(value * 1000) / 1000; }
|
||||
function clamp(value: number): number { return Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : 0; }
|
||||
31
apps/persona-scheduler/src/void-physiology-organ.ts
Normal file
31
apps/persona-scheduler/src/void-physiology-organ.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { resolve } from "node:path";
|
||||
import { applyVoidSelfStateOperation, loadVoidSelfStateTypedDocuments } from "@voidbot/core";
|
||||
import { readVoidModerationActivity } from "./void-moderation-activity-source.js";
|
||||
import { projectVoidPhysiology, type VoidPhysiologyProjection } from "./void-physiology-domain.js";
|
||||
|
||||
export interface VoidPhysiologyOrganResult extends VoidPhysiologyProjection {
|
||||
ok: true;
|
||||
observedAt: string;
|
||||
statePath: string;
|
||||
moderationLockPath: string;
|
||||
}
|
||||
|
||||
export async function runVoidPhysiologyOrgan(input: {
|
||||
statePath: string;
|
||||
statusDirectory: string;
|
||||
observedAt?: Date;
|
||||
}, dependencies: {
|
||||
loadState?: typeof loadVoidSelfStateTypedDocuments;
|
||||
applyOperation?: typeof applyVoidSelfStateOperation;
|
||||
readModerationActivity?: typeof readVoidModerationActivity;
|
||||
} = {}): Promise<VoidPhysiologyOrganResult> {
|
||||
const observedAt = input.observedAt ?? new Date();
|
||||
const moderationLockPath = resolve(input.statusDirectory, "moderation-rumination.lock");
|
||||
const [state, moderation] = await Promise.all([
|
||||
(dependencies.loadState ?? loadVoidSelfStateTypedDocuments)({ canonicalPath: input.statePath }),
|
||||
(dependencies.readModerationActivity ?? readVoidModerationActivity)({ lockPath: moderationLockPath, observedAt }),
|
||||
]);
|
||||
const projection = projectVoidPhysiology({ state, observedAt, moderationActive: moderation.active });
|
||||
for (const operation of projection.operations) await (dependencies.applyOperation ?? applyVoidSelfStateOperation)({ canonicalPath: input.statePath }, operation);
|
||||
return { ok: true, observedAt: observedAt.toISOString(), statePath: input.statePath, moderationLockPath, ...projection };
|
||||
}
|
||||
Loading…
Reference in a new issue