Skip to content

🐛 Bugfix: fix agent generation cache not restored when switching pages#2806

Merged
Dallas98 merged 7 commits intodevelopfrom
xyq/bugfix_frontend
Apr 21, 2026
Merged

🐛 Bugfix: fix agent generation cache not restored when switching pages#2806
Dallas98 merged 7 commits intodevelopfrom
xyq/bugfix_frontend

Conversation

@xuyaqist
Copy link
Copy Markdown
Contributor

#2786
Bugfix: fix agent generation cache not restored when switching pages

fix.agent.generation.cache.not.restored.when.switching.pages.mp4

@xuyaqist xuyaqist requested a review from WMC001 as a code owner April 17, 2026 03:32
Copilot AI review requested due to automatic review settings April 17, 2026 03:32
@xuyaqist xuyaqist requested a review from Dallas98 as a code owner April 17, 2026 03:32
@xuyaqist xuyaqist changed the title Bugfix: fix agent generation cache not restored when switching pages 🐛 Bugfix: fix agent generation cache not restored when switching pages Apr 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.ts to persist streaming generation fields per-agent in localStorage.
  • Updates AgentGenerateDetail.tsx to (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.

Comment thread frontend/lib/agentGenerationCache.ts Outdated
Comment thread frontend/app/[locale]/agents/components/agentInfo/AgentGenerateDetail.tsx Outdated
Comment on lines 730 to +748
() => {
// 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;
}
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +782 to +784
// Clear the cache since generation completed successfully on this agent
clearAgentGenerationCache(generationAgentId);

Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread frontend/app/[locale]/agents/components/agentInfo/AgentGenerateDetail.tsx Outdated
Comment thread frontend/lib/agentGenerationCache.ts Outdated
Comment thread frontend/lib/agentGenerationCache.ts Outdated
Comment on lines +361 to +368
// 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
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Comment thread frontend/app/[locale]/agents/components/agentInfo/AgentGenerateDetail.tsx Outdated
@xuyaqist xuyaqist force-pushed the xyq/bugfix_frontend branch from 72af7b9 to da3c379 Compare April 17, 2026 06:25
@Dallas98 Dallas98 merged commit a90e772 into develop Apr 21, 2026
8 checks passed
@xuyaqist xuyaqist deleted the xyq/bugfix_frontend branch April 24, 2026 07:54
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.

3 participants