memory / adaptive_threshold.py — AdaptiveThreshold
Self-tuning recall acceptance threshold. Trades precision for recall based on observed usefulness.
Storage
- JSON at
memory_dir/adaptive_threshold.json. - Atomic temp +
os.replace;threading.RLock.
State
{ "threshold": float, "window": [(score, was_useful), ...] } # window cap = 100
WINDOW_SIZE = 100 · MIN_OBSERVATIONS = 20 before recalculation kicks in.
API
| Method | Purpose |
|---|---|
__init__(memory_dir, initial=0.7) | Seed threshold (overridden by --smart-memory CLI flag). |
record(score, was_useful) | Record observation, recalculate threshold. |
get_threshold() -> float | Current threshold. |
get_stats() -> dict | Diagnostics: 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.