Add a per-command `cwd` to `Workspace.run`
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.runandBubblewrapWorkspace.runaccept it too.WorkspaceBackendSuite.test_run_starts_in_cwdconformance 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 onmainwithTypeError: Workspace.run() got an unexpected keyword argument 'cwd'): relative/absolute and quoted names, wrappers, exit codes, missing program 127, aPATHwithoutsh, missing dir, file,TypeErrorordering.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.
cwdis intentionally not added to theSupportsCommandsprotocol;Workspaceimplements it once for all backends.ReadOnlyWorkspace.run,BubblewrapWorkspace.run, and durable workspace calls accept and forwardcwd;WorkspaceCall.cwdjournals it.- Adds the
test_run_starts_in_cwdconformance rule covering relative/absolute paths, quoted names, wrappers, exit codes, missing programs, and aPATHwithoutsh. - Custom
WrapperWorkspaceoverrides should take and forwardcwd; otherwise a call raisesTypeError.
Nothing changes for existing users unless cwd= is passed.