@adea-ai/app-framework 0.27.0 → 0.28.1

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 (125) hide show
  1. package/README.md +177 -15
  2. package/dist/app/create-application.d.ts +2 -0
  3. package/dist/app/create-application.js +6 -0
  4. package/dist/app/service-registry.js +4 -0
  5. package/dist/appearance/custom-theme-import.js +43 -7
  6. package/dist/appearance/custom-theme-library.d.ts +2 -0
  7. package/dist/appearance/custom-theme-library.js +5 -1
  8. package/dist/appearance/theme-record-adapter.js +4 -2
  9. package/dist/appearance/theme-registry.js +7 -1
  10. package/dist/cli/cli-runner.d.ts +4 -0
  11. package/dist/cli/cli-runner.js +14 -1
  12. package/dist/cli/create-application-cli.js +4 -2
  13. package/dist/cli/release-cut-cli.js +2 -2
  14. package/dist/cli/update-helper-cli.js +28 -6
  15. package/dist/desktop/client-asset-manifest.d.ts +47 -0
  16. package/dist/desktop/client-asset-manifest.js +114 -0
  17. package/dist/desktop/client-asset-stream.d.ts +10 -0
  18. package/dist/desktop/client-asset-stream.js +138 -33
  19. package/dist/desktop/client-assets.d.ts +20 -0
  20. package/dist/desktop/client-assets.js +132 -22
  21. package/dist/desktop/client-response-lifecycle.d.ts +22 -0
  22. package/dist/desktop/client-response-lifecycle.js +233 -0
  23. package/dist/desktop/desktop-host.d.ts +40 -2
  24. package/dist/desktop/desktop-host.js +159 -9
  25. package/dist/framework-source.json +1 -1
  26. package/dist/installer/command-artifact.d.ts +4 -2
  27. package/dist/installer/command-artifact.js +119 -19
  28. package/dist/installer/linux-process-group.d.ts +13 -0
  29. package/dist/installer/linux-process-group.js +58 -0
  30. package/dist/native/darwin-sync.d.ts +7 -0
  31. package/dist/native/darwin-sync.js +43 -0
  32. package/dist/native/desktop-build-hook.js +7 -0
  33. package/dist/native/directory-exchange.d.ts +10 -0
  34. package/dist/native/directory-exchange.js +90 -0
  35. package/dist/native/electrobun-source-patch.d.ts +5 -0
  36. package/dist/native/electrobun-source-patch.js +19 -1
  37. package/dist/native/posix-launcher-build.d.ts +24 -0
  38. package/dist/native/posix-launcher-build.js +232 -0
  39. package/dist/native/posix-tree-removal.d.ts +36 -0
  40. package/dist/native/posix-tree-removal.js +425 -0
  41. package/dist/release/feed-release-cut.js +30 -10
  42. package/dist/release/github-release-cut.js +16 -0
  43. package/dist/release/release-cut.d.ts +8 -1
  44. package/dist/release/release-cut.js +9 -1
  45. package/dist/update-helper.bundle.js +1483 -105
  46. package/dist/updates/channel-update-manager.d.ts +5 -0
  47. package/dist/updates/channel-update-manager.js +13 -1
  48. package/dist/updates/desktop-updater.d.ts +78 -2
  49. package/dist/updates/desktop-updater.js +320 -37
  50. package/dist/updates/launch-environment.d.ts +2 -0
  51. package/dist/updates/launch-environment.js +4 -0
  52. package/dist/updates/native-file-lock.d.ts +15 -0
  53. package/dist/updates/native-file-lock.js +189 -0
  54. package/dist/updates/packaged-launch-admission.d.ts +13 -0
  55. package/dist/updates/packaged-launch-admission.js +45 -0
  56. package/dist/updates/packaged-release-config.d.ts +18 -0
  57. package/dist/updates/packaged-release-config.js +60 -0
  58. package/dist/updates/packaged-update-startup.d.ts +44 -0
  59. package/dist/updates/packaged-update-startup.js +81 -0
  60. package/dist/updates/update-arm.d.ts +9 -8
  61. package/dist/updates/update-arm.js +227 -62
  62. package/dist/updates/update-cleanup-error.d.ts +23 -0
  63. package/dist/updates/update-cleanup-error.js +65 -0
  64. package/dist/updates/update-download-cleanup.d.ts +11 -0
  65. package/dist/updates/update-download-cleanup.js +112 -2
  66. package/dist/updates/update-download-lock.d.ts +20 -0
  67. package/dist/updates/update-download-lock.js +73 -0
  68. package/dist/updates/update-download.d.ts +2 -0
  69. package/dist/updates/update-download.js +7 -3
  70. package/dist/updates/update-generation-lease.d.ts +6 -0
  71. package/dist/updates/update-generation-lease.js +14 -0
  72. package/dist/updates/update-helper.d.ts +11 -2
  73. package/dist/updates/update-helper.js +322 -13
  74. package/dist/updates/update-inherited-launch-lease.d.ts +22 -0
  75. package/dist/updates/update-inherited-launch-lease.js +58 -0
  76. package/dist/updates/update-instance-lease.d.ts +15 -0
  77. package/dist/updates/update-instance-lease.js +25 -0
  78. package/dist/updates/update-launch-gate.d.ts +137 -0
  79. package/dist/updates/update-launch-gate.js +617 -0
  80. package/dist/updates/update-launch.js +3 -1
  81. package/dist/updates/update-lock.d.ts +12 -0
  82. package/dist/updates/update-lock.js +18 -0
  83. package/dist/updates/update-manager.d.ts +2 -0
  84. package/dist/updates/update-manager.js +9 -2
  85. package/dist/updates/update-marker.d.ts +7 -0
  86. package/dist/updates/update-marker.js +62 -0
  87. package/dist/updates/update-preflight-error.d.ts +7 -0
  88. package/dist/updates/update-preflight-error.js +11 -0
  89. package/dist/updates/update-process-fence.d.ts +4 -0
  90. package/dist/updates/update-process-fence.js +85 -73
  91. package/dist/updates/update-process-liveness.d.ts +2 -0
  92. package/dist/updates/update-process-liveness.js +11 -0
  93. package/dist/updates/update-resume.d.ts +37 -3
  94. package/dist/updates/update-resume.js +839 -23
  95. package/dist/updates/update-retention-marker.d.ts +67 -0
  96. package/dist/updates/update-retention-marker.js +165 -0
  97. package/dist/updates/update-retention-plan.d.ts +25 -0
  98. package/dist/updates/update-retention-plan.js +54 -0
  99. package/dist/updates/update-retention-sync.d.ts +34 -0
  100. package/dist/updates/update-retention-sync.js +83 -0
  101. package/dist/updates/update-retirement.d.ts +205 -0
  102. package/dist/updates/update-retirement.js +1485 -0
  103. package/dist/updates/update-shutdown.d.ts +16 -4
  104. package/dist/updates/update-shutdown.js +133 -8
  105. package/dist/updates/update-state-store.d.ts +58 -0
  106. package/dist/updates/update-state-store.js +139 -0
  107. package/dist/updates/update-swap-stage.d.ts +1 -1
  108. package/dist/updates/update-swap-stage.js +85 -14
  109. package/dist/updates/update-swap.d.ts +45 -2
  110. package/dist/updates/update-swap.js +207 -17
  111. package/package.json +31 -1
  112. package/src/appearance/custom-theme-import.ts +96 -14
  113. package/src/appearance/custom-theme-library.ts +5 -0
  114. package/src/appearance/theme-record-adapter.ts +15 -6
  115. package/src/appearance/theme-registry.ts +8 -1
  116. package/src/desktop/client-asset-manifest.ts +207 -0
  117. package/src/desktop/client-asset-stream.ts +193 -32
  118. package/src/desktop/client-assets.ts +210 -19
  119. package/src/desktop/client-response-lifecycle.ts +305 -0
  120. package/src/native/electrobun-posix-launch-lease.patch +398 -0
  121. package/templates/application/README.md +50 -0
  122. package/templates/application/electrobun.config.ts +5 -1
  123. package/templates/application/scripts/build-desktop.ts +4 -0
  124. package/templates/application/src/bun/index.ts +173 -16
  125. package/templates/application/vite.config.ts +112 -6
