fix(session): scope session list to current directory to prevent cross-worktree leakage#16806
Conversation
…s-worktree leakage When multiple git worktrees share the same root commit, they get the same project_id. This causes sessions from different worktrees to appear mixed in the session list. Similarly, the 'global' project (worktree '/') acts as a catch-all bucket, mixing sessions from unrelated directories. Changes: - Default the session list API route to filter by Instance.directory, ensuring sessions are scoped to the active working directory - Fix workspace_id filtering to include sessions with NULL workspace_id (pre-migration sessions) using OR condition instead of strict equality - Use explicit workspaceID input parameter in Session.list() instead of only reading from WorkspaceContext (the param was dead code before) - Add workspaceID query parameter to GET /session/ API route - Pass directory filter in CLI 'session list' command - Allow explicit workspaceID in Session.createNext() input - Add tests for directory scoping, NULL workspace_id handling, and explicit workspaceID in createNext Fixes anomalyco#15678, relates to anomalyco#16744, anomalyco#16137
|
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. |
|
The following comment was made by an LLM, it may be inaccurate: Found potentially related PRs:
These PRs address similar concerns around session scoping, worktree support, and preventing cross-directory session leakage, though they appear to be from different iterations or complementary approaches to the problem. |
|
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. |
Summary
When multiple git worktrees share the same root commit, they get the same
project_id. This causes sessions from different worktrees to appear mixed in the session list. Similarly, theglobalproject (worktree/) acts as a catch-all bucket, mixing sessions from completely unrelated directories.Before: Opening OpenCode in
marketplace-msshows 174 sessions frommarketplace-ms,news-cms, andecommerce-wiz-lastbecause they share the same git root commit.After: Opening OpenCode in
marketplace-msshows only the 4 sessions that were created in that directory.Root Cause
Three interrelated bugs:
globalproject catch-all: Directories without a git repo getproject_id = 'global'withworktree = '/'. Since/is the filesystem root, all "orphan" sessions from unrelated directories appear together.Shared
project_idacross worktrees: Git worktrees from the same repo share the same root commit hash, producing the sameproject_id. Without directory filtering, sessions from all worktrees are mixed.workspace_idNULL exclusion: The workspace filter uses strict equality (eq(workspace_id, wsID)), which excludes all pre-migration sessions that haveworkspace_id = NULL.Changes
packages/opencode/src/session/index.tslist(): Fix workspace_id filtering to include sessions withNULLworkspace_id (pre-migration sessions) usingOR(eq, isNull)instead of strict equality. Use explicitworkspaceIDinput parameter instead of only reading fromWorkspaceContext(the param was dead code before).createNext(): Accept explicitworkspaceIDin input, falling back toWorkspaceContext.workspaceID.packages/opencode/src/server/routes/session.tsGET /session/route to filter byInstance.directorywhen nodirectoryquery param is provided. This ensures sessions are scoped to the active working directory server-side.workspaceIDas an accepted query parameter.packages/opencode/src/cli/cmd/session.tsdirectory: Instance.directorytoSession.list()in the CLIsession listcommand.packages/opencode/test/server/session-list.test.tsworkspace_idare included when filtering byworkspaceIDcreateNextaccepts explicitworkspaceIDTest Results
Related Issues
opencode sessionscommand fails to list historical sessions when run inside a Git repository #15678 —opencode sessions command fails to list historical sessions when run inside a Git repositorySessions under workspace root are missing after upgrading to 1.2.21Zed ACP registry mode shows stale Recent sessions (unscoped /session)/sessions TUI only shows recent sessions, ignores historical onesRelated PRs
workspaceIDinto session create endpoint #16798 —feat(core): allow passing workspaceID into session create endpoint(complementary)fix(session): scope session list and TUI to current directory(similar approach)fix: list all sessions regardless of git context(different approach)