Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 24, 2025

This PR implements automatic caching of client-side Stockfish and Maia analysis to prevent loss when users navigate away from the analysis page.

Problem

Users were losing all their analysis progress whenever they left the analysis page, requiring them to re-analyze positions they had already studied.

Solution

Added comprehensive auto-save and load functionality:

Auto-Save Mechanism

  • Automatic saves every 10 seconds via background timer
  • Immediate saves when new analysis is detected (debounced)
  • Final save on component unmount or game change
  • Smart filtering - only saves meaningful analysis (depth ≥ 12 or Maia data)
  • Cache optimization - avoids redundant saves using cache key comparison

Load Mechanism

  • Automatic loading when opening any supported game type
  • FEN verification ensures stored analysis applies to correct positions
  • Protection against overwriting deeper analysis with shallower stored data
  • Background operation - failures don't interrupt user experience

Implementation Details

New API functions (src/api/analysis/analysis.ts):

storeEngineAnalysis(gameId: string, analysisData: EngineAnalysisData)
getEngineAnalysis(gameId: string): Promise<EngineAnalysisData | null>

Analysis storage utilities (src/lib/analysisStorage.ts):

  • collectEngineAnalysisData() - extracts analysis from GameTree nodes
  • applyEngineAnalysisData() - applies stored analysis back to tree
  • generateAnalysisCacheKey() - prevents unnecessary saves

Data format matches backend specification:

{
  "positions": [
    {
      "ply": 0,
      "fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
      "maia": {
        "maia_kdd_1100": {
          "policy": { "e2e4": 0.6614, "d2d4": 0.2185 },
          "value": 0.3905
        }
      },
      "stockfish": {
        "depth": 14,
        "cp_vec": { "e2e4": 32, "d2d4": 16 }
      }
    }
  ]
}

Game Type Support

  • ✅ Tournament games
  • ✅ Lichess games
  • ✅ User games (play/hand/brain)
  • ❌ Custom PGN/FEN games (excluded - remain local only)

Error Handling

  • Network failures logged but don't show user errors
  • Invalid data gracefully handled
  • Background operations don't block UI
  • Proper timer cleanup prevents memory leaks

The implementation is completely transparent to users - analysis is automatically saved and restored without any user intervention.

Fixes #154.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link

vercel bot commented Jul 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
maia-platform-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2025 9:38pm

Co-authored-by: kevinjosethomas <46242684+kevinjosethomas@users.noreply.github.com>
…onality

Co-authored-by: kevinjosethomas <46242684+kevinjosethomas@users.noreply.github.com>
Co-authored-by: kevinjosethomas <46242684+kevinjosethomas@users.noreply.github.com>
Copilot AI changed the title [WIP] Save client-side Stockfish/Maia analysis in the backend Implement client-side Stockfish/Maia analysis caching with auto-save and load functionality Jul 24, 2025
Copilot AI requested a review from kevinjosethomas July 24, 2025 04:51
@kevinjosethomas kevinjosethomas changed the base branch from main to dev July 24, 2025 05:08
Update feature branch
@kevinjosethomas kevinjosethomas marked this pull request as ready for review July 25, 2025 21:37
@kevinjosethomas kevinjosethomas merged commit 167c1cb into dev Jul 25, 2025
8 checks passed
@kevinjosethomas kevinjosethomas deleted the copilot/fix-154 branch July 25, 2025 21:40
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.

Save client-side Stockfish/Maia analysis in the backend

2 participants