fix(update): back up the task store the daemon actually uses, or refuse

configured_task_db_path() read the top-level db_path and handed that literal
file to the watchdog as ARCHIE_TASK_DB_PATH (scripts/archie-update-install).
The daemon's task store is not that file: taskDBPath() in
internal/app/archied/main.go derives `<db_path>-tasks.sqlite`, and the
configured path is commonly a zero-byte placeholder beside it. The updater
therefore backed up an empty file, and a rollback would have restored that
empty file over the real database and deleted its -wal/-shm -- destroying the
task store the backup exists to protect.

That awk also matched db_path in any section. [indexing] owns its own
database, so a config listing it before the top-level key handed the watchdog
an unrelated database as ARCHIE_TASK_DB_PATH and backed that up instead.

Resolve the top-level key only, append the sibling suffix the daemon uses, and
refuse the update when the backup cannot be taken. The backup is the only
route back from a forward schema migration, so an update that cannot take one
must not start; it now runs as pre-flight, ahead of the adapter refresh, so a
refusal changes nothing on the host.

The tests drive the real script with a config file the harness controls; all
three fail against the previous adapter.
This commit is contained in:
sam committed 2026-09-12 03:01:58 +00:00
1 parent 176533d181
commit dad9389530
2 files changed
+150 -10

No files matched your search

+32 -9
View File
@@ -43,9 +43,22 @@ configured_agent_image() {
printf '%s' "$image"
}
# The task store is `<db_path>-tasks.sqlite`, not db_path itself: the daemon
# derives it with taskDBPath() in internal/app/archied/main.go, and the
# configured path may be a zero-byte placeholder while the real store sits
# beside it. Backing up the literal value would produce an empty backup, and a
# rollback would then restore that empty file over the database and delete its
# -wal/-shm -- destroying the task store it exists to protect.
#
# Only the top-level key counts. db_path also appears in other sections
# ([indexing] owns its own database), and the first match in the file can
# easily be one of those.
configured_task_db_path() {
awk '
/^[[:space:]]*db_path[[:space:]]*=/ {
local base
base="$(awk '
BEGIN { top = 1 }
/^[[:space:]]*\[/ { top = 0 }
top && /^[[:space:]]*db_path[[:space:]]*=/ {
value = $0
sub(/^[[:space:]]*db_path[[:space:]]*=[[:space:]]*/, "", value)
sub(/[[:space:]]*(#.*)?$/, "", value)
@@ -53,7 +66,9 @@ configured_task_db_path() {
else if (length(value) >= 2 && substr(value, 1, 1) == sprintf("%c", 39) && substr(value, length(value), 1) == sprintf("%c", 39)) print substr(value, 2, length(value) - 2)
exit
}
' "$CONFIG_PATH" 2>/dev/null || true
' "$CONFIG_PATH" 2>/dev/null || true)"
[ -n "$base" ] || return 0
printf '%s' "$base-tasks.sqlite"
}
AGENT_IMAGE="${ARCHIE_AGENT_IMAGE:-$(configured_agent_image)}"
@@ -163,18 +178,26 @@ if [[ ",$components," == *,daemon,* ]]; then
fi
fi
# Pre-flight, before any write to the host. The task database backup is the
# watchdog's only route back from a forward schema migration, so an update
# that cannot take one must not start: once binaries are replaced, a failure
# leaves the previous release unable to read the store it just migrated. This
# runs ahead of the adapter refresh below so a refusal changes nothing at all.
if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_DB_PATH" ]; then
echo "==> backing up task database for schema-safe rollback"
mkdir -p "$(dirname "$TASK_DB_BACKUP")"
if ! sqlite3 "$TASK_DB_PATH" ".backup '$TASK_DB_BACKUP'"; then
echo "refusing to install $GW: could not back up the task database at $TASK_DB_PATH, so a failed update could not be rolled back" >&2
exit 1
fi
fi
# Refresh adapters from the approved release source. The running shell
# continues with this copy; subsequent updates use the current contract.
install -m755 "$source_dir/scripts/archie-update-install" "$BIN_DIR/archie-update-install"
install -m755 "$source_dir/scripts/archie-update-watchdog" "$BIN_DIR/archie-update-watchdog"
install -m755 "$source_dir/scripts/archie-update-check" "$BIN_DIR/archie-update-check"
if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_DB_PATH" ]; then
echo "==> backing up task database for schema-safe rollback"
mkdir -p "$(dirname "$TASK_DB_BACKUP")"
sqlite3 "$TASK_DB_PATH" ".backup '$TASK_DB_BACKUP'"
fi
# built accumulates only the binaries this release actually ships, so an
# update to a release predating one of these processes stays buildable.
built=""