Skip to content

Instantly share code, notes, and snippets.

@zeke
Last active June 29, 2026 02:10
Show Gist options
  • Select an option

  • Save zeke/d494a7e9c62c33a6f235b6591864cfb4 to your computer and use it in GitHub Desktop.

Select an option

Save zeke/d494a7e9c62c33a6f235b6591864cfb4 to your computer and use it in GitHub Desktop.
oc: resume the last OpenCode session for the current project

oc

A shell function that wraps opencode and automatically resumes the most recent session for the current project, scoped by git repo root.

The problem

opencode --continue resumes the last session globally, regardless of which project you're in. If you work across many projects, you end up in the wrong session constantly.

The solution

oc queries OpenCode's SQLite database to find the most recent non-archived session for the current project, then resumes it with --session.

Installation

Add this to your .zshrc or other shell config:

function oc() {
  local worktree
  worktree=$(git rev-parse --show-toplevel 2>/dev/null || echo "$PWD")
  local session_id
  session_id=$(sqlite3 ~/.local/share/opencode/opencode.db \
    "SELECT s.id FROM session s JOIN project p ON s.project_id = p.id WHERE p.worktree = '$worktree' AND s.time_archived IS NULL ORDER BY s.time_updated DESC LIMIT 1;" 2>/dev/null)
  if [[ -n "$session_id" ]]; then
    opencode --session "$session_id" "$@"
  else
    opencode "$@"
  fi
}

Then reload your shell.

Usage

# Resume the last session for the current project
oc

# Resume and send an initial prompt
oc "fix the failing tests"

# In a directory with no prior OpenCode session, falls back to plain opencode
oc

How it works

  1. Resolves the git repo root (git rev-parse --show-toplevel), falling back to $PWD if not in a git repo.
  2. Queries ~/.local/share/opencode/opencode.db for the most recent non-archived session whose project.worktree matches.
  3. If found, runs opencode --session <id> with any extra args passed through.
  4. If not found, runs opencode with any extra args passed through.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment