# OpenCode with multiple repositories: references vs worktrees

> Source: <https://dev.to/mnzs/opencode-with-multiple-repositories-references-vs-worktrees-4ne>
> Published: 2026-12-02 00:10:02+00:00

**Recommendation:** When giving a coding agent read-only access to another repo via OpenCode references, add edit-deny permission rules for that path and supply its conventions manually, since AGENTS.md is not loaded.
More in the playbook "Working with AI coding agents": <https://wpnews.pro/playbooks/coding-agents>

You run OpenCode in `api`, and the task needs code from `web`: the client that calls the endpoint you are changing. Sometimes the agent only has to read `web` to get the contract right. Sometimes it has to change `web` too, on the same branch name, and both changes ship together.

OpenCode now has an answer for the first case: **references**, declared in `opencode.json`. For the second case you need multi-repo worktrees for coding agents: one worktree per repo on a shared feature branch, with a write scope you choose and one delivery step. This post sets up both and compares them.

`opencode.json`
A reference is a named directory outside the project. It can be a local path or a Git repository:

`api/opencode.jsonc`

```
{
  "$schema": "https://opencode.ai/config.json",
  "references": {
    "web": {
      "path": "../web",
      "description": "Web client that calls this API. Use for request and response shapes.",
    },
    "sdk": {
      "repository": "acme/sdk",
      "description": "Public SDK. Use for the client method names.",
    },
  },
}
```

How each kind behaves, according to the OpenCode [references docs](https://opencode.ai/docs/references/):

`path` resolves from the directory that holds the config file. Absolute and `~/` paths work too.`repository` takes a GitHub `owner/repo` shorthand, a Git URL, a host/path form or an SCP-style remote. OpenCode clones it into its own cache, `~/.local/share/opencode/repos/github.com/acme/sdk` on a typical Linux install.`branch`, OpenCode checks out the remote's default branch and refreshes it. A refresh runs at most once every 24 hours and fetches, then resets the checkout to that branch.`description` is listed in the agent's instructions with its alias and resolved path. One without a description is still there to attach, but the agent is not told about it.
In the TUI, `@web` attaches the reference root and `@web/` searches files inside it:

```
Compare the request body in @web/src/api/profile.ts with the handler in src/routes/profile.ts.
```

The attachment is a listing of the root's immediate files and directories. For anything deeper, ask the agent to read a specific path.

For reading, this works well. The agent opens files in `web` and `sdk` without you copying them in, and the description tells it when each one matters.

Nothing in a reference stops a write. The v1 docs say OpenCode lets reference directories through its external-directory boundary and that normal tool permissions still apply. The current docs say references grant no extra permission, and the default permission policy allows `external_directory` and every edit. With the default build agent, the same session that reads `../web/src/api/profile.ts` can edit it without asking.

Where the edit lands depends on the kind of reference:

`../web` has checked out, which is often `main`.
To make a reference read-only in OpenCode itself, add permission rules for its path. This is the pattern the [permissions docs](https://opencode.ai/docs/permissions/) show:

```
{
  "$schema": "https://opencode.ai/config.json",
  "permissions": [
    { "action": "external_directory", "resource": "~/work/web/*", "effect": "allow" },
    { "action": "read", "resource": "~/work/web/*", "effect": "allow" },
    { "action": "edit", "resource": "~/work/web/*", "effect": "deny" },
  ],
}
```

That rule is enforced by OpenCode's tools. A shell command run by the agent is checked against `shell` and `external_directory` rules, not `edit`.

The other gap is instructions. What OpenCode adds to the agent's context for a reference is its alias, its path and its description. The `AGENTS.md` at the root of `web` is not loaded with it, so the agent reads `web` without that repo's conventions unless it happens to open the file.

When the task changes both repos, `ivar` takes a different route. A hall owns the repos. A feature names a branch. Each repo the feature promotes gets a worktree on that branch, and every other repo in the hall is mounted read-only on its default branch.

The only command you type is the install:

```
curl -fsSL 'https://ivar.run/install?ref=opencode-multiple-repositories' | sh
```

Setting up the hall with `api`, `web` and `sdk` is a one-time step, covered in the [quickstart](https://ivar.run/docs/quickstart?utm_source=devto&utm_medium=syndication&utm_campaign=opencode-multiple-repositories). After that, you open OpenCode in the hall and ask for the change. The agent runs `ivar` itself:

  Add an avatar URL to the profile: the endpoint in api and the client in web, on one branch. sdk stays as it is.

  I'll create a feature, promote api and web onto its branch, and attach to a session for it.

  api and web are writable on `avatar-url`. sdk stays read-only on its default branch.

The session lives in a view dir: a directory of symlinks, one per repo. `api` and `web` point at worktrees on the `avatar-url` branch and are writable. `sdk` points at the default-branch checkout and is not. The agent keeps the same conversation and works through the session's path.

What the session gives OpenCode:

`AGENTS.md` in the view dir, derived from the hall's `HALL.md`, with a pointer to each repo's own instruction file.` ivar guard` runs from `.opencode/plugins/ivar.js`. When it allows a call that touches `web/`, it appends every instruction file from the root of `web` down to the touched directory that the agent has not seen yet.`sdk`. The same plugin denies a structured write outside the promoted worktrees and names the writable set in the denial. It does not check what a shell command writes.
The plugin is the only guard in this setup, because the agent attached to the session from an OpenCode that was already running. The Linux kernel write sandbox (Landlock, kernel 5.13 or later) applies only to a harness that `ivar session start` launches. Under it, a shell redirect into `sdk` also fails with `EACCES`.

When the work is done, delivery covers every promoted repo at once:

  Ship it.

  The preview pushes nothing. It lists one push for api and one for web. Apply it?

  Yes.

  
  
  References vs worktrees

|  | OpenCode references | ivar worktrees | 
|---|---|---|
| Write scope | Whatever the session's permissions allow; edits are allowed by default | Promoted repos only; the plugin denies structured writes elsewhere, and the kernel enforces it on Linux only when `ivar` launches OpenCode | 
| Other repo's `AGENTS.md` | Not loaded; only alias, path and description | Delivered on the first tool call that touches the repo | 
| Branch | A local path stays on its current checkout; a Git reference tracks one branch and is reset on refresh | One feature branch in every promoted repo | 
| Delivery | Yours, repo by repo | The agent runs `ivar feature deliver` , previewed, one push per promoted repo | 
| Setup | A few lines in `opencode.json` | A hall once, then one request to the agent per feature | 

References fit when the second repo is context, such as an SDK you call or a design system you consult. They take a minute to add and need no other tool.

Worktrees fit when the second repo changes. Every promoted repo is on the same branch, and the repos the feature did not promote stay guarded.

Other tools fill parts of the same gap. [OpenCode Manager](https://github.com/chriswritescode-dev/opencode-manager) is a self-hosted web app for OpenCode that manages repos and creates worktrees for them. A [draft cross-repo tool](https://gist.github.com/elithrar/5e3a4c741c0588d007178b17f5c5079c) for OpenCode clones another GitHub repository for the session and can open pull requests there.

`ivar` records no session end for OpenCode, so the memory page of a conversation appears after 30 idle minutes or when the session stops.`ivar memory brief`. The `glob` does not follow the view dir's repo symlinks: `web/**` finds nothing. Reads and shell commands work, and still deliver the instructions.`ivar session relay`, which prepares a fresh session on the same branches. The conversation does not carry over.`.opencode/`.
