opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Search for AGENT.md in project subdirectory

Open segoon opened this issue 1 year ago • 11 comments

opencode can be started from a project's subdirectory. In this case it looks for AGENT*.md in current directory only, not in the project's root.

Suggestion: implement project root identification (e.g. based on special file existence like .git, pyproject.toml, etc.) & additionally load agents files starting from the project root.

segoon avatar Aug 20 '25 05:08 segoon

This issue might be a duplicate of existing issues. Please check:

  • #1877: Support a root folder for aggregating sessions across subdirectories in Opencode - discusses project root identification and handling subdirectories within a project structure

Feel free to ignore if none of these address your specific case.

github-actions[bot] avatar Aug 20 '25 05:08 github-actions[bot]

I think there are 2 different tasks and 1 common subtask:

  • use project's sessions
  • use project's AGENT.md
  • locate project root (common)

segoon avatar Aug 20 '25 05:08 segoon

hmm i thought we already loaded agents.md from project root everytime

rekram1-node avatar Aug 20 '25 12:08 rekram1-node

Image

Maybe I misread your wording but as you can see in this basic project we definitely read AGENTS.md from project root even when you are in subdirectory.

rekram1-node avatar Aug 20 '25 13:08 rekram1-node

Hmm looks like it works for projects with .git. However, how do you discover project root? It is this code - https://github.com/sst/opencode/blob/1f57b9a70f09bea9c9fd5662becdda18c967f9a1/packages/opencode/src/app/app.ts#L69 ? I don't think it is generic enough - there are projects without git and with other VCSs or even without VCS at all. It's better to enable user to define root marker files with some sane defaults (e.g. .git, .hg and similar).

segoon avatar Aug 23 '25 00:08 segoon

makes sense

rekram1-node avatar Aug 23 '25 13:08 rekram1-node

Hmm looks like it works for projects with .git. However, how do you discover project root? It is this code -

opencode/packages/opencode/src/app/app.ts

Line 69 in 1f57b9a const root = git ?? input.cwd ? I don't think it is generic enough - there are projects without git and with other VCSs or even without VCS at all. It's better to enable user to define root marker files with some sane defaults (e.g. .git, .hg and similar).

I started using git worktree and realized that opencode always is looking into the main worktree, but not in my current worktree. This implementation detail made me understand why. running git config --list --show-origin made me realize that opencode thinks it should look in the directory from which the worktree was generated. Not sure what a solution to this would be.

--- After some digging:

I looked: since the referenced code is old, most likely this part is relevant, which is already in develop: https://github.com/sst/opencode/blob/900fe5ca041c5b9e2de9449c441dd9cb10beed0a/packages/opencode/src/project/project.ts#L68C17-L68C70

  worktree = path.dirname(
    await $`git rev-parse --path-format=absolute --git-common-dir`
      .quiet()
      .nothrow()
      .cwd(worktree)
      .text()
      .then((x) => x.trim()),
  )

And this looks like it already solves my issue. While i think git rev-parse --path-format=absolute --git-dir would give the worktree's directory and the above command gives the common directory. I'll have to test with the current develop.

maiterth avatar Sep 05 '25 18:09 maiterth

I'll have to test with the current develop.

Does this mean you have to test on latest? sorry i didn't quite follow u

rekram1-node avatar Sep 05 '25 20:09 rekram1-node

Sorry, for the confusion, I have opened an new issue #2445 And I have tested with latest and found a quick fix. https://github.com/maiterth/opencode/compare/dev...feature-test-worktree-rootdir But this is actually unrelated to OPs issue. -> #2445

maiterth avatar Sep 05 '25 20:09 maiterth

sweet, just saw it thanks @maiterth

rekram1-node avatar Sep 05 '25 20:09 rekram1-node

[automated] Closing due to 90+ days of inactivity. Feel free to reopen if you still need this!

rekram1-node avatar Dec 28 '25 01:12 rekram1-node