Skip to content

Conversation

@dmmulroy
Copy link
Contributor

@dmmulroy dmmulroy commented Jan 11, 2026

Summary

Fixes spurious external_directory permission prompts when running opencode from a subdirectory within a git worktree (e.g., monorepo packages/some-package).

Closes #7758

Root cause: Path containment was checked only against Instance.directory (CWD), not Instance.worktree (git root). Paths within the worktree but outside CWD were incorrectly treated as external.

Related Issues

Changes

  • Add Instance.containsPath() method that checks both directory and worktree
  • Guard against non-git projects where worktree === "/" to prevent allowing all paths
  • Update assertExternalDirectory() helper to use Instance.containsPath()
  • Update direct usages in file/index.ts and bash.ts
  • Add comprehensive test coverage

Test Plan

bun test test/file/path-traversal.test.ts test/tool/external-directory.test.ts

Tests cover:

  • Path inside directory
  • Path inside worktree but outside directory (monorepo scenario)
  • Path outside both directory and worktree
  • Path with .. escaping worktree
  • directory === worktree (repo root)
  • Non-git project doesn't allow arbitrary paths

Note

This PR was written with AI assistance.

AI Session Export

{
  "info": {
    "title": "fix external_directory permission in monorepos",
    "agent": "opencode",
    "models": ["claude-opus-4-5"]
  },
  "summary": [
    "user requested fix for issue #7758 and PR creation",
    "agent analyzed external_directory permission bug in monorepo subdirectories",
    "agent added Instance.containsPath() method checking both directory and worktree",
    "agent updated path checks in read.ts, edit.ts, patch.ts, bash.ts, file/index.ts",
    "agent added guard for non-git projects where worktree === '/'",
    "agent created comprehensive test coverage for path containment scenarios",
    "agent committed changes and opened PR #7811",
    "agent ran multi-agent code review with 3 parallel reviewers",
    "reviews confirmed fix addresses root cause correctly with no critical issues"
  ]
}

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@adamdotdevin adamdotdevin merged commit fa79736 into anomalyco:dev Jan 11, 2026
3 checks passed
zerone0x pushed a commit to zerone0x/opencode that referenced this pull request Jan 12, 2026
zerone0x pushed a commit to zerone0x/opencode that referenced this pull request Jan 12, 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.

external_directory permission triggered for .opencode/state within git worktree

2 participants