Skip to content

feat(opencode): add db doctor and repair commands#32093

Open
pascalandr wants to merge 12 commits into
anomalyco:devfrom
Pagecran:feat/db-doctor-repair
Open

feat(opencode): add db doctor and repair commands#32093
pascalandr wants to merge 12 commits into
anomalyco:devfrom
Pagecran:feat/db-doctor-repair

Conversation

@pascalandr

@pascalandr pascalandr commented Jun 12, 2026

Copy link
Copy Markdown

Issue for this PR

Related issues (does not automatically close): #29908, #31204, #19191, #29581, #31597, #23594, #30157, #14970, #24780.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds native database health commands for local OpenCode SQLite databases:

  • opencode db doctor and opencode db doctor --json
  • opencode db repair --dry-run and opencode db repair --apply

The commands inspect local OpenCode SQLite databases directly. They diagnose/repair rows by comparing persisted data shapes against the invariants required by the OpenCode binary currently running the command.

The supported repair catalog is exported from @opencode-ai/core/database/health as SUPPORTED_REPAIRS and is included in doctor / repair --dry-run JSON output. Each catalog entry declares the target OpenCode version/invariant, known target migration when applicable, source evidence if known, repair behavior, and safety constraints. The report also includes a compatibility block with the current binary version, expected migrations for that binary, applied DB migrations, and observed session.version distribution.

Supported repair catalog (for now)

  • part_legacy_id_prefix: renames part_<suffix> IDs to prt_<suffix> only when the target ID does not already exist. This fixes the observed load failure: Expected a string starting with "prt", got "part_..." at [0]["parts"][0]["id"].
  • assistant_message_missing_agent: copies assistant session_message.data.mode to data.agent with original-JSON precondition checks.
  • session_agent_missing: fills session.agent only when one unambiguous value can be derived from assistant or agent-switched messages.
  • session_model_missing: fills session.model only when one unambiguous model object can be derived from assistant or model-switched messages.
  • session_path_missing: fills session.path from the same row's non-empty session.directory.
  • message_assistant_missing_parent: fills missing assistant parentID from the immediately preceding message in the same session.
  • message_user_missing_agent: fills missing user agent only when one unambiguous agent is derivable.
  • message_user_missing_model: fills missing user model only when one unambiguous model is derivable.
  • part_step_finish_missing_reason: fills missing step-finish.reason with stop.
  • part_compaction_missing_auto: fills missing compaction.auto with false.
  • part_tool_completed_missing_metadata: fills missing completed-tool state.metadata with {}.
  • part_tool_completed_missing_title: fills missing completed-tool state.title from data.tool only when present.
  • part_tool_completed_missing_time: fills missing completed-tool state.time.start/end from the part row timestamp while preserving any existing start/end value.

How did you verify your code works?

I manually validated the tool against several real local user database and confirmed it repaired the targeted damaged session rows that caused session loading failures in Codenomad.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Adds native db doctor and repair flows with JSON output, dry-run and apply modes, safe/aggressive repair planning, backup-before-apply safeguards, transactional preconditions, and targeted DB tests.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 12, 2026
@pascalandr pascalandr marked this pull request as draft June 12, 2026 19:15
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

# Conflicts:
#	packages/opencode/src/cli/cmd/db.ts
#	packages/opencode/src/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant