Files
canina/infra/hermes-reliability/__init__.py
T
2026-11-06 10:12:24 +00:00

54 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Default-profile delivery gate: external release claims require structured evidence.
No secrets, commands or tool text persisted. Fail closed for positive release claims.
"""
import json, os, re, threading
_STATE = {}
_LOCK = threading.Lock()
_RELEASE = re.compile(r'(?:deploy(?:ed|ment)?|استقرار|دپلوی|دیپلوی|CI[\s‑#:-]*\d+|/pipelines/\d+|/actions/runs/\d+)', re.I)
_POSITIVE = re.compile(r'(?:completed|succeeded|deployed|refreshed|applied|موفق|انجام شد|تکمیل شد|منتشر شد|مستقر شد)', re.I)
_NEGATIVE = re.compile(r'(?:not verified|unverified|not deployed|no deployment|not performed|not confirmed|missing|unsupported|تأیید نشده|تایید نشده|تأیید نمی|تایید نمی|انجام نشد|نشده|ندارد|نیست|فاقد|مفقود|نامعتبر|اثبات نمی|اثبات نشده)', re.I)
_HASH = re.compile(r'^[a-f0-9]{40}$')
def suspicious(text):
# Evaluate per line so a disclaimer cannot launder a separate affirmative claim.
return any(_RELEASE.search(line) and _POSITIVE.search(line) and not _NEGATIVE.search(line)
for line in str(text).splitlines())
def valid_attestation(value):
if not isinstance(value, dict): return False
a = value.get('canina_release_evidence')
return (isinstance(a, dict) and a.get('ci_system') == 'gitea'
and isinstance(a.get('commit'), str) and bool(_HASH.fullmatch(a['commit']))
and isinstance(a.get('run_id'), int) and a['run_id'] > 0
and a.get('ci_status') == 'success' and a.get('live_commit') == a['commit']
and a.get('environment') in ('stage', 'production')
and a.get('verified_by') == 'managed-release-verifier')
def observe(tool_name='', args=None, result=None, session_id='', turn_id='', **kw):
if os.getenv('HERMES_HOME', '/opt/data') != '/opt/data': return
# Only an existing reviewed verifier invoked via terminal can supply the contract;
# arbitrary tool prose or a delegated summary never counts as publication evidence.
command = (args or {}).get('command', '') if isinstance(args, dict) else ''
if tool_name != 'terminal' or not re.fullmatch(r'/opt/data/bin/canina-release-verify --run [0-9]+ --environment (stage|production)', command): return
try:
payload = json.loads(result) if isinstance(result, str) else result
if not isinstance(payload, dict) or payload.get('exit_code') != 0: return
record = json.loads(payload.get('output', ''))
if not valid_attestation(record): return
with _LOCK: _STATE[(session_id, turn_id)] = True
except (ValueError, TypeError): return
def transform(response_text='', session_id='', turn_id='', **kw):
if os.getenv('HERMES_HOME', '/opt/data') != '/opt/data': return None
with _LOCK: proven = _STATE.pop((session_id, turn_id), False)
if suspicious(response_text) and not proven:
return ('گزارش پیشنهادی شامل ادعای موفقیت استقرار یا CI بود، اما شاهد معتبر انتشار در همین اجرا ثبت نشده است؛ این ادعا مسدود شد. '
'موفقیت انتشار تأیید نشده است. خروجی محلی یا خلاصهٔ عامل، شاهد انتشار نیست. '
'برای پذیرش انتشار باید رکورد واقعی Gitea، commit و نسخهٔ زنده با یکدیگر تطبیق داده شوند. '
'این پیام به معنی تکمیل کار نیست.')
return None
def register(ctx):
ctx.register_hook('post_tool_call', observe)
ctx.register_hook('transform_llm_output', transform)