Skip to content

Conversation

@nelsonblaha
Copy link

Problem

When using happy-web to start a new session, clicking the machine name to change it and then selecting a different machine would cause the selection to revert back to the original machine.

Root Cause

Module-level callback pattern had race conditions in web environments where router.back() could complete before state updates propagated.

Solution

Pass selectedMachineId explicitly via route params when navigating back from machine picker. This ensures the selection is guaranteed to be available when the component mounts.

Changes

  • Updated sources/app/(app)/new/index.tsx to read machine selection from route params
  • Updated sources/app/(app)/new/pick/machine.tsx to pass machine ID via route params
  • Updated metro.config.js to exclude test files from web bundle (prevents vitest error)
  • Added machineSelection.spec.ts with 9 tests covering bug reproduction and fix validation

Testing

  • ✅ All 9 new tests passing
  • ✅ Manually tested in browser - machine selection now works correctly
  • ✅ No existing tests broken (same 32 pre-existing failures)

When using happy-web to start a new session, clicking the machine name
to change it and then selecting a different machine would cause the
selection to revert back to the original machine.

Root cause: Module-level callback pattern had race conditions in web
environments where router.back() could complete before state updates
propagated.

Fix: Pass selectedMachineId explicitly via route params when navigating
back from machine picker. This ensures the selection is guaranteed to
be available when the component mounts.

Also exclude test files from metro bundler to prevent vitest from being
bundled into the web app.

Tests: Added machineSelection.spec.ts with 9 tests covering the bug
reproduction, callback race conditions, and route param solution.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
@rrnewton
Copy link

rrnewton commented Dec 2, 2025

Nice, I've done a scan of the code but haven't tested the patch yet myself. When we get to monorepo + e2e testing it will be nice to have an e2e test for this kind of thing.

Copy link
Contributor

@bra1nDump bra1nDump left a comment

Choose a reason for hiding this comment

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

These tests don't seem to be testing the actual code they appear to be more as demonstrations of the bug. Can we remove all these tests, and maybe just add a extended comment describing the previous race condition briefly next to the use effect you've added?

// Navigate back with the selected machine ID as a param (fixes web bug)
router.push({
pathname: '/new',
params: { selectedMachineId: machineId }
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this erase the prompt and data id parameters?

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