Skip to content

Commit 7e49681

Browse files
committed
fix(mac): complete elevation transaction durability
1 parent 29a0d9e commit 7e49681

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎scripts/mac-elevation-host.sh‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ FINAL_RECEIPT_PATH=""
8787
PENDING_RECEIPT_PATH=""
8888
RECOVERY_PENDING_INSTALL=0
8989
PENDING_RECEIPT_CREATED=0
90+
PENDING_RECEIPT_RETIRE_ID=""
9091
EXPECTED_NODE_ID=""
9192
EXPECTED_NODE_PROFILE=""
9293
BEFORE_NODE_CONNECTED_AT=0
@@ -2557,8 +2558,7 @@ select_recovery_receipt() {
25572558
-f "$FINAL_RECEIPT_PATH" && ! -L "$FINAL_RECEIPT_PATH" ]] &&
25582559
(verify_install_receipt 1 && require_committed_install_receipt) >/dev/null 2>&1
25592560
then
2560-
remove_pending_receipt "$pending_transaction_id" ||
2561-
fail 'could not remove a completed pending install receipt'
2561+
PENDING_RECEIPT_RETIRE_ID="$pending_transaction_id"
25622562
RECEIPT_PATH="$FINAL_RECEIPT_PATH"
25632563
return 0
25642564
fi
@@ -2569,6 +2569,7 @@ select_recovery_receipt() {
25692569
recover_host() {
25702570
local current_app_valid=0 current_app_matches_receipt=0 current_receipt_sha plan_value plan_state plan_identity
25712571
local migration_identity recovery_helper_app
2572+
local pending_migration_identity_needs_record=0
25722573
if [[ ! -e "$APP_PATH" && ! -L "$APP_PATH" ]]; then
25732574
RECOVERY_CURRENT_APP_STATE="absent"
25742575
elif [[ -L "$APP_PATH" || ! -d "$APP_PATH" ]]; then
@@ -2678,8 +2679,7 @@ recover_host() {
26782679
fail 'could not bind the unchanged migration source to the pending transaction'
26792680
[[ "$migration_identity" == "$MIGRATION_CUSTODY_IDENTITY" ]] ||
26802681
fail 'pending migration source identity no longer matches the prepared transaction'
2681-
record_recovery_migration_identity "$migration_identity" ||
2682-
fail 'could not persist the pending migration source identity'
2682+
pending_migration_identity_needs_record=1
26832683
fi
26842684
if [[ -n "$migration_identity" ]]; then
26852685
[[ "$migration_identity" =~ ^[0-9A-F-]{36}:[0-9]+:[0-9]+$ ]] ||
@@ -2717,6 +2717,15 @@ recover_host() {
27172717
RECOVERY_CURRENT_APP_CDHASH_X86_64="$(jq -r '.cdhashes.x86_64' "$RECEIPT_PATH")"
27182718
prepare_current_app_rename_helper
27192719
fi
2720+
if [[ "$pending_migration_identity_needs_record" == "1" ]]; then
2721+
record_recovery_migration_identity "$migration_identity" ||
2722+
fail 'could not persist the pending migration source identity'
2723+
fi
2724+
if [[ -n "$PENDING_RECEIPT_RETIRE_ID" ]]; then
2725+
remove_pending_receipt "$PENDING_RECEIPT_RETIRE_ID" ||
2726+
fail 'could not remove a completed pending install receipt'
2727+
PENDING_RECEIPT_RETIRE_ID=""
2728+
fi
27202729
if [[ -n "$ROLLBACK_APP_PATH" ]]; then
27212730
local backup_generation="${ROLLBACK_APP_PATH#"$APP_PATH.rollback-elevation-host-"}"
27222731
if [[ "$INSTALL_RECEIPT_SCHEMA" == 'legacy' ]]; then

‎test/scripts/mac-elevation-host.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ function createArtifactVerificationHarness() {
532532
"printf '%s\\n' '<key>CFBundleShortVersionString</key><string>4.2.0</string>' '<key>CFBundleVersion</key><string>420</string>' '</dict></plist>' >>\"$app/Contents/Info.plist\"",
533533
"cat >\"$app/Contents/MacOS/OpenClaw\" <<'APP_HELPER'",
534534
"#!/bin/sh",
535+
'if [ "${1:-}" = "--elevation-sync-file" ] && [ "${TEST_KILL_AFTER_PENDING_RECEIPT:-0}" = "1" ] && [ ! -e "$TEST_PENDING_KILL_MARKER" ] && echo "${2:-}" | grep -q \'elevation-host-install[.]pending[.]json$\'; then',
536+
' : >"$TEST_PENDING_KILL_MARKER"',
537+
' kill -KILL "$PPID"',
538+
"fi",
535539
'if [ "${1:-}" = "--elevation-rename-exclusive" ]; then',
536540
' if [ "${TEST_DANGLING_ROLLBACK_DURING_MOVE:-0}" = "1" ] && echo "$3" | grep -q \'[.]rollback-elevation-host-\'; then',
537541
' ln -s /missing/openclaw-rollback-target "$3"',
@@ -701,6 +705,7 @@ function createInstallRollbackHarness(
701705
killDuringMigrationRestoreBootstrapOnce?: boolean;
702706
killAfterMigrationRestoreBootstrapOnce?: boolean;
703707
killAfterInitialMigrationCustody?: boolean;
708+
killAfterPendingReceipt?: boolean;
704709
killAfterRollbackAppCustody?: boolean;
705710
migrationRestoreBootstrapFails?: boolean;
706711
raceMigrationCustodyDestination?: boolean;
@@ -1022,6 +1027,8 @@ function createInstallRollbackHarness(
10221027
TEST_KILL_AFTER_INITIAL_MIGRATION_CUSTODY: options.killAfterInitialMigrationCustody
10231028
? "1"
10241029
: "0",
1030+
TEST_KILL_AFTER_PENDING_RECEIPT: options.killAfterPendingReceipt ? "1" : "0",
1031+
TEST_PENDING_KILL_MARKER: path.join(tempRoot, "pending-kill-marker"),
10251032
TEST_KILL_AFTER_ROLLBACK_APP_CUSTODY: options.killAfterRollbackAppCustody ? "1" : "0",
10261033
TEST_NODE_GENERATION_FILE: nodeGenerationFile,
10271034
TEST_RECOVERY_KILL_MARKER: path.join(tempRoot, "recovery-kill-marker"),
@@ -1879,6 +1886,54 @@ describe("mac elevation host command contract", () => {
18791886
},
18801887
);
18811888

1889+
it.skipIf(process.platform !== "darwin")(
1890+
"recovers on the first attempt when killed immediately after publishing the prepared receipt",
1891+
() => {
1892+
const harness = createInstallRollbackHarness({ killAfterPendingReceipt: true });
1893+
const oldBinary = readFileSync(path.join(harness.appPath, "Contents", "MacOS", "OpenClaw"));
1894+
const interrupted = runInstaller(
1895+
harness.installerPath,
1896+
[
1897+
"install",
1898+
"--archive",
1899+
harness.archivePath,
1900+
"--receipt",
1901+
harness.receiptPath,
1902+
...receiptDigestArgs(harness.receiptPath),
1903+
"--app",
1904+
harness.appPath,
1905+
"--migrate-launch-agent",
1906+
harness.sourcePlist,
1907+
],
1908+
harness.env,
1909+
);
1910+
expect(interrupted.signal).toBe("SIGKILL");
1911+
expect(readFileSync(harness.sourcePlist, "utf8")).toBe(harness.sourceContents);
1912+
1913+
const recovered = runInstaller(
1914+
harness.installerPath,
1915+
[
1916+
"recover",
1917+
"--archive",
1918+
harness.archivePath,
1919+
"--receipt",
1920+
harness.receiptPath,
1921+
...receiptDigestArgs(harness.receiptPath),
1922+
"--app",
1923+
harness.appPath,
1924+
"--state-dir",
1925+
harness.stateDir,
1926+
],
1927+
harness.env,
1928+
);
1929+
expect(recovered.status, recovered.stderr).toBe(0);
1930+
expect(readFileSync(path.join(harness.appPath, "Contents", "MacOS", "OpenClaw"))).toEqual(
1931+
oldBinary,
1932+
);
1933+
expect(readFileSync(harness.sourcePlist, "utf8")).toBe(harness.sourceContents);
1934+
},
1935+
);
1936+
18821937
it.skipIf(process.platform !== "darwin")(
18831938
"rejects a same-content migration source replacement after install process death",
18841939
() => {

0 commit comments

Comments
 (0)