Files
kefu/wechat_rpa/review_policy.py
T
2026-09-21 10:34:06 +08:00

79 lines
3.1 KiB
Python

"""Shared review decisions; model observation never grants or consumes approval."""
MODES = frozenset({'shadow', 'score_only', 'arbitrate'})
def _mode(value):
value = str(value or '').strip().lower()
return value if value in MODES else ''
def current_judge_mode():
"""Read the active application's synced plan without auth/network calls.
backend_client resolves its own runtime data directory (including frozen
installs). Never select a source checkout or a different user's config.
An absent/invalid mode is unknown, not permission to release legacy work.
"""
try:
import backend_client
settings = backend_client.load_settings()
gateway = settings.get('gateway') or {}
if gateway.get('enabled') and str(gateway.get('url') or '').strip():
roles = (settings.get('model_plan') or {}).get('roles') or {}
return _mode(roles.get('judge_mode'))
except Exception:
return ''
try:
import ai_config
if getattr(ai_config, 'AI_JUDGE_ENABLED', False):
return _mode(getattr(ai_config, 'AI_JUDGE_MODE', ''))
except Exception:
pass
return ''
def judge_review_reason(state, *, current_mode=None):
state = state if isinstance(state, dict) else {}
if str(state.get('judge_risk') or '').strip().lower() != 'high':
return ''
recorded = _mode(state.get('judge_mode'))
# A verdict's explicit mode takes priority over a possibly stale plan cache.
# Consult the current plan only to migrate historical verdicts without a mode.
effective = recorded
if not effective:
effective = current_judge_mode() if current_mode is None else _mode(current_mode)
if effective == 'shadow':
return ''
# Missing historical mode remains conservative until a valid plan arrives.
return '模型裁判评估为高风险'
def selective_review_reason(state, customer_text, reply_text, *, rules, current_mode=None):
reason = judge_review_reason(state, current_mode=current_mode)
if reason:
return reason
combined = (str(customer_text or '') + '\n' + str(reply_text or '')).lower()
for rule in rules or []:
if not isinstance(rule, dict) or not rule.get('enabled', True):
continue
keywords = rule.get('keywords') or []
if not isinstance(keywords, (list, tuple)):
continue
if any(str(word or '').strip().lower() in combined for word in keywords if str(word or '').strip()):
return '命中审核规则:' + (str(rule.get('label') or '').strip() or '未命名规则')
return ''
def can_recheck_review(state):
"""Policy changes cannot resolve delivery or independent manual safeguards."""
if not isinstance(state, dict):
return False
if state.get('send_state') or str(state.get('stage') or '') in {
'sending', 'uncertain', 'unknown', 'receipt_check', 'failed', 'sent_uncommitted',
'manual_takeover', 'error',
}:
return False
return not (state.get('manual_required') or state.get('foreign_draft_text')
or state.get('foreign_draft_at') is not None or state.get('cancel_requested'))