MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / _compute_topic_staleness

Function _compute_topic_staleness

scripts/drift_engine.py:474–506  ·  view source on GitHub ↗
(
    profile: Dict[str, Any],
    *,
    stale_topics_pool: List[str],
    window_episodes: int,
    min_total_selected: int,
)

Source from the content-addressed store, hash-verified

472
473def _compute_topic_staleness(
474 profile: Dict[str, Any],
475 *,
476 stale_topics_pool: List[str],
477 window_episodes: int,
478 min_total_selected: int,
479) -> Dict[str, Any]:
480 history = list(profile.get("reading_history") or [])
481 if not history:
482 return {"decayed_topics": [], "topic_staleness": {}}
483
484 recent_history = history[-max(1, window_episodes):]
485 selected_events = [
486 entry for entry in recent_history
487 if str(entry.get("action") or "").strip().lower() == "selected"
488 ]
489 if len(selected_events) < min_total_selected:
490 return {"decayed_topics": [], "topic_staleness": {}}
491
492 topic_staleness: Dict[str, float] = {}
493 decayed_topics: List[str] = []
494 for topic in stale_topics_pool:
495 hits = 0
496 for entry in selected_events:
497 entry_topics = _canonicalize_topics(list(entry.get("topics", []) or []))
498 if topic in entry_topics:
499 hits += 1
500 stale_ratio = 1.0 - (hits / max(1, len(selected_events)))
501 topic_staleness[topic] = round(stale_ratio, 3)
502 if hits == 0:
503 decayed_topics.append(topic)
504
505 return {"decayed_topics": decayed_topics, "topic_staleness": topic_staleness}
506
507
508def _find_stable_anchor_candidate(drift_state: Dict[str, Any], *, required_consecutive_days: int) -> Optional[Dict[str, Any]]:
509 window = list(drift_state.get("signal_window", []) or [])

Callers 1

_trigger_satisfiedFunction · 0.85

Calls 2

_canonicalize_topicsFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected