Skip to content

fix(cartesia): run the wrapped agent exactly once per event#1280

Open
abhay-codes07 wants to merge 1 commit into
supermemoryai:mainfrom
abhay-codes07:fix/cartesia-single-agent-run
Open

fix(cartesia): run the wrapped agent exactly once per event#1280
abhay-codes07 wants to merge 1 commit into
supermemoryai:mainfrom
abhay-codes07:fix/cartesia-single-agent-run

Conversation

@abhay-codes07

Copy link
Copy Markdown
Contributor

What

SupermemoryCartesiaAgent.process() could run the wrapped agent twice for one event.

The whole method body sat in a single try/except whose fallback re-invoked self.agent.process():

try:
    ...memory enrichment...
    async for output in self.agent.process(env, event):
        yield output
except Exception as e:
    logger.error(f"[Supermemory] Error in process: {e}")
    async for output in self.agent.process(env, event):   # runs the WHOLE agent again
        yield output

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 just UserTurnEnded.

Fix

Scope the guard to the memory enrichment/injection/storage block only, then run the agent iteration exactly once after it:

  • enrichment failures are logged and absorbed, and the agent still runs — same guarantee as before
  • agent errors now propagate to the caller instead of triggering a duplicate run

No changes to the enrichment, prompt-injection, or storage logic themselves.

Testing

New tests/test_single_agent_run.py streams from a fake inner agent that dies mid-stream:

  • each chunk is delivered exactly once, the inner agent's run counter stays at 1, and the error propagates — both mid-stream tests fail against the previous implementation (verified: run counter hits 2 on the old code)
  • a third test pins the intended behaviour that an enrichment failure still lets the agent run exactly once

python -m pytest tests in the package — 4 passed.

cc @MaheshtheDev

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.
Copilot AI review requested due to automatic review settings July 13, 2026 05:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants