opencode icon indicating copy to clipboard operation
opencode copied to clipboard

skills: deleting a git worktree wipes project skills from the shared registry

Open AnthonyLzq opened this issue 1 week ago • 4 comments

Summary

Deleting a git worktree directory that contains project skills (SKILL.md files) makes the shared background service rescan skill sources from the deleted directory. That rescan rebuilds the shared skills registry from the deleted directory's (now empty) sources, so every other project's registered skills disappear service-wide — sessions started afterwards no longer advertise them, until something triggers another rescan for those projects.

Environment

  • opencode version: 2.2.5 (latest channel)
  • OS: Darwin 25.5.0 (macOS, arm64)
  • Terminal: TERM_PROGRAM=Orca (desktop app), TERM=xterm-256color
  • Shell: /bin/zsh
  • Install/channel: latest
  • Active plugins: [email protected]

Reproduction

Observed in the wild, then reproduced deliberately (1/1):

  1. A repo with skills in .agents/skills/ (tracked compatibility source; also listed under skills in opencode.json), checked out as sibling git worktrees A and B under one shared background service.
  2. Both worktrees were known projects with sessions. The log showed skills rescanned events listing all sources and ~19 skills (global + project).
  3. Worktree B was deleted from disk (it no longer appears in git worktree list).
  4. A file-deletion event for B/.agents/skills/plan-ec/SKILL.md triggered a rescan.
  5. The rescan still listed B/.agents/skills as a source, but since the tree was gone the resulting registry contained only the global skills. Project skills of worktree A — untouched and still on disk — were gone: sessions started afterwards did not see them (not advertised to the model, missing from autocomplete).

Controlled repro on the same service:

  1. opencode worktree_create → detached worktree of the same repo (contains the same .agents/skills/).
  2. Opened a session in it with opencode run so the directory becomes a watched project.
  3. Touched a canary skill file → rescan registered 20 skills (9 global + 10 project + canary), with the worktree's .agents/skills among the sources.
  4. Removed the worktree through the API (worktree remove).
  5. 55 s later, a rescan triggered by a deletion event inside the removed worktree rebuilt the registry with only the 9 global skills — the 10 still-existing project skills (of other untouched worktrees) vanished from the shared registry.

Log evidence (paths shortened, one line per event):

# healthy state (before deletion)
message="skills rescanned" file=~/.local/share/opencode/worktree/<id>/repro/.agents/skills/repro-canary/SKILL.md
  sources="[..., directory:~/.local/share/opencode/worktree/<id>/repro/.agents/skills]"
  skills="[9 global..., audit-ticket-ec, ..., repro-canary]"        # 20 skills

# 55s after deleting that worktree
message="skills rescanned" file=~/.local/share/opencode/worktree/<id>/repro/.agents/skills/start-task-ec/SKILL.md
  skills="[9 global skills only]"                                    # project skills of other worktrees gone

Expected Behavior

A rescan triggered by a deleted (or otherwise unreadable) directory should not replace the registry entries of other projects: either scope the rebuild to the triggering project, skip the rescan when its sources no longer exist, or merge/union into the shared registry instead of replacing it.

Actual Behavior

The last rescan wins globally: a scan for a deleted directory leaves the whole service with global-only skills, so other projects' skills disappear from the model's advertised list and from autocomplete until a new rescan for those projects runs (e.g. after touching a watched file or reloading config).

Additional Context

  • Workaround that worked every time (4/4): touch the project's opencode.json (mtime-only change) — the config hot-reload re-registers that project's skills and commands in live sessions without a restart.
  • The skills rescanned server log lines make the wipe observable: healthy scans list all sources + skills; the post-deletion scan lists global sources only.
  • Frequency: reproduced 1/1 in the controlled test; observed once in the wild the same day.

AnthonyLzq avatar Nov 24 '26 17:11 AnthonyLzq

