fix: reject invalid and duplicate Work writes
This commit is contained in:
@@ -11,6 +11,7 @@ from agent_platform.runtime.loop import (
|
||||
AgentLoop,
|
||||
is_substantive_execution,
|
||||
normalize_write_file_content,
|
||||
python_source_error,
|
||||
select_tool_specs,
|
||||
tool_event_details,
|
||||
)
|
||||
@@ -39,6 +40,13 @@ def test_write_file_normalizes_double_escaped_plain_text() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_invalid_complete_python_source_is_detected_before_write() -> None:
|
||||
error = python_source_error({"path": "benchmark.py", "content": "# Report\n- O(n²)\n"})
|
||||
assert error is not None
|
||||
assert "invalid character" in error
|
||||
assert python_source_error({"path": "benchmark.py", "content": "print('ok')\n"}) is None
|
||||
|
||||
|
||||
def test_read_only_shell_commands_do_not_satisfy_execution_evidence() -> None:
|
||||
assert not is_substantive_execution("exec", {"command": "cat report.txt"})
|
||||
assert not is_substantive_execution("exec", {"command": "sed -n '1,20p' script.py"})
|
||||
@@ -509,6 +517,49 @@ async def test_duplicate_commands_in_one_model_response_execute_once(settings) -
|
||||
await store.close()
|
||||
|
||||
|
||||
async def test_duplicate_identical_writes_execute_once(settings) -> None:
|
||||
batch = {
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"tool_calls": [
|
||||
tool_call("1", "write_file", {"path": "sort.py", "content": "print('ok')"}),
|
||||
tool_call("2", "write_file", {"path": "sort.py", "content": "print('ok')"}),
|
||||
tool_call("3", "exec", {"command": "python3 sort.py"}),
|
||||
],
|
||||
},
|
||||
"finish_reason": "tool_calls",
|
||||
}
|
||||
]
|
||||
}
|
||||
final = {"choices": [{"message": {"role": "assistant", "content": "完成"}, "finish_reason": "stop"}]}
|
||||
provider = ScriptedProvider([batch, final])
|
||||
registry = RecordingRegistry()
|
||||
store = RuntimeStore(settings.database_url)
|
||||
await store.initialize()
|
||||
|
||||
async def callback(event_type, payload):
|
||||
return None
|
||||
|
||||
try:
|
||||
answer = await AgentLoop(provider, registry, store, max_tool_output_chars=10_000).run(
|
||||
spec=get_model_spec("work-light"),
|
||||
messages=[{"role": "user", "content": "写脚本并运行"}],
|
||||
identity=UserIdentity("u1", "", "", "user"),
|
||||
raw_user_jwt="jwt",
|
||||
chat_id="c1",
|
||||
callback=callback,
|
||||
)
|
||||
assert answer == "完成"
|
||||
assert [name for name, _ in registry.calls] == ["write_file", "exec"]
|
||||
duplicate = provider.requests[1]["messages"][-2]
|
||||
assert duplicate["role"] == "tool"
|
||||
assert "same path and identical content" in duplicate["content"]
|
||||
finally:
|
||||
await store.close()
|
||||
|
||||
|
||||
async def test_script_delivery_requires_successful_execution(settings) -> None:
|
||||
write = {
|
||||
"choices": [
|
||||
|
||||
Reference in New Issue
Block a user