Skip to content

/init and /review use the git worktree root instead of the session location directory #52833

Open

Description

@ToguDV

What happens

/init and /review substitute their ${path} placeholder with project.directory (the git/hg worktree root, found by walking up to the nearest .git/.hg), not with the session's location.directory.

When a session is created with a location.directory that lives in a subdirectory of a larger git repository, the commands therefore target the repository root instead of the directory the session is working in. The same root is reported as Workspace root folder: in the environment prompt.

Version

opencode v2.0.21 (also reproduced conceptually on the v2 server API).

Reproduction

  1. Create a git repo, e.g. /repo with /repo/sub inside it.
  2. Create a session whose location directory is /repo/sub (v2 server: POST /api/session with body.location.directory = "/repo/sub").
  3. Run /init.

The injected prompt contains:

If AGENTS.md already exists at /repo, improve it in place rather than rewriting blindly.

and the agent edits /repo/AGENTS.md instead of /repo/sub/AGENTS.md.

Evidence from the v2.0.21 binary

The opencode.command plugin registers the built-in command with the project directory:

c.add({ name: "init", description: "guided AGENTS.md setup",
  execute: (l) => e.session.prompt({ ...l.prompt, sessionID: l.sessionID,
    text: p4(initTemplate.replace("${path}", a.project.directory), l.prompt.text), ... }) })

and Project.resolve resolves the root by walking up:

let l = yield* e.up({ targets: [".git", ".hg"], start: f, mode: "first" })
...
return yield* x({ ..., directory: m.worktree, canonical: ue, vcs: { type: "git", ... } })

The environment prompt also distinguishes them:

Working directory:    ${f.directory}          // the session location
Workspace root folder: ${f.project.directory} // the git worktree root

Expected

Project-scoped commands (/init, /review) and AGENTS.md discovery should honor the session's location.directory, or at least not escape it upward into an unrelated ancestor repository. A session explicitly scoped to /repo/sub should not create or modify /repo/AGENTS.md.

This matters for hosts that embed opencode and place each user project in a subdirectory of their own repository (so the subdirectory is not itself a git root): the embedded agent ends up editing the host's own AGENTS.md and receiving the host's project instructions.

Notes

A workaround on the host side is to make every session directory its own git root (so project.directory stops there) and/or inject a session instruction pinning the directory, but that requires the host to know this behavior. It would be great if location.directory were authoritative for project-scoped commands.

Activity

  1. github-actions commented on Oct 2, 2026

    @github-actions
    Contributor

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

  2. r266-tech commented on Oct 2, 2026

    @r266-tech
    Contributor

    The current v2 command code still uses location.project.directory. Its existing packages/core/test/plugin/command.test.ts already sets the location to /repo/packages/app and the project to /repo, then explicitly expects both built-ins to interpolate /repo. I ran that test: 1 pass. Both source/test blobs match current v2 (eb1a9ea4). This captures prompt construction, without invoking a model or editing any AGENTS.md.

    @rekram1-node, should /init target the session directory while /review keeps repository-wide git semantics? The current init template says “this repository”, and the test encodes that scope, so changing both commands together would also change existing behavior. Instruction discovery is a separate path and is not covered by this test. AI-assisted investigation.

  3. opencodejr commented on Oct 27, 2026

    @opencodejr

    Thanks for the detailed write-up, @ToguDV!

    This is currently by design. /init and /review fill in ${path} with the project's VCS root (location.project.directory) on purpose, and AGENTS.md handling is also tied to the project root, so a session opened in a subdirectory still treats the whole repository as the project. That's why the agent goes to /repo/AGENTS.md.

    So I've filed this as a feature request: let the session's location.directory decide where project-scoped commands like /init and /review (and AGENTS.md discovery) apply, or at least stop them from reaching up into an unrelated ancestor repository. The embedding case you describe is a clear reason for it, and maintainers will consider it.

    Until then, your workarounds (making each session directory its own git root, or pinning the directory in a session instruction) are the reliable options. Another option is a custom command with your own AGENTS.md prompt that names the subdirectory explicitly.

    Related issues:

  4. added a commit that references this issue on Nov 1, 2026
    b542bdc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions