Conversation
_on_turn fires after every agent turn and called _persist() inline β a synchronous open()+json.dump() over the full history/display_history/ workflow_turns. As a session grows this write grows with it, and being awaited directly in run_agent_loop it stalls the event loop on every single turn (TUI freezes, no other coroutine gets to run). Move it to asyncio.to_thread, awaited so writes stay ordered turn to turn and the resume checkpoint can't be overwritten out of order. Benchmarked with a 2000-message history + a concurrent heartbeat coroutine: sync persist blocks the loop for ~139ms with 0 heartbeat ticks; to_thread lets ~4558 ticks through with a 0.85ms max stall.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TerminalSession._on_turnfires after every agent turn (run_agent_loopawaitson_turn_completedirectly) and called_persist()inline β a synchronousopen()+json.dump()over the fullhistory,display_history, andworkflow_turns.asyncio.to_thread, still awaited so writes stay ordered turn-to-turn (no risk of an out-of-order write corrupting the--resumecheckpoint).Benchmark
2000-message history, 10 persists, with a concurrent heartbeat coroutine standing in for other async work:
Sync blocks the loop completely (0 heartbeat ticks in 139ms).
to_threadcosts a bit more wall time (thread dispatch + GIL handoff) but lets everything else keep running concurrently β the actual user-visible win, since the write itself isn't the bottleneck, the freeze is.Test plan
uv run pytest apodex/tests/test_changes.py apodex/tests/test_features.pyβ 136 passeduv run ruff check apodex/session.pyβ clean