@adea-ai/app-framework 0.28.2 → 0.28.3

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.
package/README.md CHANGED
@@ -17,6 +17,22 @@ HEAD requests so their resulting length can be reported.
17
17
  Responses are buffered snapshots by default. Set `streamAssets: true` for an
18
18
  immutable asset root, or `streamClientAssets: true` on `startDesktopHost`; streamed
19
19
  bodies are not snapshots and read errors can surface after response headers are sent.
20
+ A streamed body opens its file on the first read, not when the response is created,
21
+ and closes it at end of file, on cancellation, on a read error, and when the host
22
+ stops (`server.stop(true)` cancels open bodies; a retained-lease host drains them
23
+ before releasing its lease). A client that stops reading without closing the
24
+ connection is bounded by the server's 10 s idle timeout, which cancels the body and
25
+ closes the file, so open asset files never exceed open asset connections. The host
26
+ also admits at most `maxOpenClientAssetFiles` (default 32) open asset files; later
27
+ readers wait in order without a descriptor, and a waiting reader that is cancelled
28
+ leaves the queue. Streamed read buffers are therefore bounded by that limit times one 64 KiB
29
+ read chunk, and a response that is never read holds neither a descriptor nor a slot.
30
+ `serveClient` callers get the same bound by passing a shared `openFileLimit` from
31
+ `createClientAssetFileLimit`. The
32
+ opened file is compared with the path's earlier `stat` (device, inode, size and
33
+ times) and a mismatch fails the stream instead of serving other bytes; containment
34
+ is still checked by path, so streaming assumes nobody swaps symlinks inside the
35
+ root while it is served.
20
36
 
21
37
  `serveClient` is one of the framework's desktop building blocks. Source adapted from
22
38
  `adea-ai/adea` at commit `6155d18941acbb14d9099bc2a33a50ee9683a068`,
@@ -398,6 +414,9 @@ parents are accepted). On Linux a group counts as private when `/etc/passwd` and
398
414
  refused. Cleanup removes the staging directory only while it is still the one the
399
415
  run created. A run killed mid-generation can leave a `.framework-app-*`
400
416
  directory beside the destination; it never blocks a retry and is safe to delete.
417
+ On Windows the parent check is skipped: its ACLs are not visible to Node, so
418
+ generation assumes the destination parent (normally under the user profile) does
419
+ not let other accounts rename or delete its entries.
401
420
  Run `bun run test:generated` for an isolated packed-consumer web
402
421
  build and browser check; `bun scripts/test-generated.ts --desktop` additionally
403
422
  builds the native bundle for the current platform.
@@ -653,6 +672,13 @@ newer release inventory or channel pointer.
653
672
  - `adea-finalize-release --local <config> <tag>` verifies a published release
654
673
  from the feed and lists its verified and missing platforms.
655
674
 
675
+ Local finalization and promotion stream each required update archive and compare
676
+ its bytes with the signed descriptor, once per distinct payload. This can transfer
677
+ large archives. Payloads over the desktop updater's 1 GiB limit are rejected; the
678
+ per-payload request uses a ten-minute abort signal and depends on transport
679
+ cooperation. A fetch or body read that never settles can still stall verification.
680
+ This point-in-time check does not fence later feed changes.
681
+
656
682
  ### Updating the framework pin
657
683
 
658
684
  An application references the framework in three places: the exact npm
@@ -52,6 +52,9 @@ function createAppearanceContext(ui) {
52
52
  });
53
53
  return {
54
54
  ...ui,
55
+ get themes() {
56
+ return ui.themes;
57
+ },
55
58
  update: (patch) => ui.setSelection(selectionFromPreferences(patch)),
56
59
  preview
57
60
  };
@@ -10,11 +10,31 @@ export declare function verifyClientAssetCached(cache: ClientAssetVerificationCa
10
10
  export declare function createClientAssetVerificationCache(maxEntries?: number): ClientAssetVerificationCache;
11
11
  type AssetFile = Pick<FileHandle, 'close' | 'stat' | 'read'>;
12
12
  type OpenAsset = (path: string, flags: 'r') => Promise<AssetFile>;
13
+ /** One admission to hold an asset file open. */
14
+ type ClientAssetFileSlot = {
15
+ /** Resolves true once admitted, or false if withdrawn first. */
16
+ readonly ready: Promise<boolean>;
17
+ /** Leave the queue if still waiting; does nothing once admitted. */
18
+ withdraw(): void;
19
+ /** Free an admitted slot, or withdraw a waiting one. Idempotent. */
20
+ release(): void;
21
+ };
22
+ /** Admission limit on streamed asset files a host holds open at once. */
23
+ export type ClientAssetFileLimit = {
24
+ acquire(): ClientAssetFileSlot;
25
+ readonly active: number;
26
+ readonly waiting: number;
27
+ };
28
+ /**
29
+ * Bound open streamed asset files. Readers beyond the limit wait, first in first out,
30
+ * without holding a descriptor; a waiting reader that is cancelled leaves the queue.
31
+ */
32
+ export declare function createClientAssetFileLimit(maxOpenFiles?: number): ClientAssetFileLimit;
13
33
  /**
14
34
  * Stream an immutable bundled asset. This is not a snapshot: detected file changes
15
35
  * fail the stream, and a read error can surface after response headers have been sent.
16
36
  */
17
- export declare function createClientAssetStream(path: string, expected: Stats, openAsset?: OpenAsset): ReadableStream<Uint8Array>;
37
+ export declare function createClientAssetStream(path: string, expected: Stats, openAsset?: OpenAsset, limit?: ClientAssetFileLimit): ReadableStream<Uint8Array>;
18
38
  /** Verify an opted-in asset before sending immutable cache headers without buffering it. */
19
39
  export declare function verifyClientAssetDigest(path: string, expected: Stats, expectedSha256: string, openAsset?: OpenAsset): Promise<boolean>;
20
40
  export {};
@@ -70,11 +70,75 @@ function createClientAssetVerificationCache(maxEntries = 256) {
70
70
  };
71
71
  }
72
72
  /**
73
+ * Bound open streamed asset files. Readers beyond the limit wait, first in first out,
74
+ * without holding a descriptor; a waiting reader that is cancelled leaves the queue.
75
+ */
76
+ function createClientAssetFileLimit(maxOpenFiles = 32) {
77
+ if (!Number.isInteger(maxOpenFiles) || maxOpenFiles < 1 || maxOpenFiles > 1024) throw new Error("Invalid client asset file limit");
78
+ let active = 0;
79
+ const queue = [];
80
+ const next = () => {
81
+ while (active < maxOpenFiles && queue.length) {
82
+ const waiter = queue.shift();
83
+ if (waiter.withdrawn) continue;
84
+ active++;
85
+ waiter.admit();
86
+ }
87
+ };
88
+ return {
89
+ get active() {
90
+ return active;
91
+ },
92
+ get waiting() {
93
+ return queue.filter((waiter) => !waiter.withdrawn).length;
94
+ },
95
+ acquire() {
96
+ let state = "waiting";
97
+ let settle;
98
+ const ready = new Promise((resolve) => settle = resolve);
99
+ const waiter = {
100
+ withdrawn: false,
101
+ admit() {
102
+ state = "admitted";
103
+ settle(true);
104
+ }
105
+ };
106
+ const withdraw = () => {
107
+ if (state !== "waiting") return;
108
+ state = "done";
109
+ waiter.withdrawn = true;
110
+ const index = queue.indexOf(waiter);
111
+ if (index !== -1) queue.splice(index, 1);
112
+ settle(false);
113
+ };
114
+ queue.push(waiter);
115
+ next();
116
+ return {
117
+ ready,
118
+ withdraw,
119
+ release() {
120
+ if (state === "waiting") return withdraw();
121
+ if (state !== "admitted") return;
122
+ state = "done";
123
+ active--;
124
+ next();
125
+ }
126
+ };
127
+ }
128
+ };
129
+ }
130
+ /**
73
131
  * Stream an immutable bundled asset. This is not a snapshot: detected file changes
74
132
  * fail the stream, and a read error can surface after response headers have been sent.
75
133
  */
