Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 1, 2025

Problem

The Install Packages tool in Jupyter notebooks would always prompt users to restart the kernel after installing packages, even when no cells had been executed. This created unnecessary friction for users setting up fresh notebooks.

User workflow before this fix:
1. Open new Jupyter notebook
2. Install packages via chat: "install numpy pandas"
3. 🚫 Prompted to restart kernel (unnecessary)
4. User confused why restart is needed when nothing was executed

Solution

This PR implements a smart kernel restart logic that only prompts for restart when cells have actually been executed. The solution uses VS Code's existing notebookCellExecutions.onDidChangeNotebookCellExecutionState event to track execution state.

Key Components

  1. CellExecutionTracker Service - Tracks whether any cells have been executed in each notebook
  2. Smart InstallPackagesTool - Checks execution state before deciding to restart
  3. Enhanced RestartKernelTool - Resets execution state when kernel is manually restarted

User Experience Improvements

User workflow after this fix:
1. Open new Jupyter notebook
2. Install packages via chat: "install numpy pandas"
3. ✅ No restart prompt - seamless installation
4. Message: "Installation finished successfully. The kernel was not restarted since no cells have been executed yet."

5. Execute a cell: print("Hello")
6. Install more packages: "install matplotlib"
7. ✅ Restart prompt shown (now appropriate)
8. Message: "Installation finished successfully. The kernel has been restarted, so any previously executed cells will need to be re-run."

Implementation Details

  • Event-driven tracking: Uses notebookCellExecutions.onDidChangeNotebookCellExecutionState to detect when cells finish executing
  • Memory efficient: Only tracks boolean state per notebook URI
  • Cross-platform: Works in both Node.js and web environments
  • Proper lifecycle: Service activates on extension startup and cleans up properly
  • Multiple notebook support: Handles multiple notebooks independently

Testing

  • ✅ Comprehensive unit tests for CellExecutionTracker
  • ✅ Integration tests for InstallPackagesTool workflow
  • ✅ Edge case handling (cells without execution order, multiple notebooks)
  • ✅ Manual verification scenarios documented

Backward Compatibility

This change is fully backward compatible:

  • Existing behavior preserved when cells have been executed
  • No breaking changes to existing APIs
  • Same restart functionality when needed

Fixes #16709


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Implementation Plan for Kernel Restart Prompt Logic in Jupyter Notebook Avoid kernel restart prompt when no cells have been executed in Install Packages tool Jul 1, 2025
Copilot AI requested a review from DonJayamanne July 1, 2025 04:18
Copilot finished work on behalf of DonJayamanne July 1, 2025 04:18
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.

When using the install package tool avoid prompting to restart kernel if no cells have been executed

2 participants