267 lines
11 KiB
Ziran
267 lines
11 KiB
Ziran
#import, file "../src/app/app_sync.zi";
|
|
#import, file "../src/app/settings_store.zi";
|
|
#import, file "../src/app/sync_safety.zi";
|
|
#import, file "../src/app/sync_retry.zi";
|
|
#import, file "../src/app/app_types.zi";
|
|
#import, file "../src/app/assets.zi";
|
|
#import, file "../src/storage/sync_status.zi";
|
|
#import, file "../src/storage/sync_result.zi";
|
|
using SyncResult;
|
|
// The application imports the real background-audio implementation transitively.
|
|
// Do not also export the node-app-only audio stub in this program.
|
|
|
|
// Host state the C side provides: the clock, the account switch, and the
|
|
// queue the status key reads.
|
|
recovery_c :: #system_library "c";
|
|
recovery_set_time :: (seconds: float64) #foreign recovery_c;
|
|
recovery_set_enabled :: (enabled: s32) #foreign recovery_c;
|
|
recovery_set_queue :: (queued: s64, repair: s32, full_upload: s32) #foreign recovery_c;
|
|
recovery_status_reads :: () -> s32 #foreign recovery_c;
|
|
recovery_set_url :: (valid: bool) #foreign recovery_c;
|
|
Notification :: #import "kryon/notification";
|
|
|
|
// Worker collection must be callable without a desktop or a real profile.
|
|
#program_export "NotificationAllowedHost"
|
|
RecoveryNotificationsAllowed :: () -> bool {
|
|
return false
|
|
}
|
|
|
|
#program_export "NotificationWindowFocusedHost"
|
|
RecoveryWindowFocused :: () -> bool {
|
|
return true
|
|
}
|
|
|
|
#program_export "NotificationSendHost"
|
|
RecoverySendNotification :: (props: Notification.NotificationProps) -> bool {
|
|
unused props
|
|
return false
|
|
}
|
|
|
|
#program_export "app_web_platform"
|
|
RecoveryWebPlatform :: () -> s32 {
|
|
return 0
|
|
}
|
|
|
|
#program_export "asset_entry_at"
|
|
RecoveryAssetAt :: (index: usize) -> *EmbeddedAssetEntry {
|
|
unused index
|
|
return null
|
|
}
|
|
|
|
#program_export "asset_entry_total"
|
|
RecoveryAssetCount :: () -> usize {
|
|
return 0
|
|
}
|
|
|
|
#program_export "storage_set_social_cache_json"
|
|
RecoverySaveSocialCache :: (kind: string, json: string) -> s32 {
|
|
unused kind
|
|
unused json
|
|
return 0
|
|
}
|
|
|
|
#program_export "storage_get_social_cache_json"
|
|
RecoveryLoadSocialCache :: (kind: string, output: *u8, capacity: s64) -> s32 {
|
|
unused kind
|
|
if output != null && capacity > 0 {
|
|
output[0] = cast(u8)0
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// The UI's full state is larger than a native thread stack. Keep this
|
|
// disposable test profile in static storage, as the application does.
|
|
app: InnerBreeze;
|
|
|
|
Retry :: (attempt: s32) -> bool {
|
|
return SettingsStoreInt("sync_retry_attempt", attempt)
|
|
}
|
|
|
|
Attempt :: () -> s32 {
|
|
return SettingsReadInt("sync_retry_attempt", 0)
|
|
}
|
|
|
|
// Zero when every check holds; otherwise the number of the first that fails.
|
|
#program_export
|
|
Answer :: () -> s32 {
|
|
least: s32 = -2147483647 - 1
|
|
most: s32 = 2147483647
|
|
now: float64 = 100.0
|
|
recovery_set_time(now)
|
|
recovery_set_enabled(1)
|
|
|
|
// Corrupted persisted attempt values must clamp, never overflow.
|
|
attempts: [9]s32 = .[least, -1, 0, 1, 2, 3, 4, 5, most]
|
|
expected_attempts: [9]s32 = .[1, 1, 1, 2, 3, 4, 4, 4, 4]
|
|
expected_delays: [9]s32 = .[5, 5, 5, 15, 30, 60, 60, 60, 60]
|
|
index: s32 = 0
|
|
while index < 9 {
|
|
result: s32 = 0
|
|
while result <= 7 {
|
|
unused Retry(attempts[index])
|
|
app_sync_record_result(result)
|
|
if result == 0 {
|
|
if Attempt() != 0 || g_sync.retry_at != 0.0 { return 3 }
|
|
} else if result == 3 || result == 4 || result == 6 || result == 7 {
|
|
if Attempt() != expected_attempts[index] { return 4 }
|
|
if g_sync.retry_at != now + cast(float64)expected_delays[index] { return 5 }
|
|
} else {
|
|
if Attempt() != attempts[index] { return 6 }
|
|
if g_sync.retry_at != 0.0 { return 7 }
|
|
}
|
|
result += 1
|
|
}
|
|
index += 1
|
|
}
|
|
app_sync_record_result(least)
|
|
if g_sync.retry_at != 0.0 || app_sync_failure_needs_action(least) == 0 { return 8 }
|
|
app_sync_record_result(most)
|
|
if g_sync.retry_at != 0.0 || app_sync_failure_needs_action(most) == 0 { return 9 }
|
|
if app_sync_retry_delay(least) != 5.0 { return 10 }
|
|
if app_sync_retry_delay(most) != 60.0 { return 11 }
|
|
|
|
// Social retries keep their own attempt counter and refresh flag.
|
|
g_sync.social_retry_attempt = most
|
|
app_social_record_result(6)
|
|
if g_sync.social_retry_attempt != 4 { return 12 }
|
|
if g_sync.social_retry_at != now + 60.0 { return 13 }
|
|
// A rejected sign-in retries too; an actionable failure stops.
|
|
app_social_record_result(7)
|
|
if g_sync.social_retry_at != now + 60.0 { return 14 }
|
|
if g_sync.social_refresh_pending != 1 { return 15 }
|
|
app_social_record_result(5)
|
|
if g_sync.social_retry_at != 0.0 || g_sync.social_refresh_pending != 0 { return 16 }
|
|
app_social_record_result(0)
|
|
if g_sync.social_retry_attempt != 0 { return 17 }
|
|
|
|
// Repeated request failures back off 5, 15, 30, 60, 60 seconds.
|
|
unused Retry(0)
|
|
delays: [5]float64 = .[5.0, 15.0, 30.0, 60.0, 60.0]
|
|
index = 0
|
|
while index < 5 {
|
|
app_sync_record_result(6)
|
|
if g_sync.retry_at != now + delays[index] { return 18 }
|
|
if app_sync_status_key() != "sync_retry_waiting" { return 19 }
|
|
now = g_sync.retry_at
|
|
recovery_set_time(now)
|
|
index += 1
|
|
}
|
|
// A rejected sign-in keeps retrying and still says why (owner, 2026-10-04).
|
|
app_sync_record_result(7)
|
|
if g_sync.retry_at != now + 60.0 { return 20 }
|
|
if app_sync_failure_needs_action(SettingsReadInt("sync_last_result", 0)) != 0 { return 21 }
|
|
if app_sync_status_key() != "sync_auth_failed" { return 22 }
|
|
// A failure the person must fix still stops automatic retry.
|
|
app_sync_record_result(5)
|
|
if g_sync.retry_at != 0.0 || app_sync_failure_needs_action(SettingsReadInt("sync_last_result", 0)) == 0 { return 28 }
|
|
if app_sync_status_key() != "sync_sign_failed" { return 29 }
|
|
app_sync_record_result(0)
|
|
if Attempt() != 0 || g_sync.retry_at != 0.0 { return 23 }
|
|
|
|
// The status line follows the account, the running sync, and the queue.
|
|
recovery_set_queue(4745, 1, 0)
|
|
recovery_set_enabled(0)
|
|
if app_sync_status_key() != "sync_connection_required" { return 24 }
|
|
recovery_set_enabled(1)
|
|
g_sync.sync_running = 1
|
|
if app_sync_status_key() != "sync_in_progress" { return 25 }
|
|
g_sync.sync_running = 0
|
|
if app_sync_status_key() != "sync_status_local_queued" { return 26 }
|
|
recovery_set_queue(0, 1, 1)
|
|
if app_sync_status_key() != "sync_status_ready" { return 27 }
|
|
|
|
// A payload that could not be built retries on its schedule, Sync now
|
|
// tries at once, and the old failure is not reported as a success.
|
|
app_sync_record_result(SYNC_STORAGE_FULL)
|
|
if !g_sync.retry_requested || !app_sync_retry_ready(SYNC_STORAGE_FULL, now) { return 60 }
|
|
g_sync.retry_requested = false
|
|
app_sync_record_result(SYNC_STORAGE_FULL)
|
|
if g_sync.retry_requested || app_sync_retry_ready(SYNC_STORAGE_FULL, now) { return 61 }
|
|
if app_auto_sync(*app) == 0 || !g_sync.retry_requested { return 62 }
|
|
g_sync.retry_requested = false
|
|
app_sync_record_result(SYNC_STORAGE_FULL)
|
|
if !g_sync.retry_requested { return 63 }
|
|
g_sync.retry_requested = false
|
|
app_sync_record_result(SYNC_OK)
|
|
app_sync_record_result(3)
|
|
if app_sync_retry_ready(3, now) { return 30 }
|
|
if app_auto_sync(*app) == 0 || g_sync.retry_requested { return 31 }
|
|
if app_sync_retry_ready(3, now + 1.0) { return 32 }
|
|
if app_request_sync(*app) == 0 || !g_sync.retry_requested { return 33 }
|
|
if !app_sync_retry_ready(3, now) { return 34 }
|
|
if SettingsReadInt("sync_last_result", 0) != 3 { return 35 }
|
|
if app_sync_status_key() != "sync_failed" { return 36 }
|
|
// Dispatch consumes that single request; another payload failure waits
|
|
// for its next automatic try.
|
|
g_sync.retry_requested = false
|
|
app_sync_record_result(3)
|
|
if app_sync_retry_ready(3, now) || !app_sync_retry_ready(3, now + 1000.0) { return 37 }
|
|
app_sync_record_result(6)
|
|
if app_sync_retry_ready(6, now) { return 38 }
|
|
if app_request_sync(*app) == 0 || !app_sync_retry_ready(6, now) { return 39 }
|
|
g_sync.retry_requested = false
|
|
recovery_set_enabled(0)
|
|
if app_request_sync(*app) != 0 || g_sync.retry_requested { return 40 }
|
|
recovery_set_enabled(1)
|
|
recovery_set_queue(1, 0, 1)
|
|
status: SyncStatus
|
|
if !app_sync_queue_status(*status, now) || status.queued_changes != 1 { return 41 }
|
|
reads := recovery_status_reads()
|
|
for frame: cast(s32)0..cast(s32)119 {
|
|
if !app_sync_queue_status(*status, now) { return 42 }
|
|
}
|
|
if recovery_status_reads() != reads { return 43 }
|
|
recovery_set_queue(2, 0, 1)
|
|
if !app_sync_queue_status(*status, now + 0.26) || status.queued_changes != 2 ||
|
|
recovery_status_reads() != reads + 1 { return 44 }
|
|
// A data revision, including disconnect, invalidates immediately.
|
|
recovery_set_enabled(0)
|
|
if !app_sync_queue_status(*status, now + 0.27) || status.enabled != 0 ||
|
|
recovery_status_reads() != reads + 2 { return 45 }
|
|
recovery_set_enabled(1)
|
|
g_sync.sync_running = 1
|
|
g_sync.retry_requested = true
|
|
app_sync_start_failed(3)
|
|
if g_sync.sync_running != 0 || g_sync.retry_requested ||
|
|
SettingsReadInt("sync_last_result", 0) != 3 ||
|
|
app_sync_retry_ready(3, now) { return 46 }
|
|
g_sync.retry_requested = true
|
|
app_sync_start_failed(5)
|
|
if g_sync.retry_requested || app_sync_status_key() != "sync_sign_failed" { return 47 }
|
|
app.ui.pointer_active = true
|
|
app_sync_record_input(*app, now)
|
|
if app_sync_user_idle(*app, now + 2.0) != 0 { return 48 }
|
|
app.ui.pointer_active = false
|
|
if app_sync_user_idle(*app, now + 1.49) != 0 ||
|
|
app_sync_user_idle(*app, now + 1.5) == 0 { return 49 }
|
|
app.ui.keyboard_active = true
|
|
app_sync_record_input(*app, now + 3.0)
|
|
app.ui.keyboard_active = false
|
|
if app_sync_user_idle(*app, now + 4.0) != 0 { return 50 }
|
|
recovery_set_url(false)
|
|
if app_sync_status_key() != "sync_server_url_invalid" { return 51 }
|
|
recovery_set_url(true)
|
|
// Restoring waits for both native workers before storage can close.
|
|
g_sync.sync_running = 1
|
|
if app_sync_stop_for_restore(*app) || !sync_paused_for_restore {
|
|
return 64
|
|
}
|
|
g_sync.sync_running = 0
|
|
g_sync.social_refresh_running = 1
|
|
if app_sync_stop_for_restore(*app) || !sync_paused_for_restore {
|
|
return 65
|
|
}
|
|
g_sync.social_refresh_running = 0
|
|
g_sync.retry_requested = true
|
|
g_sync.social_refresh_pending = 1
|
|
if !app_sync_stop_for_restore(*app) || !sync_paused_for_restore ||
|
|
g_sync.retry_requested || g_sync.social_refresh_pending != 0 {
|
|
return 66
|
|
}
|
|
app_sync_resume_after_restore()
|
|
if sync_paused_for_restore || app_request_sync(*app) == 0 || !g_sync.retry_requested {
|
|
return 67
|
|
}
|
|
return 0
|
|
}
|