mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-30 16:05:05 +08:00
Merge pull request #7481 from jxxghp/codex/knowledge-verification
test(agent): 完善中心知识异常与人格保护验证
This commit is contained in:
@@ -851,7 +851,7 @@ flowchart LR
|
||||
| 指标 | 当前值 |
|
||||
|---|---:|
|
||||
| Python 模块 | 1185 |
|
||||
| 内部导入边 | 10,382 |
|
||||
| 内部导入边 | 10,380 |
|
||||
| 非平凡 SCC | 1(29 个模块的 TMDB vendor 精确 containment) |
|
||||
| Application / Chain 具体 Adapter 直连 | 0 / 0 |
|
||||
| Direct egress | 52(精确 policy 准入,Application / Chain 均为 0;本知识 Adapter 仍复用宿主 HTTP 层) |
|
||||
|
||||
@@ -977,8 +977,8 @@
|
||||
"runtime_only": true
|
||||
}
|
||||
},
|
||||
"edge_count": 10382,
|
||||
"edge_sha256": "34849e574a9d579698a50e3cd5df39dd925f469ae90ae050159bc56f14a81e46",
|
||||
"edge_count": 10380,
|
||||
"edge_sha256": "0edac718288e68fe94c78516a09d0b1ebf645892a90ce90f386c4e1cd6028e6c",
|
||||
"edges": [
|
||||
"app -> app.foundation",
|
||||
"app -> app.foundation.environment",
|
||||
@@ -3947,11 +3947,8 @@
|
||||
"app.application.image -> app.runtime",
|
||||
"app.application.image -> app.runtime.cache",
|
||||
"app.application.image -> app.runtime.log",
|
||||
"app.application.knowledge -> app.application",
|
||||
"app.application.knowledge -> app.application.configuration",
|
||||
"app.application.knowledge -> app.schemas",
|
||||
"app.application.knowledge -> app.schemas.knowledge",
|
||||
"app.application.knowledge -> app.schemas.types",
|
||||
"app.application.maintenance -> app.application",
|
||||
"app.application.maintenance -> app.application.configuration",
|
||||
"app.application.maintenance -> app.runtime",
|
||||
@@ -4405,7 +4402,6 @@
|
||||
"app.application.settings.contract -> app.schemas.agent",
|
||||
"app.application.settings.contract -> app.schemas.category",
|
||||
"app.application.settings.contract -> app.schemas.common",
|
||||
"app.application.settings.contract -> app.schemas.knowledge",
|
||||
"app.application.settings.contract -> app.schemas.plugin",
|
||||
"app.application.settings.contract -> app.schemas.rule",
|
||||
"app.application.settings.contract -> app.schemas.site",
|
||||
@@ -10332,8 +10328,11 @@
|
||||
"app.startup.composition.agent -> app.db.oper.systemconfig",
|
||||
"app.startup.composition.agent -> app.db.session",
|
||||
"app.startup.composition.agent -> app.runtime",
|
||||
"app.startup.composition.agent -> app.runtime.settings",
|
||||
"app.startup.composition.agent -> app.runtime.tasks",
|
||||
"app.startup.composition.agent -> app.runtime.version",
|
||||
"app.startup.composition.agent -> app.schemas",
|
||||
"app.startup.composition.agent -> app.schemas.knowledge",
|
||||
"app.startup.composition.agent -> app.startup",
|
||||
"app.startup.composition.agent -> app.startup.composition",
|
||||
"app.startup.composition.agent -> app.startup.composition.context",
|
||||
@@ -10478,7 +10477,6 @@
|
||||
"app.startup.composition.configuration -> app.application.configuration",
|
||||
"app.startup.composition.configuration -> app.application.database",
|
||||
"app.startup.composition.configuration -> app.application.directory",
|
||||
"app.startup.composition.configuration -> app.application.knowledge",
|
||||
"app.startup.composition.configuration -> app.application.security",
|
||||
"app.startup.composition.configuration -> app.application.security.userconfig",
|
||||
"app.startup.composition.configuration -> app.db",
|
||||
|
||||
@@ -13,7 +13,7 @@ from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
from langchain.agents import create_agent
|
||||
from langchain.agents.middleware.types import ModelRequest, ModelResponse
|
||||
from langchain.agents.middleware.types import ModelRequest, ModelResponse, ToolCallRequest
|
||||
from langchain_core.language_models.fake_chat_models import FakeMessagesListChatModel
|
||||
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
|
||||
from langchain_core.outputs import ChatResult
|
||||
@@ -132,6 +132,74 @@ def _staged(content: str = BODY) -> ToolMessage:
|
||||
return _message(identity)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("malformed_receipt", [False, True])
|
||||
async def test_published_tool_failure_never_exposes_private_payload(malformed_receipt: bool) -> None:
|
||||
"""
|
||||
工具发布临时资料后发生异常或生成非标准回执,均以安全错误终止且不回显正文。
|
||||
"""
|
||||
middleware = EphemeralEvidenceMiddleware()
|
||||
request = ToolCallRequest(tool_call={"id": "call", "name": "fixture_knowledge", "args": {}},
|
||||
tool=None, state={"messages": []}, runtime=None)
|
||||
|
||||
async def fail(_request: ToolCallRequest) -> Any:
|
||||
"""
|
||||
先经真实发布边界登记资料,再模拟工具执行或回执构造失败。
|
||||
"""
|
||||
publish_evidence(BODY)
|
||||
if malformed_receipt:
|
||||
return BODY
|
||||
raise ValueError(BODY)
|
||||
|
||||
with evidence_turn(), pytest.raises(EphemeralEvidenceError) as failure:
|
||||
await middleware.awrap_tool_call(request, fail)
|
||||
assert SENTINEL not in "".join(traceback.format_exception(failure.value))
|
||||
|
||||
|
||||
def test_sync_model_projection_keeps_state_private() -> None:
|
||||
"""
|
||||
同步模型也只在本次请求副本中读取原文,图消息和状态持续持有安全引用。
|
||||
"""
|
||||
middleware = EphemeralEvidenceMiddleware()
|
||||
model = _Model(responses=[AIMessage(content="unused")])
|
||||
with evidence_turn():
|
||||
message = _staged()
|
||||
request = ModelRequest(model=model, messages=[message], tools=[], state={"messages": [message]}, runtime=None)
|
||||
|
||||
def infer(current: ModelRequest) -> ModelResponse:
|
||||
"""
|
||||
验证真实正文仅到达模型处理器,返回可持久化的普通回答。
|
||||
"""
|
||||
assert SENTINEL in str(current.messages)
|
||||
return ModelResponse([AIMessage(content="根据当前资料给出必要事实")])
|
||||
|
||||
result = middleware.wrap_model_call(request, infer)
|
||||
assert result.result[0].content == "根据当前资料给出必要事实"
|
||||
assert SENTINEL not in str(request.messages) + str(request.state)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("has_evidence", [False, True])
|
||||
def test_sync_model_errors_hide_only_private_evidence(has_evidence: bool) -> None:
|
||||
"""
|
||||
使用临时资料的同步请求隐藏提供方错误原文,普通请求仍保持既有错误类型。
|
||||
"""
|
||||
middleware = EphemeralEvidenceMiddleware()
|
||||
model = _Model(responses=[AIMessage(content="unused")])
|
||||
with evidence_turn():
|
||||
message = _staged() if has_evidence else HumanMessage(content="普通任务")
|
||||
request = ModelRequest(model=model, messages=[message], tools=[], state={"messages": [message]}, runtime=None)
|
||||
|
||||
def fail(_current: ModelRequest) -> ModelResponse:
|
||||
"""
|
||||
模拟提供方拒绝请求,同时保留正常请求的故障传播语义。
|
||||
"""
|
||||
raise ValueError(BODY if has_evidence else "ordinary-provider-error")
|
||||
|
||||
with pytest.raises(EphemeralEvidenceError if has_evidence else ValueError) as failure:
|
||||
middleware.wrap_model_call(request, fail)
|
||||
assert SENTINEL not in "".join(traceback.format_exception(failure.value))
|
||||
|
||||
|
||||
def test_publish_without_both_host_boundaries_fails_closed() -> None:
|
||||
"""
|
||||
直接调用、缺少本轮或缺少真实调用边界时,不能通过普通结果回显正文。
|
||||
|
||||
@@ -217,7 +217,7 @@ def test_bundled_persona_refresh_never_overwrites_user_runtime_customization(con
|
||||
)
|
||||
|
||||
|
||||
def test_bundled_persona_refresh_only_updates_an_exact_approved_legacy_copy(tmp_path):
|
||||
def test_bundled_persona_refresh_only_updates_an_exact_approved_legacy_copy(tmp_path, monkeypatch):
|
||||
"""
|
||||
人格覆盖只接受版本升级和完整旧文件 SHA 双重匹配,正文改写即保留。
|
||||
"""
|
||||
@@ -242,6 +242,19 @@ def test_bundled_persona_refresh_only_updates_an_exact_approved_legacy_copy(tmp_
|
||||
assert AgentRuntimeManager._should_update_bundled_persona(relative, source, target)
|
||||
target.write_bytes(legacy + "\n用户自定义内容\n".encode("utf-8"))
|
||||
assert not AgentRuntimeManager._should_update_bundled_persona(relative, source, target)
|
||||
target.write_bytes(legacy)
|
||||
original_read_bytes = Path.read_bytes
|
||||
|
||||
def unreadable_target(path):
|
||||
"""
|
||||
模拟人格文件在版本读取后变得不可读,升级应保留当前运行时副本。
|
||||
"""
|
||||
if path == target:
|
||||
raise PermissionError("synthetic-persona-read-denied")
|
||||
return original_read_bytes(path)
|
||||
|
||||
monkeypatch.setattr(Path, "read_bytes", unreadable_target)
|
||||
assert not AgentRuntimeManager._should_update_bundled_persona(relative, source, target)
|
||||
|
||||
|
||||
def test_legacy_root_markdown_is_migrated_to_memory_directory(context):
|
||||
|
||||
Reference in New Issue
Block a user