fix(cartesia): run the wrapped agent exactly once per event#1280
Open
abhay-codes07 wants to merge 1 commit into
Open
fix(cartesia): run the wrapped agent exactly once per event#1280abhay-codes07 wants to merge 1 commit into
abhay-codes07 wants to merge 1 commit into
Conversation
process() wrapped both the memory enrichment and the agent streaming loop in a single try/except whose fallback re-invoked self.agent.process(). The fallback was meant to keep the agent running when memory enrichment failed, but because the agent's own iteration lived inside the same try, an agent error raised mid-stream — after chunks had already been yielded to the caller — re-ran the entire agent from scratch: the caller received a second full response concatenated onto the partial one, plus a duplicate billable LLM call. Scope the guard to the memory-enrichment/injection/storage block only, and run the agent iteration exactly once after it. Enrichment failures are logged and absorbed as before; agent errors now propagate to the caller instead of triggering a duplicate run. Regression tests stream from a fake inner agent that dies mid-stream: each chunk is delivered exactly once, the agent's run counter stays at 1, and the error propagates (both tests fail against the previous implementation with run_count == 2). A third test pins the intended behaviour that enrichment failures still let the agent run once.
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.
What
SupermemoryCartesiaAgent.process()could run the wrapped agent twice for one event.The whole method body sat in a single
try/exceptwhose fallback re-invokedself.agent.process():The fallback exists so a memory failure never blocks the agent — but because the agent's own iteration lived inside the same
try, an agent error raised mid-stream (LLM connection drop after chunks were already yielded) triggered the fallback too. The caller received a second, full response concatenated onto the partial one, plus a duplicate billable LLM call. This applied to every event type, not justUserTurnEnded.Fix
Scope the guard to the memory enrichment/injection/storage block only, then run the agent iteration exactly once after it:
No changes to the enrichment, prompt-injection, or storage logic themselves.
Testing
New
tests/test_single_agent_run.pystreams from a fake inner agent that dies mid-stream:python -m pytest testsin the package — 4 passed.cc @MaheshtheDev