Skip to content

fix(agent): guard finalize_turn cleanup chain so it never drops the response - #50059

Closed
Megashubham wants to merge 1 commit into
NousResearch:mainfrom
Megashubham:fix/finalize-turn-cleanup-guard
Closed

fix(agent): guard finalize_turn cleanup chain so it never drops the response#50059
Megashubham wants to merge 1 commit into
NousResearch:mainfrom
Megashubham:fix/finalize-turn-cleanup-guard

Conversation

@Megashubham

Copy link
Copy Markdown

What does this PR do?

When a turn hits max_iterations, finalize_turn ran three unguarded cleanup steps after the model's summary:

  • _save_trajectory (file I/O)
  • _cleanup_task_resources (remote VM/browser teardown)
  • _persist_session (SQLite write)

Any exception from these propagated out of run_conversation, silently discarding final_response. Subprocess wrappers saw empty stdout with no traceback.

Each step is now wrapped in its own try/except so one failure cannot skip the others. Failures log at ERROR with a traceback and are surfaced on the result dict via cleanup_errors; the partial response is always returned. Clean turns never get a cleanup_errors key.

Related Issue

Fixes #8049

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✅ Tests (adding or improving test coverage)

Changes Made

  • agent/turn_finalizer.py: wrap each of the three core cleanup steps in its own try/except, collect errors in _cleanup_errors list, surface via cleanup_errors on result dict
  • tests/agent/test_turn_finalizer_cleanup_guard.py: regression coverage — all-three-raise, each-one-raises-in-isolation (others still run), and clean-turn-no-key

How to Test

  1. Run pytest tests/agent/test_turn_finalizer_cleanup_guard.py -v — 5 tests pass
  2. E2E: create a stub agent where _save_trajectory raises; before this fix run_conversation returns no response; after this fix the response is preserved and cleanup_errors shows the error.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix
  • I've run pytest tests/agent/test_turn_finalizer_cleanup_guard.py -v and all 5 tests pass
  • I've added tests for my changes
  • I've tested on: macOS

Documentation & Housekeeping

  • N/A — no config keys or tool schemas changed

…esponse

When a turn hit max_iterations, finalize_turn ran three unguarded cleanup
steps after the model's summary — _save_trajectory (file I/O),
_cleanup_task_resources (remote VM/browser teardown), and _persist_session
(SQLite write). Any raise there propagated out of run_conversation, discarding
the partial final_response the caller was waiting for; subprocess wrappers
saw an empty stdout with no traceback (#8049).

Each step is now guarded independently so one failure can't skip the others.
Failures log at ERROR with a traceback and are surfaced on the result dict
via cleanup_errors; the partial response is always returned. Clean turns
never get a cleanup_errors key.

Changes:
- agent/turn_finalizer.py: wrap each of the three core cleanup steps in its
  own try/except, collect errors in _cleanup_errors list, surface on result
- tests/agent/test_turn_finalizer_cleanup_guard.py: regression coverage —
  all-three-raise, each-one-raises-in-isolation (others still run),
  and clean-turn-no-key (5 tests, all pass)

Closes #8049
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint duplicate This issue or pull request already exists labels Jun 21, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #50009 — same fix for #8049, filed ~1.5h earlier. Both wrap the three post-loop cleanup steps (_save_trajectory, _cleanup_task_resources, _persist_session) in independent try/except blocks, collect a _cleanup_errors list, surface it via a cleanup_errors result key, and add the identically-named regression test tests/agent/test_turn_finalizer_cleanup_guard.py. Related: #43057 and #46183 (same code path, different approach).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint duplicate This issue or pull request already exists P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run_conversation silently kills the Python process when iteration budget is exhausted

2 participants