Apply deletions the node sends without content and never trap sync in review

Daochi now drops the content of a deleted record (kryonlabs/daochi d862a34)
and sends only its times. Breeze treated each such tombstone as a failed
decryption, so the whole full snapshot failed to apply: its merge preview
was unavailable, Merge and Use remote were disabled, and the review came
back after every sync.

A content-free tombstone now marks the local row deleted unless this
device changed it later, with a deletion winning an equal-time tie as on
the node. Kinds not kept as marked rows change nothing.

A full snapshot whose merge cannot be rehearsed is now a sync error rather
than a saved review, and a waiting review that can neither merge nor be
previewed is set aside instead of opening a dialog with no working choice.
A saved review may be as large as any accepted sync response (16 MB).
This commit is contained in:
waozi committed 2026-10-29 14:32:07 -03:00
1 parent 9f1fe26998
commit ee1109eef6
7 files changed
+222 -19

No files matched your search

+4
View File
@@ -1,5 +1,9 @@
# Changelog
## [Unreleased]
- Sync no longer gets stuck on a review with no preview after something was deleted.
## [2.1.61] - 2026-10-29
- The download is about 16 MB smaller: apps are stored more compactly, and shared pictures are included only once.
+2 -1
View File
@@ -23,6 +23,7 @@ app_reload_after_import :: (app: *InnerBreeze, reload_settings: s32) #foreign c
#import, file "../platform/environment.zi";
storage_sync_review_pending :: () -> s32 #foreign c "storage_sync_review_pending";
storage_sync_review_merge_pending :: () -> s32 #foreign c "storage_sync_review_merge_pending";
storage_sync_review_showable :: () -> s32 #foreign c "storage_sync_review_showable";
storage_sync_review_version :: () -> s64 #foreign c "storage_sync_review_version";
app_open_modal :: (app: *InnerBreeze, modal_type: ModalType) #foreign c "app_open_modal";
PROFILE_LEADERBOARD_STREAK :: 0
@@ -1307,7 +1308,7 @@ app_sync_pump :: (app: *InnerBreeze) {
sync_review_handled_version = sync_review_cache_version
if storage_sync_review_merge_pending() != 0 {
app_reload_after_import(app, 0)
} else {
} else if storage_sync_review_showable() != 0 {
app_open_modal(app, cast(ModalType)ModalSyncReview)
}
}
+1
View File
@@ -117,6 +117,7 @@ int storage_sync_review_json_has_visible_diff(const char *json);
int storage_sync_review_json_should_auto_apply_remote(const char *json);
int storage_sync_review_clear_if_no_visible_diff(void);
int storage_sync_review_merge_pending(void);
int storage_sync_review_showable(void);
int storage_sync_review_apply_remote_if_local_empty(void);
int storage_apply_pending_sync_review(int use_remote);
void storage_purge_synced_deleted_data(void);
+41
View File
@@ -389,6 +389,37 @@ storage_apply_entity_record :: (collection: string, record_id: string, plaintext
return 1
}
// The node keeps no content for a deleted record, only when it was deleted
// and changed. The local row is marked deleted unless this device changed it
// later; a deletion wins an equal-time tie, as on the node. A record kind
// that is not kept as a marked row here changes nothing.
storage_apply_record_tombstone :: (collection: string, record_id: string,
deleted_at: string, updated_at: string) -> s32 {
table := storage_decrypted_array_name(collection)
if table != "habits" && table != "sessions" && table != "elist_lists" &&
table != "elist_items" && table != "habit_entries" && table != "habit_changes" &&
table != "habit_rewards" && table != "habit_coins" {
return 1
}
if record_id.count == 0 {
return 1
}
sql: [768]u8
unused AppCopyText("WITH tombstone(deleted,at) AS (SELECT CAST(?3 AS INTEGER),COALESCE(NULLIF(CASE WHEN ?4 GLOB '*[^0-9]*' THEN CAST(strftime('%s',?4) AS INTEGER) ELSE CAST(?4 AS INTEGER) END,0),CAST(?3 AS INTEGER))) UPDATE ", sql[:])
unused AppAppendText(table, sql[:])
unused AppAppendText(" SET deleted_at=(SELECT deleted FROM tombstone),updated_at=(SELECT at FROM tombstone) WHERE user_id=?1 AND id=?2 AND deleted_at=0 AND updated_at<=(SELECT at FROM tombstone)", sql[:])
statement: *void = null
if SqlitePrepareText(storage_db_handle(), TextUntilNul(sql[:]), -1, *statement, null) != SQLITE_OK {
return 0
}
defer { SqliteFinalize(statement) }
bind_text(statement, 1, AppTextFromCString(storage_user_id_buffer(), 64))
bind_text(statement, 2, record_id)
bind_text(statement, 3, deleted_at)
bind_text(statement, 4, updated_at)
return ifx SqliteStep(statement) == SQLITE_DONE then 1 else 0
}
storage_apply_encrypted_records :: (response_json: string) -> s32 {
habit_batch_storage: StorageJsonBuilder = StorageJsonBuilder.{}
habit_batch: *StorageJsonBuilder = *habit_batch_storage
@@ -480,6 +511,16 @@ storage_apply_encrypted_records :: (response_json: string) -> s32 {
index += 1
continue
}
if deleted.valid && deleted.value > 0.0 && ciphertext.count == 0 {
ok = storage_apply_record_tombstone(collection, record_id,
AppTextFromCString(fields[index * 8 + 7], 32), updated_at)
if ok == 0 {
LogStorageText(LOG_WARNING, "SYNC: record deletion apply failed: ",
collection)
}
index += 1
continue
}
ok = ifx (record_id.count > 0 && key_id.count > 0 && nonce.count > 0 && ciphertext.count > 0 && storage_decrypt_record(collection, record_id, key_id, nonce,
ciphertext, content_hash, *plaintext) != 0) then (1) else (0)
if ok != 0 {
+28 -16
View File
@@ -225,26 +225,38 @@ ApplyStoredSyncResponse :: (response_json: string, page: *SyncPageState) -> s32
LogStorage(LOG_INFO, "SYNC: merging full snapshot with pending local edits")
} else if storage_sync_review_json_should_auto_apply_remote(response_json) != 0 {
return storage_apply_remote_full_snapshot(response_json)
} else if storage_sync_review_json_has_visible_diff(response_json) == 0 {
// The same merge preview shown to the user has no changes. Apply
// its bookkeeping without replacing or removing local records.
LogStorage(LOG_INFO, "SYNC: full snapshot has no visible merge changes")
} else {
if storage_sync_review_write_json(response_json) == 0 {
LogStorage(LOG_WARNING, "SYNC: response review save failed")
merge_changes := storage_sync_review_json_change_count(response_json)
if merge_changes < 0 {
// A snapshot whose merge cannot be rehearsed cannot be merged
// or replaced either. Saving it for review would offer no
// preview and no working choice, so the sync fails and local
// data stays as it is.
LogStorage(LOG_WARNING, "SYNC: full snapshot cannot be applied")
return 0
}
{
meta_key_buffer: [64] u8
meta_val_buffer: [4] u8
AppCopyText(STORAGE_SYNC_PENDING_REVIEW_KEY, meta_key_buffer[:])
AppCopyText("1", meta_val_buffer[:])
set_meta(*meta_key_buffer[0], *meta_val_buffer[0])
if merge_changes == 0 {
// The same merge preview shown to the user has no changes.
// Apply its bookkeeping without replacing or removing local
// records.
LogStorage(LOG_INFO, "SYNC: full snapshot has no visible merge changes")
} else {
if storage_sync_review_write_json(response_json) == 0 {
LogStorage(LOG_WARNING, "SYNC: response review save failed")
return 0
}
{
meta_key_buffer: [64] u8
meta_val_buffer: [4] u8
AppCopyText(STORAGE_SYNC_PENDING_REVIEW_KEY, meta_key_buffer[:])
AppCopyText("1", meta_val_buffer[:])
set_meta(*meta_key_buffer[0], *meta_val_buffer[0])
}
storage_state_handle().pending_sync_outbox_seq = 0
storage_schedule_persist()
LogStorage(LOG_WARNING, "SYNC: full snapshot review required")
return 1
}
storage_state_handle().pending_sync_outbox_seq = 0
storage_schedule_persist()
LogStorage(LOG_WARNING, "SYNC: full snapshot review required")
return 1
}
}
old_server_version = storage_meta_int64("sync_last_server_version", 0)
+41 -2
View File
@@ -195,7 +195,9 @@ storage_sync_review_read_json :: () -> *u8 {
file := OpenRead(AppTextFromCString(*path[0], STORAGE_PATH_SIZE))
if !file.valid { return null }
size = FileSize(file)
if size < 0 || size > 8 * 1024 * 1024 {
// A saved review is a whole sync response, up to the largest one a sync
// accepts (AsyncResponseCapacity in sync_async.zi).
if size < 0 || size > 16 * 1024 * 1024 {
CloseFile(file)
return null
}
@@ -671,11 +673,48 @@ storage_sync_review_apply_remote_if_local_empty :: () -> s32 {
}
storage_sync_review_json_has_visible_diff :: (json: string) -> s32 {
return ifx storage_sync_review_json_change_count(json) != 0 then 1 else 0
}
// The visible changes a merge of this snapshot makes, or -1 when its merge
// cannot be rehearsed: a snapshot that cannot be merged at all.
storage_sync_review_json_change_count :: (json: string) -> s32 {
changes: s32 = -1
if review_plan_for_json(json, 2, null, *changes) == 0 {
return -1
}
return changes
}
// Whether the waiting review has a choice besides keeping local data: a
// merge or a replacement that can be rehearsed and therefore previewed.
storage_sync_review_choice_available :: () -> s32 {
json := storage_sync_review_read_json()
if json == null {
return 0
}
text := AppTextFromCString(json, 2147483647)
changes: s32 = -1
available := review_plan_for_json(text, 2, null, *changes) != 0 ||
review_plan_for_json(text, 1, null, *changes) != 0
free(cast(*void)json)
return ifx available then 1 else 0
}
// Whether a review whose merge failed can be shown. One with no preview and
// no working choice is set aside instead, keeping local data, so it never
// traps the person; the next sync fetches the account again.
#program_export
storage_sync_review_showable :: () -> s32 {
if storage_sync_review_pending() == 0 {
return 0
}
if storage_sync_review_choice_available() != 0 {
return 1
}
return ifx changes > 0 then 1 else 0
LogStorage(LOG_WARNING, "SYNC: review set aside: its snapshot cannot be applied")
unused storage_apply_pending_sync_review(0)
return 0
}
storage_sync_review_json_should_auto_apply_remote :: (json: string) -> s32 {
+105
View File
@@ -1561,6 +1561,108 @@ test_waiting_review_merges :: () {
storage_close()
}
// The node keeps no content for a deleted record, only its times, the
// shape a node sent the owner's Pixel on 2026-10-29. Such a deletion
// reaches this device unless this device changed the record later; a kind
// not kept as a marked row here changes nothing and stops nothing.
TOMBSTONE_LOCAL_SQL :: "INSERT INTO elist_lists VALUES('gone-list',(SELECT id FROM users LIMIT 1),'Gone list',0,0,1782200000);INSERT INTO elist_items VALUES('gone-item',(SELECT id FROM users LIMIT 1),'gone-list','Gone item','',0,0,0,1782200000);INSERT INTO elist_items VALUES('tie-item',(SELECT id FROM users LIMIT 1),'gone-list','Tie item','',0,1,0,1782300000);INSERT INTO elist_items VALUES('kept-item',(SELECT id FROM users LIMIT 1),'gone-list','Kept item','',0,2,0,1782400000);INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('gone-session',(SELECT id FROM users LIMIT 1),1782200000,20260623,1,1,'test',1782200000,'303',0,1782200000);"
TOMBSTONE_RECORDS :: "{\"collection\":\"private.inbe.v1.elist-items\",\"id\":\"gone-item\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"gone\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.elist-items\",\"id\":\"tie-item\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"tie\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.elist-items\",\"id\":\"kept-item\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"kept\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.elist-items\",\"id\":\"never-here\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"never\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.elist-lists\",\"id\":\"gone-list\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"list\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.sessions\",\"id\":\"gone-session\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"session\",\"schema_version\":1},{\"collection\":\"private.inbe.v1.habit-days\",\"id\":\"remote-habit:20260101\",\"key_id\":\"breathing-main-1\",\"updated_at\":\"2026-06-24T11:20:00.000000000Z\",\"deleted_at\":1782300000,\"content_hash\":\"day\",\"schema_version\":1}"
// Puts the content-free deletions before the remote device's records, as
// the node sent them, so every record after them must still apply.
add_tombstone_records :: () {
records := TextFromCString(*remote_records_text.bytes[0])
combined: TextBuffer
append(*combined, "[")
append(*combined, TOMBSTONE_RECORDS)
if records.count > 2 {
append(*combined, ",")
append(*combined, records[1:records.count])
} else {
append(*combined, "]")
}
remote_records_text = combined
}
check_tombstones_applied :: (root: string) {
check_int("an older item is deleted", count_of(root,
"SELECT COUNT(*) FROM elist_items WHERE id='gone-item' AND deleted_at=1782300000 AND updated_at=1782300000 AND title='Gone item'"), 1)
check_int("a deletion wins an equal-time tie", count_of(root,
"SELECT COUNT(*) FROM elist_items WHERE id='tie-item' AND deleted_at=1782300000"), 1)
check_int("an item changed after the deletion stays", count_of(root,
"SELECT COUNT(*) FROM elist_items WHERE id='kept-item' AND deleted_at=0 AND updated_at=1782400000"), 1)
check_int("a deletion adds no row", count_of(root,
"SELECT COUNT(*) FROM elist_items WHERE id='never-here'"), 0)
check_int("the list is deleted", count_of(root,
"SELECT COUNT(*) FROM elist_lists WHERE id='gone-list' AND deleted_at=1782300000"), 1)
check_int("the session is deleted", count_of(root,
"SELECT COUNT(*) FROM sessions WHERE id='gone-session' AND deleted_at=1782300000"), 1)
check_int("records after the deletions apply", count_of(root,
"SELECT COUNT(*) FROM habits WHERE id='remote-habit' AND deleted_at=0"), 1)
}
test_snapshot_applies_content_free_deletions :: () {
remote_records("tombstone-remote", REMOTE_SQL)
add_tombstone_records()
seed: TextBuffer
append(*seed, LOCAL_SQL)
append(*seed, TOMBSTONE_LOCAL_SQL)
root := local_device("tombstone-merge", TextFromCString(*seed.bytes[0]), LOCAL_PENDING_SQL)
mark_pending_payload_in_flight()
check_true("snapshot with deletions applies", storage_apply_sync_response_json(
snapshot_response(70, "tombstone-receipt")) != 0)
check_tombstones_applied(root)
check_true("snapshot records its receipt", storage_meta_text("sync_last_server_state_hash") == "tombstone-receipt")
check_true("no review waits", storage_sync_review_pending() == 0)
storage_close()
}
// A waiting review of such a snapshot previews the deletions and merges.
test_review_with_content_free_deletions :: () {
remote_records("tombstone-review-remote", REMOTE_SQL)
add_tombstone_records()
seed: TextBuffer
append(*seed, LOCAL_SQL)
append(*seed, TOMBSTONE_LOCAL_SQL)
append(*seed, "UPDATE sessions SET activity=3;")
root := local_device("tombstone-review", TextFromCString(*seed.bytes[0]), "DELETE FROM sync_outbox;")
snapshot := snapshot_response(71, "tombstone-review-receipt")
check_true("snapshot with deletions is received", storage_apply_sync_response_json(snapshot) != 0)
check_true("snapshot waits for review", storage_sync_review_pending() != 0)
plan: *u8 = null
check_true("deletions preview", storage_sync_review_plan(2, *plan) != 0)
check_true("preview names a deleted item", contains(TextFromCString(plan), "Gone item"))
check_true("preview keeps a later item", !contains(TextFromCString(plan), "Kept item"))
sqlite_free(cast(*void)plan)
check_true("waiting review is showable", storage_sync_review_showable() != 0)
check_true("waiting review merges", storage_sync_review_merge_pending() != 0)
check_tombstones_applied(root)
check_true("merged review is cleared", storage_sync_review_pending() == 0)
storage_close()
}
// A snapshot that cannot be merged at all is a sync error, never a review
// with no preview and no working choice. One already waiting is set aside.
test_unmergeable_snapshot_never_waits :: () {
remote_records("unmergeable-remote", REMOTE_SQL)
seed: TextBuffer
append(*seed, LOCAL_SQL)
append(*seed, "UPDATE sessions SET activity=3;")
root := local_device("unmergeable-local", TextFromCString(*seed.bytes[0]), "DELETE FROM sync_outbox;")
unmergeable := "{\"protocol_version\":6,\"server_version\":72,\"server_state_hash\":\"unmergeable\",\"full_snapshot_required\":true,\"changes_complete\":false,\"changes\":{\"encrypted_records\":[{\"collection\":\"private.inbe.v1.sessions\",\"id\":\"bad\",\"key_id\":\"breathing-main-1\",\"nonce\":\"bad\",\"ciphertext\":\"bad\",\"updated_at\":\"2026-06-24T11:20:00Z\"}]}}"
check_true("unmergeable snapshot fails the sync", storage_apply_sync_response_json(unmergeable) == 0)
check_true("no review waits", storage_sync_review_pending() == 0)
check_int("local session kept", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='local-session' AND deleted_at=0"), 1)
check_true("save an unmergeable review", storage_sync_review_write_json(unmergeable) != 0)
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
check_true("unmergeable review does not merge", storage_sync_review_merge_pending() == 0)
check_true("unmergeable review is not shown", storage_sync_review_showable() == 0)
check_true("unmergeable review is set aside", storage_sync_review_pending() == 0)
check_int("setting it aside keeps local data", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='local-habit' AND deleted_at=0"), 1)
storage_close()
}
// A habit's questions and its entries reach another device. A habit
// record from an app before questions keeps them; an older entry does not
// replace a newer one, and a deletion reaches the other device.
@@ -2643,6 +2745,9 @@ Check :: () -> s32 {
test_review_completion_changes()
test_review_no_changes_resolves_automatically()
test_waiting_review_merges()
test_snapshot_applies_content_free_deletions()
test_review_with_content_free_deletions()
test_unmergeable_snapshot_never_waits()
test_questions_and_entries_sync()
test_kept_records_upload_kept()
test_app_choices_sync_between_devices()