fix(kanban): make _migrate_add_optional_columns idempotent on concurrent open (salvage #22627)#22800
Merged
Conversation
…ent open ALTER TABLE calls inside _migrate_add_optional_columns were guarded by a snapshot of PRAGMA table_info taken at function entry. When the gateway dispatcher opens the kanban DB twice per tick (once in _tick_once_for_board and once via init_db's discard-and-reconnect path), a second connection can run the same migration before the first one commits, causing: sqlite3.OperationalError: duplicate column name: consecutive_failures This crashed the dispatcher on every first tick after a gateway restart (subsequent ticks succeeded because the columns were then present). Fix: introduce _add_column_if_missing() which wraps ALTER TABLE in a try/except that swallows OperationalError whose message contains 'duplicate column name'. All ALTER TABLE calls in _migrate_add_optional_columns are routed through this helper. Closes #21708
Contributor
🔎 Lint report:
|
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.
Summary
Salvage of #22627 —
_migrate_add_optional_columnsis now idempotent on concurrent open. The kanban dispatcher's first-tick-after-restart no longer crashes withduplicate column name: consecutive_failures.Root cause
The dispatcher's
_tick_once_for_boardopens the DB once viaconnect()(which runs the migration), then callsinit_db()whichdiscards the path from_INITIALIZED_PATHSand re-opens — running the migration a second time on a new connection._migrate_add_optional_columnssnapshotscolsat function entry fromPRAGMA table_info(tasks). If the second connection's snapshot was taken before the first connection committed itsALTER, the guardif "consecutive_failures" not in colsis stale and the bareconn.execute("ALTER TABLE ...")raisesduplicate column name.Changes (contributor commit)
hermes_cli/kanban_db.py: new_add_column_if_missing(conn, table, column, ddl)helper. Catchessqlite3.OperationalErrorwhose message lower-cases-contains"duplicate column name", returns False; re-raises any other OperationalError. All 13 ALTER call sites in_migrate_add_optional_columnsnow route through it. Legacy backfills (spawn_failures → consecutive_failures,last_spawn_error → last_failure_error) gated on the helper's return value so they don't double-backfill.tests/hermes_cli/test_kanban_db.py: 2 idempotency tests covering the unit helper + the higher-level fully-migrated-schema scenario.Validation
The deeper smell —
_tick_once_for_boardcallinginit_db()afterconnect()(which already ran the migration) — remains. That's a separate cleanup; this PR is the right defensive layer regardless.Closes #21708 via salvage.