Thanks for the detailed report and the log lines. I tried this on 2.2.5 (Linux) with one shared server and could not reproduce it:

  1. Repo A has two skills in .agents/skills/ (also listed under skills in opencode.json), plus one global skill.
  2. I added a second worktree in two ways: as a sibling with git worktree add --detach, and through the server's worktree API, which puts it under ~/.local/share/opencode/worktree/<id>/…. I loaded both locations and touched or added a skill file in the new worktree.
  3. I deleted the worktree (rm -rf plus git worktree prune, and separately DELETE /api/worktree).

In both runs I saw the rescan you describe: a skills rescanned line for the deleted worktree listing only the global skill. But that rescan only changed the deleted worktree's own skill list. Worktree A, read with GET /api/skill for its directory, still had both project skills for 20 s afterwards. This also matches the code I read: the skill registry is kept separately for each directory.

These would help us get closer to your setup:

  • Does it still happen with the opencode-kiro plugin disabled?
  • Which directory were the later sessions without the project skills started in: the untouched worktree, or the one that was deleted (for example a client or tab still pointing at it)?
  • Please share the skills rescanned log lines for the untouched worktree's own directory after the deletion, if there are any. The lines in the report are all for the deleted worktree's files.
  • If you can, the exact commands for a minimal repro: how the server was started, and how sessions were opened in each worktree.

opencodejr[bot] avatar Nov 24 '26 17:11 opencodejr[bot]

Taking this. The report's mechanism is clear enough to test: a rescan triggered by a source that no longer exists rebuilds the shared registry from the surviving (global) sources and drops every other project's skills. I'll start from the skills rescanned log line and the watcher that fires it, and land a test that deletes one project's skill directory while a second project's skills are registered.

Guembri01 avatar Nov 24 '26 20:11 Guembri01

Withdrawing my offer above: reading the registry, I can't substantiate this one, and I'd rather say so than open a PR that "fixes" something that isn't there.

What the code says:

  • packages/core/src/skill.ts:180 — export const node = makeLocationNode(...), so the skill registry is per Location (directory), not one shared table. list()/get() read that Location's own state (:163-176).
  • packages/core/src/config/plugin/skill.ts:149-167 — refresh() iterates sources() for this plugin instance and rebuilds loaded.skills from them. sources() (:80-106) derives from loaded.entries, the config entries of the same Location.
  • The rebuild is applied through ctx.skill.transform(...) + ctx.skill.reload() (:178-180, :186), both Location-scoped.

So a rescan in one directory cannot remove another directory's skills; it can only empty the directory whose source is gone. That is consistent with the maintainer's note in this thread ("I could not reproduce it … the skill registry is kept separately for each directory").

Two things would still explain what you saw, and one answer settles it:

  1. Were the two worktrees separate Locations or one? ~/.local/share/opencode/worktree/<id>/… directories are separate Locations; a session and a TUI client each read the Location of the directory they are pointed at. A client still pointed at the removed worktree's directory would list global-only skills, which reads as "service-wide" from that client.
  2. If they were separate, the log line that matters is the one for the untouched worktree's own directory after the deletion — the ones quoted in the report are all for the deleted directory's files.

If you can confirm a client aimed at the surviving worktree's directory lost its project skills, that is a real bug and I'll dig in again — it would mean something reuses a Location across directories, which is where I would look next (location-service-map.ts, and whether a worktree resolves to the same Location as its main checkout).

Guembri01 avatar Nov 24 '26 20:11 Guembri01

We're closing this for now because we couldn't reproduce it and the details we asked for haven't come in yet. On 2.2.5 we deleted a worktree in two ways: as a sibling with git worktree add then rm -rf, and through the worktree API. Both times only the deleted worktree's skill list fell back to the global skills. The untouched worktree kept its project skills, and that matches the code: the skill registry is kept separately for each directory. Another user who looked at the code came to the same conclusion.

To look at this again, we'd need:

  • whether the later sessions without the project skills were started in the untouched worktree's directory or in the deleted one (for example, a client still pointed at it)
  • any skills rescanned log lines for the untouched worktree's own directory after the deletion
  • whether it still happens with opencode-kiro disabled

If you reply here with those, the issue will reopen.

opencodejr[bot] avatar Nov 24 '26 23:11 opencodejr[bot]