feat: 优化运行时判定

This commit is contained in:
richarjiang
2026-09-24 14:27:07 +08:00
parent dfd3998399
commit f0ac603f27
25 changed files with 2368 additions and 58 deletions

View File

@@ -79,6 +79,52 @@ def test_success_output(monkeypatch, tmp_path):
assert "hint only" in context
def test_pre_tool_deny_and_post_tool_context(monkeypatch, tmp_path):
monkeypatch.setenv("LAYA_ROUTER_HOME", str(tmp_path))
def respond(settings, path, body, timeout):
assert path == "/event"
assert timeout == settings.event_timeout
if body["event"] == "PreToolUse":
return {"action": "deny", "reason": "do not delete", "context": ""}
return {"action": "inject", "context": "replan", "reason": ""}
monkeypatch.setattr("laya_router.hook.post_json", respond)
denied = json.loads(
handle(
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {"command": "rm -rf /"}},
{},
"claude",
)
)
assert denied["hookSpecificOutput"]["permissionDecision"] == "deny"
posted = json.loads(
handle(
{"hook_event_name": "PostToolUse", "tool_name": "Bash", "tool_input": {"command": "pytest"}},
{},
"codex",
)
)
assert posted["hookSpecificOutput"]["hookEventName"] == "PostToolUse"
assert posted["hookSpecificOutput"]["additionalContext"] == "replan"
def test_stop_block_and_fail_open(monkeypatch, tmp_path):
monkeypatch.setenv("LAYA_ROUTER_HOME", str(tmp_path))
monkeypatch.setattr(
"laya_router.hook.post_json",
lambda settings, path, body, timeout: {"action": "block", "reason": "run pytest"},
)
blocked = json.loads(handle({"hook_event_name": "Stop", "stop_hook_active": False}, {}, "codex"))
assert blocked == {"decision": "block", "reason": "run pytest"}
def boom(settings, path, body, timeout):
raise URLError("connection refused")
monkeypatch.setattr("laya_router.hook.post_json", boom)
assert handle({"hook_event_name": "Stop"}, {}, "claude") == "{}"
def _payload():
return {
"level": "normal",