fix(runtime): preserve recovery history and prevent unsafe MCP replays (#6537)

* fix(recovery): preserve complete checkpoints and refresh cancellation drafts

Retain earlier tool rounds and consumed follow-ups until the transcript is committed, and classify a fresh session after cancelling active recovery. Keep history and follow-up acknowledgement atomic through the session owner.

* test(recovery): cover completion during cancellation handoff

* fix(gateway): preserve failure exit status after runtime crashes

Report uncaught gateway runtime failures as exit code 1 after the existing cleanup, just like startup failures. Remove the startup-only guard without changing controlled shutdown or supervisor policy.

Reuse lifecycle regression coverage for startup/runtime failure diagnostics and cleanup, and update runtime fault fixtures to expect failure.

Refs: NAN-394

* fix(recovery): make interrupted tool outcomes explicit

* fix(mcp): avoid unsafe replays and retain error states

Recover stale connections before non-replayable tool submissions, and report unknown outcomes without replaying calls after transport loss. Preserve structured error results for resource and prompt wrappers.

NAN-393
NAN-395

* test(recovery): assert unknown outcome guidance

* fix(runtime): revalidate replay contracts and preserve recovery input identity

Bind replay safety to each request session, adopt the rediscovered contract, and stop automatic replay when it loses its safety declaration. Treat cumulative checkpoints as uncommitted deltas rather than deduplicating independent inputs by text, retaining legacy single-batch recovery compatibility.

Extend the existing reconnect and same-text follow-up regressions, and align MCP privacy/proxy fixtures with preflight and explicitly safe replay without relaxing their privacy or routing assertions.

* fix(mcp): report unknown outcomes after submitted tool timeouts

Real HTTP response-loss probing with Codex exposed that the MCP SDK can surface a lost response as a timeout. Preserve preflight timeout semantics, but report possible completed effects after a non-replayable request has started. Never replay or weaken task cancellation.
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-02 17:55:14 +08:00
1 parent c1ed649b81
commit 384b763830
16 files changed
+815 -89

No files matched your search

+25 -14
View File
@@ -12,6 +12,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
from urllib.parse import urlparse
import pytest
import typer
from typer.testing import CliRunner
from nanobot.agent.memory import MemoryStore
@@ -3705,11 +3706,13 @@ def test_gateway_local_trigger_queue_submits_agent_turns(
_fake_run_local_trigger_queue,
)
cli_commands._run_gateway(
config,
health_server_enabled=False,
unconfigured_provider_error=setup_error,
)
with pytest.raises(typer.Exit) as exc_info:
cli_commands._run_gateway(
config,
health_server_enabled=False,
unconfigured_provider_error=setup_error,
)
assert exc_info.value.exit_code == 1
agent = seen["agent"]
agent_kwargs = seen["agent_from_config_kwargs"]
@@ -4021,7 +4024,7 @@ def test_gateway_health_endpoint_binds_and_serves_expected_responses(
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert result.exit_code == 0
assert result.exit_code == 1
assert captured["host"] == host
assert captured["port"] == 18791
assert f"Health endpoint: {display_url}" in result.stdout
@@ -4117,9 +4120,11 @@ def test_gateway_health_endpoint_binds_and_serves_expected_responses(
assert timed_out_writer.output == b""
def test_gateway_agent_task_owns_initial_mcp_provider_close(
@pytest.mark.parametrize("failure_phase", ["startup", "runtime"])
def test_gateway_failure_exits_nonzero_after_cleanup(
monkeypatch,
tmp_path: Path,
failure_phase: str,
) -> None:
config_file = _write_instance_config(tmp_path)
config = Config()
@@ -4199,7 +4204,8 @@ def test_gateway_agent_task_owns_initial_mcp_provider_close(
self.on_job = None
async def start(self) -> None:
return None
if failure_phase == "startup":
raise _StopGatewayError("gateway startup failed")
def stop(self) -> None:
seen["cron_stopped"] = True
@@ -4218,7 +4224,7 @@ def test_gateway_agent_task_owns_initial_mcp_provider_close(
return False
async def serve_forever(self) -> None:
raise _StopGatewayError("stop")
raise _StopGatewayError("gateway runtime failed")
async def _fake_start_server(_handler, _host: str, _port: int):
return _FakeServer()
@@ -4237,18 +4243,23 @@ def test_gateway_agent_task_owns_initial_mcp_provider_close(
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert result.exit_code == 0
assert result.exit_code == 1
assert "Gateway crashed unexpectedly" in result.stdout
assert f"_StopGatewayError: gateway {failure_phase} failed" in result.stdout
assert seen["agent_stopped"] is True
assert seen["inflight_turns_preserved"] is True
assert seen["agent_closed"] is True
assert seen["agent_task_cleaned_up"] is True
assert seen["channels_stopped"] is True
assert seen["cron_stopped"] is True
mcp_provider = seen["mcp_provider"]
assert isinstance(mcp_provider, _FakeMCPProvider)
assert mcp_provider.connect_task is seen["agent_task"]
assert mcp_provider.close_tasks[0] is mcp_provider.connect_task
assert len(mcp_provider.close_tasks) == 2
if failure_phase == "runtime":
assert seen["agent_task_cleaned_up"] is True
assert mcp_provider.connect_task is seen["agent_task"]
assert mcp_provider.close_tasks[0] is mcp_provider.connect_task
assert len(mcp_provider.close_tasks) == 2
else:
assert len(mcp_provider.close_tasks) == 1
hooks = seen["hooks"]
assert isinstance(hooks, list)
assert len(hooks) == 1