|
52 | 52 | # matches get a +DELTA bump and conflicting branches get -DELTA. |
53 | 53 | _DENDRITIC_DELTA: float = 0.10 |
54 | 54 |
|
| 55 | +# Emotional / mood-congruent rerank blend weights. |
| 56 | +# source: engineering default; calibration in tasks/blend-weight-calibration.md |
| 57 | +# (task #50, 6-knob grid HOPFIELD × HDC × SA × DENDRITIC × EMOTIONAL_RETRIEVAL × |
| 58 | +# MOOD_CONGRUENT). Bower (1981) "Mood and Memory," Am. Psychologist 36(2) |
| 59 | +# does not prescribe a numeric blend weight; the paper's claim is qualitative |
| 60 | +# (mood-congruent recall is faster/more accurate than incongruent), so the |
| 61 | +# magnitude is set conservatively below the perception-side stages. |
| 62 | +_EMOTIONAL_RETRIEVAL_BETA: float = 0.20 |
| 63 | +_MOOD_CONGRUENT_BETA: float = 0.15 |
| 64 | + |
| 65 | +# Below this absolute compound-valence value the query is treated as |
| 66 | +# emotionally neutral and the EMOTIONAL_RETRIEVAL stage no-ops. VADER |
| 67 | +# (Hutto & Gilbert, ICWSM 2014) §4 reports |compound| ≥ 0.05 as a useful |
| 68 | +# positive/negative cutoff for short text; we use 0.10 so that single |
| 69 | +# weakly-loaded tokens do not flip the rerank. |
| 70 | +_EMOTIONAL_QUERY_VALENCE_FLOOR: float = 0.10 |
| 71 | + |
55 | 72 |
|
56 | 73 | # ── Helpers ───────────────────────────────────────────────────────────── |
57 | 74 |
|
@@ -453,3 +470,113 @@ def dendritic_modulate( |
453 | 470 |
|
454 | 471 | modulated.sort(key=lambda c: c.get("score", 0.0), reverse=True) |
455 | 472 | return modulated |
| 473 | + |
| 474 | + |
| 475 | +# ── EMOTIONAL_RETRIEVAL stage ─────────────────────────────────────────── |
| 476 | +# Bower, G.H. (1981). "Mood and Memory." Am. Psychologist 36(2):129-148. |
| 477 | +# Mood-congruent recall: candidates whose stored emotional valence matches |
| 478 | +# the query's inferred valence are retrieved faster and more accurately. |
| 479 | +# Engineering blend via RRF (Cormack et al. SIGIR 2009). |
| 480 | + |
| 481 | + |
| 482 | +def emotional_retrieval_rerank( |
| 483 | + candidates: list[dict[str, Any]], |
| 484 | + query: str, |
| 485 | + *, |
| 486 | + blend_beta: float = _EMOTIONAL_RETRIEVAL_BETA, |
| 487 | + valence_floor: float = _EMOTIONAL_QUERY_VALENCE_FLOOR, |
| 488 | +) -> list[dict[str, Any]]: |
| 489 | + """Rerank by query-valence ↔ candidate-valence congruence. |
| 490 | +
|
| 491 | + Bower (1981): emotionally-congruent material is retrieved faster and |
| 492 | + more accurately than incongruent material. We infer the query's |
| 493 | + affective load via VADER (Hutto & Gilbert, ICWSM 2014), measure each |
| 494 | + candidate's stored ``emotional_valence`` distance from the query |
| 495 | + valence, then RRF-blend that rank with the WRRF rank. |
| 496 | +
|
| 497 | + A neutral query (|valence| < ``valence_floor``) carries no congruence |
| 498 | + signal and the stage no-ops — RRF on a uniform rank would only |
| 499 | + add noise. |
| 500 | +
|
| 501 | + Disabled when ``CORTEX_ABLATE_EMOTIONAL_RETRIEVAL=1`` — returns input |
| 502 | + unchanged. Distinct from MOOD_CONGRUENT_RERANK: this stage uses the |
| 503 | + *query's* valence (per-recall), not a session-level user mood state. |
| 504 | +
|
| 505 | + Sources: |
| 506 | + - Bower, G.H. (1981). "Mood and Memory." Am. Psychologist 36(2). |
| 507 | + - Hutto, C.J. & Gilbert, E. (2014). "VADER: A Parsimonious Rule-based |
| 508 | + Model for Sentiment Analysis of Social Media Text." ICWSM 2014. |
| 509 | + - Cormack, Clarke & Buettcher (2009). RRF blend. |
| 510 | + """ |
| 511 | + if is_mechanism_disabled(Mechanism.EMOTIONAL_RETRIEVAL): |
| 512 | + return candidates |
| 513 | + if not candidates: |
| 514 | + return candidates |
| 515 | + |
| 516 | + from mcp_server.shared.vader import vader_compound |
| 517 | + |
| 518 | + q_valence = vader_compound(query) |
| 519 | + if abs(q_valence) < valence_floor: |
| 520 | + # Neutral query — no useful congruence signal to inject. |
| 521 | + return candidates |
| 522 | + |
| 523 | + def _distance(c: dict[str, Any]) -> float: |
| 524 | + c_v = c.get("emotional_valence", 0.0) or 0.0 |
| 525 | + return abs(float(c_v) - q_valence) |
| 526 | + |
| 527 | + by_match = sorted(enumerate(candidates), key=lambda x: _distance(x[1])) |
| 528 | + mech_ranks = { |
| 529 | + candidates[i]["memory_id"]: rank for rank, (i, _) in enumerate(by_match) |
| 530 | + } |
| 531 | + return _rrf_blend(candidates, mech_ranks, blend_beta) |
| 532 | + |
| 533 | + |
| 534 | +# ── MOOD_CONGRUENT_RERANK stage ──────────────────────────────────────── |
| 535 | +# Bower (1981) mood-state-dependent recall: a person in a given mood |
| 536 | +# preferentially recalls memories acquired (or stored) in that same mood. |
| 537 | +# Distinct from EMOTIONAL_RETRIEVAL — this stage uses a USER session-level |
| 538 | +# mood signal, not the per-query valence. |
| 539 | + |
| 540 | + |
| 541 | +def mood_congruent_rerank( |
| 542 | + candidates: list[dict[str, Any]], |
| 543 | + user_mood: float | None, |
| 544 | + *, |
| 545 | + blend_beta: float = _MOOD_CONGRUENT_BETA, |
| 546 | +) -> list[dict[str, Any]]: |
| 547 | + """Rerank by user-mood ↔ candidate-valence congruence. |
| 548 | +
|
| 549 | + ``user_mood`` is a float in [-1, +1] representing the user's current |
| 550 | + affective state (e.g., set by an upstream emotion classifier or a |
| 551 | + manual ``checkpoint`` annotation). When ``None``, the stage no-ops: |
| 552 | + we do NOT fabricate a mood signal in the absence of one. |
| 553 | +
|
| 554 | + Default policy from Bower (1981): mood-congruent — candidates whose |
| 555 | + stored valence is closer to the user's current mood get a rank boost. |
| 556 | + The boost is small (RRF beta=0.15) so the underlying retrieval order |
| 557 | + still dominates; this is a tie-breaker, not a filter. |
| 558 | +
|
| 559 | + Disabled when ``CORTEX_ABLATE_MOOD_CONGRUENT_RERANK=1`` — returns |
| 560 | + input unchanged. Distinct from EMOTIONAL_RETRIEVAL (which uses the |
| 561 | + query text's inferred valence). |
| 562 | +
|
| 563 | + Sources: |
| 564 | + - Bower, G.H. (1981). "Mood and Memory." Am. Psychologist 36(2). |
| 565 | + - Cormack, Clarke & Buettcher (2009). RRF blend. |
| 566 | + """ |
| 567 | + if is_mechanism_disabled(Mechanism.MOOD_CONGRUENT_RERANK): |
| 568 | + return candidates |
| 569 | + if user_mood is None or not candidates: |
| 570 | + return candidates |
| 571 | + |
| 572 | + user_mood_f = float(user_mood) |
| 573 | + |
| 574 | + def _distance(c: dict[str, Any]) -> float: |
| 575 | + c_v = c.get("emotional_valence", 0.0) or 0.0 |
| 576 | + return abs(float(c_v) - user_mood_f) |
| 577 | + |
| 578 | + by_match = sorted(enumerate(candidates), key=lambda x: _distance(x[1])) |
| 579 | + mech_ranks = { |
| 580 | + candidates[i]["memory_id"]: rank for rank, (i, _) in enumerate(by_match) |
| 581 | + } |
| 582 | + return _rrf_blend(candidates, mech_ranks, blend_beta) |
0 commit comments