Skip to content

Fix trajectory viewer race condition with request counter#171

Merged
khaliqgant merged 1 commit intomainfrom
fix/trajectory-loading-bug
Jan 13, 2026
Merged

Fix trajectory viewer race condition with request counter#171
khaliqgant merged 1 commit intomainfrom
fix/trajectory-loading-bug

Conversation

@khaliqgant
Copy link
Copy Markdown
Member

Summary

  • Fixes trajectory viewer showing wrong data when clicking between trajectories quickly
  • Adds request counter pattern to prevent stale fetch responses from overwriting newer data
  • More robust than previous ID-only comparison approach

Problem

When clicking trajectories quickly, users would see:

  1. Wrong trajectory data loading
  2. Overview data appearing instead of specific trajectory
  3. Wrong data after another click

Root cause: Stale fetch responses could complete and update state after a newer selection was made. The previous fix using trajectory ID comparison wasn't sufficient for all race conditions.

Solution

Added requestCounterRef pattern:

  • Each fetch captures a unique request ID at start
  • selectTrajectory() increments counter to immediately invalidate in-flight fetches
  • Double-check both request ID AND trajectory ID before updating state
  • Early return when clicking same trajectory (prevents redundant fetches)
  • Only update error state if request is still current

Test plan

  • Open trajectory panel with multiple trajectories in history
  • Click rapidly between different trajectories
  • Verify correct trajectory data loads each time
  • Check console for "[useTrajectory] Ignoring superseded fetch" logs confirming stale data blocked
  • Click same trajectory twice - should not re-fetch

🤖 Generated with Claude Code

The bug occurred when clicking trajectories quickly - stale fetch responses
could overwrite newer data, showing the wrong trajectory or overview data.

Root cause: The previous fix used trajectory ID comparison to reject stale
fetches, but this wasn't sufficient because:
1. Polling could start fetches that completed after selection changed
2. Multiple rapid clicks could have overlapping fetches
3. The same trajectory ID check wouldn't catch all race conditions

Fix:
- Added requestCounterRef to uniquely identify each fetch request
- Increment counter in selectTrajectory() to immediately invalidate
  in-flight fetches when selection changes
- Double-check both request ID AND trajectory ID before updating state
- Added early return if clicking same trajectory (prevents re-fetch)
- Removed redundant initial fetchSteps() call (refresh() handles it)
- Only update error state if request is still current

The request counter provides stronger protection than ID comparison alone
because it catches ALL out-of-order responses, not just those for different
trajectory IDs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@my-senior-dev-pr-review
Copy link
Copy Markdown

🤖 My Senior Dev — Analysis Complete

👤 For @khaliqgant

📁 Expert in src/dashboard/react-components/ (7 edits) • ⚡ 29th PR this month

View your contributor analytics →


📊 1 file reviewed • 6 need attention

⚠️ Needs Attention:

  • src/dashboard/react-components/hooks/useTrajectory.ts — Significant concerns regarding error handling and request counter management which could impact data integrity and user feedback.

🚀 Open Interactive Review →

The full interface unlocks features not available in GitHub:

  • 💬 AI Chat — Ask questions on any file, get context-aware answers
  • 🔍 Smart Hovers — See symbol definitions and usage without leaving the diff
  • 📚 Code Archeology — Understand how files evolved over time (/archeology)
  • 🎯 Learning Insights — See how this PR compares to similar changes

💬 Chat here: @my-senior-dev explain this change — or try @chaos-monkey @security-auditor @optimizer @skeptic @junior-dev

📖 View all 12 personas & slash commands

You can interact with me by mentioning @my-senior-dev in any comment:

In PR comments or on any line of code:

  • Ask questions about the code or PR
  • Request explanations of specific changes
  • Get suggestions for improvements

Slash commands:

  • /help — Show all available commands
  • /archeology — See the history and evolution of changed files
  • /profile — Performance analysis and suggestions
  • /expertise — Find who knows this code best
  • /personas — List all available AI personas

AI Personas (mention to get their perspective):

Persona Focus
@chaos-monkey 🐵 Edge cases & failure scenarios
@skeptic 🤨 Challenge assumptions
@optimizer Performance & efficiency
@security-auditor 🔒 Security vulnerabilities
@accessibility-advocate Inclusive design
@junior-dev 🌱 Simple explanations
@tech-debt-collector 💳 Code quality & shortcuts
@ux-champion 🎨 User experience
@devops-engineer 🚀 Deployment & scaling
@documentation-nazi 📚 Documentation gaps
@legacy-whisperer 🏛️ Working with existing code
@test-driven-purist Testing & TDD

For the best experience, view this PR on myseniordev.com — includes AI chat, file annotations, and interactive reviews.

@khaliqgant khaliqgant merged commit 429a397 into main Jan 13, 2026
6 checks passed
@khaliqgant khaliqgant deleted the fix/trajectory-loading-bug branch January 13, 2026 11:15
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.

1 participant