muse-agent-example/tests/契约/test_角色能力与配置.py
zizi 367d01db21 W06 模型宿主、角色、预算与流式合同:模型宿主合同、角色策略冻结、预算预留结算与流式回合证据。
按 R2 串行阶段整理提交;包内文件为该阶段交付(含后续小增量),状态以工作包清单为准。
2026-09-10 19:25:41 +08:00

67 lines
2.6 KiB
Python

"""角色策略来自可审配置;不发模型请求。"""
from pathlib import Path
import pytest
import yaml
from muse.任务运行.接口 import 模型协议错误, 角色策略目录
仓库根 = Path(__file__).resolve().parents[2]
源码根 = 仓库根 / "src" / "muse"
@pytest.mark.parametrize("角色", ["writer", "planner", "judge"], ids=["writer", "planner", "judge"])
def test_固定角色拒绝降低模型能力__a63001(角色) -> None:
目录 = 角色策略目录(
yaml.safe_load((Path(__file__).resolve().parents[2] / "配置/角色策略.yaml").read_text())
)
with pytest.raises(模型协议错误):
目录.冻结(角色, provider="configured", model="deepseek-v4-flash", 阶段="生成")
固定 = 目录.冻结(
角色, provider="configured", model="claude-opus-4-8[1M]", thinking="high", 阶段="生成"
)
assert 固定.允许实际模型 == ("claude-opus-4-8[1M]", "claude-opus-4-8")
def test_写手生成与盲评拒绝工具__a63002() -> None:
目录 = 角色策略目录(
yaml.safe_load((Path(__file__).resolve().parents[2] / "配置/角色策略.yaml").read_text())
)
for 角色, 阶段 in [("writer", "生成"), ("blind_judge", "执行")]:
with pytest.raises(模型协议错误):
目录.冻结(
角色, provider="configured", model="claude-opus-4-8[1M]", 工具=("read",), 阶段=阶段
)
assert (
目录.冻结("semantic_detector", provider="configured", model="MiniMax-M3").角色 == "detector"
)
def test_active_runtime_has_no_embedded_api_token__5274c6() -> None:
"""活跃运行代码与配置模板不得内嵌凭据;密钥只能来自受控存储引用。"""
import re
模式 = [
re.compile(r"sk-[A-Za-z0-9]{20,}"),
re.compile(
r"(?:api[-_]?key|token|secret|password)\s*[:=]\s*[\"'][^\"{}$\n]{8,}[\"']", re.I
),
]
违规 = []
for 文件 in 源码根.rglob("*.py"):
if "__pycache__" in 文件.parts:
continue
文本 = 文件.read_text(encoding="utf-8")
for 规则 in 模式:
命中 = 规则.search(文本)
if 命中:
违规.append(f"{文件.relative_to(仓库根)}: {命中.group(0)[:24]}…")
for 文件 in (仓库根 / "配置").glob("*.toml"):
文本 = 文件.read_text(encoding="utf-8")
for 规则 in 模式:
命中 = 规则.search(文本)
if 命中:
违规.append(f"配置/{文件.name}: {命中.group(0)[:24]}…")
assert 违规 == [], "发现内嵌凭据形状:\n" + "\n".join(违规)