fix(updater): preserve recovery consistency across interrupted updates

Keep health checks, rollback reporting, launcher recovery, and download failure handling consistent so verified updates cannot report false success or crash during recoverable I/O failures.
This commit is contained in:
OddTurtleHead committed 2026-11-25 23:51:16 +08:00
1 parent 6273185afa
commit cf1cc9fb94
9 files changed
+325 -46

No files matched your search

+34 -23
View File
@@ -28,8 +28,8 @@ code_missing() {
return 1
}
# A backup can only repair an install if it recorded a swap and still holds
# every entry needed to boot. Partial/legacy backups must be skipped.
# A backup can only repair a missing-code install if it is complete. Partial
# backups are still resumable when they belong to the active task.
backup_is_complete() {
[ -f "$1/.webspeak-swap.json" ] || return 1
for entry in $required_entries; do
@@ -52,6 +52,30 @@ task_backup_can_repair() {
return 0
}
state_file="$data_dir/updates/state.json"
task_id=""
task_status=""
if [ -f "$state_file" ]; then
task_id=$(sed -n 's/.*"id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$state_file" | head -n 1)
task_status=$(sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$state_file" | head -n 1)
fi
case "$task_id" in
""|*[!A-Za-z0-9_-]*) task_id="" ;;
esac
recovery_needed() {
if code_missing; then
return 0
fi
case "$task_status" in
downloading|verifying|extracting|staging|restarting)
[ -n "$task_id" ] && [ -d "$backups/$task_id" ] && [ -f "$backups/$task_id/.webspeak-swap.json" ]
return $?
;;
esac
return 1
}
restore_from_backup() {
backup="$1"
# Move whatever partial new code the interrupted swap left in the install
@@ -80,33 +104,20 @@ restore_from_backup() {
done
}
if code_missing; then
if recovery_needed; then
restore_backup=""
# The interrupted task is the source of truth: backups/<task.id> holds the
# exact version that was running before the swap. A successful earlier
# update leaves its backup behind (M1 does not clean it up), so picking the
# first or alphabetically smallest backup can resurrect a much older build.
state_file="$data_dir/updates/state.json"
task_id=""
if [ -f "$state_file" ]; then
task_id=$(sed -n 's/.*"id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$state_file" | head -n 1)
fi
# Task ids are generated as upd_<base36>_<hex>. Reject anything else so a
# tampered state.json cannot become a path traversal or a glob pattern.
case "$task_id" in
""|*[!A-Za-z0-9_-]*) task_id="" ;;
esac
if [ -n "$task_id" ]; then
# The interrupted task is the source of truth. Its backup may be partial
# while the old tree is being saved or while a previous recovery is still
# consuming it, but it must never be replaced by an older complete backup.
if [ -n "$task_id" ] && [ -d "$backups/$task_id" ]; then
candidate="$backups/$task_id"
if task_backup_can_repair "$candidate"; then
restore_backup="$candidate"
fi
fi
# No usable task backup (e.g. state.json was lost): use the most recently
# modified complete backup rather than whichever name happens to sort first.
if [ -z "$restore_backup" ]; then
else
# No current task backup (e.g. state.json was lost): use the most recently
# modified complete backup rather than whichever name happens to sort first.
for manifest in "$backups"/*/.webspeak-swap.json; do
[ -f "$manifest" ] || continue
backup="${manifest%/.webspeak-swap.json}"