Repository navigation
[Ported] fix(sensor): select the opencode SQLite database by schema - #138
Merged
Merged
Conversation
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.
barisozbas
approved these changes
Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this? (check all applicable)
Related issue: N/A
What changed?
Four commits, each passing the suite on its own:
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.fix: backend detection opens each candidate read-only and uses the first one that has thesession,messageandparttables.storage/JSON tree.parse_all()recordsunsupported_schemainstead ofinput_missing.fix: ano such tableerror while parsing is recorded once asunsupported_schemainstead ofdatabase_error. Before, it could also be counted as onesession_build_errorper session. Other operational errors still recorddatabase_error.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.dbsits next to a populatedopencode-<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?
tmp_pathtests cover:unsupported_schemawith no fallbackdatabase_errortouch()-ed files.tests/test_parser_diagnostics.pynow expectsunsupported_schemafor opencode (Cursor and Warp are unchanged).pytest tests/ -qpasses (464), and so doruff check adr_sensor/andruff format --checkon the changed module.Potential risks
sqlite_masterread; usually there is only one file.database_error, as before.opencode.dbselect the same file as before.