anomalyco / anomalyco/opencode
import: only the parent session is re-scoped, orphaning subagent sessions (cascade data loss)
@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}fromsvc.messages({ sessionID }); noparent_idtraversal.packages/opencode/src/cli/cmd/import.ts— overridesprojectID/directory/pathfromctx, then upserts a single row viaonConflictDoUpdateonSessionTable.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):
- 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;" - Copy the DB to a sandbox:
mkdir -p /tmp/sb/opencode sqlite3 ~/.local/share/opencode/opencode.db ".backup '/tmp/sb/opencode/opencode.db'" - Export the parent:
opencode export <parentID> > /tmp/s.json - 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 - Parent moved, children did not:
This returns the full child count instead of 0. Deleting the oldsqlite3 /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;"projectrow 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.