Skip to content

fix: keep conda and virtualenv activation across session restore - #2367

Open
happysnehal111-del wants to merge 1 commit into
herdrdev:masterfrom
happysnehal111-del:fix/restore-pane-virtual-env
Open

fix: keep conda and virtualenv activation across session restore#2367
happysnehal111-del wants to merge 1 commit into
herdrdev:masterfrom
happysnehal111-del:fix/restore-pane-virtual-env

Conversation

@happysnehal111-del

Copy link
Copy Markdown

Problem

A pane that was working inside an activated conda environment or virtualenv comes back from a session restore on the shell's default environment. The agent resumes, but against the wrong interpreter — python, pip, and anything else installed into the environment resolve outside it.

Restore spawns the pane's shell with an empty PaneLaunchEnv, and the snapshot has nowhere to record an activation, so there is nothing to hand back.

How the environment is read

A shell mutates its own environment in place when it activates one, and that mutation is not visible from outside the process. What is visible is the environment a process was launched with, so the pane's foreground process group leader — the command the shell started — carries the activation that was in effect when it began. In the case that matters, that process is the agent.

The primitives for this were already here: /proc/<pid>/environ on Linux, KERN_PROCARGS2 on macOS, and the PEB read on Windows. This adds one function per platform on top of them, and a fallback stub elsewhere.

What gets stored

Only the activation prefix and its display name, not a copy of PATH. PATH is rebuilt from the prefix on restore, so a restored pane follows the current machine rather than being pinned to whatever it looked like when the snapshot was written. Entries already on the inherited PATH are not repeated, so repeated restores cannot grow it.

PaneSnapshot.virtual_env is optional and skipped when absent, so SNAPSHOT_VERSION stays at 3 and sessions written by this build still load on older ones.

conda's automatic base activation

Restored panes come back as interactive login shells, so they re-run the user's rc files, and conda's init hook activates base by default. That runs after the environment is handed in and replaces it. It shadows a restored virtualenv too, because base lands ahead of it on PATH. CONDA_AUTO_ACTIVATE and CONDA_AUTO_ACTIVATE_BASE are therefore set to false for restored panes that have a recorded environment — both spellings, since conda renamed the setting in 25.x. Panes without a recorded environment are untouched and still auto-activate base as before.

Testing

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, and the cross-target Windows clippy from just windows-lint are clean.
  • cargo nextest run passes except for three tests that fail identically on an unmodified master on this machine: live_handoff_keeps_agent_started_pane_after_agent_exits, live_handoff_keeps_unmanaged_agent_name_bound_to_saved_session, and pane_info_and_subscriptions_expose_done_agent_status.
  • Verified end to end on macOS against a real build in an isolated config directory: activate an environment in a pane, start a long-running process, stop the server, relaunch. Both conda and venv come back with the right prefix and with python resolving into the environment.
  • The Linux and Windows paths are compile-checked but not exercised on hardware — I don't have either available.

@kangal-bot kangal-bot added the ai-review Trigger automated AI reviews for pull requests admitted by the PR gate label Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: eb1c674f-ee13-4f0e-b79d-2686dc7b7e7b

📥 Commits

Reviewing files that changed from the base of the PR and between 2863b71 and e6308ed.

📒 Files selected for processing (12)
  • src/app/agent_resume.rs
  • src/pane.rs
  • src/persist/restore.rs
  • src/persist/snapshot.rs
  • src/platform/fallback.rs
  • src/platform/linux.rs
  • src/platform/macos.rs
  • src/platform/mod.rs
  • src/platform/windows.rs
  • src/terminal/runtime.rs
  • src/terminal/state.rs
  • src/workspace/tab.rs
🚧 Files skipped from review as they are similar to previous changes (12)
  • src/terminal/state.rs
  • src/workspace/tab.rs
  • src/platform/linux.rs
  • src/platform/fallback.rs
  • src/platform/windows.rs
  • src/platform/macos.rs
  • src/terminal/runtime.rs
  • src/app/agent_resume.rs
  • src/platform/mod.rs
  • src/pane.rs
  • src/persist/restore.rs
  • src/persist/snapshot.rs

📝 Walkthrough

Walkthrough

The change detects Conda and venv activations across platforms. Pane snapshots store activation metadata. Workspace capture, terminal restoration, and deferred agent resume preserve this state and apply its environment variables to launches.

Changes

Virtual environment persistence

Layer / File(s) Summary
Activation model and environment parsing
src/platform/mod.rs
Adds Conda and venv activation types, process-environment parsing, PATH construction, launch overrides, and tests.
Foreground process detection
src/platform/*.rs, src/pane.rs, src/terminal/runtime.rs, src/workspace/tab.rs
Reads process environments on supported platforms and exposes the foreground activation through pane, terminal, and tab runtimes.
Snapshot capture and compatibility
src/persist/snapshot.rs, src/terminal/state.rs
Snapshots optionally serialize virtual-environment metadata, restore supported kinds, preserve absent values, and ignore unknown kinds.
Restored launch integration
src/persist/restore.rs, src/app/agent_resume.rs
Restored terminals and deferred agent resumes apply activation environment overrides and retain reconstructed activation state. Cold-restore tests verify Conda metadata and removal of missing environments.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Tab
  participant TerminalRuntime
  participant PaneRuntime
  participant PlatformProcessReader
  participant Snapshot
  participant Restore
  participant Terminal
  Tab->>TerminalRuntime: request foreground_virtual_env
  TerminalRuntime->>PaneRuntime: request foreground_virtual_env
  PaneRuntime->>PlatformProcessReader: read process environment
  PlatformProcessReader-->>PaneRuntime: return VirtualEnvActivation
  PaneRuntime-->>TerminalRuntime: return activation
  TerminalRuntime-->>Tab: return activation
  Tab->>Snapshot: capture activation
  Snapshot-->>Restore: provide saved activation
  Restore->>Terminal: apply activation during launch
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes preserving Conda and virtualenv activation during session restore.
Description check ✅ Passed The description explains the restore problem, implementation, compatibility behavior, and testing for virtual-environment activation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 19bca225-41b0-4f49-8c9d-8d125c9b9874

📥 Commits

Reviewing files that changed from the base of the PR and between eb93ec8 and 7d17ab3.

📒 Files selected for processing (12)
  • src/app/agent_resume.rs
  • src/pane.rs
  • src/persist/restore.rs
  • src/persist/snapshot.rs
  • src/platform/fallback.rs
  • src/platform/linux.rs
  • src/platform/macos.rs
  • src/platform/mod.rs
  • src/platform/windows.rs
  • src/terminal/runtime.rs
  • src/terminal/state.rs
  • src/workspace/tab.rs

Comment thread src/pane.rs
Comment thread src/persist/restore.rs
Comment thread src/persist/snapshot.rs
Comment thread src/platform/mod.rs
Comment thread src/platform/mod.rs
A pane working inside an activated conda environment or virtualenv came
back from restore on the shell's default environment, so an agent resumed
into that pane ran against the wrong interpreter.

Panes now record the activation prefix alongside their cwd, and restore
hands it back to the shell it spawns, including the deferred spawn that
resumes an agent. PATH is rebuilt from the prefix instead of replayed
from the snapshot so a restored pane follows the current machine, and an
environment that has been removed since the save is dropped rather than
re-entered. The restored shell re-runs the user's rc files, so conda's
automatic base activation is suppressed for it; otherwise base lands on
PATH ahead of whatever was restored.
@happysnehal111-del
happysnehal111-del force-pushed the fix/restore-pane-virtual-env branch from 7d17ab3 to e6308ed Compare August 5, 2026 16:50
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown

Greptile Summary

This PR persists detected conda and virtualenv activation metadata and reconstructs the corresponding launch environment during pane restoration and deferred agent resume.

  • Adds cross-platform foreground-process environment detection.
  • Stores optional environment kind, prefix, and display name in pane snapshots.
  • Rebuilds activation variables and PATH while suppressing conda base auto-activation.
  • Retains restored activation state for idle-pane snapshots and deferred agent resumes.
  • Adds serialization, restoration, and environment-construction tests.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete blocking or independently actionable non-blocking defects identified.

The snapshot field is optional and backward-compatible, missing or deleted environments degrade to the inherited environment, and activation reconstruction is consistently applied to both cold restore and deferred agent resume paths.

Important Files Changed

Filename Overview
src/platform/mod.rs Defines activation parsing and reconstructs platform-specific PATH and environment variables for restored panes.
src/pane.rs Detects activation from the pane’s foreground process-group leader without adding a blocking PTY actor round trip.
src/persist/snapshot.rs Adds backward-compatible optional activation metadata and captures live or restored activation state.
src/persist/restore.rs Validates saved prefixes, applies reconstructed launch variables, and carries activation state onto restored terminals.
src/app/agent_resume.rs Applies the restored interpreter environment when resuming an agent through a newly spawned shell.
src/platform/windows.rs Reads virtual-environment variables from a selected process’s PEB environment block.
src/platform/linux.rs Reads virtual-environment variables from the foreground process environment exposed through procfs.
src/platform/macos.rs Reads virtual-environment variables from the foreground process environment exposed through KERN_PROCARGS2.

Sequence Diagram

sequenceDiagram
  participant Save as Session snapshot
  participant Pane as Pane runtime
  participant OS as Platform process API
  participant Restore as Session restore
  participant Shell as Restored shell
  Save->>Pane: Request foreground activation
  Pane->>OS: Read foreground process environment
  OS-->>Pane: Environment prefix and kind
  Pane-->>Save: VirtualEnvActivation
  Save->>Save: Persist kind, prefix, and name
  Restore->>Restore: Validate saved prefix
  Restore->>Restore: Rebuild activation variables and PATH
  Restore->>Shell: Spawn with PaneLaunchEnv
  Restore->>Restore: Retain activation in TerminalState
Loading

Reviews (1): Last reviewed commit: "fix: keep conda and virtualenv activatio..." | Re-trigger Greptile

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

Labels

ai-review Trigger automated AI reviews for pull requests admitted by the PR gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants