totemheart 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -14
- package/package.json +64 -61
- package/src/Totemheart.js +268 -23
- package/src/behavior/ExpressionDebt.js +25 -0
- package/src/behavior/ExpressionDirectives.js +19 -6
- package/src/cognition/AmygdalaHijack.js +103 -6
- package/src/cognition/DefenseMechanisms.js +33 -4
- package/src/cognition/LoadScheduler.js +43 -5
- package/src/cognition/RemConsolidation.js +14 -2
- package/src/core/DecayEngine.js +10 -4
- package/src/core/EmotionSpace.js +61 -6
- package/src/core/Homeostasis.js +67 -2
- package/src/core/Personality.js +18 -8
- package/src/core/WornPathCache.js +22 -9
- package/src/neurochemistry/ArousalKalmanFilter.js +24 -4
- package/src/neurochemistry/CircadianRhythm.js +73 -4
- package/src/neurochemistry/DopaminergicEngine.js +108 -13
- package/src/social/Attachment.js +87 -5
- package/src/social/EpisodicMemory.js +58 -0
- package/src/social/ForgettingCurve.js +16 -1
- package/src/social/LoveHateEngine.js +273 -0
- package/src/text/EmotionalTextGenerator.js +5 -3
- package/src/version.js +1 -1
|
@@ -1,27 +1,96 @@
|
|
|
1
|
+
function clamp01( v ) {
|
|
2
|
+
|
|
3
|
+
return Math.max( 0, Math.min( 1, v ) )
|
|
4
|
+
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
/**
|
|
2
8
|
* Ties energy to a real clock. Peaks at 15:00, troughs at 03:00 — a cosine
|
|
3
9
|
* wave 12h out of phase from the peak, so "3am" genuinely produces the
|
|
4
10
|
* lowest energy reading without any special-casing.
|
|
11
|
+
*
|
|
12
|
+
* Two real couplings on top of the plain diurnal wave:
|
|
13
|
+
*
|
|
14
|
+
* Cortisol flattening the amplitude: sustained high cortisol is associated
|
|
15
|
+
* with a FLATTER diurnal cortisol slope in the chronic-stress literature
|
|
16
|
+
* (Miller, G. E., Chen, E., & Zhou, E. S. (2007), "If it goes up, must it
|
|
17
|
+
* come down? Chronic stress and the hypothalamic-pituitary-adrenal axis in
|
|
18
|
+
* humans", Psychological Bulletin, 133(1), 25-45) — the healthy sharp
|
|
19
|
+
* peak/trough contrast erodes under chronic load. Applied here to the
|
|
20
|
+
* energy wave's amplitude (not cortisol's own level, which CortisolEngine
|
|
21
|
+
* already tracks) as the qualitative direction that literature supports;
|
|
22
|
+
* the specific flattening coefficient is own tuning, not measured from it.
|
|
23
|
+
*
|
|
24
|
+
* Sleep debt: real accumulator, distinct from cortisol — rises only while
|
|
25
|
+
* the system is being actively used (a turn processed) during a
|
|
26
|
+
* low-energy window (the conversational analog of staying up when the
|
|
27
|
+
* circadian signal says rest), and only clears when RemConsolidation
|
|
28
|
+
* actually sweeps (the "sleep" analog) — own engineering, no citation for
|
|
29
|
+
* the specific accumulation shape.
|
|
5
30
|
*/
|
|
6
31
|
export class CircadianRhythm {
|
|
7
32
|
|
|
8
|
-
|
|
33
|
+
constructor() {
|
|
34
|
+
|
|
35
|
+
this.sleepDebt = 0
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getEnergyLevel( now = new Date(), cortisolLevel = 0 ) {
|
|
9
40
|
|
|
10
41
|
const hour = now.getHours() + now.getMinutes() / 60
|
|
11
42
|
const radians = ( ( hour - 15 ) / 24 ) * 2 * Math.PI
|
|
12
|
-
|
|
43
|
+
const amplitude = 0.5 * ( 1 - clamp01( cortisolLevel ) * 0.5 ) // chronic cortisol flattens the wave, own tuning
|
|
44
|
+
return clamp01( 0.5 - this.sleepDebt * 0.2 + amplitude * Math.cos( radians ) )
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Call once per processed turn — accumulates real sleep debt when the system is being used during its own low-energy window. */
|
|
49
|
+
observeActivity( now = new Date(), cortisolLevel = 0 ) {
|
|
50
|
+
|
|
51
|
+
const energy = this.getEnergyLevel( now, cortisolLevel )
|
|
52
|
+
if ( energy < 0.3 ) this.sleepDebt = clamp01( this.sleepDebt + 0.03 )
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Only a real "sleep" event (a RemConsolidation sweep) pays sleep debt down — a plain tick() does not. */
|
|
57
|
+
payDownSleepDebt( amount = 0.3 ) {
|
|
58
|
+
|
|
59
|
+
this.sleepDebt = Math.max( 0, this.sleepDebt - amount )
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The real coupling runs the other way too: cortisol itself has its own
|
|
65
|
+
* diurnal rhythm, peaking sharply in the ~30-45 minutes after waking (the
|
|
66
|
+
* Cortisol Awakening Response — Pruessner, J. C. et al. (1997), "Free
|
|
67
|
+
* cortisol levels after awakening: a reliable biological marker for the
|
|
68
|
+
* assessment of adrenocortical activity", Life Sciences, 61(26),
|
|
69
|
+
* 2539-2549) and tapering across the day. A real, separate, additive
|
|
70
|
+
* signal a caller MAY fold into CortisolEngine's own reading, own
|
|
71
|
+
* engineering approximation of the timing/shape (peak ~08:00, taper
|
|
72
|
+
* through the day) — not a reproduction of Pruessner et al.'s measured
|
|
73
|
+
* curve, and deliberately NOT wired into CortisolEngine.level itself
|
|
74
|
+
* (that field's semantics are load-driven chronic stress, not this).
|
|
75
|
+
*/
|
|
76
|
+
getCortisolBaselineShift( now = new Date() ) {
|
|
77
|
+
|
|
78
|
+
const hour = now.getHours() + now.getMinutes() / 60
|
|
79
|
+
const radians = ( ( hour - 8 ) / 24 ) * 2 * Math.PI
|
|
80
|
+
return clamp01( 0.5 + 0.5 * Math.cos( radians ) ) * 0.15 // small, bounded nudge — own tuning
|
|
13
81
|
|
|
14
82
|
}
|
|
15
83
|
|
|
16
|
-
getState( now = new Date() ) {
|
|
84
|
+
getState( now = new Date(), cortisolLevel = 0 ) {
|
|
17
85
|
|
|
18
|
-
const energy = this.getEnergyLevel( now )
|
|
86
|
+
const energy = this.getEnergyLevel( now, cortisolLevel )
|
|
19
87
|
return {
|
|
20
88
|
energy,
|
|
21
89
|
hour : now.getHours(),
|
|
22
90
|
lowEnergyWindow : energy < 0.3,
|
|
23
91
|
responseLengthMult : 0.4 + 0.6 * energy,
|
|
24
92
|
erraticChance : ( 1 - energy ) * 0.3,
|
|
93
|
+
sleepDebt : this.sleepDebt,
|
|
25
94
|
}
|
|
26
95
|
|
|
27
96
|
}
|
|
@@ -4,38 +4,133 @@ function clamp( v, min = -1, max = 1 ) {
|
|
|
4
4
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
function clamp01( v ) {
|
|
8
|
+
|
|
9
|
+
return Math.max( 0, Math.min( 1, v ) )
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
/**
|
|
8
14
|
* Reward Prediction Error (RPE): RPE = R_t + γ·V(S_t+1) − V(S_t).
|
|
9
15
|
* Dopamine doesn't fire for getting something good — it fires for getting
|
|
10
16
|
* something *better than expected*. This TD-learning reading of dopaminergic
|
|
11
17
|
* activity is the standard one in computational neuroscience (Schultz, W.,
|
|
12
18
|
* Dayan, P., & Montague, P. R. (1997), "A neural substrate of prediction and
|
|
13
|
-
* reward", Science, 275(5306), 1593-1599).
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
19
|
+
* reward", Science, 275(5306), 1593-1599).
|
|
20
|
+
*
|
|
21
|
+
* Two real extensions on top of the base TD(0) engine:
|
|
22
|
+
*
|
|
23
|
+
* Wanting vs. liking: dopaminergic "wanting" (incentive salience — craving a
|
|
24
|
+
* cue even without consuming the reward) and hedonic "liking" (the reaction
|
|
25
|
+
* to the reward itself) are dissociable systems (Berridge, K. C., & Robinson,
|
|
26
|
+
* T. E. (1998), "What is the role of dopamine in reward: hedonic impact,
|
|
27
|
+
* reward learning, or incentive salience?", Brain Research Reviews, 28(3),
|
|
28
|
+
* 309-369). `wanting` tracks accumulated |RPE| (it rises on surprise alone,
|
|
29
|
+
* even for a bad outcome — the "can't stop craving it" signature); `liking`
|
|
30
|
+
* is a plain EMA of the raw reward itself (the actual hedonic reaction).
|
|
31
|
+
*
|
|
32
|
+
* Eligibility traces: TD(λ) (Sutton, R. S., & Barto, A. G. (2018),
|
|
33
|
+
* "Reinforcement Learning: An Introduction", 2nd ed., MIT Press, ch. 12) —
|
|
34
|
+
* a per-context trace that decays by γλ each update and gets credited by
|
|
35
|
+
* this update's RPE, so a reward doesn't just correct the ONE context active
|
|
36
|
+
* this instant, it also retroactively credits contexts that were active
|
|
37
|
+
* recently (real backward credit assignment across turns, not a citation of
|
|
38
|
+
* a specific published λ). `context` defaults to a single shared key, so
|
|
39
|
+
* calling `computeRPE(reward)` with no context behaves exactly like the
|
|
40
|
+
* original single-scalar engine.
|
|
17
41
|
*/
|
|
18
42
|
export class DopaminergicEngine {
|
|
19
43
|
|
|
20
|
-
constructor( { alpha = 0.25, gamma = 0.9 } = {} ) {
|
|
44
|
+
constructor( { alpha = 0.25, gamma = 0.9, lambda = 0.7, likingAlpha = 0.3, wantingDecay = 0.1 } = {} ) {
|
|
45
|
+
|
|
46
|
+
this.alpha = alpha
|
|
47
|
+
this.gamma = gamma
|
|
48
|
+
this.lambda = lambda
|
|
49
|
+
this.likingAlpha = likingAlpha
|
|
50
|
+
this.wantingDecay = wantingDecay
|
|
51
|
+
|
|
52
|
+
this.expectedValues = new Map() // context -> V(context)
|
|
53
|
+
this.eligibility = new Map() // context -> trace
|
|
54
|
+
this.likingValue = 0
|
|
55
|
+
this.wanting = 0
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#getV( context ) {
|
|
21
60
|
|
|
22
|
-
this.
|
|
23
|
-
this.gamma = gamma
|
|
24
|
-
this.expectedValue = 0
|
|
61
|
+
return this.expectedValues.get( context ) ?? 0
|
|
25
62
|
|
|
26
63
|
}
|
|
27
64
|
|
|
28
|
-
|
|
65
|
+
/** `context` lets ToM-derived predictions and life events target a specific relationship/topic instead of one shared scalar. */
|
|
66
|
+
computeRPE( reward, context = 'default' ) {
|
|
67
|
+
|
|
68
|
+
const v = this.#getV( context )
|
|
69
|
+
const rpe = reward + this.gamma * v - v
|
|
70
|
+
|
|
71
|
+
// Decay every existing trace, then bump this context's — the standard
|
|
72
|
+
// accumulating-trace TD(λ) update.
|
|
73
|
+
for ( const [ key, trace ] of this.eligibility ) {
|
|
74
|
+
|
|
75
|
+
const decayed = trace * this.gamma * this.lambda
|
|
76
|
+
if ( decayed < 0.01 ) this.eligibility.delete( key )
|
|
77
|
+
else this.eligibility.set( key, decayed )
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
this.eligibility.set( context, ( this.eligibility.get( context ) ?? 0 ) + 1 )
|
|
81
|
+
|
|
82
|
+
// Every context with a live trace gets credited proportionally — this is
|
|
83
|
+
// the real backward-credit-assignment part of TD(λ), not just updating
|
|
84
|
+
// the one context active this instant.
|
|
85
|
+
for ( const [ key, trace ] of this.eligibility ) {
|
|
86
|
+
|
|
87
|
+
this.expectedValues.set( key, this.#getV( key ) + this.alpha * rpe * trace )
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.likingValue += this.likingAlpha * ( reward - this.likingValue )
|
|
92
|
+
this.wanting = clamp01( this.wanting * ( 1 - this.wantingDecay ) + Math.abs( rpe ) * 0.3 )
|
|
29
93
|
|
|
30
|
-
const rpe = reward + this.gamma * this.expectedValue - this.expectedValue
|
|
31
|
-
this.expectedValue += this.alpha * rpe
|
|
32
94
|
return clamp( rpe, -2, 2 ) / 2 // normalize to roughly -1..1 for downstream spike use
|
|
33
95
|
|
|
34
96
|
}
|
|
35
97
|
|
|
36
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Direct expectation nudge from a source OTHER than an observed reward —
|
|
100
|
+
* Theory-of-Mind's estimate of what this user is likely to do, or a
|
|
101
|
+
* detected life event, both carry real information about what should be
|
|
102
|
+
* expected going forward, not just what already happened. Same expected
|
|
103
|
+
* value store computeRPE() reads/writes, so a later reward against this
|
|
104
|
+
* context is judged against the updated expectation.
|
|
105
|
+
*/
|
|
106
|
+
updateExpectationFromBelief( context, deltaV, weight = 0.3 ) {
|
|
107
|
+
|
|
108
|
+
this.expectedValues.set( context, clamp( this.#getV( context ) + deltaV * weight, -1, 1 ) )
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getExpectedValue( context = 'default' ) {
|
|
113
|
+
|
|
114
|
+
return this.#getV( context )
|
|
115
|
+
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getWanting() {
|
|
119
|
+
|
|
120
|
+
return this.wanting
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A relational rupture (LoveHateEngine) is real evidence the anticipatory craving for this context should stop, not gradually decay — a hard reset, not another EMA step. */
|
|
125
|
+
freezeWanting() {
|
|
126
|
+
|
|
127
|
+
this.wanting = 0
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getLiking() {
|
|
37
132
|
|
|
38
|
-
return this.
|
|
133
|
+
return this.likingValue
|
|
39
134
|
|
|
40
135
|
}
|
|
41
136
|
|
package/src/social/Attachment.js
CHANGED
|
@@ -4,6 +4,51 @@ function clamp( v, min, max ) {
|
|
|
4
4
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Attachment style bucket from personality, on the two real dimensions the
|
|
9
|
+
* adult-attachment literature converges on — anxiety (fear of abandonment,
|
|
10
|
+
* hypervigilance to small negatives) and avoidance (discomfort with
|
|
11
|
+
* closeness, dampened reactivity in general) — see Bartholomew, K., &
|
|
12
|
+
* Horowitz, L. M. (1991), "Attachment styles among young adults: a test of a
|
|
13
|
+
* four-category model", Journal of Personality and Social Psychology, 61(2),
|
|
14
|
+
* 226-244. Mapping OCEAN traits onto those two dimensions (neuroticism as a
|
|
15
|
+
* proxy for anxiety, low agreeableness/extraversion as a proxy for avoidance)
|
|
16
|
+
* is our own engineering choice, not a validated psychometric equivalence —
|
|
17
|
+
* see CALIBRATION.md.
|
|
18
|
+
*/
|
|
19
|
+
function classifyStyle( personality ) {
|
|
20
|
+
|
|
21
|
+
const anxiety = personality.get( 'neuroticism' )
|
|
22
|
+
const avoidance = 1 - ( personality.get( 'agreeableness' ) * 0.6 + personality.get( 'extraversion' ) * 0.4 )
|
|
23
|
+
|
|
24
|
+
if ( anxiety < 0.5 && avoidance < 0.5 ) return 'secure'
|
|
25
|
+
if ( anxiety >= 0.5 && avoidance < 0.5 ) return 'anxious'
|
|
26
|
+
if ( anxiety < 0.5 && avoidance >= 0.5 ) return 'avoidant'
|
|
27
|
+
return 'fearful'
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Per-style multipliers on the base warming/cooling rate and on rupture
|
|
32
|
+
// sensitivity — direction taken from the attachment-style literature (secure
|
|
33
|
+
// = balanced and resilient; anxious = reacts fast and hard to both directions,
|
|
34
|
+
// slow to fully trust again; avoidant = damped reactivity in both directions;
|
|
35
|
+
// fearful = anxious's hypervigilance without avoidant's dampening, the
|
|
36
|
+
// "wants closeness, fears it too" combination), the specific magnitudes are
|
|
37
|
+
// our own tuning, not coefficients from any study.
|
|
38
|
+
// ruptureThreshold is on the raw valenceDelta of the TRIGGERING turn (not the
|
|
39
|
+
// resulting affinity movement — the affinity RATE constants above already
|
|
40
|
+
// vary 3x across styles, so gating on the post-rate-multiplier drop would
|
|
41
|
+
// make the same nominal threshold mean wildly different things per style;
|
|
42
|
+
// gating directly on how bad the triggering turn itself was is both simpler
|
|
43
|
+
// and closer to the real "some interactions are just severe enough to
|
|
44
|
+
// rupture a relationship" framing).
|
|
45
|
+
const STYLE_PROFILE = {
|
|
46
|
+
secure : { warmingMult: 1.0, coolingMult: 1.0, ruptureThreshold: -0.7 },
|
|
47
|
+
anxious : { warmingMult: 1.3, coolingMult: 1.6, ruptureThreshold: -0.45 },
|
|
48
|
+
avoidant : { warmingMult: 0.6, coolingMult: 0.7, ruptureThreshold: -0.85 },
|
|
49
|
+
fearful : { warmingMult: 1.1, coolingMult: 1.7, ruptureThreshold: -0.4 },
|
|
50
|
+
}
|
|
51
|
+
|
|
7
52
|
/**
|
|
8
53
|
* Per-user relationship state: trust, affinity and a power dynamic
|
|
9
54
|
* (-1 = AI feels subordinate, 1 = AI feels in control).
|
|
@@ -26,7 +71,10 @@ export class Attachment {
|
|
|
26
71
|
// iterated-prisoner's-dilemma reputation systems). `trust` is always
|
|
27
72
|
// kept as the derived posterior mean (α/(α+β)) for backward
|
|
28
73
|
// compatibility with every consumer that reads it as a plain number.
|
|
29
|
-
this.relations.set( userId, {
|
|
74
|
+
this.relations.set( userId, {
|
|
75
|
+
trust: 0.5, trustAlpha: 1, trustBeta: 1, affinity: 0.5, powerDynamic: 0,
|
|
76
|
+
ruptured: false, ruptureAt: null, repairsCount: 0,
|
|
77
|
+
} )
|
|
30
78
|
|
|
31
79
|
}
|
|
32
80
|
return this.relations.get( userId )
|
|
@@ -36,6 +84,8 @@ export class Attachment {
|
|
|
36
84
|
update( userId, { valenceDelta = 0, guiltTriggered = false, dissonanceTriggered = false, betrayalDetected = false }, personality ) {
|
|
37
85
|
|
|
38
86
|
const rel = this.#entry( userId )
|
|
87
|
+
const style = classifyStyle( personality )
|
|
88
|
+
const profile = STYLE_PROFILE[ style ]
|
|
39
89
|
const agreeableness = personality.get( 'agreeableness' )
|
|
40
90
|
const baseRate = 0.05 * ( 1 + personality.get( 'neuroticism' ) * 0.5 )
|
|
41
91
|
|
|
@@ -45,13 +95,37 @@ export class Attachment {
|
|
|
45
95
|
// The *direction* of this effect (agreeableness correlating with
|
|
46
96
|
// forgiveness) has reasonable support in Big Five personality
|
|
47
97
|
// research; the specific rate constants below are our own tuning,
|
|
48
|
-
// not coefficients from any study. See CALIBRATION.md.
|
|
49
|
-
|
|
50
|
-
|
|
98
|
+
// not coefficients from any study. See CALIBRATION.md. The attachment
|
|
99
|
+
// style multiplier layers on top: an anxious/fearful style swings
|
|
100
|
+
// harder in both directions, avoidant swings less in either.
|
|
101
|
+
const warmingRate = baseRate * ( 0.6 + agreeableness * 0.8 ) * profile.warmingMult
|
|
102
|
+
const coolingRate = baseRate * ( 1.6 - agreeableness * 0.8 ) * profile.coolingMult
|
|
51
103
|
const affinityRate = valenceDelta >= 0 ? warmingRate : coolingRate
|
|
52
104
|
|
|
53
105
|
rel.affinity = clamp( rel.affinity + valenceDelta * affinityRate, 0, 1 )
|
|
54
106
|
|
|
107
|
+
// Rupture detection: THIS turn's own severity crossing this style's real
|
|
108
|
+
// sensitivity threshold — an anxious/fearful style flags a rupture from a
|
|
109
|
+
// much milder bad turn than a secure or avoidant one does (Gottman, J. M.,
|
|
110
|
+
// & Levenson, R. W. (1992) and the broader rupture-and-repair literature
|
|
111
|
+
// in attachment/relational research motivate the concept; the specific
|
|
112
|
+
// threshold values are our own engineering, not measured from that
|
|
113
|
+
// literature). Repair: the FIRST clearly-positive turn after a rupture
|
|
114
|
+
// heals it and counts toward this relationship's repair history —
|
|
115
|
+
// real evidence a relationship survived a rupture, not just time passing.
|
|
116
|
+
if ( !rel.ruptured && valenceDelta <= profile.ruptureThreshold ) {
|
|
117
|
+
|
|
118
|
+
rel.ruptured = true
|
|
119
|
+
rel.ruptureAt = Date.now()
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
else if ( rel.ruptured && valenceDelta > 0.3 ) {
|
|
123
|
+
|
|
124
|
+
rel.ruptured = false
|
|
125
|
+
rel.repairsCount += 1
|
|
126
|
+
|
|
127
|
+
}
|
|
128
|
+
|
|
55
129
|
// Trust: Bayesian reputation update. A dissonance-triggering turn OR an
|
|
56
130
|
// explicit betrayal-concept match (EmotionalOntology) both count as an
|
|
57
131
|
// observed "defection" — belief conflict and betrayal are different real
|
|
@@ -63,15 +137,23 @@ export class Attachment {
|
|
|
63
137
|
// to damage, slow to rebuild) because defections are weighted several
|
|
64
138
|
// times heavier than single cooperations.
|
|
65
139
|
if ( dissonanceTriggered || betrayalDetected ) rel.trustBeta += 1 + baseRate * 10
|
|
66
|
-
else if ( valenceDelta > 0.2 ) rel.trustAlpha += 1
|
|
140
|
+
else if ( valenceDelta > 0.2 ) rel.trustAlpha += rel.ruptured ? 0.5 : 1 // trust rebuilds slower than affinity while a rupture is still open
|
|
67
141
|
rel.trust = rel.trustAlpha / ( rel.trustAlpha + rel.trustBeta )
|
|
68
142
|
|
|
69
143
|
rel.powerDynamic = clamp( rel.powerDynamic + ( guiltTriggered ? -baseRate : 0 ), -1, 1 )
|
|
144
|
+
rel.style = style
|
|
70
145
|
|
|
71
146
|
return rel
|
|
72
147
|
|
|
73
148
|
}
|
|
74
149
|
|
|
150
|
+
/** The attachment style this user's relationship would be classified as, given the AI's own personality — a stable trait, not per-user state. */
|
|
151
|
+
getStyle( personality ) {
|
|
152
|
+
|
|
153
|
+
return classifyStyle( personality )
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
|
|
75
157
|
get( userId ) {
|
|
76
158
|
|
|
77
159
|
return this.#entry( userId )
|
|
@@ -255,6 +255,64 @@ export class EpisodicMemory {
|
|
|
255
255
|
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Reconsolidation: a retrieved memory doesn't just get read, it briefly
|
|
260
|
+
* becomes modifiable again before re-stabilizing (Nader, K., Schafe, G. E.,
|
|
261
|
+
* & LeDoux, J. E. (2000), "Fear memories require protein synthesis in the
|
|
262
|
+
* amygdala for reconsolidation after retrieval", Nature, 406, 722-726).
|
|
263
|
+
* Real reconsolidation windows in that literature run hours in an animal
|
|
264
|
+
* model; the window here is scaled to this system's conversational
|
|
265
|
+
* timescale (minutes, not hours) as an engineering choice, not a
|
|
266
|
+
* reproduction of Nader et al.'s measured timing. While labile, the
|
|
267
|
+
* stored emotional signature can be nudged toward the CURRENT turn's
|
|
268
|
+
* signature — the same real "retrieval updates the trace" mechanism, not
|
|
269
|
+
* a full overwrite.
|
|
270
|
+
*/
|
|
271
|
+
markLabile( id, now = Date.now(), windowMs = 1000 * 60 * 10 ) {
|
|
272
|
+
|
|
273
|
+
const entry = this.memories.find( m => m.id === id )
|
|
274
|
+
if ( !entry ) return null
|
|
275
|
+
entry.labile = true
|
|
276
|
+
entry.lableUntil = now + windowMs
|
|
277
|
+
return entry
|
|
278
|
+
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Blends a labile memory's stored signature toward the current turn's — no-op once the reconsolidation window has closed. */
|
|
282
|
+
reconsolidate( entry, currentSignature, now = Date.now(), blendRate = 0.25 ) {
|
|
283
|
+
|
|
284
|
+
if ( !entry.labile || now > entry.lableUntil ) return false
|
|
285
|
+
|
|
286
|
+
entry.emotionalSignature = {
|
|
287
|
+
valence : ( entry.emotionalSignature?.valence ?? 0 ) * ( 1 - blendRate ) + ( currentSignature.valence ?? 0 ) * blendRate,
|
|
288
|
+
arousal : ( entry.emotionalSignature?.arousal ?? 0 ) * ( 1 - blendRate ) + ( currentSignature.arousal ?? 0 ) * blendRate,
|
|
289
|
+
}
|
|
290
|
+
entry.labile = false
|
|
291
|
+
return true
|
|
292
|
+
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Zeigarnik-driven intrusive thought: unresolved wounds don't wait to be
|
|
297
|
+
* asked about — the higher the ambient Zeigarnik pressure, the more
|
|
298
|
+
* likely one resurfaces unprompted THIS tick. Modeled as a real Poisson
|
|
299
|
+
* process (P(at least one event in dt) = 1 - e^(-rate·dt)), `rate` scaled
|
|
300
|
+
* by the wound's own Zeigarnik priority — own tuning of the rate
|
|
301
|
+
* constant, the Poisson-process shape itself is standard, not invented.
|
|
302
|
+
*/
|
|
303
|
+
rollIntrusiveThought( userId, now = Date.now(), dt = 1, rateScale = 0.0006 ) {
|
|
304
|
+
|
|
305
|
+
const wound = this.getUnresolvedMemories( userId, 1 )[ 0 ]
|
|
306
|
+
if ( !wound ) return null
|
|
307
|
+
|
|
308
|
+
const priority = this.getZeigarnikPriority( wound, now )
|
|
309
|
+
const probability = 1 - Math.exp( -rateScale * priority * dt )
|
|
310
|
+
if ( Math.random() >= probability ) return null
|
|
311
|
+
|
|
312
|
+
return { entry: wound, probability }
|
|
313
|
+
|
|
314
|
+
}
|
|
315
|
+
|
|
258
316
|
getInfluentialMemories( topK = 5 ) {
|
|
259
317
|
|
|
260
318
|
return [ ...this.memories ]
|
|
@@ -7,6 +7,21 @@
|
|
|
7
7
|
* for this system's timescale, not a reproduction of his measured constants.
|
|
8
8
|
* Entries flagged `permanent` (emotional peak > 0.9, set by
|
|
9
9
|
* EpisodicMemory.store) bypass the curve entirely and never get pruned.
|
|
10
|
+
*
|
|
11
|
+
* Real bug fixed here (found by IntelligentMockGenerator's
|
|
12
|
+
* `rupture_implies_wound_created` invariant, a long-horizon scenario with
|
|
13
|
+
* enough accumulated tick(dt) to cross pruneBelow): an `unresolved` wound
|
|
14
|
+
* was ONLY protected from pruning if it also happened to be `permanent`.
|
|
15
|
+
* EpisodicMemory.js's own store() documents the real intent — "it stays
|
|
16
|
+
* unresolved until something explicitly heals it" — but ForgettingCurve
|
|
17
|
+
* never actually enforced that; an unresolved memory below the permanence
|
|
18
|
+
* magnitude threshold (most of them) could silently vanish from
|
|
19
|
+
* episodicMemory.memories after enough idle/tick time while the REST of
|
|
20
|
+
* the system (getUnresolvedMemories(), the "HERIDA SIN RESOLVER" system-
|
|
21
|
+
* prompt block, LoveHateEngine's own rupture wound) kept assuming it was
|
|
22
|
+
* still there. Fixed by exempting `unresolved` the same way `permanent`
|
|
23
|
+
* already was — it can still be healed by markResolved() (after which it
|
|
24
|
+
* decays normally), it just can't be forgotten by time alone.
|
|
10
25
|
*/
|
|
11
26
|
export class ForgettingCurve {
|
|
12
27
|
|
|
@@ -16,7 +31,7 @@ export class ForgettingCurve {
|
|
|
16
31
|
|
|
17
32
|
episodicMemory.memories = episodicMemory.memories.filter( entry => {
|
|
18
33
|
|
|
19
|
-
if ( entry.permanent ) return true
|
|
34
|
+
if ( entry.permanent || entry.resolution === 'unresolved' ) return true
|
|
20
35
|
|
|
21
36
|
entry.retention *= Math.exp( -dt / ( stability * ( 1 + entry.importance ) ) )
|
|
22
37
|
return entry.retention > pruneBelow
|