🐛 Bugfix: fix agent generation cache not restored when switching pages#2806
🐛 Bugfix: fix agent generation cache not restored when switching pages#2806
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a client-side (localStorage) cache for agent prompt generation and updates the agent generation UI logic to avoid streaming updates leaking into the currently visible agent when users switch agents/pages mid-generation.
Changes:
- Introduces
frontend/lib/agentGenerationCache.tsto persist streaming generation fields per-agent in localStorage. - Updates
AgentGenerateDetail.tsxto (a) gate UI updates to the currently visible agent while still caching stream output, and (b) attempt restoring cached content when switching agents.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| frontend/lib/agentGenerationCache.ts | New localStorage-backed cache utilities for persisting generation state/content per agent. |
| frontend/app/[locale]/agents/components/agentInfo/AgentGenerateDetail.tsx | Uses the cache during streaming and on agent switches to prevent cross-agent UI updates and restore cached generation results. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| () => { | ||
| // Track the agent this generation was for | ||
| const generationAgentId = effectiveAgentId; | ||
|
|
||
| // Check if we're still on the same agent | ||
| const currentEffectiveAgentId = useAgentConfigStore.getState().currentAgentId ?? 0; | ||
| const isSameAgent = generationInitiatorRef.current === currentEffectiveAgentId; | ||
|
|
||
| // Clear generating state immediately for ALL cases | ||
| // This prevents the "stuck in generating" state when user switches agents | ||
| setIsGenerating(false); | ||
| setGeneratingForAgentId(null); | ||
| generationInitiatorRef.current = null; | ||
|
|
||
| // If not on same agent, keep the cache so user can restore when switching back | ||
| // Do NOT clear cache here - the cache contains the completed generation result | ||
| if (!isSameAgent) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
On stream completion, when the user is no longer on the initiating agent (!isSameAgent), the callback returns before marking the cache as finished. That leaves isGenerating=true in localStorage, and the switch-back effect later clears it, so the completed generation can never be restored. Ensure completion always updates the cache status to isGenerating=false (even if the UI isn’t currently showing that agent) and keep the cached content for restoration.
| // Clear the cache since generation completed successfully on this agent | ||
| clearAgentGenerationCache(generationAgentId); | ||
|
|
There was a problem hiding this comment.
The success path clears the cache immediately after generation completes on the same agent. If the goal is to restore generated prompts when navigating away/back (or switching agents after completion but before saving), clearing here will prevent restoration. Consider keeping the completed cache (with isGenerating=false) until the user saves/discards, or clearing only after persistence to the backend.
| // If cache has isGenerating=true, it means a previous session was interrupted | ||
| // Clear it and return - user will need to regenerate | ||
| if (cached?.isGenerating) { | ||
| clearAgentGenerationCache(effectiveAgentId); | ||
| return; | ||
| } | ||
|
|
||
| // For completed generation (isGenerating was cleared), restore the content |
There was a problem hiding this comment.
This effect deletes the cache whenever cached.isGenerating is true. But isGenerating will be true while a stream is running (including when the user switches away and comes back), so this will drop partial/complete results and prevent restoration. Instead of clearing here, restore cached partial content (and/or set UI generating state) and only clear caches when they are expired or explicitly discarded.
| // If cache has isGenerating=true, it means a previous session was interrupted | |
| // Clear it and return - user will need to regenerate | |
| if (cached?.isGenerating) { | |
| clearAgentGenerationCache(effectiveAgentId); | |
| return; | |
| } | |
| // For completed generation (isGenerating was cleared), restore the content | |
| // Restore any cached content, including partial results from an in-progress generation. | |
| // `isGenerating` indicates generation status and should not cause the cache to be discarded. |
Made-with: Cursor (cherry picked from commit 07130f1)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
72af7b9 to
da3c379
Compare
#2786
Bugfix: fix agent generation cache not restored when switching pages
fix.agent.generation.cache.not.restored.when.switching.pages.mp4