feat: 扩展风险规则体系、审批动态路由与预算中心列表化改造
- 新增 25+ 条风险规则(预算/报销/申请/通用类),完善风险规则模拟与反馈发布机制 - 引入费用审批动态路由、平台风险分级、预审与风险阶段管理 - 预算中心列表化改造,优化票据夹仪表盘与数字员工工作看板 - 新增 Hermes 风险线索收集器、Agent 链路追踪中心 - 扩展数字员工能力库(18 个领域 Skill)与交通费用自动预估 - 完善报销申请快速预览、权限控制与前端测试覆盖
This commit is contained in:
@@ -7,13 +7,16 @@ from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.core.agent_enums import AgentAssetDomain, AgentAssetStatus
|
||||
from app.db.base import Base
|
||||
from app.models.agent_asset import AgentAsset, AgentAssetVersion
|
||||
from app.models.agent_asset import AgentAsset, AgentAssetTestRun, AgentAssetVersion
|
||||
from app.schemas.agent_asset import (
|
||||
AgentAssetRiskRuleDraftUpdate,
|
||||
AgentAssetRiskRuleGenerateRequest,
|
||||
AgentAssetRiskRuleRegenerateRequest,
|
||||
AgentAssetRiskRuleRevisionCreate,
|
||||
)
|
||||
from app.services.agent_asset_rule_library import AgentAssetRuleLibraryManager
|
||||
from app.services.agent_asset_risk_rule_regeneration import AgentAssetRiskRuleRegenerationService
|
||||
from app.services.agent_assets import AgentAssetService
|
||||
from app.services.risk_rule_generation import RiskRuleGenerationService
|
||||
from app.services.agent_asset_risk_rule_revision import AgentAssetRiskRuleRevisionService
|
||||
|
||||
@@ -107,10 +110,173 @@ def test_create_revision_draft_for_published_rule_does_not_overwrite_active_vers
|
||||
assert db.query(AgentAssetVersion).filter_by(asset_id=asset_id, version="v0.1.1").one()
|
||||
|
||||
|
||||
def _create_rule(db: Session, tmp_path) -> str:
|
||||
def test_regenerate_unpublished_draft_updates_dsl_and_score(tmp_path) -> None:
|
||||
with build_session() as db:
|
||||
manager = AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules")
|
||||
asset_id = _create_rule(db, tmp_path, manager=manager)
|
||||
updated = AgentAssetRiskRuleRegenerationService(
|
||||
db,
|
||||
rule_library_manager=manager,
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).regenerate(
|
||||
asset_id,
|
||||
AgentAssetRiskRuleRegenerateRequest(
|
||||
rule_title="差旅城市一致性复核",
|
||||
natural_language="差旅报销票据城市与申报目的地不一致时,要求补充说明。",
|
||||
requires_attachment=True,
|
||||
),
|
||||
actor="finance",
|
||||
)
|
||||
|
||||
assert updated.status == AgentAssetStatus.DRAFT.value
|
||||
assert updated.config_json["generation_status"] == "completed"
|
||||
assert updated.config_json["risk_score"] is not None
|
||||
assert updated.config_json["last_operation"]["action"] == "regenerate"
|
||||
payload = manager.read_rule_library_json(
|
||||
library="risk-rules",
|
||||
file_name=updated.config_json["rule_document"]["file_name"],
|
||||
)
|
||||
assert payload["name"] == "差旅城市一致性复核"
|
||||
assert payload["flow_diagram_svg"]
|
||||
assert payload["metadata"]["risk_score"] == updated.config_json["risk_score"]
|
||||
|
||||
|
||||
def test_regenerate_revision_draft_keeps_active_document_unchanged(tmp_path) -> None:
|
||||
with build_session() as db:
|
||||
manager = AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules")
|
||||
asset_id = _create_rule(db, tmp_path, manager=manager)
|
||||
asset = db.get(AgentAsset, asset_id)
|
||||
assert asset is not None
|
||||
active_document = asset.config_json["rule_document"]
|
||||
active_payload_before = manager.read_rule_library_json(
|
||||
library="risk-rules",
|
||||
file_name=active_document["file_name"],
|
||||
)
|
||||
asset.status = AgentAssetStatus.ACTIVE.value
|
||||
asset.published_version = "v0.1.0"
|
||||
asset.current_version = "v0.1.0"
|
||||
asset.working_version = "v0.1.0"
|
||||
db.add(asset)
|
||||
db.flush()
|
||||
|
||||
AgentAssetRiskRuleRevisionService(db).create_revision_draft(
|
||||
asset_id,
|
||||
AgentAssetRiskRuleRevisionCreate(
|
||||
rule_title="票据城市一致性复核",
|
||||
natural_language="票据城市与申报目的地不一致时,要求补充说明。",
|
||||
requires_attachment=True,
|
||||
change_reason="补充城市一致性判断。",
|
||||
),
|
||||
actor="manager",
|
||||
)
|
||||
updated = AgentAssetRiskRuleRegenerationService(
|
||||
db,
|
||||
rule_library_manager=manager,
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).regenerate(
|
||||
asset_id,
|
||||
AgentAssetRiskRuleRegenerateRequest(),
|
||||
actor="manager",
|
||||
)
|
||||
|
||||
revision = updated.config_json["revision_draft"]
|
||||
assert updated.status == AgentAssetStatus.ACTIVE.value
|
||||
assert updated.published_version == "v0.1.0"
|
||||
assert updated.config_json["rule_document"] == active_document
|
||||
assert revision["generation_status"] == "completed"
|
||||
assert revision["risk_score"] is not None
|
||||
assert revision["rule_document"]["file_name"] != active_document["file_name"]
|
||||
active_payload_after = manager.read_rule_library_json(
|
||||
library="risk-rules",
|
||||
file_name=active_document["file_name"],
|
||||
)
|
||||
assert active_payload_after == active_payload_before
|
||||
revision_payload = manager.read_rule_library_json(
|
||||
library="risk-rules",
|
||||
file_name=revision["rule_document"]["file_name"],
|
||||
)
|
||||
assert revision_payload["rule_code"] == updated.code
|
||||
assert revision_payload["enabled"] is False
|
||||
|
||||
detail_service = AgentAssetService(db)
|
||||
detail_service.rule_library_manager = manager
|
||||
displayed = detail_service.read_rule_json(asset_id)
|
||||
assert displayed.file_name == revision["rule_document"]["file_name"]
|
||||
assert displayed.payload["rule_code"] == updated.code
|
||||
|
||||
|
||||
def test_publish_regenerated_revision_replaces_online_document(tmp_path) -> None:
|
||||
with build_session() as db:
|
||||
manager = AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules")
|
||||
asset_id = _create_rule(db, tmp_path, manager=manager)
|
||||
asset = db.get(AgentAsset, asset_id)
|
||||
assert asset is not None
|
||||
old_document = asset.config_json["rule_document"]
|
||||
asset.status = AgentAssetStatus.ACTIVE.value
|
||||
asset.published_version = "v0.1.0"
|
||||
asset.current_version = "v0.1.0"
|
||||
asset.working_version = "v0.1.0"
|
||||
db.add(asset)
|
||||
db.flush()
|
||||
AgentAssetRiskRuleRevisionService(db).create_revision_draft(
|
||||
asset_id,
|
||||
AgentAssetRiskRuleRevisionCreate(
|
||||
rule_title="差旅票据城市复核",
|
||||
natural_language="票据城市与申报目的地不一致时,要求补充说明。",
|
||||
requires_attachment=True,
|
||||
change_reason="补充城市一致性判断。",
|
||||
),
|
||||
actor="manager",
|
||||
)
|
||||
regenerated = AgentAssetRiskRuleRegenerationService(
|
||||
db,
|
||||
rule_library_manager=manager,
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).regenerate(asset_id, AgentAssetRiskRuleRegenerateRequest(), actor="manager")
|
||||
revision = regenerated.config_json["revision_draft"]
|
||||
db.add(
|
||||
AgentAssetTestRun(
|
||||
asset_id=asset_id,
|
||||
version="v0.1.1",
|
||||
test_type="report",
|
||||
status="passed",
|
||||
passed=True,
|
||||
summary="测试报告已确认。",
|
||||
input_json={},
|
||||
result_json={},
|
||||
created_by="manager",
|
||||
)
|
||||
)
|
||||
db.flush()
|
||||
|
||||
service = AgentAssetService(db)
|
||||
service.rule_library_manager = manager
|
||||
published = service.publish_risk_rule(asset_id, actor="manager")
|
||||
|
||||
assert published.status == AgentAssetStatus.ACTIVE.value
|
||||
assert published.current_version == "v0.1.1"
|
||||
assert published.published_version == "v0.1.1"
|
||||
assert "revision_draft" not in published.config_json
|
||||
assert published.config_json["rule_document"] == revision["rule_document"]
|
||||
assert published.config_json["revision_history"][0]["previous_rule_document"] == old_document
|
||||
assert published.config_json["last_operation"]["action"] == "publish_revision"
|
||||
manifest = manager.read_rule_library_json(
|
||||
library="risk-rules",
|
||||
file_name=published.config_json["rule_document"]["file_name"],
|
||||
)
|
||||
assert manifest["enabled"] is True
|
||||
assert manifest["rule_code"] == published.code
|
||||
|
||||
|
||||
def _create_rule(
|
||||
db: Session,
|
||||
tmp_path,
|
||||
*,
|
||||
manager: AgentAssetRuleLibraryManager | None = None,
|
||||
) -> str:
|
||||
return RiskRuleGenerationService(
|
||||
db,
|
||||
rule_library_manager=AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules"),
|
||||
rule_library_manager=manager or AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules"),
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).generate_rule_asset(
|
||||
AgentAssetRiskRuleGenerateRequest(
|
||||
|
||||
Reference in New Issue
Block a user