fix(kanban): make _migrate_add_optional_columns idempotent on concurrent open#22627
Closed
wesleysimplicio wants to merge 1 commit into
Closed
fix(kanban): make _migrate_add_optional_columns idempotent on concurrent open#22627wesleysimplicio wants to merge 1 commit into
wesleysimplicio wants to merge 1 commit into
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 NousResearch#21708
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a startup-time race in the kanban DB migration where two concurrent connections can both attempt the same ALTER TABLE ... ADD COLUMN, causing sqlite3.OperationalError: duplicate column name: ... and crashing the first dispatcher tick after gateway restart.
Changes:
- Add
_add_column_if_missing()helper that makesADD COLUMNidempotent by swallowing the specific duplicate-columnOperationalError. - Route all
ALTER TABLE ... ADD COLUMNstatements in_migrate_add_optional_columns()through the helper (with backfill updates gated on whether this call actually added the column). - Add regression tests covering idempotency and “already migrated schema” behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
hermes_cli/kanban_db.py |
Introduces idempotent column-add helper and uses it throughout optional-column migrations to prevent duplicate-column crashes under concurrent opens. |
tests/hermes_cli/test_kanban_db.py |
Adds tests validating the helper’s duplicate-column handling and that the full migration doesn’t raise when columns already exist. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+966
to
+968
| def _add_column_if_missing( | ||
| conn: sqlite3.Connection, table: str, column: str, ddl: str | ||
| ) -> bool: |
Contributor
This was referenced May 10, 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.
Summary
The kanban dispatcher opens the DB twice per tick: once in
_tick_once_for_boardand once viainit_db's discard-and-reconnect path.Both connections call
_migrate_add_optional_columns, which snapshotsPRAGMA table_info(tasks)at entry. When they race, the second connectionsees the columns as absent (its snapshot was taken before the first commit)
and issues a duplicate
ALTER TABLE, crashing with:This fires once per gateway restart, writing one ERROR to
errors.log; thedispatcher recovers silently on the next tick (60 s later).
Root Cause
_migrate_add_optional_columnsused rawconn.execute("ALTER TABLE ...").No error handling for the duplicate-column case existed.
Fix
Introduce
_add_column_if_missing(conn, table, column, ddl)— a thinwrapper that catches
OperationalErrorwhose message contains"duplicate column name"and returnsFalseinstead of raising. AllALTER TABLEcalls in_migrate_add_optional_columnsare routed throughthis helper.
Tests
Two new tests in
tests/hermes_cli/test_kanban_db.py:test_add_column_if_missing_is_idempotent_on_race— verifies the helperreturns
Trueon first add,Falseon duplicate without raising.test_migrate_add_optional_columns_tolerates_concurrent_migration—runs the full migration against an already-migrated in-memory schema and
asserts no exception is raised.
Closes #21708