fix: make invalid source retries actionable

This commit is contained in:
wuyang
2026-07-26 17:08:27 +08:00
parent f64380a0f4
commit ade5ea1210
2 changed files with 72 additions and 1 deletions
+46
View File
@@ -59,6 +59,7 @@ 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 "2: - O(n²)" in error
assert python_source_error({"path": "benchmark.py", "content": "print('ok')\n"}) is None
@@ -900,6 +901,51 @@ async def test_bare_interactive_exec_is_blocked_before_registry(settings) -> Non
await store.close()
async def test_unchanged_invalid_python_write_is_not_revalidated(settings) -> None:
invalid_call = {
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [tool_call("1", "write_file", {"path": "broken.py", "content": "print('x'\n"})],
},
"finish_reason": "tool_calls",
}
]
}
repeated_call = copy.deepcopy(invalid_call)
repeated_call["choices"][0]["message"]["tool_calls"][0]["id"] = "2"
final = {"choices": [{"message": {"role": "assistant", "content": "无法完成"}, "finish_reason": "stop"}]}
provider = ScriptedProvider([invalid_call, repeated_call, 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="invalid-repeat",
callback=callback,
)
finally:
await store.close()
assert answer == "无法完成"
assert registry.calls == []
repeated_result = next(
message["content"]
for message in provider.requests[2]["messages"]
if message.get("role") == "tool" and "unchanged write" in message.get("content", "")
)
assert "already failed validation" in repeated_result
def test_tool_events_render_one_friendly_completed_card() -> None:
assert tool_event_details("tool.started", {"name": "exec"}) is None
rendered = tool_event_details(