@adea-ai/app-framework 0.28.1 → 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.1","commit":"9ced256448ba7f6b0d77d359dc5a68157dc34dd7"}
1
+ {"version":"0.28.3","commit":"4c25930a1bae82800d320983b3b3943897361e1d"}
@@ -23,6 +23,12 @@ export declare function readCommandArtifact(options: {
23
23
  entries: number;
24
24
  bytes: number;
25
25
  }>;
26
+ /**
27
+ * Whether a failed signal to a process group means no live process remains in it. macOS answers
28
+ * EPERM, not ESRCH, while the group holds only unreaped zombies. The qualification service runs
29
+ * as this user, so EPERM there does not mean a live process this user may not signal.
30
+ */
31
+ export declare function processGroupIsGone(code: string | undefined, platform?: NodeJS.Platform): boolean;
26
32
  /** The bounded result of qualifying one extracted command artifact. */
27
33
  export type CommandQualification = {
28
34
  installed: true;
@@ -133,12 +133,20 @@ async function exitedWithin(child, milliseconds) {
133
133
  clearTimeout(timer);
134
134
  }
135
135
  }
136
+ /**
137
+ * Whether a failed signal to a process group means no live process remains in it. macOS answers
138
+ * EPERM, not ESRCH, while the group holds only unreaped zombies. The qualification service runs
139
+ * as this user, so EPERM there does not mean a live process this user may not signal.
140
+ */
141
+ function processGroupIsGone(code, platform = process.platform) {
142
+ return code === "ESRCH" || platform === "darwin" && code === "EPERM";
143
+ }
136
144
  /** Signal remaining processes in the detached Unix qualification service's group. */
137
145
  function signalProcessGroup(pid, signal) {
138
146
  try {
139
147
  process.kill(-pid, signal);
140
148
  } catch (error) {
141
- if (error.code !== "ESRCH") throw error;
149
+ if (!processGroupIsGone(error.code)) throw error;
142
150
  }
143
151
  }
144
152
  async function serviceExitStatus(path) {
@@ -168,7 +176,7 @@ async function processGroupExited(pid, milliseconds) {
168
176
  try {
169
177
  process.kill(-pid, 0);
170
178
  } catch (error) {
171
- if (error.code === "ESRCH") return true;
179
+ if (processGroupIsGone(error.code)) return true;
172
180
  throw error;
173
181
  }
174
182
  if (process.platform === "linux" && await linuxProcessGroupIsExited(pid)) return true;
@@ -358,4 +366,4 @@ async function qualifyCommandArtifact(options) {
358
366
  if (!qualification) throw new Error("Command artifact qualification did not complete");
359
367
  return qualification;
360
368
  }
361
- export { commandArtifactRoot, packCommandArtifact, qualifyCommandArtifact, readCommandArtifact };
369
+ export { commandArtifactRoot, packCommandArtifact, processGroupIsGone, qualifyCommandArtifact, readCommandArtifact };
@@ -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");
@@ -6,7 +6,11 @@ export declare function gitCli(): GitRun;
6
6
  export declare function gitIsAncestor(git: GitRun, older: string, newer: string): Promise<boolean>;
7
7
  /** The released version in the source manifest at the exact source commit. */
8
8
  export declare function readSourceVersion(git: GitRun, sourceSha: string, path: string): Promise<string>;
9
- /** The `owner/name` of the checkout's GitHub origin, as descriptors record it. */
9
+ /**
10
+ * The checkout's origin as `owner/name`, as descriptors record it. A GitLab project keeps that
11
+ * two-part form whatever groups it sits in (`adea-ai/ade/adea` is `adea-ai/adea`), so moving forge
12
+ * does not change the identity recorded in new descriptors or in installed apps' feeds.
13
+ */
10
14
  export declare function originRepository(git: GitRun): Promise<string>;
11
15
  /**
12
16
  * Local release provenance, replacing a trusted workflow run: the working tree is a clean
@@ -32,16 +32,23 @@ async function readSourceVersion(git, sourceSha, path) {
32
32
  if (typeof manifest !== "object" || manifest === null || Array.isArray(manifest) || typeof manifest.version !== "string") throw new Error("Invalid source version manifest");
33
33
  return manifest.version;
34
34
  }
35
- /** The `owner/name` of the checkout's GitHub origin, as descriptors record it. */
35
+ /**
36
+ * The checkout's origin as `owner/name`, as descriptors record it. A GitLab project keeps that
37
+ * two-part form whatever groups it sits in (`adea-ai/ade/adea` is `adea-ai/adea`), so moving forge
38
+ * does not change the identity recorded in new descriptors or in installed apps' feeds.
39
+ */
36
40
  async function originRepository(git) {
37
41
  const url = await git([
38
42
  "remote",
39
43
  "get-url",
40
44
  "origin"
41
45
  ]);
42
- const match = /^(?:https:\/\/github\.com\/|git@github\.com:)([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?)(?:\.git)?\/?$/.exec(url);
43
- if (!match || match[1].split("/").some((part) => part === "." || part === "..")) throw new Error("Origin is not a GitHub repository");
44
- return match[1];
46
+ const part = "[A-Za-z0-9_.-]+";
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 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]}`;
45
52
  }
46
53
  /**
47
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 };