fix(运行时): 要求会话提供请求路径

普通插件会话必须显式传入请求路径,启动全量校验和协议错误适配器保留专用入口。同步更新运行时测试调用。
This commit is contained in:
starrrLight committed 2026-10-15 15:59:03 +08:00
1 parent d1f5f69454
commit 1ad508c413
3 files changed
+22 -18

No files matched your search

+3 -1
View File
@@ -298,8 +298,10 @@ class PluginManager:
async def open(self, request_id=None, path=None, adapter_only=False, all_plugins=False): async def open(self, request_id=None, path=None, adapter_only=False, all_plugins=False):
if self.stopping or len(self.sessions) >= self.settings.limits.max_sessions: if self.stopping or len(self.sessions) >= self.settings.limits.max_sessions:
raise Overloaded() raise Overloaded()
if path is None and not adapter_only and not all_plugins:
raise ValueError("path is required when opening a plugin session")
await self.start() await self.start()
plugins = self.plugins if all_plugins or path is None else self.plugins_for_path(path) plugins = self.plugins if all_plugins else self.plugins_for_path(path)
session = Session(self, request_id, plugins) session = Session(self, request_id, plugins)
self.sessions.add(session) self.sessions.add(session)
try: try:
+1 -1
View File
@@ -110,7 +110,7 @@ async def test_lua_llm_list_call_and_callable(tmp_path):
manager = PluginManager(settings) manager = PluginManager(settings)
fake = FakeAdapter() fake = FakeAdapter()
manager.llm.adapters["guard"] = fake manager.llm.adapters["guard"] = fake
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
assert await session.apply("on_request", {"body": "x"}) == [{"body": "x"}] assert await session.apply("on_request", {"body": "x"}) == [{"body": "x"}]
assert len(fake.calls) == 2 assert len(fake.calls) == 2
+18 -16
View File
@@ -47,7 +47,7 @@ async def test_chain_isolation_json_and_disabled(tmp_path):
], ],
) )
for _ in range(2): for _ in range(2):
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
result = await session.apply("on_request", {"body": '{"items":[],"empty":null}'}) result = await session.apply("on_request", {"body": '{"items":[],"empty":null}'})
assert '"count":1' in result[0]["body"] assert '"count":1' in result[0]["body"]
@@ -76,6 +76,8 @@ async def test_host_selects_plugins_once_by_exact_path(tmp_path):
], ],
) )
manager = PluginManager(settings) manager = PluginManager(settings)
with pytest.raises(ValueError, match="path is required"):
await manager.open()
first_session = await manager.open(path="/first") first_session = await manager.open(path="/first")
try: try:
assert await first_session.apply("on_request", {"body": "x"}) == [{"body": "x first"}] assert await first_session.apply("on_request", {"body": "x"}) == [{"body": "x first"}]
@@ -107,7 +109,7 @@ async def test_hold_flush_runs_downstream(tmp_path):
""", """,
], ],
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
assert await session.apply("on_event", {"data": "secret"}) == [] assert await session.apply("on_event", {"data": "secret"}) == []
assert await session.end() == [{"data": "secret checked"}] assert await session.end() == [{"data": "secret checked"}]
@@ -125,7 +127,7 @@ async def test_hold_flush_runs_downstream(tmp_path):
) )
async def test_block_bad_results_and_errors(tmp_path, source, caplog): async def test_block_bad_results_and_errors(tmp_path, source, caplog):
manager = manager_for(tmp_path, [source]) manager = manager_for(tmp_path, [source])
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
with pytest.raises((PolicyBlocked, PluginFailure)): with pytest.raises((PolicyBlocked, PluginFailure)):
await session.apply("on_request", {}) await session.apply("on_request", {})
@@ -138,9 +140,9 @@ async def test_timeout_overload_and_recovery(tmp_path):
manager = manager_for( manager = manager_for(
tmp_path, ["return {on_request=function() while true do end end}"], max_sessions=1 tmp_path, ["return {on_request=function() while true do end end}"], max_sessions=1
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
with pytest.raises(Overloaded): with pytest.raises(Overloaded):
await manager.open() await manager.open(path="/chat/stream")
manager.settings = manager.settings.model_copy( manager.settings = manager.settings.model_copy(
update={"limits": manager.settings.limits.model_copy(update={"hook_timeout_seconds": 0.15})} update={"limits": manager.settings.limits.model_copy(update={"hook_timeout_seconds": 0.15})}
) )
@@ -151,13 +153,13 @@ async def test_timeout_overload_and_recovery(tmp_path):
finally: finally:
await session.aclose() await session.aclose()
manager.settings = manager.settings.model_copy(update={"limits": Limits(max_sessions=1)}) manager.settings = manager.settings.model_copy(update={"limits": Limits(max_sessions=1)})
second = await manager.open() second = await manager.open(path="/chat/stream")
await second.aclose() await second.aclose()
async def test_cancel_kills_worker(tmp_path): async def test_cancel_kills_worker(tmp_path):
manager = manager_for(tmp_path, ["return {on_request=function() while true do end end}"]) manager = manager_for(tmp_path, ["return {on_request=function() while true do end end}"])
session = await manager.open() session = await manager.open(path="/chat/stream")
task = asyncio.create_task(session.apply("on_request", {})) task = asyncio.create_task(session.apply("on_request", {}))
await asyncio.sleep(0.05) await asyncio.sleep(0.05)
task.cancel() task.cancel()
@@ -182,7 +184,7 @@ async def test_sandbox_and_python_callback_attributes(tmp_path):
""" """
], ],
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
assert await session.apply("on_request", {"body": "x"}) == [{"body": "x"}] assert await session.apply("on_request", {"body": "x"}) == [{"body": "x"}]
finally: finally:
@@ -209,7 +211,7 @@ async def test_invalid_module_init_and_buffer(tmp_path):
], ],
max_plugin_buffer_bytes=10000, max_plugin_buffer_bytes=10000,
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
with pytest.raises((PluginFailure, LimitExceeded)): with pytest.raises((PluginFailure, LimitExceeded)):
await session.apply("on_event", {"data": "x"}) await session.apply("on_event", {"data": "x"})
@@ -226,7 +228,7 @@ async def test_close_failure_does_not_skip_other_plugins(tmp_path, caplog):
"return {close=function() error('private') end}", "return {close=function() error('private') end}",
], ],
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
await session.aclose() await session.aclose()
assert "plugin_log" in caplog.text assert "plugin_log" in caplog.text
assert "raw-secret" not in caplog.text assert "raw-secret" not in caplog.text
@@ -235,7 +237,7 @@ async def test_close_failure_does_not_skip_other_plugins(tmp_path, caplog):
async def test_example_redacts_cross_event_phone(tmp_path): async def test_example_redacts_cross_event_phone(tmp_path):
source = (Path(__file__).parents[1] / "plugins/stream_redact.lua").read_text(encoding="utf-8") source = (Path(__file__).parents[1] / "plugins/stream_redact.lua").read_text(encoding="utf-8")
manager = manager_for(tmp_path, [source]) manager = manager_for(tmp_path, [source])
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
for content in ["号码138", "1234", "5678"]: for content in ["号码138", "1234", "5678"]:
import json import json
@@ -263,7 +265,7 @@ async def test_lua_memory_exhaustion_and_cyclic_return(tmp_path):
"return {on_request=function() local x={}; x.self=x; return x end}", "return {on_request=function() local x={}; x.self=x; return x end}",
]: ]:
manager = manager_for(tmp_path, [source]) manager = manager_for(tmp_path, [source])
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
with pytest.raises(PluginFailure): with pytest.raises(PluginFailure):
await session.apply("on_request", {}) await session.apply("on_request", {})
@@ -292,7 +294,7 @@ async def test_network_denial_and_cancellation_through_ipc(tmp_path):
end} end}
""" """
manager = manager_for(tmp_path, [source]) manager = manager_for(tmp_path, [source])
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
with pytest.raises(PluginFailure): with pytest.raises(PluginFailure):
await session.apply("on_request", {}) await session.apply("on_request", {})
@@ -309,7 +311,7 @@ async def test_network_denial_and_cancellation_through_ipc(tmp_path):
cancelled.set() cancelled.set()
manager.broker = WaitingBroker() manager.broker = WaitingBroker()
session = await manager.open() session = await manager.open(path="/chat/stream")
task = asyncio.create_task(session.apply("on_request", {})) task = asyncio.create_task(session.apply("on_request", {}))
await asyncio.wait_for(started.wait(), 3) await asyncio.wait_for(started.wait(), 3)
task.cancel() task.cancel()
@@ -340,7 +342,7 @@ async def test_network_example_uses_configuration_and_http_result(tmp_path):
return {"status": 200, "headers": {}, "body": '{"allow":true}'} return {"status": 200, "headers": {}, "body": '{"allow":true}'}
manager.broker = RecordingBroker() manager.broker = RecordingBroker()
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
original = {"body": '{"message":"test"}'} original = {"body": '{"message":"test"}'}
assert await session.apply("on_request", original) == [original] assert await session.apply("on_request", original) == [original]
@@ -359,7 +361,7 @@ async def test_event_fanout_total_is_bounded(tmp_path):
], ],
max_plugin_buffer_bytes=5000, max_plugin_buffer_bytes=5000,
) )
session = await manager.open() session = await manager.open(path="/chat/stream")
try: try:
# Each plugin's individual result fits, but the second stage's accumulated fanout does not. # Each plugin's individual result fits, but the second stage's accumulated fanout does not.
with pytest.raises(LimitExceeded): with pytest.raises(LimitExceeded):