Skip to content

Pass cwd to agents from backend#8491

Merged
mscolnick merged 5 commits intomainfrom
sham/pass-filepath-to-agents
Mar 3, 2026
Merged

Pass cwd to agents from backend#8491
mscolnick merged 5 commits intomainfrom
sham/pass-filepath-to-agents

Conversation

@Light2Dark
Copy link
Collaborator

@Light2Dark Light2Dark commented Feb 27, 2026

📝 Summary

Previously, the agent would not know the absolute filepath, this led to errors when an agent is instantiated outside of the current directory. This fix ensures current working directory is passed from the backend on-mount, and subsequently passed to the agent.

Screenshot 2026-03-03 at 11 31 58 PM

🔍 Description of Changes

📋 Checklist

  • I have read the contributor guidelines.
  • For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on Discord, or the community discussions (Please provide a link if applicable).
  • Tests have been added for the changes made.
  • Documentation has been updated where applicable, including docstrings for API changes.
  • Pull request title is a good summary of the changes - it will be used in the release notes.

@Light2Dark Light2Dark requested a review from manzt as a code owner February 27, 2026 08:12
@vercel
Copy link

vercel bot commented Feb 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Error Error Mar 3, 2026 7:37pm

Request Review

@Light2Dark Light2Dark added the bug Something isn't working label Feb 27, 2026
@mscolnick
Copy link
Contributor

it's unfortunate to have to pass filename and filepath. can these be merged? I thought filename was absolute? should we force it?

Another option is to call the /files api which gives you the root iirc

@Light2Dark
Copy link
Collaborator Author

Light2Dark commented Feb 27, 2026

it's unfortunate to have to pass filename and filepath. can these be merged? I thought filename was absolute? should we force it?

Another option is to call the /files api which gives you the root iirc

It's a bit tricky, we started passing the relative paths instead of absolute path for filename (where possible). #7722 Not sure if I want to touch this.

The other option, I think we could use files_details endpoint from file_explorer, but this will fetch contents of the file as well. Maybe can create another light endpoint to get absolute path

@mscolnick
Copy link
Contributor

@Light2Dark got it, this is just for home mode? maybe we could just make those absolute URLs. @dmadisetti do you see any issue with that? that does help with consistency, but maybe folks used "home mode" in order to avoid absolute paths.

separately, right now for Agents we use basedir(filename) as the CWD. we don't need to do this now, but maybe it would be better to actually use the CWD, in which the better long-term approach would be to pass that down instead of filepath.

manzt
manzt previously approved these changes Mar 2, 2026
Copy link
Collaborator

@manzt manzt left a comment

Choose a reason for hiding this comment

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

Similar comment to @mscolnick. Is trying to think if there is a way we can require/use one attribute instead of two that could get out of sync.

@manzt
Copy link
Collaborator

manzt commented Mar 2, 2026

I think I like the idea of cwd

@Light2Dark Light2Dark changed the title Pass filepath to agents from backend Pass cwd to agents from backend Mar 3, 2026
Copy link
Contributor

Copilot AI left a comment

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 ensures the frontend agent receives a correct working directory (cwd) derived from the backend’s absolute notebook filepath, preventing path-resolution errors when notebooks are mounted from outside the current directory.

Changes:

  • Add cwd to the server-generated mount config (derived from filepath) and thread it into the notebook/static templates.
  • Introduce a cwdAtom on the frontend, parse cwd at mount-time, and use it to initialize agent sessions / compute absolute notebook paths for agent resources.
  • Update template tests and export HTML snapshots to include the new cwd field.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
marimo/_server/templates/templates.py Adds cwd to mount config and derives it from filepath for notebook pages.
frontend/src/mount.tsx Extends mount options schema to accept cwd and stores it in state.
frontend/src/core/saving/file-state.ts Adds cwdAtom to store the notebook working directory.
frontend/src/components/chat/acp/agent-panel.tsx Uses cwdAtom to set agent session cwd and build an absolute filename for agent resources.
tests/_server/templates/test_templates.py Adds assertions for cwd and a new filepath-driven test case.
tests/_server/templates/snapshots/export1.txt Snapshot update to include "cwd": "" in mount config.
tests/_server/templates/snapshots/export2.txt Snapshot update to include "cwd": "" in mount config.
tests/_server/templates/snapshots/export3.txt Snapshot update to include "cwd": "" in mount config.
tests/_server/templates/snapshots/export4.txt Snapshot update to include "cwd": "" in mount config.
tests/_server/templates/snapshots/export5.txt Snapshot update to include "cwd": "" in mount config.
tests/_server/templates/snapshots/export6.txt Snapshot update to include "cwd": "" in mount config.
Comments suppressed due to low confidence (3)

marimo/_server/templates/templates.py:71

  • cwd is derived from filepath (an absolute server-side path) and embedded into window.__MARIMO_MOUNT_CONFIG__, which is sent to the browser. This can unintentionally disclose server filesystem layout (especially when filename is made relative for directory mode). Consider limiting exposure (e.g., only include cwd when running in a trusted/local mode or when the agent feature is enabled), or pass a server-resolvable identifier instead of an absolute path and have the backend resolve paths for agent operations.
    options: dict[str, Any] = {
        "filename": filename or "",
        "cwd": cwd or "",
        "mode": mode,
        "version": version or get_version(),
        "server_token": str(server_token),
        "user_config": user_config,

tests/_server/templates/test_templates.py:101

  • This assertion is not portable on Windows: the rendered JSON will escape backslashes (e.g., C:\\...), so f'"cwd": "{expected_cwd}"' won’t match. Consider asserting against json.dumps(expected_cwd) (or parsing the mount config JSON and comparing the cwd field) so the test is robust across path separators/escaping.
        )

        assert self.filename.name in result
        assert f'"cwd": "{expected_cwd}"' in result
        _assert_no_leftover_replacements(result)

frontend/src/mount.tsx:153

  • The schema/docs say cwd is an absolute working directory, but the backend currently emits an empty string when unknown. Consider normalizing "" to null in the schema/transform (or updating the doc comment to explicitly state it may be empty/unknown) to avoid downstream code treating an empty string as a valid absolute path.
  /**
   * absolute working directory of the notebook
   */
  cwd: z.string().nullish().default(null),
  /**

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mscolnick mscolnick requested a review from manzt March 3, 2026 18:43
mscolnick
mscolnick previously approved these changes Mar 3, 2026
dmadisetti
dmadisetti previously approved these changes Mar 3, 2026
Copy link
Collaborator

@dmadisetti dmadisetti left a comment

Choose a reason for hiding this comment

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

No issues passing the data, assuming our permissions work

@dmadisetti
Copy link
Collaborator

The windows failure might be real (file path issues)

@Light2Dark
Copy link
Collaborator Author

Light2Dark commented Mar 3, 2026

I pushed a fix but not showing up here. I think its a test issue, it's escaping \ with \ so shows up as \\

@mscolnick mscolnick dismissed stale reviews from dmadisetti and themself via ccab452 March 3, 2026 19:37
@mscolnick mscolnick merged commit 73fc8f7 into main Mar 3, 2026
5 of 9 checks passed
@mscolnick mscolnick deleted the sham/pass-filepath-to-agents branch March 3, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants