Skip to content

The seed tables' unique index is never created: both owners list two migrations at version 1 #1367

Closed

Description

@V3RON

The unique index that allows one ready seed per kind is never created, on either platform.

iosSourceDataOwner and androidSourceDataOwner each declare version: 1 with two entries in migrations: the CREATE TABLE, then the CREATE UNIQUE INDEX … WHERE kind = 'seed' AND state = 'ready'. openStateDatabase runs one entry per version step (src/state/open.ts, database.exec(owner.migrations[from]) for from < owner.version), so only the first runs.

Seen at e3a497278: a state.db opened with both owners holds the two tables and no index.

owners: ios-source-data version 1, 2 migrations; android-source-data version 1, 2 migrations
objects: table android_source_data, table ios_source_data, table schema_versions

Found on the slow lane: two ready iOS seed rows with one spec_key sat in one state.db (a row a test helper inserted with a damaged key, and the driver's own). The drivers' per-kind lock is what keeps one seed per kind today; the index would have refused the second row.

A fix has to reach databases already at version 1, so it is a version 2 step, not a change to step 1.

Written by an agent.

Activity

  1. added
    bug:new
    platform:ios
    platform:android
    on Nov 6, 2026
  2. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    This was generated by AI during triage.

    Triage notes

    Confirmed from the code (read only, nothing run):

    Things to settle in the fix:

    • Bump both owners to version: 2, with the index as step 2 (index [1]), as the issue says. Step 1 stays the same so databases already at version 1 pick it up.
    • A database that already holds two ready seed rows for one spec_key (as on the slow lane) makes CREATE UNIQUE INDEX fail. Then migrateOwner refuses the start with "could not move from version 1 to 2". The fix should decide whether step 2 first deletes or marks the extra rows, or whether that refusal is fine.
    • A guard worth having: openStateDatabase, or a test, checks migrations.length === version for each owner, so the next owner can't make this mistake.

    Labels: added platform:ios and platform:android. Kept bug:new. The full rule 8 report (failing test, fast-track lines) and bug:ready are left to the triage-bug run or the maintainer.

    Written by an agent.

  3. self-assigned this
    on Nov 6, 2026
  4. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Reproduction

    Confirmed: both seed tables lack the index, on a new state.db and on one at version 1. Branch bug/1367-repro, src/daemon/state-owners.test.ts, "the seed tables' one-ready-seed-per-kind index (#1367)": five cases (two per platform, plus one over every owner). All five fail on main.

    pnpm exec vitest run --project unit src/daemon/state-owners.test.ts
    ✗ ios_source_data / android_source_data: a new state.db refuses a second ready seed of one kind, ...
      AssertionError: expected [Function] to throw an error
    ✗ ios_source_data / android_source_data: a state.db at version 1 moves to version 2 at open, keeping the newest ready seed of each kind (a tie kept by the larger source id), demoting the others to making, ...
      AssertionError: expected { version: 1 } to deeply equal { version: 2 }
    ✗ every schema owner a worker or a gateway opens has one migration per version
      - "migrations": 1, + "migrations": 2   (ios-source-data, android-source-data; the other 7 owners match)
    Tests  5 failed (5)
    

    Root cause

    Defect. src/drivers/ios/source-data.ts:26 and src/drivers/android/source-data.ts:26 declare version: 1 but list two steps. migrateOwner (src/state/open.ts) runs migrations[from] only for from < version, so the CREATE UNIQUE INDEX step (index 1) never runs. ADR 0036 says one step per version. The symptom shows only when the per-kind lock fails, as on the slow lane.

    Simplest fix

    The maintainer's choice. Set both owners to version: 2. Step 2 first sets the older ready seed rows of a spec_key to making: it keeps the newest created_at, and on a tie the larger source_id. It then creates the index. Each driver's sweep then deletes those rows with their files. iOS: #removeLeftover runs simctl delete at the first listSources (create-core.ts:473). Android: #removeSource stops the emulator, then removes the folder and the row.

    -  version: 1,
    +  version: 2,
    +    `UPDATE <t> SET state = 'making' WHERE kind = 'seed' AND state = 'ready' AND EXISTS (
    +       SELECT 1 FROM <t> AS newer WHERE newer.kind = 'seed' AND newer.state = 'ready'
    +         AND newer.spec_key = <t>.spec_key AND (newer.created_at > <t>.created_at
    +           OR (newer.created_at = <t>.created_at AND newer.source_id > <t>.source_id)));
         CREATE UNIQUE INDEX <t>_ready_seed ...`
    vitest run state-owners.test.ts src/drivers/ios src/drivers/android src/state src/daemon/device-data-tables.test.ts
    Tests  1462 passed | 2 skipped
    

    pnpm test:changed: 2 failures, the same on main. A gateway already holds port 4700 on this host (#1382).

    Alternatives rejected

    • Refuse the start while two ready seeds share a kind: the user would have to fix it by hand.
    • Delete the extra rows inside the step: leaves the simulator and emulator files with no row.

    Risk

    • The guard test lists the owners by hand, copied from main.ts:268-279. An owner added later is not checked. The same check in migrateOwner would cover every owner.
    • Step 2 runs two statements in one exec. Check that a failed index rolls the demotion back inside the start transaction.

    Fast track

    • Defect: yes
    • Fix tried: yes
    • Nothing public added or broken: yes
    • No ADR or rule changed: yes
    • One fix: no — refusing the start is a choice users would see; the maintainer picked demotion

    Side findings

    Written by an agent.

  5. removed their assignment
    on Nov 6, 2026
  6. added
    bug:ready
    and removed
    bug:new
    on Nov 6, 2026
  7. self-assigned this
    on Nov 6, 2026
  8. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: #1384 (#1367)
    Parked: none
    Waiting: none
    Decided: none

    Written by an agent.

  9. added a commit that references this issue on Nov 10, 2026
    f9ff4a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:readyplatform:androidplatform:ios

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions