muse-agent-example/tests/集成/test_规则改进与回执.py
zizi 9e6f1c4481 R2 改造交付:新版模块化单体全量成果
- src/muse 新版全模块(装配/共享/上下文/任务运行/作品规划/故事世界/正文写作/审校修订/知识方法/作者经验/效果评测/交付连载/资料研究/正式变更/元数据/接入/基础设施/编排)+ 测试树(单元/契约/集成/架构/迁移/端到端/夹具)
- 129 项功能全部实现与自动验证(功能覆盖.json/矩阵),含 W31 补齐的规则与代价/节奏安排/伏笔与承诺
- 旧实现按处置清单退出(702 条中 324 删,保护合同与未迁移条目留存有据);web/app.py 旧工作台退役,新工作台为唯一写入口
- 数据库/旧库迁移:真实旧库内容批次迁移链(端点守卫/PG作品正文映射/质量资产缺省投影)
- 运行手册 docs/运行手册.md;W30 本机服务阶段一已运行(infra PG 为正式内容权威)
- R2 执行证据与私有运行材料在 .agents.local/改造/R2-20260909/(不入库)
2026-09-15 12:47:42 +08:00

268 lines
10 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.

"""B07规则改进只登记意向;B10验证与B06审阅、启停、消费保持各自owner。"""
from dataclasses import asdict, replace
from datetime import UTC, datetime, timedelta
from uuid import uuid4
import pytest
import test_规则诊断资格与消费 as 规则测试
from muse.作者经验.存储 import 经验存储
from muse.作者经验.接口 import (
反馈版本,
反馈请求,
改进定位,
改进请求,
规则启停请求,
规则改进请求,
规则验证请求,
)
from muse.共享.调用身份 import 用途
from muse.共享.错误 import Muse错误
from muse.审校修订.接口 import 规则状态命令
from muse.正式变更.接口 import 固定哈希
from muse.正文写作.接口 import 文本节点, 正文草稿, 段落, 读取候选依据
pytestmark = pytest.mark.数据库
规则资格环境 = 规则测试.规则资格环境
def _规则改进(env):
author = env["author"]
service = env["app"].要求作者经验()
body = env["app"].要求正文()
candidate = body.创建人工候选(
author,
"rule-feedback-candidate",
"rule-ch",
env["target"]["revision"],
正文草稿((段落("p1", (文本节点("值得注意的是,门外的雨已经停了。"),)),)),
)["results"][0]
with env["pools"][用途.生产].连接(只读=True) as conn:
changed = 读取候选依据(conn, author.作者, candidate["candidate_id"], 1)
feedback = service.记录反馈(
author,
"rule-improvement-feedback",
反馈请求(
"rule-work",
反馈版本(
"document",
"rule-ch",
env["target"]["revision"],
env["target"]["document_hash"],
),
反馈版本("candidate", candidate["candidate_id"], 1, changed["document_hash"]),
"这处提示语需要识别,但不能改掉人物明确保留的口头禅。",
"revise",
),
)
version = env["review"].读取规则版本(author, "l002", 1)
request = 规则改进请求(
improvement_id=str(uuid4()),
expected_revision=0,
title="校准提示语规则",
rationale="依据实际正文反馈补足正例、负例与保护表达验证。",
rule_id="l002",
rule_version=1,
rule_hash=固定哈希(version["payload"]["rule"]),
feedback_ids=(str(feedback["feedback_id"]),),
)
row = service.提出规则改进(author, request)
point = 改进定位(request.improvement_id, row["revision"], row["content_hash"])
return service, request, point
def _启停请求(action, proposal, opened, revision, credential_id=""):
return 规则启停请求(
action=action,
rule_id=proposal.rule_id,
rule_version=proposal.rule_version,
rule_hash=proposal.rule_hash,
expected_activation_revision=revision,
review_id=opened["review_id"],
review_hash=opened["review_hash"],
credential_id=credential_id,
)
def test_固定规则验证到B06启停消费及原回执恢复__26d101(规则资格环境, monkeypatch):
env = 规则资格环境
service, proposal, point = _规则改进(env)
published = 规则测试._publish(env, "b07-owned-rule-improvement", "owned")
direct = env["evaluate"].执行规则诊断验证(env["evaluation_identity"], published["version_id"])
proof = env["maintain"].导出规则诊断凭据(
env["maintenance_identity"],
direct["run_id"],
批准引用="rule-improvement-owner-proof",
有效期=datetime.now(UTC) + timedelta(hours=1),
)
command = 规则状态命令(
proposal.rule_id,
proposal.rule_version,
"enable",
expected_activation_revision=0,
credential_id=proof["receipt_id"],
)
opened = env["review"].打开规则状态审阅(env["author"], command)
enable = _启停请求("enable", proposal, opened, 0, proof["receipt_id"])
with pytest.raises(Muse错误, match="尚未登记同一目标"):
service.请求规则启停(env["author"], "enable-before-validation", point, enable)
with pytest.raises(Muse错误, match="审阅身份"):
replace(enable, review_id="")
validation = service.申请规则验证(
env["author"],
"validate-rule-improvement",
point,
env["evaluate"],
env["evaluation_identity"],
规则验证请求(
dataset_version_id=published["version_id"],
rule_id=proposal.rule_id,
rule_version=proposal.rule_version,
rule_hash=proposal.rule_hash,
),
)
assert set(validation["receipt"]) == {"run_id", "target", "result_hash"}
assert validation["receipt"]["run_id"] == direct["run_id"]
assert validation["receipt"]["target"] == published["target"]
assert "experiment_id" not in validation["receipt"]
assert (
service.申请规则验证(
env["author"],
"validate-rule-improvement",
point,
env["evaluate"],
env["evaluation_identity"],
规则验证请求(
published["version_id"],
proposal.rule_id,
proposal.rule_version,
proposal.rule_hash,
),
)["receipt"]
== validation["receipt"]
)
save_result = 经验存储.保存改进结果
failures = {"enable-rule": 1}
def interrupt_once(self, author, command_id, receipt):
if failures.get(command_id, 0):
failures[command_id] -= 1
raise RuntimeError("B06已经提交,B07登记前中断")
return save_result(self, author, command_id, receipt)
monkeypatch.setattr(经验存储, "保存改进结果", interrupt_once)
with pytest.raises(RuntimeError, match="登记前中断"):
service.请求规则启停(env["author"], "enable-rule", point, enable)
active = env["review"].读取规则状态(env["author"], proposal.rule_id)
assert active["current"]["state"] == "active"
pending = next(
action
for action in service.读取改进(env["author"], proposal.improvement_id)["actions"]
if action["kind"] == "enable"
)
assert pending["receipt"] is None
original = service.正式.读取回执(env["author"], "B07.owner:" + str(pending["action_id"]))
diagnosis = env["review"].生成诊断(env["author"], env["target"])
assert (
diagnosis["report"]["rule_bindings"][0]["version_id"] == published["target"]["version_id"]
)
env["evaluate"].停止规则诊断验证(
env["evaluation_identity"], direct["run_id"], "stop-after-real-consumption"
)
recovered = service.请求规则启停(env["author"], "enable-rule", point, enable)
assert recovered["receipt"] == original
disable_command = 规则状态命令(
proposal.rule_id,
proposal.rule_version,
"disable",
expected_activation_revision=1,
)
disable_opened = env["review"].打开规则状态审阅(env["author"], disable_command)
disabled = service.请求规则启停(
env["author"],
"disable-rule",
point,
_启停请求("disable", proposal, disable_opened, 1),
)
assert disabled["receipt"]["results"][0]["state"] == "disabled"
detail = service.读取改进(env["author"], proposal.improvement_id)
assert detail["target_state"] == "disabled"
assert detail["rule_state"]["current"]["activation_revision"] == 2
assert len(detail["consumption"]) == 1
assert detail["consumption"][0]["diagnosis_id"] == diagnosis["diagnosis_id"]
assert detail["consumption"][0]["binding"]["credential_id"] == proof["receipt_id"]
with pytest.raises(Muse错误, match="凭据不可用|凭据.*失效"):
env["review"].打开规则状态审阅(
env["author"],
规则状态命令(
proposal.rule_id,
proposal.rule_version,
"enable",
expected_activation_revision=2,
credential_id=proof["receipt_id"],
),
)
def test_异版他作者与空反馈不产生规则改进行动__26d102(规则资格环境):
env = 规则资格环境
service, proposal, point = _规则改进(env)
published = 规则测试._publish(env, "b07-rule-negative", "owned")
with pytest.raises(Muse错误, match="不是请求点名"):
service.申请规则验证(
env["author"],
"wrong-rule-version",
point,
env["evaluate"],
env["evaluation_identity"],
规则验证请求(published["version_id"], "l002", 2, proposal.rule_hash),
)
with pytest.raises(Muse错误, match="非空"):
replace(proposal, improvement_id=str(uuid4()), feedback_ids=())
stranger = replace(env["author"], 作者="other-author")
with pytest.raises(Muse错误):
service.提出规则改进(stranger, replace(proposal, improvement_id=str(uuid4())))
with pytest.raises(Muse错误):
service.读取改进(stranger, proposal.improvement_id)
first = service.读取改进(env["author"], proposal.improvement_id)
assert first["actions"] == []
revised = service.提出规则改进(
env["author"],
replace(
proposal,
expected_revision=1,
rationale="作者补充了保护表达必须保留的改进理由。",
),
)
assert revised["revision"] == 2
historical = service.读取改进(env["author"], proposal.improvement_id, revision=1)
assert historical["content_hash"] == point.content_hash
def test_旧方法改进请求固定字节不增加规则默认字段__26d103():
request = 改进请求(
"11111111-1111-1111-1111-111111111111",
0,
"旧方法标题",
"旧方法理由",
"22222222-2222-2222-2222-222222222222",
("33333333-3333-3333-3333-333333333333",),
)
assert set(asdict(request)) == {
"improvement_id",
"expected_revision",
"title",
"rationale",
"method_version_id",
"feedback_ids",
}
assert (
固定哈希(asdict(request))
== "8c3b0f01e086c99906c9a5e239ea82c26a9b612badb05c234a2465f7c645d955"
)