OpenCode with multiple repositories: references vs worktrees

A dithered lighthouse with an amber lamp room, standing over a band of blue sea.

One session, two repos

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.

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

References in 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:

  • A path resolves from the directory that holds the config file. Absolute and ~/ paths work too.
  • A 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.
  • Without 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.
  • A reference with a 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.

References are not read-only

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:

  • A local path is your real checkout. The edit goes onto whatever branch ../web has checked out, which is often main.
  • A Git repository is OpenCode's shared cache. The docs advise against editing it, because the next refresh resets the checkout and the edit is gone.

To make a reference read-only in OpenCode itself, add permission rules for its path. This is the pattern the permissions docs 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.

The same task with a worktree per repo

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. After that, you open OpenCode in the hall and ask for the change. The agent runs ivar itself:

opencode — ~/acme

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

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

Bash(ivar feature create avatar-url)

Bash(ivar feature promote avatar-url api)

Bash(ivar feature promote avatar-url web)

Bash(ivar session connect --feature avatar-url --create --json)

Agent: 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:

  • An AGENTS.md in the view dir, derived from the hall's HALL.md, with a pointer to each repo's own instruction file.
  • Each repo's own AGENTS.md, delivered as the agent works. Before every tool call, 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.
  • A write guard on 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:

opencode — ~/acme

You: Ship it.

Skill(ivar-deliver)

Bash(ivar feature deliver avatar-url --preview)

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

You: Yes.

Bash(ivar feature deliver avatar-url --fingerprint <fp>)

The preview prints a fingerprint, and the apply runs only with the fingerprint of the preview you approved. It is refused if anything changed since the preview, for example when a branch's base has moved.

References vs worktrees

OpenCode referencesivar worktrees
Write scopeWhatever the session's permissions allow; edits are allowed by defaultPromoted repos only; the plugin denies structured writes elsewhere, and the kernel enforces it on Linux only when ivar launches OpenCode
Other repo's AGENTS.mdNot loaded; only alias, path and descriptionDelivered on the first tool call that touches the repo
BranchA local path stays on its current checkout; a Git reference tracks one branch and is reset on refreshOne feature branch in every promoted repo
DeliveryYours, repo by repoThe agent runs ivar feature deliver, previewed, one push per promoted repo
SetupA few lines in opencode.jsonA 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 is a self-hosted web app for OpenCode that manages repos and creates worktrees for them. A draft cross-repo tool for OpenCode clones another GitHub repository for the session and can open pull requests there.

Limits

  • Use references for reading. If you edit through one, you are on someone else's branch or in a cache that resets.
  • OpenCode has no session-end event. ivar records no session end for OpenCode, so the memory page of a conversation appears after 30 idle minutes or when the session stops.
  • OpenCode plugins cannot add context at session start, so OpenCode gets no ivar memory brief. The ivar MCP server's instructions ask the agent to list open handoffs and messages itself.
  • OpenCode's glob does not follow the view dir's repo symlinks: web/** finds nothing. Reads and shell commands work, and still deliver the instructions.
  • OpenCode sessions cannot be resumed. To move a feature to another harness, the agent can relay it with ivar session relay, which prepares a fresh session on the same branches. The conversation does not carry over.
  • Without Landlock (on macOS, or with an attached OpenCode on Linux), the plugin still blocks structured writes, but only the root of an unpromoted checkout loses its write bit, so a shell command can still write deeper inside it.
  • ivar runs on macOS and Linux. Windows is not supported.