Repository navigation
fix(desktop): sync each agent-VM row with its own column set (#10602) - #10608
Merged
Merged
Conversation
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
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
Fixes #10602 — the agent VM's
/syncendpoint dropped columns the desktop had populated.The desktop serializes each row with its NULL columns omitted (
AgentSyncService.swift:523skips.nullso the VM applies its own defaults), so column sets differ row to row inside one batch.agent.mjsderived the whole batch's schema fromObject.keys(rows[0])and applied it to all 100 rows.Failure: a
screenshotsbatch whose first row was captured but not yet OCR'd/embedded omitsocrTextandembedding, so the other 99 rows — which do have them — were inserted without them. Because it isINSERT OR REPLACE(delete + insert), values already on the VM were wiped as well./syncstill 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 andscreenshots_ftshas no text to match. The converse held too: a column present only on row 0 was written asNULLover 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, andappliednow counts rows actually inserted.The fix stays inside
agent.mjson purpose: the VM's hot-update path (checkAndApplyUpdate) downloads onlyagent.mjs, so a new sibling module would not reach a running VM.Verification
desktop/macos/agent-cloud/sync-e2e.mjs(new): starts the realagent.mjs --serveagainst a real SQLite file and POSTs a desktop-shaped batch over HTTP, then reads the rows back.row 2 ocrText: null,row 2 embedding: undefined,row 3 ocrText: null,row 3 windowTitle: null.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 prebuiltbetter-sqlite3exists 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()createsidx_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 uploadedomi.dbwith screenshots)CREATE INDEXfails and is swallowed by the surroundingtry, so nothing breaks. If the table is empty at open time, the index is created successfully and every laterINSERTintoscreenshotsfails withnon-deterministic use of date() in an index— permanently, since the index persists in the database file. Reproduced with the bundledbetter-sqlite3(SQLite 3.49.2). Left out of this PR to keep the sync-path change reviewable.Failure-Class: none