Skip to content

fix: resolve TUI worker startup crash from circular dependency#47

Merged
anandgupta42 merged 1 commit intomainfrom
fix/tui-worker-startup-crash
Mar 5, 2026
Merged

fix: resolve TUI worker startup crash from circular dependency#47
anandgupta42 merged 1 commit intomainfrom
fix/tui-worker-startup-crash

Conversation

@anandgupta42
Copy link
Contributor

Summary

  • Fixed empty screen on npm install -g @altimateai/altimate-code@0.2.2: The TUI worker thread crashed on startup due to a circular dependency — ModelsDev.refresh() ran at module load time and accessed Installation.USER_AGENT before the Installation module finished initializing (import chain: worker.tsconfig.tsmodels.tsinstallation). Deferred the initial refresh via setTimeout(…, 0) so all modules are fully loaded first.
  • Improved worker error logging: The worker.onerror handler now extracts e.message and e.error.stack instead of logging the raw ErrorEvent object (which produced the useless [object ErrorEvent]).
  • Added e2e test for worker startup: Spawns the actual TUI worker and asserts it starts without errors and responds to RPC calls. Verified the test catches this exact bug (fails with the fix reverted).

Test plan

  • bun test test/cli/tui/worker.test.ts passes (2 tests)
  • Verified test fails when fix is reverted (catches TypeError: undefined is not an object (evaluating 'Installation.USER_AGENT'))
  • altimate run "hello" works correctly
  • TUI starts without [object ErrorEvent] in logs

🤖 Generated with Claude Code

The TUI showed an empty screen because the worker thread crashed on startup.
ModelsDev.refresh() ran at module load time and accessed Installation.USER_AGENT
before the Installation module finished initializing (circular import chain:
worker.ts → config.ts → models.ts → installation). Deferring the initial
refresh via setTimeout(…, 0) ensures all modules are fully loaded first.

Also improves worker error logging to extract the actual error message from
ErrorEvent (previously logged as useless "[object ErrorEvent]"), and adds an
e2e test that spawns the worker and verifies it starts without errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@jontsai jontsai left a comment

Choose a reason for hiding this comment

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

PR #47 Review Tour Guide

fix: resolve TUI worker startup crash from circular dependency

# File +/- Purpose
1 src/cli/cmd/tui/thread.ts +1/-1 Extract useful error info from ErrorEvent
2 src/provider/models.ts +11/-7 Defer initial ModelsDev.refresh() via setTimeout(…, 0)
3 test/cli/tui/worker.test.ts +70 E2E worker startup test

Tour

Stop 1: The Fix (models.ts) — RIGOROUSLY REVIEW

The root cause is a classic circular dependency: worker.ts → config.ts → models.ts → installation, where ModelsDev.refresh() runs at module load time and hits Installation.USER_AGENT before Installation is initialized.

The setTimeout(…, 0) defers execution to the next microtask tick, allowing all modules to finish loading. This is the standard Node.js/Bun pattern for breaking circular init dependencies. The setInterval for periodic refresh is now nested inside the timeout, which is correct — no point scheduling refreshes if the first one hasn't run.

Stop 2: Error Logging (thread.ts) — Notice

Good improvement. ErrorEvent objects stringify to [object ErrorEvent], which is useless for debugging. Now extracts .message and .error?.stack.

Stop 3: E2E Test (worker.test.ts) — Notice

Spawns the actual worker and verifies it starts without errors, then checks RPC communication works. The 3-second wait for module loading is reasonable for an e2e test. Good that the test was verified to catch this exact bug when the fix is reverted.

Summary

Clean, well-diagnosed fix. The setTimeout(…, 0) is the right approach for circular dependency init ordering. No issues found. LGTM.

@anandgupta42 anandgupta42 merged commit fdf662b into main Mar 5, 2026
5 checks passed
@kulvirgit kulvirgit deleted the fix/tui-worker-startup-crash branch March 10, 2026 21:06
anandgupta42 added a commit that referenced this pull request Mar 17, 2026
The TUI showed an empty screen because the worker thread crashed on startup.
ModelsDev.refresh() ran at module load time and accessed Installation.USER_AGENT
before the Installation module finished initializing (circular import chain:
worker.ts → config.ts → models.ts → installation). Deferring the initial
refresh via setTimeout(…, 0) ensures all modules are fully loaded first.

Also improves worker error logging to extract the actual error message from
ErrorEvent (previously logged as useless "[object ErrorEvent]"), and adds an
e2e test that spawns the worker and verifies it starts without errors.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants