Skip to content

fix: [AI-198] prioritize managed engine venv over CWD .venv#199

Merged
anandgupta42 merged 7 commits intomainfrom
fix/engine-python-resolution-priority
Mar 16, 2026
Merged

fix: [AI-198] prioritize managed engine venv over CWD .venv#199
anandgupta42 merged 7 commits intomainfrom
fix/engine-python-resolution-priority

Conversation

@anandgupta42
Copy link
Contributor

What does this PR do?

Fixes altimate-engine not being found when the user runs the CLI from a directory that has its own .venv (any Python project).

ensureEngine() installs altimate-engine into the managed venv (~/.opencode/data/engine/venv/), but resolvePython() was checking the CWD .venv first. This meant the bridge would pick up the user's project Python — which doesn't have altimate-engine — and fail.

Fix: Swap the priority so managed engine venv is checked before CWD .venv.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Issue for this PR

Closes #198

How did you verify your code works?

  • Existing tests pass (bun test test/bridge/client.test.ts — 8/8 pass)
  • Added test: prefers managed engine venv over .venv in cwd
  • TypeScript typecheck passes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • New and existing unit tests pass locally with my changes

…olvePython()`

`ensureEngine()` installs altimate-engine into the managed venv at
`~/.opencode/data/engine/venv/`, but `resolvePython()` checked for a
`.venv` in the user's current working directory first. When running
from any Python project with its own `.venv`, the bridge would spawn
that project's Python — which doesn't have altimate-engine — and fail.

Swap the priority so the managed engine venv is checked before the
CWD `.venv`. Add a test covering the new ordering.

Closes #198
@claude
Copy link

claude bot commented Mar 16, 2026

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review.

The managed engine install was missing the `[warehouses]` optional
dependency group, which includes `snowflake-connector-python`,
`psycopg2-binary`, `duckdb`, and other database connectors. This
caused FinOps tools and warehouse operations to fail with
"snowflake-connector-python not installed" errors.

Closes #196
@anandgupta42
Copy link
Contributor Author

@claude review

…ws paths, add startup mutex

- Validate Python binary exists on disk before trusting manifest
  (fixes: manifest exists but venv manually deleted → silent failure)
- Track installed extras in manifest so version match with different
  extras (e.g. upgrading from no-extras to `[warehouses]`) triggers reinstall
- Use platform-aware `venvPythonBin()` helper for dev/cwd venv paths
  (fixes: Windows uses `Scripts/python.exe`, not `bin/python`)
- Add `pendingStart` mutex to `Bridge.call()` preventing concurrent
  `start()` calls from spawning duplicate Python processes
- Add 25 new tests covering: priority ordering, env var edge cases,
  extras tracking, manifest validation, Windows paths, startup mutex,
  concurrent calls
@claude
Copy link

claude bot commented Mar 16, 2026

⚠️ Code review skipped — your organization's overage spend limit has been reached.

Code review is billed via overage credits. To resume reviews, an organization admin can raise the monthly limit in Settings → Usage.

Once credits are available, reopen this pull request to trigger a review.

…s in tests

Address Sentry bot review comments on PR #199:

1. When `start()` spawns a child but `ping` verification fails, kill
   and clear the `child` handle so subsequent `call()` invocations
   trigger a restart instead of writing to a broken process and
   hanging for 30s timeout.

2. Use platform-aware `testVenvPythonBin()` helper in test file for
   dev/cwd venv detection, matching production `venvPythonBin()`
   behavior. Fixes incorrect test skip logic on Windows.
- Remove dead else branch in `ENGINE_INSTALL_SPEC` conditional (constant
  is always truthy)
- Add `extras` field to `engine_started` telemetry event for debugging
- Update `ENGINE_INSTALL_SPEC` comment to reflect production usage
- Tighten concurrent startup test assertion to verify mutex coalescing
  (`ensureEngineCalls <= 2` instead of just `>= 1`)
Address Gemini review findings:

1. Add `error` event handler on spawned child process to prevent
   unhandled ENOENT/EACCES from crashing the host process when
   `resolvePython()` returns a nonexistent or non-executable path.

2. Recreate venv when Python binary is missing even if the venv
   directory still exists (e.g. user deleted just the binary).

3. Re-check `child` state after awaiting `pendingStart` mutex —
   the process may have died between startup completing and the
   secondary caller resuming.
When `child.kill()` is called (e.g. after ping failure), the exit
handler fires with `code = null`. The check `code !== 0` treated
this as a crash, incrementing `restartCount`. After two ping
failures, the bridge would be permanently disabled.

Changed to `code !== null && code !== 0` so only actual non-zero
exit codes (real crashes) count toward the restart limit.
@anandgupta42
Copy link
Contributor Author

@claude review

@anandgupta42 anandgupta42 merged commit 97dfa67 into main Mar 16, 2026
7 checks passed
anandgupta42 added a commit that referenced this pull request Mar 17, 2026
* fix: [AI-198] prioritize managed engine venv over CWD `.venv` in `resolvePython()`

`ensureEngine()` installs altimate-engine into the managed venv at
`~/.opencode/data/engine/venv/`, but `resolvePython()` checked for a
`.venv` in the user's current working directory first. When running
from any Python project with its own `.venv`, the bridge would spawn
that project's Python — which doesn't have altimate-engine — and fail.

Swap the priority so the managed engine venv is checked before the
CWD `.venv`. Add a test covering the new ordering.

Closes #198

* fix: [AI-196] install `altimate-engine[warehouses]` extra by default

The managed engine install was missing the `[warehouses]` optional
dependency group, which includes `snowflake-connector-python`,
`psycopg2-binary`, `duckdb`, and other database connectors. This
caused FinOps tools and warehouse operations to fail with
"snowflake-connector-python not installed" errors.

Closes #196

* fix: harden engine bootstrap — validate venv, track extras, fix Windows paths, add startup mutex

- Validate Python binary exists on disk before trusting manifest
  (fixes: manifest exists but venv manually deleted → silent failure)
- Track installed extras in manifest so version match with different
  extras (e.g. upgrading from no-extras to `[warehouses]`) triggers reinstall
- Use platform-aware `venvPythonBin()` helper for dev/cwd venv paths
  (fixes: Windows uses `Scripts/python.exe`, not `bin/python`)
- Add `pendingStart` mutex to `Bridge.call()` preventing concurrent
  `start()` calls from spawning duplicate Python processes
- Add 25 new tests covering: priority ordering, env var edge cases,
  extras tracking, manifest validation, Windows paths, startup mutex,
  concurrent calls

* fix: clean up child process on ping failure + use platform-aware paths in tests

Address Sentry bot review comments on PR #199:

1. When `start()` spawns a child but `ping` verification fails, kill
   and clear the `child` handle so subsequent `call()` invocations
   trigger a restart instead of writing to a broken process and
   hanging for 30s timeout.

2. Use platform-aware `testVenvPythonBin()` helper in test file for
   dev/cwd venv detection, matching production `venvPythonBin()`
   behavior. Fixes incorrect test skip logic on Windows.

* fix: address multi-model code review feedback

- Remove dead else branch in `ENGINE_INSTALL_SPEC` conditional (constant
  is always truthy)
- Add `extras` field to `engine_started` telemetry event for debugging
- Update `ENGINE_INSTALL_SPEC` comment to reflect production usage
- Tighten concurrent startup test assertion to verify mutex coalescing
  (`ensureEngineCalls <= 2` instead of just `>= 1`)

* fix: handle spawn errors, venv recovery, and post-mutex child guard

Address Gemini review findings:

1. Add `error` event handler on spawned child process to prevent
   unhandled ENOENT/EACCES from crashing the host process when
   `resolvePython()` returns a nonexistent or non-executable path.

2. Recreate venv when Python binary is missing even if the venv
   directory still exists (e.g. user deleted just the binary).

3. Re-check `child` state after awaiting `pendingStart` mutex —
   the process may have died between startup completing and the
   secondary caller resuming.

* fix: don't increment `restartCount` when process is killed by signal

When `child.kill()` is called (e.g. after ping failure), the exit
handler fires with `code = null`. The check `code !== 0` treated
this as a crash, incrementing `restartCount`. After two ping
failures, the bridge would be permanently disabled.

Changed to `code !== null && code !== 0` so only actual non-zero
exit codes (real crashes) count toward the restart limit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

altimate-engine not found when user's project has .venv in cwd

1 participant