memory / adaptive_threshold.py — AdaptiveThreshold

Self-tuning recall acceptance threshold. Trades precision for recall based on observed usefulness.

Storage

State

{ "threshold": float, "window": [(score, was_useful), ...] }   # window cap = 100

WINDOW_SIZE = 100 · MIN_OBSERVATIONS = 20 before recalculation kicks in.

API

MethodPurpose
__init__(memory_dir, initial=0.7)Seed threshold (overridden by --smart-memory CLI flag).
record(score, was_useful)Record observation, recalculate threshold.
get_threshold() -> floatCurrent threshold.
get_stats() -> dictDiagnostics: observations, useful_rate, etc.

Recalculation rule

The threshold is set to 90 % of the minimum useful score, but never below the median of useless scores. Floor 0.3, ceiling 0.95.

Concurrency

threading.RLock guards both reads and writes. Writes are atomic via temp file.