Skip to content

fix(core)!: give each sessionless run its own transcript, and free failed runs - #104

Merged
gopaljigaur merged 2 commits into
mainfrom
fix/per-run-history
Oct 15, 2026
Merged

gopaljigaur merged 2 commits into
mainfrom
fix/per-run-history

Conversation

@gopaljigaur

@gopaljigaur gopaljigaur commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

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, and Session is how a conversation continues.

Agent.messages and Agent.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 a Session and read session.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.steer now 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 inspected agent.messages pass a session and read its messages.

BREAKING CHANGE: Agent.messages and Agent.reset() are removed, and a run
without a session no longer sees earlier runs.

A failed run is freed without the cycle collector (#87)

RunResult.error kept its traceback, whose frames held the run, its transcript and the agent in a cycle.

  • Once _RunLoop.execute returns, a callback clears the run's own frames with traceback.walk_tb, which lead the traceback. The exception, its message and the traceback's lines stay.
  • It does not call traceback.clear_frames on 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 broke test_arun_raises_the_run_error and test_failed_run_ends_events_with_error_and_astream_raises, which reuse one error instance across runs.
  • LiteLLM's frames in the traceback hold a copy of the run's context, and through _current_loop that copy kept the run alive too. The variable now holds a weak reference to the loop.
  • 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 (the __context__ chain into the locals of litellm.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_collector fails without either change.
  • test_a_reused_error_leaves_frames_of_its_earlier_raise_alone fails on 3.10 with traceback.clear_frames.

Checks

uv run pytest (449 passed, also on 3.10), uv run pre-commit run --all-files and uv run mypy cyclops pass.

Closes #80
Refs #87

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_referrers on the #80 commit: _Run -> result -> error -> traceback -> frames of execute, _loop and _steps (locals self, run, executor) -> _Run. The cycle does not go through the e variable (Python already deletes it at the end of the except), so the del e idiom does not help, and the self = None idiom from asyncio/concurrent.futures cannot cover the finished _loop/_steps frames. 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_collector fails on 3.10 and 3.14; with traceback.clear_frames on the whole traceback, test_a_reused_error_leaves_frames_of_its_earlier_raise_alone fails on 3.10 and passes on 3.14. test_a_run_without_a_session_starts_a_fresh_transcript fails 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 AgentTool calls, an astream run and an AgentTool called outside a run, while another coroutine that caught the same instance is suspended. The loop's exception handler recorded nothing.
  • The weakref on _current_loop is 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 SDK Agent holds no conversation (it continues through a session, result.to_input_list(), conversation_id or previous_response_id), and a Pydantic AI agent gets history only through message_history/conversation. Both also expose a run's messages on the result. Cyclops reads them through a Session, which is fine without a caller asking for more.
  • Subagents and AgentTool still start each call from _Run(config, [], ...).
  • basic_agent, cost_tracking, multi_turn_agent, router_fallback and tool_loop_demo run to exit 0 with mocked completions.
  • uv run pytest (417 passed on 3.14, 3.10 and 3.12), pre-commit and mypy cyclops pass. CI is green. The confidentiality and dash checks over the title, body, commits and added lines found nothing.

Findings:

  1. The branch is behind main, and merged with main one test fails: tests/test_provider_options.py:438, which #101 added, reads agent.messages.
  2. Without a session, steering that had no step left is dropped silently, but the docs say it stays in the session.
  3. 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.
  4. Nit: traceback.walk_tb can replace the hand-rolled loop in _clear_frames.

Comment thread cyclops/core/agent.py Resolved
Comment thread docs/guide/agents.md Resolved
Comment thread cyclops/core/run.py Resolved
Comment thread cyclops/core/run.py Resolved

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@gopaljigaur
gopaljigaur force-pushed the fix/per-run-history branch 2 times, most recently from 71c6ef9 to dd65de1 Compare October 15, 2026 03:32

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

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 gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@gopaljigaur
gopaljigaur merged commit ba63027 into main Oct 15, 2026
10 checks passed
@gopaljigaur
gopaljigaur deleted the fix/per-run-history branch October 15, 2026 03:47
gopaljigaur added a commit that referenced this pull request Oct 15, 2026
#104 fixed #80, so the long lived agent check is a plain test.
gopaljigaur added a commit that referenced this pull request Oct 15, 2026
#104 fixed #80, so the long lived agent check is a plain test.
gopaljigaur added a commit that referenced this pull request Oct 15, 2026
#104 fixed #80, so the long lived agent check is a plain test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A run without a session appends to one shared, ever growing history

1 participant