Skip to content

fix(desktop): sync each agent-VM row with its own column set (#10602) - #10608

Merged
kodjima33 merged 2 commits into
mainfrom
watchdog/issue-10602-sync-column-sets
Jul 25, 2026
Merged

kodjima33 merged 2 commits into
mainfrom
watchdog/issue-10602-sync-column-sets

Conversation

@kodjima33

@kodjima33 kodjima33 commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #10602 — the agent VM's /sync endpoint dropped columns the desktop had populated.

The desktop serializes each row with its NULL columns omitted (AgentSyncService.swift:523 skips .null so the VM applies its own defaults), so column sets differ row to row inside one batch. agent.mjs derived the whole batch's schema from Object.keys(rows[0]) and applied it to all 100 rows.

Failure: a screenshots batch whose first row was captured but not yet OCR'd/embedded omits ocrText and embedding, so the other 99 rows — which do have them — were inserted without them. Because it is INSERT OR REPLACE (delete + insert), values already on the VM were wiped as well. /sync still answered {"applied": 100}, so the desktop advanced its cursor and the rows were never resent. semantic_search (WHERE embedding IS NOT NULL) then silently returns nothing for those screenshots and screenshots_fts has no text to match. The converse held too: a column present only on row 0 was written as NULL over real data on the rest.

Fix: group the batch by column set and prepare one statement per group, so every row is written with exactly the columns it carries. The union of all column sets still drives the missing-column ALTER TABLE, and applied now counts rows actually inserted.

The fix stays inside agent.mjs on purpose: the VM's hot-update path (checkAndApplyUpdate) downloads only agent.mjs, so a new sibling module would not reach a running VM.

Verification

  • desktop/macos/agent-cloud/sync-e2e.mjs (new): starts the real agent.mjs --serve against a real SQLite file and POSTs a desktop-shaped batch over HTTP, then reads the rows back.
    • Fails on the pre-fix code: row 2 ocrText: null, row 2 embedding: undefined, row 3 ocrText: null, row 3 windowTitle: null.
    • Passes on the fix, including the converse order (populated row first, bare row second) and leaving a pre-existing row untouched.
  • desktop/macos/tests/test-agent-cloud-sync.sh (new) runs it from the auto-discovered desktop launcher-test lane. agent-cloud's runtime deps are not vendored, so it installs them on first run and skips loudly (SKIP: could not install agent-cloud dependencies) rather than failing an unrelated desktop change when no prebuilt better-sqlite3 exists for the runner's Node — verified in both states locally (Node 22: runs and passes; Node 26: skips).
  • make preflight, scripts/pr-preflight --suggest.

Adjacent finding (not fixed here)

openDatabase() creates idx_screenshots_date_local ON screenshots(date(timestamp, 'localtime')). date(..., 'localtime') is non-deterministic, so SQLite rejects the index — but only when the table already has rows. On the usual path (an uploaded omi.db with screenshots) CREATE INDEX fails and is swallowed by the surrounding try, so nothing breaks. If the table is empty at open time, the index is created successfully and every later INSERT into screenshots fails with non-deterministic use of date() in an index — permanently, since the index persists in the database file. Reproduced with the bundled better-sqlite3 (SQLite 3.49.2). Left out of this PR to keep the sync-path change reviewable.

Failure-Class: none

Review in cubic

The desktop serializes each /sync row with its NULL columns omitted
(AgentSyncService.swift skips .null so the VM applies its own defaults), so
column sets differ from row to row inside one batch. agent.mjs took the whole
batch's schema from Object.keys(rows[0]) and applied it to all 100 rows: a
screenshots batch whose first row was captured but not yet OCR'd/embedded
omitted ocrText and embedding, so the other 99 rows were inserted without the
OCR text and embeddings they did carry — and because it is INSERT OR REPLACE,
any values already on the VM were wiped too. /sync still answered
{"applied": 100}, so the desktop advanced its cursor and never resent them.
semantic_search (WHERE embedding IS NOT NULL) then returns nothing for those
screenshots and screenshots_fts has no text to match.

Group the batch by column set and prepare one statement per group, so every row
is written with exactly the columns it carries. The union of all column sets
still drives the missing-column ALTER TABLE, and applied now counts rows
actually inserted.

Verification: sync-e2e.mjs drives the real agent.mjs server against a real
SQLite file with a desktop-shaped batch. It fails on the pre-fix code (row 2
ocrText/embedding null, row 3 ocrText/windowTitle null) and passes on the fix,
including the converse order (populated row first, bare row second).

Fixes #10602

Failure-Class: none
@kodjima33
kodjima33 merged commit d3e7015 into main Jul 25, 2026
29 of 32 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-10602-sync-column-sets branch July 25, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agent-cloud /sync drops non-NULL columns when the first row of a batch has NULLs

1 participant