Add a per-command `cwd` to `Workspace.run`

Merged
MP
23d ago
+100-12

Fixes #12535

Why we make these changes

Running a workspace command outside the working directory meant shell-wrapping and quoting every call (sh -c 'cd "$0" && exec "$@"'). Per the maintainer decision on the issue (option A), Workspace.run() now takes a backend-neutral cwd=; working_dir keeps its must-exist behavior on attach.

Choice made (noting the deviation from the decision's wording): cwd is not added to the SupportsCommands protocol, which protocol.py declares frozen because conformance is structural, so a new parameter would break every third-party backend. Workspace instead implements it once for every backend: it resolves cwd against the working directory, checks that it is a directory (FileNotFoundError / NotADirectoryError), and at the layer that reaches the backend starts the command through /bin/sh (cd -- <dir> || exit before a shell string, cd -- "$1" && exec "$@" around an argv). So every checked-in backend (Sprites, E2B, Modal, Docker, Daytona, SSH, ...) gets the same semantics without per-backend code, and a new conformance rule checks it on each backend's fake.

New public surface

  • Workspace.run(..., cwd: str | None = None); ReadOnlyWorkspace.run and BubblewrapWorkspace.run accept it too.
  • WorkspaceBackendSuite.test_run_starts_in_cwd conformance rule.

User-visible behavior

await ctx.workspace.run(['pytest'], cwd='packages/core')   # before: TypeError
await ctx.workspace.run('make test', shell=True, cwd='/srv/app')

Under durable execution the call is still one workspace unit, and WorkspaceCall.cwd journals the directory as given; the directory check runs inside the unit. BubblewrapWorkspace applies cwd inside the sandbox.

Verification

  • tests/test_workspace.py::test_run_starts_in_cwd (fails on main with TypeError: Workspace.run() got an unexpected keyword argument 'cwd'): relative/absolute and quoted names, wrappers, exit codes, missing program 127, a PATH without sh, missing dir, file, TypeError ordering.
  • tests/durable_exec/test_durable_workspace.py::test_run_journals_cwd_and_checks_it_inside_the_unit
  • Conformance rule passes for the local, run-only, SSH, bubblewrap and every sandbox fake; bubblewrap rejects run([], cwd='.').
  • ruff and pyright on the changed files.

What changes for existing users

Nothing unless cwd= is passed. A custom WrapperWorkspace that overrides run() should take and forward cwd (documented); otherwise pyright flags the override, and passing cwd through it raises TypeError.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

Summary by cubic

Adds a per-command cwd parameter to Workspace.run so commands can start in another directory without shell-wrapping and quoting each call. cwd resolves relative to the working directory and must be an existing directory: a missing one raises FileNotFoundError, a file NotADirectoryError. The command starts through /bin/sh, so every backend gets the same semantics.

  • cwd is intentionally not added to the SupportsCommands protocol; Workspace implements it once for all backends.
  • ReadOnlyWorkspace.run, BubblewrapWorkspace.run, and durable workspace calls accept and forward cwd; WorkspaceCall.cwd journals it.
  • Adds the test_run_starts_in_cwd conformance rule covering relative/absolute paths, quoted names, wrappers, exit codes, missing programs, and a PATH without sh.
  • Custom WrapperWorkspace overrides should take and forward cwd; otherwise a call raises TypeError.

Nothing changes for existing users unless cwd= is passed.