Add tool failure recovery decisions

This commit is contained in:
2026-05-11 00:33:06 +08:00
parent 717e931a5e
commit 1b4f4c836e
5 changed files with 206 additions and 30 deletions
+26 -10
View File
@@ -1,7 +1,7 @@
import json
from pathlib import Path
from app.core.contracts import UserTask
from app.core.contracts import ExecutionDirective, UserTask
from app.runtime.runtime_controller import RuntimeController
@@ -112,21 +112,37 @@ def test_shell_exec_allows_safe_command(tmp_path: Path) -> None:
assert str(tmp_path) in result["result"]["output"]
def test_shell_exec_treats_grep_no_matches_as_information(tmp_path: Path) -> None:
class _RecoveryCritic:
async def generate(self, prompt: str, max_tokens: int | None = None) -> str:
return '{"action":"continue","reason":"No matches is acceptable information for this exploratory check."}'
def test_failed_shell_step_can_recover_and_continue(tmp_path: Path) -> None:
_write_config_tree(tmp_path)
controller = RuntimeController(base_dir=tmp_path)
result = controller.handle_task(
controller.execution_engine.set_critic(_RecoveryCritic())
controller.execution_engine._recovery_limit = 1
result = controller.execution_engine.execute(
UserTask(
input="run grep with no matches",
context={
"requested_tool": "shell_exec",
"tool_args": {"command": "printf 'abc\\n' | grep definitely_missing"},
input="run grep with no matches and recover",
),
ExecutionDirective(
type="plan",
payload={
"steps": [
{
"id": "1",
"tool": "shell_exec",
"args": {"command": "printf 'abc\\n' | grep definitely_missing"},
"depends_on": [],
}
]
},
)
),
)
assert result["status"] == "completed"
assert result["result"]["metadata"]["exit_code"] == 1
assert result["result"]["metadata"]["no_matches"] is True
failed_result = result["result"]["step_results"][0]["result"]["result"]
assert failed_result["metadata"]["exit_code"] == 1
def test_permission_resolution_can_resume_task(tmp_path: Path) -> None: