Skip to content

[Ported] fix(sensor): select the opencode SQLite database by schema - #138

Merged
barisozbas merged 6 commits into
mainfrom
port/opencode-schema-aware-db
Sep 29, 2026
Merged

barisozbas merged 6 commits into
mainfrom
port/opencode-schema-aware-db

Conversation

@v314b0i

@v314b0i v314b0i commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Related issue: N/A

What changed?
Four commits, each passing the suite on its own:

  1. refactor: _find_db_files() lists every candidate database (OPENCODE_DB, opencode.db, opencode-<channel>.db) in the existing priority order. _find_db_file() still returns the first one, so there is no behaviour change.
  2. fix: backend detection opens each candidate read-only and uses the first one that has the session, message and part tables.
    • When databases exist but none has those tables, it falls back to the storage/ JSON tree.
    • With no JSON tree either, parse_all() records unsupported_schema instead of input_missing.
  3. fix: a no such table error while parsing is recorded once as unsupported_schema instead of database_error. Before, it could also be counted as one session_build_error per session. Other operational errors still record database_error.
  4. docs: the README and the module docstring describe the selection rule.

Why?
Picking the database by filename alone chooses the wrong source when a stale, empty or migration-only opencode.db sits next to a populated opencode-<channel>.db (e.g. after switching release channels), or next to pre-SQLite JSON history. Those sessions were skipped without any signal, and the failure looked like a corrupt or locked database rather than a format mismatch.

How did you test it?

  • New synthetic tmp_path tests cover:
    • a channel database chosen over a default database without tables
    • JSON fallback
    • an empty database file
    • an env-override database without tables
    • an unreadable candidate
    • unsupported_schema with no fallback
    • a table missing during parsing
    • an unreadable database still recording database_error
    • candidate ordering and deduplication
  • Existing detection tests now build real schema databases instead of touch()-ed files.
  • The shared incompatible-database case in tests/test_parser_diagnostics.py now expects unsupported_schema for opencode (Cursor and Warp are unchanged).
  • pytest tests/ -q passes (464), and so do ruff check adr_sensor/ and ruff format --check on the changed module.

Potential risks

  • Each candidate database is opened read-only once more at startup, for one sqlite_master read; usually there is only one file.
  • A database that has the tables but is otherwise unreadable still records database_error, as before.
  • Setups with a valid opencode.db select the same file as before.

Split candidate discovery out of _find_db_file into _find_db_files, which
returns every opencode*.db under a data directory in the existing priority
order (OPENCODE_DB override, opencode.db, then opencode-<channel>.db)
without duplicates. _find_db_file keeps its behaviour by returning the
first candidate.

No behaviour change; this prepares backend detection to consider more than
one database.
Backend detection used the first opencode*.db by filename alone. An empty
or migration-only opencode.db therefore shadowed a populated
opencode-<channel>.db, or hid an older storage/ JSON tree, and those
sessions were silently skipped.

Probe each candidate read-only and use the first one that contains the
session, message and part tables. When databases exist but none has them,
fall back to the JSON storage tree; with no JSON tree either, record
unsupported_schema instead of input_missing so the mismatch shows up as
suspected format drift.

Existing detection tests now build real schema databases instead of
empty files.
A database that lacks, or loses, the message or part table failed with
"no such table", which was counted as a generic database_error (or as one
session_build_error per session).

Treat "no such table" as unsupported_schema: the per-session loop stops
on it and the error is recorded once for the database. Other operational
errors, such as an unreadable file, still record database_error. The
shared incompatible-database diagnostics test now expects
unsupported_schema for opencode.
Document that an opencode database is only used when it has the session,
message and part tables, that other candidates and the JSON tree are tried
otherwise, and that a database without those tables is reported as
unsupported_schema.
@CLAassistant

CLAassistant commented Sep 27, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@barisozbas
barisozbas merged commit 4d90fd9 into main Sep 29, 2026
14 checks passed
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.

3 participants