anomalyco / anomalyco/opencode

import: only the parent session is re-scoped, orphaning subagent sessions (cascade data loss)

Open
#40,352 1 comment 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Aug 4, 2026.

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

Description

Description

opencode import rewrites a session's projectID/directory to the current working directory, but only for the parent row. Child sessions (session.parent_id) keep their old project_id/directory, and they aren't included in the export payload to begin with.

So after relocating a session that used subagents, the children are stranded in the old project. Because session.project_id is ON DELETE CASCADE, deleting that old project silently destroys the orphaned subagent transcripts.

Measured on a copy of my DB using a session with 38 subagents:

  • parent moved to the new project
  • all 38 children stayed on the old project (796 messages)
  • deleting the old project left 0 of 38 children

Relevant code:

  • packages/opencode/src/cli/cmd/export.ts — builds {info, messages} from svc.messages({ sessionID }); no parent_id traversal.
  • packages/opencode/src/cli/cmd/import.ts — overrides projectID/directory/path from ctx, then upserts a single row via onConflictDoUpdate on SessionTable.id.

Export/import is currently the only way to move a session between projects, so this is the path users hit when they start a session in one directory and later want it under a dedicated folder.

Possible fix: a --with-children flag on export, and have import re-scope the whole session tree in one transaction.

Possibly related: #40258 (archiving a parent leaves children active) looks like the same "parent/child not handled atomically" class of bug. Also #36605, and #35427 / #35492 on stale session.directory.

Plugins

None

OpenCode version

1.18.11

Steps to reproduce

Sandboxed so this doesn't touch your real DB (XDG_DATA_HOME is respected):

  1. Pick a session that spawned subagents:
    sqlite3 ~/.local/share/opencode/opencode.db \
      "SELECT parent_id, count(*) FROM session WHERE parent_id IS NOT NULL GROUP BY parent_id ORDER BY 2 DESC LIMIT 1;"
    
  2. Copy the DB to a sandbox:
    mkdir -p /tmp/sb/opencode
    sqlite3 ~/.local/share/opencode/opencode.db ".backup '/tmp/sb/opencode/opencode.db'"
    
  3. Export the parent:
    opencode export <parentID> > /tmp/s.json
    
  4. Import from a different directory, against the sandbox:
    mkdir /tmp/target && cd /tmp/target && git init -q && git commit -q --allow-empty -m init
    XDG_DATA_HOME=/tmp/sb opencode import /tmp/s.json
    
  5. Parent moved, children did not:
    sqlite3 /tmp/sb/opencode/opencode.db \
      "SELECT count(*) FROM session c JOIN session p ON p.id=c.parent_id
       WHERE c.parent_id='<parentID>' AND c.project_id <> p.project_id;"
    
    This returns the full child count instead of 0. Deleting the old project row then removes them via cascade.
Screenshot and/or share link

n/a

Operating System

macOS 26.6 (arm64)

Terminal

VS Code integrated terminal (1.127.0-insider)

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.