Files
Dobromir Popov 9abe83b5f4 feat(alpha): complete hardening backlog
Complete the alpha-hardening Ralph task set, including tracker billing/accounting guards, validator fraud-audit primitives, wallet binding proof support, documentation runbooks, and updated tests.

Verification: .venv/bin/python -m compileall -q packages tests; .venv/bin/python -m pytest -q --tb=short (313 passed, 3 skipped, 1 failed: tests/test_mining_cli.py::test_legacy_start_without_port_uses_next_available_port because meshnet-node pid 1263451 is already listening on port 7000).
2026-07-05 21:47:23 +03:00

37 lines
1.2 KiB
Python

"""Passive output-quality tripwires (ADR-0018 §7).
Cheap heuristics over every completed request's text -- no model access, no
extra inference -- that flag likely-degenerate output (repetition loops,
truncation) so ``AdaptiveAuditSampler`` can bump that single request's audit
probability. A flag never strikes or bans a wallet by itself; it only raises
the odds that request gets a real audit (research §8 layer 5).
True perplexity scoring needs the serving model's logits, which the validator
does not have outside an audit call, so it stays roadmap-only (ADR-0018 §8);
this covers the repetition/truncation half of the passive-tripwire layer.
"""
from __future__ import annotations
def detect_output_tripwire(
text: str,
*,
min_words: int = 4,
repetition_threshold: float = 0.4,
) -> bool:
"""Flag empty output or a single word/token dominating the response."""
if not text or not text.strip():
return True
words = text.split()
if len(words) < min_words:
return False
counts: dict[str, int] = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
most_common = max(counts.values())
return (most_common / len(words)) >= repetition_threshold
__all__ = ["detect_output_tripwire"]