@@ -0,0 +1,189 @@
1
+ import { open } from "node:fs/promises";
2
+ import { FFIType, dlopen, ptr, read } from "bun:ffi";
3
+ import { constants as constants$1 } from "node:fs";
4
+ // src/updates/native-file-lock.ts
5
+ var LOCK_SH = 1;
6
+ var LOCK_EX = 2;
7
+ var LOCK_NB = 4;
8
+ var LOCK_UN = 8;
9
+ var LOCKFILE_EXCLUSIVE_LOCK = 2;
10
+ var LOCKFILE_FAIL_IMMEDIATELY = 1;
11
+ var INVALID_HANDLE_VALUE = 18446744073709551615n;
12
+ var ERROR_LOCK_VIOLATION = 33;
13
+ /**
14
+ * Try to lock a persistent file without waiting. undefined means another
15
+ * process owns an incompatible lock; unsupported platforms and native errors
16
+ * reject so callers cannot mistake a failed lock for a successful one.
17
+ *
18
+ * The path must remain stable and the file must not be removed or replaced.
19
+ */
20
+ async function tryAcquireNativeFileLock(path, mode) {
21
+ if (mode !== "shared" && mode !== "exclusive") throw new Error("Invalid native file lock mode");
22
+ if (process.platform === "win32") return tryAcquireWindows(path, mode);
23
+ if (process.platform !== "darwin" && process.platform !== "linux") throw new Error("Unsupported native file lock platform");
24
+ const noFollow = constants$1.O_NOFOLLOW;
25
+ if (noFollow === void 0) throw new Error("Safe native lock file opening is unavailable");
26
+ const file = await open(path, constants$1.O_CREAT | constants$1.O_RDWR | noFollow, 384);
27
+ let library;
28
+ let acquired = false;
29
+ try {
30
+ if (!(await file.stat()).isFile()) throw new Error("Invalid native lock file");
31
+ const darwin = process.platform === "darwin";
32
+ library = loadPosixLibrary(darwin);
33
+ if (library.symbols.flock(file.fd, (mode === "shared" ? LOCK_SH : LOCK_EX) | LOCK_NB) !== 0) {
34
+ const errnoSymbol = darwin ? "__error" : "__errno_location";
35
+ const errno = read.i32(library.symbols[errnoSymbol](), 0);
36
+ if (errno === (darwin ? 35 : 11)) return void 0;
37
+ throw new Error("Could not acquire native file lock (errno " + errno + ")");
38
+ }
39
+ acquired = true;
40
+ return { async release() {
41
+ if (!acquired) return;
42
+ acquired = false;
43
+ let failure;
44
+ try {
45
+ if (library.symbols.flock(file.fd, LOCK_UN) !== 0) {
46
+ const errnoSymbol = darwin ? "__error" : "__errno_location";
47
+ const errno = read.i32(library.symbols[errnoSymbol](), 0);
48
+ failure = /* @__PURE__ */ new Error("Could not release native file lock (errno " + errno + ")");
49
+ }
50
+ } finally {
51
+ const heldLibrary = library;
52
+ library = void 0;
53
+ try {
54
+ heldLibrary.close();
55
+ } finally {
56
+ await file.close();
57
+ }
58
+ }
59
+ if (failure) throw failure;
60
+ } };
61
+ } finally {
62
+ if (!acquired) {
63
+ const openedLibrary = library;
64
+ library = void 0;
65
+ try {
66
+ openedLibrary?.close();
67
+ } finally {
68
+ await file.close();
69
+ }
70
+ }
71
+ }
72
+ }
73
+ function loadPosixLibrary(darwin) {
74
+ const errnoSymbol = darwin ? "__error" : "__errno_location";
75
+ const candidates = darwin ? ["libSystem.B.dylib"] : ["libc.so.6", "libc.so"];
76
+ let failure;
77
+ for (const name of candidates) try {
78
+ return dlopen(name, {
79
+ flock: {
80
+ args: [FFIType.i32, FFIType.i32],
81
+ returns: FFIType.i32
82
+ },
83
+ [errnoSymbol]: {
84
+ args: [],
85
+ returns: FFIType.ptr
86
+ }
87
+ });
88
+ } catch (error) {
89
+ failure = error;
90
+ }
91
+ throw new Error("Could not load native file locking support", { cause: failure });
92
+ }
93
+ /** Ensure an inherited descriptor currently holds a nonblocking shared lock. */
94
+ function tryLockInheritedSharedFileDescriptor(fd) {
95
+ if (process.platform !== "darwin" && process.platform !== "linux") throw new Error("Inherited POSIX locks are unavailable on this platform");
96
+ if (!Number.isInteger(fd) || fd < 3 || fd > 2147483647) throw new Error("Invalid inherited lock descriptor");
97
+ const darwin = process.platform === "darwin";
98
+ const library = loadPosixLibrary(darwin);
99
+ try {
100
+ if (library.symbols.flock(fd, 5) === 0) return true;
101
+ const errno = read.i32(library.symbols[darwin ? "__error" : "__errno_location"](), 0);
102
+ if (errno === (darwin ? 35 : 11)) return false;
103
+ throw new Error("Could not acquire inherited shared lock (errno " + errno + ")");
104
+ } finally {
105
+ library.close();
106
+ }
107
+ }
108
+ async function tryAcquireWindows(path, mode) {
109
+ if (process.arch !== "x64" && process.arch !== "arm64") throw new Error("Unsupported Windows native file lock architecture");
110
+ const library = dlopen("kernel32.dll", {
111
+ CreateFileW: {
112
+ args: [
113
+ FFIType.ptr,
114
+ FFIType.u32,
115
+ FFIType.u32,
116
+ FFIType.ptr,
117
+ FFIType.u32,
118
+ FFIType.u32,
119
+ FFIType.u64
120
+ ],
121
+ returns: FFIType.u64
122
+ },
123
+ LockFileEx: {
124
+ args: [
125
+ FFIType.u64,
126
+ FFIType.u32,
127
+ FFIType.u32,
128
+ FFIType.u32,
129
+ FFIType.u32,
130
+ FFIType.ptr
131
+ ],
132
+ returns: FFIType.i32
133
+ },
134
+ UnlockFileEx: {
135
+ args: [
136
+ FFIType.u64,
137
+ FFIType.u32,
138
+ FFIType.u32,
139
+ FFIType.u32,
140
+ FFIType.ptr
141
+ ],
142
+ returns: FFIType.i32
143
+ },
144
+ CloseHandle: {
145
+ args: [FFIType.u64],
146
+ returns: FFIType.i32
147
+ },
148
+ GetLastError: {
149
+ args: [],
150
+ returns: FFIType.u32
151
+ }
152
+ });
153
+ const name = Buffer.from(path + "\0", "utf16le");
154
+ const handle = library.symbols.CreateFileW(ptr(name), 3221225472, 3, 0, 4, 128, 0);
155
+ if (handle === 0n || handle === INVALID_HANDLE_VALUE) {
156
+ const error = library.symbols.GetLastError();
157
+ library.close();
158
+ throw new Error("Could not open native lock file (Windows error " + error + ")");
159
+ }
160
+ const overlapped = Buffer.alloc(32);
161
+ const flags = LOCKFILE_FAIL_IMMEDIATELY | (mode === "exclusive" ? LOCKFILE_EXCLUSIVE_LOCK : 0);
162
+ if (!library.symbols.LockFileEx(handle, flags, 0, 1, 0, ptr(overlapped))) {
163
+ const error = library.symbols.GetLastError();
164
+ library.symbols.CloseHandle(handle);
165
+ library.close();
166
+ if (error === ERROR_LOCK_VIOLATION) return void 0;
167
+ throw new Error("Could not acquire native file lock (Windows error " + error + ")");
168
+ }
169
+ let acquired = true;
170
+ return { async release() {
171
+ if (!acquired) return;
172
+ acquired = false;
173
+ let failure;
174
+ try {
175
+ if (!library.symbols.UnlockFileEx(handle, 0, 1, 0, ptr(overlapped))) {
176
+ const error = library.symbols.GetLastError();
177
+ failure = /* @__PURE__ */ new Error("Could not release native file lock (Windows error " + error + ")");
178
+ }
179
+ } finally {
180
+ try {
181
+ library.symbols.CloseHandle(handle);
182
+ } finally {
183
+ library.close();
184
+ }
185
+ }
186
+ if (failure) throw failure;
187
+ } };
188
+ }
189
+ export { tryAcquireNativeFileLock, tryLockInheritedSharedFileDescriptor };
@@ -0,0 +1,13 @@
1
+ export { INHERITED_LAUNCH_LEASE_ENV } from './launch-environment';
2
+ /**
3
+ * Consume a launcher-supplied inherited lease before starting app services.
4
+ * An absent descriptor reports unmanaged startup; it does not prove that
5
+ * native launch admission occurred. A gated or invalid handoff throws so the
6
+ * caller can terminate with the inherited descriptor still held.
7
+ */
8
+ export declare function acquirePackagedLaunchAdmission(options: {
9
+ entryDirectory: string;
10
+ descriptor: string | undefined;
11
+ timeoutMs?: number;
12
+ intervalMs?: number;
13
+ }): Promise<'unmanaged' | 'acquired'>;
@@ -0,0 +1,45 @@
1
+ import { INHERITED_LAUNCH_LEASE_ENV } from "./launch-environment.js";
2
+ import { realpath } from "node:fs/promises";
3
+ import { isAbsolute, resolve } from "node:path";
4
+ // src/updates/packaged-launch-admission.ts
5
+ var processLeases = /* @__PURE__ */ new Set();
6
+ var admissionStarted = false;
7
+ /**
8
+ * Consume a launcher-supplied inherited lease before starting app services.
9
+ * An absent descriptor reports unmanaged startup; it does not prove that
10
+ * native launch admission occurred. A gated or invalid handoff throws so the
11
+ * caller can terminate with the inherited descriptor still held.
12
+ */
13
+ async function acquirePackagedLaunchAdmission(options) {
14
+ if (options.descriptor === void 0) return "unmanaged";
15
+ const { installTarget, updatePlatform } = await import("./desktop-updater.js");
16
+ const { adoptInheritedUpdateLaunchLease } = await import("./update-inherited-launch-lease.js");
17
+ const platform = updatePlatform();
18
+ if (!platform || platform !== "macos-arm64" && platform !== "linux-x64") throw new Error("Inherited launch admission is unavailable on this platform");
19
+ if (typeof options.entryDirectory !== "string" || !isAbsolute(options.entryDirectory) || resolve(options.entryDirectory) !== options.entryDirectory || typeof options.descriptor !== "string" || !/^[1-9]\d{0,9}$/.test(options.descriptor)) throw new Error("Invalid packaged launch admission");
20
+ const descriptor = Number(options.descriptor);
21
+ if (!Number.isSafeInteger(descriptor) || descriptor < 3 || descriptor > 2147483647) throw new Error("Invalid packaged launch admission");
22
+ const timeoutMs = options.timeoutMs ?? 5e3;
23
+ const intervalMs = options.intervalMs ?? 25;
24
+ if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 12e4 || !Number.isSafeInteger(intervalMs) || intervalMs < 1 || intervalMs > 5e3) throw new Error("Invalid packaged launch admission retry policy");
25
+ const lexicalTarget = installTarget(options.entryDirectory, platform);
26
+ if (resolve(lexicalTarget, ...platform === "macos-arm64" ? ["Contents"] : [], "Resources", "app", "bun") !== options.entryDirectory) throw new Error("Invalid packaged launch layout");
27
+ const target = await realpath(lexicalTarget);
28
+ if (admissionStarted) throw new Error("Packaged launch admission was already attempted");
29
+ admissionStarted = true;
30
+ const deadline = performance.now() + timeoutMs;
31
+ for (;;) {
32
+ const result = await adoptInheritedUpdateLaunchLease({
33
+ target,
34
+ descriptor
35
+ });
36
+ if (result.status === "acquired") {
37
+ processLeases.add(result.lease);
38
+ return "acquired";
39
+ }
40
+ if (result.status === "gated") throw new Error("Update launch is gated");
41
+ if (performance.now() >= deadline) throw new Error("Update launch admission is busy");
42
+ await Bun.sleep(Math.min(intervalMs, Math.max(1, deadline - performance.now())));
43
+ }
44
+ }
45
+ export { INHERITED_LAUNCH_LEASE_ENV, acquirePackagedLaunchAdmission };
@@ -0,0 +1,18 @@
1
+ import type { ReleaseFeed } from './release-feed';
2
+ export type PackagedReleaseIdentity = Readonly<{
3
+ slug: string;
4
+ identifier: string;
5
+ name: string;
6
+ }>;
7
+ export type PackagedDesktopRelease = Readonly<{
8
+ appId: string;
9
+ appName: string;
10
+ repository: string;
11
+ releaseSigningKeys: Readonly<Record<string, string>>;
12
+ releaseFeed: ReleaseFeed;
13
+ sidecars?: readonly string[];
14
+ }>;
15
+ /** Read only the release identity and public keys shipped beside the Bun entry. */
16
+ export declare function readPackagedReleaseConfig(entryDirectory: string, identity: PackagedReleaseIdentity): Promise<PackagedDesktopRelease>;
17
+ /** Validate packaged trust against the generated app's compile-time identity. */
18
+ export declare function parsePackagedReleaseConfig(value: unknown, identity: PackagedReleaseIdentity): PackagedDesktopRelease;
@@ -0,0 +1,60 @@
1
+ import { isRepositorySlug } from "./signed-release.js";
2
+ import { parseReleaseConfig } from "../release/release-config.js";
3
+ import { openUnchangedFile, sameFile } from "./stable-file.js";
4
+ import { lstat } from "node:fs/promises";
5
+ import { isAbsolute, join, resolve } from "node:path";
6
+ // src/updates/packaged-release-config.ts
7
+ var MAX_CONFIG_BYTES = 262144;
8
+ var record = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
9
+ /** Read only the release identity and public keys shipped beside the Bun entry. */
10
+ async function readPackagedReleaseConfig(entryDirectory, identity) {
11
+ if (typeof entryDirectory !== "string" || !isAbsolute(entryDirectory) || resolve(entryDirectory) !== entryDirectory) throw new Error("Invalid packaged release config location");
12
+ const path = join(entryDirectory, "..", "release-config.json");
13
+ const info = await lstat(path);
14
+ if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > MAX_CONFIG_BYTES) throw new Error("Invalid packaged release config file");
15
+ const { file, before } = await openUnchangedFile(path, info, "Invalid packaged release config file");
16
+ try {
17
+ const bytes = Buffer.alloc(before.size);
18
+ const read = await file.read(bytes, 0, bytes.length, 0);
19
+ const after = await file.stat();
20
+ if (read.bytesRead !== before.size || !sameFile(before, after)) throw new Error("Invalid packaged release config file");
21
+ let value;
22
+ try {
23
+ value = JSON.parse(bytes.toString("utf8"));
24
+ } catch {
25
+ throw new Error("Invalid packaged release config JSON");
26
+ }
27
+ return parsePackagedReleaseConfig(value, identity);
28
+ } finally {
29
+ await file.close();
30
+ }
31
+ }
32
+ /** Validate packaged trust against the generated app's compile-time identity. */
33
+ function parsePackagedReleaseConfig(value, identity) {
34
+ if (!record(value) || typeof identity?.slug !== "string" || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(identity.slug) || typeof identity.identifier !== "string" || !/^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)+$/.test(identity.identifier) || typeof identity.name !== "string" || !identity.name.trim() || identity.name.length > 80 || typeof value.appId !== "string" || value.appId !== identity.identifier || typeof value.appName !== "string" || value.appName !== identity.name || typeof value.repository !== "string" || !isRepositorySlug(value.repository)) throw new Error("Packaged release identity does not match the application");
35
+ let parsedConfig;
36
+ try {
37
+ parsedConfig = parseReleaseConfig(value);
38
+ } catch {
39
+ throw new Error("Invalid packaged release configuration");
40
+ }
41
+ const releaseFeed = parsedConfig.feed;
42
+ if (releaseFeed.app !== identity.slug) throw new Error("Packaged release feed identity mismatch");
43
+ if (!record(value.releaseSigningKeys) || Object.keys(value.releaseSigningKeys).length > 8) throw new Error("Invalid packaged release signing keys");
44
+ const keys = Object.create(null);
45
+ for (const [id, encoded] of Object.entries(value.releaseSigningKeys)) {
46
+ if (!/^[a-z0-9][a-z0-9._-]{0,63}$/.test(id) || typeof encoded !== "string") throw new Error("Invalid packaged release signing keys");
47
+ const raw = Buffer.from(encoded, "base64");
48
+ if (raw.length !== 32 || raw.toString("base64") !== encoded) throw new Error("Invalid packaged release signing keys");
49
+ keys[id] = encoded;
50
+ }
51
+ return Object.freeze({
52
+ appId: value.appId,
53
+ appName: value.appName,
54
+ repository: value.repository,
55
+ releaseSigningKeys: Object.freeze(keys),
56
+ releaseFeed: Object.freeze({ ...releaseFeed }),
57
+ sidecars: Object.freeze([...parsedConfig.sidecars])
58
+ });
59
+ }
60
+ export { parsePackagedReleaseConfig, readPackagedReleaseConfig };
@@ -0,0 +1,44 @@
1
+ import { type UpdateLaunchGateRecoveryResult } from './update-launch-gate';
2
+ import type { UpdateChannel } from './release-version';
3
+ import type { UpdateResumeResult } from './update-resume';
4
+ import { type PackagedDesktopRelease, type PackagedReleaseIdentity } from './packaged-release-config';
5
+ export type PreparedPackagedUpdateStartup = Readonly<{
6
+ status: 'ready';
7
+ release: PackagedDesktopRelease;
8
+ currentVersion: string;
9
+ savedChannel: UpdateChannel;
10
+ resume: UpdateResumeResult;
11
+ retainedClientAssets: readonly {
12
+ root: string;
13
+ immutableAssets: ReadonlyMap<string, string>;
14
+ }[];
15
+ releaseLease: {
16
+ release(): Promise<void>;
17
+ };
18
+ }>;
19
+ /**
20
+ * Validate bundled release trust, select only its channel from private app state,
21
+ * then reconcile and lease retained assets before a managed packaged host starts.
22
+ */
23
+ export declare function preparePackagedUpdateStartup(options: {
24
+ entryDirectory: string;
25
+ dataDirectory: string;
26
+ identity: PackagedReleaseIdentity;
27
+ loadManifest(root: string): Promise<ReadonlyMap<string, string>>;
28
+ }): Promise<PreparedPackagedUpdateStartup>;
29
+ /**
30
+ * Pre-launch dispatcher for an update launch gate left behind by a helper
31
+ * that crashed mid-transition. Call it before launch admission, while this
32
+ * process holds no instance lease. When no gate exists it returns `absent`
33
+ * without reading release configuration. Otherwise it runs the exact schema-3
34
+ * journal and tree recovery under the bundled release trust: `recovered`
35
+ * clears the gate; `busy` (another instance or transition holds a lock),
36
+ * `helper-live` and `unreconciled` leave every artifact in place and the gate
37
+ * blocked. It never weakens the gate: recovery still requires the helper's
38
+ * liveness lock to be free and an exact gate, journal and tree match.
39
+ */
40
+ export declare function recoverPackagedUpdateLaunchGate(options: {
41
+ entryDirectory: string;
42
+ dataDirectory: string;
43
+ identity: PackagedReleaseIdentity;
44
+ }): Promise<UpdateLaunchGateRecoveryResult>;
@@ -0,0 +1,81 @@
1
+ import { readPackagedVersion } from "../native/app-bundle.js";
2
+ import { feedUrl } from "./release-feed.js";
3
+ import { readUpdateLaunchGate } from "./update-launch-gate.js";
4
+ import { createUpdateChannelStore, releaseChannelForUpdate } from "./update-channel-store.js";
5
+ import { recoverUpdateLaunchGate } from "./update-resume.js";
6
+ import { installTarget, resumeAndAcquireRetainedDesktopClientAssets, updatePlatform, updateTrust } from "./desktop-updater.js";
7
+ import { readPackagedReleaseConfig } from "./packaged-release-config.js";
8
+ import { realpath } from "node:fs/promises";
9
+ // src/updates/packaged-update-startup.ts
10
+ /**
11
+ * Validate bundled release trust, select only its channel from private app state,
12
+ * then reconcile and lease retained assets before a managed packaged host starts.
13
+ */
14
+ async function preparePackagedUpdateStartup(options) {
15
+ const { release, trust, savedChannel, channel } = await packagedReleaseTrust(options);
16
+ const { version: currentVersion } = readPackagedVersion(options.entryDirectory);
17
+ const resumed = await resumeAndAcquireRetainedDesktopClientAssets({
18
+ entryDirectory: options.entryDirectory,
19
+ currentVersion,
20
+ trust: {
21
+ ...trust,
22
+ channel
23
+ },
24
+ loadManifest: options.loadManifest
25
+ });
26
+ if (resumed.status !== "ready") throw new Error(`Packaged update recovery is ${resumed.status}; startup is paused`);
27
+ return {
28
+ status: "ready",
29
+ release,
30
+ currentVersion,
31
+ savedChannel,
32
+ resume: resumed.resume,
33
+ retainedClientAssets: resumed.retainedClientAssets,
34
+ releaseLease: { release: resumed.release }
35
+ };
36
+ }
37
+ /** Bundled release trust narrowed to the channel saved in private app state. */
38
+ async function packagedReleaseTrust(options) {
39
+ const release = await readPackagedReleaseConfig(options.entryDirectory, options.identity);
40
+ const platform = updatePlatform();
41
+ if (!platform) throw new Error("Packaged updates are unavailable on this platform");
42
+ const trust = updateTrust(platform, {
43
+ appId: release.appId,
44
+ repository: release.repository,
45
+ feed: feedUrl(release.releaseFeed),
46
+ keys: release.releaseSigningKeys
47
+ });
48
+ if (!trust) throw new Error("Packaged release signing keys are not configured");
49
+ const stateDirectory = await realpath(options.dataDirectory);
50
+ const savedChannel = await createUpdateChannelStore(stateDirectory).read();
51
+ return {
52
+ release,
53
+ platform,
54
+ trust,
55
+ savedChannel,
56
+ channel: releaseChannelForUpdate(savedChannel)
57
+ };
58
+ }
59
+ /**
60
+ * Pre-launch dispatcher for an update launch gate left behind by a helper
61
+ * that crashed mid-transition. Call it before launch admission, while this
62
+ * process holds no instance lease. When no gate exists it returns `absent`
63
+ * without reading release configuration. Otherwise it runs the exact schema-3
64
+ * journal and tree recovery under the bundled release trust: `recovered`
65
+ * clears the gate; `busy` (another instance or transition holds a lock),
66
+ * `helper-live` and `unreconciled` leave every artifact in place and the gate
67
+ * blocked. It never weakens the gate: recovery still requires the helper's
68
+ * liveness lock to be free and an exact gate, journal and tree match.
69
+ */
70
+ async function recoverPackagedUpdateLaunchGate(options) {
71
+ const platform = updatePlatform();
72
+ if (!platform) throw new Error("Packaged updates are unavailable on this platform");
73
+ const target = await realpath(installTarget(options.entryDirectory, platform));
74
+ if (!await readUpdateLaunchGate(target)) return "absent";
75
+ const { trust, channel } = await packagedReleaseTrust(options);
76
+ return recoverUpdateLaunchGate(target, {
77
+ ...trust,
78
+ channel
79
+ });
80
+ }
81
+ export { preparePackagedUpdateStartup, recoverPackagedUpdateLaunchGate };
@@ -1,26 +1,27 @@
1
- import type { UpdateHelperRequest } from './update-helper';
1
+ import { type ReleaseTrust } from './signed-release';
2
+ import { type UpdateHelperRequest } from './update-helper';
2
3
  /**
3
4
  * Start the post-exit update helper for a staged update, then resolve once the
4
- * helper has verified this app's processes. The helper runs from copies of the
5
- * app's Bun runtime and bundled helper script inside the private staging
6
- * directory, because the install tree is about to move.
7
- *
8
- * Use it as the `arm` step of `scheduleUpdateShutdown`: quit only after it
9
- * resolves, and call `cancel()` if the quit is vetoed.
5
+ * helper has verified the requested processes and published readiness for
6
+ * this helper owner and generation. The helper runs from copied app files
7
+ * because the install tree is about to move.
10
8
  */
11
9
  export declare function armUpdateHelper(options: {
12
10
  target: string;
13
11
  platform: UpdateHelperRequest['platform'];
14
12
  version: string;
13
+ currentVersion: string;
14
+ trust: ReleaseTrust;
15
15
  /** Absolute path of the bundled helper script shipped with the app. */
16
16
  helperScript: string;
17
17
  /** Processes that must exit before the swap. Defaults to this process and its launcher parent. */
18
18
  processes?: UpdateHelperRequest['processes'];
19
19
  processTimeoutMs?: number;
20
20
  armTimeoutMs?: number;
21
- /** Maximum time to confirm helper exit before cancellation preserves recovery state. */
21
+ /** Maximum time to wait for a cooperative helper cancellation before preserving recovery state. */
22
22
  cancelTimeoutMs?: number;
23
23
  env?: Record<string, string | undefined>;
24
24
  }): Promise<{
25
25
  cancel(): Promise<void>;
26
+ processDeadline: number;
26
27
  }>;