mirror of
https://github.com/usestrix/strix.git
synced 2026-11-18 09:42:48 +00:00
fix(agents): stop repeat polls of dead PTY sessions
This commit is contained in:
parent
4ac5dc65e4
commit
a6aec4e712
2 changed files with 109 additions and 5 deletions
|
|
@ -7,7 +7,7 @@ import inspect
|
|||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
from agents.agent import ToolsToFinalOutputResult
|
||||
from agents.sandbox import SandboxAgent
|
||||
|
|
@ -395,6 +395,13 @@ def _make_filesystem_configurator(
|
|||
|
||||
|
||||
_CHARS_ESCAPE_RE = re.compile(r"\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|[0abtnvfr\\])")
|
||||
_PTY_SESSION_NOT_FOUND_RE = re.compile(
|
||||
r"write_stdin failed: PTY session not found: (?P<session_id>\d+)"
|
||||
)
|
||||
_WRITE_STDIN_DEAD_SESSION_RESULT_TEMPLATE = (
|
||||
"write_stdin: session {session_id} is already terminal/non-retryable "
|
||||
"after PTY session not found."
|
||||
)
|
||||
_CHARS_ESCAPE_MAP = {
|
||||
"\\\\": "\\",
|
||||
"\\n": "\n",
|
||||
|
|
@ -515,21 +522,42 @@ def _wrap_exec_command(tool: FunctionTool) -> FunctionTool:
|
|||
|
||||
def _wrap_write_stdin(tool: FunctionTool) -> FunctionTool:
|
||||
invoke_tool = tool.on_invoke_tool
|
||||
dead_pty_session_ids: set[int] = set()
|
||||
|
||||
async def invoke(ctx: Any, raw_input: str) -> Any:
|
||||
parsed: dict[str, Any] | None
|
||||
try:
|
||||
parsed = json.loads(raw_input)
|
||||
raw_parsed: Any = json.loads(raw_input)
|
||||
except json.JSONDecodeError:
|
||||
parsed = None
|
||||
else:
|
||||
parsed = cast("dict[str, Any]", raw_parsed) if isinstance(raw_parsed, dict) else None
|
||||
session_id = parsed.get("session_id") if isinstance(parsed, dict) else None
|
||||
chars = parsed.get("chars") if isinstance(parsed, dict) else None
|
||||
# An empty poll sends no input: `chars` is "" or omitted entirely (None).
|
||||
# Both must hit the dead-session guard, otherwise dropping `chars` reopens
|
||||
# the exact repeat-poll storm this wrapper exists to stop.
|
||||
is_empty_poll = chars is None or chars == ""
|
||||
if isinstance(session_id, int) and is_empty_poll and session_id in dead_pty_session_ids:
|
||||
return _WRITE_STDIN_DEAD_SESSION_RESULT_TEMPLATE.format(session_id=session_id)
|
||||
if isinstance(parsed, dict):
|
||||
if isinstance(parsed.get("chars"), str):
|
||||
parsed["chars"] = _decode_chars_escape(parsed["chars"])
|
||||
if isinstance(chars, str):
|
||||
parsed["chars"] = _decode_chars_escape(chars)
|
||||
_apply_shell_output_cap(parsed)
|
||||
raw_input = json.dumps(parsed)
|
||||
try:
|
||||
return await invoke_tool(ctx, raw_input)
|
||||
result = await invoke_tool(ctx, raw_input)
|
||||
except ValidationError as exc:
|
||||
return _format_validation_error(tool.name, exc)
|
||||
if isinstance(result, str):
|
||||
# Only a result that is *exactly* the terminal failure marks the
|
||||
# session dead. A live poll whose captured output merely contains
|
||||
# this text must never poison the session (cf. the running-marker
|
||||
# guard in _annotate_running_non_tty).
|
||||
match = _PTY_SESSION_NOT_FOUND_RE.fullmatch(result.strip())
|
||||
if match is not None:
|
||||
dead_pty_session_ids.add(int(match.group("session_id")))
|
||||
return result
|
||||
|
||||
tool.on_invoke_tool = invoke
|
||||
return tool
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ from strix.agents import factory
|
|||
from strix.config import load_settings
|
||||
|
||||
|
||||
_MISSING_PTY_SESSION_ID = 60418
|
||||
_MISSING_PTY_RESULT = f"write_stdin failed: PTY session not found: {_MISSING_PTY_SESSION_ID}"
|
||||
|
||||
|
||||
def _capturing_exec_tool(captured: dict[str, str]) -> FunctionTool:
|
||||
async def invoke(_ctx: Any, raw_input: str) -> str:
|
||||
captured["raw_input"] = raw_input
|
||||
|
|
@ -26,6 +30,19 @@ def _capturing_exec_tool(captured: dict[str, str]) -> FunctionTool:
|
|||
)
|
||||
|
||||
|
||||
def _capturing_write_stdin_tool(results: list[str], captured: list[str]) -> FunctionTool:
|
||||
async def invoke(_ctx: Any, raw_input: str) -> str:
|
||||
captured.append(raw_input)
|
||||
return results.pop(0)
|
||||
|
||||
return FunctionTool(
|
||||
name="write_stdin",
|
||||
description="test tool",
|
||||
params_json_schema={"type": "object", "properties": {}},
|
||||
on_invoke_tool=invoke,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wrap_exec_command_defaults_shell_to_bash() -> None:
|
||||
captured: dict[str, str] = {}
|
||||
|
|
@ -215,3 +232,62 @@ def test_view_image_is_left_out_for_text_only_models(
|
|||
|
||||
view_image = next(tool for tool in filesystem.tools() if tool.name == "view_image")
|
||||
assert cast("FunctionTool", view_image).is_enabled is has_view_image
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wrap_write_stdin_blocks_repeat_empty_poll_after_dead_pty() -> None:
|
||||
captured: list[str] = []
|
||||
wrapped = factory._wrap_write_stdin(
|
||||
_capturing_write_stdin_tool([_MISSING_PTY_RESULT], captured)
|
||||
)
|
||||
raw_input = json.dumps({"session_id": _MISSING_PTY_SESSION_ID, "chars": ""})
|
||||
|
||||
first_result = await wrapped.on_invoke_tool(cast("Any", None), raw_input)
|
||||
second_result = await wrapped.on_invoke_tool(cast("Any", None), raw_input)
|
||||
|
||||
assert first_result == _MISSING_PTY_RESULT
|
||||
assert "non-retryable" in second_result
|
||||
assert len(captured) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wrap_write_stdin_does_not_mark_session_dead_from_echoed_output() -> None:
|
||||
# A live poll whose captured output merely echoes the not-found text must
|
||||
# not poison the session; later empty polls must still reach the tool.
|
||||
live_output = (
|
||||
f"Process running with session ID {_MISSING_PTY_SESSION_ID}\nOutput:\n"
|
||||
f"{_MISSING_PTY_RESULT}\n"
|
||||
)
|
||||
captured: list[str] = []
|
||||
wrapped = factory._wrap_write_stdin(
|
||||
_capturing_write_stdin_tool([live_output, live_output], captured)
|
||||
)
|
||||
raw_input = json.dumps({"session_id": _MISSING_PTY_SESSION_ID, "chars": ""})
|
||||
|
||||
first_result = await wrapped.on_invoke_tool(cast("Any", None), raw_input)
|
||||
second_result = await wrapped.on_invoke_tool(cast("Any", None), raw_input)
|
||||
|
||||
assert first_result == live_output
|
||||
assert second_result == live_output
|
||||
assert len(captured) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wrap_write_stdin_blocks_repeat_poll_when_chars_is_omitted() -> None:
|
||||
# An empty poll may omit `chars` entirely; a known-dead session must still
|
||||
# be short-circuited rather than polled again.
|
||||
captured: list[str] = []
|
||||
wrapped = factory._wrap_write_stdin(
|
||||
_capturing_write_stdin_tool([_MISSING_PTY_RESULT, _MISSING_PTY_RESULT], captured)
|
||||
)
|
||||
|
||||
await wrapped.on_invoke_tool(
|
||||
cast("Any", None),
|
||||
json.dumps({"session_id": _MISSING_PTY_SESSION_ID, "chars": ""}),
|
||||
)
|
||||
second_result = await wrapped.on_invoke_tool(
|
||||
cast("Any", None), json.dumps({"session_id": _MISSING_PTY_SESSION_ID})
|
||||
)
|
||||
|
||||
assert "non-retryable" in second_result
|
||||
assert len(captured) == 1
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue