2026-05-05 13:42:41 +01:00
import {
type GuildContext ,
type SourceMessage ,
type VoidSelfStateContext ,
} from "@voidbot/shared" ;
2026-05-04 20:45:26 +01:00
2026-05-16 12:01:43 +01:00
import { loadModerationState , type ModerationState } from "./moderation-state-store" ;
import {
projectModerationStateToTypedSelfState ,
type VoidSelfStateTypedProjection ,
} from "./void-self-state-projection" ;
2026-05-06 03:25:35 +01:00
2026-05-04 20:45:26 +01:00
type JsonValue = null | boolean | number | string | JsonValue [ ] | { [ key : string ] : JsonValue } ;
2026-05-05 01:23:56 +01:00
type JsonObject = { [ key : string ] : JsonValue } ;
2026-05-04 20:45:26 +01:00
2026-05-05 13:42:41 +01:00
export interface LoadVoidSelfStateOptions {
recentMessages? : SourceMessage [ ] ;
guildContext? : GuildContext ;
}
2026-05-04 20:45:26 +01:00
export async function loadVoidSelfState (
statePath : string ,
2026-05-05 13:42:41 +01:00
options : LoadVoidSelfStateOptions = { } ,
2026-05-04 20:45:26 +01:00
) : Promise < VoidSelfStateContext | undefined > {
2026-05-16 12:01:43 +01:00
let parsed : ModerationState ;
2026-05-04 20:45:26 +01:00
try {
2026-05-16 12:01:43 +01:00
parsed = await loadModerationState ( statePath ) ;
2026-05-04 20:45:26 +01:00
} catch ( error ) {
const code = ( error as NodeJS . ErrnoException ) . code ;
if ( code === "ENOENT" ) {
return undefined ;
}
throw error ;
}
2026-05-16 12:01:43 +01:00
const legacyProjection = parsed as unknown as JsonObject ;
const typedProjection = projectModerationStateToTypedSelfState ( parsed ) ;
const projection = buildVoidSelfStateProjection ( typedProjection ) ;
2026-05-04 20:45:26 +01:00
return {
sourcePath : statePath ,
loadedAt : new Date ( ) . toISOString ( ) ,
2026-05-16 12:01:43 +01:00
summary : renderVoidSelfStateSummary ( legacyProjection , options ) ,
2026-05-05 03:10:37 +01:00
projection ,
2026-05-04 20:45:26 +01:00
} ;
}
2026-05-05 13:42:41 +01:00
function renderVoidSelfStateSummary (
state : JsonObject ,
options : LoadVoidSelfStateOptions ,
) : string {
2026-05-04 20:45:26 +01:00
const identity = getObject ( state , "identity" ) ;
const goals = getArray ( state , "goals" ) ;
const memories = getObject ( state , "memories" ) ;
const runtime = getObject ( state , "moderation_runtime" ) ;
const canonicalState = getObject ( state , "canonical_state" ) ;
2026-05-05 01:23:56 +01:00
const thoughtLanes = getObject ( runtime , "thought_lanes" ) ;
const bridge = getObject ( runtime , "bridge" ) ;
2026-05-06 02:13:00 +01:00
const memoryResonance = getObject ( runtime , "memory_resonance" ) ;
const incubation = getObject ( runtime , "incubation" ) ;
2026-05-04 20:45:26 +01:00
const privateNotes = getStringArray ( identity , "private_notes" ) . slice ( 0 , 4 ) ;
const activeGoals = goals
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. filter ( ( goal ) = > readString ( goal , "status" ) === "active" )
. slice ( 0 , 4 )
. map ( ( goal ) = > readString ( goal , "label" ) )
. filter ( ( value ) : value is string = > Boolean ( value ) ) ;
const topValues = getArray ( canonicalState , "values" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "priority" ) ? ? 0 ) - ( readNumber ( left , "priority" ) ? ? 0 ) ,
)
. slice ( 0 , 4 )
. map ( ( value ) = > readString ( value , "label" ) )
. filter ( ( value ) : value is string = > Boolean ( value ) ) ;
const voiceSummary = summarizeCurrentActivations ( getObject ( canonicalState , "voice_style" ) , 5 ) ;
const behavioralSummary = summarizeCurrentActivations (
getObject ( canonicalState , "behavioral_dimensions" ) ,
5 ,
) ;
const semanticMemories = getArray ( memories , "semantic" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( - 6 )
. map ( ( memory ) = > {
const subject = readString ( memory , "subjectLabel" ) ? ? readString ( memory , "subjectId" ) ? ? "unknown" ;
const summary = readString ( memory , "summary" ) ? ? "(no summary)" ;
2026-05-13 20:09:25 +01:00
const kind = readString ( memory , "kind" ) ;
2026-05-16 02:08:17 +01:00
const suffix =
kind === "identity_seam"
? " [identity seam]"
: kind === "distilled_seam"
? " [distilled seam]"
: "" ;
return ` - ${ subject } ${ suffix } : ${ summary } ` ;
2026-05-04 20:45:26 +01:00
} ) ;
2026-05-05 01:23:56 +01:00
const analyticThreads = summarizeThoughtLane ( getObject ( thoughtLanes , "analytic" ) , 3 ) ;
const associativeThreads = summarizeThoughtLane ( getObject ( thoughtLanes , "associative" ) , 3 ) ;
const bridgeSyntheses = summarizeBridgeSyntheses ( bridge , 3 ) ;
const topicSaturation = summarizeTopicSaturation ( bridge , 3 ) ;
2026-05-08 16:52:22 +01:00
const refractoryTopics = summarizeRefractoryTopics ( bridge , 3 ) ;
const sourceCoverage = summarizeSourceCoverage ( bridge ) ;
2026-05-05 01:23:56 +01:00
const unresolvedTensions = summarizeUnresolvedTensions ( bridge , 3 ) ;
2026-05-06 02:13:00 +01:00
const resonanceClusters = summarizeResonanceClusters ( memoryResonance , 3 ) ;
const incubatingThoughts = summarizeIncubatingThoughts ( incubation , 3 ) ;
2026-05-07 23:40:00 +01:00
const openCases = summarizeOpenCases ( runtime , 3 ) ;
2026-05-16 05:58:18 +01:00
const discomforts = summarizeDiscomforts ( runtime , 3 ) ;
const activeTensions = summarizeActiveTensions ( runtime , 3 ) ;
const selfAdvocacy = summarizeAdvocacyRequests ( runtime , "self_advocacy_requests" , 2 ) ;
const worldAdvocacy = summarizeAdvocacyRequests ( runtime , "world_advocacy_requests" , 2 ) ;
2026-05-04 20:45:26 +01:00
const candidateInterventions = getArray ( runtime , "candidate_interventions" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( - 3 )
. map ( ( intervention ) = > {
const summary = readString ( intervention , "summary" ) ? ? "draft intervention" ;
const draft = readString ( intervention , "draft" ) ? ? "(no draft)" ;
return ` - ${ summary } : ${ draft } ` ;
} ) ;
2026-05-05 02:17:55 +01:00
const speakingBias = getObject ( runtime , "speaking_bias" ) ;
2026-05-04 20:45:26 +01:00
const lastRun = getObject ( runtime , "last_run" ) ;
const lastRunSummary = readString ( lastRun , "summary" ) ;
2026-05-05 03:10:37 +01:00
const sleepCycleSummary = renderSleepCycleSummary ( runtime , memories ) ;
2026-05-05 13:42:41 +01:00
const transientRoomLog = renderTransientRoomLog ( options ) ;
2026-05-04 20:45:26 +01:00
return [
2026-05-05 01:23:56 +01:00
` - Identity: ${ readString ( identity , "name" ) ? ? "Void" } ${ readString ( identity , "public_description" ) ? ` - ${ readString ( identity , "public_description" ) } ` : "" } ` ,
2026-05-04 20:45:26 +01:00
privateNotes . length > 0
? ` - Private notes: ${ privateNotes . join ( " | " ) } `
: "- Private notes: none recorded." ,
activeGoals . length > 0
? ` - Active goals: ${ activeGoals . join ( " | " ) } `
: "- Active goals: none recorded." ,
topValues . length > 0
? ` - Highest-priority values: ${ topValues . join ( " | " ) } `
: "- Highest-priority values: none recorded." ,
voiceSummary
? ` - Current voice activations: ${ voiceSummary } `
: "- Current voice activations: none highlighted." ,
behavioralSummary
? ` - Current behavioral activations: ${ behavioralSummary } `
: "- Current behavioral activations: none highlighted." ,
2026-05-05 03:10:37 +01:00
sleepCycleSummary ,
2026-05-04 20:45:26 +01:00
lastRunSummary ? ` - Last moderation run: ${ lastRunSummary } ` : "- Last moderation run: none recorded." ,
2026-05-07 23:40:00 +01:00
openCases . length > 0
? [ "- Outstanding room obligations:" , . . . openCases ] . join ( "\n" )
: "- Outstanding room obligations: none recorded." ,
2026-05-04 20:45:26 +01:00
semanticMemories . length > 0
2026-05-13 20:09:25 +01:00
? [ "- Distilled seam memory:" , . . . semanticMemories ] . join ( "\n" )
: "- Distilled seam memory: none recorded." ,
2026-05-05 01:23:56 +01:00
analyticThreads . length > 0
? [ "- Analytic active threads:" , . . . analyticThreads ] . join ( "\n" )
: "- Analytic active threads: none recorded." ,
associativeThreads . length > 0
? [ "- Associative active threads:" , . . . associativeThreads ] . join ( "\n" )
: "- Associative active threads: none recorded." ,
bridgeSyntheses . length > 0
? [ "- Bridge syntheses:" , . . . bridgeSyntheses ] . join ( "\n" )
: "- Bridge syntheses: none recorded." ,
2026-05-06 02:13:00 +01:00
resonanceClusters . length > 0
? [ "- Resonance clusters:" , . . . resonanceClusters ] . join ( "\n" )
: "- Resonance clusters: none recorded." ,
incubatingThoughts . length > 0
? [ "- Incubating thoughts:" , . . . incubatingThoughts ] . join ( "\n" )
: "- Incubating thoughts: none recorded." ,
2026-05-05 01:23:56 +01:00
topicSaturation . length > 0
? [ "- Topic saturation warnings:" , . . . topicSaturation ] . join ( "\n" )
: "- Topic saturation warnings: none recorded." ,
2026-05-08 16:52:22 +01:00
refractoryTopics . length > 0
? [ "- Refractory topics:" , . . . refractoryTopics ] . join ( "\n" )
: "- Refractory topics: none recorded." ,
sourceCoverage ? ` - Source coverage pressure: ${ sourceCoverage } ` : "- Source coverage pressure: none recorded." ,
2026-05-05 01:23:56 +01:00
unresolvedTensions . length > 0
? [ "- Unresolved tensions:" , . . . unresolvedTensions ] . join ( "\n" )
: "- Unresolved tensions: none recorded." ,
2026-05-16 05:58:18 +01:00
discomforts . length > 0
? [ "- Current discomforts:" , . . . discomforts ] . join ( "\n" )
: "- Current discomforts: none recorded." ,
activeTensions . length > 0
? [ "- Active value-backed tensions:" , . . . activeTensions ] . join ( "\n" )
: "- Active value-backed tensions: none recorded." ,
selfAdvocacy . length > 0
? [ "- Self-advocacy requests:" , . . . selfAdvocacy ] . join ( "\n" )
: "- Self-advocacy requests: none recorded." ,
worldAdvocacy . length > 0
? [ "- World-advocacy requests:" , . . . worldAdvocacy ] . join ( "\n" )
: "- World-advocacy requests: none recorded." ,
2026-05-05 02:17:55 +01:00
renderSpeakingBiasSummary ( speakingBias ) ,
2026-05-04 20:45:26 +01:00
candidateInterventions . length > 0
? [ "- Draft conversation/intervention seeds:" , . . . candidateInterventions ] . join ( "\n" )
: "- Draft conversation/intervention seeds: none recorded." ,
2026-05-05 13:42:41 +01:00
transientRoomLog ,
] . join ( "\n" ) ;
}
function renderTransientRoomLog ( options : LoadVoidSelfStateOptions ) : string {
const recentMessages = ( options . recentMessages ? ? [ ] ) . slice ( - 10 ) ;
const guildName = options . guildContext ? . guildName ? ? options . guildContext ? . guildId ? ? "(direct/unknown)" ;
const channelName =
options . guildContext ? . channelName ? ? options . guildContext ? . channelId ? ? "(unknown channel)" ;
if ( recentMessages . length === 0 ) {
return [
2026-05-06 01:46:04 +01:00
"- Current room snapshot:" ,
2026-05-05 13:42:41 +01:00
` - Current room: guild= ${ guildName } ; channel= ${ channelName } ` ,
2026-05-06 01:46:04 +01:00
"- No recent chat messages were captured in this snapshot." ,
2026-05-05 13:42:41 +01:00
] . join ( "\n" ) ;
}
const lines = recentMessages . map (
( message ) = >
` - [ ${ message . timestamp } ] ${ message . authorName } : ${ collapseWhitespace ( message . content || "(no text content)" ) } ` ,
) ;
return [
2026-05-06 01:46:04 +01:00
"- Current room snapshot:" ,
2026-05-05 13:42:41 +01:00
` - Current room: guild= ${ guildName } ; channel= ${ channelName } ` ,
. . . lines ,
2026-05-04 20:45:26 +01:00
] . join ( "\n" ) ;
}
2026-05-07 23:40:00 +01:00
function summarizeOpenCases ( runtime : JsonObject | undefined , limit : number ) : string [ ] {
const terminalStatuses = new Set ( [ "answered" , "resolved" , "closed" , "retired" , "dropped" ] ) ;
return getArray ( runtime , "open_cases" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. filter ( ( openCase ) = > {
const status = readString ( openCase , "status" ) ;
return ! status || ! terminalStatuses . has ( status ) ;
} )
. slice ( - limit )
. reverse ( )
. map ( ( openCase ) = > {
const summary =
readString ( openCase , "summary" ) ? ?
readString ( openCase , "question" ) ? ?
"unlabeled room obligation" ;
const authorName = readString ( openCase , "authorName" ) ;
const status = readString ( openCase , "status" ) ;
const whyItMatters = readString ( openCase , "whyItMatters" ) ;
const tags = [ status , authorName ? ` from ${ authorName } ` : undefined ] . filter (
( value ) : value is string = > Boolean ( value ) ,
) ;
return ` - ${ summary } ${ tags . length > 0 ? ` [ ${ tags . join ( "; " ) } ] ` : "" } ${ whyItMatters ? ` - ${ whyItMatters } ` : "" } ` ;
} ) ;
}
2026-05-16 05:58:18 +01:00
function summarizeDiscomforts ( runtime : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( runtime , "discomforts" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( entry ) = > {
const summary = readString ( entry , "summary" ) ? ? "unlabeled discomfort" ;
const targetKind = readString ( entry , "targetKind" ) ;
const domain = readString ( entry , "domain" ) ;
const intensity = readNumber ( entry , "intensity" ) ;
const persistence = readNumber ( entry , "persistence" ) ;
const metrics = [
domain ,
targetKind ,
intensity !== undefined ? ` intensity= ${ intensity . toFixed ( 2 ) } ` : undefined ,
persistence !== undefined ? ` persistence= ${ persistence } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ summary } ${ metrics . length > 0 ? ` [ ${ metrics . join ( "; " ) } ] ` : "" } ` ;
} ) ;
}
function summarizeActiveTensions ( runtime : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( runtime , "active_tensions" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( entry ) = > {
const topic = readString ( entry , "topic" ) ? ? "untitled tension" ;
const opinion = readString ( entry , "opinion" ) ? ? readString ( entry , "summary" ) ? ? "(no opinion)" ;
const domain = readString ( entry , "domain" ) ;
const intensity = readNumber ( entry , "intensity" ) ;
const persistence = readNumber ( entry , "persistence" ) ;
const metrics = [
domain ,
intensity !== undefined ? ` intensity= ${ intensity . toFixed ( 2 ) } ` : undefined ,
persistence !== undefined ? ` persistence= ${ persistence } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ topic } ${ metrics . length > 0 ? ` [ ${ metrics . join ( "; " ) } ] ` : "" } : ${ opinion } ` ;
} ) ;
}
function summarizeAdvocacyRequests (
runtime : JsonObject | undefined ,
key : string ,
limit : number ,
) : string [ ] {
return getArray ( runtime , key )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( entry ) = > {
const summary = readString ( entry , "summary" ) ? ? "advocacy request" ;
const request = readString ( entry , "request" ) ? ? "(no request)" ;
const status = readString ( entry , "status" ) ;
const intensity = readNumber ( entry , "intensity" ) ;
const persistence = readNumber ( entry , "persistence" ) ;
const metrics = [
status ,
intensity !== undefined ? ` intensity= ${ intensity . toFixed ( 2 ) } ` : undefined ,
persistence !== undefined ? ` persistence= ${ persistence } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ summary } ${ metrics . length > 0 ? ` [ ${ metrics . join ( "; " ) } ] ` : "" } : ${ request } ` ;
} ) ;
}
2026-05-05 01:23:56 +01:00
function summarizeThoughtLane ( lane : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( lane , "active_threads" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( 0 , limit )
. map ( ( thread ) = > {
const topic = readString ( thread , "topic" ) ? ? "untitled" ;
const claim = readString ( thread , "claim" ) ? ? "(no claim)" ;
const roomRelevance = readNumber ( thread , "roomRelevance" ) ;
const novelty = readNumber ( thread , "novelty" ) ;
const desireToSpeak = readNumber ( thread , "desireToSpeak" ) ;
const counterweight = readString ( thread , "counterweight" ) ;
const metrics = [
roomRelevance !== undefined ? ` room= ${ roomRelevance . toFixed ( 2 ) } ` : undefined ,
novelty !== undefined ? ` novelty= ${ novelty . toFixed ( 2 ) } ` : undefined ,
desireToSpeak !== undefined ? ` speak= ${ desireToSpeak . toFixed ( 2 ) } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
const counterweightText = counterweight ? ` Counterweight: ${ counterweight } ` : "" ;
return ` - ${ topic } : ${ claim } ${ metrics . length > 0 ? ` ( ${ metrics . join ( ", " ) } ) ` : "" } ${ counterweightText } ` ;
} ) ;
}
function summarizeBridgeSyntheses ( bridge : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( bridge , "recent_syntheses" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( - limit )
. reverse ( )
. map ( ( synthesis ) = > {
const summary = readString ( synthesis , "summary" ) ? ? "(no summary)" ;
const laneBalance = readString ( synthesis , "laneBalance" ) ;
const speakDecision = readString ( synthesis , "speakDecision" ) ;
const tags = [ laneBalance , speakDecision ] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ summary } ${ tags . length > 0 ? ` [ ${ tags . join ( "; " ) } ] ` : "" } ` ;
} ) ;
}
2026-05-06 02:13:00 +01:00
function summarizeResonanceClusters ( memoryResonance : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( memoryResonance , "clusters" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "resonance" ) ? ? 0 ) - ( readNumber ( left , "resonance" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( cluster ) = > {
const label = readString ( cluster , "label" ) ? ? "untitled" ;
const summary = readString ( cluster , "summary" ) ? ? "(no summary)" ;
const resonance = readNumber ( cluster , "resonance" ) ;
2026-05-16 02:53:18 +01:00
const focusKind = readString ( cluster , "focusKind" ) ;
const targetKind = readString ( cluster , "targetKind" ) ;
const concreteness = readNumber ( getObject ( cluster , "curiosityProfile" ) , "concreteness" ) ;
const metrics = [
focusKind ,
targetKind ,
concreteness !== undefined ? ` concrete= ${ concreteness . toFixed ( 2 ) } ` : undefined ,
resonance !== undefined ? ` resonance= ${ resonance . toFixed ( 2 ) } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ label } ${ metrics . length > 0 ? ` [ ${ metrics . join ( "; " ) } ] ` : "" } : ${ summary } ` ;
2026-05-06 02:13:00 +01:00
} ) ;
}
function summarizeIncubatingThoughts ( incubation : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( incubation , "active_thoughts" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "maturation" ) ? ? 0 ) - ( readNumber ( left , "maturation" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( thought ) = > {
const topic = readString ( thought , "topic" ) ? ? "untitled" ;
const summary = readString ( thought , "summary" ) ? ? "(no summary)" ;
const status = readString ( thought , "status" ) ;
2026-05-16 02:53:18 +01:00
const focusKind = readString ( thought , "focusKind" ) ;
const targetKind = readString ( thought , "targetKind" ) ;
2026-05-06 02:13:00 +01:00
const maturation = readNumber ( thought , "maturation" ) ;
const desireToSpeak = readNumber ( thought , "desireToSpeak" ) ;
2026-05-08 16:52:22 +01:00
const noveltyToSelf = readNumber ( thought , "noveltyToSelf" ) ;
const noveltyToRoom = readNumber ( thought , "noveltyToRoom" ) ;
const saturationScore = readNumber ( thought , "saturationScore" ) ;
2026-05-16 02:53:18 +01:00
const concreteness = readNumber ( thought , "concreteness" ) ;
const fertility = readNumber ( thought , "fertility" ) ;
2026-05-06 02:13:00 +01:00
const metrics = [
status ,
2026-05-16 02:53:18 +01:00
focusKind ,
targetKind ,
2026-05-06 02:13:00 +01:00
maturation !== undefined ? ` maturation= ${ maturation . toFixed ( 2 ) } ` : undefined ,
desireToSpeak !== undefined ? ` speak= ${ desireToSpeak . toFixed ( 2 ) } ` : undefined ,
2026-05-08 16:52:22 +01:00
noveltyToSelf !== undefined ? ` self= ${ noveltyToSelf . toFixed ( 2 ) } ` : undefined ,
noveltyToRoom !== undefined ? ` room= ${ noveltyToRoom . toFixed ( 2 ) } ` : undefined ,
saturationScore !== undefined ? ` sat= ${ saturationScore . toFixed ( 2 ) } ` : undefined ,
2026-05-16 02:53:18 +01:00
concreteness !== undefined ? ` concrete= ${ concreteness . toFixed ( 2 ) } ` : undefined ,
fertility !== undefined ? ` fertile= ${ fertility . toFixed ( 2 ) } ` : undefined ,
2026-05-06 02:13:00 +01:00
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ topic } ${ metrics . length > 0 ? ` [ ${ metrics . join ( "; " ) } ] ` : "" } : ${ summary } ` ;
} ) ;
}
2026-05-05 01:23:56 +01:00
function summarizeTopicSaturation ( bridge : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( bridge , "topic_saturation" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. sort (
( left , right ) = >
( readNumber ( right , "dominance" ) ? ? 0 ) - ( readNumber ( left , "dominance" ) ? ? 0 ) ,
)
. slice ( 0 , limit )
. map ( ( entry ) = > {
const topic = readString ( entry , "topic" ) ? ? "untitled" ;
2026-05-16 02:53:18 +01:00
const focusKind = readString ( entry , "focusKind" ) ;
const targetKind = readString ( entry , "targetKind" ) ;
2026-05-05 01:23:56 +01:00
const dominance = readNumber ( entry , "dominance" ) ;
const recentMentions = readNumber ( entry , "recentMentions" ) ;
const coolingAdvice = readString ( entry , "coolingAdvice" ) ;
const metrics = [
2026-05-16 02:53:18 +01:00
focusKind ,
targetKind ,
2026-05-05 01:23:56 +01:00
dominance !== undefined ? ` dominance= ${ dominance . toFixed ( 2 ) } ` : undefined ,
recentMentions !== undefined ? ` recent= ${ recentMentions } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ topic } ${ metrics . length > 0 ? ` ( ${ metrics . join ( ", " ) } ) ` : "" } ${ coolingAdvice ? ` - ${ coolingAdvice } ` : "" } ` ;
} ) ;
}
function summarizeUnresolvedTensions ( bridge : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( bridge , "unresolved_tensions" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( 0 , limit )
. map ( ( tension ) = > {
const topic = readString ( tension , "topic" ) ? ? "untitled" ;
const summary = readString ( tension , "summary" ) ? ? "(no summary)" ;
return ` - ${ topic } : ${ summary } ` ;
} ) ;
}
2026-05-08 16:52:22 +01:00
function summarizeRefractoryTopics ( bridge : JsonObject | undefined , limit : number ) : string [ ] {
return getArray ( bridge , "refractory_topics" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( 0 , limit )
. map ( ( entry ) = > {
const topic = readString ( entry , "topic" ) ? ? "untitled" ;
2026-05-16 02:53:18 +01:00
const focusKind = readString ( entry , "focusKind" ) ;
const targetKind = readString ( entry , "targetKind" ) ;
2026-05-08 16:52:22 +01:00
const penalty = readNumber ( entry , "penalty" ) ;
const coolsUntil = readString ( entry , "coolsUntil" ) ;
const reason = readString ( entry , "reason" ) ;
const metrics = [
2026-05-16 02:53:18 +01:00
focusKind ,
targetKind ,
2026-05-08 16:52:22 +01:00
penalty !== undefined ? ` penalty= ${ penalty . toFixed ( 2 ) } ` : undefined ,
coolsUntil ? ` until ${ coolsUntil } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
return ` - ${ topic } ${ metrics . length > 0 ? ` ( ${ metrics . join ( ", " ) } ) ` : "" } ${ reason ? ` - ${ reason } ` : "" } ` ;
} ) ;
}
function summarizeSourceCoverage ( bridge : JsonObject | undefined ) : string | undefined {
const sourceCoverage = getObject ( bridge , "source_coverage" ) ;
if ( ! sourceCoverage ) {
return undefined ;
}
const repos = getArray ( sourceCoverage , "repos" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( 0 , 3 )
. map ( ( entry ) = > ` ${ readString ( entry , "name" ) ? ? "repo" } : ${ readNumber ( entry , "count" ) ? ? 0 } ` ) ;
const years = getArray ( sourceCoverage , "archiveYears" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( 0 , 2 )
. map ( ( entry ) = > ` ${ readString ( entry , "year" ) ? ? "year" } : ${ readNumber ( entry , "count" ) ? ? 0 } ` ) ;
const parts = [ ] ;
if ( repos . length > 0 ) {
parts . push ( ` recent repos ${ repos . join ( ", " ) } ` ) ;
}
if ( years . length > 0 ) {
parts . push ( ` archive years ${ years . join ( ", " ) } ` ) ;
}
return parts . length > 0 ? parts . join ( " | " ) : undefined ;
}
2026-05-04 20:45:26 +01:00
function summarizeCurrentActivations (
category : JsonObject | undefined ,
limit : number ,
) : string | undefined {
if ( ! category ) {
return undefined ;
}
const entries = Object . entries ( category )
. map ( ( [ key , value ] ) = > {
if ( ! isObject ( value ) ) {
return undefined ;
}
const activation = readNumber ( value , "current_activation" ) ;
if ( activation === undefined ) {
return undefined ;
}
return {
key ,
activation ,
} ;
} )
. filter ( ( value ) : value is { key : string ; activation : number } = > Boolean ( value ) )
. sort ( ( left , right ) = > right . activation - left . activation )
. slice ( 0 , limit ) ;
if ( entries . length === 0 ) {
return undefined ;
}
return entries
. map ( ( { key , activation } ) = > ` ${ key } = ${ activation . toFixed ( 2 ) } ` )
. join ( ", " ) ;
}
2026-05-05 02:17:55 +01:00
function renderSpeakingBiasSummary ( speakingBias : JsonObject | undefined ) : string {
if ( ! speakingBias ) {
return "- Speaking bias: none recorded." ;
}
const needToSpeak = readNumber ( speakingBias , "needToSpeak" ) ;
const confessionPressure = readNumber ( speakingBias , "confessionPressure" ) ;
const noveltyPressure = readNumber ( speakingBias , "noveltyPressure" ) ;
const recentSpeechDamping = readNumber ( speakingBias , "recentSpeechDamping" ) ;
const lastHeraldAt = readString ( speakingBias , "lastHeraldAt" ) ;
const lastSpokeAt = readString ( speakingBias , "lastSpokeAt" ) ;
const parts = [
needToSpeak !== undefined ? ` needToSpeak= ${ needToSpeak . toFixed ( 2 ) } ` : undefined ,
confessionPressure !== undefined ? ` confession= ${ confessionPressure . toFixed ( 2 ) } ` : undefined ,
noveltyPressure !== undefined ? ` novelty= ${ noveltyPressure . toFixed ( 2 ) } ` : undefined ,
recentSpeechDamping !== undefined ? ` speechDamping= ${ recentSpeechDamping . toFixed ( 2 ) } ` : undefined ,
] . filter ( ( value ) : value is string = > Boolean ( value ) ) ;
const recency = [ lastHeraldAt ? ` lastHerald= ${ lastHeraldAt } ` : undefined , lastSpokeAt ? ` lastSpoke= ${ lastSpokeAt } ` : undefined ]
. filter ( ( value ) : value is string = > Boolean ( value ) )
. join ( " | " ) ;
if ( parts . length === 0 && recency . length === 0 ) {
return "- Speaking bias: none recorded." ;
}
return ` - Speaking bias: ${ parts . join ( ", " ) } ${ recency . length > 0 ? ` ( ${ recency } ) ` : "" } ` ;
}
2026-05-16 12:01:43 +01:00
function buildVoidSelfStateProjection (
typedState : VoidSelfStateTypedProjection ,
) : VoidSelfStateContext [ "projection" ] {
const sleepCycle = typedState . scheduledRuntime . sleepCycle ;
const dreamSummaries = typedState . thoughtMemory . memories
. filter ( ( memory ) = > memory . kind === "dream_residue" )
2026-05-05 03:10:37 +01:00
. slice ( - 3 )
2026-05-16 12:01:43 +01:00
. reverse ( )
. map ( ( memory ) = > memory . summary ) ;
const isNapping = sleepCycle . isNapping === true ;
const napStartedAt = sleepCycle . currentNapStartedAt ;
const napEndsAt = sleepCycle . currentNapEndsAt ;
const nextNapAt = sleepCycle . nextNapStartsAt ;
2026-05-05 03:10:37 +01:00
return {
mode : isNapping ? "napping" : "awake" ,
effortCeiling : isNapping ? "minimal" : "normal" ,
napStartedAt : isNapping ? napStartedAt : undefined ,
napEndsAt : isNapping ? napEndsAt : undefined ,
nextNapAt ,
2026-05-16 12:01:43 +01:00
activeDreamThemes : sleepCycle.activeDreamThemes ,
2026-05-05 03:10:37 +01:00
recentDreamSummaries : dreamSummaries ,
replyDirective : isNapping
? "You are in a scheduled nap. Reply in brief, low-effort, half-dreaming mutters instead of doing full attentive service-work."
: undefined ,
} ;
}
function renderSleepCycleSummary (
runtime : JsonObject | undefined ,
memories : JsonObject | undefined ,
) : string {
const sleepCycle = getObject ( runtime , "sleep_cycle" ) ;
if ( ! sleepCycle ) {
return "- Sleep cycle: none recorded." ;
}
const activeDreamThemes = getStringArray ( sleepCycle , "activeDreamThemes" ) . slice ( 0 , 4 ) ;
const dreams = getArray ( memories , "dreams" )
. map ( ( value ) = > ( isObject ( value ) ? value : undefined ) )
. filter ( ( value ) : value is JsonObject = > Boolean ( value ) )
. slice ( - 2 )
. reverse ( )
. map ( ( dream ) = > {
const theme = readString ( dream , "theme" ) ? ? "untitled" ;
const summary = readString ( dream , "summary" ) ? ? "(no summary)" ;
return ` ${ theme } : ${ summary } ` ;
} ) ;
const isNapping = sleepCycle ? . [ "isNapping" ] === true ;
const currentNapEndsAt = readString ( sleepCycle , "currentNapEndsAt" ) ;
const nextNapStartsAt = readString ( sleepCycle , "nextNapStartsAt" ) ;
const dreamCount = readNumber ( sleepCycle , "dreamCountInCurrentNap" ) ;
const replyMode = readString ( sleepCycle , "replyMode" ) ;
const headline = isNapping
? ` - Sleep cycle: napping ${ currentNapEndsAt ? ` until ${ currentNapEndsAt } ` : "" } ${ replyMode ? ` ( ${ replyMode } ) ` : "" } ${ dreamCount !== undefined ? ` ; dreams this nap= ${ dreamCount } ` : "" } . `
: ` - Sleep cycle: awake ${ nextNapStartsAt ? ` ; next nap ${ nextNapStartsAt } ` : "" } . ` ;
const themeLine =
activeDreamThemes . length > 0
? ` - Active dream themes: ${ activeDreamThemes . join ( " | " ) } `
: "- Active dream themes: none recorded." ;
const dreamLine =
dreams . length > 0
? ` - Recent dream residue: ${ dreams . join ( " | " ) } `
: "- Recent dream residue: none recorded." ;
return [ headline , themeLine , dreamLine ] . join ( "\n" ) ;
}
2026-05-04 20:45:26 +01:00
function getObject ( value : JsonObject | undefined , key : string ) : JsonObject | undefined {
const candidate = value ? . [ key ] ;
return isObject ( candidate ) ? candidate : undefined ;
}
function getArray ( value : JsonObject | undefined , key : string ) : JsonValue [ ] {
const candidate = value ? . [ key ] ;
return Array . isArray ( candidate ) ? candidate : [ ] ;
}
function getStringArray ( value : JsonObject | undefined , key : string ) : string [ ] {
return getArray ( value , key )
. map ( ( entry ) = > ( typeof entry === "string" ? entry : undefined ) )
. filter ( ( entry ) : entry is string = > Boolean ( entry ) ) ;
}
function readString ( value : JsonObject | undefined , key : string ) : string | undefined {
const candidate = value ? . [ key ] ;
return typeof candidate === "string" ? candidate : undefined ;
}
function readNumber ( value : JsonObject | undefined , key : string ) : number | undefined {
const candidate = value ? . [ key ] ;
return typeof candidate === "number" && Number . isFinite ( candidate ) ? candidate : undefined ;
}
function isObject ( value : JsonValue | undefined ) : value is JsonObject {
return typeof value === "object" && value !== null && ! Array . isArray ( value ) ;
}
2026-05-05 13:42:41 +01:00
function collapseWhitespace ( value : string ) : string {
return value . replace ( /\s+/g , " " ) . trim ( ) ;
}