refactor(db): remove redundant data, significantly simplify - #629
refactor(db): remove redundant data, significantly simplify#629cooper (czxtm) wants to merge 1 commit into
Conversation
did a pass on the sqlite schema, replaced it with an equivalent but much
smaller and simpler schema, and realized the downstream simplifications
that became possible as a result.
Replace the speculative-changeset store (changesets.rs, commits.rs,
store_{bare,whole_diff}_changeset.rs, summarize/group_existing.rs and
migrations 02/03) with a content-addressed schema keyed by sha256 of
sorted change hashes:
- db/keys.rs — snapshot_key / group_key helpers
- db/snapshots.rs — snapshots table (cached commit message, its id is
the changeset_id plumbed through evolve/build)
- db/summaries.rs — patch_summaries (singles) + summary_groups
- migrations/01_initial/up.sql rewritten to define the new tables
- migrations 02-restore-commits, 03-drop-queued-summaries dropped
summarize::find_existing now returns a SemanticChangeMap directly:
greedily selects non-overlapping summary_groups (members ⊔ live hashes),
falls back to patch_summaries for uncovered hashes, then loads the
snapshot.
🎨 Storybook previewUpdated for fcf2f6c
|
There was a problem hiding this comment.
Caution
The entire 01-initial migration is rewritten in place, and migrations 02-restore-commits and 03-drop-queued-summaries are deleted — but no new migration (e.g.
apps/native/src-tauri/migrations/01-initial/up.sql:61
Warning
commit: db_commit is replaced with commit: None, meaning the Commit field in every HistoryEntry is now permanently null.
apps/native/src-tauri/src/history/get_history.rs:113
2 finding(s) posted as inline comments.
| -- Content-addressed summary schema. | ||
| -- | ||
| -- Summaries describe *what a patch does*, keyed only by the content hash of the | ||
| -- change(s) they cover. Git remains the source of truth for diffs; these tables | ||
| -- are a local cache that can always be rebuilt by re-summarizing. | ||
|
|
||
| -- Per-change summary. Used for singletons / fallback (a change the model | ||
| -- described on its own, or a one-member group). | ||
| CREATE TABLE IF NOT EXISTS patch_summaries ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| change_hash TEXT NOT NULL UNIQUE, | ||
| title TEXT NOT NULL DEFAULT '', | ||
| description TEXT NOT NULL DEFAULT '', | ||
| status TEXT NOT NULL DEFAULT 'DONE' CHECK(status IN ('QUEUED', 'DONE', 'FAILED', 'CANCELLED')), | ||
| created_at INTEGER NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS evolutions ( | ||
| -- First-class group summary. `group_key` content-addresses the exact set of | ||
| -- member change hashes (sha256 of the sorted hashes), so a group's identity is | ||
| -- its membership. | ||
| CREATE TABLE IF NOT EXISTS summary_groups ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| origin_branch TEXT NOT NULL, | ||
| merged INTEGER NOT NULL DEFAULT 0, | ||
| builds INTEGER NOT NULL DEFAULT 0 | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS change_summaries ( | ||
| id INTEGER PRIMARY KEY, | ||
| group_key TEXT NOT NULL UNIQUE, | ||
| title TEXT NOT NULL DEFAULT '', | ||
| description TEXT NOT NULL DEFAULT '', | ||
| status TEXT NOT NULL DEFAULT 'QUEUED' CHECK(status IN ('QUEUED', 'DONE', 'FAILED', 'CANCELLED')), | ||
| status TEXT NOT NULL DEFAULT 'DONE' CHECK(status IN ('QUEUED', 'DONE', 'FAILED', 'CANCELLED')), | ||
| created_at INTEGER NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS changes ( | ||
| id INTEGER PRIMARY KEY, | ||
| hash TEXT NOT NULL UNIQUE, | ||
| filename TEXT NOT NULL, | ||
| diff TEXT NOT NULL, | ||
| line_count INTEGER NOT NULL, | ||
| created_at INTEGER NOT NULL, | ||
| own_summary_id INTEGER REFERENCES change_summaries(id) | ||
| -- Membership of a group. Content-addressed by `group_key`. | ||
| CREATE TABLE IF NOT EXISTS summary_group_members ( | ||
| group_key TEXT NOT NULL REFERENCES summary_groups(group_key), | ||
| change_hash TEXT NOT NULL, | ||
| PRIMARY KEY (group_key, change_hash) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS group_summaries ( | ||
| change_id INTEGER NOT NULL REFERENCES changes(id), | ||
| change_summary_id INTEGER NOT NULL REFERENCES change_summaries(id) | ||
| -- Thin evolution record — just the origin branch an evolution started from. | ||
| CREATE TABLE IF NOT EXISTS evolutions ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| origin_branch TEXT NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS change_sets ( | ||
| -- A snapshot caches the generated commit message for an exact set of change | ||
| -- hashes. `snapshot_key` = sha256(sorted change hashes). The integer `id` is the | ||
| -- value historically plumbed as `changeset_id` throughout evolve/build state. | ||
| CREATE TABLE IF NOT EXISTS snapshots ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| commit_id INTEGER REFERENCES commits(id), | ||
| base_commit_id INTEGER NOT NULL REFERENCES commits(id), | ||
| commit_message TEXT, | ||
| snapshot_key TEXT NOT NULL UNIQUE, | ||
| generated_commit_message TEXT, | ||
| created_at INTEGER NOT NULL, | ||
| evolution_id INTEGER REFERENCES evolutions(id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS set_changes ( | ||
| change_set_id INTEGER NOT NULL REFERENCES change_sets(id), | ||
| change_id INTEGER NOT NULL REFERENCES changes(id), | ||
| PRIMARY KEY (change_set_id, change_id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS queued_summaries ( | ||
| id INTEGER PRIMARY KEY, | ||
| status TEXT NOT NULL DEFAULT 'QUEUED' CHECK(status IN ('QUEUED', 'DONE', 'FAILED', 'CANCELLED')), | ||
| attempted_count INTEGER NOT NULL DEFAULT 0, | ||
| prompt TEXT NOT NULL, | ||
| model_response TEXT, | ||
| group_summary_id INTEGER REFERENCES change_summaries(id), | ||
| hash_own_summary_id_pairs TEXT, | ||
| type TEXT NOT NULL CHECK(type IN ('NEW_SINGLE', 'NEW_GROUP', 'EVOLVED_GROUP')) | ||
| evolution_id INTEGER REFERENCES evolutions(id), | ||
| created_at INTEGER NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS prompts ( | ||
| id INTEGER PRIMARY KEY, | ||
| text TEXT NOT NULL, | ||
| commit_id INTEGER REFERENCES commits(id) ON DELETE SET NULL, | ||
| created_at INTEGER NOT NULL | ||
| -- Records which commits were created by a restore operation and their origin. | ||
| CREATE TABLE IF NOT EXISTS restore_commits ( | ||
| commit_hash TEXT PRIMARY KEY, | ||
| origin_hash TEXT NOT NULL | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_commits_tree_hash ON commits(tree_hash); | ||
| CREATE INDEX IF NOT EXISTS idx_evolutions_origin_branch ON evolutions(origin_branch); | ||
| CREATE INDEX IF NOT EXISTS idx_prompts_commit ON prompts(commit_id); | ||
| CREATE INDEX IF NOT EXISTS idx_change_sets_commit ON change_sets(commit_id); | ||
| CREATE INDEX IF NOT EXISTS idx_change_sets_base ON change_sets(base_commit_id); | ||
| CREATE INDEX IF NOT EXISTS idx_set_changes_change ON set_changes(change_id); | ||
| CREATE INDEX IF NOT EXISTS idx_queued_summaries_status ON queued_summaries(status); | ||
| CREATE INDEX IF NOT EXISTS idx_summary_group_members_hash ON summary_group_members(change_hash); |
There was a problem hiding this comment.
Caution
The entire 01-initial migration is rewritten in place, and migrations 02-restore-commits and 03-drop-queued-summaries are deleted — but no new migration (e.g. 04-new-schema) is added to carry existing databases forward. Diesel migrations are identified by directory name and tracked in __diesel_schema_migrations; any database that already applied 01-initial will not re-run it, so patch_summaries, summary_groups, summary_group_members, and snapshots are never created, causing every call to db::summaries and db::snapshots to fail with "no such table." The state management migration plan locks "Preserve compatibility during frontend migration" and requires migrations to be "tested against temporary database," with no provision for rewriting baseline migrations in place.
| @@ -126,7 +113,7 @@ pub async fn get_history<R: Runtime>( | |||
| is_base, | |||
There was a problem hiding this comment.
Warning
commit: db_commit is replaced with commit: None, meaning the Commit field in every HistoryEntry is now permanently null. The Commit struct is still registered in specta_gen_ts.rs and exported to apps/native/src/ipc/sqlite.ts, suggesting the frontend type contract still expects this field to be populated for some entries; callers that read historyEntry.commit.hash (or similar) will now silently receive null where they previously received data.
📋 PR Overview
🔬 Coverage
|

did a pass on the sqlite schema, replaced it with an equivalent but much
smaller and simpler schema, and realized the downstream simplifications
that became possible as a result. There's no need to store information
which is already available via a
gitcommand, it just creates a burdenhaving to keep it updated.
summarize::find_existing now returns a SemanticChangeMap directly which
summarize multiple files. falls back to single file summary