Skip to content

fix(session): inject directory into session API calls for worktree support#1382

Open
gburch wants to merge 1 commit intocode-yeongyu:devfrom
gburch:fix/worktree-directory-injection
Open

fix(session): inject directory into session API calls for worktree support#1382
gburch wants to merge 1 commit intocode-yeongyu:devfrom
gburch:fix/worktree-directory-injection

Conversation

@gburch
Copy link

@gburch gburch commented Feb 2, 2026

Problem

When using OpenCode with git worktrees (or any workspace where process.cwd() differs from the project directory), session API calls fail with NotFoundError:

NotFoundError: Resource not found: .../storage/session/global/ses_xxx.json

The session file exists under the correct project directory, but the server looks in global/ because it doesn't receive the directory context.

Root Cause

OpenCode's session APIs accept an optional query: { directory } parameter to resolve which project's session storage to use. Without it, the server falls back to process.cwd(), which resolves to project ID global instead of the actual worktree's project ID.

The plugin has access to the correct directory via ctx.directory, but this wasn't being passed to session API calls.

Solution

Wrap ctx.client.session with a Proxy at plugin initialization that automatically injects query: { directory: ctx.directory } into every session method call. This ensures all session operations (.get(), .messages(), .prompt(), etc.) are correctly scoped to the workspace's project.

Changes

  • Added withDirectoryArgs() helper to merge directory into query parameters
  • Added wrapSession() to create a Proxy that intercepts session method calls
  • Added injectDirectoryClient() to apply the wrapper at startup
  • Called injectDirectoryClient(ctx.client, ctx.directory) during plugin initialization

Testing

Verified that delegate_task and other session-dependent tools now work correctly when running OpenCode from a git worktree.


Summary by cubic

Fixes session API failures in git worktrees by injecting the workspace directory into all session calls. Ensures session reads/writes target the correct project and removes NotFoundError.

  • Bug Fixes
    • Wraps ctx.client.session with a Proxy to auto-add query.directory on every call.
    • Adds withDirectoryArgs, wrapSession, and injectDirectoryClient; applied at plugin init.
    • Restores session.get/messages/prompt in worktrees and alternate workspaces.

Written for commit 05d00c8. Summary will update on new commits.

…pport

When using OpenCode with git worktrees, session API calls fail with
NotFoundError because the server can't resolve the correct project
directory. The server falls back to process.cwd() which resolves to
'global' instead of the actual worktree's project ID.

This fix wraps ctx.client.session with a Proxy that automatically
injects query.directory into every session method call, ensuring
session operations are correctly scoped to the worktree's project.

Fixes session.get, session.messages, session.prompt, and other
session APIs when running from git worktrees or alternate workspaces.
@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

@gburch
Copy link
Author

gburch commented Feb 2, 2026

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 2, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

nludd25 pushed a commit to nludd25/oh-my-opencode that referenced this pull request Feb 2, 2026
A1pha3 pushed a commit to A1pha3/oh-my-opencode that referenced this pull request Feb 2, 2026
leoisadev1 pushed a commit to leoisadev1/oh-my-opencode that referenced this pull request Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant