Repository navigation
Conversation
Agent.__aexit__ only saved the session state on non-success exits (exc_type set, or finish never called), so a run that ended with a clean finish wrote nothing. clear_cache_on_success=False only ever governed the cleanup side, leaving session(resume=True, clear_cache_on_success=False) a no-op across successful runs: a later run on the same task text found no cache and started fresh. Treat clear_cache_on_success=False as save-and-keep, as the name reads: a clean finish now also passes the __aexit__ cache gate and the final state is saved, so a later session(resume=True) can reopen the successfully finished session. Behavior is unchanged when clear_cache_on_success=True (run() still clears the cache at finish and no state is saved), on error/interrupt exits, and when cache_on_interrupt=False. Fixes ArtificialAnalysis#91
This branch has not been deployed
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
Fixes #91.
Agent.__aexit__only saved the session state on non-success exits — an exception, or a run that never calledfinish. A run that ended with a clean finish wrote nothing, so the state was discarded even when the caller passedclear_cache_on_success=False. That parameter only ever governed the cleanup side (run()clearing the cache at finish), never the saving side — which madesession(resume=True, clear_cache_on_success=False)a no-op across successful runs: a laterrun()on the same task text found no cache and started fresh, silently losing the finished session's context for task-scoped orchestration workflows that reactivate a session hours apart.Changes
src/stirrup/core/agent.pyonly:should_cachegate in__aexit__now also passes on a clean finish whenclear_cache_on_success=False— i.e. the parameter is treated as "save AND keep", which is what its name already promises. The saved state is the same_current_run_statesnapshot the interrupt path saves (and the same one the issue's private-API workaround saves manually).session()docstring forclear_cache_on_successnow states this save-and-keep semantics.Behavior that does not change:
clear_cache_on_success=True(default):run()still clears any cache the moment the run finishes, and nulls_current_task_hash/_current_run_state, so__aexit__still saves nothing after a clean finish.finishnever called): cached exactly as before, under either flag value.cache_on_interrupt=False: still disables__aexit__caching entirely.The alternative from the issue — a new explicit
cache_on_successparameter — was not taken: it would add a second flag whose interaction withclear_cache_on_successstill needs defining, while reading the existing flag as save-and-keep matches the name and needs no API addition. Happy to switch if maintainers prefer the explicit parameter.Tests
I wrote the tests first and checked that they fail without the fix. With
src/stirrup/core/agent.pyreverted, the first test below fails (no cache is written on the clean finish) and the other two still pass; with the fix, all three pass:test_clean_finish_is_cached_and_resumed_when_clear_cache_on_success_is_false— the reported flow end to end: a first session (resume=True, clear_cache_on_success=False) does one turn of work and finishes cleanly; its state is in the cache afterwards (verified viaCacheManager.load_state, including the first turn's content), and a second session withresume=Trueon the same task text opens with that history in context.test_error_exit_is_still_cached— a run whose client raises still writes its state for resume (pre-existing path, unchanged).test_clean_finish_still_clears_cache_when_clear_cache_on_success_is_true— seeds a cache via an error exit, then a cleanly finishing run with the defaultclear_cache_on_success=Trueclears it and saves nothing.Local runs (Python 3.13,
uv sync --locked --dev --extra mcp):tests/test_agent.py+tests/test_logging.py: 62 passed, 1 skipped.pytest --ignore=tests/test_browser_use.py -m "not docker and not e2b and not browser"): 319 passed / 34 failed with the fix, against 316 passed / 34 failed on unmodifiedmainin the same sandbox (the 3 extra passes are the new tests). The failure sets differ only by onetest_web_fetch.pyDNS test in each direction; both pass on a re-run, and the remaining failures are the pre-existing sandbox environment failures in the chat-completions/open-responses client tests (httpx/proxy), identical onmainand unrelated to this change.ruff format --check .andruff check .pass;ty checkreports no diagnostics in the changed files.