Skip to content

Commit f235319

Browse files
committed
perf: run post-completion memory/knowledge extraction in background
Move _extract_memories and _store_knowledge into a fire-and-forget asyncio task so the "completed" WebSocket message reaches the frontend immediately after the agent loop ends. Made-with: Cursor
1 parent b21ef93 commit f235319

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

backend/app/services/agent.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,11 @@ async def run(
489489
self._stop_shake_detector()
490490

491491
await self._notify_state_change(state)
492-
await self._extract_memories(state, instruction)
493-
await self._store_knowledge(state, instruction)
492+
493+
# Run post-completion work in background so the caller can send
494+
# the "completed" message to the frontend immediately.
495+
asyncio.create_task(self._post_completion(state, instruction))
496+
494497
return state
495498

496499
# ── Helpers ──────────────────────────────────────────────────────────
@@ -618,6 +621,11 @@ def _apply_stagnation_warning(self, state: AgentState, warning: str):
618621
"result": warning,
619622
})
620623

624+
async def _post_completion(self, state: AgentState, instruction: str):
625+
"""Background work after task ends (non-blocking)."""
626+
await self._extract_memories(state, instruction)
627+
await self._store_knowledge(state, instruction)
628+
621629
async def _extract_memories(self, state: AgentState, instruction: str):
622630
"""Extract and save memories on successful completion."""
623631
if state.status != TaskStatus.COMPLETED or not state.history:

0 commit comments

Comments
 (0)