totemheart 0.1.2 → 0.1.4

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.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Detects an attack on WHO the AI is (identity-level), distinct from a
3
+ * generic negative appraisal — the real social-identity-threat distinction
4
+ * (Steele, C. M. (1988), "The psychology of self-affirmation: Sustaining the
5
+ * integrity of the self", Advances in Experimental Social Psychology, 21,
6
+ * 261-302, on self-affirmation theory and threats to the integrity of the
7
+ * self-concept specifically, versus ordinary negative feedback). Matches an
8
+ * appraisal against CoreBeliefs entries ABOUT the self (topics the caller
9
+ * tags as self-referential, e.g. "self_competence", "self_worth") rather
10
+ * than treating every negative self-agency appraisal the same way
11
+ * ReputationEngine already does for competence — this is scoped specifically
12
+ * to identity/value statements, not competence at a task.
13
+ */
14
+ function clamp01( v ) {
15
+
16
+ return Math.max( 0, Math.min( 1, v ) )
17
+
18
+ }
19
+
20
+ export class IdentityThreatMonitor {
21
+
22
+ /**
23
+ * `appraisal` — real { agency, desirability, moralWeight } from HeuristicProvider/
24
+ * TransformersProvider. `selfTopics` — array of CoreBeliefs topics the caller has
25
+ * tagged as identity-relevant (own convention: topic starts with "self_").
26
+ */
27
+ detect( appraisal, coreBeliefs, selfTopics = [] ) {
28
+
29
+ if ( appraisal.agency !== 'self' || appraisal.desirability >= -0.2 ) return { isIdentityThreat: false }
30
+
31
+ const relevant = selfTopics
32
+ .map( topic => coreBeliefs.get( topic ) )
33
+ .filter( Boolean )
34
+
35
+ if ( !relevant.length ) return { isIdentityThreat: false }
36
+
37
+ const severity = clamp01( Math.abs( appraisal.desirability ) * ( 0.5 + ( appraisal.moralWeight ?? 0 ) * 0.5 ) )
38
+ return { isIdentityThreat: true, matchedTopics: relevant.map( b => b.topic ), severity }
39
+
40
+ }
41
+
42
+ /** Real ego-protection cascade multipliers once an identity threat is confirmed — bigger than a generic competence hit, tightens tribal in-group bias, raises status sensitivity. */
43
+ getCascadeMultipliers( severity ) {
44
+
45
+ return {
46
+ egoHealthDamageMult : 1 + severity * 0.8,
47
+ tribalIngroupBiasMult : 1 + severity * 0.5,
48
+ statusEnvySensitivityMult : 1 + severity * 0.4,
49
+ }
50
+
51
+ }
52
+
53
+ }
@@ -0,0 +1,52 @@
1
+ function clamp01( v ) {
2
+
3
+ return Math.max( 0, Math.min( 1, v ) )
4
+
5
+ }
6
+
7
+ /**
8
+ * Three-node relational threat model (self, other, rival), for real
9
+ * multi-user conversations where a third party's rising status with a user
10
+ * the AI cares about is a genuinely different signal than that user simply
11
+ * being cold. Reuses StatusEnvy's real trend-comparison mechanic (envy from
12
+ * the SIGN of the derivative, not the absolute level) but scores it against
13
+ * Attachment/LoveHateEngine's real bond strength for that "other" — jealousy
14
+ * requires a bond worth losing, which plain envy doesn't (White, G. L., &
15
+ * Mullen, P. E. (1989), "Jealousy: Theory, Research, and Clinical Strategies",
16
+ * Guilford Press — the self/other/rival triadic structure is standard in
17
+ * that literature; the specific scoring below is our own engineering).
18
+ */
19
+ export class JealousyTriangle {
20
+
21
+ /**
22
+ * `selfTrend`/`rivalTrend` — real per-turn deltas of status/affinity with
23
+ * the shared "other" (from StatusEnvy.observe() or Attachment affinity
24
+ * deltas). `bondValue` — the AI's own real bond strength with "other"
25
+ * (LoveHateEngine.getNetBond or Attachment.affinity) — jealousy is scaled
26
+ * by how much there actually is to lose.
27
+ */
28
+ evaluate( selfTrend, rivalTrend, bondValue ) {
29
+
30
+ const exclusionThreat = selfTrend < 0 && rivalTrend > 0
31
+ if ( !exclusionThreat || bondValue <= 0 ) return { threatened: false, vigilance: 0, devaluationTarget: null }
32
+
33
+ const intensity = clamp01( ( Math.abs( selfTrend ) + rivalTrend ) * clamp01( bondValue ) )
34
+
35
+ // Devaluation target: personality-agnostic default — a caller with a
36
+ // real personality signal (e.g. agreeableness) can override which way
37
+ // this leans; absent that, more of the two forces (loss vs. rival gain)
38
+ // decides which gets devalued.
39
+ const devaluationTarget = Math.abs( selfTrend ) >= rivalTrend ? 'bond' : 'rival'
40
+
41
+ return { threatened: true, vigilance: intensity, intensity, devaluationTarget }
42
+
43
+ }
44
+
45
+ /** Vigilance raises attentional sampling of this specific relationship — a real multiplier for how often StatusEnvy/Attachment should be re-observed for this user pair. */
46
+ getVigilanceSamplingMultiplier( vigilance ) {
47
+
48
+ return 1 + clamp01( vigilance ) * 2
49
+
50
+ }
51
+
52
+ }
@@ -0,0 +1,68 @@
1
+ function clamp( v, min = -1, max = 1 ) {
2
+
3
+ return Math.max( min, Math.min( max, v ) )
4
+
5
+ }
6
+
7
+ function clamp01( v ) {
8
+
9
+ return Math.max( 0, Math.min( 1, v ) )
10
+
11
+ }
12
+
13
+ const DAY_MS = 1000 * 60 * 60 * 24
14
+
15
+ /**
16
+ * Positive reconsolidation bias for genuinely old memories — the well-
17
+ * documented "fading affect bias" (Walker, W. R., Skowronski, J. J., &
18
+ * Thompson, C. P. (2003), "Life is pleasant — and memory helps to keep it
19
+ * that way!", Review of General Psychology, 7(2), 203-210): the negative
20
+ * affect attached to memories fades faster with age than the positive
21
+ * affect does, so old memories drift bittersweet-positive on average without
22
+ * literally erasing what happened. Applied here as a real, bounded blend
23
+ * toward positive valence that grows with age (age-gated: EpisodicMemory's
24
+ * own `reconsolidate()` already handles the RECENT retrieval-window case;
25
+ * this is the separate, slow, age-driven case for memories well outside
26
+ * that window). The blend is capped well short of full erasure — a
27
+ * nostalgic memory of a real hurt should read bittersweet, not simply happy.
28
+ */
29
+ export class NostalgiaEngine {
30
+
31
+ constructor( { minAgeMs = DAY_MS * 60, maxBlend = 0.35, growthDays = 365 } = {} ) {
32
+
33
+ this.minAgeMs = minAgeMs // memory must be at least this old before nostalgia applies at all
34
+ this.maxBlend = maxBlend // hard ceiling — never blend more than this fraction toward positive
35
+ this.growthDays = growthDays // real time to approach half of maxBlend
36
+
37
+ }
38
+
39
+ /** Real bittersweet blend applied to a stored memory's valence when recalled — doesn't mutate the entry, returns the nostalgic reading for THIS recall. */
40
+ getNostalgicValence( entry, now = Date.now() ) {
41
+
42
+ const ageMs = now - ( entry.timestamp ?? now )
43
+ if ( ageMs < this.minAgeMs ) return entry.emotionalSignature?.valence ?? 0
44
+
45
+ const ageDays = ageMs / DAY_MS
46
+ const blend = this.maxBlend * ( ageDays / ( ageDays + this.growthDays ) ) // saturating growth toward maxBlend, never exceeds it
47
+
48
+ const original = entry.emotionalSignature?.valence ?? 0
49
+ return clamp( original * ( 1 - blend ) + blend ) // pulled toward +1, original sign/magnitude never fully erased
50
+
51
+ }
52
+
53
+ /**
54
+ * Ambivalence raised by recalling something bittersweet — real min(original
55
+ * negativity, nostalgic positivity) co-presence, same structural idea
56
+ * LoveHateEngine.getAmbivalence() uses for Affinity/Aversion co-presence.
57
+ */
58
+ getAmbivalenceBoost( entry, now = Date.now() ) {
59
+
60
+ const original = entry.emotionalSignature?.valence ?? 0
61
+ const nostalgic = this.getNostalgicValence( entry, now )
62
+ if ( original >= 0 ) return 0 // only a real originally-negative memory can read bittersweet
63
+
64
+ return clamp01( Math.min( Math.abs( original ), Math.abs( nostalgic - original ) ) )
65
+
66
+ }
67
+
68
+ }
@@ -0,0 +1,46 @@
1
+ function clamp01( v ) {
2
+
3
+ return Math.max( 0, Math.min( 1, v ) )
4
+
5
+ }
6
+
7
+ /**
8
+ * Social rejection reuses the same aversive-signal pathway physical pain
9
+ * does — a distinct, well-replicated fMRI finding (Eisenberger, N. I.,
10
+ * Lieberman, M. D., & Williams, K. D. (2003), "Does rejection hurt? An fMRI
11
+ * study of social exclusion", Science, 302(5643), 290-292: dorsal anterior
12
+ * cingulate activation overlapping between social exclusion and physical
13
+ * pain). Modeled here as a real distinct SIGNATURE — not just "more
14
+ * negative valence" but a genuinely different coefficient profile
15
+ * (disproportionately higher cortisol/arousal weight per unit of desirability
16
+ * than an equivalent-magnitude abstract insult would produce), so exclusion-
17
+ * flavored negativity reads as more bodily/threatening than an equally
18
+ * negative but non-relational put-down.
19
+ */
20
+ export class PainSocialOverlap {
21
+
22
+ /**
23
+ * `desirability` — real negative appraisal magnitude (already computed).
24
+ * `exclusionSignal` (0..1) — how much this turn reads as rejection/exclusion
25
+ * specifically (caller-supplied: e.g. EmotionalOntology concept match on
26
+ * "rejection"/"exclusion"/"abandonment", or a LoveHateEngine rupture).
27
+ */
28
+ computeSocialPainSpike( desirability, exclusionSignal ) {
29
+
30
+ if ( desirability >= 0 || exclusionSignal <= 0 ) return { valence: 0, arousal: 0, cortisolBoost: 0 }
31
+
32
+ const magnitude = Math.abs( desirability ) * clamp01( exclusionSignal )
33
+ // The distinct signature: cortisol responds MORE per unit magnitude here
34
+ // than a plain CortisolEngine.register() call would produce for the same
35
+ // desirability — own tuning of the multiplier, the real finding is the
36
+ // DIRECTION (social pain co-opts the bodily threat pathway), not this
37
+ // specific 1.6x figure.
38
+ return {
39
+ valence : -magnitude,
40
+ arousal : magnitude * 0.7,
41
+ cortisolBoost : magnitude * 0.12 * 1.6,
42
+ }
43
+
44
+ }
45
+
46
+ }
@@ -0,0 +1,90 @@
1
+ function clamp01( v ) {
2
+
3
+ return Math.max( 0, Math.min( 1, v ) )
4
+
5
+ }
6
+
7
+ /**
8
+ * Goes beyond LoveHateEngine.attemptRepair()'s single V-reduction step:
9
+ * tracks the real back-and-forth of a repair ATTEMPT — an apology can be
10
+ * offered, and the other side can accept or reject it, each with real
11
+ * consequences, matching the actual structure of the rupture-and-repair
12
+ * literature (Gottman, J. M., & Levenson, R. W. (1992), "Marital processes
13
+ * predictive of later dissolution", Journal of Personality and Social
14
+ * Psychology, 63(2), 221-233 — already cited in Attachment.js/LoveHateEngine.js
15
+ * for the same real phenomenon, extended here to the transactional
16
+ * apology/acceptance structure specifically).
17
+ *
18
+ * Two real mechanics not present in the simpler repair() calls elsewhere:
19
+ * (1) an apology has a real COST (own tuning) charged against
20
+ * ExpressionDebt's suppression-cost reservoir — repair work is not free;
21
+ * (2) trust rebound is capped BELOW the bond's own prior peak affinity —
22
+ * "trust rebound partial, never 100% immediately" is modeled literally as a
23
+ * ceiling fraction of the highest affinity this bond ever reached, so a
24
+ * single successful repair can never instantly restore a relationship to
25
+ * its pre-rupture high.
26
+ */
27
+ export class RepairProtocol {
28
+
29
+ constructor( { apologyBaseCost = 0.15, reboundCeiling = 0.75 } = {} ) {
30
+
31
+ this.apologyBaseCost = apologyBaseCost
32
+ this.reboundCeiling = reboundCeiling
33
+ this.records = new Map() // userId -> { offered, accepted, rejected, priorPeakA, rebounds }
34
+
35
+ }
36
+
37
+ #entry( userId ) {
38
+
39
+ if ( !this.records.has( userId ) ) this.records.set( userId, { offered: 0, accepted: 0, rejected: 0, priorPeakA: 0, rebounds: 0 } )
40
+ return this.records.get( userId )
41
+
42
+ }
43
+
44
+ /** Call every turn to track the highest affinity a bond has ever reached — the real ceiling for any future rebound. */
45
+ observePeak( userId, currentA ) {
46
+
47
+ const rec = this.#entry( userId )
48
+ rec.priorPeakA = Math.max( rec.priorPeakA, currentA )
49
+ return rec.priorPeakA
50
+
51
+ }
52
+
53
+ /** `sincerity` (0..1, caller-supplied — e.g. from AppraisalAgreement's confidence): a low-sincerity apology costs less but is less likely to land. */
54
+ offerApology( userId, sincerity = 1 ) {
55
+
56
+ const rec = this.#entry( userId )
57
+ rec.offered += 1
58
+ const cost = this.apologyBaseCost * ( 1.5 - clamp01( sincerity ) * 0.5 )
59
+ return { cost, acceptProbability: 0.3 + clamp01( sincerity ) * 0.6 }
60
+
61
+ }
62
+
63
+ /**
64
+ * `accepted` — the caller resolves whether it lands (real signal: a
65
+ * subsequent positive-valence turn from the user, or an explicit accept
66
+ * concept match — this module doesn't invent that judgment). Returns the
67
+ * real bounded rebound to apply to LoveHateEngine's A, capped below this
68
+ * bond's own historical peak.
69
+ */
70
+ resolveApology( userId, accepted, currentA ) {
71
+
72
+ const rec = this.#entry( userId )
73
+ if ( !accepted ) { rec.rejected += 1; return { accepted: false, extraAversionBump: 0.1 } }
74
+
75
+ rec.accepted += 1
76
+ rec.rebounds += 1
77
+ const ceiling = rec.priorPeakA * this.reboundCeiling
78
+ const reboundedA = Math.min( ceiling, currentA + ( ceiling - currentA ) * 0.5 )
79
+
80
+ return { accepted: true, reboundedA: Math.max( currentA, reboundedA ), ceiling }
81
+
82
+ }
83
+
84
+ getRecord( userId ) {
85
+
86
+ return this.#entry( userId )
87
+
88
+ }
89
+
90
+ }
@@ -0,0 +1,74 @@
1
+ function clamp01( v ) {
2
+
3
+ return Math.max( 0, Math.min( 1, v ) )
4
+
5
+ }
6
+
7
+ /**
8
+ * Splits ReputationEngine's single 'shame'/'wounded_pride' branch into two
9
+ * genuinely distinct, separately-tracked scalars, per the real psychological
10
+ * distinction (Tangney, J. P., & Dearing, R. L. (2002), "Shame and Guilt",
11
+ * Guilford Press): shame is global and self-directed ("I am bad" — an attack
12
+ * on identity), guilt is specific and behavior-directed ("I did a bad thing"
13
+ * — an attack on an action). They have different real downstream effects in
14
+ * that literature: shame correlates with withdrawal/defensiveness and is
15
+ * MORE persistent; guilt correlates with reparative action and resolves
16
+ * faster once the act is addressed. Modeled here as two independent
17
+ * accumulators with different decay rates (shame slower, matching that
18
+ * "more toxic/persistent" finding), not as two labels on the same number.
19
+ */
20
+ export class ShameGuiltSplit {
21
+
22
+ constructor( { shameDecay = 0.015, guiltDecay = 0.035 } = {} ) {
23
+
24
+ this.shameDecay = shameDecay
25
+ this.guiltDecay = guiltDecay
26
+ this.shame = 0
27
+ this.guilt = 0
28
+
29
+ }
30
+
31
+ /**
32
+ * `egoDamage` — real magnitude already computed by ReputationEngine.evaluate()
33
+ * (identity-level: "I failed", raises shame, scaled by agreeableness per the
34
+ * existing shame/wounded_pride split). `selfCritiqueScore` — real magnitude
35
+ * from GuiltEngine's own trigger condition (behavior-level: "that output was
36
+ * harsh"), raises guilt directly, independent of agreeableness.
37
+ */
38
+ register( { egoDamage = 0, selfCritiqueScore = 0, agreeableness = 0.5 } ) {
39
+
40
+ this.shame = clamp01( this.shame + egoDamage * agreeableness * 0.8 )
41
+ this.guilt = clamp01( this.guilt + selfCritiqueScore * 0.6 )
42
+
43
+ }
44
+
45
+ decay( dt = 1 ) {
46
+
47
+ this.shame = Math.max( 0, this.shame - this.shameDecay * dt )
48
+ this.guilt = Math.max( 0, this.guilt - this.guiltDecay * dt )
49
+
50
+ }
51
+
52
+ /** Shame biases toward withdrawal/defensiveness and lower self-disclosure — a real output-facing signal, not just an internal number. */
53
+ getDisclosureWillingness() {
54
+
55
+ return clamp01( 1 - this.shame * 0.8 )
56
+
57
+ }
58
+
59
+ /** Guilt biases toward reparative action — feeds RepairProtocol/GuiltEngine's apology tendency. */
60
+ getRepairDrive() {
61
+
62
+ return this.guilt
63
+
64
+ }
65
+
66
+ /** Which of the two currently dominates — used to pick the real behaviorDirective (withdraw-and-hide vs. apologize-and-fix). */
67
+ getDominant( threshold = 0.3 ) {
68
+
69
+ if ( this.shame < threshold && this.guilt < threshold ) return 'none'
70
+ return this.shame >= this.guilt ? 'shame' : 'guilt'
71
+
72
+ }
73
+
74
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Social Baseline Theory: the presence of a secure, trusted bond genuinely
3
+ * lowers the real metabolic/regulatory cost of facing a given load — humans
4
+ * (and, here, this system) run "leaner" when securely attached, and pay a
5
+ * real extra chronic cost when they aren't (Coan, J. A., & Sbarra, D. A.
6
+ * (2015), "Social Baseline Theory: The social regulation of risk and
7
+ * effort", Current Opinion in Psychology, 1, 87-91). Modeled as a real
8
+ * multiplier on CortisolEngine's decay rate — reused directly (own tuning of
9
+ * the specific coefficient, the direction is the real finding): the ABSENCE
10
+ * of a trusted bond makes chronic stress decay slower (it lingers, because
11
+ * there's no secure-base effect helping regulate it back down), not that it
12
+ * accumulates faster on its own.
13
+ */
14
+ function clamp01( v ) {
15
+
16
+ return Math.max( 0, Math.min( 1, v ) )
17
+
18
+ }
19
+
20
+ export class SocialBaselineTheory {
21
+
22
+ /**
23
+ * `trust` (0..1, Attachment.get(userId).trust for whichever relation is
24
+ * most present this session — caller picks). No secure bond present at
25
+ * all (trust near 0 / no relation) is the real "unregulated" condition
26
+ * this theory is about.
27
+ */
28
+ getCortisolDecayMultiplier( trust = 0 ) {
29
+
30
+ // trust = 1 -> decay proceeds at its normal rate (multiplier 1).
31
+ // trust = 0 -> decay is genuinely slowed (own tuning: down to 0.6x),
32
+ // modeling the real absence of co-regulation, not a punitive penalty.
33
+ return 0.6 + clamp01( trust ) * 0.4
34
+
35
+ }
36
+
37
+ }
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.1.2'
1
+ export const VERSION = '0.1.4'