anomalyco / anomalyco/opencode

Sessions disappear from the /sessions list after a local .git repo in the session's working directory is removed

Open
#47,652 2 comments 0 reactions 1 assignee View on GitHub

@nexxeln is already working on this.

Since Sep 6, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Description

After removing a project's .git directory, all existing sessions for that directory stop appearing in /sessions, even though the rows remain intact in opencode.db. Sessions created after the removal do appear (tagged with the global project), so history fragments across two project ids.

No data is deleted — only the list query stops matching.

Root cause (as of 1.18.29):

  1. Project identity is git-derived. When git.repo.discover finds no repo, ProjectV2.resolve collapses the directory to the global project with no previous (packages/core/src/project.ts:110-122).
  2. Session migration only happens via migrateProjectId(data.previous, projectID) in fromDirectory (packages/opencode/src/project/project.ts:213-310). The previous identity cache is stored inside .git/opencode — deleting .git destroys it, so no git-project-id → global migration ever fires and old sessions keep their stale project_id.
  3. The list query then hides them twice over: listByProject (packages/opencode/src/session/session.ts:955-1008) always filters project_id = <current project>, and — with the collapsed worktree of / — the TUI additionally filters by path = <absolute directory> while old sessions store an empty path. Either filter alone hides the sessions.

Expected:
old sessions remain visible — it is the same project directory, only the git metadata was removed.

Workaround (verified, script in comments):
Re-tag the orphaned sessions' project_id and path to match what their directory currently resolves to. A standalone repair script doing exactly this will be attached in comments (zero dependencies, runs with plain bun; dry-run by default, --apply --backup to write with a VACUUM INTO copy that backs up the current DB).

Related:

  • #30683 (auto-closed after 60 days of inactivity; its closing bot comment invites a new issue if still relevant) — same bug family (previous cache lost → migrateProjectId no-op → stranded sessions) but a much rarer trigger: root-commit SHA drift after history rewrite plus cache loss (AV quarantine, WSL) while the repo still exists. Here the repo itself is gone: one rm -rf .git is the entire trigger, and the identity collapses to global.
  • PR #30682 (fix for #30683, closed unmerged) — proposed a project.worktree scan to recover orphaned projects. That scan would also recover this case (the old project row's worktree still matches the directory), but its author explicitly scoped out the path = "" filter edge case in listByProject — which is the second filter hiding these sessions after re-homing (global project → worktree=/ → TUI filters by absolute path; old sessions store empty path). A complete fix needs both legs: id recovery and path convergence (or a TUI guard).
  • #45133 (in-flight, fixes #23248) — repo rename/move: same identity resolving at a new path. Does not cover this case: path unchanged, identity changes (git id → global), previous cache deleted with .git.
  • #5155 — the opposite direction (git init orphans global sessions; fixed by migrateFromGlobal at packages/opencode/src/project/project.ts:291-298, which only matches the exact repo root).

Proposed fix:

  1. ProjectDirectories already records every directory associated with a project (project_directory table). Add a reverse lookup in ProjectV2.resolve: when no repo is found, consult project_directory for the directory (deepest match) and resolve to that project's id instead of global — the association row survives the .git removal.
  2. TUI list-query guard: when the resolved worktree is the filesystem root, use project scope instead of a path filter.
Plugins

None

OpenCode version

1.18.29

Steps to reproduce
  1. Have an existing session in a directory that is a git repository.
  2. rm -rf <YOUR_PROJECT_HERE>/.git
  3. Restart the server and re-attach or restart the OpenCode CLI client.
  4. Run /sessions.
Screenshot and/or share link

N/A

Operating System

MacOS 26.6.2, Ubuntu 24.04.4

Terminal

Terminal.app(MacOS), xterm-256color(Ubuntu)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.