fix(server): isolate sessions per-worktree — require an explicit directory - #40143
fix(server): isolate sessions per-worktree — require an explicit directory#40143JoshGatto wants to merge 1 commit into
Conversation
Stop binding directory-less requests to the server's process.cwd() so sessions can never resolve to a shared or server-global worktree. - LocationMiddleware: 400 when x-opencode-directory / location[directory] is absent (packages/server/src/location.ts) - workspace-routing: drop cwd from defaultDirectory(); new MissingDirectory plan returns 400 for directory-less requests - session.create: reject payloads without a location (400) instead of defaulting to process.cwd() - test: assert directory-less session creation returns a v2 public request error (400)
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found several related PRs but none that appear to be true duplicates of PR #40143. Here are the most relevant ones: Related PRs (not duplicates):
No duplicate PRs found |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
What this PR does (plain English)
Makes sure that a session (a chat with the coding agent) always knows which project folder it belongs to — and never silently assumes the folder of whatever process happened to be running the server.
Right now, when a request reaches the server without saying which folder it is for, the server "falls back" to its own current working directory (
process.cwd()). That is a problem when the same server is shared across many projects (worktrees): two sessions can end up bound to the wrong folder, or a folder that doesn't belong to anyone, because they inherited the server's folder instead of the session's folder.This PR changes that rule: a request that does not declare its folder is rejected with a clear 400 error, instead of being silently attached to a guess. The folder must now be provided explicitly, either in the request URL or via the
x-opencode-directoryheader.Why this matters
How (conceptual, no code needed)
The server had three places that used the "fallback to the current folder" rule. All three now follow the new rule: no folder declared → 400 error.
Sessions that already have a folder (for example, replying to an existing conversation, forking, or renaming) are unaffected — they keep working because the folder is recovered from the session itself.
A regression test was added that proves a folder-less "new session" request is rejected with the new 400 error.
What changed (this phase)
How it was tested
bun run typecheck— all packages passworkspace-proxy,httpapi-provider,httpapi-mcp,httpapi-session) — 37 passing, 1 pre-existing skip, 0 failuresPhases / roadmap
This is Phase 1 of a larger effort to give each worktree a fully isolated server. Later phases (each kept reviewable on its own) will: