The analyzer has provided a comprehensive report. Let me summarize the key findings for the user in a clear and concise way.
Summary: OpenCode Session Management & Git Sync
Based on the analyzer's comprehensive investigation, here's how the system works:
1. Git Sync for Rollback - Separate Snapshot Repository
OpenCode uses a clever dual-repository approach:
- User's Git: The normal
.git directory for version control
- Snapshot Git: A hidden repository at
~/.opencode/data/snapshot/<project-id>/ for tracking file states
The snapshot system uses git tree objects (not commits) to track state:
git write-tree - Creates a tree hash representing current file state
git checkout <hash> -- <file> - Restores individual files
git read-tree + git checkout-index - Full restoration
Session messages contain step-start and step-finish parts with snapshot hashes, plus patch parts listing modified files. Rollback collects patches after the revert point and restores files using git checkout.
2. Multiple Sessions - No Worktree Isolation
Sessions do NOT use worktrees for isolation. Instead, they share the same working directory with:
- Per-session read time tracking (
src/file/time.ts) - Tracks when each session last read each file
- Per-file write locks - Serializes concurrent writes via chained promises
- Session busy state - Prevents concurrent operations on the same session
This is optimistic concurrency control - they rely on file modification time checks and write serialization rather than git-level locking.
3. Worktrees - For Parallel Development, Not Sessions
The src/worktree/ module creates separate git worktrees for isolated development environments (like testing branches), stored at ~/.opencode/data/worktree/<project-id>/<name>/. These are not used for session rollback - they're for creating parallel development sandboxes.
4. Git Implementation - Native Commands via Bun Shell
They use native git commands through Bun's shell syntax ($), not a JavaScript library:
// Example: Creating a snapshot
const hash = await $`git --git-dir ${git} --work-tree ${Instance.worktree} write-tree`
// Example: Restoring a file
await $`git --git-dir ${git} --work-tree ${Instance.worktree} checkout ${hash} -- ${file}`
Key Architecture Decisions
| Aspect |
Approach |
| Rollback tracking |
Separate git repo with tree objects |
| Session isolation |
File-level locks + read time tracking |
| Worktrees |
For dev sandboxes, not sessions |
| Git operations |
Native commands via Bun shell |
| Event coordination |
Bus system for cross-component sync |
The design is elegant: using git's tree objects avoids needing branches/commits in the snapshot repo, and native git commands are simpler and more reliable than JavaScript git libraries.