···7373 const tombstones = this.#getTombstones(name);
7474 let tombstonesChanged = false;
75757676- const oldCol = await Output.data(l[name]);
7777- if (oldCol && Array.isArray(oldCol.data)) {
7878- for (const record of oldCol.data) {
7979- if (!newIds.has(record.id) && !tombstones.has(record.id)) {
8080- tombstones.add(record.id);
8181- tombstonesChanged = true;
8282- }
7676+ const existing = l[name].collection();
7777+ const existingArr =
7878+ existing.state === "loaded" && Array.isArray(existing.data)
7979+ ? existing.data
8080+ : [];
8181+8282+ for (const record of existingArr) {
8383+ if (!newIds.has(record.id) && !tombstones.has(record.id)) {
8484+ tombstones.add(record.id);
8585+ tombstonesChanged = true;
8386 }
8487 }
8588···103106 await l[name].save(newData);
104107105108 if (remote.ready()) {
106106- remote[name].save(newData).then(() => {
109109+ // Merge with any records added remotely since the last sync so we
110110+ // don't accidentally overwrite them with our local-only view.
111111+ const remoteCol = remote[name].collection();
112112+ const remoteArr =
113113+ remoteCol.state === "loaded" && Array.isArray(remoteCol.data)
114114+ ? remoteCol.data
115115+ : [];
116116+ const dataForRemote = this.#mergeRecords(name, newData, remoteArr);
117117+118118+ remote[name].save(dataForRemote).then(() => {
107119 const rev = this.#atproto()?.rev();
108120 if (rev) this.#storeRev(rev);
109121 this.#clearDirty();
···265277 if (tombstones.has(record.id)) continue;
266278267279 // If this id was previously known but is absent from local,
268268- // it was deleted locally — skip it.
269269- if (knownIds.has(record.id) && !merged.has(record.id)) continue;
280280+ // it was deleted locally — skip it. Only apply this heuristic when
281281+ // localArr is non-empty; an empty localArr could mean the local cache
282282+ // was cleared rather than the user deleting everything.
283283+ if (
284284+ localArr.length > 0 &&
285285+ knownIds.has(record.id) &&
286286+ !merged.has(record.id)
287287+ ) continue;
270288271289 const existing = merged.get(record.id);
272290