Skip to content

fix(opencode): pass --dir to opencode so agents run in the worktree, not main repo#531

Open
jregeimbal wants to merge 1 commit into
the-open-engine:devfrom
jregeimbal:fix/opencode-dir-worktree-isolation
Open

fix(opencode): pass --dir to opencode so agents run in the worktree, not main repo#531
jregeimbal wants to merge 1 commit into
the-open-engine:devfrom
jregeimbal:fix/opencode-dir-worktree-isolation

Conversation

@jregeimbal

Copy link
Copy Markdown

Summary

When opencode is launched inside a git worktree, it follows the .git file pointer back to the main repository root, causing all agent file operations to target the main repo instead of the isolated worktree. Passing --dir <worktree> prevents this gitdir resolution.

Fixes #530.

Motivation

Worktree isolation exists to keep each cluster's changes sandboxed. opencode's internal git detection walks .git (which in a worktree is a file like gitdir: /main/.git/worktrees/...) up to the main repo root, silently defeating the isolation — the agent reads and modifies files in the main repo rather than the worktree.

The fix is to prefer --dir over --cwd when opencode supports it. Unlike --cwd, --dir tells opencode explicitly which directory to treat as root and bypasses the gitdir pointer walk.

Changes

  • src/agent-cli-provider/types.ts: Add supportsDir to OpencodeCliFeatures and CliFeatureOverrides
  • src/agent-cli-provider/adapters/opencode.ts: Detect --dir in detectCliFeatures() via help text scan; in addOpencodeOptionalArgs(), pass --dir <cwd> when supportsDir is true, falling back to --cwd for older opencode versions that lack the flag
  • tests/provider-cli-builder.test.js: 5 new regression tests covering:
    • --dir is passed when supportsDir is true (the fix)
    • --cwd is used as fallback when supportsDir is false
    • commandSpec.cwd is always set regardless of CLI flag support
    • detectCliFeatures correctly detects supportsDir from help text
    • detectCliFeatures correctly rejects supportsDir when --dir is absent

Testing

  • Unit tests added (tests/provider-cli-builder.test.js — 5 new tests, all passing)
  • Full test suite passes (1244 tests)
  • Linting passes (0 errors)
  • Type checking passes

Breaking Changes

None. --dir is only passed when detectCliFeatures() confirms opencode supports it; older versions continue to receive --cwd as before.

Checklist

  • Tests pass (npm test)
  • Linting passes (npm run lint)
  • Type checking passes (npm run typecheck)
  • Documentation updated (JSDoc on affected functions)
  • CLAUDE.md not affected (no architecture change)

@jregeimbal jregeimbal force-pushed the fix/opencode-dir-worktree-isolation branch from d2adbe8 to 218049c Compare June 28, 2026 19:00
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