@adea-ai/app-framework 0.31.0 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +33 -15
  2. package/dist/app/create-application.js +4 -3
  3. package/dist/app/serial-queue.d.ts +5 -0
  4. package/dist/app/serial-queue.js +14 -0
  5. package/dist/app/value-guards.d.ts +15 -0
  6. package/dist/app/value-guards.js +29 -0
  7. package/dist/ci/toolchain-identity.js +1 -1
  8. package/dist/desktop/channel-proof.js +2 -1
  9. package/dist/desktop/client-asset-stream.js +6 -12
  10. package/dist/desktop/desktop-host.d.ts +25 -5
  11. package/dist/desktop/desktop-host.js +75 -20
  12. package/dist/framework-source.json +1 -1
  13. package/dist/installer/command-artifact.js +4 -3
  14. package/dist/installer/command-qualification-command.js +6 -5
  15. package/dist/installer/hosted-runner.d.ts +9 -2
  16. package/dist/installer/hosted-runner.js +18 -5
  17. package/dist/installer/installed-payload.js +20 -46
  18. package/dist/installer/installer-archive.d.ts +33 -0
  19. package/dist/installer/installer-archive.js +116 -0
  20. package/dist/installer/installer-ci-bootstrap.js +10 -6
  21. package/dist/installer/installer-extraction.js +54 -120
  22. package/dist/installer/installer-identity.js +33 -111
  23. package/dist/installer/installer-observation.js +3 -2
  24. package/dist/installer/installer-process-cleanup.js +7 -46
  25. package/dist/installer/installer-qualification-command.js +6 -5
  26. package/dist/installer/installer-upgrade-command.js +5 -5
  27. package/dist/installer/installer-worker-guard.js +2 -1
  28. package/dist/native/canonical-path.d.ts +5 -0
  29. package/dist/native/canonical-path.js +10 -0
  30. package/dist/native/cef-helper.js +5 -4
  31. package/dist/native/electrobun-source-patch.js +3 -2
  32. package/dist/native/{directory-exchange.d.ts → native-rename.d.ts} +2 -0
  33. package/dist/native/native-rename.js +126 -0
  34. package/dist/native/native-secret-store.js +2 -6
  35. package/dist/native/native-utilities.js +4 -4
  36. package/dist/native/posix-launcher-build.js +1 -1
  37. package/dist/native/posix-tree-removal.js +2 -2
  38. package/dist/native/windows-process.d.ts +9 -0
  39. package/dist/native/windows-process.js +60 -0
  40. package/dist/release/feed-release-cut.js +2 -1
  41. package/dist/release/framework-package-publish.js +1 -1
  42. package/dist/release/framework-update-lock.js +2 -1
  43. package/dist/release/github-cli.d.ts +18 -0
  44. package/dist/release/github-cli.js +13 -1
  45. package/dist/release/github-release-cut.js +3 -2
  46. package/dist/release/github-release-provenance.js +2 -1
  47. package/dist/release/github-release-publisher.js +2 -1
  48. package/dist/release/local-provenance.js +3 -2
  49. package/dist/release/local-release.js +6 -6
  50. package/dist/release/qualification-run.d.ts +25 -0
  51. package/dist/release/qualification-run.js +43 -0
  52. package/dist/release/release-artifact.js +3 -11
  53. package/dist/release/release-assets-plan.js +16 -6
  54. package/dist/release/release-config.js +14 -14
  55. package/dist/release/release-cut-command.js +14 -4
  56. package/dist/release/release-cut.js +3 -2
  57. package/dist/release/release-descriptor-staging.js +13 -27
  58. package/dist/release/release-finalize.js +2 -1
  59. package/dist/release/release-publication.js +3 -11
  60. package/dist/release/release-publish-command.js +15 -6
  61. package/dist/release/release-qualification.js +3 -2
  62. package/dist/release/retained-release-build.js +12 -12
  63. package/dist/update-helper.bundle.js +250 -178
  64. package/dist/updates/desktop-updater.js +53 -58
  65. package/dist/updates/packaged-launch-admission.js +3 -2
  66. package/dist/updates/packaged-release-config.js +6 -5
  67. package/dist/updates/packaged-update-startup.d.ts +25 -5
  68. package/dist/updates/packaged-update-startup.js +30 -5
  69. package/dist/updates/release-feed.js +9 -9
  70. package/dist/updates/signed-release.js +5 -5
  71. package/dist/updates/stable-file.d.ts +18 -0
  72. package/dist/updates/stable-file.js +65 -2
  73. package/dist/updates/update-adapter.d.ts +2 -13
  74. package/dist/updates/update-arm.js +13 -13
  75. package/dist/updates/update-channel-store.js +7 -9
  76. package/dist/updates/update-discovery.js +30 -34
  77. package/dist/updates/update-download-cleanup.js +2 -1
  78. package/dist/updates/update-download.js +2 -4
  79. package/dist/updates/update-extraction.js +1 -1
  80. package/dist/updates/update-generation-lease.js +4 -3
  81. package/dist/updates/update-handoff-plan.js +3 -2
  82. package/dist/updates/update-helper.js +8 -6
  83. package/dist/updates/update-instance-lease.js +3 -2
  84. package/dist/updates/update-launch-gate.js +12 -25
  85. package/dist/updates/update-launch.js +3 -2
  86. package/dist/updates/update-lock.js +3 -2
  87. package/dist/updates/update-marker.js +3 -2
  88. package/dist/updates/update-process-fence.js +10 -39
  89. package/dist/updates/update-process-liveness.js +2 -1
  90. package/dist/updates/update-resume.js +30 -27
  91. package/dist/updates/update-retention-marker.js +7 -5
  92. package/dist/updates/update-retention-plan.js +2 -2
  93. package/dist/updates/update-retirement.js +52 -52
  94. package/dist/updates/update-state-store.d.ts +2 -0
  95. package/dist/updates/update-state-store.js +34 -38
  96. package/dist/updates/update-state.d.ts +8 -3
  97. package/dist/updates/update-swap-stage.js +6 -5
  98. package/dist/updates/update-swap.d.ts +1 -1
  99. package/dist/updates/update-swap.js +8 -6
  100. package/package.json +4 -12
  101. package/src/desktop/client-asset-stream.ts +6 -12
  102. package/src/updates/update-adapter.ts +2 -14
  103. package/src/updates/update-state.ts +11 -2
  104. package/templates/application/src/bun/index.ts +13 -9
  105. package/dist/native/directory-exchange.js +0 -90
  106. package/dist/native/exclusive-rename.d.ts +0 -2
  107. package/dist/native/exclusive-rename.js +0 -52
  108. package/dist/release/retention-dry-run.d.ts +0 -46
  109. package/dist/release/retention-dry-run.js +0 -94
  110. package/dist/updates/update-exclusive-rename.d.ts +0 -3
  111. package/dist/updates/update-exclusive-rename.js +0 -5
@@ -1,5 +1,8 @@
1
+ import { errorCode, isUuid } from "../app/value-guards.js";
2
+ import { isCanonicalPath } from "../native/canonical-path.js";
3
+ import { renameExclusive } from "../native/native-rename.js";
1
4
  import { verifyReleaseDescriptor } from "./signed-release.js";
2
- import { renameUpdateExclusive } from "./update-exclusive-rename.js";
5
+ import { readSmallStateFile } from "./stable-file.js";
3
6
  import { readUpdateStageState, withUpdateLock, writeUpdateStageState } from "./update-state-store.js";
4
7
  import { readUpdateTreeDigest, readUpdateTreeIdentity, readUpdateTreeVersion, sameUpdateTreeDigest, sameUpdateTreeIdentity, updateStagingFor } from "./update-swap.js";
5
8
  import { readUpdateLaunchGate, tryAcquireUpdateHelperLiveness } from "./update-launch-gate.js";
@@ -11,10 +14,10 @@ import { updateProcessMayBeAlive } from "./update-process-liveness.js";
11
14
  import { planUpdateRetention, retentionIdentityKey } from "./update-retention-plan.js";
12
15
  import { publishUpdateRetentionIntent, readUpdateRetentionIntent, readUpdateRetentionIntentFile, sameRetentionIntent, updateRetentionIntentPath } from "./update-retention-marker.js";
13
16
  import { syncUpdateRetentionIntent } from "./update-retention-sync.js";
14
- import { link, lstat, mkdir, open, readFile, readdir, rename, rm, rmdir, unlink, writeFile } from "node:fs/promises";
15
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
16
- import { constants as constants$1 } from "node:fs";
17
+ import { link, lstat, mkdir, open, readdir, rename, rm, rmdir, unlink, writeFile } from "node:fs/promises";
18
+ import { basename, dirname, join } from "node:path";
17
19
  import { randomUUID } from "node:crypto";
20
+ import { constants as constants$1 } from "node:fs";
18
21
  // src/updates/update-retirement.ts
19
22
  /** A failed promotion may have left the final marker in place; inspect before retrying. */
20
23
  var UpdateRetentionOrphanPromotionError = class extends Error {
@@ -30,7 +33,6 @@ var UpdateRetentionOrphanPromotionError = class extends Error {
30
33
  }
31
34
  };
32
35
  var VERSION = /^\d+\.\d+\.\d+(?:-dev\.\d+)?$/;
33
- var UUID = /^[0-9a-f]{8}-[0-9a-f-]{27}$/i;
34
36
  var PUBLISHER_TEMP_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
35
37
  function hasExactOwnKeys(value, expected) {
36
38
  const actual = Reflect.ownKeys(value);
@@ -38,7 +40,7 @@ function hasExactOwnKeys(value, expected) {
38
40
  }
39
41
  /** Generation-specific retirement paths, all beside the fixed staging path. */
40
42
  function updateRetirementPaths(target, generation) {
41
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || !UUID.test(generation)) throw new Error("Invalid update retirement identity");
43
+ if (!isCanonicalPath(target) || !isUuid(generation)) throw new Error("Invalid update retirement identity");
42
44
  const prefix = `.${basename(target)}.framework-retired-${generation}`;
43
45
  const container = join(dirname(target), prefix);
44
46
  return Object.freeze({
@@ -60,7 +62,7 @@ function isPruneProgressName(name, target) {
60
62
  if (!name.startsWith(prefix)) return false;
61
63
  const suffix = name.slice(prefix.length);
62
64
  const match = /^([0-9a-f-]{36})\.prune-progress\.json$/i.exec(suffix);
63
- return Boolean(match && UUID.test(match[1]));
65
+ return Boolean(match && isUuid(match[1]));
64
66
  }
65
67
  function pruneProgressNames(target, generation) {
66
68
  return [basename(retentionPruneProgressPath(target, generation))];
@@ -85,7 +87,7 @@ async function findPruneProgressTemporaryName(target, generation) {
85
87
  const name = names[0];
86
88
  if (!name) return void 0;
87
89
  const temporaryId = name.slice(prefix.length, -4);
88
- if (!UUID.test(temporaryId)) throw new Error("An update retention progress temporary requires recovery");
90
+ if (!isUuid(temporaryId)) throw new Error("An update retention progress temporary requires recovery");
89
91
  return name;
90
92
  }
91
93
  async function readPruneProgressFile(path, target, generation, expectedIntent, expectedOwner) {
@@ -94,10 +96,11 @@ async function readPruneProgressFile(path, target, generation, expectedIntent, e
94
96
  const canonicalName = basename(canonicalPath);
95
97
  const pathName = basename(path);
96
98
  const temporaryId = pathName.startsWith(`${canonicalName}.`) && pathName.endsWith(".tmp") ? pathName.slice(canonicalName.length + 1, -4) : "";
97
- const canonicalTemporary = dirname(path) === dirname(canonicalPath) && UUID.test(temporaryId);
98
- const info = await lstat(path);
99
- if (path !== canonicalPath && !canonicalTemporary || !info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 8192) return void 0;
100
- const value = JSON.parse(await readFile(path, "utf8"));
99
+ const canonicalTemporary = dirname(path) === dirname(canonicalPath) && isUuid(temporaryId);
100
+ if (path !== canonicalPath && !canonicalTemporary) return void 0;
101
+ const text = await readSmallStateFile(path, 8192, "Invalid retention prune progress");
102
+ if (text === void 0) return void 0;
103
+ const value = JSON.parse(text);
101
104
  if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
102
105
  const record = value;
103
106
  if (!hasExactOwnKeys(record, [
@@ -116,7 +119,7 @@ async function readPruneProgressFile(path, target, generation, expectedIntent, e
116
119
  "installedTreeIdentity"
117
120
  ]) || expectedIntent !== void 0 && !sameRetentionIntent(record.intent, expectedIntent) || typeof record.owner !== "object" || record.owner === null || Array.isArray(record.owner)) return void 0;
118
121
  const intent = record.intent;
119
- if (intent.schema !== 1 || intent.phase !== "prune-previous-tree" || typeof intent.target !== "string" || intent.target !== target || typeof intent.generation !== "string" || intent.generation !== generation.toLowerCase() || !retentionIdentityKey(intent.previousTreeIdentity) || !retentionIdentityKey(intent.installedTreeIdentity) || typeof intent.installedGeneration !== "string" || !UUID.test(intent.installedGeneration) || Object.keys(intent.previousTreeIdentity).length !== 2 || !hasExactOwnKeys(intent.previousTreeIdentity, ["device", "inode"]) || Object.keys(intent.installedTreeIdentity).length !== 2 || !hasExactOwnKeys(intent.installedTreeIdentity, ["device", "inode"])) return void 0;
122
+ if (intent.schema !== 1 || intent.phase !== "prune-previous-tree" || typeof intent.target !== "string" || intent.target !== target || typeof intent.generation !== "string" || intent.generation !== generation.toLowerCase() || !retentionIdentityKey(intent.previousTreeIdentity) || !retentionIdentityKey(intent.installedTreeIdentity) || typeof intent.installedGeneration !== "string" || !isUuid(intent.installedGeneration) || Object.keys(intent.previousTreeIdentity).length !== 2 || !hasExactOwnKeys(intent.previousTreeIdentity, ["device", "inode"]) || Object.keys(intent.installedTreeIdentity).length !== 2 || !hasExactOwnKeys(intent.installedTreeIdentity, ["device", "inode"])) return void 0;
120
123
  const owner = parseOwner(record.owner);
121
124
  if (!owner || expectedOwner && !ownerMatches(owner, expectedOwner)) return void 0;
122
125
  const healthReceipt = parseHealthReceipt(record.healthReceipt);
@@ -283,7 +286,7 @@ function parseOwner(value) {
283
286
  const record = value;
284
287
  const identity = record.previousTreeIdentity;
285
288
  const newTreeIdentity = record.newTreeIdentity;
286
- if (record.schema !== 2 || typeof record.generation !== "string" || !UUID.test(record.generation) || typeof record.target !== "string" || !isAbsolute(record.target) || resolve(record.target) !== record.target || typeof record.newVersion !== "string" || !VERSION.test(record.newVersion) || typeof record.previousVersion !== "string" || !VERSION.test(record.previousVersion) || typeof identity !== "object" || identity === null || Array.isArray(identity) || !hasExactOwnKeys(identity, ["device", "inode"]) || typeof identity.device !== "string" || !/^[1-9]\d{0,19}$/.test(identity.device) || typeof identity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(identity.inode) || typeof newTreeIdentity !== "object" || newTreeIdentity === null || Array.isArray(newTreeIdentity) || !hasExactOwnKeys(newTreeIdentity, ["device", "inode"]) || typeof newTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(newTreeIdentity.device) || typeof newTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(newTreeIdentity.inode) || typeof record.container !== "string" || record.container !== `.${basename(record.target)}.framework-retired-${record.generation}` || record.tree !== "tree" || !hasExactOwnKeys(record, [
289
+ if (record.schema !== 2 || typeof record.generation !== "string" || !isUuid(record.generation) || !isCanonicalPath(record.target) || typeof record.newVersion !== "string" || !VERSION.test(record.newVersion) || typeof record.previousVersion !== "string" || !VERSION.test(record.previousVersion) || typeof identity !== "object" || identity === null || Array.isArray(identity) || !hasExactOwnKeys(identity, ["device", "inode"]) || typeof identity.device !== "string" || !/^[1-9]\d{0,19}$/.test(identity.device) || typeof identity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(identity.inode) || typeof newTreeIdentity !== "object" || newTreeIdentity === null || Array.isArray(newTreeIdentity) || !hasExactOwnKeys(newTreeIdentity, ["device", "inode"]) || typeof newTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(newTreeIdentity.device) || typeof newTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(newTreeIdentity.inode) || typeof record.container !== "string" || record.container !== `.${basename(record.target)}.framework-retired-${record.generation}` || record.tree !== "tree" || !hasExactOwnKeys(record, [
287
290
  "schema",
288
291
  "generation",
289
292
  "target",
@@ -316,14 +319,13 @@ function cleanupMarkerMatches(actual, expected) {
316
319
  function parseCleanupMarker(value) {
317
320
  if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
318
321
  const record = value;
319
- if (record.schema !== 1 || typeof record.generation !== "string" || !UUID.test(record.generation) || typeof record.target !== "string" || !isAbsolute(record.target) || resolve(record.target) !== record.target || typeof record.version !== "string" || !VERSION.test(record.version) || typeof record.claim !== "string" || record.claim !== basename(updateRetirementPaths(record.target, record.generation).claim) || record.phase !== "staging-cleanup" || Object.keys(record).length !== 6) return void 0;
322
+ if (record.schema !== 1 || typeof record.generation !== "string" || !isUuid(record.generation) || !isCanonicalPath(record.target) || typeof record.version !== "string" || !VERSION.test(record.version) || typeof record.claim !== "string" || record.claim !== basename(updateRetirementPaths(record.target, record.generation).claim) || record.phase !== "staging-cleanup" || Object.keys(record).length !== 6) return void 0;
320
323
  return record;
321
324
  }
322
325
  async function readOwner(path) {
323
326
  try {
324
- const info = await lstat(path);
325
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096) return void 0;
326
- return parseOwner(JSON.parse(await readFile(path, "utf8")));
327
+ const text = await readSmallStateFile(path, 4096, "Invalid update retirement record");
328
+ return text === void 0 ? void 0 : parseOwner(JSON.parse(text));
327
329
  } catch {
328
330
  return;
329
331
  }
@@ -332,7 +334,7 @@ function parseHealthReceipt(value) {
332
334
  if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
333
335
  const record = value;
334
336
  const identity = record.treeIdentity;
335
- if (record.schema !== 1 || typeof record.generation !== "string" || !UUID.test(record.generation) || typeof record.target !== "string" || !isAbsolute(record.target) || resolve(record.target) !== record.target || typeof record.version !== "string" || !VERSION.test(record.version) || typeof identity !== "object" || identity === null || Array.isArray(identity) || !hasExactOwnKeys(identity, ["device", "inode"]) || typeof identity.device !== "string" || !/^[1-9]\d{0,19}$/.test(identity.device) || typeof identity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(identity.inode) || !hasExactOwnKeys(record, [
337
+ if (record.schema !== 1 || typeof record.generation !== "string" || !isUuid(record.generation) || !isCanonicalPath(record.target) || typeof record.version !== "string" || !VERSION.test(record.version) || typeof identity !== "object" || identity === null || Array.isArray(identity) || !hasExactOwnKeys(identity, ["device", "inode"]) || typeof identity.device !== "string" || !/^[1-9]\d{0,19}$/.test(identity.device) || typeof identity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(identity.inode) || !hasExactOwnKeys(record, [
336
338
  "schema",
337
339
  "generation",
338
340
  "target",
@@ -343,9 +345,8 @@ function parseHealthReceipt(value) {
343
345
  }
344
346
  async function readHealthReceipt(path) {
345
347
  try {
346
- const info = await lstat(path);
347
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096) return void 0;
348
- return parseHealthReceipt(JSON.parse(await readFile(path, "utf8")));
348
+ const text = await readSmallStateFile(path, 4096, "Invalid update retirement record");
349
+ return text === void 0 ? void 0 : parseHealthReceipt(JSON.parse(text));
349
350
  } catch {
350
351
  return;
351
352
  }
@@ -364,9 +365,8 @@ function healthReceiptMatches(actual, expected) {
364
365
  }
365
366
  async function readCleanupMarker(path) {
366
367
  try {
367
- const info = await lstat(path);
368
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096) return void 0;
369
- return parseCleanupMarker(JSON.parse(await readFile(path, "utf8")));
368
+ const text = await readSmallStateFile(path, 4096, "Invalid update retirement record");
369
+ return text === void 0 ? void 0 : parseCleanupMarker(JSON.parse(text));
370
370
  } catch {
371
371
  return;
372
372
  }
@@ -375,7 +375,7 @@ async function pathInfo(path) {
375
375
  try {
376
376
  return await lstat(path);
377
377
  } catch (error) {
378
- if (error.code === "ENOENT") return void 0;
378
+ if (errorCode(error) === "ENOENT") return void 0;
379
379
  throw error;
380
380
  }
381
381
  }
@@ -390,7 +390,7 @@ async function matchesOldTree(path, state) {
390
390
  }
391
391
  function isMatchingClaimTemporary(name, paths) {
392
392
  if (!name.startsWith(paths.claimTemporaryPrefix) || !name.endsWith(".tmp")) return false;
393
- return UUID.test(name.slice(paths.claimTemporaryPrefix.length, -4));
393
+ return isUuid(name.slice(paths.claimTemporaryPrefix.length, -4));
394
394
  }
395
395
  /**
396
396
  * Publish the generation claim from a complete private temporary using an
@@ -415,7 +415,7 @@ async function ensureClaim(target, state) {
415
415
  if (!info || !info.isFile() || info.isSymbolicLink()) continue;
416
416
  if (!ownerMatches(await readOwner(temporary), expected)) continue;
417
417
  try {
418
- renameUpdateExclusive(temporary, paths.claim);
418
+ renameExclusive(temporary, paths.claim);
419
419
  } catch {
420
420
  return ownerMatches(await readOwner(paths.claim), expected);
421
421
  }
@@ -428,7 +428,7 @@ async function ensureClaim(target, state) {
428
428
  mode: 384
429
429
  });
430
430
  if (!ownerMatches(await readOwner(temporary), expected)) return false;
431
- renameUpdateExclusive(temporary, paths.claim);
431
+ renameExclusive(temporary, paths.claim);
432
432
  } catch {
433
433
  return ownerMatches(await readOwner(paths.claim), expected);
434
434
  }
@@ -436,7 +436,7 @@ async function ensureClaim(target, state) {
436
436
  }
437
437
  function isMatchingCleanupTemporary(name, paths) {
438
438
  if (!name.startsWith(paths.cleanupMarkerTemporaryPrefix) || !name.endsWith(".tmp")) return false;
439
- return UUID.test(name.slice(paths.cleanupMarkerTemporaryPrefix.length, -4));
439
+ return isUuid(name.slice(paths.cleanupMarkerTemporaryPrefix.length, -4));
440
440
  }
441
441
  async function ensureCleanupMarker(target, state) {
442
442
  const expected = cleanupMarkerFor(target, state);
@@ -454,7 +454,7 @@ async function ensureCleanupMarker(target, state) {
454
454
  if (!info?.isFile() || info.isSymbolicLink()) continue;
455
455
  if (!cleanupMarkerMatches(await readCleanupMarker(temporary), expected)) continue;
456
456
  try {
457
- renameUpdateExclusive(temporary, paths.cleanupMarker);
457
+ renameExclusive(temporary, paths.cleanupMarker);
458
458
  } catch {
459
459
  return cleanupMarkerMatches(await readCleanupMarker(paths.cleanupMarker), expected);
460
460
  }
@@ -467,7 +467,7 @@ async function ensureCleanupMarker(target, state) {
467
467
  mode: 384
468
468
  });
469
469
  if (!cleanupMarkerMatches(await readCleanupMarker(temporary), expected)) return false;
470
- renameUpdateExclusive(temporary, paths.cleanupMarker);
470
+ renameExclusive(temporary, paths.cleanupMarker);
471
471
  } catch {
472
472
  return cleanupMarkerMatches(await readCleanupMarker(paths.cleanupMarker), expected);
473
473
  }
@@ -490,7 +490,7 @@ async function matchesInstalledTree(path, owner) {
490
490
  }
491
491
  }
492
492
  function isMatchingHealthTemporary(name) {
493
- return name.startsWith(".healthy-") && name.endsWith(".tmp") && UUID.test(name.slice(9, -4));
493
+ return name.startsWith(".healthy-") && name.endsWith(".tmp") && isUuid(name.slice(9, -4));
494
494
  }
495
495
  async function validRetirementContainer(owner, paths, allowHealthTemporaries = true, pruneProgress) {
496
496
  const info = await pathInfo(paths.container);
@@ -519,7 +519,7 @@ function generationFromClaimName(name, target) {
519
519
  const prefix = `.${basename(target)}.framework-retired-`;
520
520
  if (!name.startsWith(prefix) || !name.endsWith(".claim.json")) return void 0;
521
521
  const generation = name.slice(prefix.length, -11);
522
- return UUID.test(generation) ? generation : void 0;
522
+ return isUuid(generation) ? generation : void 0;
523
523
  }
524
524
  /**
525
525
  * Read and validate every retirement claim and container while the install lock
@@ -552,7 +552,7 @@ async function readValidatedRetirementInventoryWhileLocked(target, currentVersio
552
552
  const progressTemporaries = names.filter((name) => name.startsWith(progressTemporaryPrefix) && name.endsWith(".tmp"));
553
553
  if (progressTemporaries.length > 0) {
554
554
  const temporaryName = options.allowedPruneProgressTemporaryName;
555
- if (progressTemporaries.length !== 1 || !temporaryName || progressTemporaries[0] !== temporaryName || !UUID.test(temporaryName.slice(progressTemporaryPrefix.length, -4))) throw new Error("An update retention progress publication requires recovery");
555
+ if (progressTemporaries.length !== 1 || !temporaryName || progressTemporaries[0] !== temporaryName || !isUuid(temporaryName.slice(progressTemporaryPrefix.length, -4))) throw new Error("An update retention progress publication requires recovery");
556
556
  pruneProgressTemporary = await readPruneProgressFile(join(dirname(target), temporaryName), target, generation, pruneIntentOnDisk, pruneProgress.owner);
557
557
  const [finalInfo, temporaryInfo] = await Promise.all([lstat(retentionPruneProgressPath(target, generation), { bigint: true }), lstat(join(dirname(target), temporaryName), { bigint: true })]);
558
558
  const sameHardLink = finalInfo.dev === temporaryInfo.dev && finalInfo.ino === temporaryInfo.ino;
@@ -565,7 +565,7 @@ async function readValidatedRetirementInventoryWhileLocked(target, currentVersio
565
565
  const temporaryName = options.allowedPruneProgressTemporaryName;
566
566
  const progressTemporaryPrefix = `${basename(retentionPruneProgressPath(target, generation))}.`;
567
567
  const progressTemporaries = names.filter((name) => name.startsWith(progressTemporaryPrefix) && name.endsWith(".tmp"));
568
- if (!temporaryName || progressTemporaries.length !== 1 || progressTemporaries[0] !== temporaryName || !UUID.test(temporaryName.slice(progressTemporaryPrefix.length, -4))) throw new Error("An update retention progress marker disappeared");
568
+ if (!temporaryName || progressTemporaries.length !== 1 || progressTemporaries[0] !== temporaryName || !isUuid(temporaryName.slice(progressTemporaryPrefix.length, -4))) throw new Error("An update retention progress marker disappeared");
569
569
  pruneIntentOnDisk = await readUpdateRetentionIntent(target, generation);
570
570
  pruneProgressTemporary = await readPruneProgressFile(join(dirname(target), temporaryName), target, generation, pruneIntentOnDisk);
571
571
  if (!pruneIntentOnDisk || !pruneProgressTemporary || pruneProgressTemporary.phase !== "deleting") throw new Error("An orphaned update retention progress temporary requires recovery");
@@ -582,7 +582,7 @@ async function readValidatedRetirementInventoryWhileLocked(target, currentVersio
582
582
  if (isPruneProgressName(name, target)) continue;
583
583
  if (name === options.allowedPruneProgressTemporaryName) continue;
584
584
  const suffix = name.slice(retirementPrefix.length);
585
- if (UUID.test(suffix)) {
585
+ if (isUuid(suffix)) {
586
586
  const containerInfo = await pathInfo(join(dirname(target), name));
587
587
  if (!containerInfo?.isDirectory() || containerInfo.isSymbolicLink()) throw new Error("An update retirement container requires recovery");
588
588
  containerGenerations.add(suffix);
@@ -595,7 +595,7 @@ async function readValidatedRetirementInventoryWhileLocked(target, currentVersio
595
595
  if (suffix.endsWith(".claim.json")) throw new Error("An update retirement claim has an invalid generation");
596
596
  const generation = suffix.slice(0, 36);
597
597
  const claimTemporaryId = suffix.startsWith(`${generation}.claim-`) && suffix.endsWith(".tmp") ? suffix.slice(generation.length + 7, -4) : "";
598
- if (UUID.test(generation) && UUID.test(claimTemporaryId)) throw new Error("An update retirement claim publication is incomplete");
598
+ if (isUuid(generation) && isUuid(claimTemporaryId)) throw new Error("An update retirement claim publication is incomplete");
599
599
  throw new Error("An unknown update retirement entry requires recovery");
600
600
  }
601
601
  for (const generation of containerGenerations) if (!claimNames.has(basename(updateRetirementPaths(target, generation).claim)) && generation.toLowerCase() !== pruneProgress?.intent.generation) throw new Error("An update retirement container has no matching claim");
@@ -663,7 +663,7 @@ async function readValidatedRetirementInventoryWhileLocked(target, currentVersio
663
663
  */
664
664
  async function findPendingUpdateHealthAcknowledgement(options) {
665
665
  const { target, currentVersion } = options;
666
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update health acknowledgement lookup");
666
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update health acknowledgement lookup");
667
667
  return withUpdateLock(target, async () => {
668
668
  const staging = updateStagingFor(target);
669
669
  const stagingInfo = await pathInfo(staging);
@@ -696,7 +696,7 @@ async function findPendingUpdateHealthAcknowledgement(options) {
696
696
  * require unrelated retirement history to be valid before a new stage.
697
697
  */
698
698
  async function assertNoUpdateRetentionPruneArtifactsWhileLocked(target) {
699
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0") || !basename(target)) throw new Error("Invalid update retention prune gate target");
699
+ if (!isCanonicalPath(target) || !basename(target)) throw new Error("Invalid update retention prune gate target");
700
700
  assertNoUnresolvedRetentionPruneArtifacts(target, await readdir(dirname(target)), {}, "update retention prune requires recovery before staging");
701
701
  }
702
702
  /**
@@ -706,7 +706,7 @@ async function assertNoUpdateRetentionPruneArtifactsWhileLocked(target) {
706
706
  */
707
707
  async function planValidatedUpdateRetention(options) {
708
708
  const { target, currentVersion } = options;
709
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
709
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
710
710
  return withUpdateLock(target, async () => (await planRetentionWhileLocked(target, currentVersion)).plan);
711
711
  }
712
712
  async function planRetentionWhileLocked(target, currentVersion, options = {}) {
@@ -768,7 +768,7 @@ async function planRetentionWhileLocked(target, currentVersion, options = {}) {
768
768
  */
769
769
  async function acquireRetainedUpdateTrees(options) {
770
770
  const { target, currentVersion } = options;
771
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || target.includes("\0")) throw new Error("Invalid retained update tree lookup");
771
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid retained update tree lookup");
772
772
  let retained;
773
773
  try {
774
774
  return await withUpdateLock(target, async () => {
@@ -916,7 +916,7 @@ async function assertIntentMatchesRetentionPlan(target, inventory, plan, intent)
916
916
  */
917
917
  async function inspectUpdateRetentionCleanup(options) {
918
918
  const { target, currentVersion, generation } = options;
919
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
919
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
920
920
  updateRetentionIntentPath(target, generation);
921
921
  return withUpdateLock(target, () => inspectRetentionWhileLocked(target, currentVersion, generation));
922
922
  }
@@ -936,7 +936,7 @@ async function inspectRetentionWhileLocked(target, currentVersion, generation) {
936
936
  */
937
937
  async function syncValidatedUpdateRetentionCleanup(options) {
938
938
  const { target, currentVersion, generation } = options;
939
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
939
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
940
940
  updateRetentionIntentPath(target, generation);
941
941
  return withUpdateLock(target, async () => {
942
942
  const intent = await inspectRetentionWhileLocked(target, currentVersion, generation);
@@ -961,7 +961,7 @@ async function syncValidatedUpdateRetentionCleanup(options) {
961
961
  */
962
962
  async function promoteOrphanUpdateRetentionCleanup(options) {
963
963
  const { target, currentVersion, generation } = options;
964
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
964
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
965
965
  const finalPath = updateRetentionIntentPath(target, generation);
966
966
  const readOrphan = async (expectedTemporaryName) => {
967
967
  const names = await readdir(dirname(target));
@@ -1025,7 +1025,7 @@ async function promoteOrphanUpdateRetentionCleanup(options) {
1025
1025
  */
1026
1026
  async function reconcileUpdateRetentionCleanup(options) {
1027
1027
  const { target, currentVersion, generation } = options;
1028
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
1028
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
1029
1029
  const markerPath = updateRetentionIntentPath(target, generation);
1030
1030
  return withUpdateLock(target, async () => {
1031
1031
  const temporaryName = retentionTemporaryName(await readdir(dirname(target)), target, generation);
@@ -1077,7 +1077,7 @@ async function reconcileUpdateRetentionCleanup(options) {
1077
1077
  */
1078
1078
  async function beginUpdateRetentionCleanup(options) {
1079
1079
  const { target, currentVersion } = options;
1080
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target) throw new Error("Invalid update retention lookup");
1080
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target)) throw new Error("Invalid update retention lookup");
1081
1081
  return withUpdateLock(target, async () => {
1082
1082
  const { inventory, plan } = await planRetentionWhileLocked(target, currentVersion);
1083
1083
  if (plan.status !== "ready") throw new Error("Update retention lineage requires recovery");
@@ -1116,7 +1116,7 @@ async function beginUpdateRetentionCleanup(options) {
1116
1116
  async function pruneUpdateRetentionStep(options) {
1117
1117
  const { target, currentVersion, generation } = options;
1118
1118
  const maxOperations = options.maxOperations ?? 64;
1119
- if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target || !Number.isSafeInteger(maxOperations) || maxOperations < 1 || maxOperations > 4096) throw new Error("Invalid update retention prune request");
1119
+ if (typeof currentVersion !== "string" || !VERSION.test(currentVersion) || !isCanonicalPath(target) || !Number.isSafeInteger(maxOperations) || maxOperations < 1 || maxOperations > 4096) throw new Error("Invalid update retention prune request");
1120
1120
  updateRetirementPaths(target, generation);
1121
1121
  if (process.platform !== "darwin" || process.arch !== "arm64") return { status: "recovery-required" };
1122
1122
  return withUpdateLock(target, async () => {
@@ -1289,7 +1289,7 @@ async function pruneUpdateRetentionStep(options) {
1289
1289
  });
1290
1290
  }
1291
1291
  async function acknowledgeUpdateHealthWhileLocked(target, token) {
1292
- if (typeof token !== "object" || token === null || !UUID.test(token.generation) || typeof token.version !== "string" || !VERSION.test(token.version)) return false;
1292
+ if (typeof token !== "object" || token === null || !isUuid(token.generation) || typeof token.version !== "string" || !VERSION.test(token.version)) return false;
1293
1293
  const paths = updateRetirementPaths(target, token.generation);
1294
1294
  const claimInfo = await pathInfo(paths.claim);
1295
1295
  const owner = await readOwner(paths.claim);
@@ -1303,7 +1303,7 @@ async function acknowledgeUpdateHealthWhileLocked(target, token) {
1303
1303
  if (!receipt) continue;
1304
1304
  if (!healthReceiptMatches(receipt, expected)) return false;
1305
1305
  try {
1306
- renameUpdateExclusive(temporary, paths.healthReceipt);
1306
+ renameExclusive(temporary, paths.healthReceipt);
1307
1307
  } catch {
1308
1308
  return healthReceiptMatches(await readHealthReceipt(paths.healthReceipt), expected);
1309
1309
  }
@@ -1316,7 +1316,7 @@ async function acknowledgeUpdateHealthWhileLocked(target, token) {
1316
1316
  mode: 384
1317
1317
  });
1318
1318
  if (!healthReceiptMatches(await readHealthReceipt(temporary), expected)) return false;
1319
- renameUpdateExclusive(temporary, paths.healthReceipt);
1319
+ renameExclusive(temporary, paths.healthReceipt);
1320
1320
  } catch {
1321
1321
  return healthReceiptMatches(await readHealthReceipt(paths.healthReceipt), expected);
1322
1322
  }
@@ -1461,7 +1461,7 @@ async function retireCompletedUpdateWhileLocked(options) {
1461
1461
  if (candidateInfo) {
1462
1462
  if (entries.length !== 0) return false;
1463
1463
  try {
1464
- renameUpdateExclusive(candidate, paths.tree);
1464
+ renameExclusive(candidate, paths.tree);
1465
1465
  } catch {
1466
1466
  return false;
1467
1467
  }
@@ -30,6 +30,8 @@ export type UpdateStageState = Readonly<{
30
30
  export declare class UpdateLockBusyError extends Error {
31
31
  constructor();
32
32
  }
33
+ /** The staged helper request, bounded and read without following a link. */
34
+ export declare function readUpdateHelperRequestFile(staging: string): Promise<string | undefined>;
33
35
  /** Acquire the stable per-install lock, waiting briefly for short transitions. */
34
36
  export declare function acquireUpdateLock(target: string, timeoutMs?: number): Promise<UpdateLock>;
35
37
  export declare function newUpdateStageState(input: {
@@ -1,8 +1,10 @@
1
+ import { errorCode, isSha256Hex, isUuid } from "../app/value-guards.js";
2
+ import { readSmallStateFile } from "./stable-file.js";
1
3
  import { tryAcquireUpdateLock } from "./update-lock.js";
2
- import { lstat, open, readFile, rename, unlink, writeFile } from "node:fs/promises";
4
+ import { open, rename, unlink, writeFile } from "node:fs/promises";
3
5
  import { join } from "node:path";
4
- import { constants as constants$1 } from "node:fs";
5
6
  import { randomUUID } from "node:crypto";
7
+ import { constants as constants$1 } from "node:fs";
6
8
  // src/updates/update-state-store.ts
7
9
  var UpdateLockBusyError = class extends Error {
8
10
  constructor() {
@@ -10,6 +12,10 @@ var UpdateLockBusyError = class extends Error {
10
12
  this.name = "UpdateLockBusyError";
11
13
  }
12
14
  };
15
+ /** The staged helper request, bounded and read without following a link. */
16
+ function readUpdateHelperRequestFile(staging) {
17
+ return readSmallStateFile(join(staging, "request.json"), 65536, "Invalid update helper request");
18
+ }
13
19
  /** Acquire the stable per-install lock, waiting briefly for short transitions. */
14
20
  async function acquireUpdateLock(target, timeoutMs = 5e3) {
15
21
  const deadline = Date.now() + timeoutMs;
@@ -38,7 +44,7 @@ function parseState(value) {
38
44
  const candidateTreeIdentity = record.candidateTreeIdentity;
39
45
  const candidateTreeDigest = record.candidateTreeDigest;
40
46
  const previousTreeIdentity = record.previousTreeIdentity;
41
- if (record.schema !== 1 || typeof record.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(record.generation) || ![
47
+ if (record.schema !== 1 || typeof record.generation !== "string" || !isUuid(record.generation) || ![
42
48
  "staged",
43
49
  "armed",
44
50
  "swapping",
@@ -46,7 +52,7 @@ function parseState(value) {
46
52
  "retiring",
47
53
  "retired",
48
54
  "failed"
49
- ].includes(String(record.phase)) || typeof record.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.version) || typeof record.previousVersion !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.previousVersion) || candidateTreeDigest !== void 0 && (typeof candidateTreeDigest !== "string" || !/^[0-9a-f]{64}$/.test(candidateTreeDigest)) || candidateTreeIdentity !== void 0 && (typeof candidateTreeIdentity !== "object" || candidateTreeIdentity === null || Array.isArray(candidateTreeIdentity) || Object.keys(candidateTreeIdentity).length !== 2 || typeof candidateTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.device) || typeof candidateTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.inode)) || previousTreeIdentity !== void 0 && (typeof previousTreeIdentity !== "object" || previousTreeIdentity === null || Array.isArray(previousTreeIdentity) || Object.keys(previousTreeIdentity).length !== 2 || typeof previousTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.device) || typeof previousTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.inode)) || record.signedRelease !== void 0 && (typeof record.signedRelease !== "object" || record.signedRelease === null || Array.isArray(record.signedRelease) || Object.keys(record.signedRelease).length !== 2 || !Object.hasOwn(record.signedRelease, "descriptor") || typeof record.signedRelease.signature !== "string") || record.helperPid !== void 0 && (!Number.isSafeInteger(record.helperPid) || record.helperPid < 2) || record.helperOwner !== void 0 && (typeof record.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(record.helperOwner)) || record.launchGateOwner !== void 0 && (typeof record.launchGateOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(record.launchGateOwner)) || Object.keys(record).some((key) => ![
55
+ ].includes(String(record.phase)) || typeof record.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.version) || typeof record.previousVersion !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.previousVersion) || candidateTreeDigest !== void 0 && (typeof candidateTreeDigest !== "string" || !isSha256Hex(candidateTreeDigest)) || candidateTreeIdentity !== void 0 && (typeof candidateTreeIdentity !== "object" || candidateTreeIdentity === null || Array.isArray(candidateTreeIdentity) || Object.keys(candidateTreeIdentity).length !== 2 || typeof candidateTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.device) || typeof candidateTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.inode)) || previousTreeIdentity !== void 0 && (typeof previousTreeIdentity !== "object" || previousTreeIdentity === null || Array.isArray(previousTreeIdentity) || Object.keys(previousTreeIdentity).length !== 2 || typeof previousTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.device) || typeof previousTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.inode)) || record.signedRelease !== void 0 && (typeof record.signedRelease !== "object" || record.signedRelease === null || Array.isArray(record.signedRelease) || Object.keys(record.signedRelease).length !== 2 || !Object.hasOwn(record.signedRelease, "descriptor") || typeof record.signedRelease.signature !== "string") || record.helperPid !== void 0 && (!Number.isSafeInteger(record.helperPid) || record.helperPid < 2) || record.helperOwner !== void 0 && (typeof record.helperOwner !== "string" || !isUuid(record.helperOwner)) || record.launchGateOwner !== void 0 && (typeof record.launchGateOwner !== "string" || !isUuid(record.launchGateOwner)) || Object.keys(record).some((key) => ![
50
56
  "schema",
51
57
  "generation",
52
58
  "phase",
@@ -65,16 +71,8 @@ function parseState(value) {
65
71
  return Object.freeze(record);
66
72
  }
67
73
  async function readUpdateStageState(staging) {
68
- const path = join(staging, "state.json");
69
- let info;
70
- try {
71
- info = await lstat(path);
72
- } catch (error) {
73
- if (error.code === "ENOENT") return void 0;
74
- throw error;
75
- }
76
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096) throw new Error("Invalid update staging state");
77
- return parseState(JSON.parse(await readFile(path, "utf8")));
74
+ const text = await readSmallStateFile(join(staging, "state.json"), 4096, "Invalid update staging state");
75
+ return text === void 0 ? void 0 : parseState(JSON.parse(text));
78
76
  }
79
77
  /** Sync a journal directory after a state rename or owned-entry cleanup. */
80
78
  async function syncUpdateStageDirectory(staging) {
@@ -86,19 +84,7 @@ async function syncUpdateStageDirectory(staging) {
86
84
  }
87
85
  }
88
86
  async function writeUpdateStageState(staging, state) {
89
- if (typeof state.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(state.generation)) throw new Error("Invalid update staging state");
90
- const path = join(staging, "state.json");
91
- const temporary = join(staging, `.state-${state.generation}-${randomUUID()}.tmp`);
92
- await writeFile(temporary, JSON.stringify(state) + "\n", {
93
- flag: "wx",
94
- mode: 384
95
- });
96
- try {
97
- await rename(temporary, path);
98
- } catch (error) {
99
- await unlink(temporary).catch(() => {});
100
- throw error;
101
- }
87
+ await replaceUpdateStageState(staging, state, false);
102
88
  }
103
89
  /**
104
90
  * Atomically replace and sync a journal record before a schema-3 gate decision.
@@ -107,23 +93,33 @@ async function writeUpdateStageState(staging, state) {
107
93
  * proof, callers must keep the launch gate and require recovery.
108
94
  */
109
95
  async function writeUpdateStageStateDurably(staging, state) {
110
- if (typeof state.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(state.generation)) throw new Error("Invalid update staging state");
96
+ await replaceUpdateStageState(staging, state, true);
97
+ }
98
+ /** Write `state.json` through an exclusive temporary file and rename it into place. */
99
+ async function replaceUpdateStageState(staging, state, durable) {
100
+ if (typeof state.generation !== "string" || !isUuid(state.generation)) throw new Error("Invalid update staging state");
111
101
  const path = join(staging, "state.json");
112
102
  const temporary = join(staging, `.state-${state.generation}-${randomUUID()}.tmp`);
103
+ const contents = JSON.stringify(state) + "\n";
113
104
  let renamed = false;
114
105
  try {
115
- const file = await open(temporary, constants$1.O_WRONLY | constants$1.O_CREAT | constants$1.O_EXCL, 384);
116
- try {
117
- await file.writeFile(JSON.stringify(state) + "\n");
118
- await file.sync();
119
- } finally {
120
- await file.close();
121
- }
106
+ if (durable) {
107
+ const file = await open(temporary, constants$1.O_WRONLY | constants$1.O_CREAT | constants$1.O_EXCL, 384);
108
+ try {
109
+ await file.writeFile(contents);
110
+ await file.sync();
111
+ } finally {
112
+ await file.close();
113
+ }
114
+ } else await writeFile(temporary, contents, {
115
+ flag: "wx",
116
+ mode: 384
117
+ });
122
118
  await rename(temporary, path);
123
119
  renamed = true;
124
- await syncUpdateStageDirectory(staging);
120
+ if (durable) await syncUpdateStageDirectory(staging);
125
121
  } catch (error) {
126
- if (!renamed) await unlink(temporary).catch(() => {});
122
+ if (!renamed && errorCode(error) !== "EEXIST") await unlink(temporary).catch(() => {});
127
123
  throw error;
128
124
  }
129
125
  }
@@ -136,4 +132,4 @@ async function withUpdateLock(target, operation) {
136
132
  await lock.release();
137
133
  }
138
134
  }
139
- export { UpdateLockBusyError, acquireUpdateLock, newUpdateStageState, readUpdateStageState, syncUpdateStageDirectory, withUpdateLock, writeUpdateStageState, writeUpdateStageStateDurably };
135
+ export { UpdateLockBusyError, acquireUpdateLock, newUpdateStageState, readUpdateHelperRequestFile, readUpdateStageState, syncUpdateStageDirectory, withUpdateLock, writeUpdateStageState, writeUpdateStageStateDurably };
@@ -12,11 +12,16 @@ export type NativeUpdateState<Phase extends string = string> = Readonly<{
12
12
  github_url: string;
13
13
  restart_required: boolean;
14
14
  }>;
15
- /** Project a native snapshot into the shared UI update dialog contract. */
16
- export declare function toUpdateState<Phase extends string, Url extends string | null = string>(update: Omit<NativeUpdateState<Phase>, 'github_url' | 'release_notes'> & Readonly<{
15
+ /**
16
+ * A native updater snapshot: the service fields, with a release URL that may be
17
+ * absent (`Url`) and release notes that older hosts omit.
18
+ */
19
+ export type NativeUpdateSnapshotOf<Phase extends string = string, Url extends string | null = string> = Omit<NativeUpdateState<Phase>, 'github_url' | 'release_notes'> & Readonly<{
17
20
  github_url: Url;
18
21
  release_notes?: string | null;
19
- }>, options?: {
22
+ }>;
23
+ /** Project a native snapshot into the shared UI update dialog contract. */
24
+ export declare function toUpdateState<Phase extends string, Url extends string | null = string>(update: NativeUpdateSnapshotOf<Phase, Url>, options?: {
20
25
  includeReleaseNotes?: boolean;
21
26
  }): {
22
27
  phase: Phase;
@@ -1,7 +1,8 @@
1
+ import { errorCode } from "../app/value-guards.js";
2
+ import { renameExclusive } from "../native/native-rename.js";
1
3
  import { exportVerifiedRelease } from "./signed-release.js";
2
4
  import { planUpdateHandoff } from "./update-handoff-plan.js";
3
5
  import { extractVerifiedUpdate } from "./update-extraction.js";
4
- import { renameUpdateExclusive } from "./update-exclusive-rename.js";
5
6
  import { newUpdateStageState, readUpdateStageState, withUpdateLock, writeUpdateStageState } from "./update-state-store.js";
6
7
  import { readUpdateTreeDigest, readUpdateTreeIdentity, updateStagingFor } from "./update-swap.js";
7
8
  import { assertNoUpdateRetentionPruneArtifactsWhileLocked, reconcileRetirementCleanupMarkerWhileLocked } from "./update-retirement.js";
@@ -51,7 +52,7 @@ async function stageUpdateSwap(options) {
51
52
  maxExpandedBytes: options.maxExpandedBytes,
52
53
  ...options.sidecars === void 0 ? {} : { sidecars: options.sidecars }
53
54
  });
54
- renameUpdateExclusive(extracted.payload, join(preparing, "candidate"));
55
+ renameExclusive(extracted.payload, join(preparing, "candidate"));
55
56
  await rm(extracted.directory, {
56
57
  recursive: true,
57
58
  force: true
@@ -78,13 +79,13 @@ async function stageUpdateSwap(options) {
78
79
  try {
79
80
  existingInfo = await lstat(staging);
80
81
  } catch (error) {
81
- if (error.code === "ENOENT") {
82
+ if (errorCode(error) === "ENOENT") {
82
83
  if (await reconcileRetirementCleanupMarkerWhileLocked({
83
84
  target: plan.target,
84
85
  staging,
85
86
  currentVersion: plan.currentVersion
86
87
  }) === "recovery-required") throw new Error("A previous update retirement requires recovery", { cause: error });
87
- renameUpdateExclusive(preparing, staging);
88
+ renameExclusive(preparing, staging);
88
89
  published = true;
89
90
  return {
90
91
  staging,
@@ -102,7 +103,7 @@ async function stageUpdateSwap(options) {
102
103
  currentVersion: plan.currentVersion
103
104
  });
104
105
  if (retirement === "updated") {
105
- renameUpdateExclusive(preparing, staging);
106
+ renameExclusive(preparing, staging);
106
107
  published = true;
107
108
  return {
108
109
  staging,
@@ -1,4 +1,4 @@
1
- import { exchangeDirectories } from '../native/directory-exchange';
1
+ import { exchangeDirectories } from '../native/native-rename';
2
2
  export type UpdateTreeIdentity = Readonly<{
3
3
  device: string;
4
4
  inode: string;