Skip to content

fix(kernel): keep local imports and DEEPNOTE_PROJECT_ID working outside Deepnote Cloud - #155

Merged
tkislan merged 2 commits into
mainfrom
tk/fix-set-notebook-path-detached
Nov 26, 2026
Merged

tkislan merged 2 commits into
mainfrom
tk/fix-set-notebook-path-detached

Conversation

@tkislan

@tkislan tkislan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #153.

Why

The VS Code extension and the deepnote CLI start the toolkit through runtime-core, in detached mode. Jupyter and the kernel start in the .deepnote file's folder. set_notebook_path still rewrote sys.path and the working directory against ~/work there. That removed the notebook's folder from sys.path, so a module next to the .deepnote file could not be imported.

Imports only worked when parsing the project ID from the session name crashed first. A notebook name with two colons (sales:q1:report) parsed into a garbage ID, and the rewrite broke imports again. Because of this, deepnote/vscode-deepnote#617 stopped setting DEEPNOTE_PROJECT_ID, so user code no longer saw it locally.

What changes

set_notebook_path now reads the runtime config before the Jupyter session lookup:

  • Keeps the kernel's working directory and sys.path when the toolkit runs detached, outside dev mode, and with neither paths.notebook_root nor paths.home_dir set. In that setup the session name is no longer parsed.
  • Exports runtime.project_id as DEEPNOTE_PROJECT_ID in detached or dev mode. The config loader already resolves DEEPNOTE_PROJECT_ID and DEEPNOTE_RUNTIME__PROJECT_ID and ignores empty values. The session-name parse remains the fallback when no ID is configured.

Attached runs (Deepnote Cloud), dev mode and configs with explicit paths keep the rewrite.

There is one intended change in dev mode: when DEEPNOTE_RUNTIME__PROJECT_ID or DEEPNOTE_PROJECT_ID is set, the configured ID now takes priority over the ID parsed from the session name. That is the value every other reader of the project ID already uses.

docs/user/configuration.md and the function's docstring describe the new behavior.

Tests

New tests in tests/unit/test_set_notebook_path_config.py load the config through the real loader:

Test On main
Detached, no paths, ID configured: cwd and sys.path kept, ID exported fails
Detached, no paths, no ID, two-colon session name: cwd and sys.path kept, nothing exported fails
Dev mode, ID configured: the configured ID wins over the session name fails
Attached, dev mode, and detached + dev mode still rewrite to ~/work/<dir> or /work/<dir> passes (guards today's behavior)
  • _cfg() in tests/unit/test_set_notebook_path.py gains project_id=None, because the function now reads it.
  • A mutation check over the gate and the export confirms the tests catch realistic mistakes. Each of these makes a test fail:
    • the gate includes dev mode;
    • it ignores detached mode;
    • it ignores home_dir;
    • there is no early return;
    • the ID is exported only on the early-return path.
  • tests/unit gives 1468 passed and 4 skipped, with the same 13 warnings as main.
  • An end-to-end run used a real python -m deepnote_toolkit server launched the way runtime-core does it, with session name sales:q1:report. The kernel stays in the project folder, import helper_mod works, and DEEPNOTE_PROJECT_ID is the configured ID. On main, the import fails and the ID is report.

Follow-ups (not in this PR)

  • vscode-deepnote: in the same change that bumps the toolkit pin, remove the interim workaround that serves DEEPNOTE_PROJECT_ID from the integration env endpoint. Remove it no earlier, because kernels on the currently pinned toolkit rely on it.
  • DataFrame storage location: resolve_project_root() in deepnote_toolkit/artifacts/_dataframe_storage.py still returns $HOME/work for runtime-core kernels, not their working directory. Storage lands in the same place as on main. Whether it should move to the kernel's folder is a separate decision.
  • Existing test leak: test_set_notebook_path_updates_chdir_and_env leaves DEEPNOTE_PROJECT_ID set after it runs. This is already the case on main.

…de Deepnote Cloud

runtime-core, which the VS Code extension and the deepnote CLI use,
starts the toolkit in detached mode, with Jupyter and the kernel in the
.deepnote file's folder. set_notebook_path still rewrote sys.path and
the working directory against ~/work there. That removed the
notebook's folder from sys.path, so a module next to the .deepnote
file could not be imported. Imports only worked when parsing the
project ID from the session name crashed first; a notebook name with
two colons parsed into a garbage ID and broke them again.

In detached mode, outside dev mode, and with neither
paths.notebook_root nor paths.home_dir set, the kernel now keeps the
working directory and sys.path it starts with. In detached or dev mode,
runtime.project_id is exported as DEEPNOTE_PROJECT_ID before the
Jupyter session lookup, so user code sees the same variable as in
Cloud. In the detached case above, the session name is no longer
parsed. Attached runs, dev mode and configs with explicit paths keep
the rewrite.

Closes #153
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

📦 Python package built successfully!

  • Version: 2.9.1.dev7+2266b92
  • Wheel: deepnote_toolkit-2.9.1.dev7+2266b92-py3-none-any.whl
  • Install:
    pip install "deepnote-toolkit @ https://deepnote-staging-runtime-artifactory.s3.amazonaws.com/deepnote-toolkit-packages/2.9.1.dev7%2B2266b92/deepnote_toolkit-2.9.1.dev7%2B2266b92-py3-none-any.whl"

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.57%. Comparing base (93f1afe) to head (731329d).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
deepnote_toolkit/set_notebook_path.py 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #155      +/-   ##
==========================================
+ Coverage   78.46%   78.57%   +0.10%     
==========================================
  Files         121      121              
  Lines        6928     6935       +7     
  Branches     1007     1009       +2     
==========================================
+ Hits         5436     5449      +13     
+ Misses       1182     1179       -3     
+ Partials      310      307       -3     
Flag Coverage Δ
combined 78.57% <83.33%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@deepnote-bot

deepnote-bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🚀 Review App Deployment Started

📝 Description 🌐 Link / Info
🌍 Review application ra-155
🔑 Sign-in URL Click to sign-in
📊 Application logs View logs
🔄 Actions Click to redeploy
🚀 ArgoCD deployment View deployment
⏰ Last deployed 2026-11-24 07:24:27 (UTC)
📜 Deployed commit 7e89903d72f2eaf9ae95307b732e0b1cc82358e0
🛠️ Toolkit version 2266b92

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

set_notebook_path now reads runtime and path configuration before resolving the notebook root. In detached or development mode, it exports a configured project ID as DEEPNOTE_PROJECT_ID. In detached mode outside development mode, it returns without changing the working directory or sys.path when neither notebook root nor home directory is configured. Tests and configuration documentation cover these behaviors.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant set_notebook_path
  participant RuntimePathConfig
  participant ProcessEnvironment
  participant KernelPaths
  set_notebook_path->>RuntimePathConfig: Read runtime and path configuration
  set_notebook_path->>ProcessEnvironment: Export configured project ID in detached or dev mode
  set_notebook_path->>KernelPaths: Preserve current directory and sys.path when detached without explicit paths
  set_notebook_path->>KernelPaths: Resolve notebook root and update paths in other cases
Loading

Suggested reviewers: m1so, mfranczel

Merge Risk: Minimal · up to 73132

Detached kernels outside Deepnote Cloud keep local imports working and receive the configured project ID. Cloud and attached behavior is preserved. One deliberate dev-mode change gives a configured project ID priority over the one parsed from the session name. No merge-blocking risk was found.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed Title names the kernel fix: keeping local imports and DEEPNOTE_PROJECT_ID working outside Deepnote Cloud. The raw summary and objectives show set_notebook_path skips the path rewrite in detached mode …
Description check Passed Description states the problem (runtime-core detached kernels, sys.path and cwd rewrite, session-name parsing with colons), the changes to set_notebook_path, the dev-mode priority change, and tests ru…
Linked Issues check Passed [#153] Requirements checked against deepnote_toolkit/set_notebook_path.py at head:
1. Detached gate: _keep_kernel_cwd is true only when running_in_detached_mode is set, dev_mode is not set, and …
Out of Scope Changes check Passed The changed files in the summary are deepnote_toolkit/set_notebook_path.py, docs/user/configuration.md, tests/unit/test_set_notebook_path.py, and tests/unit/test_set_notebook_path_config.py. Each maps…
Updates Docs Passed The PR updates the in-repo user documentation. The diff changes docs/user/configuration.md (lines 148 and 151). The runtime.running_in_detached_mode entry now says that kernels keep their working …
Full details: Title check

Explanation

Title names the kernel fix: keeping local imports and DEEPNOTE_PROJECT_ID working outside Deepnote Cloud. The raw summary and objectives show set_notebook_path skips the path rewrite in detached mode and exports runtime.project_id, which matches the title.

Full details: Description check

Explanation

Description states the problem (runtime-core detached kernels, sys.path and cwd rewrite, session-name parsing with colons), the changes to set_notebook_path, the dev-mode priority change, and tests run. It matches the changeset and the objectives.

Full details: Linked Issues check

Explanation

[#153] Requirements checked against deepnote_toolkit/set_notebook_path.py at head:

  1. Detached gate: _keep_kernel_cwd is true only when running_in_detached_mode is set, dev_mode is not set, and neither paths.notebook_root nor paths.home_dir is set. When true, the function returns before resolve_notebook_root() and before the Jupyter session lookup. The session name is not parsed. Met.
  2. Export: when detached or dev mode and runtime.project_id is set, env.set_env("DEEPNOTE_PROJECT_ID", project_id) runs before the session lookup and before the early return. Met.
  3. Session-name fallback: parsing runs only when _detached and DEEPNOTE_PROJECT_ID is still unset. Dev-mode fallback is kept. Met.
  4. Attached runs keep the rewrite, because _detached is false and _keep_kernel_cwd is false. Met.
  5. Dev-mode priority: the configured ID is exported before the session parse, and the parse is skipped when the variable is set. This matches the issue's dev-mode export test. It differs from the issue's general line that dev-mode behavior is unchanged. The specific criterion governs, so this is not a failure.
    Tests and docs: the PR description reports regression and guard tests in tests/unit/test_set_notebook_path_config.py and a _cfg() update. Docstring and docs/user/configuration.md are updated. I did not read the test file. Test results are taken from the PR description, not from a repository run.
    Out-of-scope items in the issue (Streamlit signal, get_project_id helper, VS Code workaround removal) are not touched by the changed files listed in the summary.
Full details: Out of Scope Changes check

Explanation

The changed files in the summary are deepnote_toolkit/set_notebook_path.py, docs/user/configuration.md, tests/unit/test_set_notebook_path.py, and tests/unit/test_set_notebook_path_config.py. Each maps to #153: the code implements the gate and export, the docs describe the new behavior, and the tests cover the acceptance criteria. The PR description lists follow-ups (VS Code workaround, DataFrame storage, test leak) as not included, and the summary shows no changes to those areas. Limitation: the base-to-head text diff could not be read, so the file list comes from the summary only.

Full details: Updates Docs

Explanation

The PR updates the in-repo user documentation. The diff changes docs/user/configuration.md (lines 148 and 151). The runtime.running_in_detached_mode entry now says that kernels keep their working directory and sys.path outside dev mode when paths.notebook_root and paths.home_dir are unset. The runtime.project_id entry now says that detached or dev-mode kernels see the ID as DEEPNOTE_PROJECT_ID. The PR also updates the set_notebook_path docstring, per its description, and adds tests. Reminder: the primary documentation lives in the deepnote/deepnote repository. The roadmap lives in the deepnote-internal repository. This run cannot see either repository. Update both for this feature. Document the detached-mode path behavior, the DEEPNOTE_PROJECT_ID export, and the dev-mode priority change for the configured project ID.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

LanguageTool

LanguageTool checks were skipped: reviews.tools.languagetool.enabled_only requires at least one selection in enabled_rules or enabled_categories. Select rules/categories, set enabled_only to false to use the default rules, or set enabled to false to disable LanguageTool.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 616341aa-eb2c-415b-a464-02f9a8edf86f
Commits

Reviewing files that changed from the base of the PR and between 93f1afe and 731329d.

Files selected for processing: 4

  • deepnote_toolkit/set_notebook_path.py
  • docs/user/configuration.md
  • tests/unit/test_set_notebook_path.py
  • tests/unit/test_set_notebook_path_config.py

Included review availability: This review used your included allowance. 3 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 4 reviews per hour.

@tkislan

tkislan commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Full review finished.

@tkislan
tkislan requested a review from m1so November 26, 2026 10:48
@tkislan
tkislan marked this pull request as ready for review November 26, 2026 10:56
@tkislan
tkislan requested a review from a team as a code owner November 26, 2026 10:56
@tkislan
tkislan requested a review from mfranczel November 26, 2026 10:56
@tkislan
tkislan merged commit 39537e8 into main Nov 26, 2026
45 checks passed
@tkislan
tkislan deleted the tk/fix-set-notebook-path-detached branch November 26, 2026 12:05
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.

set_notebook_path breaks local imports outside Deepnote Cloud, and DEEPNOTE_PROJECT_ID goes missing

3 participants