Skip to content

fix(session): use system Python for external containers#144

Open
MohamedMostafa259 wants to merge 1 commit into
vndee:mainfrom
MohamedMostafa259:fix/use-system-python-for-external-containers
Open

fix(session): use system Python for external containers#144
MohamedMostafa259 wants to merge 1 commit into
vndee:mainfrom
MohamedMostafa259:fix/use-system-python-for-external-containers

Conversation

@MohamedMostafa259

@MohamedMostafa259 MohamedMostafa259 commented Jan 20, 2026

Copy link
Copy Markdown

When connecting to containers via container_id, use system Python instead of assuming the container has llm-sandbox's venv structure.

External containers created by users likely don't have the /sandbox/.sandbox-venv/bin/python path that llm-sandbox creates. The previous logic incorrectly assumed all existing containers had this venv structure.

Fixes #127

Summary by CodeRabbit

  • Bug Fixes

    • Fixes Python environment selection so virtualenv paths are only used when environment setup runs and no existing container is used; system Python is used for external-container scenarios.
    • Clarifies differing behavior for pooled container execution paths.
  • Tests

    • Adds parameterized tests covering both skipped-setup and external-container scenarios to verify the interpreter choice.

✏️ Tip: You can customize this high-level summary in your review settings.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 20, 2026
@coderabbitai

coderabbitai Bot commented Jan 20, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Revised the condition that supplies sandbox venv paths to runtime_context: venv paths are now used only when environment setup is not skipped AND an existing container is not used. Tests were parameterized to validate both skip-environment and external-container scenarios.

Changes

Cohort / File(s) Change Summary
Core venv path logic
llm_sandbox/core/session_base.py
Changed use_venv_paths condition from not skip_environment_setup OR using_existing_container to not skip_environment_setup AND not using_existing_container; updated runtime_context assignments for python/pip executables and pip cache; added comment clarifying pooled container handling.
Test coverage
tests/test_session_base.py
Parameterized and renamed test to test_run_uses_system_python_not_venv, added config_kwargs/test_id cases to cover skip_environment_setup=True and external container (container_id) usage, and adjusted assertions to ensure system Python (no sandbox-venv paths) is used.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

python

Suggested reviewers

  • vndee

Poem

🐰 I hopped through code, adjusted the trail,

Swapped the paths so the right one prevails,
No phantom venv to trip the run,
System Python greets the sun,
Hooray — tests pass, nibble the carrot! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: using system Python for external containers instead of the sandbox virtualenv path.
Linked Issues check ✅ Passed The PR directly addresses issue #127 by fixing the logic that determines when to use venv paths, ensuring external containers use system Python.
Out of Scope Changes check ✅ Passed All changes are scoped to the venv path selection logic in session_base.py and corresponding test coverage, directly addressing the linked issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added the bug Something isn't working label Jan 20, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
llm_sandbox/core/session_base.py (1)

485-498: Align library installation with the new system‑Python path logic.

Line 485-498 correctly avoid venv paths for external containers, but run() still calls install(libraries) and install() always injects venv python/pip paths for Python. For external containers with libraries, this likely still resolves to a non‑existent .../.sandbox-venv/bin/pip and fails. Consider reusing the same use_venv_paths decision in install() or explicitly blocking installs for existing containers with a clear error.

Possible fix (apply in install())
use_venv_paths = self.language_handler.name == "python" and (
    not self.config.skip_environment_setup and not self.using_existing_container
)
runtime_context = RuntimeContext(
    workdir=self.config.workdir,
    python_executable_path=self.python_executable_path if use_venv_paths else None,
    pip_executable_path=self.pip_executable_path if use_venv_paths else None,
    pip_cache_dir=self.pip_cache_dir_path if use_venv_paths else None,
)

@MohamedMostafa259 MohamedMostafa259 force-pushed the fix/use-system-python-for-external-containers branch from f5466a8 to 05374f7 Compare January 20, 2026 17:19
@codecov

codecov Bot commented Jan 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.8%. Comparing base (9304f10) to head (2dab798).

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #144   +/-   ##
=====================================
  Coverage   91.8%   91.8%           
=====================================
  Files         42      42           
  Lines       2662    2662           
  Branches     330     330           
=====================================
  Hits        2445    2445           
  Misses       120     120           
  Partials      97      97           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

When connecting to containers via container_id, use system Python
instead of assuming the container has llm-sandbox's venv structure.

External containers created by users likely don't have the
/sandbox/.sandbox-venv/bin/python path that llm-sandbox creates.
The previous logic incorrectly assumed all existing containers
had this venv structure.

Fixes vndee#127
@MohamedMostafa259 MohamedMostafa259 force-pushed the fix/use-system-python-for-external-containers branch from 05374f7 to 2dab798 Compare January 20, 2026 17:43
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR attempts to fix an issue where external containers (connected via container_id) were incorrectly assumed to have llm-sandbox's virtualenv structure at /sandbox/.sandbox-venv/bin/python. The fix changes the logic to use system Python for external containers instead of the venv path.

Changes:

  • Modified the use_venv_paths logic in session_base.py to use system Python when using existing containers
  • Updated comments to clarify when venv paths should and shouldn't be used
  • Added parameterized tests to verify system Python is used for both skip_environment_setup=True and external container scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
llm_sandbox/core/session_base.py Changed venv path logic from OR to AND condition to exclude external containers from using venv paths
tests/test_session_base.py Added parameterized test covering skip_environment_setup and container_id scenarios, with improved error messages

Comment on lines +488 to +489
# Note: For pooled containers, they use a different code path via PooledSandboxSession
# which explicitly sets up the venv and knows it exists.

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

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

This comment is misleading. PooledSandboxSession does call BaseSession.run(), which executes this code path. The comment suggests pooled containers use a different code path, but they actually delegate to the base session's run() method (see llm_sandbox/pool/session.py line 312). Additionally, pooled containers DO have venv (created during pool initialization), but the new logic will make them use system Python, which is incorrect.

Copilot uses AI. Check for mistakes.
Comment on lines +686 to +693
"""Test that run() uses system Python instead of venv in specific scenarios.

Scenarios:
- skip_environment_setup=True: venv is not created, use system Python
- container_id (external container): venv may not exist, use system Python

Related to: https://github.com/vndee/llm-sandbox/issues/127
"""

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

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

The test covers the external container scenario (container_id set by user), but doesn't verify that pooled containers still use venv correctly. Pooled containers also set both skip_environment_setup=True and container_id, but they should use venv since it exists. Consider adding a test case for pooled containers or verifying that the venv path actually exists before deciding which Python to use.

Copilot uses AI. Check for mistakes.
Comment on lines +485 to 492
# Use venv paths for Python ONLY when:
# 1. Not skipping environment setup (normal case - we created the venv)
# 2. AND not using an existing container (external containers may not have venv)
# Note: For pooled containers, they use a different code path via PooledSandboxSession
# which explicitly sets up the venv and knows it exists.
use_venv_paths = self.language_handler.name == "python" and (
not self.config.skip_environment_setup or self.using_existing_container
not self.config.skip_environment_setup and not self.using_existing_container
)

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

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

The new logic breaks pooled containers. Pooled containers are created with venv during initialization (without skip_environment_setup or container_id flags), but when PooledSandboxSession connects to them, it passes both container_id and skip_environment_setup=True. With the new logic, this evaluates to False, causing pooled containers to use system Python instead of the venv that exists.

The old logic correctly handled this: not skip_environment_setup or using_existing_container evaluated to True for pooled containers (True or True), making them use venv.

The fix for issue #127 (external containers without venv) should not break pooled containers (which have venv). Consider adding a flag to distinguish between external user-provided containers and internal pooled containers, or checking if the venv path actually exists before deciding which Python to use.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@MohamedMostafa259 could you help me check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skip_environment_setup still uses /sandbox/.sandbox-venv/bin/python

3 participants