refactor(settings): one bounded_float for every numeric bar read from settings
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / integration (push) Successful in 52s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / Python tests (push) Successful in 1m36s
CI & Build / Build & push image (push) Successful in 32s

The shape ledger's divergence readout flagged _gate_setting (#4385).
Reading it turned up a family with no canon: floor_for,
get_duplicate_threshold, get_plan_match_threshold and _gate_setting each
parsed a stored string, fell back to the default (never 0) and clamped
into [lo, 1].

- services/settings.bounded_float(raw, default, lo=0, hi=1) is the pure
  parse, fallback and clamp. Each caller keeps its own get_setting read,
  so tests patching get_setting per module still take effect, and
  _gate_setting still fails open on an unreadable setting.
- SettingsView.saveKbInject: eleven inline Math.min/Math.max clamps and
  the local gateAt become one asBar(v, d, lo = 0), mirroring the server.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-24 12:30:58 -04:00
co-authored by Claude Opus 5.5
parent d5dad587f1
commit 3270fe90c1
4 changed files with 51 additions and 43 deletions
+12 -20
View File
@@ -287,16 +287,16 @@ def _gate_key(note_type: str) -> str:
async def _gate_setting(user_id: int, key: str, lo: float) -> float:
from scribe.services.settings import get_setting
from scribe.services.settings import bounded_float, get_setting
default = GATE_DEFAULT_THRESHOLDS[key]
try:
value = float(await get_setting(user_id, GATE_THRESHOLD_KEYS[key], str(default)))
raw = await get_setting(user_id, GATE_THRESHOLD_KEYS[key], str(default))
except Exception:
# Fail-open like the rest of the gate: an unreadable setting falls back
# to the measured default rather than blocking or waving through.
value = default
return min(1.0, max(lo, value))
raw = None
return bounded_float(raw, default, lo)
async def gate_bars(user_id: int, note_type: str) -> tuple[float, float]:
@@ -557,16 +557,11 @@ _MAX_DUPLICATE_PAIRS = 200
async def get_duplicate_threshold(user_id: int, kind: str = "snippet") -> float:
"""The user's near-duplicate similarity floor for `kind`, clamped to [0, 1]."""
from scribe.services.settings import get_setting
from scribe.services.settings import bounded_float, get_setting
default = DUPLICATE_DEFAULT_THRESHOLDS[kind]
try:
value = float(await get_setting(
user_id, DUPLICATE_THRESHOLD_KEYS[kind], str(default)
))
except (TypeError, ValueError):
value = default
return min(1.0, max(0.0, value))
raw = await get_setting(user_id, DUPLICATE_THRESHOLD_KEYS[kind], str(default))
return bounded_float(raw, default)
def group_pairs(pairs: list[tuple[int, int, float]]) -> list[list[int]]:
@@ -1120,15 +1115,12 @@ PLAN_MATCH_DEFAULT_THRESHOLD = 0.80
async def get_plan_match_threshold(user_id: int) -> float:
"""The user's plan-gate similarity floor, clamped to [0, 1]."""
from scribe.services.settings import get_setting
from scribe.services.settings import bounded_float, get_setting
try:
value = float(await get_setting(
user_id, PLAN_MATCH_THRESHOLD_KEY, str(PLAN_MATCH_DEFAULT_THRESHOLD)
))
except (TypeError, ValueError):
value = PLAN_MATCH_DEFAULT_THRESHOLD
return min(1.0, max(0.0, value))
raw = await get_setting(
user_id, PLAN_MATCH_THRESHOLD_KEY, str(PLAN_MATCH_DEFAULT_THRESHOLD)
)
return bounded_float(raw, PLAN_MATCH_DEFAULT_THRESHOLD)
def plan_candidate_text(