[PR #12008] fix: filter CLI sessions by current directory for worktree support #14026

Open
opened 2026-02-16 18:18:50 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12008

State: open
Merged: No


Summary

Fixes an issue where opencode -c (continue) and opencode session list showed sessions from all worktrees of the same repository instead of only sessions for the current directory.

Problem

When running opencode from a git worktree directory (e.g., ~/workspace/project.worktree1), the CLI commands would incorrectly show/continue sessions from the main repository or other worktrees. This happened because:

  1. Sessions are stored with a directory field that correctly identifies which worktree they belong to
  2. Sessions are grouped by projectID (derived from git root commit hash) - this is the same for all worktrees
  3. The CLI commands were not filtering sessions by the current directory

Changes

  • run.ts: Pass process.cwd() to sdk.session.list() when using the -c (continue) flag
  • session.ts: Filter session list to only show sessions where session.directory === Instance.directory

Testing

This change aligns CLI behavior with the TUI, which already filters sessions by directory in packages/app/src/pages/layout.tsx.

Before: Running opencode session list from project.worktree1 shows sessions from project, project.worktree1, project.worktree2, etc.
After: Running opencode session list from project.worktree1 shows only sessions created in project.worktree1.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12008 **State:** open **Merged:** No --- ## Summary Fixes an issue where `opencode -c` (continue) and `opencode session list` showed sessions from all worktrees of the same repository instead of only sessions for the current directory. ## Problem When running opencode from a git worktree directory (e.g., `~/workspace/project.worktree1`), the CLI commands would incorrectly show/continue sessions from the main repository or other worktrees. This happened because: 1. Sessions are stored with a `directory` field that correctly identifies which worktree they belong to 2. Sessions are grouped by `projectID` (derived from git root commit hash) - this is the same for all worktrees 3. **The CLI commands were not filtering sessions by the current directory** ## Changes - **`run.ts`**: Pass `process.cwd()` to `sdk.session.list()` when using the `-c` (continue) flag - **`session.ts`**: Filter session list to only show sessions where `session.directory === Instance.directory` ## Testing This change aligns CLI behavior with the TUI, which already filters sessions by directory in `packages/app/src/pages/layout.tsx`. Before: Running `opencode session list` from `project.worktree1` shows sessions from `project`, `project.worktree1`, `project.worktree2`, etc. After: Running `opencode session list` from `project.worktree1` shows only sessions created in `project.worktree1`.
yindo added the pull-request label 2026-02-16 18:18:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14026