76
- function createClientAssetStream(path, expected, openAsset = open) {
134
+ function createClientAssetStream(path, expected, openAsset = open, limit) {
77
135
  let file;
136
+ let slot;
137
+ const releaseSlot = () => {
138
+ const current = slot;
139
+ slot = void 0;
140
+ current?.release();
141
+ };
78
142
  let opening;
79
143
  let closing;
80
144
  let pulling;
@@ -86,12 +150,15 @@ function createClientAssetStream(path, expected, openAsset = open) {
86
150
  if (closing) return closing;
87
151
  const current = file;
88
152
  file = void 0;
89
- if (!current) return Promise.resolve();
153
+ if (!current) {
154
+ releaseSlot();
155
+ return Promise.resolve();
156
+ }
90
157
  const pending = current.close().catch((error) => {
91
158
  closeFailed = true;
92
159
  closeFailure = error;
93
160
  throw error;
94
- });
161
+ }).finally(releaseSlot);
95
162
  closing = pending;
96
163
  const clear = () => {
97
164
  if (closing === pending) closing = void 0;
@@ -103,7 +170,21 @@ function createClientAssetStream(path, expected, openAsset = open) {
103
170
  if (cancelled || file) return Promise.resolve();
104
171
  if (opening) return opening;
105
172
  const pending = (async () => {
106
- const opened = await openAsset(path, "r");
173
+ if (limit && !slot) {
174
+ const admission = limit.acquire();
175
+ slot = admission;
176
+ if (!await admission.ready || cancelled) {
177
+ releaseSlot();
178
+ return;
179
+ }
180
+ }
181
+ let opened;
182
+ try {
183
+ opened = await openAsset(path, "r");
184
+ } catch (error) {
185
+ releaseSlot();
186
+ throw error;
187
+ }
107
188
  if (cancelled) {
108
189
  try {
109
190
  await opened.close();
@@ -111,6 +192,8 @@ function createClientAssetStream(path, expected, openAsset = open) {
111
192
  closeFailed = true;
112
193
  closeFailure = error;
113
194
  throw error;
195
+ } finally {
196
+ releaseSlot();
114
197
  }
115
198
  return;
116
199
  }
@@ -171,6 +254,7 @@ function createClientAssetStream(path, expected, openAsset = open) {
171
254
  },
172
255
  async cancel() {
173
256
  cancelled = true;
257
+ slot?.withdraw();
174
258
  const pending = opening;
175
259
  if (pending) await pending.catch(() => {});
176
260
  const activePull = pulling;
@@ -199,4 +283,4 @@ async function verifyClientAssetDigest(path, expected, expectedSha256, openAsset
199
283
  await file.close();
200
284
  }
201
285
  }
202
- export { createClientAssetStream, createClientAssetVerificationCache, verifyClientAssetCached, verifyClientAssetDigest };
286
+ export { createClientAssetFileLimit, createClientAssetStream, createClientAssetVerificationCache, verifyClientAssetCached, verifyClientAssetDigest };
@@ -1,5 +1,5 @@
1
- import { type ClientAssetVerificationCache } from './client-asset-stream';
2
- export { createClientAssetVerificationCache, type ClientAssetVerificationCache, } from './client-asset-stream';
1
+ import { type ClientAssetFileLimit, type ClientAssetVerificationCache } from './client-asset-stream';
2
+ export { createClientAssetFileLimit, createClientAssetVerificationCache, type ClientAssetFileLimit, type ClientAssetVerificationCache, } from './client-asset-stream';
3
3
  export { CLIENT_ASSET_ASSET_PATTERN, CLIENT_ASSET_CHUNK_PATTERN, CLIENT_ASSET_MANIFEST_FILE, clientAssetAssetEvidenceKey, clientAssetChunkEvidenceKey, createClientAssetManifest, parseClientAssetManifest, snapshotClientAssetAllowlist, type ClientAssetManifest, } from './client-asset-manifest';
4
4
  export type ClientOptions = {
5
5
  root: string;
@@ -11,6 +11,8 @@ export type ClientOptions = {
11
11
  immutableAssets?: ReadonlyMap<string, string>;
12
12
  /** Bounded, host-lifetime cache of successful checks for unchanged immutable files. */
13
13
  verificationCache?: ClientAssetVerificationCache;
14
+ /** Admission limit shared by streamed GET bodies; readers past it wait without a descriptor. */
15
+ openFileLimit?: ClientAssetFileLimit;
14
16
  };
15
17
  /** A caller-owned, immutable client root kept available for the resolver's lifetime. */
16
18
  export type RetainedClientAssetRoot = Readonly<{
@@ -1,5 +1,5 @@
1
1
  import { CLIENT_ASSET_ASSET_PATTERN, CLIENT_ASSET_CHUNK_PATTERN, CLIENT_ASSET_MANIFEST_FILE, clientAssetAssetEvidenceKey, clientAssetChunkEvidenceKey, createClientAssetManifest, parseClientAssetManifest, snapshotClientAssetAllowlist } from "./client-asset-manifest.js";
2
- import { createClientAssetStream, createClientAssetVerificationCache, verifyClientAssetCached, verifyClientAssetDigest } from "./client-asset-stream.js";
2
+ import { createClientAssetFileLimit, createClientAssetStream, createClientAssetVerificationCache, verifyClientAssetCached, verifyClientAssetDigest } from "./client-asset-stream.js";
3
3
  import { open, readFile, realpath, stat } from "node:fs/promises";
4
4
  import { extname, join, posix, relative, resolve, sep } from "node:path";
5
5
  import { createHash } from "node:crypto";
@@ -185,7 +185,7 @@ async function serveClientInternal(request, options, onMissingRoot) {
185
185
  }
186
186
  if (options.streamAssets === true && request.method === "GET" && !(extension === ".html" && options.transformHtml)) {
187
187
  const cacheVerified = immutableSha256 !== void 0 && await matchesManifestDigest(physical, metadata, immutableSha256, options.verificationCache);
188
- return new Response(createClientAssetStream(physical, metadata), { headers: {
188
+ return new Response(createClientAssetStream(physical, metadata, void 0, options.openFileLimit), { headers: {
189
189
  "content-type": contentType,
190
190
  "cache-control": cacheVerified ? IMMUTABLE_CACHE_CONTROL : "no-store",
191
191
  "content-length": String(metadata.size)
@@ -201,4 +201,4 @@ async function serveClientInternal(request, options, onMissingRoot) {
201
201
  ...request.method === "HEAD" ? { "content-length": String(body.byteLength) } : {}
202
202
  } });
203
203
  }
204
- export { CLIENT_ASSET_ASSET_PATTERN, CLIENT_ASSET_CHUNK_PATTERN, CLIENT_ASSET_MANIFEST_FILE, clientAssetAssetEvidenceKey, clientAssetChunkEvidenceKey, createClientAssetManifest, createClientAssetResolver, createClientAssetVerificationCache, loadClientAssetManifest, parseClientAssetManifest, serveClient, snapshotClientAssetAllowlist };
204
+ export { CLIENT_ASSET_ASSET_PATTERN, CLIENT_ASSET_CHUNK_PATTERN, CLIENT_ASSET_MANIFEST_FILE, clientAssetAssetEvidenceKey, clientAssetChunkEvidenceKey, createClientAssetFileLimit, createClientAssetManifest, createClientAssetResolver, createClientAssetVerificationCache, loadClientAssetManifest, parseClientAssetManifest, serveClient, snapshotClientAssetAllowlist };
@@ -9,6 +9,8 @@ type DesktopHostOptions<Principal, Window extends NativeWindow> = {
9
9
  clientRoot: string;
10
10
  /** Stream only when the client root is immutable; defaults to buffered snapshots. */
11
11
  streamClientAssets?: boolean;
12
+ /** Streamed asset files held open at once (1-1024, default 32); later readers wait. */
13
+ maxOpenClientAssetFiles?: number;
12
14
  /** Explicit content-verified cache allowlist; omitted assets and HTML stay no-store. */
13
15
  immutableClientAssets?: ReadonlyMap<string, string>;
14
16
  /** Caller-owned roots kept immutable and available until host responses finish. */
@@ -1,5 +1,5 @@
1
1
  import { createChannelAuthority } from "./channel-handshake.js";
2
- import { createClientAssetVerificationCache } from "./client-asset-stream.js";
2
+ import { createClientAssetFileLimit, createClientAssetVerificationCache } from "./client-asset-stream.js";
3
3
  import { createClientAssetResolver } from "./client-assets.js";
4
4
  import { ClientResponseLifecycleClosedError, createClientResponseLifecycleTracker } from "./client-response-lifecycle.js";
5
5
  import { createLoopbackPolicy, matchesLoopbackOrigin } from "./loopback-policy.js";
@@ -31,10 +31,12 @@ function createDesktopHost(options, lifecycle) {
31
31
  const dispatchTimeoutMs = options.dispatchTimeoutMs;
32
32
  if (!isValidGatewayTimeoutMs(dispatchTimeoutMs)) throw new Error("Invalid gateway limits");
33
33
  if (options.streamClientAssets !== void 0 && typeof options.streamClientAssets !== "boolean") throw new Error("Invalid desktop client asset policy");
34
+ const clientAssetFileLimit = createClientAssetFileLimit(options.maxOpenClientAssetFiles ?? 32);
34
35
  const clientAssetVerificationCache = createClientAssetVerificationCache();
35
36
  const serveAssets = createClientAssetResolver({
36
37
  root: options.clientRoot,
37
38
  streamAssets: options.streamClientAssets,
39
+ openFileLimit: clientAssetFileLimit,
38
40
  immutableAssets: options.immutableClientAssets,
39
41
  verificationCache: clientAssetVerificationCache
40
42
  }, options.retainedClientAssets);
@@ -1 +1 @@
1
- {"version":"0.28.2","commit":"467eed71bb6f007ae6c25caae2e28534622a2b7d"}
1
+ {"version":"0.28.3","commit":"4c25930a1bae82800d320983b3b3943897361e1d"}
@@ -1,6 +1,7 @@
1
1
  import type { FeedRelease, ReleaseLedger } from '../updates/release-feed';
2
2
  import { type GitRun } from './local-provenance';
3
3
  import type { ReleaseCutProvider } from './release-cut';
4
+ import type { HistoryRewrite } from './release-config';
4
5
  /**
5
6
  * Release feed adapter for local release cuts. The feed's ledger replaces the GitHub release
6
7
  * listing and the local checkout's fetched `origin/main` replaces GitHub's compare API. A dev
@@ -20,4 +21,6 @@ export declare function createFeedReleaseCutProvider(options: {
20
21
  releasedVersion: string;
21
22
  /** The payload names this host builds for a tag. */
22
23
  payloads(tag: string): readonly string[];
24
+ /** Reviewed source commits a history rewrite replaced (release config `historyRewrite`). */
25
+ historyRewrite?: readonly HistoryRewrite[];
23
26
  }): ReleaseCutProvider;
@@ -20,6 +20,27 @@ function highestCounter(tags, version, tagsOnly = false) {
20
20
  }
21
21
  return highest;
22
22
  }
23
+ async function commitTree(git, sha) {
24
+ const tree = await git([
25
+ "rev-parse",
26
+ "--verify",
27
+ `${sha}^{tree}`
28
+ ]);
29
+ if (!/^[a-f0-9]{40}$/.test(tree)) throw new Error("Invalid commit tree");
30
+ return tree;
31
+ }
32
+ async function hasCommit(git, sha) {
33
+ try {
34
+ await git([
35
+ "cat-file",
36
+ "-e",
37
+ `${sha}^{commit}`
38
+ ]);
39
+ return true;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
23
44
  function checkTag(tag) {
24
45
  if (!/^v\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(tag) || tag.length > 65) throw new Error("Invalid release tag");
25
46
  }
@@ -34,6 +55,7 @@ function checkTag(tag) {
34
55
  */
35
56
  function createFeedReleaseCutProvider(options) {
36
57
  const { ledger, git } = options;
58
+ const rewrites = options.historyRewrite ?? [];
37
59
  const created = /* @__PURE__ */ new Map();
38
60
  return {
39
61
  async find(tag) {
@@ -67,7 +89,14 @@ function createFeedReleaseCutProvider(options) {
67
89
  },
68
90
  isMainAncestor: (sha) => gitIsAncestor(git, sha, "refs/remotes/origin/main"),
69
91
  async isAncestor(older, newer) {
70
- return older !== newer && await gitIsAncestor(git, older, newer);
92
+ if (older === newer) return false;
93
+ const rewrite = rewrites.find((entry) => entry.from === older);
94
+ if (!rewrite) return gitIsAncestor(git, older, newer);
95
+ if (!await gitIsAncestor(git, rewrite.to, "refs/remotes/origin/main")) throw new Error("History rewrite target is not on main");
96
+ if (await commitTree(git, rewrite.to) !== rewrite.tree) throw new Error("History rewrite tree does not match");
97
+ const oldTree = await hasCommit(git, rewrite.from) ? await commitTree(git, rewrite.from) : null;
98
+ if (oldTree !== null && oldTree !== rewrite.tree) throw new Error("History rewrite tree does not match");
99
+ return rewrite.to !== newer && await gitIsAncestor(git, rewrite.to, newer);
71
100
  },
72
101
  async create(identity) {
73
102
  checkTag(identity.tag);
@@ -8,8 +8,9 @@ export declare function readFeedRelease(feed: ReleaseFeed, tag: string, fetcher:
8
8
  * Verify a published release from the feed: its ledger entry and release.json agree, and every
9
9
  * signed descriptor it lists for `channels` (all of its channels by default) has the listed
10
10
  * bytes, authenticates against the app's trust and names a listed payload with the same size
11
- * and digest. Returns the release platforms whose descriptors all verified; a platform whose
12
- * payload is listed but whose descriptors are missing fails.
11
+ * and digest. Streams each distinct payload and verifies its bytes against the descriptor.
12
+ * Returns the release platforms whose descriptors all verified; a platform whose payload is
13
+ * listed but whose descriptors are missing fails.
13
14
  */
14
15
  export declare function verifyFeedRelease(options: {
15
16
  config: unknown;
@@ -1,4 +1,4 @@
1
- import { verifyReleaseDescriptor } from "../updates/signed-release.js";
1
+ import { verifyReleaseDescriptor, verifyReleaseStream } from "../updates/signed-release.js";
2
2
  import { feedUrl, parseFeedRelease, parseReleaseLedger } from "../updates/release-feed.js";
3
3
  import { discoveryPolicies } from "./release-config.js";
4
4
  import { discardStream } from "../shared/discard-stream.js";
@@ -7,6 +7,7 @@ import { createHash } from "node:crypto";
7
7
  // src/release/feed-state.ts
8
8
  var MAX_LEDGER_BYTES = 1048576;
9
9
  var MAX_RELEASE_BYTES = 262144;
10
+ var MAX_PAYLOAD_BYTES = 1073741824;
10
11
  /** Read one feed object anonymously over HTTPS; null when the feed answers 404. */
11
12
  async function readFeedObject(feed, path, fetcher, maximum) {
12
13
  const response = await fetchReleaseLocation(`${feedUrl(feed)}${path}`, feedUrl(feed), fetcher, AbortSignal.timeout(6e4));
@@ -40,12 +41,42 @@ async function readFeedRelease(feed, tag, fetcher) {
40
41
  if (release.tag !== tag) throw new Error("Feed release names another tag");
41
42
  return release;
42
43
  }
44
+ /** Stream the actual published payload and compare it to its authenticated descriptor. */
45
+ async function verifyFeedPayload(feed, descriptor, fetcher) {
46
+ if (descriptor.asset.size > MAX_PAYLOAD_BYTES) throw new Error("Release payload exceeds the desktop update limit");
47
+ const signal = AbortSignal.timeout(6e5);
48
+ const response = await fetchReleaseLocation(descriptor.asset.url, feedUrl(feed), fetcher, signal);
49
+ if (signal.aborted) {
50
+ discardStream(response.body);
51
+ throw new Error("Release payload verification cancelled");
52
+ }
53
+ if (response.status !== 200 || !response.body) {
54
+ discardStream(response.body);
55
+ throw new Error("Release payload is unavailable");
56
+ }
57
+ const length = response.headers.get("content-length");
58
+ if (length !== null && (!/^\d+$/.test(length) || Number(length) !== descriptor.asset.size)) {
59
+ discardStream(response.body);
60
+ throw new Error("Release payload size differs from its descriptor");
61
+ }
62
+ const body = response.body;
63
+ async function* chunks() {
64
+ for await (const chunk of body) {
65
+ if (signal.aborted) throw new Error("Release payload verification cancelled");
66
+ yield chunk;
67
+ }
68
+ }
69
+ const valid = await verifyReleaseStream(chunks(), descriptor);
70
+ if (signal.aborted) throw new Error("Release payload verification cancelled");
71
+ if (!valid) throw new Error("Release payload differs from its descriptor");
72
+ }
43
73
  /**
44
74
  * Verify a published release from the feed: its ledger entry and release.json agree, and every
45
75
  * signed descriptor it lists for `channels` (all of its channels by default) has the listed
46
76
  * bytes, authenticates against the app's trust and names a listed payload with the same size
47
- * and digest. Returns the release platforms whose descriptors all verified; a platform whose
48
- * payload is listed but whose descriptors are missing fails.
77
+ * and digest. Streams each distinct payload and verifies its bytes against the descriptor.
78
+ * Returns the release platforms whose descriptors all verified; a platform whose payload is
79
+ * listed but whose descriptors are missing fails.
49
80
  */
50
81
  async function verifyFeedRelease(options) {
51
82
  const { config, feed, tag, fetch: fetcher } = options;
@@ -54,6 +85,7 @@ async function verifyFeedRelease(options) {
54
85
  if (!release || !entry || entry.kind !== release.channel) throw new Error("Release is not published consistently");
55
86
  const policies = discoveryPolicies(config, tag).filter((policy) => !options.channels || options.channels.includes(policy.channel));
56
87
  const platforms = /* @__PURE__ */ new Set();
88
+ const verifiedPayloads = /* @__PURE__ */ new Set();
57
89
  for (const policy of policies) {
58
90
  const payload = release.assets.find((asset) => asset.name === policy.payload);
59
91
  if (!payload) continue;
@@ -71,6 +103,10 @@ async function verifyFeedRelease(options) {
71
103
  keys: options.keys
72
104
  });
73
105
  if (!verified.ok || verified.descriptor.tag !== tag || verified.descriptor.sourceSha !== entry.sourceSha || verified.descriptor.asset.name !== payload.name || verified.descriptor.asset.size !== payload.size || verified.descriptor.asset.sha256 !== payload.sha256) throw new Error(`Untrusted signed descriptor: ${policy.name}`);
106
+ if (!verifiedPayloads.has(payload.name)) {
107
+ await verifyFeedPayload(feed, verified.descriptor, fetcher);
108
+ verifiedPayloads.add(payload.name);
109
+ }
74
110
  platforms.add(policy.platform);
75
111
  }
76
112
  if (!platforms.size) throw new Error("Release has no verified platform");
@@ -45,10 +45,10 @@ async function originRepository(git) {
45
45
  ]);
46
46
  const part = "[A-Za-z0-9_.-]+";
47
47
  const github = new RegExp(`^(?:https://github\\.com/|git@github\\.com:)(${part}/${part}?)(?:\\.git)?/?$`).exec(url);
48
- const gitlab = new RegExp(`^(?:https://gitlab\\.com/|git@gitlab\\.com:)(${part})/(?:${part}/)*(${part}?)(?:\\.git)?/?$`).exec(url);
49
- const repository = github ? github[1] : gitlab ? `${gitlab[1]}/${gitlab[2]}` : void 0;
50
- if (!repository || repository.split("/").some((segment) => segment === "." || segment === "..")) throw new Error("Origin is not a GitHub or GitLab repository");
51
- return repository;
48
+ const gitlab = new RegExp(`^(?:https://gitlab\\.com/|git@gitlab\\.com:)(${part}/(?:${part}/)*${part}?)(?:\\.git)?/?$`).exec(url);
49
+ const segments = (github?.[1] ?? gitlab?.[1])?.split("/");
50
+ if (!segments || segments.some((segment) => segment === "." || segment === "..")) throw new Error("Origin is not a GitHub or GitLab repository");
51
+ return `${segments[0]}/${segments[segments.length - 1]}`;
52
52
  }
53
53
  /**
54
54
  * Local release provenance, replacing a trusted workflow run: the working tree is a clean
@@ -73,7 +73,8 @@ async function runLocalReleaseCut(options) {
73
73
  git,
74
74
  sourceSha: provenance.sourceSha,
75
75
  releasedVersion: provenance.releasedVersion,
76
- payloads: (tag) => platformPayloads(parsed.requirements(tag), platform)
76
+ payloads: (tag) => platformPayloads(parsed.requirements(tag), platform),
77
+ historyRewrite: parsed.historyRewrite
77
78
  });
78
79
  const result = await cutRelease({
79
80
  kind,
@@ -51,8 +51,21 @@ export declare function parseReleaseConfig(config: unknown): {
51
51
  /** The HTTPS feed releases are published to and discovered from. */
52
52
  feed: ReleaseFeed;
53
53
  commandArtifacts: readonly CommandArtifactPolicy[];
54
+ /** Reviewed source commits that a history rewrite replaced; see `parseHistoryRewrite`. */
55
+ historyRewrite: readonly HistoryRewrite[];
54
56
  requirements(tag: string): Requirements;
55
57
  };
58
+ /** A source commit that a history rewrite (such as a repository move) replaced with `to`,
59
+ * a commit with the same tree on the new main. `tree` is that tree, recorded so the entry can
60
+ * be checked when the old commit is no longer in the checkout. */
61
+ export type HistoryRewrite = {
62
+ from: string;
63
+ to: string;
64
+ tree: string;
65
+ };
66
+ /** Parse the optional `historyRewrite` list: at most 256 entries of exactly `from`, `to` and
67
+ * `tree`, each a full lowercase SHA-1, with `from` distinct from `to` and never listed twice. */
68
+ export declare function parseHistoryRewrite(value: unknown): readonly HistoryRewrite[];
56
69
  /** Every payload and release asset a release's requirements name, desktop payloads first. */
57
70
  export declare function releaseAssetNames(requirements: Requirements): string[];
58
71
  /** The payloads one platform's build stages: its installer and archive, then its command artifacts. */
@@ -114,9 +114,27 @@ function parseReleaseConfig(config) {
114
114
  releaseWorkflows,
115
115
  feed,
116
116
  commandArtifacts,
117
+ historyRewrite: parseHistoryRewrite(config.historyRewrite),
117
118
  requirements
118
119
  };
119
120
  }
121
+ var sha = (value) => typeof value === "string" && /^[a-f0-9]{40}$/.test(value);
122
+ /** Parse the optional `historyRewrite` list: at most 256 entries of exactly `from`, `to` and
123
+ * `tree`, each a full lowercase SHA-1, with `from` distinct from `to` and never listed twice. */
124
+ function parseHistoryRewrite(value) {
125
+ if (value === void 0) return [];
126
+ if (!Array.isArray(value) || value.length > 256) throw new Error("Invalid history rewrite");
127
+ const entries = value.map((entry) => {
128
+ if (!object(entry) || Object.keys(entry).length !== 3 || !sha(entry.from) || !sha(entry.to) || !sha(entry.tree) || entry.from === entry.to) throw new Error("Invalid history rewrite");
129
+ return {
130
+ from: entry.from,
131
+ to: entry.to,
132
+ tree: entry.tree
133
+ };
134
+ });
135
+ if (new Set(entries.map((entry) => entry.from)).size !== entries.length) throw new Error("Invalid history rewrite");
136
+ return entries;
137
+ }
120
138
  /** Every payload and release asset a release's requirements name, desktop payloads first. */
121
139
  function releaseAssetNames(requirements) {
122
140
  return [
@@ -177,4 +195,4 @@ function discoveryPolicies(config, tag) {
177
195
  }
178
196
  return result;
179
197
  }
180
- export { discoveryPolicies, parseReleaseConfig, platformPayloads, platformTargets, releaseAssetNames, workflowFile };
198
+ export { discoveryPolicies, parseHistoryRewrite, parseReleaseConfig, platformPayloads, platformTargets, releaseAssetNames, workflowFile };
@@ -17,7 +17,8 @@ export type UpdateChannelSource = UpdateChannelStore | UpdateChannelService;
17
17
  export declare function updateChannelStore(source: UpdateChannelSource): UpdateChannelStore;
18
18
  /**
19
19
  * Report every snapshot the updater answers with, for a pending-update badge that
20
- * must not run a second checker. The wrapped adapter otherwise behaves identically.
20
+ * must not run a second checker. Observer failures are ignored; updater failures still propagate.
21
+ * The wrapped adapter otherwise behaves identically.
21
22
  */
22
23
  export declare function mirrorUpdateState(adapter: UpdateAdapter, onState: (state: UpdateState) => void): UpdateAdapter;
23
24
  /** A native updater snapshot in snake_case, as Rust and older desktop hosts report it. */
@@ -48,7 +49,8 @@ export declare function updateStateFromNative(update: NativeUpdateSnapshot, opti
48
49
  }): UpdateState;
49
50
  /**
50
51
  * Bridge a snake_case native updater to the shared adapter. Release notes are dropped
51
- * unless requested, which keeps the dialog's per-release notes section hidden.
52
+ * unless requested, which keeps the dialog's per-release notes section hidden. An accessor is
53
+ * read when an operation or polling interval is requested, so it can provide a replacement.
52
54
  */
53
55
  export declare function nativeUpdateAdapter(native: NativeUpdateAdapter | (() => NativeUpdateAdapter), options?: {
54
56
  releaseNotes?: boolean;
@@ -1,3 +1,4 @@
1
+ import { toUpdateState } from "./update-state.js";
1
2
  // src/updates/update-adapter.ts
2
3
  /** Normalize either channel shape to `read`/`persist`. */
3
4
  function updateChannelStore(source) {
@@ -7,19 +8,35 @@ function updateChannelStore(source) {
7
8
  persist: (channel) => source.setChannel(channel)
8
9
  };
9
10
  }
11
+ var mirroredAdapterKeys = /* @__PURE__ */ new Set([
12
+ "check",
13
+ "getStatus",
14
+ "install",
15
+ "isDesktopRuntime",
16
+ "pollIntervalMs",
17
+ "cancel",
18
+ "openExternal"
19
+ ]);
10
20
  /**
11
21
  * Report every snapshot the updater answers with, for a pending-update badge that
12
- * must not run a second checker. The wrapped adapter otherwise behaves identically.
22
+ * must not run a second checker. Observer failures are ignored; updater failures still propagate.
23
+ * The wrapped adapter otherwise behaves identically.
13
24
  */
14
25
  function mirrorUpdateState(adapter, onState) {
15
26
  const note = async (pending) => {
16
27
  const state = await pending;
17
- onState(state);
28
+ try {
29
+ Promise.resolve(onState(state)).catch(() => {});
30
+ } catch {}
18
31
  return state;
19
32
  };
20
- const cancel = adapter.cancel;
33
+ const forwarded = Object.create(null);
34
+ for (const key of Reflect.ownKeys(adapter)) {
35
+ if (mirroredAdapterKeys.has(key)) continue;
36
+ if (Object.prototype.propertyIsEnumerable.call(adapter, key)) forwarded[key] = Reflect.get(adapter, key);
37
+ }
21
38
  return {
22
- ...adapter,
39
+ ...forwarded,
23
40
  check: () => note(adapter.check()),
24
41
  getStatus: () => note(adapter.getStatus()),
25
42
  install: (expectedVersion) => note(adapter.install(expectedVersion)),
@@ -27,29 +44,24 @@ function mirrorUpdateState(adapter, onState) {
27
44
  get pollIntervalMs() {
28
45
  return adapter.pollIntervalMs;
29
46
  },
30
- ...adapter.openExternal ? { openExternal: (url) => adapter.openExternal(url) } : {},
31
- ...cancel ? { cancel: () => note(cancel.call(adapter)) } : {}
47
+ get cancel() {
48
+ const cancel = adapter.cancel;
49
+ return cancel ? () => note(cancel.call(adapter)) : void 0;
50
+ },
51
+ get openExternal() {
52
+ const openExternal = adapter.openExternal;
53
+ return openExternal ? (url) => openExternal.call(adapter, url) : void 0;
54
+ }
32
55
  };
33
56
  }
34
57
  /** Map a snake_case native snapshot to the shared update state. */
35
58
  function updateStateFromNative(update, options = {}) {
36
- return {
37
- availableVersion: update.available_version,
38
- changelog: update.changelog,
39
- currentVersion: update.current_version,
40
- downloadedBytes: update.downloaded_bytes,
41
- error: update.error,
42
- phase: update.phase,
43
- releaseDate: update.release_date,
44
- releaseNotes: options.releaseNotes ? update.release_notes ?? null : null,
45
- releaseUrl: update.github_url,
46
- restartRequired: update.restart_required,
47
- totalBytes: update.total_bytes
48
- };
59
+ return toUpdateState(update, { includeReleaseNotes: Boolean(options.releaseNotes) });
49
60
  }
50
61
  /**
51
62
  * Bridge a snake_case native updater to the shared adapter. Release notes are dropped
52
- * unless requested, which keeps the dialog's per-release notes section hidden.
63
+ * unless requested, which keeps the dialog's per-release notes section hidden. An accessor is
64
+ * read when an operation or polling interval is requested, so it can provide a replacement.
53
65
  */
54
66
  function nativeUpdateAdapter(native, options = {}) {
55
67
  const current = typeof native === "function" ? native : () => native;
@@ -58,7 +70,9 @@ function nativeUpdateAdapter(native, options = {}) {
58
70
  getStatus: async () => updateStateFromNative(await current().getStatus(), options),
59
71
  install: async (expectedVersion) => updateStateFromNative(await current().install(expectedVersion), options),
60
72
  isDesktopRuntime: () => current().isDesktopRuntime(),
61
- pollIntervalMs: current().pollIntervalMs
73
+ get pollIntervalMs() {
74
+ return current().pollIntervalMs;
75
+ }
62
76
  };
63
77
  }
64
78
  /** The adapter for a build with no installer: the dialog explains updates are desktop-only. */
@@ -13,7 +13,10 @@ export type NativeUpdateState<Phase extends string = string> = Readonly<{
13
13
  restart_required: boolean;
14
14
  }>;
15
15
  /** Project a native snapshot into the shared UI update dialog contract. */
16
- export declare function toUpdateState<Phase extends string>(update: NativeUpdateState<Phase>, options?: {
16
+ export declare function toUpdateState<Phase extends string, Url extends string | null = string>(update: Omit<NativeUpdateState<Phase>, 'github_url' | 'release_notes'> & Readonly<{
17
+ github_url: Url;
18
+ release_notes?: string | null;
19
+ }>, options?: {
17
20
  includeReleaseNotes?: boolean;
18
21
  }): {
19
22
  phase: Phase;
@@ -25,6 +28,6 @@ export declare function toUpdateState<Phase extends string>(update: NativeUpdate
25
28
  downloadedBytes: number;
26
29
  totalBytes: number | null;
27
30
  error: string | null;
28
- releaseUrl: string;
31
+ releaseUrl: Url;
29
32
  restartRequired: boolean;
30
33
  };
@@ -6,7 +6,7 @@ function toUpdateState(update, options = {}) {
6
6
  currentVersion: update.current_version,
7
7
  availableVersion: update.available_version,
8
8
  changelog: update.changelog,
9
- releaseNotes: options.includeReleaseNotes === false ? null : update.release_notes,
9
+ releaseNotes: options.includeReleaseNotes === false ? null : update.release_notes ?? null,
10
10
  releaseDate: update.release_date,
11
11
  downloadedBytes: update.downloaded_bytes,
12
12
  totalBytes: update.total_bytes,
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@adea-ai/app-framework",
3
- "version": "0.28.2",
3
+ "version": "0.28.3",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/adea-ai/app-framework.git"
7
+ "url": "git+https://gitlab.com/adea-ai/ade/app-framework.git"
8
8
  },
9
9
  "bin": {
10
10
  "adea-cef-helper": "./dist/cli/cef-helper-cli.js",
@@ -127,6 +127,11 @@ export function createAppearanceContext(ui: UiThemeContextValue): AppearanceCont
127
127
 
128
128
  return {
129
129
  ...ui,
130
+ // `themes` is a live getter on the shared provider. Spreading the context
131
+ // would otherwise capture only the initial catalogue snapshot.
132
+ get themes() {
133
+ return ui.themes
134
+ },
130
135
  update: (patch) => ui.setSelection(selectionFromPreferences(patch)),
131
136
  preview,
132
137
  }
@@ -122,6 +122,83 @@ export function createClientAssetVerificationCache(maxEntries = 256): ClientAsse
122
122
  type AssetFile = Pick<FileHandle, 'close' | 'stat' | 'read'>
123
123
  type OpenAsset = (path: string, flags: 'r') => Promise<AssetFile>
124
124
 
125
+ /** One admission to hold an asset file open. */
126
+ type ClientAssetFileSlot = {
127
+ /** Resolves true once admitted, or false if withdrawn first. */
128
+ readonly ready: Promise<boolean>
129
+ /** Leave the queue if still waiting; does nothing once admitted. */
130
+ withdraw(): void
131
+ /** Free an admitted slot, or withdraw a waiting one. Idempotent. */
132
+ release(): void
133
+ }
134
+
135
+ /** Admission limit on streamed asset files a host holds open at once. */
136
+ export type ClientAssetFileLimit = {
137
+ acquire(): ClientAssetFileSlot
138
+ readonly active: number
139
+ readonly waiting: number
140
+ }
141
+
142
+ /**
143
+ * Bound open streamed asset files. Readers beyond the limit wait, first in first out,
144
+ * without holding a descriptor; a waiting reader that is cancelled leaves the queue.
145
+ */
146
+ export function createClientAssetFileLimit(maxOpenFiles = 32): ClientAssetFileLimit {
147
+ if (!Number.isInteger(maxOpenFiles) || maxOpenFiles < 1 || maxOpenFiles > 1024)
148
+ throw new Error('Invalid client asset file limit')
149
+ let active = 0
150
+ const queue: { admit(): void; withdrawn: boolean }[] = []
151
+ const next = () => {
152
+ while (active < maxOpenFiles && queue.length) {
153
+ const waiter = queue.shift()!
154
+ if (waiter.withdrawn) continue
155
+ active++
156
+ waiter.admit()
157
+ }
158
+ }
159
+ return {
160
+ get active() {
161
+ return active
162
+ },
163
+ get waiting() {
164
+ return queue.filter((waiter) => !waiter.withdrawn).length
165
+ },
166
+ acquire() {
167
+ let state: 'waiting' | 'admitted' | 'done' = 'waiting'
168
+ let settle!: (admitted: boolean) => void
169
+ const ready = new Promise<boolean>((resolve) => (settle = resolve))
170
+ const waiter = {
171
+ withdrawn: false,
172
+ admit() {
173
+ state = 'admitted'
174
+ settle(true)
175
+ },
176
+ }
177
+ const withdraw = () => {
178
+ if (state !== 'waiting') return
179
+ state = 'done'
180
+ waiter.withdrawn = true
181
+ const index = queue.indexOf(waiter)
182
+ if (index !== -1) queue.splice(index, 1)
183
+ settle(false)
184
+ }
185
+ queue.push(waiter)
186
+ next()
187
+ return {
188
+ ready,
189
+ withdraw,
190
+ release() {
191
+ if (state === 'waiting') return withdraw()
192
+ if (state !== 'admitted') return
193
+ state = 'done'
194
+ active--
195
+ next()
196
+ },
197
+ }
198
+ },
199
+ }
200
+ }
201
+
125
202
  /**
126
203
  * Stream an immutable bundled asset. This is not a snapshot: detected file changes
127
204
  * fail the stream, and a read error can surface after response headers have been sent.
@@ -129,9 +206,17 @@ type OpenAsset = (path: string, flags: 'r') => Promise<AssetFile>
129
206
  export function createClientAssetStream(
130
207
  path: string,
131
208
  expected: Stats,
132
- openAsset: OpenAsset = open
209
+ openAsset: OpenAsset = open,
210
+ limit?: ClientAssetFileLimit
133
211
  ): ReadableStream<Uint8Array> {
134
212
  let file: AssetFile | undefined
213
+ // The admission held while this stream owns an open descriptor (or waits for one).
214
+ let slot: ClientAssetFileSlot | undefined
215
+ const releaseSlot = () => {
216
+ const current = slot
217
+ slot = undefined
218
+ current?.release()
219
+ }
135
220
  let opening: Promise<void> | undefined
136
221
  let closing: Promise<void> | undefined
137
222
  let pulling: Promise<void> | undefined
@@ -143,12 +228,19 @@ export function createClientAssetStream(
143
228
  if (closing) return closing
144
229
  const current = file
145
230
  file = undefined
146
- if (!current) return Promise.resolve()
147
- const pending = current.close().catch((error: unknown) => {
148
- closeFailed = true
149
- closeFailure = error
150
- throw error
151
- })
231
+ if (!current) {
232
+ releaseSlot()
233
+ return Promise.resolve()
234
+ }
235
+ const pending = current
236
+ .close()
237
+ .catch((error: unknown) => {
238
+ closeFailed = true
239
+ closeFailure = error
240
+ throw error
241
+ })
242
+ // The descriptor is the stream's to close; once closing settles, its slot is free.
243
+ .finally(releaseSlot)
152
244
  closing = pending
153
245
  const clear = () => {
154
246
  if (closing === pending) closing = undefined
@@ -160,7 +252,21 @@ export function createClientAssetStream(
160
252
  if (cancelled || file) return Promise.resolve()
161
253
  if (opening) return opening
162
254
  const task = (async () => {
163
- const opened = await openAsset(path, 'r')
255
+ if (limit && !slot) {
256
+ const admission = limit.acquire()
257
+ slot = admission
258
+ if (!(await admission.ready) || cancelled) {
259
+ releaseSlot()
260
+ return
261
+ }
262
+ }
263
+ let opened: AssetFile
264
+ try {
265
+ opened = await openAsset(path, 'r')
266
+ } catch (error) {
267
+ releaseSlot()
268
+ throw error
269
+ }
164
270
  if (cancelled) {
165
271
  try {
166
272
  await opened.close()
@@ -168,6 +274,8 @@ export function createClientAssetStream(
168
274
  closeFailed = true
169
275
  closeFailure = error
170
276
  throw error
277
+ } finally {
278
+ releaseSlot()
171
279
  }
172
280
  return
173
281
  }
@@ -233,6 +341,8 @@ export function createClientAssetStream(
233
341
  },
234
342
  async cancel() {
235
343
  cancelled = true
344
+ // A reader still queued for admission holds no descriptor; leave the queue now.
345
+ slot?.withdraw()
236
346
  const pending = opening
237
347
  if (pending) await pending.catch(() => {})
238
348
  // A read or final stat can still be using the file after it opened.
@@ -4,13 +4,16 @@ import { open, readFile, realpath, stat } from 'node:fs/promises'
4
4
  import { extname, join, posix, relative, resolve, sep } from 'node:path'
5
5
  import {
6
6
  createClientAssetStream,
7
+ type ClientAssetFileLimit,
7
8
  type ClientAssetVerificationCache,
8
9
  verifyClientAssetCached,
9
10
  verifyClientAssetDigest,
10
11
  } from './client-asset-stream'
11
12
  import { parseClientAssetManifest, snapshotClientAssetAllowlist } from './client-asset-manifest'
12
13
  export {
14
+ createClientAssetFileLimit,
13
15
  createClientAssetVerificationCache,
16
+ type ClientAssetFileLimit,
14
17
  type ClientAssetVerificationCache,
15
18
  } from './client-asset-stream'
16
19
  export {
@@ -54,6 +57,8 @@ export type ClientOptions = {
54
57
  immutableAssets?: ReadonlyMap<string, string>
55
58
  /** Bounded, host-lifetime cache of successful checks for unchanged immutable files. */
56
59
  verificationCache?: ClientAssetVerificationCache
60
+ /** Admission limit shared by streamed GET bodies; readers past it wait without a descriptor. */
61
+ openFileLimit?: ClientAssetFileLimit
57
62
  }
58
63
 
59
64
  const unavailable = (status = 404) =>
@@ -287,13 +292,16 @@ async function serveClientInternal(
287
292
  const cacheVerified =
288
293
  immutableSha256 !== undefined &&
289
294
  (await matchesManifestDigest(physical, metadata, immutableSha256, options.verificationCache))
290
- return new Response(createClientAssetStream(physical, metadata), {
291
- headers: {
292
- 'content-type': contentType,
293
- 'cache-control': cacheVerified ? IMMUTABLE_CACHE_CONTROL : 'no-store',
294
- 'content-length': String(metadata.size),
295
- },
296
- })
295
+ return new Response(
296
+ createClientAssetStream(physical, metadata, undefined, options.openFileLimit),
297
+ {
298
+ headers: {
299
+ 'content-type': contentType,
300
+ 'cache-control': cacheVerified ? IMMUTABLE_CACHE_CONTROL : 'no-store',
301
+ 'content-length': String(metadata.size),
302
+ },
303
+ }
304
+ )
297
305
  }
298
306
 
299
307
  let body = new Uint8Array(await Bun.file(physical).arrayBuffer())
@@ -3,6 +3,7 @@ import type {
3
3
  UpdateChannel,
4
4
  UpdateState,
5
5
  } from '@adea-ai/ui/components/composites/update-dialog'
6
+ import { toUpdateState } from './update-state'
6
7
 
7
8
  export type { UpdateAdapter, UpdateChannel, UpdateState }
8
9
 
@@ -31,9 +32,20 @@ export function updateChannelStore(source: UpdateChannelSource): UpdateChannelSt
31
32
  }
32
33
  }
33
34
 
35
+ const mirroredAdapterKeys = new Set<PropertyKey>([
36
+ 'check',
37
+ 'getStatus',
38
+ 'install',
39
+ 'isDesktopRuntime',
40
+ 'pollIntervalMs',
41
+ 'cancel',
42
+ 'openExternal',
43
+ ])
44
+
34
45
  /**
35
46
  * Report every snapshot the updater answers with, for a pending-update badge that
36
- * must not run a second checker. The wrapped adapter otherwise behaves identically.
47
+ * must not run a second checker. Observer failures are ignored; updater failures still propagate.
48
+ * The wrapped adapter otherwise behaves identically.
37
49
  */
38
50
  export function mirrorUpdateState(
39
51
  adapter: UpdateAdapter,
@@ -41,12 +53,24 @@ export function mirrorUpdateState(
41
53
  ): UpdateAdapter {
42
54
  const note = async (pending: Promise<UpdateState>) => {
43
55
  const state = await pending
44
- onState(state)
56
+ try {
57
+ // An observer updates a badge; it must not change the updater's result or leave a rejected
58
+ // promise unhandled when an application supplies an async observer.
59
+ void Promise.resolve(onState(state)).catch(() => {})
60
+ } catch {
61
+ // Synchronous observer failures are isolated for the same reason.
62
+ }
45
63
  return state
46
64
  }
47
- const cancel = adapter.cancel
65
+ // Copy enumerable extension properties without evaluating overridden adapter getters early.
66
+ const forwarded: Record<PropertyKey, unknown> = Object.create(null)
67
+ for (const key of Reflect.ownKeys(adapter)) {
68
+ if (mirroredAdapterKeys.has(key)) continue
69
+ if (Object.prototype.propertyIsEnumerable.call(adapter, key))
70
+ forwarded[key] = Reflect.get(adapter, key)
71
+ }
48
72
  return {
49
- ...adapter,
73
+ ...forwarded,
50
74
  check: () => note(adapter.check()),
51
75
  getStatus: () => note(adapter.getStatus()),
52
76
  install: (expectedVersion) => note(adapter.install(expectedVersion)),
@@ -54,8 +78,14 @@ export function mirrorUpdateState(
54
78
  get pollIntervalMs() {
55
79
  return adapter.pollIntervalMs
56
80
  },
57
- ...(adapter.openExternal ? { openExternal: (url: string) => adapter.openExternal!(url) } : {}),
58
- ...(cancel ? { cancel: () => note(cancel.call(adapter)) } : {}),
81
+ get cancel() {
82
+ const cancel = adapter.cancel
83
+ return cancel ? () => note(cancel.call(adapter)) : undefined
84
+ },
85
+ get openExternal() {
86
+ const openExternal = adapter.openExternal
87
+ return openExternal ? (url: string) => openExternal.call(adapter, url) : undefined
88
+ },
59
89
  }
60
90
  }
61
91
 
@@ -88,24 +118,13 @@ export function updateStateFromNative(
88
118
  update: NativeUpdateSnapshot,
89
119
  options: { releaseNotes?: boolean } = {}
90
120
  ): UpdateState {
91
- return {
92
- availableVersion: update.available_version,
93
- changelog: update.changelog,
94
- currentVersion: update.current_version,
95
- downloadedBytes: update.downloaded_bytes,
96
- error: update.error,
97
- phase: update.phase,
98
- releaseDate: update.release_date,
99
- releaseNotes: options.releaseNotes ? (update.release_notes ?? null) : null,
100
- releaseUrl: update.github_url,
101
- restartRequired: update.restart_required,
102
- totalBytes: update.total_bytes,
103
- }
121
+ return toUpdateState(update, { includeReleaseNotes: Boolean(options.releaseNotes) })
104
122
  }
105
123
 
106
124
  /**
107
125
  * Bridge a snake_case native updater to the shared adapter. Release notes are dropped
108
- * unless requested, which keeps the dialog's per-release notes section hidden.
126
+ * unless requested, which keeps the dialog's per-release notes section hidden. An accessor is
127
+ * read when an operation or polling interval is requested, so it can provide a replacement.
109
128
  */
110
129
  export function nativeUpdateAdapter(
111
130
  native: NativeUpdateAdapter | (() => NativeUpdateAdapter),
@@ -118,7 +137,9 @@ export function nativeUpdateAdapter(
118
137
  install: async (expectedVersion) =>
119
138
  updateStateFromNative(await current().install(expectedVersion), options),
120
139
  isDesktopRuntime: () => current().isDesktopRuntime(),
121
- pollIntervalMs: current().pollIntervalMs,
140
+ get pollIntervalMs() {
141
+ return current().pollIntervalMs
142
+ },
122
143
  }
123
144
  }
124
145
 
@@ -14,8 +14,9 @@ export type NativeUpdateState<Phase extends string = string> = Readonly<{
14
14
  }>
15
15
 
16
16
  /** Project a native snapshot into the shared UI update dialog contract. */
17
- export function toUpdateState<Phase extends string>(
18
- update: NativeUpdateState<Phase>,
17
+ export function toUpdateState<Phase extends string, Url extends string | null = string>(
18
+ update: Omit<NativeUpdateState<Phase>, 'github_url' | 'release_notes'> &
19
+ Readonly<{ github_url: Url; release_notes?: string | null }>,
19
20
  options: { includeReleaseNotes?: boolean } = {}
20
21
  ) {
21
22
  return {
@@ -23,7 +24,7 @@ export function toUpdateState<Phase extends string>(
23
24
  currentVersion: update.current_version,
24
25
  availableVersion: update.available_version,
25
26
  changelog: update.changelog,
26
- releaseNotes: options.includeReleaseNotes === false ? null : update.release_notes,
27
+ releaseNotes: options.includeReleaseNotes === false ? null : (update.release_notes ?? null),
27
28
  releaseDate: update.release_date,
28
29
  downloadedBytes: update.downloaded_bytes,
29
30
  totalBytes: update.total_bytes,