A shell function that wraps opencode and automatically resumes the most recent session for the current project, scoped by git repo root.
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.
oc queries OpenCode's SQLite database to find the most recent non-archived session for the current project, then resumes it with --session.
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.
# 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- Resolves the git repo root (
git rev-parse --show-toplevel), falling back to$PWDif not in a git repo. - Queries
~/.local/share/opencode/opencode.dbfor the most recent non-archived session whoseproject.worktreematches. - If found, runs
opencode --session <id>with any extra args passed through. - If not found, runs
opencodewith any extra args passed through.