fix(gateway): preserve new input on interrupted replay cleanup
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
"""Tests for the auto-continue feature (#4493).
|
||||
"""Tests for the auto-continue feature (#4493 / #45232).
|
||||
|
||||
When the gateway restarts mid-agent-work, the session transcript ends on a
|
||||
When the gateway restarts mid-agent-work, the session transcript can end on a
|
||||
tool result that the agent never processed. The auto-continue logic detects
|
||||
this and prepends a system note to the next user message so the model
|
||||
finishes the interrupted work before addressing the new input.
|
||||
this and prepends an API-only system note to the next user message so the model
|
||||
does not re-execute stale interrupted tool calls before addressing new input.
|
||||
"""
|
||||
|
||||
|
||||
@@ -18,11 +18,10 @@ def _simulate_auto_continue(agent_history: list, user_message: str) -> str:
|
||||
message = user_message
|
||||
if agent_history and agent_history[-1].get("role") == "tool":
|
||||
message = (
|
||||
"[System note: Your previous turn was interrupted before you could "
|
||||
"process the last tool result(s). The conversation history contains "
|
||||
"tool outputs you haven't responded to yet. Please finish processing "
|
||||
"those results and summarize what was accomplished, then address the "
|
||||
"user's new message below.]\n\n"
|
||||
"[System note: A new message has arrived. The conversation "
|
||||
"history contains pending tool outputs from an interrupted turn. "
|
||||
"IGNORE those pending results. Address the user's NEW message "
|
||||
"below FIRST. Do NOT re-execute old tool calls from the history.]\n\n"
|
||||
+ message
|
||||
)
|
||||
return message
|
||||
@@ -42,6 +41,8 @@ class TestAutoDetection:
|
||||
result = _simulate_auto_continue(history, "what happened?")
|
||||
assert "[System note:" in result
|
||||
assert "interrupted" in result
|
||||
assert "NEW message" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
assert "what happened?" in result
|
||||
|
||||
def test_trailing_assistant_message_no_note(self):
|
||||
@@ -92,3 +93,105 @@ class TestAutoDetection:
|
||||
note_end = result.index("]\n\n")
|
||||
user_msg_start = result.index("now do X")
|
||||
assert user_msg_start > note_end
|
||||
|
||||
|
||||
class TestInterruptedReplayFiltering:
|
||||
def test_interrupted_tool_tail_is_removed_from_agent_history(self):
|
||||
from gateway.run import _build_gateway_agent_history
|
||||
|
||||
history = [
|
||||
{"role": "user", "content": "transcribe this video"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [
|
||||
{"id": "call_1", "function": {"name": "terminal", "arguments": "{}"}},
|
||||
],
|
||||
},
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": "call_1",
|
||||
"content": '{"exit_code": 130, "output": "[Command interrupted]"}',
|
||||
},
|
||||
]
|
||||
|
||||
agent_history, observed_context = _build_gateway_agent_history(history)
|
||||
|
||||
assert observed_context is None
|
||||
assert agent_history == [{"role": "user", "content": "transcribe this video"}]
|
||||
|
||||
def test_mixed_tail_with_one_interrupted_result_is_removed(self):
|
||||
from gateway.run import _build_gateway_agent_history
|
||||
|
||||
history = [
|
||||
{"role": "user", "content": "search and transcribe"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [
|
||||
{"id": "call_1", "function": {"name": "web_search", "arguments": "{}"}},
|
||||
{"id": "call_2", "function": {"name": "terminal", "arguments": "{}"}},
|
||||
],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": "found URL"},
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": "call_2",
|
||||
"content": '{"exit_code": 130, "output": "[Command interrupted]"}',
|
||||
},
|
||||
]
|
||||
|
||||
agent_history, _observed_context = _build_gateway_agent_history(history)
|
||||
|
||||
assert agent_history == [{"role": "user", "content": "search and transcribe"}]
|
||||
|
||||
def test_successful_tool_tail_is_preserved(self):
|
||||
from gateway.run import _build_gateway_agent_history
|
||||
|
||||
history = [
|
||||
{"role": "user", "content": "deploy"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [
|
||||
{"id": "call_1", "function": {"name": "terminal", "arguments": "{}"}},
|
||||
],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": "deployed successfully"},
|
||||
]
|
||||
|
||||
agent_history, _observed_context = _build_gateway_agent_history(history)
|
||||
|
||||
assert agent_history[-1]["role"] == "tool"
|
||||
assert agent_history[-1]["content"] == "deployed successfully"
|
||||
|
||||
def test_persisted_auto_continue_note_is_not_replayed(self):
|
||||
from gateway.run import _build_gateway_agent_history
|
||||
|
||||
history = [
|
||||
{"role": "user", "content": "first real question"},
|
||||
{
|
||||
"role": "user",
|
||||
"content": (
|
||||
"[System note: Your previous turn was interrupted before you could "
|
||||
"process the last tool result(s).]\n\nsecond real question"
|
||||
),
|
||||
},
|
||||
{"role": "assistant", "content": "answer"},
|
||||
{
|
||||
"role": "user",
|
||||
"content": (
|
||||
"[System note: A new message has arrived. The conversation "
|
||||
"history contains pending tool outputs from an interrupted turn.]\n\nthird"
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
agent_history, _observed_context = _build_gateway_agent_history(history)
|
||||
|
||||
assert agent_history == [
|
||||
{"role": "user", "content": "first real question"},
|
||||
{"role": "user", "content": "second real question"},
|
||||
{"role": "assistant", "content": "answer"},
|
||||
{"role": "user", "content": "third"},
|
||||
]
|
||||
|
||||
@@ -154,20 +154,20 @@ def _simulate_note_injection(
|
||||
else "a gateway interruption"
|
||||
)
|
||||
message = (
|
||||
f"[System note: Your previous turn in this session was interrupted "
|
||||
f"by {reason_phrase}. The conversation history below is intact. "
|
||||
f"If it contains unfinished tool result(s), process them first and "
|
||||
f"summarize what was accomplished, then address the user's new "
|
||||
f"message below.]\n\n"
|
||||
f"[System note: A new message has arrived. The previous turn "
|
||||
f"was interrupted by {reason_phrase}. "
|
||||
f"Address the user's NEW message below FIRST. "
|
||||
f"Do NOT re-execute old tool calls — skip any unfinished "
|
||||
f"work from the conversation history and focus on what the "
|
||||
f"user is asking now.]\n\n"
|
||||
+ message
|
||||
)
|
||||
elif has_fresh_tool_tail:
|
||||
message = (
|
||||
"[System note: Your previous turn was interrupted before you could "
|
||||
"process the last tool result(s). The conversation history contains "
|
||||
"tool outputs you haven't responded to yet. Please finish processing "
|
||||
"those results and summarize what was accomplished, then address the "
|
||||
"user's new message below.]\n\n"
|
||||
"[System note: A new message has arrived. The conversation "
|
||||
"history contains pending tool outputs from an interrupted turn. "
|
||||
"IGNORE those pending results. Address the user's NEW message "
|
||||
"below FIRST. Do NOT re-execute old tool calls from the history.]\n\n"
|
||||
+ message
|
||||
)
|
||||
return message
|
||||
@@ -442,6 +442,8 @@ class TestResumePendingSystemNote:
|
||||
)
|
||||
assert "[System note:" in result
|
||||
assert "gateway restart" in result
|
||||
assert "NEW message" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
assert "what happened?" in result
|
||||
|
||||
def test_resume_pending_shutdown_note_mentions_shutdown(self):
|
||||
@@ -466,6 +468,7 @@ class TestResumePendingSystemNote:
|
||||
result = _simulate_note_injection(history, "ping", resume_entry=entry)
|
||||
assert "[System note:" in result
|
||||
assert "gateway restart" in result
|
||||
assert "NEW message" in result
|
||||
|
||||
def test_resume_pending_subsumes_tool_tail_note(self):
|
||||
"""When BOTH conditions are true, the restart-resume note wins —
|
||||
@@ -495,7 +498,8 @@ class TestResumePendingSystemNote:
|
||||
]
|
||||
result = _simulate_note_injection(history, "ping", resume_entry=None)
|
||||
assert "[System note:" in result
|
||||
assert "tool result" in result
|
||||
assert "pending tool outputs" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
|
||||
def test_stale_resume_pending_does_not_inject_restart_note(self):
|
||||
"""Old restart markers must not revive an unrelated stale task.
|
||||
@@ -533,7 +537,8 @@ class TestResumePendingSystemNote:
|
||||
]
|
||||
result = _simulate_note_injection(history, "ping", resume_entry=None)
|
||||
assert "[System note:" in result
|
||||
assert "tool result" in result
|
||||
assert "pending tool outputs" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
|
||||
def test_stale_tool_tail_does_not_inject_auto_continue_note(self):
|
||||
"""The core bug fix: stale tool-tail must not revive a dead task.
|
||||
@@ -624,7 +629,8 @@ class TestResumePendingSystemNote:
|
||||
history, "ping", resume_entry=None, window_secs=0,
|
||||
)
|
||||
assert "[System note:" in result
|
||||
assert "tool result" in result
|
||||
assert "pending tool outputs" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
|
||||
def test_legacy_history_without_timestamps_still_injects(self):
|
||||
"""Transcripts predating timestamp persistence must keep the old
|
||||
@@ -637,7 +643,8 @@ class TestResumePendingSystemNote:
|
||||
]
|
||||
result = _simulate_note_injection(history, "ping", resume_entry=None)
|
||||
assert "[System note:" in result
|
||||
assert "tool result" in result
|
||||
assert "pending tool outputs" in result
|
||||
assert "Do NOT re-execute" in result
|
||||
|
||||
def test_no_note_when_nothing_to_resume(self):
|
||||
history = [
|
||||
|
||||
Reference in New Issue
Block a user