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

65 lines
2.2 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.

"""工具范围的独立协议测试;合成读取器不代表B09真实数据库权限。"""
import pytest
from muse.任务运行.接口 import (
只读工具集,
工具定义,
工具来源,
工具结果,
工具范围,
工具请求,
模型协议错误,
)
def test_工具只能消费冻结范围且返回可追溯版本__a65001() -> None:
记录 = []
def 读取(范围, 条件):
assert 范围.作品ID == "work-a" and 范围.截止位置 == 3
assert 条件 == {"query": "桥"}
return 工具结果("第三章的桥", (工具来源("source-a", "3", "schema1", "projection1"),))
定义 = 工具定义(
"read",
"读取指定来源",
{
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
"additionalProperties": False,
},
读取,
)
集合 = 只读工具集(
(定义,),
("read",),
工具范围("task", "work-a", ("source-a",), 3, "production", "generation"),
记录.append,
)
assert 集合.调用(工具请求("call1", "read", {"query": "桥"})).内容 == "第三章的桥"
assert 记录[0]["source_refs"][0]["revision"] == "3"
with pytest.raises(模型协议错误):
集合.调用(工具请求("call2", "read", {"query": "桥", "work_id": "work-b"}))
with pytest.raises(模型协议错误):
集合.调用(工具请求("call3", "write", {"query": "桥"}))
assert len(记录) == 1
def test_所需工具未加载和越界来源均拒绝__a65002() -> None:
范围 = 工具范围("task", "work-a", ("source-a",), 3, "production", "generation")
with pytest.raises(模型协议错误):
只读工具集((), ("read",), 范围, lambda _: None)
工具 = 工具定义(
"read",
"读取",
{"type": "object"},
lambda *_: 工具结果("不应送入模型", (工具来源("source-b", "1", "s", "p"),)),
)
记录 = []
集合 = 只读工具集((工具,), ("read",), 范围, 记录.append)
with pytest.raises(模型协议错误):
集合.调用(工具请求("call", "read", {}))
assert not 记录