fix(agent): suppress detached fork session-end hooks

This commit is contained in:
fangliquanflq
2026-09-10 12:22:49 +08:00
committed by jonny
parent feb03196a2
commit 77f2ce853f
2 changed files with 44 additions and 12 deletions
+13 -12
View File
@@ -623,18 +623,19 @@ def finalize_turn(
# Memory provider on_session_end()/shutdown_all() are NOT called here:
# run_conversation() runs once per message; CLI/gateway own session-end cleanup.
_invoke_hook_safely(
"on_session_end", logger,
session_id=agent.session_id,
task_id=effective_task_id,
turn_id=turn_id,
completed=completed,
failed=failed,
interrupted=interrupted,
turn_exit_reason=_turn_exit_reason,
model=agent.model,
platform=_platform,
)
if not getattr(agent, "_persist_disabled", False):
_invoke_hook_safely(
"on_session_end", logger,
session_id=agent.session_id,
task_id=effective_task_id,
turn_id=turn_id,
completed=completed,
failed=failed,
interrupted=interrupted,
turn_exit_reason=_turn_exit_reason,
model=agent.model,
platform=_platform,
)
agent._turn_preflight_display_snapshot = None
agent._turn_received_provider_response = False
@@ -8,6 +8,8 @@ for must still be returned. Previously any of those raised straight out of
traceback and lost the whole turn.
"""
from unittest.mock import patch
import pytest
from agent.turn_finalizer import finalize_turn
@@ -164,3 +166,32 @@ def test_clean_turn_has_no_cleanup_errors_key():
assert "cleanup_errors" not in result
@pytest.mark.parametrize(
("persist_disabled", "expected_calls"),
[
(True, ["transform_llm_output"]),
(False, ["transform_llm_output", "post_llm_call", "on_session_end"]),
],
)
def test_persist_disabled_turn_skips_session_end_hook(
persist_disabled, expected_calls
):
agent = _StubAgent(raise_in=())
agent._persist_disabled = persist_disabled
calls = []
def capture(name, _logger, **_kwargs):
calls.append(name)
return []
with patch("agent.turn_finalizer._invoke_hook_safely", side_effect=capture):
_run(
agent,
final_response="done",
api_call_count=1,
turn_exit_reason="text_response(stop)",
)
assert calls == expected_calls