Module guard

Module guard 

Source
Expand description

Scope 守卫 + 限额检查 + 审计日志

两种模式:

  • scope 模式state.is_scope_mode()):必须持有 api-key,且 scope 匹配
  • legacy 模式(没配 keys-file):读工具全放行;写工具走 enable_trading / allow_real_trading 两级开关

Enums§

GuardOutcome
守卫结果:Allow 或携带拒绝原因的 JSON
ToolScope
工具需要的 scope 类别

Functions§

args_short_hash
计算 args 的短哈希(前 8 hex),用于审计日志(不存原始敏感字段)
audit 🔒
审计日志:key_id / tool / result / reason
current_authed_key 🔒
从 KeyStore 里按 id 取当前的 KeyRecord(对齐 SIGHUP 热重载)。
emit_trade_outcome
交易工具执行完后的审计事件:解析 handler 返回的 JSON,success / failure 写入 audit JSONL。key_id = None 时用 “” 占位(legacy 模式)。
require_scope
基础 scope 守卫(用于只读工具)
require_tool_scope
基于注册表的只读 scope 守卫;若工具未登记则 fail-closed 拒绝。
require_trading
交易写守卫:scope + legacy 兼容 + (可选)限额检查 + (可选)per-call key 覆盖
scope_for_tool
工具名 → 所需 scope 的中央注册表