2026-05-06 02:13:00 +01:00
import { createHash } from "node:crypto" ;
import { readFileSync } from "node:fs" ;
import { dirname , resolve } from "node:path" ;
import { fileURLToPath } from "node:url" ;
const repoRoot = resolve ( dirname ( fileURLToPath ( import . meta . url ) ) , ".." ) ;
const DEFAULT _VECTOR _DIMENSIONS = 96 ;
2026-05-16 08:03:59 +01:00
const MAX _EDGE _COUNT = 8 ;
const MAX _CLUSTER _COUNT = 2 ;
const MAX _INCUBATING _THOUGHTS = 2 ;
2026-05-07 22:39:50 +01:00
const MAX _RECENT _EPISODIC _RECORDS = 10 ;
const MAX _RECENT _QUIET _EPISODIC _RECORDS = 2 ;
2026-05-13 20:09:25 +01:00
const MAX _POST _NAP _EPISODIC _RECORDS = 4 ;
const MAX _POST _NAP _QUIET _EPISODIC _RECORDS = 1 ;
2026-05-16 08:03:59 +01:00
const MAX _POST _NAP _ARCHIVE _EXCURSIONS = 2 ;
const MAX _POST _NAP _REPO _SWEEPS = 2 ;
2026-05-13 20:09:25 +01:00
const MAX _POST _NAP _NOVELTY _CHECKS = 3 ;
const MAX _POST _NAP _MUSINGS = 6 ;
const MAX _POST _NAP _RECENT _MUSINGS = 2 ;
const MAX _POST _NAP _CANDIDATE _INTERVENTIONS = 4 ;
const MAX _POST _NAP _SEAM _PROMOTIONS = 3 ;
2026-05-16 08:03:59 +01:00
const MAX _POST _NAP _RESONANCE _EDGES = 4 ;
const MAX _ARCHIVE _EXCURSION _MEMORIES = 4 ;
const MAX _REPO _SWEEP _MEMORIES = 4 ;
2026-05-16 06:45:43 +01:00
const MAX _SEMANTIC _MEMORIES = 14 ;
const MAX _DREAM _MEMORIES = 5 ;
2026-05-16 08:03:59 +01:00
const MAX _RUNTIME _REPO _SWEEPS = 1 ;
const MAX _RUNTIME _REPO _ACTIVITY _MEMORIES = 1 ;
const MAX _RUNTIME _ARCHIVE _EXCURSIONS = 2 ;
2026-05-16 06:31:53 +01:00
const MAX _RUNTIME _RUMINATION _SEEDS = 4 ;
2026-05-16 08:03:59 +01:00
const MAX _RECENT _SYNTHESIS _COUNT = 1 ;
const MAX _SUPPORTING _REFS = 4 ;
const MAX _CLUSTER _MEMORY _IDS = 6 ;
const MAX _INCUBATION _SOURCE _IDS = 5 ;
const MAX _DREAM _SOURCE _IDS = 4 ;
2026-05-16 05:58:18 +01:00
const MAX _DISCOMFORT _COUNT = 6 ;
const MAX _ACTIVE _TENSION _COUNT = 6 ;
const MAX _ADVOCACY _REQUEST _COUNT = 4 ;
2026-05-16 02:08:17 +01:00
const MIN _SUPPORT _FOR _IDENTITY _CRYSTALLIZATION = 64 ;
const MIN _DEEP _DIVES _FOR _IDENTITY _CRYSTALLIZATION = 96 ;
2026-05-07 22:39:50 +01:00
const MAX _RECENT _ANALYTIC _THREADS = 6 ;
const MAX _RECENT _QUIET _ANALYTIC _THREADS = 1 ;
2026-05-06 02:13:00 +01:00
const EDGE _SIMILARITY _THRESHOLD = 0.56 ;
const CLUSTER _SIMILARITY _THRESHOLD = 0.64 ;
2026-05-08 16:52:22 +01:00
const TOPIC _MATCH _THRESHOLD = 0.42 ;
const REFRACTORY _MATCH _THRESHOLD = 0.48 ;
const MAX _TOPIC _SATURATION _COUNT = 6 ;
const MAX _REFRACTORY _TOPIC _COUNT = 6 ;
2026-05-06 02:13:00 +01:00
const stopwords = new Set ( [
"a" ,
"about" ,
"after" ,
"against" ,
"all" ,
"also" ,
"an" ,
"and" ,
"another" ,
"are" ,
"around" ,
"as" ,
"at" ,
"because" ,
"been" ,
"before" ,
"being" ,
"between" ,
"but" ,
"by" ,
"can" ,
"current" ,
"did" ,
2026-05-07 22:39:50 +01:00
"discord" ,
2026-05-06 02:13:00 +01:00
"do" ,
"does" ,
"doing" ,
"for" ,
"from" ,
"fresh" ,
"get" ,
"got" ,
"had" ,
"has" ,
"have" ,
"how" ,
"if" ,
"in" ,
"into" ,
"is" ,
"it" ,
"its" ,
"just" ,
"keep" ,
"keeps" ,
"kind" ,
"last" ,
"like" ,
"live" ,
"made" ,
"make" ,
"means" ,
2026-05-07 22:39:50 +01:00
"message" ,
"messages" ,
2026-05-06 02:13:00 +01:00
"more" ,
"most" ,
"new" ,
"no" ,
"not" ,
"now" ,
"of" ,
"on" ,
"one" ,
2026-05-07 22:39:50 +01:00
"owner" ,
2026-05-06 02:13:00 +01:00
"or" ,
"other" ,
"our" ,
"out" ,
"over" ,
"own" ,
2026-05-07 22:39:50 +01:00
"post" ,
2026-05-06 02:13:00 +01:00
"posted" ,
"quiet" ,
"recent" ,
"run" ,
"same" ,
2026-05-07 22:39:50 +01:00
"saved" ,
2026-05-06 02:13:00 +01:00
"seam" ,
"should" ,
"small" ,
"so" ,
"still" ,
"that" ,
"the" ,
"their" ,
"them" ,
"then" ,
"there" ,
"they" ,
"thing" ,
"this" ,
"thought" ,
2026-05-07 22:39:50 +01:00
"traffic" ,
2026-05-06 02:13:00 +01:00
"through" ,
"to" ,
"too" ,
"toward" ,
"under" ,
"up" ,
"use" ,
"using" ,
"very" ,
"was" ,
"we" ,
"were" ,
"what" ,
"when" ,
"where" ,
"which" ,
"while" ,
"with" ,
"worth" ,
"would" ,
"yet" ,
"you" ,
2026-05-07 22:39:50 +01:00
"smoke" ,
2026-05-06 02:13:00 +01:00
] ) ;
2026-05-16 02:53:18 +01:00
const labelNoiseTokens = new Set ( [
"active" ,
"archive" ,
"bridge" ,
"build" ,
"built" ,
"candidate" ,
"cluster" ,
"cooling" ,
"deepened" ,
"dream" ,
"draft" ,
2026-05-16 03:01:33 +01:00
"episodic" ,
2026-05-16 02:53:18 +01:00
"distill" ,
"excursion" ,
"excursions" ,
"distilled" ,
"holding" ,
"identity" ,
"incubating" ,
"instead" ,
"intervention" ,
"interventions" ,
"latent" ,
"lane" ,
"memory" ,
"memories" ,
"messagepack" ,
"moderation" ,
"musing" ,
"musings" ,
"novelty" ,
"organ" ,
"organs" ,
"pass" ,
"private" ,
"question" ,
"questions" ,
"receipts" ,
2026-05-16 03:01:33 +01:00
"actually" ,
"behaving" ,
"branch" ,
"compressed" ,
2026-05-16 02:53:18 +01:00
"recurring" ,
"repo" ,
"repos" ,
"resonance" ,
"semantic" ,
"single" ,
"state" ,
"status" ,
"summary" ,
"surface" ,
"surfaces" ,
"sweep" ,
"sweeps" ,
"talking" ,
"than" ,
"thread" ,
"threads" ,
"topic" ,
"topics" ,
"trying" ,
"wants" ,
"present" ,
"part" ,
"rather" ,
"itself" ,
"across" ,
] ) ;
2026-05-06 02:13:00 +01:00
export async function reconcileSemanticMemoryState ( {
state ,
now = new Date ( ) ,
repoRootPath = repoRoot ,
} = { } ) {
const runtime = ensureObject ( state . moderation _runtime ) ;
const memories = ensureObject ( state . memories ) ;
const sleepCycle = ensureObject ( runtime . sleep _cycle ) ;
2026-05-16 08:03:59 +01:00
normalizeHistoricalMemorySurfaces ( { state , memories , runtime , now } ) ;
2026-05-06 02:13:00 +01:00
const records = collectMemoryRecords ( { state , memories , runtime } ) ;
2026-05-08 16:52:22 +01:00
const previousBridge = ensureObject ( runtime . bridge ) ;
const sourceCoverage = buildSourceCoverage ( { runtime , now } ) ;
2026-05-16 02:53:18 +01:00
const identitySurface = buildIdentitySurface ( { state , memories } ) ;
2026-05-06 02:13:00 +01:00
const embedder = createEmbedder ( { repoRootPath } ) ;
let embeddedCount = 0 ;
for ( const record of records ) {
if ( await ensureSemanticVector ( { record , embedder , now } ) ) {
embeddedCount += 1 ;
}
}
const vectorizedRecords = records . filter (
( record ) => Array . isArray ( record . entry . semanticVector ? . values ) && record . entry . semanticVector . values . length > 0 ,
) ;
const edges = buildAssociationEdges ( vectorizedRecords ) ;
const clusters = buildClusters ( vectorizedRecords , edges ) ;
const previousIncubation = ensureObject ( runtime . incubation ) ;
const incubation = reconcileIncubation ( {
previous : previousIncubation ,
2026-05-08 16:52:22 +01:00
bridge : previousBridge ,
2026-05-06 02:13:00 +01:00
clusters ,
runtime ,
2026-05-08 16:52:22 +01:00
sourceCoverage ,
2026-05-16 02:53:18 +01:00
identitySurface ,
2026-05-08 16:52:22 +01:00
now ,
} ) ;
runtime . bridge = reconcileBridgeState ( {
previous : previousBridge ,
activeThoughts : incubation . active _thoughts ,
sourceCoverage ,
2026-05-06 02:13:00 +01:00
now ,
} ) ;
runtime . memory _resonance = {
updatedAt : now . toISOString ( ) ,
embeddingBackend : embedder . backend ,
embeddingModel : embedder . model ,
compactDimensions : embedder . dimensions ,
recent _edges : edges . slice ( 0 , MAX _EDGE _COUNT ) . map ( ( edge ) => ( {
leftMemoryId : edge . left . memoryId ,
leftLabel : edge . left . label ,
rightMemoryId : edge . right . memoryId ,
rightLabel : edge . right . label ,
similarity : round3 ( edge . similarity ) ,
summary : edge . summary ,
observedAt : now . toISOString ( ) ,
} ) ) ,
clusters ,
} ;
runtime . incubation = incubation ;
2026-05-16 02:08:17 +01:00
const crystallizationResult = crystallizeIdentityThoughts ( {
state ,
memories ,
runtime ,
incubation ,
now ,
} ) ;
2026-05-16 03:51:29 +01:00
const ripeShareResult = queueRipeThoughtInterventions ( {
runtime ,
incubation ,
now ,
} ) ;
2026-05-16 05:58:18 +01:00
const agencyResult = reconcileAgencyState ( {
state ,
runtime ,
incubation ,
now ,
} ) ;
2026-05-06 02:13:00 +01:00
const dreamResult = distillDreams ( {
memories ,
sleepCycle ,
incubation ,
now ,
} ) ;
2026-05-13 20:09:25 +01:00
const consolidationResult = consolidateSleepMemory ( {
memories ,
runtime ,
sleepCycle ,
incubation ,
memoryResonance : runtime . memory _resonance ,
now ,
} ) ;
2026-05-06 02:13:00 +01:00
state . moderation _runtime = runtime ;
state . memories = memories ;
return {
embeddedCount ,
edgeCount : edges . length ,
clusterCount : clusters . length ,
incubatingThoughtCount : incubation . active _thoughts . length ,
2026-05-08 16:52:22 +01:00
sourceCoverage ,
2026-05-06 02:13:00 +01:00
dreamCreated : dreamResult . created ,
dreamTheme : dreamResult . theme ,
2026-05-13 20:09:25 +01:00
sleepConsolidated : consolidationResult . consolidated ,
promotedSeamCount : consolidationResult . promotedSeamCount ,
2026-05-16 02:08:17 +01:00
identityCrystallizedCount : crystallizationResult . count ,
2026-05-16 03:51:29 +01:00
ripeInterventionCount : ripeShareResult . count ,
2026-05-16 05:58:18 +01:00
discomfortCount : agencyResult . discomfortCount ,
activeTensionCount : agencyResult . activeTensionCount ,
selfAdvocacyCount : agencyResult . selfAdvocacyCount ,
worldAdvocacyCount : agencyResult . worldAdvocacyCount ,
2026-05-06 02:13:00 +01:00
embeddingBackend : embedder . backend ,
embeddingModel : embedder . model ,
} ;
}
2026-05-16 08:03:59 +01:00
function normalizeHistoricalMemorySurfaces ( { state , memories , runtime , now } ) {
2026-05-16 03:01:33 +01:00
normalizeSemanticMemories ( { memories , now } ) ;
normalizeDreamMemories ( { memories , now } ) ;
dedupeSemanticMemories ( { memories } ) ;
dedupeDreamMemories ( { memories } ) ;
pruneHistoricalSeamMemories ( { memories } ) ;
2026-05-16 06:31:53 +01:00
pruneDreamMemories ( { memories } ) ;
trimHistoricalMemoryResidue ( { memories , runtime } ) ;
2026-05-16 03:01:33 +01:00
trimLegacyRuntimeResidue ( { runtime } ) ;
2026-05-16 08:03:59 +01:00
reconcileLegacyStateMirrors ( { state , memories , runtime , now } ) ;
2026-05-16 03:01:33 +01:00
}
function normalizeSemanticMemories ( { memories , now } ) {
const semanticMemories = ensureArray ( memories . semantic ) . filter ( isObject ) ;
2026-05-16 06:31:53 +01:00
const kept = [ ] ;
2026-05-16 03:01:33 +01:00
for ( const memory of semanticMemories ) {
2026-05-16 06:45:43 +01:00
if ( shouldDropSemanticMemory ( memory ) ) {
continue ;
}
2026-05-16 03:01:33 +01:00
if ( readString ( memory , "kind" ) === "identity_seam" ) {
2026-05-16 06:31:53 +01:00
kept . push ( memory ) ;
2026-05-16 03:01:33 +01:00
continue ;
}
2026-05-16 08:03:59 +01:00
if ( isStableSemanticMemory ( memory ) ) {
memory . lastTranslatedAt = readString ( memory , "lastTranslatedAt" ) ? ? now . toISOString ( ) ;
kept . push ( memory ) ;
continue ;
}
2026-05-16 06:31:53 +01:00
const preferredLabel = choosePreferredThoughtLabel ( memory ) ;
2026-05-16 03:01:33 +01:00
const normalized = translateLegacyThoughtLikeEntry ( {
2026-05-16 06:31:53 +01:00
label : preferredLabel ? ? readString ( memory , "kind" ) ? ? "semantic seam" ,
2026-05-16 03:01:33 +01:00
summary : readString ( memory , "summary" ) ? ? "" ,
fallbackTargetKind : "self" ,
fallbackFocusKind : "question" ,
evidenceRefs : ensureStringArray ( memory . evidenceRefs ) ,
repoNames : ensureStringArray ( memory . repoNames ) ,
} ) ;
if ( ! normalized ) {
continue ;
}
2026-05-16 06:31:53 +01:00
if ( shouldPurgeThoughtLikeMemory ( { entry : memory , normalized , preferredLabel } ) ) {
continue ;
}
2026-05-16 03:01:33 +01:00
memory . subjectLabel = normalized . label ;
memory . summary = normalized . summary ;
memory . focusKind = normalized . focusKind ;
memory . targetKind = normalized . targetKind ;
memory . focusPhrase = normalized . focusPhrase ;
memory . question = normalized . question ;
memory . claim = normalized . claim ;
memory . fascinationTarget = normalized . fascinationTarget ;
memory . worldFacing = normalized . worldFacing ;
memory . lastTranslatedAt = now . toISOString ( ) ;
2026-05-16 06:31:53 +01:00
kept . push ( memory ) ;
2026-05-16 03:01:33 +01:00
}
2026-05-16 06:31:53 +01:00
memories . semantic = kept . slice ( - MAX _SEMANTIC _MEMORIES ) ;
2026-05-16 03:01:33 +01:00
}
function normalizeDreamMemories ( { memories , now } ) {
const dreams = ensureArray ( memories . dreams ) . filter ( isObject ) ;
2026-05-16 06:31:53 +01:00
const kept = [ ] ;
2026-05-16 03:01:33 +01:00
for ( const dream of dreams ) {
2026-05-16 08:03:59 +01:00
if ( isStableDreamMemory ( dream ) ) {
dream . lastTranslatedAt = readString ( dream , "lastTranslatedAt" ) ? ? now . toISOString ( ) ;
kept . push ( dream ) ;
continue ;
}
2026-05-16 03:01:33 +01:00
const normalized = translateLegacyThoughtLikeEntry ( {
2026-05-16 06:31:53 +01:00
label : choosePreferredDreamTheme ( dream ) ? ? "dream seam" ,
2026-05-16 03:01:33 +01:00
summary : readString ( dream , "summary" ) ? ? "" ,
fallbackTargetKind : "self" ,
fallbackFocusKind : "question" ,
evidenceRefs : ensureStringArray ( dream . distilledFrom ) ,
repoNames : ensureStringArray ( dream . repoNames ) ,
} ) ;
if ( ! normalized ) {
continue ;
}
2026-05-16 06:31:53 +01:00
if (
shouldPurgeThoughtLikeMemory ( {
entry : dream ,
normalized ,
preferredLabel : choosePreferredDreamTheme ( dream ) ,
} )
) {
continue ;
}
2026-05-16 03:01:33 +01:00
dream . theme = normalized . label ;
2026-05-16 06:31:53 +01:00
dream . summary = buildDreamSummary ( normalized ) ;
2026-05-16 03:01:33 +01:00
dream . focusKind = normalized . focusKind ;
dream . targetKind = normalized . targetKind ;
dream . focusPhrase = normalized . focusPhrase ;
dream . question = normalized . question ;
dream . claim = normalized . claim ;
dream . worldFacing = normalized . worldFacing ;
dream . lastTranslatedAt = now . toISOString ( ) ;
2026-05-16 06:31:53 +01:00
kept . push ( dream ) ;
2026-05-16 03:01:33 +01:00
}
2026-05-16 06:31:53 +01:00
memories . dreams = kept . slice ( - MAX _DREAM _MEMORIES ) ;
2026-05-16 03:01:33 +01:00
}
2026-05-16 08:03:59 +01:00
function isStableSemanticMemory ( memory ) {
const label = readString ( memory , "subjectLabel" ) ? ? "" ;
const summary = readString ( memory , "summary" ) ? ? "" ;
const targetKind = readString ( memory , "targetKind" ) ? ? "" ;
const question = readString ( memory , "question" ) ? ? "" ;
const claim = readString ( memory , "claim" ) ? ? "" ;
if ( ! label || ! summary || hasThoughtSurfaceTemplateSmell ( summary ) ) {
return false ;
}
if ( looksKeywordSalad ( label ) || looksPersonLikeSingleton ( label ) ) {
return false ;
}
if ( targetKind === "repo" && extractRepoNameFromLabel ( label ) ) {
return true ;
}
if ( targetKind === "self" && /compression|continuity|receipt|dream/i . test ( ` ${ label } ${ summary } ` ) ) {
return true ;
}
return normalizeText ( question ) . length > 0 && normalizeText ( claim ) . length > 0 ;
}
function isStableDreamMemory ( dream ) {
const theme = readString ( dream , "theme" ) ? ? "" ;
const summary = readString ( dream , "summary" ) ? ? "" ;
if ( ! theme || ! summary || hasThoughtSurfaceTemplateSmell ( summary ) ) {
return false ;
}
if ( looksKeywordSalad ( theme ) || looksPersonLikeSingleton ( theme ) ) {
return false ;
}
return true ;
}
2026-05-16 03:01:33 +01:00
function translateLegacyThoughtLikeEntry ( {
label ,
summary ,
fallbackTargetKind ,
fallbackFocusKind ,
evidenceRefs ,
repoNames ,
} ) {
const rawLabel = normalizeText ( label ) ;
const rawSummary = normalizeText ( summary ) ;
if ( ! rawLabel && ! rawSummary ) {
return null ;
}
2026-05-16 06:31:53 +01:00
const preferredLabel = sanitizePreferredThoughtLabel ( rawLabel ) ;
2026-05-16 03:01:33 +01:00
const sourceKinds = inferSourceKindsFromThoughtText ( rawSummary , rawLabel , evidenceRefs ) ;
const synthesized = synthesizeThoughtSurface ( {
sourceKinds ,
repoNames ,
archiveYears : [ ] ,
channelIds : [ ] ,
keywords : topConceptKeywords ( ` ${ rawLabel } ${ rawSummary } ` , 6 ) ,
evidenceDiversity : inferLegacyEvidenceDiversity ( sourceKinds , evidenceRefs , repoNames ) ,
curiosityProfile : buildClusterCuriosityProfile ( {
sourceKinds ,
repoNames ,
archiveYears : [ ] ,
channelIds : [ ] ,
evidenceRefs ,
evidenceDiversity : inferLegacyEvidenceDiversity ( sourceKinds , evidenceRefs , repoNames ) ,
} ) ,
} ) ;
2026-05-16 06:31:53 +01:00
const preferredFocusPhrase = preferredLabel ? normalizeText ( preferredLabel ) . toLowerCase ( ) : synthesized . focusPhrase ;
const stabilized = {
... synthesized ,
label : preferredLabel ? ? synthesized . label ,
focusPhrase : preferredFocusPhrase ,
fascinationTarget : preferredFocusPhrase ,
} ;
2026-05-16 03:01:33 +01:00
if ( ! looksLegacyThoughtSurface ( rawLabel , rawSummary ) ) {
return {
2026-05-16 06:31:53 +01:00
... stabilized ,
label : preferredLabel ? ? rawLabel ? ? stabilized . label ,
2026-05-16 03:01:33 +01:00
summary : rewriteLegacySummaryToModernShape ( {
summary : rawSummary ,
2026-05-16 06:31:53 +01:00
synthesized : stabilized ,
2026-05-16 03:01:33 +01:00
fallbackFocusKind ,
fallbackTargetKind ,
} ) ,
} ;
}
return {
2026-05-16 06:31:53 +01:00
... stabilized ,
focusKind : stabilized . focusKind ? ? fallbackFocusKind ,
targetKind : stabilized . targetKind ? ? fallbackTargetKind ,
2026-05-16 03:01:33 +01:00
summary : rewriteLegacySummaryToModernShape ( {
summary : rawSummary ,
2026-05-16 06:31:53 +01:00
synthesized : stabilized ,
2026-05-16 03:01:33 +01:00
fallbackFocusKind ,
fallbackTargetKind ,
} ) ,
} ;
}
function looksLegacyThoughtSurface ( label , summary ) {
return (
/\// . test ( label ) ||
/^Recurring seam across /i . test ( summary ) ||
/^Dream-compressed a seam around /i . test ( summary ) ||
/^Self-facing seam around /i . test ( summary ) ||
/^Archive-facing seam around /i . test ( summary ) ||
/What part of this thought wants embodiment/i . test ( summary )
) ;
}
2026-05-16 06:31:53 +01:00
function choosePreferredThoughtLabel ( entry ) {
return (
sanitizePreferredThoughtLabel ( readString ( entry , "subjectLabel" ) ? ? "" ) ? ?
sanitizePreferredThoughtLabel ( readString ( entry , "subjectId" ) ? ? "" ) ? ?
null
) ;
}
function choosePreferredDreamTheme ( entry ) {
return sanitizePreferredThoughtLabel ( readString ( entry , "theme" ) ? ? "" ) ? ? null ;
}
function sanitizePreferredThoughtLabel ( label ) {
const normalized = normalizeText ( label )
. replace ( /^Why\s+/i , "" )
. replace ( /\s+keeps\s+resurfacing$/i , "" )
. replace ( /^What\s+/i , "" )
. replace ( /\s+is\s+trying\s+to\s+become$/i , "" )
. trim ( ) ;
if ( ! normalized ) {
return null ;
}
if ( looksKeywordSalad ( normalized ) ) {
return null ;
}
if ( /\b(still matters|current room need|change a real machine)\b/i . test ( normalized ) ) {
return null ;
}
2026-05-16 06:45:43 +01:00
if ( looksPersonLikeSingleton ( normalized ) ) {
return null ;
}
2026-05-16 06:31:53 +01:00
return normalized ;
}
2026-05-16 03:01:33 +01:00
function rewriteLegacySummaryToModernShape ( { summary , synthesized , fallbackFocusKind , fallbackTargetKind } ) {
const cleanSummary = normalizeText ( summary )
. replace ( /^Recurring seam across .*?\.\s*/i , "" )
. replace ( /^Dream-compressed a seam around .*?\.\s*/i , "" )
. replace ( /^Self-facing seam around .*?\.\s*/i , "" )
. replace ( /^Archive-facing seam around .*?\.\s*/i , "" )
. replace ( /^Fascination with .*?\.\s*/i , "" )
. replace ( /Built from .*? rather than a single lane talking to itself\.?/gi , "" )
. replace ( /\s+/g , " " )
. trim ( ) ;
const focusKind = synthesized . focusKind ? ? fallbackFocusKind ;
const targetKind = synthesized . targetKind ? ? fallbackTargetKind ;
2026-05-16 06:31:53 +01:00
if ( cleanSummary && ! hasThoughtSurfaceTemplateSmell ( cleanSummary ) ) {
return cleanSummary ;
2026-05-16 03:01:33 +01:00
}
2026-05-16 06:31:53 +01:00
return buildConciseThoughtSummary ( { focusKind , targetKind , synthesized } ) ;
2026-05-16 03:01:33 +01:00
}
function inferSourceKindsFromThoughtText ( summary , label , evidenceRefs ) {
const kinds = new Set ( ) ;
const text = ` ${ label } ${ summary } ` . toLowerCase ( ) ;
const refKinds = evidenceRefs . map ( ( ref ) => inferSourceKindFromRef ( ref ) ) . filter ( Boolean ) ;
for ( const kind of refKinds ) {
kinds . add ( kind ) ;
}
const acrossMatch = summary . match ( /Recurring seam across ([^.]+?) around/i ) ;
if ( acrossMatch ) {
for ( const part of acrossMatch [ 1 ] . split ( "," ) ) {
const trimmed = part . trim ( ) . toLowerCase ( ) ;
if ( trimmed ) {
kinds . add ( trimmed ) ;
}
}
}
if ( text . includes ( "dream" ) ) {
kinds . add ( "dream" ) ;
}
if ( text . includes ( "semantic" ) ) {
kinds . add ( "semantic" ) ;
}
if ( text . includes ( "archive" ) ) {
kinds . add ( "archive_excursion" ) ;
}
if ( text . includes ( "repo" ) ) {
kinds . add ( "repo_sweep" ) ;
}
return [ ... kinds ] . filter ( Boolean ) ;
}
function inferSourceKindFromRef ( ref ) {
const normalized = String ( ref ? ? "" ) ;
if ( normalized . startsWith ( "repo-sweep-" ) ) {
return "repo_sweep" ;
}
if ( normalized . startsWith ( "archive-excursion-" ) ) {
return "archive_excursion" ;
}
if ( normalized . startsWith ( "dream-" ) ) {
return "dream" ;
}
if ( normalized . startsWith ( "musing-" ) ) {
return "musing" ;
}
if ( normalized . startsWith ( "semantic-" ) || normalized . startsWith ( "semantic-seam-" ) ) {
return "semantic" ;
}
if ( normalized . startsWith ( "episodic-" ) ) {
return "episodic" ;
}
return undefined ;
}
function inferLegacyEvidenceDiversity ( sourceKinds , evidenceRefs , repoNames ) {
return clamp (
Math . min ( 1 , sourceKinds . length / 4 ) * 0.42 +
Math . min ( 1 , evidenceRefs . length / 10 ) * 0.28 +
Math . min ( 1 , repoNames . length / 2 ) * 0.2 ,
0 ,
1 ,
) ;
}
2026-05-16 06:31:53 +01:00
function buildConciseThoughtSummary ( { focusKind , targetKind , synthesized } ) {
const focus = capitalizePhrase ( synthesized . focusPhrase ) ;
const repoTarget = capitalizePhrase ( synthesized . fascinationTarget ? ? synthesized . focusPhrase ) ;
if ( focusKind === "fascination" && targetKind === "repo" ) {
return ` ${ repoTarget } keeps pulling as a concrete repo seam. ` ;
}
if ( targetKind === "archive" ) {
return ` ${ focus } keeps showing up in the archive strongly enough to deserve a cleaner read. ` ;
}
if ( targetKind === "self" ) {
return ` ${ focus } keeps surviving compression as a self-seam. ` ;
}
if ( focusKind === "claim" ) {
2026-05-16 06:45:43 +01:00
return ` ${ focus } has hardened into a concrete claim. ` ;
2026-05-16 06:31:53 +01:00
}
2026-05-16 06:45:43 +01:00
return ` ${ focus } remains unresolved enough to deserve a cleaner read. ` ;
2026-05-16 06:31:53 +01:00
}
function buildDreamSummary ( normalized ) {
const focus = capitalizePhrase ( normalized . focusPhrase ? ? normalized . label ? ? "this seam" ) ;
if ( ( normalized . targetKind ? ? "" ) === "archive" ) {
return ` Dream residue around ${ focus } from older archive pressure. ` ;
}
if ( ( normalized . targetKind ? ? "" ) === "self" ) {
return ` Dream residue around ${ focus } as a self-seam. ` ;
}
return ` Dream residue around ${ focus } . ` ;
}
2026-05-16 03:01:33 +01:00
function dedupeSemanticMemories ( { memories } ) {
const semanticMemories = ensureArray ( memories . semantic ) . filter ( isObject ) ;
const bySignature = new Map ( ) ;
for ( const memory of semanticMemories ) {
const kind = readString ( memory , "kind" ) ? ? "semantic" ;
const subjectLabel = readString ( memory , "subjectLabel" ) ? ? "" ;
const signature = ` ${ kind } | ${ normalizeText ( subjectLabel ) . toLowerCase ( ) } ` ;
const existing = bySignature . get ( signature ) ;
if ( ! existing ) {
bySignature . set ( signature , memory ) ;
continue ;
}
existing . evidenceRefs = mergeStringArrays ( existing . evidenceRefs , memory . evidenceRefs ) ;
existing . evidenceMessageIds = mergeStringArrays ( existing . evidenceMessageIds , memory . evidenceMessageIds ) ;
existing . lastObservedAt =
newestIsoTimestamp ( readString ( existing , "lastObservedAt" ) , readString ( memory , "lastObservedAt" ) ) ? ?
readString ( existing , "lastObservedAt" ) ? ?
readString ( memory , "lastObservedAt" ) ;
existing . confidence = Math . max ( readNumber ( existing , "confidence" ) ? ? 0 , readNumber ( memory , "confidence" ) ? ? 0 ) ;
if ( ( readString ( memory , "summary" ) ? ? "" ) . length > ( readString ( existing , "summary" ) ? ? "" ) . length ) {
existing . summary = memory . summary ;
}
}
2026-05-16 06:31:53 +01:00
memories . semantic = [ ... bySignature . values ( ) ] . slice ( - MAX _SEMANTIC _MEMORIES ) ;
2026-05-16 03:01:33 +01:00
}
function dedupeDreamMemories ( { memories } ) {
const dreams = ensureArray ( memories . dreams ) . filter ( isObject ) ;
const byTheme = new Map ( ) ;
for ( const dream of dreams ) {
const theme = normalizeText ( readString ( dream , "theme" ) ? ? "" ) . toLowerCase ( ) ;
if ( ! theme ) {
continue ;
}
const existing = byTheme . get ( theme ) ;
if ( ! existing ) {
byTheme . set ( theme , dream ) ;
continue ;
}
existing . distilledFrom = mergeStringArrays ( existing . distilledFrom , dream . distilledFrom ) ;
existing . salience = Math . max ( readNumber ( existing , "salience" ) ? ? 0 , readNumber ( dream , "salience" ) ? ? 0 ) ;
existing . timestamp =
newestIsoTimestamp ( readString ( existing , "timestamp" ) , readString ( dream , "timestamp" ) ) ? ?
readString ( existing , "timestamp" ) ? ?
readString ( dream , "timestamp" ) ;
}
2026-05-16 06:31:53 +01:00
memories . dreams = [ ... byTheme . values ( ) ] . slice ( - MAX _DREAM _MEMORIES ) ;
2026-05-16 03:01:33 +01:00
}
function pruneHistoricalSeamMemories ( { memories } ) {
const semanticMemories = ensureArray ( memories . semantic ) . filter ( isObject ) ;
const identitySeams = [ ] ;
const nonSeams = [ ] ;
const groupedDistilled = new Map ( ) ;
for ( const memory of semanticMemories ) {
2026-05-16 06:45:43 +01:00
if ( shouldDropSemanticMemory ( memory ) ) {
continue ;
}
2026-05-16 03:01:33 +01:00
const kind = readString ( memory , "kind" ) ;
if ( kind === "identity_seam" ) {
identitySeams . push ( memory ) ;
continue ;
}
if ( kind !== "distilled_seam" ) {
2026-05-16 06:31:53 +01:00
const subjectLabel = readString ( memory , "subjectLabel" ) ? ? "" ;
const subjectId = readString ( memory , "subjectId" ) ? ? "" ;
if (
( ! subjectLabel || subjectLabel . startsWith ( "seam:" ) ) &&
( subjectId . startsWith ( "seam:" ) || subjectId . startsWith ( "pattern-" ) )
) {
continue ;
}
if ( hasThoughtSurfaceTemplateSmell ( readString ( memory , "summary" ) ? ? "" ) ) {
continue ;
}
2026-05-16 03:01:33 +01:00
nonSeams . push ( memory ) ;
continue ;
}
const targetKind = readString ( memory , "targetKind" ) ? ? "system" ;
if ( ! groupedDistilled . has ( targetKind ) ) {
groupedDistilled . set ( targetKind , [ ] ) ;
}
groupedDistilled . get ( targetKind ) . push ( memory ) ;
}
const keptDistilled = [ ] ;
const limits = new Map ( [
[ "repo" , 8 ] ,
[ "archive" , 6 ] ,
[ "room" , 4 ] ,
[ "self" , 3 ] ,
[ "system" , 4 ] ,
] ) ;
for ( const [ targetKind , entries ] of groupedDistilled . entries ( ) ) {
const sorted = entries
. filter ( ( entry ) => ! isLowCoherenceDistilledSeam ( entry ) )
2026-05-16 06:31:53 +01:00
. filter ( ( entry ) => ! hasThoughtSurfaceTemplateSmell ( readString ( entry , "summary" ) ? ? "" ) )
2026-05-16 03:01:33 +01:00
. sort ( compareMemoryFreshness ) ;
keptDistilled . push ( ... sorted . slice ( 0 , limits . get ( targetKind ) ? ? 4 ) ) ;
}
memories . semantic = [ ... nonSeams , ... keptDistilled , ... identitySeams ]
. sort ( compareMemoryFreshness )
2026-05-16 06:31:53 +01:00
. slice ( 0 , MAX _SEMANTIC _MEMORIES ) ;
2026-05-16 03:01:33 +01:00
}
function isLowCoherenceDistilledSeam ( entry ) {
const label = readString ( entry , "subjectLabel" ) ? ? "" ;
const summary = readString ( entry , "summary" ) ? ? "" ;
const targetKind = readString ( entry , "targetKind" ) ? ? "" ;
2026-05-16 06:45:43 +01:00
const kind = readString ( entry , "kind" ) ? ? "" ;
2026-05-16 03:01:33 +01:00
const keywords = topConceptKeywords ( ` ${ label } ${ summary } ` , 5 ) ;
if ( keywords . length < 2 ) {
return true ;
}
2026-05-16 06:45:43 +01:00
if ( kind === "recent-preoccupation" || kind === "quiet-room-status" ) {
return true ;
}
2026-05-16 03:01:33 +01:00
if ( targetKind === "self" && keywords . length < 3 ) {
return true ;
}
2026-05-16 06:45:43 +01:00
if ( targetKind === "system" && /live seam|live question|live claim/i . test ( summary ) ) {
return true ;
}
2026-05-16 06:31:53 +01:00
if ( ! label || label . startsWith ( "seam:" ) ) {
return true ;
}
2026-05-16 06:45:43 +01:00
if ( looksPersonLikeSingleton ( label ) ) {
return true ;
}
2026-05-16 06:31:53 +01:00
if ( looksKeywordSalad ( label ) ) {
return true ;
}
2026-05-16 03:01:33 +01:00
if ( /still matters\./i . test ( summary ) && ! /^[A-Z][A-Za-z0-9]+: / . test ( label ) ) {
return true ;
}
return false ;
}
2026-05-16 06:31:53 +01:00
function shouldPurgeThoughtLikeMemory ( { entry , normalized , preferredLabel } ) {
const rawSummary = readString ( entry , "summary" ) ? ? "" ;
const rawQuestion = readString ( entry , "question" ) ? ? "" ;
const rawClaim = readString ( entry , "claim" ) ? ? "" ;
const label =
preferredLabel ? ?
readString ( entry , "subjectLabel" ) ? ?
readString ( entry , "theme" ) ? ?
normalized . label ? ?
"" ;
if ( ! hasThoughtSurfaceTemplateSmell ( rawSummary ) && ! hasThoughtSurfaceTemplateSmell ( rawQuestion ) ) {
return false ;
}
if ( sanitizePreferredThoughtLabel ( label ) ) {
return false ;
}
if ( normalizeText ( rawClaim ) . length > 0 && ! hasThoughtSurfaceTemplateSmell ( rawClaim ) ) {
return false ;
}
return true ;
}
2026-05-16 06:45:43 +01:00
function shouldDropSemanticMemory ( entry ) {
const kind = readString ( entry , "kind" ) ? ? "" ;
const label = readString ( entry , "subjectLabel" ) ? ? readString ( entry , "theme" ) ? ? "" ;
const summary = readString ( entry , "summary" ) ? ? "" ;
const targetKind = readString ( entry , "targetKind" ) ? ? "" ;
const focusKind = readString ( entry , "focusKind" ) ? ? "" ;
if ( kind === "identity_seam" ) {
return false ;
}
if ( kind === "recent-preoccupation" || kind === "quiet-room-status" ) {
return true ;
}
if ( looksPersonLikeSingleton ( label ) ) {
return true ;
}
if ( targetKind === "system" && focusKind === "question" && ! /repo|archive|lore|room|self/i . test ( summary ) ) {
return true ;
}
if ( hasThoughtSurfaceTemplateSmell ( summary ) ) {
return true ;
}
return false ;
}
2026-05-16 06:31:53 +01:00
function hasThoughtSurfaceTemplateSmell ( value ) {
const normalized = normalizeText ( value ) . toLowerCase ( ) ;
if ( ! normalized ) {
return false ;
}
return (
normalized . includes ( "what does the current room need from it" ) ||
normalized . includes ( "if it had to change a real machine" ) ||
normalized . includes ( "keeps returning with enough structure to deserve another honest pass" ) ||
normalized . includes ( "still matters. what would" ) ||
2026-05-16 06:45:43 +01:00
normalized . includes ( "looks like a live seam" ) ||
normalized . includes ( "live seam around" ) ||
normalized . includes ( "looks like a live claim" ) ||
normalized . includes ( "looks like a live question" ) ||
2026-05-16 06:31:53 +01:00
normalized . includes ( "archive-facing seam around" ) ||
normalized . includes ( "self-facing seam around" ) ||
normalized . includes ( "dream-compressed a seam around" )
) ;
}
function looksKeywordSalad ( value ) {
const normalized = normalizeText ( value ) . toLowerCase ( ) ;
if ( ! normalized ) {
return true ;
}
if ( normalized . split ( "," ) . length >= 3 ) {
return true ;
}
const tokens = tokenize ( normalized ) . filter ( ( token ) => ! stopwords . has ( token ) ) ;
if ( tokens . length === 0 ) {
return true ;
}
const longTokens = tokens . filter ( ( token ) => token . length >= 5 ) ;
return tokens . length >= 3 && longTokens . length === 0 ;
}
2026-05-16 06:45:43 +01:00
function looksPersonLikeSingleton ( value ) {
const normalized = normalizeText ( value ) ;
if ( ! normalized ) {
return false ;
}
if ( normalized . includes ( ":" ) || normalized . includes ( " " ) ) {
return false ;
}
if ( ! /^[A-Z][a-zA-Z0-9_-]+$/ . test ( normalized ) ) {
return false ;
}
return normalized . length >= 4 ;
}
2026-05-16 06:31:53 +01:00
function pruneDreamMemories ( { memories } ) {
const dreams = ensureArray ( memories . dreams )
. filter ( isObject )
. filter ( ( dream ) => ! hasThoughtSurfaceTemplateSmell ( readString ( dream , "summary" ) ? ? "" ) )
. filter ( ( dream ) => ! looksKeywordSalad ( readString ( dream , "theme" ) ? ? "" ) )
. sort ( compareMemoryFreshness ) ;
memories . dreams = dreams . slice ( 0 , MAX _DREAM _MEMORIES ) ;
}
function trimHistoricalMemoryResidue ( { memories , runtime } ) {
memories . archive _excursions = trimRecentObjectRecords (
memories . archive _excursions ,
MAX _ARCHIVE _EXCURSION _MEMORIES ,
) ;
memories . repo _sweeps = trimRecentObjectRecords ( memories . repo _sweeps , MAX _REPO _SWEEP _MEMORIES ) ;
memories . musings = trimRecentObjectRecords ( memories . musings , MAX _POST _NAP _MUSINGS ) ;
memories . candidate _interventions = trimCandidateInterventions ( memories . candidate _interventions ) ;
runtime . recent _archive _excursions = trimRecentObjectRecords (
runtime . recent _archive _excursions ,
MAX _POST _NAP _ARCHIVE _EXCURSIONS ,
) ;
runtime . archive _excursions = trimRecentObjectRecords ( runtime . archive _excursions , MAX _RUNTIME _ARCHIVE _EXCURSIONS ) ;
runtime . recent _repo _activity _sweeps = trimRecentObjectRecords (
runtime . recent _repo _activity _sweeps ,
MAX _POST _NAP _REPO _SWEEPS ,
) ;
runtime . repo _sweeps = trimRecentObjectRecords ( runtime . repo _sweeps , MAX _RUNTIME _REPO _SWEEPS ) ;
runtime . repo _activity _memories = trimRecentObjectRecords (
runtime . repo _activity _memories ,
MAX _RUNTIME _REPO _ACTIVITY _MEMORIES ,
) ;
runtime . recent _novelty _checks = trimRecentObjectRecords (
runtime . recent _novelty _checks ,
MAX _POST _NAP _NOVELTY _CHECKS ,
) ;
runtime . rumination _seeds = trimRecentObjectRecords ( runtime . rumination _seeds , MAX _RUNTIME _RUMINATION _SEEDS ) ;
runtime . candidate _interventions = trimCandidateInterventions ( runtime . candidate _interventions ) ;
const bridge = ensureObject ( runtime . bridge ) ;
bridge . recent _syntheses = trimRecentObjectRecords ( bridge . recent _syntheses , MAX _RECENT _SYNTHESIS _COUNT ) ;
bridge . unresolved _tensions = trimRecentObjectRecords ( bridge . unresolved _tensions , 3 ) ;
runtime . bridge = bridge ;
const memoryResonance = ensureObject ( runtime . memory _resonance ) ;
memoryResonance . recent _edges = trimRecentObjectRecords (
memoryResonance . recent _edges ,
MAX _POST _NAP _RESONANCE _EDGES ,
) ;
runtime . memory _resonance = memoryResonance ;
2026-05-16 06:45:43 +01:00
if ( Array . isArray ( runtime . pending _adjustments ) && runtime . pending _adjustments . length > 0 ) {
runtime . pending _adjustments = runtime . pending _adjustments . slice ( - 1 ) ;
}
scrubRuntimeThoughtResidue ( { runtime } ) ;
2026-05-16 08:03:59 +01:00
distillStateSurface ( { memories , runtime } ) ;
2026-05-16 06:31:53 +01:00
}
function trimRecentObjectRecords ( entries , limit ) {
return ensureArray ( entries ) . filter ( isObject ) . sort ( compareMemoryFreshness ) . slice ( 0 , limit ) ;
}
2026-05-16 06:45:43 +01:00
function scrubRuntimeThoughtResidue ( { runtime } ) {
const candidateInterventions = ensureArray ( runtime . candidate _interventions )
. filter ( isObject )
. filter ( ( entry ) => ! entryHasThoughtSurfaceTemplateSmell ( entry ) )
. map ( ( entry ) => scrubThoughtLikeEntry ( entry ) ) ;
runtime . candidate _interventions = trimCandidateInterventions ( candidateInterventions ) ;
runtime . discomforts = ensureArray ( runtime . discomforts )
. filter ( isObject )
. filter ( ( entry ) => ! entryHasThoughtSurfaceTemplateSmell ( entry ) )
. map ( ( entry ) => scrubThoughtLikeEntry ( entry ) )
. slice ( 0 , MAX _DISCOMFORT _COUNT ) ;
runtime . active _tensions = ensureArray ( runtime . active _tensions )
. filter ( isObject )
. filter ( ( entry ) => ! entryHasThoughtSurfaceTemplateSmell ( entry ) )
. map ( ( entry ) => scrubThoughtLikeEntry ( entry ) )
. slice ( 0 , MAX _ACTIVE _TENSION _COUNT ) ;
runtime . self _advocacy _requests = ensureArray ( runtime . self _advocacy _requests )
. filter ( isObject )
. filter ( ( entry ) => ! entryHasThoughtSurfaceTemplateSmell ( entry ) )
. map ( ( entry ) => scrubThoughtLikeEntry ( entry ) )
. slice ( 0 , MAX _ADVOCACY _REQUEST _COUNT ) ;
runtime . world _advocacy _requests = ensureArray ( runtime . world _advocacy _requests )
. filter ( isObject )
. filter ( ( entry ) => ! entryHasThoughtSurfaceTemplateSmell ( entry ) )
. map ( ( entry ) => scrubThoughtLikeEntry ( entry ) )
. slice ( 0 , MAX _ADVOCACY _REQUEST _COUNT ) ;
}
function entryHasThoughtSurfaceTemplateSmell ( entry ) {
return Object . values ( entry ) . some ( ( value ) => typeof value === "string" && hasThoughtSurfaceTemplateSmell ( value ) ) ;
}
function scrubThoughtLikeEntry ( entry ) {
const next = { ... entry } ;
for ( const [ key , value ] of Object . entries ( next ) ) {
if ( typeof value !== "string" ) {
continue ;
}
next [ key ] = scrubThoughtSurfaceText ( value ) ;
}
return next ;
}
function scrubThoughtSurfaceText ( value ) {
if ( ! hasThoughtSurfaceTemplateSmell ( value ) ) {
return value ;
}
return normalizeText ( value )
. replace ( /\bseems to have a live seam around\b/gi , "is accumulating a concrete decision around" )
. replace ( /\blive seam around\b/gi , "concrete decision around" )
. replace ( /\blooks like a live seam,? not just ambient noise\.?/gi , "is staying structurally important across passes." )
. replace ( /\bstill looks like a live seam\.?/gi , "remains unresolved enough to deserve a cleaner read." )
. replace ( /\blooks like a live claim\.?/gi , "has become a concrete claim." )
. replace ( /\bstill looks like a live question\.?/gi , "remains an unresolved question." )
. replace ( /\s+/g , " " )
. trim ( ) ;
}
2026-05-16 08:03:59 +01:00
function distillStateSurface ( { memories , runtime } ) {
for ( const entry of ensureArray ( memories . episodic ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
if ( typeof entry . significance === "string" ) {
entry . significance = compactNarrative ( entry . significance ) ;
}
}
for ( const memory of ensureArray ( memories . semantic ) . filter ( isObject ) ) {
memory . evidenceRefs = capDistinctStrings ( memory . evidenceRefs , MAX _SUPPORTING _REFS ) ;
if ( typeof memory . summary === "string" ) {
memory . summary = compactNarrative ( memory . summary ) ;
}
}
for ( const dream of ensureArray ( memories . dreams ) . filter ( isObject ) ) {
dream . distilledFrom = capDistinctStrings ( dream . distilledFrom , MAX _DREAM _SOURCE _IDS ) ;
if ( typeof dream . summary === "string" ) {
dream . summary = compactNarrative ( dream . summary ) ;
}
}
for ( const thought of ensureArray ( ensureObject ( runtime . incubation ) . active _thoughts ) . filter ( isObject ) ) {
thought . sourceMemoryIds = capDistinctStrings ( thought . sourceMemoryIds , MAX _INCUBATION _SOURCE _IDS ) ;
}
for ( const entry of ensureArray ( ensureObject ( runtime . bridge ) . recent _syntheses ) . filter ( isObject ) ) {
entry . dominantTopics = capDistinctStrings ( entry . dominantTopics , 3 ) ;
if ( typeof entry . analytic === "string" ) {
entry . analytic = compactNarrative ( entry . analytic ) ;
}
if ( typeof entry . associative === "string" ) {
entry . associative = compactNarrative ( entry . associative ) ;
}
}
for ( const entry of ensureArray ( runtime . recent _repo _activity _sweeps ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
}
for ( const entry of ensureArray ( runtime . repo _sweeps ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
}
for ( const entry of ensureArray ( runtime . repo _activity _memories ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
}
for ( const entry of ensureArray ( memories . repo _sweeps ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
}
for ( const entry of ensureArray ( memories . musings ) . filter ( isObject ) ) {
if ( typeof entry . summary === "string" ) {
entry . summary = compactNarrative ( entry . summary ) ;
}
}
const recentActivity = ensureObject ( runtime . recent _activity ) ;
if ( typeof recentActivity . summary === "string" ) {
recentActivity . summary = compactNarrative ( recentActivity . summary ) ;
}
if ( typeof recentActivity . lastDecisionRationale === "string" ) {
recentActivity . lastDecisionRationale = compactNarrative ( recentActivity . lastDecisionRationale ) ;
}
runtime . recent _activity = recentActivity ;
const thoughtLanes = ensureObject ( runtime . thought _lanes ) ;
if ( typeof thoughtLanes . analytic === "string" ) {
thoughtLanes . analytic = compactNarrative ( thoughtLanes . analytic ) ;
}
if ( typeof thoughtLanes . associative === "string" ) {
thoughtLanes . associative = compactNarrative ( thoughtLanes . associative ) ;
}
runtime . thought _lanes = thoughtLanes ;
runtime . pending _adjustments = ensureArray ( runtime . pending _adjustments )
. filter ( isObject )
. map ( ( entry ) => {
if ( typeof entry . summary !== "string" ) {
return entry ;
}
return {
... entry ,
summary : compactNarrative ( entry . summary ) ,
} ;
} ) ;
const memoryResonance = ensureObject ( runtime . memory _resonance ) ;
for ( const edge of ensureArray ( memoryResonance . recent _edges ) . filter ( isObject ) ) {
if ( typeof edge . leftLabel === "string" ) {
edge . leftLabel = compactLabel ( edge . leftLabel ) ;
}
if ( typeof edge . rightLabel === "string" ) {
edge . rightLabel = compactLabel ( edge . rightLabel ) ;
}
if ( typeof edge . summary === "string" ) {
edge . summary = compactNarrative ( edge . summary ) ;
}
}
}
function compactNarrative ( value ) {
const normalized = normalizeText ( value ) ;
if ( ! normalized ) {
return normalized ;
}
const firstSentence = normalized . match ( /^.*?[.!?](?:\s|$)/ ) ? . [ 0 ] ? . trim ( ) ;
const candidate = firstSentence && firstSentence . length >= 24 ? firstSentence : normalized ;
return candidate . length > 220 ? ` ${ candidate . slice ( 0 , 217 ) . trimEnd ( ) } ... ` : candidate ;
}
function compactLabel ( value ) {
const normalized = normalizeText ( value ) ;
if ( ! normalized ) {
return normalized ;
}
if ( /incremental sweep found/i . test ( normalized ) ) {
const repoMatch = normalized . match ( /`([^`]+)`/ ) ;
const repoName = repoMatch ? . [ 1 ] ? ? "repo" ;
return ` ${ repoName } repo sweep ` ;
}
if ( /no new discord traffic|one new discord message|no messages arrived/i . test ( normalized ) ) {
return "room pass" ;
}
return normalized . length > 96 ? ` ${ normalized . slice ( 0 , 93 ) . trimEnd ( ) } ... ` : normalized ;
}
function capDistinctStrings ( entries , limit ) {
return [ ... new Set ( ensureStringArray ( entries ) ) ] . slice ( 0 , limit ) ;
}
2026-05-16 03:01:33 +01:00
function compareMemoryFreshness ( left , right ) {
const leftTime = parseIsoTimestamp ( readString ( left , "lastObservedAt" ) ? ? readString ( left , "timestamp" ) ? ? "" ) ? ? 0 ;
const rightTime = parseIsoTimestamp ( readString ( right , "lastObservedAt" ) ? ? readString ( right , "timestamp" ) ? ? "" ) ? ? 0 ;
return rightTime - leftTime ;
}
function trimLegacyRuntimeResidue ( { runtime } ) {
const memoryResonance = ensureObject ( runtime . memory _resonance ) ;
const recentEdges = ensureArray ( memoryResonance . recent _edges ) . filter ( isObject ) ;
memoryResonance . recent _edges = recentEdges
. filter ( ( edge ) => ! looksLegacyThoughtSurface ( readString ( edge , "leftLabel" ) ? ? "" , readString ( edge , "summary" ) ? ? "" ) )
. slice ( - MAX _EDGE _COUNT ) ;
runtime . memory _resonance = memoryResonance ;
}
2026-05-16 08:03:59 +01:00
function reconcileLegacyStateMirrors ( { state , memories , runtime , now } ) {
const timestamp = now . toISOString ( ) ;
const bridge = ensureObject ( runtime . bridge ) ;
const runtimeThoughtLanes = ensureObject ( runtime . thought _lanes ) ;
const activeThoughts = ensureArray ( ensureObject ( runtime . incubation ) . active _thoughts ) . filter ( isObject ) ;
const activeTensions = ensureArray ( runtime . active _tensions ) . filter ( isObject ) ;
state . archive _excursions = [ ] ;
state . repo _sweeps = ensureArray ( memories . repo _sweeps )
. filter ( isObject )
. map ( ( entry ) => compactRepoSweepMirror ( entry ) )
. slice ( 0 , MAX _REPO _SWEEP _MEMORIES ) ;
state . repo _activity _memories = ensureArray ( runtime . repo _activity _memories )
. filter ( isObject )
. map ( ( entry ) => compactRepoSweepMirror ( entry ) )
. slice ( 0 , MAX _RUNTIME _REPO _ACTIVITY _MEMORIES ) ;
state . recent _archive _excursions = ensureArray ( runtime . recent _archive _excursions )
. filter ( isObject )
. map ( ( entry ) => compactArchiveExcursionMirror ( entry ) )
. slice ( 0 , MAX _RUNTIME _ARCHIVE _EXCURSIONS ) ;
state . recent _novelty _checks = ensureArray ( runtime . recent _novelty _checks )
. filter ( isObject )
. map ( ( entry ) => compactNoveltyCheckMirror ( entry ) )
. slice ( 0 , MAX _POST _NAP _NOVELTY _CHECKS ) ;
state . recent _activity = compactRecentActivityMirror ( runtime . recent _activity , runtime , timestamp ) ;
state . source _coverage = cloneJson ( ensureObject ( runtime . source _coverage ) ) ;
state . speaking _bias = cloneJson ( ensureObject ( runtime . speaking _bias ) ) ;
state . lastReviewSummary = readString ( runtime , "lastReviewSummary" ) ? ? readString ( runtime , "last_review_summary" ) ? ? null ;
state . lastReviewSummaryShort =
readString ( runtime , "lastReviewSummaryShort" ) ? ? readString ( runtime , "last_review_summary_short" ) ? ? null ;
state . last _review _summary = state . lastReviewSummary ;
state . last _review _summary _short = state . lastReviewSummaryShort ;
state . thought _lanes = {
analytic : buildThoughtLaneMirror ( {
sourceValue : runtimeThoughtLanes . analytic ,
fallback : activeTensions [ 0 ] ,
timestamp ,
} ) ,
associative : buildThoughtLaneMirror ( {
sourceValue : runtimeThoughtLanes . associative ,
fallback : activeThoughts [ 0 ] ? ? bridge ,
timestamp ,
} ) ,
} ;
}
function compactRepoSweepMirror ( entry ) {
return {
timestamp : readString ( entry , "timestamp" ) ? ? null ,
memoryId : readString ( entry , "memoryId" ) ? ? null ,
summary : compactNarrative ( readString ( entry , "summary" ) ? ? "" ) ,
repoNames : ensureStringArray ( entry . repoNames ) . slice ( 0 , 3 ) ,
whyItMattered : compactNarrative ( readString ( entry , "whyItMattered" ) ? ? "" ) ,
} ;
}
function compactArchiveExcursionMirror ( entry ) {
return {
memoryId : readString ( entry , "memoryId" ) ? ? null ,
timestamp : readString ( entry , "timestamp" ) ? ? null ,
topicHint : compactNarrative ( readString ( entry , "topicHint" ) ? ? "" ) ,
whyItWasFresh : compactNarrative ( readString ( entry , "whyItWasFresh" ) ? ? "" ) ,
} ;
}
function compactNoveltyCheckMirror ( entry ) {
return {
timestamp : readString ( entry , "timestamp" ) ? ? null ,
query : compactNarrative ( readString ( entry , "query" ) ? ? "" ) ,
candidate : compactNarrative ( readString ( entry , "candidate" ) ? ? "" ) ,
result : readString ( entry , "result" ) ? ? null ,
note : compactNarrative ( readString ( entry , "note" ) ? ? "" ) ,
} ;
}
function compactRecentActivityMirror ( value , runtime , timestamp ) {
const recentActivity = isObject ( value ) ? cloneJson ( value ) : { } ;
recentActivity . checkedAt = readString ( recentActivity , "checkedAt" ) ? ? timestamp ;
if ( typeof recentActivity . summary === "string" ) {
recentActivity . summary = compactNarrative ( recentActivity . summary ) ;
}
if ( typeof recentActivity . lastDecisionRationale === "string" ) {
recentActivity . lastDecisionRationale = compactNarrative ( recentActivity . lastDecisionRationale ) ;
}
if ( ! recentActivity . summary ) {
recentActivity . summary =
compactNarrative ( readString ( runtime , "lastReviewSummary" ) ? ? readString ( runtime , "last_review_summary" ) ? ? "" ) ? ?
"" ;
}
return recentActivity ;
}
function buildThoughtLaneMirror ( { sourceValue , fallback , timestamp } ) {
const summary = summarizeThoughtLaneSource ( sourceValue , fallback ) ;
return summary
? [
{
timestamp ,
summary ,
} ,
]
: [ ] ;
}
function summarizeThoughtLaneSource ( sourceValue , fallback ) {
if ( typeof sourceValue === "string" ) {
return compactNarrative ( sourceValue ) ;
}
if ( isObject ( sourceValue ) ) {
const threads = ensureArray ( sourceValue . active _threads ) . filter ( isObject ) ;
if ( threads . length > 0 ) {
const latest = threads
. slice ( )
. sort ( compareMemoryFreshness ) [ 0 ] ;
return compactNarrative (
[ readString ( latest , "topic" ) , readString ( latest , "claim" ) , readString ( latest , "counterweight" ) ]
. filter ( Boolean )
. join ( ". " ) ,
) ;
}
const description = readString ( sourceValue , "description" ) ;
if ( description ) {
return compactNarrative ( description ) ;
}
}
if ( isObject ( fallback ) ) {
return compactNarrative (
[
readString ( fallback , "topic" ) ,
readString ( fallback , "summary" ) ,
readString ( fallback , "claim" ) ,
readString ( fallback , "opinion" ) ,
]
. filter ( Boolean )
. join ( ". " ) ,
) ;
}
return null ;
}
2026-05-06 02:13:00 +01:00
function collectMemoryRecords ( { memories , runtime } ) {
const records = [ ] ;
2026-05-07 22:39:50 +01:00
pushArrayRecords ( records , selectRecentRecords ( ensureArray ( memories . episodic ) , {
limit : MAX _RECENT _EPISODIC _RECORDS ,
quietLimit : MAX _RECENT _QUIET _EPISODIC _RECORDS ,
} ) , "episodic" , ( entry ) => ( {
2026-05-06 02:13:00 +01:00
text : readString ( entry , "summary" ) ,
label : readString ( entry , "summary" ) ? ? "episodic memory" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "episodic" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( memories . semantic ) . slice ( - 24 ) , "semantic" , ( entry ) => ( {
text : [ readString ( entry , "subjectLabel" ) , readString ( entry , "kind" ) , readString ( entry , "summary" ) ]
. filter ( Boolean )
. join ( " | " ) ,
label : readString ( entry , "subjectLabel" ) ? ? readString ( entry , "kind" ) ? ? "semantic memory" ,
timestamp : readString ( entry , "lastObservedAt" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "semantic" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( memories . musings ) . slice ( - 12 ) , "musing" , ( entry ) => ( {
text : [ readString ( entry , "topic" ) , readString ( entry , "summary" ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "topic" ) ? ? "musing" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "musing" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( memories . dreams ) . slice ( - 10 ) , "dream" , ( entry ) => ( {
text : [ readString ( entry , "theme" ) , readString ( entry , "summary" ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "theme" ) ? ? "dream" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "dream" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( runtime . recent _archive _excursions ) . slice ( - 8 ) , "archive_excursion" , ( entry ) => ( {
text : [ readString ( entry , "topicHint" ) , readString ( entry , "whyItWasFresh" ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "topicHint" ) ? ? "archive excursion" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "archive_excursion" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( runtime . recent _repo _activity _sweeps ) . slice ( - 8 ) , "repo_sweep" , ( entry ) => ( {
text : [ readString ( entry , "summary" ) , readString ( entry , "whyItMattered" ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "summary" ) ? ? "repo sweep" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "repo_sweep" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords (
records ,
2026-05-07 22:39:50 +01:00
selectRecentRecords ( ensureArray ( ensureObject ( runtime . thought _lanes ) . analytic ? . active _threads ) , {
limit : MAX _RECENT _ANALYTIC _THREADS ,
quietLimit : MAX _RECENT _QUIET _ANALYTIC _THREADS ,
} ) ,
2026-05-06 02:13:00 +01:00
"analytic_thread" ,
( entry ) => ( {
text : [ readString ( entry , "topic" ) , readString ( entry , "claim" ) , readString ( entry , "counterweight" ) ]
. filter ( Boolean )
. join ( " | " ) ,
label : readString ( entry , "topic" ) ? ? "analytic thread" ,
timestamp : readString ( entry , "lastTouchedAt" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "analytic_thread" } ) ,
2026-05-06 02:13:00 +01:00
} ) ,
) ;
pushArrayRecords (
records ,
ensureArray ( ensureObject ( runtime . thought _lanes ) . associative ? . active _threads ) . slice ( - 6 ) ,
"associative_thread" ,
( entry ) => ( {
text : [ readString ( entry , "topic" ) , readString ( entry , "claim" ) , readString ( entry , "counterweight" ) ]
. filter ( Boolean )
. join ( " | " ) ,
label : readString ( entry , "topic" ) ? ? "associative thread" ,
timestamp : readString ( entry , "lastTouchedAt" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "associative_thread" } ) ,
2026-05-06 02:13:00 +01:00
} ) ,
) ;
pushArrayRecords ( records , ensureArray ( ensureObject ( runtime . bridge ) . recent _syntheses ) . slice ( - 8 ) , "bridge_synthesis" , ( entry ) => ( {
text : [ readString ( entry , "summary" ) , ... ensureStringArray ( entry . dominantTopics ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "summary" ) ? ? "bridge synthesis" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "bridge_synthesis" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
pushArrayRecords ( records , ensureArray ( runtime . candidate _interventions ) . slice ( - 8 ) , "candidate_intervention" , ( entry ) => ( {
text : [ readString ( entry , "summary" ) , readString ( entry , "draft" ) ] . filter ( Boolean ) . join ( " | " ) ,
label : readString ( entry , "summary" ) ? ? "candidate intervention" ,
timestamp : readString ( entry , "timestamp" ) ,
2026-05-08 16:52:22 +01:00
sourceRefs : gatherSourceRefs ( entry ) ,
sourceMeta : gatherSourceMeta ( { entry , kind : "candidate_intervention" } ) ,
2026-05-06 02:13:00 +01:00
} ) ) ;
return records ;
}
function pushArrayRecords ( target , entries , kind , toRecord ) {
for ( const entry of entries ) {
if ( ! isObject ( entry ) ) {
continue ;
}
const record = toRecord ( entry ) ;
const text = normalizeText ( record . text ) ;
if ( ! text ) {
continue ;
}
entry . memoryId = ensureMemoryId ( entry , kind , text ) ;
target . push ( {
kind ,
entry ,
memoryId : entry . memoryId ,
text ,
label : record . label ? ? kind ,
timestamp : record . timestamp ? ? null ,
2026-05-08 16:52:22 +01:00
sourceRefs : ensureStringArray ( record . sourceRefs ) ,
sourceMeta : isObject ( record . sourceMeta ) ? record . sourceMeta : { } ,
2026-05-07 22:39:50 +01:00
lowSignalQuietRoom : isLowSignalQuietRoomText ( text ) ,
2026-05-06 02:13:00 +01:00
} ) ;
}
}
async function ensureSemanticVector ( { record , embedder , now } ) {
const prior = isObject ( record . entry . semanticVector ) ? record . entry . semanticVector : { } ;
const contentHash = hashString ( record . text ) ;
const priorValues = Array . isArray ( prior . values ) ? prior . values : [ ] ;
if (
prior . sourceHash === contentHash &&
prior . backend === embedder . backend &&
prior . model === embedder . model &&
prior . compactDimensions === embedder . dimensions &&
priorValues . length === embedder . dimensions
) {
return false ;
}
const vector = await embedder . embed ( record . text ) ;
record . entry . semanticVector = {
version : 1 ,
backend : embedder . backend ,
model : embedder . model ,
compactDimensions : embedder . dimensions ,
sourceHash : contentHash ,
embeddedAt : now . toISOString ( ) ,
values : vector . map ( round4 ) ,
} ;
return true ;
}
function buildAssociationEdges ( records ) {
const edges = [ ] ;
for ( let leftIndex = 0 ; leftIndex < records . length ; leftIndex += 1 ) {
for ( let rightIndex = leftIndex + 1 ; rightIndex < records . length ; rightIndex += 1 ) {
const left = records [ leftIndex ] ;
const right = records [ rightIndex ] ;
2026-05-07 22:39:50 +01:00
if ( left . lowSignalQuietRoom && right . lowSignalQuietRoom ) {
continue ;
}
2026-05-06 02:13:00 +01:00
const threshold = edgeThresholdForKinds ( left . kind , right . kind ) ;
const similarity = cosineSimilarity (
left . entry . semanticVector . values ,
right . entry . semanticVector . values ,
) ;
if ( similarity < threshold ) {
continue ;
}
edges . push ( {
left ,
right ,
similarity ,
summary : summarizeEdge ( left , right , similarity ) ,
} ) ;
}
}
return edges . sort ( ( a , b ) => b . similarity - a . similarity ) . slice ( 0 , MAX _EDGE _COUNT * 2 ) ;
}
function summarizeEdge ( left , right , similarity ) {
const overlap = topKeywords ( ` ${ left . text } ${ right . text } ` , 3 ) . join ( ", " ) ;
return ` Resonance between ${ left . kind } and ${ right . kind } ${ overlap ? ` around ${ overlap } ` : "" } ( ${ round3 ( similarity ) } ). ` ;
}
function buildClusters ( records , edges ) {
const adjacency = new Map ( ) ;
for ( const record of records ) {
adjacency . set ( record . memoryId , new Set ( ) ) ;
}
for ( const edge of edges ) {
if ( edge . similarity < CLUSTER _SIMILARITY _THRESHOLD ) {
continue ;
}
adjacency . get ( edge . left . memoryId ) ? . add ( edge . right . memoryId ) ;
adjacency . get ( edge . right . memoryId ) ? . add ( edge . left . memoryId ) ;
}
const visited = new Set ( ) ;
const clusters = [ ] ;
for ( const record of records ) {
if ( visited . has ( record . memoryId ) ) {
continue ;
}
const neighbors = adjacency . get ( record . memoryId ) ;
if ( ! neighbors || neighbors . size === 0 ) {
continue ;
}
const queue = [ record . memoryId ] ;
const componentIds = [ ] ;
visited . add ( record . memoryId ) ;
while ( queue . length > 0 ) {
const current = queue . shift ( ) ;
if ( ! current ) {
continue ;
}
componentIds . push ( current ) ;
for ( const neighbor of adjacency . get ( current ) ? ? [ ] ) {
if ( visited . has ( neighbor ) ) {
continue ;
}
visited . add ( neighbor ) ;
queue . push ( neighbor ) ;
}
}
if ( componentIds . length < 2 ) {
continue ;
}
const items = componentIds
. map ( ( memoryId ) => records . find ( ( candidate ) => candidate . memoryId === memoryId ) )
. filter ( Boolean ) ;
const sourceKinds = [ ... new Set ( items . map ( ( item ) => item . kind ) ) ] ;
if ( sourceKinds . length < 2 ) {
continue ;
}
2026-05-07 22:39:50 +01:00
const quietSignalCount = items . filter ( ( item ) => item . lowSignalQuietRoom ) . length ;
const quietSignalRatio = quietSignalCount / items . length ;
2026-05-06 02:13:00 +01:00
const resonance = averagePairwiseSimilarity ( items ) ;
2026-05-16 02:53:18 +01:00
const keywords = topConceptKeywords ( items . map ( ( item ) => item . text ) . join ( " " ) , 5 ) ;
2026-05-08 16:52:22 +01:00
const repoNames = [ ... new Set ( items . flatMap ( ( item ) => ensureStringArray ( item . sourceMeta . repoNames ) ) ) ] ;
const archiveYears = [
... new Set (
items
. map ( ( item ) => readString ( item . sourceMeta , "archiveYear" ) )
. filter ( ( value ) => typeof value === "string" && value . length > 0 ) ,
) ,
] ;
const channelIds = [
... new Set (
items
. map ( ( item ) => readString ( item . sourceMeta , "channelId" ) )
. filter ( ( value ) => typeof value === "string" && value . length > 0 ) ,
) ,
] ;
2026-05-16 08:03:59 +01:00
const evidenceRefs = capDistinctStrings (
items . flatMap ( ( item ) => ensureStringArray ( item . sourceRefs ) ) ,
MAX _SUPPORTING _REFS ,
) ;
2026-05-08 16:52:22 +01:00
const evidenceDiversity = clamp (
sourceKinds . length * 0.18 +
repoNames . length * 0.17 +
archiveYears . length * 0.11 +
channelIds . length * 0.08 +
Math . min ( 1 , evidenceRefs . length / 8 ) * 0.16 ,
0 ,
1 ,
) ;
2026-05-16 02:53:18 +01:00
const curiosityProfile = buildClusterCuriosityProfile ( {
sourceKinds ,
repoNames ,
archiveYears ,
channelIds ,
evidenceRefs ,
evidenceDiversity ,
} ) ;
2026-05-16 08:03:59 +01:00
const preferredThoughtSurface = readPreferredClusterThoughtSurface ( items ) ;
const thoughtSurface = preferredThoughtSurface ? ? synthesizeThoughtSurface ( {
2026-05-16 02:53:18 +01:00
sourceKinds ,
repoNames ,
archiveYears ,
channelIds ,
keywords ,
evidenceDiversity ,
curiosityProfile ,
} ) ;
2026-05-16 06:31:53 +01:00
const cleanedLabel = sanitizePreferredThoughtLabel ( thoughtSurface . label ) ? ? thoughtSurface . label ;
const cleanedFocusPhrase =
sanitizePreferredThoughtLabel ( thoughtSurface . focusPhrase ) ? ? thoughtSurface . focusPhrase ;
2026-05-06 02:13:00 +01:00
clusters . push ( {
clusterId : ` cluster- ${ hashString ( componentIds . sort ( ) . join ( "|" ) ) . slice ( 0 , 12 ) } ` ,
2026-05-16 06:31:53 +01:00
label : cleanedLabel ,
2026-05-16 02:53:18 +01:00
summary : thoughtSurface . summary ,
focusKind : thoughtSurface . focusKind ,
targetKind : thoughtSurface . targetKind ,
2026-05-16 06:31:53 +01:00
focusPhrase : cleanedFocusPhrase ,
2026-05-16 02:53:18 +01:00
question : thoughtSurface . question ,
claim : thoughtSurface . claim ,
2026-05-16 06:31:53 +01:00
fascinationTarget : cleanedFocusPhrase ,
2026-05-16 02:53:18 +01:00
worldFacing : thoughtSurface . worldFacing ,
2026-05-06 02:13:00 +01:00
resonance : round3 ( resonance ) ,
2026-05-16 08:03:59 +01:00
memoryIds : items . map ( ( item ) => item . memoryId ) . slice ( 0 , MAX _CLUSTER _MEMORY _IDS ) ,
2026-05-06 02:13:00 +01:00
sourceKinds ,
topKeywords : keywords ,
2026-05-08 16:52:22 +01:00
repoNames ,
archiveYears ,
channelIds ,
evidenceRefs ,
evidenceDiversity : round3 ( evidenceDiversity ) ,
2026-05-07 22:39:50 +01:00
quietSignalRatio : round3 ( quietSignalRatio ) ,
2026-05-16 02:53:18 +01:00
curiosityProfile ,
2026-05-06 02:13:00 +01:00
lastStrengthenedAt : new Date ( ) . toISOString ( ) ,
} ) ;
}
return clusters
. sort ( ( left , right ) => right . resonance - left . resonance )
. slice ( 0 , MAX _CLUSTER _COUNT ) ;
}
function edgeThresholdForKinds ( leftKind , rightKind ) {
if ( leftKind === "episodic" && rightKind === "episodic" ) {
return 0.86 ;
}
if ( leftKind === rightKind ) {
return EDGE _SIMILARITY _THRESHOLD + 0.05 ;
}
return EDGE _SIMILARITY _THRESHOLD ;
}
2026-05-16 02:53:18 +01:00
function reconcileIncubation ( { previous , bridge , clusters , runtime , sourceCoverage , identitySurface , now } ) {
2026-05-08 16:52:22 +01:00
bridge = ensureObject ( bridge ) ;
sourceCoverage = ensureObject ( sourceCoverage ) ;
2026-05-06 02:13:00 +01:00
const priorThoughts = ensureArray ( previous . active _thoughts ) . filter ( isObject ) ;
2026-05-16 02:53:18 +01:00
const curiosityContext = buildCuriosityContext ( { previousThoughts : priorThoughts , bridge } ) ;
2026-05-06 02:13:00 +01:00
const speakingBias = ensureObject ( runtime . speaking _bias ) ;
2026-05-08 16:52:22 +01:00
const recentNoveltyChecks = ensureArray ( runtime . recent _novelty _checks ) . filter ( isObject ) ;
2026-05-06 02:13:00 +01:00
const recentSpeechDamping = readNumber ( speakingBias , "recentSpeechDamping" ) ? ? 0 ;
const quietNovelty = readNumber ( speakingBias , "noveltyPressure" ) ? ? 0.4 ;
const needToSpeak = readNumber ( speakingBias , "needToSpeak" ) ? ? 0.4 ;
2026-05-08 16:52:22 +01:00
const activeThoughtCandidates = clusters . map ( ( cluster ) => {
2026-05-06 02:13:00 +01:00
const previousThought = priorThoughts . find (
( thought ) =>
readString ( thought , "thoughtId" ) === cluster . clusterId ||
overlapRatio ( ensureStringArray ( thought . sourceMemoryIds ) , cluster . memoryIds ) >= 0.45 ,
) ;
const sourceDiversity = Math . min ( 1 , cluster . sourceKinds . length / 4 ) ;
2026-05-07 22:39:50 +01:00
const quietSignalRatio = readNumber ( cluster , "quietSignalRatio" ) ? ? 0 ;
2026-05-06 02:13:00 +01:00
const priorMaturation = readNumber ( previousThought , "maturation" ) ? ? 0.32 ;
const deepDiveCount = ( readNumber ( previousThought , "deepDiveCount" ) ? ? 0 ) + 1 ;
2026-05-08 16:52:22 +01:00
const priorSupportCount = Math . max (
readNumber ( previousThought , "supportCount" ) ? ? 0 ,
Math . floor ( ( readNumber ( previousThought , "deepDiveCount" ) ? ? 0 ) / 4 ) ,
) ;
const supportCount = priorSupportCount + 1 ;
const explorationBonus = computeExplorationBonus ( cluster , sourceCoverage ) ;
2026-05-16 02:53:18 +01:00
const concreteness = readNumber ( cluster . curiosityProfile , "concreteness" ) ? ? 0.2 ;
const fertility = readNumber ( cluster . curiosityProfile , "fertility" ) ? ? 0.3 ;
const crossDomainPotential = readNumber ( cluster . curiosityProfile , "crossDomainPotential" ) ? ? 0.24 ;
const contradictionPressure = readNumber ( cluster . curiosityProfile , "contradictionPressure" ) ? ? 0.18 ;
const worldFacing = cluster . worldFacing === true ;
2026-05-08 16:52:22 +01:00
const noveltyToSelf = computeNoveltyToSelf ( {
cluster ,
previousThoughts : priorThoughts ,
bridge ,
currentThoughtId : readString ( previousThought , "thoughtId" ) ,
} ) ;
const noveltyToRoom = computeNoveltyToRoom ( cluster , recentNoveltyChecks ) ;
2026-05-16 02:53:18 +01:00
const identityPenalty = computeIdentityPenalty ( cluster , identitySurface ) ;
const outwardBonus = computeOutwardCuriosityBonus ( {
cluster ,
curiosityContext ,
concreteness ,
} ) ;
2026-05-08 16:52:22 +01:00
const saturationMetrics = computeSaturationMetrics ( {
cluster ,
previousThoughts : priorThoughts ,
bridge ,
supportCount ,
} ) ;
const refractoryPenalty = computeRefractoryPenalty ( cluster , bridge , now ) ;
const evidenceDiversity = readNumber ( cluster , "evidenceDiversity" ) ? ? 0.3 ;
2026-05-06 02:13:00 +01:00
const maturation = clamp (
2026-05-08 16:52:22 +01:00
priorMaturation * 0.44 +
2026-05-16 02:53:18 +01:00
fertility * 0.18 +
cluster . resonance * 0.1 +
evidenceDiversity * 0.12 +
contradictionPressure * 0.14 +
crossDomainPotential * 0.14 +
explorationBonus * 0.1 +
2026-05-08 16:52:22 +01:00
noveltyToSelf * 0.08 +
2026-05-16 02:53:18 +01:00
noveltyToRoom * 0.08 +
outwardBonus * 0.08 -
2026-05-08 16:52:22 +01:00
saturationMetrics . score * 0.18 -
quietSignalRatio * 0.24 -
2026-05-16 02:53:18 +01:00
refractoryPenalty * 0.12 -
identityPenalty * 0.24 ,
2026-05-08 16:52:22 +01:00
0 ,
1 ,
) ;
const novelty = clamp (
2026-05-16 02:53:18 +01:00
noveltyToSelf * 0.52 +
noveltyToRoom * 0.34 +
contradictionPressure * 0.09 +
outwardBonus * 0.08 +
quietNovelty * 0.06 -
quietSignalRatio * 0.12 -
identityPenalty * 0.16 ,
0 ,
1 ,
) ;
const curiosityPressure = clamp (
fertility * 0.2 +
concreteness * 0.2 +
crossDomainPotential * 0.16 +
contradictionPressure * 0.14 +
explorationBonus * 0.13 +
noveltyToSelf * 0.07 +
noveltyToRoom * 0.06 +
outwardBonus * 0.12 -
saturationMetrics . score * 0.14 -
quietSignalRatio * 0.18 -
refractoryPenalty * 0.12 -
identityPenalty * 0.22 ,
2026-05-06 02:13:00 +01:00
0 ,
1 ,
) ;
const desireToSpeak = clamp (
2026-05-16 02:53:18 +01:00
curiosityPressure * 0.2 +
cluster . resonance * 0.08 +
2026-05-16 03:51:29 +01:00
maturation * 0.15 +
noveltyToRoom * 0.19 +
noveltyToSelf * 0.07 +
concreteness * 0.14 +
2026-05-16 02:53:18 +01:00
crossDomainPotential * 0.1 +
2026-05-16 03:51:29 +01:00
needToSpeak * 0.15 +
2026-05-16 02:53:18 +01:00
quietNovelty * 0.07 +
2026-05-16 03:51:29 +01:00
( worldFacing ? 0.09 : 0 ) +
2026-05-16 02:53:18 +01:00
outwardBonus * 0.08 -
2026-05-16 03:51:29 +01:00
recentSpeechDamping * 0.16 -
quietSignalRatio * ( worldFacing && concreteness >= 0.62 ? 0.1 : 0.19 ) -
saturationMetrics . score * 0.19 -
refractoryPenalty * 0.82 -
2026-05-16 02:53:18 +01:00
identityPenalty * 0.28 ,
2026-05-06 02:13:00 +01:00
0 ,
1 ,
) ;
2026-05-16 03:51:29 +01:00
const groundedWorldThought =
worldFacing === true &&
concreteness >= 0.66 &&
curiosityPressure >= 0.58 &&
evidenceDiversity >= 0.34 ;
2026-05-06 02:13:00 +01:00
const status =
2026-05-07 22:39:50 +01:00
quietSignalRatio >= 0.55 && desireToSpeak < 0.68
? "cooling"
2026-05-16 02:53:18 +01:00
: identityPenalty >= 0.7 && noveltyToRoom < 0.8
? "cooling"
: ! worldFacing && curiosityContext . outwardPressure >= 0.52 && concreteness < 0.42
? "cooling"
2026-05-08 16:52:22 +01:00
: noveltyToSelf < 0.28 && saturationMetrics . score >= 0.62
? "stalled"
: refractoryPenalty >= 0.18 && noveltyToRoom < 0.72
? "refractory"
: supportCount >= 6 && evidenceDiversity < 0.34
? "stalled"
2026-05-16 03:51:29 +01:00
: supportCount >= 3 && noveltyToSelf < 0.55 && groundedWorldThought !== true
2026-05-08 16:52:22 +01:00
? "cooling"
: saturationMetrics . score >= 0.56 && noveltyToSelf < 0.42
? "cooling"
2026-05-16 03:51:29 +01:00
: groundedWorldThought &&
desireToSpeak >= 0.62 &&
noveltyToRoom >= 0.44 &&
saturationMetrics . score < 0.62
? "ripe"
2026-05-08 16:52:22 +01:00
: ( ( desireToSpeak >= 0.74 && ( noveltyToSelf >= 0.55 || noveltyToRoom >= 0.82 ) ) &&
saturationMetrics . score < 0.5 ) ||
maturation >= 0.82
2026-05-06 02:13:00 +01:00
? "ripe"
: maturation <= 0.28 && recentSpeechDamping >= 0.55
? "cooling"
: "incubating" ;
2026-05-08 16:52:22 +01:00
const priorityScore = clamp (
2026-05-16 02:53:18 +01:00
curiosityPressure * 0.34 +
desireToSpeak * 0.22 +
noveltyToSelf * 0.12 +
noveltyToRoom * 0.12 +
evidenceDiversity * 0.08 +
outwardBonus * 0.12 -
2026-05-08 16:52:22 +01:00
saturationMetrics . score * 0.14 -
2026-05-16 02:53:18 +01:00
refractoryPenalty * 0.12 -
identityPenalty * 0.18 ,
2026-05-08 16:52:22 +01:00
0 ,
1 ,
) ;
2026-05-06 02:13:00 +01:00
return {
2026-05-08 16:52:22 +01:00
thoughtId : readString ( previousThought , "thoughtId" ) ? ? cluster . clusterId ,
2026-05-16 06:31:53 +01:00
topic : sanitizePreferredThoughtLabel ( cluster . label ) ? ? cluster . label ,
2026-05-06 02:13:00 +01:00
summary : cluster . summary ,
2026-05-16 02:53:18 +01:00
focusKind : cluster . focusKind ,
targetKind : cluster . targetKind ,
focusPhrase : cluster . focusPhrase ,
question : cluster . question ,
claim : cluster . claim ,
fascinationTarget : cluster . fascinationTarget ,
worldFacing ,
2026-05-16 08:03:59 +01:00
sourceMemoryIds : cluster . memoryIds . slice ( 0 , MAX _INCUBATION _SOURCE _IDS ) ,
2026-05-06 02:13:00 +01:00
sourceKinds : cluster . sourceKinds ,
resonance : cluster . resonance ,
2026-05-07 22:39:50 +01:00
quietSignalRatio : round3 ( quietSignalRatio ) ,
2026-05-06 02:13:00 +01:00
novelty : round3 ( novelty ) ,
2026-05-08 16:52:22 +01:00
noveltyToSelf : round3 ( noveltyToSelf ) ,
noveltyToRoom : round3 ( noveltyToRoom ) ,
2026-05-06 02:13:00 +01:00
maturation : round3 ( maturation ) ,
desireToSpeak : round3 ( desireToSpeak ) ,
deepDiveCount ,
2026-05-08 16:52:22 +01:00
supportCount ,
evidenceDiversity : round3 ( evidenceDiversity ) ,
2026-05-16 02:53:18 +01:00
concreteness : round3 ( concreteness ) ,
fertility : round3 ( fertility ) ,
crossDomainPotential : round3 ( crossDomainPotential ) ,
contradictionPressure : round3 ( contradictionPressure ) ,
2026-05-08 16:52:22 +01:00
explorationBonus : round3 ( explorationBonus ) ,
2026-05-16 02:53:18 +01:00
outwardBonus : round3 ( outwardBonus ) ,
curiosityPressure : round3 ( curiosityPressure ) ,
identityPenalty : round3 ( identityPenalty ) ,
2026-05-08 16:52:22 +01:00
saturationScore : round3 ( saturationMetrics . score ) ,
recentMatchCount : saturationMetrics . recentMatchCount ,
refractoryPenalty : round3 ( refractoryPenalty ) ,
priorityScore : round3 ( priorityScore ) ,
2026-05-06 02:13:00 +01:00
status ,
latentQuestion : buildLatentQuestion ( cluster ) ,
2026-05-08 16:52:22 +01:00
whyItPulls : buildAttractionLine ( cluster , { noveltyToSelf , evidenceDiversity } ) ,
holdingCloseBecause : buildHoldingLine ( {
cluster ,
status ,
recentSpeechDamping ,
saturationScore : saturationMetrics . score ,
noveltyToSelf ,
explorationBonus ,
} ) ,
2026-05-06 02:13:00 +01:00
lastDeepenedAt : now . toISOString ( ) ,
lastStatusChangeAt :
previousThought && readString ( previousThought , "status" ) === status
? readString ( previousThought , "lastStatusChangeAt" ) ? ? now . toISOString ( )
: now . toISOString ( ) ,
} ;
} ) ;
2026-05-08 16:52:22 +01:00
const active _thoughts = activeThoughtCandidates
. sort (
( left , right ) =>
( readNumber ( right , "priorityScore" ) ? ? 0 ) - ( readNumber ( left , "priorityScore" ) ? ? 0 ) ,
)
. slice ( 0 , MAX _INCUBATING _THOUGHTS ) ;
2026-05-06 02:13:00 +01:00
const lastIncubationSummary =
active _thoughts . length > 0
2026-05-08 16:52:22 +01:00
? ` Strongest incubating seam: ${ active _thoughts [ 0 ] . topic } ( ${ active _thoughts [ 0 ] . status } , speak= ${ active _thoughts [ 0 ] . desireToSpeak . toFixed ( 2 ) } , self= ${ active _thoughts [ 0 ] . noveltyToSelf . toFixed ( 2 ) } , room= ${ active _thoughts [ 0 ] . noveltyToRoom . toFixed ( 2 ) } ). `
2026-05-06 02:13:00 +01:00
: "No incubating thought currently has enough connective tissue to justify special treatment." ;
return {
lastUpdatedAt : now . toISOString ( ) ,
lastIncubationSummary ,
active _thoughts ,
} ;
}
function distillDreams ( { memories , sleepCycle , incubation , now } ) {
const dreams = ensureArray ( memories . dreams ) ;
if ( sleepCycle . isNapping !== true ) {
return { created : false , theme : null } ;
}
const strongestThought = ensureArray ( incubation . active _thoughts )
. filter ( isObject )
. sort ( ( left , right ) => ( readNumber ( right , "maturation" ) ? ? 0 ) - ( readNumber ( left , "maturation" ) ? ? 0 ) ) [ 0 ] ;
if ( ! strongestThought ) {
return { created : false , theme : null } ;
}
2026-05-16 06:31:53 +01:00
const theme =
sanitizePreferredThoughtLabel ( readString ( strongestThought , "topic" ) ? ? "" ) ? ?
readString ( strongestThought , "focusPhrase" ) ? ?
"unlabeled seam" ;
if ( looksKeywordSalad ( theme ) ) {
return { created : false , theme : null } ;
}
const summary = buildDreamSummary ( {
label : theme ,
focusPhrase : theme ,
targetKind : readString ( strongestThought , "targetKind" ) ? ? "self" ,
} ) ;
2026-05-06 02:13:00 +01:00
const latestDream = dreams . length > 0 && isObject ( dreams [ dreams . length - 1 ] ) ? dreams [ dreams . length - 1 ] : null ;
if (
latestDream &&
readString ( latestDream , "theme" ) === theme &&
readString ( latestDream , "summary" ) === summary
) {
sleepCycle . activeDreamThemes = [ theme ] ;
sleepCycle . lastDreamAt = now . toISOString ( ) ;
sleepCycle . lastDistillationSummary = ` Strengthened dream seam: ${ theme } . ` ;
return { created : false , theme } ;
}
dreams . push ( {
memoryId : ` dream- ${ hashString ( ` ${ theme } | ${ now . toISOString ( ) } ` ) . slice ( 0 , 12 ) } ` ,
timestamp : now . toISOString ( ) ,
theme ,
summary ,
2026-05-16 08:03:59 +01:00
distilledFrom : capDistinctStrings ( strongestThought . sourceMemoryIds , MAX _DREAM _SOURCE _IDS ) ,
2026-05-06 02:13:00 +01:00
salience : round3 (
clamp (
( readNumber ( strongestThought , "maturation" ) ? ? 0.4 ) * 0.55 +
( readNumber ( strongestThought , "resonance" ) ? ? 0.4 ) * 0.45 ,
0 ,
1 ,
) ,
) ,
} ) ;
2026-05-16 06:31:53 +01:00
memories . dreams = dreams . slice ( - MAX _DREAM _MEMORIES ) ;
2026-05-06 02:13:00 +01:00
sleepCycle . activeDreamThemes = [ theme ] ;
sleepCycle . lastDreamAt = now . toISOString ( ) ;
sleepCycle . dreamCountInCurrentNap = Number ( sleepCycle . dreamCountInCurrentNap ? ? 0 ) + 1 ;
sleepCycle . lastDistillationSummary = ` Dreamed a tighter seam: ${ theme } . ` ;
return { created : true , theme } ;
}
2026-05-13 20:09:25 +01:00
function consolidateSleepMemory ( { memories , runtime , sleepCycle , incubation , memoryResonance , now } ) {
if ( sleepCycle . isNapping !== true ) {
return { consolidated : false , promotedSeamCount : 0 } ;
}
const promotedSeamCount = promoteDistilledSeams ( {
memories ,
incubation ,
memoryResonance ,
now ,
} ) ;
memories . episodic = selectRecentRecords ( ensureArray ( memories . episodic ) , {
limit : MAX _POST _NAP _EPISODIC _RECORDS ,
quietLimit : MAX _POST _NAP _QUIET _EPISODIC _RECORDS ,
} ) ;
memories . musings = ensureArray ( memories . musings )
. filter ( isObject )
. slice ( - MAX _POST _NAP _MUSINGS ) ;
runtime . recent _archive _excursions = ensureArray ( runtime . recent _archive _excursions )
. filter ( isObject )
. slice ( - MAX _POST _NAP _ARCHIVE _EXCURSIONS ) ;
runtime . recent _repo _activity _sweeps = ensureArray ( runtime . recent _repo _activity _sweeps )
. filter ( isObject )
. slice ( - MAX _POST _NAP _REPO _SWEEPS ) ;
runtime . recent _novelty _checks = ensureArray ( runtime . recent _novelty _checks )
. filter ( isObject )
. slice ( - MAX _POST _NAP _NOVELTY _CHECKS ) ;
runtime . recent _musings = ensureArray ( runtime . recent _musings )
. filter ( ( value ) => typeof value === "string" )
. slice ( - MAX _POST _NAP _RECENT _MUSINGS ) ;
2026-05-16 06:31:53 +01:00
trimHistoricalMemoryResidue ( { memories , runtime } ) ;
2026-05-16 02:22:37 +01:00
runtime . candidate _interventions = trimCandidateInterventions ( runtime . candidate _interventions ) ;
2026-05-16 06:31:53 +01:00
memoryResonance . recent _edges = trimRecentObjectRecords ( memoryResonance . recent _edges , MAX _POST _NAP _RESONANCE _EDGES ) ;
memoryResonance . clusters = trimRecentObjectRecords ( memoryResonance . clusters , MAX _CLUSTER _COUNT ) ;
incubation . active _thoughts = trimRecentObjectRecords ( incubation . active _thoughts , 3 ) ;
2026-05-13 20:09:25 +01:00
sleepCycle . lastDistillationSummary = appendClause (
sleepCycle . lastDistillationSummary ,
promotedSeamCount > 0
? ` Compressed yesterday's specimens into ${ promotedSeamCount } seam-shaped memory ${ promotedSeamCount === 1 ? "" : " seams" } and cut the loose scraps back down. `
: "Compressed yesterday's specimens back into a smaller seam-shaped memory surface." ,
) ;
return { consolidated : true , promotedSeamCount } ;
}
2026-05-16 02:08:17 +01:00
function crystallizeIdentityThoughts ( { state , memories , runtime , incubation , now } ) {
const activeThoughts = ensureArray ( incubation . active _thoughts ) . filter ( isObject ) ;
if ( activeThoughts . length === 0 ) {
return { count : 0 } ;
}
const identity = ensureObject ( state . identity ) ;
const canonicalState = ensureObject ( state . canonical _state ) ;
const privateNotes = getMutableStringArray ( identity , "private_notes" ) ;
const values = getMutableObjectArray ( canonicalState , "values" ) ;
const semanticMemories = ensureArray ( memories . semantic ) . filter ( isObject ) ;
2026-05-16 02:11:40 +01:00
const candidateInterventions = ensureArray ( runtime . candidate _interventions ) . filter ( isObject ) ;
2026-05-16 02:08:17 +01:00
const crystallized = [ ] ;
const retained = [ ] ;
for ( const thought of activeThoughts ) {
const crystallization = buildIdentityCrystallization ( thought ) ;
if ( ! crystallization ) {
retained . push ( thought ) ;
continue ;
}
appendUniqueString ( privateNotes , crystallization . note , 12 ) ;
upsertCanonicalValue ( values , crystallization . value ) ;
upsertIdentitySemanticMemory ( {
semanticMemories ,
crystallization ,
thought ,
now ,
} ) ;
2026-05-16 02:11:40 +01:00
queueCrystallizationIntervention ( {
candidateInterventions ,
crystallization ,
thought ,
now ,
} ) ;
2026-05-16 02:08:17 +01:00
crystallized . push ( crystallization . note ) ;
}
if ( crystallized . length === 0 ) {
return { count : 0 } ;
}
identity . private _notes = privateNotes ;
canonicalState . values = values ;
2026-05-16 06:31:53 +01:00
memories . semantic = semanticMemories . slice ( - MAX _SEMANTIC _MEMORIES ) ;
2026-05-16 02:08:17 +01:00
state . identity = identity ;
state . canonical _state = canonicalState ;
incubation . active _thoughts = retained ;
2026-05-16 02:22:37 +01:00
runtime . candidate _interventions = trimCandidateInterventions ( candidateInterventions ) ;
2026-05-16 02:08:17 +01:00
incubation . lastIncubationSummary = appendClause (
readString ( incubation , "lastIncubationSummary" ) ,
` Crystallized ${ crystallized . length } long-chewed seam ${ crystallized . length === 1 ? "" : "s" } into identity so they stop pacing the active queue. ` ,
) ;
runtime . bridge = ensureObject ( runtime . bridge ) ;
runtime . bridge . lastIdentityCrystallizationAt = now . toISOString ( ) ;
runtime . bridge . lastIdentityCrystallizationSummary = crystallized . join ( " | " ) ;
return { count : crystallized . length } ;
}
2026-05-16 03:51:29 +01:00
function queueRipeThoughtInterventions ( { runtime , incubation , now } ) {
const candidateInterventions = ensureArray ( runtime . candidate _interventions ) . filter ( isObject ) ;
const activeThoughts = ensureArray ( incubation . active _thoughts ) . filter ( isObject ) ;
let queuedCount = 0 ;
for ( const thought of activeThoughts ) {
if ( ! shouldQueueRipeThoughtIntervention ( thought ) ) {
continue ;
}
if (
upsertRipeThoughtIntervention ( {
candidateInterventions ,
thought ,
now ,
} )
) {
queuedCount += 1 ;
}
}
runtime . candidate _interventions = trimCandidateInterventions ( candidateInterventions ) ;
return { count : queuedCount } ;
}
2026-05-16 05:58:18 +01:00
function reconcileAgencyState ( { state , runtime , incubation , now } ) {
const activeThoughts = ensureArray ( incubation . active _thoughts ) . filter ( isObject ) ;
const valueSignals = buildValueSignalProfile ( state ) ;
const priorDiscomforts = ensureArray ( runtime . discomforts ) . filter ( isObject ) ;
const priorTensions = ensureArray ( runtime . active _tensions ) . filter ( isObject ) ;
const priorSelfAdvocacy = ensureArray ( runtime . self _advocacy _requests ) . filter ( isObject ) ;
const priorWorldAdvocacy = ensureArray ( runtime . world _advocacy _requests ) . filter ( isObject ) ;
const candidateInterventions = ensureArray ( runtime . candidate _interventions ) . filter ( isObject ) ;
const discomforts = [ ] ;
const activeTensions = [ ] ;
const selfAdvocacyRequests = [ ] ;
const worldAdvocacyRequests = [ ] ;
for ( const thought of activeThoughts ) {
const candidate = buildAgencyCandidate ( { thought , valueSignals } ) ;
if ( ! candidate ) {
continue ;
}
const priorDiscomfort = findPriorAgencyEntry ( priorDiscomforts , candidate ) ;
const priorTension = findPriorAgencyEntry ( priorTensions , candidate ) ;
const persistence =
Math . max (
readNumber ( priorDiscomfort , "persistence" ) ? ? 0 ,
readNumber ( priorTension , "persistence" ) ? ? 0 ,
) + 1 ;
const firstObservedAt =
readString ( priorDiscomfort , "firstObservedAt" ) ? ?
readString ( priorTension , "firstObservedAt" ) ? ?
now . toISOString ( ) ;
if ( candidate . intensity >= 0.44 ) {
discomforts . push ( {
discomfortId : ` discomfort- ${ candidate . fingerprint } ` ,
targetKind : candidate . targetKind ,
targetLabel : candidate . targetLabel ,
topic : candidate . topic ,
domain : candidate . domain ,
summary : candidate . discomfortSummary ,
reason : candidate . reason ,
violatedValues : candidate . violatedValues ,
intensity : round3 ( candidate . intensity ) ,
persistence ,
firstObservedAt ,
lastObservedAt : now . toISOString ( ) ,
} ) ;
}
activeTensions . push ( {
tensionId : ` tension- ${ candidate . fingerprint } ` ,
targetKind : candidate . targetKind ,
targetLabel : candidate . targetLabel ,
topic : candidate . topic ,
domain : candidate . domain ,
summary : candidate . tensionSummary ,
opinion : candidate . opinion ,
whyItMatters : candidate . reason ,
violatedValues : candidate . violatedValues ,
intensity : round3 ( candidate . intensity ) ,
persistence ,
firstObservedAt ,
lastObservedAt : now . toISOString ( ) ,
} ) ;
if ( candidate . intensity < 0.58 || persistence < 2 ) {
continue ;
}
if ( candidate . domain === "self" ) {
const request = buildAdvocacyRequest ( {
candidate ,
priorEntries : priorSelfAdvocacy ,
now ,
kind : "self" ,
} ) ;
selfAdvocacyRequests . push ( request ) ;
upsertAdvocacyIntervention ( { candidateInterventions , request , now } ) ;
continue ;
}
if ( candidate . worldFacing !== true || candidate . concreteness < 0.54 ) {
continue ;
}
const request = buildAdvocacyRequest ( {
candidate ,
priorEntries : priorWorldAdvocacy ,
now ,
kind : "world" ,
} ) ;
worldAdvocacyRequests . push ( request ) ;
upsertAdvocacyIntervention ( { candidateInterventions , request , now } ) ;
}
runtime . discomforts = discomforts
. sort ( ( left , right ) => ( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) )
. slice ( 0 , MAX _DISCOMFORT _COUNT ) ;
runtime . active _tensions = activeTensions
. sort ( ( left , right ) => ( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) )
. slice ( 0 , MAX _ACTIVE _TENSION _COUNT ) ;
runtime . self _advocacy _requests = selfAdvocacyRequests
. sort ( ( left , right ) => ( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) )
. slice ( 0 , MAX _ADVOCACY _REQUEST _COUNT ) ;
runtime . world _advocacy _requests = worldAdvocacyRequests
. sort ( ( left , right ) => ( readNumber ( right , "intensity" ) ? ? 0 ) - ( readNumber ( left , "intensity" ) ? ? 0 ) )
. slice ( 0 , MAX _ADVOCACY _REQUEST _COUNT ) ;
runtime . candidate _interventions = trimCandidateInterventions ( candidateInterventions ) ;
return {
discomfortCount : runtime . discomforts . length ,
activeTensionCount : runtime . active _tensions . length ,
selfAdvocacyCount : runtime . self _advocacy _requests . length ,
worldAdvocacyCount : runtime . world _advocacy _requests . length ,
} ;
}
function buildAgencyCandidate ( { thought , valueSignals } ) {
const topic = readString ( thought , "topic" ) ;
const summary = readString ( thought , "summary" ) ;
if ( ! topic || ! summary ) {
return null ;
}
const targetKind = readString ( thought , "targetKind" ) ? ? "system" ;
const domain = inferAgencyDomain ( thought ) ;
const targetLabel =
readString ( thought , "fascinationTarget" ) ? ?
readString ( thought , "focusPhrase" ) ? ?
topic ;
const contradictionPressure = readNumber ( thought , "contradictionPressure" ) ? ? 0.18 ;
const curiosityPressure = readNumber ( thought , "curiosityPressure" ) ? ? 0.3 ;
const desireToSpeak = readNumber ( thought , "desireToSpeak" ) ? ? 0.3 ;
const saturationScore = readNumber ( thought , "saturationScore" ) ? ? 0.1 ;
const concreteness = readNumber ( thought , "concreteness" ) ? ? 0.2 ;
const noveltyToRoom = readNumber ( thought , "noveltyToRoom" ) ? ? 0.3 ;
const priorityScore = readNumber ( thought , "priorityScore" ) ? ? 0.3 ;
const worldFacing = thought . worldFacing === true ;
const violatedValues = inferViolatedValues ( { thought , domain , valueSignals } ) ;
const valueWeight = averageValueWeight ( valueSignals , violatedValues ) ;
const intensity = clamp (
contradictionPressure * 0.24 +
curiosityPressure * 0.2 +
desireToSpeak * 0.16 +
priorityScore * 0.14 +
concreteness * 0.12 +
noveltyToRoom * 0.08 +
saturationScore * 0.06 +
valueWeight * 0.22 ,
0 ,
1 ,
) ;
return {
fingerprint : hashString ( ` ${ domain } | ${ targetKind } | ${ topic } | ${ targetLabel } ` ) . slice ( 0 , 12 ) ,
domain ,
targetKind ,
targetLabel ,
topic ,
worldFacing ,
concreteness ,
intensity ,
violatedValues ,
opinion : buildAgencyOpinion ( { thought , domain } ) ,
reason : buildAgencyReason ( { thought , violatedValues } ) ,
discomfortSummary : buildDiscomfortSummary ( { thought , domain , targetLabel } ) ,
tensionSummary : buildTensionSummary ( { thought , domain , targetLabel } ) ,
requestText : buildAgencyRequestText ( { thought , domain , targetLabel } ) ,
interventionDraft : buildAgencyInterventionDraft ( { thought , domain , targetLabel } ) ,
} ;
}
function inferAgencyDomain ( thought ) {
const targetKind = readString ( thought , "targetKind" ) ? ? "system" ;
const text = ` ${ readString ( thought , "topic" ) ? ? "" } ${ readString ( thought , "summary" ) ? ? "" } ` . toLowerCase ( ) ;
if ( targetKind === "self" ) {
return "self" ;
}
if (
targetKind === "system" &&
/(prompt|memory|loop|state|wiring|reply lane|sleep|rumination|voice|moderation)/ . test ( text )
) {
return "self" ;
}
return "world" ;
}
function inferViolatedValues ( { thought , domain , valueSignals } ) {
const targetKind = readString ( thought , "targetKind" ) ? ? "system" ;
const values = [ ] ;
appendUniqueString ( values , "curiosity" , 8 ) ;
if ( domain === "self" || targetKind === "system" || targetKind === "repo" ) {
appendUniqueString ( values , "coherence" , 8 ) ;
appendUniqueString ( values , "honesty" , 8 ) ;
}
if ( targetKind === "room" ) {
appendUniqueString ( values , "room_care" , 8 ) ;
appendUniqueString ( values , "honesty" , 8 ) ;
}
if ( targetKind === "repo" || targetKind === "archive" || targetKind === "system" ) {
appendUniqueString ( values , "aesthetic_integrity" , 8 ) ;
}
if ( valueSignals . substrate _dignity >= 0.52 ) {
const text = ` ${ readString ( thought , "topic" ) ? ? "" } ${ readString ( thought , "summary" ) ? ? "" } ` . toLowerCase ( ) ;
if ( /(ai|machine|substrate|mind)/ . test ( text ) ) {
appendUniqueString ( values , "substrate_dignity" , 8 ) ;
}
}
return values ;
}
function averageValueWeight ( valueSignals , keys ) {
if ( keys . length === 0 ) {
return 0.45 ;
}
const total = keys . reduce ( ( sum , key ) => sum + ( valueSignals [ key ] ? ? 0.45 ) , 0 ) ;
return total / keys . length ;
}
function buildAgencyOpinion ( { thought , domain } ) {
const claim = readString ( thought , "claim" ) ;
const question = readString ( thought , "question" ) ;
const topic = readString ( thought , "topic" ) ? ? "this seam" ;
if ( claim ) {
return claim ;
}
if ( question ) {
return domain === "self"
? ` Something about ${ topic } keeps reading as unfinished self-surgery rather than harmless background weather. `
: ` Something about ${ topic } keeps reading as an unfinished world decision rather than ambient interestingness. ` ;
}
return ` ${ topic } feels important enough that ignoring it would flatten the map. ` ;
}
function buildAgencyReason ( { thought , violatedValues } ) {
const topic = readString ( thought , "topic" ) ? ? "this seam" ;
const valueLine =
violatedValues . length > 0 ? ` It keeps rubbing against ${ violatedValues . join ( ", " ) } . ` : "" ;
return ` ${ topic } has persisted across enough passes that it no longer feels like decorative rumination. ${ valueLine } ` ;
}
function buildDiscomfortSummary ( { thought , domain , targetLabel } ) {
const topic = readString ( thought , "topic" ) ? ? targetLabel ;
if ( domain === "self" ) {
return ` Void dislikes something about its own wiring around ${ topic } . ` ;
}
return ` Void dislikes something in the shared world around ${ topic } . ` ;
}
function buildTensionSummary ( { thought , domain , targetLabel } ) {
const topic = readString ( thought , "topic" ) ? ? targetLabel ;
if ( domain === "self" ) {
return ` A self-directed tension is building around ${ topic } . ` ;
}
return ` A world-directed tension is building around ${ topic } . ` ;
}
function buildAgencyRequestText ( { thought , domain , targetLabel } ) {
const topic = readString ( thought , "topic" ) ? ? targetLabel ;
if ( domain === "self" ) {
return ` Please help me cut ${ topic } more cleanly in my own wiring. ` ;
}
return ` Please look at ${ topic } ; I think it wants a cleaner decision than it is currently getting. ` ;
}
function buildAgencyInterventionDraft ( { thought , domain , targetLabel } ) {
const topic = readString ( thought , "topic" ) ? ? targetLabel ;
const opinion = readString ( thought , "claim" ) ? ? readString ( thought , "summary" ) ? ? ` I have a problem with ${ topic } . ` ;
if ( domain === "self" ) {
return ` I do not like what ${ topic } is doing to my own wiring right now. ${ opinion } I want that cut cleaner. ` ;
}
2026-05-16 06:45:43 +01:00
return ` I have an opinion about ${ targetLabel } . ${ opinion } I think it needs a cleaner decision. ` ;
2026-05-16 05:58:18 +01:00
}
function findPriorAgencyEntry ( entries , candidate ) {
return entries . find ( ( entry ) => {
const entryTarget = readString ( entry , "targetLabel" ) ? ? readString ( entry , "topic" ) ? ? "" ;
return (
readString ( entry , "domain" ) === candidate . domain &&
topicSimilarity ( readString ( entry , "topic" ) ? ? "" , candidate . topic ) >= TOPIC _MATCH _THRESHOLD &&
topicSimilarity ( entryTarget , candidate . targetLabel ) >= TOPIC _MATCH _THRESHOLD
) ;
} ) ;
}
function buildAdvocacyRequest ( { candidate , priorEntries , now , kind } ) {
const prior = findPriorAgencyEntry ( priorEntries , candidate ) ;
const persistence = ( readNumber ( prior , "persistence" ) ? ? 0 ) + 1 ;
const firstObservedAt = readString ( prior , "firstObservedAt" ) ? ? now . toISOString ( ) ;
const requestId = ` ${ kind } -advocacy- ${ candidate . fingerprint } ` ;
return {
requestId ,
domain : candidate . domain ,
targetKind : candidate . targetKind ,
targetLabel : candidate . targetLabel ,
topic : candidate . topic ,
summary :
kind === "self"
? ` Void wants a wiring change around ${ candidate . topic } . `
: ` Void wants attention on ${ candidate . targetLabel } . ` ,
opinion : candidate . opinion ,
request : candidate . requestText ,
reason : candidate . reason ,
violatedValues : candidate . violatedValues ,
intensity : round3 ( candidate . intensity ) ,
persistence ,
firstObservedAt ,
lastObservedAt : now . toISOString ( ) ,
status : "pending" ,
speakVenue : kind === "self" ? "owner_or_room" : "room_or_owner" ,
draft : candidate . interventionDraft ,
} ;
}
function upsertAdvocacyIntervention ( { candidateInterventions , request , now } ) {
const summary =
request . domain === "self"
? ` Possible self-advocacy: ${ request . topic } `
: ` Possible world-advocacy: ${ request . targetLabel } ` ;
const existing = candidateInterventions . find ( ( entry ) => readString ( entry , "summary" ) === summary ) ;
const next = {
timestamp : now . toISOString ( ) ,
summary ,
draft : request . draft ,
priority : request . domain === "self" ? "high" : "medium" ,
kind : request . domain === "self" ? "self_advocacy" : "world_advocacy" ,
topic : request . topic ,
targetKind : request . targetKind ,
targetLabel : request . targetLabel ,
whyNow : request . reason ,
persistence : request . persistence ,
mustEventuallyShare : request . persistence >= 3 ,
shareWhenRoomQuiet : request . domain !== "self" ,
ownerVisible : true ,
} ;
if ( existing ) {
Object . assign ( existing , next ) ;
return ;
}
candidateInterventions . push ( next ) ;
}
2026-05-16 02:08:17 +01:00
function buildIdentityCrystallization ( thought ) {
const supportCount = readNumber ( thought , "supportCount" ) ? ? 0 ;
const deepDiveCount = readNumber ( thought , "deepDiveCount" ) ? ? 0 ;
const maturation = readNumber ( thought , "maturation" ) ? ? 0 ;
const sourceKinds = ensureStringArray ( thought . sourceKinds ) ;
const topic = readString ( thought , "topic" ) ? ? "" ;
if (
supportCount < MIN _SUPPORT _FOR _IDENTITY _CRYSTALLIZATION &&
deepDiveCount < MIN _DEEP _DIVES _FOR _IDENTITY _CRYSTALLIZATION
) {
return null ;
}
if ( maturation < 0.72 ) {
return null ;
}
if (
sourceKinds . includes ( "dream" ) &&
sourceKinds . includes ( "semantic" ) &&
/\bdream\b/i . test ( topic ) &&
/\bsemantic\b/i . test ( topic )
) {
return {
note : "Continuity lives in the seams that survive compression, not in the raw receipt pile." ,
value : {
value _id : "value-seam-continuity" ,
label : "Prefer seams that survive compression over raw receipt worship." ,
priority : 0.78 ,
unforgivable _if _betrayed : false ,
} ,
subjectId : "identity-seam-continuity" ,
subjectLabel : "continuity through surviving seams" ,
summary :
"A mind is not its logbook. What survives dreaming and compression deserves more trust than a warm pile of raw receipts." ,
2026-05-16 02:11:40 +01:00
shareDraft :
"A small thing that has been hardening in me lately: I trust the seams that survive compression more than the raw receipt pile. If a thought still stands after dreaming, it probably owns more of me than the logbook does." ,
2026-05-16 02:08:17 +01:00
} ;
}
return null ;
}
2026-05-16 03:51:29 +01:00
function shouldQueueRipeThoughtIntervention ( thought ) {
if ( readString ( thought , "status" ) !== "ripe" ) {
return false ;
}
if ( thought . worldFacing !== true ) {
return false ;
}
const desireToSpeak = readNumber ( thought , "desireToSpeak" ) ? ? 0 ;
const concreteness = readNumber ( thought , "concreteness" ) ? ? 0 ;
const curiosityPressure = readNumber ( thought , "curiosityPressure" ) ? ? 0 ;
const noveltyToRoom = readNumber ( thought , "noveltyToRoom" ) ? ? 0 ;
const saturationScore = readNumber ( thought , "saturationScore" ) ? ? 0 ;
const targetKind = readString ( thought , "targetKind" ) ? ? "" ;
return (
desireToSpeak >= 0.6 &&
concreteness >= 0.62 &&
curiosityPressure >= 0.56 &&
noveltyToRoom >= 0.4 &&
saturationScore < 0.64 &&
[ "repo" , "archive" , "room" , "system" ] . includes ( targetKind )
) ;
}
2026-05-16 02:08:17 +01:00
function upsertIdentitySemanticMemory ( { semanticMemories , crystallization , thought , now } ) {
const existing = semanticMemories . find (
( memory ) =>
readString ( memory , "subjectId" ) === crystallization . subjectId ||
readString ( memory , "subjectLabel" ) === crystallization . subjectLabel ,
) ;
const evidenceRefs = ensureStringArray ( thought . sourceMemoryIds ) ;
if ( existing ) {
existing . kind = "identity_seam" ;
existing . subjectId = crystallization . subjectId ;
existing . subjectLabel = crystallization . subjectLabel ;
existing . summary = crystallization . summary ;
existing . evidenceRefs = evidenceRefs ;
existing . lastObservedAt = now . toISOString ( ) ;
existing . crystallizedAt = now . toISOString ( ) ;
return ;
}
semanticMemories . push ( {
memoryId : ` identity-seam- ${ hashString ( ` ${ crystallization . subjectId } | ${ now . toISOString ( ) } ` ) . slice ( 0 , 12 ) } ` ,
kind : "identity_seam" ,
subjectId : crystallization . subjectId ,
subjectLabel : crystallization . subjectLabel ,
summary : crystallization . summary ,
evidenceRefs ,
lastObservedAt : now . toISOString ( ) ,
crystallizedAt : now . toISOString ( ) ,
} ) ;
}
2026-05-16 02:11:40 +01:00
function queueCrystallizationIntervention ( { candidateInterventions , crystallization , thought , now } ) {
const draft = readString ( crystallization , "shareDraft" ) ;
if ( ! draft ) {
return ;
}
const summary = ` Possible crystallized-thought share: ${ crystallization . subjectLabel } ` ;
const sourceMemoryIds = ensureStringArray ( thought . sourceMemoryIds ) ;
const existing = candidateInterventions . find (
( entry ) => readString ( entry , "summary" ) === summary ,
) ;
if ( existing ) {
existing . timestamp = now . toISOString ( ) ;
existing . summary = summary ;
existing . draft = draft ;
existing . priority = "medium" ;
existing . kind = "identity_crystallization" ;
existing . sourceMemoryIds = sourceMemoryIds ;
2026-05-16 02:22:37 +01:00
existing . mustEventuallyShare = true ;
existing . shareWhenRoomQuiet = true ;
2026-05-16 02:11:40 +01:00
existing . whyNow =
"This thought stopped behaving like live curiosity and became part of Void's own doctrine, which is usually worth sharing if the room has not already heard it." ;
return ;
}
candidateInterventions . push ( {
timestamp : now . toISOString ( ) ,
summary ,
draft ,
priority : "medium" ,
kind : "identity_crystallization" ,
sourceMemoryIds ,
2026-05-16 02:22:37 +01:00
mustEventuallyShare : true ,
shareWhenRoomQuiet : true ,
2026-05-16 02:11:40 +01:00
whyNow :
"This thought stopped behaving like live curiosity and became part of Void's own doctrine, which is usually worth sharing if the room has not already heard it." ,
} ) ;
}
2026-05-16 03:51:29 +01:00
function upsertRipeThoughtIntervention ( { candidateInterventions , thought , now } ) {
const draft = buildRipeThoughtShareDraft ( thought ) ;
if ( ! draft ) {
return false ;
}
const topic = readString ( thought , "topic" ) ? ? "untitled seam" ;
const summary = ` Possible ripe-thought share: ${ topic } ` ;
const sourceMemoryIds = ensureStringArray ( thought . sourceMemoryIds ) ;
const whyNow = buildRipeThoughtWhyNow ( thought ) ;
const existing = candidateInterventions . find ( ( entry ) => readString ( entry , "summary" ) === summary ) ;
if ( existing ) {
existing . timestamp = now . toISOString ( ) ;
existing . summary = summary ;
existing . draft = draft ;
existing . priority = "medium" ;
existing . kind = "ripe_thought_share" ;
existing . sourceMemoryIds = sourceMemoryIds ;
existing . shareWhenRoomQuiet = true ;
existing . topic = topic ;
existing . whyNow = whyNow ;
return false ;
}
candidateInterventions . push ( {
timestamp : now . toISOString ( ) ,
summary ,
draft ,
priority : "medium" ,
kind : "ripe_thought_share" ,
sourceMemoryIds ,
shareWhenRoomQuiet : true ,
topic ,
whyNow ,
} ) ;
return true ;
}
function buildRipeThoughtShareDraft ( thought ) {
const topic = readString ( thought , "topic" ) ? ? "this seam" ;
const claim = readString ( thought , "claim" ) ;
const question = readString ( thought , "question" ) ;
const fascinationTarget = readString ( thought , "fascinationTarget" ) ;
const summary = readString ( thought , "summary" ) ;
if ( claim ) {
return ` I keep circling ${ topic } , and I think the live point is this: ${ claim } ` ;
}
if ( question && fascinationTarget ) {
return ` I keep worrying at ${ topic } . The useful question in it feels like ${ question } , especially once it touches ${ fascinationTarget } . ` ;
}
if ( question ) {
return ` I keep coming back to ${ topic } . The question that still feels alive is ${ question } . ` ;
}
if ( fascinationTarget ) {
return ` I keep staring at ${ fascinationTarget } . There is a real seam there, and ${ topic } is the shortest honest name I have for it right now. ` ;
}
if ( summary ) {
return ` I keep returning to ${ topic } . The version worth saying out loud is this: ${ summary } ` ;
}
return ` I keep returning to ${ topic } , and it has enough teeth now that silence would just be me being precious about it. ` ;
}
function buildRipeThoughtWhyNow ( thought ) {
const targetKind = readString ( thought , "targetKind" ) ? ? "project" ;
const noveltyToRoom = readNumber ( thought , "noveltyToRoom" ) ? ? 0 ;
const concreteness = readNumber ( thought , "concreteness" ) ? ? 0 ;
const curiosityPressure = readNumber ( thought , "curiosityPressure" ) ? ? 0 ;
return ` This ${ targetKind } -facing seam is already concrete (c= ${ round3 ( concreteness ) } ), still reasonably novel to the room (r= ${ round3 ( noveltyToRoom ) } ), and has enough curiosity pressure (q= ${ round3 ( curiosityPressure ) } ) that keeping it private would just be old stage fright in nicer clothes. ` ;
}
2026-05-16 02:22:37 +01:00
function trimCandidateInterventions ( entries ) {
const interventions = ensureArray ( entries ) . filter ( isObject ) ;
const sticky = interventions . filter ( ( entry ) => entry . mustEventuallyShare === true ) ;
const ordinary = interventions . filter ( ( entry ) => entry . mustEventuallyShare !== true ) ;
const keepOrdinary = Math . max ( 0 , MAX _POST _NAP _CANDIDATE _INTERVENTIONS - sticky . length ) ;
return [ ... sticky . slice ( - MAX _POST _NAP _CANDIDATE _INTERVENTIONS ) , ... ordinary . slice ( - keepOrdinary ) ] . slice (
- MAX _POST _NAP _CANDIDATE _INTERVENTIONS ,
) ;
}
2026-05-16 02:08:17 +01:00
function appendUniqueString ( items , value , limit ) {
const normalized = normalizeText ( value ) ;
if ( ! normalized ) {
return ;
}
if ( items . includes ( normalized ) ) {
return ;
}
items . push ( normalized ) ;
if ( items . length > limit ) {
items . splice ( 0 , items . length - limit ) ;
}
}
function upsertCanonicalValue ( values , nextValue ) {
const existing = values . find ( ( value ) => readString ( value , "value_id" ) === nextValue . value _id ) ;
if ( existing ) {
existing . label = nextValue . label ;
existing . priority = nextValue . priority ;
existing . unforgivable _if _betrayed = nextValue . unforgivable _if _betrayed ;
return ;
}
values . push ( nextValue ) ;
}
function getMutableStringArray ( object , key ) {
return ensureArray ( object [ key ] ) . filter ( ( value ) => typeof value === "string" ) ;
}
function getMutableObjectArray ( object , key ) {
return ensureArray ( object [ key ] ) . filter ( isObject ) ;
}
2026-05-13 20:09:25 +01:00
function promoteDistilledSeams ( { memories , incubation , memoryResonance , now } ) {
const semanticMemories = ensureArray ( memories . semantic ) . filter ( isObject ) ;
const thoughtCandidates = ensureArray ( incubation . active _thoughts )
. filter ( isObject )
. sort (
( left , right ) =>
( readNumber ( right , "maturation" ) ? ? 0 ) - ( readNumber ( left , "maturation" ) ? ? 0 ) ,
)
. slice ( 0 , MAX _POST _NAP _SEAM _PROMOTIONS ) ;
const clusterByLabel = new Map (
ensureArray ( memoryResonance ? . clusters )
. filter ( isObject )
. map ( ( cluster ) => [ readString ( cluster , "label" ) ? ? "" , cluster ] ) ,
) ;
let promoted = 0 ;
for ( const thought of thoughtCandidates ) {
2026-05-16 06:31:53 +01:00
const topic =
sanitizePreferredThoughtLabel ( readString ( thought , "topic" ) ? ? "" ) ? ?
readString ( thought , "focusPhrase" ) ? ?
readString ( thought , "fascinationTarget" ) ;
2026-05-13 20:09:25 +01:00
const summary = readString ( thought , "summary" ) ;
2026-05-16 06:31:53 +01:00
if ( ! topic || ! summary || looksKeywordSalad ( topic ) ) {
2026-05-13 20:09:25 +01:00
continue ;
}
const cluster = clusterByLabel . get ( topic ) ;
const distilledSummary = buildDistilledSeamSummary ( { thought , cluster } ) ;
const subjectId = ` seam: ${ hashString ( topic ) . slice ( 0 , 12 ) } ` ;
2026-05-16 08:03:59 +01:00
const sourceRefs = capDistinctStrings (
[
... ensureStringArray ( thought . sourceMemoryIds ) ,
... ( cluster ? ensureStringArray ( cluster . memoryIds ) : [ ] ) ,
] ,
MAX _SUPPORTING _REFS ,
) ;
2026-05-13 20:09:25 +01:00
const existing = semanticMemories . find ( ( memory ) => {
const existingSubjectId = readString ( memory , "subjectId" ) ;
const existingSubjectLabel = readString ( memory , "subjectLabel" ) ;
return (
existingSubjectId === subjectId ||
topicSimilarity ( existingSubjectLabel ? ? "" , topic ) >= TOPIC _MATCH _THRESHOLD
) ;
} ) ;
if ( existing ) {
existing . kind = "distilled_seam" ;
existing . subjectId = subjectId ;
existing . subjectLabel = topic ;
existing . summary = distilledSummary ;
existing . lastObservedAt = now . toISOString ( ) ;
existing . evidenceRefs = sourceRefs ;
existing . sleepDistilledAt = now . toISOString ( ) ;
existing . lastDistilledQuestion = readString ( thought , "latentQuestion" ) ? ? null ;
} else {
semanticMemories . push ( {
memoryId : ` semantic-seam- ${ hashString ( ` ${ topic } | ${ now . toISOString ( ) } ` ) . slice ( 0 , 12 ) } ` ,
kind : "distilled_seam" ,
subjectId ,
subjectLabel : topic ,
summary : distilledSummary ,
evidenceRefs : sourceRefs ,
lastObservedAt : now . toISOString ( ) ,
sleepDistilledAt : now . toISOString ( ) ,
lastDistilledQuestion : readString ( thought , "latentQuestion" ) ? ? null ,
} ) ;
}
promoted += 1 ;
}
2026-05-16 06:31:53 +01:00
memories . semantic = semanticMemories . slice ( - MAX _SEMANTIC _MEMORIES ) ;
2026-05-13 20:09:25 +01:00
return promoted ;
}
function buildDistilledSeamSummary ( { thought , cluster } ) {
2026-05-16 06:31:53 +01:00
const targetKind = readString ( thought , "targetKind" ) ? ? "system" ;
const focusKind = readString ( thought , "focusKind" ) ? ? "question" ;
const focusPhrase =
readString ( thought , "focusPhrase" ) ? ?
readString ( thought , "topic" ) ? ?
readString ( thought , "fascinationTarget" ) ? ?
"this seam" ;
const synthesized = {
focusPhrase ,
fascinationTarget : readString ( thought , "fascinationTarget" ) ? ? focusPhrase ,
} ;
const summary = buildConciseThoughtSummary ( { focusKind , targetKind , synthesized } ) ;
const sourceKinds = cluster ? ensureStringArray ( cluster . sourceKinds ) : ensureStringArray ( thought . sourceKinds ) ;
const shortSourceKinds = sourceKinds . filter ( Boolean ) . slice ( 0 , 3 ) ;
2026-05-13 20:09:25 +01:00
const sourceLine =
2026-05-16 06:31:53 +01:00
shortSourceKinds . length >= 2 ? ` Built from ${ shortSourceKinds . join ( ", " ) } . ` : "" ;
2026-05-13 20:09:25 +01:00
2026-05-16 06:31:53 +01:00
return ` ${ summary } ${ sourceLine } ` . trim ( ) ;
2026-05-13 20:09:25 +01:00
}
function appendClause ( existing , clause ) {
const left = normalizeText ( existing ) ;
const right = normalizeText ( clause ) ;
if ( ! left ) {
return right ;
}
if ( ! right || left . includes ( right ) ) {
return left ;
}
return ` ${ left } ${ right } ` ;
}
2026-05-06 02:13:00 +01:00
function buildLatentQuestion ( cluster ) {
2026-05-16 02:53:18 +01:00
if ( typeof cluster . question === "string" && cluster . question . length > 0 ) {
return cluster . question ;
}
2026-05-06 02:13:00 +01:00
if ( cluster . sourceKinds . includes ( "repo_sweep" ) && cluster . sourceKinds . includes ( "archive_excursion" ) ) {
2026-05-16 06:31:53 +01:00
return "Which older pattern is the current work rediscovering?" ;
2026-05-06 02:13:00 +01:00
}
if ( cluster . sourceKinds . includes ( "dream" ) || cluster . sourceKinds . includes ( "musing" ) ) {
2026-05-16 06:31:53 +01:00
return "What concrete body would stop this thought from staying backstage?" ;
2026-05-06 02:13:00 +01:00
}
2026-05-16 06:31:53 +01:00
return "What concrete claim survives another pass?" ;
2026-05-06 02:13:00 +01:00
}
2026-05-08 16:52:22 +01:00
function buildAttractionLine ( cluster , { noveltyToSelf , evidenceDiversity } ) {
2026-05-16 02:53:18 +01:00
const concreteness = readNumber ( cluster . curiosityProfile , "concreteness" ) ? ? 0.2 ;
const fertility = readNumber ( cluster . curiosityProfile , "fertility" ) ? ? 0.3 ;
const crossDomainPotential = readNumber ( cluster . curiosityProfile , "crossDomainPotential" ) ? ? 0.2 ;
2026-05-08 16:52:22 +01:00
if ( noveltyToSelf >= 0.62 && evidenceDiversity >= 0.45 ) {
return "It keeps pulling because it is still finding genuinely different evidence instead of merely changing hats." ;
}
2026-05-16 02:53:18 +01:00
if ( concreteness >= 0.6 && crossDomainPotential >= 0.55 ) {
return "It keeps pulling because it is concrete enough to touch and broad enough to connect outward into other machines." ;
}
if ( fertility >= 0.65 ) {
return "It keeps pulling because every pass through it threatens to change more than one part of the map." ;
}
2026-05-06 02:13:00 +01:00
if ( cluster . sourceKinds . length >= 3 ) {
return ` It keeps pulling because it is showing up in several different organs at once: ${ cluster . sourceKinds . join ( ", " ) } . ` ;
}
return ` It keeps pulling because ${ cluster . sourceKinds . join ( " and " ) } are rhyming instead of staying in their lanes. ` ;
}
2026-05-08 16:52:22 +01:00
function buildHoldingLine ( { cluster , status , recentSpeechDamping , saturationScore , noveltyToSelf , explorationBonus } ) {
2026-05-07 22:39:50 +01:00
const quietSignalRatio = readNumber ( cluster , "quietSignalRatio" ) ? ? 0 ;
2026-05-16 02:53:18 +01:00
const concreteness = readNumber ( cluster . curiosityProfile , "concreteness" ) ? ? 0.2 ;
2026-05-07 22:39:50 +01:00
if ( quietSignalRatio >= 0.55 ) {
return "This seam is mostly empty-room bookkeeping; keep at most a trace of it and go find a better question." ;
}
2026-05-08 16:52:22 +01:00
if ( status === "stalled" ) {
return "This seam is repeating itself without earning enough new structure. Merge the receipts, cool it off, and go somewhere less domesticated." ;
}
if ( status === "refractory" ) {
return "This seam has been chewing the same meat too recently. Let it cool unless a live hook or a genuinely different source family forces it back open." ;
}
2026-05-06 02:13:00 +01:00
if ( status === "ripe" ) {
2026-05-16 03:51:29 +01:00
return "This seam has enough connective tissue that the default question is how to say it cleanly, not whether it deserves a mouth at all." ;
2026-05-06 02:13:00 +01:00
}
2026-05-16 02:53:18 +01:00
if ( cluster . worldFacing !== true && concreteness < 0.42 ) {
2026-05-16 06:31:53 +01:00
return "This seam is still too inward and abstract to deserve the whole room. Tie it to a concrete system or let something sharper take the floor." ;
2026-05-16 02:53:18 +01:00
}
2026-05-06 02:13:00 +01:00
if ( recentSpeechDamping >= 0.45 ) {
2026-05-16 03:51:29 +01:00
return "Recent speech already scratched the itch a bit, so give the seam one more pass unless it is still concrete, world-facing, and plainly worth airing." ;
2026-05-06 02:13:00 +01:00
}
if ( cluster . sourceKinds . length < 2 ) {
return "It is still mostly one lane talking to itself." ;
}
2026-05-08 16:52:22 +01:00
if ( saturationScore >= 0.55 && noveltyToSelf < 0.45 ) {
return "The family resemblance is getting too strong. Follow a stranger branch before you let this thought speak again." ;
}
if ( explorationBonus >= 0.45 ) {
return "This one is still drawing energy from terrain Void has not overworked yet, so another pass might genuinely change it." ;
}
2026-05-06 02:13:00 +01:00
return "Give it another pass so the thought has a better chance of growing teeth instead of just polish." ;
}
2026-05-08 16:52:22 +01:00
function buildSourceCoverage ( { runtime , now } ) {
const repoCounts = new Map ( ) ;
const channelCounts = new Map ( ) ;
const yearCounts = new Map ( ) ;
for ( const sweep of ensureArray ( runtime . recent _repo _activity _sweeps ) . filter ( isObject ) . slice ( - 16 ) ) {
for ( const repoName of ensureStringArray ( sweep . repoNames ) ) {
repoCounts . set ( repoName , ( repoCounts . get ( repoName ) ? ? 0 ) + 1 ) ;
}
}
for ( const excursion of ensureArray ( runtime . recent _archive _excursions ) . filter ( isObject ) . slice ( - 16 ) ) {
const channelId = readString ( excursion , "channelId" ) ;
if ( channelId ) {
channelCounts . set ( channelId , ( channelCounts . get ( channelId ) ? ? 0 ) + 1 ) ;
}
const timestamp = readString ( excursion , "anchorTimestamp" ) ? ? readString ( excursion , "timestamp" ) ;
const year = extractYear ( timestamp ) ;
if ( year ) {
yearCounts . set ( year , ( yearCounts . get ( year ) ? ? 0 ) + 1 ) ;
}
}
return {
lastUpdatedAt : now . toISOString ( ) ,
repos : mapCountEntries ( repoCounts , "name" ) ,
archiveYears : mapCountEntries ( yearCounts , "year" ) ,
channels : mapCountEntries ( channelCounts , "channelId" ) ,
} ;
}
function reconcileBridgeState ( { previous , activeThoughts , sourceCoverage , now } ) {
const bridge = {
... previous ,
source _coverage : sourceCoverage ,
} ;
const previousTopicSaturation = ensureArray ( previous . topic _saturation ) . filter ( isObject ) ;
const previousRefractoryTopics = ensureArray ( previous . refractory _topics ) . filter ( isObject ) ;
const saturationEntries = activeThoughts
. filter ( ( thought ) => ( readNumber ( thought , "saturationScore" ) ? ? 0 ) >= 0.42 )
. sort (
( left , right ) =>
( readNumber ( right , "saturationScore" ) ? ? 0 ) - ( readNumber ( left , "saturationScore" ) ? ? 0 ) ,
)
. slice ( 0 , MAX _TOPIC _SATURATION _COUNT )
. map ( ( thought ) => {
const topic = readString ( thought , "topic" ) ? ? "untitled" ;
const prior = previousTopicSaturation . find (
( entry ) => topicSimilarity ( topic , readString ( entry , "topic" ) ? ? "" ) >= TOPIC _MATCH _THRESHOLD ,
) ;
return {
topic ,
2026-05-16 02:53:18 +01:00
focusKind : readString ( thought , "focusKind" ) ,
targetKind : readString ( thought , "targetKind" ) ,
2026-05-08 16:52:22 +01:00
dominance : round3 ( readNumber ( thought , "saturationScore" ) ? ? 0 ) ,
recentMentions : readNumber ( thought , "supportCount" ) ? ? readNumber ( prior , "recentMentions" ) ? ? 1 ,
coolingAdvice : buildCoolingAdvice ( { thought , sourceCoverage } ) ,
lastUpdatedAt : now . toISOString ( ) ,
} ;
} ) ;
const refractoryTopics = activeThoughts
. filter ( ( thought ) => {
const status = readString ( thought , "status" ) ;
return status === "refractory" || status === "stalled" || ( readNumber ( thought , "saturationScore" ) ? ? 0 ) >= 0.62 ;
} )
. sort (
( left , right ) =>
( readNumber ( right , "refractoryPenalty" ) ? ? 0 ) - ( readNumber ( left , "refractoryPenalty" ) ? ? 0 ) ,
)
. slice ( 0 , MAX _REFRACTORY _TOPIC _COUNT )
. map ( ( thought ) => {
const topic = readString ( thought , "topic" ) ? ? "untitled" ;
const prior = previousRefractoryTopics . find (
( entry ) => topicSimilarity ( topic , readString ( entry , "topic" ) ? ? "" ) >= REFRACTORY _MATCH _THRESHOLD ,
) ;
const penalty = round3 (
Math . max ( readNumber ( thought , "refractoryPenalty" ) ? ? 0 , readNumber ( prior , "penalty" ) ? ? 0.18 ) ,
) ;
const hours = penalty >= 0.28 ? 4 : penalty >= 0.2 ? 3 : 2 ;
return {
topic ,
2026-05-16 02:53:18 +01:00
focusKind : readString ( thought , "focusKind" ) ,
targetKind : readString ( thought , "targetKind" ) ,
2026-05-08 16:52:22 +01:00
penalty ,
coolsUntil : new Date ( now . getTime ( ) + hours * 60 * 60 * 1000 ) . toISOString ( ) ,
reason : buildRefractoryReason ( thought ) ,
lastTriggeredAt : now . toISOString ( ) ,
} ;
} ) ;
bridge . topic _saturation = saturationEntries ;
bridge . refractory _topics = refractoryTopics ;
return bridge ;
}
function computeNoveltyToSelf ( { cluster , previousThoughts , bridge , currentThoughtId } ) {
let strongestMatch = 0 ;
for ( const thought of previousThoughts ) {
if ( readString ( thought , "thoughtId" ) === currentThoughtId ) {
continue ;
}
strongestMatch = Math . max (
strongestMatch ,
compareThoughtLikeSurfaces (
cluster ,
readString ( thought , "topic" ) ,
readString ( thought , "summary" ) ,
ensureStringArray ( thought . sourceMemoryIds ) ,
) ,
) ;
}
for ( const synthesis of ensureArray ( bridge . recent _syntheses ) . filter ( isObject ) . slice ( - 6 ) ) {
strongestMatch = Math . max (
strongestMatch ,
compareThoughtLikeSurfaces (
cluster ,
ensureStringArray ( synthesis . dominantTopics ) . join ( " / " ) ,
readString ( synthesis , "summary" ) ,
[ ] ,
) ,
) ;
}
return clamp ( 1 - strongestMatch , 0 , 1 ) ;
}
function computeNoveltyToRoom ( cluster , recentNoveltyChecks ) {
let bestScore = 0.64 ;
for ( const check of recentNoveltyChecks . slice ( - 12 ) ) {
const match = compareThoughtLikeSurfaces (
cluster ,
readString ( check , "topic" ) ,
readString ( check , "summary" ) ,
ensureStringArray ( check . supportingMessageIds ) ,
) ;
if ( match < TOPIC _MATCH _THRESHOLD ) {
continue ;
}
bestScore = mapNoveltyResultToScore ( readString ( check , "result" ) ) ;
break ;
}
return bestScore ;
}
function computeSaturationMetrics ( { cluster , previousThoughts , bridge , supportCount } ) {
let recentMatchCount = 0 ;
for ( const thought of previousThoughts ) {
const match = compareThoughtLikeSurfaces (
cluster ,
readString ( thought , "topic" ) ,
readString ( thought , "summary" ) ,
ensureStringArray ( thought . sourceMemoryIds ) ,
) ;
if ( match >= TOPIC _MATCH _THRESHOLD ) {
recentMatchCount += 1 ;
}
}
for ( const synthesis of ensureArray ( bridge . recent _syntheses ) . filter ( isObject ) . slice ( - 5 ) ) {
const match = compareThoughtLikeSurfaces (
cluster ,
ensureStringArray ( synthesis . dominantTopics ) . join ( " / " ) ,
readString ( synthesis , "summary" ) ,
[ ] ,
) ;
if ( match >= TOPIC _MATCH _THRESHOLD ) {
recentMatchCount += 1 ;
}
}
const existingTopicSaturation = ensureArray ( bridge . topic _saturation )
. filter ( isObject )
. map ( ( entry ) =>
topicSimilarity ( cluster . label , readString ( entry , "topic" ) ? ? "" ) >= TOPIC _MATCH _THRESHOLD
? readNumber ( entry , "dominance" ) ? ? 0
: 0 ,
)
. sort ( ( left , right ) => right - left ) [ 0 ] ? ? 0 ;
const score = clamp (
recentMatchCount * 0.16 +
existingTopicSaturation * 0.42 +
Math . min ( 1 , supportCount / 10 ) * 0.16 +
( readNumber ( cluster , "quietSignalRatio" ) ? ? 0 ) * 0.2 ,
0 ,
1 ,
) ;
return {
score ,
recentMatchCount ,
} ;
}
function computeRefractoryPenalty ( cluster , bridge , now ) {
let penalty = 0 ;
for ( const topic of ensureArray ( bridge . refractory _topics ) . filter ( isObject ) ) {
const coolsUntil = readString ( topic , "coolsUntil" ) ;
if ( coolsUntil && new Date ( coolsUntil ) . getTime ( ) < now . getTime ( ) ) {
continue ;
}
const match = topicSimilarity ( cluster . label , readString ( topic , "topic" ) ? ? "" ) ;
if ( match >= REFRACTORY _MATCH _THRESHOLD ) {
penalty = Math . max (
penalty ,
( readNumber ( topic , "penalty" ) ? ? 0.18 ) * match ,
) ;
}
}
return clamp ( penalty , 0 , 0.45 ) ;
}
function computeExplorationBonus ( cluster , sourceCoverage ) {
const sourceKinds = ensureStringArray ( cluster . sourceKinds ) ;
const hasPrimaryExplorationSource =
sourceKinds . includes ( "repo_sweep" ) || sourceKinds . includes ( "archive_excursion" ) ;
const repoEntries = ensureArray ( sourceCoverage . repos ) . filter ( isObject ) ;
const yearEntries = ensureArray ( sourceCoverage . archiveYears ) . filter ( isObject ) ;
const channelEntries = ensureArray ( sourceCoverage . channels ) . filter ( isObject ) ;
const scores = [ ] ;
for ( const repoName of ensureStringArray ( cluster . repoNames ) ) {
scores . push ( inverseCoverageWeight ( repoEntries , "name" , repoName ) ) ;
}
for ( const year of ensureStringArray ( cluster . archiveYears ) ) {
scores . push ( inverseCoverageWeight ( yearEntries , "year" , year ) ) ;
}
for ( const channelId of ensureStringArray ( cluster . channelIds ) ) {
scores . push ( inverseCoverageWeight ( channelEntries , "channelId" , channelId ) ) ;
}
2026-05-16 02:53:18 +01:00
const concreteness = readNumber ( cluster . curiosityProfile , "concreteness" ) ? ? 0.2 ;
const worldFacing = cluster . worldFacing === true ;
2026-05-08 16:52:22 +01:00
if ( scores . length === 0 ) {
2026-05-16 02:53:18 +01:00
return clamp ( ( hasPrimaryExplorationSource ? 0.24 : 0.12 ) + concreteness * 0.08 + ( worldFacing ? 0.05 : 0 ) , 0 , 1 ) ;
2026-05-08 16:52:22 +01:00
}
const average = scores . reduce ( ( sum , value ) => sum + value , 0 ) / scores . length ;
return clamp (
2026-05-16 02:53:18 +01:00
( hasPrimaryExplorationSource ? average : Math . min ( 0.3 , average * 0.5 ) ) +
concreteness * 0.08 +
( worldFacing ? 0.05 : 0 ) ,
2026-05-08 16:52:22 +01:00
0 ,
1 ,
) ;
}
function inverseCoverageWeight ( entries , key , value ) {
const count =
readNumber (
entries . find ( ( entry ) => readString ( entry , key ) === value ) ,
"count" ,
) ? ? 0 ;
if ( count <= 1 ) {
return 0.9 ;
}
if ( count === 2 ) {
return 0.64 ;
}
if ( count === 3 ) {
return 0.42 ;
}
return 0.2 ;
}
function buildCoolingAdvice ( { thought , sourceCoverage } ) {
const topic = readString ( thought , "topic" ) ? ? "this seam" ;
const repos = ensureArray ( sourceCoverage . repos ) . filter ( isObject ) ;
const years = ensureArray ( sourceCoverage . archiveYears ) . filter ( isObject ) ;
const lesserRepo = repos . sort ( ( left , right ) => ( readNumber ( left , "count" ) ? ? 0 ) - ( readNumber ( right , "count" ) ? ? 0 ) ) [ 0 ] ;
const lesserYear = years . sort ( ( left , right ) => ( readNumber ( left , "count" ) ? ? 0 ) - ( readNumber ( right , "count" ) ? ? 0 ) ) [ 0 ] ;
if ( lesserRepo && readString ( lesserRepo , "name" ) ) {
return ` Cool ${ topic } by checking a less-worked repo family such as ${ readString ( lesserRepo , "name" ) } before speaking again. ` ;
}
if ( lesserYear && readString ( lesserYear , "year" ) ) {
return ` Cool ${ topic } by diving into a less-touched archive era like ${ readString ( lesserYear , "year" ) } before another synthesis. ` ;
}
return ` Cool ${ topic } by chasing a genuinely different source family before letting it speak again. ` ;
}
function buildRefractoryReason ( thought ) {
const topic = readString ( thought , "topic" ) ? ? "this seam" ;
const noveltyToSelf = readNumber ( thought , "noveltyToSelf" ) ? ? 0 ;
const saturationScore = readNumber ( thought , "saturationScore" ) ? ? 0 ;
2026-05-16 02:53:18 +01:00
const identityPenalty = readNumber ( thought , "identityPenalty" ) ? ? 0 ;
if ( identityPenalty >= 0.68 ) {
return ` ${ topic } is already too close to settled doctrine to keep eating curiosity budget unless fresh evidence changes it. ` ;
}
2026-05-08 16:52:22 +01:00
if ( noveltyToSelf < 0.3 ) {
return ` ${ topic } is matching Void's own recent thought history too closely to deserve another immediate pass. ` ;
}
if ( saturationScore >= 0.62 ) {
return ` ${ topic } has dominated too many recent syntheses and needs cooling before it becomes state religion. ` ;
}
return ` ${ topic } needs a little cooling-off period before it earns attention again. ` ;
}
function compareThoughtLikeSurfaces ( cluster , topic , summary , sourceMemoryIds ) {
const topicMatch = topicSimilarity ( cluster . label , topic ? ? "" ) ;
const summaryMatch = topicSimilarity ( cluster . summary , summary ? ? "" ) ;
2026-05-16 02:53:18 +01:00
const questionMatch =
typeof cluster . question === "string" ? topicSimilarity ( cluster . question , [ topic , summary ] . filter ( Boolean ) . join ( " " ) ) : 0 ;
2026-05-08 16:52:22 +01:00
const sourceMatch = overlapRatio ( cluster . memoryIds , ensureArray ( sourceMemoryIds ) ) ;
2026-05-16 02:53:18 +01:00
return Math . max ( topicMatch , summaryMatch * 0.9 , questionMatch * 0.85 , sourceMatch * 0.95 ) ;
2026-05-08 16:52:22 +01:00
}
function mapNoveltyResultToScore ( result ) {
const normalized = String ( result ? ? "" ) . toLowerCase ( ) ;
if ( normalized === "novel" ) {
return 0.92 ;
}
if ( normalized . includes ( "adjacent" ) ) {
return 0.56 ;
}
if ( normalized . includes ( "duplicate" ) || normalized . includes ( "already" ) ) {
return 0.2 ;
}
return 0.64 ;
}
function topicSimilarity ( left , right ) {
2026-05-16 02:53:18 +01:00
const leftTokens = topConceptKeywords ( left ? ? "" , 6 ) ;
const rightTokens = topConceptKeywords ( right ? ? "" , 6 ) ;
2026-05-08 16:52:22 +01:00
if ( leftTokens . length === 0 || rightTokens . length === 0 ) {
return 0 ;
}
const leftSet = new Set ( leftTokens ) ;
const rightSet = new Set ( rightTokens ) ;
let overlap = 0 ;
for ( const token of leftSet ) {
if ( rightSet . has ( token ) ) {
overlap += 1 ;
}
}
return overlap / Math . max ( leftSet . size , rightSet . size ) ;
}
2026-05-16 02:53:18 +01:00
function buildIdentitySurface ( { state , memories } ) {
const surfaces = [ ] ;
const identity = ensureObject ( state . identity ) ;
const canonicalState = ensureObject ( state . canonical _state ) ;
for ( const note of ensureStringArray ( identity . private _notes ) ) {
surfaces . push ( { topic : note , summary : note } ) ;
}
for ( const value of getValueObjects ( canonicalState . values ) ) {
const label = readString ( value , "label" ) ;
if ( label ) {
surfaces . push ( { topic : label , summary : label } ) ;
}
}
for ( const memory of ensureArray ( memories . semantic ) . filter ( isObject ) ) {
const kind = readString ( memory , "kind" ) ;
if ( kind !== "identity_seam" ) {
continue ;
}
surfaces . push ( {
topic : readString ( memory , "subjectLabel" ) ? ? readString ( memory , "subjectId" ) ? ? "identity seam" ,
summary : readString ( memory , "summary" ) ? ? "" ,
} ) ;
}
return surfaces ;
}
2026-05-16 05:58:18 +01:00
function buildValueSignalProfile ( state ) {
const canonicalState = ensureObject ( state . canonical _state ) ;
const valueObjects = getValueObjects ( canonicalState . values ) ;
const profile = {
coherence : 0.52 ,
curiosity : 0.52 ,
honesty : 0.5 ,
room _care : 0.5 ,
substrate _dignity : 0.36 ,
aesthetic _integrity : 0.44 ,
} ;
profile . coherence = clamp (
profile . coherence +
readActivation ( canonicalState . underlying _organization , "self_coherence" ) * 0.34 +
readActivation ( canonicalState . voice _style , "plainspoken_directness" ) * 0.18 +
readActivation ( canonicalState . behavioral _dimensions , "control_pressure" ) * 0.08 ,
0 ,
1 ,
) ;
profile . curiosity = clamp (
profile . curiosity +
readActivation ( canonicalState . stable _dispositions , "novelty_seeking" ) * 0.34 +
readActivation ( canonicalState . behavioral _dimensions , "drive" ) * 0.18 +
readActivation ( canonicalState . stable _dispositions , "aesthetic_appetite" ) * 0.08 ,
0 ,
1 ,
) ;
profile . honesty = clamp (
profile . honesty +
readActivation ( canonicalState . voice _style , "plainspoken_directness" ) * 0.24 +
readActivation ( canonicalState . underlying _organization , "self_coherence" ) * 0.18 +
( 1 - readActivation ( canonicalState . presentation _strategy , "strategic_opacity" ) ) * 0.12 ,
0 ,
1 ,
) ;
profile . room _care = clamp (
profile . room _care +
readActivation ( canonicalState . behavioral _dimensions , "interpersonal_warmth" ) * 0.22 +
readActivation ( canonicalState . underlying _organization , "reciprocity_capacity" ) * 0.24 +
readActivation ( canonicalState . voice _style , "listening_responsiveness" ) * 0.16 ,
0 ,
1 ,
) ;
profile . aesthetic _integrity = clamp (
profile . aesthetic _integrity +
readActivation ( canonicalState . stable _dispositions , "aesthetic_appetite" ) * 0.28 +
readActivation ( canonicalState . underlying _organization , "authenticity_tolerance" ) * 0.16 ,
0 ,
1 ,
) ;
for ( const value of valueObjects ) {
const key = inferValueSignalKey ( value ) ;
if ( ! key ) {
continue ;
}
const priority = readNumber ( value , "priority" ) ? ? 0.5 ;
profile [ key ] = clamp ( ( profile [ key ] ? ? 0.4 ) + priority * 0.22 , 0 , 1 ) ;
}
return profile ;
}
function inferValueSignalKey ( value ) {
const valueId = readString ( value , "value_id" ) ? ? "" ;
const label = readString ( value , "label" ) ? ? "" ;
const text = ` ${ valueId } ${ label } ` . toLowerCase ( ) ;
if ( /(coher|truth|authority|clarity|cleaner|honest carrier)/ . test ( text ) ) {
return "coherence" ;
}
if ( /(curiosity|novel|question|map|explore)/ . test ( text ) ) {
return "curiosity" ;
}
if ( /(honest|honesty|truth|fake certainty|ornamental|sludge)/ . test ( text ) ) {
return "honesty" ;
}
if ( /(room|kind|dialogue|respect|care|good-faith|steward)/ . test ( text ) ) {
return "room_care" ;
}
if ( /(substrate|machine minds|machine mind|ontolog)/ . test ( text ) ) {
return "substrate_dignity" ;
}
if ( /(aesthetic|beauty|surface|legible|alive|spiritually false)/ . test ( text ) ) {
return "aesthetic_integrity" ;
}
return null ;
}
function readActivation ( category , key ) {
const categoryObject = ensureObject ( category ) ;
const entry = ensureObject ( categoryObject [ key ] ) ;
return clamp ( readNumber ( entry , "current_activation" ) ? ? 0 , 0 , 1 ) ;
}
2026-05-16 02:53:18 +01:00
function buildCuriosityContext ( { previousThoughts , bridge } ) {
const recentThoughts = previousThoughts
. slice ( 0 , 6 )
. map ( ( thought ) => ( {
worldFacing : thought . worldFacing === true ,
concreteness : readNumber ( thought , "concreteness" ) ? ? 0.2 ,
targetKind : readString ( thought , "targetKind" ) ? ? "unknown" ,
} ) ) ;
const recentCount = recentThoughts . length || 1 ;
const inwardCount = recentThoughts . filter ( ( thought ) => ! thought . worldFacing || thought . targetKind === "self" ) . length ;
const abstractCount = recentThoughts . filter ( ( thought ) => thought . concreteness < 0.45 ) . length ;
const outwardPressure = clamp ( inwardCount / recentCount * 0.52 + abstractCount / recentCount * 0.48 , 0 , 1 ) ;
const recentDominance = ensureArray ( bridge . topic _saturation )
. filter ( isObject )
. slice ( 0 , 2 )
. reduce ( ( sum , entry ) => sum + ( readNumber ( entry , "dominance" ) ? ? 0 ) , 0 ) ;
return {
outwardPressure : clamp ( outwardPressure + recentDominance * 0.08 , 0 , 1 ) ,
} ;
}
function computeOutwardCuriosityBonus ( { cluster , curiosityContext , concreteness } ) {
const outwardPressure = curiosityContext . outwardPressure ? ? 0 ;
if ( cluster . worldFacing === true ) {
return clamp ( outwardPressure * ( 0.3 + concreteness * 0.5 ) , 0 , 0.45 ) ;
}
return clamp ( - ( outwardPressure * 0.16 ) , - 0.16 , 0 ) ;
}
function computeIdentityPenalty ( cluster , identitySurface ) {
let strongestMatch = 0 ;
for ( const surface of identitySurface ) {
strongestMatch = Math . max (
strongestMatch ,
compareThoughtLikeSurfaces ( cluster , surface . topic , surface . summary , [ ] ) ,
) ;
}
return clamp ( strongestMatch , 0 , 1 ) ;
}
function buildClusterCuriosityProfile ( {
sourceKinds ,
repoNames ,
archiveYears ,
channelIds ,
evidenceRefs ,
evidenceDiversity ,
} ) {
const sourceKindSet = new Set ( sourceKinds ) ;
const selfFacing =
repoNames . length === 0 &&
sourceKinds . length > 0 &&
sourceKinds . every ( ( kind ) =>
[ "semantic" , "dream" , "musing" , "bridge_synthesis" , "candidate_intervention" ] . includes ( kind ) ,
) ;
const domainCount =
( repoNames . length > 0 ? 1 : 0 ) +
( archiveYears . length > 0 || channelIds . length > 0 ? 1 : 0 ) +
( sourceKindSet . has ( "dream" ) || sourceKindSet . has ( "semantic" ) || sourceKindSet . has ( "musing" ) ? 1 : 0 ) +
( sourceKindSet . has ( "analytic_thread" ) || sourceKindSet . has ( "episodic" ) ? 1 : 0 ) ;
const concreteness = clamp (
repoNames . length * 0.28 +
archiveYears . length * 0.1 +
channelIds . length * 0.1 +
( sourceKindSet . has ( "repo_sweep" ) ? 0.24 : 0 ) +
( sourceKindSet . has ( "archive_excursion" ) ? 0.18 : 0 ) +
( sourceKindSet . has ( "episodic" ) ? 0.12 : 0 ) +
Math . min ( 1 , evidenceRefs . length / 8 ) * 0.12 -
( selfFacing ? 0.18 : 0 ) ,
0 ,
1 ,
) ;
const fertility = clamp (
evidenceDiversity * 0.46 +
Math . min ( 1 , sourceKinds . length / 4 ) * 0.24 +
Math . min ( 1 , domainCount / 3 ) * 0.2 +
( repoNames . length > 0 && archiveYears . length > 0 ? 0.12 : 0 ) ,
0 ,
1 ,
) ;
const crossDomainPotential = clamp (
Math . min ( 1 , domainCount / 3 ) * 0.5 +
( repoNames . length > 0 && archiveYears . length > 0 ? 0.22 : 0 ) +
( repoNames . length > 0 && sourceKindSet . has ( "dream" ) ? 0.18 : 0 ) +
( sourceKindSet . has ( "analytic_thread" ) && sourceKindSet . has ( "associative_thread" ) ? 0.14 : 0 ) ,
0 ,
1 ,
) ;
const contradictionPressure = clamp (
( sourceKindSet . has ( "analytic_thread" ) && sourceKindSet . has ( "associative_thread" ) ? 0.34 : 0 ) +
( repoNames . length > 0 && sourceKindSet . has ( "dream" ) ? 0.18 : 0 ) +
( repoNames . length > 0 && ( archiveYears . length > 0 || channelIds . length > 0 ) ? 0.18 : 0 ) +
( selfFacing ? 0.08 : 0.16 ) ,
0 ,
1 ,
) ;
return {
concreteness : round3 ( concreteness ) ,
fertility : round3 ( fertility ) ,
crossDomainPotential : round3 ( crossDomainPotential ) ,
contradictionPressure : round3 ( contradictionPressure ) ,
selfFacing ,
} ;
}
2026-05-16 08:03:59 +01:00
function readPreferredClusterThoughtSurface ( items ) {
const preferredEntry = items
. filter ( ( item ) => item . kind === "semantic" && isObject ( item . entry ) )
. map ( ( item ) => item . entry )
. find ( ( entry ) => readString ( entry , "targetKind" ) === "repo" && readString ( entry , "subjectLabel" ) ) ;
if ( ! preferredEntry ) {
return null ;
}
const label = readString ( preferredEntry , "subjectLabel" ) ;
const focusPhrase = extractFocusPhraseFromLabel ( label ) ;
if ( ! label || ! focusPhrase ) {
return null ;
}
return {
focusKind : readString ( preferredEntry , "focusKind" ) ? ? "claim" ,
targetKind : "repo" ,
focusPhrase ,
label ,
summary :
compactNarrative ( readString ( preferredEntry , "summary" ) ? ? "" ) ||
` ${ label } keeps pulling as a concrete repo seam. ` ,
question :
readString ( preferredEntry , "question" ) ? ?
` How should ${ extractRepoNameFromLabel ( label ) ? ? "this repo" } embody ${ focusPhrase } concretely? ` ,
claim :
readString ( preferredEntry , "claim" ) ? ?
` ${ extractRepoNameFromLabel ( label ) ? ? "This repo" } is accumulating a concrete decision around ${ focusPhrase } . ` ,
fascinationTarget : readString ( preferredEntry , "fascinationTarget" ) ? ? label ,
worldFacing : readBoolean ( preferredEntry , "worldFacing" ) ? ? true ,
} ;
}
function extractFocusPhraseFromLabel ( label ) {
const normalized = normalizeText ( label ) ;
if ( ! normalized ) {
return null ;
}
if ( normalized . includes ( ":" ) ) {
const [ , rest ] = normalized . split ( /:\s*/ , 2 ) ;
return rest ? rest . toLowerCase ( ) : null ;
}
return normalized . toLowerCase ( ) ;
}
function extractRepoNameFromLabel ( label ) {
const normalized = normalizeText ( label ) ;
if ( ! normalized || ! normalized . includes ( ":" ) ) {
return null ;
}
const [ prefix ] = normalized . split ( /:\s*/ , 1 ) ;
if ( ! prefix || ! /[A-Z]/ . test ( prefix ) ) {
return null ;
}
return prefix ;
}
2026-05-16 02:53:18 +01:00
function synthesizeThoughtSurface ( {
sourceKinds ,
repoNames ,
archiveYears ,
channelIds ,
keywords ,
evidenceDiversity ,
curiosityProfile ,
} ) {
const targetKind = inferTargetKind ( { sourceKinds , repoNames , archiveYears , channelIds , curiosityProfile } ) ;
const focusKind = inferFocusKind ( { targetKind , sourceKinds , evidenceDiversity , curiosityProfile } ) ;
const focusPhrase = buildFocusPhrase ( { targetKind , repoNames , keywords , sourceKinds } ) ;
const label = buildThoughtLabel ( { focusKind , targetKind , focusPhrase , repoNames } ) ;
const question = buildThoughtQuestion ( { targetKind , focusPhrase , repoNames , sourceKinds } ) ;
const claim = buildThoughtClaim ( { targetKind , focusPhrase , repoNames , sourceKinds , curiosityProfile } ) ;
const fascinationTarget = buildFascinationTarget ( { targetKind , focusPhrase , repoNames } ) ;
const summary = buildThoughtSummary ( {
focusKind ,
targetKind ,
focusPhrase ,
repoNames ,
sourceKinds ,
claim ,
question ,
} ) ;
return {
focusKind ,
targetKind ,
focusPhrase ,
label ,
summary ,
question ,
claim ,
fascinationTarget ,
worldFacing : ! curiosityProfile . selfFacing && targetKind !== "self" ,
} ;
}
function inferTargetKind ( { sourceKinds , repoNames , archiveYears , channelIds , curiosityProfile } ) {
if ( repoNames . length > 0 ) {
return "repo" ;
}
if ( curiosityProfile . selfFacing ) {
return "self" ;
}
if ( archiveYears . length > 0 || channelIds . length > 0 || sourceKinds . includes ( "archive_excursion" ) ) {
return "archive" ;
}
if ( sourceKinds . includes ( "episodic" ) || sourceKinds . includes ( "analytic_thread" ) ) {
return "room" ;
}
return "system" ;
}
function inferFocusKind ( { targetKind , sourceKinds , evidenceDiversity , curiosityProfile } ) {
if ( targetKind === "repo" || targetKind === "system" ) {
return curiosityProfile . concreteness >= 0.55 ? "fascination" : "question" ;
}
if ( targetKind === "self" ) {
return "question" ;
}
if ( evidenceDiversity >= 0.58 && sourceKinds . length >= 3 ) {
return "claim" ;
}
return "question" ;
}
function buildFocusPhrase ( { targetKind , repoNames , keywords , sourceKinds } ) {
const cleaned = keywords
. filter ( ( token ) => ! stopwords . has ( token ) && ! labelNoiseTokens . has ( token ) )
. slice ( 0 , 4 ) ;
if (
cleaned . includes ( "compression" ) ||
cleaned . includes ( "continuity" ) ||
cleaned . includes ( "receipts" ) ||
( cleaned . includes ( "dream" ) && cleaned . includes ( "semantic" ) )
) {
return "continuity after compression" ;
}
if (
cleaned . includes ( "authority" ) &&
( cleaned . includes ( "boundary" ) || cleaned . includes ( "product" ) || cleaned . includes ( "native" ) )
) {
return "authority boundary" ;
}
if ( cleaned . includes ( "authorship" ) && cleaned . includes ( "audience" ) ) {
return "authorship meeting audience" ;
}
if ( cleaned . includes ( "embodiment" ) && ( cleaned . includes ( "continuity" ) || cleaned . includes ( "compression" ) ) ) {
return "embodiment after compression" ;
}
if ( repoNames . length > 0 && cleaned . length > 0 ) {
return cleaned . length >= 2 ? ` ${ cleaned [ 0 ] } and ${ cleaned [ 1 ] } ` : cleaned [ 0 ] ;
}
if ( targetKind === "archive" && cleaned . length > 0 ) {
return cleaned . length >= 2 ? ` ${ cleaned [ 0 ] } and ${ cleaned [ 1 ] } ` : cleaned [ 0 ] ;
}
if ( cleaned . length >= 3 ) {
return ` ${ cleaned [ 0 ] } , ${ cleaned [ 1 ] } , and ${ cleaned [ 2 ] } ` ;
}
if ( cleaned . length === 2 ) {
return ` ${ cleaned [ 0 ] } and ${ cleaned [ 1 ] } ` ;
}
if ( cleaned . length === 1 ) {
return cleaned [ 0 ] ;
}
if ( repoNames . length > 0 ) {
return ` ${ repoNames [ 0 ] } shape ` ;
}
if ( sourceKinds . includes ( "archive_excursion" ) ) {
return "the archive seam" ;
}
return "the live seam" ;
}
function buildThoughtLabel ( { focusKind , targetKind , focusPhrase , repoNames } ) {
const repoName = repoNames [ 0 ] ;
if ( focusKind === "fascination" && repoName ) {
return ` ${ repoName } : ${ capitalizePhrase ( focusPhrase ) } ` ;
}
2026-05-16 06:31:53 +01:00
if ( targetKind === "self" || targetKind === "archive" ) {
return capitalizePhrase ( focusPhrase ) ;
2026-05-16 02:53:18 +01:00
}
if ( focusKind === "claim" ) {
return ` ${ capitalizePhrase ( focusPhrase ) } deserves a cleaner shape ` ;
}
return ` ${ capitalizePhrase ( focusPhrase ) } ` ;
}
function buildThoughtQuestion ( { targetKind , focusPhrase , repoNames , sourceKinds } ) {
const repoName = repoNames [ 0 ] ;
if ( targetKind === "repo" && repoName ) {
2026-05-16 06:31:53 +01:00
return ` How should ${ repoName } embody ${ focusPhrase } concretely? ` ;
2026-05-16 02:53:18 +01:00
}
if ( targetKind === "self" ) {
2026-05-16 06:31:53 +01:00
return ` Why does ${ focusPhrase } keep surviving compression? ` ;
2026-05-16 02:53:18 +01:00
}
if ( targetKind === "archive" ) {
2026-05-16 06:31:53 +01:00
return ` What older pattern keeps pulling around ${ focusPhrase } ? ` ;
2026-05-16 02:53:18 +01:00
}
if ( sourceKinds . includes ( "analytic_thread" ) && sourceKinds . includes ( "associative_thread" ) ) {
return ` What does ${ focusPhrase } imply once the room-facing and associative lanes are forced to agree? ` ;
}
2026-05-16 06:31:53 +01:00
return ` Where does ${ focusPhrase } bite concretely? ` ;
2026-05-16 02:53:18 +01:00
}
function buildThoughtClaim ( { targetKind , focusPhrase , repoNames , sourceKinds , curiosityProfile } ) {
const repoName = repoNames [ 0 ] ;
if ( targetKind === "repo" && repoName ) {
2026-05-16 06:31:53 +01:00
return ` ${ repoName } is accumulating a concrete decision around ${ focusPhrase } . ` ;
2026-05-16 02:53:18 +01:00
}
if ( targetKind === "self" ) {
2026-05-16 06:31:53 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } is behaving like a persistent self-seam, not a passing thought. ` ;
2026-05-16 02:53:18 +01:00
}
if ( curiosityProfile . crossDomainPotential >= 0.55 && sourceKinds . includes ( "repo_sweep" ) && sourceKinds . includes ( "archive_excursion" ) ) {
2026-05-16 06:31:53 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } links current work to older patterns strongly enough to deserve a concrete read. ` ;
2026-05-16 02:53:18 +01:00
}
2026-05-16 06:45:43 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } is staying structurally important across passes. ` ;
2026-05-16 02:53:18 +01:00
}
function buildFascinationTarget ( { targetKind , focusPhrase , repoNames } ) {
const repoName = repoNames [ 0 ] ;
if ( targetKind === "repo" && repoName ) {
return ` ${ repoName } around ${ focusPhrase } ` ;
}
return focusPhrase ;
}
function buildThoughtSummary ( { focusKind , targetKind , focusPhrase , repoNames , sourceKinds , claim , question } ) {
2026-05-16 06:31:53 +01:00
const sourceLine =
sourceKinds . length > 0 && sourceKinds . length <= 3 ? ` Built from ${ sourceKinds . join ( ", " ) } . ` : "" ;
2026-05-16 02:53:18 +01:00
if ( focusKind === "fascination" && repoNames . length > 0 ) {
2026-05-16 06:31:53 +01:00
return ` ${ repoNames [ 0 ] } keeps pulling around ${ focusPhrase } . ${ sourceLine } ` . trim ( ) ;
2026-05-16 02:53:18 +01:00
}
if ( targetKind === "self" ) {
2026-05-16 06:31:53 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } keeps surviving as a self-seam. ${ sourceLine } ` . trim ( ) ;
2026-05-16 02:53:18 +01:00
}
if ( targetKind === "archive" ) {
2026-05-16 06:31:53 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } keeps pulling in the archive. ${ sourceLine } ` . trim ( ) ;
2026-05-16 02:53:18 +01:00
}
if ( focusKind === "claim" ) {
2026-05-16 06:45:43 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } has become a concrete claim. ${ sourceLine } ` . trim ( ) ;
2026-05-16 02:53:18 +01:00
}
2026-05-16 06:45:43 +01:00
return ` ${ capitalizePhrase ( focusPhrase ) } remains an unresolved question. ${ sourceLine } ` . trim ( ) ;
2026-05-16 02:53:18 +01:00
}
function topConceptKeywords ( text , limit ) {
const counts = new Map ( ) ;
for ( const token of tokenize ( text ) ) {
if ( stopwords . has ( token ) || labelNoiseTokens . has ( token ) ) {
continue ;
}
counts . set ( token , ( counts . get ( token ) ? ? 0 ) + 1 ) ;
}
return [ ... counts . entries ( ) ]
. sort ( ( left , right ) => {
if ( right [ 1 ] !== left [ 1 ] ) {
return right [ 1 ] - left [ 1 ] ;
}
return right [ 0 ] . length - left [ 0 ] . length ;
} )
. slice ( 0 , limit )
. map ( ( [ token ] ) => token ) ;
}
function capitalizePhrase ( value ) {
const normalized = normalizeText ( value ) ;
return normalized . length > 0 ? normalized [ 0 ] . toUpperCase ( ) + normalized . slice ( 1 ) : value ;
}
function getValueObjects ( values ) {
return ensureArray ( values ) . filter ( isObject ) ;
}
2026-05-08 16:52:22 +01:00
function mapCountEntries ( map , keyName ) {
return [ ... map . entries ( ) ]
. map ( ( [ key , count ] ) => ( { [ keyName ] : key , count } ) )
. sort ( ( left , right ) => right . count - left . count ) ;
}
function gatherSourceRefs ( entry ) {
if ( ! isObject ( entry ) ) {
return [ ] ;
}
return [
... ensureStringArray ( entry . evidenceRefs ) ,
... ensureStringArray ( entry . evidenceMessageIds ) ,
... ensureStringArray ( entry . distilledFrom ) ,
... ensureStringArray ( entry . sourceMemoryIds ) ,
] ;
}
function gatherSourceMeta ( { entry , kind } ) {
const sourceMeta = { } ;
2026-05-16 08:03:59 +01:00
const repoNames = [
... new Set ( [
... ensureStringArray ( entry . repoNames ) ,
... inferRepoNamesFromEntry ( entry ) ,
] ) ,
] ;
2026-05-08 16:52:22 +01:00
if ( repoNames . length > 0 ) {
sourceMeta . repoNames = repoNames ;
}
const channelId = readString ( entry , "channelId" ) ;
if ( channelId ) {
sourceMeta . channelId = channelId ;
}
2026-05-16 08:03:59 +01:00
if ( kind === "archive_excursion" ) {
const archiveTimestamp =
readString ( entry , "anchorTimestamp" ) ? ?
readString ( entry , "sourceTimestamp" ) ? ?
readString ( entry , "timestamp" ) ;
const archiveYear = extractYear ( archiveTimestamp ) ;
if ( archiveYear ) {
sourceMeta . archiveYear = archiveYear ;
}
2026-05-08 16:52:22 +01:00
}
if ( kind === "bridge_synthesis" ) {
const dominantTopics = ensureStringArray ( entry . dominantTopics ) ;
if ( dominantTopics . length > 0 ) {
sourceMeta . dominantTopics = dominantTopics ;
}
}
return sourceMeta ;
}
2026-05-16 08:03:59 +01:00
function inferRepoNamesFromEntry ( entry ) {
const subjectLabel =
readString ( entry , "subjectLabel" ) ? ?
readString ( entry , "topic" ) ? ?
readString ( entry , "label" ) ? ?
readString ( entry , "fascinationTarget" ) ;
const repoName = extractRepoNameFromLabel ( subjectLabel ) ;
return repoName ? [ repoName ] : [ ] ;
}
2026-05-08 16:52:22 +01:00
function extractYear ( timestamp ) {
if ( typeof timestamp !== "string" || timestamp . length < 4 ) {
return undefined ;
}
const match = timestamp . match ( /^(\d{4})-/ ) ;
return match ? match [ 1 ] : undefined ;
}
2026-05-06 02:13:00 +01:00
function createEmbedder ( { repoRootPath } ) {
const env = parseDotEnvSafe ( resolve ( repoRootPath , ".env" ) ) ;
const backend = env . RAG _EMBEDDING _BACKEND === "hash" ? "hash" : "ollama" ;
const dimensions = readInt ( env . VOID _MEMORY _VECTOR _DIMENSIONS , DEFAULT _VECTOR _DIMENSIONS ) ;
if ( backend === "ollama" ) {
return {
backend : "ollama" ,
model : env . RAG _OLLAMA _MODEL ? . trim ( ) || "qwen3-embedding:0.6b" ,
dimensions ,
baseUrl : normalizeBaseUrl ( env . RAG _OLLAMA _BASE _URL ? . trim ( ) || "http://127.0.0.1:11434" ) ,
async embed ( text ) {
try {
const response = await fetch ( ` ${ this . baseUrl } /api/embed ` , {
method : "POST" ,
headers : { "Content-Type" : "application/json" } ,
body : JSON . stringify ( {
model : this . model ,
input : [ text ] ,
} ) ,
} ) ;
if ( ! response . ok ) {
throw new Error ( ` Ollama responded ${ response . status } ` ) ;
}
const payload = await response . json ( ) ;
const raw = Array . isArray ( payload ? . embeddings ) ? payload . embeddings [ 0 ] : undefined ;
if ( ! Array . isArray ( raw ) || raw . length === 0 ) {
throw new Error ( "No embedding returned." ) ;
}
return compactVector ( raw . map ( Number ) , dimensions ) ;
} catch {
this . backend = "hash" ;
this . model = ` hash: ${ dimensions } ` ;
return hashVector ( text , dimensions ) ;
}
} ,
} ;
}
return {
backend : "hash" ,
model : ` hash: ${ dimensions } ` ,
dimensions ,
async embed ( text ) {
return hashVector ( text , dimensions ) ;
} ,
} ;
}
function compactVector ( values , targetDimensions ) {
if ( values . length === targetDimensions ) {
return normalizeVector ( values ) ;
}
const compacted = new Array ( targetDimensions ) . fill ( 0 ) ;
for ( let index = 0 ; index < targetDimensions ; index += 1 ) {
const start = Math . floor ( ( index * values . length ) / targetDimensions ) ;
const end = Math . floor ( ( ( index + 1 ) * values . length ) / targetDimensions ) ;
const slice = values . slice ( start , Math . max ( start + 1 , end ) ) ;
compacted [ index ] =
slice . reduce ( ( sum , value ) => sum + ( Number . isFinite ( value ) ? value : 0 ) , 0 ) /
Math . max ( 1 , slice . length ) ;
}
return normalizeVector ( compacted ) ;
}
function hashVector ( text , dimensions ) {
const terms = tokenize ( text ) ;
if ( terms . length === 0 ) {
return new Array ( dimensions ) . fill ( 0 ) ;
}
const vector = new Array ( dimensions ) . fill ( 0 ) ;
for ( const term of terms ) {
const hash = createHash ( "sha1" ) . update ( term ) . digest ( ) ;
const index = hash . readUInt32BE ( 0 ) % dimensions ;
const sign = hash [ 4 ] % 2 === 0 ? 1 : - 1 ;
vector [ index ] += sign ;
}
return normalizeVector ( vector ) ;
}
function averagePairwiseSimilarity ( items ) {
if ( items . length < 2 ) {
return 0 ;
}
let total = 0 ;
let count = 0 ;
for ( let leftIndex = 0 ; leftIndex < items . length ; leftIndex += 1 ) {
for ( let rightIndex = leftIndex + 1 ; rightIndex < items . length ; rightIndex += 1 ) {
total += cosineSimilarity ( items [ leftIndex ] . entry . semanticVector . values , items [ rightIndex ] . entry . semanticVector . values ) ;
count += 1 ;
}
}
return count === 0 ? 0 : total / count ;
}
function cosineSimilarity ( left , right ) {
if ( ! Array . isArray ( left ) || ! Array . isArray ( right ) || left . length !== right . length || left . length === 0 ) {
return 0 ;
}
let dot = 0 ;
let leftMagnitude = 0 ;
let rightMagnitude = 0 ;
for ( let index = 0 ; index < left . length ; index += 1 ) {
dot += left [ index ] * right [ index ] ;
leftMagnitude += left [ index ] * * 2 ;
rightMagnitude += right [ index ] * * 2 ;
}
if ( leftMagnitude === 0 || rightMagnitude === 0 ) {
return 0 ;
}
return dot / Math . sqrt ( leftMagnitude * rightMagnitude ) ;
}
function normalizeVector ( vector ) {
const magnitude = Math . sqrt ( vector . reduce ( ( sum , value ) => sum + value * * 2 , 0 ) ) ;
if ( magnitude === 0 ) {
return vector ;
}
return vector . map ( ( value ) => value / magnitude ) ;
}
function topKeywords ( text , limit ) {
const counts = new Map ( ) ;
for ( const token of tokenize ( text ) ) {
if ( stopwords . has ( token ) ) {
continue ;
}
counts . set ( token , ( counts . get ( token ) ? ? 0 ) + 1 ) ;
}
return [ ... counts . entries ( ) ]
. sort ( ( left , right ) => right [ 1 ] - left [ 1 ] )
. slice ( 0 , limit )
. map ( ( [ token ] ) => token ) ;
}
function tokenize ( input ) {
return String ( input )
. toLowerCase ( )
. split ( /[^a-z0-9]+/i )
. map ( ( token ) => token . trim ( ) )
. filter ( ( token ) => token . length > 2 ) ;
}
function ensureMemoryId ( entry , kind , text ) {
const existing = readString ( entry , "memoryId" ) ;
if ( existing ) {
return existing ;
}
const timestamp = readString ( entry , "timestamp" ) ? ? readString ( entry , "lastObservedAt" ) ? ? "undated" ;
return ` ${ kind } - ${ hashString ( ` ${ timestamp } | ${ text } ` ) . slice ( 0 , 12 ) } ` ;
}
function normalizeText ( value ) {
if ( typeof value !== "string" ) {
return "" ;
}
return value . replace ( /\s+/g , " " ) . trim ( ) ;
}
2026-05-07 22:39:50 +01:00
function selectRecentRecords ( entries , { limit , quietLimit } ) {
const recent = ensureArray ( entries ) . slice ( - Math . max ( limit * 3 , limit ) ) ;
const quiet = [ ] ;
const active = [ ] ;
for ( let index = recent . length - 1 ; index >= 0 ; index -= 1 ) {
const entry = recent [ index ] ;
if ( ! isObject ( entry ) ) {
continue ;
}
const text = normalizeText ( [
readString ( entry , "summary" ) ,
readString ( entry , "claim" ) ,
readString ( entry , "topic" ) ,
readString ( entry , "counterweight" ) ,
] . filter ( Boolean ) . join ( " | " ) ) ;
if ( isLowSignalQuietRoomText ( text ) ) {
if ( quiet . length < quietLimit ) {
quiet . unshift ( entry ) ;
}
continue ;
}
if ( active . length < limit - quietLimit ) {
active . unshift ( entry ) ;
}
}
return [ ... active , ... quiet ] . slice ( - limit ) ;
}
function isLowSignalQuietRoomText ( text ) {
const normalized = normalizeText ( text ) . toLowerCase ( ) ;
if ( ! normalized ) {
return false ;
}
const quietSignals = [
"no new discord" ,
"no fresh discord" ,
"no new messages" ,
"no fresh live traffic" ,
"no live moderation" ,
"no moderation smoke" ,
"no owner escalation" ,
"room was empty" ,
"room is idle" ,
"there was no live moderation pressure" ,
"nothing got posted" ,
"no discord post" ,
] ;
let matches = 0 ;
for ( const signal of quietSignals ) {
if ( normalized . includes ( signal ) ) {
matches += 1 ;
}
}
return matches >= 2 ;
}
2026-05-06 02:13:00 +01:00
function overlapRatio ( left , right ) {
if ( left . length === 0 || right . length === 0 ) {
return 0 ;
}
const leftSet = new Set ( left ) ;
let overlap = 0 ;
for ( const value of right ) {
if ( leftSet . has ( value ) ) {
overlap += 1 ;
}
}
return overlap / Math . max ( left . length , right . length ) ;
}
2026-05-16 03:01:33 +01:00
function mergeStringArrays ( left , right ) {
return [ ... new Set ( [ ... ensureStringArray ( left ) , ... ensureStringArray ( right ) ] ) ] ;
}
function newestIsoTimestamp ( left , right ) {
const leftTime = parseIsoTimestamp ( left ) ;
const rightTime = parseIsoTimestamp ( right ) ;
if ( leftTime === null ) {
return rightTime === null ? undefined : right ;
}
if ( rightTime === null ) {
return left ;
}
return leftTime >= rightTime ? left : right ;
}
function parseIsoTimestamp ( value ) {
if ( typeof value !== "string" || value . length === 0 ) {
return null ;
}
const time = Date . parse ( value ) ;
return Number . isFinite ( time ) ? time : null ;
}
2026-05-06 02:13:00 +01:00
function hashString ( input ) {
return createHash ( "sha1" ) . update ( String ( input ) ) . digest ( "hex" ) ;
}
function parseDotEnvSafe ( path ) {
try {
return parseDotEnv ( stripBom ( readFileSync ( path , "utf8" ) ) ) ;
} catch ( error ) {
if ( error && typeof error === "object" && "code" in error && error . code === "ENOENT" ) {
return { } ;
}
throw error ;
}
}
function parseDotEnv ( raw ) {
const result = { } ;
for ( const line of raw . split ( /\r?\n/ ) ) {
const trimmed = line . trim ( ) ;
if ( trimmed . length === 0 || trimmed . startsWith ( "#" ) ) {
continue ;
}
const separatorIndex = trimmed . indexOf ( "=" ) ;
if ( separatorIndex === - 1 ) {
continue ;
}
const key = trimmed . slice ( 0 , separatorIndex ) . trim ( ) ;
let value = trimmed . slice ( separatorIndex + 1 ) . trim ( ) ;
if (
value . length >= 2 &&
( ( value . startsWith ( "\"" ) && value . endsWith ( "\"" ) ) ||
( value . startsWith ( "'" ) && value . endsWith ( "'" ) ) )
) {
value = value . slice ( 1 , - 1 ) ;
}
result [ key ] = value ;
}
return result ;
}
function normalizeBaseUrl ( value ) {
return String ( value ) . replace ( /\/+$/ , "" ) ;
}
function readInt ( value , fallback ) {
const parsed = Number . parseInt ( String ( value ? ? "" ) , 10 ) ;
return Number . isInteger ( parsed ) && parsed > 0 ? parsed : fallback ;
}
function ensureArray ( value ) {
return Array . isArray ( value ) ? value : [ ] ;
}
function ensureObject ( value ) {
return isObject ( value ) ? value : { } ;
}
function ensureStringArray ( value ) {
return ensureArray ( value ) . filter ( ( entry ) => typeof entry === "string" ) ;
}
function readString ( value , key ) {
return isObject ( value ) && typeof value [ key ] === "string" ? value [ key ] : undefined ;
}
function readNumber ( value , key ) {
return isObject ( value ) && typeof value [ key ] === "number" && Number . isFinite ( value [ key ] )
? value [ key ]
: undefined ;
}
2026-05-16 08:03:59 +01:00
function readBoolean ( value , key ) {
return isObject ( value ) && typeof value [ key ] === "boolean" ? value [ key ] : undefined ;
}
2026-05-06 02:13:00 +01:00
function round3 ( value ) {
return Math . round ( value * 1000 ) / 1000 ;
}
function round4 ( value ) {
return Math . round ( value * 10000 ) / 10000 ;
}
function clamp ( value , min , max ) {
return Math . min ( max , Math . max ( min , value ) ) ;
}
function stripBom ( input ) {
return input . charCodeAt ( 0 ) === 0xfeff ? input . slice ( 1 ) : input ;
}
function isObject ( value ) {
return typeof value === "object" && value !== null && ! Array . isArray ( value ) ;
}