Skip to content

[Enh] Set the session cwd to use server root_dir instead of chat directory#108

Draft
Zsailer wants to merge 2 commits into
jupyter-ai-contrib:mainfrom
Zsailer:fix/session-cwd-root-dir
Draft

[Enh] Set the session cwd to use server root_dir instead of chat directory#108
Zsailer wants to merge 2 commits into
jupyter-ai-contrib:mainfrom
Zsailer:fix/session-cwd-root-dir

Conversation

@Zsailer

@Zsailer Zsailer commented Apr 30, 2026

Copy link
Copy Markdown

When you open a chat while browsing a subdirectory in JupyterLab, you'd expect the AI agent to start working in that same directory. Instead, it was starting in the server process's working directory or the chat file's parent directory. I like to centralize all my chats in to a hidden folder (e.g. .jupyter/chat). I think a better place for the agent to start is in my current working directory (determined by the user/client's UI).

This PR reads the user's file browser path from YChat metadata and uses it as the agent's session working directory. The metadata is set by the frontend when a chat is opened (see jupyterlab/jupyter-chat#420).

The cwd resolution priority is:

  1. Chat metadata cwd (from the file browser) joined with root_dir
  2. root_dir alone (when there's no chat metadata, e.g. older chats)
  3. get_chat_dir() (last resort — the chat file's parent directory)

One subtlety: session creation is deferred from persona __init__ to the first process_message call. This gives the Y.js document time to sync the metadata from the browser before the session reads it. Without this, the metadata would always be empty because the session was created before the frontend's update arrived.

The session cwd was set to persona.get_chat_dir() which resolves
to .jupyter/chats — the directory containing the chat file. This
caused Claude Code to start inside the chat storage directory rather
than the user's file tree.

Use persona.parent.root_dir (the Jupyter server's root directory)
as the session cwd, falling back to get_chat_dir() if root_dir is
not available.
@Zsailer Zsailer changed the title [Bug] Fix session cwd to use server root_dir instead of chat directory [Enh] Set the session cwd to use server root_dir instead of chat directory Apr 30, 2026
@Zsailer Zsailer added the enhancement New feature or request label Apr 30, 2026
@Zsailer

Zsailer commented Apr 30, 2026

Copy link
Copy Markdown
Author

Actually perhaps this is a matter of opinion—I could see a case for either chat directory or server's root dir.

The reason I made this PR is I would like to bury my chats into a hidden folder, i.e. .jupyter/chats, and I don't want my ACP client to start in that hidden folder.

Perhaps we need a way to toggle whether the ACP client follows the chat directory to the filebrowser's CWD.

This is outdated now, since I've made changes to this PR and above.

@Zsailer Zsailer force-pushed the fix/session-cwd-root-dir branch 3 times, most recently from da1d902 to 97d2d43 Compare April 30, 2026 17:14
Read cwd from YChat metadata (set by the frontend when a chat is
opened) and resolve it against root_dir. Falls back to root_dir
then get_chat_dir() when chat metadata has no cwd.

Session creation is deferred from __init__ to the first
process_message call so the Y.js metadata has time to sync from
the browser.
@Zsailer Zsailer force-pushed the fix/session-cwd-root-dir branch from 97d2d43 to e874687 Compare April 30, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant