Extract person-shaped rumination organ
This commit is contained in:
parent
11bacf1c61
commit
c0cf87520b
7 changed files with 273 additions and 37 deletions
21
apps/persona-scheduler/src/relative-chronology-projector.ts
Normal file
21
apps/persona-scheduler/src/relative-chronology-projector.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
export function projectRelativeChronology<T>(value: T, observedAt: Date): T {
|
||||
return projectValue(value, observedAt) as T;
|
||||
}
|
||||
|
||||
function projectValue(value: unknown, observedAt: Date, key = ""): unknown {
|
||||
if (Array.isArray(value)) return value.map((entry) => projectValue(entry, observedAt));
|
||||
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([entryKey, entry]) => [entryKey, projectValue(entry, observedAt, entryKey)]));
|
||||
if (typeof value === "string" && /(?:at|timestamp)$/i.test(key) && Number.isFinite(Date.parse(value))) return relativeTime(value, observedAt);
|
||||
return value;
|
||||
}
|
||||
|
||||
function relativeTime(value: string, observedAt: Date): string {
|
||||
const difference = observedAt.getTime() - Date.parse(value);
|
||||
const future = difference < 0;
|
||||
const minutes = Math.max(0, Math.round(Math.abs(difference) / 60_000));
|
||||
const phrase = (amount: number, unit: string) => future ? `in ${amount} ${unit}${amount === 1 ? "" : "s"}` : `${amount} ${unit}${amount === 1 ? "" : "s"} ago`;
|
||||
if (minutes < 60) return phrase(minutes, "minute");
|
||||
const hours = Math.round(minutes / 60);
|
||||
if (hours < 24) return phrase(hours, "hour");
|
||||
return phrase(Math.round(hours / 24), "day");
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { voidSelfStateOperationSchema, type VoidSelfStateOperation, type VoidSelfStateTypedProjection } from "@voidbot/core";
|
||||
import { projectRelativeChronology } from "./relative-chronology-projector.js";
|
||||
|
||||
const ALLOWED_OPERATIONS = new Set([
|
||||
"merge_incubation_support", "queue_candidate_intervention", "retire_candidate_intervention",
|
||||
|
|
@ -39,7 +40,7 @@ export function projectVoidMemoryMaintenanceContext(input: {
|
|||
const activeAgency = state.agencyPressure.pressures.filter((entry) => ["active", "cooling", "ready_to_act"].includes(entry.status));
|
||||
const activeCandidates = state.candidateInterventions.interventions.filter((entry) => entry.status === "queued" || entry.status === "deferred");
|
||||
const maintenancePressure = activeShortTerm.length + state.thoughtMemory.incubation.length + activeAgency.length + activeCandidates.length;
|
||||
const relative = <T>(value: T): T => projectRelativeChronology(value, input.observedAt) as T;
|
||||
const relative = <T>(value: T): T => projectRelativeChronology(value, input.observedAt);
|
||||
return {
|
||||
mode: sleeping ? "sleep_maintenance" : "awake_memory_maintenance",
|
||||
operationTimestamp: input.observedAt.toISOString(),
|
||||
|
|
@ -85,19 +86,3 @@ export function parseVoidMemoryMaintenanceOperations(outputText: string): VoidSe
|
|||
return operation;
|
||||
});
|
||||
}
|
||||
|
||||
function projectRelativeChronology(value: unknown, observedAt: Date, key = ""): unknown {
|
||||
if (Array.isArray(value)) return value.map((entry) => projectRelativeChronology(entry, observedAt));
|
||||
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([entryKey, entry]) => [entryKey, projectRelativeChronology(entry, observedAt, entryKey)]));
|
||||
if (typeof value === "string" && /(?:At|Timestamp)$/.test(key) && Number.isFinite(Date.parse(value))) return relativeTime(value, observedAt);
|
||||
return value;
|
||||
}
|
||||
|
||||
function relativeTime(value: string, observedAt: Date): string {
|
||||
const differenceMs = observedAt.getTime() - Date.parse(value);
|
||||
if (!Number.isFinite(differenceMs)) return "unknown time";
|
||||
const future = differenceMs < 0;
|
||||
const minutes = Math.max(0, Math.round(Math.abs(differenceMs) / 60_000));
|
||||
const amount = minutes < 60 ? `${minutes} minute${minutes === 1 ? "" : "s"}` : minutes < 1_440 ? `${Math.round(minutes / 60)} hour${Math.round(minutes / 60) === 1 ? "" : "s"}` : `${Math.round(minutes / 1_440)} day${Math.round(minutes / 1_440) === 1 ? "" : "s"}`;
|
||||
return future ? `in ${amount}` : `${amount} ago`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { voidSelfStateOperationSchema, type VoidSelfStateOperation, type VoidSelfStateTypedProjection } from "@voidbot/core";
|
||||
import { z } from "zod";
|
||||
import { projectRelativeChronology } from "./relative-chronology-projector.js";
|
||||
|
||||
const ALLOWED_MODERATION_TAGS = new Set(["moderation:instaban", "moderation:strike", "moderation:case_only"]);
|
||||
|
||||
|
|
@ -30,7 +31,7 @@ export function projectVoidModerationHeartbeatContext(input: {
|
|||
enforcementMode: string;
|
||||
rules: string;
|
||||
}): VoidModerationHeartbeatContext {
|
||||
const relative = <T>(value: T): T => projectRelativeChronology(value, input.observedAt) as T;
|
||||
const relative = <T>(value: T): T => projectRelativeChronology(value, input.observedAt);
|
||||
return {
|
||||
mode: "moderation_heartbeat",
|
||||
operationTimestamp: input.observedAt.toISOString(),
|
||||
|
|
@ -112,22 +113,3 @@ function validateOperation(entry: unknown, index: number, state: VoidSelfStateTy
|
|||
if (moderationTags.length !== 1) throw new Error("Moderation heartbeat open cases must include exactly one moderation classification tag.");
|
||||
return operation;
|
||||
}
|
||||
|
||||
function projectRelativeChronology(value: unknown, observedAt: Date, key = ""): unknown {
|
||||
if (Array.isArray(value)) return value.map((entry) => projectRelativeChronology(entry, observedAt));
|
||||
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([entryKey, entry]) => [entryKey, projectRelativeChronology(entry, observedAt, entryKey)]));
|
||||
if (typeof value === "string" && /(?:at|timestamp)$/i.test(key) && Number.isFinite(Date.parse(value))) return relativeTime(value, observedAt);
|
||||
return value;
|
||||
}
|
||||
|
||||
function relativeTime(value: string, observedAt: Date): string {
|
||||
const difference = observedAt.getTime() - Date.parse(value);
|
||||
const future = difference < 0;
|
||||
const minutes = Math.max(0, Math.round(Math.abs(difference) / 60_000));
|
||||
const phrase = (amount: number, unit: string) => future ? `in ${amount} ${unit}${amount === 1 ? "" : "s"}` : `${amount} ${unit}${amount === 1 ? "" : "s"} ago`;
|
||||
if (minutes < 60) return phrase(minutes, "minute");
|
||||
const hours = Math.round(minutes / 60);
|
||||
if (hours < 24) return phrase(hours, "hour");
|
||||
const days = Math.round(hours / 24);
|
||||
return phrase(days, "day");
|
||||
}
|
||||
|
|
|
|||
53
apps/persona-scheduler/src/void-rumination-organ.ts
Normal file
53
apps/persona-scheduler/src/void-rumination-organ.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { applyVoidSelfStateOperation, loadVoidSelfStateTypedDocuments } from "@voidbot/core";
|
||||
import { buildVoidRuminationPrompt, parseVoidRuminationOperations, projectVoidRuminationContext } from "./void-rumination-projector.js";
|
||||
|
||||
export type VoidRuminationResult =
|
||||
| { status: "skipped"; reason: "already_attempted_this_pressure" | "napping_without_pressure"; runnerId?: string }
|
||||
| { status: "ok"; runnerId: string; appliedOperationCount: number; queuedCandidateIds: string[] };
|
||||
|
||||
export async function runVoidRumination(input: {
|
||||
statePath: string;
|
||||
observedAt?: Date;
|
||||
pendingMentions?: unknown[];
|
||||
recentHistory?: unknown;
|
||||
repoActivity?: unknown;
|
||||
recentRoomLinks?: unknown;
|
||||
recentConversationTarget?: unknown;
|
||||
publicSpeechTarget?: unknown;
|
||||
doctrine: string;
|
||||
rules: string;
|
||||
voice: string;
|
||||
}, dependencies: {
|
||||
loadState?: typeof loadVoidSelfStateTypedDocuments;
|
||||
applyOperation?: typeof applyVoidSelfStateOperation;
|
||||
projectText: (prompt: string) => Promise<string>;
|
||||
}): Promise<VoidRuminationResult> {
|
||||
const loadState = dependencies.loadState ?? loadVoidSelfStateTypedDocuments;
|
||||
const applyOperation = dependencies.applyOperation ?? applyVoidSelfStateOperation;
|
||||
const state = await loadState({ canonicalPath: input.statePath });
|
||||
const observedAt = input.observedAt ?? new Date();
|
||||
const context = projectVoidRuminationContext({ ...input, state, observedAt });
|
||||
const hasExternalPressure = (input.pendingMentions?.length ?? 0) > 0 || hasObservedItems(input.recentHistory) || hasObservedItems(input.repoActivity) || hasObservedItems(input.recentRoomLinks);
|
||||
const hasInternalPressure = context.speechPressureObligationIds.length > 0 || state.moderationCursor.openCases.some((entry) => ["pending", "watching"].includes(entry.status)) || state.candidateInterventions.interventions.some((entry) => entry.status === "queued");
|
||||
if (state.scheduledRuntime.sleepCycle.isNapping && !hasExternalPressure && !hasInternalPressure) return { status: "skipped", reason: "napping_without_pressure" };
|
||||
const runnerId = `void-rumination-attempt:${context.pressureFingerprint}`;
|
||||
if (state.scheduledRuntime.lastRuns.some((run) => run.runner === runnerId)) return { status: "skipped", reason: "already_attempted_this_pressure", runnerId };
|
||||
await applyOperation({ canonicalPath: input.statePath }, {
|
||||
operation: "record_scheduled_run",
|
||||
run: { runner: runnerId, ranAt: observedAt.toISOString(), summary: "Started one bounded person-shaped rumination attempt for this pressure fingerprint." },
|
||||
});
|
||||
const operations = parseVoidRuminationOperations({ outputText: await dependencies.projectText(buildVoidRuminationPrompt(context)), state });
|
||||
for (const operation of operations) await applyOperation({ canonicalPath: input.statePath }, operation);
|
||||
return {
|
||||
status: "ok", runnerId, appliedOperationCount: operations.length,
|
||||
queuedCandidateIds: operations.flatMap((operation) => operation.operation === "queue_candidate_intervention" ? [operation.intervention.interventionId] : []),
|
||||
};
|
||||
}
|
||||
|
||||
function hasObservedItems(value: unknown): boolean {
|
||||
if (Array.isArray(value)) return value.length > 0;
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const record = value as Record<string, unknown>;
|
||||
for (const key of ["messages", "items", "commits", "repos"]) if (Array.isArray(record[key]) && record[key].length > 0) return true;
|
||||
return false;
|
||||
}
|
||||
127
apps/persona-scheduler/src/void-rumination-projector.ts
Normal file
127
apps/persona-scheduler/src/void-rumination-projector.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
import { createHash } from "node:crypto";
|
||||
import { voidSelfStateOperationSchema, type VoidSelfStateOperation, type VoidSelfStateTypedProjection } from "@voidbot/core";
|
||||
import { projectRelativeChronology } from "./relative-chronology-projector.js";
|
||||
|
||||
const ALLOWED_OPERATIONS = new Set([
|
||||
"upsert_open_case", "close_open_case", "record_short_term_memory", "merge_incubation_support",
|
||||
"upsert_agency_pressure", "retire_agency_pressure", "upsert_affect_need", "retire_affect_need",
|
||||
"upsert_social_bond", "retire_social_bond", "upsert_status_read", "retire_status_read",
|
||||
"update_mood_dimensions", "queue_candidate_intervention", "retire_candidate_intervention",
|
||||
]);
|
||||
|
||||
export interface VoidRuminationContext {
|
||||
mode: "person_shaped_rumination";
|
||||
operationTimestamp: string;
|
||||
pressureFingerprint: string;
|
||||
selfProfile: unknown;
|
||||
affect: unknown;
|
||||
memories: unknown[];
|
||||
shortTermMemories: unknown[];
|
||||
incubation: unknown[];
|
||||
agencyPressure: unknown[];
|
||||
speechPressureObligationIds: string[];
|
||||
candidateInterventions: unknown[];
|
||||
speechReceipts: unknown[];
|
||||
openCases: unknown[];
|
||||
scheduledRuntime: unknown;
|
||||
pendingMentions: unknown[];
|
||||
recentHistory: unknown;
|
||||
repoActivity: unknown;
|
||||
recentRoomLinks: unknown;
|
||||
recentConversationTarget: unknown;
|
||||
publicSpeechTarget: unknown;
|
||||
doctrine: string;
|
||||
rules: string;
|
||||
voice: string;
|
||||
}
|
||||
|
||||
export function projectVoidRuminationContext(input: {
|
||||
state: VoidSelfStateTypedProjection;
|
||||
observedAt: Date;
|
||||
pendingMentions?: unknown[];
|
||||
recentHistory?: unknown;
|
||||
repoActivity?: unknown;
|
||||
recentRoomLinks?: unknown;
|
||||
recentConversationTarget?: unknown;
|
||||
publicSpeechTarget?: unknown;
|
||||
doctrine: string;
|
||||
rules: string;
|
||||
voice: string;
|
||||
}): VoidRuminationContext {
|
||||
const obligationIds = speechPressureObligationIds(input.state);
|
||||
const fingerprint = fingerprintVoidRuminationPressure({
|
||||
state: input.state,
|
||||
pendingMentions: input.pendingMentions ?? [], recentHistory: input.recentHistory ?? null,
|
||||
repoActivity: input.repoActivity ?? null, recentRoomLinks: input.recentRoomLinks ?? null,
|
||||
});
|
||||
const relative = <T>(value: T): T => projectRelativeChronology(value, input.observedAt);
|
||||
return {
|
||||
mode: "person_shaped_rumination", operationTimestamp: input.observedAt.toISOString(), pressureFingerprint: fingerprint,
|
||||
selfProfile: relative(input.state.selfProfile), affect: relative(input.state.faceAffect),
|
||||
memories: relative(input.state.thoughtMemory.memories), shortTermMemories: relative(input.state.thoughtMemory.shortTerm),
|
||||
incubation: relative(input.state.thoughtMemory.incubation), agencyPressure: relative(input.state.agencyPressure.pressures),
|
||||
speechPressureObligationIds: obligationIds, candidateInterventions: relative(input.state.candidateInterventions.interventions),
|
||||
speechReceipts: relative(input.state.speechReceipts.recentReceipts), openCases: relative(input.state.moderationCursor.openCases),
|
||||
scheduledRuntime: relative(input.state.scheduledRuntime), pendingMentions: relative(input.pendingMentions ?? []),
|
||||
recentHistory: relative(input.recentHistory ?? null), repoActivity: relative(input.repoActivity ?? null),
|
||||
recentRoomLinks: relative(input.recentRoomLinks ?? null), recentConversationTarget: input.recentConversationTarget ?? null,
|
||||
publicSpeechTarget: input.publicSpeechTarget ?? null, doctrine: input.doctrine, rules: input.rules, voice: input.voice,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildVoidRuminationPrompt(context: VoidRuminationContext): string {
|
||||
return [
|
||||
"You are Void's private person-shaped rumination organ. Think as a participant with values and relationships, not as a maintenance report.",
|
||||
"Return only a JSON array of complete typed Void self-state operation objects. No markdown, commentary, tools, file writes, or direct transport calls.",
|
||||
`Allowed operations: ${[...ALLOWED_OPERATIONS].join(", ")}.`,
|
||||
"New memories must be short-term and meaning-preserving. Queue speech only as candidate interventions; the parent delivery organ owns transport and receipts.",
|
||||
"A reply candidate must retain the source channel and message in deliveryTarget. publicSpeechTarget is only for unanchored public artifacts or herald speech.",
|
||||
"For every speechPressureObligationId, queue a live candidate tagged source_pressure:<id>, retire the pressure, or upsert it with cooling/resolved status. Silent [] is invalid while obligations exist.",
|
||||
"Use operationTimestamp for fresh exact timestamps. Relative chronology is evidence only.",
|
||||
"If no anchored state change or sayable pressure exists, return [].",
|
||||
"Rumination context:", JSON.stringify(context, null, 2),
|
||||
].join("\n\n");
|
||||
}
|
||||
|
||||
export function parseVoidRuminationOperations(input: { outputText: string; state: VoidSelfStateTypedProjection }): VoidSelfStateOperation[] {
|
||||
const trimmed = input.outputText.trim();
|
||||
const candidate = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i)?.[1]?.trim() ?? trimmed;
|
||||
let raw: unknown;
|
||||
try { raw = JSON.parse(candidate); } catch (error) { throw new Error(`Void rumination output is not valid JSON: ${error instanceof Error ? error.message : String(error)}`); }
|
||||
if (!Array.isArray(raw)) throw new Error("Void rumination output must be a JSON array.");
|
||||
const operations = raw.map((entry, index) => {
|
||||
const operation = voidSelfStateOperationSchema.parse(entry);
|
||||
if (!ALLOWED_OPERATIONS.has(operation.operation)) throw new Error(`Void rumination operation ${index + 1} is not allowed: ${operation.operation}.`);
|
||||
return operation;
|
||||
});
|
||||
assertSpeechPressureResolved(input.state, operations);
|
||||
return operations;
|
||||
}
|
||||
|
||||
export function fingerprintVoidRuminationPressure(input: { state: VoidSelfStateTypedProjection; pendingMentions: unknown[]; recentHistory: unknown; repoActivity: unknown; recentRoomLinks: unknown }): string {
|
||||
const state = input.state;
|
||||
const pressure = {
|
||||
affect: state.faceAffect, shortTerm: state.thoughtMemory.shortTerm, incubation: state.thoughtMemory.incubation,
|
||||
agency: state.agencyPressure.pressures, candidates: state.candidateInterventions.interventions,
|
||||
openCases: state.moderationCursor.openCases, receipts: state.speechReceipts.recentReceipts.slice(-16),
|
||||
pendingMentions: input.pendingMentions, recentHistory: input.recentHistory, repoActivity: input.repoActivity, recentRoomLinks: input.recentRoomLinks,
|
||||
};
|
||||
return createHash("sha256").update(JSON.stringify(pressure)).digest("hex").slice(0, 24);
|
||||
}
|
||||
|
||||
function speechPressureObligationIds(state: VoidSelfStateTypedProjection): string[] {
|
||||
return state.agencyPressure.pressures
|
||||
.filter((pressure) => ["active", "ready_to_act"].includes(pressure.status) && pressure.intensity >= 0.55)
|
||||
.filter((pressure) => !state.candidateInterventions.interventions.some((candidate) => ["queued", "spoken"].includes(candidate.status) && candidate.tags.includes(`source_pressure:${pressure.pressureId}`)))
|
||||
.map((pressure) => pressure.pressureId);
|
||||
}
|
||||
|
||||
function assertSpeechPressureResolved(state: VoidSelfStateTypedProjection, operations: VoidSelfStateOperation[]): void {
|
||||
for (const pressureId of speechPressureObligationIds(state)) {
|
||||
const resolved = operations.some((operation) =>
|
||||
(operation.operation === "queue_candidate_intervention" && operation.intervention.status === "queued" && operation.intervention.tags.includes(`source_pressure:${pressureId}`))
|
||||
|| (operation.operation === "retire_agency_pressure" && operation.pressureId === pressureId)
|
||||
|| (operation.operation === "upsert_agency_pressure" && operation.pressure.pressureId === pressureId && ["cooling", "resolved", "retired"].includes(operation.pressure.status)));
|
||||
if (!resolved) throw new Error(`Active advocacy pressure '${pressureId}' requires a candidate intervention or explicit cooling/retirement.`);
|
||||
}
|
||||
}
|
||||
10
apps/persona-scheduler/src/void-rumination-text-actuator.ts
Normal file
10
apps/persona-scheduler/src/void-rumination-text-actuator.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type { loadConfig } from "@voidbot/config";
|
||||
import { requestBoundedTextCompletion } from "./bounded-text-completion-actuator.js";
|
||||
|
||||
export async function projectVoidRuminationOperations(input: { prompt: string; config: ReturnType<typeof loadConfig>; model?: string }, dependencies: { fetch?: typeof fetch; readText?: (path: string) => Promise<string> } = {}): Promise<string> {
|
||||
return requestBoundedTextCompletion({
|
||||
prompt: input.prompt, config: input.config, purpose: "Void person-shaped rumination",
|
||||
model: input.model ?? process.env.VOID_RUMINATION_MODEL,
|
||||
maxCompletionTokens: Number(process.env.VOID_RUMINATION_MAX_COMPLETION_TOKENS) || 3072,
|
||||
}, dependencies);
|
||||
}
|
||||
Loading…
Reference in a new issue