Repository navigation
fix(core)!: give each sessionless run its own transcript, and free failed runs - #104
Merged
Merged
Conversation
gopaljigaur
commented
Oct 15, 2026
gopaljigaur
left a comment
Owner
Author
There was a problem hiding this comment.
Four findings. The #87 approach holds up: it is a scoped traceback.clear_frames, deferred one loop iteration because the head frame (execute) is still running when the error is caught, and I found no simpler fix that meets the issue.
What I checked:
- The cycle, found with
gc.get_referrerson the #80 commit:_Run->result->error-> traceback -> frames ofexecute,_loopand_steps(localsself,run,executor) ->_Run. The cycle does not go through theevariable (Python already deletes it at the end of theexcept), so thedel eidiom does not help, and theself = Noneidiom fromasyncio/concurrent.futurescannot cover the finished_loop/_stepsframes. Storing the error without its traceback loses the lines and mutates an instance the caller may share. - With the collector disabled, a failed result is freed by refcount on 3.10, 3.12 and 3.14, with a patched completion and with
mock_response=Exception(...). - Each new #87 test fails without its change: without the clearing or without the weakref,
test_a_failed_run_is_freed_without_the_cycle_collectorfails on 3.10 and 3.14; withtraceback.clear_frameson the whole traceback,test_a_reused_error_leaves_frames_of_its_earlier_raise_alonefails on 3.10 and passes on 3.14.test_a_run_without_a_session_starts_a_fresh_transcriptfails against main's code. - No live coroutine is closed on 3.10, 3.12 and 3.14 when a shared error instance fails two parallel
AgentToolcalls, anastreamrun and anAgentToolcalled outside a run, while another coroutine that caught the same instance is suspended. The loop's exception handler recorded nothing. - The weakref on
_current_loopis right. LiteLLM copies the context into its executor and background tasks, and those copies outlive the run. - Removing
Agent.messages/reset()matches both cited libraries: an OpenAI Agents SDKAgentholds no conversation (it continues through a session,result.to_input_list(),conversation_idorprevious_response_id), and a Pydantic AI agent gets history only throughmessage_history/conversation. Both also expose a run's messages on the result. Cyclops reads them through aSession, which is fine without a caller asking for more. - Subagents and
AgentToolstill start each call from_Run(config, [], ...). basic_agent,cost_tracking,multi_turn_agent,router_fallbackandtool_loop_demorun to exit 0 with mocked completions.uv run pytest(417 passed on 3.14, 3.10 and 3.12),pre-commitandmypy cyclopspass. CI is green. The confidentiality and dash checks over the title, body, commits and added lines found nothing.
Findings:
- The branch is behind main, and merged with main one test fails:
tests/test_provider_options.py:438, which #101 added, readsagent.messages. - Without a session, steering that had no step left is dropped silently, but the docs say it stays in the session.
- A kept failed result still holds the run's transcript when LiteLLM raises, through LiteLLM's frames. The issue names that harm, and the PR closes the issue.
- Nit:
traceback.walk_tbcan replace the hand-rolled loop in_clear_frames.
gopaljigaur
force-pushed
the
fix/per-run-history
branch
from
October 15, 2026 03:02
e0cc8b1 to
bcc02fd
Compare
gopaljigaur
force-pushed
the
fix/per-run-history
branch
2 times, most recently
from
October 15, 2026 03:32
71c6ef9 to
dd65de1
Compare
A run without a session appended to one list on the agent that nothing trimmed, so a long lived agent sent every earlier run's messages with each completion, and concurrent runs on one agent wrote into the same list. Such a run now starts from an empty transcript; a Session is how a conversation continues. The docs and examples that relied on the agent remembering earlier runs now pass a session. BREAKING CHANGE: Agent.messages and Agent.reset() are removed, and a run without a session no longer sees earlier runs. Pass the same Session to each run to continue a conversation and read its transcript from session.messages.
RunResult.error kept its traceback, whose frames held the run, its transcript and the agent in a cycle that only the cyclic collector freed. Once the run's coroutine has returned, its own frames, which lead the traceback, are cleared; the exception, its message and the traceback's lines stay. The frames past them, of the code that raised the error and of any earlier raise of the same instance, are left alone: they cannot be told apart, and before Python 3.13 clearing a suspended frame closes its coroutine, so traceback.clear_frames on the whole traceback would close a caller that caught the same error before and is still running. Contexts copied during a run, by tasks or by LiteLLM, kept the run alive through the variable that names the current run, and LiteLLM's frames in the traceback hold such a copy. The variable now holds a weak reference. This is partial: on LiteLLM's own failure path a kept result still keeps the run's transcript alive, through the frames of LiteLLM that raised the error (its __context__ chain into the locals of litellm.main.completion), which are among the frames left alone.
gopaljigaur
force-pushed
the
fix/per-run-history
branch
from
October 15, 2026 03:40
dd65de1 to
2942555
Compare
This was referenced Oct 15, 2026
gopaljigaur
added a commit
that referenced
this pull request
Oct 15, 2026
gopaljigaur
added a commit
that referenced
this pull request
Oct 15, 2026
gopaljigaur
added a commit
that referenced
this pull request
Oct 15, 2026
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.
Two fixes for runs that kept too much alive. One commit each.
A run without a session starts from a fresh transcript (#80)
Every run without a session appended to
Agent._history, which nothing trimmed: a long lived agent sent every earlier run's messages with each completion, and concurrent runs on one agent wrote into the same list. Such a run now starts from an empty transcript, andSessionis how a conversation continues.Agent.messagesandAgent.reset()are removed, so the agent keeps no conversation of its own. That follows the stateless agents of the OpenAI Agents SDK and Pydantic AI, where a conversation lives in a session or in what the caller passes back; the last run's transcript would mean nothing once two runs overlap. To read a transcript, pass aSessionand readsession.messages.Steering that arrives after the last step stays in the session for its next run; without a session it is dropped, and the docs and
RunHandle.steernow say so.Docs (
docs/and the website), the README and the examples that relied on the agent remembering earlier calls now pass a session. The tests that inspectedagent.messagespass a session and read its messages.A failed run is freed without the cycle collector (#87)
RunResult.errorkept its traceback, whose frames held the run, its transcript and the agent in a cycle._RunLoop.executereturns, a callback clears the run's own frames withtraceback.walk_tb, which lead the traceback. The exception, its message and the traceback's lines stay.traceback.clear_frameson the whole traceback. Past the run's frames come the frames of the code that raised the error and, when the same exception instance was raised before, frames of that earlier raise. Before Python 3.13, clearing a suspended frame closes its coroutine, so clearing everything closed a caller that had caught the same error and was still running. On 3.10 this broketest_arun_raises_the_run_errorandtest_failed_run_ends_events_with_error_and_astream_raises, which reuse one error instance across runs._current_loopthat copy kept the run alive too. The variable now holds a weak reference to the loop.__context__chain into the locals oflitellm.main.completion). Those frames are among the ones left alone: past the run's own frames there is no telling them from frames of an earlier raise, and clearing a suspended one closes its coroutine before 3.13.Verified with the cycle collector disabled. A failed run's result is freed by reference counting once it is dropped, on 3.10, 3.11, 3.12 and 3.14. That holds with a patched completion and with LiteLLM's own failure path (
mock_response=Exception(...)). The garbage left on LiteLLM's failure path is LiteLLM's own exception cycle and holds no object of the run, but it holds the transcript as above. New tests:test_a_failed_run_is_freed_without_the_cycle_collectorfails without either change.test_a_reused_error_leaves_frames_of_its_earlier_raise_alonefails on 3.10 withtraceback.clear_frames.Checks
uv run pytest(449 passed, also on 3.10),uv run pre-commit run --all-filesanduv run mypy cyclopspass.Closes #80
Refs #87